Merge pull request #67 from smarchetto/gsoc2012-scilab

Scilab: generic support of STL containers
This commit is contained in:
Sylvestre Ledru 2013-08-01 04:43:34 -07:00
commit dde9dea5b0
60 changed files with 2047 additions and 995 deletions

View file

@ -1147,14 +1147,14 @@ R_CFLAGS=-fPIC
r: $(SRCS)
$(SWIG) -r $(SWIGOPT) $(INTERFACEPATH)
ifneq ($(SRCS),)
$(CXX) -g -c $(CFLAGS) $(R_CFLAGS) $(SRCS) $(INCLUDES)
$(CXX) -g -c $(CFLAGS) $(R_CFLAGS) $(SRCS) $(INCLUDES)
endif
+( PKG_CPPFLAGS="$(INCLUDES)" $(COMPILETOOL) $(R) CMD SHLIB -o $(LIBPREFIX)$(TARGET)$(SO) $(ISRCS) $(OBJS) > /dev/null )
r_cpp: $(CXXSRCS)
$(SWIG) -c++ -r $(SWIGOPT) -o $(RCXXSRCS) $(INTERFACEPATH)
ifneq ($(CXXSRCS),)
$(CXX) -g -c $(CFLAGS) $(R_CFLAGS) $(CXXSRCS) $(INCLUDES)
$(CXX) -g -c $(CFLAGS) $(R_CFLAGS) $(CXXSRCS) $(INCLUDES)
endif
+( PKG_CPPFLAGS="$(INCLUDES)" $(COMPILETOOL) $(R) CMD SHLIB -o $(LIBPREFIX)$(TARGET)$(SO) $(RCXXSRCS) $(OBJS) > /dev/null )
@ -1217,19 +1217,26 @@ scilab_cpp: $(SRCS)
env LD_LIBRARY_PATH=.:$$LD_LIBRARY_PATH SCILABPATH=$(srcdir):$$SCILABPATH echo 'exit(1)' |$(SCILAB) -nwni -noatomsautoload -nb -f builder.sce; \
fi
# -----------------------------------------------------------------
# -----------------------------------------------------------------
# Running a Scilab example
# -----------------------------------------------------------------
# -----------------------------------------------------------------
scilab_run:
@env LD_LIBRARY_PATH=.:$$LD_LIBRARY_PATH SCILABPATH=$(srcdir):$$SCILABPATH $(SCILAB) -nwni -noatomsautoload -nb -f runme.sci
# -----------------------------------------------------------------
# Debugging a scilab example
# -----------------------------------------------------------------
scilab_debug:
@env LD_LIBRARY_PATH=.:$$LD_LIBRARY_PATH SCILABPATH=$(srcdir):$$SCILABPATH $(SCILAB) -noatomsautoload -nb -debug -f runme.sci
# -----------------------------------------------------------------
# Cleaning the scilab examples
# -----------------------------------------------------------------
scilab_clean:
rm -f *.sce *.so lib*lib.c
rm -f *.sce *.so lib*lib.c *_wrap.*
##################################################################
##### Go ######

View file

@ -5,10 +5,14 @@ contract
enum
funcptr
matrix
#matrix2
matrix2
pointer
simple
std_set
std_vector/std_vector
std_vector/std_vector_as_function_argument
struct
template
variables

View file

