forward_list 66 KB

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