_list.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. *
  3. *
  4. * Copyright (c) 1994
  5. * Hewlett-Packard Company
  6. *
  7. * Copyright (c) 1996,1997
  8. * Silicon Graphics Computer Systems, Inc.
  9. *
  10. * Copyright (c) 1997
  11. * Moscow Center for SPARC Technology
  12. *
  13. * Copyright (c) 1999
  14. * Boris Fomitchev
  15. *
  16. * This material is provided "as is", with absolutely no warranty expressed
  17. * or implied. Any use is at your own risk.
  18. *
  19. * Permission to use or copy this software for any purpose is hereby granted
  20. * without fee, provided the above notices are retained on all copies.
  21. * Permission to modify the code and to distribute modified code is granted,
  22. * provided the above notices are retained, and a notice that the code was
  23. * modified is included with the above copyright notice.
  24. *
  25. */
  26. #ifndef _STLP_LIST_C
  27. #define _STLP_LIST_C
  28. #ifndef _STLP_INTERNAL_LIST_H
  29. # include <stl/_list.h>
  30. #endif
  31. #ifndef _STLP_CARRAY_H
  32. # include <stl/_carray.h>
  33. #endif
  34. #ifndef _STLP_RANGE_ERRORS_H
  35. # include <stl/_range_errors.h>
  36. #endif
  37. _STLP_BEGIN_NAMESPACE
  38. _STLP_MOVE_TO_PRIV_NAMESPACE
  39. #if defined (_STLP_EXPOSE_GLOBALS_IMPLEMENTATION)
  40. template <class _Dummy>
  41. void _STLP_CALL
  42. _List_global<_Dummy>::_Transfer(_List_node_base* __position,
  43. _List_node_base* __first, _List_node_base* __last) {
  44. if (__position != __last) {
  45. // Remove [first, last) from its old position.
  46. __last->_M_prev->_M_next = __position;
  47. __first->_M_prev->_M_next = __last;
  48. __position->_M_prev->_M_next = __first;
  49. // Splice [first, last) into its new position.
  50. _Node_base* __tmp = __position->_M_prev;
  51. __position->_M_prev = __last->_M_prev;
  52. __last->_M_prev = __first->_M_prev;
  53. __first->_M_prev = __tmp;
  54. }
  55. }
  56. #endif /* _STLP_EXPOSE_GLOBALS_IMPLEMENTATION */
  57. template <class _Tp, class _Alloc>
  58. void _List_base<_Tp,_Alloc>::clear() {
  59. _Node* __cur = __STATIC_CAST(_Node*, _M_node._M_data._M_next);
  60. while (
  61. #if defined (__BORLANDC__) // runtime error
  62. __cur &&
  63. #endif
  64. __cur != &(_M_node._M_data)) {
  65. _Node* __tmp = __cur;
  66. __cur = __STATIC_CAST(_Node*, __cur->_M_next);
  67. _STLP_STD::_Destroy(&__tmp->_M_data);
  68. this->_M_node.deallocate(__tmp, 1);
  69. }
  70. _M_node._M_data._M_next = &_M_node._M_data;
  71. _M_node._M_data._M_prev = &_M_node._M_data;
  72. }
  73. #if defined (_STLP_NESTED_TYPE_PARAM_BUG)
  74. # define size_type size_t
  75. #endif
  76. #if defined (_STLP_USE_PTR_SPECIALIZATIONS)
  77. # define list _STLP_PTR_IMPL_NAME(list)
  78. #elif defined (_STLP_DEBUG)
  79. # define list _STLP_NON_DBG_NAME(list)
  80. #else
  81. _STLP_MOVE_TO_STD_NAMESPACE
  82. #endif
  83. template <class _Tp, class _Alloc>
  84. void list<_Tp, _Alloc>::resize(size_type __new_size, const _Tp& __x) {
  85. iterator __i = begin();
  86. size_type __len = 0;
  87. for ( ; __i != end() && __len < __new_size; ++__i, ++__len);
  88. if (__len == __new_size)
  89. erase(__i, end());
  90. else // __i == end()
  91. insert(end(), __new_size - __len, __x);
  92. }
  93. template <class _Tp, class _Alloc>
  94. list<_Tp, _Alloc>& list<_Tp, _Alloc>::operator=(const list<_Tp, _Alloc>& __x) {
  95. if (this != &__x) {
  96. iterator __first1 = begin();
  97. iterator __last1 = end();
  98. const_iterator __first2 = __x.begin();
  99. const_iterator __last2 = __x.end();
  100. while (__first1 != __last1 && __first2 != __last2)
  101. *__first1++ = *__first2++;
  102. if (__first2 == __last2)
  103. erase(__first1, __last1);
  104. else
  105. insert(__last1, __first2, __last2);
  106. }
  107. return *this;
  108. }
  109. template <class _Tp, class _Alloc>
  110. void list<_Tp, _Alloc>::_M_fill_assign(size_type __n, const _Tp& __val) {
  111. iterator __i = begin();
  112. for ( ; __i != end() && __n > 0; ++__i, --__n)
  113. *__i = __val;
  114. if (__n > 0)
  115. insert(end(), __n, __val);
  116. else
  117. erase(__i, end());
  118. }
  119. #if !defined (list)
  120. _STLP_MOVE_TO_PRIV_NAMESPACE
  121. #endif
  122. template <class _Tp, class _Alloc, class _Predicate>
  123. void _S_remove_if(list<_Tp, _Alloc>& __that, _Predicate __pred) {
  124. typedef typename list<_Tp, _Alloc>::iterator _Literator;
  125. _Literator __first = __that.begin();
  126. _Literator __last = __that.end();
  127. while (__first != __last) {
  128. _Literator __next = __first;
  129. ++__next;
  130. if (__pred(*__first)) __that.erase(__first);
  131. __first = __next;
  132. }
  133. }
  134. template <class _Tp, class _Alloc, class _BinaryPredicate>
  135. void _S_unique(list<_Tp, _Alloc>& __that, _BinaryPredicate __binary_pred) {
  136. typedef typename list<_Tp, _Alloc>::iterator _Literator;
  137. _Literator __first = __that.begin();
  138. _Literator __last = __that.end();
  139. if (__first == __last) return;
  140. _Literator __next = __first;
  141. while (++__next != __last) {
  142. if (__binary_pred(*__first, *__next))
  143. __that.erase(__next);
  144. else
  145. __first = __next;
  146. __next = __first;
  147. }
  148. }
  149. template <class _Tp, class _Alloc, class _StrictWeakOrdering>
  150. void _S_merge(list<_Tp, _Alloc>& __that, list<_Tp, _Alloc>& __x,
  151. _StrictWeakOrdering __comp) {
  152. typedef typename list<_Tp, _Alloc>::iterator _Literator;
  153. _Literator __first1 = __that.begin();
  154. _Literator __last1 = __that.end();
  155. _Literator __first2 = __x.begin();
  156. _Literator __last2 = __x.end();
  157. if (__that.get_allocator() == __x.get_allocator()) {
  158. while (__first1 != __last1 && __first2 != __last2) {
  159. if (__comp(*__first2, *__first1)) {
  160. _STLP_VERBOSE_ASSERT(!__comp(*__first1, *__first2), _StlMsg_INVALID_STRICT_WEAK_PREDICATE)
  161. _Literator __next = __first2;
  162. _List_global_inst::_Transfer(__first1._M_node, __first2._M_node, (++__next)._M_node);
  163. __first2 = __next;
  164. }
  165. else
  166. ++__first1;
  167. }
  168. if (__first2 != __last2)
  169. _List_global_inst::_Transfer(__last1._M_node, __first2._M_node, __last2._M_node);
  170. }
  171. else {
  172. while (__first1 != __last1 && __first2 != __last2) {
  173. if (__comp(*__first2, *__first1)) {
  174. _STLP_VERBOSE_ASSERT(!__comp(*__first1, *__first2), _StlMsg_INVALID_STRICT_WEAK_PREDICATE)
  175. __first1 = __that.insert(__first1, *__first2);
  176. }
  177. else
  178. ++__first1;
  179. }
  180. if (__first2 != __last2) {
  181. __that.insert(__first1, __first2, __last2);
  182. }
  183. __x.clear();
  184. }
  185. }
  186. template <class _Tp, class _Alloc, class _StrictWeakOrdering>
  187. void _S_sort(list<_Tp, _Alloc>& __that, _StrictWeakOrdering __comp) {
  188. // Do nothing if the list has length 0 or 1.
  189. if (__that._M_node._M_data._M_next == &__that._M_node._M_data ||
  190. __that._M_node._M_data._M_next->_M_next == &__that._M_node._M_data)
  191. return;
  192. list<_Tp, _Alloc> __carry(__that.get_allocator());
  193. const int NB = 64;
  194. _STLP_PRIV _CArray<list<_Tp, _Alloc>, NB> __counter(__carry);
  195. int __fill = 0;
  196. while (!__that.empty()) {
  197. __carry.splice(__carry.begin(), __that, __that.begin());
  198. int __i = 0;
  199. while (__i < __fill && !__counter[__i].empty()) {
  200. _S_merge(__counter[__i], __carry, __comp);
  201. __carry.swap(__counter[__i++]);
  202. }
  203. __carry.swap(__counter[__i]);
  204. if (__i == __fill) {
  205. ++__fill;
  206. if (__fill >= NB) {
  207. //Looks like the list has too many elements to be sorted with this algorithm:
  208. __stl_throw_overflow_error("list::sort");
  209. }
  210. }
  211. }
  212. for (int __i = 1; __i < __fill; ++__i)
  213. _S_merge(__counter[__i], __counter[__i - 1], __comp);
  214. __that.swap(__counter[__fill - 1]);
  215. }
  216. #if defined (list)
  217. # undef list
  218. #endif
  219. _STLP_MOVE_TO_STD_NAMESPACE
  220. _STLP_END_NAMESPACE
  221. #endif /* _STLP_LIST_C */
  222. // Local Variables:
  223. // mode:C++
  224. // End: