_slist.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  1. /*
  2. *
  3. * Copyright (c) 1996,1997
  4. * Silicon Graphics Computer Systems, Inc.
  5. *
  6. * Copyright (c) 1997
  7. * Moscow Center for SPARC Technology
  8. *
  9. * Copyright (c) 1999
  10. * Boris Fomitchev
  11. *
  12. * This material is provided "as is", with absolutely no warranty expressed
  13. * or implied. Any use is at your own risk.
  14. *
  15. * Permission to use or copy this software for any purpose is hereby granted
  16. * without fee, provided the above notices are retained on all copies.
  17. * Permission to modify the code and to distribute modified code is granted,
  18. * provided the above notices are retained, and a notice that the code was
  19. * modified is included with the above copyright notice.
  20. *
  21. */
  22. /* NOTE: This is an internal header file, included by other STL headers.
  23. * You should not attempt to use it directly.
  24. */
  25. #ifndef _STLP_INTERNAL_SLIST_H
  26. #define _STLP_INTERNAL_SLIST_H
  27. #ifndef _STLP_INTERNAL_ALGOBASE_H
  28. # include <stl/_algobase.h>
  29. #endif
  30. #ifndef _STLP_INTERNAL_ALLOC_H
  31. # include <stl/_alloc.h>
  32. #endif
  33. #ifndef _STLP_INTERNAL_ITERATOR_H
  34. # include <stl/_iterator.h>
  35. #endif
  36. #ifndef _STLP_INTERNAL_CONSTRUCT_H
  37. # include <stl/_construct.h>
  38. #endif
  39. #ifndef _STLP_INTERNAL_FUNCTION_BASE_H
  40. # include <stl/_function_base.h>
  41. #endif
  42. #ifndef _STLP_INTERNAL_SLIST_BASE_H
  43. # include <stl/_slist_base.h>
  44. #endif
  45. _STLP_BEGIN_NAMESPACE
  46. _STLP_MOVE_TO_PRIV_NAMESPACE
  47. template <class _Tp>
  48. class _Slist_node : public _Slist_node_base {
  49. public:
  50. _Tp _M_data;
  51. __TRIVIAL_STUFF(_Slist_node)
  52. };
  53. struct _Slist_iterator_base {
  54. typedef size_t size_type;
  55. typedef ptrdiff_t difference_type;
  56. typedef forward_iterator_tag iterator_category;
  57. _Slist_node_base *_M_node;
  58. _Slist_iterator_base(_Slist_node_base *__x) : _M_node(__x) {}
  59. void _M_incr() {
  60. _M_node = _M_node->_M_next;
  61. }
  62. };
  63. template <class _Tp, class _Traits>
  64. class _Slist_iterator : public _Slist_iterator_base {
  65. public:
  66. typedef typename _Traits::value_type value_type;
  67. typedef typename _Traits::pointer pointer;
  68. typedef typename _Traits::reference reference;
  69. typedef forward_iterator_tag iterator_category;
  70. typedef size_t size_type;
  71. typedef ptrdiff_t difference_type;
  72. typedef _Slist_iterator<_Tp, _Traits> _Self;
  73. typedef typename _Traits::_NonConstTraits _NonConstTraits;
  74. typedef _Slist_iterator<_Tp, _NonConstTraits> iterator;
  75. typedef typename _Traits::_ConstTraits _ConstTraits;
  76. typedef _Slist_iterator<_Tp, _ConstTraits> const_iterator;
  77. typedef _Slist_node<value_type> _Node;
  78. explicit _Slist_iterator(_Slist_node_base *__x) : _Slist_iterator_base(__x) {}
  79. _Slist_iterator() : _Slist_iterator_base(0) {}
  80. //copy constructor for iterator and constructor from iterator for const_iterator
  81. _Slist_iterator(const iterator& __x) : _Slist_iterator_base(__x._M_node) {}
  82. reference operator*() const { return __STATIC_CAST(_Node*, this->_M_node)->_M_data; }
  83. _STLP_DEFINE_ARROW_OPERATOR
  84. _Self& operator++() {
  85. _M_incr();
  86. return *this;
  87. }
  88. _Self operator++(int) {
  89. _Self __tmp = *this;
  90. _M_incr();
  91. return __tmp;
  92. }
  93. bool operator==(const_iterator __y ) const {
  94. return this->_M_node == __y._M_node;
  95. }
  96. bool operator!=(const_iterator __y ) const {
  97. return this->_M_node != __y._M_node;
  98. }
  99. };
  100. #if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
  101. _STLP_MOVE_TO_STD_NAMESPACE
  102. template <class _Tp, class _Traits>
  103. struct __type_traits<_STLP_PRIV _Slist_iterator<_Tp, _Traits> > {
  104. typedef __false_type has_trivial_default_constructor;
  105. typedef __true_type has_trivial_copy_constructor;
  106. typedef __true_type has_trivial_assignment_operator;
  107. typedef __true_type has_trivial_destructor;
  108. typedef __false_type is_POD_type;
  109. };
  110. _STLP_MOVE_TO_PRIV_NAMESPACE
  111. #endif /* _STLP_CLASS_PARTIAL_SPECIALIZATION */
  112. #if defined (_STLP_USE_OLD_HP_ITERATOR_QUERIES)
  113. _STLP_MOVE_TO_STD_NAMESPACE
  114. template <class _Tp, class _Traits>
  115. inline _Tp* _STLP_CALL value_type(const _STLP_PRIV _Slist_iterator<_Tp, _Traits>&) { return __STATIC_CAST(_Tp*, 0); }
  116. inline ptrdiff_t* _STLP_CALL distance_type(const _STLP_PRIV _Slist_iterator_base&) { return 0; }
  117. inline forward_iterator_tag _STLP_CALL iterator_category(const _STLP_PRIV _Slist_iterator_base&) { return forward_iterator_tag(); }
  118. _STLP_MOVE_TO_PRIV_NAMESPACE
  119. #endif /* OLD_QUERIES */
  120. // Base class that encapsulates details of allocators and simplifies EH
  121. template <class _Tp, class _Alloc>
  122. class _Slist_base {
  123. protected:
  124. typedef _Slist_node<_Tp> _Node;
  125. typedef typename _Alloc_traits<_Node,_Alloc>::allocator_type _M_node_allocator_type;
  126. typedef _Slist_base<_Tp, _Alloc> _Self;
  127. public:
  128. typedef _STLP_alloc_proxy<_Slist_node_base, _Node, _M_node_allocator_type> _AllocProxy;
  129. _STLP_FORCE_ALLOCATORS(_Tp, _Alloc)
  130. typedef _Alloc allocator_type;
  131. _Slist_base(const allocator_type& __a) :
  132. _M_head(_STLP_CONVERT_ALLOCATOR(__a, _Node), _Slist_node_base() )
  133. { _M_head._M_data._M_next = 0; }
  134. #if !defined (_STLP_NO_MOVE_SEMANTIC)
  135. _Slist_base(__move_source<_Self> src) :
  136. _M_head(__move_source<_AllocProxy>(src.get()._M_head))
  137. { src.get()._M_head._M_data._M_next = 0; }
  138. #endif
  139. ~_Slist_base() { _M_erase_after(&_M_head._M_data, 0); }
  140. protected:
  141. _Slist_node_base* _M_erase_after(_Slist_node_base* __pos) {
  142. _Node* __next = __STATIC_CAST(_Node*, __pos->_M_next);
  143. _Slist_node_base* __next_next = __next->_M_next;
  144. __pos->_M_next = __next_next;
  145. _STLP_STD::_Destroy(&__next->_M_data);
  146. _M_head.deallocate(__next,1);
  147. return __next_next;
  148. }
  149. _Slist_node_base* _M_erase_after(_Slist_node_base*, _Slist_node_base*);
  150. public:
  151. allocator_type get_allocator() const
  152. { return _STLP_CONVERT_ALLOCATOR((const _M_node_allocator_type&)_M_head, _Tp); }
  153. _AllocProxy _M_head;
  154. };
  155. #if defined (_STLP_USE_PTR_SPECIALIZATIONS)
  156. # define slist _STLP_PTR_IMPL_NAME(slist)
  157. #elif defined (_STLP_DEBUG)
  158. # define slist _STLP_NON_DBG_NAME(slist)
  159. #else
  160. _STLP_MOVE_TO_STD_NAMESPACE
  161. #endif
  162. template <class _Tp, _STLP_DFL_TMPL_PARAM(_Alloc, allocator<_Tp>) >
  163. class slist;
  164. #if !defined (slist)
  165. _STLP_MOVE_TO_PRIV_NAMESPACE
  166. #endif
  167. // helper functions to reduce code duplication
  168. template <class _Tp, class _Alloc, class _BinaryPredicate>
  169. void _Slist_unique(slist<_Tp, _Alloc>& __that, _BinaryPredicate __binary_pred);
  170. template <class _Tp, class _Alloc, class _StrictWeakOrdering>
  171. void _Slist_merge(slist<_Tp, _Alloc>& __that, slist<_Tp, _Alloc>& __x,
  172. _StrictWeakOrdering __comp);
  173. template <class _Tp, class _Alloc, class _StrictWeakOrdering>
  174. void _Slist_sort(slist<_Tp, _Alloc>& __that, _StrictWeakOrdering __comp);
  175. #if !defined (slist)
  176. _STLP_MOVE_TO_STD_NAMESPACE
  177. #endif
  178. template <class _Tp, class _Alloc>
  179. class slist : protected _STLP_PRIV _Slist_base<_Tp,_Alloc>
  180. #if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND) && !defined (slist)
  181. , public __stlport_class<slist<_Tp, _Alloc> >
  182. #endif
  183. {
  184. private:
  185. typedef _STLP_PRIV _Slist_base<_Tp,_Alloc> _Base;
  186. typedef slist<_Tp,_Alloc> _Self;
  187. public:
  188. typedef _Tp value_type;
  189. typedef value_type* pointer;
  190. typedef const value_type* const_pointer;
  191. typedef value_type& reference;
  192. typedef const value_type& const_reference;
  193. typedef size_t size_type;
  194. typedef ptrdiff_t difference_type;
  195. typedef forward_iterator_tag _Iterator_category;
  196. typedef _STLP_PRIV _Slist_iterator<_Tp, _Nonconst_traits<_Tp> > iterator;
  197. typedef _STLP_PRIV _Slist_iterator<_Tp, _Const_traits<_Tp> > const_iterator;
  198. _STLP_FORCE_ALLOCATORS(_Tp, _Alloc)
  199. typedef typename _Base::allocator_type allocator_type;
  200. private:
  201. typedef _STLP_PRIV _Slist_node<_Tp> _Node;
  202. typedef _STLP_PRIV _Slist_node_base _Node_base;
  203. #if !defined(_STLP_DONT_SUP_DFLT_PARAM)
  204. _Node* _M_create_node(const value_type& __x = _Tp()) {
  205. #else
  206. _Node* _M_create_node(const value_type& __x) {
  207. #endif /*_STLP_DONT_SUP_DFLT_PARAM*/
  208. _Node* __node = this->_M_head.allocate(1);
  209. _STLP_TRY {
  210. _Copy_Construct(&__node->_M_data, __x);
  211. __node->_M_next = 0;
  212. }
  213. _STLP_UNWIND(this->_M_head.deallocate(__node, 1))
  214. return __node;
  215. }
  216. #if defined(_STLP_DONT_SUP_DFLT_PARAM)
  217. _Node* _M_create_node() {
  218. _Node* __node = this->_M_head.allocate(1);
  219. _STLP_TRY {
  220. _STLP_STD::_Construct(&__node->_M_data);
  221. __node->_M_next = 0;
  222. }
  223. _STLP_UNWIND(this->_M_head.deallocate(__node, 1))
  224. return __node;
  225. }
  226. #endif /*_STLP_DONT_SUP_DFLT_PARAM*/
  227. public:
  228. allocator_type get_allocator() const { return _Base::get_allocator(); }
  229. #if !defined (_STLP_DONT_SUP_DFLT_PARAM)
  230. explicit slist(const allocator_type& __a = allocator_type())
  231. #else
  232. slist()
  233. : _STLP_PRIV _Slist_base<_Tp,_Alloc>(allocator_type()) {}
  234. slist(const allocator_type& __a)
  235. #endif
  236. : _STLP_PRIV _Slist_base<_Tp,_Alloc>(__a) {}
  237. #if !defined (_STLP_DONT_SUP_DFLT_PARAM)
  238. explicit slist(size_type __n, const value_type& __x = _STLP_DEFAULT_CONSTRUCTED(_Tp),
  239. const allocator_type& __a = allocator_type())
  240. #else
  241. explicit slist(size_type __n)
  242. : _STLP_PRIV _Slist_base<_Tp,_Alloc>(allocator_type())
  243. { _M_insert_after_fill(&this->_M_head._M_data, __n, _STLP_DEFAULT_CONSTRUCTED(_Tp)); }
  244. slist(size_type __n, const value_type& __x)
  245. : _STLP_PRIV _Slist_base<_Tp,_Alloc>(allocator_type())
  246. { _M_insert_after_fill(&this->_M_head._M_data, __n, __x); }
  247. slist(size_type __n, const value_type& __x, const allocator_type& __a)
  248. #endif
  249. : _STLP_PRIV _Slist_base<_Tp,_Alloc>(__a)
  250. { _M_insert_after_fill(&this->_M_head._M_data, __n, __x); }
  251. #if defined (_STLP_MEMBER_TEMPLATES)
  252. // We don't need any dispatching tricks here, because _M_insert_after_range
  253. // already does them.
  254. template <class _InputIterator>
  255. slist(_InputIterator __first, _InputIterator __last,
  256. const allocator_type& __a _STLP_ALLOCATOR_TYPE_DFL)
  257. : _STLP_PRIV _Slist_base<_Tp,_Alloc>(__a)
  258. { _M_insert_after_range(&this->_M_head._M_data, __first, __last); }
  259. # if defined (_STLP_NEEDS_EXTRA_TEMPLATE_CONSTRUCTORS)
  260. // VC++ needs this crazyness
  261. template <class _InputIterator>
  262. slist(_InputIterator __first, _InputIterator __last)
  263. : _STLP_PRIV _Slist_base<_Tp,_Alloc>(allocator_type())
  264. { _M_insert_after_range(&this->_M_head._M_data, __first, __last); }
  265. # endif
  266. #else /* _STLP_MEMBER_TEMPLATES */
  267. slist(const_iterator __first, const_iterator __last,
  268. const allocator_type& __a = allocator_type() )
  269. : _STLP_PRIV _Slist_base<_Tp,_Alloc>(__a)
  270. { _M_insert_after_range(&this->_M_head._M_data, __first, __last); }
  271. slist(const value_type* __first, const value_type* __last,
  272. const allocator_type& __a = allocator_type())
  273. : _STLP_PRIV _Slist_base<_Tp,_Alloc>(__a)
  274. { _M_insert_after_range(&this->_M_head._M_data, __first, __last); }
  275. #endif /* _STLP_MEMBER_TEMPLATES */
  276. slist(const _Self& __x)
  277. : _STLP_PRIV _Slist_base<_Tp,_Alloc>(__x.get_allocator())
  278. { _M_insert_after_range(&this->_M_head._M_data, __x.begin(), __x.end()); }
  279. #if !defined (_STLP_NO_MOVE_SEMANTIC)
  280. slist(__move_source<_Self> src)
  281. : _STLP_PRIV _Slist_base<_Tp, _Alloc>(__move_source<_Base>(src.get())) {}
  282. #endif
  283. _Self& operator= (const _Self& __x);
  284. ~slist() {}
  285. public:
  286. // assign(), a generalized assignment member function. Two
  287. // versions: one that takes a count, and one that takes a range.
  288. // The range version is a member template, so we dispatch on whether
  289. // or not the type is an integer.
  290. void assign(size_type __n, const _Tp& __val) { _M_fill_assign(__n, __val); }
  291. private:
  292. void _M_fill_assign(size_type __n, const _Tp& __val);
  293. #if defined (_STLP_MEMBER_TEMPLATES)
  294. public:
  295. template <class _InputIterator>
  296. void assign(_InputIterator __first, _InputIterator __last) {
  297. typedef typename _IsIntegral<_InputIterator>::_Ret _Integral;
  298. _M_assign_dispatch(__first, __last, _Integral());
  299. }
  300. private:
  301. template <class _Integer>
  302. void _M_assign_dispatch(_Integer __n, _Integer __val,
  303. const __true_type& /*_IsIntegral*/) {
  304. _M_fill_assign((size_type) __n, (_Tp) __val);
  305. }
  306. template <class _InputIter>
  307. void _M_assign_dispatch(_InputIter __first, _InputIter __last,
  308. const __false_type& /*_IsIntegral*/) {
  309. #else
  310. public:
  311. void assign(const_pointer __first, const_pointer __last) {
  312. _Node_base* __prev = &this->_M_head._M_data;
  313. _Node_base* __node = this->_M_head._M_data._M_next;
  314. while (__node != 0 && __first != __last) {
  315. __STATIC_CAST(_Node*, __node)->_M_data = *__first;
  316. __prev = __node;
  317. __node = __node->_M_next;
  318. ++__first;
  319. }
  320. if (__first != __last)
  321. _M_insert_after_range(__prev, __first, __last);
  322. else
  323. this->_M_erase_after(__prev, 0);
  324. }
  325. void assign(const_iterator __first, const_iterator __last) {
  326. #endif /* _STLP_MEMBER_TEMPLATES */
  327. _Node_base* __prev = &this->_M_head._M_data;
  328. _Node_base* __node = this->_M_head._M_data._M_next;
  329. while (__node != 0 && __first != __last) {
  330. __STATIC_CAST(_Node*, __node)->_M_data = *__first;
  331. __prev = __node;
  332. __node = __node->_M_next;
  333. ++__first;
  334. }
  335. if (__first != __last)
  336. _M_insert_after_range(__prev, __first, __last);
  337. else
  338. this->_M_erase_after(__prev, 0);
  339. }
  340. public:
  341. // Experimental new feature: before_begin() returns a
  342. // non-dereferenceable iterator that, when incremented, yields
  343. // begin(). This iterator may be used as the argument to
  344. // insert_after, erase_after, etc. Note that even for an empty
  345. // slist, before_begin() is not the same iterator as end(). It
  346. // is always necessary to increment before_begin() at least once to
  347. // obtain end().
  348. iterator before_begin() { return iterator(&this->_M_head._M_data); }
  349. const_iterator before_begin() const
  350. { return const_iterator(__CONST_CAST(_Node_base*, &this->_M_head._M_data)); }
  351. iterator begin() { return iterator(this->_M_head._M_data._M_next); }
  352. const_iterator begin() const
  353. { return const_iterator(this->_M_head._M_data._M_next);}
  354. iterator end() { return iterator(); }
  355. const_iterator end() const { return const_iterator(); }
  356. size_type size() const
  357. { return _STLP_PRIV _Sl_global_inst::size(this->_M_head._M_data._M_next); }
  358. size_type max_size() const { return size_type(-1); }
  359. bool empty() const { return this->_M_head._M_data._M_next == 0; }
  360. void swap(_Self& __x)
  361. { this->_M_head.swap(__x._M_head); }
  362. #if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND) && !defined (_STLP_FUNCTION_TMPL_PARTIAL_ORDER)
  363. void _M_swap_workaround(_Self& __x) { swap(__x); }
  364. #endif
  365. public:
  366. reference front() { return *begin(); }
  367. const_reference front() const { return *begin(); }
  368. #if !defined (_STLP_DONT_SUP_DFLT_PARAM) && !defined (_STLP_NO_ANACHRONISMS)
  369. void push_front(const value_type& __x = _Tp()) {
  370. #else
  371. void push_front(const value_type& __x) {
  372. #endif /*!_STLP_DONT_SUP_DFLT_PARAM && !_STLP_NO_ANACHRONISMS*/
  373. _STLP_PRIV __slist_make_link(&this->_M_head._M_data, _M_create_node(__x));
  374. }
  375. #if defined (_STLP_DONT_SUP_DFLT_PARAM) && !defined (_STLP_NO_ANACHRONISMS)
  376. void push_front() { _STLP_PRIV __slist_make_link(&this->_M_head._M_data, _M_create_node());}
  377. #endif /*_STLP_DONT_SUP_DFLT_PARAM && !_STLP_NO_ANACHRONISMS*/
  378. void pop_front() {
  379. _Node* __node = __STATIC_CAST(_Node*, this->_M_head._M_data._M_next);
  380. this->_M_head._M_data._M_next = __node->_M_next;
  381. _STLP_STD::_Destroy(&__node->_M_data);
  382. this->_M_head.deallocate(__node, 1);
  383. }
  384. iterator previous(const_iterator __pos) {
  385. return iterator(_STLP_PRIV _Sl_global_inst::__previous(&this->_M_head._M_data, __pos._M_node));
  386. }
  387. const_iterator previous(const_iterator __pos) const {
  388. return const_iterator(__CONST_CAST(_Node_base*,
  389. _STLP_PRIV _Sl_global_inst::__previous(&this->_M_head._M_data,
  390. __pos._M_node)));
  391. }
  392. private:
  393. #if !defined (_STLP_DONT_SUP_DFLT_PARAM)
  394. _Node* _M_insert_after(_Node_base* __pos, const value_type& __x = _Tp()) {
  395. #else
  396. _Node* _M_insert_after(_Node_base* __pos, const value_type& __x) {
  397. #endif /*_STLP_DONT_SUP_DFLT_PARAM*/
  398. return __STATIC_CAST(_Node*, _STLP_PRIV __slist_make_link(__pos, _M_create_node(__x)));
  399. }
  400. #if defined (_STLP_DONT_SUP_DFLT_PARAM)
  401. _Node* _M_insert_after(_Node_base* __pos) {
  402. return __STATIC_CAST(_Node*, _STLP_PRIV __slist_make_link(__pos, _M_create_node()));
  403. }
  404. #endif /*_STLP_DONT_SUP_DFLT_PARAM*/
  405. void _M_insert_after_fill(_Node_base* __pos,
  406. size_type __n, const value_type& __x) {
  407. for (size_type __i = 0; __i < __n; ++__i)
  408. __pos = _STLP_PRIV __slist_make_link(__pos, _M_create_node(__x));
  409. }
  410. #if defined (_STLP_MEMBER_TEMPLATES)
  411. // Check whether it's an integral type. If so, it's not an iterator.
  412. template <class _InIter>
  413. void _M_insert_after_range(_Node_base* __pos,
  414. _InIter __first, _InIter __last) {
  415. typedef typename _IsIntegral<_InIter>::_Ret _Integral;
  416. _M_insert_after_range(__pos, __first, __last, _Integral());
  417. }
  418. template <class _Integer>
  419. void _M_insert_after_range(_Node_base* __pos, _Integer __n, _Integer __x,
  420. const __true_type&) {
  421. _M_insert_after_fill(__pos, __n, __x);
  422. }
  423. template <class _InIter>
  424. void _M_insert_after_range(_Node_base* __pos,
  425. _InIter __first, _InIter __last,
  426. const __false_type&) {
  427. #else /* _STLP_MEMBER_TEMPLATES */
  428. void _M_insert_after_range(_Node_base* __pos,
  429. const value_type* __first,
  430. const value_type* __last) {
  431. while (__first != __last) {
  432. __pos = _STLP_PRIV __slist_make_link(__pos, _M_create_node(*__first));
  433. ++__first;
  434. }
  435. }
  436. void _M_insert_after_range(_Node_base* __pos,
  437. const_iterator __first, const_iterator __last) {
  438. #endif /* _STLP_MEMBER_TEMPLATES */
  439. while (__first != __last) {
  440. __pos = _STLP_PRIV __slist_make_link(__pos, _M_create_node(*__first));
  441. ++__first;
  442. }
  443. }
  444. #if defined (_STLP_MEMBER_TEMPLATES)
  445. // Check whether it's an integral type. If so, it's not an iterator.
  446. template <class _InIter>
  447. void _M_splice_after_range(_Node_base* __pos,
  448. _InIter __first, _InIter __last) {
  449. typedef typename _IsIntegral<_InIter>::_Ret _Integral;
  450. _M_splice_after_range(__pos, __first, __last, _Integral());
  451. }
  452. template <class _Integer>
  453. void _M_splice_after_range(_Node_base* __pos, _Integer __n, _Integer __x,
  454. const __true_type&) {
  455. _M_insert_after_fill(__pos, __n, __x);
  456. }
  457. template <class _InIter>
  458. void _M_splice_after_range(_Node_base* __pos,
  459. _InIter __first, _InIter __last,
  460. const __false_type&) {
  461. #else /* _STLP_MEMBER_TEMPLATES */
  462. void _M_splice_after_range(_Node_base* __pos,
  463. const value_type* __first,
  464. const value_type* __last) {
  465. while (__first != __last) {
  466. __pos = _STLP_PRIV __slist_make_link(__pos, _M_create_node(*__first));
  467. ++__first;
  468. }
  469. }
  470. void _M_splice_after_range(_Node_base* __pos,
  471. const_iterator __first, const_iterator __last) {
  472. #endif /* _STLP_MEMBER_TEMPLATES */
  473. //We use a temporary slist to avoid the auto reference troubles (infinite loop)
  474. _Self __tmp(__first, __last, this->get_allocator());
  475. splice_after(iterator(__pos), __tmp);
  476. }
  477. #if defined (_STLP_MEMBER_TEMPLATES)
  478. // Check whether it's an integral type. If so, it's not an iterator.
  479. template <class _InIter>
  480. void _M_splice_range(_Node_base* __pos,
  481. _InIter __first, _InIter __last) {
  482. typedef typename _IsIntegral<_InIter>::_Ret _Integral;
  483. _M_splice_range(__pos, __first, __last, _Integral());
  484. }
  485. template <class _Integer>
  486. void _M_splice_range(_Node_base* __pos, _Integer __n, _Integer __x,
  487. const __true_type&) {
  488. _M_insert_after_fill(_STLP_PRIV _Sl_global_inst::__previous(&this->_M_head._M_data, __pos),
  489. __n, __x);
  490. }
  491. template <class _InIter>
  492. void _M_splice_range(_Node_base* __pos,
  493. _InIter __first, _InIter __last,
  494. const __false_type&) {
  495. #else /* _STLP_MEMBER_TEMPLATES */
  496. void _M_splice_range(_Node_base* __pos,
  497. const value_type* __first,
  498. const value_type* __last) {
  499. while (__first != __last) {
  500. __pos = _STLP_PRIV __slist_make_link(__pos, _M_create_node(*__first));
  501. ++__first;
  502. }
  503. }
  504. void _M_splice_range(_Node_base* __pos,
  505. const_iterator __first, const_iterator __last) {
  506. #endif /* _STLP_MEMBER_TEMPLATES */
  507. //We use a temporary slist to avoid the auto reference troubles (infinite loop)
  508. _Self __tmp(__first, __last, this->get_allocator());
  509. splice(iterator(__pos), __tmp);
  510. }
  511. public:
  512. #if !defined (_STLP_DONT_SUP_DFLT_PARAM)
  513. iterator insert_after(iterator __pos, const value_type& __x = _Tp()) {
  514. #else
  515. iterator insert_after(iterator __pos, const value_type& __x) {
  516. #endif /*_STLP_DONT_SUP_DFLT_PARAM*/
  517. return iterator(_M_insert_after(__pos._M_node, __x));
  518. }
  519. #if defined (_STLP_DONT_SUP_DFLT_PARAM)
  520. iterator insert_after(iterator __pos) {
  521. return insert_after(__pos, _STLP_DEFAULT_CONSTRUCTED(_Tp));
  522. }
  523. #endif /*_STLP_DONT_SUP_DFLT_PARAM*/
  524. void insert_after(iterator __pos, size_type __n, const value_type& __x) {
  525. _M_insert_after_fill(__pos._M_node, __n, __x);
  526. }
  527. #if defined (_STLP_MEMBER_TEMPLATES)
  528. // We don't need any dispatching tricks here, because _M_insert_after_range
  529. // already does them.
  530. template <class _InIter>
  531. void insert_after(iterator __pos, _InIter __first, _InIter __last) {
  532. #else /* _STLP_MEMBER_TEMPLATES */
  533. void insert_after(iterator __pos,
  534. const value_type* __first, const value_type* __last) {
  535. _M_insert_after_range(__pos._M_node, __first, __last);
  536. }
  537. void insert_after(iterator __pos,
  538. const_iterator __first, const_iterator __last) {
  539. #endif /* _STLP_MEMBER_TEMPLATES */
  540. _M_splice_after_range(__pos._M_node, __first, __last);
  541. }
  542. #if !defined (_STLP_DONT_SUP_DFLT_PARAM)
  543. iterator insert(iterator __pos, const value_type& __x = _Tp()) {
  544. #else
  545. iterator insert(iterator __pos, const value_type& __x) {
  546. #endif /*_STLP_DONT_SUP_DFLT_PARAM*/
  547. return iterator(_M_insert_after(_STLP_PRIV _Sl_global_inst::__previous(&this->_M_head._M_data, __pos._M_node),
  548. __x));
  549. }
  550. #if defined (_STLP_DONT_SUP_DFLT_PARAM)
  551. iterator insert(iterator __pos) {
  552. return iterator(_M_insert_after(_STLP_PRIV _Sl_global_inst::__previous(&this->_M_head._M_data, __pos._M_node),
  553. _STLP_DEFAULT_CONSTRUCTED(_Tp)));
  554. }
  555. #endif /*_STLP_DONT_SUP_DFLT_PARAM*/
  556. void insert(iterator __pos, size_type __n, const value_type& __x) {
  557. _M_insert_after_fill(_STLP_PRIV _Sl_global_inst::__previous(&this->_M_head._M_data, __pos._M_node), __n, __x);
  558. }
  559. #if defined (_STLP_MEMBER_TEMPLATES)
  560. // We don't need any dispatching tricks here, because _M_insert_after_range
  561. // already does them.
  562. template <class _InIter>
  563. void insert(iterator __pos, _InIter __first, _InIter __last) {
  564. #else /* _STLP_MEMBER_TEMPLATES */
  565. void insert(iterator __pos, const value_type* __first,
  566. const value_type* __last) {
  567. _M_insert_after_range(_STLP_PRIV _Sl_global_inst::__previous(&this->_M_head._M_data, __pos._M_node),
  568. __first, __last);
  569. }
  570. void insert(iterator __pos, const_iterator __first, const_iterator __last) {
  571. #endif /* _STLP_MEMBER_TEMPLATES */
  572. _M_splice_range(__pos._M_node, __first, __last);
  573. }
  574. public:
  575. iterator erase_after(iterator __pos)
  576. { return iterator(this->_M_erase_after(__pos._M_node)); }
  577. iterator erase_after(iterator __before_first, iterator __last)
  578. { return iterator(this->_M_erase_after(__before_first._M_node, __last._M_node)); }
  579. iterator erase(iterator __pos)
  580. { return iterator(this->_M_erase_after(_STLP_PRIV _Sl_global_inst::__previous(&this->_M_head._M_data, __pos._M_node))); }
  581. iterator erase(iterator __first, iterator __last)
  582. { return iterator(this->_M_erase_after(_STLP_PRIV _Sl_global_inst::__previous(&this->_M_head._M_data, __first._M_node), __last._M_node)); }
  583. #if !defined (_STLP_DONT_SUP_DFLT_PARAM)
  584. void resize(size_type new_size, const value_type& __x = _Tp());
  585. #else
  586. void resize(size_type new_size, const value_type& __x);
  587. #endif /*_STLP_DONT_SUP_DFLT_PARAM*/
  588. #if defined (_STLP_DONT_SUP_DFLT_PARAM)
  589. void resize(size_type new_size) { resize(new_size, _STLP_DEFAULT_CONSTRUCTED(_Tp)); }
  590. #endif /*_STLP_DONT_SUP_DFLT_PARAM*/
  591. void clear()
  592. { this->_M_erase_after(&this->_M_head._M_data, 0); }
  593. public:
  594. // Moves the range [__before_first + 1, __before_last + 1) to *this,
  595. // inserting it immediately after __pos. This is constant time.
  596. void splice_after(iterator __pos, _Self& __x,
  597. iterator __before_first, iterator __before_last) {
  598. if (__before_first != __before_last) {
  599. if (this->get_allocator() == __x.get_allocator()) {
  600. _STLP_PRIV _Sl_global_inst::__splice_after(__pos._M_node,
  601. __before_first._M_node, __before_last._M_node);
  602. }
  603. else {
  604. this->insert_after(__pos, iterator(__before_first._M_node->_M_next), iterator(__before_last._M_node->_M_next));
  605. __x.erase_after(__before_first, ++__before_last);
  606. }
  607. }
  608. }
  609. // Moves the element that follows __prev to *this, inserting it immediately
  610. // after __pos. This is constant time.
  611. void splice_after(iterator __pos, _Self& __x, iterator __prev) {
  612. if (this->get_allocator() == __x.get_allocator()) {
  613. _STLP_PRIV _Sl_global_inst::__splice_after(__pos._M_node,
  614. __prev._M_node, __prev._M_node->_M_next);
  615. }
  616. else {
  617. this->insert_after(__pos, __STATIC_CAST(_Node*, __prev._M_node->_M_next)->_M_data);
  618. __x.erase_after(__prev);
  619. }
  620. }
  621. // Removes all of the elements from the list __x to *this, inserting
  622. // them immediately after __pos. __x must not be *this. Complexity:
  623. // linear in __x.size().
  624. void splice_after(iterator __pos, _Self& __x) {
  625. if (this->get_allocator() == __x.get_allocator())
  626. _STLP_PRIV _Sl_global_inst::__splice_after(__pos._M_node, &__x._M_head._M_data);
  627. else {
  628. this->insert_after(__pos, __x.begin(), __x.end());
  629. __x.clear();
  630. }
  631. }
  632. // Linear in distance(begin(), __pos), and linear in __x.size().
  633. void splice(iterator __pos, _Self& __x) {
  634. if (__x._M_head._M_data._M_next) {
  635. if (this->get_allocator() == __x.get_allocator()) {
  636. _STLP_PRIV _Sl_global_inst::__splice_after(_STLP_PRIV _Sl_global_inst::__previous(&this->_M_head._M_data, __pos._M_node),
  637. &__x._M_head._M_data,
  638. _STLP_PRIV _Sl_global_inst::__previous(&__x._M_head._M_data, 0));
  639. }
  640. else {
  641. insert(__pos, __x.begin(), __x.end());
  642. __x.clear();
  643. }
  644. }
  645. }
  646. // Linear in distance(begin(), __pos), and in distance(__x.begin(), __i).
  647. void splice(iterator __pos, _Self& __x, iterator __i) {
  648. if (this->get_allocator() == __x.get_allocator()) {
  649. _STLP_PRIV _Sl_global_inst::__splice_after(_STLP_PRIV _Sl_global_inst::__previous(&this->_M_head._M_data, __pos._M_node),
  650. _STLP_PRIV _Sl_global_inst::__previous(&__x._M_head._M_data, __i._M_node),
  651. __i._M_node);
  652. }
  653. else {
  654. insert(__pos, *__i);
  655. __x.erase(__i);
  656. }
  657. }
  658. // Linear in distance(begin(), __pos), in distance(__x.begin(), __first),
  659. // and in distance(__first, __last).
  660. void splice(iterator __pos, _Self& __x, iterator __first, iterator __last) {
  661. if (__first != __last) {
  662. if (this->get_allocator() == __x.get_allocator()) {
  663. _STLP_PRIV _Sl_global_inst::__splice_after(_STLP_PRIV _Sl_global_inst::__previous(&this->_M_head._M_data, __pos._M_node),
  664. _STLP_PRIV _Sl_global_inst::__previous(&__x._M_head._M_data, __first._M_node),
  665. _STLP_PRIV _Sl_global_inst::__previous(__first._M_node, __last._M_node));
  666. }
  667. else {
  668. insert(__pos, __first, __last);
  669. __x.erase(__first, __last);
  670. }
  671. }
  672. }
  673. public:
  674. void reverse() {
  675. if (this->_M_head._M_data._M_next)
  676. this->_M_head._M_data._M_next = _STLP_PRIV _Sl_global_inst::__reverse(this->_M_head._M_data._M_next);
  677. }
  678. void remove(const _Tp& __val);
  679. void unique() { _STLP_PRIV _Slist_unique(*this, equal_to<value_type>()); }
  680. void merge(_Self& __x) { _STLP_PRIV _Slist_merge(*this, __x, less<value_type>()); }
  681. void sort() { _STLP_PRIV _Slist_sort(*this, less<value_type>()); }
  682. #if defined (_STLP_MEMBER_TEMPLATES)
  683. template <class _Predicate>
  684. void remove_if(_Predicate __pred) {
  685. _Node_base* __cur = &this->_M_head._M_data;
  686. while (__cur->_M_next) {
  687. if (__pred(__STATIC_CAST(_Node*, __cur->_M_next)->_M_data))
  688. this->_M_erase_after(__cur);
  689. else
  690. __cur = __cur->_M_next;
  691. }
  692. }
  693. template <class _BinaryPredicate>
  694. void unique(_BinaryPredicate __pred)
  695. { _STLP_PRIV _Slist_unique(*this, __pred); }
  696. template <class _StrictWeakOrdering>
  697. void merge(_Self& __x, _StrictWeakOrdering __comp)
  698. { _STLP_PRIV _Slist_merge(*this, __x, __comp); }
  699. template <class _StrictWeakOrdering>
  700. void sort(_StrictWeakOrdering __comp)
  701. { _STLP_PRIV _Slist_sort(*this, __comp); }
  702. #endif /* _STLP_MEMBER_TEMPLATES */
  703. };
  704. #if defined (slist)
  705. # undef slist
  706. _STLP_MOVE_TO_STD_NAMESPACE
  707. #endif
  708. _STLP_END_NAMESPACE
  709. #if !defined (_STLP_LINK_TIME_INSTANTIATION)
  710. # include <stl/_slist.c>
  711. #endif
  712. #if defined (_STLP_USE_PTR_SPECIALIZATIONS)
  713. # include <stl/pointers/_slist.h>
  714. #endif
  715. #if defined (_STLP_DEBUG)
  716. # include <stl/debug/_slist.h>
  717. #endif
  718. _STLP_BEGIN_NAMESPACE
  719. template <class _Tp, class _Alloc>
  720. inline bool _STLP_CALL
  721. operator == (const slist<_Tp,_Alloc>& _SL1, const slist<_Tp,_Alloc>& _SL2) {
  722. typedef typename slist<_Tp,_Alloc>::const_iterator const_iterator;
  723. const_iterator __end1 = _SL1.end();
  724. const_iterator __end2 = _SL2.end();
  725. const_iterator __i1 = _SL1.begin();
  726. const_iterator __i2 = _SL2.begin();
  727. while (__i1 != __end1 && __i2 != __end2 && *__i1 == *__i2) {
  728. ++__i1;
  729. ++__i2;
  730. }
  731. return __i1 == __end1 && __i2 == __end2;
  732. }
  733. #define _STLP_EQUAL_OPERATOR_SPECIALIZED
  734. #define _STLP_TEMPLATE_HEADER template <class _Tp, class _Alloc>
  735. #define _STLP_TEMPLATE_CONTAINER slist<_Tp, _Alloc>
  736. #include <stl/_relops_cont.h>
  737. #undef _STLP_TEMPLATE_CONTAINER
  738. #undef _STLP_TEMPLATE_HEADER
  739. #undef _STLP_EQUAL_OPERATOR_SPECIALIZED
  740. #if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
  741. # if !defined (_STLP_NO_MOVE_SEMANTIC)
  742. template <class _Tp, class _Alloc>
  743. struct __move_traits<slist<_Tp, _Alloc> > {
  744. typedef __true_type implemented;
  745. typedef typename __move_traits<_Alloc>::complete complete;
  746. };
  747. # endif
  748. // Specialization of insert_iterator so that insertions will be constant
  749. // time rather than linear time.
  750. template <class _Tp, class _Alloc>
  751. class insert_iterator<slist<_Tp, _Alloc> > {
  752. protected:
  753. typedef slist<_Tp, _Alloc> _Container;
  754. _Container* _M_container;
  755. typename _Container::iterator _M_iter;
  756. public:
  757. typedef _Container container_type;
  758. typedef output_iterator_tag iterator_category;
  759. typedef void value_type;
  760. typedef void difference_type;
  761. typedef void pointer;
  762. typedef void reference;
  763. insert_iterator(_Container& __x, typename _Container::iterator __i)
  764. : _M_container(&__x) {
  765. if (__i == __x.begin())
  766. _M_iter = __x.before_begin();
  767. else
  768. _M_iter = __x.previous(__i);
  769. }
  770. insert_iterator<_Container>&
  771. operator = (const typename _Container::value_type& __val) {
  772. _M_iter = _M_container->insert_after(_M_iter, __val);
  773. return *this;
  774. }
  775. insert_iterator<_Container>& operator*() { return *this; }
  776. insert_iterator<_Container>& operator++() { return *this; }
  777. insert_iterator<_Container>& operator++(int) { return *this; }
  778. };
  779. #endif /* _STLP_CLASS_PARTIAL_SPECIALIZATION */
  780. _STLP_END_NAMESPACE
  781. #endif /* _STLP_INTERNAL_SLIST_H */
  782. // Local Variables:
  783. // mode:C++
  784. // End: