PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Lib/ocaml/std_list.i

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