PageRenderTime 46ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/rel-1.3.35/Lib/ocaml/std_list.i

#
Swig | 222 lines | 184 code | 30 blank | 8 comment | 0 complexity | a72a50ac7471458fb80a37a5601818f9 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  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. %include <std_common.i>
  10. %module std_list
  11. %{
  12. #include <list>
  13. #include <stdexcept>
  14. %}
  15. namespace std{
  16. template<class T> class list
  17. {
  18. public:
  19. typedef T &reference;
  20. typedef const T& const_reference;
  21. typedef T &iterator;
  22. typedef const T& const_iterator;
  23. list();
  24. list(unsigned int size, const T& value = T());
  25. list(const list<T> &);
  26. ~list();
  27. void assign(unsigned int n, const T& value);
  28. void swap(list<T> &x);
  29. const_reference front();
  30. const_reference back();
  31. const_iterator begin();
  32. const_iterator end();
  33. void resize(unsigned int n, T c = T());
  34. bool empty() const;
  35. void push_front(const T& x);
  36. void push_back(const T& x);
  37. void pop_front();
  38. void pop_back();
  39. void clear();
  40. unsigned int size() const;
  41. unsigned int max_size() const;
  42. void resize(unsigned int n, const T& value);
  43. void remove(const T& value);
  44. void unique();
  45. void reverse();
  46. void sort();
  47. %extend
  48. {
  49. const_reference __getitem__(int i) throw (std::out_of_range)
  50. {
  51. std::list<T>::iterator first = self->begin();
  52. int size = int(self->size());
  53. if (i<0) i += size;
  54. if (i>=0 && i<size)
  55. {
  56. for (int k=0;k<i;k++)
  57. {
  58. first++;
  59. }
  60. return *first;
  61. }
  62. else throw std::out_of_range("list index out of range");
  63. }
  64. void __setitem__(int i, const T& x) throw (std::out_of_range)
  65. {
  66. std::list<T>::iterator first = self->begin();
  67. int size = int(self->size());
  68. if (i<0) i += size;
  69. if (i>=0 && i<size)
  70. {
  71. for (int k=0;k<i;k++)
  72. {
  73. first++;
  74. }
  75. *first = x;
  76. }
  77. else throw std::out_of_range("list index out of range");
  78. }
  79. void __delitem__(int i) throw (std::out_of_range)
  80. {
  81. std::list<T>::iterator first = self->begin();
  82. int size = int(self->size());
  83. if (i<0) i += size;
  84. if (i>=0 && i<size)
  85. {
  86. for (int k=0;k<i;k++)
  87. {
  88. first++;
  89. }
  90. self->erase(first);
  91. }
  92. else throw std::out_of_range("list index out of range");
  93. }
  94. std::list<T> __getslice__(int i,int j)
  95. {
  96. std::list<T>::iterator first = self->begin();
  97. std::list<T>::iterator end = self->end();
  98. int size = int(self->size());
  99. if (i<0) i += size;
  100. if (j<0) j += size;
  101. if (i<0) i = 0;
  102. if (j>size) j = size;
  103. if (i>=j) i=j;
  104. if (i>=0 && i<size && j>=0)
  105. {
  106. for (int k=0;k<i;k++)
  107. {
  108. first++;
  109. }
  110. for (int m=0;m<j;m++)
  111. {
  112. end++;
  113. }
  114. std::list<T> tmp(j-i);
  115. if (j>i) std::copy(first,end,tmp.begin());
  116. return tmp;
  117. }
  118. else throw std::out_of_range("list index out of range");
  119. }
  120. void __delslice__(int i,int j)
  121. {
  122. std::list<T>::iterator first = self->begin();
  123. std::list<T>::iterator end = self->end();
  124. int size = int(self->size());
  125. if (i<0) i += size;
  126. if (j<0) j += size;
  127. if (i<0) i = 0;
  128. if (j>size) j = size;
  129. for (int k=0;k<i;k++)
  130. {
  131. first++;
  132. }
  133. for (int m=0;m<=j;m++)
  134. {
  135. end++;
  136. }
  137. self->erase(first,end);
  138. }
  139. void __setslice__(int i,int j, const std::list<T>& v)
  140. {
  141. std::list<T>::iterator first = self->begin();
  142. std::list<T>::iterator end = self->end();
  143. int size = int(self->size());
  144. if (i<0) i += size;
  145. if (j<0) j += size;
  146. if (i<0) i = 0;
  147. if (j>size) j = size;
  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. if (int(v.size()) == j-i)
  157. {
  158. std::copy(v.begin(),v.end(),first);
  159. }
  160. else {
  161. self->erase(first,end);
  162. if (i+1 <= int(self->size()))
  163. {
  164. first = self->begin();
  165. for (int k=0;k<i;k++)
  166. {
  167. first++;
  168. }
  169. self->insert(first,v.begin(),v.end());
  170. }
  171. else self->insert(self->end(),v.begin(),v.end());
  172. }
  173. }
  174. unsigned int __len__()
  175. {
  176. return self->size();
  177. }
  178. bool __nonzero__()
  179. {
  180. return !(self->empty());
  181. }
  182. void append(const T& x)
  183. {
  184. self->push_back(x);
  185. }
  186. void pop()
  187. {
  188. self->pop_back();
  189. }
  190. };
  191. };
  192. }