@ -1,6 +1,8 @@
#include <stdlib.h>
double sumMatrixElements(double *inputMatrix, int nbRow, int nbCol)
// Double matrix functions
double sumDoubleMatrix(double *inputMatrix, int nbRow, int nbCol)
{
int i;
double total = 0.0;
@ -11,7 +13,7 @@ double sumMatrixElements(double *inputMatrix, int nbRow, int nbCol)
return total;
}
void squareMatrixElements(double *inputMatrix, int nbRow, int nbCol, double** resultMatrix, int* nbRowRes, int* nbColRes)
void squareDoubleMatrix(double *inputMatrix, int nbRow, int nbCol, double** resultMatrix, int* nbRowRes, int* nbColRes)
{
int i;
int size = nbRow * nbCol;
@ -24,7 +26,7 @@ void squareMatrixElements(double *inputMatrix, int nbRow, int nbCol, double** re
}
}
void getMatrix(double **resultMatrix, int *nbRowRes, int *nbColRes)
void getDoubleMatrix(double **resultMatrix, int *nbRowRes, int *nbColRes)
{
int i;
int size;
@ -37,3 +39,77 @@ void getMatrix(double **resultMatrix, int *nbRowRes, int *nbColRes)
(*resultMatrix)[i] = i*2;
}
}
// Integer matrix functions
int sumIntegerMatrix(int *inputMatrix, int nbRow, int nbCol)
{
int i;
int total = 0;
for (i=0; i<nbRow*nbCol; i++)
{
total += inputMatrix[i];
}
return total;
}
void squareIntegerMatrix(int *inputMatrix, int nbRow, int nbCol, int** resultMatrix, int* nbRowRes, int* nbColRes)
{
int i;
int size = nbRow * nbCol;
*nbRowRes = nbRow;
*nbColRes = nbCol;
*resultMatrix = (int*) malloc(size * sizeof(int));
for (i=0; i<size; i++)
{
(*resultMatrix)[i] = inputMatrix[i] * inputMatrix[i];
}
}
void getIntegerMatrix(int **resultMatrix, int *nbRowRes, int *nbColRes)
{
int i;
int size;
*nbRowRes = 5;
*nbColRes = 3;
size = (*nbRowRes) * (*nbColRes);
*resultMatrix = (int*) malloc(size * sizeof(int));
for (i=0; i<size; i++)
{
(*resultMatrix)[i] = i*2;
}
}
// String matrix functions
char* concatStringVector(char **inputVector, int size)
{
int i;
int resultSize;
char *result;
resultSize = 3 * size + 1;
result = calloc(resultSize, sizeof(char));
strcpy(result, inputVector[0]);
for (i=1; i<size; i++)
{
strcat(result, " ");
strcat(result, (const char*) inputVector[i]);
}
return result;
}
void getStringVector(char ***resultVector, int *sizeRes)
{
int i;
*sizeRes = 12;
*resultVector = (char**) malloc((*sizeRes) * sizeof(char*));
for (i=0; i<*sizeRes; i++)
{
char* pc = (char*) calloc(3, sizeof(char));
sprintf(pc, "%d", i);
(*resultVector)[i] = pc;
}
}

View file

@ -2,14 +2,25 @@
%include matrix.i
%apply (double *matrixAsInput, int rows, int cols) { (double *inputMatrix, int nbRow, int nbCol) }
%apply (double **matrixAsOutput, int *rows, int *cols) { (double **resultMatrix, int *nbRowRes, int *nbColRes) }
%apply (double *matrixIn, int matrixInRowCount, int matrixInColCount) { (double *inputMatrix, int nbRow, int nbCol) }
%apply (double **matrixOut, int *matrixOutRowCount, int *matrixOutColCount) { (double **resultMatrix, int *nbRowRes, int *nbColRes) }
%apply (int *matrixIn, int matrixInRowCount, int matrixInColCount) { (int *inputMatrix, int nbRow, int nbCol) }
%apply (int **matrixOut, int *matrixOutRowCount, int *matrixOutColCount) { (int **resultMatrix, int *nbRowRes, int *nbColRes) }
%inline
{
extern double sumMatrixElements(double *inputMatrix, int nbRow, int nbCol);
extern void squareMatrixElements(double *inputMatrix, int nbRow, int nbCol, double **resultMatrix, int *nbRowRes, int *nbColRes);
extern void getMatrix(double **resultMatrix, int *nbRowRes, int *nbColRes);
}
%apply (char **vectorIn, int vectorInSize) { (char **inputVector, int size) }
%apply (char ***vectorOut, int *vectorOutSize) { (char ***resultVector, int *sizeRes) }
%inline %{
extern double sumDoubleMatrix(double *inputMatrix, int nbRow, int nbCol);
extern void squareDoubleMatrix(double *inputMatrix, int nbRow, int nbCol, double **resultMatrix, int *nbRowRes, int *nbColRes);
extern void getDoubleMatrix(double **resultMatrix, int *nbRowRes, int *nbColRes);
extern int sumIntegerMatrix(int *inputMatrix, int nbRow, int nbCol);
extern void squareIntegerMatrix(int *inputMatrix, int nbRow, int nbCol, int **resultMatrix, int *nbRowRes, int *nbColRes);
extern void getIntegerMatrix(int **resultMatrix, int *nbRowRes, int *nbColRes);
extern char* concatStringVector(char **inputVector, int size);
extern void getStringVector(char ***resultVector, int *sizeRes);
%}

