/project/jni/stlport/stlport/stl/_list.c

https://github.com/aichunyu/FFPlayer · C · 246 lines · 188 code · 26 blank · 32 comment · 57 complexity · c8aedaad7bcfa6428e6aff4ba87b8cea MD5 · raw file

  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 (__cur != &(_M_node._M_data)) {
  61. _Node* __tmp = __cur;
  62. __cur = __STATIC_CAST(_Node*, __cur->_M_next);
  63. _STLP_STD::_Destroy(&__tmp->_M_data);
  64. this->_M_node.deallocate(__tmp, 1);
  65. }
  66. _M_node._M_data._M_next = &_M_node._M_data;
  67. _M_node._M_data._M_prev = &_M_node._M_data;
  68. }
  69. #if defined (_STLP_NESTED_TYPE_PARAM_BUG)
  70. # define size_type size_t
  71. #endif
  72. #if defined (_STLP_USE_PTR_SPECIALIZATIONS)
  73. # define list _STLP_PTR_IMPL_NAME(list)
  74. #elif defined (_STLP_DEBUG)
  75. # define list _STLP_NON_DBG_NAME(list)
  76. #else
  77. _STLP_MOVE_TO_STD_NAMESPACE
  78. #endif
  79. template <class _Tp, class _Alloc>
  80. void list<_Tp, _Alloc>::resize(size_type __new_size, const _Tp& __x) {
  81. iterator __i = begin();
  82. size_type __len = 0;
  83. for ( ; __i != end() && __len < __new_size; ++__i, ++__len);
  84. if (__len == __new_size)
  85. erase(__i, end());
  86. else // __i == end()
  87. insert(end(), __new_size - __len, __x);
  88. }
  89. template <class _Tp, class _Alloc>
  90. list<_Tp, _Alloc>& list<_Tp, _Alloc>::operator=(const list<_Tp, _Alloc>& __x) {
  91. if (this != &__x) {
  92. iterator __first1 = begin();
  93. iterator __last1 = end();
  94. const_iterator __first2 = __x.begin();
  95. const_iterator __last2 = __x.end();
  96. while (__first1 != __last1 && __first2 != __last2)
  97. *__first1++ = *__first2++;
  98. if (__first2 == __last2)
  99. erase(__first1, __last1);
  100. else
  101. insert(__last1, __first2, __last2);
  102. }
  103. return *this;
  104. }
  105. template <class _Tp, class _Alloc>
  106. void list<_Tp, _Alloc>::_M_fill_assign(size_type __n, const _Tp& __val) {
  107. iterator __i = begin();
  108. for ( ; __i != end() && __n > 0; ++__i, --__n)
  109. *__i = __val;
  110. if (__n > 0)
  111. insert(end(), __n, __val);
  112. else
  113. erase(__i, end());
  114. }
  115. #if !defined (list)
  116. _STLP_MOVE_TO_PRIV_NAMESPACE
  117. #endif
  118. template <class _Tp, class _Alloc, class _Predicate>
  119. void _S_remove_if(list<_Tp, _Alloc>& __that, _Predicate __pred) {
  120. typedef typename list<_Tp, _Alloc>::iterator _Literator;
  121. _Literator __first = __that.begin();
  122. _Literator __last = __that.end();
  123. while (__first != __last) {
  124. _Literator __next = __first;
  125. ++__next;
  126. if (__pred(*__first)) __that.erase(__first);
  127. __first = __next;
  128. }
  129. }
  130. template <class _Tp, class _Alloc, class _BinaryPredicate>
  131. void _S_unique(list<_Tp, _Alloc>& __that, _BinaryPredicate __binary_pred) {
  132. typedef typename list<_Tp, _Alloc>::iterator _Literator;
  133. _Literator __first = __that.begin();
  134. _Literator __last = __that.end();
  135. if (__first == __last) return;
  136. _Literator __next = __first;
  137. while (++__next != __last) {
  138. if (__binary_pred(*__first, *__next))
  139. __that.erase(__next);
  140. else
  141. __first = __next;
  142. __next = __first;
  143. }
  144. }
  145. template <class _Tp, class _Alloc, class _StrictWeakOrdering>
  146. void _S_merge(list<_Tp, _Alloc>& __that, list<_Tp, _Alloc>& __x,
  147. _StrictWeakOrdering __comp) {
  148. typedef typename list<_Tp, _Alloc>::iterator _Literator;
  149. _Literator __first1 = __that.begin();
  150. _Literator __last1 = __that.end();
  151. _Literator __first2 = __x.begin();
  152. _Literator __last2 = __x.end();
  153. if (__that.get_allocator() == __x.get_allocator()) {
  154. while (__first1 != __last1 && __first2 != __last2) {
  155. if (__comp(*__first2, *__first1)) {
  156. _STLP_VERBOSE_ASSERT(!__comp(*__first1, *__first2), _StlMsg_INVALID_STRICT_WEAK_PREDICATE)
  157. _Literator __next = __first2;
  158. _List_global_inst::_Transfer(__first1._M_node, __first2._M_node, (++__next)._M_node);
  159. __first2 = __next;
  160. }
  161. else
  162. ++__first1;
  163. }
  164. if (__first2 != __last2)
  165. _List_global_inst::_Transfer(__last1._M_node, __first2._M_node, __last2._M_node);
  166. }
  167. else {
  168. while (__first1 != __last1 && __first2 != __last2) {
  169. if (__comp(*__first2, *__first1)) {
  170. _STLP_VERBOSE_ASSERT(!__comp(*__first1, *__first2), _StlMsg_INVALID_STRICT_WEAK_PREDICATE)
  171. __first1 = __that.insert(__first1, *__first2);
  172. }
  173. else
  174. ++__first1;
  175. }
  176. if (__first2 != __last2) {
  177. __that.insert(__first1, __first2, __last2);
  178. }
  179. __x.clear();
  180. }
  181. }
  182. template <class _Tp, class _Alloc, class _StrictWeakOrdering>
  183. void _S_sort(list<_Tp, _Alloc>& __that, _StrictWeakOrdering __comp) {
  184. // Do nothing if the list has length 0 or 1.
  185. if (__that._M_node._M_data._M_next == &__that._M_node._M_data ||
  186. __that._M_node._M_data._M_next->_M_next == &__that._M_node._M_data)
  187. return;
  188. list<_Tp, _Alloc> __carry(__that.get_allocator());
  189. const int NB = 64;
  190. _STLP_PRIV _CArray<list<_Tp, _Alloc>, NB> __counter(__carry);
  191. int __fill = 0;
  192. while (!__that.empty()) {
  193. __carry.splice(__carry.begin(), __that, __that.begin());
  194. int __i = 0;
  195. while (__i < __fill && !__counter[__i].empty()) {
  196. _S_merge(__counter[__i], __carry, __comp);
  197. __carry.swap(__counter[__i++]);
  198. }
  199. __carry.swap(__counter[__i]);
  200. if (__i == __fill) {
  201. ++__fill;
  202. if (__fill >= NB) {
  203. //Looks like the list has too many elements to be sorted with this algorithm:
  204. __stl_throw_overflow_error("list::sort");
  205. }
  206. }
  207. }
  208. for (int __i = 1; __i < __fill; ++__i)
  209. _S_merge(__counter[__i], __counter[__i - 1], __comp);
  210. __that.swap(__counter[__fill - 1]);
  211. }
  212. #if defined (list)
  213. # undef list
  214. #endif
  215. _STLP_MOVE_TO_STD_NAMESPACE
  216. _STLP_END_NAMESPACE
  217. #endif /* _STLP_LIST_C */
  218. // Local Variables:
  219. // mode:C++
  220. // End: