list 78 KB

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