blob: 8a87fc5e1f26adb3f2ec318b93ea6ca78d722d7e [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===----------------------- forward_list ---------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_FORWARD_LIST
12#define _LIBCPP_FORWARD_LIST
13
14/*
15 forward_list synopsis
16
17namespace std
18{
19
20template <class T, class Allocator = allocator<T>>
21class forward_list
22{
23public:
24 typedef T value_type;
25 typedef Allocator allocator_type;
26
27 typedef value_type& reference;
28 typedef const value_type& const_reference;
29 typedef typename allocator_traits<allocator_type>::pointer pointer;
30 typedef typename allocator_traits<allocator_type>::const_pointer const_pointer;
31 typedef typename allocator_traits<allocator_type>::size_type size_type;
32 typedef typename allocator_traits<allocator_type>::difference_type difference_type;
33
34 typedef <details> iterator;
35 typedef <details> const_iterator;
36
Howard Hinnantb965fed2011-06-03 16:20:53 +000037 forward_list()
38 noexcept(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000039 explicit forward_list(const allocator_type& a);
40 explicit forward_list(size_type n);
Marshall Clowe00f53b2013-09-09 18:19:45 +000041 explicit forward_list(size_type n, const allocator_type& a); // C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000042 forward_list(size_type n, const value_type& v);
43 forward_list(size_type n, const value_type& v, const allocator_type& a);
44 template <class InputIterator>
45 forward_list(InputIterator first, InputIterator last);
46 template <class InputIterator>
47 forward_list(InputIterator first, InputIterator last, const allocator_type& a);
48 forward_list(const forward_list& x);
49 forward_list(const forward_list& x, const allocator_type& a);
Howard Hinnantb965fed2011-06-03 16:20:53 +000050 forward_list(forward_list&& x)
51 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000052 forward_list(forward_list&& x, const allocator_type& a);
53 forward_list(initializer_list<value_type> il);
54 forward_list(initializer_list<value_type> il, const allocator_type& a);
55
56 ~forward_list();
57
58 forward_list& operator=(const forward_list& x);
Howard Hinnantb965fed2011-06-03 16:20:53 +000059 forward_list& operator=(forward_list&& x)
60 noexcept(
61 allocator_type::propagate_on_container_move_assignment::value &&
62 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000063 forward_list& operator=(initializer_list<value_type> il);
64
65 template <class InputIterator>
66 void assign(InputIterator first, InputIterator last);
67 void assign(size_type n, const value_type& v);
68 void assign(initializer_list<value_type> il);
69
Howard Hinnant8790cab2011-06-02 16:44:28 +000070 allocator_type get_allocator() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000071
Howard Hinnant8790cab2011-06-02 16:44:28 +000072 iterator begin() noexcept;
73 const_iterator begin() const noexcept;
74 iterator end() noexcept;
75 const_iterator end() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000076
Howard Hinnant8790cab2011-06-02 16:44:28 +000077 const_iterator cbegin() const noexcept;
78 const_iterator cend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000079
Howard Hinnant8790cab2011-06-02 16:44:28 +000080 iterator before_begin() noexcept;
81 const_iterator before_begin() const noexcept;
82 const_iterator cbefore_begin() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000083
Howard Hinnant8790cab2011-06-02 16:44:28 +000084 bool empty() const noexcept;
85 size_type max_size() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000086
87 reference front();
88 const_reference front() const;
89
90 template <class... Args> void emplace_front(Args&&... args);
91 void push_front(const value_type& v);
92 void push_front(value_type&& v);
93
94 void pop_front();
95
96 template <class... Args>
97 iterator emplace_after(const_iterator p, Args&&... args);
98 iterator insert_after(const_iterator p, const value_type& v);
99 iterator insert_after(const_iterator p, value_type&& v);
100 iterator insert_after(const_iterator p, size_type n, const value_type& v);
101 template <class InputIterator>
102 iterator insert_after(const_iterator p,
103 InputIterator first, InputIterator last);
104 iterator insert_after(const_iterator p, initializer_list<value_type> il);
105
Howard Hinnant7a2523b2010-08-21 20:58:44 +0000106 iterator erase_after(const_iterator p);
107 iterator erase_after(const_iterator first, const_iterator last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000108
Howard Hinnantb965fed2011-06-03 16:20:53 +0000109 void swap(forward_list& x)
Marshall Clow7d914d12015-07-13 20:04:56 +0000110 noexcept(allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000111
112 void resize(size_type n);
113 void resize(size_type n, const value_type& v);
Howard Hinnant8790cab2011-06-02 16:44:28 +0000114 void clear() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000115
Howard Hinnant99b2f762011-01-27 21:00:35 +0000116 void splice_after(const_iterator p, forward_list& x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000117 void splice_after(const_iterator p, forward_list&& x);
Howard Hinnant99b2f762011-01-27 21:00:35 +0000118 void splice_after(const_iterator p, forward_list& x, const_iterator i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000119 void splice_after(const_iterator p, forward_list&& x, const_iterator i);
Howard Hinnant99b2f762011-01-27 21:00:35 +0000120 void splice_after(const_iterator p, forward_list& x,
121 const_iterator first, const_iterator last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000122 void splice_after(const_iterator p, forward_list&& x,
123 const_iterator first, const_iterator last);
124 void remove(const value_type& v);
125 template <class Predicate> void remove_if(Predicate pred);
126 void unique();
127 template <class BinaryPredicate> void unique(BinaryPredicate binary_pred);
Howard Hinnant99b2f762011-01-27 21:00:35 +0000128 void merge(forward_list& x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000129 void merge(forward_list&& x);
Howard Hinnant99b2f762011-01-27 21:00:35 +0000130 template <class Compare> void merge(forward_list& x, Compare comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000131 template <class Compare> void merge(forward_list&& x, Compare comp);
132 void sort();
133 template <class Compare> void sort(Compare comp);
Howard Hinnant8790cab2011-06-02 16:44:28 +0000134 void reverse() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000135};
136
137template <class T, class Allocator>
138 bool operator==(const forward_list<T, Allocator>& x,
139 const forward_list<T, Allocator>& y);
140
141template <class T, class Allocator>
142 bool operator< (const forward_list<T, Allocator>& x,
143 const forward_list<T, Allocator>& y);
144
145template <class T, class Allocator>
146 bool operator!=(const forward_list<T, Allocator>& x,
147 const forward_list<T, Allocator>& y);
148
149template <class T, class Allocator>
150 bool operator> (const forward_list<T, Allocator>& x,
151 const forward_list<T, Allocator>& y);
152
153template <class T, class Allocator>
154 bool operator>=(const forward_list<T, Allocator>& x,
155 const forward_list<T, Allocator>& y);
156
157template <class T, class Allocator>
158 bool operator<=(const forward_list<T, Allocator>& x,
159 const forward_list<T, Allocator>& y);
160
161template <class T, class Allocator>
Howard Hinnantb965fed2011-06-03 16:20:53 +0000162 void swap(forward_list<T, Allocator>& x, forward_list<T, Allocator>& y)
Howard Hinnantc5607272011-06-03 17:30:28 +0000163 noexcept(noexcept(x.swap(y)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000164
165} // std
166
167*/
168
169#include <__config>
170
171#include <initializer_list>
172#include <memory>
173#include <limits>
174#include <iterator>
175#include <algorithm>
176
Howard Hinnant66c6f972011-11-29 16:45:27 +0000177#include <__undef_min_max>
178
Howard Hinnant08e17472011-10-17 20:05:10 +0000179#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000180#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000181#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000182
183_LIBCPP_BEGIN_NAMESPACE_STD
184
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000185template <class _Tp, class _VoidPtr> struct __forward_list_node;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000186
187template <class _NodePtr>
188struct __forward_begin_node
189{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000190 typedef _NodePtr pointer;
191
192 pointer __next_;
193
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700194 _LIBCPP_INLINE_VISIBILITY __forward_begin_node() : __next_(nullptr) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000195};
196
197template <class _Tp, class _VoidPtr>
Peter Collingbournea3dc8f32014-02-05 01:44:17 +0000198struct _LIBCPP_HIDDEN __begin_node_of
199{
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700200 typedef __forward_begin_node
201 <
202 typename pointer_traits<_VoidPtr>::template
203#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
204 rebind<__forward_list_node<_Tp, _VoidPtr> >
205#else
206 rebind<__forward_list_node<_Tp, _VoidPtr> >::other
207#endif
208 > type;
Peter Collingbournea3dc8f32014-02-05 01:44:17 +0000209};
210
211template <class _Tp, class _VoidPtr>
212struct __forward_list_node
213 : public __begin_node_of<_Tp, _VoidPtr>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000214{
215 typedef _Tp value_type;
216
217 value_type __value_;
218};
219
Marshall Clowceead9c2015-02-18 17:24:08 +0000220template <class _Tp, class _Alloc = allocator<_Tp> > class _LIBCPP_TYPE_VIS_ONLY forward_list;
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000221template<class _NodeConstPtr> class _LIBCPP_TYPE_VIS_ONLY __forward_list_const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000222
223template <class _NodePtr>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000224class _LIBCPP_TYPE_VIS_ONLY __forward_list_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000225{
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700226 typedef _NodePtr __node_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000227
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700228 __node_pointer __ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000229
Howard Hinnant42a63a72010-09-21 22:55:27 +0000230 _LIBCPP_INLINE_VISIBILITY
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700231 explicit __forward_list_iterator(__node_pointer __p) _NOEXCEPT : __ptr_(__p) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000232
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000233 template<class, class> friend class _LIBCPP_TYPE_VIS_ONLY forward_list;
234 template<class> friend class _LIBCPP_TYPE_VIS_ONLY __forward_list_const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000235
236public:
237 typedef forward_iterator_tag iterator_category;
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700238 typedef typename pointer_traits<__node_pointer>::element_type::value_type
239 value_type;
Howard Hinnant81381a92013-06-24 17:17:28 +0000240 typedef value_type& reference;
Howard Hinnant324bb032010-08-22 00:02:43 +0000241 typedef typename pointer_traits<__node_pointer>::difference_type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000242 difference_type;
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700243 typedef typename pointer_traits<__node_pointer>::template
244#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
245 rebind<value_type>
246#else
247 rebind<value_type>::other
248#endif
249 pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000250
Howard Hinnant42a63a72010-09-21 22:55:27 +0000251 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8790cab2011-06-02 16:44:28 +0000252 __forward_list_iterator() _NOEXCEPT : __ptr_(nullptr) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000253
Howard Hinnant42a63a72010-09-21 22:55:27 +0000254 _LIBCPP_INLINE_VISIBILITY
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700255 reference operator*() const {return __ptr_->__value_;}
Howard Hinnant42a63a72010-09-21 22:55:27 +0000256 _LIBCPP_INLINE_VISIBILITY
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700257 pointer operator->() const {return pointer_traits<pointer>::pointer_to(__ptr_->__value_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000258
Howard Hinnant42a63a72010-09-21 22:55:27 +0000259 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000260 __forward_list_iterator& operator++()
261 {
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700262 __ptr_ = __ptr_->__next_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000263 return *this;
264 }
Howard Hinnant42a63a72010-09-21 22:55:27 +0000265 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000266 __forward_list_iterator operator++(int)
267 {
268 __forward_list_iterator __t(*this);
269 ++(*this);
270 return __t;
271 }
272
Howard Hinnant42a63a72010-09-21 22:55:27 +0000273 friend _LIBCPP_INLINE_VISIBILITY
274 bool operator==(const __forward_list_iterator& __x,
275 const __forward_list_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000276 {return __x.__ptr_ == __y.__ptr_;}
Howard Hinnant42a63a72010-09-21 22:55:27 +0000277 friend _LIBCPP_INLINE_VISIBILITY
278 bool operator!=(const __forward_list_iterator& __x,
279 const __forward_list_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000280 {return !(__x == __y);}
281};
282
283template <class _NodeConstPtr>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000284class _LIBCPP_TYPE_VIS_ONLY __forward_list_const_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000285{
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700286 typedef _NodeConstPtr __node_const_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000287
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700288 __node_const_pointer __ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000289
Howard Hinnant42a63a72010-09-21 22:55:27 +0000290 _LIBCPP_INLINE_VISIBILITY
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700291 explicit __forward_list_const_iterator(__node_const_pointer __p) _NOEXCEPT
292 : __ptr_(__p) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000293
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700294 typedef typename remove_const
295 <
296 typename pointer_traits<__node_const_pointer>::element_type
297 >::type __node;
298 typedef typename pointer_traits<__node_const_pointer>::template
299#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
300 rebind<__node>
301#else
302 rebind<__node>::other
303#endif
304 __node_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000305
306 template<class, class> friend class forward_list;
307
308public:
309 typedef forward_iterator_tag iterator_category;
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700310 typedef typename __node::value_type value_type;
Howard Hinnant81381a92013-06-24 17:17:28 +0000311 typedef const value_type& reference;
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700312 typedef typename pointer_traits<__node_const_pointer>::difference_type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000313 difference_type;
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700314 typedef typename pointer_traits<__node_const_pointer>::template
315#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
316 rebind<const value_type>
317#else
318 rebind<const value_type>::other
319#endif
Eric Fiselierde637db2016-01-27 00:11:54 +0000320 pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000321
Howard Hinnant42a63a72010-09-21 22:55:27 +0000322 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8790cab2011-06-02 16:44:28 +0000323 __forward_list_const_iterator() _NOEXCEPT : __ptr_(nullptr) {}
Howard Hinnant42a63a72010-09-21 22:55:27 +0000324 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8790cab2011-06-02 16:44:28 +0000325 __forward_list_const_iterator(__forward_list_iterator<__node_pointer> __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000326 : __ptr_(__p.__ptr_) {}
327
Howard Hinnant42a63a72010-09-21 22:55:27 +0000328 _LIBCPP_INLINE_VISIBILITY
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700329 reference operator*() const {return __ptr_->__value_;}
Howard Hinnant42a63a72010-09-21 22:55:27 +0000330 _LIBCPP_INLINE_VISIBILITY
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700331 pointer operator->() const {return pointer_traits<pointer>::pointer_to(__ptr_->__value_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000332
Howard Hinnant42a63a72010-09-21 22:55:27 +0000333 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000334 __forward_list_const_iterator& operator++()
335 {
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700336 __ptr_ = __ptr_->__next_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000337 return *this;
338 }
Howard Hinnant42a63a72010-09-21 22:55:27 +0000339 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000340 __forward_list_const_iterator operator++(int)
341 {
342 __forward_list_const_iterator __t(*this);
343 ++(*this);
344 return __t;
345 }
346
Howard Hinnant42a63a72010-09-21 22:55:27 +0000347 friend _LIBCPP_INLINE_VISIBILITY
348 bool operator==(const __forward_list_const_iterator& __x,
349 const __forward_list_const_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000350 {return __x.__ptr_ == __y.__ptr_;}
Howard Hinnant42a63a72010-09-21 22:55:27 +0000351 friend _LIBCPP_INLINE_VISIBILITY
352 bool operator!=(const __forward_list_const_iterator& __x,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000353 const __forward_list_const_iterator& __y)
354 {return !(__x == __y);}
355};
356
357template <class _Tp, class _Alloc>
358class __forward_list_base
359{
360protected:
361 typedef _Tp value_type;
362 typedef _Alloc allocator_type;
363
Peter Collingbournea3dc8f32014-02-05 01:44:17 +0000364 typedef typename allocator_traits<allocator_type>::void_pointer void_pointer;
365 typedef __forward_list_node<value_type, void_pointer> __node;
366 typedef typename __begin_node_of<value_type, void_pointer>::type __begin_node;
Marshall Clow66302c62015-04-07 05:21:38 +0000367 typedef typename __rebind_alloc_helper<allocator_traits<allocator_type>, __node>::type __node_allocator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000368 typedef allocator_traits<__node_allocator> __node_traits;
369 typedef typename __node_traits::pointer __node_pointer;
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700370 typedef typename __node_traits::pointer __node_const_pointer;
Howard Hinnant81381a92013-06-24 17:17:28 +0000371
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700372 typedef typename __rebind_alloc_helper<allocator_traits<allocator_type>, __begin_node>::type __begin_node_allocator;
373 typedef typename allocator_traits<__begin_node_allocator>::pointer __begin_node_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000374
375 __compressed_pair<__begin_node, __node_allocator> __before_begin_;
376
Howard Hinnant42a63a72010-09-21 22:55:27 +0000377 _LIBCPP_INLINE_VISIBILITY
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700378 __node_pointer __before_begin() _NOEXCEPT
379 {return static_cast<__node_pointer>(pointer_traits<__begin_node_pointer>::
380 pointer_to(__before_begin_.first()));}
Howard Hinnant42a63a72010-09-21 22:55:27 +0000381 _LIBCPP_INLINE_VISIBILITY
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700382 __node_const_pointer __before_begin() const _NOEXCEPT
383 {return static_cast<__node_const_pointer>(pointer_traits<__begin_node_pointer>::
384 pointer_to(const_cast<__begin_node&>(__before_begin_.first())));}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000385
Howard Hinnant42a63a72010-09-21 22:55:27 +0000386 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb965fed2011-06-03 16:20:53 +0000387 __node_allocator& __alloc() _NOEXCEPT
388 {return __before_begin_.second();}
Howard Hinnant42a63a72010-09-21 22:55:27 +0000389 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8790cab2011-06-02 16:44:28 +0000390 const __node_allocator& __alloc() const _NOEXCEPT
391 {return __before_begin_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000392
393 typedef __forward_list_iterator<__node_pointer> iterator;
Howard Hinnant81381a92013-06-24 17:17:28 +0000394 typedef __forward_list_const_iterator<__node_pointer> const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000395
Howard Hinnant42a63a72010-09-21 22:55:27 +0000396 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000397 __forward_list_base()
Howard Hinnantb965fed2011-06-03 16:20:53 +0000398 _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000399 : __before_begin_(__begin_node()) {}
Howard Hinnant42a63a72010-09-21 22:55:27 +0000400 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000401 __forward_list_base(const allocator_type& __a)
402 : __before_begin_(__begin_node(), __node_allocator(__a)) {}
403
Howard Hinnant73d21a42010-09-04 23:28:19 +0000404#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb965fed2011-06-03 16:20:53 +0000405public:
406 __forward_list_base(__forward_list_base&& __x)
407 _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000408 __forward_list_base(__forward_list_base&& __x, const allocator_type& __a);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000409#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000410
411private:
412 __forward_list_base(const __forward_list_base&);
413 __forward_list_base& operator=(const __forward_list_base&);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000414
Howard Hinnantb965fed2011-06-03 16:20:53 +0000415public:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000416 ~__forward_list_base();
417
Howard Hinnantb965fed2011-06-03 16:20:53 +0000418protected:
Howard Hinnant42a63a72010-09-21 22:55:27 +0000419 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000420 void __copy_assign_alloc(const __forward_list_base& __x)
421 {__copy_assign_alloc(__x, integral_constant<bool,
422 __node_traits::propagate_on_container_copy_assignment::value>());}
423
Howard Hinnant42a63a72010-09-21 22:55:27 +0000424 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000425 void __move_assign_alloc(__forward_list_base& __x)
Howard Hinnantb965fed2011-06-03 16:20:53 +0000426 _NOEXCEPT_(!__node_traits::propagate_on_container_move_assignment::value ||
427 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000428 {__move_assign_alloc(__x, integral_constant<bool,
429 __node_traits::propagate_on_container_move_assignment::value>());}
430
Howard Hinnantb965fed2011-06-03 16:20:53 +0000431public:
432 void swap(__forward_list_base& __x)
Marshall Clow7d914d12015-07-13 20:04:56 +0000433#if _LIBCPP_STD_VER >= 14
434 _NOEXCEPT;
435#else
436 _NOEXCEPT_(!__node_traits::propagate_on_container_move_assignment::value ||
437 __is_nothrow_swappable<__node_allocator>::value);
438#endif
Howard Hinnantb965fed2011-06-03 16:20:53 +0000439protected:
Howard Hinnant8790cab2011-06-02 16:44:28 +0000440 void clear() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000441
442private:
Howard Hinnant42a63a72010-09-21 22:55:27 +0000443 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000444 void __copy_assign_alloc(const __forward_list_base&, false_type) {}
Howard Hinnant42a63a72010-09-21 22:55:27 +0000445 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000446 void __copy_assign_alloc(const __forward_list_base& __x, true_type)
447 {
448 if (__alloc() != __x.__alloc())
449 clear();
450 __alloc() = __x.__alloc();
451 }
452
Howard Hinnant42a63a72010-09-21 22:55:27 +0000453 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb965fed2011-06-03 16:20:53 +0000454 void __move_assign_alloc(__forward_list_base& __x, false_type) _NOEXCEPT
455 {}
Howard Hinnant42a63a72010-09-21 22:55:27 +0000456 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000457 void __move_assign_alloc(__forward_list_base& __x, true_type)
Howard Hinnantb965fed2011-06-03 16:20:53 +0000458 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000459 {__alloc() = _VSTD::move(__x.__alloc());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000460};
461
Howard Hinnant73d21a42010-09-04 23:28:19 +0000462#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000463
464template <class _Tp, class _Alloc>
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700465inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000466__forward_list_base<_Tp, _Alloc>::__forward_list_base(__forward_list_base&& __x)
Howard Hinnantb965fed2011-06-03 16:20:53 +0000467 _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000468 : __before_begin_(_VSTD::move(__x.__before_begin_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000469{
470 __x.__before_begin()->__next_ = nullptr;
471}
472
473template <class _Tp, class _Alloc>
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700474inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000475__forward_list_base<_Tp, _Alloc>::__forward_list_base(__forward_list_base&& __x,
476 const allocator_type& __a)
477 : __before_begin_(__begin_node(), __node_allocator(__a))
478{
479 if (__alloc() == __x.__alloc())
480 {
481 __before_begin()->__next_ = __x.__before_begin()->__next_;
482 __x.__before_begin()->__next_ = nullptr;
483 }
484}
485
Howard Hinnant73d21a42010-09-04 23:28:19 +0000486#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000487
488template <class _Tp, class _Alloc>
489__forward_list_base<_Tp, _Alloc>::~__forward_list_base()
490{
491 clear();
492}
493
494template <class _Tp, class _Alloc>
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700495inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000496void
497__forward_list_base<_Tp, _Alloc>::swap(__forward_list_base& __x)
Marshall Clow7d914d12015-07-13 20:04:56 +0000498#if _LIBCPP_STD_VER >= 14
499 _NOEXCEPT
500#else
501 _NOEXCEPT_(!__node_traits::propagate_on_container_move_assignment::value ||
502 __is_nothrow_swappable<__node_allocator>::value)
503#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000504{
Marshall Clow7d914d12015-07-13 20:04:56 +0000505 __swap_allocator(__alloc(), __x.__alloc(),
506 integral_constant<bool, __node_traits::propagate_on_container_swap::value>());
Howard Hinnant0949eed2011-06-30 21:18:19 +0000507 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000508 swap(__before_begin()->__next_, __x.__before_begin()->__next_);
509}
510
511template <class _Tp, class _Alloc>
512void
Howard Hinnant8790cab2011-06-02 16:44:28 +0000513__forward_list_base<_Tp, _Alloc>::clear() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000514{
515 __node_allocator& __a = __alloc();
516 for (__node_pointer __p = __before_begin()->__next_; __p != nullptr;)
517 {
518 __node_pointer __next = __p->__next_;
Howard Hinnant0949eed2011-06-30 21:18:19 +0000519 __node_traits::destroy(__a, _VSTD::addressof(__p->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000520 __node_traits::deallocate(__a, __p, 1);
521 __p = __next;
522 }
523 __before_begin()->__next_ = nullptr;
524}
525
Marshall Clowceead9c2015-02-18 17:24:08 +0000526template <class _Tp, class _Alloc /*= allocator<_Tp>*/>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000527class _LIBCPP_TYPE_VIS_ONLY forward_list
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000528 : private __forward_list_base<_Tp, _Alloc>
529{
530 typedef __forward_list_base<_Tp, _Alloc> base;
Howard Hinnantb965fed2011-06-03 16:20:53 +0000531 typedef typename base::__node_allocator __node_allocator;
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700532 typedef typename base::__node __node;
533 typedef typename base::__node_traits __node_traits;
534 typedef typename base::__node_pointer __node_pointer;
Howard Hinnantb965fed2011-06-03 16:20:53 +0000535
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000536public:
537 typedef _Tp value_type;
538 typedef _Alloc allocator_type;
539
540 typedef value_type& reference;
541 typedef const value_type& const_reference;
542 typedef typename allocator_traits<allocator_type>::pointer pointer;
543 typedef typename allocator_traits<allocator_type>::const_pointer const_pointer;
544 typedef typename allocator_traits<allocator_type>::size_type size_type;
545 typedef typename allocator_traits<allocator_type>::difference_type difference_type;
546
547 typedef typename base::iterator iterator;
548 typedef typename base::const_iterator const_iterator;
549
Howard Hinnantb965fed2011-06-03 16:20:53 +0000550 _LIBCPP_INLINE_VISIBILITY
551 forward_list()
552 _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value)
553 {} // = default;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000554 explicit forward_list(const allocator_type& __a);
555 explicit forward_list(size_type __n);
Marshall Clow955f2c82013-09-08 19:11:51 +0000556#if _LIBCPP_STD_VER > 11
557 explicit forward_list(size_type __n, const allocator_type& __a);
558#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000559 forward_list(size_type __n, const value_type& __v);
560 forward_list(size_type __n, const value_type& __v, const allocator_type& __a);
561 template <class _InputIterator>
562 forward_list(_InputIterator __f, _InputIterator __l,
563 typename enable_if<
564 __is_input_iterator<_InputIterator>::value
565 >::type* = nullptr);
566 template <class _InputIterator>
567 forward_list(_InputIterator __f, _InputIterator __l,
568 const allocator_type& __a,
569 typename enable_if<
570 __is_input_iterator<_InputIterator>::value
571 >::type* = nullptr);
572 forward_list(const forward_list& __x);
573 forward_list(const forward_list& __x, const allocator_type& __a);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000574#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant42a63a72010-09-21 22:55:27 +0000575 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb965fed2011-06-03 16:20:53 +0000576 forward_list(forward_list&& __x)
577 _NOEXCEPT_(is_nothrow_move_constructible<base>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000578 : base(_VSTD::move(__x)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000579 forward_list(forward_list&& __x, const allocator_type& __a);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000580#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnante3e32912011-08-12 21:56:02 +0000581#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000582 forward_list(initializer_list<value_type> __il);
583 forward_list(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnante3e32912011-08-12 21:56:02 +0000584#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000585
586 // ~forward_list() = default;
587
588 forward_list& operator=(const forward_list& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000589#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb965fed2011-06-03 16:20:53 +0000590 forward_list& operator=(forward_list&& __x)
591 _NOEXCEPT_(
592 __node_traits::propagate_on_container_move_assignment::value &&
593 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000594#endif
Howard Hinnante3e32912011-08-12 21:56:02 +0000595#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000596 forward_list& operator=(initializer_list<value_type> __il);
Howard Hinnante3e32912011-08-12 21:56:02 +0000597#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000598
599 template <class _InputIterator>
600 typename enable_if
601 <
602 __is_input_iterator<_InputIterator>::value,
603 void
604 >::type
605 assign(_InputIterator __f, _InputIterator __l);
606 void assign(size_type __n, const value_type& __v);
Howard Hinnante3e32912011-08-12 21:56:02 +0000607#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000608 void assign(initializer_list<value_type> __il);
Howard Hinnante3e32912011-08-12 21:56:02 +0000609#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000610
Howard Hinnant42a63a72010-09-21 22:55:27 +0000611 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8790cab2011-06-02 16:44:28 +0000612 allocator_type get_allocator() const _NOEXCEPT
613 {return allocator_type(base::__alloc());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000614
Howard Hinnant42a63a72010-09-21 22:55:27 +0000615 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8790cab2011-06-02 16:44:28 +0000616 iterator begin() _NOEXCEPT
617 {return iterator(base::__before_begin()->__next_);}
Howard Hinnant42a63a72010-09-21 22:55:27 +0000618 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8790cab2011-06-02 16:44:28 +0000619 const_iterator begin() const _NOEXCEPT
620 {return const_iterator(base::__before_begin()->__next_);}
Howard Hinnant42a63a72010-09-21 22:55:27 +0000621 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8790cab2011-06-02 16:44:28 +0000622 iterator end() _NOEXCEPT
623 {return iterator(nullptr);}
Howard Hinnant42a63a72010-09-21 22:55:27 +0000624 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8790cab2011-06-02 16:44:28 +0000625 const_iterator end() const _NOEXCEPT
626 {return const_iterator(nullptr);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000627
Howard Hinnant42a63a72010-09-21 22:55:27 +0000628 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8790cab2011-06-02 16:44:28 +0000629 const_iterator cbegin() const _NOEXCEPT
630 {return const_iterator(base::__before_begin()->__next_);}
Howard Hinnant42a63a72010-09-21 22:55:27 +0000631 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8790cab2011-06-02 16:44:28 +0000632 const_iterator cend() const _NOEXCEPT
633 {return const_iterator(nullptr);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000634
Howard Hinnant42a63a72010-09-21 22:55:27 +0000635 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8790cab2011-06-02 16:44:28 +0000636 iterator before_begin() _NOEXCEPT
637 {return iterator(base::__before_begin());}
Howard Hinnant42a63a72010-09-21 22:55:27 +0000638 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8790cab2011-06-02 16:44:28 +0000639 const_iterator before_begin() const _NOEXCEPT
640 {return const_iterator(base::__before_begin());}
Howard Hinnant42a63a72010-09-21 22:55:27 +0000641 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8790cab2011-06-02 16:44:28 +0000642 const_iterator cbefore_begin() const _NOEXCEPT
643 {return const_iterator(base::__before_begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000644
Howard Hinnant42a63a72010-09-21 22:55:27 +0000645 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8790cab2011-06-02 16:44:28 +0000646 bool empty() const _NOEXCEPT
647 {return base::__before_begin()->__next_ == nullptr;}
Howard Hinnant42a63a72010-09-21 22:55:27 +0000648 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8790cab2011-06-02 16:44:28 +0000649 size_type max_size() const _NOEXCEPT
650 {return numeric_limits<size_type>::max();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000651
Howard Hinnant42a63a72010-09-21 22:55:27 +0000652 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000653 reference front() {return base::__before_begin()->__next_->__value_;}
Howard Hinnant42a63a72010-09-21 22:55:27 +0000654 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000655 const_reference front() const {return base::__before_begin()->__next_->__value_;}
656
Howard Hinnant73d21a42010-09-04 23:28:19 +0000657#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
658#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000659 template <class... _Args> void emplace_front(_Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000660#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000661 void push_front(value_type&& __v);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000662#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000663 void push_front(const value_type& __v);
664
665 void pop_front();
666
Howard Hinnant73d21a42010-09-04 23:28:19 +0000667#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
668#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000669 template <class... _Args>
670 iterator emplace_after(const_iterator __p, _Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000671#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000672 iterator insert_after(const_iterator __p, value_type&& __v);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000673#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000674 iterator insert_after(const_iterator __p, const value_type& __v);
675 iterator insert_after(const_iterator __p, size_type __n, const value_type& __v);
676 template <class _InputIterator>
Howard Hinnant42a63a72010-09-21 22:55:27 +0000677 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000678 typename enable_if
679 <
680 __is_input_iterator<_InputIterator>::value,
681 iterator
682 >::type
683 insert_after(const_iterator __p, _InputIterator __f, _InputIterator __l);
Howard Hinnante3e32912011-08-12 21:56:02 +0000684#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000685 iterator insert_after(const_iterator __p, initializer_list<value_type> __il)
686 {return insert_after(__p, __il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +0000687#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000688
Howard Hinnant7a2523b2010-08-21 20:58:44 +0000689 iterator erase_after(const_iterator __p);
690 iterator erase_after(const_iterator __f, const_iterator __l);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000691
Howard Hinnant42a63a72010-09-21 22:55:27 +0000692 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb965fed2011-06-03 16:20:53 +0000693 void swap(forward_list& __x)
Marshall Clow7d914d12015-07-13 20:04:56 +0000694#if _LIBCPP_STD_VER >= 14
695 _NOEXCEPT
696#else
Howard Hinnantb965fed2011-06-03 16:20:53 +0000697 _NOEXCEPT_(!__node_traits::propagate_on_container_swap::value ||
698 __is_nothrow_swappable<__node_allocator>::value)
Marshall Clow7d914d12015-07-13 20:04:56 +0000699#endif
Howard Hinnantb965fed2011-06-03 16:20:53 +0000700 {base::swap(__x);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000701
702 void resize(size_type __n);
703 void resize(size_type __n, const value_type& __v);
Howard Hinnant42a63a72010-09-21 22:55:27 +0000704 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8790cab2011-06-02 16:44:28 +0000705 void clear() _NOEXCEPT {base::clear();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000706
Howard Hinnant73d21a42010-09-04 23:28:19 +0000707#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99b2f762011-01-27 21:00:35 +0000708 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000709 void splice_after(const_iterator __p, forward_list&& __x);
Howard Hinnant99b2f762011-01-27 21:00:35 +0000710 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000711 void splice_after(const_iterator __p, forward_list&& __x, const_iterator __i);
Howard Hinnant99b2f762011-01-27 21:00:35 +0000712 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000713 void splice_after(const_iterator __p, forward_list&& __x,
714 const_iterator __f, const_iterator __l);
Howard Hinnant99b2f762011-01-27 21:00:35 +0000715#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000716 void splice_after(const_iterator __p, forward_list& __x);
717 void splice_after(const_iterator __p, forward_list& __x, const_iterator __i);
718 void splice_after(const_iterator __p, forward_list& __x,
719 const_iterator __f, const_iterator __l);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000720 void remove(const value_type& __v);
721 template <class _Predicate> void remove_if(_Predicate __pred);
Howard Hinnant42a63a72010-09-21 22:55:27 +0000722 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000723 void unique() {unique(__equal_to<value_type>());}
724 template <class _BinaryPredicate> void unique(_BinaryPredicate __binary_pred);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000725#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant42a63a72010-09-21 22:55:27 +0000726 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99b2f762011-01-27 21:00:35 +0000727 void merge(forward_list&& __x) {merge(__x, __less<value_type>());}
728 template <class _Compare>
729 _LIBCPP_INLINE_VISIBILITY
730 void merge(forward_list&& __x, _Compare __comp)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000731 {merge(__x, _VSTD::move(__comp));}
Howard Hinnant99b2f762011-01-27 21:00:35 +0000732#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant42a63a72010-09-21 22:55:27 +0000733 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000734 void merge(forward_list& __x) {merge(__x, __less<value_type>());}
735 template <class _Compare> void merge(forward_list& __x, _Compare __comp);
Howard Hinnant42a63a72010-09-21 22:55:27 +0000736 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000737 void sort() {sort(__less<value_type>());}
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700738 template <class _Compare> void sort(_Compare __comp);
Howard Hinnant8790cab2011-06-02 16:44:28 +0000739 void reverse() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000740
741private:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000742
Howard Hinnant73d21a42010-09-04 23:28:19 +0000743#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb965fed2011-06-03 16:20:53 +0000744 void __move_assign(forward_list& __x, true_type)
745 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000746 void __move_assign(forward_list& __x, false_type);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000747#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000748
749 template <class _Compare>
750 static
751 __node_pointer
752 __merge(__node_pointer __f1, __node_pointer __f2, _Compare& __comp);
753
754 template <class _Compare>
755 static
756 __node_pointer
757 __sort(__node_pointer __f, difference_type __sz, _Compare& __comp);
758};
759
760template <class _Tp, class _Alloc>
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700761inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000762forward_list<_Tp, _Alloc>::forward_list(const allocator_type& __a)
763 : base(__a)
764{
765}
766
767template <class _Tp, class _Alloc>
768forward_list<_Tp, _Alloc>::forward_list(size_type __n)
769{
770 if (__n > 0)
771 {
772 __node_allocator& __a = base::__alloc();
Howard Hinnant99968442011-11-29 18:15:50 +0000773 typedef __allocator_destructor<__node_allocator> _Dp;
774 unique_ptr<__node, _Dp> __h(nullptr, _Dp(__a, 1));
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700775 for (__node_pointer __p = base::__before_begin(); __n > 0; --__n,
776 __p = __p->__next_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000777 {
778 __h.reset(__node_traits::allocate(__a, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +0000779 __node_traits::construct(__a, _VSTD::addressof(__h->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000780 __h->__next_ = nullptr;
781 __p->__next_ = __h.release();
782 }
783 }
784}
785
Marshall Clow955f2c82013-09-08 19:11:51 +0000786#if _LIBCPP_STD_VER > 11
787template <class _Tp, class _Alloc>
788forward_list<_Tp, _Alloc>::forward_list(size_type __n, const allocator_type& __a)
789 : base ( __a )
790{
791 if (__n > 0)
792 {
793 __node_allocator& __a = base::__alloc();
794 typedef __allocator_destructor<__node_allocator> _Dp;
795 unique_ptr<__node, _Dp> __h(nullptr, _Dp(__a, 1));
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700796 for (__node_pointer __p = base::__before_begin(); __n > 0; --__n,
797 __p = __p->__next_)
Marshall Clow955f2c82013-09-08 19:11:51 +0000798 {
799 __h.reset(__node_traits::allocate(__a, 1));
800 __node_traits::construct(__a, _VSTD::addressof(__h->__value_));
801 __h->__next_ = nullptr;
802 __p->__next_ = __h.release();
803 }
804 }
805}
806#endif
807
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000808template <class _Tp, class _Alloc>
809forward_list<_Tp, _Alloc>::forward_list(size_type __n, const value_type& __v)
810{
811 insert_after(cbefore_begin(), __n, __v);
812}
813
814template <class _Tp, class _Alloc>
815forward_list<_Tp, _Alloc>::forward_list(size_type __n, const value_type& __v,
816 const allocator_type& __a)
817 : base(__a)
818{
819 insert_after(cbefore_begin(), __n, __v);
820}
821
822template <class _Tp, class _Alloc>
823template <class _InputIterator>
824forward_list<_Tp, _Alloc>::forward_list(_InputIterator __f, _InputIterator __l,
825 typename enable_if<
826 __is_input_iterator<_InputIterator>::value
827 >::type*)
828{
829 insert_after(cbefore_begin(), __f, __l);
830}
831
832template <class _Tp, class _Alloc>
833template <class _InputIterator>
834forward_list<_Tp, _Alloc>::forward_list(_InputIterator __f, _InputIterator __l,
835 const allocator_type& __a,
836 typename enable_if<
837 __is_input_iterator<_InputIterator>::value
838 >::type*)
839 : base(__a)
840{
841 insert_after(cbefore_begin(), __f, __l);
842}
843
844template <class _Tp, class _Alloc>
845forward_list<_Tp, _Alloc>::forward_list(const forward_list& __x)
846 : base(allocator_type(
847 __node_traits::select_on_container_copy_construction(__x.__alloc())
848 )
849 )
850{
851 insert_after(cbefore_begin(), __x.begin(), __x.end());
852}
853
854template <class _Tp, class _Alloc>
855forward_list<_Tp, _Alloc>::forward_list(const forward_list& __x,
856 const allocator_type& __a)
857 : base(__a)
858{
859 insert_after(cbefore_begin(), __x.begin(), __x.end());
860}
861
Howard Hinnant73d21a42010-09-04 23:28:19 +0000862#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000863
864template <class _Tp, class _Alloc>
865forward_list<_Tp, _Alloc>::forward_list(forward_list&& __x,
866 const allocator_type& __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000867 : base(_VSTD::move(__x), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000868{
869 if (base::__alloc() != __x.__alloc())
870 {
Howard Hinnant99968442011-11-29 18:15:50 +0000871 typedef move_iterator<iterator> _Ip;
872 insert_after(cbefore_begin(), _Ip(__x.begin()), _Ip(__x.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000873 }
874}
875
Howard Hinnant73d21a42010-09-04 23:28:19 +0000876#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000877
Howard Hinnante3e32912011-08-12 21:56:02 +0000878#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
879
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000880template <class _Tp, class _Alloc>
881forward_list<_Tp, _Alloc>::forward_list(initializer_list<value_type> __il)
882{
883 insert_after(cbefore_begin(), __il.begin(), __il.end());
884}
885
886template <class _Tp, class _Alloc>
887forward_list<_Tp, _Alloc>::forward_list(initializer_list<value_type> __il,
888 const allocator_type& __a)
889 : base(__a)
890{
891 insert_after(cbefore_begin(), __il.begin(), __il.end());
892}
893
Howard Hinnante3e32912011-08-12 21:56:02 +0000894#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
895
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000896template <class _Tp, class _Alloc>
897forward_list<_Tp, _Alloc>&
898forward_list<_Tp, _Alloc>::operator=(const forward_list& __x)
899{
900 if (this != &__x)
901 {
902 base::__copy_assign_alloc(__x);
903 assign(__x.begin(), __x.end());
904 }
905 return *this;
906}
907
Howard Hinnant73d21a42010-09-04 23:28:19 +0000908#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000909
910template <class _Tp, class _Alloc>
911void
912forward_list<_Tp, _Alloc>::__move_assign(forward_list& __x, true_type)
Howard Hinnantb965fed2011-06-03 16:20:53 +0000913 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000914{
915 clear();
916 base::__move_assign_alloc(__x);
917 base::__before_begin()->__next_ = __x.__before_begin()->__next_;
918 __x.__before_begin()->__next_ = nullptr;
919}
920
921template <class _Tp, class _Alloc>
922void
923forward_list<_Tp, _Alloc>::__move_assign(forward_list& __x, false_type)
924{
925 if (base::__alloc() == __x.__alloc())
926 __move_assign(__x, true_type());
927 else
928 {
Howard Hinnant99968442011-11-29 18:15:50 +0000929 typedef move_iterator<iterator> _Ip;
930 assign(_Ip(__x.begin()), _Ip(__x.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000931 }
932}
933
934template <class _Tp, class _Alloc>
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700935inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000936forward_list<_Tp, _Alloc>&
937forward_list<_Tp, _Alloc>::operator=(forward_list&& __x)
Howard Hinnantb965fed2011-06-03 16:20:53 +0000938 _NOEXCEPT_(
939 __node_traits::propagate_on_container_move_assignment::value &&
940 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000941{
942 __move_assign(__x, integral_constant<bool,
943 __node_traits::propagate_on_container_move_assignment::value>());
944 return *this;
945}
946
Howard Hinnant73d21a42010-09-04 23:28:19 +0000947#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000948
Howard Hinnante3e32912011-08-12 21:56:02 +0000949#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
950
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000951template <class _Tp, class _Alloc>
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700952inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000953forward_list<_Tp, _Alloc>&
954forward_list<_Tp, _Alloc>::operator=(initializer_list<value_type> __il)
955{
956 assign(__il.begin(), __il.end());
957 return *this;
958}
959
Howard Hinnante3e32912011-08-12 21:56:02 +0000960#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
961
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000962template <class _Tp, class _Alloc>
963template <class _InputIterator>
964typename enable_if
965<
966 __is_input_iterator<_InputIterator>::value,
967 void
968>::type
969forward_list<_Tp, _Alloc>::assign(_InputIterator __f, _InputIterator __l)
970{
971 iterator __i = before_begin();
Howard Hinnant0949eed2011-06-30 21:18:19 +0000972 iterator __j = _VSTD::next(__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000973 iterator __e = end();
Eric Fiselierb9919752014-10-27 19:28:20 +0000974 for (; __j != __e && __f != __l; ++__i, (void) ++__j, ++__f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000975 *__j = *__f;
976 if (__j == __e)
977 insert_after(__i, __f, __l);
978 else
979 erase_after(__i, __e);
980}
981
982template <class _Tp, class _Alloc>
983void
984forward_list<_Tp, _Alloc>::assign(size_type __n, const value_type& __v)
985{
986 iterator __i = before_begin();
Howard Hinnant0949eed2011-06-30 21:18:19 +0000987 iterator __j = _VSTD::next(__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000988 iterator __e = end();
989 for (; __j != __e && __n > 0; --__n, ++__i, ++__j)
990 *__j = __v;
991 if (__j == __e)
992 insert_after(__i, __n, __v);
993 else
994 erase_after(__i, __e);
995}
996
Howard Hinnante3e32912011-08-12 21:56:02 +0000997#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
998
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000999template <class _Tp, class _Alloc>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001000inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001001void
1002forward_list<_Tp, _Alloc>::assign(initializer_list<value_type> __il)
1003{
1004 assign(__il.begin(), __il.end());
1005}
1006
Howard Hinnante3e32912011-08-12 21:56:02 +00001007#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1008
Howard Hinnant73d21a42010-09-04 23:28:19 +00001009#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1010#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001011
1012template <class _Tp, class _Alloc>
1013template <class... _Args>
1014void
1015forward_list<_Tp, _Alloc>::emplace_front(_Args&&... __args)
1016{
1017 __node_allocator& __a = base::__alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001018 typedef __allocator_destructor<__node_allocator> _Dp;
1019 unique_ptr<__node, _Dp> __h(__node_traits::allocate(__a, 1), _Dp(__a, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001020 __node_traits::construct(__a, _VSTD::addressof(__h->__value_),
1021 _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001022 __h->__next_ = base::__before_begin()->__next_;
1023 base::__before_begin()->__next_ = __h.release();
1024}
1025
Howard Hinnant73d21a42010-09-04 23:28:19 +00001026#endif // _LIBCPP_HAS_NO_VARIADICS
1027
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001028template <class _Tp, class _Alloc>
1029void
1030forward_list<_Tp, _Alloc>::push_front(value_type&& __v)
1031{
1032 __node_allocator& __a = base::__alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001033 typedef __allocator_destructor<__node_allocator> _Dp;
1034 unique_ptr<__node, _Dp> __h(__node_traits::allocate(__a, 1), _Dp(__a, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001035 __node_traits::construct(__a, _VSTD::addressof(__h->__value_), _VSTD::move(__v));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001036 __h->__next_ = base::__before_begin()->__next_;
1037 base::__before_begin()->__next_ = __h.release();
1038}
1039
Howard Hinnant73d21a42010-09-04 23:28:19 +00001040#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001041
1042template <class _Tp, class _Alloc>
1043void
1044forward_list<_Tp, _Alloc>::push_front(const value_type& __v)
1045{
1046 __node_allocator& __a = base::__alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001047 typedef __allocator_destructor<__node_allocator> _Dp;
1048 unique_ptr<__node, _Dp> __h(__node_traits::allocate(__a, 1), _Dp(__a, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001049 __node_traits::construct(__a, _VSTD::addressof(__h->__value_), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001050 __h->__next_ = base::__before_begin()->__next_;
1051 base::__before_begin()->__next_ = __h.release();
1052}
1053
1054template <class _Tp, class _Alloc>
1055void
1056forward_list<_Tp, _Alloc>::pop_front()
1057{
1058 __node_allocator& __a = base::__alloc();
1059 __node_pointer __p = base::__before_begin()->__next_;
1060 base::__before_begin()->__next_ = __p->__next_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001061 __node_traits::destroy(__a, _VSTD::addressof(__p->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001062 __node_traits::deallocate(__a, __p, 1);
1063}
1064
Howard Hinnant73d21a42010-09-04 23:28:19 +00001065#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1066#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001067
1068template <class _Tp, class _Alloc>
1069template <class... _Args>
1070typename forward_list<_Tp, _Alloc>::iterator
1071forward_list<_Tp, _Alloc>::emplace_after(const_iterator __p, _Args&&... __args)
1072{
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001073 __node_pointer const __r = __p.__ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001074 __node_allocator& __a = base::__alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001075 typedef __allocator_destructor<__node_allocator> _Dp;
1076 unique_ptr<__node, _Dp> __h(__node_traits::allocate(__a, 1), _Dp(__a, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001077 __node_traits::construct(__a, _VSTD::addressof(__h->__value_),
1078 _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001079 __h->__next_ = __r->__next_;
1080 __r->__next_ = __h.release();
1081 return iterator(__r->__next_);
1082}
1083
Howard Hinnant73d21a42010-09-04 23:28:19 +00001084#endif // _LIBCPP_HAS_NO_VARIADICS
1085
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001086template <class _Tp, class _Alloc>
1087typename forward_list<_Tp, _Alloc>::iterator
1088forward_list<_Tp, _Alloc>::insert_after(const_iterator __p, value_type&& __v)
1089{
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001090 __node_pointer const __r = __p.__ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001091 __node_allocator& __a = base::__alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001092 typedef __allocator_destructor<__node_allocator> _Dp;
1093 unique_ptr<__node, _Dp> __h(__node_traits::allocate(__a, 1), _Dp(__a, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001094 __node_traits::construct(__a, _VSTD::addressof(__h->__value_), _VSTD::move(__v));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001095 __h->__next_ = __r->__next_;
1096 __r->__next_ = __h.release();
1097 return iterator(__r->__next_);
1098}
1099
Howard Hinnant73d21a42010-09-04 23:28:19 +00001100#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001101
1102template <class _Tp, class _Alloc>
1103typename forward_list<_Tp, _Alloc>::iterator
1104forward_list<_Tp, _Alloc>::insert_after(const_iterator __p, const value_type& __v)
1105{
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001106 __node_pointer const __r = __p.__ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001107 __node_allocator& __a = base::__alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001108 typedef __allocator_destructor<__node_allocator> _Dp;
1109 unique_ptr<__node, _Dp> __h(__node_traits::allocate(__a, 1), _Dp(__a, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001110 __node_traits::construct(__a, _VSTD::addressof(__h->__value_), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001111 __h->__next_ = __r->__next_;
1112 __r->__next_ = __h.release();
1113 return iterator(__r->__next_);
1114}
1115
1116template <class _Tp, class _Alloc>
1117typename forward_list<_Tp, _Alloc>::iterator
1118forward_list<_Tp, _Alloc>::insert_after(const_iterator __p, size_type __n,
1119 const value_type& __v)
1120{
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001121 __node_pointer __r = __p.__ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001122 if (__n > 0)
1123 {
1124 __node_allocator& __a = base::__alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001125 typedef __allocator_destructor<__node_allocator> _Dp;
1126 unique_ptr<__node, _Dp> __h(__node_traits::allocate(__a, 1), _Dp(__a, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001127 __node_traits::construct(__a, _VSTD::addressof(__h->__value_), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001128 __node_pointer __first = __h.release();
1129 __node_pointer __last = __first;
1130#ifndef _LIBCPP_NO_EXCEPTIONS
1131 try
1132 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001133#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001134 for (--__n; __n != 0; --__n, __last = __last->__next_)
1135 {
1136 __h.reset(__node_traits::allocate(__a, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001137 __node_traits::construct(__a, _VSTD::addressof(__h->__value_), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001138 __last->__next_ = __h.release();
1139 }
1140#ifndef _LIBCPP_NO_EXCEPTIONS
1141 }
1142 catch (...)
1143 {
1144 while (__first != nullptr)
1145 {
1146 __node_pointer __next = __first->__next_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001147 __node_traits::destroy(__a, _VSTD::addressof(__first->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001148 __node_traits::deallocate(__a, __first, 1);
1149 __first = __next;
1150 }
1151 throw;
1152 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001153#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001154 __last->__next_ = __r->__next_;
1155 __r->__next_ = __first;
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001156 __r = __last;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001157 }
1158 return iterator(__r);
1159}
1160
1161template <class _Tp, class _Alloc>
1162template <class _InputIterator>
1163typename enable_if
1164<
1165 __is_input_iterator<_InputIterator>::value,
1166 typename forward_list<_Tp, _Alloc>::iterator
1167>::type
1168forward_list<_Tp, _Alloc>::insert_after(const_iterator __p,
1169 _InputIterator __f, _InputIterator __l)
1170{
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001171 __node_pointer __r = __p.__ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001172 if (__f != __l)
1173 {
1174 __node_allocator& __a = base::__alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001175 typedef __allocator_destructor<__node_allocator> _Dp;
1176 unique_ptr<__node, _Dp> __h(__node_traits::allocate(__a, 1), _Dp(__a, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001177 __node_traits::construct(__a, _VSTD::addressof(__h->__value_), *__f);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001178 __node_pointer __first = __h.release();
1179 __node_pointer __last = __first;
1180#ifndef _LIBCPP_NO_EXCEPTIONS
1181 try
1182 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001183#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiselierb9919752014-10-27 19:28:20 +00001184 for (++__f; __f != __l; ++__f, ((void)(__last = __last->__next_)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001185 {
1186 __h.reset(__node_traits::allocate(__a, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001187 __node_traits::construct(__a, _VSTD::addressof(__h->__value_), *__f);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001188 __last->__next_ = __h.release();
1189 }
1190#ifndef _LIBCPP_NO_EXCEPTIONS
1191 }
1192 catch (...)
1193 {
1194 while (__first != nullptr)
1195 {
1196 __node_pointer __next = __first->__next_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001197 __node_traits::destroy(__a, _VSTD::addressof(__first->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001198 __node_traits::deallocate(__a, __first, 1);
1199 __first = __next;
1200 }
1201 throw;
1202 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001203#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001204 __last->__next_ = __r->__next_;
1205 __r->__next_ = __first;
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001206 __r = __last;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001207 }
1208 return iterator(__r);
1209}
1210
1211template <class _Tp, class _Alloc>
Howard Hinnant7a2523b2010-08-21 20:58:44 +00001212typename forward_list<_Tp, _Alloc>::iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001213forward_list<_Tp, _Alloc>::erase_after(const_iterator __f)
1214{
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001215 __node_pointer __p = __f.__ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001216 __node_pointer __n = __p->__next_;
1217 __p->__next_ = __n->__next_;
1218 __node_allocator& __a = base::__alloc();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001219 __node_traits::destroy(__a, _VSTD::addressof(__n->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001220 __node_traits::deallocate(__a, __n, 1);
Howard Hinnant7a2523b2010-08-21 20:58:44 +00001221 return iterator(__p->__next_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001222}
1223
1224template <class _Tp, class _Alloc>
Howard Hinnant7a2523b2010-08-21 20:58:44 +00001225typename forward_list<_Tp, _Alloc>::iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001226forward_list<_Tp, _Alloc>::erase_after(const_iterator __f, const_iterator __l)
1227{
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001228 __node_pointer __e = __l.__ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001229 if (__f != __l)
1230 {
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001231 __node_pointer __p = __f.__ptr_;
1232 __node_pointer __n = __p->__next_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001233 if (__n != __e)
1234 {
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001235 __p->__next_ = __e;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001236 __node_allocator& __a = base::__alloc();
1237 do
1238 {
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001239 __p = __n->__next_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001240 __node_traits::destroy(__a, _VSTD::addressof(__n->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001241 __node_traits::deallocate(__a, __n, 1);
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001242 __n = __p;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001243 } while (__n != __e);
1244 }
1245 }
Howard Hinnant7a2523b2010-08-21 20:58:44 +00001246 return iterator(__e);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001247}
1248
1249template <class _Tp, class _Alloc>
1250void
1251forward_list<_Tp, _Alloc>::resize(size_type __n)
1252{
1253 size_type __sz = 0;
1254 iterator __p = before_begin();
1255 iterator __i = begin();
1256 iterator __e = end();
1257 for (; __i != __e && __sz < __n; ++__p, ++__i, ++__sz)
1258 ;
1259 if (__i != __e)
1260 erase_after(__p, __e);
1261 else
1262 {
1263 __n -= __sz;
1264 if (__n > 0)
1265 {
1266 __node_allocator& __a = base::__alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001267 typedef __allocator_destructor<__node_allocator> _Dp;
1268 unique_ptr<__node, _Dp> __h(nullptr, _Dp(__a, 1));
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001269 for (__node_pointer __ptr = __p.__ptr_; __n > 0; --__n,
1270 __ptr = __ptr->__next_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001271 {
1272 __h.reset(__node_traits::allocate(__a, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001273 __node_traits::construct(__a, _VSTD::addressof(__h->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001274 __h->__next_ = nullptr;
1275 __ptr->__next_ = __h.release();
1276 }
1277 }
1278 }
1279}
1280
1281template <class _Tp, class _Alloc>
1282void
1283forward_list<_Tp, _Alloc>::resize(size_type __n, const value_type& __v)
1284{
1285 size_type __sz = 0;
1286 iterator __p = before_begin();
1287 iterator __i = begin();
1288 iterator __e = end();
1289 for (; __i != __e && __sz < __n; ++__p, ++__i, ++__sz)
1290 ;
1291 if (__i != __e)
1292 erase_after(__p, __e);
1293 else
1294 {
1295 __n -= __sz;
1296 if (__n > 0)
1297 {
1298 __node_allocator& __a = base::__alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001299 typedef __allocator_destructor<__node_allocator> _Dp;
1300 unique_ptr<__node, _Dp> __h(nullptr, _Dp(__a, 1));
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001301 for (__node_pointer __ptr = __p.__ptr_; __n > 0; --__n,
1302 __ptr = __ptr->__next_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001303 {
1304 __h.reset(__node_traits::allocate(__a, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001305 __node_traits::construct(__a, _VSTD::addressof(__h->__value_), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001306 __h->__next_ = nullptr;
1307 __ptr->__next_ = __h.release();
1308 }
1309 }
1310 }
1311}
1312
1313template <class _Tp, class _Alloc>
1314void
1315forward_list<_Tp, _Alloc>::splice_after(const_iterator __p,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001316 forward_list& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001317{
1318 if (!__x.empty())
1319 {
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001320 if (__p.__ptr_->__next_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001321 {
1322 const_iterator __lm1 = __x.before_begin();
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001323 while (__lm1.__ptr_->__next_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001324 ++__lm1;
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001325 __lm1.__ptr_->__next_ = __p.__ptr_->__next_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001326 }
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001327 __p.__ptr_->__next_ = __x.__before_begin()->__next_;
Howard Hinnant81381a92013-06-24 17:17:28 +00001328 __x.__before_begin()->__next_ = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001329 }
1330}
1331
1332template <class _Tp, class _Alloc>
1333void
1334forward_list<_Tp, _Alloc>::splice_after(const_iterator __p,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001335 forward_list& __x,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001336 const_iterator __i)
1337{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001338 const_iterator __lm1 = _VSTD::next(__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001339 if (__p != __i && __p != __lm1)
1340 {
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001341 __i.__ptr_->__next_ = __lm1.__ptr_->__next_;
1342 __lm1.__ptr_->__next_ = __p.__ptr_->__next_;
1343 __p.__ptr_->__next_ = __lm1.__ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001344 }
1345}
1346
1347template <class _Tp, class _Alloc>
1348void
1349forward_list<_Tp, _Alloc>::splice_after(const_iterator __p,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001350 forward_list& __x,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001351 const_iterator __f, const_iterator __l)
1352{
1353 if (__f != __l && __p != __f)
1354 {
1355 const_iterator __lm1 = __f;
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001356 while (__lm1.__ptr_->__next_ != __l.__ptr_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001357 ++__lm1;
1358 if (__f != __lm1)
1359 {
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001360 __lm1.__ptr_->__next_ = __p.__ptr_->__next_;
1361 __p.__ptr_->__next_ = __f.__ptr_->__next_;
1362 __f.__ptr_->__next_ = __l.__ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001363 }
1364 }
1365}
1366
Howard Hinnant99b2f762011-01-27 21:00:35 +00001367#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1368
1369template <class _Tp, class _Alloc>
1370inline _LIBCPP_INLINE_VISIBILITY
1371void
1372forward_list<_Tp, _Alloc>::splice_after(const_iterator __p,
1373 forward_list&& __x)
1374{
1375 splice_after(__p, __x);
1376}
1377
1378template <class _Tp, class _Alloc>
1379inline _LIBCPP_INLINE_VISIBILITY
1380void
1381forward_list<_Tp, _Alloc>::splice_after(const_iterator __p,
1382 forward_list&& __x,
1383 const_iterator __i)
1384{
1385 splice_after(__p, __x, __i);
1386}
1387
1388template <class _Tp, class _Alloc>
1389inline _LIBCPP_INLINE_VISIBILITY
1390void
1391forward_list<_Tp, _Alloc>::splice_after(const_iterator __p,
1392 forward_list&& __x,
1393 const_iterator __f, const_iterator __l)
1394{
1395 splice_after(__p, __x, __f, __l);
1396}
1397
1398#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1399
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001400template <class _Tp, class _Alloc>
1401void
1402forward_list<_Tp, _Alloc>::remove(const value_type& __v)
1403{
Marshall Clow095c3dd2014-08-08 15:58:00 +00001404 forward_list<_Tp, _Alloc> __deleted_nodes; // collect the nodes we're removing
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001405 iterator __e = end();
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001406 for (iterator __i = before_begin(); __i.__ptr_->__next_ != nullptr;)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001407 {
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001408 if (__i.__ptr_->__next_->__value_ == __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001409 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001410 iterator __j = _VSTD::next(__i, 2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001411 for (; __j != __e && *__j == __v; ++__j)
1412 ;
Marshall Clow38d90052014-10-18 11:03:33 +00001413 __deleted_nodes.splice_after(__deleted_nodes.before_begin(), *this, __i, __j);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001414 if (__j == __e)
1415 break;
1416 __i = __j;
1417 }
1418 else
1419 ++__i;
1420 }
1421}
1422
1423template <class _Tp, class _Alloc>
1424template <class _Predicate>
1425void
1426forward_list<_Tp, _Alloc>::remove_if(_Predicate __pred)
1427{
1428 iterator __e = end();
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001429 for (iterator __i = before_begin(); __i.__ptr_->__next_ != nullptr;)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001430 {
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001431 if (__pred(__i.__ptr_->__next_->__value_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001432 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001433 iterator __j = _VSTD::next(__i, 2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001434 for (; __j != __e && __pred(*__j); ++__j)
1435 ;
1436 erase_after(__i, __j);
1437 if (__j == __e)
1438 break;
1439 __i = __j;
1440 }
1441 else
1442 ++__i;
1443 }
1444}
1445
1446template <class _Tp, class _Alloc>
1447template <class _BinaryPredicate>
1448void
1449forward_list<_Tp, _Alloc>::unique(_BinaryPredicate __binary_pred)
1450{
1451 for (iterator __i = begin(), __e = end(); __i != __e;)
1452 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001453 iterator __j = _VSTD::next(__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001454 for (; __j != __e && __binary_pred(*__i, *__j); ++__j)
1455 ;
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001456 if (__i.__ptr_->__next_ != __j.__ptr_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001457 erase_after(__i, __j);
1458 __i = __j;
1459 }
1460}
1461
1462template <class _Tp, class _Alloc>
1463template <class _Compare>
1464void
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001465forward_list<_Tp, _Alloc>::merge(forward_list& __x, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001466{
1467 if (this != &__x)
1468 {
1469 base::__before_begin()->__next_ = __merge(base::__before_begin()->__next_,
1470 __x.__before_begin()->__next_,
1471 __comp);
1472 __x.__before_begin()->__next_ = nullptr;
1473 }
1474}
1475
1476template <class _Tp, class _Alloc>
1477template <class _Compare>
1478typename forward_list<_Tp, _Alloc>::__node_pointer
1479forward_list<_Tp, _Alloc>::__merge(__node_pointer __f1, __node_pointer __f2,
1480 _Compare& __comp)
1481{
1482 if (__f1 == nullptr)
1483 return __f2;
1484 if (__f2 == nullptr)
1485 return __f1;
1486 __node_pointer __r;
1487 if (__comp(__f2->__value_, __f1->__value_))
1488 {
1489 __node_pointer __t = __f2;
1490 while (__t->__next_ != nullptr &&
1491 __comp(__t->__next_->__value_, __f1->__value_))
1492 __t = __t->__next_;
1493 __r = __f2;
1494 __f2 = __t->__next_;
1495 __t->__next_ = __f1;
1496 }
1497 else
1498 __r = __f1;
1499 __node_pointer __p = __f1;
1500 __f1 = __f1->__next_;
1501 while (__f1 != nullptr && __f2 != nullptr)
1502 {
1503 if (__comp(__f2->__value_, __f1->__value_))
1504 {
1505 __node_pointer __t = __f2;
1506 while (__t->__next_ != nullptr &&
1507 __comp(__t->__next_->__value_, __f1->__value_))
1508 __t = __t->__next_;
1509 __p->__next_ = __f2;
1510 __f2 = __t->__next_;
1511 __t->__next_ = __f1;
1512 }
1513 __p = __f1;
1514 __f1 = __f1->__next_;
1515 }
1516 if (__f2 != nullptr)
1517 __p->__next_ = __f2;
1518 return __r;
1519}
1520
1521template <class _Tp, class _Alloc>
1522template <class _Compare>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001523inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001524void
1525forward_list<_Tp, _Alloc>::sort(_Compare __comp)
1526{
1527 base::__before_begin()->__next_ = __sort(base::__before_begin()->__next_,
Howard Hinnant0949eed2011-06-30 21:18:19 +00001528 _VSTD::distance(begin(), end()), __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001529}
1530
1531template <class _Tp, class _Alloc>
1532template <class _Compare>
1533typename forward_list<_Tp, _Alloc>::__node_pointer
1534forward_list<_Tp, _Alloc>::__sort(__node_pointer __f1, difference_type __sz,
1535 _Compare& __comp)
1536{
1537 switch (__sz)
1538 {
1539 case 0:
1540 case 1:
1541 return __f1;
1542 case 2:
1543 if (__comp(__f1->__next_->__value_, __f1->__value_))
1544 {
1545 __node_pointer __t = __f1->__next_;
1546 __t->__next_ = __f1;
1547 __f1->__next_ = nullptr;
1548 __f1 = __t;
1549 }
1550 return __f1;
1551 }
1552 difference_type __sz1 = __sz / 2;
1553 difference_type __sz2 = __sz - __sz1;
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001554 __node_pointer __t = _VSTD::next(iterator(__f1), __sz1 - 1).__ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001555 __node_pointer __f2 = __t->__next_;
1556 __t->__next_ = nullptr;
1557 return __merge(__sort(__f1, __sz1, __comp),
1558 __sort(__f2, __sz2, __comp), __comp);
1559}
1560
1561template <class _Tp, class _Alloc>
1562void
Howard Hinnant8790cab2011-06-02 16:44:28 +00001563forward_list<_Tp, _Alloc>::reverse() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001564{
1565 __node_pointer __p = base::__before_begin()->__next_;
1566 if (__p != nullptr)
1567 {
1568 __node_pointer __f = __p->__next_;
1569 __p->__next_ = nullptr;
1570 while (__f != nullptr)
1571 {
1572 __node_pointer __t = __f->__next_;
1573 __f->__next_ = __p;
1574 __p = __f;
1575 __f = __t;
1576 }
1577 base::__before_begin()->__next_ = __p;
1578 }
1579}
1580
1581template <class _Tp, class _Alloc>
1582bool operator==(const forward_list<_Tp, _Alloc>& __x,
1583 const forward_list<_Tp, _Alloc>& __y)
1584{
Howard Hinnant99968442011-11-29 18:15:50 +00001585 typedef forward_list<_Tp, _Alloc> _Cp;
1586 typedef typename _Cp::const_iterator _Ip;
1587 _Ip __ix = __x.begin();
1588 _Ip __ex = __x.end();
1589 _Ip __iy = __y.begin();
1590 _Ip __ey = __y.end();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001591 for (; __ix != __ex && __iy != __ey; ++__ix, ++__iy)
1592 if (!(*__ix == *__iy))
1593 return false;
1594 return (__ix == __ex) == (__iy == __ey);
1595}
1596
1597template <class _Tp, class _Alloc>
Howard Hinnant42a63a72010-09-21 22:55:27 +00001598inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001599bool operator!=(const forward_list<_Tp, _Alloc>& __x,
1600 const forward_list<_Tp, _Alloc>& __y)
1601{
1602 return !(__x == __y);
1603}
1604
1605template <class _Tp, class _Alloc>
Howard Hinnant42a63a72010-09-21 22:55:27 +00001606inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001607bool operator< (const forward_list<_Tp, _Alloc>& __x,
1608 const forward_list<_Tp, _Alloc>& __y)
1609{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001610 return _VSTD::lexicographical_compare(__x.begin(), __x.end(),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001611 __y.begin(), __y.end());
1612}
1613
1614template <class _Tp, class _Alloc>
Howard Hinnant42a63a72010-09-21 22:55:27 +00001615inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001616bool operator> (const forward_list<_Tp, _Alloc>& __x,
1617 const forward_list<_Tp, _Alloc>& __y)
1618{
1619 return __y < __x;
1620}
1621
1622template <class _Tp, class _Alloc>
Howard Hinnant42a63a72010-09-21 22:55:27 +00001623inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001624bool operator>=(const forward_list<_Tp, _Alloc>& __x,
1625 const forward_list<_Tp, _Alloc>& __y)
1626{
1627 return !(__x < __y);
1628}
1629
1630template <class _Tp, class _Alloc>
Howard Hinnant42a63a72010-09-21 22:55:27 +00001631inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001632bool operator<=(const forward_list<_Tp, _Alloc>& __x,
1633 const forward_list<_Tp, _Alloc>& __y)
1634{
1635 return !(__y < __x);
1636}
1637
1638template <class _Tp, class _Alloc>
Howard Hinnant42a63a72010-09-21 22:55:27 +00001639inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001640void
1641swap(forward_list<_Tp, _Alloc>& __x, forward_list<_Tp, _Alloc>& __y)
Howard Hinnantb965fed2011-06-03 16:20:53 +00001642 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001643{
1644 __x.swap(__y);
1645}
1646
1647_LIBCPP_END_NAMESPACE_STD
1648
1649#endif // _LIBCPP_FORWARD_LIST