View file

@ -1,14 +1,45 @@
exec loader.sce
disp("Call lib function getMatrix()");
matrix1 = getMatrix();
disp(matrix1);
// Test lib double matrix functions
disp("Call lib function getDoubleMatrix()");
doubleMatrix = getDoubleMatrix();
disp(doubleMatrix);
disp("Call lib function sumMatrixElements()");
s = sumMatrixElements(matrix1);
disp("Call lib function sumDoubleMatrix()");
s = sumDoubleMatrix(doubleMatrix);
disp(s);
disp("Call lib function squareMatrixElements()");
squareMatrix = squareMatrixElements(matrix1);
disp(squareMatrix);
disp("Call lib function squareDoubleMatrix()");
squareDoubleMatrix = squareDoubleMatrix(doubleMatrix);
disp(squareDoubleMatrix);
// Test lib integer matrix functions
disp("Call lib function getIntegerMatrix()");
integerMatrix = getIntegerMatrix();
disp(integerMatrix);
disp("Call lib function sumIntegerMatrix()");
s = sumIntegerMatrix(integerMatrix);
disp(s);
disp("Call lib function squareIntegerMatrix()");
squareIntegerMatrix = squareIntegerMatrix(integerMatrix);
disp(squareIntegerMatrix);
// Test lib string matrix functions
disp("Call lib function getStringVector()");
stringVector = getStringVector();
disp(stringVector);
disp("Call lib function concatStringVector()");
stringVector2 = concatStringVector(stringVector);
disp(stringVector2);
exit

View file

@ -1,29 +0,0 @@
/* File : example.cpp */
#include "example.hxx"
std::set<int> create_iset(const int size, const int* values)
{
std::set<int> iset;
std::copy(values, values + size, std::inserter(iset, iset.begin()));
return iset;
}
int sum_iset(const std::set<int> iset)
{
int sum = 0;
std::set<int>::iterator it;
for (it = iset.begin(); it != iset.end(); it++)
{
sum += *it;
}
return sum;
}
void concat_iset(std::set<int>& iset, const std::set<int> other_iset)
{
std::copy(other_iset.begin(), other_iset.end(), std::inserter(iset, iset.begin()));
}

View file

@ -1,13 +0,0 @@
/* File : example.hxx */
#include <set>
std::set<int> create_iset(const int size, const int* values);
int sum_iset(const std::set<int> iset);
void concat_iset(std::set<int>& iset, const std::set<int> other_iset);

View file

@ -1,14 +0,0 @@
/* File : example.i */
%module example
%{
#include "example.hxx"
%}
%include "std_set.i"
%include "matrix.i"
%apply (int size, int* matrixAsInput) { (const int size, const int* values) };
%include "example.hxx";

View file

@ -1,16 +0,0 @@
exec loader.sce;
disp("this is an example with sets of int.");
disp("create the set from matrix [1, 2, 4, 4]:");
iset = create_iset(int32([1, 2, 4, 4]));
disp(iset);
s = sum_iset(iset);
disp("sum of this set elements is:");
disp(s);
iset2 = create_iset(int32([1, 10]));
disp("concat this set with the set of int {1, 10}:");
iset3 = concat_iset(iset, iset2);
disp(iset3);

View file

@ -0,0 +1,20 @@
TOP = ../..
SWIG = $(TOP)/../preinst-swig
SRCS = example.cpp
TARGET = example
INTERFACE = example.i
all: run
loader.sce:
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' scilab_cpp
clean:
$(MAKE) -f $(TOP)/Makefile scilab_clean
run: loader.sce
$(MAKE) -f $(TOP)/Makefile scilab_run
debug: loader.sce
$(MAKE) -f $(TOP)/Makefile scilab_debug

