forward_list 60 KB

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