forward_list 61 KB

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