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