aboutsummaryrefslogtreecommitdiff
path: root/nerv/matrix/generic/mmatrix.c
diff options
context:
space:
mode:
Diffstat (limited to 'nerv/matrix/generic/mmatrix.c')
-rw-r--r--nerv/matrix/generic/mmatrix.c80
1 files changed, 20 insertions, 60 deletions
diff --git a/nerv/matrix/generic/mmatrix.c b/nerv/matrix/generic/mmatrix.c
index 697c9fc..233102a 100644
--- a/nerv/matrix/generic/mmatrix.c
+++ b/nerv/matrix/generic/mmatrix.c
@@ -1,9 +1,6 @@
#ifdef NERV_GENERIC_MMATRIX
-#include "matrix.h"
+#include "../../lib/matrix/generic/matrix.h"
#include "elem_type.h"
-#define MATRIX_DATA_FREE(L, ptr) free(ptr)
-#define MATRIX_DATA_ALLOC(L, dptr, stride, width, height) \
- host_matrix_(alloc)(L, dptr, stride, width, height)
#define MATRIX_DATA_WRITE(L, data, idx, val) (data[idx] = val)
#define MATRIX_DATA_READ(L, data, idx) (data[idx])
#define MATRIX_INIT(L) host_matrix_(init)(L)
@@ -11,17 +8,10 @@
#define NERV_GENERIC_MATRIX
#include "../../common.h"
#include "../../io/chunk_file.h"
+#include "../../lib/matrix/generic/mmatrix.h"
#include "string.h"
-static void host_matrix_(alloc)(lua_State *L,
- MATRIX_ELEM **dptr, size_t *stride,
- long width, long height) {
- if ((*dptr = (MATRIX_ELEM *)malloc(width * height)) == NULL)
- nerv_error(L, "mmatrix insufficient memory");
- *stride = width;
-}
-
-int nerv_matrix_(get_elem)(lua_State *L) {
+int nerv_matrix_(lua_get_elem)(lua_State *L) {
Matrix *self = luaT_checkudata(L, 1, nerv_matrix_(tname));
int idx = luaL_checkinteger(L, 2);
if (idx < 0 || idx >= self->nmax)
@@ -30,7 +20,7 @@ int nerv_matrix_(get_elem)(lua_State *L) {
return 1;
}
-int nerv_matrix_(set_elem)(lua_State *L) {
+int nerv_matrix_(lua_set_elem)(lua_State *L) {
Matrix *self = luaT_checkudata(L, 1, nerv_matrix_(tname));
int idx = luaL_checkinteger(L, 2);
MATRIX_ELEM v = luaL_checknumber(L, 3);
@@ -50,72 +40,42 @@ static void host_matrix_(init)(lua_State *L) {
#include "matrix.c"
-int nerv_matrix_(load)(lua_State *L) {
+int nerv_matrix_(lua_load)(lua_State *L) {
+ Status status;
ChunkData *cdp = luaT_checkudata(L, 1, nerv_chunk_data_tname);
- Matrix *self;
- int i, j;
- long nrow, ncol;
- FILE *fp = cdp->fp;
- if (fscanf(fp, "%ld %ld", &nrow, &ncol) != 2)
- return 0;
- self = nerv_matrix_(new_)(L, nrow, ncol);
- for (i = 0; i < nrow; i++)
- {
- MATRIX_ELEM *row = MATRIX_ROW_PTR(self, i);
- for (j = 0; j < ncol; j++)
- if (fscanf(fp, MATRIX_ELEM_FMT, row + j) != 1)
- {
- free(self);
- return 0;
- }
- }
+ Matrix *self = nerv_matrix_(load)(cdp, &status);
+ NERV_LUA_CHECK_STATUS(L, status);
luaT_pushudata(L, self, nerv_matrix_(tname));
return 1;
}
-int nerv_matrix_(save)(lua_State *L) {
+int nerv_matrix_(lua_save)(lua_State *L) {
+ Status status;
ChunkFile *cfp = luaT_checkudata(L, 2,
nerv_chunk_file_handle_tname);
Matrix *self = luaT_checkudata(L, 1, nerv_matrix_(tname));
- int i, j;
- long nrow = self->nrow, ncol = self->ncol;
- FILE *fp = cfp->fp;
- if (fprintf(fp, "%ld %ld\n", nrow, ncol) < 0)
- return 0;
- for (i = 0; i < nrow; i++)
- {
- MATRIX_ELEM *row = MATRIX_ROW_PTR(self, i);
- for (j = 0; j < ncol; j++)
- if (fprintf(fp, MATRIX_ELEM_WRITE_FMT " ", row[j]) < 0)
- return 0;
- if (fprintf(fp, "\n") < 0)
- return 0;
- }
+ nerv_matrix_(save)(self, cfp, &status);
+ NERV_LUA_CHECK_STATUS(L, status);
return 0;
}
-static int nerv_matrix_(copy_from)(lua_State *L) {
+int nerv_matrix_(lua_copy_from)(lua_State *L) {
+ Status status;
Matrix *a = luaT_checkudata(L, 1, nerv_matrix_(tname));
- Matrix *b = luaT_checkudata(L, 2, nerv_matrix_(tname));
+ const Matrix *b = luaT_checkudata(L, 2, nerv_matrix_(tname));
int nargs = lua_gettop(L);
int b_begin = nargs > 2 ? luaL_checkinteger(L, 3) : 0;
int b_end = nargs > 3 ? luaL_checkinteger(L, 4) : b->nrow;
int a_begin = nargs > 4 ? luaL_checkinteger(L, 5) : 0;
- if (!(0 <= b_begin && b_begin < b_end && b_end <= b->nrow &&
- a_begin + b_end - b_begin <= a->nrow))
- nerv_error(L, "invalid copy interval");
- if (a->ncol != b->ncol)
- nerv_error(L, "matrices should be of the same dimension");
- memmove(MATRIX_ROW_PTR(a, a_begin),
- MATRIX_ROW_PTR(b, b_begin),
- sizeof(MATRIX_ELEM) * b->ncol * (b_end - b_begin));
+ nerv_matrix_(copy_from)(a, b, a_begin, b_begin, b_end, &status);
+ NERV_LUA_CHECK_STATUS(L, status);
return 0;
}
static const luaL_Reg nerv_matrix_(extra_methods)[] = {
- {"load", nerv_matrix_(load)},
- {"save", nerv_matrix_(save)},
- {"copy_from", nerv_matrix_(copy_from)},
+ {"load", nerv_matrix_(lua_load)},
+ {"save", nerv_matrix_(lua_save)},
+ {"copy_from", nerv_matrix_(lua_copy_from)},
{NULL, NULL}
};