Scilab: add STL simple vector example (available also for Python, Ruby...)

This commit is contained in:
Simon Marchetto 2013-07-17 16:55:32 +02:00
commit 28dd6985c2
4 changed files with 98 additions and 0 deletions

View file

@ -0,0 +1,20 @@
TOP = ../../..
SWIG = $(TOP)/../preinst-swig
SRCS =
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,25 @@
/* File : example.h */
#include <vector>
#include <algorithm>
#include <functional>
#include <numeric>
double average(std::vector<int> v) {
return std::accumulate(v.begin(),v.end(),0.0)/v.size();
}
std::vector<double> half(const std::vector<double> v) {
std::vector<double> w(v);
for (unsigned int i=0; i<w.size(); i++)
w[i] /= 2.0;
return w;
}
void halve_in_place(std::vector<double>& v) {
// would you believe this is the same as the above?
std::transform(v.begin(),v.end(),v.begin(),
std::bind2nd(std::divides<double>(),2.0));
}

View file

@ -0,0 +1,18 @@
/* File : example.i */
%module example
%{
#include "example.h"
%}
%include stl.i
/* instantiate the required template specializations */
namespace std {
%template(IntVector) vector<int>;
%template(DoubleVector) vector<double>;
}
/* Let's just grab the original header file here */
%include "example.h"

View file

@ -0,0 +1,35 @@
// file: runme.sci
exec loader.sce;
SWIG_Init();
disp(mean([1,2,3,4]));
// ... or a wrapped std::vector<int>
v = new_IntVector();
for i = 1:4
IntVector_push_back(v, i);
end;
disp(average(v));
// half will return a Scilab matrix.
// Call it with a Scilab matrix...
disp(half([1.0, 1.5, 2.0, 2.5, 3.0]));
// ... or a wrapped std::vector<double>
v = new_DoubleVector();
for i = 1:4
DoubleVector_push_back(v, i);
end;
disp(half(v));
// now halve a wrapped std::vector<double> in place
halve_in_place(v);
disp(v);