PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Lib/ocaml/std_vector.i

#
Swig | 89 lines | 74 code | 10 blank | 5 comment | 0 complexity | 389ed1c36ca1cd9627ae4a316c4479ed MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. /* -----------------------------------------------------------------------------
  2. * std_vector.i
  3. *
  4. * SWIG typemaps for std::vector types
  5. * ----------------------------------------------------------------------------- */
  6. %include <std_common.i>
  7. // ------------------------------------------------------------------------
  8. // std::vector
  9. //
  10. // The aim of all that follows would be to integrate std::vector with
  11. // Python as much as possible, namely, to allow the user to pass and
  12. // be returned Python tuples or lists.
  13. // const declarations are used to guess the intent of the function being
  14. // exported; therefore, the following rationale is applied:
  15. //
  16. // -- f(std::vector<T>), f(const std::vector<T>&), f(const std::vector<T>*):
  17. // the parameter being read-only, either a Python sequence or a
  18. // previously wrapped std::vector<T> can be passed.
  19. // -- f(std::vector<T>&), f(std::vector<T>*):
  20. // the parameter must be modified; therefore, only a wrapped std::vector
  21. // can be passed.
  22. // -- std::vector<T> f():
  23. // the vector is returned by copy; therefore, a Python sequence of T:s
  24. // is returned which is most easily used in other Python functions
  25. // -- std::vector<T>& f(), std::vector<T>* f(), const std::vector<T>& f(),
  26. // const std::vector<T>* f():
  27. // the vector is returned by reference; therefore, a wrapped std::vector
  28. // is returned
  29. // ------------------------------------------------------------------------
  30. %{
  31. #include <vector>
  32. #include <algorithm>
  33. #include <stdexcept>
  34. %}
  35. // exported class
  36. namespace std {
  37. template <class T> class vector {
  38. public:
  39. vector(unsigned int size = 0);
  40. vector(unsigned int size, const T& value);
  41. vector(const vector<T>&);
  42. unsigned int size() const;
  43. bool empty() const;
  44. void clear();
  45. void push_back(const T& x);
  46. T operator [] ( int f );
  47. vector <T> &operator = ( vector <T> &other );
  48. %extend {
  49. void set( int i, const T &x ) {
  50. self->resize(i+1);
  51. (*self)[i] = x;
  52. }
  53. };
  54. %extend {
  55. T *to_array() {
  56. T *array = new T[self->size() + 1];
  57. for( int i = 0; i < self->size(); i++ )
  58. array[i] = (*self)[i];
  59. return array;
  60. }
  61. };
  62. };
  63. };
  64. %insert(ml) %{
  65. let array_to_vector v argcons array =
  66. for i = 0 to (Array.length array) - 1 do
  67. (invoke v) "set" (C_list [ C_int i ; (argcons array.(i)) ])
  68. done ;
  69. v
  70. let vector_to_array v argcons array =
  71. for i = 0; to (get_int ((invoke v) "size" C_void)) - 1 do
  72. array.(i) <- argcons ((invoke v) "[]" (C_int i))
  73. done ;
  74. v
  75. %}
  76. %insert(mli) %{
  77. val array_to_vector : c_obj -> ('a -> c_obj) -> 'a array -> c_obj
  78. val vector_to_array : c_obj -> (c_obj -> 'a) -> 'a array -> c_obj
  79. %}