list 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114
  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> // all public C++ headers provide the assertion handler
  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_default_constructible.h>
  204. #include <__type_traits/is_nothrow_move_assignable.h>
  205. #include <__type_traits/is_nothrow_move_constructible.h>
  206. #include <__type_traits/is_pointer.h>
  207. #include <__type_traits/is_same.h>
  208. #include <__type_traits/type_identity.h>
  209. #include <__utility/forward.h>
  210. #include <__utility/move.h>
  211. #include <__utility/swap.h>
  212. #include <cstring>
  213. #include <limits>
  214. #include <new> // __launder
  215. #include <version>
  216. // standard-mandated includes
  217. // [iterator.range]
  218. #include <__iterator/access.h>
  219. #include <__iterator/data.h>
  220. #include <__iterator/empty.h>
  221. #include <__iterator/reverse_access.h>
  222. #include <__iterator/size.h>
  223. // [list.syn]
  224. #include <compare>
  225. #include <initializer_list>
  226. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  227. # pragma GCC system_header
  228. #endif
  229. _LIBCPP_PUSH_MACROS
  230. #include <__undef_macros>
  231. _LIBCPP_BEGIN_NAMESPACE_STD
  232. template <class _Tp, class _VoidPtr> struct __list_node;
  233. template <class _Tp, class _VoidPtr> struct __list_node_base;
  234. template <class _Tp, class _VoidPtr>
  235. struct __list_node_pointer_traits {
  236. typedef __rebind_pointer_t<_VoidPtr, __list_node<_Tp, _VoidPtr> >
  237. __node_pointer;
  238. typedef __rebind_pointer_t<_VoidPtr, __list_node_base<_Tp, _VoidPtr> >
  239. __base_pointer;
  240. #if defined(_LIBCPP_ABI_LIST_REMOVE_NODE_POINTER_UB)
  241. typedef __base_pointer __link_pointer;
  242. #else
  243. typedef __conditional_t<is_pointer<_VoidPtr>::value, __base_pointer, __node_pointer> __link_pointer;
  244. #endif
  245. typedef __conditional_t<is_same<__link_pointer, __node_pointer>::value, __base_pointer, __node_pointer>
  246. __non_link_pointer;
  247. static _LIBCPP_INLINE_VISIBILITY
  248. __link_pointer __unsafe_link_pointer_cast(__link_pointer __p) {
  249. return __p;
  250. }
  251. static _LIBCPP_INLINE_VISIBILITY
  252. __link_pointer __unsafe_link_pointer_cast(__non_link_pointer __p) {
  253. return static_cast<__link_pointer>(static_cast<_VoidPtr>(__p));
  254. }
  255. };
  256. template <class _Tp, class _VoidPtr>
  257. struct __list_node_base
  258. {
  259. typedef __list_node_pointer_traits<_Tp, _VoidPtr> _NodeTraits;
  260. typedef typename _NodeTraits::__node_pointer __node_pointer;
  261. typedef typename _NodeTraits::__base_pointer __base_pointer;
  262. typedef typename _NodeTraits::__link_pointer __link_pointer;
  263. __link_pointer __prev_;
  264. __link_pointer __next_;
  265. _LIBCPP_INLINE_VISIBILITY
  266. __list_node_base() : __prev_(_NodeTraits::__unsafe_link_pointer_cast(__self())),
  267. __next_(_NodeTraits::__unsafe_link_pointer_cast(__self())) {}
  268. _LIBCPP_HIDE_FROM_ABI explicit __list_node_base(__link_pointer __prev, __link_pointer __next)
  269. : __prev_(__prev), __next_(__next) {}
  270. _LIBCPP_INLINE_VISIBILITY
  271. __base_pointer __self() {
  272. return pointer_traits<__base_pointer>::pointer_to(*this);
  273. }
  274. _LIBCPP_INLINE_VISIBILITY
  275. __node_pointer __as_node() {
  276. return static_cast<__node_pointer>(__self());
  277. }
  278. };
  279. template <class _Tp, class _VoidPtr>
  280. struct __list_node
  281. : public __list_node_base<_Tp, _VoidPtr>
  282. {
  283. // We allow starting the lifetime of nodes without initializing the value held by the node,
  284. // since that is handled by the list itself in order to be allocator-aware.
  285. #ifndef _LIBCPP_CXX03_LANG
  286. private:
  287. union {
  288. _Tp __value_;
  289. };
  290. public:
  291. _LIBCPP_HIDE_FROM_ABI _Tp& __get_value() { return __value_; }
  292. #else
  293. private:
  294. _ALIGNAS_TYPE(_Tp) char __buffer_[sizeof(_Tp)];
  295. public:
  296. _LIBCPP_HIDE_FROM_ABI _Tp& __get_value() {
  297. return *std::__launder(reinterpret_cast<_Tp*>(&__buffer_));
  298. }
  299. #endif
  300. typedef __list_node_base<_Tp, _VoidPtr> __base;
  301. typedef typename __base::__link_pointer __link_pointer;
  302. _LIBCPP_HIDE_FROM_ABI explicit __list_node(__link_pointer __prev, __link_pointer __next) : __base(__prev, __next) {}
  303. _LIBCPP_HIDE_FROM_ABI ~__list_node() {}
  304. _LIBCPP_INLINE_VISIBILITY
  305. __link_pointer __as_link() {
  306. return static_cast<__link_pointer>(__base::__self());
  307. }
  308. };
  309. template <class _Tp, class _Alloc = allocator<_Tp> > class _LIBCPP_TEMPLATE_VIS list;
  310. template <class _Tp, class _Alloc> class __list_imp;
  311. template <class _Tp, class _VoidPtr> class _LIBCPP_TEMPLATE_VIS __list_const_iterator;
  312. template <class _Tp, class _VoidPtr>
  313. class _LIBCPP_TEMPLATE_VIS __list_iterator
  314. {
  315. typedef __list_node_pointer_traits<_Tp, _VoidPtr> _NodeTraits;
  316. typedef typename _NodeTraits::__link_pointer __link_pointer;
  317. __link_pointer __ptr_;
  318. _LIBCPP_INLINE_VISIBILITY
  319. explicit __list_iterator(__link_pointer __p) _NOEXCEPT
  320. : __ptr_(__p)
  321. {
  322. }
  323. template<class, class> friend class list;
  324. template<class, class> friend class __list_imp;
  325. template<class, class> friend class __list_const_iterator;
  326. public:
  327. typedef bidirectional_iterator_tag iterator_category;
  328. typedef _Tp value_type;
  329. typedef value_type& reference;
  330. typedef __rebind_pointer_t<_VoidPtr, value_type> pointer;
  331. typedef typename pointer_traits<pointer>::difference_type difference_type;
  332. _LIBCPP_INLINE_VISIBILITY
  333. __list_iterator() _NOEXCEPT : __ptr_(nullptr)
  334. {
  335. }
  336. _LIBCPP_INLINE_VISIBILITY
  337. reference operator*() const
  338. {
  339. return __ptr_->__as_node()->__get_value();
  340. }
  341. _LIBCPP_INLINE_VISIBILITY
  342. pointer operator->() const
  343. {
  344. return pointer_traits<pointer>::pointer_to(__ptr_->__as_node()->__get_value());
  345. }
  346. _LIBCPP_INLINE_VISIBILITY
  347. __list_iterator& operator++()
  348. {
  349. __ptr_ = __ptr_->__next_;
  350. return *this;
  351. }
  352. _LIBCPP_INLINE_VISIBILITY
  353. __list_iterator operator++(int) {__list_iterator __t(*this); ++(*this); return __t;}
  354. _LIBCPP_INLINE_VISIBILITY
  355. __list_iterator& operator--()
  356. {
  357. __ptr_ = __ptr_->__prev_;
  358. return *this;
  359. }
  360. _LIBCPP_INLINE_VISIBILITY
  361. __list_iterator operator--(int) {__list_iterator __t(*this); --(*this); return __t;}
  362. friend _LIBCPP_INLINE_VISIBILITY
  363. bool operator==(const __list_iterator& __x, const __list_iterator& __y)
  364. {
  365. return __x.__ptr_ == __y.__ptr_;
  366. }
  367. friend _LIBCPP_INLINE_VISIBILITY
  368. bool operator!=(const __list_iterator& __x, const __list_iterator& __y)
  369. {return !(__x == __y);}
  370. };
  371. template <class _Tp, class _VoidPtr>
  372. class _LIBCPP_TEMPLATE_VIS __list_const_iterator
  373. {
  374. typedef __list_node_pointer_traits<_Tp, _VoidPtr> _NodeTraits;
  375. typedef typename _NodeTraits::__link_pointer __link_pointer;
  376. __link_pointer __ptr_;
  377. _LIBCPP_INLINE_VISIBILITY
  378. explicit __list_const_iterator(__link_pointer __p) _NOEXCEPT
  379. : __ptr_(__p)
  380. {
  381. }
  382. template<class, class> friend class list;
  383. template<class, class> friend class __list_imp;
  384. public:
  385. typedef bidirectional_iterator_tag iterator_category;
  386. typedef _Tp value_type;
  387. typedef const value_type& reference;
  388. typedef __rebind_pointer_t<_VoidPtr, const value_type> pointer;
  389. typedef typename pointer_traits<pointer>::difference_type difference_type;
  390. _LIBCPP_INLINE_VISIBILITY
  391. __list_const_iterator() _NOEXCEPT : __ptr_(nullptr)
  392. {
  393. }
  394. _LIBCPP_INLINE_VISIBILITY
  395. __list_const_iterator(const __list_iterator<_Tp, _VoidPtr>& __p) _NOEXCEPT
  396. : __ptr_(__p.__ptr_)
  397. {
  398. }
  399. _LIBCPP_INLINE_VISIBILITY
  400. reference operator*() const
  401. {
  402. return __ptr_->__as_node()->__get_value();
  403. }
  404. _LIBCPP_INLINE_VISIBILITY
  405. pointer operator->() const
  406. {
  407. return pointer_traits<pointer>::pointer_to(__ptr_->__as_node()->__get_value());
  408. }
  409. _LIBCPP_INLINE_VISIBILITY
  410. __list_const_iterator& operator++()
  411. {
  412. __ptr_ = __ptr_->__next_;
  413. return *this;
  414. }
  415. _LIBCPP_INLINE_VISIBILITY
  416. __list_const_iterator operator++(int) {__list_const_iterator __t(*this); ++(*this); return __t;}
  417. _LIBCPP_INLINE_VISIBILITY
  418. __list_const_iterator& operator--()
  419. {
  420. __ptr_ = __ptr_->__prev_;
  421. return *this;
  422. }
  423. _LIBCPP_INLINE_VISIBILITY
  424. __list_const_iterator operator--(int) {__list_const_iterator __t(*this); --(*this); return __t;}
  425. friend _LIBCPP_INLINE_VISIBILITY
  426. bool operator==(const __list_const_iterator& __x, const __list_const_iterator& __y)
  427. {
  428. return __x.__ptr_ == __y.__ptr_;
  429. }
  430. friend _LIBCPP_INLINE_VISIBILITY
  431. bool operator!=(const __list_const_iterator& __x, const __list_const_iterator& __y)
  432. {return !(__x == __y);}
  433. };
  434. template <class _Tp, class _Alloc>
  435. class __list_imp
  436. {
  437. __list_imp(const __list_imp&);
  438. __list_imp& operator=(const __list_imp&);
  439. public:
  440. typedef _Alloc allocator_type;
  441. typedef allocator_traits<allocator_type> __alloc_traits;
  442. typedef typename __alloc_traits::size_type size_type;
  443. protected:
  444. typedef _Tp value_type;
  445. typedef typename __alloc_traits::void_pointer __void_pointer;
  446. typedef __list_iterator<value_type, __void_pointer> iterator;
  447. typedef __list_const_iterator<value_type, __void_pointer> const_iterator;
  448. typedef __list_node_base<value_type, __void_pointer> __node_base;
  449. typedef __list_node<value_type, __void_pointer> __node_type;
  450. typedef __rebind_alloc<__alloc_traits, __node_type> __node_allocator;
  451. typedef allocator_traits<__node_allocator> __node_alloc_traits;
  452. typedef typename __node_alloc_traits::pointer __node_pointer;
  453. typedef typename __node_alloc_traits::pointer __node_const_pointer;
  454. typedef __list_node_pointer_traits<value_type, __void_pointer> __node_pointer_traits;
  455. typedef typename __node_pointer_traits::__link_pointer __link_pointer;
  456. typedef __link_pointer __link_const_pointer;
  457. typedef typename __alloc_traits::pointer pointer;
  458. typedef typename __alloc_traits::const_pointer const_pointer;
  459. typedef typename __alloc_traits::difference_type difference_type;
  460. typedef __rebind_alloc<__alloc_traits, __node_base> __node_base_allocator;
  461. typedef typename allocator_traits<__node_base_allocator>::pointer __node_base_pointer;
  462. static_assert((!is_same<allocator_type, __node_allocator>::value),
  463. "internal allocator type must differ from user-specified "
  464. "type; otherwise overload resolution breaks");
  465. __node_base __end_;
  466. __compressed_pair<size_type, __node_allocator> __size_alloc_;
  467. _LIBCPP_INLINE_VISIBILITY
  468. __link_pointer __end_as_link() const _NOEXCEPT {
  469. return __node_pointer_traits::__unsafe_link_pointer_cast(
  470. const_cast<__node_base&>(__end_).__self());
  471. }
  472. _LIBCPP_INLINE_VISIBILITY
  473. size_type& __sz() _NOEXCEPT {return __size_alloc_.first();}
  474. _LIBCPP_INLINE_VISIBILITY
  475. const size_type& __sz() const _NOEXCEPT
  476. {return __size_alloc_.first();}
  477. _LIBCPP_INLINE_VISIBILITY
  478. __node_allocator& __node_alloc() _NOEXCEPT
  479. {return __size_alloc_.second();}
  480. _LIBCPP_INLINE_VISIBILITY
  481. const __node_allocator& __node_alloc() const _NOEXCEPT
  482. {return __size_alloc_.second();}
  483. _LIBCPP_INLINE_VISIBILITY
  484. size_type __node_alloc_max_size() const _NOEXCEPT {
  485. return __node_alloc_traits::max_size(__node_alloc());
  486. }
  487. _LIBCPP_INLINE_VISIBILITY
  488. static void __unlink_nodes(__link_pointer __f, __link_pointer __l) _NOEXCEPT;
  489. _LIBCPP_INLINE_VISIBILITY
  490. __list_imp()
  491. _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value);
  492. _LIBCPP_INLINE_VISIBILITY
  493. __list_imp(const allocator_type& __a);
  494. _LIBCPP_INLINE_VISIBILITY
  495. __list_imp(const __node_allocator& __a);
  496. #ifndef _LIBCPP_CXX03_LANG
  497. _LIBCPP_HIDE_FROM_ABI __list_imp(__node_allocator&& __a) _NOEXCEPT;
  498. #endif
  499. _LIBCPP_HIDE_FROM_ABI ~__list_imp();
  500. _LIBCPP_HIDE_FROM_ABI void clear() _NOEXCEPT;
  501. _LIBCPP_INLINE_VISIBILITY
  502. bool empty() const _NOEXCEPT {return __sz() == 0;}
  503. _LIBCPP_INLINE_VISIBILITY
  504. iterator begin() _NOEXCEPT
  505. {
  506. return iterator(__end_.__next_);
  507. }
  508. _LIBCPP_INLINE_VISIBILITY
  509. const_iterator begin() const _NOEXCEPT
  510. {
  511. return const_iterator(__end_.__next_);
  512. }
  513. _LIBCPP_INLINE_VISIBILITY
  514. iterator end() _NOEXCEPT
  515. {
  516. return iterator(__end_as_link());
  517. }
  518. _LIBCPP_INLINE_VISIBILITY
  519. const_iterator end() const _NOEXCEPT
  520. {
  521. return const_iterator(__end_as_link());
  522. }
  523. _LIBCPP_HIDE_FROM_ABI void swap(__list_imp& __c)
  524. #if _LIBCPP_STD_VER >= 14
  525. _NOEXCEPT;
  526. #else
  527. _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
  528. __is_nothrow_swappable<allocator_type>::value);
  529. #endif
  530. _LIBCPP_INLINE_VISIBILITY
  531. void __copy_assign_alloc(const __list_imp& __c)
  532. {__copy_assign_alloc(__c, integral_constant<bool,
  533. __node_alloc_traits::propagate_on_container_copy_assignment::value>());}
  534. _LIBCPP_INLINE_VISIBILITY
  535. void __move_assign_alloc(__list_imp& __c)
  536. _NOEXCEPT_(
  537. !__node_alloc_traits::propagate_on_container_move_assignment::value ||
  538. is_nothrow_move_assignable<__node_allocator>::value)
  539. {__move_assign_alloc(__c, integral_constant<bool,
  540. __node_alloc_traits::propagate_on_container_move_assignment::value>());}
  541. template <class ..._Args>
  542. _LIBCPP_HIDE_FROM_ABI __node_pointer __create_node(__link_pointer __prev, __link_pointer __next, _Args&& ...__args) {
  543. __node_allocator& __alloc = __node_alloc();
  544. __allocation_guard<__node_allocator> __guard(__alloc, 1);
  545. // Begin the lifetime of the node itself. Note that this doesn't begin the lifetime of the value
  546. // held inside the node, since we need to use the allocator's construct() method for that.
  547. //
  548. // We don't use the allocator's construct() method to construct the node itself since the
  549. // Cpp17FooInsertable named requirements don't require the allocator's construct() method
  550. // to work on anything other than the value_type.
  551. std::__construct_at(std::addressof(*__guard.__get()), __prev, __next);
  552. // Now construct the value_type using the allocator's construct() method.
  553. __node_alloc_traits::construct(__alloc, std::addressof(__guard.__get()->__get_value()), std::forward<_Args>(__args)...);
  554. return __guard.__release_ptr();
  555. }
  556. template <class ..._Args>
  557. _LIBCPP_HIDE_FROM_ABI void __delete_node(__node_pointer __node) {
  558. // For the same reason as above, we use the allocator's destroy() method for the value_type,
  559. // but not for the node itself.
  560. __node_allocator& __alloc = __node_alloc();
  561. __node_alloc_traits::destroy(__alloc, std::addressof(__node->__get_value()));
  562. std::__destroy_at(std::addressof(*__node));
  563. __node_alloc_traits::deallocate(__alloc, __node, 1);
  564. }
  565. private:
  566. _LIBCPP_INLINE_VISIBILITY
  567. void __copy_assign_alloc(const __list_imp& __c, true_type)
  568. {
  569. if (__node_alloc() != __c.__node_alloc())
  570. clear();
  571. __node_alloc() = __c.__node_alloc();
  572. }
  573. _LIBCPP_INLINE_VISIBILITY
  574. void __copy_assign_alloc(const __list_imp&, false_type)
  575. {}
  576. _LIBCPP_INLINE_VISIBILITY
  577. void __move_assign_alloc(__list_imp& __c, true_type)
  578. _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
  579. {
  580. __node_alloc() = _VSTD::move(__c.__node_alloc());
  581. }
  582. _LIBCPP_INLINE_VISIBILITY
  583. void __move_assign_alloc(__list_imp&, false_type)
  584. _NOEXCEPT
  585. {}
  586. };
  587. // Unlink nodes [__f, __l]
  588. template <class _Tp, class _Alloc>
  589. inline
  590. void
  591. __list_imp<_Tp, _Alloc>::__unlink_nodes(__link_pointer __f, __link_pointer __l)
  592. _NOEXCEPT
  593. {
  594. __f->__prev_->__next_ = __l->__next_;
  595. __l->__next_->__prev_ = __f->__prev_;
  596. }
  597. template <class _Tp, class _Alloc>
  598. inline
  599. __list_imp<_Tp, _Alloc>::__list_imp()
  600. _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value)
  601. : __size_alloc_(0, __default_init_tag())
  602. {
  603. }
  604. template <class _Tp, class _Alloc>
  605. inline
  606. __list_imp<_Tp, _Alloc>::__list_imp(const allocator_type& __a)
  607. : __size_alloc_(0, __node_allocator(__a))
  608. {
  609. }
  610. template <class _Tp, class _Alloc>
  611. inline __list_imp<_Tp, _Alloc>::__list_imp(const __node_allocator& __a)
  612. : __size_alloc_(0, __a) {}
  613. #ifndef _LIBCPP_CXX03_LANG
  614. template <class _Tp, class _Alloc>
  615. inline __list_imp<_Tp, _Alloc>::__list_imp(__node_allocator&& __a) _NOEXCEPT
  616. : __size_alloc_(0, _VSTD::move(__a)) {}
  617. #endif
  618. template <class _Tp, class _Alloc>
  619. __list_imp<_Tp, _Alloc>::~__list_imp() {
  620. clear();
  621. }
  622. template <class _Tp, class _Alloc>
  623. void
  624. __list_imp<_Tp, _Alloc>::clear() _NOEXCEPT
  625. {
  626. if (!empty())
  627. {
  628. __link_pointer __f = __end_.__next_;
  629. __link_pointer __l = __end_as_link();
  630. __unlink_nodes(__f, __l->__prev_);
  631. __sz() = 0;
  632. while (__f != __l)
  633. {
  634. __node_pointer __np = __f->__as_node();
  635. __f = __f->__next_;
  636. __delete_node(__np);
  637. }
  638. }
  639. }
  640. template <class _Tp, class _Alloc>
  641. void
  642. __list_imp<_Tp, _Alloc>::swap(__list_imp& __c)
  643. #if _LIBCPP_STD_VER >= 14
  644. _NOEXCEPT
  645. #else
  646. _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
  647. __is_nothrow_swappable<allocator_type>::value)
  648. #endif
  649. {
  650. _LIBCPP_ASSERT_COMPATIBLE_ALLOCATOR(__alloc_traits::propagate_on_container_swap::value ||
  651. this->__node_alloc() == __c.__node_alloc(),
  652. "list::swap: Either propagate_on_container_swap must be true"
  653. " or the allocators must compare equal");
  654. using _VSTD::swap;
  655. _VSTD::__swap_allocator(__node_alloc(), __c.__node_alloc());
  656. swap(__sz(), __c.__sz());
  657. swap(__end_, __c.__end_);
  658. if (__sz() == 0)
  659. __end_.__next_ = __end_.__prev_ = __end_as_link();
  660. else
  661. __end_.__prev_->__next_ = __end_.__next_->__prev_ = __end_as_link();
  662. if (__c.__sz() == 0)
  663. __c.__end_.__next_ = __c.__end_.__prev_ = __c.__end_as_link();
  664. else
  665. __c.__end_.__prev_->__next_ = __c.__end_.__next_->__prev_ = __c.__end_as_link();
  666. }
  667. template <class _Tp, class _Alloc /*= allocator<_Tp>*/>
  668. class _LIBCPP_TEMPLATE_VIS list
  669. : private __list_imp<_Tp, _Alloc>
  670. {
  671. typedef __list_imp<_Tp, _Alloc> base;
  672. typedef typename base::__node_type __node_type;
  673. typedef typename base::__node_allocator __node_allocator;
  674. typedef typename base::__node_pointer __node_pointer;
  675. typedef typename base::__node_alloc_traits __node_alloc_traits;
  676. typedef typename base::__node_base __node_base;
  677. typedef typename base::__node_base_pointer __node_base_pointer;
  678. typedef typename base::__link_pointer __link_pointer;
  679. public:
  680. typedef _Tp value_type;
  681. typedef _Alloc allocator_type;
  682. static_assert((is_same<value_type, typename allocator_type::value_type>::value),
  683. "Allocator::value_type must be same type as value_type");
  684. typedef value_type& reference;
  685. typedef const value_type& const_reference;
  686. typedef typename base::pointer pointer;
  687. typedef typename base::const_pointer const_pointer;
  688. typedef typename base::size_type size_type;
  689. typedef typename base::difference_type difference_type;
  690. typedef typename base::iterator iterator;
  691. typedef typename base::const_iterator const_iterator;
  692. typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
  693. typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
  694. #if _LIBCPP_STD_VER >= 20
  695. typedef size_type __remove_return_type;
  696. #else
  697. typedef void __remove_return_type;
  698. #endif
  699. static_assert(is_same<allocator_type, __rebind_alloc<allocator_traits<allocator_type>, value_type> >::value,
  700. "[allocator.requirements] states that rebinding an allocator to the same type should result in the "
  701. "original allocator");
  702. _LIBCPP_INLINE_VISIBILITY
  703. list()
  704. _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value)
  705. {
  706. }
  707. _LIBCPP_INLINE_VISIBILITY
  708. explicit list(const allocator_type& __a) : base(__a)
  709. {
  710. }
  711. _LIBCPP_HIDE_FROM_ABI explicit list(size_type __n);
  712. #if _LIBCPP_STD_VER >= 14
  713. _LIBCPP_HIDE_FROM_ABI explicit list(size_type __n, const allocator_type& __a);
  714. #endif
  715. _LIBCPP_HIDE_FROM_ABI list(size_type __n, const value_type& __x);
  716. template <class = __enable_if_t<__is_allocator<_Alloc>::value> >
  717. _LIBCPP_HIDE_FROM_ABI list(size_type __n, const value_type& __x, const allocator_type& __a) : base(__a)
  718. {
  719. for (; __n > 0; --__n)
  720. push_back(__x);
  721. }
  722. template <class _InpIter>
  723. _LIBCPP_HIDE_FROM_ABI list(_InpIter __f, _InpIter __l,
  724. __enable_if_t<__has_input_iterator_category<_InpIter>::value>* = 0);
  725. template <class _InpIter>
  726. _LIBCPP_HIDE_FROM_ABI list(_InpIter __f, _InpIter __l, const allocator_type& __a,
  727. __enable_if_t<__has_input_iterator_category<_InpIter>::value>* = 0);
  728. #if _LIBCPP_STD_VER >= 23
  729. template <_ContainerCompatibleRange<_Tp> _Range>
  730. _LIBCPP_HIDE_FROM_ABI list(from_range_t, _Range&& __range,
  731. const allocator_type& __a = allocator_type()) : base(__a) {
  732. prepend_range(std::forward<_Range>(__range));
  733. }
  734. #endif
  735. _LIBCPP_HIDE_FROM_ABI list(const list& __c);
  736. _LIBCPP_HIDE_FROM_ABI list(const list& __c, const __type_identity_t<allocator_type>& __a);
  737. _LIBCPP_INLINE_VISIBILITY
  738. list& operator=(const list& __c);
  739. #ifndef _LIBCPP_CXX03_LANG
  740. _LIBCPP_HIDE_FROM_ABI list(initializer_list<value_type> __il);
  741. _LIBCPP_HIDE_FROM_ABI list(initializer_list<value_type> __il, const allocator_type& __a);
  742. _LIBCPP_INLINE_VISIBILITY
  743. list(list&& __c)
  744. _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value);
  745. _LIBCPP_INLINE_VISIBILITY
  746. list(list&& __c, const __type_identity_t<allocator_type>& __a);
  747. _LIBCPP_INLINE_VISIBILITY
  748. list& operator=(list&& __c)
  749. _NOEXCEPT_(
  750. __node_alloc_traits::propagate_on_container_move_assignment::value &&
  751. is_nothrow_move_assignable<__node_allocator>::value);
  752. _LIBCPP_INLINE_VISIBILITY
  753. list& operator=(initializer_list<value_type> __il)
  754. {assign(__il.begin(), __il.end()); return *this;}
  755. _LIBCPP_INLINE_VISIBILITY
  756. void assign(initializer_list<value_type> __il)
  757. {assign(__il.begin(), __il.end());}
  758. #endif // _LIBCPP_CXX03_LANG
  759. template <class _InpIter>
  760. _LIBCPP_HIDE_FROM_ABI void assign(_InpIter __f, _InpIter __l,
  761. __enable_if_t<__has_input_iterator_category<_InpIter>::value>* = 0);
  762. #if _LIBCPP_STD_VER >= 23
  763. template <_ContainerCompatibleRange<_Tp> _Range>
  764. _LIBCPP_HIDE_FROM_ABI
  765. void assign_range(_Range&& __range) {
  766. __assign_with_sentinel(ranges::begin(__range), ranges::end(__range));
  767. }
  768. #endif
  769. _LIBCPP_HIDE_FROM_ABI void assign(size_type __n, const value_type& __x);
  770. _LIBCPP_INLINE_VISIBILITY
  771. allocator_type get_allocator() const _NOEXCEPT;
  772. _LIBCPP_INLINE_VISIBILITY
  773. size_type size() const _NOEXCEPT {return base::__sz();}
  774. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
  775. bool empty() const _NOEXCEPT {return base::empty();}
  776. _LIBCPP_INLINE_VISIBILITY
  777. size_type max_size() const _NOEXCEPT
  778. {
  779. return _VSTD::min<size_type>(
  780. base::__node_alloc_max_size(),
  781. numeric_limits<difference_type >::max());
  782. }
  783. _LIBCPP_INLINE_VISIBILITY
  784. iterator begin() _NOEXCEPT {return base::begin();}
  785. _LIBCPP_INLINE_VISIBILITY
  786. const_iterator begin() const _NOEXCEPT {return base::begin();}
  787. _LIBCPP_INLINE_VISIBILITY
  788. iterator end() _NOEXCEPT {return base::end();}
  789. _LIBCPP_INLINE_VISIBILITY
  790. const_iterator end() const _NOEXCEPT {return base::end();}
  791. _LIBCPP_INLINE_VISIBILITY
  792. const_iterator cbegin() const _NOEXCEPT {return base::begin();}
  793. _LIBCPP_INLINE_VISIBILITY
  794. const_iterator cend() const _NOEXCEPT {return base::end();}
  795. _LIBCPP_INLINE_VISIBILITY
  796. reverse_iterator rbegin() _NOEXCEPT
  797. {return reverse_iterator(end());}
  798. _LIBCPP_INLINE_VISIBILITY
  799. const_reverse_iterator rbegin() const _NOEXCEPT
  800. {return const_reverse_iterator(end());}
  801. _LIBCPP_INLINE_VISIBILITY
  802. reverse_iterator rend() _NOEXCEPT
  803. {return reverse_iterator(begin());}
  804. _LIBCPP_INLINE_VISIBILITY
  805. const_reverse_iterator rend() const _NOEXCEPT
  806. {return const_reverse_iterator(begin());}
  807. _LIBCPP_INLINE_VISIBILITY
  808. const_reverse_iterator crbegin() const _NOEXCEPT
  809. {return const_reverse_iterator(end());}
  810. _LIBCPP_INLINE_VISIBILITY
  811. const_reverse_iterator crend() const _NOEXCEPT
  812. {return const_reverse_iterator(begin());}
  813. _LIBCPP_INLINE_VISIBILITY
  814. reference front()
  815. {
  816. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "list::front called on empty list");
  817. return base::__end_.__next_->__as_node()->__get_value();
  818. }
  819. _LIBCPP_INLINE_VISIBILITY
  820. const_reference front() const
  821. {
  822. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "list::front called on empty list");
  823. return base::__end_.__next_->__as_node()->__get_value();
  824. }
  825. _LIBCPP_INLINE_VISIBILITY
  826. reference back()
  827. {
  828. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "list::back called on empty list");
  829. return base::__end_.__prev_->__as_node()->__get_value();
  830. }
  831. _LIBCPP_INLINE_VISIBILITY
  832. const_reference back() const
  833. {
  834. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "list::back called on empty list");
  835. return base::__end_.__prev_->__as_node()->__get_value();
  836. }
  837. #ifndef _LIBCPP_CXX03_LANG
  838. _LIBCPP_HIDE_FROM_ABI void push_front(value_type&& __x);
  839. _LIBCPP_HIDE_FROM_ABI void push_back(value_type&& __x);
  840. #if _LIBCPP_STD_VER >= 23
  841. template <_ContainerCompatibleRange<_Tp> _Range>
  842. _LIBCPP_HIDE_FROM_ABI
  843. void prepend_range(_Range&& __range) {
  844. insert_range(begin(), std::forward<_Range>(__range));
  845. }
  846. template <_ContainerCompatibleRange<_Tp> _Range>
  847. _LIBCPP_HIDE_FROM_ABI
  848. void append_range(_Range&& __range) {
  849. insert_range(end(), std::forward<_Range>(__range));
  850. }
  851. #endif
  852. template <class... _Args>
  853. #if _LIBCPP_STD_VER >= 17
  854. _LIBCPP_HIDE_FROM_ABI reference emplace_front(_Args&&... __args);
  855. #else
  856. _LIBCPP_HIDE_FROM_ABI void emplace_front(_Args&&... __args);
  857. #endif
  858. template <class... _Args>
  859. #if _LIBCPP_STD_VER >= 17
  860. _LIBCPP_HIDE_FROM_ABI reference emplace_back(_Args&&... __args);
  861. #else
  862. _LIBCPP_HIDE_FROM_ABI void emplace_back(_Args&&... __args);
  863. #endif
  864. template <class... _Args>
  865. _LIBCPP_HIDE_FROM_ABI iterator emplace(const_iterator __p, _Args&&... __args);
  866. _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, value_type&& __x);
  867. _LIBCPP_INLINE_VISIBILITY
  868. iterator insert(const_iterator __p, initializer_list<value_type> __il)
  869. {return insert(__p, __il.begin(), __il.end());}
  870. #endif // _LIBCPP_CXX03_LANG
  871. _LIBCPP_HIDE_FROM_ABI void push_front(const value_type& __x);
  872. _LIBCPP_HIDE_FROM_ABI void push_back(const value_type& __x);
  873. #ifndef _LIBCPP_CXX03_LANG
  874. template <class _Arg>
  875. _LIBCPP_INLINE_VISIBILITY
  876. void __emplace_back(_Arg&& __arg) { emplace_back(_VSTD::forward<_Arg>(__arg)); }
  877. #else
  878. _LIBCPP_INLINE_VISIBILITY
  879. void __emplace_back(value_type const& __arg) { push_back(__arg); }
  880. #endif
  881. _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, const value_type& __x);
  882. _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, size_type __n, const value_type& __x);
  883. template <class _InpIter>
  884. _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, _InpIter __f, _InpIter __l,
  885. __enable_if_t<__has_input_iterator_category<_InpIter>::value>* = 0);
  886. #if _LIBCPP_STD_VER >= 23
  887. template <_ContainerCompatibleRange<_Tp> _Range>
  888. _LIBCPP_HIDE_FROM_ABI
  889. iterator insert_range(const_iterator __position, _Range&& __range) {
  890. return __insert_with_sentinel(__position, ranges::begin(__range), ranges::end(__range));
  891. }
  892. #endif
  893. _LIBCPP_INLINE_VISIBILITY
  894. void swap(list& __c)
  895. #if _LIBCPP_STD_VER >= 14
  896. _NOEXCEPT
  897. #else
  898. _NOEXCEPT_(!__node_alloc_traits::propagate_on_container_swap::value ||
  899. __is_nothrow_swappable<__node_allocator>::value)
  900. #endif
  901. {base::swap(__c);}
  902. _LIBCPP_INLINE_VISIBILITY
  903. void clear() _NOEXCEPT {base::clear();}
  904. _LIBCPP_HIDE_FROM_ABI void pop_front();
  905. _LIBCPP_HIDE_FROM_ABI void pop_back();
  906. _LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __p);
  907. _LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __f, const_iterator __l);
  908. _LIBCPP_HIDE_FROM_ABI void resize(size_type __n);
  909. _LIBCPP_HIDE_FROM_ABI void resize(size_type __n, const value_type& __x);
  910. _LIBCPP_HIDE_FROM_ABI void splice(const_iterator __p, list& __c);
  911. #ifndef _LIBCPP_CXX03_LANG
  912. _LIBCPP_INLINE_VISIBILITY
  913. void splice(const_iterator __p, list&& __c) {splice(__p, __c);}
  914. _LIBCPP_INLINE_VISIBILITY
  915. void splice(const_iterator __p, list&& __c, const_iterator __i)
  916. {splice(__p, __c, __i);}
  917. _LIBCPP_INLINE_VISIBILITY
  918. void splice(const_iterator __p, list&& __c, const_iterator __f, const_iterator __l)
  919. {splice(__p, __c, __f, __l);}
  920. #endif
  921. _LIBCPP_HIDE_FROM_ABI void splice(const_iterator __p, list& __c, const_iterator __i);
  922. _LIBCPP_HIDE_FROM_ABI void splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l);
  923. _LIBCPP_HIDE_FROM_ABI __remove_return_type remove(const value_type& __x);
  924. template <class _Pred>
  925. _LIBCPP_HIDE_FROM_ABI __remove_return_type remove_if(_Pred __pred);
  926. _LIBCPP_INLINE_VISIBILITY
  927. __remove_return_type unique() { return unique(__equal_to()); }
  928. template <class _BinaryPred>
  929. _LIBCPP_HIDE_FROM_ABI __remove_return_type unique(_BinaryPred __binary_pred);
  930. _LIBCPP_INLINE_VISIBILITY
  931. void merge(list& __c);
  932. #ifndef _LIBCPP_CXX03_LANG
  933. _LIBCPP_INLINE_VISIBILITY
  934. void merge(list&& __c) {merge(__c);}
  935. template <class _Comp>
  936. _LIBCPP_INLINE_VISIBILITY
  937. void merge(list&& __c, _Comp __comp) {merge(__c, __comp);}
  938. #endif
  939. template <class _Comp>
  940. _LIBCPP_HIDE_FROM_ABI void merge(list& __c, _Comp __comp);
  941. _LIBCPP_INLINE_VISIBILITY
  942. void sort();
  943. template <class _Comp>
  944. _LIBCPP_INLINE_VISIBILITY
  945. void sort(_Comp __comp);
  946. _LIBCPP_HIDE_FROM_ABI void reverse() _NOEXCEPT;
  947. _LIBCPP_HIDE_FROM_ABI bool __invariants() const;
  948. private:
  949. template <class _Iterator, class _Sentinel>
  950. _LIBCPP_HIDE_FROM_ABI
  951. void __assign_with_sentinel(_Iterator __f, _Sentinel __l);
  952. template <class _Iterator, class _Sentinel>
  953. _LIBCPP_HIDE_FROM_ABI
  954. iterator __insert_with_sentinel(const_iterator __p, _Iterator __f, _Sentinel __l);
  955. _LIBCPP_INLINE_VISIBILITY
  956. static void __link_nodes (__link_pointer __p, __link_pointer __f, __link_pointer __l);
  957. _LIBCPP_INLINE_VISIBILITY
  958. void __link_nodes_at_front(__link_pointer __f, __link_pointer __l);
  959. _LIBCPP_INLINE_VISIBILITY
  960. void __link_nodes_at_back (__link_pointer __f, __link_pointer __l);
  961. _LIBCPP_HIDE_FROM_ABI iterator __iterator(size_type __n);
  962. // TODO: Make this _LIBCPP_HIDE_FROM_ABI
  963. template <class _Comp>
  964. _LIBCPP_HIDDEN static iterator __sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp);
  965. _LIBCPP_HIDE_FROM_ABI void __move_assign(list& __c, true_type)
  966. _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value);
  967. _LIBCPP_HIDE_FROM_ABI void __move_assign(list& __c, false_type);
  968. };
  969. #if _LIBCPP_STD_VER >= 17
  970. template<class _InputIterator,
  971. class _Alloc = allocator<__iter_value_type<_InputIterator>>,
  972. class = enable_if_t<__has_input_iterator_category<_InputIterator>::value>,
  973. class = enable_if_t<__is_allocator<_Alloc>::value>
  974. >
  975. list(_InputIterator, _InputIterator)
  976. -> list<__iter_value_type<_InputIterator>, _Alloc>;
  977. template<class _InputIterator,
  978. class _Alloc,
  979. class = enable_if_t<__has_input_iterator_category<_InputIterator>::value>,
  980. class = enable_if_t<__is_allocator<_Alloc>::value>
  981. >
  982. list(_InputIterator, _InputIterator, _Alloc)
  983. -> list<__iter_value_type<_InputIterator>, _Alloc>;
  984. #endif
  985. #if _LIBCPP_STD_VER >= 23
  986. template <ranges::input_range _Range,
  987. class _Alloc = allocator<ranges::range_value_t<_Range>>,
  988. class = enable_if_t<__is_allocator<_Alloc>::value>
  989. >
  990. list(from_range_t, _Range&&, _Alloc = _Alloc())
  991. -> list<ranges::range_value_t<_Range>, _Alloc>;
  992. #endif
  993. // Link in nodes [__f, __l] just prior to __p
  994. template <class _Tp, class _Alloc>
  995. inline
  996. void
  997. list<_Tp, _Alloc>::__link_nodes(__link_pointer __p, __link_pointer __f, __link_pointer __l)
  998. {
  999. __p->__prev_->__next_ = __f;
  1000. __f->__prev_ = __p->__prev_;
  1001. __p->__prev_ = __l;
  1002. __l->__next_ = __p;
  1003. }
  1004. // Link in nodes [__f, __l] at the front of the list
  1005. template <class _Tp, class _Alloc>
  1006. inline
  1007. void
  1008. list<_Tp, _Alloc>::__link_nodes_at_front(__link_pointer __f, __link_pointer __l)
  1009. {
  1010. __f->__prev_ = base::__end_as_link();
  1011. __l->__next_ = base::__end_.__next_;
  1012. __l->__next_->__prev_ = __l;
  1013. base::__end_.__next_ = __f;
  1014. }
  1015. // Link in nodes [__f, __l] at the back of the list
  1016. template <class _Tp, class _Alloc>
  1017. inline
  1018. void
  1019. list<_Tp, _Alloc>::__link_nodes_at_back(__link_pointer __f, __link_pointer __l)
  1020. {
  1021. __l->__next_ = base::__end_as_link();
  1022. __f->__prev_ = base::__end_.__prev_;
  1023. __f->__prev_->__next_ = __f;
  1024. base::__end_.__prev_ = __l;
  1025. }
  1026. template <class _Tp, class _Alloc>
  1027. inline
  1028. typename list<_Tp, _Alloc>::iterator
  1029. list<_Tp, _Alloc>::__iterator(size_type __n)
  1030. {
  1031. return __n <= base::__sz() / 2 ? _VSTD::next(begin(), __n)
  1032. : _VSTD::prev(end(), base::__sz() - __n);
  1033. }
  1034. template <class _Tp, class _Alloc>
  1035. list<_Tp, _Alloc>::list(size_type __n)
  1036. {
  1037. for (; __n > 0; --__n)
  1038. #ifndef _LIBCPP_CXX03_LANG
  1039. emplace_back();
  1040. #else
  1041. push_back(value_type());
  1042. #endif
  1043. }
  1044. #if _LIBCPP_STD_VER >= 14
  1045. template <class _Tp, class _Alloc>
  1046. list<_Tp, _Alloc>::list(size_type __n, const allocator_type& __a) : base(__a)
  1047. {
  1048. for (; __n > 0; --__n)
  1049. emplace_back();
  1050. }
  1051. #endif
  1052. template <class _Tp, class _Alloc>
  1053. list<_Tp, _Alloc>::list(size_type __n, const value_type& __x)
  1054. {
  1055. for (; __n > 0; --__n)
  1056. push_back(__x);
  1057. }
  1058. template <class _Tp, class _Alloc>
  1059. template <class _InpIter>
  1060. list<_Tp, _Alloc>::list(_InpIter __f, _InpIter __l,
  1061. __enable_if_t<__has_input_iterator_category<_InpIter>::value>*)
  1062. {
  1063. for (; __f != __l; ++__f)
  1064. __emplace_back(*__f);
  1065. }
  1066. template <class _Tp, class _Alloc>
  1067. template <class _InpIter>
  1068. list<_Tp, _Alloc>::list(_InpIter __f, _InpIter __l, const allocator_type& __a,
  1069. __enable_if_t<__has_input_iterator_category<_InpIter>::value>*)
  1070. : base(__a)
  1071. {
  1072. for (; __f != __l; ++__f)
  1073. __emplace_back(*__f);
  1074. }
  1075. template <class _Tp, class _Alloc>
  1076. list<_Tp, _Alloc>::list(const list& __c)
  1077. : base(__node_alloc_traits::select_on_container_copy_construction(
  1078. __c.__node_alloc())) {
  1079. for (const_iterator __i = __c.begin(), __e = __c.end(); __i != __e; ++__i)
  1080. push_back(*__i);
  1081. }
  1082. template <class _Tp, class _Alloc>
  1083. list<_Tp, _Alloc>::list(const list& __c, const __type_identity_t<allocator_type>& __a)
  1084. : base(__a)
  1085. {
  1086. for (const_iterator __i = __c.begin(), __e = __c.end(); __i != __e; ++__i)
  1087. push_back(*__i);
  1088. }
  1089. #ifndef _LIBCPP_CXX03_LANG
  1090. template <class _Tp, class _Alloc>
  1091. list<_Tp, _Alloc>::list(initializer_list<value_type> __il, const allocator_type& __a)
  1092. : base(__a)
  1093. {
  1094. for (typename initializer_list<value_type>::const_iterator __i = __il.begin(),
  1095. __e = __il.end(); __i != __e; ++__i)
  1096. push_back(*__i);
  1097. }
  1098. template <class _Tp, class _Alloc>
  1099. list<_Tp, _Alloc>::list(initializer_list<value_type> __il)
  1100. {
  1101. for (typename initializer_list<value_type>::const_iterator __i = __il.begin(),
  1102. __e = __il.end(); __i != __e; ++__i)
  1103. push_back(*__i);
  1104. }
  1105. template <class _Tp, class _Alloc>
  1106. inline list<_Tp, _Alloc>::list(list&& __c)
  1107. _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value)
  1108. : base(_VSTD::move(__c.__node_alloc())) {
  1109. splice(end(), __c);
  1110. }
  1111. template <class _Tp, class _Alloc>
  1112. inline
  1113. list<_Tp, _Alloc>::list(list&& __c, const __type_identity_t<allocator_type>& __a)
  1114. : base(__a)
  1115. {
  1116. if (__a == __c.get_allocator())
  1117. splice(end(), __c);
  1118. else
  1119. {
  1120. typedef move_iterator<iterator> _Ip;
  1121. assign(_Ip(__c.begin()), _Ip(__c.end()));
  1122. }
  1123. }
  1124. template <class _Tp, class _Alloc>
  1125. inline
  1126. list<_Tp, _Alloc>&
  1127. list<_Tp, _Alloc>::operator=(list&& __c)
  1128. _NOEXCEPT_(
  1129. __node_alloc_traits::propagate_on_container_move_assignment::value &&
  1130. is_nothrow_move_assignable<__node_allocator>::value)
  1131. {
  1132. __move_assign(__c, integral_constant<bool,
  1133. __node_alloc_traits::propagate_on_container_move_assignment::value>());
  1134. return *this;
  1135. }
  1136. template <class _Tp, class _Alloc>
  1137. void
  1138. list<_Tp, _Alloc>::__move_assign(list& __c, false_type)
  1139. {
  1140. if (base::__node_alloc() != __c.__node_alloc())
  1141. {
  1142. typedef move_iterator<iterator> _Ip;
  1143. assign(_Ip(__c.begin()), _Ip(__c.end()));
  1144. }
  1145. else
  1146. __move_assign(__c, true_type());
  1147. }
  1148. template <class _Tp, class _Alloc>
  1149. void
  1150. list<_Tp, _Alloc>::__move_assign(list& __c, true_type)
  1151. _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
  1152. {
  1153. clear();
  1154. base::__move_assign_alloc(__c);
  1155. splice(end(), __c);
  1156. }
  1157. #endif // _LIBCPP_CXX03_LANG
  1158. template <class _Tp, class _Alloc>
  1159. inline
  1160. list<_Tp, _Alloc>&
  1161. list<_Tp, _Alloc>::operator=(const list& __c)
  1162. {
  1163. if (this != _VSTD::addressof(__c))
  1164. {
  1165. base::__copy_assign_alloc(__c);
  1166. assign(__c.begin(), __c.end());
  1167. }
  1168. return *this;
  1169. }
  1170. template <class _Tp, class _Alloc>
  1171. template <class _InpIter>
  1172. void
  1173. list<_Tp, _Alloc>::assign(_InpIter __f, _InpIter __l,
  1174. __enable_if_t<__has_input_iterator_category<_InpIter>::value>*)
  1175. {
  1176. __assign_with_sentinel(__f, __l);
  1177. }
  1178. template <class _Tp, class _Alloc>
  1179. template <class _Iterator, class _Sentinel>
  1180. _LIBCPP_HIDE_FROM_ABI
  1181. void list<_Tp, _Alloc>::__assign_with_sentinel(_Iterator __f, _Sentinel __l) {
  1182. iterator __i = begin();
  1183. iterator __e = end();
  1184. for (; __f != __l && __i != __e; ++__f, (void) ++__i)
  1185. *__i = *__f;
  1186. if (__i == __e)
  1187. __insert_with_sentinel(__e, std::move(__f), std::move(__l));
  1188. else
  1189. erase(__i, __e);
  1190. }
  1191. template <class _Tp, class _Alloc>
  1192. void
  1193. list<_Tp, _Alloc>::assign(size_type __n, const value_type& __x)
  1194. {
  1195. iterator __i = begin();
  1196. iterator __e = end();
  1197. for (; __n > 0 && __i != __e; --__n, (void) ++__i)
  1198. *__i = __x;
  1199. if (__i == __e)
  1200. insert(__e, __n, __x);
  1201. else
  1202. erase(__i, __e);
  1203. }
  1204. template <class _Tp, class _Alloc>
  1205. inline
  1206. _Alloc
  1207. list<_Tp, _Alloc>::get_allocator() const _NOEXCEPT
  1208. {
  1209. return allocator_type(base::__node_alloc());
  1210. }
  1211. template <class _Tp, class _Alloc>
  1212. typename list<_Tp, _Alloc>::iterator
  1213. list<_Tp, _Alloc>::insert(const_iterator __p, const value_type& __x)
  1214. {
  1215. __node_pointer __node = this->__create_node(/* prev = */nullptr, /* next = */nullptr, __x);
  1216. __link_nodes(__p.__ptr_, __node->__as_link(), __node->__as_link());
  1217. ++base::__sz();
  1218. return iterator(__node->__as_link());
  1219. }
  1220. template <class _Tp, class _Alloc>
  1221. typename list<_Tp, _Alloc>::iterator
  1222. list<_Tp, _Alloc>::insert(const_iterator __p, size_type __n, const value_type& __x)
  1223. {
  1224. iterator __r(__p.__ptr_);
  1225. if (__n > 0)
  1226. {
  1227. size_type __ds = 0;
  1228. __node_pointer __node = this->__create_node(/* prev = */nullptr, /* next = */nullptr, __x);
  1229. ++__ds;
  1230. __r = iterator(__node->__as_link());
  1231. iterator __e = __r;
  1232. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  1233. try
  1234. {
  1235. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  1236. for (--__n; __n != 0; --__n, (void) ++__e, ++__ds)
  1237. {
  1238. __e.__ptr_->__next_ = this->__create_node(/* prev = */__e.__ptr_, /* next = */nullptr, __x)->__as_link();
  1239. }
  1240. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  1241. }
  1242. catch (...)
  1243. {
  1244. while (true)
  1245. {
  1246. __link_pointer __prev = __e.__ptr_->__prev_;
  1247. __node_pointer __current = __e.__ptr_->__as_node();
  1248. this->__delete_node(__current);
  1249. if (__prev == 0)
  1250. break;
  1251. __e = iterator(__prev);
  1252. }
  1253. throw;
  1254. }
  1255. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  1256. __link_nodes(__p.__ptr_, __r.__ptr_, __e.__ptr_);
  1257. base::__sz() += __ds;
  1258. }
  1259. return __r;
  1260. }
  1261. template <class _Tp, class _Alloc>
  1262. template <class _InpIter>
  1263. typename list<_Tp, _Alloc>::iterator
  1264. list<_Tp, _Alloc>::insert(const_iterator __p, _InpIter __f, _InpIter __l,
  1265. __enable_if_t<__has_input_iterator_category<_InpIter>::value>*)
  1266. {
  1267. return __insert_with_sentinel(__p, __f, __l);
  1268. }
  1269. template <class _Tp, class _Alloc>
  1270. template <class _Iterator, class _Sentinel>
  1271. _LIBCPP_HIDE_FROM_ABI
  1272. typename list<_Tp, _Alloc>::iterator
  1273. list<_Tp, _Alloc>::__insert_with_sentinel(const_iterator __p, _Iterator __f, _Sentinel __l) {
  1274. iterator __r(__p.__ptr_);
  1275. if (__f != __l)
  1276. {
  1277. size_type __ds = 0;
  1278. __node_pointer __node = this->__create_node(/* prev = */nullptr, /* next = */nullptr, *__f);
  1279. ++__ds;
  1280. __r = iterator(__node->__as_link());
  1281. iterator __e = __r;
  1282. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  1283. try
  1284. {
  1285. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  1286. for (++__f; __f != __l; ++__f, (void) ++__e, ++__ds)
  1287. {
  1288. __e.__ptr_->__next_ = this->__create_node(/* prev = */__e.__ptr_, /* next = */nullptr, *__f)->__as_link();
  1289. }
  1290. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  1291. }
  1292. catch (...)
  1293. {
  1294. while (true)
  1295. {
  1296. __link_pointer __prev = __e.__ptr_->__prev_;
  1297. __node_pointer __current = __e.__ptr_->__as_node();
  1298. this->__delete_node(__current);
  1299. if (__prev == 0)
  1300. break;
  1301. __e = iterator(__prev);
  1302. }
  1303. throw;
  1304. }
  1305. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  1306. __link_nodes(__p.__ptr_, __r.__ptr_, __e.__ptr_);
  1307. base::__sz() += __ds;
  1308. }
  1309. return __r;
  1310. }
  1311. template <class _Tp, class _Alloc>
  1312. void
  1313. list<_Tp, _Alloc>::push_front(const value_type& __x)
  1314. {
  1315. __node_pointer __node = this->__create_node(/* prev = */nullptr, /* next = */nullptr, __x);
  1316. __link_pointer __nl = __node->__as_link();
  1317. __link_nodes_at_front(__nl, __nl);
  1318. ++base::__sz();
  1319. }
  1320. template <class _Tp, class _Alloc>
  1321. void
  1322. list<_Tp, _Alloc>::push_back(const value_type& __x)
  1323. {
  1324. __node_pointer __node = this->__create_node(/* prev = */nullptr, /* next = */nullptr, __x);
  1325. __link_pointer __nl = __node->__as_link();
  1326. __link_nodes_at_back(__nl, __nl);
  1327. ++base::__sz();
  1328. }
  1329. #ifndef _LIBCPP_CXX03_LANG
  1330. template <class _Tp, class _Alloc>
  1331. void
  1332. list<_Tp, _Alloc>::push_front(value_type&& __x)
  1333. {
  1334. __node_pointer __node = this->__create_node(/* prev = */nullptr, /* next = */nullptr, std::move(__x));
  1335. __link_pointer __nl = __node->__as_link();
  1336. __link_nodes_at_front(__nl, __nl);
  1337. ++base::__sz();
  1338. }
  1339. template <class _Tp, class _Alloc>
  1340. void
  1341. list<_Tp, _Alloc>::push_back(value_type&& __x)
  1342. {
  1343. __node_pointer __node = this->__create_node(/* prev = */nullptr, /* next = */nullptr, std::move(__x));
  1344. __link_pointer __nl = __node->__as_link();
  1345. __link_nodes_at_back(__nl, __nl);
  1346. ++base::__sz();
  1347. }
  1348. template <class _Tp, class _Alloc>
  1349. template <class... _Args>
  1350. #if _LIBCPP_STD_VER >= 17
  1351. typename list<_Tp, _Alloc>::reference
  1352. #else
  1353. void
  1354. #endif
  1355. list<_Tp, _Alloc>::emplace_front(_Args&&... __args)
  1356. {
  1357. __node_pointer __node = this->__create_node(/* prev = */nullptr, /* next = */nullptr, std::forward<_Args>(__args)...);
  1358. __link_pointer __nl = __node->__as_link();
  1359. __link_nodes_at_front(__nl, __nl);
  1360. ++base::__sz();
  1361. #if _LIBCPP_STD_VER >= 17
  1362. return __node->__get_value();
  1363. #endif
  1364. }
  1365. template <class _Tp, class _Alloc>
  1366. template <class... _Args>
  1367. #if _LIBCPP_STD_VER >= 17
  1368. typename list<_Tp, _Alloc>::reference
  1369. #else
  1370. void
  1371. #endif
  1372. list<_Tp, _Alloc>::emplace_back(_Args&&... __args)
  1373. {
  1374. __node_pointer __node = this->__create_node(/* prev = */nullptr, /* next = */nullptr, std::forward<_Args>(__args)...);
  1375. __link_pointer __nl = __node->__as_link();
  1376. __link_nodes_at_back(__nl, __nl);
  1377. ++base::__sz();
  1378. #if _LIBCPP_STD_VER >= 17
  1379. return __node->__get_value();
  1380. #endif
  1381. }
  1382. template <class _Tp, class _Alloc>
  1383. template <class... _Args>
  1384. typename list<_Tp, _Alloc>::iterator
  1385. list<_Tp, _Alloc>::emplace(const_iterator __p, _Args&&... __args)
  1386. {
  1387. __node_pointer __node = this->__create_node(/* prev = */nullptr, /* next = */nullptr, std::forward<_Args>(__args)...);
  1388. __link_pointer __nl = __node->__as_link();
  1389. __link_nodes(__p.__ptr_, __nl, __nl);
  1390. ++base::__sz();
  1391. return iterator(__nl);
  1392. }
  1393. template <class _Tp, class _Alloc>
  1394. typename list<_Tp, _Alloc>::iterator
  1395. list<_Tp, _Alloc>::insert(const_iterator __p, value_type&& __x)
  1396. {
  1397. __node_pointer __node = this->__create_node(/* prev = */nullptr, /* next = */nullptr, std::move(__x));
  1398. __link_pointer __nl = __node->__as_link();
  1399. __link_nodes(__p.__ptr_, __nl, __nl);
  1400. ++base::__sz();
  1401. return iterator(__nl);
  1402. }
  1403. #endif // _LIBCPP_CXX03_LANG
  1404. template <class _Tp, class _Alloc>
  1405. void
  1406. list<_Tp, _Alloc>::pop_front()
  1407. {
  1408. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "list::pop_front() called with empty list");
  1409. __link_pointer __n = base::__end_.__next_;
  1410. base::__unlink_nodes(__n, __n);
  1411. --base::__sz();
  1412. this->__delete_node(__n->__as_node());
  1413. }
  1414. template <class _Tp, class _Alloc>
  1415. void
  1416. list<_Tp, _Alloc>::pop_back()
  1417. {
  1418. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "list::pop_back() called on an empty list");
  1419. __link_pointer __n = base::__end_.__prev_;
  1420. base::__unlink_nodes(__n, __n);
  1421. --base::__sz();
  1422. this->__delete_node(__n->__as_node());
  1423. }
  1424. template <class _Tp, class _Alloc>
  1425. typename list<_Tp, _Alloc>::iterator
  1426. list<_Tp, _Alloc>::erase(const_iterator __p)
  1427. {
  1428. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__p != end(),
  1429. "list::erase(iterator) called with a non-dereferenceable iterator");
  1430. __link_pointer __n = __p.__ptr_;
  1431. __link_pointer __r = __n->__next_;
  1432. base::__unlink_nodes(__n, __n);
  1433. --base::__sz();
  1434. this->__delete_node(__n->__as_node());
  1435. return iterator(__r);
  1436. }
  1437. template <class _Tp, class _Alloc>
  1438. typename list<_Tp, _Alloc>::iterator
  1439. list<_Tp, _Alloc>::erase(const_iterator __f, const_iterator __l)
  1440. {
  1441. if (__f != __l)
  1442. {
  1443. base::__unlink_nodes(__f.__ptr_, __l.__ptr_->__prev_);
  1444. while (__f != __l)
  1445. {
  1446. __link_pointer __n = __f.__ptr_;
  1447. ++__f;
  1448. --base::__sz();
  1449. this->__delete_node(__n->__as_node());
  1450. }
  1451. }
  1452. return iterator(__l.__ptr_);
  1453. }
  1454. template <class _Tp, class _Alloc>
  1455. void
  1456. list<_Tp, _Alloc>::resize(size_type __n)
  1457. {
  1458. if (__n < base::__sz())
  1459. erase(__iterator(__n), end());
  1460. else if (__n > base::__sz())
  1461. {
  1462. __n -= base::__sz();
  1463. size_type __ds = 0;
  1464. __node_pointer __node = this->__create_node(/* prev = */nullptr, /* next = */nullptr);
  1465. ++__ds;
  1466. iterator __r = iterator(__node->__as_link());
  1467. iterator __e = __r;
  1468. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  1469. try
  1470. {
  1471. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  1472. for (--__n; __n != 0; --__n, (void) ++__e, ++__ds)
  1473. {
  1474. __e.__ptr_->__next_ = this->__create_node(/* prev = */__e.__ptr_, /* next = */nullptr)->__as_link();
  1475. }
  1476. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  1477. }
  1478. catch (...)
  1479. {
  1480. while (true)
  1481. {
  1482. __link_pointer __prev = __e.__ptr_->__prev_;
  1483. __node_pointer __current = __e.__ptr_->__as_node();
  1484. this->__delete_node(__current);
  1485. if (__prev == 0)
  1486. break;
  1487. __e = iterator(__prev);
  1488. }
  1489. throw;
  1490. }
  1491. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  1492. __link_nodes_at_back(__r.__ptr_, __e.__ptr_);
  1493. base::__sz() += __ds;
  1494. }
  1495. }
  1496. template <class _Tp, class _Alloc>
  1497. void
  1498. list<_Tp, _Alloc>::resize(size_type __n, const value_type& __x)
  1499. {
  1500. if (__n < base::__sz())
  1501. erase(__iterator(__n), end());
  1502. else if (__n > base::__sz())
  1503. {
  1504. __n -= base::__sz();
  1505. size_type __ds = 0;
  1506. __node_pointer __node = this->__create_node(/* prev = */nullptr, /* next = */nullptr, __x);
  1507. ++__ds;
  1508. __link_pointer __nl = __node->__as_link();
  1509. iterator __r = iterator(__nl);
  1510. iterator __e = __r;
  1511. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  1512. try
  1513. {
  1514. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  1515. for (--__n; __n != 0; --__n, (void) ++__e, ++__ds)
  1516. {
  1517. __e.__ptr_->__next_ = this->__create_node(/* prev = */__e.__ptr_, /* next = */nullptr, __x)->__as_link();
  1518. }
  1519. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  1520. }
  1521. catch (...)
  1522. {
  1523. while (true)
  1524. {
  1525. __link_pointer __prev = __e.__ptr_->__prev_;
  1526. __node_pointer __current = __e.__ptr_->__as_node();
  1527. this->__delete_node(__current);
  1528. if (__prev == 0)
  1529. break;
  1530. __e = iterator(__prev);
  1531. }
  1532. throw;
  1533. }
  1534. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  1535. __link_nodes(base::__end_as_link(), __r.__ptr_, __e.__ptr_);
  1536. base::__sz() += __ds;
  1537. }
  1538. }
  1539. template <class _Tp, class _Alloc>
  1540. void
  1541. list<_Tp, _Alloc>::splice(const_iterator __p, list& __c)
  1542. {
  1543. _LIBCPP_ASSERT_VALID_INPUT_RANGE(this != _VSTD::addressof(__c),
  1544. "list::splice(iterator, list) called with this == &list");
  1545. if (!__c.empty())
  1546. {
  1547. __link_pointer __f = __c.__end_.__next_;
  1548. __link_pointer __l = __c.__end_.__prev_;
  1549. base::__unlink_nodes(__f, __l);
  1550. __link_nodes(__p.__ptr_, __f, __l);
  1551. base::__sz() += __c.__sz();
  1552. __c.__sz() = 0;
  1553. }
  1554. }
  1555. template <class _Tp, class _Alloc>
  1556. void
  1557. list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __i)
  1558. {
  1559. if (__p.__ptr_ != __i.__ptr_ && __p.__ptr_ != __i.__ptr_->__next_)
  1560. {
  1561. __link_pointer __f = __i.__ptr_;
  1562. base::__unlink_nodes(__f, __f);
  1563. __link_nodes(__p.__ptr_, __f, __f);
  1564. --__c.__sz();
  1565. ++base::__sz();
  1566. }
  1567. }
  1568. template <class _Tp, class _Alloc>
  1569. void
  1570. list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l)
  1571. {
  1572. if (__f != __l)
  1573. {
  1574. __link_pointer __first = __f.__ptr_;
  1575. --__l;
  1576. __link_pointer __last = __l.__ptr_;
  1577. if (this != _VSTD::addressof(__c))
  1578. {
  1579. size_type __s = _VSTD::distance(__f, __l) + 1;
  1580. __c.__sz() -= __s;
  1581. base::__sz() += __s;
  1582. }
  1583. base::__unlink_nodes(__first, __last);
  1584. __link_nodes(__p.__ptr_, __first, __last);
  1585. }
  1586. }
  1587. template <class _Tp, class _Alloc>
  1588. typename list<_Tp, _Alloc>::__remove_return_type
  1589. list<_Tp, _Alloc>::remove(const value_type& __x)
  1590. {
  1591. list<_Tp, _Alloc> __deleted_nodes(get_allocator()); // collect the nodes we're removing
  1592. for (const_iterator __i = begin(), __e = end(); __i != __e;)
  1593. {
  1594. if (*__i == __x)
  1595. {
  1596. const_iterator __j = _VSTD::next(__i);
  1597. for (; __j != __e && *__j == __x; ++__j)
  1598. ;
  1599. __deleted_nodes.splice(__deleted_nodes.end(), *this, __i, __j);
  1600. __i = __j;
  1601. if (__i != __e)
  1602. ++__i;
  1603. }
  1604. else
  1605. ++__i;
  1606. }
  1607. return (__remove_return_type) __deleted_nodes.size();
  1608. }
  1609. template <class _Tp, class _Alloc>
  1610. template <class _Pred>
  1611. typename list<_Tp, _Alloc>::__remove_return_type
  1612. list<_Tp, _Alloc>::remove_if(_Pred __pred)
  1613. {
  1614. list<_Tp, _Alloc> __deleted_nodes(get_allocator()); // collect the nodes we're removing
  1615. for (iterator __i = begin(), __e = end(); __i != __e;)
  1616. {
  1617. if (__pred(*__i))
  1618. {
  1619. iterator __j = _VSTD::next(__i);
  1620. for (; __j != __e && __pred(*__j); ++__j)
  1621. ;
  1622. __deleted_nodes.splice(__deleted_nodes.end(), *this, __i, __j);
  1623. __i = __j;
  1624. if (__i != __e)
  1625. ++__i;
  1626. }
  1627. else
  1628. ++__i;
  1629. }
  1630. return (__remove_return_type) __deleted_nodes.size();
  1631. }
  1632. template <class _Tp, class _Alloc>
  1633. template <class _BinaryPred>
  1634. typename list<_Tp, _Alloc>::__remove_return_type
  1635. list<_Tp, _Alloc>::unique(_BinaryPred __binary_pred)
  1636. {
  1637. list<_Tp, _Alloc> __deleted_nodes(get_allocator()); // collect the nodes we're removing
  1638. for (iterator __i = begin(), __e = end(); __i != __e;)
  1639. {
  1640. iterator __j = _VSTD::next(__i);
  1641. for (; __j != __e && __binary_pred(*__i, *__j); ++__j)
  1642. ;
  1643. if (++__i != __j) {
  1644. __deleted_nodes.splice(__deleted_nodes.end(), *this, __i, __j);
  1645. __i = __j;
  1646. }
  1647. }
  1648. return (__remove_return_type) __deleted_nodes.size();
  1649. }
  1650. template <class _Tp, class _Alloc>
  1651. inline
  1652. void
  1653. list<_Tp, _Alloc>::merge(list& __c)
  1654. {
  1655. merge(__c, __less<>());
  1656. }
  1657. template <class _Tp, class _Alloc>
  1658. template <class _Comp>
  1659. void
  1660. list<_Tp, _Alloc>::merge(list& __c, _Comp __comp)
  1661. {
  1662. if (this != _VSTD::addressof(__c))
  1663. {
  1664. iterator __f1 = begin();
  1665. iterator __e1 = end();
  1666. iterator __f2 = __c.begin();
  1667. iterator __e2 = __c.end();
  1668. while (__f1 != __e1 && __f2 != __e2)
  1669. {
  1670. if (__comp(*__f2, *__f1))
  1671. {
  1672. size_type __ds = 1;
  1673. iterator __m2 = _VSTD::next(__f2);
  1674. for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2, (void) ++__ds)
  1675. ;
  1676. base::__sz() += __ds;
  1677. __c.__sz() -= __ds;
  1678. __link_pointer __f = __f2.__ptr_;
  1679. __link_pointer __l = __m2.__ptr_->__prev_;
  1680. __f2 = __m2;
  1681. base::__unlink_nodes(__f, __l);
  1682. __m2 = _VSTD::next(__f1);
  1683. __link_nodes(__f1.__ptr_, __f, __l);
  1684. __f1 = __m2;
  1685. }
  1686. else
  1687. ++__f1;
  1688. }
  1689. splice(__e1, __c);
  1690. }
  1691. }
  1692. template <class _Tp, class _Alloc>
  1693. inline
  1694. void
  1695. list<_Tp, _Alloc>::sort()
  1696. {
  1697. sort(__less<>());
  1698. }
  1699. template <class _Tp, class _Alloc>
  1700. template <class _Comp>
  1701. inline
  1702. void
  1703. list<_Tp, _Alloc>::sort(_Comp __comp)
  1704. {
  1705. __sort(begin(), end(), base::__sz(), __comp);
  1706. }
  1707. template <class _Tp, class _Alloc>
  1708. template <class _Comp>
  1709. typename list<_Tp, _Alloc>::iterator
  1710. list<_Tp, _Alloc>::__sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp)
  1711. {
  1712. switch (__n)
  1713. {
  1714. case 0:
  1715. case 1:
  1716. return __f1;
  1717. case 2:
  1718. if (__comp(*--__e2, *__f1))
  1719. {
  1720. __link_pointer __f = __e2.__ptr_;
  1721. base::__unlink_nodes(__f, __f);
  1722. __link_nodes(__f1.__ptr_, __f, __f);
  1723. return __e2;
  1724. }
  1725. return __f1;
  1726. }
  1727. size_type __n2 = __n / 2;
  1728. iterator __e1 = _VSTD::next(__f1, __n2);
  1729. iterator __r = __f1 = __sort(__f1, __e1, __n2, __comp);
  1730. iterator __f2 = __e1 = __sort(__e1, __e2, __n - __n2, __comp);
  1731. if (__comp(*__f2, *__f1))
  1732. {
  1733. iterator __m2 = _VSTD::next(__f2);
  1734. for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2)
  1735. ;
  1736. __link_pointer __f = __f2.__ptr_;
  1737. __link_pointer __l = __m2.__ptr_->__prev_;
  1738. __r = __f2;
  1739. __e1 = __f2 = __m2;
  1740. base::__unlink_nodes(__f, __l);
  1741. __m2 = _VSTD::next(__f1);
  1742. __link_nodes(__f1.__ptr_, __f, __l);
  1743. __f1 = __m2;
  1744. }
  1745. else
  1746. ++__f1;
  1747. while (__f1 != __e1 && __f2 != __e2)
  1748. {
  1749. if (__comp(*__f2, *__f1))
  1750. {
  1751. iterator __m2 = _VSTD::next(__f2);
  1752. for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2)
  1753. ;
  1754. __link_pointer __f = __f2.__ptr_;
  1755. __link_pointer __l = __m2.__ptr_->__prev_;
  1756. if (__e1 == __f2)
  1757. __e1 = __m2;
  1758. __f2 = __m2;
  1759. base::__unlink_nodes(__f, __l);
  1760. __m2 = _VSTD::next(__f1);
  1761. __link_nodes(__f1.__ptr_, __f, __l);
  1762. __f1 = __m2;
  1763. }
  1764. else
  1765. ++__f1;
  1766. }
  1767. return __r;
  1768. }
  1769. template <class _Tp, class _Alloc>
  1770. void
  1771. list<_Tp, _Alloc>::reverse() _NOEXCEPT
  1772. {
  1773. if (base::__sz() > 1)
  1774. {
  1775. iterator __e = end();
  1776. for (iterator __i = begin(); __i.__ptr_ != __e.__ptr_;)
  1777. {
  1778. _VSTD::swap(__i.__ptr_->__prev_, __i.__ptr_->__next_);
  1779. __i.__ptr_ = __i.__ptr_->__prev_;
  1780. }
  1781. _VSTD::swap(__e.__ptr_->__prev_, __e.__ptr_->__next_);
  1782. }
  1783. }
  1784. template <class _Tp, class _Alloc>
  1785. bool
  1786. list<_Tp, _Alloc>::__invariants() const
  1787. {
  1788. return size() == _VSTD::distance(begin(), end());
  1789. }
  1790. template <class _Tp, class _Alloc>
  1791. inline _LIBCPP_INLINE_VISIBILITY
  1792. bool
  1793. operator==(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
  1794. {
  1795. return __x.size() == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
  1796. }
  1797. #if _LIBCPP_STD_VER <= 17
  1798. template <class _Tp, class _Alloc>
  1799. inline _LIBCPP_INLINE_VISIBILITY
  1800. bool
  1801. operator< (const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
  1802. {
  1803. return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
  1804. }
  1805. template <class _Tp, class _Alloc>
  1806. inline _LIBCPP_INLINE_VISIBILITY
  1807. bool
  1808. operator!=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
  1809. {
  1810. return !(__x == __y);
  1811. }
  1812. template <class _Tp, class _Alloc>
  1813. inline _LIBCPP_INLINE_VISIBILITY
  1814. bool
  1815. operator> (const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
  1816. {
  1817. return __y < __x;
  1818. }
  1819. template <class _Tp, class _Alloc>
  1820. inline _LIBCPP_INLINE_VISIBILITY
  1821. bool
  1822. operator>=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
  1823. {
  1824. return !(__x < __y);
  1825. }
  1826. template <class _Tp, class _Alloc>
  1827. inline _LIBCPP_INLINE_VISIBILITY
  1828. bool
  1829. operator<=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
  1830. {
  1831. return !(__y < __x);
  1832. }
  1833. #else // _LIBCPP_STD_VER <= 17
  1834. template <class _Tp, class _Allocator>
  1835. _LIBCPP_HIDE_FROM_ABI __synth_three_way_result<_Tp>
  1836. operator<=>(const list<_Tp, _Allocator>& __x, const list<_Tp, _Allocator>& __y) {
  1837. return std::lexicographical_compare_three_way(
  1838. __x.begin(), __x.end(), __y.begin(), __y.end(), std::__synth_three_way<_Tp, _Tp>);
  1839. }
  1840. #endif // _LIBCPP_STD_VER <= 17
  1841. template <class _Tp, class _Alloc>
  1842. inline _LIBCPP_INLINE_VISIBILITY
  1843. void
  1844. swap(list<_Tp, _Alloc>& __x, list<_Tp, _Alloc>& __y)
  1845. _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
  1846. {
  1847. __x.swap(__y);
  1848. }
  1849. #if _LIBCPP_STD_VER >= 20
  1850. template <class _Tp, class _Allocator, class _Predicate>
  1851. inline _LIBCPP_INLINE_VISIBILITY typename list<_Tp, _Allocator>::size_type
  1852. erase_if(list<_Tp, _Allocator>& __c, _Predicate __pred) {
  1853. return __c.remove_if(__pred);
  1854. }
  1855. template <class _Tp, class _Allocator, class _Up>
  1856. inline _LIBCPP_INLINE_VISIBILITY typename list<_Tp, _Allocator>::size_type
  1857. erase(list<_Tp, _Allocator>& __c, const _Up& __v) {
  1858. return _VSTD::erase_if(__c, [&](auto& __elem) { return __elem == __v; });
  1859. }
  1860. template <>
  1861. inline constexpr bool __format::__enable_insertable<std::list<char>> = true;
  1862. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  1863. template <>
  1864. inline constexpr bool __format::__enable_insertable<std::list<wchar_t>> = true;
  1865. #endif
  1866. #endif // _LIBCPP_STD_VER >= 20
  1867. _LIBCPP_END_NAMESPACE_STD
  1868. #if _LIBCPP_STD_VER >= 17
  1869. _LIBCPP_BEGIN_NAMESPACE_STD
  1870. namespace pmr {
  1871. template <class _ValueT>
  1872. using list _LIBCPP_AVAILABILITY_PMR = std::list<_ValueT, polymorphic_allocator<_ValueT>>;
  1873. } // namespace pmr
  1874. _LIBCPP_END_NAMESPACE_STD
  1875. #endif
  1876. _LIBCPP_POP_MACROS
  1877. #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
  1878. # include <algorithm>
  1879. # include <atomic>
  1880. # include <concepts>
  1881. # include <cstdint>
  1882. # include <cstdlib>
  1883. # include <functional>
  1884. # include <iosfwd>
  1885. # include <iterator>
  1886. # include <type_traits>
  1887. # include <typeinfo>
  1888. #endif
  1889. #endif // _LIBCPP_LIST