slist 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082
  1. // Singly-linked list implementation -*- C++ -*-
  2. // Copyright (C) 2001-2018 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. namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
  45. {
  46. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  47. using std::size_t;
  48. using std::ptrdiff_t;
  49. using std::_Construct;
  50. using std::_Destroy;
  51. using std::allocator;
  52. using std::__true_type;
  53. using std::__false_type;
  54. struct _Slist_node_base
  55. {
  56. _Slist_node_base* _M_next;
  57. };
  58. inline _Slist_node_base*
  59. __slist_make_link(_Slist_node_base* __prev_node,
  60. _Slist_node_base* __new_node)
  61. {
  62. __new_node->_M_next = __prev_node->_M_next;
  63. __prev_node->_M_next = __new_node;
  64. return __new_node;
  65. }
  66. inline _Slist_node_base*
  67. __slist_previous(_Slist_node_base* __head,
  68. const _Slist_node_base* __node)
  69. {
  70. while (__head && __head->_M_next != __node)
  71. __head = __head->_M_next;
  72. return __head;
  73. }
  74. inline const _Slist_node_base*
  75. __slist_previous(const _Slist_node_base* __head,
  76. const _Slist_node_base* __node)
  77. {
  78. while (__head && __head->_M_next != __node)
  79. __head = __head->_M_next;
  80. return __head;
  81. }
  82. inline void
  83. __slist_splice_after(_Slist_node_base* __pos,
  84. _Slist_node_base* __before_first,
  85. _Slist_node_base* __before_last)
  86. {
  87. if (__pos != __before_first && __pos != __before_last)
  88. {
  89. _Slist_node_base* __first = __before_first->_M_next;
  90. _Slist_node_base* __after = __pos->_M_next;
  91. __before_first->_M_next = __before_last->_M_next;
  92. __pos->_M_next = __first;
  93. __before_last->_M_next = __after;
  94. }
  95. }
  96. inline void
  97. __slist_splice_after(_Slist_node_base* __pos, _Slist_node_base* __head)
  98. {
  99. _Slist_node_base* __before_last = __slist_previous(__head, 0);
  100. if (__before_last != __head)
  101. {
  102. _Slist_node_base* __after = __pos->_M_next;
  103. __pos->_M_next = __head->_M_next;
  104. __head->_M_next = 0;
  105. __before_last->_M_next = __after;
  106. }
  107. }
  108. inline _Slist_node_base*
  109. __slist_reverse(_Slist_node_base* __node)
  110. {
  111. _Slist_node_base* __result = __node;
  112. __node = __node->_M_next;
  113. __result->_M_next = 0;
  114. while(__node)
  115. {
  116. _Slist_node_base* __next = __node->_M_next;
  117. __node->_M_next = __result;
  118. __result = __node;
  119. __node = __next;
  120. }
  121. return __result;
  122. }
  123. inline size_t
  124. __slist_size(_Slist_node_base* __node)
  125. {
  126. size_t __result = 0;
  127. for (; __node != 0; __node = __node->_M_next)
  128. ++__result;
  129. return __result;
  130. }
  131. template <class _Tp>
  132. struct _Slist_node : public _Slist_node_base
  133. {
  134. _Tp _M_data;
  135. };
  136. struct _Slist_iterator_base
  137. {
  138. typedef size_t size_type;
  139. typedef ptrdiff_t difference_type;
  140. typedef std::forward_iterator_tag iterator_category;
  141. _Slist_node_base* _M_node;
  142. _Slist_iterator_base(_Slist_node_base* __x)
  143. : _M_node(__x) {}
  144. void
  145. _M_incr()
  146. { _M_node = _M_node->_M_next; }
  147. bool
  148. operator==(const _Slist_iterator_base& __x) const
  149. { return _M_node == __x._M_node; }
  150. bool
  151. operator!=(const _Slist_iterator_base& __x) const
  152. { return _M_node != __x._M_node; }
  153. };
  154. template <class _Tp, class _Ref, class _Ptr>
  155. struct _Slist_iterator : public _Slist_iterator_base
  156. {
  157. typedef _Slist_iterator<_Tp, _Tp&, _Tp*> iterator;
  158. typedef _Slist_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
  159. typedef _Slist_iterator<_Tp, _Ref, _Ptr> _Self;
  160. typedef _Tp value_type;
  161. typedef _Ptr pointer;
  162. typedef _Ref reference;
  163. typedef _Slist_node<_Tp> _Node;
  164. explicit
  165. _Slist_iterator(_Node* __x)
  166. : _Slist_iterator_base(__x) {}
  167. _Slist_iterator()
  168. : _Slist_iterator_base(0) {}
  169. _Slist_iterator(const iterator& __x)
  170. : _Slist_iterator_base(__x._M_node) {}
  171. reference
  172. operator*() const
  173. { return ((_Node*) _M_node)->_M_data; }
  174. pointer
  175. operator->() const
  176. { return &(operator*()); }
  177. _Self&
  178. operator++()
  179. {
  180. _M_incr();
  181. return *this;
  182. }
  183. _Self
  184. operator++(int)
  185. {
  186. _Self __tmp = *this;
  187. _M_incr();
  188. return __tmp;
  189. }
  190. };
  191. template <class _Tp, class _Alloc>
  192. struct _Slist_base
  193. : public _Alloc::template rebind<_Slist_node<_Tp> >::other
  194. {
  195. typedef typename _Alloc::template rebind<_Slist_node<_Tp> >::other
  196. _Node_alloc;
  197. typedef _Alloc allocator_type;
  198. allocator_type
  199. get_allocator() const
  200. { return *static_cast<const _Node_alloc*>(this); }
  201. _Slist_base(const allocator_type& __a)
  202. : _Node_alloc(__a)
  203. { this->_M_head._M_next = 0; }
  204. ~_Slist_base()
  205. { _M_erase_after(&this->_M_head, 0); }
  206. protected:
  207. _Slist_node_base _M_head;
  208. _Slist_node<_Tp>*
  209. _M_get_node()
  210. { return _Node_alloc::allocate(1); }
  211. void
  212. _M_put_node(_Slist_node<_Tp>* __p)
  213. { _Node_alloc::deallocate(__p, 1); }
  214. protected:
  215. _Slist_node_base* _M_erase_after(_Slist_node_base* __pos)
  216. {
  217. _Slist_node<_Tp>* __next = (_Slist_node<_Tp>*) (__pos->_M_next);
  218. _Slist_node_base* __next_next = __next->_M_next;
  219. __pos->_M_next = __next_next;
  220. get_allocator().destroy(&__next->_M_data);
  221. _M_put_node(__next);
  222. return __next_next;
  223. }
  224. _Slist_node_base* _M_erase_after(_Slist_node_base*, _Slist_node_base*);
  225. };
  226. template <class _Tp, class _Alloc>
  227. _Slist_node_base*
  228. _Slist_base<_Tp,_Alloc>::_M_erase_after(_Slist_node_base* __before_first,
  229. _Slist_node_base* __last_node)
  230. {
  231. _Slist_node<_Tp>* __cur = (_Slist_node<_Tp>*) (__before_first->_M_next);
  232. while (__cur != __last_node)
  233. {
  234. _Slist_node<_Tp>* __tmp = __cur;
  235. __cur = (_Slist_node<_Tp>*) __cur->_M_next;
  236. get_allocator().destroy(&__tmp->_M_data);
  237. _M_put_node(__tmp);
  238. }
  239. __before_first->_M_next = __last_node;
  240. return __last_node;
  241. }
  242. /**
  243. * This is an SGI extension.
  244. * @ingroup SGIextensions
  245. * @doctodo
  246. */
  247. template <class _Tp, class _Alloc = allocator<_Tp> >
  248. class slist : private _Slist_base<_Tp,_Alloc>
  249. {
  250. // concept requirements
  251. __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
  252. private:
  253. typedef _Slist_base<_Tp,_Alloc> _Base;
  254. public:
  255. typedef _Tp value_type;
  256. typedef value_type* pointer;
  257. typedef const value_type* const_pointer;
  258. typedef value_type& reference;
  259. typedef const value_type& const_reference;
  260. typedef size_t size_type;
  261. typedef ptrdiff_t difference_type;
  262. typedef _Slist_iterator<_Tp, _Tp&, _Tp*> iterator;
  263. typedef _Slist_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
  264. typedef typename _Base::allocator_type allocator_type;
  265. allocator_type
  266. get_allocator() const
  267. { return _Base::get_allocator(); }
  268. private:
  269. typedef _Slist_node<_Tp> _Node;
  270. typedef _Slist_node_base _Node_base;
  271. typedef _Slist_iterator_base _Iterator_base;
  272. _Node*
  273. _M_create_node(const value_type& __x)
  274. {
  275. _Node* __node = this->_M_get_node();
  276. __try
  277. {
  278. get_allocator().construct(&__node->_M_data, __x);
  279. __node->_M_next = 0;
  280. }
  281. __catch(...)
  282. {
  283. this->_M_put_node(__node);
  284. __throw_exception_again;
  285. }
  286. return __node;
  287. }
  288. _Node*
  289. _M_create_node()
  290. {
  291. _Node* __node = this->_M_get_node();
  292. __try
  293. {
  294. get_allocator().construct(&__node->_M_data, 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, __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. __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. 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. get_allocator().destroy(&__node->_M_data);
  411. this->_M_put_node(__node);
  412. }
  413. iterator
  414. previous(const_iterator __pos)
  415. { return iterator((_Node*) __slist_previous(&this->_M_head,
  416. __pos._M_node)); }
  417. const_iterator
  418. previous(const_iterator __pos) const
  419. { return const_iterator((_Node*) __slist_previous(&this->_M_head,
  420. __pos._M_node)); }
  421. private:
  422. _Node*
  423. _M_insert_after(_Node_base* __pos, const value_type& __x)
  424. { return (_Node*) (__slist_make_link(__pos, _M_create_node(__x))); }
  425. _Node*
  426. _M_insert_after(_Node_base* __pos)
  427. { return (_Node*) (__slist_make_link(__pos, _M_create_node())); }
  428. void
  429. _M_insert_after_fill(_Node_base* __pos,
  430. size_type __n, const value_type& __x)
  431. {
  432. for (size_type __i = 0; __i < __n; ++__i)
  433. __pos = __slist_make_link(__pos, _M_create_node(__x));
  434. }
  435. // Check whether it's an integral type. If so, it's not an iterator.
  436. template <class _InIterator>
  437. void
  438. _M_insert_after_range(_Node_base* __pos,
  439. _InIterator __first, _InIterator __last)
  440. {
  441. typedef typename std::__is_integer<_InIterator>::__type _Integral;
  442. _M_insert_after_range(__pos, __first, __last, _Integral());
  443. }
  444. template <class _Integer>
  445. void
  446. _M_insert_after_range(_Node_base* __pos, _Integer __n, _Integer __x,
  447. __true_type)
  448. { _M_insert_after_fill(__pos, __n, __x); }
  449. template <class _InIterator>
  450. void
  451. _M_insert_after_range(_Node_base* __pos,
  452. _InIterator __first, _InIterator __last,
  453. __false_type)
  454. {
  455. while (__first != __last)
  456. {
  457. __pos = __slist_make_link(__pos, _M_create_node(*__first));
  458. ++__first;
  459. }
  460. }
  461. public:
  462. iterator
  463. insert_after(iterator __pos, const value_type& __x)
  464. { return iterator(_M_insert_after(__pos._M_node, __x)); }
  465. iterator
  466. insert_after(iterator __pos)
  467. { return insert_after(__pos, value_type()); }
  468. void
  469. insert_after(iterator __pos, size_type __n, const value_type& __x)
  470. { _M_insert_after_fill(__pos._M_node, __n, __x); }
  471. // We don't need any dispatching tricks here, because
  472. // _M_insert_after_range already does them.
  473. template <class _InIterator>
  474. void
  475. insert_after(iterator __pos, _InIterator __first, _InIterator __last)
  476. { _M_insert_after_range(__pos._M_node, __first, __last); }
  477. iterator
  478. insert(iterator __pos, const value_type& __x)
  479. { return iterator(_M_insert_after(__slist_previous(&this->_M_head,
  480. __pos._M_node),
  481. __x)); }
  482. iterator
  483. insert(iterator __pos)
  484. { return iterator(_M_insert_after(__slist_previous(&this->_M_head,
  485. __pos._M_node),
  486. value_type())); }
  487. void
  488. insert(iterator __pos, size_type __n, const value_type& __x)
  489. { _M_insert_after_fill(__slist_previous(&this->_M_head, __pos._M_node),
  490. __n, __x); }
  491. // We don't need any dispatching tricks here, because
  492. // _M_insert_after_range already does them.
  493. template <class _InIterator>
  494. void
  495. insert(iterator __pos, _InIterator __first, _InIterator __last)
  496. { _M_insert_after_range(__slist_previous(&this->_M_head, __pos._M_node),
  497. __first, __last); }
  498. public:
  499. iterator
  500. erase_after(iterator __pos)
  501. { return iterator((_Node*) this->_M_erase_after(__pos._M_node)); }
  502. iterator
  503. erase_after(iterator __before_first, iterator __last)
  504. {
  505. return iterator((_Node*) this->_M_erase_after(__before_first._M_node,
  506. __last._M_node));
  507. }
  508. iterator
  509. erase(iterator __pos)
  510. {
  511. return iterator((_Node*) this->_M_erase_after
  512. (__slist_previous(&this->_M_head, __pos._M_node)));
  513. }
  514. iterator
  515. erase(iterator __first, iterator __last)
  516. {
  517. return iterator((_Node*) this->_M_erase_after
  518. (__slist_previous(&this->_M_head, __first._M_node),
  519. __last._M_node));
  520. }
  521. void
  522. resize(size_type new_size, const _Tp& __x);
  523. void
  524. resize(size_type new_size)
  525. { resize(new_size, _Tp()); }
  526. void
  527. clear()
  528. { this->_M_erase_after(&this->_M_head, 0); }
  529. public:
  530. // Moves the range [__before_first + 1, __before_last + 1) to *this,
  531. // inserting it immediately after __pos. This is constant time.
  532. void
  533. splice_after(iterator __pos,
  534. iterator __before_first, iterator __before_last)
  535. {
  536. if (__before_first != __before_last)
  537. __slist_splice_after(__pos._M_node, __before_first._M_node,
  538. __before_last._M_node);
  539. }
  540. // Moves the element that follows __prev to *this, inserting it
  541. // immediately after __pos. This is constant time.
  542. void
  543. splice_after(iterator __pos, iterator __prev)
  544. { __slist_splice_after(__pos._M_node,
  545. __prev._M_node, __prev._M_node->_M_next); }
  546. // Removes all of the elements from the list __x to *this, inserting
  547. // them immediately after __pos. __x must not be *this. Complexity:
  548. // linear in __x.size().
  549. void
  550. splice_after(iterator __pos, slist& __x)
  551. { __slist_splice_after(__pos._M_node, &__x._M_head); }
  552. // Linear in distance(begin(), __pos), and linear in __x.size().
  553. void
  554. splice(iterator __pos, slist& __x)
  555. {
  556. if (__x._M_head._M_next)
  557. __slist_splice_after(__slist_previous(&this->_M_head, __pos._M_node),
  558. &__x._M_head,
  559. __slist_previous(&__x._M_head, 0)); }
  560. // Linear in distance(begin(), __pos), and in distance(__x.begin(), __i).
  561. void
  562. splice(iterator __pos, slist& __x, iterator __i)
  563. { __slist_splice_after(__slist_previous(&this->_M_head, __pos._M_node),
  564. __slist_previous(&__x._M_head, __i._M_node),
  565. __i._M_node); }
  566. // Linear in distance(begin(), __pos), in distance(__x.begin(), __first),
  567. // and in distance(__first, __last).
  568. void
  569. splice(iterator __pos, slist& __x, iterator __first, iterator __last)
  570. {
  571. if (__first != __last)
  572. __slist_splice_after(__slist_previous(&this->_M_head, __pos._M_node),
  573. __slist_previous(&__x._M_head, __first._M_node),
  574. __slist_previous(__first._M_node,
  575. __last._M_node));
  576. }
  577. public:
  578. void
  579. reverse()
  580. {
  581. if (this->_M_head._M_next)
  582. this->_M_head._M_next = __slist_reverse(this->_M_head._M_next);
  583. }
  584. void
  585. remove(const _Tp& __val);
  586. void
  587. unique();
  588. void
  589. merge(slist& __x);
  590. void
  591. sort();
  592. template <class _Predicate>
  593. void
  594. remove_if(_Predicate __pred);
  595. template <class _BinaryPredicate>
  596. void
  597. unique(_BinaryPredicate __pred);
  598. template <class _StrictWeakOrdering>
  599. void
  600. merge(slist&, _StrictWeakOrdering);
  601. template <class _StrictWeakOrdering>
  602. void
  603. sort(_StrictWeakOrdering __comp);
  604. };
  605. template <class _Tp, class _Alloc>
  606. slist<_Tp, _Alloc>&
  607. slist<_Tp, _Alloc>::operator=(const slist<_Tp, _Alloc>& __x)
  608. {
  609. if (&__x != this)
  610. {
  611. _Node_base* __p1 = &this->_M_head;
  612. _Node* __n1 = (_Node*) this->_M_head._M_next;
  613. const _Node* __n2 = (const _Node*) __x._M_head._M_next;
  614. while (__n1 && __n2)
  615. {
  616. __n1->_M_data = __n2->_M_data;
  617. __p1 = __n1;
  618. __n1 = (_Node*) __n1->_M_next;
  619. __n2 = (const _Node*) __n2->_M_next;
  620. }
  621. if (__n2 == 0)
  622. this->_M_erase_after(__p1, 0);
  623. else
  624. _M_insert_after_range(__p1, const_iterator((_Node*)__n2),
  625. const_iterator(0));
  626. }
  627. return *this;
  628. }
  629. template <class _Tp, class _Alloc>
  630. void
  631. slist<_Tp, _Alloc>::_M_fill_assign(size_type __n, const _Tp& __val)
  632. {
  633. _Node_base* __prev = &this->_M_head;
  634. _Node* __node = (_Node*) this->_M_head._M_next;
  635. for (; __node != 0 && __n > 0; --__n)
  636. {
  637. __node->_M_data = __val;
  638. __prev = __node;
  639. __node = (_Node*) __node->_M_next;
  640. }
  641. if (__n > 0)
  642. _M_insert_after_fill(__prev, __n, __val);
  643. else
  644. this->_M_erase_after(__prev, 0);
  645. }
  646. template <class _Tp, class _Alloc>
  647. template <class _InputIterator>
  648. void
  649. slist<_Tp, _Alloc>::_M_assign_dispatch(_InputIterator __first,
  650. _InputIterator __last,
  651. __false_type)
  652. {
  653. _Node_base* __prev = &this->_M_head;
  654. _Node* __node = (_Node*) this->_M_head._M_next;
  655. while (__node != 0 && __first != __last)
  656. {
  657. __node->_M_data = *__first;
  658. __prev = __node;
  659. __node = (_Node*) __node->_M_next;
  660. ++__first;
  661. }
  662. if (__first != __last)
  663. _M_insert_after_range(__prev, __first, __last);
  664. else
  665. this->_M_erase_after(__prev, 0);
  666. }
  667. template <class _Tp, class _Alloc>
  668. inline bool
  669. operator==(const slist<_Tp, _Alloc>& _SL1, const slist<_Tp, _Alloc>& _SL2)
  670. {
  671. typedef typename slist<_Tp,_Alloc>::const_iterator const_iterator;
  672. const_iterator __end1 = _SL1.end();
  673. const_iterator __end2 = _SL2.end();
  674. const_iterator __i1 = _SL1.begin();
  675. const_iterator __i2 = _SL2.begin();
  676. while (__i1 != __end1 && __i2 != __end2 && *__i1 == *__i2)
  677. {
  678. ++__i1;
  679. ++__i2;
  680. }
  681. return __i1 == __end1 && __i2 == __end2;
  682. }
  683. template <class _Tp, class _Alloc>
  684. inline bool
  685. operator<(const slist<_Tp, _Alloc>& _SL1, const slist<_Tp, _Alloc>& _SL2)
  686. { return std::lexicographical_compare(_SL1.begin(), _SL1.end(),
  687. _SL2.begin(), _SL2.end()); }
  688. template <class _Tp, class _Alloc>
  689. inline bool
  690. operator!=(const slist<_Tp, _Alloc>& _SL1, const slist<_Tp, _Alloc>& _SL2)
  691. { return !(_SL1 == _SL2); }
  692. template <class _Tp, class _Alloc>
  693. inline bool
  694. operator>(const slist<_Tp, _Alloc>& _SL1, const slist<_Tp, _Alloc>& _SL2)
  695. { return _SL2 < _SL1; }
  696. template <class _Tp, class _Alloc>
  697. inline bool
  698. operator<=(const slist<_Tp, _Alloc>& _SL1, const slist<_Tp, _Alloc>& _SL2)
  699. { return !(_SL2 < _SL1); }
  700. template <class _Tp, class _Alloc>
  701. inline bool
  702. operator>=(const slist<_Tp, _Alloc>& _SL1, const slist<_Tp, _Alloc>& _SL2)
  703. { return !(_SL1 < _SL2); }
  704. template <class _Tp, class _Alloc>
  705. inline void
  706. swap(slist<_Tp, _Alloc>& __x, slist<_Tp, _Alloc>& __y)
  707. { __x.swap(__y); }
  708. template <class _Tp, class _Alloc>
  709. void
  710. slist<_Tp, _Alloc>::resize(size_type __len, const _Tp& __x)
  711. {
  712. _Node_base* __cur = &this->_M_head;
  713. while (__cur->_M_next != 0 && __len > 0)
  714. {
  715. --__len;
  716. __cur = __cur->_M_next;
  717. }
  718. if (__cur->_M_next)
  719. this->_M_erase_after(__cur, 0);
  720. else
  721. _M_insert_after_fill(__cur, __len, __x);
  722. }
  723. template <class _Tp, class _Alloc>
  724. void
  725. slist<_Tp, _Alloc>::remove(const _Tp& __val)
  726. {
  727. _Node_base* __cur = &this->_M_head;
  728. while (__cur && __cur->_M_next)
  729. {
  730. if (((_Node*) __cur->_M_next)->_M_data == __val)
  731. this->_M_erase_after(__cur);
  732. else
  733. __cur = __cur->_M_next;
  734. }
  735. }
  736. template <class _Tp, class _Alloc>
  737. void
  738. slist<_Tp, _Alloc>::unique()
  739. {
  740. _Node_base* __cur = this->_M_head._M_next;
  741. if (__cur)
  742. {
  743. while (__cur->_M_next)
  744. {
  745. if (((_Node*)__cur)->_M_data
  746. == ((_Node*)(__cur->_M_next))->_M_data)
  747. this->_M_erase_after(__cur);
  748. else
  749. __cur = __cur->_M_next;
  750. }
  751. }
  752. }
  753. template <class _Tp, class _Alloc>
  754. void
  755. slist<_Tp, _Alloc>::merge(slist<_Tp, _Alloc>& __x)
  756. {
  757. _Node_base* __n1 = &this->_M_head;
  758. while (__n1->_M_next && __x._M_head._M_next)
  759. {
  760. if (((_Node*) __x._M_head._M_next)->_M_data
  761. < ((_Node*) __n1->_M_next)->_M_data)
  762. __slist_splice_after(__n1, &__x._M_head, __x._M_head._M_next);
  763. __n1 = __n1->_M_next;
  764. }
  765. if (__x._M_head._M_next)
  766. {
  767. __n1->_M_next = __x._M_head._M_next;
  768. __x._M_head._M_next = 0;
  769. }
  770. }
  771. template <class _Tp, class _Alloc>
  772. void
  773. slist<_Tp, _Alloc>::sort()
  774. {
  775. if (this->_M_head._M_next && this->_M_head._M_next->_M_next)
  776. {
  777. slist __carry;
  778. slist __counter[64];
  779. int __fill = 0;
  780. while (!empty())
  781. {
  782. __slist_splice_after(&__carry._M_head,
  783. &this->_M_head, this->_M_head._M_next);
  784. int __i = 0;
  785. while (__i < __fill && !__counter[__i].empty())
  786. {
  787. __counter[__i].merge(__carry);
  788. __carry.swap(__counter[__i]);
  789. ++__i;
  790. }
  791. __carry.swap(__counter[__i]);
  792. if (__i == __fill)
  793. ++__fill;
  794. }
  795. for (int __i = 1; __i < __fill; ++__i)
  796. __counter[__i].merge(__counter[__i-1]);
  797. this->swap(__counter[__fill-1]);
  798. }
  799. }
  800. template <class _Tp, class _Alloc>
  801. template <class _Predicate>
  802. void slist<_Tp, _Alloc>::remove_if(_Predicate __pred)
  803. {
  804. _Node_base* __cur = &this->_M_head;
  805. while (__cur->_M_next)
  806. {
  807. if (__pred(((_Node*) __cur->_M_next)->_M_data))
  808. this->_M_erase_after(__cur);
  809. else
  810. __cur = __cur->_M_next;
  811. }
  812. }
  813. template <class _Tp, class _Alloc>
  814. template <class _BinaryPredicate>
  815. void
  816. slist<_Tp, _Alloc>::unique(_BinaryPredicate __pred)
  817. {
  818. _Node* __cur = (_Node*) this->_M_head._M_next;
  819. if (__cur)
  820. {
  821. while (__cur->_M_next)
  822. {
  823. if (__pred(((_Node*)__cur)->_M_data,
  824. ((_Node*)(__cur->_M_next))->_M_data))
  825. this->_M_erase_after(__cur);
  826. else
  827. __cur = (_Node*) __cur->_M_next;
  828. }
  829. }
  830. }
  831. template <class _Tp, class _Alloc>
  832. template <class _StrictWeakOrdering>
  833. void
  834. slist<_Tp, _Alloc>::merge(slist<_Tp, _Alloc>& __x,
  835. _StrictWeakOrdering __comp)
  836. {
  837. _Node_base* __n1 = &this->_M_head;
  838. while (__n1->_M_next && __x._M_head._M_next)
  839. {
  840. if (__comp(((_Node*) __x._M_head._M_next)->_M_data,
  841. ((_Node*) __n1->_M_next)->_M_data))
  842. __slist_splice_after(__n1, &__x._M_head, __x._M_head._M_next);
  843. __n1 = __n1->_M_next;
  844. }
  845. if (__x._M_head._M_next)
  846. {
  847. __n1->_M_next = __x._M_head._M_next;
  848. __x._M_head._M_next = 0;
  849. }
  850. }
  851. template <class _Tp, class _Alloc>
  852. template <class _StrictWeakOrdering>
  853. void
  854. slist<_Tp, _Alloc>::sort(_StrictWeakOrdering __comp)
  855. {
  856. if (this->_M_head._M_next && this->_M_head._M_next->_M_next)
  857. {
  858. slist __carry;
  859. slist __counter[64];
  860. int __fill = 0;
  861. while (!empty())
  862. {
  863. __slist_splice_after(&__carry._M_head,
  864. &this->_M_head, this->_M_head._M_next);
  865. int __i = 0;
  866. while (__i < __fill && !__counter[__i].empty())
  867. {
  868. __counter[__i].merge(__carry, __comp);
  869. __carry.swap(__counter[__i]);
  870. ++__i;
  871. }
  872. __carry.swap(__counter[__i]);
  873. if (__i == __fill)
  874. ++__fill;
  875. }
  876. for (int __i = 1; __i < __fill; ++__i)
  877. __counter[__i].merge(__counter[__i-1], __comp);
  878. this->swap(__counter[__fill-1]);
  879. }
  880. }
  881. _GLIBCXX_END_NAMESPACE_VERSION
  882. } // namespace
  883. namespace std _GLIBCXX_VISIBILITY(default)
  884. {
  885. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  886. // Specialization of insert_iterator so that insertions will be constant
  887. // time rather than linear time.
  888. template <class _Tp, class _Alloc>
  889. class insert_iterator<__gnu_cxx::slist<_Tp, _Alloc> >
  890. {
  891. protected:
  892. typedef __gnu_cxx::slist<_Tp, _Alloc> _Container;
  893. _Container* container;
  894. typename _Container::iterator iter;
  895. public:
  896. typedef _Container container_type;
  897. typedef output_iterator_tag iterator_category;
  898. typedef void value_type;
  899. typedef void difference_type;
  900. typedef void pointer;
  901. typedef void reference;
  902. insert_iterator(_Container& __x, typename _Container::iterator __i)
  903. : container(&__x)
  904. {
  905. if (__i == __x.begin())
  906. iter = __x.before_begin();
  907. else
  908. iter = __x.previous(__i);
  909. }
  910. insert_iterator<_Container>&
  911. operator=(const typename _Container::value_type& __value)
  912. {
  913. iter = container->insert_after(iter, __value);
  914. return *this;
  915. }
  916. insert_iterator<_Container>&
  917. operator*()
  918. { return *this; }
  919. insert_iterator<_Container>&
  920. operator++()
  921. { return *this; }
  922. insert_iterator<_Container>&
  923. operator++(int)
  924. { return *this; }
  925. };
  926. _GLIBCXX_END_NAMESPACE_VERSION
  927. } // namespace
  928. #endif