Scilab: update STL set example

Fixes:
- use generic support of STL set
- add example for set of string
This commit is contained in:
Simon Marchetto 2013-07-17 16:59:00 +02:00
commit 1cf1e72e72
9 changed files with 131 additions and 75 deletions

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

@ -4,13 +4,17 @@ SRCS = example.cpp
TARGET = example
INTERFACE = example.i
all:
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
rm -f *.sce *.so lib*lib.c *_wrap.cxx
check: all
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::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,31 @@
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);