PageRenderTime 18ms CodeModel.GetById 13ms app.highlight 2ms RepoModel.GetById 1ms 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
  1%module li_std_vector_extra
  2
  3%warnfilter(509) overloaded1;
  4%warnfilter(509) overloaded2;
  5
  6%include "std_string.i"
  7%include "std_vector.i"
  8%include "cpointer.i"
  9%include "carrays.i"
 10
 11%{
 12#include <algorithm>
 13#include <functional>
 14#include <numeric>
 15%}
 16
 17namespace std {
 18    %template() vector<short>;
 19    %template(IntVector) vector<int>;
 20    %template(BoolVector) vector<bool>;
 21    %template() vector<string>;
 22}
 23
 24%template(DoubleVector) std::vector<double>;
 25
 26
 27%template(sizeVector) std::vector<size_t>;
 28%{
 29  template <class T>
 30  struct Param
 31  {
 32    T val;
 33
 34    Param(T v = 0): val(v) {
 35    }
 36    
 37    operator T() const { return val; }
 38  };
 39%}
 40specialize_std_vector(Param<int>,PyInt_Check,PyInt_AsLong,PyInt_FromLong);
 41%template(PIntVector) std::vector<Param<int> >;
 42
 43%inline %{
 44typedef float Real;
 45%}
 46
 47namespace std {
 48    %template(RealVector) vector<Real>;
 49}
 50
 51%inline %{
 52
 53double average(std::vector<int> v) {
 54    return std::accumulate(v.begin(),v.end(),0.0)/v.size();
 55}
 56
 57std::vector<Real> half(const std::vector<Real>& v) {
 58    std::vector<Real> w(v);
 59    for (std::vector<Real>::size_type i=0; i<w.size(); i++)
 60        w[i] /= 2.0;
 61    return w;
 62}
 63
 64void halve_in_place(std::vector<double>& v) {
 65    std::transform(v.begin(),v.end(),v.begin(),
 66                   std::bind2nd(std::divides<double>(),2.0));
 67}
 68
 69%}
 70
 71%template(IntPtrVector) std::vector<int *>;
 72
 73
 74
 75//
 76//
 77%{
 78#include <iostream>
 79%}
 80
 81%inline %{
 82  
 83namespace Test {
 84struct A {
 85    virtual ~A() {}    
 86    virtual int f(const int i) const = 0;
 87};
 88
 89struct B : public A {
 90  int val;
 91  
 92  B(int i = 0) : val(i)
 93  {
 94  }
 95  
 96  int f(const int i) const { return i + val; }
 97};
 98
 99
100int vecAptr(const std::vector<A*>& v) {
101    return v[0]->f(1);
102}
103
104} 
105
106std::vector<short> halfs(const std::vector<short>& v) {
107    std::vector<short> w(v);
108    for (std::vector<short>::size_type i=0; i<w.size(); i++)
109        w[i] /= 2;
110    return w;
111}
112
113
114std::vector<std::string>  vecStr(std::vector<std::string> v) {
115  v[0] += v[1];
116  return v;
117}
118
119%}
120%template(VecB) std::vector<Test::B>; 
121%template(VecA) std::vector<Test::A*>; 
122
123%pointer_class(int,PtrInt)
124%array_functions(int,ArrInt)
125
126%inline %{
127  int *makeIntPtr(int v) { return new int(v); }
128  const short *makeConstShortPtr(int v) { return new short(v); }
129  double *makeDoublePtr(double v) { return new double(v); }
130  int extractInt(int *p) { return *p; }
131  short extractConstShort(const short *p) { return *p; }
132%}
133
134%template(pyvector) std::vector<swig::SwigPtr_PyObject>; 
135
136namespace std {
137   %template(ConstShortPtrVector) vector<const short *>;
138}
139
140%inline %{
141std::string overloaded1(std::vector<double> vi) { return "vector<double>"; }
142std::string overloaded1(std::vector<int> vi) { return "vector<int>"; }
143std::string overloaded2(std::vector<int> vi) { return "vector<int>"; }
144std::string overloaded2(std::vector<double> vi) { return "vector<double>"; }
145std::string overloaded3(std::vector<int> *vi) { return "vector<int> *"; }
146std::string overloaded3(int i) { return "int"; }
147%}
148