diff --git a/src/mappings/swig/ruby/example/ruby_example.i b/src/mappings/swig/ruby/example/ruby_example.i index e0392af..f16adf6 100644 --- a/src/mappings/swig/ruby/example/ruby_example.i +++ b/src/mappings/swig/ruby/example/ruby_example.i @@ -1,12 +1,12 @@ %module example %include ../../typemaps/ruby/biolib_matrix.i + +MAP_INOUT_DIM_MATRIXASARRAY(double,arg1,arg2,matrix_as_array) + %include ../../typemaps/ruby/biolib_array.i -BIOLIB_INARRAY(double, data) -BIOLIB_INOUTARRAY(double,num,data,result) -BIOLIB_OUTARRAY(double,result) -BIOLIB_INMATRIXASARRAY(double,arg1,arg2,matrix_as_array) +MAP_INOUT_ARRAY(double,data,result) %include ../../example.i diff --git a/src/mappings/swig/ruby/rqtl/ruby_rqtl.i b/src/mappings/swig/ruby/rqtl/ruby_rqtl.i index dc7c5ca..eac8705 100644 --- a/src/mappings/swig/ruby/rqtl/ruby_rqtl.i +++ b/src/mappings/swig/ruby/rqtl/ruby_rqtl.i @@ -22,9 +22,8 @@ BIOLIB_INMATRIXASARRAY(double,arg8,arg1,Intcov) %include ../../typemaps/ruby/biolib_array.i -/* BIOLIB_INARRAY(double,weights) +BIOLIB_INARRAY(double,weights) BIOLIB_INARRAY(double,pheno) -*/ /* BIOLIB_INOUTARRAY(double,n_ind,pheno,result) */ diff --git a/src/mappings/swig/ruby/test/test_example.rb b/src/mappings/swig/ruby/test/test_example.rb index 4879e62..223ca0d 100644 --- a/src/mappings/swig/ruby/test/test_example.rb +++ b/src/mappings/swig/ruby/test/test_example.rb @@ -7,6 +7,7 @@ values = [1.1,1.2,2.1,2.2,3.1,3.2] result = Biolib::Example.matrix_as_array_change(2,3,values) +p values raise "Test failed with value #{result}" if result[3] != 4 result = Biolib::Example.array_transform_to_result([1,2,3,4]) diff --git a/src/mappings/swig/typemaps/ruby/biolib_array.i b/src/mappings/swig/typemaps/ruby/biolib_array.i index 4128df4..40531d8 100644 --- a/src/mappings/swig/typemaps/ruby/biolib_array.i +++ b/src/mappings/swig/typemaps/ruby/biolib_array.i @@ -1,10 +1,29 @@ -%define BIOLIB_INARRAY(type,name) +%define MAP_IN_DIM_ARRAY(type,sizearg,name) %typemap(in) type* name { - /* %typemap(in) type* name */ + /* MAP_IN_DIM_ARRAY %typemap(in) type* name */ int i, len; if (!rb_obj_is_kind_of($input,rb_cArray)) - rb_raise(rb_eArgError, "BIOLIB_INARRAY expected Array of values for $1_name"); + rb_raise(rb_eArgError, "MAP_IN_ARRAY expected Array of values for $1_name"); + len = sizearg; + $1 = (type *)malloc(len*sizeof(type)); + for (i=0; iptr[i]); + } + + %typemap(freearg) type *name { + /* MAP_IN_DIM_ARRAY %typemap(freearg) type *name */ + if ($1) free($1); + } +%enddef + +%define MAP_IN_ARRAY(type,name) + %typemap(in) type* name { + /* MAP_IN_ARRAY %typemap(in) type* name */ + int i, len; + + if (!rb_obj_is_kind_of($input,rb_cArray)) + rb_raise(rb_eArgError, "MAP_IN_ARRAY expected Array of values for $1_name"); len = RARRAY($input)->len; $1 = (type *)malloc(len*sizeof(type)); for (i=0; ilen; + rb_raise(rb_eArgError, "MAP_INOUT_DIM_ARRAY expected Array of values for $1_name"); + asize = sizearg; $1 = (type *)malloc(asize*sizeof(type)); for (i=0; iptr[i]); + ($1)[i] = rb_num2dbl(RARRAY($input)->ptr[i]); $2 = (type *)malloc(asize*sizeof(type)); } %typemap(freearg) (type* data, type *result) { - /* %typemap(in) (type* data, type *result) */ + /* MAP_INOUT_DIM_ARRAY %typemap(in) (type* data, type *result) */ if ($1) free($1); if ($2) free($2); } %enddef -%define BIOLIB_OUTARRAY(type,sizearg,result) +%define MAP_INOUT_ARRAY(type,data,result) + + %typemap(in) (int num, type* data, type *result) { + /* MAP_INOUT_ARRAY %typemap(in) (int, type* data, type *result) */ + int i, asize; + + if (!rb_obj_is_kind_of($input,rb_cArray)) + rb_raise(rb_eArgError, "MAP_INOUT_ARRAY expected Array of values for $2_name"); + asize = RARRAY($input)->len; + $1 = asize; + $2 = (type *)malloc(asize*sizeof(type)); + for (i=0; iptr[i]); + $3 = (type *)malloc(asize*sizeof(type)); + } + + %typemap(argout) (int num, type *data, type *result) { + /* MAP_OUT_ARRAY %typemap(argout) int type, *data type, *result */ + int i; + + int asize = RARRAY($input)->len; + /* example: printf("%f,%f",$1[0][0],$1[1][0]); */ + $result = rb_ary_new(); + for (i=0; ilen; */ dptr = (type **)malloc(rows*sizeof(type *)); @@ -38,7 +38,7 @@ %typemap(freearg) type **name { - /* BIOLIB_INMATRIXASARRAY %typemap(freearg) type **name */ + /* MAP_IN_DIM_MATRIXASARRAY %typemap(freearg) type **name */ if ($1) { free(*$1); free($1); @@ -46,11 +46,11 @@ } %enddef -%define BIOLIB_INOUTMATRIXASARRAY(type,colsarg,rowsarg,name) - BIOLIB_INMATRIXASARRAY(type,colsarg,rowsarg,name); +%define MAP_INOUT_DIM_MATRIXASARRAY(type,colsarg,rowsarg,name) + MAP_IN_DIM_MATRIXASARRAY(type,colsarg,rowsarg,name); %typemap(argout) type **name { - /* BIOLIB_INOUTMATRIXASARRAY %typemap(argout) type **name */ + /* MAP_INOUT_DIM_MATRIXASARRAY %typemap(argout) type **name */ int i,j; int cols = colsarg; int rows = rowsarg;