View file

@ -0,0 +1,61 @@
/* File : example.cpp */
#include "example.hxx"
#include <iostream>
#include <iterator>
#include <algorithm>
#include <numeric>
#include <sstream>
template <typename T>
std::list<T> concat_list(const std::list<T> list, const std::list<T> other_list)
{
std::list<T> out_list(list);
out_list.insert(out_list.end(), other_list.begin(), other_list.end());
return out_list;
}
// int lists
std::list<int> create_integer_list(const int rangemin, const int rangemax)
{
std::list<int> out_list;
for (int i = rangemin; i <= rangemax; i++)
{
out_list.push_back(i);
}
return out_list;
}
int sum_integer_list(const std::list<int>& list)
{
return std::accumulate(list.begin(), list.end(), 0);
}
std::list<int> concat_integer_list(const std::list<int> list, const std::list<int> other_list)
{
return concat_list<int>(list, other_list);
}
// string lists
std::list<std::string> create_string_list(const char* svalue)
{
std::list<std::string> out_list;
std::string str(svalue);
std::istringstream iss(str);
std::copy(std::istream_iterator<std::string>(iss),
std::istream_iterator<std::string>(),
std::inserter<std::list<std::string> >(out_list, out_list.begin()));
return out_list;
}
std::list<std::string> concat_string_list(const std::list<std::string> list, const std::list<std::string> other_list)
{
return concat_list<std::string>(list, other_list);
}

View file

@ -0,0 +1,14 @@
/* File : example.hxx */
#include <list>
#include <string>
// integer lists
std::list<int> create_integer_list(const int size, const int value);
int sum_integer_list(const std::list<int>& list);
std::list<int> concat_integer_list(const std::list<int> list, const std::list<int> other_list);
// string lists
std::list<std::string> create_string_list(const char* value);
std::list<std::string> concat_string_list(const std::list<std::string> list, const std::list<std::string> other_list);

View file

@ -0,0 +1,18 @@
/* File : example.i */
%module example
%{
#include "example.hxx"
%}
%include stl.i
/* instantiate the required template specializations */
namespace std
{
%template(IntList) list<int>;
%template(StringList) list<std::string>;
}
%include "example.hxx"

View file

@ -0,0 +1,31 @@
exec loader.sce;
SWIG_Init();
// This example shows how to use C++ fonctions with STL lists arguments
// Here, STL lists are converted from/to Scilab matrices (SWIG_SCILAB_EXTRA_NATIVE_CONTAINERS is not defined)
// integer lists
disp("Example of passing matrices of int as list arguments of C++ functions.");
disp("get a list of int {1...4} from create_integer_list():");
is = create_integer_list(1, 4);
disp(is);
disp("get the sum of this list elements with sum_integer_list():")
sum = sum_integer_list(is);
disp(is);
is2 = create_integer_list(3, 6);
disp("concat this list with the list of int {3...6} with concat_integer_list():");
is3 = concat_integer_list(is, is2);
disp(is3);
// string lists
disp("Example of passing matrices of string as list arguments of C++ functions.");
disp("get a list of string {''aa'', ''bb'', ''cc'', ''dd''} with create_string_list():");
ss = create_string_list("aa bb cc dd");
disp(ss);
ss2 = create_string_list("cc dd ee ff");
disp("concat this list with the list of string {''cc'', ''dd'', ''ee'', ''ff''} with concat_string_list():");
ss3 = concat_string_list(ss, ss2);
disp(ss3);

View file

@ -0,0 +1,20 @@
TOP = ../..
SWIG = $(TOP)/../preinst-swig
SRCS = example.cpp
TARGET = example
INTERFACE = example.i
all: check
loader.sce:
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' scilab_cpp
clean:
$(MAKE) -f $(TOP)/Makefile scilab_clean
check: loader.sce
$(MAKE) -f $(TOP)/Makefile scilab_run
debug: loader.sce
$(MAKE) -f $(TOP)/Makefile scilab_debug

