forward_list 61 KB

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