scilab: fix typemaps for bool matrix
This commit is contained in:
parent
2db941df1e
commit
22dad86b40
4 changed files with 68 additions and 15 deletions
|
|
@ -42,5 +42,8 @@ test_matrix_typemaps("double", m, sum(m), m .* m);
|
||||||
//m = ["0" "3"; "1" "4"; "2" "5"]
|
//m = ["0" "3"; "1" "4"; "2" "5"]
|
||||||
//test_matrix_typemaps("charptr", m, strcat(m), m + m);
|
//test_matrix_typemaps("charptr", m, strcat(m), m + m);
|
||||||
|
|
||||||
|
m = [%T, %F; %F, %T; %T, %F];
|
||||||
|
test_matrix_typemaps("bool", m, %T, ~m);
|
||||||
|
|
||||||
|
|
||||||
exec("swigtest.quit", -1);
|
exec("swigtest.quit", -1);
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@
|
||||||
%use_matrix_apply(int);
|
%use_matrix_apply(int);
|
||||||
%use_matrix_apply(double);
|
%use_matrix_apply(double);
|
||||||
%use_matrix_apply(char *);
|
%use_matrix_apply(char *);
|
||||||
|
%use_matrix_apply(bool);
|
||||||
|
|
||||||
%{
|
%{
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
@ -25,16 +26,16 @@ template<typename T> T in_matrix_func(T *inputMatrix, int nbRow, int nbCol) {
|
||||||
for (i=0; i<nbRow*nbCol; i++)
|
for (i=0; i<nbRow*nbCol; i++)
|
||||||
sum += inputMatrix[i];
|
sum += inputMatrix[i];
|
||||||
return sum;
|
return sum;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T> void out_matrix_func(T **resultMatrix, int *nbRowRes, int *nbColRes) {
|
template<typename T> void out_matrix_func(T **resultMatrix, int *nbRowRes, int *nbColRes) {
|
||||||
int size;
|
int size;
|
||||||
int i;
|
int i;
|
||||||
*nbRowRes = 3;
|
*nbRowRes = 3;
|
||||||
*nbColRes = 2;
|
*nbColRes = 2;
|
||||||
size = (*nbRowRes) * (*nbColRes);
|
size = (*nbRowRes) * (*nbColRes);
|
||||||
*resultMatrix = (T*) malloc(size * sizeof(T));
|
*resultMatrix = (T*) malloc(size * sizeof(T));
|
||||||
for (i=0; i<size; i++)
|
for (i=0; i<size; i++)
|
||||||
(*resultMatrix)[i] = i;
|
(*resultMatrix)[i] = i;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -56,9 +57,9 @@ template<> char* in_matrix_func(char **inputMatrix, int nbRow, int nbCol) {
|
||||||
for (i=0; i<nbRow*nbCol; i++)
|
for (i=0; i<nbRow*nbCol; i++)
|
||||||
strcat(s, inputMatrix[i]);
|
strcat(s, inputMatrix[i]);
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<> void out_matrix_func(char ***resultMatrix, int *nbRowRes, int *nbColRes) {
|
template<> void out_matrix_func(char ***resultMatrix, int *nbRowRes, int *nbColRes) {
|
||||||
int size;
|
int size;
|
||||||
char *s;
|
char *s;
|
||||||
int i;
|
int i;
|
||||||
|
|
@ -68,7 +69,7 @@ template<> void out_matrix_func(char ***resultMatrix, int *nbRowRes, int *nbColR
|
||||||
*resultMatrix = (char **) malloc(size * sizeof(char *));
|
*resultMatrix = (char **) malloc(size * sizeof(char *));
|
||||||
for (i=0; i<size; i++) {
|
for (i=0; i<size; i++) {
|
||||||
s = (char *) malloc(sizeof(char)+1);
|
s = (char *) malloc(sizeof(char)+1);
|
||||||
sprintf(s, "%d", i);
|
sprintf(s, "%d", i);
|
||||||
(*resultMatrix)[i] = s;
|
(*resultMatrix)[i] = s;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -86,6 +87,39 @@ template<> void inout_matrix_func(char **inputMatrix, int nbRow, int nbCol, char
|
||||||
(*resultMatrix)[i] = s;
|
(*resultMatrix)[i] = s;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// bool matrix functions
|
||||||
|
template<> bool in_matrix_func(bool *inputMatrix, int nbRow, int nbCol) {
|
||||||
|
bool b = true;
|
||||||
|
int i;
|
||||||
|
b = inputMatrix[0];
|
||||||
|
for (i=1; i<nbRow*nbCol; i++)
|
||||||
|
b = b ^ inputMatrix[i];
|
||||||
|
return b;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<> void out_matrix_func(bool **resultMatrix, int *nbRowRes, int *nbColRes) {
|
||||||
|
int size;
|
||||||
|
int i;
|
||||||
|
*nbRowRes = 3;
|
||||||
|
*nbColRes = 2;
|
||||||
|
size = (*nbRowRes) * (*nbColRes);
|
||||||
|
*resultMatrix = (bool*) malloc(size * sizeof(bool));
|
||||||
|
for (i=0; i<size; i++) {
|
||||||
|
(*resultMatrix)[i] = i % 2 == 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<> void inout_matrix_func(bool *inputMatrix, int nbRow, int nbCol, bool **resultMatrix, int *nbRowRes, int *nbColRes) {
|
||||||
|
int i;
|
||||||
|
int size = nbRow * nbCol;
|
||||||
|
*nbRowRes = nbRow;
|
||||||
|
*nbColRes = nbCol;
|
||||||
|
*resultMatrix = (bool*) malloc(size * sizeof(bool));
|
||||||
|
for (i=0; i<size; i++) {
|
||||||
|
(*resultMatrix)[i] = ~inputMatrix[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
%}
|
%}
|
||||||
|
|
||||||
%define %instantiate_matrix_template_functions(NAME, TYPE...)
|
%define %instantiate_matrix_template_functions(NAME, TYPE...)
|
||||||
|
|
@ -97,7 +131,7 @@ template<> void inout_matrix_func(char **inputMatrix, int nbRow, int nbCol, char
|
||||||
%instantiate_matrix_template_functions(int, int);
|
%instantiate_matrix_template_functions(int, int);
|
||||||
%instantiate_matrix_template_functions(double, double);
|
%instantiate_matrix_template_functions(double, double);
|
||||||
%instantiate_matrix_template_functions(charptr, char *);
|
%instantiate_matrix_template_functions(charptr, char *);
|
||||||
//%instantiate_matrix_template_functions(bool);
|
%instantiate_matrix_template_functions(bool, bool);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,5 +6,6 @@
|
||||||
%include <scimatrixdouble.swg>
|
%include <scimatrixdouble.swg>
|
||||||
%include <scimatrixint.swg>
|
%include <scimatrixint.swg>
|
||||||
%include <scimatrixchar.swg>
|
%include <scimatrixchar.swg>
|
||||||
|
%include <scimatrixbool.swg>
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -51,11 +51,12 @@ SWIG_From_dec(bool)(bool _bValue) {
|
||||||
* C-type: bool[]
|
* C-type: bool[]
|
||||||
* Scilab type: boolean vector (but converted to int first because can not cast bool** to int **
|
* Scilab type: boolean vector (but converted to int first because can not cast bool** to int **
|
||||||
*/
|
*/
|
||||||
%fragment("SWIG_SciBoolean_AsIntArrayAndSize", "header") {
|
%fragment("SWIG_SciBoolean_AsBoolArrayAndSize", "header") {
|
||||||
SWIGINTERN int
|
SWIGINTERN int
|
||||||
SWIG_SciBoolean_AsIntArrayAndSize(void *_pvApiCtx, int _iVar, int *_iRows, int *_iCols, int **_piValue, char *_fname) {
|
SWIG_SciBoolean_AsBoolArrayAndSize(void *_pvApiCtx, int _iVar, int *_iRows, int *_iCols, bool **_pbValue, char *_fname) {
|
||||||
SciErr sciErr;
|
SciErr sciErr;
|
||||||
int *piAddrVar = NULL;
|
int *piAddrVar = NULL;
|
||||||
|
int *piValue = NULL;
|
||||||
|
|
||||||
sciErr = getVarAddressFromPosition(_pvApiCtx, _iVar, &piAddrVar);
|
sciErr = getVarAddressFromPosition(_pvApiCtx, _iVar, &piAddrVar);
|
||||||
if (sciErr.iErr) {
|
if (sciErr.iErr) {
|
||||||
|
|
@ -64,13 +65,19 @@ SWIG_SciBoolean_AsIntArrayAndSize(void *_pvApiCtx, int _iVar, int *_iRows, int *
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isBooleanType(_pvApiCtx, piAddrVar)) {
|
if (isBooleanType(_pvApiCtx, piAddrVar)) {
|
||||||
sciErr = getMatrixOfBoolean(_pvApiCtx, piAddrVar, _iRows, _iCols, _piValue);
|
int i;
|
||||||
|
sciErr = getMatrixOfBoolean(_pvApiCtx, piAddrVar, _iRows, _iCols, &piValue);
|
||||||
if (sciErr.iErr) {
|
if (sciErr.iErr) {
|
||||||
printError(&sciErr, 0);
|
printError(&sciErr, 0);
|
||||||
return SWIG_ERROR;
|
return SWIG_ERROR;
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
Scierror(999, _("%s: Wrong type for input argument #%d: A boolean vector expected.\n"), _fname, _iVar);
|
*_pbValue = (bool*) malloc((*_iRows) * (*_iCols) * sizeof(bool));
|
||||||
|
for (i=0; i< (*_iRows) * (*_iCols); i++)
|
||||||
|
(*_pbValue)[i] = piValue[i] != 0;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Scierror(999, _("%s: Wrong type for input argument #%d: A boolean matrix expected.\n"), _fname, _iVar);
|
||||||
return SWIG_ERROR;
|
return SWIG_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -78,17 +85,25 @@ SWIG_SciBoolean_AsIntArrayAndSize(void *_pvApiCtx, int _iVar, int *_iRows, int *
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
%fragment("SWIG_SciBoolean_FromIntArrayAndSize", "header") {
|
%fragment("SWIG_SciBoolean_FromBoolArrayAndSize", "header") {
|
||||||
SWIGINTERN int
|
SWIGINTERN int
|
||||||
SWIG_SciBoolean_FromIntArrayAndSize(void *_pvApiCtx, int _iVarOut, int _iRows, int _iCols, const int *_piData) {
|
SWIG_SciBoolean_FromBoolArrayAndSize(void *_pvApiCtx, int _iVarOut, int _iRows, int _iCols, bool *_pbValue) {
|
||||||
SciErr sciErr;
|
SciErr sciErr;
|
||||||
|
int *piValue = NULL;
|
||||||
|
int i;
|
||||||
|
|
||||||
sciErr = createMatrixOfBoolean(_pvApiCtx, SWIG_NbInputArgument(_pvApiCtx) + _iVarOut, _iRows, _iCols, _piData);
|
piValue = (int*) malloc(_iRows * _iCols * sizeof(int));
|
||||||
|
for (i=0; i< _iRows * _iCols; i++)
|
||||||
|
piValue[i] = _pbValue[i];
|
||||||
|
|
||||||
|
sciErr = createMatrixOfBoolean(_pvApiCtx, SWIG_NbInputArgument(_pvApiCtx) + _iVarOut, _iRows, _iCols, piValue);
|
||||||
if(sciErr.iErr) {
|
if(sciErr.iErr) {
|
||||||
printError(&sciErr, 0);
|
printError(&sciErr, 0);
|
||||||
|
free(piValue);
|
||||||
return SWIG_ERROR;
|
return SWIG_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
free(piValue);
|
||||||
return SWIG_OK;
|
return SWIG_OK;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue