forward_list 60 KB

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