, ? . ... Lua, , . , ... ( )
lb_newmatrix ( return) lua_pushinteger( L, nbyte ); return 1; return 2;. ( , .) ,
static int lb_peek( lua_State *L ) {
int nbytes = luaL_checkinteger( L, 2 );
char *data = (char*)luaL_checkudata(L,1,"basic");
lua_pushlstring( L, data, nbytes );
return 1;
}
LuaBasic_m {"peek",lb_peek}. , Lua .
> m, size = lb.NewMatrix( 3, 3 ) -- make a small matrix
> data = m:peek( size ) -- get the memory as a string
> (("n "):rep(3*3).."I I"):unpack( data ) -- and unpack the values
0.0 6.366e-314 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0 0 81
-- (note: the trailing '81' is the offset of the next character in
-- the string beyond what was read and not part of your data)
-- so your matrix looks like:
-- 0.0 6.366e-314 0.0
-- 0.0 0.0 0.0
-- 0.0 0.0 0.0
-- and has 0 rows and 0 columns (according to the data…)
... , 6.366e-314 . , ...
> size/4
20
> (("I4"):rep(20)):unpack( data )
0 0 3 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 81
A-ha... ! (. 3, 3 ?)
, 1x1 LUA_NUMBER s... . , , , matrix *m, m->row *(m+sizeof(LUA_NUMBER[1][1])), , ...
, struct: !
typedef struct {
int row;
int col;
LUA_NUMBER data[1][1];
} matrix;
, Lua :
> m, size = lb.NewMatrix( 3, 3 )
> data = m:peek( size )
> ("I I"..("n "):rep(3*3)):unpack( data )
3 3 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 81
3x3. . ,
static int lb_set( lua_State *L ) {
matrix *m = (matrix*)luaL_checkudata(L,1,"basic");
int i = luaL_checkinteger( L, 2 );
int j = luaL_checkinteger( L, 3 );
lua_Number v = luaL_checknumber( L, 4 );
m->data[i][j] = v;
return 0;
}
{"set",lb_set} LuaBasic_m. , :
> m, size = lb.NewMatrix( 3, 3 )
> for i = 0, 2 do for j = 0, 2 do
>> m:set( i, j, (i+1) + (j+1)/10 )
>> print( ("I I"..("n "):rep(3*3)):unpack( m:peek( size ) ) )
>> end end
3 3 1.1 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 81
3 3 1.1 1.2 0.0 0.0 0.0 0.0 0.0 0.0 0.0 81
3 3 1.1 1.2 1.3 0.0 0.0 0.0 0.0 0.0 0.0 81
3 3 1.1 2.1 1.3 0.0 0.0 0.0 0.0 0.0 0.0 81
3 3 1.1 2.1 2.2 0.0 0.0 0.0 0.0 0.0 0.0 81
3 3 1.1 2.1 2.2 2.3 0.0 0.0 0.0 0.0 0.0 81
3 3 1.1 2.1 3.1 2.3 0.0 0.0 0.0 0.0 0.0 81
3 3 1.1 2.1 3.1 3.2 0.0 0.0 0.0 0.0 0.0 81
3 3 1.1 2.1 3.1 3.2 3.3 0.0 0.0 0.0 0.0 81
... . - ?:-) data+(( 1 *i)+j)*sizeof(lua_Number), data+(( 3 *i)+j)*sizeof(lua_Number) ( ).
, , 1x1. ( , , i[m->data][j] m->data[i][j], .) , . , struct
typedef struct {
int row;
int col;
LUA_NUMBER data[0];
} matrix;
([0] - . , data[1], struct. data[0] , struct - ( ), .)
m->data[i][j] m->data[m->col*i + j]. ( , , .)
:
> m, size = lb.NewMatrix( 3, 3 )
> for i = 0, 2 do for j = 0, 2 do
>> m:set( i, j, (i+1) + (j+1)/10 )
>> print( ("I I"..("n "):rep(3*3)):unpack( m:peek( size ) ) )
>> end end
3 3 1.1 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 81
3 3 1.1 1.2 0.0 0.0 0.0 0.0 0.0 0.0 0.0 81
3 3 1.1 1.2 1.3 0.0 0.0 0.0 0.0 0.0 0.0 81
3 3 1.1 1.2 1.3 2.1 0.0 0.0 0.0 0.0 0.0 81
3 3 1.1 1.2 1.3 2.1 2.2 0.0 0.0 0.0 0.0 81
3 3 1.1 1.2 1.3 2.1 2.2 2.3 0.0 0.0 0.0 81
3 3 1.1 1.2 1.3 2.1 2.2 2.3 3.1 0.0 0.0 81
3 3 1.1 1.2 1.3 2.1 2.2 2.3 3.1 3.2 0.0 81
3 3 1.1 1.2 1.3 2.1 2.2 2.3 3.1 3.2 3.3 81
, .
, [1], [2], [3] [0], [1], [2]. C . [0] not [1], [size-1] [size]. (n + 1) x (m + 1) 1... n 1... m, ( 3x3, , ). , , C.
Lua, . (data[3][3] 3x3, "" - data[2][2] data[m-1][n-1].) , Lua/index (1-based, 1 n) C/offset (0-based, offset 0 (n-1)). ( , / 0, , , , .)