From b4a4dc7e26d86e03027d422afe3585373ebea1fd Mon Sep 17 00:00:00 2001 From: Simon Marchetto Date: Thu, 20 Mar 2014 15:03:31 +0100 Subject: [PATCH] scilab: complete doc on STL --- Doc/Manual/Scilab.html | 286 ++++++++++++++++++++++++++++++----------- 1 file changed, 208 insertions(+), 78 deletions(-) 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 @@
  • Pointers
  • Structures
  • Arrays -
  • Matrices
  • C++ classes
  • C++ templates
  • C++ STL @@ -42,6 +41,8 @@
  • Default type mapping for non-primitive types
  • Arrays
  • Pointer-to-pointers +
  • Matrices +
  • STL
  • Module

    +

    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. +

    + + +

    Sequence containers

    + +

    +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();
    +
    +

    + + +

    Associative containers

    + +

    +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);
    +
    +

    +

    37.5 Module