[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: newbie again: exposing my ignorance...
- From: "Chris" <chris@...>
- Date: Sun, 22 Oct 2000 20:13:04 +0200
Thanks a lot you for your help, but I'll implement my
matrices as userdata and let my C functions do
all the maths. This seems to be the best and fastest method.
Is the following code a *good* way to do this?
Sorry for the notation, but I'm an old *Hungarian*.
Chris
//tagMatrix is global
void InitMatrix(lua_State *L)
{
lua_register(L, "MATRIX", CreateMatrix);
lua_register(L, "MyMatrixFunc", MyMatrixFunc);
tagMatrix = lua_newtag(L);
lua_pushcfunction(L, GetIndex);
lua_settagmethod(L, tagMatrix, "gettable");
lua_pushcfunction(L, SetIndex);
lua_settagmethod(L, tagMatrix, "settable");
lua_pushcfunction(L, FreeMatrix);
lua_settagmethod(L, tagMatrix, "gc");
}
//lua: mat = MATRIX(cols, rows)
int CreateMatrix(lua_State *L) //registered function
{
PMATRIX pMat;
int iCols, iRows;
iCols = lua_tonumber(L, 1);
iRows = lua_isnull(L, 2) ? 1 : lua_tonumber(L, 2);
if((pMat = MatAlloc(iRows, iCols)) == NULL)
lua_error(L, "Out of Memory!");
lua_pushusertag(L, (void*) pMat, tagMatrix);
return 0;
}
//lua: mat[col][row] => my main problem
int GetIndex(lua_State *L) //tag method
{
PMATRIX pMat;
int iCol, iRow;
pMat = (PMATRIX) lua_touserdata(L, 1);
iCol = lua_tonumber(L, 2) - 1;
/************************************************************
* ???? iRow = lua_isnull(L, 3) ? 1 : lua_tonumber(L, 3) - 1;
* Do I get the row index here? If not - how do I get it?
************************************************************/
if((iCol < 0) || (iCol > pMat->nCols) ||
(iRow < 0) || (iRow > pMat->nRows))
lua_error(L, "Index out of Range!")
lua_pushnumber(L, pMat->pData[iRow][iCol]);
return 0;
}
int MyMatrixFunc(lua_State *L) //registered function
{
PMATRIX pMat;
if(lua_tag(L, 1) != tagMatrix)
lua_error(L, "Invalid Argument!");
pMat = (PMATRIX) lua_touserdata(L, 1);
//do something here
return 0;
}