forward_list 58 KB

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