Skip to content

Commit

Permalink
Modified INOUT_ARRAY so the examples work again
Browse files Browse the repository at this point in the history
  • Loading branch information
pjotrp committed Jun 3, 2009
1 parent c8430c0 commit 50f0712
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 36 deletions.
8 changes: 4 additions & 4 deletions 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

Expand Down
3 changes: 1 addition & 2 deletions src/mappings/swig/ruby/rqtl/ruby_rqtl.i
Expand Up @@ -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)
*/
Expand Down
1 change: 1 addition & 0 deletions src/mappings/swig/ruby/test/test_example.rb
Expand Up @@ -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])
Expand Down
91 changes: 68 additions & 23 deletions src/mappings/swig/typemaps/ruby/biolib_array.i
@@ -1,18 +1,37 @@
%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; i<len; ++i)
($1)[i] = rb_len2dbl(RARRAY($input)->ptr[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; i<len; ++i)
($1)[i] = rb_len2dbl(RARRAY($input)->ptr[i]);
}

%typemap(freearg) type *name {
/* %typemap(freearg) type *name */
/* MAP_IN_ARRAY %typemap(freearg) type *name */
if ($1) free($1);
}
%enddef
Expand All @@ -29,49 +48,75 @@
* used as a function wide parameter.
*/

%define BIOLIB_INOUTARRAY(type,len,data,result)
/* FIX This will break the example
%typemap(in, leninputs=0) int len {
*/
/* %typemap(in, leninputs=0) int len: ignore len input */
/*
}
*/
%define MAP_INOUT_DIM_ARRAY(type,sizearg,data,result)

%typemap(in) (type* data, type *result) {
/* %typemap(in) (type* data, type *result) */
/* MAP_INOUT_DIM_ARRAY %typemap(in) (type* data, type *result) */
int i, asize;

if (!rb_obj_is_kind_of($input,rb_cArray))
rb_raise(rb_eArgError, "BIOLIB_INOUTARRAY expected Array of values for $1_name");
asize = RARRAY($input)->len;
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; i<asize; ++i)
($1)[i] = rb_len2dbl(RARRAY($input)->ptr[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; i<asize; ++i)
($2)[i] = rb_num2dbl(RARRAY($input)->ptr[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; i<asize; i++)
rb_ary_push($result,rb_float_new($3[i]));
}

%typemap(freearg) (int num, type* data, type *result) {
/* MAP_INOUT_ARRAY %typemap(in) (int, type* data, type *result) */
if ($2) free($2);
if ($3) free($3);
}
%enddef

%define MAP_OUTARRAY(type,sizearg,result)
/* Pass a result through an array pointer */

%typemap(in) type *result {
/* %typemap(in) type *result: ignore */
/* MAP_OUTARRAY %typemap(in) type *result: ignore */
}

%typemap(out) type *result {
/* %typemap(out) type *result: ignore */
/* MAP_OUT_ARRAY %typemap(out) type *result: ignore */
}

%typemap(argout) type *result {
/* %typemap(argout) type *result */
/* MAP_OUT_ARRAY %typemap(argout) type *result */
int i;
int len = sizearg;

Expand All @@ -82,7 +127,7 @@
}

%typemap(freearg) type *result {
/* %typemap(freearg) type *result */
/* MAP_OUT_ARRAY %typemap(freearg) type *result */
if ($1) free($1);
}
%enddef
Expand Down
14 changes: 7 additions & 7 deletions src/mappings/swig/typemaps/ruby/biolib_matrix.i
Expand Up @@ -13,9 +13,9 @@
* raise "Test failed with value #{result}" if result[3] != 4
*/

%define BIOLIB_INMATRIXASARRAY(type,colsarg,rowsarg,name)
%define MAP_IN_DIM_MATRIXASARRAY(type,colsarg,rowsarg,name)
%typemap(in) type **name {
/* BIOLIB_INMATRIXASARRAY %typemap(in) type **name */
/* MAP_IN_DIM_MATRIXASARRAY %typemap(in) type **name */
int cols = colsarg;
int rows = rowsarg;
int i, row;
Expand All @@ -24,7 +24,7 @@
type **dptr;

if (!rb_obj_is_kind_of($input,rb_cArray))
rb_raise(rb_eArgError, "BIOLIB_INMATRIXASARRAY expected Array of values for type **$1_name");
rb_raise(rb_eArgError, "MAP_IN_DIM_MATRIXASARRAY expected Array of values for type **$1_name");
len = rows * cols;
/* len = RARRAY($input)->len; */
dptr = (type **)malloc(rows*sizeof(type *));
Expand All @@ -38,19 +38,19 @@


%typemap(freearg) type **name {
/* BIOLIB_INMATRIXASARRAY %typemap(freearg) type **name */
/* MAP_IN_DIM_MATRIXASARRAY %typemap(freearg) type **name */
if ($1) {
free(*$1);
free($1);
}
}
%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;
Expand Down

0 comments on commit 50f0712

Please sign in to comment.