1 %module("templatereduce") li_std_map
2 %feature("trackobjects");
3
4 #ifdef SWIGOCAML
5 %warnfilter(SWIGWARN_PARSE_KEYWORD) val;
6 #endif
7
8 %inline %{
9 namespace another {
10 struct map {
11 int val;
mapmap12 map(int x) : val(x) {}
13 };
14 }
15 %}
16
17 %include "std_pair.i"
18 %include "std_map.i"
19 %include "std_string.i"
20
21 // Declare some maps to play around with
22 %template(IntIntMap) std::map<int, int>;
23 %template(StringIntMap) std::map<std::string, int>;
24
25 %ignore Struct::operator<;
26 %ignore Struct::operator==;
27
28 // Add an inline function to test
29 %inline %{
30
valueAverage(std::map<std::string,int> m)31 double valueAverage(std::map<std::string, int> m) {
32 if (m.size() == 0) {
33 return 0.0;
34 }
35
36 double a = 0.0;
37 for (std::map<std::string, int>::iterator i = m.begin(); i != m.end(); i++) {
38 a += i->second;
39 }
40
41 return a / m.size();
42 }
43
stringifyKeys(std::map<std::string,int> m)44 std::string stringifyKeys(std::map<std::string, int> m) {
45 std::string a;
46 for (std::map<std::string, int>::iterator i = m.begin(); i != m.end(); i++) {
47 a += " " + i->first;
48 }
49 return a;
50 }
51
52 struct Struct {
53 double num;
StructStruct54 Struct() : num(0.0) {}
StructStruct55 Struct(double d) : num(d) {}
56 bool operator<(const Struct &other) const { return num < other.num; }
57 bool operator==(const Struct &other) const { return num == other.num; }
58 };
59
60 %}
61
62 //#if !defined(SWIGR)
63
64 // Test out some maps with pointer types
65 %template(IntIntPtrMap) std::map<int, int *>;
66 %template(IntConstIntPtrMap) std::map<int, const int *>;
67
68 //#endif
69
70
71 // Test out some maps with non-basic types and non-basic pointer types
72 %template(IntStructMap) std::map<int, Struct>;
73 %template(IntStructPtrMap) std::map<int, Struct *>;
74 %template(IntStructConstPtrMap) std::map<int, const Struct *>;
75 %template(StructPtrIntMap) std::map<Struct *, int>;
76
77 // Test out a non-specialized map
78 %template(StructIntMap) std::map<Struct, int>;
79
80 // Additional map definitions for Ruby, Python and Octave tests
81 %inline %{
82 struct A{
83 int val;
84
valA85 A(int v = 0): val(v) {
86 }
87 };
88 %}
89
90 namespace std {
91 %template(pairii) pair<int, int>;
92 %template(pairAA) pair<int, A>;
93 %template(pairA) pair<int, A*>;
94 %template(mapA) map<int, A*>;
95
96 %template(paircA1) pair<const int, A*>;
97 %template(paircA2) pair<const int, const A*>;
98 %template(pairiiA) pair<int,pair<int, A*> >;
99 %template(pairiiAc) pair<int,const pair<int, A*> >;
100
101
102 #ifdef SWIGRUBY
103 %template() pair< swig::LANGUAGE_OBJ, swig::LANGUAGE_OBJ >;
104 %template(LanguageMap) map< swig::LANGUAGE_OBJ, swig::LANGUAGE_OBJ >;
105 #endif
106
107 #ifdef SWIGPYTHON
108 %template() pair<swig::SwigPtr_PyObject, swig::SwigPtr_PyObject>;
109 %template(pymap) map<swig::SwigPtr_PyObject, swig::SwigPtr_PyObject>;
110 #endif
111
112 }
113
114 %inline {
p_identa(std::pair<int,A * > p)115 std::pair<int, A*> p_identa(std::pair<int, A*> p) {
116 return p;
117 }
118
m_identa(const std::map<int,A * > & v)119 std::map<int, A*> m_identa(const std::map<int,A*>& v) {
120 return v;
121 }
122 }
123
124 %ignore LengthCompare::operator();
125 %inline %{
126 struct LengthCompare {
operatorLengthCompare127 bool operator() (std::string s1, std::string s2) const {
128 return s1.size() < s2.size();
129 }
130 };
131 %}
132
133 // A map sorted by string lengths
134 %template(StringLengthNumberMap) std::map< std::string, int, LengthCompare >;
135
136 %inline %{
137 std::map< std::string, int, LengthCompare > MyMap;
populate(std::map<std::string,int,LengthCompare> & m)138 void populate(std::map< std::string, int, LengthCompare >&m) {
139 m["aa"] = 2;
140 m["xxxx"] = 4;
141 m["a"] = 1;
142 m["aaaaa"] = 5;
143 m["zzz"] = 3;
144 }
145 %}
146