list 63 KB

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