summaryrefslogtreecommitdiff
path: root/share/swig/2.0.11/ocaml/std_vector.i
blob: 53d10744769d6b107c9c513a66679274c0582938 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/* -----------------------------------------------------------------------------
 * std_vector.i
 *
 * SWIG typemaps for std::vector types
 * ----------------------------------------------------------------------------- */

%include <std_common.i>

// ------------------------------------------------------------------------
// std::vector
// 
// The aim of all that follows would be to integrate std::vector with 
// Python as much as possible, namely, to allow the user to pass and 
// be returned Python tuples or lists.
// const declarations are used to guess the intent of the function being
// exported; therefore, the following rationale is applied:
// 
//   -- f(std::vector<T>), f(const std::vector<T>&), f(const std::vector<T>*):
//      the parameter being read-only, either a Python sequence or a
//      previously wrapped std::vector<T> can be passed.
//   -- f(std::vector<T>&), f(std::vector<T>*):
//      the parameter must be modified; therefore, only a wrapped std::vector
//      can be passed.
//   -- std::vector<T> f():
//      the vector is returned by copy; therefore, a Python sequence of T:s 
//      is returned which is most easily used in other Python functions
//   -- std::vector<T>& f(), std::vector<T>* f(), const std::vector<T>& f(),
//      const std::vector<T>* f():
//      the vector is returned by reference; therefore, a wrapped std::vector
//      is returned
// ------------------------------------------------------------------------

%{
#include <vector>
#include <algorithm>
#include <stdexcept>
%}

// exported class

namespace std {
    template <class T> class vector {
    public:
        vector(unsigned int size = 0);
        vector(unsigned int size, const T& value);
        vector(const vector<T>&);
        unsigned int size() const;
        bool empty() const;
        void clear();
        void push_back(const T& x);
	T operator [] ( int f );
	vector <T> &operator = ( vector <T> &other );
	%extend {
	    void set( int i, const T &x ) {
		self->resize(i+1);
		(*self)[i] = x;
	    }
	};
	%extend {
	    T *to_array() {
		T *array = new T[self->size() + 1];
		for( int i = 0; i < self->size(); i++ ) 
		    array[i] = (*self)[i];
		return array;
	    }
	};
    };
};

%insert(ml) %{
  
  let array_to_vector v argcons array = 
    for i = 0 to (Array.length array) - 1 do
	(invoke v) "set" (C_list [ C_int i ; (argcons array.(i)) ])
    done ;
    v
    
  let vector_to_array v argcons array =
    for i = 0; to (get_int ((invoke v) "size" C_void)) - 1 do
	array.(i) <- argcons ((invoke v) "[]" (C_int i))
    done ; 
    v
      
%}

%insert(mli) %{
    val array_to_vector : c_obj -> ('a -> c_obj) -> 'a array -> c_obj
    val vector_to_array : c_obj -> (c_obj -> 'a) -> 'a array -> c_obj
%}