forward_list 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803
  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_FORWARD_LIST
  10. #define _LIBCPP_FORWARD_LIST
  11. /*
  12. forward_list synopsis
  13. namespace std
  14. {
  15. template <class T, class Allocator = allocator<T>>
  16. class forward_list
  17. {
  18. public:
  19. typedef T value_type;
  20. typedef Allocator allocator_type;
  21. typedef value_type& reference;
  22. typedef const value_type& const_reference;
  23. typedef typename allocator_traits<allocator_type>::pointer pointer;
  24. typedef typename allocator_traits<allocator_type>::const_pointer const_pointer;
  25. typedef typename allocator_traits<allocator_type>::size_type size_type;
  26. typedef typename allocator_traits<allocator_type>::difference_type difference_type;
  27. typedef <details> iterator;
  28. typedef <details> const_iterator;
  29. forward_list()
  30. noexcept(is_nothrow_default_constructible<allocator_type>::value);
  31. explicit forward_list(const allocator_type& a);
  32. explicit forward_list(size_type n);
  33. explicit forward_list(size_type n, const allocator_type& a); // C++14
  34. forward_list(size_type n, const value_type& v);
  35. forward_list(size_type n, const value_type& v, const allocator_type& a);
  36. template <class InputIterator>
  37. forward_list(InputIterator first, InputIterator last);
  38. template <class InputIterator>
  39. forward_list(InputIterator first, InputIterator last, const allocator_type& a);
  40. forward_list(const forward_list& x);
  41. forward_list(const forward_list& x, const allocator_type& a);
  42. forward_list(forward_list&& x)
  43. noexcept(is_nothrow_move_constructible<allocator_type>::value);
  44. forward_list(forward_list&& x, const allocator_type& a);
  45. forward_list(initializer_list<value_type> il);
  46. forward_list(initializer_list<value_type> il, const allocator_type& a);
  47. ~forward_list();
  48. forward_list& operator=(const forward_list& x);
  49. forward_list& operator=(forward_list&& x)
  50. noexcept(
  51. allocator_type::propagate_on_container_move_assignment::value &&
  52. is_nothrow_move_assignable<allocator_type>::value);
  53. forward_list& operator=(initializer_list<value_type> il);
  54. template <class InputIterator>
  55. void assign(InputIterator first, InputIterator last);
  56. void assign(size_type n, const value_type& v);
  57. void assign(initializer_list<value_type> il);
  58. allocator_type get_allocator() const noexcept;
  59. iterator begin() noexcept;
  60. const_iterator begin() const noexcept;
  61. iterator end() noexcept;
  62. const_iterator end() const noexcept;
  63. const_iterator cbegin() const noexcept;
  64. const_iterator cend() const noexcept;
  65. iterator before_begin() noexcept;
  66. const_iterator before_begin() const noexcept;
  67. const_iterator cbefore_begin() const noexcept;
  68. bool empty() const noexcept;
  69. size_type max_size() const noexcept;
  70. reference front();
  71. const_reference front() const;
  72. template <class... Args> reference emplace_front(Args&&... args); // reference in C++17
  73. void push_front(const value_type& v);
  74. void push_front(value_type&& v);
  75. void pop_front();
  76. template <class... Args>
  77. iterator emplace_after(const_iterator p, Args&&... args);
  78. iterator insert_after(const_iterator p, const value_type& v);
  79. iterator insert_after(const_iterator p, value_type&& v);
  80. iterator insert_after(const_iterator p, size_type n, const value_type& v);
  81. template <class InputIterator>
  82. iterator insert_after(const_iterator p,
  83. InputIterator first, InputIterator last);
  84. iterator insert_after(const_iterator p, initializer_list<value_type> il);
  85. iterator erase_after(const_iterator p);
  86. iterator erase_after(const_iterator first, const_iterator last);
  87. void swap(forward_list& x)
  88. noexcept(allocator_traits<allocator_type>::is_always_equal::value); // C++17
  89. void resize(size_type n);
  90. void resize(size_type n, const value_type& v);
  91. void clear() noexcept;
  92. void splice_after(const_iterator p, forward_list& x);
  93. void splice_after(const_iterator p, forward_list&& x);
  94. void splice_after(const_iterator p, forward_list& x, const_iterator i);
  95. void splice_after(const_iterator p, forward_list&& x, const_iterator i);
  96. void splice_after(const_iterator p, forward_list& x,
  97. const_iterator first, const_iterator last);
  98. void splice_after(const_iterator p, forward_list&& x,
  99. const_iterator first, const_iterator last);
  100. size_type remove(const value_type& v); // void before C++20
  101. template <class Predicate>
  102. size_type remove_if(Predicate pred); // void before C++20
  103. size_type unique(); // void before C++20
  104. template <class BinaryPredicate>
  105. size_type unique(BinaryPredicate binary_pred); // void before C++20
  106. void merge(forward_list& x);
  107. void merge(forward_list&& x);
  108. template <class Compare> void merge(forward_list& x, Compare comp);
  109. template <class Compare> void merge(forward_list&& x, Compare comp);
  110. void sort();
  111. template <class Compare> void sort(Compare comp);
  112. void reverse() noexcept;
  113. };
  114. template <class InputIterator, class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
  115. forward_list(InputIterator, InputIterator, Allocator = Allocator())
  116. -> forward_list<typename iterator_traits<InputIterator>::value_type, Allocator>; // C++17
  117. template <class T, class Allocator>
  118. bool operator==(const forward_list<T, Allocator>& x,
  119. const forward_list<T, Allocator>& y);
  120. template <class T, class Allocator>
  121. bool operator< (const forward_list<T, Allocator>& x,
  122. const forward_list<T, Allocator>& y);
  123. template <class T, class Allocator>
  124. bool operator!=(const forward_list<T, Allocator>& x,
  125. const forward_list<T, Allocator>& y);
  126. template <class T, class Allocator>
  127. bool operator> (const forward_list<T, Allocator>& x,
  128. const forward_list<T, Allocator>& y);
  129. template <class T, class Allocator>
  130. bool operator>=(const forward_list<T, Allocator>& x,
  131. const forward_list<T, Allocator>& y);
  132. template <class T, class Allocator>
  133. bool operator<=(const forward_list<T, Allocator>& x,
  134. const forward_list<T, Allocator>& y);
  135. template <class T, class Allocator>
  136. void swap(forward_list<T, Allocator>& x, forward_list<T, Allocator>& y)
  137. noexcept(noexcept(x.swap(y)));
  138. template <class T, class Allocator, class U>
  139. typename forward_list<T, Allocator>::size_type
  140. erase(forward_list<T, Allocator>& c, const U& value); // C++20
  141. template <class T, class Allocator, class Predicate>
  142. typename forward_list<T, Allocator>::size_type
  143. erase_if(forward_list<T, Allocator>& c, Predicate pred); // C++20
  144. } // std
  145. */
  146. #include <__algorithm/comp.h>
  147. #include <__algorithm/lexicographical_compare.h>
  148. #include <__algorithm/min.h>
  149. #include <__assert> // all public C++ headers provide the assertion handler
  150. #include <__config>
  151. #include <__iterator/distance.h>
  152. #include <__iterator/iterator_traits.h>
  153. #include <__iterator/move_iterator.h>
  154. #include <__iterator/next.h>
  155. #include <__memory/addressof.h>
  156. #include <__memory/allocator.h>
  157. #include <__memory/allocator_destructor.h>
  158. #include <__memory/allocator_traits.h>
  159. #include <__memory/compressed_pair.h>
  160. #include <__memory/pointer_traits.h>
  161. #include <__memory/swap_allocator.h>
  162. #include <__memory/unique_ptr.h>
  163. #include <__memory_resource/polymorphic_allocator.h>
  164. #include <__type_traits/is_allocator.h>
  165. #include <__utility/forward.h>
  166. #include <__utility/move.h>
  167. #include <limits>
  168. #include <type_traits>
  169. #include <version>
  170. // standard-mandated includes
  171. // [iterator.range]
  172. #include <__iterator/access.h>
  173. #include <__iterator/data.h>
  174. #include <__iterator/empty.h>
  175. #include <__iterator/reverse_access.h>
  176. #include <__iterator/size.h>
  177. // [forward.list.syn]
  178. #include <compare>
  179. #include <initializer_list>
  180. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  181. # pragma GCC system_header
  182. #endif
  183. _LIBCPP_PUSH_MACROS
  184. #include <__undef_macros>
  185. _LIBCPP_BEGIN_NAMESPACE_STD
  186. template <class _Tp, class _VoidPtr> struct __forward_list_node;
  187. template <class _NodePtr> struct __forward_begin_node;
  188. template <class>
  189. struct __forward_list_node_value_type;
  190. template <class _Tp, class _VoidPtr>
  191. struct __forward_list_node_value_type<__forward_list_node<_Tp, _VoidPtr> > {
  192. typedef _Tp type;
  193. };
  194. template <class _NodePtr>
  195. struct __forward_node_traits {
  196. typedef __remove_cv_t<
  197. typename pointer_traits<_NodePtr>::element_type> __node;
  198. typedef typename __forward_list_node_value_type<__node>::type __node_value_type;
  199. typedef _NodePtr __node_pointer;
  200. typedef __forward_begin_node<_NodePtr> __begin_node;
  201. typedef __rebind_pointer_t<_NodePtr, __begin_node> __begin_node_pointer;
  202. typedef __rebind_pointer_t<_NodePtr, void> __void_pointer;
  203. #if defined(_LIBCPP_ABI_FORWARD_LIST_REMOVE_NODE_POINTER_UB)
  204. typedef __begin_node_pointer __iter_node_pointer;
  205. #else
  206. typedef __conditional_t<is_pointer<__void_pointer>::value, __begin_node_pointer, __node_pointer>
  207. __iter_node_pointer;
  208. #endif
  209. typedef __conditional_t<is_same<__iter_node_pointer, __node_pointer>::value, __begin_node_pointer, __node_pointer>
  210. __non_iter_node_pointer;
  211. _LIBCPP_INLINE_VISIBILITY
  212. static __iter_node_pointer __as_iter_node(__iter_node_pointer __p) {
  213. return __p;
  214. }
  215. _LIBCPP_INLINE_VISIBILITY
  216. static __iter_node_pointer __as_iter_node(__non_iter_node_pointer __p) {
  217. return static_cast<__iter_node_pointer>(static_cast<__void_pointer>(__p));
  218. }
  219. };
  220. template <class _NodePtr>
  221. struct __forward_begin_node
  222. {
  223. typedef _NodePtr pointer;
  224. typedef __rebind_pointer_t<_NodePtr, __forward_begin_node> __begin_node_pointer;
  225. pointer __next_;
  226. _LIBCPP_INLINE_VISIBILITY __forward_begin_node() : __next_(nullptr) {}
  227. _LIBCPP_INLINE_VISIBILITY
  228. __begin_node_pointer __next_as_begin() const {
  229. return static_cast<__begin_node_pointer>(__next_);
  230. }
  231. };
  232. template <class _Tp, class _VoidPtr>
  233. using __begin_node_of = __forward_begin_node<__rebind_pointer_t<_VoidPtr, __forward_list_node<_Tp, _VoidPtr> > >;
  234. template <class _Tp, class _VoidPtr>
  235. struct _LIBCPP_STANDALONE_DEBUG __forward_list_node
  236. : public __begin_node_of<_Tp, _VoidPtr>
  237. {
  238. typedef _Tp value_type;
  239. value_type __value_;
  240. };
  241. template <class _Tp, class _Alloc = allocator<_Tp> > class _LIBCPP_TEMPLATE_VIS forward_list;
  242. template<class _NodeConstPtr> class _LIBCPP_TEMPLATE_VIS __forward_list_const_iterator;
  243. template <class _NodePtr>
  244. class _LIBCPP_TEMPLATE_VIS __forward_list_iterator
  245. {
  246. typedef __forward_node_traits<_NodePtr> __traits;
  247. typedef typename __traits::__node_pointer __node_pointer;
  248. typedef typename __traits::__begin_node_pointer __begin_node_pointer;
  249. typedef typename __traits::__iter_node_pointer __iter_node_pointer;
  250. typedef typename __traits::__void_pointer __void_pointer;
  251. __iter_node_pointer __ptr_;
  252. _LIBCPP_INLINE_VISIBILITY
  253. __begin_node_pointer __get_begin() const {
  254. return static_cast<__begin_node_pointer>(
  255. static_cast<__void_pointer>(__ptr_));
  256. }
  257. _LIBCPP_INLINE_VISIBILITY
  258. __node_pointer __get_unsafe_node_pointer() const {
  259. return static_cast<__node_pointer>(
  260. static_cast<__void_pointer>(__ptr_));
  261. }
  262. _LIBCPP_INLINE_VISIBILITY
  263. explicit __forward_list_iterator(nullptr_t) _NOEXCEPT : __ptr_(nullptr) {}
  264. _LIBCPP_INLINE_VISIBILITY
  265. explicit __forward_list_iterator(__begin_node_pointer __p) _NOEXCEPT
  266. : __ptr_(__traits::__as_iter_node(__p)) {}
  267. _LIBCPP_INLINE_VISIBILITY
  268. explicit __forward_list_iterator(__node_pointer __p) _NOEXCEPT
  269. : __ptr_(__traits::__as_iter_node(__p)) {}
  270. template<class, class> friend class _LIBCPP_TEMPLATE_VIS forward_list;
  271. template<class> friend class _LIBCPP_TEMPLATE_VIS __forward_list_const_iterator;
  272. public:
  273. typedef forward_iterator_tag iterator_category;
  274. typedef typename __traits::__node_value_type value_type;
  275. typedef value_type& reference;
  276. typedef typename pointer_traits<__node_pointer>::difference_type
  277. difference_type;
  278. typedef __rebind_pointer_t<__node_pointer, value_type> pointer;
  279. _LIBCPP_INLINE_VISIBILITY
  280. __forward_list_iterator() _NOEXCEPT : __ptr_(nullptr) {}
  281. _LIBCPP_INLINE_VISIBILITY
  282. reference operator*() const {return __get_unsafe_node_pointer()->__value_;}
  283. _LIBCPP_INLINE_VISIBILITY
  284. pointer operator->() const {
  285. return pointer_traits<pointer>::pointer_to(__get_unsafe_node_pointer()->__value_);
  286. }
  287. _LIBCPP_INLINE_VISIBILITY
  288. __forward_list_iterator& operator++()
  289. {
  290. __ptr_ = __traits::__as_iter_node(__ptr_->__next_);
  291. return *this;
  292. }
  293. _LIBCPP_INLINE_VISIBILITY
  294. __forward_list_iterator operator++(int)
  295. {
  296. __forward_list_iterator __t(*this);
  297. ++(*this);
  298. return __t;
  299. }
  300. friend _LIBCPP_INLINE_VISIBILITY
  301. bool operator==(const __forward_list_iterator& __x,
  302. const __forward_list_iterator& __y)
  303. {return __x.__ptr_ == __y.__ptr_;}
  304. friend _LIBCPP_INLINE_VISIBILITY
  305. bool operator!=(const __forward_list_iterator& __x,
  306. const __forward_list_iterator& __y)
  307. {return !(__x == __y);}
  308. };
  309. template <class _NodeConstPtr>
  310. class _LIBCPP_TEMPLATE_VIS __forward_list_const_iterator
  311. {
  312. static_assert((!is_const<typename pointer_traits<_NodeConstPtr>::element_type>::value), "");
  313. typedef _NodeConstPtr _NodePtr;
  314. typedef __forward_node_traits<_NodePtr> __traits;
  315. typedef typename __traits::__node __node;
  316. typedef typename __traits::__node_pointer __node_pointer;
  317. typedef typename __traits::__begin_node_pointer __begin_node_pointer;
  318. typedef typename __traits::__iter_node_pointer __iter_node_pointer;
  319. typedef typename __traits::__void_pointer __void_pointer;
  320. __iter_node_pointer __ptr_;
  321. __begin_node_pointer __get_begin() const {
  322. return static_cast<__begin_node_pointer>(
  323. static_cast<__void_pointer>(__ptr_));
  324. }
  325. __node_pointer __get_unsafe_node_pointer() const {
  326. return static_cast<__node_pointer>(
  327. static_cast<__void_pointer>(__ptr_));
  328. }
  329. _LIBCPP_INLINE_VISIBILITY
  330. explicit __forward_list_const_iterator(nullptr_t) _NOEXCEPT
  331. : __ptr_(nullptr) {}
  332. _LIBCPP_INLINE_VISIBILITY
  333. explicit __forward_list_const_iterator(__begin_node_pointer __p) _NOEXCEPT
  334. : __ptr_(__traits::__as_iter_node(__p)) {}
  335. _LIBCPP_INLINE_VISIBILITY
  336. explicit __forward_list_const_iterator(__node_pointer __p) _NOEXCEPT
  337. : __ptr_(__traits::__as_iter_node(__p)) {}
  338. template<class, class> friend class forward_list;
  339. public:
  340. typedef forward_iterator_tag iterator_category;
  341. typedef typename __traits::__node_value_type value_type;
  342. typedef const value_type& reference;
  343. typedef typename pointer_traits<__node_pointer>::difference_type
  344. difference_type;
  345. typedef __rebind_pointer_t<__node_pointer, const value_type>
  346. pointer;
  347. _LIBCPP_INLINE_VISIBILITY
  348. __forward_list_const_iterator() _NOEXCEPT : __ptr_(nullptr) {}
  349. _LIBCPP_INLINE_VISIBILITY
  350. __forward_list_const_iterator(__forward_list_iterator<__node_pointer> __p) _NOEXCEPT
  351. : __ptr_(__p.__ptr_) {}
  352. _LIBCPP_INLINE_VISIBILITY
  353. reference operator*() const {return __get_unsafe_node_pointer()->__value_;}
  354. _LIBCPP_INLINE_VISIBILITY
  355. pointer operator->() const {return pointer_traits<pointer>::pointer_to(
  356. __get_unsafe_node_pointer()->__value_);}
  357. _LIBCPP_INLINE_VISIBILITY
  358. __forward_list_const_iterator& operator++()
  359. {
  360. __ptr_ = __traits::__as_iter_node(__ptr_->__next_);
  361. return *this;
  362. }
  363. _LIBCPP_INLINE_VISIBILITY
  364. __forward_list_const_iterator operator++(int)
  365. {
  366. __forward_list_const_iterator __t(*this);
  367. ++(*this);
  368. return __t;
  369. }
  370. friend _LIBCPP_INLINE_VISIBILITY
  371. bool operator==(const __forward_list_const_iterator& __x,
  372. const __forward_list_const_iterator& __y)
  373. {return __x.__ptr_ == __y.__ptr_;}
  374. friend _LIBCPP_INLINE_VISIBILITY
  375. bool operator!=(const __forward_list_const_iterator& __x,
  376. const __forward_list_const_iterator& __y)
  377. {return !(__x == __y);}
  378. };
  379. template <class _Tp, class _Alloc>
  380. class __forward_list_base
  381. {
  382. protected:
  383. typedef _Tp value_type;
  384. typedef _Alloc allocator_type;
  385. typedef typename allocator_traits<allocator_type>::void_pointer void_pointer;
  386. typedef __forward_list_node<value_type, void_pointer> __node;
  387. typedef __begin_node_of<value_type, void_pointer> __begin_node;
  388. typedef __rebind_alloc<allocator_traits<allocator_type>, __node> __node_allocator;
  389. typedef allocator_traits<__node_allocator> __node_traits;
  390. typedef typename __node_traits::pointer __node_pointer;
  391. typedef __rebind_alloc<allocator_traits<allocator_type>, __begin_node> __begin_node_allocator;
  392. typedef typename allocator_traits<__begin_node_allocator>::pointer
  393. __begin_node_pointer;
  394. static_assert((!is_same<allocator_type, __node_allocator>::value),
  395. "internal allocator type must differ from user-specified "
  396. "type; otherwise overload resolution breaks");
  397. static_assert(is_same<allocator_type, __rebind_alloc<__node_traits, value_type> >::value,
  398. "[allocator.requirements] states that rebinding an allocator to the same type should result in the "
  399. "original allocator");
  400. __compressed_pair<__begin_node, __node_allocator> __before_begin_;
  401. _LIBCPP_INLINE_VISIBILITY
  402. __begin_node_pointer __before_begin() _NOEXCEPT
  403. {return pointer_traits<__begin_node_pointer>::pointer_to(__before_begin_.first());}
  404. _LIBCPP_INLINE_VISIBILITY
  405. __begin_node_pointer __before_begin() const _NOEXCEPT
  406. {return pointer_traits<__begin_node_pointer>::pointer_to(const_cast<__begin_node&>(__before_begin_.first()));}
  407. _LIBCPP_INLINE_VISIBILITY
  408. __node_allocator& __alloc() _NOEXCEPT
  409. {return __before_begin_.second();}
  410. _LIBCPP_INLINE_VISIBILITY
  411. const __node_allocator& __alloc() const _NOEXCEPT
  412. {return __before_begin_.second();}
  413. typedef __forward_list_iterator<__node_pointer> iterator;
  414. typedef __forward_list_const_iterator<__node_pointer> const_iterator;
  415. _LIBCPP_INLINE_VISIBILITY
  416. __forward_list_base()
  417. _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value)
  418. : __before_begin_(__begin_node(), __default_init_tag()) {}
  419. _LIBCPP_INLINE_VISIBILITY
  420. explicit __forward_list_base(const allocator_type& __a)
  421. : __before_begin_(__begin_node(), __node_allocator(__a)) {}
  422. _LIBCPP_INLINE_VISIBILITY
  423. explicit __forward_list_base(const __node_allocator& __a)
  424. : __before_begin_(__begin_node(), __a) {}
  425. #ifndef _LIBCPP_CXX03_LANG
  426. public:
  427. _LIBCPP_INLINE_VISIBILITY
  428. __forward_list_base(__forward_list_base&& __x)
  429. _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value);
  430. _LIBCPP_INLINE_VISIBILITY
  431. __forward_list_base(__forward_list_base&& __x, const allocator_type& __a);
  432. #endif // _LIBCPP_CXX03_LANG
  433. private:
  434. __forward_list_base(const __forward_list_base&);
  435. __forward_list_base& operator=(const __forward_list_base&);
  436. public:
  437. ~__forward_list_base();
  438. protected:
  439. _LIBCPP_INLINE_VISIBILITY
  440. void __copy_assign_alloc(const __forward_list_base& __x)
  441. {__copy_assign_alloc(__x, integral_constant<bool,
  442. __node_traits::propagate_on_container_copy_assignment::value>());}
  443. _LIBCPP_INLINE_VISIBILITY
  444. void __move_assign_alloc(__forward_list_base& __x)
  445. _NOEXCEPT_(!__node_traits::propagate_on_container_move_assignment::value ||
  446. is_nothrow_move_assignable<__node_allocator>::value)
  447. {__move_assign_alloc(__x, integral_constant<bool,
  448. __node_traits::propagate_on_container_move_assignment::value>());}
  449. public:
  450. _LIBCPP_INLINE_VISIBILITY
  451. void swap(__forward_list_base& __x)
  452. #if _LIBCPP_STD_VER >= 14
  453. _NOEXCEPT;
  454. #else
  455. _NOEXCEPT_(!__node_traits::propagate_on_container_swap::value ||
  456. __is_nothrow_swappable<__node_allocator>::value);
  457. #endif
  458. protected:
  459. void clear() _NOEXCEPT;
  460. private:
  461. _LIBCPP_INLINE_VISIBILITY
  462. void __copy_assign_alloc(const __forward_list_base&, false_type) {}
  463. _LIBCPP_INLINE_VISIBILITY
  464. void __copy_assign_alloc(const __forward_list_base& __x, true_type)
  465. {
  466. if (__alloc() != __x.__alloc())
  467. clear();
  468. __alloc() = __x.__alloc();
  469. }
  470. _LIBCPP_INLINE_VISIBILITY
  471. void __move_assign_alloc(__forward_list_base&, false_type) _NOEXCEPT
  472. {}
  473. _LIBCPP_INLINE_VISIBILITY
  474. void __move_assign_alloc(__forward_list_base& __x, true_type)
  475. _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
  476. {__alloc() = _VSTD::move(__x.__alloc());}
  477. };
  478. #ifndef _LIBCPP_CXX03_LANG
  479. template <class _Tp, class _Alloc>
  480. inline
  481. __forward_list_base<_Tp, _Alloc>::__forward_list_base(__forward_list_base&& __x)
  482. _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value)
  483. : __before_begin_(_VSTD::move(__x.__before_begin_))
  484. {
  485. __x.__before_begin()->__next_ = nullptr;
  486. }
  487. template <class _Tp, class _Alloc>
  488. inline
  489. __forward_list_base<_Tp, _Alloc>::__forward_list_base(__forward_list_base&& __x,
  490. const allocator_type& __a)
  491. : __before_begin_(__begin_node(), __node_allocator(__a))
  492. {
  493. if (__alloc() == __x.__alloc())
  494. {
  495. __before_begin()->__next_ = __x.__before_begin()->__next_;
  496. __x.__before_begin()->__next_ = nullptr;
  497. }
  498. }
  499. #endif // _LIBCPP_CXX03_LANG
  500. template <class _Tp, class _Alloc>
  501. __forward_list_base<_Tp, _Alloc>::~__forward_list_base()
  502. {
  503. clear();
  504. }
  505. template <class _Tp, class _Alloc>
  506. inline
  507. void
  508. __forward_list_base<_Tp, _Alloc>::swap(__forward_list_base& __x)
  509. #if _LIBCPP_STD_VER >= 14
  510. _NOEXCEPT
  511. #else
  512. _NOEXCEPT_(!__node_traits::propagate_on_container_swap::value ||
  513. __is_nothrow_swappable<__node_allocator>::value)
  514. #endif
  515. {
  516. _VSTD::__swap_allocator(__alloc(), __x.__alloc(),
  517. integral_constant<bool, __node_traits::propagate_on_container_swap::value>());
  518. using _VSTD::swap;
  519. swap(__before_begin()->__next_, __x.__before_begin()->__next_);
  520. }
  521. template <class _Tp, class _Alloc>
  522. void
  523. __forward_list_base<_Tp, _Alloc>::clear() _NOEXCEPT
  524. {
  525. __node_allocator& __a = __alloc();
  526. for (__node_pointer __p = __before_begin()->__next_; __p != nullptr;)
  527. {
  528. __node_pointer __next = __p->__next_;
  529. __node_traits::destroy(__a, _VSTD::addressof(__p->__value_));
  530. __node_traits::deallocate(__a, __p, 1);
  531. __p = __next;
  532. }
  533. __before_begin()->__next_ = nullptr;
  534. }
  535. template <class _Tp, class _Alloc /*= allocator<_Tp>*/>
  536. class _LIBCPP_TEMPLATE_VIS forward_list
  537. : private __forward_list_base<_Tp, _Alloc>
  538. {
  539. typedef __forward_list_base<_Tp, _Alloc> base;
  540. typedef typename base::__node_allocator __node_allocator;
  541. typedef typename base::__node __node;
  542. typedef typename base::__node_traits __node_traits;
  543. typedef typename base::__node_pointer __node_pointer;
  544. typedef typename base::__begin_node_pointer __begin_node_pointer;
  545. public:
  546. typedef _Tp value_type;
  547. typedef _Alloc allocator_type;
  548. static_assert((is_same<typename allocator_type::value_type, value_type>::value),
  549. "Allocator::value_type must be same type as value_type");
  550. typedef value_type& reference;
  551. typedef const value_type& const_reference;
  552. typedef typename allocator_traits<allocator_type>::pointer pointer;
  553. typedef typename allocator_traits<allocator_type>::const_pointer const_pointer;
  554. typedef typename allocator_traits<allocator_type>::size_type size_type;
  555. typedef typename allocator_traits<allocator_type>::difference_type difference_type;
  556. typedef typename base::iterator iterator;
  557. typedef typename base::const_iterator const_iterator;
  558. #if _LIBCPP_STD_VER > 17
  559. typedef size_type __remove_return_type;
  560. #else
  561. typedef void __remove_return_type;
  562. #endif
  563. _LIBCPP_INLINE_VISIBILITY
  564. forward_list()
  565. _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value)
  566. {} // = default;
  567. _LIBCPP_INLINE_VISIBILITY
  568. explicit forward_list(const allocator_type& __a);
  569. explicit forward_list(size_type __n);
  570. #if _LIBCPP_STD_VER > 11
  571. explicit forward_list(size_type __n, const allocator_type& __a);
  572. #endif
  573. forward_list(size_type __n, const value_type& __v);
  574. template <class = __enable_if_t<__is_allocator<_Alloc>::value> >
  575. forward_list(size_type __n, const value_type& __v, const allocator_type& __a) : base(__a)
  576. {
  577. insert_after(cbefore_begin(), __n, __v);
  578. }
  579. template <class _InputIterator>
  580. forward_list(_InputIterator __f, _InputIterator __l,
  581. __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>* = nullptr);
  582. template <class _InputIterator>
  583. forward_list(_InputIterator __f, _InputIterator __l,
  584. const allocator_type& __a,
  585. __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>* = nullptr);
  586. forward_list(const forward_list& __x);
  587. forward_list(const forward_list& __x, const __type_identity_t<allocator_type>& __a);
  588. forward_list& operator=(const forward_list& __x);
  589. #ifndef _LIBCPP_CXX03_LANG
  590. _LIBCPP_INLINE_VISIBILITY
  591. forward_list(forward_list&& __x)
  592. _NOEXCEPT_(is_nothrow_move_constructible<base>::value)
  593. : base(_VSTD::move(__x)) {}
  594. forward_list(forward_list&& __x, const __type_identity_t<allocator_type>& __a);
  595. forward_list(initializer_list<value_type> __il);
  596. forward_list(initializer_list<value_type> __il, const allocator_type& __a);
  597. _LIBCPP_INLINE_VISIBILITY
  598. forward_list& operator=(forward_list&& __x)
  599. _NOEXCEPT_(
  600. __node_traits::propagate_on_container_move_assignment::value &&
  601. is_nothrow_move_assignable<allocator_type>::value);
  602. _LIBCPP_INLINE_VISIBILITY
  603. forward_list& operator=(initializer_list<value_type> __il);
  604. _LIBCPP_INLINE_VISIBILITY
  605. void assign(initializer_list<value_type> __il);
  606. #endif // _LIBCPP_CXX03_LANG
  607. // ~forward_list() = default;
  608. template <class _InputIterator>
  609. __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value, void>
  610. assign(_InputIterator __f, _InputIterator __l);
  611. void assign(size_type __n, const value_type& __v);
  612. _LIBCPP_INLINE_VISIBILITY
  613. allocator_type get_allocator() const _NOEXCEPT
  614. {return allocator_type(base::__alloc());}
  615. _LIBCPP_INLINE_VISIBILITY
  616. iterator begin() _NOEXCEPT
  617. {return iterator(base::__before_begin()->__next_);}
  618. _LIBCPP_INLINE_VISIBILITY
  619. const_iterator begin() const _NOEXCEPT
  620. {return const_iterator(base::__before_begin()->__next_);}
  621. _LIBCPP_INLINE_VISIBILITY
  622. iterator end() _NOEXCEPT
  623. {return iterator(nullptr);}
  624. _LIBCPP_INLINE_VISIBILITY
  625. const_iterator end() const _NOEXCEPT
  626. {return const_iterator(nullptr);}
  627. _LIBCPP_INLINE_VISIBILITY
  628. const_iterator cbegin() const _NOEXCEPT
  629. {return const_iterator(base::__before_begin()->__next_);}
  630. _LIBCPP_INLINE_VISIBILITY
  631. const_iterator cend() const _NOEXCEPT
  632. {return const_iterator(nullptr);}
  633. _LIBCPP_INLINE_VISIBILITY
  634. iterator before_begin() _NOEXCEPT
  635. {return iterator(base::__before_begin());}
  636. _LIBCPP_INLINE_VISIBILITY
  637. const_iterator before_begin() const _NOEXCEPT
  638. {return const_iterator(base::__before_begin());}
  639. _LIBCPP_INLINE_VISIBILITY
  640. const_iterator cbefore_begin() const _NOEXCEPT
  641. {return const_iterator(base::__before_begin());}
  642. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
  643. bool empty() const _NOEXCEPT
  644. {return base::__before_begin()->__next_ == nullptr;}
  645. _LIBCPP_INLINE_VISIBILITY
  646. size_type max_size() const _NOEXCEPT {
  647. return _VSTD::min<size_type>(
  648. __node_traits::max_size(base::__alloc()),
  649. numeric_limits<difference_type>::max());
  650. }
  651. _LIBCPP_INLINE_VISIBILITY
  652. reference front() {return base::__before_begin()->__next_->__value_;}
  653. _LIBCPP_INLINE_VISIBILITY
  654. const_reference front() const {return base::__before_begin()->__next_->__value_;}
  655. #ifndef _LIBCPP_CXX03_LANG
  656. #if _LIBCPP_STD_VER > 14
  657. template <class... _Args> reference emplace_front(_Args&&... __args);
  658. #else
  659. template <class... _Args> void emplace_front(_Args&&... __args);
  660. #endif
  661. void push_front(value_type&& __v);
  662. #endif // _LIBCPP_CXX03_LANG
  663. void push_front(const value_type& __v);
  664. void pop_front();
  665. #ifndef _LIBCPP_CXX03_LANG
  666. template <class... _Args>
  667. iterator emplace_after(const_iterator __p, _Args&&... __args);
  668. iterator insert_after(const_iterator __p, value_type&& __v);
  669. iterator insert_after(const_iterator __p, initializer_list<value_type> __il)
  670. {return insert_after(__p, __il.begin(), __il.end());}
  671. #endif // _LIBCPP_CXX03_LANG
  672. iterator insert_after(const_iterator __p, const value_type& __v);
  673. iterator insert_after(const_iterator __p, size_type __n, const value_type& __v);
  674. template <class _InputIterator>
  675. _LIBCPP_INLINE_VISIBILITY
  676. __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value, iterator>
  677. insert_after(const_iterator __p, _InputIterator __f, _InputIterator __l);
  678. iterator erase_after(const_iterator __p);
  679. iterator erase_after(const_iterator __f, const_iterator __l);
  680. _LIBCPP_INLINE_VISIBILITY
  681. void swap(forward_list& __x)
  682. #if _LIBCPP_STD_VER >= 14
  683. _NOEXCEPT
  684. #else
  685. _NOEXCEPT_(!__node_traits::propagate_on_container_swap::value ||
  686. __is_nothrow_swappable<__node_allocator>::value)
  687. #endif
  688. {base::swap(__x);}
  689. void resize(size_type __n);
  690. void resize(size_type __n, const value_type& __v);
  691. _LIBCPP_INLINE_VISIBILITY
  692. void clear() _NOEXCEPT {base::clear();}
  693. _LIBCPP_INLINE_VISIBILITY
  694. void splice_after(const_iterator __p, forward_list&& __x);
  695. _LIBCPP_INLINE_VISIBILITY
  696. void splice_after(const_iterator __p, forward_list&& __x, const_iterator __i);
  697. _LIBCPP_INLINE_VISIBILITY
  698. void splice_after(const_iterator __p, forward_list&& __x,
  699. const_iterator __f, const_iterator __l);
  700. void splice_after(const_iterator __p, forward_list& __x);
  701. void splice_after(const_iterator __p, forward_list& __x, const_iterator __i);
  702. void splice_after(const_iterator __p, forward_list& __x,
  703. const_iterator __f, const_iterator __l);
  704. __remove_return_type remove(const value_type& __v);
  705. template <class _Predicate> __remove_return_type remove_if(_Predicate __pred);
  706. _LIBCPP_INLINE_VISIBILITY
  707. __remove_return_type unique() { return unique(__equal_to()); }
  708. template <class _BinaryPredicate> __remove_return_type unique(_BinaryPredicate __binary_pred);
  709. #ifndef _LIBCPP_CXX03_LANG
  710. _LIBCPP_INLINE_VISIBILITY
  711. void merge(forward_list&& __x) {merge(__x, __less<value_type>());}
  712. template <class _Compare>
  713. _LIBCPP_INLINE_VISIBILITY
  714. void merge(forward_list&& __x, _Compare __comp)
  715. {merge(__x, _VSTD::move(__comp));}
  716. #endif // _LIBCPP_CXX03_LANG
  717. _LIBCPP_INLINE_VISIBILITY
  718. void merge(forward_list& __x) {merge(__x, __less<value_type>());}
  719. template <class _Compare> void merge(forward_list& __x, _Compare __comp);
  720. _LIBCPP_INLINE_VISIBILITY
  721. void sort() {sort(__less<value_type>());}
  722. template <class _Compare> _LIBCPP_INLINE_VISIBILITY void sort(_Compare __comp);
  723. void reverse() _NOEXCEPT;
  724. private:
  725. #ifndef _LIBCPP_CXX03_LANG
  726. void __move_assign(forward_list& __x, true_type)
  727. _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
  728. void __move_assign(forward_list& __x, false_type);
  729. #endif // _LIBCPP_CXX03_LANG
  730. template <class _Compare>
  731. static
  732. __node_pointer
  733. __merge(__node_pointer __f1, __node_pointer __f2, _Compare& __comp);
  734. template <class _Compare>
  735. static
  736. __node_pointer
  737. __sort(__node_pointer __f, difference_type __sz, _Compare& __comp);
  738. };
  739. #if _LIBCPP_STD_VER >= 17
  740. template<class _InputIterator,
  741. class _Alloc = allocator<__iter_value_type<_InputIterator>>,
  742. class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
  743. class = enable_if_t<__is_allocator<_Alloc>::value>
  744. >
  745. forward_list(_InputIterator, _InputIterator)
  746. -> forward_list<__iter_value_type<_InputIterator>, _Alloc>;
  747. template<class _InputIterator,
  748. class _Alloc,
  749. class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
  750. class = enable_if_t<__is_allocator<_Alloc>::value>
  751. >
  752. forward_list(_InputIterator, _InputIterator, _Alloc)
  753. -> forward_list<__iter_value_type<_InputIterator>, _Alloc>;
  754. #endif
  755. template <class _Tp, class _Alloc>
  756. inline
  757. forward_list<_Tp, _Alloc>::forward_list(const allocator_type& __a)
  758. : base(__a)
  759. {
  760. }
  761. template <class _Tp, class _Alloc>
  762. forward_list<_Tp, _Alloc>::forward_list(size_type __n)
  763. {
  764. if (__n > 0)
  765. {
  766. __node_allocator& __a = base::__alloc();
  767. typedef __allocator_destructor<__node_allocator> _Dp;
  768. unique_ptr<__node, _Dp> __h(nullptr, _Dp(__a, 1));
  769. for (__begin_node_pointer __p = base::__before_begin(); __n > 0; --__n,
  770. __p = __p->__next_as_begin())
  771. {
  772. __h.reset(__node_traits::allocate(__a, 1));
  773. __node_traits::construct(__a, _VSTD::addressof(__h->__value_));
  774. __h->__next_ = nullptr;
  775. __p->__next_ = __h.release();
  776. }
  777. }
  778. }
  779. #if _LIBCPP_STD_VER > 11
  780. template <class _Tp, class _Alloc>
  781. forward_list<_Tp, _Alloc>::forward_list(size_type __n,
  782. const allocator_type& __base_alloc)
  783. : base ( __base_alloc )
  784. {
  785. if (__n > 0)
  786. {
  787. __node_allocator& __a = base::__alloc();
  788. typedef __allocator_destructor<__node_allocator> _Dp;
  789. unique_ptr<__node, _Dp> __h(nullptr, _Dp(__a, 1));
  790. for (__begin_node_pointer __p = base::__before_begin(); __n > 0; --__n,
  791. __p = __p->__next_as_begin())
  792. {
  793. __h.reset(__node_traits::allocate(__a, 1));
  794. __node_traits::construct(__a, _VSTD::addressof(__h->__value_));
  795. __h->__next_ = nullptr;
  796. __p->__next_ = __h.release();
  797. }
  798. }
  799. }
  800. #endif
  801. template <class _Tp, class _Alloc>
  802. forward_list<_Tp, _Alloc>::forward_list(size_type __n, const value_type& __v)
  803. {
  804. insert_after(cbefore_begin(), __n, __v);
  805. }
  806. template <class _Tp, class _Alloc>
  807. template <class _InputIterator>
  808. forward_list<_Tp, _Alloc>::forward_list(_InputIterator __f, _InputIterator __l,
  809. __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>*)
  810. {
  811. insert_after(cbefore_begin(), __f, __l);
  812. }
  813. template <class _Tp, class _Alloc>
  814. template <class _InputIterator>
  815. forward_list<_Tp, _Alloc>::forward_list(_InputIterator __f, _InputIterator __l,
  816. const allocator_type& __a,
  817. __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>*)
  818. : base(__a)
  819. {
  820. insert_after(cbefore_begin(), __f, __l);
  821. }
  822. template <class _Tp, class _Alloc>
  823. forward_list<_Tp, _Alloc>::forward_list(const forward_list& __x)
  824. : base(
  825. __node_traits::select_on_container_copy_construction(__x.__alloc())) {
  826. insert_after(cbefore_begin(), __x.begin(), __x.end());
  827. }
  828. template <class _Tp, class _Alloc>
  829. forward_list<_Tp, _Alloc>::forward_list(const forward_list& __x,
  830. const __type_identity_t<allocator_type>& __a)
  831. : base(__a)
  832. {
  833. insert_after(cbefore_begin(), __x.begin(), __x.end());
  834. }
  835. template <class _Tp, class _Alloc>
  836. forward_list<_Tp, _Alloc>&
  837. forward_list<_Tp, _Alloc>::operator=(const forward_list& __x)
  838. {
  839. if (this != _VSTD::addressof(__x))
  840. {
  841. base::__copy_assign_alloc(__x);
  842. assign(__x.begin(), __x.end());
  843. }
  844. return *this;
  845. }
  846. #ifndef _LIBCPP_CXX03_LANG
  847. template <class _Tp, class _Alloc>
  848. forward_list<_Tp, _Alloc>::forward_list(forward_list&& __x,
  849. const __type_identity_t<allocator_type>& __a)
  850. : base(_VSTD::move(__x), __a)
  851. {
  852. if (base::__alloc() != __x.__alloc())
  853. {
  854. typedef move_iterator<iterator> _Ip;
  855. insert_after(cbefore_begin(), _Ip(__x.begin()), _Ip(__x.end()));
  856. }
  857. }
  858. template <class _Tp, class _Alloc>
  859. forward_list<_Tp, _Alloc>::forward_list(initializer_list<value_type> __il)
  860. {
  861. insert_after(cbefore_begin(), __il.begin(), __il.end());
  862. }
  863. template <class _Tp, class _Alloc>
  864. forward_list<_Tp, _Alloc>::forward_list(initializer_list<value_type> __il,
  865. const allocator_type& __a)
  866. : base(__a)
  867. {
  868. insert_after(cbefore_begin(), __il.begin(), __il.end());
  869. }
  870. template <class _Tp, class _Alloc>
  871. void
  872. forward_list<_Tp, _Alloc>::__move_assign(forward_list& __x, true_type)
  873. _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
  874. {
  875. clear();
  876. base::__move_assign_alloc(__x);
  877. base::__before_begin()->__next_ = __x.__before_begin()->__next_;
  878. __x.__before_begin()->__next_ = nullptr;
  879. }
  880. template <class _Tp, class _Alloc>
  881. void
  882. forward_list<_Tp, _Alloc>::__move_assign(forward_list& __x, false_type)
  883. {
  884. if (base::__alloc() == __x.__alloc())
  885. __move_assign(__x, true_type());
  886. else
  887. {
  888. typedef move_iterator<iterator> _Ip;
  889. assign(_Ip(__x.begin()), _Ip(__x.end()));
  890. }
  891. }
  892. template <class _Tp, class _Alloc>
  893. inline
  894. forward_list<_Tp, _Alloc>&
  895. forward_list<_Tp, _Alloc>::operator=(forward_list&& __x)
  896. _NOEXCEPT_(
  897. __node_traits::propagate_on_container_move_assignment::value &&
  898. is_nothrow_move_assignable<allocator_type>::value)
  899. {
  900. __move_assign(__x, integral_constant<bool,
  901. __node_traits::propagate_on_container_move_assignment::value>());
  902. return *this;
  903. }
  904. template <class _Tp, class _Alloc>
  905. inline
  906. forward_list<_Tp, _Alloc>&
  907. forward_list<_Tp, _Alloc>::operator=(initializer_list<value_type> __il)
  908. {
  909. assign(__il.begin(), __il.end());
  910. return *this;
  911. }
  912. #endif // _LIBCPP_CXX03_LANG
  913. template <class _Tp, class _Alloc>
  914. template <class _InputIterator>
  915. __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value, void>
  916. forward_list<_Tp, _Alloc>::assign(_InputIterator __f, _InputIterator __l)
  917. {
  918. iterator __i = before_begin();
  919. iterator __j = _VSTD::next(__i);
  920. iterator __e = end();
  921. for (; __j != __e && __f != __l; ++__i, (void) ++__j, ++__f)
  922. *__j = *__f;
  923. if (__j == __e)
  924. insert_after(__i, __f, __l);
  925. else
  926. erase_after(__i, __e);
  927. }
  928. template <class _Tp, class _Alloc>
  929. void
  930. forward_list<_Tp, _Alloc>::assign(size_type __n, const value_type& __v)
  931. {
  932. iterator __i = before_begin();
  933. iterator __j = _VSTD::next(__i);
  934. iterator __e = end();
  935. for (; __j != __e && __n > 0; --__n, ++__i, ++__j)
  936. *__j = __v;
  937. if (__j == __e)
  938. insert_after(__i, __n, __v);
  939. else
  940. erase_after(__i, __e);
  941. }
  942. #ifndef _LIBCPP_CXX03_LANG
  943. template <class _Tp, class _Alloc>
  944. inline
  945. void
  946. forward_list<_Tp, _Alloc>::assign(initializer_list<value_type> __il)
  947. {
  948. assign(__il.begin(), __il.end());
  949. }
  950. template <class _Tp, class _Alloc>
  951. template <class... _Args>
  952. #if _LIBCPP_STD_VER > 14
  953. typename forward_list<_Tp, _Alloc>::reference
  954. #else
  955. void
  956. #endif
  957. forward_list<_Tp, _Alloc>::emplace_front(_Args&&... __args)
  958. {
  959. __node_allocator& __a = base::__alloc();
  960. typedef __allocator_destructor<__node_allocator> _Dp;
  961. unique_ptr<__node, _Dp> __h(__node_traits::allocate(__a, 1), _Dp(__a, 1));
  962. __node_traits::construct(__a, _VSTD::addressof(__h->__value_),
  963. _VSTD::forward<_Args>(__args)...);
  964. __h->__next_ = base::__before_begin()->__next_;
  965. base::__before_begin()->__next_ = __h.release();
  966. #if _LIBCPP_STD_VER > 14
  967. return base::__before_begin()->__next_->__value_;
  968. #endif
  969. }
  970. template <class _Tp, class _Alloc>
  971. void
  972. forward_list<_Tp, _Alloc>::push_front(value_type&& __v)
  973. {
  974. __node_allocator& __a = base::__alloc();
  975. typedef __allocator_destructor<__node_allocator> _Dp;
  976. unique_ptr<__node, _Dp> __h(__node_traits::allocate(__a, 1), _Dp(__a, 1));
  977. __node_traits::construct(__a, _VSTD::addressof(__h->__value_), _VSTD::move(__v));
  978. __h->__next_ = base::__before_begin()->__next_;
  979. base::__before_begin()->__next_ = __h.release();
  980. }
  981. #endif // _LIBCPP_CXX03_LANG
  982. template <class _Tp, class _Alloc>
  983. void
  984. forward_list<_Tp, _Alloc>::push_front(const value_type& __v)
  985. {
  986. __node_allocator& __a = base::__alloc();
  987. typedef __allocator_destructor<__node_allocator> _Dp;
  988. unique_ptr<__node, _Dp> __h(__node_traits::allocate(__a, 1), _Dp(__a, 1));
  989. __node_traits::construct(__a, _VSTD::addressof(__h->__value_), __v);
  990. __h->__next_ = base::__before_begin()->__next_;
  991. base::__before_begin()->__next_ = __h.release();
  992. }
  993. template <class _Tp, class _Alloc>
  994. void
  995. forward_list<_Tp, _Alloc>::pop_front()
  996. {
  997. __node_allocator& __a = base::__alloc();
  998. __node_pointer __p = base::__before_begin()->__next_;
  999. base::__before_begin()->__next_ = __p->__next_;
  1000. __node_traits::destroy(__a, _VSTD::addressof(__p->__value_));
  1001. __node_traits::deallocate(__a, __p, 1);
  1002. }
  1003. #ifndef _LIBCPP_CXX03_LANG
  1004. template <class _Tp, class _Alloc>
  1005. template <class... _Args>
  1006. typename forward_list<_Tp, _Alloc>::iterator
  1007. forward_list<_Tp, _Alloc>::emplace_after(const_iterator __p, _Args&&... __args)
  1008. {
  1009. __begin_node_pointer const __r = __p.__get_begin();
  1010. __node_allocator& __a = base::__alloc();
  1011. typedef __allocator_destructor<__node_allocator> _Dp;
  1012. unique_ptr<__node, _Dp> __h(__node_traits::allocate(__a, 1), _Dp(__a, 1));
  1013. __node_traits::construct(__a, _VSTD::addressof(__h->__value_),
  1014. _VSTD::forward<_Args>(__args)...);
  1015. __h->__next_ = __r->__next_;
  1016. __r->__next_ = __h.release();
  1017. return iterator(__r->__next_);
  1018. }
  1019. template <class _Tp, class _Alloc>
  1020. typename forward_list<_Tp, _Alloc>::iterator
  1021. forward_list<_Tp, _Alloc>::insert_after(const_iterator __p, value_type&& __v)
  1022. {
  1023. __begin_node_pointer const __r = __p.__get_begin();
  1024. __node_allocator& __a = base::__alloc();
  1025. typedef __allocator_destructor<__node_allocator> _Dp;
  1026. unique_ptr<__node, _Dp> __h(__node_traits::allocate(__a, 1), _Dp(__a, 1));
  1027. __node_traits::construct(__a, _VSTD::addressof(__h->__value_), _VSTD::move(__v));
  1028. __h->__next_ = __r->__next_;
  1029. __r->__next_ = __h.release();
  1030. return iterator(__r->__next_);
  1031. }
  1032. #endif // _LIBCPP_CXX03_LANG
  1033. template <class _Tp, class _Alloc>
  1034. typename forward_list<_Tp, _Alloc>::iterator
  1035. forward_list<_Tp, _Alloc>::insert_after(const_iterator __p, const value_type& __v)
  1036. {
  1037. __begin_node_pointer const __r = __p.__get_begin();
  1038. __node_allocator& __a = base::__alloc();
  1039. typedef __allocator_destructor<__node_allocator> _Dp;
  1040. unique_ptr<__node, _Dp> __h(__node_traits::allocate(__a, 1), _Dp(__a, 1));
  1041. __node_traits::construct(__a, _VSTD::addressof(__h->__value_), __v);
  1042. __h->__next_ = __r->__next_;
  1043. __r->__next_ = __h.release();
  1044. return iterator(__r->__next_);
  1045. }
  1046. template <class _Tp, class _Alloc>
  1047. typename forward_list<_Tp, _Alloc>::iterator
  1048. forward_list<_Tp, _Alloc>::insert_after(const_iterator __p, size_type __n,
  1049. const value_type& __v)
  1050. {
  1051. __begin_node_pointer __r = __p.__get_begin();
  1052. if (__n > 0)
  1053. {
  1054. __node_allocator& __a = base::__alloc();
  1055. typedef __allocator_destructor<__node_allocator> _Dp;
  1056. unique_ptr<__node, _Dp> __h(__node_traits::allocate(__a, 1), _Dp(__a, 1));
  1057. __node_traits::construct(__a, _VSTD::addressof(__h->__value_), __v);
  1058. __node_pointer __first = __h.release();
  1059. __node_pointer __last = __first;
  1060. #ifndef _LIBCPP_NO_EXCEPTIONS
  1061. try
  1062. {
  1063. #endif // _LIBCPP_NO_EXCEPTIONS
  1064. for (--__n; __n != 0; --__n, __last = __last->__next_)
  1065. {
  1066. __h.reset(__node_traits::allocate(__a, 1));
  1067. __node_traits::construct(__a, _VSTD::addressof(__h->__value_), __v);
  1068. __last->__next_ = __h.release();
  1069. }
  1070. #ifndef _LIBCPP_NO_EXCEPTIONS
  1071. }
  1072. catch (...)
  1073. {
  1074. while (__first != nullptr)
  1075. {
  1076. __node_pointer __next = __first->__next_;
  1077. __node_traits::destroy(__a, _VSTD::addressof(__first->__value_));
  1078. __node_traits::deallocate(__a, __first, 1);
  1079. __first = __next;
  1080. }
  1081. throw;
  1082. }
  1083. #endif // _LIBCPP_NO_EXCEPTIONS
  1084. __last->__next_ = __r->__next_;
  1085. __r->__next_ = __first;
  1086. __r = static_cast<__begin_node_pointer>(__last);
  1087. }
  1088. return iterator(__r);
  1089. }
  1090. template <class _Tp, class _Alloc>
  1091. template <class _InputIterator>
  1092. __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value, typename forward_list<_Tp, _Alloc>::iterator>
  1093. forward_list<_Tp, _Alloc>::insert_after(const_iterator __p,
  1094. _InputIterator __f, _InputIterator __l)
  1095. {
  1096. __begin_node_pointer __r = __p.__get_begin();
  1097. if (__f != __l)
  1098. {
  1099. __node_allocator& __a = base::__alloc();
  1100. typedef __allocator_destructor<__node_allocator> _Dp;
  1101. unique_ptr<__node, _Dp> __h(__node_traits::allocate(__a, 1), _Dp(__a, 1));
  1102. __node_traits::construct(__a, _VSTD::addressof(__h->__value_), *__f);
  1103. __node_pointer __first = __h.release();
  1104. __node_pointer __last = __first;
  1105. #ifndef _LIBCPP_NO_EXCEPTIONS
  1106. try
  1107. {
  1108. #endif // _LIBCPP_NO_EXCEPTIONS
  1109. for (++__f; __f != __l; ++__f, ((void)(__last = __last->__next_)))
  1110. {
  1111. __h.reset(__node_traits::allocate(__a, 1));
  1112. __node_traits::construct(__a, _VSTD::addressof(__h->__value_), *__f);
  1113. __last->__next_ = __h.release();
  1114. }
  1115. #ifndef _LIBCPP_NO_EXCEPTIONS
  1116. }
  1117. catch (...)
  1118. {
  1119. while (__first != nullptr)
  1120. {
  1121. __node_pointer __next = __first->__next_;
  1122. __node_traits::destroy(__a, _VSTD::addressof(__first->__value_));
  1123. __node_traits::deallocate(__a, __first, 1);
  1124. __first = __next;
  1125. }
  1126. throw;
  1127. }
  1128. #endif // _LIBCPP_NO_EXCEPTIONS
  1129. __last->__next_ = __r->__next_;
  1130. __r->__next_ = __first;
  1131. __r = static_cast<__begin_node_pointer>(__last);
  1132. }
  1133. return iterator(__r);
  1134. }
  1135. template <class _Tp, class _Alloc>
  1136. typename forward_list<_Tp, _Alloc>::iterator
  1137. forward_list<_Tp, _Alloc>::erase_after(const_iterator __f)
  1138. {
  1139. __begin_node_pointer __p = __f.__get_begin();
  1140. __node_pointer __n = __p->__next_;
  1141. __p->__next_ = __n->__next_;
  1142. __node_allocator& __a = base::__alloc();
  1143. __node_traits::destroy(__a, _VSTD::addressof(__n->__value_));
  1144. __node_traits::deallocate(__a, __n, 1);
  1145. return iterator(__p->__next_);
  1146. }
  1147. template <class _Tp, class _Alloc>
  1148. typename forward_list<_Tp, _Alloc>::iterator
  1149. forward_list<_Tp, _Alloc>::erase_after(const_iterator __f, const_iterator __l)
  1150. {
  1151. __node_pointer __e = __l.__get_unsafe_node_pointer();
  1152. if (__f != __l)
  1153. {
  1154. __begin_node_pointer __bp = __f.__get_begin();
  1155. __node_pointer __n = __bp->__next_;
  1156. if (__n != __e)
  1157. {
  1158. __bp->__next_ = __e;
  1159. __node_allocator& __a = base::__alloc();
  1160. do
  1161. {
  1162. __node_pointer __tmp = __n->__next_;
  1163. __node_traits::destroy(__a, _VSTD::addressof(__n->__value_));
  1164. __node_traits::deallocate(__a, __n, 1);
  1165. __n = __tmp;
  1166. } while (__n != __e);
  1167. }
  1168. }
  1169. return iterator(__e);
  1170. }
  1171. template <class _Tp, class _Alloc>
  1172. void
  1173. forward_list<_Tp, _Alloc>::resize(size_type __n)
  1174. {
  1175. size_type __sz = 0;
  1176. iterator __p = before_begin();
  1177. iterator __i = begin();
  1178. iterator __e = end();
  1179. for (; __i != __e && __sz < __n; ++__p, ++__i, ++__sz)
  1180. ;
  1181. if (__i != __e)
  1182. erase_after(__p, __e);
  1183. else
  1184. {
  1185. __n -= __sz;
  1186. if (__n > 0)
  1187. {
  1188. __node_allocator& __a = base::__alloc();
  1189. typedef __allocator_destructor<__node_allocator> _Dp;
  1190. unique_ptr<__node, _Dp> __h(nullptr, _Dp(__a, 1));
  1191. for (__begin_node_pointer __ptr = __p.__get_begin(); __n > 0; --__n,
  1192. __ptr = __ptr->__next_as_begin())
  1193. {
  1194. __h.reset(__node_traits::allocate(__a, 1));
  1195. __node_traits::construct(__a, _VSTD::addressof(__h->__value_));
  1196. __h->__next_ = nullptr;
  1197. __ptr->__next_ = __h.release();
  1198. }
  1199. }
  1200. }
  1201. }
  1202. template <class _Tp, class _Alloc>
  1203. void
  1204. forward_list<_Tp, _Alloc>::resize(size_type __n, const value_type& __v)
  1205. {
  1206. size_type __sz = 0;
  1207. iterator __p = before_begin();
  1208. iterator __i = begin();
  1209. iterator __e = end();
  1210. for (; __i != __e && __sz < __n; ++__p, ++__i, ++__sz)
  1211. ;
  1212. if (__i != __e)
  1213. erase_after(__p, __e);
  1214. else
  1215. {
  1216. __n -= __sz;
  1217. if (__n > 0)
  1218. {
  1219. __node_allocator& __a = base::__alloc();
  1220. typedef __allocator_destructor<__node_allocator> _Dp;
  1221. unique_ptr<__node, _Dp> __h(nullptr, _Dp(__a, 1));
  1222. for (__begin_node_pointer __ptr = __p.__get_begin(); __n > 0; --__n,
  1223. __ptr = __ptr->__next_as_begin())
  1224. {
  1225. __h.reset(__node_traits::allocate(__a, 1));
  1226. __node_traits::construct(__a, _VSTD::addressof(__h->__value_), __v);
  1227. __h->__next_ = nullptr;
  1228. __ptr->__next_ = __h.release();
  1229. }
  1230. }
  1231. }
  1232. }
  1233. template <class _Tp, class _Alloc>
  1234. void
  1235. forward_list<_Tp, _Alloc>::splice_after(const_iterator __p,
  1236. forward_list& __x)
  1237. {
  1238. if (!__x.empty())
  1239. {
  1240. if (__p.__get_begin()->__next_ != nullptr)
  1241. {
  1242. const_iterator __lm1 = __x.before_begin();
  1243. while (__lm1.__get_begin()->__next_ != nullptr)
  1244. ++__lm1;
  1245. __lm1.__get_begin()->__next_ = __p.__get_begin()->__next_;
  1246. }
  1247. __p.__get_begin()->__next_ = __x.__before_begin()->__next_;
  1248. __x.__before_begin()->__next_ = nullptr;
  1249. }
  1250. }
  1251. template <class _Tp, class _Alloc>
  1252. void
  1253. forward_list<_Tp, _Alloc>::splice_after(const_iterator __p,
  1254. forward_list& /*__other*/,
  1255. const_iterator __i)
  1256. {
  1257. const_iterator __lm1 = _VSTD::next(__i);
  1258. if (__p != __i && __p != __lm1)
  1259. {
  1260. __i.__get_begin()->__next_ = __lm1.__get_begin()->__next_;
  1261. __lm1.__get_begin()->__next_ = __p.__get_begin()->__next_;
  1262. __p.__get_begin()->__next_ = __lm1.__get_unsafe_node_pointer();
  1263. }
  1264. }
  1265. template <class _Tp, class _Alloc>
  1266. void
  1267. forward_list<_Tp, _Alloc>::splice_after(const_iterator __p,
  1268. forward_list& /*__other*/,
  1269. const_iterator __f, const_iterator __l)
  1270. {
  1271. if (__f != __l && __p != __f)
  1272. {
  1273. const_iterator __lm1 = __f;
  1274. while (__lm1.__get_begin()->__next_ != __l.__get_begin())
  1275. ++__lm1;
  1276. if (__f != __lm1)
  1277. {
  1278. __lm1.__get_begin()->__next_ = __p.__get_begin()->__next_;
  1279. __p.__get_begin()->__next_ = __f.__get_begin()->__next_;
  1280. __f.__get_begin()->__next_ = __l.__get_unsafe_node_pointer();
  1281. }
  1282. }
  1283. }
  1284. template <class _Tp, class _Alloc>
  1285. inline _LIBCPP_INLINE_VISIBILITY
  1286. void
  1287. forward_list<_Tp, _Alloc>::splice_after(const_iterator __p,
  1288. forward_list&& __x)
  1289. {
  1290. splice_after(__p, __x);
  1291. }
  1292. template <class _Tp, class _Alloc>
  1293. inline _LIBCPP_INLINE_VISIBILITY
  1294. void
  1295. forward_list<_Tp, _Alloc>::splice_after(const_iterator __p,
  1296. forward_list&& __x,
  1297. const_iterator __i)
  1298. {
  1299. splice_after(__p, __x, __i);
  1300. }
  1301. template <class _Tp, class _Alloc>
  1302. inline _LIBCPP_INLINE_VISIBILITY
  1303. void
  1304. forward_list<_Tp, _Alloc>::splice_after(const_iterator __p,
  1305. forward_list&& __x,
  1306. const_iterator __f, const_iterator __l)
  1307. {
  1308. splice_after(__p, __x, __f, __l);
  1309. }
  1310. template <class _Tp, class _Alloc>
  1311. typename forward_list<_Tp, _Alloc>::__remove_return_type
  1312. forward_list<_Tp, _Alloc>::remove(const value_type& __v)
  1313. {
  1314. forward_list<_Tp, _Alloc> __deleted_nodes(get_allocator()); // collect the nodes we're removing
  1315. typename forward_list<_Tp, _Alloc>::size_type __count_removed = 0;
  1316. const iterator __e = end();
  1317. for (iterator __i = before_begin(); __i.__get_begin()->__next_ != nullptr;)
  1318. {
  1319. if (__i.__get_begin()->__next_->__value_ == __v)
  1320. {
  1321. ++__count_removed;
  1322. iterator __j = _VSTD::next(__i, 2);
  1323. for (; __j != __e && *__j == __v; ++__j)
  1324. ++__count_removed;
  1325. __deleted_nodes.splice_after(__deleted_nodes.before_begin(), *this, __i, __j);
  1326. if (__j == __e)
  1327. break;
  1328. __i = __j;
  1329. }
  1330. else
  1331. ++__i;
  1332. }
  1333. return (__remove_return_type) __count_removed;
  1334. }
  1335. template <class _Tp, class _Alloc>
  1336. template <class _Predicate>
  1337. typename forward_list<_Tp, _Alloc>::__remove_return_type
  1338. forward_list<_Tp, _Alloc>::remove_if(_Predicate __pred)
  1339. {
  1340. forward_list<_Tp, _Alloc> __deleted_nodes(get_allocator()); // collect the nodes we're removing
  1341. typename forward_list<_Tp, _Alloc>::size_type __count_removed = 0;
  1342. const iterator __e = end();
  1343. for (iterator __i = before_begin(); __i.__get_begin()->__next_ != nullptr;)
  1344. {
  1345. if (__pred(__i.__get_begin()->__next_->__value_))
  1346. {
  1347. ++__count_removed;
  1348. iterator __j = _VSTD::next(__i, 2);
  1349. for (; __j != __e && __pred(*__j); ++__j)
  1350. ++__count_removed;
  1351. __deleted_nodes.splice_after(__deleted_nodes.before_begin(), *this, __i, __j);
  1352. if (__j == __e)
  1353. break;
  1354. __i = __j;
  1355. }
  1356. else
  1357. ++__i;
  1358. }
  1359. return (__remove_return_type) __count_removed;
  1360. }
  1361. template <class _Tp, class _Alloc>
  1362. template <class _BinaryPredicate>
  1363. typename forward_list<_Tp, _Alloc>::__remove_return_type
  1364. forward_list<_Tp, _Alloc>::unique(_BinaryPredicate __binary_pred)
  1365. {
  1366. forward_list<_Tp, _Alloc> __deleted_nodes(get_allocator()); // collect the nodes we're removing
  1367. typename forward_list<_Tp, _Alloc>::size_type __count_removed = 0;
  1368. for (iterator __i = begin(), __e = end(); __i != __e;)
  1369. {
  1370. iterator __j = _VSTD::next(__i);
  1371. for (; __j != __e && __binary_pred(*__i, *__j); ++__j)
  1372. ++__count_removed;
  1373. if (__i.__get_begin()->__next_ != __j.__get_unsafe_node_pointer())
  1374. __deleted_nodes.splice_after(__deleted_nodes.before_begin(), *this, __i, __j);
  1375. __i = __j;
  1376. }
  1377. return (__remove_return_type) __count_removed;
  1378. }
  1379. template <class _Tp, class _Alloc>
  1380. template <class _Compare>
  1381. void
  1382. forward_list<_Tp, _Alloc>::merge(forward_list& __x, _Compare __comp)
  1383. {
  1384. if (this != _VSTD::addressof(__x))
  1385. {
  1386. base::__before_begin()->__next_ = __merge(base::__before_begin()->__next_,
  1387. __x.__before_begin()->__next_,
  1388. __comp);
  1389. __x.__before_begin()->__next_ = nullptr;
  1390. }
  1391. }
  1392. template <class _Tp, class _Alloc>
  1393. template <class _Compare>
  1394. typename forward_list<_Tp, _Alloc>::__node_pointer
  1395. forward_list<_Tp, _Alloc>::__merge(__node_pointer __f1, __node_pointer __f2,
  1396. _Compare& __comp)
  1397. {
  1398. if (__f1 == nullptr)
  1399. return __f2;
  1400. if (__f2 == nullptr)
  1401. return __f1;
  1402. __node_pointer __r;
  1403. if (__comp(__f2->__value_, __f1->__value_))
  1404. {
  1405. __node_pointer __t = __f2;
  1406. while (__t->__next_ != nullptr &&
  1407. __comp(__t->__next_->__value_, __f1->__value_))
  1408. __t = __t->__next_;
  1409. __r = __f2;
  1410. __f2 = __t->__next_;
  1411. __t->__next_ = __f1;
  1412. }
  1413. else
  1414. __r = __f1;
  1415. __node_pointer __p = __f1;
  1416. __f1 = __f1->__next_;
  1417. while (__f1 != nullptr && __f2 != nullptr)
  1418. {
  1419. if (__comp(__f2->__value_, __f1->__value_))
  1420. {
  1421. __node_pointer __t = __f2;
  1422. while (__t->__next_ != nullptr &&
  1423. __comp(__t->__next_->__value_, __f1->__value_))
  1424. __t = __t->__next_;
  1425. __p->__next_ = __f2;
  1426. __f2 = __t->__next_;
  1427. __t->__next_ = __f1;
  1428. }
  1429. __p = __f1;
  1430. __f1 = __f1->__next_;
  1431. }
  1432. if (__f2 != nullptr)
  1433. __p->__next_ = __f2;
  1434. return __r;
  1435. }
  1436. template <class _Tp, class _Alloc>
  1437. template <class _Compare>
  1438. inline
  1439. void
  1440. forward_list<_Tp, _Alloc>::sort(_Compare __comp)
  1441. {
  1442. base::__before_begin()->__next_ = __sort(base::__before_begin()->__next_,
  1443. _VSTD::distance(begin(), end()), __comp);
  1444. }
  1445. template <class _Tp, class _Alloc>
  1446. template <class _Compare>
  1447. typename forward_list<_Tp, _Alloc>::__node_pointer
  1448. forward_list<_Tp, _Alloc>::__sort(__node_pointer __f1, difference_type __sz,
  1449. _Compare& __comp)
  1450. {
  1451. switch (__sz)
  1452. {
  1453. case 0:
  1454. case 1:
  1455. return __f1;
  1456. case 2:
  1457. if (__comp(__f1->__next_->__value_, __f1->__value_))
  1458. {
  1459. __node_pointer __t = __f1->__next_;
  1460. __t->__next_ = __f1;
  1461. __f1->__next_ = nullptr;
  1462. __f1 = __t;
  1463. }
  1464. return __f1;
  1465. }
  1466. difference_type __sz1 = __sz / 2;
  1467. difference_type __sz2 = __sz - __sz1;
  1468. __node_pointer __t = _VSTD::next(iterator(__f1), __sz1 - 1).__get_unsafe_node_pointer();
  1469. __node_pointer __f2 = __t->__next_;
  1470. __t->__next_ = nullptr;
  1471. return __merge(__sort(__f1, __sz1, __comp),
  1472. __sort(__f2, __sz2, __comp), __comp);
  1473. }
  1474. template <class _Tp, class _Alloc>
  1475. void
  1476. forward_list<_Tp, _Alloc>::reverse() _NOEXCEPT
  1477. {
  1478. __node_pointer __p = base::__before_begin()->__next_;
  1479. if (__p != nullptr)
  1480. {
  1481. __node_pointer __f = __p->__next_;
  1482. __p->__next_ = nullptr;
  1483. while (__f != nullptr)
  1484. {
  1485. __node_pointer __t = __f->__next_;
  1486. __f->__next_ = __p;
  1487. __p = __f;
  1488. __f = __t;
  1489. }
  1490. base::__before_begin()->__next_ = __p;
  1491. }
  1492. }
  1493. template <class _Tp, class _Alloc>
  1494. _LIBCPP_HIDE_FROM_ABI
  1495. bool operator==(const forward_list<_Tp, _Alloc>& __x,
  1496. const forward_list<_Tp, _Alloc>& __y)
  1497. {
  1498. typedef forward_list<_Tp, _Alloc> _Cp;
  1499. typedef typename _Cp::const_iterator _Ip;
  1500. _Ip __ix = __x.begin();
  1501. _Ip __ex = __x.end();
  1502. _Ip __iy = __y.begin();
  1503. _Ip __ey = __y.end();
  1504. for (; __ix != __ex && __iy != __ey; ++__ix, ++__iy)
  1505. if (!(*__ix == *__iy))
  1506. return false;
  1507. return (__ix == __ex) == (__iy == __ey);
  1508. }
  1509. template <class _Tp, class _Alloc>
  1510. inline _LIBCPP_INLINE_VISIBILITY
  1511. bool operator!=(const forward_list<_Tp, _Alloc>& __x,
  1512. const forward_list<_Tp, _Alloc>& __y)
  1513. {
  1514. return !(__x == __y);
  1515. }
  1516. template <class _Tp, class _Alloc>
  1517. inline _LIBCPP_INLINE_VISIBILITY
  1518. bool operator< (const forward_list<_Tp, _Alloc>& __x,
  1519. const forward_list<_Tp, _Alloc>& __y)
  1520. {
  1521. return _VSTD::lexicographical_compare(__x.begin(), __x.end(),
  1522. __y.begin(), __y.end());
  1523. }
  1524. template <class _Tp, class _Alloc>
  1525. inline _LIBCPP_INLINE_VISIBILITY
  1526. bool operator> (const forward_list<_Tp, _Alloc>& __x,
  1527. const forward_list<_Tp, _Alloc>& __y)
  1528. {
  1529. return __y < __x;
  1530. }
  1531. template <class _Tp, class _Alloc>
  1532. inline _LIBCPP_INLINE_VISIBILITY
  1533. bool operator>=(const forward_list<_Tp, _Alloc>& __x,
  1534. const forward_list<_Tp, _Alloc>& __y)
  1535. {
  1536. return !(__x < __y);
  1537. }
  1538. template <class _Tp, class _Alloc>
  1539. inline _LIBCPP_INLINE_VISIBILITY
  1540. bool operator<=(const forward_list<_Tp, _Alloc>& __x,
  1541. const forward_list<_Tp, _Alloc>& __y)
  1542. {
  1543. return !(__y < __x);
  1544. }
  1545. template <class _Tp, class _Alloc>
  1546. inline _LIBCPP_INLINE_VISIBILITY
  1547. void
  1548. swap(forward_list<_Tp, _Alloc>& __x, forward_list<_Tp, _Alloc>& __y)
  1549. _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
  1550. {
  1551. __x.swap(__y);
  1552. }
  1553. #if _LIBCPP_STD_VER > 17
  1554. template <class _Tp, class _Allocator, class _Predicate>
  1555. inline _LIBCPP_INLINE_VISIBILITY
  1556. typename forward_list<_Tp, _Allocator>::size_type
  1557. erase_if(forward_list<_Tp, _Allocator>& __c, _Predicate __pred) {
  1558. return __c.remove_if(__pred);
  1559. }
  1560. template <class _Tp, class _Allocator, class _Up>
  1561. inline _LIBCPP_INLINE_VISIBILITY
  1562. typename forward_list<_Tp, _Allocator>::size_type
  1563. erase(forward_list<_Tp, _Allocator>& __c, const _Up& __v) {
  1564. return _VSTD::erase_if(__c, [&](auto& __elem) { return __elem == __v; });
  1565. }
  1566. #endif
  1567. _LIBCPP_END_NAMESPACE_STD
  1568. #if _LIBCPP_STD_VER > 14
  1569. _LIBCPP_BEGIN_NAMESPACE_STD
  1570. namespace pmr {
  1571. template <class _ValueT>
  1572. using forward_list = std::forward_list<_ValueT, polymorphic_allocator<_ValueT>>;
  1573. } // namespace pmr
  1574. _LIBCPP_END_NAMESPACE_STD
  1575. #endif
  1576. _LIBCPP_POP_MACROS
  1577. #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
  1578. # include <algorithm>
  1579. # include <atomic>
  1580. # include <concepts>
  1581. # include <functional>
  1582. # include <iosfwd>
  1583. # include <iterator>
  1584. # include <typeinfo>
  1585. #endif
  1586. #endif // _LIBCPP_FORWARD_LIST