list 79 KB

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