_slist.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. *
  3. * Copyright (c) 1996,1997
  4. * Silicon Graphics Computer Systems, Inc.
  5. *
  6. * Copyright (c) 1999
  7. * Boris Fomitchev
  8. *
  9. * This material is provided "as is", with absolutely no warranty expressed
  10. * or implied. Any use is at your own risk.
  11. *
  12. * Permission to use or copy this software for any purpose is hereby granted
  13. * without fee, provided the above notices are retained on all copies.
  14. * Permission to modify the code and to distribute modified code is granted,
  15. * provided the above notices are retained, and a notice that the code was
  16. * modified is included with the above copyright notice.
  17. *
  18. */
  19. #ifndef _STLP_SLIST_C
  20. #define _STLP_SLIST_C
  21. #ifndef _STLP_INTERNAL_SLIST_H
  22. # include <stl/_slist.h>
  23. #endif
  24. #ifndef _STLP_CARRAY_H
  25. # include <stl/_carray.h>
  26. #endif
  27. #ifndef _STLP_RANGE_ERRORS_H
  28. # include <stl/_range_errors.h>
  29. #endif
  30. #if defined (_STLP_NESTED_TYPE_PARAM_BUG)
  31. # define size_type size_t
  32. #endif
  33. _STLP_BEGIN_NAMESPACE
  34. _STLP_MOVE_TO_PRIV_NAMESPACE
  35. template <class _Tp, class _Alloc>
  36. _Slist_node_base*
  37. _Slist_base<_Tp,_Alloc>::_M_erase_after(_Slist_node_base* __before_first,
  38. _Slist_node_base* __last_node) {
  39. _Slist_node_base* __cur = __before_first->_M_next;
  40. while (__cur != __last_node) {
  41. _Node* __tmp = __STATIC_CAST(_Node*, __cur);
  42. __cur = __cur->_M_next;
  43. _STLP_STD::_Destroy(&__tmp->_M_data);
  44. _M_head.deallocate(__tmp,1);
  45. }
  46. __before_first->_M_next = __last_node;
  47. return __last_node;
  48. }
  49. #if defined (_STLP_USE_PTR_SPECIALIZATIONS)
  50. # define slist _STLP_PTR_IMPL_NAME(slist)
  51. #elif defined (_STLP_DEBUG)
  52. # define slist _STLP_NON_DBG_NAME(slist)
  53. #else
  54. _STLP_MOVE_TO_STD_NAMESPACE
  55. #endif
  56. /* When building STLport lib Digital Mars Compiler complains on the _M_data assignment
  57. * problem which would be perfertly right if we were using it. Hiding it during build
  58. * fix this issue.
  59. */
  60. template <class _Tp, class _Alloc>
  61. slist<_Tp,_Alloc>& slist<_Tp,_Alloc>::operator=(const slist<_Tp,_Alloc>& __x) {
  62. if (&__x != this) {
  63. _Node_base* __p1 = &this->_M_head._M_data;
  64. _Node_base* __n1 = this->_M_head._M_data._M_next;
  65. const _Node_base* __n2 = __x._M_head._M_data._M_next;
  66. while (__n1 && __n2) {
  67. __STATIC_CAST(_Node*, __n1)->_M_data = __STATIC_CAST(const _Node*, __n2)->_M_data;
  68. __p1 = __n1;
  69. __n1 = __n1->_M_next;
  70. __n2 = __n2->_M_next;
  71. }
  72. if (__n2 == 0)
  73. this->_M_erase_after(__p1, 0);
  74. else
  75. _M_insert_after_range(__p1, const_iterator(__CONST_CAST(_Node_base*, __n2)),
  76. const_iterator(0));
  77. }
  78. return *this;
  79. }
  80. template <class _Tp, class _Alloc>
  81. void slist<_Tp, _Alloc>::_M_fill_assign(size_type __n, const _Tp& __val) {
  82. _Node_base* __prev = &this->_M_head._M_data;
  83. _Node_base* __node = this->_M_head._M_data._M_next;
  84. for ( ; __node != 0 && __n > 0 ; --__n) {
  85. __STATIC_CAST(_Node*, __node)->_M_data = __val;
  86. __prev = __node;
  87. __node = __node->_M_next;
  88. }
  89. if (__n > 0)
  90. _M_insert_after_fill(__prev, __n, __val);
  91. else
  92. this->_M_erase_after(__prev, 0);
  93. }
  94. template <class _Tp, class _Alloc>
  95. void slist<_Tp,_Alloc>::resize(size_type __len, const _Tp& __x) {
  96. _Node_base* __cur = &this->_M_head._M_data;
  97. while (__cur->_M_next != 0 && __len > 0) {
  98. --__len;
  99. __cur = __cur->_M_next;
  100. }
  101. if (__cur->_M_next)
  102. this->_M_erase_after(__cur, 0);
  103. else
  104. _M_insert_after_fill(__cur, __len, __x);
  105. }
  106. template <class _Tp, class _Alloc>
  107. void slist<_Tp,_Alloc>::remove(const _Tp& __val) {
  108. _Node_base* __cur = &this->_M_head._M_data;
  109. while (__cur && __cur->_M_next) {
  110. if (__STATIC_CAST(_Node*, __cur->_M_next)->_M_data == __val)
  111. this->_M_erase_after(__cur);
  112. else
  113. __cur = __cur->_M_next;
  114. }
  115. }
  116. #if !defined (slist)
  117. _STLP_MOVE_TO_PRIV_NAMESPACE
  118. #endif
  119. template <class _Tp, class _Alloc, class _BinaryPredicate>
  120. void _Slist_unique(slist<_Tp, _Alloc>& __that, _BinaryPredicate __pred) {
  121. typedef _Slist_node<_Tp> _Node;
  122. typename slist<_Tp, _Alloc>::iterator __ite(__that.begin());
  123. if (__ite != __that.end()) {
  124. while (__ite._M_node->_M_next) {
  125. if (__pred(*__ite, __STATIC_CAST(_Node*, __ite._M_node->_M_next)->_M_data))
  126. __that.erase_after(__ite);
  127. else
  128. ++__ite;
  129. }
  130. }
  131. }
  132. template <class _Tp, class _Alloc, class _StrictWeakOrdering>
  133. void _Slist_merge(slist<_Tp, _Alloc>& __that, slist<_Tp, _Alloc>& __x,
  134. _StrictWeakOrdering __comp) {
  135. typedef _Slist_node<_Tp> _Node;
  136. typedef _STLP_PRIV _Slist_node_base _Node_base;
  137. if (__that.get_allocator() == __x.get_allocator()) {
  138. typename slist<_Tp, _Alloc>::iterator __ite(__that.before_begin());
  139. while (__ite._M_node->_M_next && !__x.empty()) {
  140. if (__comp(__x.front(), __STATIC_CAST(_Node*, __ite._M_node->_M_next)->_M_data)) {
  141. _STLP_VERBOSE_ASSERT(!__comp(__STATIC_CAST(_Node*, __ite._M_node->_M_next)->_M_data, __x.front()),
  142. _StlMsg_INVALID_STRICT_WEAK_PREDICATE)
  143. __that.splice_after(__ite, __x, __x.before_begin());
  144. }
  145. ++__ite;
  146. }
  147. if (!__x.empty()) {
  148. __that.splice_after(__ite, __x);
  149. }
  150. }
  151. else {
  152. typename slist<_Tp, _Alloc>::iterator __i1(__that.before_begin()), __i2(__x.begin());
  153. while (__i1._M_node->_M_next && __i2._M_node) {
  154. if (__comp(__STATIC_CAST(_Node*, __i1._M_node->_M_next)->_M_data, *__i2)) {
  155. _STLP_VERBOSE_ASSERT(!__comp(*__i2, __STATIC_CAST(_Node*, __i1._M_node->_M_next)->_M_data),
  156. _StlMsg_INVALID_STRICT_WEAK_PREDICATE)
  157. ++__i1;
  158. }
  159. else {
  160. __i1 = __that.insert_after(__i1, *(__i2++));
  161. }
  162. }
  163. __that.insert_after(__i1, __i2, __x.end());
  164. __x.clear();
  165. }
  166. }
  167. template <class _Tp, class _Alloc, class _StrictWeakOrdering>
  168. void _Slist_sort(slist<_Tp, _Alloc>& __that, _StrictWeakOrdering __comp) {
  169. if (!__that.begin()._M_node || !__that.begin()._M_node->_M_next)
  170. return;
  171. slist<_Tp, _Alloc> __carry(__that.get_allocator());
  172. const int NB = 64;
  173. _STLP_PRIV _CArray<slist<_Tp, _Alloc>, NB> __counter(__carry);
  174. int __fill = 0;
  175. while (!__that.empty()) {
  176. __carry.splice_after(__carry.before_begin(), __that, __that.before_begin());
  177. int __i = 0;
  178. while (__i < __fill && !__counter[__i].empty()) {
  179. _STLP_PRIV _Slist_merge(__counter[__i], __carry, __comp);
  180. __carry.swap(__counter[__i]);
  181. ++__i;
  182. }
  183. __carry.swap(__counter[__i]);
  184. if (__i == __fill) {
  185. ++__fill;
  186. if (__fill >= NB) {
  187. //Looks like the slist has too many elements to be sorted with this algorithm:
  188. __stl_throw_overflow_error("slist::sort");
  189. }
  190. }
  191. }
  192. for (int __i = 1; __i < __fill; ++__i)
  193. _STLP_PRIV _Slist_merge(__counter[__i], __counter[__i - 1], __comp);
  194. __that.swap(__counter[__fill-1]);
  195. }
  196. #if defined (slist)
  197. # undef slist
  198. #endif
  199. _STLP_MOVE_TO_STD_NAMESPACE
  200. _STLP_END_NAMESPACE
  201. #if defined (_STLP_NESTED_TYPE_PARAM_BUG)
  202. # undef size_type
  203. #endif
  204. #endif /* _STLP_SLIST_C */
  205. // Local Variables:
  206. // mode:C++
  207. // End: