Scilab: std_set example converted to test

This commit is contained in:
Simon Marchetto 2013-09-09 11:13:36 +02:00
commit 3abb810cc1
6 changed files with 52 additions and 83 deletions

View file

@ -1,15 +0,0 @@
TOP = ../..
SWIG = $(TOP)/../preinst-swig
SRCS = example.cpp
TARGET = example_wrap.cxx
INTERFACE = example.i
check: build
$(MAKE) -f $(TOP)/Makefile scilab_run
build:
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' scilab_cpp
clean:
$(MAKE) -f $(TOP)/Makefile scilab_clean

View file

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

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

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

@ -1,34 +0,0 @@
lines(0);
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