diff --git a/Doc/Manual/Scilab.html b/Doc/Manual/Scilab.html index c16b5d084..438f827a4 100644 --- a/Doc/Manual/Scilab.html +++ b/Doc/Manual/Scilab.html @@ -31,7 +31,6 @@
-The Standard Template Library (STL) is partially supported. +The Standard Template Library (STL) is partially supported. See STL for more details.
--The following containers are usable: -
- -
-Each of these containers supports the following types: -
- --Some typemaps between Scilab and the STL are available. -
- -
-A STL vector/list/deque is mapped from/to a Scilab matrix or list, depending on type. -
- -
| STL type | -Scilab type | -
|---|---|
| vector/list/deque of int | int matrix |
| vector/list/deque of double | double matrix |
| vector/list/deque of string | string matrix |
| vector/list/deque of bool | bool matrix |
| vector/list/deque of pointer | pointer list |
-In the SWIG interface file, the STL support can be enabled with: -
- --%include stl.i -
As templates, for each specific type used, the STL container has the to be instantied: -
-namespace std {-->
- %template(IntVector) vector;
- %template(DoubleVector) vector;
- -At last, the module initialization function has to be executed first in Scilab, so that all that types are known by Scilab. -See 37.5.6 for more details. -
- - -The library matrix.i provides a set of typemaps which can be useful when working with one-dimensional and two-dimensional matrices. -
-To use that library, just include it in the interface file:
-+To use that library, just include it in the interface file: +
%include matrix.i @@ -1087,6 +1015,208 @@ The remarks made for arrays remain here: +37.4.6 STL
+ ++The STL library wraps some containers defined in the STL (Standard Template Library), so that they can be manipulated in Scilab. +This library provides also the typemaps to pass them as input/argument arguments of functions. +
+ +
+The list of wrapped sequence containers are: +
+And for associative containers: +
+ +
+The typemaps are available for the following types: +
+ ++Container of other item types are not supported. Using them does not break compilation, but provokes a runtime error. +
+ ++To use the STL, first the library has to be included in the SWIG interface file: +
+ ++%include stl.i +
Then for each container used, the template has to be instantied, in the std namespace: +
+namespace std {
+ %template(IntVector) vector<int>;
+ %template(DoubleVector) vector<double>;
+}
++At last, the module initialization function has to be executed first in Scilab, so that all that types are known by Scilab. +See 37.5.6 for more details. +
+ + ++Because in Scilab matrices exist for basic types only, a sequence container of pointers is mapped to a Scilab list. +For other item types (double, int, string...) the sequence container is mapped to a Scilab matrix. +
+ +
+This example shows how to create in Scilab a vector (of int), add some values in that vector, and pass it as an argument of a function. +It shows also (thanks to the typemaps) that we can also pass directly a matrix of values to the function: +
+ +
+%module example
+
+%include stl.i
+
+namespace std {
+ %template(IntVector) vector<int>;
+}
+
+%{
+#include <numeric>
+%}
+
+%inline %{
+
+double average(std::vector<int> v) {
+ return std::accumulate(v.begin(), v.end(), 0.0) / v.size();
+}
+
+%}
++
+--> example_Init(); + +--> v = new_IntVector(); + +--> for i = 1:4 +--> IntVector_push_back(v, i); +--> end; + +--> average(v) + ans = + + 2.5 + +--gt; average(int32([0 1 2 3])) + ans = + + 2.5 + +--> delete_IntVector(); +
+A set is mapped from/to a Scilab list. +
+ ++In the following example, a set of struct (Person>) is wrapped. +It is processed in a function, and as expected, the result is converted to a list of pointers in Scilab: + +
+%module example
+
+%include stl.i
+
+%{
+#include <string>
+%}
+
+%inline %{
+
+struct Person {
+ Person(std::string _name, int _age) : name(_name), age(_age) {};
+ std::string name;
+ int age;
+};
+typedef Person* PersonPtr;
+
+%}
+
+namespace std {
+ %template(PersonPtrSet) set<PersonPtr>;
+}
+
+%inline %{
+
+std::set<PersonPtr> findPersonsByAge(std::set<PersonPtr> persons, int minAge, int maxAge) {
+ std::set<PersonPtr> foundPersons;
+ for (std::set<PersonPtr>::iterator it = persons.begin(); it != persons.end(); it++) {
+ if (((*it)->age >= minAge) && ((*it)->age <= maxAge)) {
+ foundPersons.insert(*it);
+ }
+ }
+ return foundPersons;
+}
+
+%}
++
+--> example_Init();
+
+--> joe = new_Person("Joe", 25);
+--> susan = new_Person("Susan", 32);
+--> bill = new_Person("Bill", 50);
+
+--> p = new_PersonPtrSet();
+--> PersonPtrSet_insert(p, susan);
+--> PersonPtrSet_insert(p, joe);
+--> PersonPtrSet_insert(p, bill);
+
+--> l = findPersonsByAge(p, 20, 40);
+
+--> size(l)
+ ans =
+
+ 2.
+
+--> Person_name_get(l(1))
+ans =
+
+ Susan
+
+--> Person_name_get(l(2))
+ ans =
+
+ Joe
+
+--> delete_PersonPtrSet(p);
++