View file

@ -0,0 +1,61 @@
/* File : example.cpp */
#include "example.hxx"
#include <iostream>
#include <iterator>
#include <algorithm>
#include <numeric>
#include <sstream>
template <typename T>
std::set<T> concat_set(const std::set<T> set, const std::set<T> other_set)
{
std::set<T> out_set(set);
out_set.insert(other_set.begin(), other_set.end());
return out_set;
}
// int sets
std::set<int> create_integer_set(const int rangemin, const int rangemax)
{
std::set<int> out_set;
for (int i = rangemin; i <= rangemax; i++)
{
out_set.insert(i);
}
return out_set;
}
int sum_integer_set(const std::set<int>& set)
{
return std::accumulate(set.begin(), set.end(), 0);
}
std::set<int> concat_integer_set(const std::set<int> set, const std::set<int> other_set)
{
return concat_set<int>(set, other_set);
}
// string sets
std::set<std::string> create_string_set(const char* svalue)
{
std::set<std::string> out_set;
std::string str(svalue);
std::istringstream iss(str);
std::copy(std::istream_iterator<std::string>(iss),
std::istream_iterator<std::string>(),
std::inserter<std::set<std::string> >(out_set, out_set.begin()));
return out_set;
}
std::set<std::string> concat_string_set(const std::set<std::string> set, const std::set<std::string> other_set)
{
return concat_set<std::string>(set, other_set);
}

View file

@ -0,0 +1,14 @@
/* File : example.hxx */
#include <set>
#include <string>
// integer sets
std::set<int> create_integer_set(const int size, const int value);
int sum_integer_set(const std::set<int>& set);
std::set<int> concat_integer_set(const std::set<int> set, const std::set<int> other_set);
// string sets
std::set<std::string> create_string_set(const char* value);
std::set<std::string> concat_string_set(const std::set<std::string> set, const std::set<std::string> other_set);

View file

@ -0,0 +1,18 @@
/* File : example.i */
%module example
%{
#include "example.hxx"
%}
%include stl.i
/* instantiate the required template specializations */
namespace std
{
%template(IntSet) set<int>;
%template(StringSet) set<std::string>;
}
%include "example.hxx"

View file

@ -0,0 +1,33 @@
exec loader.sce;
SWIG_Init();
// This example shows how to use C++ fonctions with STL sets arguments
// Here, STL sets are converted from/to Scilab matrices (SWIG_SCILAB_EXTRA_NATIVE_CONTAINERS is not defined)
// integer sets
disp("Example of passing matrices of int as set arguments of C++ functions.");
disp("get a set of int {1...4} from create_integer_set():");
is = create_integer_set(1, 4);
disp(is);
disp("get the sum of this set elements with sum_integer_set():")
sum = sum_integer_set(is);
disp(is);
is2 = create_integer_set(3, 6);
disp("concat this set with the set of int {3...6} with concat_integer_set():");
is3 = concat_integer_set(is, is2);
disp(is3);
// string sets
disp("Example of passing matrices of string as set arguments of C++ functions.");
disp("get a set of string {''aa'', ''bb'', ''cc'', ''dd''} with create_string_set():");
ss = create_string_set("aa bb cc dd");
disp(ss);
ss2 = create_string_set("cc dd ee ff");
disp("concat this set with the set of string {''cc'', ''dd'', ''ee'', ''ff''} with concat_string_set():");
ss3 = concat_string_set(ss, ss2);
disp(ss3);
exit

View file

@ -0,0 +1,20 @@
TOP = ../../..
SWIG = $(TOP)/../preinst-swig
SRCS =
TARGET = example
INTERFACE = example.i
all: check
loader.sce:
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' scilab_cpp
clean:
$(MAKE) -f $(TOP)/Makefile scilab_clean
check: loader.sce
$(MAKE) -f $(TOP)/Makefile scilab_run
debug: loader.sce
$(MAKE) -f $(TOP)/Makefile scilab_debug

Some files were not shown because too many files have changed in this diff Show more