list 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP_LIST
  10. #define _LIBCPP_LIST
  11. /*
  12. list synopsis
  13. namespace std
  14. {
  15. template <class T, class Alloc = allocator<T> >
  16. class list
  17. {
  18. public:
  19. // types:
  20. typedef T value_type;
  21. typedef Alloc allocator_type;
  22. typedef typename allocator_type::reference reference;
  23. typedef typename allocator_type::const_reference const_reference;
  24. typedef typename allocator_type::pointer pointer;
  25. typedef typename allocator_type::const_pointer const_pointer;
  26. typedef implementation-defined iterator;
  27. typedef implementation-defined const_iterator;
  28. typedef implementation-defined size_type;
  29. typedef implementation-defined difference_type;
  30. typedef reverse_iterator<iterator> reverse_iterator;
  31. typedef reverse_iterator<const_iterator> const_reverse_iterator;
  32. list()
  33. noexcept(is_nothrow_default_constructible<allocator_type>::value);
  34. explicit list(const allocator_type& a);
  35. explicit list(size_type n);
  36. explicit list(size_type n, const allocator_type& a); // C++14
  37. list(size_type n, const value_type& value);
  38. list(size_type n, const value_type& value, const allocator_type& a);
  39. template <class Iter>
  40. list(Iter first, Iter last);
  41. template <class Iter>
  42. list(Iter first, Iter last, const allocator_type& a);
  43. template<container-compatible-range<T> R>
  44. list(from_range_t, R&& rg, const Allocator& = Allocator()); // C++23
  45. list(const list& x);
  46. list(const list&, const allocator_type& a);
  47. list(list&& x)
  48. noexcept(is_nothrow_move_constructible<allocator_type>::value);
  49. list(list&&, const allocator_type& a);
  50. list(initializer_list<value_type>);
  51. list(initializer_list<value_type>, const allocator_type& a);
  52. ~list();
  53. list& operator=(const list& x);
  54. list& operator=(list&& x)
  55. noexcept(
  56. allocator_type::propagate_on_container_move_assignment::value &&
  57. is_nothrow_move_assignable<allocator_type>::value);
  58. list& operator=(initializer_list<value_type>);
  59. template <class Iter>
  60. void assign(Iter first, Iter last);
  61. template<container-compatible-range<T> R>
  62. void assign_range(R&& rg); // C++23
  63. void assign(size_type n, const value_type& t);
  64. void assign(initializer_list<value_type>);
  65. allocator_type get_allocator() const noexcept;
  66. iterator begin() noexcept;
  67. const_iterator begin() const noexcept;
  68. iterator end() noexcept;
  69. const_iterator end() const noexcept;
  70. reverse_iterator rbegin() noexcept;
  71. const_reverse_iterator rbegin() const noexcept;
  72. reverse_iterator rend() noexcept;
  73. const_reverse_iterator rend() const noexcept;
  74. const_iterator cbegin() const noexcept;
  75. const_iterator cend() const noexcept;
  76. const_reverse_iterator crbegin() const noexcept;
  77. const_reverse_iterator crend() const noexcept;
  78. reference front();
  79. const_reference front() const;
  80. reference back();
  81. const_reference back() const;
  82. bool empty() const noexcept;
  83. size_type size() const noexcept;
  84. size_type max_size() const noexcept;
  85. template <class... Args>
  86. reference emplace_front(Args&&... args); // reference in C++17
  87. void pop_front();
  88. template <class... Args>
  89. reference emplace_back(Args&&... args); // reference in C++17
  90. void pop_back();
  91. void push_front(const value_type& x);
  92. void push_front(value_type&& x);
  93. template<container-compatible-range<T> R>
  94. void prepend_range(R&& rg); // C++23
  95. void push_back(const value_type& x);
  96. void push_back(value_type&& x);
  97. template<container-compatible-range<T> R>
  98. void append_range(R&& rg); // C++23
  99. template <class... Args>
  100. iterator emplace(const_iterator position, Args&&... args);
  101. iterator insert(const_iterator position, const value_type& x);
  102. iterator insert(const_iterator position, value_type&& x);
  103. iterator insert(const_iterator position, size_type n, const value_type& x);
  104. template <class Iter>
  105. iterator insert(const_iterator position, Iter first, Iter last);
  106. template<container-compatible-range<T> R>
  107. iterator insert_range(const_iterator position, R&& rg); // C++23
  108. iterator insert(const_iterator position, initializer_list<value_type> il);
  109. iterator erase(const_iterator position);
  110. iterator erase(const_iterator position, const_iterator last);
  111. void resize(size_type sz);
  112. void resize(size_type sz, const value_type& c);
  113. void swap(list&)
  114. noexcept(allocator_traits<allocator_type>::is_always_equal::value); // C++17
  115. void clear() noexcept;
  116. void splice(const_iterator position, list& x);
  117. void splice(const_iterator position, list&& x);
  118. void splice(const_iterator position, list& x, const_iterator i);
  119. void splice(const_iterator position, list&& x, const_iterator i);
  120. void splice(const_iterator position, list& x, const_iterator first,
  121. const_iterator last);
  122. void splice(const_iterator position, list&& x, const_iterator first,
  123. const_iterator last);
  124. size_type remove(const value_type& value); // void before C++20
  125. template <class Pred>
  126. size_type remove_if(Pred pred); // void before C++20
  127. size_type unique(); // void before C++20
  128. template <class BinaryPredicate>
  129. size_type unique(BinaryPredicate binary_pred); // void before C++20
  130. void merge(list& x);
  131. void merge(list&& x);
  132. template <class Compare>
  133. void merge(list& x, Compare comp);
  134. template <class Compare>
  135. void merge(list&& x, Compare comp);
  136. void sort();
  137. template <class Compare>
  138. void sort(Compare comp);
  139. void reverse() noexcept;
  140. };
  141. template <class InputIterator, class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
  142. list(InputIterator, InputIterator, Allocator = Allocator())
  143. -> list<typename iterator_traits<InputIterator>::value_type, Allocator>; // C++17
  144. template<ranges::input_range R, class Allocator = allocator<ranges::range_value_t<R>>>
  145. list(from_range_t, R&&, Allocator = Allocator())
  146. -> list<ranges::range_value_t<R>, Allocator>; // C++23
  147. template <class T, class Alloc>
  148. bool operator==(const list<T,Alloc>& x, const list<T,Alloc>& y);
  149. template <class T, class Alloc>
  150. bool operator< (const list<T,Alloc>& x, const list<T,Alloc>& y); // removed in C++20
  151. template <class T, class Alloc>
  152. bool operator!=(const list<T,Alloc>& x, const list<T,Alloc>& y); // removed in C++20
  153. template <class T, class Alloc>
  154. bool operator> (const list<T,Alloc>& x, const list<T,Alloc>& y); // removed in C++20
  155. template <class T, class Alloc>
  156. bool operator>=(const list<T,Alloc>& x, const list<T,Alloc>& y); // removed in C++20
  157. template <class T, class Alloc>
  158. bool operator<=(const list<T,Alloc>& x, const list<T,Alloc>& y); // removed in C++20
  159. template<class T, class Allocator>
  160. synth-three-way-result<T> operator<=>(const list<T, Allocator>& x,
  161. const list<T, Allocator>& y); // since C++20
  162. template <class T, class Alloc>
  163. void swap(list<T,Alloc>& x, list<T,Alloc>& y)
  164. noexcept(noexcept(x.swap(y)));
  165. template <class T, class Allocator, class U>
  166. typename list<T, Allocator>::size_type
  167. erase(list<T, Allocator>& c, const U& value); // since C++20
  168. template <class T, class Allocator, class Predicate>
  169. typename list<T, Allocator>::size_type
  170. erase_if(list<T, Allocator>& c, Predicate pred); // since C++20
  171. } // std
  172. */
  173. #include <__algorithm/comp.h>
  174. #include <__algorithm/equal.h>
  175. #include <__algorithm/lexicographical_compare.h>
  176. #include <__algorithm/lexicographical_compare_three_way.h>
  177. #include <__algorithm/min.h>
  178. #include <__assert>
  179. #include <__availability>
  180. #include <__config>
  181. #include <__format/enable_insertable.h>
  182. #include <__iterator/distance.h>
  183. #include <__iterator/iterator_traits.h>
  184. #include <__iterator/move_iterator.h>
  185. #include <__iterator/next.h>
  186. #include <__iterator/prev.h>
  187. #include <__iterator/reverse_iterator.h>
  188. #include <__memory/addressof.h>
  189. #include <__memory/allocation_guard.h>
  190. #include <__memory/allocator.h>
  191. #include <__memory/allocator_traits.h>
  192. #include <__memory/compressed_pair.h>
  193. #include <__memory/construct_at.h>
  194. #include <__memory/pointer_traits.h>
  195. #include <__memory/swap_allocator.h>
  196. #include <__memory_resource/polymorphic_allocator.h>
  197. #include <__ranges/access.h>
  198. #include <__ranges/concepts.h>
  199. #include <__ranges/container_compatible_range.h>
  200. #include <__ranges/from_range.h>
  201. #include <__type_traits/conditional.h>
  202. #include <__type_traits/is_allocator.h>
  203. #include <__type_traits/is_nothrow_assignable.h>
  204. #include <__type_traits/is_nothrow_constructible.h>
  205. #include <__type_traits/is_pointer.h>
  206. #include <__type_traits/is_same.h>
  207. #include <__type_traits/type_identity.h>
  208. #include <__utility/forward.h>
  209. #include <__utility/move.h>
  210. #include <__utility/swap.h>
  211. #include <cstring>
  212. #include <limits>
  213. #include <new> // __launder
  214. #include <version>
  215. // standard-mandated includes
  216. // [iterator.range]
  217. #include <__iterator/access.h>
  218. #include <__iterator/data.h>
  219. #include <__iterator/empty.h>
  220. #include <__iterator/reverse_access.h>
  221. #include <__iterator/size.h>
  222. // [list.syn]
  223. #include <compare>
  224. #include <initializer_list>
  225. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  226. # pragma GCC system_header
  227. #endif
  228. _LIBCPP_PUSH_MACROS
  229. #include <__undef_macros>
  230. _LIBCPP_BEGIN_NAMESPACE_STD
  231. template <class _Tp, class _VoidPtr>
  232. struct __list_node;
  233. template <class _Tp, class _VoidPtr>
  234. struct __list_node_base;
  235. template <class _Tp, class _VoidPtr>
  236. struct __list_node_pointer_traits {
  237. typedef __rebind_pointer_t<_VoidPtr, __list_node<_Tp, _VoidPtr> > __node_pointer;
  238. typedef __rebind_pointer_t<_VoidPtr, __list_node_base<_Tp, _VoidPtr> > __base_pointer;
  239. #if defined(_LIBCPP_ABI_LIST_REMOVE_NODE_POINTER_UB)
  240. typedef __base_pointer __link_pointer;
  241. #else
  242. typedef __conditional_t<is_pointer<_VoidPtr>::value, __base_pointer, __node_pointer> __link_pointer;
  243. #endif
  244. typedef __conditional_t<is_same<__link_pointer, __node_pointer>::value, __base_pointer, __node_pointer>
  245. __non_link_pointer;
  246. static _LIBCPP_HIDE_FROM_ABI __link_pointer __unsafe_link_pointer_cast(__link_pointer __p) { return __p; }
  247. static _LIBCPP_HIDE_FROM_ABI __link_pointer __unsafe_link_pointer_cast(__non_link_pointer __p) {
  248. return static_cast<__link_pointer>(static_cast<_VoidPtr>(__p));
  249. }
  250. };
  251. template <class _Tp, class _VoidPtr>
  252. struct __list_node_base {
  253. typedef __list_node_pointer_traits<_Tp, _VoidPtr> _NodeTraits;
  254. typedef typename _NodeTraits::__node_pointer __node_pointer;
  255. typedef typename _NodeTraits::__base_pointer __base_pointer;
  256. typedef typename _NodeTraits::__link_pointer __link_pointer;
  257. __link_pointer __prev_;
  258. __link_pointer __next_;
  259. _LIBCPP_HIDE_FROM_ABI __list_node_base()
  260. : __prev_(_NodeTraits::__unsafe_link_pointer_cast(__self())),
  261. __next_(_NodeTraits::__unsafe_link_pointer_cast(__self())) {}
  262. _LIBCPP_HIDE_FROM_ABI explicit __list_node_base(__link_pointer __prev, __link_pointer __next)
  263. : __prev_(__prev), __next_(__next) {}
  264. _LIBCPP_HIDE_FROM_ABI __base_pointer __self() { return pointer_traits<__base_pointer>::pointer_to(*this); }
  265. _LIBCPP_HIDE_FROM_ABI __node_pointer __as_node() { return static_cast<__node_pointer>(__self()); }
  266. };
  267. template <class _Tp, class _VoidPtr>
  268. struct __list_node : public __list_node_base<_Tp, _VoidPtr> {
  269. // We allow starting the lifetime of nodes without initializing the value held by the node,
  270. // since that is handled by the list itself in order to be allocator-aware.
  271. #ifndef _LIBCPP_CXX03_LANG
  272. private:
  273. union {
  274. _Tp __value_;
  275. };
  276. public:
  277. _LIBCPP_HIDE_FROM_ABI _Tp& __get_value() { return __value_; }
  278. #else
  279. private:
  280. _ALIGNAS_TYPE(_Tp) char __buffer_[sizeof(_Tp)];
  281. public:
  282. _LIBCPP_HIDE_FROM_ABI _Tp& __get_value() { return *std::__launder(reinterpret_cast<_Tp*>(&__buffer_)); }
  283. #endif
  284. typedef __list_node_base<_Tp, _VoidPtr> __base;
  285. typedef typename __base::__link_pointer __link_pointer;
  286. _LIBCPP_HIDE_FROM_ABI explicit __list_node(__link_pointer __prev, __link_pointer __next) : __base(__prev, __next) {}
  287. _LIBCPP_HIDE_FROM_ABI ~__list_node() {}
  288. _LIBCPP_HIDE_FROM_ABI __link_pointer __as_link() { return static_cast<__link_pointer>(__base::__self()); }
  289. };
  290. template <class _Tp, class _Alloc = allocator<_Tp> >
  291. class _LIBCPP_TEMPLATE_VIS list;
  292. template <class _Tp, class _Alloc>
  293. class __list_imp;
  294. template <class _Tp, class _VoidPtr>
  295. class _LIBCPP_TEMPLATE_VIS __list_const_iterator;
  296. template <class _Tp, class _VoidPtr>
  297. class _LIBCPP_TEMPLATE_VIS __list_iterator {
  298. typedef __list_node_pointer_traits<_Tp, _VoidPtr> _NodeTraits;
  299. typedef typename _NodeTraits::__link_pointer __link_pointer;
  300. __link_pointer __ptr_;
  301. _LIBCPP_HIDE_FROM_ABI explicit __list_iterator(__link_pointer __p) _NOEXCEPT : __ptr_(__p) {}
  302. template <class, class>
  303. friend class list;
  304. template <class, class>
  305. friend class __list_imp;
  306. template <class, class>
  307. friend class __list_const_iterator;
  308. public:
  309. typedef bidirectional_iterator_tag iterator_category;
  310. typedef _Tp value_type;
  311. typedef value_type& reference;
  312. typedef __rebind_pointer_t<_VoidPtr, value_type> pointer;
  313. typedef typename pointer_traits<pointer>::difference_type difference_type;
  314. _LIBCPP_HIDE_FROM_ABI __list_iterator() _NOEXCEPT : __ptr_(nullptr) {}
  315. _LIBCPP_HIDE_FROM_ABI reference operator*() const { return __ptr_->__as_node()->__get_value(); }
  316. _LIBCPP_HIDE_FROM_ABI pointer operator->() const {
  317. return pointer_traits<pointer>::pointer_to(__ptr_->__as_node()->__get_value());
  318. }
  319. _LIBCPP_HIDE_FROM_ABI __list_iterator& operator++() {
  320. __ptr_ = __ptr_->__next_;
  321. return *this;
  322. }
  323. _LIBCPP_HIDE_FROM_ABI __list_iterator operator++(int) {
  324. __list_iterator __t(*this);
  325. ++(*this);
  326. return __t;
  327. }
  328. _LIBCPP_HIDE_FROM_ABI __list_iterator& operator--() {
  329. __ptr_ = __ptr_->__prev_;
  330. return *this;
  331. }
  332. _LIBCPP_HIDE_FROM_ABI __list_iterator operator--(int) {
  333. __list_iterator __t(*this);
  334. --(*this);
  335. return __t;
  336. }
  337. friend _LIBCPP_HIDE_FROM_ABI bool operator==(const __list_iterator& __x, const __list_iterator& __y) {
  338. return __x.__ptr_ == __y.__ptr_;
  339. }
  340. friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const __list_iterator& __x, const __list_iterator& __y) {
  341. return !(__x == __y);
  342. }
  343. };
  344. template <class _Tp, class _VoidPtr>
  345. class _LIBCPP_TEMPLATE_VIS __list_const_iterator {
  346. typedef __list_node_pointer_traits<_Tp, _VoidPtr> _NodeTraits;
  347. typedef typename _NodeTraits::__link_pointer __link_pointer;
  348. __link_pointer __ptr_;
  349. _LIBCPP_HIDE_FROM_ABI explicit __list_const_iterator(__link_pointer __p) _NOEXCEPT : __ptr_(__p) {}
  350. template <class, class>
  351. friend class list;
  352. template <class, class>
  353. friend class __list_imp;
  354. public:
  355. typedef bidirectional_iterator_tag iterator_category;
  356. typedef _Tp value_type;
  357. typedef const value_type& reference;
  358. typedef __rebind_pointer_t<_VoidPtr, const value_type> pointer;
  359. typedef typename pointer_traits<pointer>::difference_type difference_type;
  360. _LIBCPP_HIDE_FROM_ABI __list_const_iterator() _NOEXCEPT : __ptr_(nullptr) {}
  361. _LIBCPP_HIDE_FROM_ABI __list_const_iterator(const __list_iterator<_Tp, _VoidPtr>& __p) _NOEXCEPT
  362. : __ptr_(__p.__ptr_) {}
  363. _LIBCPP_HIDE_FROM_ABI reference operator*() const { return __ptr_->__as_node()->__get_value(); }
  364. _LIBCPP_HIDE_FROM_ABI pointer operator->() const {
  365. return pointer_traits<pointer>::pointer_to(__ptr_->__as_node()->__get_value());
  366. }
  367. _LIBCPP_HIDE_FROM_ABI __list_const_iterator& operator++() {
  368. __ptr_ = __ptr_->__next_;
  369. return *this;
  370. }
  371. _LIBCPP_HIDE_FROM_ABI __list_const_iterator operator++(int) {
  372. __list_const_iterator __t(*this);
  373. ++(*this);
  374. return __t;
  375. }
  376. _LIBCPP_HIDE_FROM_ABI __list_const_iterator& operator--() {
  377. __ptr_ = __ptr_->__prev_;
  378. return *this;
  379. }
  380. _LIBCPP_HIDE_FROM_ABI __list_const_iterator operator--(int) {
  381. __list_const_iterator __t(*this);
  382. --(*this);
  383. return __t;
  384. }
  385. friend _LIBCPP_HIDE_FROM_ABI bool operator==(const __list_const_iterator& __x, const __list_const_iterator& __y) {
  386. return __x.__ptr_ == __y.__ptr_;
  387. }
  388. friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const __list_const_iterator& __x, const __list_const_iterator& __y) {
  389. return !(__x == __y);
  390. }
  391. };
  392. template <class _Tp, class _Alloc>
  393. class __list_imp {
  394. __list_imp(const __list_imp&);
  395. __list_imp& operator=(const __list_imp&);
  396. public:
  397. typedef _Alloc allocator_type;
  398. typedef allocator_traits<allocator_type> __alloc_traits;
  399. typedef typename __alloc_traits::size_type size_type;
  400. protected:
  401. typedef _Tp value_type;
  402. typedef typename __alloc_traits::void_pointer __void_pointer;
  403. typedef __list_iterator<value_type, __void_pointer> iterator;
  404. typedef __list_const_iterator<value_type, __void_pointer> const_iterator;
  405. typedef __list_node_base<value_type, __void_pointer> __node_base;
  406. typedef __list_node<value_type, __void_pointer> __node_type;
  407. typedef __rebind_alloc<__alloc_traits, __node_type> __node_allocator;
  408. typedef allocator_traits<__node_allocator> __node_alloc_traits;
  409. typedef typename __node_alloc_traits::pointer __node_pointer;
  410. typedef typename __node_alloc_traits::pointer __node_const_pointer;
  411. typedef __list_node_pointer_traits<value_type, __void_pointer> __node_pointer_traits;
  412. typedef typename __node_pointer_traits::__link_pointer __link_pointer;
  413. typedef __link_pointer __link_const_pointer;
  414. typedef typename __alloc_traits::pointer pointer;
  415. typedef typename __alloc_traits::const_pointer const_pointer;
  416. typedef typename __alloc_traits::difference_type difference_type;
  417. typedef __rebind_alloc<__alloc_traits, __node_base> __node_base_allocator;
  418. typedef typename allocator_traits<__node_base_allocator>::pointer __node_base_pointer;
  419. static_assert((!is_same<allocator_type, __node_allocator>::value),
  420. "internal allocator type must differ from user-specified "
  421. "type; otherwise overload resolution breaks");
  422. __node_base __end_;
  423. __compressed_pair<size_type, __node_allocator> __size_alloc_;
  424. _LIBCPP_HIDE_FROM_ABI __link_pointer __end_as_link() const _NOEXCEPT {
  425. return __node_pointer_traits::__unsafe_link_pointer_cast(const_cast<__node_base&>(__end_).__self());
  426. }
  427. _LIBCPP_HIDE_FROM_ABI size_type& __sz() _NOEXCEPT { return __size_alloc_.first(); }
  428. _LIBCPP_HIDE_FROM_ABI const size_type& __sz() const _NOEXCEPT { return __size_alloc_.first(); }
  429. _LIBCPP_HIDE_FROM_ABI __node_allocator& __node_alloc() _NOEXCEPT { return __size_alloc_.second(); }
  430. _LIBCPP_HIDE_FROM_ABI const __node_allocator& __node_alloc() const _NOEXCEPT { return __size_alloc_.second(); }
  431. _LIBCPP_HIDE_FROM_ABI size_type __node_alloc_max_size() const _NOEXCEPT {
  432. return __node_alloc_traits::max_size(__node_alloc());
  433. }
  434. _LIBCPP_HIDE_FROM_ABI static void __unlink_nodes(__link_pointer __f, __link_pointer __l) _NOEXCEPT;
  435. _LIBCPP_HIDE_FROM_ABI __list_imp() _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value);
  436. _LIBCPP_HIDE_FROM_ABI __list_imp(const allocator_type& __a);
  437. _LIBCPP_HIDE_FROM_ABI __list_imp(const __node_allocator& __a);
  438. #ifndef _LIBCPP_CXX03_LANG
  439. _LIBCPP_HIDE_FROM_ABI __list_imp(__node_allocator&& __a) _NOEXCEPT;
  440. #endif
  441. _LIBCPP_HIDE_FROM_ABI ~__list_imp();
  442. _LIBCPP_HIDE_FROM_ABI void clear() _NOEXCEPT;
  443. _LIBCPP_HIDE_FROM_ABI bool empty() const _NOEXCEPT { return __sz() == 0; }
  444. _LIBCPP_HIDE_FROM_ABI iterator begin() _NOEXCEPT { return iterator(__end_.__next_); }
  445. _LIBCPP_HIDE_FROM_ABI const_iterator begin() const _NOEXCEPT { return const_iterator(__end_.__next_); }
  446. _LIBCPP_HIDE_FROM_ABI iterator end() _NOEXCEPT { return iterator(__end_as_link()); }
  447. _LIBCPP_HIDE_FROM_ABI const_iterator end() const _NOEXCEPT { return const_iterator(__end_as_link()); }
  448. _LIBCPP_HIDE_FROM_ABI void swap(__list_imp& __c)
  449. #if _LIBCPP_STD_VER >= 14
  450. _NOEXCEPT;
  451. #else
  452. _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || __is_nothrow_swappable<allocator_type>::value);
  453. #endif
  454. _LIBCPP_HIDE_FROM_ABI void __copy_assign_alloc(const __list_imp& __c) {
  455. __copy_assign_alloc(
  456. __c, integral_constant<bool, __node_alloc_traits::propagate_on_container_copy_assignment::value>());
  457. }
  458. _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(__list_imp& __c)
  459. _NOEXCEPT_(!__node_alloc_traits::propagate_on_container_move_assignment::value ||
  460. is_nothrow_move_assignable<__node_allocator>::value) {
  461. __move_assign_alloc(
  462. __c, integral_constant<bool, __node_alloc_traits::propagate_on_container_move_assignment::value>());
  463. }
  464. template <class... _Args>
  465. _LIBCPP_HIDE_FROM_ABI __node_pointer __create_node(__link_pointer __prev, __link_pointer __next, _Args&&... __args) {
  466. __node_allocator& __alloc = __node_alloc();
  467. __allocation_guard<__node_allocator> __guard(__alloc, 1);
  468. // Begin the lifetime of the node itself. Note that this doesn't begin the lifetime of the value
  469. // held inside the node, since we need to use the allocator's construct() method for that.
  470. //
  471. // We don't use the allocator's construct() method to construct the node itself since the
  472. // Cpp17FooInsertable named requirements don't require the allocator's construct() method
  473. // to work on anything other than the value_type.
  474. std::__construct_at(std::addressof(*__guard.__get()), __prev, __next);
  475. // Now construct the value_type using the allocator's construct() method.
  476. __node_alloc_traits::construct(
  477. __alloc, std::addressof(__guard.__get()->__get_value()), std::forward<_Args>(__args)...);
  478. return __guard.__release_ptr();
  479. }
  480. template <class... _Args>
  481. _LIBCPP_HIDE_FROM_ABI void __delete_node(__node_pointer __node) {
  482. // For the same reason as above, we use the allocator's destroy() method for the value_type,
  483. // but not for the node itself.
  484. __node_allocator& __alloc = __node_alloc();
  485. __node_alloc_traits::destroy(__alloc, std::addressof(__node->__get_value()));
  486. std::__destroy_at(std::addressof(*__node));
  487. __node_alloc_traits::deallocate(__alloc, __node, 1);
  488. }
  489. private:
  490. _LIBCPP_HIDE_FROM_ABI void __copy_assign_alloc(const __list_imp& __c, true_type) {
  491. if (__node_alloc() != __c.__node_alloc())
  492. clear();
  493. __node_alloc() = __c.__node_alloc();
  494. }
  495. _LIBCPP_HIDE_FROM_ABI void __copy_assign_alloc(const __list_imp&, false_type) {}
  496. _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(__list_imp& __c, true_type)
  497. _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value) {
  498. __node_alloc() = std::move(__c.__node_alloc());
  499. }
  500. _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(__list_imp&, false_type) _NOEXCEPT {}
  501. };
  502. // Unlink nodes [__f, __l]
  503. template <class _Tp, class _Alloc>
  504. inline void __list_imp<_Tp, _Alloc>::__unlink_nodes(__link_pointer __f, __link_pointer __l) _NOEXCEPT {
  505. __f->__prev_->__next_ = __l->__next_;
  506. __l->__next_->__prev_ = __f->__prev_;
  507. }
  508. template <class _Tp, class _Alloc>
  509. inline __list_imp<_Tp, _Alloc>::__list_imp() _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value)
  510. : __size_alloc_(0, __default_init_tag()) {}
  511. template <class _Tp, class _Alloc>
  512. inline __list_imp<_Tp, _Alloc>::__list_imp(const allocator_type& __a) : __size_alloc_(0, __node_allocator(__a)) {}
  513. template <class _Tp, class _Alloc>
  514. inline __list_imp<_Tp, _Alloc>::__list_imp(const __node_allocator& __a) : __size_alloc_(0, __a) {}
  515. #ifndef _LIBCPP_CXX03_LANG
  516. template <class _Tp, class _Alloc>
  517. inline __list_imp<_Tp, _Alloc>::__list_imp(__node_allocator&& __a) _NOEXCEPT : __size_alloc_(0, std::move(__a)) {}
  518. #endif
  519. template <class _Tp, class _Alloc>
  520. __list_imp<_Tp, _Alloc>::~__list_imp() {
  521. clear();
  522. }
  523. template <class _Tp, class _Alloc>
  524. void __list_imp<_Tp, _Alloc>::clear() _NOEXCEPT {
  525. if (!empty()) {
  526. __link_pointer __f = __end_.__next_;
  527. __link_pointer __l = __end_as_link();
  528. __unlink_nodes(__f, __l->__prev_);
  529. __sz() = 0;
  530. while (__f != __l) {
  531. __node_pointer __np = __f->__as_node();
  532. __f = __f->__next_;
  533. __delete_node(__np);
  534. }
  535. }
  536. }
  537. template <class _Tp, class _Alloc>
  538. void __list_imp<_Tp, _Alloc>::swap(__list_imp& __c)
  539. #if _LIBCPP_STD_VER >= 14
  540. _NOEXCEPT
  541. #else
  542. _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || __is_nothrow_swappable<allocator_type>::value)
  543. #endif
  544. {
  545. _LIBCPP_ASSERT_COMPATIBLE_ALLOCATOR(
  546. __alloc_traits::propagate_on_container_swap::value || this->__node_alloc() == __c.__node_alloc(),
  547. "list::swap: Either propagate_on_container_swap must be true"
  548. " or the allocators must compare equal");
  549. using std::swap;
  550. std::__swap_allocator(__node_alloc(), __c.__node_alloc());
  551. swap(__sz(), __c.__sz());
  552. swap(__end_, __c.__end_);
  553. if (__sz() == 0)
  554. __end_.__next_ = __end_.__prev_ = __end_as_link();
  555. else
  556. __end_.__prev_->__next_ = __end_.__next_->__prev_ = __end_as_link();
  557. if (__c.__sz() == 0)
  558. __c.__end_.__next_ = __c.__end_.__prev_ = __c.__end_as_link();
  559. else
  560. __c.__end_.__prev_->__next_ = __c.__end_.__next_->__prev_ = __c.__end_as_link();
  561. }
  562. template <class _Tp, class _Alloc /*= allocator<_Tp>*/>
  563. class _LIBCPP_TEMPLATE_VIS list : private __list_imp<_Tp, _Alloc> {
  564. typedef __list_imp<_Tp, _Alloc> base;
  565. typedef typename base::__node_type __node_type;
  566. typedef typename base::__node_allocator __node_allocator;
  567. typedef typename base::__node_pointer __node_pointer;
  568. typedef typename base::__node_alloc_traits __node_alloc_traits;
  569. typedef typename base::__node_base __node_base;
  570. typedef typename base::__node_base_pointer __node_base_pointer;
  571. typedef typename base::__link_pointer __link_pointer;
  572. public:
  573. typedef _Tp value_type;
  574. typedef _Alloc allocator_type;
  575. static_assert((is_same<value_type, typename allocator_type::value_type>::value),
  576. "Allocator::value_type must be same type as value_type");
  577. typedef value_type& reference;
  578. typedef const value_type& const_reference;
  579. typedef typename base::pointer pointer;
  580. typedef typename base::const_pointer const_pointer;
  581. typedef typename base::size_type size_type;
  582. typedef typename base::difference_type difference_type;
  583. typedef typename base::iterator iterator;
  584. typedef typename base::const_iterator const_iterator;
  585. typedef std::reverse_iterator<iterator> reverse_iterator;
  586. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  587. #if _LIBCPP_STD_VER >= 20
  588. typedef size_type __remove_return_type;
  589. #else
  590. typedef void __remove_return_type;
  591. #endif
  592. static_assert(is_same<allocator_type, __rebind_alloc<allocator_traits<allocator_type>, value_type> >::value,
  593. "[allocator.requirements] states that rebinding an allocator to the same type should result in the "
  594. "original allocator");
  595. _LIBCPP_HIDE_FROM_ABI list() _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value) {}
  596. _LIBCPP_HIDE_FROM_ABI explicit list(const allocator_type& __a) : base(__a) {}
  597. _LIBCPP_HIDE_FROM_ABI explicit list(size_type __n);
  598. #if _LIBCPP_STD_VER >= 14
  599. _LIBCPP_HIDE_FROM_ABI explicit list(size_type __n, const allocator_type& __a);
  600. #endif
  601. _LIBCPP_HIDE_FROM_ABI list(size_type __n, const value_type& __x);
  602. template <__enable_if_t<__is_allocator<_Alloc>::value, int> = 0>
  603. _LIBCPP_HIDE_FROM_ABI list(size_type __n, const value_type& __x, const allocator_type& __a) : base(__a) {
  604. for (; __n > 0; --__n)
  605. push_back(__x);
  606. }
  607. template <class _InpIter, __enable_if_t<__has_input_iterator_category<_InpIter>::value, int> = 0>
  608. _LIBCPP_HIDE_FROM_ABI list(_InpIter __f, _InpIter __l);
  609. template <class _InpIter, __enable_if_t<__has_input_iterator_category<_InpIter>::value, int> = 0>
  610. _LIBCPP_HIDE_FROM_ABI list(_InpIter __f, _InpIter __l, const allocator_type& __a);
  611. #if _LIBCPP_STD_VER >= 23
  612. template <_ContainerCompatibleRange<_Tp> _Range>
  613. _LIBCPP_HIDE_FROM_ABI list(from_range_t, _Range&& __range, const allocator_type& __a = allocator_type()) : base(__a) {
  614. prepend_range(std::forward<_Range>(__range));
  615. }
  616. #endif
  617. _LIBCPP_HIDE_FROM_ABI list(const list& __c);
  618. _LIBCPP_HIDE_FROM_ABI list(const list& __c, const __type_identity_t<allocator_type>& __a);
  619. _LIBCPP_HIDE_FROM_ABI list& operator=(const list& __c);
  620. #ifndef _LIBCPP_CXX03_LANG
  621. _LIBCPP_HIDE_FROM_ABI list(initializer_list<value_type> __il);
  622. _LIBCPP_HIDE_FROM_ABI list(initializer_list<value_type> __il, const allocator_type& __a);
  623. _LIBCPP_HIDE_FROM_ABI list(list&& __c) _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value);
  624. _LIBCPP_HIDE_FROM_ABI list(list&& __c, const __type_identity_t<allocator_type>& __a);
  625. _LIBCPP_HIDE_FROM_ABI list& operator=(list&& __c)
  626. _NOEXCEPT_(__node_alloc_traits::propagate_on_container_move_assignment::value&&
  627. is_nothrow_move_assignable<__node_allocator>::value);
  628. _LIBCPP_HIDE_FROM_ABI list& operator=(initializer_list<value_type> __il) {
  629. assign(__il.begin(), __il.end());
  630. return *this;
  631. }
  632. _LIBCPP_HIDE_FROM_ABI void assign(initializer_list<value_type> __il) { assign(__il.begin(), __il.end()); }
  633. #endif // _LIBCPP_CXX03_LANG
  634. template <class _InpIter, __enable_if_t<__has_input_iterator_category<_InpIter>::value, int> = 0>
  635. _LIBCPP_HIDE_FROM_ABI void assign(_InpIter __f, _InpIter __l);
  636. #if _LIBCPP_STD_VER >= 23
  637. template <_ContainerCompatibleRange<_Tp> _Range>
  638. _LIBCPP_HIDE_FROM_ABI void assign_range(_Range&& __range) {
  639. __assign_with_sentinel(ranges::begin(__range), ranges::end(__range));
  640. }
  641. #endif
  642. _LIBCPP_HIDE_FROM_ABI void assign(size_type __n, const value_type& __x);
  643. _LIBCPP_HIDE_FROM_ABI allocator_type get_allocator() const _NOEXCEPT;
  644. _LIBCPP_HIDE_FROM_ABI size_type size() const _NOEXCEPT { return base::__sz(); }
  645. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI bool empty() const _NOEXCEPT { return base::empty(); }
  646. _LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT {
  647. return std::min<size_type>(base::__node_alloc_max_size(), numeric_limits<difference_type >::max());
  648. }
  649. _LIBCPP_HIDE_FROM_ABI iterator begin() _NOEXCEPT { return base::begin(); }
  650. _LIBCPP_HIDE_FROM_ABI const_iterator begin() const _NOEXCEPT { return base::begin(); }
  651. _LIBCPP_HIDE_FROM_ABI iterator end() _NOEXCEPT { return base::end(); }
  652. _LIBCPP_HIDE_FROM_ABI const_iterator end() const _NOEXCEPT { return base::end(); }
  653. _LIBCPP_HIDE_FROM_ABI const_iterator cbegin() const _NOEXCEPT { return base::begin(); }
  654. _LIBCPP_HIDE_FROM_ABI const_iterator cend() const _NOEXCEPT { return base::end(); }
  655. _LIBCPP_HIDE_FROM_ABI reverse_iterator rbegin() _NOEXCEPT { return reverse_iterator(end()); }
  656. _LIBCPP_HIDE_FROM_ABI const_reverse_iterator rbegin() const _NOEXCEPT { return const_reverse_iterator(end()); }
  657. _LIBCPP_HIDE_FROM_ABI reverse_iterator rend() _NOEXCEPT { return reverse_iterator(begin()); }
  658. _LIBCPP_HIDE_FROM_ABI const_reverse_iterator rend() const _NOEXCEPT { return const_reverse_iterator(begin()); }
  659. _LIBCPP_HIDE_FROM_ABI const_reverse_iterator crbegin() const _NOEXCEPT { return const_reverse_iterator(end()); }
  660. _LIBCPP_HIDE_FROM_ABI const_reverse_iterator crend() const _NOEXCEPT { return const_reverse_iterator(begin()); }
  661. _LIBCPP_HIDE_FROM_ABI reference front() {
  662. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "list::front called on empty list");
  663. return base::__end_.__next_->__as_node()->__get_value();
  664. }
  665. _LIBCPP_HIDE_FROM_ABI const_reference front() const {
  666. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "list::front called on empty list");
  667. return base::__end_.__next_->__as_node()->__get_value();
  668. }
  669. _LIBCPP_HIDE_FROM_ABI reference back() {
  670. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "list::back called on empty list");
  671. return base::__end_.__prev_->__as_node()->__get_value();
  672. }
  673. _LIBCPP_HIDE_FROM_ABI const_reference back() const {
  674. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "list::back called on empty list");
  675. return base::__end_.__prev_->__as_node()->__get_value();
  676. }
  677. #ifndef _LIBCPP_CXX03_LANG
  678. _LIBCPP_HIDE_FROM_ABI void push_front(value_type&& __x);
  679. _LIBCPP_HIDE_FROM_ABI void push_back(value_type&& __x);
  680. # if _LIBCPP_STD_VER >= 23
  681. template <_ContainerCompatibleRange<_Tp> _Range>
  682. _LIBCPP_HIDE_FROM_ABI void prepend_range(_Range&& __range) {
  683. insert_range(begin(), std::forward<_Range>(__range));
  684. }
  685. template <_ContainerCompatibleRange<_Tp> _Range>
  686. _LIBCPP_HIDE_FROM_ABI void append_range(_Range&& __range) {
  687. insert_range(end(), std::forward<_Range>(__range));
  688. }
  689. # endif
  690. template <class... _Args>
  691. # if _LIBCPP_STD_VER >= 17
  692. _LIBCPP_HIDE_FROM_ABI reference emplace_front(_Args&&... __args);
  693. # else
  694. _LIBCPP_HIDE_FROM_ABI void emplace_front(_Args&&... __args);
  695. # endif
  696. template <class... _Args>
  697. # if _LIBCPP_STD_VER >= 17
  698. _LIBCPP_HIDE_FROM_ABI reference emplace_back(_Args&&... __args);
  699. # else
  700. _LIBCPP_HIDE_FROM_ABI void emplace_back(_Args&&... __args);
  701. # endif
  702. template <class... _Args>
  703. _LIBCPP_HIDE_FROM_ABI iterator emplace(const_iterator __p, _Args&&... __args);
  704. _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, value_type&& __x);
  705. _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, initializer_list<value_type> __il) {
  706. return insert(__p, __il.begin(), __il.end());
  707. }
  708. #endif // _LIBCPP_CXX03_LANG
  709. _LIBCPP_HIDE_FROM_ABI void push_front(const value_type& __x);
  710. _LIBCPP_HIDE_FROM_ABI void push_back(const value_type& __x);
  711. #ifndef _LIBCPP_CXX03_LANG
  712. template <class _Arg>
  713. _LIBCPP_HIDE_FROM_ABI void __emplace_back(_Arg&& __arg) {
  714. emplace_back(std::forward<_Arg>(__arg));
  715. }
  716. #else
  717. _LIBCPP_HIDE_FROM_ABI void __emplace_back(value_type const& __arg) { push_back(__arg); }
  718. #endif
  719. _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, const value_type& __x);
  720. _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, size_type __n, const value_type& __x);
  721. template <class _InpIter, __enable_if_t<__has_input_iterator_category<_InpIter>::value, int> = 0>
  722. _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, _InpIter __f, _InpIter __l);
  723. #if _LIBCPP_STD_VER >= 23
  724. template <_ContainerCompatibleRange<_Tp> _Range>
  725. _LIBCPP_HIDE_FROM_ABI iterator insert_range(const_iterator __position, _Range&& __range) {
  726. return __insert_with_sentinel(__position, ranges::begin(__range), ranges::end(__range));
  727. }
  728. #endif
  729. _LIBCPP_HIDE_FROM_ABI void swap(list& __c)
  730. #if _LIBCPP_STD_VER >= 14
  731. _NOEXCEPT
  732. #else
  733. _NOEXCEPT_(!__node_alloc_traits::propagate_on_container_swap::value ||
  734. __is_nothrow_swappable<__node_allocator>::value)
  735. #endif
  736. {
  737. base::swap(__c);
  738. }
  739. _LIBCPP_HIDE_FROM_ABI void clear() _NOEXCEPT { base::clear(); }
  740. _LIBCPP_HIDE_FROM_ABI void pop_front();
  741. _LIBCPP_HIDE_FROM_ABI void pop_back();
  742. _LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __p);
  743. _LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __f, const_iterator __l);
  744. _LIBCPP_HIDE_FROM_ABI void resize(size_type __n);
  745. _LIBCPP_HIDE_FROM_ABI void resize(size_type __n, const value_type& __x);
  746. _LIBCPP_HIDE_FROM_ABI void splice(const_iterator __p, list& __c);
  747. #ifndef _LIBCPP_CXX03_LANG
  748. _LIBCPP_HIDE_FROM_ABI void splice(const_iterator __p, list&& __c) { splice(__p, __c); }
  749. _LIBCPP_HIDE_FROM_ABI void splice(const_iterator __p, list&& __c, const_iterator __i) { splice(__p, __c, __i); }
  750. _LIBCPP_HIDE_FROM_ABI void splice(const_iterator __p, list&& __c, const_iterator __f, const_iterator __l) {
  751. splice(__p, __c, __f, __l);
  752. }
  753. #endif
  754. _LIBCPP_HIDE_FROM_ABI void splice(const_iterator __p, list& __c, const_iterator __i);
  755. _LIBCPP_HIDE_FROM_ABI void splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l);
  756. _LIBCPP_HIDE_FROM_ABI __remove_return_type remove(const value_type& __x);
  757. template <class _Pred>
  758. _LIBCPP_HIDE_FROM_ABI __remove_return_type remove_if(_Pred __pred);
  759. _LIBCPP_HIDE_FROM_ABI __remove_return_type unique() { return unique(__equal_to()); }
  760. template <class _BinaryPred>
  761. _LIBCPP_HIDE_FROM_ABI __remove_return_type unique(_BinaryPred __binary_pred);
  762. _LIBCPP_HIDE_FROM_ABI void merge(list& __c);
  763. #ifndef _LIBCPP_CXX03_LANG
  764. _LIBCPP_HIDE_FROM_ABI void merge(list&& __c) { merge(__c); }
  765. template <class _Comp>
  766. _LIBCPP_HIDE_FROM_ABI void merge(list&& __c, _Comp __comp) {
  767. merge(__c, __comp);
  768. }
  769. #endif
  770. template <class _Comp>
  771. _LIBCPP_HIDE_FROM_ABI void merge(list& __c, _Comp __comp);
  772. _LIBCPP_HIDE_FROM_ABI void sort();
  773. template <class _Comp>
  774. _LIBCPP_HIDE_FROM_ABI void sort(_Comp __comp);
  775. _LIBCPP_HIDE_FROM_ABI void reverse() _NOEXCEPT;
  776. _LIBCPP_HIDE_FROM_ABI bool __invariants() const;
  777. private:
  778. template <class _Iterator, class _Sentinel>
  779. _LIBCPP_HIDE_FROM_ABI void __assign_with_sentinel(_Iterator __f, _Sentinel __l);
  780. template <class _Iterator, class _Sentinel>
  781. _LIBCPP_HIDE_FROM_ABI iterator __insert_with_sentinel(const_iterator __p, _Iterator __f, _Sentinel __l);
  782. _LIBCPP_HIDE_FROM_ABI static void __link_nodes(__link_pointer __p, __link_pointer __f, __link_pointer __l);
  783. _LIBCPP_HIDE_FROM_ABI void __link_nodes_at_front(__link_pointer __f, __link_pointer __l);
  784. _LIBCPP_HIDE_FROM_ABI void __link_nodes_at_back(__link_pointer __f, __link_pointer __l);
  785. _LIBCPP_HIDE_FROM_ABI iterator __iterator(size_type __n);
  786. // TODO: Make this _LIBCPP_HIDE_FROM_ABI
  787. template <class _Comp>
  788. _LIBCPP_HIDDEN static iterator __sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp);
  789. _LIBCPP_HIDE_FROM_ABI void __move_assign(list& __c, true_type)
  790. _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value);
  791. _LIBCPP_HIDE_FROM_ABI void __move_assign(list& __c, false_type);
  792. };
  793. #if _LIBCPP_STD_VER >= 17
  794. template <class _InputIterator,
  795. class _Alloc = allocator<__iter_value_type<_InputIterator>>,
  796. class = enable_if_t<__has_input_iterator_category<_InputIterator>::value>,
  797. class = enable_if_t<__is_allocator<_Alloc>::value> >
  798. list(_InputIterator, _InputIterator) -> list<__iter_value_type<_InputIterator>, _Alloc>;
  799. template <class _InputIterator,
  800. class _Alloc,
  801. class = enable_if_t<__has_input_iterator_category<_InputIterator>::value>,
  802. class = enable_if_t<__is_allocator<_Alloc>::value> >
  803. list(_InputIterator, _InputIterator, _Alloc) -> list<__iter_value_type<_InputIterator>, _Alloc>;
  804. #endif
  805. #if _LIBCPP_STD_VER >= 23
  806. template <ranges::input_range _Range,
  807. class _Alloc = allocator<ranges::range_value_t<_Range>>,
  808. class = enable_if_t<__is_allocator<_Alloc>::value> >
  809. list(from_range_t, _Range&&, _Alloc = _Alloc()) -> list<ranges::range_value_t<_Range>, _Alloc>;
  810. #endif
  811. // Link in nodes [__f, __l] just prior to __p
  812. template <class _Tp, class _Alloc>
  813. inline void list<_Tp, _Alloc>::__link_nodes(__link_pointer __p, __link_pointer __f, __link_pointer __l) {
  814. __p->__prev_->__next_ = __f;
  815. __f->__prev_ = __p->__prev_;
  816. __p->__prev_ = __l;
  817. __l->__next_ = __p;
  818. }
  819. // Link in nodes [__f, __l] at the front of the list
  820. template <class _Tp, class _Alloc>
  821. inline void list<_Tp, _Alloc>::__link_nodes_at_front(__link_pointer __f, __link_pointer __l) {
  822. __f->__prev_ = base::__end_as_link();
  823. __l->__next_ = base::__end_.__next_;
  824. __l->__next_->__prev_ = __l;
  825. base::__end_.__next_ = __f;
  826. }
  827. // Link in nodes [__f, __l] at the back of the list
  828. template <class _Tp, class _Alloc>
  829. inline void list<_Tp, _Alloc>::__link_nodes_at_back(__link_pointer __f, __link_pointer __l) {
  830. __l->__next_ = base::__end_as_link();
  831. __f->__prev_ = base::__end_.__prev_;
  832. __f->__prev_->__next_ = __f;
  833. base::__end_.__prev_ = __l;
  834. }
  835. template <class _Tp, class _Alloc>
  836. inline typename list<_Tp, _Alloc>::iterator list<_Tp, _Alloc>::__iterator(size_type __n) {
  837. return __n <= base::__sz() / 2 ? std::next(begin(), __n) : std::prev(end(), base::__sz() - __n);
  838. }
  839. template <class _Tp, class _Alloc>
  840. list<_Tp, _Alloc>::list(size_type __n) {
  841. for (; __n > 0; --__n)
  842. #ifndef _LIBCPP_CXX03_LANG
  843. emplace_back();
  844. #else
  845. push_back(value_type());
  846. #endif
  847. }
  848. #if _LIBCPP_STD_VER >= 14
  849. template <class _Tp, class _Alloc>
  850. list<_Tp, _Alloc>::list(size_type __n, const allocator_type& __a) : base(__a) {
  851. for (; __n > 0; --__n)
  852. emplace_back();
  853. }
  854. #endif
  855. template <class _Tp, class _Alloc>
  856. list<_Tp, _Alloc>::list(size_type __n, const value_type& __x) {
  857. for (; __n > 0; --__n)
  858. push_back(__x);
  859. }
  860. template <class _Tp, class _Alloc>
  861. template <class _InpIter, __enable_if_t<__has_input_iterator_category<_InpIter>::value, int> >
  862. list<_Tp, _Alloc>::list(_InpIter __f, _InpIter __l) {
  863. for (; __f != __l; ++__f)
  864. __emplace_back(*__f);
  865. }
  866. template <class _Tp, class _Alloc>
  867. template <class _InpIter, __enable_if_t<__has_input_iterator_category<_InpIter>::value, int> >
  868. list<_Tp, _Alloc>::list(_InpIter __f, _InpIter __l, const allocator_type& __a) : base(__a) {
  869. for (; __f != __l; ++__f)
  870. __emplace_back(*__f);
  871. }
  872. template <class _Tp, class _Alloc>
  873. list<_Tp, _Alloc>::list(const list& __c)
  874. : base(__node_alloc_traits::select_on_container_copy_construction(__c.__node_alloc())) {
  875. for (const_iterator __i = __c.begin(), __e = __c.end(); __i != __e; ++__i)
  876. push_back(*__i);
  877. }
  878. template <class _Tp, class _Alloc>
  879. list<_Tp, _Alloc>::list(const list& __c, const __type_identity_t<allocator_type>& __a) : base(__a) {
  880. for (const_iterator __i = __c.begin(), __e = __c.end(); __i != __e; ++__i)
  881. push_back(*__i);
  882. }
  883. #ifndef _LIBCPP_CXX03_LANG
  884. template <class _Tp, class _Alloc>
  885. list<_Tp, _Alloc>::list(initializer_list<value_type> __il, const allocator_type& __a) : base(__a) {
  886. for (typename initializer_list<value_type>::const_iterator __i = __il.begin(), __e = __il.end(); __i != __e; ++__i)
  887. push_back(*__i);
  888. }
  889. template <class _Tp, class _Alloc>
  890. list<_Tp, _Alloc>::list(initializer_list<value_type> __il) {
  891. for (typename initializer_list<value_type>::const_iterator __i = __il.begin(), __e = __il.end(); __i != __e; ++__i)
  892. push_back(*__i);
  893. }
  894. template <class _Tp, class _Alloc>
  895. inline list<_Tp, _Alloc>::list(list&& __c) _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value)
  896. : base(std::move(__c.__node_alloc())) {
  897. splice(end(), __c);
  898. }
  899. template <class _Tp, class _Alloc>
  900. inline list<_Tp, _Alloc>::list(list&& __c, const __type_identity_t<allocator_type>& __a) : base(__a) {
  901. if (__a == __c.get_allocator())
  902. splice(end(), __c);
  903. else {
  904. typedef move_iterator<iterator> _Ip;
  905. assign(_Ip(__c.begin()), _Ip(__c.end()));
  906. }
  907. }
  908. template <class _Tp, class _Alloc>
  909. inline list<_Tp, _Alloc>& list<_Tp, _Alloc>::operator=(list&& __c)
  910. _NOEXCEPT_(__node_alloc_traits::propagate_on_container_move_assignment::value&&
  911. is_nothrow_move_assignable<__node_allocator>::value) {
  912. __move_assign(__c, integral_constant<bool, __node_alloc_traits::propagate_on_container_move_assignment::value>());
  913. return *this;
  914. }
  915. template <class _Tp, class _Alloc>
  916. void list<_Tp, _Alloc>::__move_assign(list& __c, false_type) {
  917. if (base::__node_alloc() != __c.__node_alloc()) {
  918. typedef move_iterator<iterator> _Ip;
  919. assign(_Ip(__c.begin()), _Ip(__c.end()));
  920. } else
  921. __move_assign(__c, true_type());
  922. }
  923. template <class _Tp, class _Alloc>
  924. void list<_Tp, _Alloc>::__move_assign(list& __c, true_type)
  925. _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value) {
  926. clear();
  927. base::__move_assign_alloc(__c);
  928. splice(end(), __c);
  929. }
  930. #endif // _LIBCPP_CXX03_LANG
  931. template <class _Tp, class _Alloc>
  932. inline list<_Tp, _Alloc>& list<_Tp, _Alloc>::operator=(const list& __c) {
  933. if (this != std::addressof(__c)) {
  934. base::__copy_assign_alloc(__c);
  935. assign(__c.begin(), __c.end());
  936. }
  937. return *this;
  938. }
  939. template <class _Tp, class _Alloc>
  940. template <class _InpIter, __enable_if_t<__has_input_iterator_category<_InpIter>::value, int> >
  941. void list<_Tp, _Alloc>::assign(_InpIter __f, _InpIter __l) {
  942. __assign_with_sentinel(__f, __l);
  943. }
  944. template <class _Tp, class _Alloc>
  945. template <class _Iterator, class _Sentinel>
  946. _LIBCPP_HIDE_FROM_ABI void list<_Tp, _Alloc>::__assign_with_sentinel(_Iterator __f, _Sentinel __l) {
  947. iterator __i = begin();
  948. iterator __e = end();
  949. for (; __f != __l && __i != __e; ++__f, (void)++__i)
  950. *__i = *__f;
  951. if (__i == __e)
  952. __insert_with_sentinel(__e, std::move(__f), std::move(__l));
  953. else
  954. erase(__i, __e);
  955. }
  956. template <class _Tp, class _Alloc>
  957. void list<_Tp, _Alloc>::assign(size_type __n, const value_type& __x) {
  958. iterator __i = begin();
  959. iterator __e = end();
  960. for (; __n > 0 && __i != __e; --__n, (void)++__i)
  961. *__i = __x;
  962. if (__i == __e)
  963. insert(__e, __n, __x);
  964. else
  965. erase(__i, __e);
  966. }
  967. template <class _Tp, class _Alloc>
  968. inline _Alloc list<_Tp, _Alloc>::get_allocator() const _NOEXCEPT {
  969. return allocator_type(base::__node_alloc());
  970. }
  971. template <class _Tp, class _Alloc>
  972. typename list<_Tp, _Alloc>::iterator list<_Tp, _Alloc>::insert(const_iterator __p, const value_type& __x) {
  973. __node_pointer __node = this->__create_node(/* prev = */ nullptr, /* next = */ nullptr, __x);
  974. __link_nodes(__p.__ptr_, __node->__as_link(), __node->__as_link());
  975. ++base::__sz();
  976. return iterator(__node->__as_link());
  977. }
  978. template <class _Tp, class _Alloc>
  979. typename list<_Tp, _Alloc>::iterator
  980. list<_Tp, _Alloc>::insert(const_iterator __p, size_type __n, const value_type& __x) {
  981. iterator __r(__p.__ptr_);
  982. if (__n > 0) {
  983. size_type __ds = 0;
  984. __node_pointer __node = this->__create_node(/* prev = */ nullptr, /* next = */ nullptr, __x);
  985. ++__ds;
  986. __r = iterator(__node->__as_link());
  987. iterator __e = __r;
  988. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  989. try {
  990. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  991. for (--__n; __n != 0; --__n, (void)++__e, ++__ds) {
  992. __e.__ptr_->__next_ = this->__create_node(/* prev = */ __e.__ptr_, /* next = */ nullptr, __x)->__as_link();
  993. }
  994. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  995. } catch (...) {
  996. while (true) {
  997. __link_pointer __prev = __e.__ptr_->__prev_;
  998. __node_pointer __current = __e.__ptr_->__as_node();
  999. this->__delete_node(__current);
  1000. if (__prev == 0)
  1001. break;
  1002. __e = iterator(__prev);
  1003. }
  1004. throw;
  1005. }
  1006. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  1007. __link_nodes(__p.__ptr_, __r.__ptr_, __e.__ptr_);
  1008. base::__sz() += __ds;
  1009. }
  1010. return __r;
  1011. }
  1012. template <class _Tp, class _Alloc>
  1013. template <class _InpIter, __enable_if_t<__has_input_iterator_category<_InpIter>::value, int> >
  1014. typename list<_Tp, _Alloc>::iterator list<_Tp, _Alloc>::insert(const_iterator __p, _InpIter __f, _InpIter __l) {
  1015. return __insert_with_sentinel(__p, __f, __l);
  1016. }
  1017. template <class _Tp, class _Alloc>
  1018. template <class _Iterator, class _Sentinel>
  1019. _LIBCPP_HIDE_FROM_ABI typename list<_Tp, _Alloc>::iterator
  1020. list<_Tp, _Alloc>::__insert_with_sentinel(const_iterator __p, _Iterator __f, _Sentinel __l) {
  1021. iterator __r(__p.__ptr_);
  1022. if (__f != __l) {
  1023. size_type __ds = 0;
  1024. __node_pointer __node = this->__create_node(/* prev = */ nullptr, /* next = */ nullptr, *__f);
  1025. ++__ds;
  1026. __r = iterator(__node->__as_link());
  1027. iterator __e = __r;
  1028. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  1029. try {
  1030. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  1031. for (++__f; __f != __l; ++__f, (void)++__e, ++__ds) {
  1032. __e.__ptr_->__next_ = this->__create_node(/* prev = */ __e.__ptr_, /* next = */ nullptr, *__f)->__as_link();
  1033. }
  1034. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  1035. } catch (...) {
  1036. while (true) {
  1037. __link_pointer __prev = __e.__ptr_->__prev_;
  1038. __node_pointer __current = __e.__ptr_->__as_node();
  1039. this->__delete_node(__current);
  1040. if (__prev == 0)
  1041. break;
  1042. __e = iterator(__prev);
  1043. }
  1044. throw;
  1045. }
  1046. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  1047. __link_nodes(__p.__ptr_, __r.__ptr_, __e.__ptr_);
  1048. base::__sz() += __ds;
  1049. }
  1050. return __r;
  1051. }
  1052. template <class _Tp, class _Alloc>
  1053. void list<_Tp, _Alloc>::push_front(const value_type& __x) {
  1054. __node_pointer __node = this->__create_node(/* prev = */ nullptr, /* next = */ nullptr, __x);
  1055. __link_pointer __nl = __node->__as_link();
  1056. __link_nodes_at_front(__nl, __nl);
  1057. ++base::__sz();
  1058. }
  1059. template <class _Tp, class _Alloc>
  1060. void list<_Tp, _Alloc>::push_back(const value_type& __x) {
  1061. __node_pointer __node = this->__create_node(/* prev = */ nullptr, /* next = */ nullptr, __x);
  1062. __link_pointer __nl = __node->__as_link();
  1063. __link_nodes_at_back(__nl, __nl);
  1064. ++base::__sz();
  1065. }
  1066. #ifndef _LIBCPP_CXX03_LANG
  1067. template <class _Tp, class _Alloc>
  1068. void list<_Tp, _Alloc>::push_front(value_type&& __x) {
  1069. __node_pointer __node = this->__create_node(/* prev = */ nullptr, /* next = */ nullptr, std::move(__x));
  1070. __link_pointer __nl = __node->__as_link();
  1071. __link_nodes_at_front(__nl, __nl);
  1072. ++base::__sz();
  1073. }
  1074. template <class _Tp, class _Alloc>
  1075. void list<_Tp, _Alloc>::push_back(value_type&& __x) {
  1076. __node_pointer __node = this->__create_node(/* prev = */ nullptr, /* next = */ nullptr, std::move(__x));
  1077. __link_pointer __nl = __node->__as_link();
  1078. __link_nodes_at_back(__nl, __nl);
  1079. ++base::__sz();
  1080. }
  1081. template <class _Tp, class _Alloc>
  1082. template <class... _Args>
  1083. # if _LIBCPP_STD_VER >= 17
  1084. typename list<_Tp, _Alloc>::reference
  1085. # else
  1086. void
  1087. # endif
  1088. list<_Tp, _Alloc>::emplace_front(_Args&&... __args) {
  1089. __node_pointer __node =
  1090. this->__create_node(/* prev = */ nullptr, /* next = */ nullptr, std::forward<_Args>(__args)...);
  1091. __link_pointer __nl = __node->__as_link();
  1092. __link_nodes_at_front(__nl, __nl);
  1093. ++base::__sz();
  1094. # if _LIBCPP_STD_VER >= 17
  1095. return __node->__get_value();
  1096. # endif
  1097. }
  1098. template <class _Tp, class _Alloc>
  1099. template <class... _Args>
  1100. # if _LIBCPP_STD_VER >= 17
  1101. typename list<_Tp, _Alloc>::reference
  1102. # else
  1103. void
  1104. # endif
  1105. list<_Tp, _Alloc>::emplace_back(_Args&&... __args) {
  1106. __node_pointer __node =
  1107. this->__create_node(/* prev = */ nullptr, /* next = */ nullptr, std::forward<_Args>(__args)...);
  1108. __link_pointer __nl = __node->__as_link();
  1109. __link_nodes_at_back(__nl, __nl);
  1110. ++base::__sz();
  1111. # if _LIBCPP_STD_VER >= 17
  1112. return __node->__get_value();
  1113. # endif
  1114. }
  1115. template <class _Tp, class _Alloc>
  1116. template <class... _Args>
  1117. typename list<_Tp, _Alloc>::iterator list<_Tp, _Alloc>::emplace(const_iterator __p, _Args&&... __args) {
  1118. __node_pointer __node =
  1119. this->__create_node(/* prev = */ nullptr, /* next = */ nullptr, std::forward<_Args>(__args)...);
  1120. __link_pointer __nl = __node->__as_link();
  1121. __link_nodes(__p.__ptr_, __nl, __nl);
  1122. ++base::__sz();
  1123. return iterator(__nl);
  1124. }
  1125. template <class _Tp, class _Alloc>
  1126. typename list<_Tp, _Alloc>::iterator list<_Tp, _Alloc>::insert(const_iterator __p, value_type&& __x) {
  1127. __node_pointer __node = this->__create_node(/* prev = */ nullptr, /* next = */ nullptr, std::move(__x));
  1128. __link_pointer __nl = __node->__as_link();
  1129. __link_nodes(__p.__ptr_, __nl, __nl);
  1130. ++base::__sz();
  1131. return iterator(__nl);
  1132. }
  1133. #endif // _LIBCPP_CXX03_LANG
  1134. template <class _Tp, class _Alloc>
  1135. void list<_Tp, _Alloc>::pop_front() {
  1136. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "list::pop_front() called with empty list");
  1137. __link_pointer __n = base::__end_.__next_;
  1138. base::__unlink_nodes(__n, __n);
  1139. --base::__sz();
  1140. this->__delete_node(__n->__as_node());
  1141. }
  1142. template <class _Tp, class _Alloc>
  1143. void list<_Tp, _Alloc>::pop_back() {
  1144. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "list::pop_back() called on an empty list");
  1145. __link_pointer __n = base::__end_.__prev_;
  1146. base::__unlink_nodes(__n, __n);
  1147. --base::__sz();
  1148. this->__delete_node(__n->__as_node());
  1149. }
  1150. template <class _Tp, class _Alloc>
  1151. typename list<_Tp, _Alloc>::iterator list<_Tp, _Alloc>::erase(const_iterator __p) {
  1152. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__p != end(), "list::erase(iterator) called with a non-dereferenceable iterator");
  1153. __link_pointer __n = __p.__ptr_;
  1154. __link_pointer __r = __n->__next_;
  1155. base::__unlink_nodes(__n, __n);
  1156. --base::__sz();
  1157. this->__delete_node(__n->__as_node());
  1158. return iterator(__r);
  1159. }
  1160. template <class _Tp, class _Alloc>
  1161. typename list<_Tp, _Alloc>::iterator list<_Tp, _Alloc>::erase(const_iterator __f, const_iterator __l) {
  1162. if (__f != __l) {
  1163. base::__unlink_nodes(__f.__ptr_, __l.__ptr_->__prev_);
  1164. while (__f != __l) {
  1165. __link_pointer __n = __f.__ptr_;
  1166. ++__f;
  1167. --base::__sz();
  1168. this->__delete_node(__n->__as_node());
  1169. }
  1170. }
  1171. return iterator(__l.__ptr_);
  1172. }
  1173. template <class _Tp, class _Alloc>
  1174. void list<_Tp, _Alloc>::resize(size_type __n) {
  1175. if (__n < base::__sz())
  1176. erase(__iterator(__n), end());
  1177. else if (__n > base::__sz()) {
  1178. __n -= base::__sz();
  1179. size_type __ds = 0;
  1180. __node_pointer __node = this->__create_node(/* prev = */ nullptr, /* next = */ nullptr);
  1181. ++__ds;
  1182. iterator __r = iterator(__node->__as_link());
  1183. iterator __e = __r;
  1184. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  1185. try {
  1186. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  1187. for (--__n; __n != 0; --__n, (void)++__e, ++__ds) {
  1188. __e.__ptr_->__next_ = this->__create_node(/* prev = */ __e.__ptr_, /* next = */ nullptr)->__as_link();
  1189. }
  1190. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  1191. } catch (...) {
  1192. while (true) {
  1193. __link_pointer __prev = __e.__ptr_->__prev_;
  1194. __node_pointer __current = __e.__ptr_->__as_node();
  1195. this->__delete_node(__current);
  1196. if (__prev == 0)
  1197. break;
  1198. __e = iterator(__prev);
  1199. }
  1200. throw;
  1201. }
  1202. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  1203. __link_nodes_at_back(__r.__ptr_, __e.__ptr_);
  1204. base::__sz() += __ds;
  1205. }
  1206. }
  1207. template <class _Tp, class _Alloc>
  1208. void list<_Tp, _Alloc>::resize(size_type __n, const value_type& __x) {
  1209. if (__n < base::__sz())
  1210. erase(__iterator(__n), end());
  1211. else if (__n > base::__sz()) {
  1212. __n -= base::__sz();
  1213. size_type __ds = 0;
  1214. __node_pointer __node = this->__create_node(/* prev = */ nullptr, /* next = */ nullptr, __x);
  1215. ++__ds;
  1216. __link_pointer __nl = __node->__as_link();
  1217. iterator __r = iterator(__nl);
  1218. iterator __e = __r;
  1219. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  1220. try {
  1221. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  1222. for (--__n; __n != 0; --__n, (void)++__e, ++__ds) {
  1223. __e.__ptr_->__next_ = this->__create_node(/* prev = */ __e.__ptr_, /* next = */ nullptr, __x)->__as_link();
  1224. }
  1225. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  1226. } catch (...) {
  1227. while (true) {
  1228. __link_pointer __prev = __e.__ptr_->__prev_;
  1229. __node_pointer __current = __e.__ptr_->__as_node();
  1230. this->__delete_node(__current);
  1231. if (__prev == 0)
  1232. break;
  1233. __e = iterator(__prev);
  1234. }
  1235. throw;
  1236. }
  1237. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  1238. __link_nodes(base::__end_as_link(), __r.__ptr_, __e.__ptr_);
  1239. base::__sz() += __ds;
  1240. }
  1241. }
  1242. template <class _Tp, class _Alloc>
  1243. void list<_Tp, _Alloc>::splice(const_iterator __p, list& __c) {
  1244. _LIBCPP_ASSERT_VALID_INPUT_RANGE(
  1245. this != std::addressof(__c), "list::splice(iterator, list) called with this == &list");
  1246. if (!__c.empty()) {
  1247. __link_pointer __f = __c.__end_.__next_;
  1248. __link_pointer __l = __c.__end_.__prev_;
  1249. base::__unlink_nodes(__f, __l);
  1250. __link_nodes(__p.__ptr_, __f, __l);
  1251. base::__sz() += __c.__sz();
  1252. __c.__sz() = 0;
  1253. }
  1254. }
  1255. template <class _Tp, class _Alloc>
  1256. void list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __i) {
  1257. if (__p.__ptr_ != __i.__ptr_ && __p.__ptr_ != __i.__ptr_->__next_) {
  1258. __link_pointer __f = __i.__ptr_;
  1259. base::__unlink_nodes(__f, __f);
  1260. __link_nodes(__p.__ptr_, __f, __f);
  1261. --__c.__sz();
  1262. ++base::__sz();
  1263. }
  1264. }
  1265. template <class _Tp, class _Alloc>
  1266. void list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l) {
  1267. if (__f != __l) {
  1268. __link_pointer __first = __f.__ptr_;
  1269. --__l;
  1270. __link_pointer __last = __l.__ptr_;
  1271. if (this != std::addressof(__c)) {
  1272. size_type __s = std::distance(__f, __l) + 1;
  1273. __c.__sz() -= __s;
  1274. base::__sz() += __s;
  1275. }
  1276. base::__unlink_nodes(__first, __last);
  1277. __link_nodes(__p.__ptr_, __first, __last);
  1278. }
  1279. }
  1280. template <class _Tp, class _Alloc>
  1281. typename list<_Tp, _Alloc>::__remove_return_type list<_Tp, _Alloc>::remove(const value_type& __x) {
  1282. list<_Tp, _Alloc> __deleted_nodes(get_allocator()); // collect the nodes we're removing
  1283. for (const_iterator __i = begin(), __e = end(); __i != __e;) {
  1284. if (*__i == __x) {
  1285. const_iterator __j = std::next(__i);
  1286. for (; __j != __e && *__j == __x; ++__j)
  1287. ;
  1288. __deleted_nodes.splice(__deleted_nodes.end(), *this, __i, __j);
  1289. __i = __j;
  1290. if (__i != __e)
  1291. ++__i;
  1292. } else
  1293. ++__i;
  1294. }
  1295. return (__remove_return_type)__deleted_nodes.size();
  1296. }
  1297. template <class _Tp, class _Alloc>
  1298. template <class _Pred>
  1299. typename list<_Tp, _Alloc>::__remove_return_type list<_Tp, _Alloc>::remove_if(_Pred __pred) {
  1300. list<_Tp, _Alloc> __deleted_nodes(get_allocator()); // collect the nodes we're removing
  1301. for (iterator __i = begin(), __e = end(); __i != __e;) {
  1302. if (__pred(*__i)) {
  1303. iterator __j = std::next(__i);
  1304. for (; __j != __e && __pred(*__j); ++__j)
  1305. ;
  1306. __deleted_nodes.splice(__deleted_nodes.end(), *this, __i, __j);
  1307. __i = __j;
  1308. if (__i != __e)
  1309. ++__i;
  1310. } else
  1311. ++__i;
  1312. }
  1313. return (__remove_return_type)__deleted_nodes.size();
  1314. }
  1315. template <class _Tp, class _Alloc>
  1316. template <class _BinaryPred>
  1317. typename list<_Tp, _Alloc>::__remove_return_type list<_Tp, _Alloc>::unique(_BinaryPred __binary_pred) {
  1318. list<_Tp, _Alloc> __deleted_nodes(get_allocator()); // collect the nodes we're removing
  1319. for (iterator __i = begin(), __e = end(); __i != __e;) {
  1320. iterator __j = std::next(__i);
  1321. for (; __j != __e && __binary_pred(*__i, *__j); ++__j)
  1322. ;
  1323. if (++__i != __j) {
  1324. __deleted_nodes.splice(__deleted_nodes.end(), *this, __i, __j);
  1325. __i = __j;
  1326. }
  1327. }
  1328. return (__remove_return_type)__deleted_nodes.size();
  1329. }
  1330. template <class _Tp, class _Alloc>
  1331. inline void list<_Tp, _Alloc>::merge(list& __c) {
  1332. merge(__c, __less<>());
  1333. }
  1334. template <class _Tp, class _Alloc>
  1335. template <class _Comp>
  1336. void list<_Tp, _Alloc>::merge(list& __c, _Comp __comp) {
  1337. if (this != std::addressof(__c)) {
  1338. iterator __f1 = begin();
  1339. iterator __e1 = end();
  1340. iterator __f2 = __c.begin();
  1341. iterator __e2 = __c.end();
  1342. while (__f1 != __e1 && __f2 != __e2) {
  1343. if (__comp(*__f2, *__f1)) {
  1344. size_type __ds = 1;
  1345. iterator __m2 = std::next(__f2);
  1346. for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2, (void)++__ds)
  1347. ;
  1348. base::__sz() += __ds;
  1349. __c.__sz() -= __ds;
  1350. __link_pointer __f = __f2.__ptr_;
  1351. __link_pointer __l = __m2.__ptr_->__prev_;
  1352. __f2 = __m2;
  1353. base::__unlink_nodes(__f, __l);
  1354. __m2 = std::next(__f1);
  1355. __link_nodes(__f1.__ptr_, __f, __l);
  1356. __f1 = __m2;
  1357. } else
  1358. ++__f1;
  1359. }
  1360. splice(__e1, __c);
  1361. }
  1362. }
  1363. template <class _Tp, class _Alloc>
  1364. inline void list<_Tp, _Alloc>::sort() {
  1365. sort(__less<>());
  1366. }
  1367. template <class _Tp, class _Alloc>
  1368. template <class _Comp>
  1369. inline void list<_Tp, _Alloc>::sort(_Comp __comp) {
  1370. __sort(begin(), end(), base::__sz(), __comp);
  1371. }
  1372. template <class _Tp, class _Alloc>
  1373. template <class _Comp>
  1374. typename list<_Tp, _Alloc>::iterator
  1375. list<_Tp, _Alloc>::__sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp) {
  1376. switch (__n) {
  1377. case 0:
  1378. case 1:
  1379. return __f1;
  1380. case 2:
  1381. if (__comp(*--__e2, *__f1)) {
  1382. __link_pointer __f = __e2.__ptr_;
  1383. base::__unlink_nodes(__f, __f);
  1384. __link_nodes(__f1.__ptr_, __f, __f);
  1385. return __e2;
  1386. }
  1387. return __f1;
  1388. }
  1389. size_type __n2 = __n / 2;
  1390. iterator __e1 = std::next(__f1, __n2);
  1391. iterator __r = __f1 = __sort(__f1, __e1, __n2, __comp);
  1392. iterator __f2 = __e1 = __sort(__e1, __e2, __n - __n2, __comp);
  1393. if (__comp(*__f2, *__f1)) {
  1394. iterator __m2 = std::next(__f2);
  1395. for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2)
  1396. ;
  1397. __link_pointer __f = __f2.__ptr_;
  1398. __link_pointer __l = __m2.__ptr_->__prev_;
  1399. __r = __f2;
  1400. __e1 = __f2 = __m2;
  1401. base::__unlink_nodes(__f, __l);
  1402. __m2 = std::next(__f1);
  1403. __link_nodes(__f1.__ptr_, __f, __l);
  1404. __f1 = __m2;
  1405. } else
  1406. ++__f1;
  1407. while (__f1 != __e1 && __f2 != __e2) {
  1408. if (__comp(*__f2, *__f1)) {
  1409. iterator __m2 = std::next(__f2);
  1410. for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2)
  1411. ;
  1412. __link_pointer __f = __f2.__ptr_;
  1413. __link_pointer __l = __m2.__ptr_->__prev_;
  1414. if (__e1 == __f2)
  1415. __e1 = __m2;
  1416. __f2 = __m2;
  1417. base::__unlink_nodes(__f, __l);
  1418. __m2 = std::next(__f1);
  1419. __link_nodes(__f1.__ptr_, __f, __l);
  1420. __f1 = __m2;
  1421. } else
  1422. ++__f1;
  1423. }
  1424. return __r;
  1425. }
  1426. template <class _Tp, class _Alloc>
  1427. void list<_Tp, _Alloc>::reverse() _NOEXCEPT {
  1428. if (base::__sz() > 1) {
  1429. iterator __e = end();
  1430. for (iterator __i = begin(); __i.__ptr_ != __e.__ptr_;) {
  1431. std::swap(__i.__ptr_->__prev_, __i.__ptr_->__next_);
  1432. __i.__ptr_ = __i.__ptr_->__prev_;
  1433. }
  1434. std::swap(__e.__ptr_->__prev_, __e.__ptr_->__next_);
  1435. }
  1436. }
  1437. template <class _Tp, class _Alloc>
  1438. bool list<_Tp, _Alloc>::__invariants() const {
  1439. return size() == std::distance(begin(), end());
  1440. }
  1441. template <class _Tp, class _Alloc>
  1442. inline _LIBCPP_HIDE_FROM_ABI bool operator==(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y) {
  1443. return __x.size() == __y.size() && std::equal(__x.begin(), __x.end(), __y.begin());
  1444. }
  1445. #if _LIBCPP_STD_VER <= 17
  1446. template <class _Tp, class _Alloc>
  1447. inline _LIBCPP_HIDE_FROM_ABI bool operator<(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y) {
  1448. return std::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
  1449. }
  1450. template <class _Tp, class _Alloc>
  1451. inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y) {
  1452. return !(__x == __y);
  1453. }
  1454. template <class _Tp, class _Alloc>
  1455. inline _LIBCPP_HIDE_FROM_ABI bool operator>(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y) {
  1456. return __y < __x;
  1457. }
  1458. template <class _Tp, class _Alloc>
  1459. inline _LIBCPP_HIDE_FROM_ABI bool operator>=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y) {
  1460. return !(__x < __y);
  1461. }
  1462. template <class _Tp, class _Alloc>
  1463. inline _LIBCPP_HIDE_FROM_ABI bool operator<=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y) {
  1464. return !(__y < __x);
  1465. }
  1466. #else // _LIBCPP_STD_VER <= 17
  1467. template <class _Tp, class _Allocator>
  1468. _LIBCPP_HIDE_FROM_ABI __synth_three_way_result<_Tp>
  1469. operator<=>(const list<_Tp, _Allocator>& __x, const list<_Tp, _Allocator>& __y) {
  1470. return std::lexicographical_compare_three_way(
  1471. __x.begin(), __x.end(), __y.begin(), __y.end(), std::__synth_three_way<_Tp, _Tp>);
  1472. }
  1473. #endif // _LIBCPP_STD_VER <= 17
  1474. template <class _Tp, class _Alloc>
  1475. inline _LIBCPP_HIDE_FROM_ABI void swap(list<_Tp, _Alloc>& __x, list<_Tp, _Alloc>& __y)
  1476. _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y))) {
  1477. __x.swap(__y);
  1478. }
  1479. #if _LIBCPP_STD_VER >= 20
  1480. template <class _Tp, class _Allocator, class _Predicate>
  1481. inline _LIBCPP_HIDE_FROM_ABI typename list<_Tp, _Allocator>::size_type
  1482. erase_if(list<_Tp, _Allocator>& __c, _Predicate __pred) {
  1483. return __c.remove_if(__pred);
  1484. }
  1485. template <class _Tp, class _Allocator, class _Up>
  1486. inline _LIBCPP_HIDE_FROM_ABI typename list<_Tp, _Allocator>::size_type
  1487. erase(list<_Tp, _Allocator>& __c, const _Up& __v) {
  1488. return std::erase_if(__c, [&](auto& __elem) { return __elem == __v; });
  1489. }
  1490. template <>
  1491. inline constexpr bool __format::__enable_insertable<std::list<char>> = true;
  1492. # ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  1493. template <>
  1494. inline constexpr bool __format::__enable_insertable<std::list<wchar_t>> = true;
  1495. # endif
  1496. #endif // _LIBCPP_STD_VER >= 20
  1497. _LIBCPP_END_NAMESPACE_STD
  1498. #if _LIBCPP_STD_VER >= 17
  1499. _LIBCPP_BEGIN_NAMESPACE_STD
  1500. namespace pmr {
  1501. template <class _ValueT>
  1502. using list _LIBCPP_AVAILABILITY_PMR = std::list<_ValueT, polymorphic_allocator<_ValueT>>;
  1503. } // namespace pmr
  1504. _LIBCPP_END_NAMESPACE_STD
  1505. #endif
  1506. _LIBCPP_POP_MACROS
  1507. #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
  1508. # include <algorithm>
  1509. # include <atomic>
  1510. # include <concepts>
  1511. # include <cstdint>
  1512. # include <cstdlib>
  1513. # include <functional>
  1514. # include <iosfwd>
  1515. # include <iterator>
  1516. # include <stdexcept>
  1517. # include <type_traits>
  1518. # include <typeinfo>
  1519. #endif
  1520. #endif // _LIBCPP_LIST