Add example for STL sets

This commit is contained in:
Simon Marchetto 2013-06-03 15:24:53 +02:00
commit 2e7a435440
5 changed files with 88 additions and 0 deletions

View file

@ -0,0 +1,16 @@
TOP = ../..
SWIG = $(TOP)/../preinst-swig
SRCS = example.cpp
TARGET = example
INTERFACE = example.i
all:
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' scilab_cpp
clean:
$(MAKE) -f $(TOP)/Makefile scilab_clean
rm -f *.sce *.so lib*lib.c *_wrap.cxx
check: all
$(MAKE) -f $(TOP)/Makefile scilab_run

View file

@ -0,0 +1,29 @@
/* 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

@ -0,0 +1,13 @@
/* 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

@ -0,0 +1,14 @@
/* 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

@ -0,0 +1,16 @@
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);