list 27 KB

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