blob: 88bf75f90d7cc56d20f6e25455225f2e3cce964e [file] [log] [blame]
Howard Hinnant3e519522010-05-11 19:42:16 +00001// -*- C++ -*-
2//===----------------------- forward_list ---------------------------------===//
3//
Howard Hinnant5b08a8a2010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnant3e519522010-05-11 19:42:16 +00005//
Howard Hinnant412dbeb2010-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 Hinnant3e519522010-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 Hinnant91a47502011-06-03 16:20:53 +000037 forward_list()
38 noexcept(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnant3e519522010-05-11 19:42:16 +000039 explicit forward_list(const allocator_type& a);
40 explicit forward_list(size_type n);
41 forward_list(size_type n, const value_type& v);
42 forward_list(size_type n, const value_type& v, const allocator_type& a);
43 template <class InputIterator>
44 forward_list(InputIterator first, InputIterator last);
45 template <class InputIterator>
46 forward_list(InputIterator first, InputIterator last, const allocator_type& a);
47 forward_list(const forward_list& x);
48 forward_list(const forward_list& x, const allocator_type& a);
Howard Hinnant91a47502011-06-03 16:20:53 +000049 forward_list(forward_list&& x)
50 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnant3e519522010-05-11 19:42:16 +000051 forward_list(forward_list&& x, const allocator_type& a);
52 forward_list(initializer_list<value_type> il);
53 forward_list(initializer_list<value_type> il, const allocator_type& a);
54
55 ~forward_list();
56
57 forward_list& operator=(const forward_list& x);
Howard Hinnant91a47502011-06-03 16:20:53 +000058 forward_list& operator=(forward_list&& x)
59 noexcept(
60 allocator_type::propagate_on_container_move_assignment::value &&
61 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant3e519522010-05-11 19:42:16 +000062 forward_list& operator=(initializer_list<value_type> il);
63
64 template <class InputIterator>
65 void assign(InputIterator first, InputIterator last);
66 void assign(size_type n, const value_type& v);
67 void assign(initializer_list<value_type> il);
68
Howard Hinnantf9dc2832011-06-02 16:44:28 +000069 allocator_type get_allocator() const noexcept;
Howard Hinnant3e519522010-05-11 19:42:16 +000070
Howard Hinnantf9dc2832011-06-02 16:44:28 +000071 iterator begin() noexcept;
72 const_iterator begin() const noexcept;
73 iterator end() noexcept;
74 const_iterator end() const noexcept;
Howard Hinnant3e519522010-05-11 19:42:16 +000075
Howard Hinnantf9dc2832011-06-02 16:44:28 +000076 const_iterator cbegin() const noexcept;
77 const_iterator cend() const noexcept;
Howard Hinnant3e519522010-05-11 19:42:16 +000078
Howard Hinnantf9dc2832011-06-02 16:44:28 +000079 iterator before_begin() noexcept;
80 const_iterator before_begin() const noexcept;
81 const_iterator cbefore_begin() const noexcept;
Howard Hinnant3e519522010-05-11 19:42:16 +000082
Howard Hinnantf9dc2832011-06-02 16:44:28 +000083 bool empty() const noexcept;
84 size_type max_size() const noexcept;
Howard Hinnant3e519522010-05-11 19:42:16 +000085
86 reference front();
87 const_reference front() const;
88
89 template <class... Args> void emplace_front(Args&&... args);
90 void push_front(const value_type& v);
91 void push_front(value_type&& v);
92
93 void pop_front();
94
95 template <class... Args>
96 iterator emplace_after(const_iterator p, Args&&... args);
97 iterator insert_after(const_iterator p, const value_type& v);
98 iterator insert_after(const_iterator p, value_type&& v);
99 iterator insert_after(const_iterator p, size_type n, const value_type& v);
100 template <class InputIterator>
101 iterator insert_after(const_iterator p,
102 InputIterator first, InputIterator last);
103 iterator insert_after(const_iterator p, initializer_list<value_type> il);
104
Howard Hinnant3db88032010-08-21 20:58:44 +0000105 iterator erase_after(const_iterator p);
106 iterator erase_after(const_iterator first, const_iterator last);
Howard Hinnant3e519522010-05-11 19:42:16 +0000107
Howard Hinnant91a47502011-06-03 16:20:53 +0000108 void swap(forward_list& x)
109 noexcept(!allocator_type::propagate_on_container_swap::value ||
110 __is_nothrow_swappable<allocator_type>::value);
Howard Hinnant3e519522010-05-11 19:42:16 +0000111
112 void resize(size_type n);
113 void resize(size_type n, const value_type& v);
Howard Hinnantf9dc2832011-06-02 16:44:28 +0000114 void clear() noexcept;
Howard Hinnant3e519522010-05-11 19:42:16 +0000115
Howard Hinnanteb92df72011-01-27 21:00:35 +0000116 void splice_after(const_iterator p, forward_list& x);
Howard Hinnant3e519522010-05-11 19:42:16 +0000117 void splice_after(const_iterator p, forward_list&& x);
Howard Hinnanteb92df72011-01-27 21:00:35 +0000118 void splice_after(const_iterator p, forward_list& x, const_iterator i);
Howard Hinnant3e519522010-05-11 19:42:16 +0000119 void splice_after(const_iterator p, forward_list&& x, const_iterator i);
Howard Hinnanteb92df72011-01-27 21:00:35 +0000120 void splice_after(const_iterator p, forward_list& x,
121 const_iterator first, const_iterator last);
Howard Hinnant3e519522010-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 Hinnanteb92df72011-01-27 21:00:35 +0000128 void merge(forward_list& x);
Howard Hinnant3e519522010-05-11 19:42:16 +0000129 void merge(forward_list&& x);
Howard Hinnanteb92df72011-01-27 21:00:35 +0000130 template <class Compare> void merge(forward_list& x, Compare comp);
Howard Hinnant3e519522010-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 Hinnantf9dc2832011-06-02 16:44:28 +0000134 void reverse() noexcept;
Howard Hinnant3e519522010-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 Hinnant91a47502011-06-03 16:20:53 +0000162 void swap(forward_list<T, Allocator>& x, forward_list<T, Allocator>& y)
Howard Hinnant45900102011-06-03 17:30:28 +0000163 noexcept(noexcept(x.swap(y)));
Howard Hinnant3e519522010-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 Hinnantab4f4382011-11-29 16:45:27 +0000177#include <__undef_min_max>
178
Howard Hinnant073458b2011-10-17 20:05:10 +0000179#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnant3e519522010-05-11 19:42:16 +0000180#pragma GCC system_header
Howard Hinnant073458b2011-10-17 20:05:10 +0000181#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000182
183_LIBCPP_BEGIN_NAMESPACE_STD
184
Howard Hinnantce534202011-06-14 19:58:17 +0000185template <class _Tp, class _VoidPtr> struct __forward_list_node;
Howard Hinnant3e519522010-05-11 19:42:16 +0000186
187template <class _NodePtr>
188struct __forward_begin_node
189{
190 typedef __forward_begin_node __self;
191 typedef _NodePtr pointer;
192
193 pointer __next_;
194
Howard Hinnant0af133f2010-09-21 22:55:27 +0000195 _LIBCPP_INLINE_VISIBILITY __forward_begin_node() : __next_(nullptr) {}
Howard Hinnant3e519522010-05-11 19:42:16 +0000196};
197
198template <class _Tp, class _VoidPtr>
199struct __forward_list_node
200 : public __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 >
209{
210 typedef _Tp value_type;
211
212 value_type __value_;
213};
214
Howard Hinnant6e412562013-03-06 23:30:19 +0000215template<class _Tp, class _Alloc> class _LIBCPP_TYPE_VIS forward_list;
216template<class _NodeConstPtr> class _LIBCPP_TYPE_VIS __forward_list_const_iterator;
Howard Hinnant3e519522010-05-11 19:42:16 +0000217
218template <class _NodePtr>
Howard Hinnant6e412562013-03-06 23:30:19 +0000219class _LIBCPP_TYPE_VIS __forward_list_iterator
Howard Hinnant3e519522010-05-11 19:42:16 +0000220{
221 typedef _NodePtr __node_pointer;
222
223 __node_pointer __ptr_;
224
Howard Hinnant0af133f2010-09-21 22:55:27 +0000225 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf9dc2832011-06-02 16:44:28 +0000226 explicit __forward_list_iterator(__node_pointer __p) _NOEXCEPT : __ptr_(__p) {}
Howard Hinnant3e519522010-05-11 19:42:16 +0000227
Howard Hinnant6e412562013-03-06 23:30:19 +0000228 template<class, class> friend class _LIBCPP_TYPE_VIS forward_list;
229 template<class> friend class _LIBCPP_TYPE_VIS __forward_list_const_iterator;
Howard Hinnant3e519522010-05-11 19:42:16 +0000230
231public:
232 typedef forward_iterator_tag iterator_category;
233 typedef typename pointer_traits<__node_pointer>::element_type::value_type
234 value_type;
Howard Hinnant8a27ba82013-06-24 17:17:28 +0000235 typedef value_type& reference;
Howard Hinnantb3371f62010-08-22 00:02:43 +0000236 typedef typename pointer_traits<__node_pointer>::difference_type
Howard Hinnant3e519522010-05-11 19:42:16 +0000237 difference_type;
238 typedef typename pointer_traits<__node_pointer>::template
239#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
240 rebind<value_type>
241#else
242 rebind<value_type>::other
243#endif
244 pointer;
245
Howard Hinnant0af133f2010-09-21 22:55:27 +0000246 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf9dc2832011-06-02 16:44:28 +0000247 __forward_list_iterator() _NOEXCEPT : __ptr_(nullptr) {}
Howard Hinnant3e519522010-05-11 19:42:16 +0000248
Howard Hinnant0af133f2010-09-21 22:55:27 +0000249 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000250 reference operator*() const {return __ptr_->__value_;}
Howard Hinnant0af133f2010-09-21 22:55:27 +0000251 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8a27ba82013-06-24 17:17:28 +0000252 pointer operator->() const {return pointer_traits<pointer>::pointer_to(__ptr_->__value_);}
Howard Hinnant3e519522010-05-11 19:42:16 +0000253
Howard Hinnant0af133f2010-09-21 22:55:27 +0000254 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000255 __forward_list_iterator& operator++()
256 {
257 __ptr_ = __ptr_->__next_;
258 return *this;
259 }
Howard Hinnant0af133f2010-09-21 22:55:27 +0000260 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000261 __forward_list_iterator operator++(int)
262 {
263 __forward_list_iterator __t(*this);
264 ++(*this);
265 return __t;
266 }
267
Howard Hinnant0af133f2010-09-21 22:55:27 +0000268 friend _LIBCPP_INLINE_VISIBILITY
269 bool operator==(const __forward_list_iterator& __x,
270 const __forward_list_iterator& __y)
Howard Hinnant3e519522010-05-11 19:42:16 +0000271 {return __x.__ptr_ == __y.__ptr_;}
Howard Hinnant0af133f2010-09-21 22:55:27 +0000272 friend _LIBCPP_INLINE_VISIBILITY
273 bool operator!=(const __forward_list_iterator& __x,
274 const __forward_list_iterator& __y)
Howard Hinnant3e519522010-05-11 19:42:16 +0000275 {return !(__x == __y);}
276};
277
278template <class _NodeConstPtr>
Howard Hinnant6e412562013-03-06 23:30:19 +0000279class _LIBCPP_TYPE_VIS __forward_list_const_iterator
Howard Hinnant3e519522010-05-11 19:42:16 +0000280{
281 typedef _NodeConstPtr __node_const_pointer;
282
283 __node_const_pointer __ptr_;
284
Howard Hinnant0af133f2010-09-21 22:55:27 +0000285 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf9dc2832011-06-02 16:44:28 +0000286 explicit __forward_list_const_iterator(__node_const_pointer __p) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000287 : __ptr_(__p) {}
288
289 typedef typename remove_const
290 <
291 typename pointer_traits<__node_const_pointer>::element_type
292 >::type __node;
293 typedef typename pointer_traits<__node_const_pointer>::template
294#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
295 rebind<__node>
296#else
297 rebind<__node>::other
298#endif
299 __node_pointer;
300
301 template<class, class> friend class forward_list;
302
303public:
304 typedef forward_iterator_tag iterator_category;
305 typedef typename __node::value_type value_type;
Howard Hinnant8a27ba82013-06-24 17:17:28 +0000306 typedef const value_type& reference;
Howard Hinnantb3371f62010-08-22 00:02:43 +0000307 typedef typename pointer_traits<__node_const_pointer>::difference_type
Howard Hinnant3e519522010-05-11 19:42:16 +0000308 difference_type;
309 typedef typename pointer_traits<__node_const_pointer>::template
310#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
311 rebind<const value_type>
312#else
313 rebind<const value_type>::other
314#endif
315 pointer;
316
Howard Hinnant0af133f2010-09-21 22:55:27 +0000317 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf9dc2832011-06-02 16:44:28 +0000318 __forward_list_const_iterator() _NOEXCEPT : __ptr_(nullptr) {}
Howard Hinnant0af133f2010-09-21 22:55:27 +0000319 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf9dc2832011-06-02 16:44:28 +0000320 __forward_list_const_iterator(__forward_list_iterator<__node_pointer> __p) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000321 : __ptr_(__p.__ptr_) {}
322
Howard Hinnant0af133f2010-09-21 22:55:27 +0000323 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000324 reference operator*() const {return __ptr_->__value_;}
Howard Hinnant0af133f2010-09-21 22:55:27 +0000325 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8a27ba82013-06-24 17:17:28 +0000326 pointer operator->() const {return pointer_traits<pointer>::pointer_to(__ptr_->__value_);}
Howard Hinnant3e519522010-05-11 19:42:16 +0000327
Howard Hinnant0af133f2010-09-21 22:55:27 +0000328 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000329 __forward_list_const_iterator& operator++()
330 {
331 __ptr_ = __ptr_->__next_;
332 return *this;
333 }
Howard Hinnant0af133f2010-09-21 22:55:27 +0000334 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000335 __forward_list_const_iterator operator++(int)
336 {
337 __forward_list_const_iterator __t(*this);
338 ++(*this);
339 return __t;
340 }
341
Howard Hinnant0af133f2010-09-21 22:55:27 +0000342 friend _LIBCPP_INLINE_VISIBILITY
343 bool operator==(const __forward_list_const_iterator& __x,
344 const __forward_list_const_iterator& __y)
Howard Hinnant3e519522010-05-11 19:42:16 +0000345 {return __x.__ptr_ == __y.__ptr_;}
Howard Hinnant0af133f2010-09-21 22:55:27 +0000346 friend _LIBCPP_INLINE_VISIBILITY
347 bool operator!=(const __forward_list_const_iterator& __x,
Howard Hinnant3e519522010-05-11 19:42:16 +0000348 const __forward_list_const_iterator& __y)
349 {return !(__x == __y);}
350};
351
352template <class _Tp, class _Alloc>
353class __forward_list_base
354{
355protected:
356 typedef _Tp value_type;
357 typedef _Alloc allocator_type;
358
359 typedef typename allocator_traits<allocator_type>::void_pointer void_pointer;
360 typedef __forward_list_node<value_type, void_pointer> __node;
361 typedef typename __node::__self __begin_node;
362 typedef typename allocator_traits<allocator_type>::template
363#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
364 rebind_alloc<__node>
365#else
366 rebind_alloc<__node>::other
367#endif
368 __node_allocator;
369 typedef allocator_traits<__node_allocator> __node_traits;
370 typedef typename __node_traits::pointer __node_pointer;
Howard Hinnant8a27ba82013-06-24 17:17:28 +0000371 typedef typename __node_traits::pointer __node_const_pointer;
372
373 typedef typename allocator_traits<allocator_type>::template
374#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
375 rebind_alloc<__begin_node>
376#else
377 rebind_alloc<__begin_node>::other
378#endif
379 __begin_node_allocator;
380 typedef typename allocator_traits<__begin_node_allocator>::pointer __begin_node_pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000381
382 __compressed_pair<__begin_node, __node_allocator> __before_begin_;
383
Howard Hinnant0af133f2010-09-21 22:55:27 +0000384 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf9dc2832011-06-02 16:44:28 +0000385 __node_pointer __before_begin() _NOEXCEPT
Howard Hinnant8a27ba82013-06-24 17:17:28 +0000386 {return static_cast<__node_pointer>(pointer_traits<__begin_node_pointer>::
387 pointer_to(__before_begin_.first()));}
Howard Hinnant0af133f2010-09-21 22:55:27 +0000388 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf9dc2832011-06-02 16:44:28 +0000389 __node_const_pointer __before_begin() const _NOEXCEPT
Howard Hinnant8a27ba82013-06-24 17:17:28 +0000390 {return static_cast<__node_const_pointer>(pointer_traits<__begin_node_pointer>::
391 pointer_to(const_cast<__begin_node&>(__before_begin_.first())));}
Howard Hinnant3e519522010-05-11 19:42:16 +0000392
Howard Hinnant0af133f2010-09-21 22:55:27 +0000393 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant91a47502011-06-03 16:20:53 +0000394 __node_allocator& __alloc() _NOEXCEPT
395 {return __before_begin_.second();}
Howard Hinnant0af133f2010-09-21 22:55:27 +0000396 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf9dc2832011-06-02 16:44:28 +0000397 const __node_allocator& __alloc() const _NOEXCEPT
398 {return __before_begin_.second();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000399
400 typedef __forward_list_iterator<__node_pointer> iterator;
Howard Hinnant8a27ba82013-06-24 17:17:28 +0000401 typedef __forward_list_const_iterator<__node_pointer> const_iterator;
Howard Hinnant3e519522010-05-11 19:42:16 +0000402
Howard Hinnant0af133f2010-09-21 22:55:27 +0000403 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000404 __forward_list_base()
Howard Hinnant91a47502011-06-03 16:20:53 +0000405 _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +0000406 : __before_begin_(__begin_node()) {}
Howard Hinnant0af133f2010-09-21 22:55:27 +0000407 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000408 __forward_list_base(const allocator_type& __a)
409 : __before_begin_(__begin_node(), __node_allocator(__a)) {}
410
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000411#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant91a47502011-06-03 16:20:53 +0000412public:
413 __forward_list_base(__forward_list_base&& __x)
414 _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value);
Howard Hinnant3e519522010-05-11 19:42:16 +0000415 __forward_list_base(__forward_list_base&& __x, const allocator_type& __a);
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000416#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +0000417
418private:
419 __forward_list_base(const __forward_list_base&);
420 __forward_list_base& operator=(const __forward_list_base&);
Howard Hinnant3e519522010-05-11 19:42:16 +0000421
Howard Hinnant91a47502011-06-03 16:20:53 +0000422public:
Howard Hinnant3e519522010-05-11 19:42:16 +0000423 ~__forward_list_base();
424
Howard Hinnant91a47502011-06-03 16:20:53 +0000425protected:
Howard Hinnant0af133f2010-09-21 22:55:27 +0000426 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000427 void __copy_assign_alloc(const __forward_list_base& __x)
428 {__copy_assign_alloc(__x, integral_constant<bool,
429 __node_traits::propagate_on_container_copy_assignment::value>());}
430
Howard Hinnant0af133f2010-09-21 22:55:27 +0000431 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000432 void __move_assign_alloc(__forward_list_base& __x)
Howard Hinnant91a47502011-06-03 16:20:53 +0000433 _NOEXCEPT_(!__node_traits::propagate_on_container_move_assignment::value ||
434 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +0000435 {__move_assign_alloc(__x, integral_constant<bool,
436 __node_traits::propagate_on_container_move_assignment::value>());}
437
Howard Hinnant91a47502011-06-03 16:20:53 +0000438public:
439 void swap(__forward_list_base& __x)
440 _NOEXCEPT_(!__node_traits::propagate_on_container_swap::value ||
441 __is_nothrow_swappable<__node_allocator>::value);
442protected:
Howard Hinnantf9dc2832011-06-02 16:44:28 +0000443 void clear() _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +0000444
445private:
Howard Hinnant0af133f2010-09-21 22:55:27 +0000446 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000447 void __copy_assign_alloc(const __forward_list_base&, false_type) {}
Howard Hinnant0af133f2010-09-21 22:55:27 +0000448 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000449 void __copy_assign_alloc(const __forward_list_base& __x, true_type)
450 {
451 if (__alloc() != __x.__alloc())
452 clear();
453 __alloc() = __x.__alloc();
454 }
455
Howard Hinnant0af133f2010-09-21 22:55:27 +0000456 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant91a47502011-06-03 16:20:53 +0000457 void __move_assign_alloc(__forward_list_base& __x, false_type) _NOEXCEPT
458 {}
Howard Hinnant0af133f2010-09-21 22:55:27 +0000459 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000460 void __move_assign_alloc(__forward_list_base& __x, true_type)
Howard Hinnant91a47502011-06-03 16:20:53 +0000461 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantce48a112011-06-30 21:18:19 +0000462 {__alloc() = _VSTD::move(__x.__alloc());}
Howard Hinnant3e519522010-05-11 19:42:16 +0000463
Howard Hinnant0af133f2010-09-21 22:55:27 +0000464 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000465 static void __swap_alloc(__node_allocator& __x, __node_allocator& __y)
Howard Hinnant91a47502011-06-03 16:20:53 +0000466 _NOEXCEPT_(!__node_traits::propagate_on_container_swap::value ||
467 __is_nothrow_swappable<__node_allocator>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +0000468 {__swap_alloc(__x, __y, integral_constant<bool,
469 __node_traits::propagate_on_container_swap::value>());}
Howard Hinnant0af133f2010-09-21 22:55:27 +0000470 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000471 static void __swap_alloc(__node_allocator& __x, __node_allocator& __y,
472 false_type)
Howard Hinnant91a47502011-06-03 16:20:53 +0000473 _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000474 {}
Howard Hinnant0af133f2010-09-21 22:55:27 +0000475 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000476 static void __swap_alloc(__node_allocator& __x, __node_allocator& __y,
477 true_type)
Howard Hinnant91a47502011-06-03 16:20:53 +0000478 _NOEXCEPT_(__is_nothrow_swappable<__node_allocator>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +0000479 {
Howard Hinnantce48a112011-06-30 21:18:19 +0000480 using _VSTD::swap;
Howard Hinnant3e519522010-05-11 19:42:16 +0000481 swap(__x, __y);
482 }
483};
484
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000485#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +0000486
487template <class _Tp, class _Alloc>
Howard Hinnant0af133f2010-09-21 22:55:27 +0000488inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000489__forward_list_base<_Tp, _Alloc>::__forward_list_base(__forward_list_base&& __x)
Howard Hinnant91a47502011-06-03 16:20:53 +0000490 _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value)
Howard Hinnantce48a112011-06-30 21:18:19 +0000491 : __before_begin_(_VSTD::move(__x.__before_begin_))
Howard Hinnant3e519522010-05-11 19:42:16 +0000492{
493 __x.__before_begin()->__next_ = nullptr;
494}
495
496template <class _Tp, class _Alloc>
Howard Hinnant0af133f2010-09-21 22:55:27 +0000497inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000498__forward_list_base<_Tp, _Alloc>::__forward_list_base(__forward_list_base&& __x,
499 const allocator_type& __a)
500 : __before_begin_(__begin_node(), __node_allocator(__a))
501{
502 if (__alloc() == __x.__alloc())
503 {
504 __before_begin()->__next_ = __x.__before_begin()->__next_;
505 __x.__before_begin()->__next_ = nullptr;
506 }
507}
508
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000509#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +0000510
511template <class _Tp, class _Alloc>
512__forward_list_base<_Tp, _Alloc>::~__forward_list_base()
513{
514 clear();
515}
516
517template <class _Tp, class _Alloc>
Howard Hinnant0af133f2010-09-21 22:55:27 +0000518inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000519void
520__forward_list_base<_Tp, _Alloc>::swap(__forward_list_base& __x)
Howard Hinnant91a47502011-06-03 16:20:53 +0000521 _NOEXCEPT_(!__node_traits::propagate_on_container_swap::value ||
522 __is_nothrow_swappable<__node_allocator>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +0000523{
524 __swap_alloc(__alloc(), __x.__alloc());
Howard Hinnantce48a112011-06-30 21:18:19 +0000525 using _VSTD::swap;
Howard Hinnant3e519522010-05-11 19:42:16 +0000526 swap(__before_begin()->__next_, __x.__before_begin()->__next_);
527}
528
529template <class _Tp, class _Alloc>
530void
Howard Hinnantf9dc2832011-06-02 16:44:28 +0000531__forward_list_base<_Tp, _Alloc>::clear() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000532{
533 __node_allocator& __a = __alloc();
534 for (__node_pointer __p = __before_begin()->__next_; __p != nullptr;)
535 {
536 __node_pointer __next = __p->__next_;
Howard Hinnantce48a112011-06-30 21:18:19 +0000537 __node_traits::destroy(__a, _VSTD::addressof(__p->__value_));
Howard Hinnant3e519522010-05-11 19:42:16 +0000538 __node_traits::deallocate(__a, __p, 1);
539 __p = __next;
540 }
541 __before_begin()->__next_ = nullptr;
542}
543
544template <class _Tp, class _Alloc = allocator<_Tp> >
Howard Hinnant6e412562013-03-06 23:30:19 +0000545class _LIBCPP_TYPE_VIS forward_list
Howard Hinnant3e519522010-05-11 19:42:16 +0000546 : private __forward_list_base<_Tp, _Alloc>
547{
548 typedef __forward_list_base<_Tp, _Alloc> base;
Howard Hinnant91a47502011-06-03 16:20:53 +0000549 typedef typename base::__node_allocator __node_allocator;
550 typedef typename base::__node __node;
551 typedef typename base::__node_traits __node_traits;
552 typedef typename base::__node_pointer __node_pointer;
553
Howard Hinnant3e519522010-05-11 19:42:16 +0000554public:
555 typedef _Tp value_type;
556 typedef _Alloc allocator_type;
557
558 typedef value_type& reference;
559 typedef const value_type& const_reference;
560 typedef typename allocator_traits<allocator_type>::pointer pointer;
561 typedef typename allocator_traits<allocator_type>::const_pointer const_pointer;
562 typedef typename allocator_traits<allocator_type>::size_type size_type;
563 typedef typename allocator_traits<allocator_type>::difference_type difference_type;
564
565 typedef typename base::iterator iterator;
566 typedef typename base::const_iterator const_iterator;
567
Howard Hinnant91a47502011-06-03 16:20:53 +0000568 _LIBCPP_INLINE_VISIBILITY
569 forward_list()
570 _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value)
571 {} // = default;
Howard Hinnant3e519522010-05-11 19:42:16 +0000572 explicit forward_list(const allocator_type& __a);
573 explicit forward_list(size_type __n);
574 forward_list(size_type __n, const value_type& __v);
575 forward_list(size_type __n, const value_type& __v, const allocator_type& __a);
576 template <class _InputIterator>
577 forward_list(_InputIterator __f, _InputIterator __l,
578 typename enable_if<
579 __is_input_iterator<_InputIterator>::value
580 >::type* = nullptr);
581 template <class _InputIterator>
582 forward_list(_InputIterator __f, _InputIterator __l,
583 const allocator_type& __a,
584 typename enable_if<
585 __is_input_iterator<_InputIterator>::value
586 >::type* = nullptr);
587 forward_list(const forward_list& __x);
588 forward_list(const forward_list& __x, const allocator_type& __a);
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000589#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant0af133f2010-09-21 22:55:27 +0000590 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant91a47502011-06-03 16:20:53 +0000591 forward_list(forward_list&& __x)
592 _NOEXCEPT_(is_nothrow_move_constructible<base>::value)
Howard Hinnantce48a112011-06-30 21:18:19 +0000593 : base(_VSTD::move(__x)) {}
Howard Hinnant3e519522010-05-11 19:42:16 +0000594 forward_list(forward_list&& __x, const allocator_type& __a);
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000595#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54976f22011-08-12 21:56:02 +0000596#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant3e519522010-05-11 19:42:16 +0000597 forward_list(initializer_list<value_type> __il);
598 forward_list(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnant54976f22011-08-12 21:56:02 +0000599#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant3e519522010-05-11 19:42:16 +0000600
601 // ~forward_list() = default;
602
603 forward_list& operator=(const forward_list& __x);
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000604#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant91a47502011-06-03 16:20:53 +0000605 forward_list& operator=(forward_list&& __x)
606 _NOEXCEPT_(
607 __node_traits::propagate_on_container_move_assignment::value &&
608 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant3e519522010-05-11 19:42:16 +0000609#endif
Howard Hinnant54976f22011-08-12 21:56:02 +0000610#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant3e519522010-05-11 19:42:16 +0000611 forward_list& operator=(initializer_list<value_type> __il);
Howard Hinnant54976f22011-08-12 21:56:02 +0000612#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant3e519522010-05-11 19:42:16 +0000613
614 template <class _InputIterator>
615 typename enable_if
616 <
617 __is_input_iterator<_InputIterator>::value,
618 void
619 >::type
620 assign(_InputIterator __f, _InputIterator __l);
621 void assign(size_type __n, const value_type& __v);
Howard Hinnant54976f22011-08-12 21:56:02 +0000622#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant3e519522010-05-11 19:42:16 +0000623 void assign(initializer_list<value_type> __il);
Howard Hinnant54976f22011-08-12 21:56:02 +0000624#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant3e519522010-05-11 19:42:16 +0000625
Howard Hinnant0af133f2010-09-21 22:55:27 +0000626 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf9dc2832011-06-02 16:44:28 +0000627 allocator_type get_allocator() const _NOEXCEPT
628 {return allocator_type(base::__alloc());}
Howard Hinnant3e519522010-05-11 19:42:16 +0000629
Howard Hinnant0af133f2010-09-21 22:55:27 +0000630 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf9dc2832011-06-02 16:44:28 +0000631 iterator begin() _NOEXCEPT
632 {return iterator(base::__before_begin()->__next_);}
Howard Hinnant0af133f2010-09-21 22:55:27 +0000633 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf9dc2832011-06-02 16:44:28 +0000634 const_iterator begin() const _NOEXCEPT
635 {return const_iterator(base::__before_begin()->__next_);}
Howard Hinnant0af133f2010-09-21 22:55:27 +0000636 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf9dc2832011-06-02 16:44:28 +0000637 iterator end() _NOEXCEPT
638 {return iterator(nullptr);}
Howard Hinnant0af133f2010-09-21 22:55:27 +0000639 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf9dc2832011-06-02 16:44:28 +0000640 const_iterator end() const _NOEXCEPT
641 {return const_iterator(nullptr);}
Howard Hinnant3e519522010-05-11 19:42:16 +0000642
Howard Hinnant0af133f2010-09-21 22:55:27 +0000643 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf9dc2832011-06-02 16:44:28 +0000644 const_iterator cbegin() const _NOEXCEPT
645 {return const_iterator(base::__before_begin()->__next_);}
Howard Hinnant0af133f2010-09-21 22:55:27 +0000646 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf9dc2832011-06-02 16:44:28 +0000647 const_iterator cend() const _NOEXCEPT
648 {return const_iterator(nullptr);}
Howard Hinnant3e519522010-05-11 19:42:16 +0000649
Howard Hinnant0af133f2010-09-21 22:55:27 +0000650 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf9dc2832011-06-02 16:44:28 +0000651 iterator before_begin() _NOEXCEPT
652 {return iterator(base::__before_begin());}
Howard Hinnant0af133f2010-09-21 22:55:27 +0000653 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf9dc2832011-06-02 16:44:28 +0000654 const_iterator before_begin() const _NOEXCEPT
655 {return const_iterator(base::__before_begin());}
Howard Hinnant0af133f2010-09-21 22:55:27 +0000656 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf9dc2832011-06-02 16:44:28 +0000657 const_iterator cbefore_begin() const _NOEXCEPT
658 {return const_iterator(base::__before_begin());}
Howard Hinnant3e519522010-05-11 19:42:16 +0000659
Howard Hinnant0af133f2010-09-21 22:55:27 +0000660 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf9dc2832011-06-02 16:44:28 +0000661 bool empty() const _NOEXCEPT
662 {return base::__before_begin()->__next_ == nullptr;}
Howard Hinnant0af133f2010-09-21 22:55:27 +0000663 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf9dc2832011-06-02 16:44:28 +0000664 size_type max_size() const _NOEXCEPT
665 {return numeric_limits<size_type>::max();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000666
Howard Hinnant0af133f2010-09-21 22:55:27 +0000667 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000668 reference front() {return base::__before_begin()->__next_->__value_;}
Howard Hinnant0af133f2010-09-21 22:55:27 +0000669 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000670 const_reference front() const {return base::__before_begin()->__next_->__value_;}
671
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000672#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
673#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnant3e519522010-05-11 19:42:16 +0000674 template <class... _Args> void emplace_front(_Args&&... __args);
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000675#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000676 void push_front(value_type&& __v);
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000677#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +0000678 void push_front(const value_type& __v);
679
680 void pop_front();
681
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000682#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
683#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnant3e519522010-05-11 19:42:16 +0000684 template <class... _Args>
685 iterator emplace_after(const_iterator __p, _Args&&... __args);
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000686#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnant3e519522010-05-11 19:42:16 +0000687 iterator insert_after(const_iterator __p, value_type&& __v);
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000688#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +0000689 iterator insert_after(const_iterator __p, const value_type& __v);
690 iterator insert_after(const_iterator __p, size_type __n, const value_type& __v);
691 template <class _InputIterator>
Howard Hinnant0af133f2010-09-21 22:55:27 +0000692 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000693 typename enable_if
694 <
695 __is_input_iterator<_InputIterator>::value,
696 iterator
697 >::type
698 insert_after(const_iterator __p, _InputIterator __f, _InputIterator __l);
Howard Hinnant54976f22011-08-12 21:56:02 +0000699#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant3e519522010-05-11 19:42:16 +0000700 iterator insert_after(const_iterator __p, initializer_list<value_type> __il)
701 {return insert_after(__p, __il.begin(), __il.end());}
Howard Hinnant54976f22011-08-12 21:56:02 +0000702#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant3e519522010-05-11 19:42:16 +0000703
Howard Hinnant3db88032010-08-21 20:58:44 +0000704 iterator erase_after(const_iterator __p);
705 iterator erase_after(const_iterator __f, const_iterator __l);
Howard Hinnant3e519522010-05-11 19:42:16 +0000706
Howard Hinnant0af133f2010-09-21 22:55:27 +0000707 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant91a47502011-06-03 16:20:53 +0000708 void swap(forward_list& __x)
709 _NOEXCEPT_(!__node_traits::propagate_on_container_swap::value ||
710 __is_nothrow_swappable<__node_allocator>::value)
711 {base::swap(__x);}
Howard Hinnant3e519522010-05-11 19:42:16 +0000712
713 void resize(size_type __n);
714 void resize(size_type __n, const value_type& __v);
Howard Hinnant0af133f2010-09-21 22:55:27 +0000715 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf9dc2832011-06-02 16:44:28 +0000716 void clear() _NOEXCEPT {base::clear();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000717
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000718#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnanteb92df72011-01-27 21:00:35 +0000719 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000720 void splice_after(const_iterator __p, forward_list&& __x);
Howard Hinnanteb92df72011-01-27 21:00:35 +0000721 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000722 void splice_after(const_iterator __p, forward_list&& __x, const_iterator __i);
Howard Hinnanteb92df72011-01-27 21:00:35 +0000723 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000724 void splice_after(const_iterator __p, forward_list&& __x,
725 const_iterator __f, const_iterator __l);
Howard Hinnanteb92df72011-01-27 21:00:35 +0000726#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +0000727 void splice_after(const_iterator __p, forward_list& __x);
728 void splice_after(const_iterator __p, forward_list& __x, const_iterator __i);
729 void splice_after(const_iterator __p, forward_list& __x,
730 const_iterator __f, const_iterator __l);
Howard Hinnant3e519522010-05-11 19:42:16 +0000731 void remove(const value_type& __v);
732 template <class _Predicate> void remove_if(_Predicate __pred);
Howard Hinnant0af133f2010-09-21 22:55:27 +0000733 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000734 void unique() {unique(__equal_to<value_type>());}
735 template <class _BinaryPredicate> void unique(_BinaryPredicate __binary_pred);
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000736#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant0af133f2010-09-21 22:55:27 +0000737 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteb92df72011-01-27 21:00:35 +0000738 void merge(forward_list&& __x) {merge(__x, __less<value_type>());}
739 template <class _Compare>
740 _LIBCPP_INLINE_VISIBILITY
741 void merge(forward_list&& __x, _Compare __comp)
Howard Hinnantce48a112011-06-30 21:18:19 +0000742 {merge(__x, _VSTD::move(__comp));}
Howard Hinnanteb92df72011-01-27 21:00:35 +0000743#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant0af133f2010-09-21 22:55:27 +0000744 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000745 void merge(forward_list& __x) {merge(__x, __less<value_type>());}
746 template <class _Compare> void merge(forward_list& __x, _Compare __comp);
Howard Hinnant0af133f2010-09-21 22:55:27 +0000747 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000748 void sort() {sort(__less<value_type>());}
749 template <class _Compare> void sort(_Compare __comp);
Howard Hinnantf9dc2832011-06-02 16:44:28 +0000750 void reverse() _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +0000751
752private:
Howard Hinnant3e519522010-05-11 19:42:16 +0000753
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000754#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant91a47502011-06-03 16:20:53 +0000755 void __move_assign(forward_list& __x, true_type)
756 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant3e519522010-05-11 19:42:16 +0000757 void __move_assign(forward_list& __x, false_type);
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000758#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +0000759
760 template <class _Compare>
761 static
762 __node_pointer
763 __merge(__node_pointer __f1, __node_pointer __f2, _Compare& __comp);
764
765 template <class _Compare>
766 static
767 __node_pointer
768 __sort(__node_pointer __f, difference_type __sz, _Compare& __comp);
769};
770
771template <class _Tp, class _Alloc>
Howard Hinnant0af133f2010-09-21 22:55:27 +0000772inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000773forward_list<_Tp, _Alloc>::forward_list(const allocator_type& __a)
774 : base(__a)
775{
776}
777
778template <class _Tp, class _Alloc>
779forward_list<_Tp, _Alloc>::forward_list(size_type __n)
780{
781 if (__n > 0)
782 {
783 __node_allocator& __a = base::__alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +0000784 typedef __allocator_destructor<__node_allocator> _Dp;
785 unique_ptr<__node, _Dp> __h(nullptr, _Dp(__a, 1));
Howard Hinnant3e519522010-05-11 19:42:16 +0000786 for (__node_pointer __p = base::__before_begin(); __n > 0; --__n,
787 __p = __p->__next_)
788 {
789 __h.reset(__node_traits::allocate(__a, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +0000790 __node_traits::construct(__a, _VSTD::addressof(__h->__value_));
Howard Hinnant3e519522010-05-11 19:42:16 +0000791 __h->__next_ = nullptr;
792 __p->__next_ = __h.release();
793 }
794 }
795}
796
797template <class _Tp, class _Alloc>
798forward_list<_Tp, _Alloc>::forward_list(size_type __n, const value_type& __v)
799{
800 insert_after(cbefore_begin(), __n, __v);
801}
802
803template <class _Tp, class _Alloc>
804forward_list<_Tp, _Alloc>::forward_list(size_type __n, const value_type& __v,
805 const allocator_type& __a)
806 : base(__a)
807{
808 insert_after(cbefore_begin(), __n, __v);
809}
810
811template <class _Tp, class _Alloc>
812template <class _InputIterator>
813forward_list<_Tp, _Alloc>::forward_list(_InputIterator __f, _InputIterator __l,
814 typename enable_if<
815 __is_input_iterator<_InputIterator>::value
816 >::type*)
817{
818 insert_after(cbefore_begin(), __f, __l);
819}
820
821template <class _Tp, class _Alloc>
822template <class _InputIterator>
823forward_list<_Tp, _Alloc>::forward_list(_InputIterator __f, _InputIterator __l,
824 const allocator_type& __a,
825 typename enable_if<
826 __is_input_iterator<_InputIterator>::value
827 >::type*)
828 : base(__a)
829{
830 insert_after(cbefore_begin(), __f, __l);
831}
832
833template <class _Tp, class _Alloc>
834forward_list<_Tp, _Alloc>::forward_list(const forward_list& __x)
835 : base(allocator_type(
836 __node_traits::select_on_container_copy_construction(__x.__alloc())
837 )
838 )
839{
840 insert_after(cbefore_begin(), __x.begin(), __x.end());
841}
842
843template <class _Tp, class _Alloc>
844forward_list<_Tp, _Alloc>::forward_list(const forward_list& __x,
845 const allocator_type& __a)
846 : base(__a)
847{
848 insert_after(cbefore_begin(), __x.begin(), __x.end());
849}
850
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000851#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +0000852
853template <class _Tp, class _Alloc>
854forward_list<_Tp, _Alloc>::forward_list(forward_list&& __x,
855 const allocator_type& __a)
Howard Hinnantce48a112011-06-30 21:18:19 +0000856 : base(_VSTD::move(__x), __a)
Howard Hinnant3e519522010-05-11 19:42:16 +0000857{
858 if (base::__alloc() != __x.__alloc())
859 {
Howard Hinnantc003db12011-11-29 18:15:50 +0000860 typedef move_iterator<iterator> _Ip;
861 insert_after(cbefore_begin(), _Ip(__x.begin()), _Ip(__x.end()));
Howard Hinnant3e519522010-05-11 19:42:16 +0000862 }
863}
864
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000865#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +0000866
Howard Hinnant54976f22011-08-12 21:56:02 +0000867#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
868
Howard Hinnant3e519522010-05-11 19:42:16 +0000869template <class _Tp, class _Alloc>
870forward_list<_Tp, _Alloc>::forward_list(initializer_list<value_type> __il)
871{
872 insert_after(cbefore_begin(), __il.begin(), __il.end());
873}
874
875template <class _Tp, class _Alloc>
876forward_list<_Tp, _Alloc>::forward_list(initializer_list<value_type> __il,
877 const allocator_type& __a)
878 : base(__a)
879{
880 insert_after(cbefore_begin(), __il.begin(), __il.end());
881}
882
Howard Hinnant54976f22011-08-12 21:56:02 +0000883#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
884
Howard Hinnant3e519522010-05-11 19:42:16 +0000885template <class _Tp, class _Alloc>
886forward_list<_Tp, _Alloc>&
887forward_list<_Tp, _Alloc>::operator=(const forward_list& __x)
888{
889 if (this != &__x)
890 {
891 base::__copy_assign_alloc(__x);
892 assign(__x.begin(), __x.end());
893 }
894 return *this;
895}
896
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000897#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +0000898
899template <class _Tp, class _Alloc>
900void
901forward_list<_Tp, _Alloc>::__move_assign(forward_list& __x, true_type)
Howard Hinnant91a47502011-06-03 16:20:53 +0000902 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +0000903{
904 clear();
905 base::__move_assign_alloc(__x);
906 base::__before_begin()->__next_ = __x.__before_begin()->__next_;
907 __x.__before_begin()->__next_ = nullptr;
908}
909
910template <class _Tp, class _Alloc>
911void
912forward_list<_Tp, _Alloc>::__move_assign(forward_list& __x, false_type)
913{
914 if (base::__alloc() == __x.__alloc())
915 __move_assign(__x, true_type());
916 else
917 {
Howard Hinnantc003db12011-11-29 18:15:50 +0000918 typedef move_iterator<iterator> _Ip;
919 assign(_Ip(__x.begin()), _Ip(__x.end()));
Howard Hinnant3e519522010-05-11 19:42:16 +0000920 }
921}
922
923template <class _Tp, class _Alloc>
Howard Hinnant0af133f2010-09-21 22:55:27 +0000924inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000925forward_list<_Tp, _Alloc>&
926forward_list<_Tp, _Alloc>::operator=(forward_list&& __x)
Howard Hinnant91a47502011-06-03 16:20:53 +0000927 _NOEXCEPT_(
928 __node_traits::propagate_on_container_move_assignment::value &&
929 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +0000930{
931 __move_assign(__x, integral_constant<bool,
932 __node_traits::propagate_on_container_move_assignment::value>());
933 return *this;
934}
935
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000936#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +0000937
Howard Hinnant54976f22011-08-12 21:56:02 +0000938#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
939
Howard Hinnant3e519522010-05-11 19:42:16 +0000940template <class _Tp, class _Alloc>
Howard Hinnant0af133f2010-09-21 22:55:27 +0000941inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000942forward_list<_Tp, _Alloc>&
943forward_list<_Tp, _Alloc>::operator=(initializer_list<value_type> __il)
944{
945 assign(__il.begin(), __il.end());
946 return *this;
947}
948
Howard Hinnant54976f22011-08-12 21:56:02 +0000949#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
950
Howard Hinnant3e519522010-05-11 19:42:16 +0000951template <class _Tp, class _Alloc>
952template <class _InputIterator>
953typename enable_if
954<
955 __is_input_iterator<_InputIterator>::value,
956 void
957>::type
958forward_list<_Tp, _Alloc>::assign(_InputIterator __f, _InputIterator __l)
959{
960 iterator __i = before_begin();
Howard Hinnantce48a112011-06-30 21:18:19 +0000961 iterator __j = _VSTD::next(__i);
Howard Hinnant3e519522010-05-11 19:42:16 +0000962 iterator __e = end();
963 for (; __j != __e && __f != __l; ++__i, ++__j, ++__f)
964 *__j = *__f;
965 if (__j == __e)
966 insert_after(__i, __f, __l);
967 else
968 erase_after(__i, __e);
969}
970
971template <class _Tp, class _Alloc>
972void
973forward_list<_Tp, _Alloc>::assign(size_type __n, const value_type& __v)
974{
975 iterator __i = before_begin();
Howard Hinnantce48a112011-06-30 21:18:19 +0000976 iterator __j = _VSTD::next(__i);
Howard Hinnant3e519522010-05-11 19:42:16 +0000977 iterator __e = end();
978 for (; __j != __e && __n > 0; --__n, ++__i, ++__j)
979 *__j = __v;
980 if (__j == __e)
981 insert_after(__i, __n, __v);
982 else
983 erase_after(__i, __e);
984}
985
Howard Hinnant54976f22011-08-12 21:56:02 +0000986#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
987
Howard Hinnant3e519522010-05-11 19:42:16 +0000988template <class _Tp, class _Alloc>
Howard Hinnant0af133f2010-09-21 22:55:27 +0000989inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000990void
991forward_list<_Tp, _Alloc>::assign(initializer_list<value_type> __il)
992{
993 assign(__il.begin(), __il.end());
994}
995
Howard Hinnant54976f22011-08-12 21:56:02 +0000996#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
997
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000998#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
999#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnant3e519522010-05-11 19:42:16 +00001000
1001template <class _Tp, class _Alloc>
1002template <class... _Args>
1003void
1004forward_list<_Tp, _Alloc>::emplace_front(_Args&&... __args)
1005{
1006 __node_allocator& __a = base::__alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001007 typedef __allocator_destructor<__node_allocator> _Dp;
1008 unique_ptr<__node, _Dp> __h(__node_traits::allocate(__a, 1), _Dp(__a, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001009 __node_traits::construct(__a, _VSTD::addressof(__h->__value_),
1010 _VSTD::forward<_Args>(__args)...);
Howard Hinnant3e519522010-05-11 19:42:16 +00001011 __h->__next_ = base::__before_begin()->__next_;
1012 base::__before_begin()->__next_ = __h.release();
1013}
1014
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001015#endif // _LIBCPP_HAS_NO_VARIADICS
1016
Howard Hinnant3e519522010-05-11 19:42:16 +00001017template <class _Tp, class _Alloc>
1018void
1019forward_list<_Tp, _Alloc>::push_front(value_type&& __v)
1020{
1021 __node_allocator& __a = base::__alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001022 typedef __allocator_destructor<__node_allocator> _Dp;
1023 unique_ptr<__node, _Dp> __h(__node_traits::allocate(__a, 1), _Dp(__a, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001024 __node_traits::construct(__a, _VSTD::addressof(__h->__value_), _VSTD::move(__v));
Howard Hinnant3e519522010-05-11 19:42:16 +00001025 __h->__next_ = base::__before_begin()->__next_;
1026 base::__before_begin()->__next_ = __h.release();
1027}
1028
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001029#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +00001030
1031template <class _Tp, class _Alloc>
1032void
1033forward_list<_Tp, _Alloc>::push_front(const value_type& __v)
1034{
1035 __node_allocator& __a = base::__alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001036 typedef __allocator_destructor<__node_allocator> _Dp;
1037 unique_ptr<__node, _Dp> __h(__node_traits::allocate(__a, 1), _Dp(__a, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001038 __node_traits::construct(__a, _VSTD::addressof(__h->__value_), __v);
Howard Hinnant3e519522010-05-11 19:42:16 +00001039 __h->__next_ = base::__before_begin()->__next_;
1040 base::__before_begin()->__next_ = __h.release();
1041}
1042
1043template <class _Tp, class _Alloc>
1044void
1045forward_list<_Tp, _Alloc>::pop_front()
1046{
1047 __node_allocator& __a = base::__alloc();
1048 __node_pointer __p = base::__before_begin()->__next_;
1049 base::__before_begin()->__next_ = __p->__next_;
Howard Hinnantce48a112011-06-30 21:18:19 +00001050 __node_traits::destroy(__a, _VSTD::addressof(__p->__value_));
Howard Hinnant3e519522010-05-11 19:42:16 +00001051 __node_traits::deallocate(__a, __p, 1);
1052}
1053
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001054#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1055#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnant3e519522010-05-11 19:42:16 +00001056
1057template <class _Tp, class _Alloc>
1058template <class... _Args>
1059typename forward_list<_Tp, _Alloc>::iterator
1060forward_list<_Tp, _Alloc>::emplace_after(const_iterator __p, _Args&&... __args)
1061{
Howard Hinnant8a27ba82013-06-24 17:17:28 +00001062 __node_pointer const __r = __p.__ptr_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001063 __node_allocator& __a = base::__alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001064 typedef __allocator_destructor<__node_allocator> _Dp;
1065 unique_ptr<__node, _Dp> __h(__node_traits::allocate(__a, 1), _Dp(__a, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001066 __node_traits::construct(__a, _VSTD::addressof(__h->__value_),
1067 _VSTD::forward<_Args>(__args)...);
Howard Hinnant3e519522010-05-11 19:42:16 +00001068 __h->__next_ = __r->__next_;
1069 __r->__next_ = __h.release();
1070 return iterator(__r->__next_);
1071}
1072
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001073#endif // _LIBCPP_HAS_NO_VARIADICS
1074
Howard Hinnant3e519522010-05-11 19:42:16 +00001075template <class _Tp, class _Alloc>
1076typename forward_list<_Tp, _Alloc>::iterator
1077forward_list<_Tp, _Alloc>::insert_after(const_iterator __p, value_type&& __v)
1078{
Howard Hinnant8a27ba82013-06-24 17:17:28 +00001079 __node_pointer const __r = __p.__ptr_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001080 __node_allocator& __a = base::__alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001081 typedef __allocator_destructor<__node_allocator> _Dp;
1082 unique_ptr<__node, _Dp> __h(__node_traits::allocate(__a, 1), _Dp(__a, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001083 __node_traits::construct(__a, _VSTD::addressof(__h->__value_), _VSTD::move(__v));
Howard Hinnant3e519522010-05-11 19:42:16 +00001084 __h->__next_ = __r->__next_;
1085 __r->__next_ = __h.release();
1086 return iterator(__r->__next_);
1087}
1088
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001089#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +00001090
1091template <class _Tp, class _Alloc>
1092typename forward_list<_Tp, _Alloc>::iterator
1093forward_list<_Tp, _Alloc>::insert_after(const_iterator __p, const value_type& __v)
1094{
Howard Hinnant8a27ba82013-06-24 17:17:28 +00001095 __node_pointer const __r = __p.__ptr_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001096 __node_allocator& __a = base::__alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001097 typedef __allocator_destructor<__node_allocator> _Dp;
1098 unique_ptr<__node, _Dp> __h(__node_traits::allocate(__a, 1), _Dp(__a, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001099 __node_traits::construct(__a, _VSTD::addressof(__h->__value_), __v);
Howard Hinnant3e519522010-05-11 19:42:16 +00001100 __h->__next_ = __r->__next_;
1101 __r->__next_ = __h.release();
1102 return iterator(__r->__next_);
1103}
1104
1105template <class _Tp, class _Alloc>
1106typename forward_list<_Tp, _Alloc>::iterator
1107forward_list<_Tp, _Alloc>::insert_after(const_iterator __p, size_type __n,
1108 const value_type& __v)
1109{
Howard Hinnant8a27ba82013-06-24 17:17:28 +00001110 __node_pointer __r = __p.__ptr_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001111 if (__n > 0)
1112 {
1113 __node_allocator& __a = base::__alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001114 typedef __allocator_destructor<__node_allocator> _Dp;
1115 unique_ptr<__node, _Dp> __h(__node_traits::allocate(__a, 1), _Dp(__a, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001116 __node_traits::construct(__a, _VSTD::addressof(__h->__value_), __v);
Howard Hinnant3e519522010-05-11 19:42:16 +00001117 __node_pointer __first = __h.release();
1118 __node_pointer __last = __first;
1119#ifndef _LIBCPP_NO_EXCEPTIONS
1120 try
1121 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001122#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001123 for (--__n; __n != 0; --__n, __last = __last->__next_)
1124 {
1125 __h.reset(__node_traits::allocate(__a, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001126 __node_traits::construct(__a, _VSTD::addressof(__h->__value_), __v);
Howard Hinnant3e519522010-05-11 19:42:16 +00001127 __last->__next_ = __h.release();
1128 }
1129#ifndef _LIBCPP_NO_EXCEPTIONS
1130 }
1131 catch (...)
1132 {
1133 while (__first != nullptr)
1134 {
1135 __node_pointer __next = __first->__next_;
Howard Hinnantce48a112011-06-30 21:18:19 +00001136 __node_traits::destroy(__a, _VSTD::addressof(__first->__value_));
Howard Hinnant3e519522010-05-11 19:42:16 +00001137 __node_traits::deallocate(__a, __first, 1);
1138 __first = __next;
1139 }
1140 throw;
1141 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001142#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001143 __last->__next_ = __r->__next_;
1144 __r->__next_ = __first;
Howard Hinnante57dc142010-08-19 17:40:04 +00001145 __r = __last;
Howard Hinnant3e519522010-05-11 19:42:16 +00001146 }
1147 return iterator(__r);
1148}
1149
1150template <class _Tp, class _Alloc>
1151template <class _InputIterator>
1152typename enable_if
1153<
1154 __is_input_iterator<_InputIterator>::value,
1155 typename forward_list<_Tp, _Alloc>::iterator
1156>::type
1157forward_list<_Tp, _Alloc>::insert_after(const_iterator __p,
1158 _InputIterator __f, _InputIterator __l)
1159{
Howard Hinnant8a27ba82013-06-24 17:17:28 +00001160 __node_pointer __r = __p.__ptr_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001161 if (__f != __l)
1162 {
1163 __node_allocator& __a = base::__alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001164 typedef __allocator_destructor<__node_allocator> _Dp;
1165 unique_ptr<__node, _Dp> __h(__node_traits::allocate(__a, 1), _Dp(__a, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001166 __node_traits::construct(__a, _VSTD::addressof(__h->__value_), *__f);
Howard Hinnant3e519522010-05-11 19:42:16 +00001167 __node_pointer __first = __h.release();
1168 __node_pointer __last = __first;
1169#ifndef _LIBCPP_NO_EXCEPTIONS
1170 try
1171 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001172#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001173 for (++__f; __f != __l; ++__f, __last = __last->__next_)
1174 {
1175 __h.reset(__node_traits::allocate(__a, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001176 __node_traits::construct(__a, _VSTD::addressof(__h->__value_), *__f);
Howard Hinnant3e519522010-05-11 19:42:16 +00001177 __last->__next_ = __h.release();
1178 }
1179#ifndef _LIBCPP_NO_EXCEPTIONS
1180 }
1181 catch (...)
1182 {
1183 while (__first != nullptr)
1184 {
1185 __node_pointer __next = __first->__next_;
Howard Hinnantce48a112011-06-30 21:18:19 +00001186 __node_traits::destroy(__a, _VSTD::addressof(__first->__value_));
Howard Hinnant3e519522010-05-11 19:42:16 +00001187 __node_traits::deallocate(__a, __first, 1);
1188 __first = __next;
1189 }
1190 throw;
1191 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001192#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001193 __last->__next_ = __r->__next_;
1194 __r->__next_ = __first;
Howard Hinnante57dc142010-08-19 17:40:04 +00001195 __r = __last;
Howard Hinnant3e519522010-05-11 19:42:16 +00001196 }
1197 return iterator(__r);
1198}
1199
1200template <class _Tp, class _Alloc>
Howard Hinnant3db88032010-08-21 20:58:44 +00001201typename forward_list<_Tp, _Alloc>::iterator
Howard Hinnant3e519522010-05-11 19:42:16 +00001202forward_list<_Tp, _Alloc>::erase_after(const_iterator __f)
1203{
Howard Hinnant8a27ba82013-06-24 17:17:28 +00001204 __node_pointer __p = __f.__ptr_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001205 __node_pointer __n = __p->__next_;
1206 __p->__next_ = __n->__next_;
1207 __node_allocator& __a = base::__alloc();
Howard Hinnantce48a112011-06-30 21:18:19 +00001208 __node_traits::destroy(__a, _VSTD::addressof(__n->__value_));
Howard Hinnant3e519522010-05-11 19:42:16 +00001209 __node_traits::deallocate(__a, __n, 1);
Howard Hinnant3db88032010-08-21 20:58:44 +00001210 return iterator(__p->__next_);
Howard Hinnant3e519522010-05-11 19:42:16 +00001211}
1212
1213template <class _Tp, class _Alloc>
Howard Hinnant3db88032010-08-21 20:58:44 +00001214typename forward_list<_Tp, _Alloc>::iterator
Howard Hinnant3e519522010-05-11 19:42:16 +00001215forward_list<_Tp, _Alloc>::erase_after(const_iterator __f, const_iterator __l)
1216{
Howard Hinnant8a27ba82013-06-24 17:17:28 +00001217 __node_pointer __e = __l.__ptr_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001218 if (__f != __l)
1219 {
Howard Hinnant8a27ba82013-06-24 17:17:28 +00001220 __node_pointer __p = __f.__ptr_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001221 __node_pointer __n = __p->__next_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001222 if (__n != __e)
1223 {
1224 __p->__next_ = __e;
1225 __node_allocator& __a = base::__alloc();
1226 do
1227 {
1228 __p = __n->__next_;
Howard Hinnantce48a112011-06-30 21:18:19 +00001229 __node_traits::destroy(__a, _VSTD::addressof(__n->__value_));
Howard Hinnant3e519522010-05-11 19:42:16 +00001230 __node_traits::deallocate(__a, __n, 1);
1231 __n = __p;
1232 } while (__n != __e);
1233 }
1234 }
Howard Hinnant3db88032010-08-21 20:58:44 +00001235 return iterator(__e);
Howard Hinnant3e519522010-05-11 19:42:16 +00001236}
1237
1238template <class _Tp, class _Alloc>
1239void
1240forward_list<_Tp, _Alloc>::resize(size_type __n)
1241{
1242 size_type __sz = 0;
1243 iterator __p = before_begin();
1244 iterator __i = begin();
1245 iterator __e = end();
1246 for (; __i != __e && __sz < __n; ++__p, ++__i, ++__sz)
1247 ;
1248 if (__i != __e)
1249 erase_after(__p, __e);
1250 else
1251 {
1252 __n -= __sz;
1253 if (__n > 0)
1254 {
1255 __node_allocator& __a = base::__alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001256 typedef __allocator_destructor<__node_allocator> _Dp;
1257 unique_ptr<__node, _Dp> __h(nullptr, _Dp(__a, 1));
Howard Hinnant3e519522010-05-11 19:42:16 +00001258 for (__node_pointer __ptr = __p.__ptr_; __n > 0; --__n,
1259 __ptr = __ptr->__next_)
1260 {
1261 __h.reset(__node_traits::allocate(__a, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001262 __node_traits::construct(__a, _VSTD::addressof(__h->__value_));
Howard Hinnant3e519522010-05-11 19:42:16 +00001263 __h->__next_ = nullptr;
1264 __ptr->__next_ = __h.release();
1265 }
1266 }
1267 }
1268}
1269
1270template <class _Tp, class _Alloc>
1271void
1272forward_list<_Tp, _Alloc>::resize(size_type __n, const value_type& __v)
1273{
1274 size_type __sz = 0;
1275 iterator __p = before_begin();
1276 iterator __i = begin();
1277 iterator __e = end();
1278 for (; __i != __e && __sz < __n; ++__p, ++__i, ++__sz)
1279 ;
1280 if (__i != __e)
1281 erase_after(__p, __e);
1282 else
1283 {
1284 __n -= __sz;
1285 if (__n > 0)
1286 {
1287 __node_allocator& __a = base::__alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001288 typedef __allocator_destructor<__node_allocator> _Dp;
1289 unique_ptr<__node, _Dp> __h(nullptr, _Dp(__a, 1));
Howard Hinnant3e519522010-05-11 19:42:16 +00001290 for (__node_pointer __ptr = __p.__ptr_; __n > 0; --__n,
1291 __ptr = __ptr->__next_)
1292 {
1293 __h.reset(__node_traits::allocate(__a, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001294 __node_traits::construct(__a, _VSTD::addressof(__h->__value_), __v);
Howard Hinnant3e519522010-05-11 19:42:16 +00001295 __h->__next_ = nullptr;
1296 __ptr->__next_ = __h.release();
1297 }
1298 }
1299 }
1300}
1301
1302template <class _Tp, class _Alloc>
1303void
1304forward_list<_Tp, _Alloc>::splice_after(const_iterator __p,
Howard Hinnant3e519522010-05-11 19:42:16 +00001305 forward_list& __x)
Howard Hinnant3e519522010-05-11 19:42:16 +00001306{
1307 if (!__x.empty())
1308 {
1309 if (__p.__ptr_->__next_ != nullptr)
1310 {
1311 const_iterator __lm1 = __x.before_begin();
1312 while (__lm1.__ptr_->__next_ != nullptr)
1313 ++__lm1;
Howard Hinnant8a27ba82013-06-24 17:17:28 +00001314 __lm1.__ptr_->__next_ = __p.__ptr_->__next_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001315 }
Howard Hinnant8a27ba82013-06-24 17:17:28 +00001316 __p.__ptr_->__next_ = __x.__before_begin()->__next_;
1317 __x.__before_begin()->__next_ = nullptr;
Howard Hinnant3e519522010-05-11 19:42:16 +00001318 }
1319}
1320
1321template <class _Tp, class _Alloc>
1322void
1323forward_list<_Tp, _Alloc>::splice_after(const_iterator __p,
Howard Hinnant3e519522010-05-11 19:42:16 +00001324 forward_list& __x,
Howard Hinnant3e519522010-05-11 19:42:16 +00001325 const_iterator __i)
1326{
Howard Hinnantce48a112011-06-30 21:18:19 +00001327 const_iterator __lm1 = _VSTD::next(__i);
Howard Hinnant3e519522010-05-11 19:42:16 +00001328 if (__p != __i && __p != __lm1)
1329 {
Howard Hinnant8a27ba82013-06-24 17:17:28 +00001330 __i.__ptr_->__next_ = __lm1.__ptr_->__next_;
1331 __lm1.__ptr_->__next_ = __p.__ptr_->__next_;
1332 __p.__ptr_->__next_ = __lm1.__ptr_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001333 }
1334}
1335
1336template <class _Tp, class _Alloc>
1337void
1338forward_list<_Tp, _Alloc>::splice_after(const_iterator __p,
Howard Hinnant3e519522010-05-11 19:42:16 +00001339 forward_list& __x,
Howard Hinnant3e519522010-05-11 19:42:16 +00001340 const_iterator __f, const_iterator __l)
1341{
1342 if (__f != __l && __p != __f)
1343 {
1344 const_iterator __lm1 = __f;
1345 while (__lm1.__ptr_->__next_ != __l.__ptr_)
1346 ++__lm1;
1347 if (__f != __lm1)
1348 {
Howard Hinnant8a27ba82013-06-24 17:17:28 +00001349 __lm1.__ptr_->__next_ = __p.__ptr_->__next_;
1350 __p.__ptr_->__next_ = __f.__ptr_->__next_;
1351 __f.__ptr_->__next_ = __l.__ptr_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001352 }
1353 }
1354}
1355
Howard Hinnanteb92df72011-01-27 21:00:35 +00001356#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1357
1358template <class _Tp, class _Alloc>
1359inline _LIBCPP_INLINE_VISIBILITY
1360void
1361forward_list<_Tp, _Alloc>::splice_after(const_iterator __p,
1362 forward_list&& __x)
1363{
1364 splice_after(__p, __x);
1365}
1366
1367template <class _Tp, class _Alloc>
1368inline _LIBCPP_INLINE_VISIBILITY
1369void
1370forward_list<_Tp, _Alloc>::splice_after(const_iterator __p,
1371 forward_list&& __x,
1372 const_iterator __i)
1373{
1374 splice_after(__p, __x, __i);
1375}
1376
1377template <class _Tp, class _Alloc>
1378inline _LIBCPP_INLINE_VISIBILITY
1379void
1380forward_list<_Tp, _Alloc>::splice_after(const_iterator __p,
1381 forward_list&& __x,
1382 const_iterator __f, const_iterator __l)
1383{
1384 splice_after(__p, __x, __f, __l);
1385}
1386
1387#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1388
Howard Hinnant3e519522010-05-11 19:42:16 +00001389template <class _Tp, class _Alloc>
1390void
1391forward_list<_Tp, _Alloc>::remove(const value_type& __v)
1392{
1393 iterator __e = end();
1394 for (iterator __i = before_begin(); __i.__ptr_->__next_ != nullptr;)
1395 {
1396 if (__i.__ptr_->__next_->__value_ == __v)
1397 {
Howard Hinnantce48a112011-06-30 21:18:19 +00001398 iterator __j = _VSTD::next(__i, 2);
Howard Hinnant3e519522010-05-11 19:42:16 +00001399 for (; __j != __e && *__j == __v; ++__j)
1400 ;
1401 erase_after(__i, __j);
1402 if (__j == __e)
1403 break;
1404 __i = __j;
1405 }
1406 else
1407 ++__i;
1408 }
1409}
1410
1411template <class _Tp, class _Alloc>
1412template <class _Predicate>
1413void
1414forward_list<_Tp, _Alloc>::remove_if(_Predicate __pred)
1415{
1416 iterator __e = end();
1417 for (iterator __i = before_begin(); __i.__ptr_->__next_ != nullptr;)
1418 {
1419 if (__pred(__i.__ptr_->__next_->__value_))
1420 {
Howard Hinnantce48a112011-06-30 21:18:19 +00001421 iterator __j = _VSTD::next(__i, 2);
Howard Hinnant3e519522010-05-11 19:42:16 +00001422 for (; __j != __e && __pred(*__j); ++__j)
1423 ;
1424 erase_after(__i, __j);
1425 if (__j == __e)
1426 break;
1427 __i = __j;
1428 }
1429 else
1430 ++__i;
1431 }
1432}
1433
1434template <class _Tp, class _Alloc>
1435template <class _BinaryPredicate>
1436void
1437forward_list<_Tp, _Alloc>::unique(_BinaryPredicate __binary_pred)
1438{
1439 for (iterator __i = begin(), __e = end(); __i != __e;)
1440 {
Howard Hinnantce48a112011-06-30 21:18:19 +00001441 iterator __j = _VSTD::next(__i);
Howard Hinnant3e519522010-05-11 19:42:16 +00001442 for (; __j != __e && __binary_pred(*__i, *__j); ++__j)
1443 ;
1444 if (__i.__ptr_->__next_ != __j.__ptr_)
1445 erase_after(__i, __j);
1446 __i = __j;
1447 }
1448}
1449
1450template <class _Tp, class _Alloc>
1451template <class _Compare>
1452void
Howard Hinnant3e519522010-05-11 19:42:16 +00001453forward_list<_Tp, _Alloc>::merge(forward_list& __x, _Compare __comp)
Howard Hinnant3e519522010-05-11 19:42:16 +00001454{
1455 if (this != &__x)
1456 {
1457 base::__before_begin()->__next_ = __merge(base::__before_begin()->__next_,
1458 __x.__before_begin()->__next_,
1459 __comp);
1460 __x.__before_begin()->__next_ = nullptr;
1461 }
1462}
1463
1464template <class _Tp, class _Alloc>
1465template <class _Compare>
1466typename forward_list<_Tp, _Alloc>::__node_pointer
1467forward_list<_Tp, _Alloc>::__merge(__node_pointer __f1, __node_pointer __f2,
1468 _Compare& __comp)
1469{
1470 if (__f1 == nullptr)
1471 return __f2;
1472 if (__f2 == nullptr)
1473 return __f1;
1474 __node_pointer __r;
1475 if (__comp(__f2->__value_, __f1->__value_))
1476 {
1477 __node_pointer __t = __f2;
1478 while (__t->__next_ != nullptr &&
1479 __comp(__t->__next_->__value_, __f1->__value_))
1480 __t = __t->__next_;
1481 __r = __f2;
1482 __f2 = __t->__next_;
1483 __t->__next_ = __f1;
1484 }
1485 else
1486 __r = __f1;
1487 __node_pointer __p = __f1;
1488 __f1 = __f1->__next_;
1489 while (__f1 != nullptr && __f2 != nullptr)
1490 {
1491 if (__comp(__f2->__value_, __f1->__value_))
1492 {
1493 __node_pointer __t = __f2;
1494 while (__t->__next_ != nullptr &&
1495 __comp(__t->__next_->__value_, __f1->__value_))
1496 __t = __t->__next_;
1497 __p->__next_ = __f2;
1498 __f2 = __t->__next_;
1499 __t->__next_ = __f1;
1500 }
1501 __p = __f1;
1502 __f1 = __f1->__next_;
1503 }
1504 if (__f2 != nullptr)
1505 __p->__next_ = __f2;
1506 return __r;
1507}
1508
1509template <class _Tp, class _Alloc>
1510template <class _Compare>
Howard Hinnant0af133f2010-09-21 22:55:27 +00001511inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001512void
1513forward_list<_Tp, _Alloc>::sort(_Compare __comp)
1514{
1515 base::__before_begin()->__next_ = __sort(base::__before_begin()->__next_,
Howard Hinnantce48a112011-06-30 21:18:19 +00001516 _VSTD::distance(begin(), end()), __comp);
Howard Hinnant3e519522010-05-11 19:42:16 +00001517}
1518
1519template <class _Tp, class _Alloc>
1520template <class _Compare>
1521typename forward_list<_Tp, _Alloc>::__node_pointer
1522forward_list<_Tp, _Alloc>::__sort(__node_pointer __f1, difference_type __sz,
1523 _Compare& __comp)
1524{
1525 switch (__sz)
1526 {
1527 case 0:
1528 case 1:
1529 return __f1;
1530 case 2:
1531 if (__comp(__f1->__next_->__value_, __f1->__value_))
1532 {
1533 __node_pointer __t = __f1->__next_;
1534 __t->__next_ = __f1;
1535 __f1->__next_ = nullptr;
1536 __f1 = __t;
1537 }
1538 return __f1;
1539 }
1540 difference_type __sz1 = __sz / 2;
1541 difference_type __sz2 = __sz - __sz1;
Howard Hinnantce48a112011-06-30 21:18:19 +00001542 __node_pointer __t = _VSTD::next(iterator(__f1), __sz1 - 1).__ptr_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001543 __node_pointer __f2 = __t->__next_;
1544 __t->__next_ = nullptr;
1545 return __merge(__sort(__f1, __sz1, __comp),
1546 __sort(__f2, __sz2, __comp), __comp);
1547}
1548
1549template <class _Tp, class _Alloc>
1550void
Howard Hinnantf9dc2832011-06-02 16:44:28 +00001551forward_list<_Tp, _Alloc>::reverse() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001552{
1553 __node_pointer __p = base::__before_begin()->__next_;
1554 if (__p != nullptr)
1555 {
1556 __node_pointer __f = __p->__next_;
1557 __p->__next_ = nullptr;
1558 while (__f != nullptr)
1559 {
1560 __node_pointer __t = __f->__next_;
1561 __f->__next_ = __p;
1562 __p = __f;
1563 __f = __t;
1564 }
1565 base::__before_begin()->__next_ = __p;
1566 }
1567}
1568
1569template <class _Tp, class _Alloc>
1570bool operator==(const forward_list<_Tp, _Alloc>& __x,
1571 const forward_list<_Tp, _Alloc>& __y)
1572{
Howard Hinnantc003db12011-11-29 18:15:50 +00001573 typedef forward_list<_Tp, _Alloc> _Cp;
1574 typedef typename _Cp::const_iterator _Ip;
1575 _Ip __ix = __x.begin();
1576 _Ip __ex = __x.end();
1577 _Ip __iy = __y.begin();
1578 _Ip __ey = __y.end();
Howard Hinnant3e519522010-05-11 19:42:16 +00001579 for (; __ix != __ex && __iy != __ey; ++__ix, ++__iy)
1580 if (!(*__ix == *__iy))
1581 return false;
1582 return (__ix == __ex) == (__iy == __ey);
1583}
1584
1585template <class _Tp, class _Alloc>
Howard Hinnant0af133f2010-09-21 22:55:27 +00001586inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001587bool operator!=(const forward_list<_Tp, _Alloc>& __x,
1588 const forward_list<_Tp, _Alloc>& __y)
1589{
1590 return !(__x == __y);
1591}
1592
1593template <class _Tp, class _Alloc>
Howard Hinnant0af133f2010-09-21 22:55:27 +00001594inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001595bool operator< (const forward_list<_Tp, _Alloc>& __x,
1596 const forward_list<_Tp, _Alloc>& __y)
1597{
Howard Hinnantce48a112011-06-30 21:18:19 +00001598 return _VSTD::lexicographical_compare(__x.begin(), __x.end(),
Howard Hinnant3e519522010-05-11 19:42:16 +00001599 __y.begin(), __y.end());
1600}
1601
1602template <class _Tp, class _Alloc>
Howard Hinnant0af133f2010-09-21 22:55:27 +00001603inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001604bool operator> (const forward_list<_Tp, _Alloc>& __x,
1605 const forward_list<_Tp, _Alloc>& __y)
1606{
1607 return __y < __x;
1608}
1609
1610template <class _Tp, class _Alloc>
Howard Hinnant0af133f2010-09-21 22:55:27 +00001611inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001612bool operator>=(const forward_list<_Tp, _Alloc>& __x,
1613 const forward_list<_Tp, _Alloc>& __y)
1614{
1615 return !(__x < __y);
1616}
1617
1618template <class _Tp, class _Alloc>
Howard Hinnant0af133f2010-09-21 22:55:27 +00001619inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001620bool operator<=(const forward_list<_Tp, _Alloc>& __x,
1621 const forward_list<_Tp, _Alloc>& __y)
1622{
1623 return !(__y < __x);
1624}
1625
1626template <class _Tp, class _Alloc>
Howard Hinnant0af133f2010-09-21 22:55:27 +00001627inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001628void
1629swap(forward_list<_Tp, _Alloc>& __x, forward_list<_Tp, _Alloc>& __y)
Howard Hinnant91a47502011-06-03 16:20:53 +00001630 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnant3e519522010-05-11 19:42:16 +00001631{
1632 __x.swap(__y);
1633}
1634
1635_LIBCPP_END_NAMESPACE_STD
1636
1637#endif // _LIBCPP_FORWARD_LIST