aboutsummaryrefslogtreecommitdiff
path: root/matrix/generic/mmatrix.c
diff options
context:
space:
mode:
authorcloudygoose <[email protected]>2015-06-04 12:47:18 +0800
committercloudygoose <[email protected]>2015-06-04 12:47:18 +0800
commit2301cba19914f35a8c34c3d27d98deb43ddaaf1d (patch)
treea2a58889ad90c684a00512037f4f3d3d566c3f60 /matrix/generic/mmatrix.c
parent88a2c29f347df2ef75b9891235bc176676e5dafd (diff)
parentea6f2990f99dd9ded6a0e74d75a3ec84900a2518 (diff)
...
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'matrix/generic/mmatrix.c')
-rw-r--r--matrix/generic/mmatrix.c20
1 files changed, 19 insertions, 1 deletions
diff --git a/matrix/generic/mmatrix.c b/matrix/generic/mmatrix.c
index 3a9ae79..4b722f3 100644
--- a/matrix/generic/mmatrix.c
+++ b/matrix/generic/mmatrix.c
@@ -11,6 +11,7 @@
#define NERV_GENERIC_MATRIX
#include "../../common.h"
#include "../../io/chunk_file.h"
+#include "string.h"
static void host_matrix_(alloc)(lua_State *L,
MATRIX_ELEM **dptr, size_t *stride,
@@ -96,10 +97,27 @@ int nerv_matrix_(save)(lua_State *L) {
return 0;
}
-
+static int nerv_matrix_(copy_from)(lua_State *L) {
+ Matrix *a = luaT_checkudata(L, 1, nerv_matrix_(tname));
+ 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));
+ return 0;
+}
static const luaL_Reg nerv_matrix_(extra_methods)[] = {
{"load", nerv_matrix_(load)},
{"save", nerv_matrix_(save)},
+ {"copy_from", nerv_matrix_(copy_from)},
{NULL, NULL}
};