list 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386
  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. list(const list& x);
  44. list(const list&, const allocator_type& a);
  45. list(list&& x)
  46. noexcept(is_nothrow_move_constructible<allocator_type>::value);
  47. list(list&&, const allocator_type& a);
  48. list(initializer_list<value_type>);
  49. list(initializer_list<value_type>, const allocator_type& a);
  50. ~list();
  51. list& operator=(const list& x);
  52. list& operator=(list&& x)
  53. noexcept(
  54. allocator_type::propagate_on_container_move_assignment::value &&
  55. is_nothrow_move_assignable<allocator_type>::value);
  56. list& operator=(initializer_list<value_type>);
  57. template <class Iter>
  58. void assign(Iter first, Iter last);
  59. void assign(size_type n, const value_type& t);
  60. void assign(initializer_list<value_type>);
  61. allocator_type get_allocator() const noexcept;
  62. iterator begin() noexcept;
  63. const_iterator begin() const noexcept;
  64. iterator end() noexcept;
  65. const_iterator end() const noexcept;
  66. reverse_iterator rbegin() noexcept;
  67. const_reverse_iterator rbegin() const noexcept;
  68. reverse_iterator rend() noexcept;
  69. const_reverse_iterator rend() const noexcept;
  70. const_iterator cbegin() const noexcept;
  71. const_iterator cend() const noexcept;
  72. const_reverse_iterator crbegin() const noexcept;
  73. const_reverse_iterator crend() const noexcept;
  74. reference front();
  75. const_reference front() const;
  76. reference back();
  77. const_reference back() const;
  78. bool empty() const noexcept;
  79. size_type size() const noexcept;
  80. size_type max_size() const noexcept;
  81. template <class... Args>
  82. reference emplace_front(Args&&... args); // reference in C++17
  83. void pop_front();
  84. template <class... Args>
  85. reference emplace_back(Args&&... args); // reference in C++17
  86. void pop_back();
  87. void push_front(const value_type& x);
  88. void push_front(value_type&& x);
  89. void push_back(const value_type& x);
  90. void push_back(value_type&& x);
  91. template <class... Args>
  92. iterator emplace(const_iterator position, Args&&... args);
  93. iterator insert(const_iterator position, const value_type& x);
  94. iterator insert(const_iterator position, value_type&& x);
  95. iterator insert(const_iterator position, size_type n, const value_type& x);
  96. template <class Iter>
  97. iterator insert(const_iterator position, Iter first, Iter last);
  98. iterator insert(const_iterator position, initializer_list<value_type> il);
  99. iterator erase(const_iterator position);
  100. iterator erase(const_iterator position, const_iterator last);
  101. void resize(size_type sz);
  102. void resize(size_type sz, const value_type& c);
  103. void swap(list&)
  104. noexcept(allocator_traits<allocator_type>::is_always_equal::value); // C++17
  105. void clear() noexcept;
  106. void splice(const_iterator position, list& x);
  107. void splice(const_iterator position, list&& x);
  108. void splice(const_iterator position, list& x, const_iterator i);
  109. void splice(const_iterator position, list&& x, const_iterator i);
  110. void splice(const_iterator position, list& x, const_iterator first,
  111. const_iterator last);
  112. void splice(const_iterator position, list&& x, const_iterator first,
  113. const_iterator last);
  114. size_type remove(const value_type& value); // void before C++20
  115. template <class Pred>
  116. size_type remove_if(Pred pred); // void before C++20
  117. size_type unique(); // void before C++20
  118. template <class BinaryPredicate>
  119. size_type unique(BinaryPredicate binary_pred); // void before C++20
  120. void merge(list& x);
  121. void merge(list&& x);
  122. template <class Compare>
  123. void merge(list& x, Compare comp);
  124. template <class Compare>
  125. void merge(list&& x, Compare comp);
  126. void sort();
  127. template <class Compare>
  128. void sort(Compare comp);
  129. void reverse() noexcept;
  130. };
  131. template <class InputIterator, class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
  132. list(InputIterator, InputIterator, Allocator = Allocator())
  133. -> list<typename iterator_traits<InputIterator>::value_type, Allocator>; // C++17
  134. template <class T, class Alloc>
  135. bool operator==(const list<T,Alloc>& x, const list<T,Alloc>& y);
  136. template <class T, class Alloc>
  137. bool operator< (const list<T,Alloc>& x, const list<T,Alloc>& y);
  138. template <class T, class Alloc>
  139. bool operator!=(const list<T,Alloc>& x, const list<T,Alloc>& y);
  140. template <class T, class Alloc>
  141. bool operator> (const list<T,Alloc>& x, const list<T,Alloc>& y);
  142. template <class T, class Alloc>
  143. bool operator>=(const list<T,Alloc>& x, const list<T,Alloc>& y);
  144. template <class T, class Alloc>
  145. bool operator<=(const list<T,Alloc>& x, const list<T,Alloc>& y);
  146. template <class T, class Alloc>
  147. void swap(list<T,Alloc>& x, list<T,Alloc>& y)
  148. noexcept(noexcept(x.swap(y)));
  149. template <class T, class Allocator, class U>
  150. typename list<T, Allocator>::size_type
  151. erase(list<T, Allocator>& c, const U& value); // C++20
  152. template <class T, class Allocator, class Predicate>
  153. typename list<T, Allocator>::size_type
  154. erase_if(list<T, Allocator>& c, Predicate pred); // C++20
  155. } // std
  156. */
  157. #include <__algorithm/comp.h>
  158. #include <__algorithm/equal.h>
  159. #include <__algorithm/lexicographical_compare.h>
  160. #include <__algorithm/min.h>
  161. #include <__assert> // all public C++ headers provide the assertion handler
  162. #include <__config>
  163. #include <__debug>
  164. #include <__format/enable_insertable.h>
  165. #include <__iterator/distance.h>
  166. #include <__iterator/iterator_traits.h>
  167. #include <__iterator/move_iterator.h>
  168. #include <__iterator/next.h>
  169. #include <__iterator/prev.h>
  170. #include <__iterator/reverse_iterator.h>
  171. #include <__memory/addressof.h>
  172. #include <__memory/allocator.h>
  173. #include <__memory/allocator_destructor.h>
  174. #include <__memory/allocator_traits.h>
  175. #include <__memory/compressed_pair.h>
  176. #include <__memory/pointer_traits.h>
  177. #include <__memory/swap_allocator.h>
  178. #include <__memory/unique_ptr.h>
  179. #include <__memory_resource/polymorphic_allocator.h>
  180. #include <__type_traits/is_allocator.h>
  181. #include <__utility/forward.h>
  182. #include <__utility/move.h>
  183. #include <__utility/swap.h>
  184. #include <cstring>
  185. #include <limits>
  186. #include <type_traits>
  187. #include <version>
  188. // standard-mandated includes
  189. // [iterator.range]
  190. #include <__iterator/access.h>
  191. #include <__iterator/data.h>
  192. #include <__iterator/empty.h>
  193. #include <__iterator/reverse_access.h>
  194. #include <__iterator/size.h>
  195. // [list.syn]
  196. #include <compare>
  197. #include <initializer_list>
  198. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  199. # pragma GCC system_header
  200. #endif
  201. _LIBCPP_PUSH_MACROS
  202. #include <__undef_macros>
  203. _LIBCPP_BEGIN_NAMESPACE_STD
  204. template <class _Tp, class _VoidPtr> struct __list_node;
  205. template <class _Tp, class _VoidPtr> struct __list_node_base;
  206. template <class _Tp, class _VoidPtr>
  207. struct __list_node_pointer_traits {
  208. typedef __rebind_pointer_t<_VoidPtr, __list_node<_Tp, _VoidPtr> >
  209. __node_pointer;
  210. typedef __rebind_pointer_t<_VoidPtr, __list_node_base<_Tp, _VoidPtr> >
  211. __base_pointer;
  212. #if defined(_LIBCPP_ABI_LIST_REMOVE_NODE_POINTER_UB)
  213. typedef __base_pointer __link_pointer;
  214. #else
  215. typedef __conditional_t<is_pointer<_VoidPtr>::value, __base_pointer, __node_pointer> __link_pointer;
  216. #endif
  217. typedef __conditional_t<is_same<__link_pointer, __node_pointer>::value, __base_pointer, __node_pointer>
  218. __non_link_pointer;
  219. static _LIBCPP_INLINE_VISIBILITY
  220. __link_pointer __unsafe_link_pointer_cast(__link_pointer __p) {
  221. return __p;
  222. }
  223. static _LIBCPP_INLINE_VISIBILITY
  224. __link_pointer __unsafe_link_pointer_cast(__non_link_pointer __p) {
  225. return static_cast<__link_pointer>(static_cast<_VoidPtr>(__p));
  226. }
  227. };
  228. template <class _Tp, class _VoidPtr>
  229. struct __list_node_base
  230. {
  231. typedef __list_node_pointer_traits<_Tp, _VoidPtr> _NodeTraits;
  232. typedef typename _NodeTraits::__node_pointer __node_pointer;
  233. typedef typename _NodeTraits::__base_pointer __base_pointer;
  234. typedef typename _NodeTraits::__link_pointer __link_pointer;
  235. __link_pointer __prev_;
  236. __link_pointer __next_;
  237. _LIBCPP_INLINE_VISIBILITY
  238. __list_node_base() : __prev_(_NodeTraits::__unsafe_link_pointer_cast(__self())),
  239. __next_(_NodeTraits::__unsafe_link_pointer_cast(__self())) {}
  240. _LIBCPP_INLINE_VISIBILITY
  241. __base_pointer __self() {
  242. return pointer_traits<__base_pointer>::pointer_to(*this);
  243. }
  244. _LIBCPP_INLINE_VISIBILITY
  245. __node_pointer __as_node() {
  246. return static_cast<__node_pointer>(__self());
  247. }
  248. };
  249. template <class _Tp, class _VoidPtr>
  250. struct _LIBCPP_STANDALONE_DEBUG __list_node
  251. : public __list_node_base<_Tp, _VoidPtr>
  252. {
  253. _Tp __value_;
  254. typedef __list_node_base<_Tp, _VoidPtr> __base;
  255. typedef typename __base::__link_pointer __link_pointer;
  256. _LIBCPP_INLINE_VISIBILITY
  257. __link_pointer __as_link() {
  258. return static_cast<__link_pointer>(__base::__self());
  259. }
  260. };
  261. template <class _Tp, class _Alloc = allocator<_Tp> > class _LIBCPP_TEMPLATE_VIS list;
  262. template <class _Tp, class _Alloc> class __list_imp;
  263. template <class _Tp, class _VoidPtr> class _LIBCPP_TEMPLATE_VIS __list_const_iterator;
  264. template <class _Tp, class _VoidPtr>
  265. class _LIBCPP_TEMPLATE_VIS __list_iterator
  266. {
  267. typedef __list_node_pointer_traits<_Tp, _VoidPtr> _NodeTraits;
  268. typedef typename _NodeTraits::__link_pointer __link_pointer;
  269. __link_pointer __ptr_;
  270. _LIBCPP_INLINE_VISIBILITY
  271. explicit __list_iterator(__link_pointer __p, const void* __c) _NOEXCEPT
  272. : __ptr_(__p)
  273. {
  274. (void)__c;
  275. #ifdef _LIBCPP_ENABLE_DEBUG_MODE
  276. __get_db()->__insert_ic(this, __c);
  277. #endif
  278. }
  279. template<class, class> friend class list;
  280. template<class, class> friend class __list_imp;
  281. template<class, class> friend class __list_const_iterator;
  282. public:
  283. typedef bidirectional_iterator_tag iterator_category;
  284. typedef _Tp value_type;
  285. typedef value_type& reference;
  286. typedef __rebind_pointer_t<_VoidPtr, value_type> pointer;
  287. typedef typename pointer_traits<pointer>::difference_type difference_type;
  288. _LIBCPP_INLINE_VISIBILITY
  289. __list_iterator() _NOEXCEPT : __ptr_(nullptr)
  290. {
  291. _VSTD::__debug_db_insert_i(this);
  292. }
  293. #ifdef _LIBCPP_ENABLE_DEBUG_MODE
  294. _LIBCPP_INLINE_VISIBILITY
  295. __list_iterator(const __list_iterator& __p)
  296. : __ptr_(__p.__ptr_)
  297. {
  298. __get_db()->__iterator_copy(this, _VSTD::addressof(__p));
  299. }
  300. _LIBCPP_INLINE_VISIBILITY
  301. ~__list_iterator()
  302. {
  303. __get_db()->__erase_i(this);
  304. }
  305. _LIBCPP_INLINE_VISIBILITY
  306. __list_iterator& operator=(const __list_iterator& __p)
  307. {
  308. if (this != _VSTD::addressof(__p))
  309. {
  310. __get_db()->__iterator_copy(this, _VSTD::addressof(__p));
  311. __ptr_ = __p.__ptr_;
  312. }
  313. return *this;
  314. }
  315. #endif // _LIBCPP_ENABLE_DEBUG_MODE
  316. _LIBCPP_INLINE_VISIBILITY
  317. reference operator*() const
  318. {
  319. _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
  320. "Attempted to dereference a non-dereferenceable list::iterator");
  321. return __ptr_->__as_node()->__value_;
  322. }
  323. _LIBCPP_INLINE_VISIBILITY
  324. pointer operator->() const
  325. {
  326. _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
  327. "Attempted to dereference a non-dereferenceable list::iterator");
  328. return pointer_traits<pointer>::pointer_to(__ptr_->__as_node()->__value_);
  329. }
  330. _LIBCPP_INLINE_VISIBILITY
  331. __list_iterator& operator++()
  332. {
  333. _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
  334. "Attempted to increment a non-incrementable list::iterator");
  335. __ptr_ = __ptr_->__next_;
  336. return *this;
  337. }
  338. _LIBCPP_INLINE_VISIBILITY
  339. __list_iterator operator++(int) {__list_iterator __t(*this); ++(*this); return __t;}
  340. _LIBCPP_INLINE_VISIBILITY
  341. __list_iterator& operator--()
  342. {
  343. _LIBCPP_DEBUG_ASSERT(__get_const_db()->__decrementable(this),
  344. "Attempted to decrement a non-decrementable list::iterator");
  345. __ptr_ = __ptr_->__prev_;
  346. return *this;
  347. }
  348. _LIBCPP_INLINE_VISIBILITY
  349. __list_iterator operator--(int) {__list_iterator __t(*this); --(*this); return __t;}
  350. friend _LIBCPP_INLINE_VISIBILITY
  351. bool operator==(const __list_iterator& __x, const __list_iterator& __y)
  352. {
  353. return __x.__ptr_ == __y.__ptr_;
  354. }
  355. friend _LIBCPP_INLINE_VISIBILITY
  356. bool operator!=(const __list_iterator& __x, const __list_iterator& __y)
  357. {return !(__x == __y);}
  358. };
  359. template <class _Tp, class _VoidPtr>
  360. class _LIBCPP_TEMPLATE_VIS __list_const_iterator
  361. {
  362. typedef __list_node_pointer_traits<_Tp, _VoidPtr> _NodeTraits;
  363. typedef typename _NodeTraits::__link_pointer __link_pointer;
  364. __link_pointer __ptr_;
  365. _LIBCPP_INLINE_VISIBILITY
  366. explicit __list_const_iterator(__link_pointer __p, const void* __c) _NOEXCEPT
  367. : __ptr_(__p)
  368. {
  369. (void)__c;
  370. #ifdef _LIBCPP_ENABLE_DEBUG_MODE
  371. __get_db()->__insert_ic(this, __c);
  372. #endif
  373. }
  374. template<class, class> friend class list;
  375. template<class, class> friend class __list_imp;
  376. public:
  377. typedef bidirectional_iterator_tag iterator_category;
  378. typedef _Tp value_type;
  379. typedef const value_type& reference;
  380. typedef __rebind_pointer_t<_VoidPtr, const value_type> pointer;
  381. typedef typename pointer_traits<pointer>::difference_type difference_type;
  382. _LIBCPP_INLINE_VISIBILITY
  383. __list_const_iterator() _NOEXCEPT : __ptr_(nullptr)
  384. {
  385. _VSTD::__debug_db_insert_i(this);
  386. }
  387. _LIBCPP_INLINE_VISIBILITY
  388. __list_const_iterator(const __list_iterator<_Tp, _VoidPtr>& __p) _NOEXCEPT
  389. : __ptr_(__p.__ptr_)
  390. {
  391. #ifdef _LIBCPP_ENABLE_DEBUG_MODE
  392. __get_db()->__iterator_copy(this, _VSTD::addressof(__p));
  393. #endif
  394. }
  395. #ifdef _LIBCPP_ENABLE_DEBUG_MODE
  396. _LIBCPP_INLINE_VISIBILITY
  397. __list_const_iterator(const __list_const_iterator& __p)
  398. : __ptr_(__p.__ptr_)
  399. {
  400. __get_db()->__iterator_copy(this, _VSTD::addressof(__p));
  401. }
  402. _LIBCPP_INLINE_VISIBILITY
  403. ~__list_const_iterator()
  404. {
  405. __get_db()->__erase_i(this);
  406. }
  407. _LIBCPP_INLINE_VISIBILITY
  408. __list_const_iterator& operator=(const __list_const_iterator& __p)
  409. {
  410. if (this != _VSTD::addressof(__p))
  411. {
  412. __get_db()->__iterator_copy(this, _VSTD::addressof(__p));
  413. __ptr_ = __p.__ptr_;
  414. }
  415. return *this;
  416. }
  417. #endif // _LIBCPP_ENABLE_DEBUG_MODE
  418. _LIBCPP_INLINE_VISIBILITY
  419. reference operator*() const
  420. {
  421. _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
  422. "Attempted to dereference a non-dereferenceable list::const_iterator");
  423. return __ptr_->__as_node()->__value_;
  424. }
  425. _LIBCPP_INLINE_VISIBILITY
  426. pointer operator->() const
  427. {
  428. _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
  429. "Attempted to dereference a non-dereferenceable list::const_iterator");
  430. return pointer_traits<pointer>::pointer_to(__ptr_->__as_node()->__value_);
  431. }
  432. _LIBCPP_INLINE_VISIBILITY
  433. __list_const_iterator& operator++()
  434. {
  435. _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
  436. "Attempted to increment a non-incrementable list::const_iterator");
  437. __ptr_ = __ptr_->__next_;
  438. return *this;
  439. }
  440. _LIBCPP_INLINE_VISIBILITY
  441. __list_const_iterator operator++(int) {__list_const_iterator __t(*this); ++(*this); return __t;}
  442. _LIBCPP_INLINE_VISIBILITY
  443. __list_const_iterator& operator--()
  444. {
  445. _LIBCPP_DEBUG_ASSERT(__get_const_db()->__decrementable(this),
  446. "Attempted to decrement a non-decrementable list::const_iterator");
  447. __ptr_ = __ptr_->__prev_;
  448. return *this;
  449. }
  450. _LIBCPP_INLINE_VISIBILITY
  451. __list_const_iterator operator--(int) {__list_const_iterator __t(*this); --(*this); return __t;}
  452. friend _LIBCPP_INLINE_VISIBILITY
  453. bool operator==(const __list_const_iterator& __x, const __list_const_iterator& __y)
  454. {
  455. return __x.__ptr_ == __y.__ptr_;
  456. }
  457. friend _LIBCPP_INLINE_VISIBILITY
  458. bool operator!=(const __list_const_iterator& __x, const __list_const_iterator& __y)
  459. {return !(__x == __y);}
  460. };
  461. template <class _Tp, class _Alloc>
  462. class __list_imp
  463. {
  464. __list_imp(const __list_imp&);
  465. __list_imp& operator=(const __list_imp&);
  466. public:
  467. typedef _Alloc allocator_type;
  468. typedef allocator_traits<allocator_type> __alloc_traits;
  469. typedef typename __alloc_traits::size_type size_type;
  470. protected:
  471. typedef _Tp value_type;
  472. typedef typename __alloc_traits::void_pointer __void_pointer;
  473. typedef __list_iterator<value_type, __void_pointer> iterator;
  474. typedef __list_const_iterator<value_type, __void_pointer> const_iterator;
  475. typedef __list_node_base<value_type, __void_pointer> __node_base;
  476. typedef __list_node<value_type, __void_pointer> __node;
  477. typedef __rebind_alloc<__alloc_traits, __node> __node_allocator;
  478. typedef allocator_traits<__node_allocator> __node_alloc_traits;
  479. typedef typename __node_alloc_traits::pointer __node_pointer;
  480. typedef typename __node_alloc_traits::pointer __node_const_pointer;
  481. typedef __list_node_pointer_traits<value_type, __void_pointer> __node_pointer_traits;
  482. typedef typename __node_pointer_traits::__link_pointer __link_pointer;
  483. typedef __link_pointer __link_const_pointer;
  484. typedef typename __alloc_traits::pointer pointer;
  485. typedef typename __alloc_traits::const_pointer const_pointer;
  486. typedef typename __alloc_traits::difference_type difference_type;
  487. typedef __rebind_alloc<__alloc_traits, __node_base> __node_base_allocator;
  488. typedef typename allocator_traits<__node_base_allocator>::pointer __node_base_pointer;
  489. static_assert((!is_same<allocator_type, __node_allocator>::value),
  490. "internal allocator type must differ from user-specified "
  491. "type; otherwise overload resolution breaks");
  492. __node_base __end_;
  493. __compressed_pair<size_type, __node_allocator> __size_alloc_;
  494. _LIBCPP_INLINE_VISIBILITY
  495. __link_pointer __end_as_link() const _NOEXCEPT {
  496. return __node_pointer_traits::__unsafe_link_pointer_cast(
  497. const_cast<__node_base&>(__end_).__self());
  498. }
  499. _LIBCPP_INLINE_VISIBILITY
  500. size_type& __sz() _NOEXCEPT {return __size_alloc_.first();}
  501. _LIBCPP_INLINE_VISIBILITY
  502. const size_type& __sz() const _NOEXCEPT
  503. {return __size_alloc_.first();}
  504. _LIBCPP_INLINE_VISIBILITY
  505. __node_allocator& __node_alloc() _NOEXCEPT
  506. {return __size_alloc_.second();}
  507. _LIBCPP_INLINE_VISIBILITY
  508. const __node_allocator& __node_alloc() const _NOEXCEPT
  509. {return __size_alloc_.second();}
  510. _LIBCPP_INLINE_VISIBILITY
  511. size_type __node_alloc_max_size() const _NOEXCEPT {
  512. return __node_alloc_traits::max_size(__node_alloc());
  513. }
  514. _LIBCPP_INLINE_VISIBILITY
  515. static void __unlink_nodes(__link_pointer __f, __link_pointer __l) _NOEXCEPT;
  516. _LIBCPP_INLINE_VISIBILITY
  517. __list_imp()
  518. _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value);
  519. _LIBCPP_INLINE_VISIBILITY
  520. __list_imp(const allocator_type& __a);
  521. _LIBCPP_INLINE_VISIBILITY
  522. __list_imp(const __node_allocator& __a);
  523. #ifndef _LIBCPP_CXX03_LANG
  524. __list_imp(__node_allocator&& __a) _NOEXCEPT;
  525. #endif
  526. ~__list_imp();
  527. void clear() _NOEXCEPT;
  528. _LIBCPP_INLINE_VISIBILITY
  529. bool empty() const _NOEXCEPT {return __sz() == 0;}
  530. _LIBCPP_INLINE_VISIBILITY
  531. iterator begin() _NOEXCEPT
  532. {
  533. return iterator(__end_.__next_, this);
  534. }
  535. _LIBCPP_INLINE_VISIBILITY
  536. const_iterator begin() const _NOEXCEPT
  537. {
  538. return const_iterator(__end_.__next_, this);
  539. }
  540. _LIBCPP_INLINE_VISIBILITY
  541. iterator end() _NOEXCEPT
  542. {
  543. return iterator(__end_as_link(), this);
  544. }
  545. _LIBCPP_INLINE_VISIBILITY
  546. const_iterator end() const _NOEXCEPT
  547. {
  548. return const_iterator(__end_as_link(), this);
  549. }
  550. void swap(__list_imp& __c)
  551. #if _LIBCPP_STD_VER >= 14
  552. _NOEXCEPT;
  553. #else
  554. _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
  555. __is_nothrow_swappable<allocator_type>::value);
  556. #endif
  557. _LIBCPP_INLINE_VISIBILITY
  558. void __copy_assign_alloc(const __list_imp& __c)
  559. {__copy_assign_alloc(__c, integral_constant<bool,
  560. __node_alloc_traits::propagate_on_container_copy_assignment::value>());}
  561. _LIBCPP_INLINE_VISIBILITY
  562. void __move_assign_alloc(__list_imp& __c)
  563. _NOEXCEPT_(
  564. !__node_alloc_traits::propagate_on_container_move_assignment::value ||
  565. is_nothrow_move_assignable<__node_allocator>::value)
  566. {__move_assign_alloc(__c, integral_constant<bool,
  567. __node_alloc_traits::propagate_on_container_move_assignment::value>());}
  568. private:
  569. _LIBCPP_INLINE_VISIBILITY
  570. void __copy_assign_alloc(const __list_imp& __c, true_type)
  571. {
  572. if (__node_alloc() != __c.__node_alloc())
  573. clear();
  574. __node_alloc() = __c.__node_alloc();
  575. }
  576. _LIBCPP_INLINE_VISIBILITY
  577. void __copy_assign_alloc(const __list_imp&, false_type)
  578. {}
  579. _LIBCPP_INLINE_VISIBILITY
  580. void __move_assign_alloc(__list_imp& __c, true_type)
  581. _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
  582. {
  583. __node_alloc() = _VSTD::move(__c.__node_alloc());
  584. }
  585. _LIBCPP_INLINE_VISIBILITY
  586. void __move_assign_alloc(__list_imp&, false_type)
  587. _NOEXCEPT
  588. {}
  589. };
  590. // Unlink nodes [__f, __l]
  591. template <class _Tp, class _Alloc>
  592. inline
  593. void
  594. __list_imp<_Tp, _Alloc>::__unlink_nodes(__link_pointer __f, __link_pointer __l)
  595. _NOEXCEPT
  596. {
  597. __f->__prev_->__next_ = __l->__next_;
  598. __l->__next_->__prev_ = __f->__prev_;
  599. }
  600. template <class _Tp, class _Alloc>
  601. inline
  602. __list_imp<_Tp, _Alloc>::__list_imp()
  603. _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value)
  604. : __size_alloc_(0, __default_init_tag())
  605. {
  606. }
  607. template <class _Tp, class _Alloc>
  608. inline
  609. __list_imp<_Tp, _Alloc>::__list_imp(const allocator_type& __a)
  610. : __size_alloc_(0, __node_allocator(__a))
  611. {
  612. }
  613. template <class _Tp, class _Alloc>
  614. inline __list_imp<_Tp, _Alloc>::__list_imp(const __node_allocator& __a)
  615. : __size_alloc_(0, __a) {}
  616. #ifndef _LIBCPP_CXX03_LANG
  617. template <class _Tp, class _Alloc>
  618. inline __list_imp<_Tp, _Alloc>::__list_imp(__node_allocator&& __a) _NOEXCEPT
  619. : __size_alloc_(0, _VSTD::move(__a)) {}
  620. #endif
  621. template <class _Tp, class _Alloc>
  622. __list_imp<_Tp, _Alloc>::~__list_imp() {
  623. clear();
  624. std::__debug_db_erase_c(this);
  625. }
  626. template <class _Tp, class _Alloc>
  627. void
  628. __list_imp<_Tp, _Alloc>::clear() _NOEXCEPT
  629. {
  630. if (!empty())
  631. {
  632. __node_allocator& __na = __node_alloc();
  633. __link_pointer __f = __end_.__next_;
  634. __link_pointer __l = __end_as_link();
  635. __unlink_nodes(__f, __l->__prev_);
  636. __sz() = 0;
  637. while (__f != __l)
  638. {
  639. __node_pointer __np = __f->__as_node();
  640. __f = __f->__next_;
  641. __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
  642. __node_alloc_traits::deallocate(__na, __np, 1);
  643. }
  644. std::__debug_db_invalidate_all(this);
  645. }
  646. }
  647. template <class _Tp, class _Alloc>
  648. void
  649. __list_imp<_Tp, _Alloc>::swap(__list_imp& __c)
  650. #if _LIBCPP_STD_VER >= 14
  651. _NOEXCEPT
  652. #else
  653. _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
  654. __is_nothrow_swappable<allocator_type>::value)
  655. #endif
  656. {
  657. _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
  658. this->__node_alloc() == __c.__node_alloc(),
  659. "list::swap: Either propagate_on_container_swap must be true"
  660. " or the allocators must compare equal");
  661. using _VSTD::swap;
  662. _VSTD::__swap_allocator(__node_alloc(), __c.__node_alloc());
  663. swap(__sz(), __c.__sz());
  664. swap(__end_, __c.__end_);
  665. if (__sz() == 0)
  666. __end_.__next_ = __end_.__prev_ = __end_as_link();
  667. else
  668. __end_.__prev_->__next_ = __end_.__next_->__prev_ = __end_as_link();
  669. if (__c.__sz() == 0)
  670. __c.__end_.__next_ = __c.__end_.__prev_ = __c.__end_as_link();
  671. else
  672. __c.__end_.__prev_->__next_ = __c.__end_.__next_->__prev_ = __c.__end_as_link();
  673. #ifdef _LIBCPP_ENABLE_DEBUG_MODE
  674. __libcpp_db* __db = __get_db();
  675. __c_node* __cn1 = __db->__find_c_and_lock(this);
  676. __c_node* __cn2 = __db->__find_c(_VSTD::addressof(__c));
  677. _VSTD::swap(__cn1->beg_, __cn2->beg_);
  678. _VSTD::swap(__cn1->end_, __cn2->end_);
  679. _VSTD::swap(__cn1->cap_, __cn2->cap_);
  680. for (__i_node** __p = __cn1->end_; __p != __cn1->beg_;)
  681. {
  682. --__p;
  683. const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
  684. if (__i->__ptr_ == __c.__end_as_link())
  685. {
  686. __cn2->__add(*__p);
  687. if (--__cn1->end_ != __p)
  688. _VSTD::memmove(__p, __p+1, (__cn1->end_ - __p)*sizeof(__i_node*));
  689. }
  690. else
  691. (*__p)->__c_ = __cn1;
  692. }
  693. for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
  694. {
  695. --__p;
  696. const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
  697. if (__i->__ptr_ == __end_as_link())
  698. {
  699. __cn1->__add(*__p);
  700. if (--__cn2->end_ != __p)
  701. _VSTD::memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
  702. }
  703. else
  704. (*__p)->__c_ = __cn2;
  705. }
  706. __db->unlock();
  707. #endif
  708. }
  709. template <class _Tp, class _Alloc /*= allocator<_Tp>*/>
  710. class _LIBCPP_TEMPLATE_VIS list
  711. : private __list_imp<_Tp, _Alloc>
  712. {
  713. typedef __list_imp<_Tp, _Alloc> base;
  714. typedef typename base::__node __node;
  715. typedef typename base::__node_allocator __node_allocator;
  716. typedef typename base::__node_pointer __node_pointer;
  717. typedef typename base::__node_alloc_traits __node_alloc_traits;
  718. typedef typename base::__node_base __node_base;
  719. typedef typename base::__node_base_pointer __node_base_pointer;
  720. typedef typename base::__link_pointer __link_pointer;
  721. public:
  722. typedef _Tp value_type;
  723. typedef _Alloc allocator_type;
  724. static_assert((is_same<value_type, typename allocator_type::value_type>::value),
  725. "Invalid allocator::value_type");
  726. typedef value_type& reference;
  727. typedef const value_type& const_reference;
  728. typedef typename base::pointer pointer;
  729. typedef typename base::const_pointer const_pointer;
  730. typedef typename base::size_type size_type;
  731. typedef typename base::difference_type difference_type;
  732. typedef typename base::iterator iterator;
  733. typedef typename base::const_iterator const_iterator;
  734. typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
  735. typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
  736. #if _LIBCPP_STD_VER > 17
  737. typedef size_type __remove_return_type;
  738. #else
  739. typedef void __remove_return_type;
  740. #endif
  741. static_assert(is_same<allocator_type, __rebind_alloc<allocator_traits<allocator_type>, value_type> >::value,
  742. "[allocator.requirements] states that rebinding an allocator to the same type should result in the "
  743. "original allocator");
  744. _LIBCPP_INLINE_VISIBILITY
  745. list()
  746. _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value)
  747. {
  748. _VSTD::__debug_db_insert_c(this);
  749. }
  750. _LIBCPP_INLINE_VISIBILITY
  751. explicit list(const allocator_type& __a) : base(__a)
  752. {
  753. _VSTD::__debug_db_insert_c(this);
  754. }
  755. explicit list(size_type __n);
  756. #if _LIBCPP_STD_VER > 11
  757. explicit list(size_type __n, const allocator_type& __a);
  758. #endif
  759. list(size_type __n, const value_type& __x);
  760. template <class = __enable_if_t<__is_allocator<_Alloc>::value> >
  761. list(size_type __n, const value_type& __x, const allocator_type& __a) : base(__a)
  762. {
  763. _VSTD::__debug_db_insert_c(this);
  764. for (; __n > 0; --__n)
  765. push_back(__x);
  766. }
  767. template <class _InpIter>
  768. list(_InpIter __f, _InpIter __l,
  769. __enable_if_t<__is_cpp17_input_iterator<_InpIter>::value>* = 0);
  770. template <class _InpIter>
  771. list(_InpIter __f, _InpIter __l, const allocator_type& __a,
  772. __enable_if_t<__is_cpp17_input_iterator<_InpIter>::value>* = 0);
  773. list(const list& __c);
  774. list(const list& __c, const __type_identity_t<allocator_type>& __a);
  775. _LIBCPP_INLINE_VISIBILITY
  776. list& operator=(const list& __c);
  777. #ifndef _LIBCPP_CXX03_LANG
  778. list(initializer_list<value_type> __il);
  779. list(initializer_list<value_type> __il, const allocator_type& __a);
  780. _LIBCPP_INLINE_VISIBILITY
  781. list(list&& __c)
  782. _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value);
  783. _LIBCPP_INLINE_VISIBILITY
  784. list(list&& __c, const __type_identity_t<allocator_type>& __a);
  785. _LIBCPP_INLINE_VISIBILITY
  786. list& operator=(list&& __c)
  787. _NOEXCEPT_(
  788. __node_alloc_traits::propagate_on_container_move_assignment::value &&
  789. is_nothrow_move_assignable<__node_allocator>::value);
  790. _LIBCPP_INLINE_VISIBILITY
  791. list& operator=(initializer_list<value_type> __il)
  792. {assign(__il.begin(), __il.end()); return *this;}
  793. _LIBCPP_INLINE_VISIBILITY
  794. void assign(initializer_list<value_type> __il)
  795. {assign(__il.begin(), __il.end());}
  796. #endif // _LIBCPP_CXX03_LANG
  797. template <class _InpIter>
  798. void assign(_InpIter __f, _InpIter __l,
  799. __enable_if_t<__is_cpp17_input_iterator<_InpIter>::value>* = 0);
  800. void assign(size_type __n, const value_type& __x);
  801. _LIBCPP_INLINE_VISIBILITY
  802. allocator_type get_allocator() const _NOEXCEPT;
  803. _LIBCPP_INLINE_VISIBILITY
  804. size_type size() const _NOEXCEPT {return base::__sz();}
  805. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
  806. bool empty() const _NOEXCEPT {return base::empty();}
  807. _LIBCPP_INLINE_VISIBILITY
  808. size_type max_size() const _NOEXCEPT
  809. {
  810. return _VSTD::min<size_type>(
  811. base::__node_alloc_max_size(),
  812. numeric_limits<difference_type >::max());
  813. }
  814. _LIBCPP_INLINE_VISIBILITY
  815. iterator begin() _NOEXCEPT {return base::begin();}
  816. _LIBCPP_INLINE_VISIBILITY
  817. const_iterator begin() const _NOEXCEPT {return base::begin();}
  818. _LIBCPP_INLINE_VISIBILITY
  819. iterator end() _NOEXCEPT {return base::end();}
  820. _LIBCPP_INLINE_VISIBILITY
  821. const_iterator end() const _NOEXCEPT {return base::end();}
  822. _LIBCPP_INLINE_VISIBILITY
  823. const_iterator cbegin() const _NOEXCEPT {return base::begin();}
  824. _LIBCPP_INLINE_VISIBILITY
  825. const_iterator cend() const _NOEXCEPT {return base::end();}
  826. _LIBCPP_INLINE_VISIBILITY
  827. reverse_iterator rbegin() _NOEXCEPT
  828. {return reverse_iterator(end());}
  829. _LIBCPP_INLINE_VISIBILITY
  830. const_reverse_iterator rbegin() const _NOEXCEPT
  831. {return const_reverse_iterator(end());}
  832. _LIBCPP_INLINE_VISIBILITY
  833. reverse_iterator rend() _NOEXCEPT
  834. {return reverse_iterator(begin());}
  835. _LIBCPP_INLINE_VISIBILITY
  836. const_reverse_iterator rend() const _NOEXCEPT
  837. {return const_reverse_iterator(begin());}
  838. _LIBCPP_INLINE_VISIBILITY
  839. const_reverse_iterator crbegin() const _NOEXCEPT
  840. {return const_reverse_iterator(end());}
  841. _LIBCPP_INLINE_VISIBILITY
  842. const_reverse_iterator crend() const _NOEXCEPT
  843. {return const_reverse_iterator(begin());}
  844. _LIBCPP_INLINE_VISIBILITY
  845. reference front()
  846. {
  847. _LIBCPP_ASSERT(!empty(), "list::front called on empty list");
  848. return base::__end_.__next_->__as_node()->__value_;
  849. }
  850. _LIBCPP_INLINE_VISIBILITY
  851. const_reference front() const
  852. {
  853. _LIBCPP_ASSERT(!empty(), "list::front called on empty list");
  854. return base::__end_.__next_->__as_node()->__value_;
  855. }
  856. _LIBCPP_INLINE_VISIBILITY
  857. reference back()
  858. {
  859. _LIBCPP_ASSERT(!empty(), "list::back called on empty list");
  860. return base::__end_.__prev_->__as_node()->__value_;
  861. }
  862. _LIBCPP_INLINE_VISIBILITY
  863. const_reference back() const
  864. {
  865. _LIBCPP_ASSERT(!empty(), "list::back called on empty list");
  866. return base::__end_.__prev_->__as_node()->__value_;
  867. }
  868. #ifndef _LIBCPP_CXX03_LANG
  869. void push_front(value_type&& __x);
  870. void push_back(value_type&& __x);
  871. template <class... _Args>
  872. #if _LIBCPP_STD_VER > 14
  873. reference emplace_front(_Args&&... __args);
  874. #else
  875. void emplace_front(_Args&&... __args);
  876. #endif
  877. template <class... _Args>
  878. #if _LIBCPP_STD_VER > 14
  879. reference emplace_back(_Args&&... __args);
  880. #else
  881. void emplace_back(_Args&&... __args);
  882. #endif
  883. template <class... _Args>
  884. iterator emplace(const_iterator __p, _Args&&... __args);
  885. iterator insert(const_iterator __p, value_type&& __x);
  886. _LIBCPP_INLINE_VISIBILITY
  887. iterator insert(const_iterator __p, initializer_list<value_type> __il)
  888. {return insert(__p, __il.begin(), __il.end());}
  889. #endif // _LIBCPP_CXX03_LANG
  890. void push_front(const value_type& __x);
  891. void push_back(const value_type& __x);
  892. #ifndef _LIBCPP_CXX03_LANG
  893. template <class _Arg>
  894. _LIBCPP_INLINE_VISIBILITY
  895. void __emplace_back(_Arg&& __arg) { emplace_back(_VSTD::forward<_Arg>(__arg)); }
  896. #else
  897. _LIBCPP_INLINE_VISIBILITY
  898. void __emplace_back(value_type const& __arg) { push_back(__arg); }
  899. #endif
  900. iterator insert(const_iterator __p, const value_type& __x);
  901. iterator insert(const_iterator __p, size_type __n, const value_type& __x);
  902. template <class _InpIter>
  903. iterator insert(const_iterator __p, _InpIter __f, _InpIter __l,
  904. __enable_if_t<__is_cpp17_input_iterator<_InpIter>::value>* = 0);
  905. _LIBCPP_INLINE_VISIBILITY
  906. void swap(list& __c)
  907. #if _LIBCPP_STD_VER >= 14
  908. _NOEXCEPT
  909. #else
  910. _NOEXCEPT_(!__node_alloc_traits::propagate_on_container_swap::value ||
  911. __is_nothrow_swappable<__node_allocator>::value)
  912. #endif
  913. {base::swap(__c);}
  914. _LIBCPP_INLINE_VISIBILITY
  915. void clear() _NOEXCEPT {base::clear();}
  916. void pop_front();
  917. void pop_back();
  918. iterator erase(const_iterator __p);
  919. iterator erase(const_iterator __f, const_iterator __l);
  920. void resize(size_type __n);
  921. void resize(size_type __n, const value_type& __x);
  922. void splice(const_iterator __p, list& __c);
  923. #ifndef _LIBCPP_CXX03_LANG
  924. _LIBCPP_INLINE_VISIBILITY
  925. void splice(const_iterator __p, list&& __c) {splice(__p, __c);}
  926. _LIBCPP_INLINE_VISIBILITY
  927. void splice(const_iterator __p, list&& __c, const_iterator __i)
  928. {splice(__p, __c, __i);}
  929. _LIBCPP_INLINE_VISIBILITY
  930. void splice(const_iterator __p, list&& __c, const_iterator __f, const_iterator __l)
  931. {splice(__p, __c, __f, __l);}
  932. #endif
  933. void splice(const_iterator __p, list& __c, const_iterator __i);
  934. void splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l);
  935. __remove_return_type remove(const value_type& __x);
  936. template <class _Pred> __remove_return_type remove_if(_Pred __pred);
  937. _LIBCPP_INLINE_VISIBILITY
  938. __remove_return_type unique() { return unique(__equal_to()); }
  939. template <class _BinaryPred>
  940. __remove_return_type unique(_BinaryPred __binary_pred);
  941. _LIBCPP_INLINE_VISIBILITY
  942. void merge(list& __c);
  943. #ifndef _LIBCPP_CXX03_LANG
  944. _LIBCPP_INLINE_VISIBILITY
  945. void merge(list&& __c) {merge(__c);}
  946. template <class _Comp>
  947. _LIBCPP_INLINE_VISIBILITY
  948. void merge(list&& __c, _Comp __comp) {merge(__c, __comp);}
  949. #endif
  950. template <class _Comp>
  951. void merge(list& __c, _Comp __comp);
  952. _LIBCPP_INLINE_VISIBILITY
  953. void sort();
  954. template <class _Comp>
  955. _LIBCPP_INLINE_VISIBILITY
  956. void sort(_Comp __comp);
  957. void reverse() _NOEXCEPT;
  958. bool __invariants() const;
  959. typedef __allocator_destructor<__node_allocator> __node_destructor;
  960. typedef unique_ptr<__node, __node_destructor> __hold_pointer;
  961. _LIBCPP_INLINE_VISIBILITY
  962. __hold_pointer __allocate_node(__node_allocator& __na) {
  963. __node_pointer __p = __node_alloc_traits::allocate(__na, 1);
  964. __p->__prev_ = nullptr;
  965. return __hold_pointer(__p, __node_destructor(__na, 1));
  966. }
  967. #ifdef _LIBCPP_ENABLE_DEBUG_MODE
  968. bool __dereferenceable(const const_iterator* __i) const;
  969. bool __decrementable(const const_iterator* __i) const;
  970. bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
  971. bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
  972. #endif // _LIBCPP_ENABLE_DEBUG_MODE
  973. private:
  974. _LIBCPP_INLINE_VISIBILITY
  975. static void __link_nodes (__link_pointer __p, __link_pointer __f, __link_pointer __l);
  976. _LIBCPP_INLINE_VISIBILITY
  977. void __link_nodes_at_front(__link_pointer __f, __link_pointer __l);
  978. _LIBCPP_INLINE_VISIBILITY
  979. void __link_nodes_at_back (__link_pointer __f, __link_pointer __l);
  980. iterator __iterator(size_type __n);
  981. template <class _Comp>
  982. static iterator __sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp);
  983. void __move_assign(list& __c, true_type)
  984. _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value);
  985. void __move_assign(list& __c, false_type);
  986. };
  987. #if _LIBCPP_STD_VER >= 17
  988. template<class _InputIterator,
  989. class _Alloc = allocator<__iter_value_type<_InputIterator>>,
  990. class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
  991. class = enable_if_t<__is_allocator<_Alloc>::value>
  992. >
  993. list(_InputIterator, _InputIterator)
  994. -> list<__iter_value_type<_InputIterator>, _Alloc>;
  995. template<class _InputIterator,
  996. class _Alloc,
  997. class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
  998. class = enable_if_t<__is_allocator<_Alloc>::value>
  999. >
  1000. list(_InputIterator, _InputIterator, _Alloc)
  1001. -> list<__iter_value_type<_InputIterator>, _Alloc>;
  1002. #endif
  1003. // Link in nodes [__f, __l] just prior to __p
  1004. template <class _Tp, class _Alloc>
  1005. inline
  1006. void
  1007. list<_Tp, _Alloc>::__link_nodes(__link_pointer __p, __link_pointer __f, __link_pointer __l)
  1008. {
  1009. __p->__prev_->__next_ = __f;
  1010. __f->__prev_ = __p->__prev_;
  1011. __p->__prev_ = __l;
  1012. __l->__next_ = __p;
  1013. }
  1014. // Link in nodes [__f, __l] at the front of the list
  1015. template <class _Tp, class _Alloc>
  1016. inline
  1017. void
  1018. list<_Tp, _Alloc>::__link_nodes_at_front(__link_pointer __f, __link_pointer __l)
  1019. {
  1020. __f->__prev_ = base::__end_as_link();
  1021. __l->__next_ = base::__end_.__next_;
  1022. __l->__next_->__prev_ = __l;
  1023. base::__end_.__next_ = __f;
  1024. }
  1025. // Link in nodes [__f, __l] at the back of the list
  1026. template <class _Tp, class _Alloc>
  1027. inline
  1028. void
  1029. list<_Tp, _Alloc>::__link_nodes_at_back(__link_pointer __f, __link_pointer __l)
  1030. {
  1031. __l->__next_ = base::__end_as_link();
  1032. __f->__prev_ = base::__end_.__prev_;
  1033. __f->__prev_->__next_ = __f;
  1034. base::__end_.__prev_ = __l;
  1035. }
  1036. template <class _Tp, class _Alloc>
  1037. inline
  1038. typename list<_Tp, _Alloc>::iterator
  1039. list<_Tp, _Alloc>::__iterator(size_type __n)
  1040. {
  1041. return __n <= base::__sz() / 2 ? _VSTD::next(begin(), __n)
  1042. : _VSTD::prev(end(), base::__sz() - __n);
  1043. }
  1044. template <class _Tp, class _Alloc>
  1045. list<_Tp, _Alloc>::list(size_type __n)
  1046. {
  1047. _VSTD::__debug_db_insert_c(this);
  1048. for (; __n > 0; --__n)
  1049. #ifndef _LIBCPP_CXX03_LANG
  1050. emplace_back();
  1051. #else
  1052. push_back(value_type());
  1053. #endif
  1054. }
  1055. #if _LIBCPP_STD_VER > 11
  1056. template <class _Tp, class _Alloc>
  1057. list<_Tp, _Alloc>::list(size_type __n, const allocator_type& __a) : base(__a)
  1058. {
  1059. _VSTD::__debug_db_insert_c(this);
  1060. for (; __n > 0; --__n)
  1061. emplace_back();
  1062. }
  1063. #endif
  1064. template <class _Tp, class _Alloc>
  1065. list<_Tp, _Alloc>::list(size_type __n, const value_type& __x)
  1066. {
  1067. _VSTD::__debug_db_insert_c(this);
  1068. for (; __n > 0; --__n)
  1069. push_back(__x);
  1070. }
  1071. template <class _Tp, class _Alloc>
  1072. template <class _InpIter>
  1073. list<_Tp, _Alloc>::list(_InpIter __f, _InpIter __l,
  1074. __enable_if_t<__is_cpp17_input_iterator<_InpIter>::value>*)
  1075. {
  1076. _VSTD::__debug_db_insert_c(this);
  1077. for (; __f != __l; ++__f)
  1078. __emplace_back(*__f);
  1079. }
  1080. template <class _Tp, class _Alloc>
  1081. template <class _InpIter>
  1082. list<_Tp, _Alloc>::list(_InpIter __f, _InpIter __l, const allocator_type& __a,
  1083. __enable_if_t<__is_cpp17_input_iterator<_InpIter>::value>*)
  1084. : base(__a)
  1085. {
  1086. _VSTD::__debug_db_insert_c(this);
  1087. for (; __f != __l; ++__f)
  1088. __emplace_back(*__f);
  1089. }
  1090. template <class _Tp, class _Alloc>
  1091. list<_Tp, _Alloc>::list(const list& __c)
  1092. : base(__node_alloc_traits::select_on_container_copy_construction(
  1093. __c.__node_alloc())) {
  1094. _VSTD::__debug_db_insert_c(this);
  1095. for (const_iterator __i = __c.begin(), __e = __c.end(); __i != __e; ++__i)
  1096. push_back(*__i);
  1097. }
  1098. template <class _Tp, class _Alloc>
  1099. list<_Tp, _Alloc>::list(const list& __c, const __type_identity_t<allocator_type>& __a)
  1100. : base(__a)
  1101. {
  1102. _VSTD::__debug_db_insert_c(this);
  1103. for (const_iterator __i = __c.begin(), __e = __c.end(); __i != __e; ++__i)
  1104. push_back(*__i);
  1105. }
  1106. #ifndef _LIBCPP_CXX03_LANG
  1107. template <class _Tp, class _Alloc>
  1108. list<_Tp, _Alloc>::list(initializer_list<value_type> __il, const allocator_type& __a)
  1109. : base(__a)
  1110. {
  1111. _VSTD::__debug_db_insert_c(this);
  1112. for (typename initializer_list<value_type>::const_iterator __i = __il.begin(),
  1113. __e = __il.end(); __i != __e; ++__i)
  1114. push_back(*__i);
  1115. }
  1116. template <class _Tp, class _Alloc>
  1117. list<_Tp, _Alloc>::list(initializer_list<value_type> __il)
  1118. {
  1119. _VSTD::__debug_db_insert_c(this);
  1120. for (typename initializer_list<value_type>::const_iterator __i = __il.begin(),
  1121. __e = __il.end(); __i != __e; ++__i)
  1122. push_back(*__i);
  1123. }
  1124. template <class _Tp, class _Alloc>
  1125. inline list<_Tp, _Alloc>::list(list&& __c)
  1126. _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value)
  1127. : base(_VSTD::move(__c.__node_alloc())) {
  1128. _VSTD::__debug_db_insert_c(this);
  1129. splice(end(), __c);
  1130. }
  1131. template <class _Tp, class _Alloc>
  1132. inline
  1133. list<_Tp, _Alloc>::list(list&& __c, const __type_identity_t<allocator_type>& __a)
  1134. : base(__a)
  1135. {
  1136. _VSTD::__debug_db_insert_c(this);
  1137. if (__a == __c.get_allocator())
  1138. splice(end(), __c);
  1139. else
  1140. {
  1141. typedef move_iterator<iterator> _Ip;
  1142. assign(_Ip(__c.begin()), _Ip(__c.end()));
  1143. }
  1144. }
  1145. template <class _Tp, class _Alloc>
  1146. inline
  1147. list<_Tp, _Alloc>&
  1148. list<_Tp, _Alloc>::operator=(list&& __c)
  1149. _NOEXCEPT_(
  1150. __node_alloc_traits::propagate_on_container_move_assignment::value &&
  1151. is_nothrow_move_assignable<__node_allocator>::value)
  1152. {
  1153. __move_assign(__c, integral_constant<bool,
  1154. __node_alloc_traits::propagate_on_container_move_assignment::value>());
  1155. return *this;
  1156. }
  1157. template <class _Tp, class _Alloc>
  1158. void
  1159. list<_Tp, _Alloc>::__move_assign(list& __c, false_type)
  1160. {
  1161. if (base::__node_alloc() != __c.__node_alloc())
  1162. {
  1163. typedef move_iterator<iterator> _Ip;
  1164. assign(_Ip(__c.begin()), _Ip(__c.end()));
  1165. }
  1166. else
  1167. __move_assign(__c, true_type());
  1168. }
  1169. template <class _Tp, class _Alloc>
  1170. void
  1171. list<_Tp, _Alloc>::__move_assign(list& __c, true_type)
  1172. _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
  1173. {
  1174. clear();
  1175. base::__move_assign_alloc(__c);
  1176. splice(end(), __c);
  1177. }
  1178. #endif // _LIBCPP_CXX03_LANG
  1179. template <class _Tp, class _Alloc>
  1180. inline
  1181. list<_Tp, _Alloc>&
  1182. list<_Tp, _Alloc>::operator=(const list& __c)
  1183. {
  1184. if (this != _VSTD::addressof(__c))
  1185. {
  1186. base::__copy_assign_alloc(__c);
  1187. assign(__c.begin(), __c.end());
  1188. }
  1189. return *this;
  1190. }
  1191. template <class _Tp, class _Alloc>
  1192. template <class _InpIter>
  1193. void
  1194. list<_Tp, _Alloc>::assign(_InpIter __f, _InpIter __l,
  1195. __enable_if_t<__is_cpp17_input_iterator<_InpIter>::value>*)
  1196. {
  1197. iterator __i = begin();
  1198. iterator __e = end();
  1199. for (; __f != __l && __i != __e; ++__f, (void) ++__i)
  1200. *__i = *__f;
  1201. if (__i == __e)
  1202. insert(__e, __f, __l);
  1203. else
  1204. erase(__i, __e);
  1205. std::__debug_db_invalidate_all(this);
  1206. }
  1207. template <class _Tp, class _Alloc>
  1208. void
  1209. list<_Tp, _Alloc>::assign(size_type __n, const value_type& __x)
  1210. {
  1211. iterator __i = begin();
  1212. iterator __e = end();
  1213. for (; __n > 0 && __i != __e; --__n, (void) ++__i)
  1214. *__i = __x;
  1215. if (__i == __e)
  1216. insert(__e, __n, __x);
  1217. else
  1218. erase(__i, __e);
  1219. std::__debug_db_invalidate_all(this);
  1220. }
  1221. template <class _Tp, class _Alloc>
  1222. inline
  1223. _Alloc
  1224. list<_Tp, _Alloc>::get_allocator() const _NOEXCEPT
  1225. {
  1226. return allocator_type(base::__node_alloc());
  1227. }
  1228. template <class _Tp, class _Alloc>
  1229. typename list<_Tp, _Alloc>::iterator
  1230. list<_Tp, _Alloc>::insert(const_iterator __p, const value_type& __x)
  1231. {
  1232. _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__p)) == this,
  1233. "list::insert(iterator, x) called with an iterator not referring to this list");
  1234. __node_allocator& __na = base::__node_alloc();
  1235. __hold_pointer __hold = __allocate_node(__na);
  1236. __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
  1237. __link_nodes(__p.__ptr_, __hold->__as_link(), __hold->__as_link());
  1238. ++base::__sz();
  1239. return iterator(__hold.release()->__as_link(), this);
  1240. }
  1241. template <class _Tp, class _Alloc>
  1242. typename list<_Tp, _Alloc>::iterator
  1243. list<_Tp, _Alloc>::insert(const_iterator __p, size_type __n, const value_type& __x)
  1244. {
  1245. _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__p)) == this,
  1246. "list::insert(iterator, n, x) called with an iterator not referring to this list");
  1247. iterator __r(__p.__ptr_, this);
  1248. if (__n > 0)
  1249. {
  1250. size_type __ds = 0;
  1251. __node_allocator& __na = base::__node_alloc();
  1252. __hold_pointer __hold = __allocate_node(__na);
  1253. __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
  1254. ++__ds;
  1255. __r = iterator(__hold->__as_link(), this);
  1256. __hold.release();
  1257. iterator __e = __r;
  1258. #ifndef _LIBCPP_NO_EXCEPTIONS
  1259. try
  1260. {
  1261. #endif // _LIBCPP_NO_EXCEPTIONS
  1262. for (--__n; __n != 0; --__n, (void) ++__e, ++__ds)
  1263. {
  1264. __hold.reset(__node_alloc_traits::allocate(__na, 1));
  1265. __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
  1266. __e.__ptr_->__next_ = __hold->__as_link();
  1267. __hold->__prev_ = __e.__ptr_;
  1268. __hold.release();
  1269. }
  1270. #ifndef _LIBCPP_NO_EXCEPTIONS
  1271. }
  1272. catch (...)
  1273. {
  1274. while (true)
  1275. {
  1276. __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
  1277. __link_pointer __prev = __e.__ptr_->__prev_;
  1278. __node_alloc_traits::deallocate(__na, __e.__ptr_->__as_node(), 1);
  1279. if (__prev == 0)
  1280. break;
  1281. __e = iterator(__prev, this);
  1282. }
  1283. throw;
  1284. }
  1285. #endif // _LIBCPP_NO_EXCEPTIONS
  1286. __link_nodes(__p.__ptr_, __r.__ptr_, __e.__ptr_);
  1287. base::__sz() += __ds;
  1288. }
  1289. return __r;
  1290. }
  1291. template <class _Tp, class _Alloc>
  1292. template <class _InpIter>
  1293. typename list<_Tp, _Alloc>::iterator
  1294. list<_Tp, _Alloc>::insert(const_iterator __p, _InpIter __f, _InpIter __l,
  1295. __enable_if_t<__is_cpp17_input_iterator<_InpIter>::value>*)
  1296. {
  1297. _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__p)) == this,
  1298. "list::insert(iterator, range) called with an iterator not referring to this list");
  1299. iterator __r(__p.__ptr_, this);
  1300. if (__f != __l)
  1301. {
  1302. size_type __ds = 0;
  1303. __node_allocator& __na = base::__node_alloc();
  1304. __hold_pointer __hold = __allocate_node(__na);
  1305. __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), *__f);
  1306. ++__ds;
  1307. __r = iterator(__hold.get()->__as_link(), this);
  1308. __hold.release();
  1309. iterator __e = __r;
  1310. #ifndef _LIBCPP_NO_EXCEPTIONS
  1311. try
  1312. {
  1313. #endif // _LIBCPP_NO_EXCEPTIONS
  1314. for (++__f; __f != __l; ++__f, (void) ++__e, ++__ds)
  1315. {
  1316. __hold.reset(__node_alloc_traits::allocate(__na, 1));
  1317. __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), *__f);
  1318. __e.__ptr_->__next_ = __hold.get()->__as_link();
  1319. __hold->__prev_ = __e.__ptr_;
  1320. __hold.release();
  1321. }
  1322. #ifndef _LIBCPP_NO_EXCEPTIONS
  1323. }
  1324. catch (...)
  1325. {
  1326. while (true)
  1327. {
  1328. __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
  1329. __link_pointer __prev = __e.__ptr_->__prev_;
  1330. __node_alloc_traits::deallocate(__na, __e.__ptr_->__as_node(), 1);
  1331. if (__prev == 0)
  1332. break;
  1333. __e = iterator(__prev, this);
  1334. }
  1335. throw;
  1336. }
  1337. #endif // _LIBCPP_NO_EXCEPTIONS
  1338. __link_nodes(__p.__ptr_, __r.__ptr_, __e.__ptr_);
  1339. base::__sz() += __ds;
  1340. }
  1341. return __r;
  1342. }
  1343. template <class _Tp, class _Alloc>
  1344. void
  1345. list<_Tp, _Alloc>::push_front(const value_type& __x)
  1346. {
  1347. __node_allocator& __na = base::__node_alloc();
  1348. __hold_pointer __hold = __allocate_node(__na);
  1349. __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
  1350. __link_pointer __nl = __hold->__as_link();
  1351. __link_nodes_at_front(__nl, __nl);
  1352. ++base::__sz();
  1353. __hold.release();
  1354. }
  1355. template <class _Tp, class _Alloc>
  1356. void
  1357. list<_Tp, _Alloc>::push_back(const value_type& __x)
  1358. {
  1359. __node_allocator& __na = base::__node_alloc();
  1360. __hold_pointer __hold = __allocate_node(__na);
  1361. __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
  1362. __link_nodes_at_back(__hold.get()->__as_link(), __hold.get()->__as_link());
  1363. ++base::__sz();
  1364. __hold.release();
  1365. }
  1366. #ifndef _LIBCPP_CXX03_LANG
  1367. template <class _Tp, class _Alloc>
  1368. void
  1369. list<_Tp, _Alloc>::push_front(value_type&& __x)
  1370. {
  1371. __node_allocator& __na = base::__node_alloc();
  1372. __hold_pointer __hold = __allocate_node(__na);
  1373. __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
  1374. __link_nodes_at_front(__hold.get()->__as_link(), __hold.get()->__as_link());
  1375. ++base::__sz();
  1376. __hold.release();
  1377. }
  1378. template <class _Tp, class _Alloc>
  1379. void
  1380. list<_Tp, _Alloc>::push_back(value_type&& __x)
  1381. {
  1382. __node_allocator& __na = base::__node_alloc();
  1383. __hold_pointer __hold = __allocate_node(__na);
  1384. __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
  1385. __link_nodes_at_back(__hold.get()->__as_link(), __hold.get()->__as_link());
  1386. ++base::__sz();
  1387. __hold.release();
  1388. }
  1389. template <class _Tp, class _Alloc>
  1390. template <class... _Args>
  1391. #if _LIBCPP_STD_VER > 14
  1392. typename list<_Tp, _Alloc>::reference
  1393. #else
  1394. void
  1395. #endif
  1396. list<_Tp, _Alloc>::emplace_front(_Args&&... __args)
  1397. {
  1398. __node_allocator& __na = base::__node_alloc();
  1399. __hold_pointer __hold = __allocate_node(__na);
  1400. __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
  1401. __link_nodes_at_front(__hold.get()->__as_link(), __hold.get()->__as_link());
  1402. ++base::__sz();
  1403. #if _LIBCPP_STD_VER > 14
  1404. return __hold.release()->__value_;
  1405. #else
  1406. __hold.release();
  1407. #endif
  1408. }
  1409. template <class _Tp, class _Alloc>
  1410. template <class... _Args>
  1411. #if _LIBCPP_STD_VER > 14
  1412. typename list<_Tp, _Alloc>::reference
  1413. #else
  1414. void
  1415. #endif
  1416. list<_Tp, _Alloc>::emplace_back(_Args&&... __args)
  1417. {
  1418. __node_allocator& __na = base::__node_alloc();
  1419. __hold_pointer __hold = __allocate_node(__na);
  1420. __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
  1421. __link_pointer __nl = __hold->__as_link();
  1422. __link_nodes_at_back(__nl, __nl);
  1423. ++base::__sz();
  1424. #if _LIBCPP_STD_VER > 14
  1425. return __hold.release()->__value_;
  1426. #else
  1427. __hold.release();
  1428. #endif
  1429. }
  1430. template <class _Tp, class _Alloc>
  1431. template <class... _Args>
  1432. typename list<_Tp, _Alloc>::iterator
  1433. list<_Tp, _Alloc>::emplace(const_iterator __p, _Args&&... __args)
  1434. {
  1435. _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__p)) == this,
  1436. "list::emplace(iterator, args...) called with an iterator not referring to this list");
  1437. __node_allocator& __na = base::__node_alloc();
  1438. __hold_pointer __hold = __allocate_node(__na);
  1439. __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
  1440. __link_pointer __nl = __hold.get()->__as_link();
  1441. __link_nodes(__p.__ptr_, __nl, __nl);
  1442. ++base::__sz();
  1443. __hold.release();
  1444. return iterator(__nl, this);
  1445. }
  1446. template <class _Tp, class _Alloc>
  1447. typename list<_Tp, _Alloc>::iterator
  1448. list<_Tp, _Alloc>::insert(const_iterator __p, value_type&& __x)
  1449. {
  1450. _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__p)) == this,
  1451. "list::insert(iterator, x) called with an iterator not referring to this list");
  1452. __node_allocator& __na = base::__node_alloc();
  1453. __hold_pointer __hold = __allocate_node(__na);
  1454. __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
  1455. __link_pointer __nl = __hold->__as_link();
  1456. __link_nodes(__p.__ptr_, __nl, __nl);
  1457. ++base::__sz();
  1458. __hold.release();
  1459. return iterator(__nl, this);
  1460. }
  1461. #endif // _LIBCPP_CXX03_LANG
  1462. template <class _Tp, class _Alloc>
  1463. void
  1464. list<_Tp, _Alloc>::pop_front()
  1465. {
  1466. _LIBCPP_ASSERT(!empty(), "list::pop_front() called with empty list");
  1467. __node_allocator& __na = base::__node_alloc();
  1468. __link_pointer __n = base::__end_.__next_;
  1469. base::__unlink_nodes(__n, __n);
  1470. --base::__sz();
  1471. #ifdef _LIBCPP_ENABLE_DEBUG_MODE
  1472. __c_node* __c = __get_db()->__find_c_and_lock(this);
  1473. for (__i_node** __p = __c->end_; __p != __c->beg_; )
  1474. {
  1475. --__p;
  1476. iterator* __i = static_cast<iterator*>((*__p)->__i_);
  1477. if (__i->__ptr_ == __n)
  1478. {
  1479. (*__p)->__c_ = nullptr;
  1480. if (--__c->end_ != __p)
  1481. _VSTD::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
  1482. }
  1483. }
  1484. __get_db()->unlock();
  1485. #endif
  1486. __node_pointer __np = __n->__as_node();
  1487. __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
  1488. __node_alloc_traits::deallocate(__na, __np, 1);
  1489. }
  1490. template <class _Tp, class _Alloc>
  1491. void
  1492. list<_Tp, _Alloc>::pop_back()
  1493. {
  1494. _LIBCPP_ASSERT(!empty(), "list::pop_back() called on an empty list");
  1495. __node_allocator& __na = base::__node_alloc();
  1496. __link_pointer __n = base::__end_.__prev_;
  1497. base::__unlink_nodes(__n, __n);
  1498. --base::__sz();
  1499. #ifdef _LIBCPP_ENABLE_DEBUG_MODE
  1500. __c_node* __c = __get_db()->__find_c_and_lock(this);
  1501. for (__i_node** __p = __c->end_; __p != __c->beg_; )
  1502. {
  1503. --__p;
  1504. iterator* __i = static_cast<iterator*>((*__p)->__i_);
  1505. if (__i->__ptr_ == __n)
  1506. {
  1507. (*__p)->__c_ = nullptr;
  1508. if (--__c->end_ != __p)
  1509. _VSTD::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
  1510. }
  1511. }
  1512. __get_db()->unlock();
  1513. #endif
  1514. __node_pointer __np = __n->__as_node();
  1515. __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
  1516. __node_alloc_traits::deallocate(__na, __np, 1);
  1517. }
  1518. template <class _Tp, class _Alloc>
  1519. typename list<_Tp, _Alloc>::iterator
  1520. list<_Tp, _Alloc>::erase(const_iterator __p)
  1521. {
  1522. _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__p)) == this,
  1523. "list::erase(iterator) called with an iterator not referring to this list");
  1524. _LIBCPP_ASSERT(__p != end(),
  1525. "list::erase(iterator) called with a non-dereferenceable iterator");
  1526. __node_allocator& __na = base::__node_alloc();
  1527. __link_pointer __n = __p.__ptr_;
  1528. __link_pointer __r = __n->__next_;
  1529. base::__unlink_nodes(__n, __n);
  1530. --base::__sz();
  1531. #ifdef _LIBCPP_ENABLE_DEBUG_MODE
  1532. __c_node* __c = __get_db()->__find_c_and_lock(this);
  1533. for (__i_node** __ip = __c->end_; __ip != __c->beg_; )
  1534. {
  1535. --__ip;
  1536. iterator* __i = static_cast<iterator*>((*__ip)->__i_);
  1537. if (__i->__ptr_ == __n)
  1538. {
  1539. (*__ip)->__c_ = nullptr;
  1540. if (--__c->end_ != __ip)
  1541. _VSTD::memmove(__ip, __ip+1, (__c->end_ - __ip)*sizeof(__i_node*));
  1542. }
  1543. }
  1544. __get_db()->unlock();
  1545. #endif
  1546. __node_pointer __np = __n->__as_node();
  1547. __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
  1548. __node_alloc_traits::deallocate(__na, __np, 1);
  1549. return iterator(__r, this);
  1550. }
  1551. template <class _Tp, class _Alloc>
  1552. typename list<_Tp, _Alloc>::iterator
  1553. list<_Tp, _Alloc>::erase(const_iterator __f, const_iterator __l)
  1554. {
  1555. _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__f)) == this,
  1556. "list::erase(iterator, iterator) called with an iterator not referring to this list");
  1557. _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__l)) == this,
  1558. "list::erase(iterator, iterator) called with an iterator not referring to this list");
  1559. if (__f != __l)
  1560. {
  1561. __node_allocator& __na = base::__node_alloc();
  1562. base::__unlink_nodes(__f.__ptr_, __l.__ptr_->__prev_);
  1563. while (__f != __l)
  1564. {
  1565. __link_pointer __n = __f.__ptr_;
  1566. ++__f;
  1567. --base::__sz();
  1568. #ifdef _LIBCPP_ENABLE_DEBUG_MODE
  1569. __c_node* __c = __get_db()->__find_c_and_lock(this);
  1570. for (__i_node** __p = __c->end_; __p != __c->beg_; )
  1571. {
  1572. --__p;
  1573. iterator* __i = static_cast<iterator*>((*__p)->__i_);
  1574. if (__i->__ptr_ == __n)
  1575. {
  1576. (*__p)->__c_ = nullptr;
  1577. if (--__c->end_ != __p)
  1578. _VSTD::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
  1579. }
  1580. }
  1581. __get_db()->unlock();
  1582. #endif
  1583. __node_pointer __np = __n->__as_node();
  1584. __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
  1585. __node_alloc_traits::deallocate(__na, __np, 1);
  1586. }
  1587. }
  1588. return iterator(__l.__ptr_, this);
  1589. }
  1590. template <class _Tp, class _Alloc>
  1591. void
  1592. list<_Tp, _Alloc>::resize(size_type __n)
  1593. {
  1594. if (__n < base::__sz())
  1595. erase(__iterator(__n), end());
  1596. else if (__n > base::__sz())
  1597. {
  1598. __n -= base::__sz();
  1599. size_type __ds = 0;
  1600. __node_allocator& __na = base::__node_alloc();
  1601. __hold_pointer __hold = __allocate_node(__na);
  1602. __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_));
  1603. ++__ds;
  1604. iterator __r = iterator(__hold.release()->__as_link(), this);
  1605. iterator __e = __r;
  1606. #ifndef _LIBCPP_NO_EXCEPTIONS
  1607. try
  1608. {
  1609. #endif // _LIBCPP_NO_EXCEPTIONS
  1610. for (--__n; __n != 0; --__n, (void) ++__e, ++__ds)
  1611. {
  1612. __hold.reset(__node_alloc_traits::allocate(__na, 1));
  1613. __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_));
  1614. __e.__ptr_->__next_ = __hold.get()->__as_link();
  1615. __hold->__prev_ = __e.__ptr_;
  1616. __hold.release();
  1617. }
  1618. #ifndef _LIBCPP_NO_EXCEPTIONS
  1619. }
  1620. catch (...)
  1621. {
  1622. while (true)
  1623. {
  1624. __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
  1625. __link_pointer __prev = __e.__ptr_->__prev_;
  1626. __node_alloc_traits::deallocate(__na, __e.__ptr_->__as_node(), 1);
  1627. if (__prev == 0)
  1628. break;
  1629. __e = iterator(__prev, this);
  1630. }
  1631. throw;
  1632. }
  1633. #endif // _LIBCPP_NO_EXCEPTIONS
  1634. __link_nodes_at_back(__r.__ptr_, __e.__ptr_);
  1635. base::__sz() += __ds;
  1636. }
  1637. }
  1638. template <class _Tp, class _Alloc>
  1639. void
  1640. list<_Tp, _Alloc>::resize(size_type __n, const value_type& __x)
  1641. {
  1642. if (__n < base::__sz())
  1643. erase(__iterator(__n), end());
  1644. else if (__n > base::__sz())
  1645. {
  1646. __n -= base::__sz();
  1647. size_type __ds = 0;
  1648. __node_allocator& __na = base::__node_alloc();
  1649. __hold_pointer __hold = __allocate_node(__na);
  1650. __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
  1651. ++__ds;
  1652. __link_pointer __nl = __hold.release()->__as_link();
  1653. iterator __r = iterator(__nl, this);
  1654. iterator __e = __r;
  1655. #ifndef _LIBCPP_NO_EXCEPTIONS
  1656. try
  1657. {
  1658. #endif // _LIBCPP_NO_EXCEPTIONS
  1659. for (--__n; __n != 0; --__n, (void) ++__e, ++__ds)
  1660. {
  1661. __hold.reset(__node_alloc_traits::allocate(__na, 1));
  1662. __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
  1663. __e.__ptr_->__next_ = __hold.get()->__as_link();
  1664. __hold->__prev_ = __e.__ptr_;
  1665. __hold.release();
  1666. }
  1667. #ifndef _LIBCPP_NO_EXCEPTIONS
  1668. }
  1669. catch (...)
  1670. {
  1671. while (true)
  1672. {
  1673. __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
  1674. __link_pointer __prev = __e.__ptr_->__prev_;
  1675. __node_alloc_traits::deallocate(__na, __e.__ptr_->__as_node(), 1);
  1676. if (__prev == 0)
  1677. break;
  1678. __e = iterator(__prev, this);
  1679. }
  1680. throw;
  1681. }
  1682. #endif // _LIBCPP_NO_EXCEPTIONS
  1683. __link_nodes(base::__end_as_link(), __r.__ptr_, __e.__ptr_);
  1684. base::__sz() += __ds;
  1685. }
  1686. }
  1687. template <class _Tp, class _Alloc>
  1688. void
  1689. list<_Tp, _Alloc>::splice(const_iterator __p, list& __c)
  1690. {
  1691. _LIBCPP_ASSERT(this != _VSTD::addressof(__c),
  1692. "list::splice(iterator, list) called with this == &list");
  1693. _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__p)) == this,
  1694. "list::splice(iterator, list) called with an iterator not referring to this list");
  1695. if (!__c.empty())
  1696. {
  1697. __link_pointer __f = __c.__end_.__next_;
  1698. __link_pointer __l = __c.__end_.__prev_;
  1699. base::__unlink_nodes(__f, __l);
  1700. __link_nodes(__p.__ptr_, __f, __l);
  1701. base::__sz() += __c.__sz();
  1702. __c.__sz() = 0;
  1703. #ifdef _LIBCPP_ENABLE_DEBUG_MODE
  1704. if (_VSTD::addressof(__c) != this) {
  1705. __libcpp_db* __db = __get_db();
  1706. __c_node* __cn1 = __db->__find_c_and_lock(this);
  1707. __c_node* __cn2 = __db->__find_c(_VSTD::addressof(__c));
  1708. for (__i_node** __ip = __cn2->end_; __ip != __cn2->beg_;)
  1709. {
  1710. --__ip;
  1711. iterator* __i = static_cast<iterator*>((*__ip)->__i_);
  1712. if (__i->__ptr_ != __c.__end_as_link())
  1713. {
  1714. __cn1->__add(*__ip);
  1715. (*__ip)->__c_ = __cn1;
  1716. if (--__cn2->end_ != __ip)
  1717. _VSTD::memmove(__ip, __ip+1, (__cn2->end_ - __ip)*sizeof(__i_node*));
  1718. }
  1719. }
  1720. __db->unlock();
  1721. }
  1722. #endif
  1723. }
  1724. }
  1725. template <class _Tp, class _Alloc>
  1726. void
  1727. list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __i)
  1728. {
  1729. _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__p)) == this,
  1730. "list::splice(iterator, list, iterator) called with the first iterator not referring to this list");
  1731. _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__i)) == _VSTD::addressof(__c),
  1732. "list::splice(iterator, list, iterator) called with the second iterator not referring to the list argument");
  1733. _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(_VSTD::addressof(__i)),
  1734. "list::splice(iterator, list, iterator) called with the second iterator not dereferenceable");
  1735. if (__p.__ptr_ != __i.__ptr_ && __p.__ptr_ != __i.__ptr_->__next_)
  1736. {
  1737. __link_pointer __f = __i.__ptr_;
  1738. base::__unlink_nodes(__f, __f);
  1739. __link_nodes(__p.__ptr_, __f, __f);
  1740. --__c.__sz();
  1741. ++base::__sz();
  1742. #ifdef _LIBCPP_ENABLE_DEBUG_MODE
  1743. if (_VSTD::addressof(__c) != this) {
  1744. __libcpp_db* __db = __get_db();
  1745. __c_node* __cn1 = __db->__find_c_and_lock(this);
  1746. __c_node* __cn2 = __db->__find_c(_VSTD::addressof(__c));
  1747. for (__i_node** __ip = __cn2->end_; __ip != __cn2->beg_;)
  1748. {
  1749. --__ip;
  1750. iterator* __j = static_cast<iterator*>((*__ip)->__i_);
  1751. if (__j->__ptr_ == __f)
  1752. {
  1753. __cn1->__add(*__ip);
  1754. (*__ip)->__c_ = __cn1;
  1755. if (--__cn2->end_ != __ip)
  1756. _VSTD::memmove(__ip, __ip+1, (__cn2->end_ - __ip)*sizeof(__i_node*));
  1757. }
  1758. }
  1759. __db->unlock();
  1760. }
  1761. #endif
  1762. }
  1763. }
  1764. template <class _Iterator>
  1765. _LIBCPP_HIDE_FROM_ABI
  1766. bool __iterator_in_range(_Iterator __first, _Iterator __last, _Iterator __it) {
  1767. for (_Iterator __p = __first; __p != __last; ++__p) {
  1768. if (__p == __it) {
  1769. return true;
  1770. }
  1771. }
  1772. return false;
  1773. }
  1774. template <class _Tp, class _Alloc>
  1775. void
  1776. list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l)
  1777. {
  1778. _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__p)) == this,
  1779. "list::splice(iterator, list, iterator, iterator) called with first iterator not referring to this list");
  1780. _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__f)) == _VSTD::addressof(__c),
  1781. "list::splice(iterator, list, iterator, iterator) called with second iterator not referring to the list argument");
  1782. _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__l)) == _VSTD::addressof(__c),
  1783. "list::splice(iterator, list, iterator, iterator) called with third iterator not referring to the list argument");
  1784. _LIBCPP_DEBUG_ASSERT(this != std::addressof(__c) || !std::__iterator_in_range(__f, __l, __p),
  1785. "list::splice(iterator, list, iterator, iterator)"
  1786. " called with the first iterator within the range of the second and third iterators");
  1787. if (__f != __l)
  1788. {
  1789. __link_pointer __first = __f.__ptr_;
  1790. --__l;
  1791. __link_pointer __last = __l.__ptr_;
  1792. if (this != _VSTD::addressof(__c))
  1793. {
  1794. size_type __s = _VSTD::distance(__f, __l) + 1;
  1795. __c.__sz() -= __s;
  1796. base::__sz() += __s;
  1797. }
  1798. base::__unlink_nodes(__first, __last);
  1799. __link_nodes(__p.__ptr_, __first, __last);
  1800. #ifdef _LIBCPP_ENABLE_DEBUG_MODE
  1801. if (_VSTD::addressof(__c) != this) {
  1802. __libcpp_db* __db = __get_db();
  1803. __c_node* __cn1 = __db->__find_c_and_lock(this);
  1804. __c_node* __cn2 = __db->__find_c(_VSTD::addressof(__c));
  1805. for (__i_node** __ip = __cn2->end_; __ip != __cn2->beg_;)
  1806. {
  1807. --__ip;
  1808. iterator* __j = static_cast<iterator*>((*__ip)->__i_);
  1809. for (__link_pointer __k = __f.__ptr_;
  1810. __k != __l.__ptr_; __k = __k->__next_)
  1811. {
  1812. if (__j->__ptr_ == __k)
  1813. {
  1814. __cn1->__add(*__ip);
  1815. (*__ip)->__c_ = __cn1;
  1816. if (--__cn2->end_ != __ip)
  1817. _VSTD::memmove(__ip, __ip+1, (__cn2->end_ - __ip)*sizeof(__i_node*));
  1818. }
  1819. }
  1820. }
  1821. __db->unlock();
  1822. }
  1823. #endif
  1824. }
  1825. }
  1826. template <class _Tp, class _Alloc>
  1827. typename list<_Tp, _Alloc>::__remove_return_type
  1828. list<_Tp, _Alloc>::remove(const value_type& __x)
  1829. {
  1830. list<_Tp, _Alloc> __deleted_nodes(get_allocator()); // collect the nodes we're removing
  1831. for (const_iterator __i = begin(), __e = end(); __i != __e;)
  1832. {
  1833. if (*__i == __x)
  1834. {
  1835. const_iterator __j = _VSTD::next(__i);
  1836. for (; __j != __e && *__j == __x; ++__j)
  1837. ;
  1838. __deleted_nodes.splice(__deleted_nodes.end(), *this, __i, __j);
  1839. __i = __j;
  1840. if (__i != __e)
  1841. ++__i;
  1842. }
  1843. else
  1844. ++__i;
  1845. }
  1846. return (__remove_return_type) __deleted_nodes.size();
  1847. }
  1848. template <class _Tp, class _Alloc>
  1849. template <class _Pred>
  1850. typename list<_Tp, _Alloc>::__remove_return_type
  1851. list<_Tp, _Alloc>::remove_if(_Pred __pred)
  1852. {
  1853. list<_Tp, _Alloc> __deleted_nodes(get_allocator()); // collect the nodes we're removing
  1854. for (iterator __i = begin(), __e = end(); __i != __e;)
  1855. {
  1856. if (__pred(*__i))
  1857. {
  1858. iterator __j = _VSTD::next(__i);
  1859. for (; __j != __e && __pred(*__j); ++__j)
  1860. ;
  1861. __deleted_nodes.splice(__deleted_nodes.end(), *this, __i, __j);
  1862. __i = __j;
  1863. if (__i != __e)
  1864. ++__i;
  1865. }
  1866. else
  1867. ++__i;
  1868. }
  1869. return (__remove_return_type) __deleted_nodes.size();
  1870. }
  1871. template <class _Tp, class _Alloc>
  1872. template <class _BinaryPred>
  1873. typename list<_Tp, _Alloc>::__remove_return_type
  1874. list<_Tp, _Alloc>::unique(_BinaryPred __binary_pred)
  1875. {
  1876. list<_Tp, _Alloc> __deleted_nodes(get_allocator()); // collect the nodes we're removing
  1877. for (iterator __i = begin(), __e = end(); __i != __e;)
  1878. {
  1879. iterator __j = _VSTD::next(__i);
  1880. for (; __j != __e && __binary_pred(*__i, *__j); ++__j)
  1881. ;
  1882. if (++__i != __j) {
  1883. __deleted_nodes.splice(__deleted_nodes.end(), *this, __i, __j);
  1884. __i = __j;
  1885. }
  1886. }
  1887. return (__remove_return_type) __deleted_nodes.size();
  1888. }
  1889. template <class _Tp, class _Alloc>
  1890. inline
  1891. void
  1892. list<_Tp, _Alloc>::merge(list& __c)
  1893. {
  1894. merge(__c, __less<value_type>());
  1895. }
  1896. template <class _Tp, class _Alloc>
  1897. template <class _Comp>
  1898. void
  1899. list<_Tp, _Alloc>::merge(list& __c, _Comp __comp)
  1900. {
  1901. if (this != _VSTD::addressof(__c))
  1902. {
  1903. iterator __f1 = begin();
  1904. iterator __e1 = end();
  1905. iterator __f2 = __c.begin();
  1906. iterator __e2 = __c.end();
  1907. while (__f1 != __e1 && __f2 != __e2)
  1908. {
  1909. if (__comp(*__f2, *__f1))
  1910. {
  1911. size_type __ds = 1;
  1912. iterator __m2 = _VSTD::next(__f2);
  1913. for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2, (void) ++__ds)
  1914. ;
  1915. base::__sz() += __ds;
  1916. __c.__sz() -= __ds;
  1917. __link_pointer __f = __f2.__ptr_;
  1918. __link_pointer __l = __m2.__ptr_->__prev_;
  1919. __f2 = __m2;
  1920. base::__unlink_nodes(__f, __l);
  1921. __m2 = _VSTD::next(__f1);
  1922. __link_nodes(__f1.__ptr_, __f, __l);
  1923. __f1 = __m2;
  1924. }
  1925. else
  1926. ++__f1;
  1927. }
  1928. splice(__e1, __c);
  1929. #ifdef _LIBCPP_ENABLE_DEBUG_MODE
  1930. __libcpp_db* __db = __get_db();
  1931. __c_node* __cn1 = __db->__find_c_and_lock(this);
  1932. __c_node* __cn2 = __db->__find_c(_VSTD::addressof(__c));
  1933. for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
  1934. {
  1935. --__p;
  1936. iterator* __i = static_cast<iterator*>((*__p)->__i_);
  1937. if (__i->__ptr_ != __c.__end_as_link())
  1938. {
  1939. __cn1->__add(*__p);
  1940. (*__p)->__c_ = __cn1;
  1941. if (--__cn2->end_ != __p)
  1942. _VSTD::memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
  1943. }
  1944. }
  1945. __db->unlock();
  1946. #endif
  1947. }
  1948. }
  1949. template <class _Tp, class _Alloc>
  1950. inline
  1951. void
  1952. list<_Tp, _Alloc>::sort()
  1953. {
  1954. sort(__less<value_type>());
  1955. }
  1956. template <class _Tp, class _Alloc>
  1957. template <class _Comp>
  1958. inline
  1959. void
  1960. list<_Tp, _Alloc>::sort(_Comp __comp)
  1961. {
  1962. __sort(begin(), end(), base::__sz(), __comp);
  1963. }
  1964. template <class _Tp, class _Alloc>
  1965. template <class _Comp>
  1966. typename list<_Tp, _Alloc>::iterator
  1967. list<_Tp, _Alloc>::__sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp)
  1968. {
  1969. switch (__n)
  1970. {
  1971. case 0:
  1972. case 1:
  1973. return __f1;
  1974. case 2:
  1975. if (__comp(*--__e2, *__f1))
  1976. {
  1977. __link_pointer __f = __e2.__ptr_;
  1978. base::__unlink_nodes(__f, __f);
  1979. __link_nodes(__f1.__ptr_, __f, __f);
  1980. return __e2;
  1981. }
  1982. return __f1;
  1983. }
  1984. size_type __n2 = __n / 2;
  1985. iterator __e1 = _VSTD::next(__f1, __n2);
  1986. iterator __r = __f1 = __sort(__f1, __e1, __n2, __comp);
  1987. iterator __f2 = __e1 = __sort(__e1, __e2, __n - __n2, __comp);
  1988. if (__comp(*__f2, *__f1))
  1989. {
  1990. iterator __m2 = _VSTD::next(__f2);
  1991. for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2)
  1992. ;
  1993. __link_pointer __f = __f2.__ptr_;
  1994. __link_pointer __l = __m2.__ptr_->__prev_;
  1995. __r = __f2;
  1996. __e1 = __f2 = __m2;
  1997. base::__unlink_nodes(__f, __l);
  1998. __m2 = _VSTD::next(__f1);
  1999. __link_nodes(__f1.__ptr_, __f, __l);
  2000. __f1 = __m2;
  2001. }
  2002. else
  2003. ++__f1;
  2004. while (__f1 != __e1 && __f2 != __e2)
  2005. {
  2006. if (__comp(*__f2, *__f1))
  2007. {
  2008. iterator __m2 = _VSTD::next(__f2);
  2009. for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2)
  2010. ;
  2011. __link_pointer __f = __f2.__ptr_;
  2012. __link_pointer __l = __m2.__ptr_->__prev_;
  2013. if (__e1 == __f2)
  2014. __e1 = __m2;
  2015. __f2 = __m2;
  2016. base::__unlink_nodes(__f, __l);
  2017. __m2 = _VSTD::next(__f1);
  2018. __link_nodes(__f1.__ptr_, __f, __l);
  2019. __f1 = __m2;
  2020. }
  2021. else
  2022. ++__f1;
  2023. }
  2024. return __r;
  2025. }
  2026. template <class _Tp, class _Alloc>
  2027. void
  2028. list<_Tp, _Alloc>::reverse() _NOEXCEPT
  2029. {
  2030. if (base::__sz() > 1)
  2031. {
  2032. iterator __e = end();
  2033. for (iterator __i = begin(); __i.__ptr_ != __e.__ptr_;)
  2034. {
  2035. _VSTD::swap(__i.__ptr_->__prev_, __i.__ptr_->__next_);
  2036. __i.__ptr_ = __i.__ptr_->__prev_;
  2037. }
  2038. _VSTD::swap(__e.__ptr_->__prev_, __e.__ptr_->__next_);
  2039. }
  2040. }
  2041. template <class _Tp, class _Alloc>
  2042. bool
  2043. list<_Tp, _Alloc>::__invariants() const
  2044. {
  2045. return size() == _VSTD::distance(begin(), end());
  2046. }
  2047. #ifdef _LIBCPP_ENABLE_DEBUG_MODE
  2048. template <class _Tp, class _Alloc>
  2049. bool
  2050. list<_Tp, _Alloc>::__dereferenceable(const const_iterator* __i) const
  2051. {
  2052. return __i->__ptr_ != this->__end_as_link();
  2053. }
  2054. template <class _Tp, class _Alloc>
  2055. bool
  2056. list<_Tp, _Alloc>::__decrementable(const const_iterator* __i) const
  2057. {
  2058. return !empty() && __i->__ptr_ != base::__end_.__next_;
  2059. }
  2060. template <class _Tp, class _Alloc>
  2061. bool
  2062. list<_Tp, _Alloc>::__addable(const const_iterator*, ptrdiff_t) const
  2063. {
  2064. return false;
  2065. }
  2066. template <class _Tp, class _Alloc>
  2067. bool
  2068. list<_Tp, _Alloc>::__subscriptable(const const_iterator*, ptrdiff_t) const
  2069. {
  2070. return false;
  2071. }
  2072. #endif // _LIBCPP_ENABLE_DEBUG_MODE
  2073. template <class _Tp, class _Alloc>
  2074. inline _LIBCPP_INLINE_VISIBILITY
  2075. bool
  2076. operator==(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
  2077. {
  2078. return __x.size() == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
  2079. }
  2080. template <class _Tp, class _Alloc>
  2081. inline _LIBCPP_INLINE_VISIBILITY
  2082. bool
  2083. operator< (const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
  2084. {
  2085. return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
  2086. }
  2087. template <class _Tp, class _Alloc>
  2088. inline _LIBCPP_INLINE_VISIBILITY
  2089. bool
  2090. operator!=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
  2091. {
  2092. return !(__x == __y);
  2093. }
  2094. template <class _Tp, class _Alloc>
  2095. inline _LIBCPP_INLINE_VISIBILITY
  2096. bool
  2097. operator> (const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
  2098. {
  2099. return __y < __x;
  2100. }
  2101. template <class _Tp, class _Alloc>
  2102. inline _LIBCPP_INLINE_VISIBILITY
  2103. bool
  2104. operator>=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
  2105. {
  2106. return !(__x < __y);
  2107. }
  2108. template <class _Tp, class _Alloc>
  2109. inline _LIBCPP_INLINE_VISIBILITY
  2110. bool
  2111. operator<=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
  2112. {
  2113. return !(__y < __x);
  2114. }
  2115. template <class _Tp, class _Alloc>
  2116. inline _LIBCPP_INLINE_VISIBILITY
  2117. void
  2118. swap(list<_Tp, _Alloc>& __x, list<_Tp, _Alloc>& __y)
  2119. _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
  2120. {
  2121. __x.swap(__y);
  2122. }
  2123. #if _LIBCPP_STD_VER > 17
  2124. template <class _Tp, class _Allocator, class _Predicate>
  2125. inline _LIBCPP_INLINE_VISIBILITY typename list<_Tp, _Allocator>::size_type
  2126. erase_if(list<_Tp, _Allocator>& __c, _Predicate __pred) {
  2127. return __c.remove_if(__pred);
  2128. }
  2129. template <class _Tp, class _Allocator, class _Up>
  2130. inline _LIBCPP_INLINE_VISIBILITY typename list<_Tp, _Allocator>::size_type
  2131. erase(list<_Tp, _Allocator>& __c, const _Up& __v) {
  2132. return _VSTD::erase_if(__c, [&](auto& __elem) { return __elem == __v; });
  2133. }
  2134. template <>
  2135. inline constexpr bool __format::__enable_insertable<std::list<char>> = true;
  2136. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  2137. template <>
  2138. inline constexpr bool __format::__enable_insertable<std::list<wchar_t>> = true;
  2139. #endif
  2140. #endif // _LIBCPP_STD_VER > 17
  2141. _LIBCPP_END_NAMESPACE_STD
  2142. #if _LIBCPP_STD_VER > 14
  2143. _LIBCPP_BEGIN_NAMESPACE_STD
  2144. namespace pmr {
  2145. template <class _ValueT>
  2146. using list = std::list<_ValueT, polymorphic_allocator<_ValueT>>;
  2147. } // namespace pmr
  2148. _LIBCPP_END_NAMESPACE_STD
  2149. #endif
  2150. _LIBCPP_POP_MACROS
  2151. #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
  2152. # include <algorithm>
  2153. # include <atomic>
  2154. # include <concepts>
  2155. # include <functional>
  2156. # include <iosfwd>
  2157. # include <iterator>
  2158. # include <typeinfo>
  2159. #endif
  2160. #endif // _LIBCPP_LIST