list 79 KB

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