1 %module li_std_vector 2 3 %include "std_vector.i" 4 %include "std_string.i" 5 6 %{ 7 #include <algorithm> 8 #include <functional> 9 #include <numeric> 10 %} 11 12 namespace std { 13 %template(IntVector) vector<int>; 14 } 15 16 %template(BoolVector) std::vector<bool>; 17 %template(CharVector) std::vector<char>; 18 %template(ShortVector) std::vector<short>; 19 %template(LongVector) std::vector<long>; 20 %template(UCharVector) std::vector<unsigned char>; 21 %template(UIntVector) std::vector<unsigned int>; 22 %template(UShortVector) std::vector<unsigned short>; 23 %template(ULongVector) std::vector<unsigned long>; 24 %template(DoubleVector) std::vector<double>; 25 %template(StringVector) std::vector<std::string>; 26 27 28 %inline %{ 29 typedef float Real; typedef_test(std::vector<int>::size_type s)30size_t typedef_test(std::vector<int>::size_type s) { return s; } 31 %} 32 33 namespace std { 34 %template(RealVector) vector<Real>; 35 } 36 37 %inline %{ 38 average(std::vector<int> v)39double average(std::vector<int> v) { 40 return std::accumulate(v.begin(),v.end(),0.0)/v.size(); 41 } 42 half(const std::vector<Real> & v)43std::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 halve_in_place(std::vector<double> & v)50void halve_in_place(std::vector<double>& v) { 51 for (std::vector<double>::iterator it = v.begin(); it != v.end(); ++it) 52 *it /= 2.0; 53 } 54 55 struct Struct { 56 double num; StructStruct57 Struct() : num(0.0) {} StructStruct58 Struct(double d) : num(d) {} 59 }; 60 61 struct Structure { 62 double num; StructureStructure63 Structure() : num(0.0) {} StructureStructure64 Structure(double d) : num(d) {} 65 }; 66 vecreal(const std::vector<Real> & vec)67const std::vector<Real> & vecreal(const std::vector<Real> & vec) { return vec; } 68 vecintptr(const std::vector<int> & vec)69const std::vector<int> & vecintptr(const std::vector<int> & vec) { return vec; } vecintptr(const std::vector<int * > & vec)70const std::vector<int *> & vecintptr(const std::vector<int *> & vec) { return vec; } vecintconstptr(const std::vector<const int * > & vec)71const std::vector<const int *> & vecintconstptr(const std::vector<const int *> & vec) { return vec; } 72 vecstruct(const std::vector<Struct> & vec)73const std::vector<Struct> & vecstruct(const std::vector<Struct> & vec) { return vec; } vecstructptr(const std::vector<Struct * > & vec)74const std::vector<Struct *> & vecstructptr(const std::vector<Struct *> & vec) { return vec; } vecstructconstptr(const std::vector<const Struct * > & vec)75const std::vector<const Struct *> & vecstructconstptr(const std::vector<const Struct *> & vec) { return vec; } 76 %} 77 78 #if !defined(SWIGR) 79 %template(IntPtrVector) std::vector<int *>; 80 %template(IntConstPtrVector) std::vector<const int *>; 81 #endif 82 %template(StructVector) std::vector<Struct>; 83 %template(StructPtrVector) std::vector<Struct *>; 84 %template(StructConstPtrVector) std::vector<const Struct *>; 85 86 %inline { 87 struct MyClass {}; 88 typedef MyClass *MyClassPtr; 89 typedef std::vector<MyClassPtr> MyClassVector; 90 } 91 %template(MyClassPtrVector) std::vector<MyClassPtr>; 92 93 %inline { 94 class RetsMetadata 95 { 96 public: GetAllResources(size_t n)97 MyClassVector GetAllResources(size_t n) const 98 { 99 return MyClassVector(n, 0); 100 } 101 }; 102 } 103 104 #if defined(SWIGRUBY) 105 %template(LanguageVector) std::vector< swig::LANGUAGE_OBJ >; 106 107 %inline { 108 std::vector< swig::LANGUAGE_OBJ > LanguageVector; 109 } 110 #endif 111 112 113 // Test that the digraph <::aa::Holder> is not generated 114 %include <std_vector.i> 115 116 %inline %{ 117 namespace aa { 118 struct Holder { numberHolder119 Holder(int n = 0) : number(n) {} 120 int number; 121 }; 122 } 123 %} 124 125 #if !defined(SWIGOCTAVE) 126 // To fix: something different in Octave is preventing this from working 127 %template(VectorTest) std::vector< ::aa::Holder >; 128 129 %inline %{ vec1(std::vector<::aa::Holder> x)130std::vector< ::aa::Holder > vec1(std::vector< ::aa::Holder > x) { return x; } 131 %} 132 #endif 133 134 // exercising vectors of strings 135 %inline %{ RevStringVec(const std::vector<std::string> & In)136std::vector<std::string> RevStringVec (const std::vector<std::string> &In) 137 { 138 std::vector<std::string> result(In); 139 std::reverse(result.begin(), result.end()); 140 return(result); 141 } 142 %} 143