list 26 KB

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