list 25 KB

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