PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Examples/test-suite/li_std_vector_extra.i

#
Swig | 148 lines | 110 code | 38 blank | 0 comment | 0 complexity | d5dea50d73fc551f4ee1e57480d3416d MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. %module li_std_vector_extra
  2. %warnfilter(509) overloaded1;
  3. %warnfilter(509) overloaded2;
  4. %include "std_string.i"
  5. %include "std_vector.i"
  6. %include "cpointer.i"
  7. %include "carrays.i"
  8. %{
  9. #include <algorithm>
  10. #include <functional>
  11. #include <numeric>
  12. %}
  13. namespace std {
  14. %template() vector<short>;
  15. %template(IntVector) vector<int>;
  16. %template(BoolVector) vector<bool>;
  17. %template() vector<string>;
  18. }
  19. %template(DoubleVector) std::vector<double>;
  20. %template(sizeVector) std::vector<size_t>;
  21. %{
  22. template <class T>
  23. struct Param
  24. {
  25. T val;
  26. Param(T v = 0): val(v) {
  27. }
  28. operator T() const { return val; }
  29. };
  30. %}
  31. specialize_std_vector(Param<int>,PyInt_Check,PyInt_AsLong,PyInt_FromLong);
  32. %template(PIntVector) std::vector<Param<int> >;
  33. %inline %{
  34. typedef float Real;
  35. %}
  36. namespace std {
  37. %template(RealVector) vector<Real>;
  38. }
  39. %inline %{
  40. double average(std::vector<int> v) {
  41. return std::accumulate(v.begin(),v.end(),0.0)/v.size();
  42. }
  43. std::vector<Real> half(const std::vector<Real>& v) {
  44. std::vector<Real> w(v);
  45. for (std::vector<Real>::size_type i=0; i<w.size(); i++)
  46. w[i] /= 2.0;
  47. return w;
  48. }
  49. void halve_in_place(std::vector<double>& v) {
  50. std::transform(v.begin(),v.end(),v.begin(),
  51. std::bind2nd(std::divides<double>(),2.0));
  52. }
  53. %}
  54. %template(IntPtrVector) std::vector<int *>;
  55. //
  56. //
  57. %{
  58. #include <iostream>
  59. %}
  60. %inline %{
  61. namespace Test {
  62. struct A {
  63. virtual ~A() {}
  64. virtual int f(const int i) const = 0;
  65. };
  66. struct B : public A {
  67. int val;
  68. B(int i = 0) : val(i)
  69. {
  70. }
  71. int f(const int i) const { return i + val; }
  72. };
  73. int vecAptr(const std::vector<A*>& v) {
  74. return v[0]->f(1);
  75. }
  76. }
  77. std::vector<short> halfs(const std::vector<short>& v) {
  78. std::vector<short> w(v);
  79. for (std::vector<short>::size_type i=0; i<w.size(); i++)
  80. w[i] /= 2;
  81. return w;
  82. }
  83. std::vector<std::string> vecStr(std::vector<std::string> v) {
  84. v[0] += v[1];
  85. return v;
  86. }
  87. %}
  88. %template(VecB) std::vector<Test::B>;
  89. %template(VecA) std::vector<Test::A*>;
  90. %pointer_class(int,PtrInt)
  91. %array_functions(int,ArrInt)
  92. %inline %{
  93. int *makeIntPtr(int v) { return new int(v); }
  94. const short *makeConstShortPtr(int v) { return new short(v); }
  95. double *makeDoublePtr(double v) { return new double(v); }
  96. int extractInt(int *p) { return *p; }
  97. short extractConstShort(const short *p) { return *p; }
  98. %}
  99. %template(pyvector) std::vector<swig::SwigPtr_PyObject>;
  100. namespace std {
  101. %template(ConstShortPtrVector) vector<const short *>;
  102. }
  103. %inline %{
  104. std::string overloaded1(std::vector<double> vi) { return "vector<double>"; }
  105. std::string overloaded1(std::vector<int> vi) { return "vector<int>"; }
  106. std::string overloaded2(std::vector<int> vi) { return "vector<int>"; }
  107. std::string overloaded2(std::vector<double> vi) { return "vector<double>"; }
  108. std::string overloaded3(std::vector<int> *vi) { return "vector<int> *"; }
  109. std::string overloaded3(int i) { return "int"; }
  110. %}