list 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  1. // Debugging list implementation -*- C++ -*-
  2. // Copyright (C) 2003-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. /** @file debug/list
  21. * This file is a GNU debug extension to the Standard C++ Library.
  22. */
  23. #ifndef _GLIBCXX_DEBUG_LIST
  24. #define _GLIBCXX_DEBUG_LIST 1
  25. #pragma GCC system_header
  26. #include <list>
  27. #include <debug/safe_sequence.h>
  28. #include <debug/safe_container.h>
  29. #include <debug/safe_iterator.h>
  30. namespace std _GLIBCXX_VISIBILITY(default)
  31. {
  32. namespace __debug
  33. {
  34. /// Class std::list with safety/checking/debug instrumentation.
  35. template<typename _Tp, typename _Allocator = std::allocator<_Tp> >
  36. class list
  37. : public __gnu_debug::_Safe_container<
  38. list<_Tp, _Allocator>, _Allocator,
  39. __gnu_debug::_Safe_node_sequence>,
  40. public _GLIBCXX_STD_C::list<_Tp, _Allocator>
  41. {
  42. typedef _GLIBCXX_STD_C::list<_Tp, _Allocator> _Base;
  43. typedef __gnu_debug::_Safe_container<
  44. list, _Allocator, __gnu_debug::_Safe_node_sequence> _Safe;
  45. typedef typename _Base::iterator _Base_iterator;
  46. typedef typename _Base::const_iterator _Base_const_iterator;
  47. typedef __gnu_debug::_Equal_to<_Base_const_iterator> _Equal;
  48. typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
  49. public:
  50. typedef typename _Base::reference reference;
  51. typedef typename _Base::const_reference const_reference;
  52. typedef __gnu_debug::_Safe_iterator<_Base_iterator, list>
  53. iterator;
  54. typedef __gnu_debug::_Safe_iterator<_Base_const_iterator, list>
  55. const_iterator;
  56. typedef typename _Base::size_type size_type;
  57. typedef typename _Base::difference_type difference_type;
  58. typedef _Tp value_type;
  59. typedef _Allocator allocator_type;
  60. typedef typename _Base::pointer pointer;
  61. typedef typename _Base::const_pointer const_pointer;
  62. typedef std::reverse_iterator<iterator> reverse_iterator;
  63. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  64. // 23.2.2.1 construct/copy/destroy:
  65. #if __cplusplus < 201103L
  66. list()
  67. : _Base() { }
  68. list(const list& __x)
  69. : _Base(__x) { }
  70. ~list() { }
  71. #else
  72. list() = default;
  73. list(const list&) = default;
  74. list(list&&) = default;
  75. list(initializer_list<value_type> __l,
  76. const allocator_type& __a = allocator_type())
  77. : _Base(__l, __a) { }
  78. ~list() = default;
  79. list(const list& __x, const allocator_type& __a)
  80. : _Base(__x, __a) { }
  81. list(list&& __x, const allocator_type& __a)
  82. : _Base(std::move(__x), __a) { }
  83. #endif
  84. explicit
  85. list(const _Allocator& __a) _GLIBCXX_NOEXCEPT
  86. : _Base(__a) { }
  87. #if __cplusplus >= 201103L
  88. explicit
  89. list(size_type __n, const allocator_type& __a = allocator_type())
  90. : _Base(__n, __a) { }
  91. list(size_type __n, const _Tp& __value,
  92. const _Allocator& __a = _Allocator())
  93. : _Base(__n, __value, __a) { }
  94. #else
  95. explicit
  96. list(size_type __n, const _Tp& __value = _Tp(),
  97. const _Allocator& __a = _Allocator())
  98. : _Base(__n, __value, __a) { }
  99. #endif
  100. #if __cplusplus >= 201103L
  101. template<class _InputIterator,
  102. typename = std::_RequireInputIter<_InputIterator>>
  103. #else
  104. template<class _InputIterator>
  105. #endif
  106. list(_InputIterator __first, _InputIterator __last,
  107. const _Allocator& __a = _Allocator())
  108. : _Base(__gnu_debug::__base(__gnu_debug::__check_valid_range(__first,
  109. __last)),
  110. __gnu_debug::__base(__last), __a)
  111. { }
  112. list(const _Base& __x)
  113. : _Base(__x) { }
  114. #if __cplusplus < 201103L
  115. list&
  116. operator=(const list& __x)
  117. {
  118. this->_M_safe() = __x;
  119. _M_base() = __x;
  120. return *this;
  121. }
  122. #else
  123. list&
  124. operator=(const list&) = default;
  125. list&
  126. operator=(list&&) = default;
  127. list&
  128. operator=(initializer_list<value_type> __l)
  129. {
  130. this->_M_invalidate_all();
  131. _M_base() = __l;
  132. return *this;
  133. }
  134. void
  135. assign(initializer_list<value_type> __l)
  136. {
  137. _Base::assign(__l);
  138. this->_M_invalidate_all();
  139. }
  140. #endif
  141. #if __cplusplus >= 201103L
  142. template<class _InputIterator,
  143. typename = std::_RequireInputIter<_InputIterator>>
  144. #else
  145. template<class _InputIterator>
  146. #endif
  147. void
  148. assign(_InputIterator __first, _InputIterator __last)
  149. {
  150. typename __gnu_debug::_Distance_traits<_InputIterator>::__type __dist;
  151. __glibcxx_check_valid_range2(__first, __last, __dist);
  152. if (__dist.second >= __gnu_debug::__dp_sign)
  153. _Base::assign(__gnu_debug::__unsafe(__first),
  154. __gnu_debug::__unsafe(__last));
  155. else
  156. _Base::assign(__first, __last);
  157. this->_M_invalidate_all();
  158. }
  159. void
  160. assign(size_type __n, const _Tp& __t)
  161. {
  162. _Base::assign(__n, __t);
  163. this->_M_invalidate_all();
  164. }
  165. using _Base::get_allocator;
  166. // iterators:
  167. iterator
  168. begin() _GLIBCXX_NOEXCEPT
  169. { return iterator(_Base::begin(), this); }
  170. const_iterator
  171. begin() const _GLIBCXX_NOEXCEPT
  172. { return const_iterator(_Base::begin(), this); }
  173. iterator
  174. end() _GLIBCXX_NOEXCEPT
  175. { return iterator(_Base::end(), this); }
  176. const_iterator
  177. end() const _GLIBCXX_NOEXCEPT
  178. { return const_iterator(_Base::end(), this); }
  179. reverse_iterator
  180. rbegin() _GLIBCXX_NOEXCEPT
  181. { return reverse_iterator(end()); }
  182. const_reverse_iterator
  183. rbegin() const _GLIBCXX_NOEXCEPT
  184. { return const_reverse_iterator(end()); }
  185. reverse_iterator
  186. rend() _GLIBCXX_NOEXCEPT
  187. { return reverse_iterator(begin()); }
  188. const_reverse_iterator
  189. rend() const _GLIBCXX_NOEXCEPT
  190. { return const_reverse_iterator(begin()); }
  191. #if __cplusplus >= 201103L
  192. const_iterator
  193. cbegin() const noexcept
  194. { return const_iterator(_Base::begin(), this); }
  195. const_iterator
  196. cend() const noexcept
  197. { return const_iterator(_Base::end(), this); }
  198. const_reverse_iterator
  199. crbegin() const noexcept
  200. { return const_reverse_iterator(end()); }
  201. const_reverse_iterator
  202. crend() const noexcept
  203. { return const_reverse_iterator(begin()); }
  204. #endif
  205. // 23.2.2.2 capacity:
  206. using _Base::empty;
  207. using _Base::size;
  208. using _Base::max_size;
  209. #if __cplusplus >= 201103L
  210. void
  211. resize(size_type __sz)
  212. {
  213. this->_M_detach_singular();
  214. // if __sz < size(), invalidate all iterators in [begin + __sz, end())
  215. _Base_iterator __victim = _Base::begin();
  216. _Base_iterator __end = _Base::end();
  217. for (size_type __i = __sz; __victim != __end && __i > 0; --__i)
  218. ++__victim;
  219. for (; __victim != __end; ++__victim)
  220. this->_M_invalidate_if(_Equal(__victim));
  221. __try
  222. {
  223. _Base::resize(__sz);
  224. }
  225. __catch(...)
  226. {
  227. this->_M_revalidate_singular();
  228. __throw_exception_again;
  229. }
  230. }
  231. void
  232. resize(size_type __sz, const _Tp& __c)
  233. {
  234. this->_M_detach_singular();
  235. // if __sz < size(), invalidate all iterators in [begin + __sz, end())
  236. _Base_iterator __victim = _Base::begin();
  237. _Base_iterator __end = _Base::end();
  238. for (size_type __i = __sz; __victim != __end && __i > 0; --__i)
  239. ++__victim;
  240. for (; __victim != __end; ++__victim)
  241. this->_M_invalidate_if(_Equal(__victim));
  242. __try
  243. {
  244. _Base::resize(__sz, __c);
  245. }
  246. __catch(...)
  247. {
  248. this->_M_revalidate_singular();
  249. __throw_exception_again;
  250. }
  251. }
  252. #else
  253. void
  254. resize(size_type __sz, _Tp __c = _Tp())
  255. {
  256. this->_M_detach_singular();
  257. // if __sz < size(), invalidate all iterators in [begin + __sz, end())
  258. _Base_iterator __victim = _Base::begin();
  259. _Base_iterator __end = _Base::end();
  260. for (size_type __i = __sz; __victim != __end && __i > 0; --__i)
  261. ++__victim;
  262. for (; __victim != __end; ++__victim)
  263. this->_M_invalidate_if(_Equal(__victim));
  264. __try
  265. {
  266. _Base::resize(__sz, __c);
  267. }
  268. __catch(...)
  269. {
  270. this->_M_revalidate_singular();
  271. __throw_exception_again;
  272. }
  273. }
  274. #endif
  275. // element access:
  276. reference
  277. front() _GLIBCXX_NOEXCEPT
  278. {
  279. __glibcxx_check_nonempty();
  280. return _Base::front();
  281. }
  282. const_reference
  283. front() const _GLIBCXX_NOEXCEPT
  284. {
  285. __glibcxx_check_nonempty();
  286. return _Base::front();
  287. }
  288. reference
  289. back() _GLIBCXX_NOEXCEPT
  290. {
  291. __glibcxx_check_nonempty();
  292. return _Base::back();
  293. }
  294. const_reference
  295. back() const _GLIBCXX_NOEXCEPT
  296. {
  297. __glibcxx_check_nonempty();
  298. return _Base::back();
  299. }
  300. // 23.2.2.3 modifiers:
  301. using _Base::push_front;
  302. #if __cplusplus >= 201103L
  303. using _Base::emplace_front;
  304. #endif
  305. void
  306. pop_front() _GLIBCXX_NOEXCEPT
  307. {
  308. __glibcxx_check_nonempty();
  309. this->_M_invalidate_if(_Equal(_Base::begin()));
  310. _Base::pop_front();
  311. }
  312. using _Base::push_back;
  313. #if __cplusplus >= 201103L
  314. using _Base::emplace_back;
  315. #endif
  316. void
  317. pop_back() _GLIBCXX_NOEXCEPT
  318. {
  319. __glibcxx_check_nonempty();
  320. this->_M_invalidate_if(_Equal(--_Base::end()));
  321. _Base::pop_back();
  322. }
  323. #if __cplusplus >= 201103L
  324. template<typename... _Args>
  325. iterator
  326. emplace(const_iterator __position, _Args&&... __args)
  327. {
  328. __glibcxx_check_insert(__position);
  329. return iterator(_Base::emplace(__position.base(),
  330. std::forward<_Args>(__args)...), this);
  331. }
  332. #endif
  333. iterator
  334. #if __cplusplus >= 201103L
  335. insert(const_iterator __position, const _Tp& __x)
  336. #else
  337. insert(iterator __position, const _Tp& __x)
  338. #endif
  339. {
  340. __glibcxx_check_insert(__position);
  341. return iterator(_Base::insert(__position.base(), __x), this);
  342. }
  343. #if __cplusplus >= 201103L
  344. iterator
  345. insert(const_iterator __position, _Tp&& __x)
  346. { return emplace(__position, std::move(__x)); }
  347. iterator
  348. insert(const_iterator __p, initializer_list<value_type> __l)
  349. {
  350. __glibcxx_check_insert(__p);
  351. return iterator(_Base::insert(__p.base(), __l), this);
  352. }
  353. #endif
  354. #if __cplusplus >= 201103L
  355. iterator
  356. insert(const_iterator __position, size_type __n, const _Tp& __x)
  357. {
  358. __glibcxx_check_insert(__position);
  359. return iterator(_Base::insert(__position.base(), __n, __x), this);
  360. }
  361. #else
  362. void
  363. insert(iterator __position, size_type __n, const _Tp& __x)
  364. {
  365. __glibcxx_check_insert(__position);
  366. _Base::insert(__position.base(), __n, __x);
  367. }
  368. #endif
  369. #if __cplusplus >= 201103L
  370. template<class _InputIterator,
  371. typename = std::_RequireInputIter<_InputIterator>>
  372. iterator
  373. insert(const_iterator __position, _InputIterator __first,
  374. _InputIterator __last)
  375. {
  376. typename __gnu_debug::_Distance_traits<_InputIterator>::__type __dist;
  377. __glibcxx_check_insert_range(__position, __first, __last, __dist);
  378. if (__dist.second >= __gnu_debug::__dp_sign)
  379. return
  380. {
  381. _Base::insert(__position.base(),
  382. __gnu_debug::__unsafe(__first),
  383. __gnu_debug::__unsafe(__last)),
  384. this
  385. };
  386. else
  387. return { _Base::insert(__position.base(), __first, __last), this };
  388. }
  389. #else
  390. template<class _InputIterator>
  391. void
  392. insert(iterator __position, _InputIterator __first,
  393. _InputIterator __last)
  394. {
  395. typename __gnu_debug::_Distance_traits<_InputIterator>::__type __dist;
  396. __glibcxx_check_insert_range(__position, __first, __last, __dist);
  397. if (__dist.second >= __gnu_debug::__dp_sign)
  398. _Base::insert(__position.base(), __gnu_debug::__unsafe(__first),
  399. __gnu_debug::__unsafe(__last));
  400. else
  401. _Base::insert(__position.base(), __first, __last);
  402. }
  403. #endif
  404. private:
  405. _Base_iterator
  406. #if __cplusplus >= 201103L
  407. _M_erase(_Base_const_iterator __position) noexcept
  408. #else
  409. _M_erase(_Base_iterator __position)
  410. #endif
  411. {
  412. this->_M_invalidate_if(_Equal(__position));
  413. return _Base::erase(__position);
  414. }
  415. public:
  416. iterator
  417. #if __cplusplus >= 201103L
  418. erase(const_iterator __position) noexcept
  419. #else
  420. erase(iterator __position)
  421. #endif
  422. {
  423. __glibcxx_check_erase(__position);
  424. return iterator(_M_erase(__position.base()), this);
  425. }
  426. iterator
  427. #if __cplusplus >= 201103L
  428. erase(const_iterator __first, const_iterator __last) noexcept
  429. #else
  430. erase(iterator __first, iterator __last)
  431. #endif
  432. {
  433. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  434. // 151. can't currently clear() empty container
  435. __glibcxx_check_erase_range(__first, __last);
  436. for (_Base_const_iterator __victim = __first.base();
  437. __victim != __last.base(); ++__victim)
  438. {
  439. _GLIBCXX_DEBUG_VERIFY(__victim != _Base::end(),
  440. _M_message(__gnu_debug::__msg_valid_range)
  441. ._M_iterator(__first, "position")
  442. ._M_iterator(__last, "last"));
  443. this->_M_invalidate_if(_Equal(__victim));
  444. }
  445. return iterator(_Base::erase(__first.base(), __last.base()), this);
  446. }
  447. void
  448. swap(list& __x)
  449. _GLIBCXX_NOEXCEPT_IF( noexcept(declval<_Base&>().swap(__x)) )
  450. {
  451. _Safe::_M_swap(__x);
  452. _Base::swap(__x);
  453. }
  454. void
  455. clear() _GLIBCXX_NOEXCEPT
  456. {
  457. _Base::clear();
  458. this->_M_invalidate_all();
  459. }
  460. // 23.2.2.4 list operations:
  461. void
  462. #if __cplusplus >= 201103L
  463. splice(const_iterator __position, list&& __x) noexcept
  464. #else
  465. splice(iterator __position, list& __x)
  466. #endif
  467. {
  468. _GLIBCXX_DEBUG_VERIFY(std::__addressof(__x) != this,
  469. _M_message(__gnu_debug::__msg_self_splice)
  470. ._M_sequence(*this, "this"));
  471. this->_M_transfer_from_if(__x, _Not_equal(__x._M_base().end()));
  472. _Base::splice(__position.base(), _GLIBCXX_MOVE(__x._M_base()));
  473. }
  474. #if __cplusplus >= 201103L
  475. void
  476. splice(const_iterator __position, list& __x) noexcept
  477. { splice(__position, std::move(__x)); }
  478. #endif
  479. void
  480. #if __cplusplus >= 201103L
  481. splice(const_iterator __position, list&& __x, const_iterator __i) noexcept
  482. #else
  483. splice(iterator __position, list& __x, iterator __i)
  484. #endif
  485. {
  486. __glibcxx_check_insert(__position);
  487. // We used to perform the splice_alloc check: not anymore, redundant
  488. // after implementing the relevant bits of N1599.
  489. _GLIBCXX_DEBUG_VERIFY(__i._M_dereferenceable(),
  490. _M_message(__gnu_debug::__msg_splice_bad)
  491. ._M_iterator(__i, "__i"));
  492. _GLIBCXX_DEBUG_VERIFY(__i._M_attached_to(std::__addressof(__x)),
  493. _M_message(__gnu_debug::__msg_splice_other)
  494. ._M_iterator(__i, "__i")._M_sequence(__x, "__x"));
  495. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  496. // 250. splicing invalidates iterators
  497. this->_M_transfer_from_if(__x, _Equal(__i.base()));
  498. _Base::splice(__position.base(), _GLIBCXX_MOVE(__x._M_base()),
  499. __i.base());
  500. }
  501. #if __cplusplus >= 201103L
  502. void
  503. splice(const_iterator __position, list& __x, const_iterator __i) noexcept
  504. { splice(__position, std::move(__x), __i); }
  505. #endif
  506. void
  507. #if __cplusplus >= 201103L
  508. splice(const_iterator __position, list&& __x, const_iterator __first,
  509. const_iterator __last) noexcept
  510. #else
  511. splice(iterator __position, list& __x, iterator __first,
  512. iterator __last)
  513. #endif
  514. {
  515. __glibcxx_check_insert(__position);
  516. __glibcxx_check_valid_range(__first, __last);
  517. _GLIBCXX_DEBUG_VERIFY(__first._M_attached_to(std::__addressof(__x)),
  518. _M_message(__gnu_debug::__msg_splice_other)
  519. ._M_sequence(__x, "x")
  520. ._M_iterator(__first, "first"));
  521. // We used to perform the splice_alloc check: not anymore, redundant
  522. // after implementing the relevant bits of N1599.
  523. for (_Base_const_iterator __tmp = __first.base();
  524. __tmp != __last.base(); ++__tmp)
  525. {
  526. _GLIBCXX_DEBUG_VERIFY(__tmp != _Base::end(),
  527. _M_message(__gnu_debug::__msg_valid_range)
  528. ._M_iterator(__first, "first")
  529. ._M_iterator(__last, "last"));
  530. _GLIBCXX_DEBUG_VERIFY(std::__addressof(__x) != this
  531. || __tmp != __position.base(),
  532. _M_message(__gnu_debug::__msg_splice_overlap)
  533. ._M_iterator(__tmp, "position")
  534. ._M_iterator(__first, "first")
  535. ._M_iterator(__last, "last"));
  536. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  537. // 250. splicing invalidates iterators
  538. this->_M_transfer_from_if(__x, _Equal(__tmp));
  539. }
  540. _Base::splice(__position.base(), _GLIBCXX_MOVE(__x._M_base()),
  541. __first.base(), __last.base());
  542. }
  543. #if __cplusplus >= 201103L
  544. void
  545. splice(const_iterator __position, list& __x,
  546. const_iterator __first, const_iterator __last) noexcept
  547. { splice(__position, std::move(__x), __first, __last); }
  548. #endif
  549. void
  550. remove(const _Tp& __value)
  551. {
  552. for (_Base_iterator __x = _Base::begin(); __x != _Base::end(); )
  553. {
  554. if (*__x == __value)
  555. __x = _M_erase(__x);
  556. else
  557. ++__x;
  558. }
  559. }
  560. template<class _Predicate>
  561. void
  562. remove_if(_Predicate __pred)
  563. {
  564. for (_Base_iterator __x = _Base::begin(); __x != _Base::end(); )
  565. {
  566. if (__pred(*__x))
  567. __x = _M_erase(__x);
  568. else
  569. ++__x;
  570. }
  571. }
  572. void
  573. unique()
  574. {
  575. _Base_iterator __first = _Base::begin();
  576. _Base_iterator __last = _Base::end();
  577. if (__first == __last)
  578. return;
  579. _Base_iterator __next = __first; ++__next;
  580. while (__next != __last)
  581. {
  582. if (*__first == *__next)
  583. __next = _M_erase(__next);
  584. else
  585. __first = __next++;
  586. }
  587. }
  588. template<class _BinaryPredicate>
  589. void
  590. unique(_BinaryPredicate __binary_pred)
  591. {
  592. _Base_iterator __first = _Base::begin();
  593. _Base_iterator __last = _Base::end();
  594. if (__first == __last)
  595. return;
  596. _Base_iterator __next = __first; ++__next;
  597. while (__next != __last)
  598. {
  599. if (__binary_pred(*__first, *__next))
  600. __next = _M_erase(__next);
  601. else
  602. __first = __next++;
  603. }
  604. }
  605. void
  606. #if __cplusplus >= 201103L
  607. merge(list&& __x)
  608. #else
  609. merge(list& __x)
  610. #endif
  611. {
  612. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  613. // 300. list::merge() specification incomplete
  614. if (this != std::__addressof(__x))
  615. {
  616. __glibcxx_check_sorted(_Base::begin(), _Base::end());
  617. __glibcxx_check_sorted(__x.begin().base(), __x.end().base());
  618. this->_M_transfer_from_if(__x, _Not_equal(__x._M_base().end()));
  619. _Base::merge(_GLIBCXX_MOVE(__x._M_base()));
  620. }
  621. }
  622. #if __cplusplus >= 201103L
  623. void
  624. merge(list& __x)
  625. { merge(std::move(__x)); }
  626. #endif
  627. template<class _Compare>
  628. void
  629. #if __cplusplus >= 201103L
  630. merge(list&& __x, _Compare __comp)
  631. #else
  632. merge(list& __x, _Compare __comp)
  633. #endif
  634. {
  635. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  636. // 300. list::merge() specification incomplete
  637. if (this != std::__addressof(__x))
  638. {
  639. __glibcxx_check_sorted_pred(_Base::begin(), _Base::end(),
  640. __comp);
  641. __glibcxx_check_sorted_pred(__x.begin().base(), __x.end().base(),
  642. __comp);
  643. this->_M_transfer_from_if(__x, _Not_equal(__x._M_base().end()));
  644. _Base::merge(_GLIBCXX_MOVE(__x._M_base()), __comp);
  645. }
  646. }
  647. #if __cplusplus >= 201103L
  648. template<typename _Compare>
  649. void
  650. merge(list& __x, _Compare __comp)
  651. { merge(std::move(__x), __comp); }
  652. #endif
  653. void
  654. sort() { _Base::sort(); }
  655. template<typename _StrictWeakOrdering>
  656. void
  657. sort(_StrictWeakOrdering __pred) { _Base::sort(__pred); }
  658. using _Base::reverse;
  659. _Base&
  660. _M_base() _GLIBCXX_NOEXCEPT { return *this; }
  661. const _Base&
  662. _M_base() const _GLIBCXX_NOEXCEPT { return *this; }
  663. };
  664. #if __cpp_deduction_guides >= 201606
  665. template<typename _InputIterator, typename _ValT
  666. = typename iterator_traits<_InputIterator>::value_type,
  667. typename _Allocator = allocator<_ValT>,
  668. typename = _RequireInputIter<_InputIterator>,
  669. typename = _RequireAllocator<_Allocator>>
  670. list(_InputIterator, _InputIterator, _Allocator = _Allocator())
  671. -> list<_ValT, _Allocator>;
  672. #endif
  673. template<typename _Tp, typename _Alloc>
  674. inline bool
  675. operator==(const list<_Tp, _Alloc>& __lhs,
  676. const list<_Tp, _Alloc>& __rhs)
  677. { return __lhs._M_base() == __rhs._M_base(); }
  678. template<typename _Tp, typename _Alloc>
  679. inline bool
  680. operator!=(const list<_Tp, _Alloc>& __lhs,
  681. const list<_Tp, _Alloc>& __rhs)
  682. { return __lhs._M_base() != __rhs._M_base(); }
  683. template<typename _Tp, typename _Alloc>
  684. inline bool
  685. operator<(const list<_Tp, _Alloc>& __lhs,
  686. const list<_Tp, _Alloc>& __rhs)
  687. { return __lhs._M_base() < __rhs._M_base(); }
  688. template<typename _Tp, typename _Alloc>
  689. inline bool
  690. operator<=(const list<_Tp, _Alloc>& __lhs,
  691. const list<_Tp, _Alloc>& __rhs)
  692. { return __lhs._M_base() <= __rhs._M_base(); }
  693. template<typename _Tp, typename _Alloc>
  694. inline bool
  695. operator>=(const list<_Tp, _Alloc>& __lhs,
  696. const list<_Tp, _Alloc>& __rhs)
  697. { return __lhs._M_base() >= __rhs._M_base(); }
  698. template<typename _Tp, typename _Alloc>
  699. inline bool
  700. operator>(const list<_Tp, _Alloc>& __lhs,
  701. const list<_Tp, _Alloc>& __rhs)
  702. { return __lhs._M_base() > __rhs._M_base(); }
  703. template<typename _Tp, typename _Alloc>
  704. inline void
  705. swap(list<_Tp, _Alloc>& __lhs, list<_Tp, _Alloc>& __rhs)
  706. _GLIBCXX_NOEXCEPT_IF(noexcept(__lhs.swap(__rhs)))
  707. { __lhs.swap(__rhs); }
  708. } // namespace __debug
  709. } // namespace std
  710. namespace __gnu_debug
  711. {
  712. #ifndef _GLIBCXX_USE_CXX11_ABI
  713. // If not using C++11 list::size() is not in O(1) so we do not use it.
  714. template<typename _Tp, typename _Alloc>
  715. struct _Sequence_traits<std::__debug::list<_Tp, _Alloc> >
  716. {
  717. typedef typename std::__debug::list<_Tp, _Alloc>::iterator _It;
  718. static typename _Distance_traits<_It>::__type
  719. _S_size(const std::__debug::list<_Tp, _Alloc>& __seq)
  720. {
  721. return __seq.empty()
  722. ? std::make_pair(0, __dp_exact) : std::make_pair(1, __dp_equality);
  723. }
  724. };
  725. #endif
  726. #ifndef _GLIBCXX_DEBUG_PEDANTIC
  727. template<class _Tp, class _Alloc>
  728. struct _Insert_range_from_self_is_safe<std::__debug::list<_Tp, _Alloc> >
  729. { enum { __value = 1 }; };
  730. #endif
  731. }
  732. #endif