1 /* ----------------------------------------------------------------------------- 2 * std_list.i 3 * 4 * SWIG typemaps for std::list types 5 * ----------------------------------------------------------------------------- */ 6 7 %include <std_common.i> 8 9 %module std_list 10 %{ 11 #include <list> 12 #include <stdexcept> 13 %} 14 15 16 namespace std{ 17 template<class T> class list 18 { 19 public: 20 21 typedef T &reference; 22 typedef const T& const_reference; 23 typedef T &iterator; 24 typedef const T& const_iterator; 25 26 list(); 27 list(unsigned int size, const T& value = T()); 28 list(const list<T> &); 29 30 ~list(); 31 void assign(unsigned int n, const T& value); 32 void swap(list<T> &x); 33 34 const_reference front(); 35 const_reference back(); 36 const_iterator begin(); 37 const_iterator end(); 38 39 void resize(unsigned int n, T c = T()); 40 bool empty() const; 41 42 void push_front(const T& x); 43 void push_back(const T& x); 44 45 46 void pop_front(); 47 void pop_back(); 48 void clear(); 49 unsigned int size() const; 50 unsigned int max_size() const; 51 void resize(unsigned int n, const T& value); 52 53 void remove(const T& value); 54 void unique(); 55 void reverse(); 56 void sort(); 57 58 59 60 %extend 61 { 62 const_reference __getitem__(int i) throw (std::out_of_range) 63 { 64 std::list<T>::iterator first = self->begin(); 65 int size = int(self->size()); 66 if (i<0) i += size; 67 if (i>=0 && i<size) 68 { 69 for (int k=0;k<i;k++) 70 { 71 first++; 72 } 73 return *first; 74 } 75 else throw std::out_of_range("list index out of range"); 76 } 77 void __setitem__(int i, const T& x) throw (std::out_of_range) 78 { 79 std::list<T>::iterator first = self->begin(); 80 int size = int(self->size()); 81 if (i<0) i += size; 82 if (i>=0 && i<size) 83 { 84 for (int k=0;k<i;k++) 85 { 86 first++; 87 } 88 *first = x; 89 } 90 else throw std::out_of_range("list index out of range"); 91 } 92 void __delitem__(int i) throw (std::out_of_range) 93 { 94 std::list<T>::iterator first = self->begin(); 95 int size = int(self->size()); 96 if (i<0) i += size; 97 if (i>=0 && i<size) 98 { 99 for (int k=0;k<i;k++) 100 { 101 first++; 102 } 103 self->erase(first); 104 } 105 else throw std::out_of_range("list index out of range"); 106 } 107 std::list<T> __getslice__(int i,int j) 108 { 109 std::list<T>::iterator first = self->begin(); 110 std::list<T>::iterator end = self->end(); 111 112 int size = int(self->size()); 113 if (i<0) i += size; 114 if (j<0) j += size; 115 if (i<0) i = 0; 116 if (j>size) j = size; 117 if (i>=j) i=j; 118 if (i>=0 && i<size && j>=0) 119 { 120 for (int k=0;k<i;k++) 121 { 122 first++; 123 } 124 for (int m=0;m<j;m++) 125 { 126 end++; 127 } 128 std::list<T> tmp(j-i); 129 if (j>i) std::copy(first,end,tmp.begin()); 130 return tmp; 131 } 132 else throw std::out_of_range("list index out of range"); 133 } 134 void __delslice__(int i,int j) 135 { 136 std::list<T>::iterator first = self->begin(); 137 std::list<T>::iterator end = self->end(); 138 139 int size = int(self->size()); 140 if (i<0) i += size; 141 if (j<0) j += size; 142 if (i<0) i = 0; 143 if (j>size) j = size; 144 145 for (int k=0;k<i;k++) 146 { 147 first++; 148 } 149 for (int m=0;m<=j;m++) 150 { 151 end++; 152 } 153 self->erase(first,end); 154 } 155 void __setslice__(int i,int j, const std::list<T>& v) 156 { 157 std::list<T>::iterator first = self->begin(); 158 std::list<T>::iterator end = self->end(); 159 160 int size = int(self->size()); 161 if (i<0) i += size; 162 if (j<0) j += size; 163 if (i<0) i = 0; 164 if (j>size) j = size; 165 166 for (int k=0;k<i;k++) 167 { 168 first++; 169 } 170 for (int m=0;m<=j;m++) 171 { 172 end++; 173 } 174 if (int(v.size()) == j-i) 175 { 176 std::copy(v.begin(),v.end(),first); 177 } 178 else { 179 self->erase(first,end); 180 if (i+1 <= int(self->size())) 181 { 182 first = self->begin(); 183 for (int k=0;k<i;k++) 184 { 185 first++; 186 } 187 self->insert(first,v.begin(),v.end()); 188 } 189 else self->insert(self->end(),v.begin(),v.end()); 190 } 191 192 } 193 unsigned int __len__() 194 { 195 return self->size(); 196 } 197 bool __nonzero__() 198 { 199 return !(self->empty()); 200 } 201 void append(const T& x) 202 { 203 self->push_back(x); 204 } 205 void pop() 206 { 207 self->pop_back(); 208 } 209 210 }; 211 212 }; 213 } 214 215 216 217 218 219 220