slist 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082
  1. // Singly-linked list implementation -*- C++ -*-
  2. // Copyright (C) 2001-2020 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /*
  21. * Copyright (c) 1997
  22. * Silicon Graphics Computer Systems, Inc.
  23. *
  24. * Permission to use, copy, modify, distribute and sell this software
  25. * and its documentation for any purpose is hereby granted without fee,
  26. * provided that the above copyright notice appear in all copies and
  27. * that both that copyright notice and this permission notice appear
  28. * in supporting documentation. Silicon Graphics makes no
  29. * representations about the suitability of this software for any
  30. * purpose. It is provided "as is" without express or implied warranty.
  31. *
  32. */
  33. /** @file ext/slist
  34. * This file is a GNU extension to the Standard C++ Library (possibly
  35. * containing extensions from the HP/SGI STL subset).
  36. */
  37. #ifndef _SLIST
  38. #define _SLIST 1
  39. #include <algorithm>
  40. #include <bits/allocator.h>
  41. #include <bits/stl_construct.h>
  42. #include <bits/stl_uninitialized.h>
  43. #include <bits/concept_check.h>
  44. #include <ext/alloc_traits.h>
  45. namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
  46. {
  47. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  48. struct _Slist_node_base
  49. {
  50. _Slist_node_base* _M_next;
  51. };
  52. inline _Slist_node_base*
  53. __slist_make_link(_Slist_node_base* __prev_node,
  54. _Slist_node_base* __new_node)
  55. {
  56. __new_node->_M_next = __prev_node->_M_next;
  57. __prev_node->_M_next = __new_node;
  58. return __new_node;
  59. }
  60. inline _Slist_node_base*
  61. __slist_previous(_Slist_node_base* __head,
  62. const _Slist_node_base* __node)
  63. {
  64. while (__head && __head->_M_next != __node)
  65. __head = __head->_M_next;
  66. return __head;
  67. }
  68. inline const _Slist_node_base*
  69. __slist_previous(const _Slist_node_base* __head,
  70. const _Slist_node_base* __node)
  71. {
  72. while (__head && __head->_M_next != __node)
  73. __head = __head->_M_next;
  74. return __head;
  75. }
  76. inline void
  77. __slist_splice_after(_Slist_node_base* __pos,
  78. _Slist_node_base* __before_first,
  79. _Slist_node_base* __before_last)
  80. {
  81. if (__pos != __before_first && __pos != __before_last)
  82. {
  83. _Slist_node_base* __first = __before_first->_M_next;
  84. _Slist_node_base* __after = __pos->_M_next;
  85. __before_first->_M_next = __before_last->_M_next;
  86. __pos->_M_next = __first;
  87. __before_last->_M_next = __after;
  88. }
  89. }
  90. inline void
  91. __slist_splice_after(_Slist_node_base* __pos, _Slist_node_base* __head)
  92. {
  93. _Slist_node_base* __before_last = __slist_previous(__head, 0);
  94. if (__before_last != __head)
  95. {
  96. _Slist_node_base* __after = __pos->_M_next;
  97. __pos->_M_next = __head->_M_next;
  98. __head->_M_next = 0;
  99. __before_last->_M_next = __after;
  100. }
  101. }
  102. inline _Slist_node_base*
  103. __slist_reverse(_Slist_node_base* __node)
  104. {
  105. _Slist_node_base* __result = __node;
  106. __node = __node->_M_next;
  107. __result->_M_next = 0;
  108. while(__node)
  109. {
  110. _Slist_node_base* __next = __node->_M_next;
  111. __node->_M_next = __result;
  112. __result = __node;
  113. __node = __next;
  114. }
  115. return __result;
  116. }
  117. inline std::size_t
  118. __slist_size(_Slist_node_base* __node)
  119. {
  120. std::size_t __result = 0;
  121. for (; __node != 0; __node = __node->_M_next)
  122. ++__result;
  123. return __result;
  124. }
  125. template <class _Tp>
  126. struct _Slist_node : public _Slist_node_base
  127. {
  128. _Tp _M_data;
  129. };
  130. struct _Slist_iterator_base
  131. {
  132. typedef std::size_t size_type;
  133. typedef std::ptrdiff_t difference_type;
  134. typedef std::forward_iterator_tag iterator_category;
  135. _Slist_node_base* _M_node;
  136. _Slist_iterator_base(_Slist_node_base* __x)
  137. : _M_node(__x) {}
  138. void
  139. _M_incr()
  140. { _M_node = _M_node->_M_next; }
  141. bool
  142. operator==(const _Slist_iterator_base& __x) const
  143. { return _M_node == __x._M_node; }
  144. bool
  145. operator!=(const _Slist_iterator_base& __x) const
  146. { return _M_node != __x._M_node; }
  147. };
  148. template <class _Tp, class _Ref, class _Ptr>
  149. struct _Slist_iterator : public _Slist_iterator_base
  150. {
  151. typedef _Slist_iterator<_Tp, _Tp&, _Tp*> iterator;
  152. typedef _Slist_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
  153. typedef _Slist_iterator<_Tp, _Ref, _Ptr> _Self;
  154. typedef _Tp value_type;
  155. typedef _Ptr pointer;
  156. typedef _Ref reference;
  157. typedef _Slist_node<_Tp> _Node;
  158. explicit
  159. _Slist_iterator(_Node* __x)
  160. : _Slist_iterator_base(__x) {}
  161. _Slist_iterator()
  162. : _Slist_iterator_base(0) {}
  163. _Slist_iterator(const iterator& __x)
  164. : _Slist_iterator_base(__x._M_node) {}
  165. reference
  166. operator*() const
  167. { return ((_Node*) _M_node)->_M_data; }
  168. pointer
  169. operator->() const
  170. { return &(operator*()); }
  171. _Self&
  172. operator++()
  173. {
  174. _M_incr();
  175. return *this;
  176. }
  177. _Self
  178. operator++(int)
  179. {
  180. _Self __tmp = *this;
  181. _M_incr();
  182. return __tmp;
  183. }
  184. };
  185. template <class _Tp, class _Alloc>
  186. struct _Slist_base
  187. : public __alloc_traits<_Alloc>::template rebind<_Slist_node<_Tp> >::other
  188. {
  189. typedef typename __alloc_traits<_Alloc>::template
  190. rebind<_Slist_node<_Tp> >::other _Node_alloc;
  191. typedef _Alloc allocator_type;
  192. allocator_type
  193. get_allocator() const
  194. { return *static_cast<const _Node_alloc*>(this); }
  195. _Slist_base(const allocator_type& __a)
  196. : _Node_alloc(__a)
  197. { this->_M_head._M_next = 0; }
  198. ~_Slist_base()
  199. { _M_erase_after(&this->_M_head, 0); }
  200. protected:
  201. _Slist_node_base _M_head;
  202. _Slist_node<_Tp>*
  203. _M_get_node()
  204. { return _Node_alloc::allocate(1); }
  205. void
  206. _M_put_node(_Slist_node<_Tp>* __p)
  207. { _Node_alloc::deallocate(__p, 1); }
  208. protected:
  209. _Slist_node_base* _M_erase_after(_Slist_node_base* __pos)
  210. {
  211. _Slist_node<_Tp>* __next = (_Slist_node<_Tp>*) (__pos->_M_next);
  212. _Slist_node_base* __next_next = __next->_M_next;
  213. __pos->_M_next = __next_next;
  214. allocator_type __a = get_allocator();
  215. __alloc_traits<allocator_type>::destroy(__a, &__next->_M_data);
  216. _M_put_node(__next);
  217. return __next_next;
  218. }
  219. _Slist_node_base* _M_erase_after(_Slist_node_base*, _Slist_node_base*);
  220. };
  221. template <class _Tp, class _Alloc>
  222. _Slist_node_base*
  223. _Slist_base<_Tp,_Alloc>::_M_erase_after(_Slist_node_base* __before_first,
  224. _Slist_node_base* __last_node)
  225. {
  226. _Slist_node<_Tp>* __cur = (_Slist_node<_Tp>*) (__before_first->_M_next);
  227. while (__cur != __last_node)
  228. {
  229. _Slist_node<_Tp>* __tmp = __cur;
  230. __cur = (_Slist_node<_Tp>*) __cur->_M_next;
  231. allocator_type __a = get_allocator();
  232. __alloc_traits<allocator_type>::destroy(__a, &__tmp->_M_data);
  233. _M_put_node(__tmp);
  234. }
  235. __before_first->_M_next = __last_node;
  236. return __last_node;
  237. }
  238. /**
  239. * This is an SGI extension.
  240. * @ingroup SGIextensions
  241. * @doctodo
  242. */
  243. template <class _Tp, class _Alloc = std::allocator<_Tp> >
  244. class slist : private _Slist_base<_Tp,_Alloc>
  245. {
  246. // concept requirements
  247. __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
  248. private:
  249. typedef _Slist_base<_Tp,_Alloc> _Base;
  250. public:
  251. typedef _Tp value_type;
  252. typedef value_type* pointer;
  253. typedef const value_type* const_pointer;
  254. typedef value_type& reference;
  255. typedef const value_type& const_reference;
  256. typedef std::size_t size_type;
  257. typedef std::ptrdiff_t difference_type;
  258. typedef _Slist_iterator<_Tp, _Tp&, _Tp*> iterator;
  259. typedef _Slist_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
  260. typedef typename _Base::allocator_type allocator_type;
  261. allocator_type
  262. get_allocator() const
  263. { return _Base::get_allocator(); }
  264. private:
  265. typedef _Slist_node<_Tp> _Node;
  266. typedef _Slist_node_base _Node_base;
  267. typedef _Slist_iterator_base _Iterator_base;
  268. _Node*
  269. _M_create_node(const value_type& __x)
  270. {
  271. _Node* __node = this->_M_get_node();
  272. __try
  273. {
  274. allocator_type __a = get_allocator();
  275. __alloc_traits<allocator_type>::construct(__a, &__node->_M_data,
  276. __x);
  277. __node->_M_next = 0;
  278. }
  279. __catch(...)
  280. {
  281. this->_M_put_node(__node);
  282. __throw_exception_again;
  283. }
  284. return __node;
  285. }
  286. _Node*
  287. _M_create_node()
  288. {
  289. _Node* __node = this->_M_get_node();
  290. __try
  291. {
  292. allocator_type __a = get_allocator();
  293. __alloc_traits<allocator_type>::construct(__a, &__node->_M_data,
  294. value_type());
  295. __node->_M_next = 0;
  296. }
  297. __catch(...)
  298. {
  299. this->_M_put_node(__node);
  300. __throw_exception_again;
  301. }
  302. return __node;
  303. }
  304. public:
  305. explicit
  306. slist(const allocator_type& __a = allocator_type())
  307. : _Base(__a) {}
  308. slist(size_type __n, const value_type& __x,
  309. const allocator_type& __a = allocator_type())
  310. : _Base(__a)
  311. { _M_insert_after_fill(&this->_M_head, __n, __x); }
  312. explicit
  313. slist(size_type __n)
  314. : _Base(allocator_type())
  315. { _M_insert_after_fill(&this->_M_head, __n, value_type()); }
  316. // We don't need any dispatching tricks here, because
  317. // _M_insert_after_range already does them.
  318. template <class _InputIterator>
  319. slist(_InputIterator __first, _InputIterator __last,
  320. const allocator_type& __a = allocator_type())
  321. : _Base(__a)
  322. { _M_insert_after_range(&this->_M_head, __first, __last); }
  323. slist(const slist& __x)
  324. : _Base(__x.get_allocator())
  325. { _M_insert_after_range(&this->_M_head, __x.begin(), __x.end()); }
  326. slist&
  327. operator= (const slist& __x);
  328. ~slist() {}
  329. public:
  330. // assign(), a generalized assignment member function. Two
  331. // versions: one that takes a count, and one that takes a range.
  332. // The range version is a member template, so we dispatch on whether
  333. // or not the type is an integer.
  334. void
  335. assign(size_type __n, const _Tp& __val)
  336. { _M_fill_assign(__n, __val); }
  337. void
  338. _M_fill_assign(size_type __n, const _Tp& __val);
  339. template <class _InputIterator>
  340. void
  341. assign(_InputIterator __first, _InputIterator __last)
  342. {
  343. typedef typename std::__is_integer<_InputIterator>::__type _Integral;
  344. _M_assign_dispatch(__first, __last, _Integral());
  345. }
  346. template <class _Integer>
  347. void
  348. _M_assign_dispatch(_Integer __n, _Integer __val, std::__true_type)
  349. { _M_fill_assign((size_type) __n, (_Tp) __val); }
  350. template <class _InputIterator>
  351. void
  352. _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
  353. std::__false_type);
  354. public:
  355. iterator
  356. begin()
  357. { return iterator((_Node*)this->_M_head._M_next); }
  358. const_iterator
  359. begin() const
  360. { return const_iterator((_Node*)this->_M_head._M_next);}
  361. iterator
  362. end()
  363. { return iterator(0); }
  364. const_iterator
  365. end() const
  366. { return const_iterator(0); }
  367. // Experimental new feature: before_begin() returns a
  368. // non-dereferenceable iterator that, when incremented, yields
  369. // begin(). This iterator may be used as the argument to
  370. // insert_after, erase_after, etc. Note that even for an empty
  371. // slist, before_begin() is not the same iterator as end(). It
  372. // is always necessary to increment before_begin() at least once to
  373. // obtain end().
  374. iterator
  375. before_begin()
  376. { return iterator((_Node*) &this->_M_head); }
  377. const_iterator
  378. before_begin() const
  379. { return const_iterator((_Node*) &this->_M_head); }
  380. size_type
  381. size() const
  382. { return __slist_size(this->_M_head._M_next); }
  383. size_type
  384. max_size() const
  385. { return size_type(-1); }
  386. _GLIBCXX_NODISCARD bool
  387. empty() const
  388. { return this->_M_head._M_next == 0; }
  389. void
  390. swap(slist& __x)
  391. { std::swap(this->_M_head._M_next, __x._M_head._M_next); }
  392. public:
  393. reference
  394. front()
  395. { return ((_Node*) this->_M_head._M_next)->_M_data; }
  396. const_reference
  397. front() const
  398. { return ((_Node*) this->_M_head._M_next)->_M_data; }
  399. void
  400. push_front(const value_type& __x)
  401. { __slist_make_link(&this->_M_head, _M_create_node(__x)); }
  402. void
  403. push_front()
  404. { __slist_make_link(&this->_M_head, _M_create_node()); }
  405. void
  406. pop_front()
  407. {
  408. _Node* __node = (_Node*) this->_M_head._M_next;
  409. this->_M_head._M_next = __node->_M_next;
  410. allocator_type __a = get_allocator();
  411. __alloc_traits<allocator_type>::destroy(__a, &__node->_M_data);
  412. this->_M_put_node(__node);
  413. }
  414. iterator
  415. previous(const_iterator __pos)
  416. { return iterator((_Node*) __slist_previous(&this->_M_head,
  417. __pos._M_node)); }
  418. const_iterator
  419. previous(const_iterator __pos) const
  420. { return const_iterator((_Node*) __slist_previous(&this->_M_head,
  421. __pos._M_node)); }
  422. private:
  423. _Node*
  424. _M_insert_after(_Node_base* __pos, const value_type& __x)
  425. { return (_Node*) (__slist_make_link(__pos, _M_create_node(__x))); }
  426. _Node*
  427. _M_insert_after(_Node_base* __pos)
  428. { return (_Node*) (__slist_make_link(__pos, _M_create_node())); }
  429. void
  430. _M_insert_after_fill(_Node_base* __pos,
  431. size_type __n, const value_type& __x)
  432. {
  433. for (size_type __i = 0; __i < __n; ++__i)
  434. __pos = __slist_make_link(__pos, _M_create_node(__x));
  435. }
  436. // Check whether it's an integral type. If so, it's not an iterator.
  437. template <class _InIterator>
  438. void
  439. _M_insert_after_range(_Node_base* __pos,
  440. _InIterator __first, _InIterator __last)
  441. {
  442. typedef typename std::__is_integer<_InIterator>::__type _Integral;
  443. _M_insert_after_range(__pos, __first, __last, _Integral());
  444. }
  445. template <class _Integer>
  446. void
  447. _M_insert_after_range(_Node_base* __pos, _Integer __n, _Integer __x,
  448. std::__true_type)
  449. { _M_insert_after_fill(__pos, __n, __x); }
  450. template <class _InIterator>
  451. void
  452. _M_insert_after_range(_Node_base* __pos,
  453. _InIterator __first, _InIterator __last,
  454. std::__false_type)
  455. {
  456. while (__first != __last)
  457. {
  458. __pos = __slist_make_link(__pos, _M_create_node(*__first));
  459. ++__first;
  460. }
  461. }
  462. public:
  463. iterator
  464. insert_after(iterator __pos, const value_type& __x)
  465. { return iterator(_M_insert_after(__pos._M_node, __x)); }
  466. iterator
  467. insert_after(iterator __pos)
  468. { return insert_after(__pos, value_type()); }
  469. void
  470. insert_after(iterator __pos, size_type __n, const value_type& __x)
  471. { _M_insert_after_fill(__pos._M_node, __n, __x); }
  472. // We don't need any dispatching tricks here, because
  473. // _M_insert_after_range already does them.
  474. template <class _InIterator>
  475. void
  476. insert_after(iterator __pos, _InIterator __first, _InIterator __last)
  477. { _M_insert_after_range(__pos._M_node, __first, __last); }
  478. iterator
  479. insert(iterator __pos, const value_type& __x)
  480. { return iterator(_M_insert_after(__slist_previous(&this->_M_head,
  481. __pos._M_node),
  482. __x)); }
  483. iterator
  484. insert(iterator __pos)
  485. { return iterator(_M_insert_after(__slist_previous(&this->_M_head,
  486. __pos._M_node),
  487. value_type())); }
  488. void
  489. insert(iterator __pos, size_type __n, const value_type& __x)
  490. { _M_insert_after_fill(__slist_previous(&this->_M_head, __pos._M_node),
  491. __n, __x); }
  492. // We don't need any dispatching tricks here, because
  493. // _M_insert_after_range already does them.
  494. template <class _InIterator>
  495. void
  496. insert(iterator __pos, _InIterator __first, _InIterator __last)
  497. { _M_insert_after_range(__slist_previous(&this->_M_head, __pos._M_node),
  498. __first, __last); }
  499. public:
  500. iterator
  501. erase_after(iterator __pos)
  502. { return iterator((_Node*) this->_M_erase_after(__pos._M_node)); }
  503. iterator
  504. erase_after(iterator __before_first, iterator __last)
  505. {
  506. return iterator((_Node*) this->_M_erase_after(__before_first._M_node,
  507. __last._M_node));
  508. }
  509. iterator
  510. erase(iterator __pos)
  511. {
  512. return iterator((_Node*) this->_M_erase_after
  513. (__slist_previous(&this->_M_head, __pos._M_node)));
  514. }
  515. iterator
  516. erase(iterator __first, iterator __last)
  517. {
  518. return iterator((_Node*) this->_M_erase_after
  519. (__slist_previous(&this->_M_head, __first._M_node),
  520. __last._M_node));
  521. }
  522. void
  523. resize(size_type new_size, const _Tp& __x);
  524. void
  525. resize(size_type new_size)
  526. { resize(new_size, _Tp()); }
  527. void
  528. clear()
  529. { this->_M_erase_after(&this->_M_head, 0); }
  530. public:
  531. // Moves the range [__before_first + 1, __before_last + 1) to *this,
  532. // inserting it immediately after __pos. This is constant time.
  533. void
  534. splice_after(iterator __pos,
  535. iterator __before_first, iterator __before_last)
  536. {
  537. if (__before_first != __before_last)
  538. __slist_splice_after(__pos._M_node, __before_first._M_node,
  539. __before_last._M_node);
  540. }
  541. // Moves the element that follows __prev to *this, inserting it
  542. // immediately after __pos. This is constant time.
  543. void
  544. splice_after(iterator __pos, iterator __prev)
  545. { __slist_splice_after(__pos._M_node,
  546. __prev._M_node, __prev._M_node->_M_next); }
  547. // Removes all of the elements from the list __x to *this, inserting
  548. // them immediately after __pos. __x must not be *this. Complexity:
  549. // linear in __x.size().
  550. void
  551. splice_after(iterator __pos, slist& __x)
  552. { __slist_splice_after(__pos._M_node, &__x._M_head); }
  553. // Linear in distance(begin(), __pos), and linear in __x.size().
  554. void
  555. splice(iterator __pos, slist& __x)
  556. {
  557. if (__x._M_head._M_next)
  558. __slist_splice_after(__slist_previous(&this->_M_head, __pos._M_node),
  559. &__x._M_head,
  560. __slist_previous(&__x._M_head, 0)); }
  561. // Linear in distance(begin(), __pos), and in distance(__x.begin(), __i).
  562. void
  563. splice(iterator __pos, slist& __x, iterator __i)
  564. { __slist_splice_after(__slist_previous(&this->_M_head, __pos._M_node),
  565. __slist_previous(&__x._M_head, __i._M_node),
  566. __i._M_node); }
  567. // Linear in distance(begin(), __pos), in distance(__x.begin(), __first),
  568. // and in distance(__first, __last).
  569. void
  570. splice(iterator __pos, slist& __x, iterator __first, iterator __last)
  571. {
  572. if (__first != __last)
  573. __slist_splice_after(__slist_previous(&this->_M_head, __pos._M_node),
  574. __slist_previous(&__x._M_head, __first._M_node),
  575. __slist_previous(__first._M_node,
  576. __last._M_node));
  577. }
  578. public:
  579. void
  580. reverse()
  581. {
  582. if (this->_M_head._M_next)
  583. this->_M_head._M_next = __slist_reverse(this->_M_head._M_next);
  584. }
  585. void
  586. remove(const _Tp& __val);
  587. void
  588. unique();
  589. void
  590. merge(slist& __x);
  591. void
  592. sort();
  593. template <class _Predicate>
  594. void
  595. remove_if(_Predicate __pred);
  596. template <class _BinaryPredicate>
  597. void
  598. unique(_BinaryPredicate __pred);
  599. template <class _StrictWeakOrdering>
  600. void
  601. merge(slist&, _StrictWeakOrdering);
  602. template <class _StrictWeakOrdering>
  603. void
  604. sort(_StrictWeakOrdering __comp);
  605. };
  606. template <class _Tp, class _Alloc>
  607. slist<_Tp, _Alloc>&
  608. slist<_Tp, _Alloc>::operator=(const slist<_Tp, _Alloc>& __x)
  609. {
  610. if (&__x != this)
  611. {
  612. _Node_base* __p1 = &this->_M_head;
  613. _Node* __n1 = (_Node*) this->_M_head._M_next;
  614. const _Node* __n2 = (const _Node*) __x._M_head._M_next;
  615. while (__n1 && __n2)
  616. {
  617. __n1->_M_data = __n2->_M_data;
  618. __p1 = __n1;
  619. __n1 = (_Node*) __n1->_M_next;
  620. __n2 = (const _Node*) __n2->_M_next;
  621. }
  622. if (__n2 == 0)
  623. this->_M_erase_after(__p1, 0);
  624. else
  625. _M_insert_after_range(__p1, const_iterator((_Node*)__n2),
  626. const_iterator(0));
  627. }
  628. return *this;
  629. }
  630. template <class _Tp, class _Alloc>
  631. void
  632. slist<_Tp, _Alloc>::_M_fill_assign(size_type __n, const _Tp& __val)
  633. {
  634. _Node_base* __prev = &this->_M_head;
  635. _Node* __node = (_Node*) this->_M_head._M_next;
  636. for (; __node != 0 && __n > 0; --__n)
  637. {
  638. __node->_M_data = __val;
  639. __prev = __node;
  640. __node = (_Node*) __node->_M_next;
  641. }
  642. if (__n > 0)
  643. _M_insert_after_fill(__prev, __n, __val);
  644. else
  645. this->_M_erase_after(__prev, 0);
  646. }
  647. template <class _Tp, class _Alloc>
  648. template <class _InputIterator>
  649. void
  650. slist<_Tp, _Alloc>::_M_assign_dispatch(_InputIterator __first,
  651. _InputIterator __last,
  652. std::__false_type)
  653. {
  654. _Node_base* __prev = &this->_M_head;
  655. _Node* __node = (_Node*) this->_M_head._M_next;
  656. while (__node != 0 && __first != __last)
  657. {
  658. __node->_M_data = *__first;
  659. __prev = __node;
  660. __node = (_Node*) __node->_M_next;
  661. ++__first;
  662. }
  663. if (__first != __last)
  664. _M_insert_after_range(__prev, __first, __last);
  665. else
  666. this->_M_erase_after(__prev, 0);
  667. }
  668. template <class _Tp, class _Alloc>
  669. inline bool
  670. operator==(const slist<_Tp, _Alloc>& _SL1, const slist<_Tp, _Alloc>& _SL2)
  671. {
  672. typedef typename slist<_Tp,_Alloc>::const_iterator const_iterator;
  673. const_iterator __end1 = _SL1.end();
  674. const_iterator __end2 = _SL2.end();
  675. const_iterator __i1 = _SL1.begin();
  676. const_iterator __i2 = _SL2.begin();
  677. while (__i1 != __end1 && __i2 != __end2 && *__i1 == *__i2)
  678. {
  679. ++__i1;
  680. ++__i2;
  681. }
  682. return __i1 == __end1 && __i2 == __end2;
  683. }
  684. template <class _Tp, class _Alloc>
  685. inline bool
  686. operator<(const slist<_Tp, _Alloc>& _SL1, const slist<_Tp, _Alloc>& _SL2)
  687. { return std::lexicographical_compare(_SL1.begin(), _SL1.end(),
  688. _SL2.begin(), _SL2.end()); }
  689. template <class _Tp, class _Alloc>
  690. inline bool
  691. operator!=(const slist<_Tp, _Alloc>& _SL1, const slist<_Tp, _Alloc>& _SL2)
  692. { return !(_SL1 == _SL2); }
  693. template <class _Tp, class _Alloc>
  694. inline bool
  695. operator>(const slist<_Tp, _Alloc>& _SL1, const slist<_Tp, _Alloc>& _SL2)
  696. { return _SL2 < _SL1; }
  697. template <class _Tp, class _Alloc>
  698. inline bool
  699. operator<=(const slist<_Tp, _Alloc>& _SL1, const slist<_Tp, _Alloc>& _SL2)
  700. { return !(_SL2 < _SL1); }
  701. template <class _Tp, class _Alloc>
  702. inline bool
  703. operator>=(const slist<_Tp, _Alloc>& _SL1, const slist<_Tp, _Alloc>& _SL2)
  704. { return !(_SL1 < _SL2); }
  705. template <class _Tp, class _Alloc>
  706. inline void
  707. swap(slist<_Tp, _Alloc>& __x, slist<_Tp, _Alloc>& __y)
  708. { __x.swap(__y); }
  709. template <class _Tp, class _Alloc>
  710. void
  711. slist<_Tp, _Alloc>::resize(size_type __len, const _Tp& __x)
  712. {
  713. _Node_base* __cur = &this->_M_head;
  714. while (__cur->_M_next != 0 && __len > 0)
  715. {
  716. --__len;
  717. __cur = __cur->_M_next;
  718. }
  719. if (__cur->_M_next)
  720. this->_M_erase_after(__cur, 0);
  721. else
  722. _M_insert_after_fill(__cur, __len, __x);
  723. }
  724. template <class _Tp, class _Alloc>
  725. void
  726. slist<_Tp, _Alloc>::remove(const _Tp& __val)
  727. {
  728. _Node_base* __cur = &this->_M_head;
  729. while (__cur && __cur->_M_next)
  730. {
  731. if (((_Node*) __cur->_M_next)->_M_data == __val)
  732. this->_M_erase_after(__cur);
  733. else
  734. __cur = __cur->_M_next;
  735. }
  736. }
  737. template <class _Tp, class _Alloc>
  738. void
  739. slist<_Tp, _Alloc>::unique()
  740. {
  741. _Node_base* __cur = this->_M_head._M_next;
  742. if (__cur)
  743. {
  744. while (__cur->_M_next)
  745. {
  746. if (((_Node*)__cur)->_M_data
  747. == ((_Node*)(__cur->_M_next))->_M_data)
  748. this->_M_erase_after(__cur);
  749. else
  750. __cur = __cur->_M_next;
  751. }
  752. }
  753. }
  754. template <class _Tp, class _Alloc>
  755. void
  756. slist<_Tp, _Alloc>::merge(slist<_Tp, _Alloc>& __x)
  757. {
  758. _Node_base* __n1 = &this->_M_head;
  759. while (__n1->_M_next && __x._M_head._M_next)
  760. {
  761. if (((_Node*) __x._M_head._M_next)->_M_data
  762. < ((_Node*) __n1->_M_next)->_M_data)
  763. __slist_splice_after(__n1, &__x._M_head, __x._M_head._M_next);
  764. __n1 = __n1->_M_next;
  765. }
  766. if (__x._M_head._M_next)
  767. {
  768. __n1->_M_next = __x._M_head._M_next;
  769. __x._M_head._M_next = 0;
  770. }
  771. }
  772. template <class _Tp, class _Alloc>
  773. void
  774. slist<_Tp, _Alloc>::sort()
  775. {
  776. if (this->_M_head._M_next && this->_M_head._M_next->_M_next)
  777. {
  778. slist __carry;
  779. slist __counter[64];
  780. int __fill = 0;
  781. while (!empty())
  782. {
  783. __slist_splice_after(&__carry._M_head,
  784. &this->_M_head, this->_M_head._M_next);
  785. int __i = 0;
  786. while (__i < __fill && !__counter[__i].empty())
  787. {
  788. __counter[__i].merge(__carry);
  789. __carry.swap(__counter[__i]);
  790. ++__i;
  791. }
  792. __carry.swap(__counter[__i]);
  793. if (__i == __fill)
  794. ++__fill;
  795. }
  796. for (int __i = 1; __i < __fill; ++__i)
  797. __counter[__i].merge(__counter[__i-1]);
  798. this->swap(__counter[__fill-1]);
  799. }
  800. }
  801. template <class _Tp, class _Alloc>
  802. template <class _Predicate>
  803. void slist<_Tp, _Alloc>::remove_if(_Predicate __pred)
  804. {
  805. _Node_base* __cur = &this->_M_head;
  806. while (__cur->_M_next)
  807. {
  808. if (__pred(((_Node*) __cur->_M_next)->_M_data))
  809. this->_M_erase_after(__cur);
  810. else
  811. __cur = __cur->_M_next;
  812. }
  813. }
  814. template <class _Tp, class _Alloc>
  815. template <class _BinaryPredicate>
  816. void
  817. slist<_Tp, _Alloc>::unique(_BinaryPredicate __pred)
  818. {
  819. _Node* __cur = (_Node*) this->_M_head._M_next;
  820. if (__cur)
  821. {
  822. while (__cur->_M_next)
  823. {
  824. if (__pred(((_Node*)__cur)->_M_data,
  825. ((_Node*)(__cur->_M_next))->_M_data))
  826. this->_M_erase_after(__cur);
  827. else
  828. __cur = (_Node*) __cur->_M_next;
  829. }
  830. }
  831. }
  832. template <class _Tp, class _Alloc>
  833. template <class _StrictWeakOrdering>
  834. void
  835. slist<_Tp, _Alloc>::merge(slist<_Tp, _Alloc>& __x,
  836. _StrictWeakOrdering __comp)
  837. {
  838. _Node_base* __n1 = &this->_M_head;
  839. while (__n1->_M_next && __x._M_head._M_next)
  840. {
  841. if (__comp(((_Node*) __x._M_head._M_next)->_M_data,
  842. ((_Node*) __n1->_M_next)->_M_data))
  843. __slist_splice_after(__n1, &__x._M_head, __x._M_head._M_next);
  844. __n1 = __n1->_M_next;
  845. }
  846. if (__x._M_head._M_next)
  847. {
  848. __n1->_M_next = __x._M_head._M_next;
  849. __x._M_head._M_next = 0;
  850. }
  851. }
  852. template <class _Tp, class _Alloc>
  853. template <class _StrictWeakOrdering>
  854. void
  855. slist<_Tp, _Alloc>::sort(_StrictWeakOrdering __comp)
  856. {
  857. if (this->_M_head._M_next && this->_M_head._M_next->_M_next)
  858. {
  859. slist __carry;
  860. slist __counter[64];
  861. int __fill = 0;
  862. while (!empty())
  863. {
  864. __slist_splice_after(&__carry._M_head,
  865. &this->_M_head, this->_M_head._M_next);
  866. int __i = 0;
  867. while (__i < __fill && !__counter[__i].empty())
  868. {
  869. __counter[__i].merge(__carry, __comp);
  870. __carry.swap(__counter[__i]);
  871. ++__i;
  872. }
  873. __carry.swap(__counter[__i]);
  874. if (__i == __fill)
  875. ++__fill;
  876. }
  877. for (int __i = 1; __i < __fill; ++__i)
  878. __counter[__i].merge(__counter[__i-1], __comp);
  879. this->swap(__counter[__fill-1]);
  880. }
  881. }
  882. _GLIBCXX_END_NAMESPACE_VERSION
  883. } // namespace
  884. namespace std _GLIBCXX_VISIBILITY(default)
  885. {
  886. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  887. // Specialization of insert_iterator so that insertions will be constant
  888. // time rather than linear time.
  889. template <class _Tp, class _Alloc>
  890. class insert_iterator<__gnu_cxx::slist<_Tp, _Alloc> >
  891. {
  892. protected:
  893. typedef __gnu_cxx::slist<_Tp, _Alloc> _Container;
  894. _Container* container;
  895. typename _Container::iterator iter;
  896. public:
  897. typedef _Container container_type;
  898. typedef output_iterator_tag iterator_category;
  899. typedef void value_type;
  900. typedef void difference_type;
  901. typedef void pointer;
  902. typedef void reference;
  903. insert_iterator(_Container& __x, typename _Container::iterator __i)
  904. : container(&__x)
  905. {
  906. if (__i == __x.begin())
  907. iter = __x.before_begin();
  908. else
  909. iter = __x.previous(__i);
  910. }
  911. insert_iterator<_Container>&
  912. operator=(const typename _Container::value_type& __value)
  913. {
  914. iter = container->insert_after(iter, __value);
  915. return *this;
  916. }
  917. insert_iterator<_Container>&
  918. operator*()
  919. { return *this; }
  920. insert_iterator<_Container>&
  921. operator++()
  922. { return *this; }
  923. insert_iterator<_Container>&
  924. operator++(int)
  925. { return *this; }
  926. };
  927. _GLIBCXX_END_NAMESPACE_VERSION
  928. } // namespace
  929. #endif