list 76 KB

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