blob: 9c70fff946c7df803dddc679e42a8f6aa408f857 [file] [log] [blame]
Howard Hinnant3e519522010-05-11 19:42:16 +00001// -*- C++ -*-
2//===---------------------------- 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_LIST
12#define _LIBCPP_LIST
13
14/*
15 list synopsis
16
17namespace std
18{
19
20template <class T, class Alloc = allocator<T> >
21class list
22{
23public:
24
25 // types:
26 typedef T value_type;
27 typedef Alloc allocator_type;
28 typedef typename allocator_type::reference reference;
29 typedef typename allocator_type::const_reference const_reference;
30 typedef typename allocator_type::pointer pointer;
31 typedef typename allocator_type::const_pointer const_pointer;
32 typedef implementation-defined iterator;
33 typedef implementation-defined const_iterator;
34 typedef implementation-defined size_type;
35 typedef implementation-defined difference_type;
36 typedef reverse_iterator<iterator> reverse_iterator;
37 typedef reverse_iterator<const_iterator> const_reverse_iterator;
38
Howard Hinnant45900102011-06-03 17:30:28 +000039 list()
40 noexcept(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnant3e519522010-05-11 19:42:16 +000041 explicit list(const allocator_type& a);
42 explicit list(size_type n);
Marshall Clowf1b6d1b2013-09-09 18:19:45 +000043 explicit list(size_type n, const allocator_type& a); // C++14
Howard Hinnant3e519522010-05-11 19:42:16 +000044 list(size_type n, const value_type& value);
45 list(size_type n, const value_type& value, const allocator_type& a);
46 template <class Iter>
47 list(Iter first, Iter last);
48 template <class Iter>
49 list(Iter first, Iter last, const allocator_type& a);
50 list(const list& x);
51 list(const list&, const allocator_type& a);
Howard Hinnant45900102011-06-03 17:30:28 +000052 list(list&& x)
53 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnant3e519522010-05-11 19:42:16 +000054 list(list&&, const allocator_type& a);
55 list(initializer_list<value_type>);
56 list(initializer_list<value_type>, const allocator_type& a);
57
58 ~list();
59
60 list& operator=(const list& x);
Howard Hinnant45900102011-06-03 17:30:28 +000061 list& operator=(list&& x)
62 noexcept(
63 allocator_type::propagate_on_container_move_assignment::value &&
64 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant3e519522010-05-11 19:42:16 +000065 list& operator=(initializer_list<value_type>);
66 template <class Iter>
67 void assign(Iter first, Iter last);
68 void assign(size_type n, const value_type& t);
69 void assign(initializer_list<value_type>);
70
Howard Hinnant45900102011-06-03 17:30:28 +000071 allocator_type get_allocator() const noexcept;
Howard Hinnant3e519522010-05-11 19:42:16 +000072
Howard Hinnant45900102011-06-03 17:30:28 +000073 iterator begin() noexcept;
74 const_iterator begin() const noexcept;
75 iterator end() noexcept;
76 const_iterator end() const noexcept;
77 reverse_iterator rbegin() noexcept;
78 const_reverse_iterator rbegin() const noexcept;
79 reverse_iterator rend() noexcept;
80 const_reverse_iterator rend() const noexcept;
81 const_iterator cbegin() const noexcept;
82 const_iterator cend() const noexcept;
83 const_reverse_iterator crbegin() const noexcept;
84 const_reverse_iterator crend() const noexcept;
Howard Hinnant3e519522010-05-11 19:42:16 +000085
86 reference front();
87 const_reference front() const;
88 reference back();
89 const_reference back() const;
90
Howard Hinnant45900102011-06-03 17:30:28 +000091 bool empty() const noexcept;
92 size_type size() const noexcept;
93 size_type max_size() const noexcept;
Howard Hinnant3e519522010-05-11 19:42:16 +000094
95 template <class... Args>
Marshall Clow63b560b2017-01-24 23:09:12 +000096 reference emplace_front(Args&&... args); // reference in C++17
Howard Hinnant3e519522010-05-11 19:42:16 +000097 void pop_front();
98 template <class... Args>
Marshall Clow63b560b2017-01-24 23:09:12 +000099 reference emplace_back(Args&&... args); // reference in C++17
Howard Hinnant3e519522010-05-11 19:42:16 +0000100 void pop_back();
101 void push_front(const value_type& x);
102 void push_front(value_type&& x);
103 void push_back(const value_type& x);
104 void push_back(value_type&& x);
105 template <class... Args>
106 iterator emplace(const_iterator position, Args&&... args);
107 iterator insert(const_iterator position, const value_type& x);
108 iterator insert(const_iterator position, value_type&& x);
109 iterator insert(const_iterator position, size_type n, const value_type& x);
110 template <class Iter>
111 iterator insert(const_iterator position, Iter first, Iter last);
112 iterator insert(const_iterator position, initializer_list<value_type> il);
113
114 iterator erase(const_iterator position);
115 iterator erase(const_iterator position, const_iterator last);
116
117 void resize(size_type sz);
118 void resize(size_type sz, const value_type& c);
119
Howard Hinnant45900102011-06-03 17:30:28 +0000120 void swap(list&)
Marshall Clowe3fbe142015-07-13 20:04:56 +0000121 noexcept(allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnant45900102011-06-03 17:30:28 +0000122 void clear() noexcept;
Howard Hinnant3e519522010-05-11 19:42:16 +0000123
124 void splice(const_iterator position, list& x);
125 void splice(const_iterator position, list&& x);
126 void splice(const_iterator position, list& x, const_iterator i);
127 void splice(const_iterator position, list&& x, const_iterator i);
128 void splice(const_iterator position, list& x, const_iterator first,
129 const_iterator last);
130 void splice(const_iterator position, list&& x, const_iterator first,
131 const_iterator last);
132
133 void remove(const value_type& value);
134 template <class Pred> void remove_if(Pred pred);
135 void unique();
136 template <class BinaryPredicate>
137 void unique(BinaryPredicate binary_pred);
138 void merge(list& x);
139 void merge(list&& x);
140 template <class Compare>
141 void merge(list& x, Compare comp);
142 template <class Compare>
143 void merge(list&& x, Compare comp);
144 void sort();
145 template <class Compare>
146 void sort(Compare comp);
Howard Hinnant45900102011-06-03 17:30:28 +0000147 void reverse() noexcept;
Howard Hinnant3e519522010-05-11 19:42:16 +0000148};
149
150template <class T, class Alloc>
151 bool operator==(const list<T,Alloc>& x, const list<T,Alloc>& y);
152template <class T, class Alloc>
153 bool operator< (const list<T,Alloc>& x, const list<T,Alloc>& y);
154template <class T, class Alloc>
155 bool operator!=(const list<T,Alloc>& x, const list<T,Alloc>& y);
156template <class T, class Alloc>
157 bool operator> (const list<T,Alloc>& x, const list<T,Alloc>& y);
158template <class T, class Alloc>
159 bool operator>=(const list<T,Alloc>& x, const list<T,Alloc>& y);
160template <class T, class Alloc>
161 bool operator<=(const list<T,Alloc>& x, const list<T,Alloc>& y);
162
163template <class T, class Alloc>
Howard Hinnant45900102011-06-03 17:30:28 +0000164 void swap(list<T,Alloc>& x, list<T,Alloc>& y)
165 noexcept(noexcept(x.swap(y)));
Howard Hinnant3e519522010-05-11 19:42:16 +0000166
167} // std
168
169*/
170
171#include <__config>
172
173#include <memory>
174#include <limits>
175#include <initializer_list>
176#include <iterator>
177#include <algorithm>
Eric Fiselierb88ea352015-12-30 20:57:59 +0000178#include <type_traits>
Howard Hinnant3e519522010-05-11 19:42:16 +0000179
Eric Fiselierc1bd9192014-08-10 23:53:08 +0000180#include <__debug>
Howard Hinnant42a30462013-08-02 00:26:35 +0000181
Howard Hinnant073458b2011-10-17 20:05:10 +0000182#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnant3e519522010-05-11 19:42:16 +0000183#pragma GCC system_header
Howard Hinnant073458b2011-10-17 20:05:10 +0000184#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000185
Eric Fiseliera016efb2017-05-31 22:07:49 +0000186_LIBCPP_PUSH_MACROS
187#include <__undef_macros>
188
189
Howard Hinnant3e519522010-05-11 19:42:16 +0000190_LIBCPP_BEGIN_NAMESPACE_STD
191
Howard Hinnantce534202011-06-14 19:58:17 +0000192template <class _Tp, class _VoidPtr> struct __list_node;
Eric Fiselierb88ea352015-12-30 20:57:59 +0000193template <class _Tp, class _VoidPtr> struct __list_node_base;
194
195template <class _Tp, class _VoidPtr>
196struct __list_node_pointer_traits {
197 typedef typename __rebind_pointer<_VoidPtr, __list_node<_Tp, _VoidPtr> >::type
198 __node_pointer;
199 typedef typename __rebind_pointer<_VoidPtr, __list_node_base<_Tp, _VoidPtr> >::type
200 __base_pointer;
201
202#if defined(_LIBCPP_ABI_LIST_REMOVE_NODE_POINTER_UB)
203 typedef __base_pointer __link_pointer;
204#else
205 typedef typename conditional<
206 is_pointer<_VoidPtr>::value,
207 __base_pointer,
208 __node_pointer
209 >::type __link_pointer;
210#endif
211
Eric Fiselier5243e192016-01-04 03:27:52 +0000212 typedef typename conditional<
213 is_same<__link_pointer, __node_pointer>::value,
214 __base_pointer,
215 __node_pointer
216 >::type __non_link_pointer;
217
218 static _LIBCPP_INLINE_VISIBILITY
219 __link_pointer __unsafe_link_pointer_cast(__link_pointer __p) {
220 return __p;
221 }
222
223 static _LIBCPP_INLINE_VISIBILITY
224 __link_pointer __unsafe_link_pointer_cast(__non_link_pointer __p) {
225 return static_cast<__link_pointer>(static_cast<_VoidPtr>(__p));
226 }
227
Eric Fiselierb88ea352015-12-30 20:57:59 +0000228};
Howard Hinnant3e519522010-05-11 19:42:16 +0000229
230template <class _Tp, class _VoidPtr>
231struct __list_node_base
232{
Eric Fiselierb88ea352015-12-30 20:57:59 +0000233 typedef __list_node_pointer_traits<_Tp, _VoidPtr> _NodeTraits;
234 typedef typename _NodeTraits::__node_pointer __node_pointer;
235 typedef typename _NodeTraits::__base_pointer __base_pointer;
236 typedef typename _NodeTraits::__link_pointer __link_pointer;
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000237
Eric Fiselierb88ea352015-12-30 20:57:59 +0000238 __link_pointer __prev_;
239 __link_pointer __next_;
Howard Hinnant3e519522010-05-11 19:42:16 +0000240
Howard Hinnant848a5372010-09-22 16:48:34 +0000241 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5243e192016-01-04 03:27:52 +0000242 __list_node_base() : __prev_(_NodeTraits::__unsafe_link_pointer_cast(__self())),
243 __next_(_NodeTraits::__unsafe_link_pointer_cast(__self())) {}
Marshall Clow28d65da2014-08-05 01:34:12 +0000244
245 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5243e192016-01-04 03:27:52 +0000246 __base_pointer __self() {
Eric Fiselierb88ea352015-12-30 20:57:59 +0000247 return pointer_traits<__base_pointer>::pointer_to(*this);
248 }
249
250 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierb88ea352015-12-30 20:57:59 +0000251 __node_pointer __as_node() {
Eric Fiselier5243e192016-01-04 03:27:52 +0000252 return static_cast<__node_pointer>(__self());
Marshall Clow28d65da2014-08-05 01:34:12 +0000253 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000254};
255
256template <class _Tp, class _VoidPtr>
257struct __list_node
258 : public __list_node_base<_Tp, _VoidPtr>
259{
260 _Tp __value_;
Eric Fiselier5243e192016-01-04 03:27:52 +0000261
262 typedef __list_node_base<_Tp, _VoidPtr> __base;
263 typedef typename __base::__link_pointer __link_pointer;
264
265 _LIBCPP_INLINE_VISIBILITY
266 __link_pointer __as_link() {
267 return static_cast<__link_pointer>(__base::__self());
268 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000269};
270
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +0000271template <class _Tp, class _Alloc = allocator<_Tp> > class _LIBCPP_TEMPLATE_VIS list;
Howard Hinnantce534202011-06-14 19:58:17 +0000272template <class _Tp, class _Alloc> class __list_imp;
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +0000273template <class _Tp, class _VoidPtr> class _LIBCPP_TEMPLATE_VIS __list_const_iterator;
Howard Hinnant3e519522010-05-11 19:42:16 +0000274
275template <class _Tp, class _VoidPtr>
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +0000276class _LIBCPP_TEMPLATE_VIS __list_iterator
Howard Hinnant3e519522010-05-11 19:42:16 +0000277{
Eric Fiselierb88ea352015-12-30 20:57:59 +0000278 typedef __list_node_pointer_traits<_Tp, _VoidPtr> _NodeTraits;
279 typedef typename _NodeTraits::__link_pointer __link_pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000280
Eric Fiselierb88ea352015-12-30 20:57:59 +0000281 __link_pointer __ptr_;
Howard Hinnant3e519522010-05-11 19:42:16 +0000282
Howard Hinnant920b56c2011-09-27 23:55:03 +0000283#if _LIBCPP_DEBUG_LEVEL >= 2
284 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierb88ea352015-12-30 20:57:59 +0000285 explicit __list_iterator(__link_pointer __p, const void* __c) _NOEXCEPT
Howard Hinnant920b56c2011-09-27 23:55:03 +0000286 : __ptr_(__p)
287 {
288 __get_db()->__insert_ic(this, __c);
289 }
290#else
Howard Hinnant848a5372010-09-22 16:48:34 +0000291 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierb88ea352015-12-30 20:57:59 +0000292 explicit __list_iterator(__link_pointer __p) _NOEXCEPT : __ptr_(__p) {}
Howard Hinnant920b56c2011-09-27 23:55:03 +0000293#endif
294
295
Howard Hinnant3e519522010-05-11 19:42:16 +0000296
297 template<class, class> friend class list;
298 template<class, class> friend class __list_imp;
299 template<class, class> friend class __list_const_iterator;
300public:
301 typedef bidirectional_iterator_tag iterator_category;
302 typedef _Tp value_type;
303 typedef value_type& reference;
Eric Fiselier1c813402015-08-23 02:56:05 +0000304 typedef typename __rebind_pointer<_VoidPtr, value_type>::type pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000305 typedef typename pointer_traits<pointer>::difference_type difference_type;
306
Howard Hinnant848a5372010-09-22 16:48:34 +0000307 _LIBCPP_INLINE_VISIBILITY
Marshall Clow0c37cfd2013-08-05 21:23:28 +0000308 __list_iterator() _NOEXCEPT : __ptr_(nullptr)
Howard Hinnant920b56c2011-09-27 23:55:03 +0000309 {
310#if _LIBCPP_DEBUG_LEVEL >= 2
311 __get_db()->__insert_i(this);
312#endif
313 }
314
315#if _LIBCPP_DEBUG_LEVEL >= 2
316
Howard Hinnant27745452011-01-28 23:46:28 +0000317 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000318 __list_iterator(const __list_iterator& __p)
319 : __ptr_(__p.__ptr_)
320 {
321 __get_db()->__iterator_copy(this, &__p);
322 }
323
324 _LIBCPP_INLINE_VISIBILITY
325 ~__list_iterator()
326 {
327 __get_db()->__erase_i(this);
328 }
329
330 _LIBCPP_INLINE_VISIBILITY
331 __list_iterator& operator=(const __list_iterator& __p)
332 {
333 if (this != &__p)
334 {
335 __get_db()->__iterator_copy(this, &__p);
336 __ptr_ = __p.__ptr_;
337 }
338 return *this;
339 }
340
341#endif // _LIBCPP_DEBUG_LEVEL >= 2
342
343 _LIBCPP_INLINE_VISIBILITY
344 reference operator*() const
345 {
346#if _LIBCPP_DEBUG_LEVEL >= 2
347 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
348 "Attempted to dereference a non-dereferenceable list::iterator");
349#endif
Eric Fiselierb88ea352015-12-30 20:57:59 +0000350 return __ptr_->__as_node()->__value_;
Howard Hinnant920b56c2011-09-27 23:55:03 +0000351 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000352 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000353 pointer operator->() const
354 {
355#if _LIBCPP_DEBUG_LEVEL >= 2
356 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
357 "Attempted to dereference a non-dereferenceable list::iterator");
358#endif
Eric Fiselierb88ea352015-12-30 20:57:59 +0000359 return pointer_traits<pointer>::pointer_to(__ptr_->__as_node()->__value_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000360 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000361
Howard Hinnant848a5372010-09-22 16:48:34 +0000362 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000363 __list_iterator& operator++()
364 {
365#if _LIBCPP_DEBUG_LEVEL >= 2
366 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
367 "Attempted to increment non-incrementable list::iterator");
368#endif
369 __ptr_ = __ptr_->__next_;
370 return *this;
371 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000372 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000373 __list_iterator operator++(int) {__list_iterator __t(*this); ++(*this); return __t;}
374
Howard Hinnant848a5372010-09-22 16:48:34 +0000375 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000376 __list_iterator& operator--()
377 {
378#if _LIBCPP_DEBUG_LEVEL >= 2
379 _LIBCPP_ASSERT(__get_const_db()->__decrementable(this),
380 "Attempted to decrement non-decrementable list::iterator");
381#endif
382 __ptr_ = __ptr_->__prev_;
383 return *this;
384 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000385 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000386 __list_iterator operator--(int) {__list_iterator __t(*this); --(*this); return __t;}
387
Howard Hinnant848a5372010-09-22 16:48:34 +0000388 friend _LIBCPP_INLINE_VISIBILITY
389 bool operator==(const __list_iterator& __x, const __list_iterator& __y)
Howard Hinnant920b56c2011-09-27 23:55:03 +0000390 {
Howard Hinnant920b56c2011-09-27 23:55:03 +0000391 return __x.__ptr_ == __y.__ptr_;
392 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000393 friend _LIBCPP_INLINE_VISIBILITY
394 bool operator!=(const __list_iterator& __x, const __list_iterator& __y)
Howard Hinnant3e519522010-05-11 19:42:16 +0000395 {return !(__x == __y);}
396};
397
398template <class _Tp, class _VoidPtr>
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +0000399class _LIBCPP_TEMPLATE_VIS __list_const_iterator
Howard Hinnant3e519522010-05-11 19:42:16 +0000400{
Eric Fiselierb88ea352015-12-30 20:57:59 +0000401 typedef __list_node_pointer_traits<_Tp, _VoidPtr> _NodeTraits;
402 typedef typename _NodeTraits::__link_pointer __link_pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000403
Eric Fiselierb88ea352015-12-30 20:57:59 +0000404 __link_pointer __ptr_;
Howard Hinnant3e519522010-05-11 19:42:16 +0000405
Howard Hinnant920b56c2011-09-27 23:55:03 +0000406#if _LIBCPP_DEBUG_LEVEL >= 2
407 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierb88ea352015-12-30 20:57:59 +0000408 explicit __list_const_iterator(__link_pointer __p, const void* __c) _NOEXCEPT
Howard Hinnant920b56c2011-09-27 23:55:03 +0000409 : __ptr_(__p)
410 {
411 __get_db()->__insert_ic(this, __c);
412 }
413#else
Howard Hinnant848a5372010-09-22 16:48:34 +0000414 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierb88ea352015-12-30 20:57:59 +0000415 explicit __list_const_iterator(__link_pointer __p) _NOEXCEPT : __ptr_(__p) {}
Howard Hinnant920b56c2011-09-27 23:55:03 +0000416#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000417
418 template<class, class> friend class list;
419 template<class, class> friend class __list_imp;
420public:
421 typedef bidirectional_iterator_tag iterator_category;
422 typedef _Tp value_type;
423 typedef const value_type& reference;
Eric Fiselier1c813402015-08-23 02:56:05 +0000424 typedef typename __rebind_pointer<_VoidPtr, const value_type>::type pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000425 typedef typename pointer_traits<pointer>::difference_type difference_type;
426
Howard Hinnant848a5372010-09-22 16:48:34 +0000427 _LIBCPP_INLINE_VISIBILITY
Marshall Clow0c37cfd2013-08-05 21:23:28 +0000428 __list_const_iterator() _NOEXCEPT : __ptr_(nullptr)
Howard Hinnant920b56c2011-09-27 23:55:03 +0000429 {
430#if _LIBCPP_DEBUG_LEVEL >= 2
431 __get_db()->__insert_i(this);
432#endif
433 }
Howard Hinnant27745452011-01-28 23:46:28 +0000434 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf7509232013-04-05 17:58:52 +0000435 __list_const_iterator(const __list_iterator<_Tp, _VoidPtr>& __p) _NOEXCEPT
Howard Hinnant920b56c2011-09-27 23:55:03 +0000436 : __ptr_(__p.__ptr_)
437 {
438#if _LIBCPP_DEBUG_LEVEL >= 2
439 __get_db()->__iterator_copy(this, &__p);
440#endif
441 }
442
443#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant3e519522010-05-11 19:42:16 +0000444
Howard Hinnant848a5372010-09-22 16:48:34 +0000445 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000446 __list_const_iterator(const __list_const_iterator& __p)
447 : __ptr_(__p.__ptr_)
448 {
449 __get_db()->__iterator_copy(this, &__p);
450 }
451
452 _LIBCPP_INLINE_VISIBILITY
453 ~__list_const_iterator()
454 {
455 __get_db()->__erase_i(this);
456 }
457
458 _LIBCPP_INLINE_VISIBILITY
459 __list_const_iterator& operator=(const __list_const_iterator& __p)
460 {
461 if (this != &__p)
462 {
463 __get_db()->__iterator_copy(this, &__p);
464 __ptr_ = __p.__ptr_;
465 }
466 return *this;
467 }
468
469#endif // _LIBCPP_DEBUG_LEVEL >= 2
470 _LIBCPP_INLINE_VISIBILITY
471 reference operator*() const
472 {
473#if _LIBCPP_DEBUG_LEVEL >= 2
474 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
475 "Attempted to dereference a non-dereferenceable list::const_iterator");
476#endif
Eric Fiselierb88ea352015-12-30 20:57:59 +0000477 return __ptr_->__as_node()->__value_;
Howard Hinnant920b56c2011-09-27 23:55:03 +0000478 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000479 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000480 pointer operator->() const
481 {
482#if _LIBCPP_DEBUG_LEVEL >= 2
483 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
484 "Attempted to dereference a non-dereferenceable list::iterator");
485#endif
Eric Fiselierb88ea352015-12-30 20:57:59 +0000486 return pointer_traits<pointer>::pointer_to(__ptr_->__as_node()->__value_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000487 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000488
Howard Hinnant848a5372010-09-22 16:48:34 +0000489 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000490 __list_const_iterator& operator++()
491 {
492#if _LIBCPP_DEBUG_LEVEL >= 2
493 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
494 "Attempted to increment non-incrementable list::const_iterator");
495#endif
496 __ptr_ = __ptr_->__next_;
497 return *this;
498 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000499 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000500 __list_const_iterator operator++(int) {__list_const_iterator __t(*this); ++(*this); return __t;}
501
Howard Hinnant848a5372010-09-22 16:48:34 +0000502 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000503 __list_const_iterator& operator--()
504 {
505#if _LIBCPP_DEBUG_LEVEL >= 2
506 _LIBCPP_ASSERT(__get_const_db()->__decrementable(this),
507 "Attempted to decrement non-decrementable list::const_iterator");
508#endif
509 __ptr_ = __ptr_->__prev_;
510 return *this;
511 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000512 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000513 __list_const_iterator operator--(int) {__list_const_iterator __t(*this); --(*this); return __t;}
514
Howard Hinnant848a5372010-09-22 16:48:34 +0000515 friend _LIBCPP_INLINE_VISIBILITY
516 bool operator==(const __list_const_iterator& __x, const __list_const_iterator& __y)
Howard Hinnant920b56c2011-09-27 23:55:03 +0000517 {
Howard Hinnant920b56c2011-09-27 23:55:03 +0000518 return __x.__ptr_ == __y.__ptr_;
519 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000520 friend _LIBCPP_INLINE_VISIBILITY
521 bool operator!=(const __list_const_iterator& __x, const __list_const_iterator& __y)
Howard Hinnant3e519522010-05-11 19:42:16 +0000522 {return !(__x == __y);}
523};
524
525template <class _Tp, class _Alloc>
526class __list_imp
527{
528 __list_imp(const __list_imp&);
529 __list_imp& operator=(const __list_imp&);
530protected:
531 typedef _Tp value_type;
532 typedef _Alloc allocator_type;
533 typedef allocator_traits<allocator_type> __alloc_traits;
534 typedef typename __alloc_traits::size_type size_type;
535 typedef typename __alloc_traits::void_pointer __void_pointer;
536 typedef __list_iterator<value_type, __void_pointer> iterator;
537 typedef __list_const_iterator<value_type, __void_pointer> const_iterator;
538 typedef __list_node_base<value_type, __void_pointer> __node_base;
539 typedef __list_node<value_type, __void_pointer> __node;
Marshall Clow1f508012015-04-07 05:21:38 +0000540 typedef typename __rebind_alloc_helper<__alloc_traits, __node>::type __node_allocator;
Howard Hinnant3e519522010-05-11 19:42:16 +0000541 typedef allocator_traits<__node_allocator> __node_alloc_traits;
542 typedef typename __node_alloc_traits::pointer __node_pointer;
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000543 typedef typename __node_alloc_traits::pointer __node_const_pointer;
Eric Fiselier5243e192016-01-04 03:27:52 +0000544 typedef __list_node_pointer_traits<value_type, __void_pointer> __node_pointer_traits;
545 typedef typename __node_pointer_traits::__link_pointer __link_pointer;
Eric Fiselierb88ea352015-12-30 20:57:59 +0000546 typedef __link_pointer __link_const_pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000547 typedef typename __alloc_traits::pointer pointer;
548 typedef typename __alloc_traits::const_pointer const_pointer;
549 typedef typename __alloc_traits::difference_type difference_type;
550
Marshall Clow1f508012015-04-07 05:21:38 +0000551 typedef typename __rebind_alloc_helper<__alloc_traits, __node_base>::type __node_base_allocator;
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000552 typedef typename allocator_traits<__node_base_allocator>::pointer __node_base_pointer;
553
Howard Hinnant3e519522010-05-11 19:42:16 +0000554 __node_base __end_;
555 __compressed_pair<size_type, __node_allocator> __size_alloc_;
556
Howard Hinnant848a5372010-09-22 16:48:34 +0000557 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5243e192016-01-04 03:27:52 +0000558 __link_pointer __end_as_link() const _NOEXCEPT {
559 return __node_pointer_traits::__unsafe_link_pointer_cast(
560 const_cast<__node_base&>(__end_).__self());
561 }
562
563 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000564 size_type& __sz() _NOEXCEPT {return __size_alloc_.first();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000565 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000566 const size_type& __sz() const _NOEXCEPT
567 {return __size_alloc_.first();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000568 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000569 __node_allocator& __node_alloc() _NOEXCEPT
570 {return __size_alloc_.second();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000571 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000572 const __node_allocator& __node_alloc() const _NOEXCEPT
573 {return __size_alloc_.second();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000574
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +0000575 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier55b31b4e2016-11-23 01:18:56 +0000576 size_type __node_alloc_max_size() const _NOEXCEPT {
577 return __node_alloc_traits::max_size(__node_alloc());
578 }
579 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierb88ea352015-12-30 20:57:59 +0000580 static void __unlink_nodes(__link_pointer __f, __link_pointer __l) _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +0000581
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +0000582 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000583 __list_imp()
584 _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value);
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +0000585 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000586 __list_imp(const allocator_type& __a);
587 ~__list_imp();
Howard Hinnant45900102011-06-03 17:30:28 +0000588 void clear() _NOEXCEPT;
Howard Hinnant848a5372010-09-22 16:48:34 +0000589 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000590 bool empty() const _NOEXCEPT {return __sz() == 0;}
Howard Hinnant3e519522010-05-11 19:42:16 +0000591
Howard Hinnant848a5372010-09-22 16:48:34 +0000592 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000593 iterator begin() _NOEXCEPT
594 {
595#if _LIBCPP_DEBUG_LEVEL >= 2
596 return iterator(__end_.__next_, this);
597#else
598 return iterator(__end_.__next_);
599#endif
600 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000601 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000602 const_iterator begin() const _NOEXCEPT
Howard Hinnant920b56c2011-09-27 23:55:03 +0000603 {
604#if _LIBCPP_DEBUG_LEVEL >= 2
605 return const_iterator(__end_.__next_, this);
606#else
607 return const_iterator(__end_.__next_);
608#endif
609 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000610 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000611 iterator end() _NOEXCEPT
612 {
613#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselier5243e192016-01-04 03:27:52 +0000614 return iterator(__end_as_link(), this);
Howard Hinnant920b56c2011-09-27 23:55:03 +0000615#else
Eric Fiselier5243e192016-01-04 03:27:52 +0000616 return iterator(__end_as_link());
Howard Hinnant920b56c2011-09-27 23:55:03 +0000617#endif
618 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000619 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000620 const_iterator end() const _NOEXCEPT
Howard Hinnant920b56c2011-09-27 23:55:03 +0000621 {
622#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselier5243e192016-01-04 03:27:52 +0000623 return const_iterator(__end_as_link(), this);
Howard Hinnant920b56c2011-09-27 23:55:03 +0000624#else
Eric Fiselier5243e192016-01-04 03:27:52 +0000625 return const_iterator(__end_as_link());
Howard Hinnant920b56c2011-09-27 23:55:03 +0000626#endif
627 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000628
Howard Hinnant45900102011-06-03 17:30:28 +0000629 void swap(__list_imp& __c)
Marshall Clowe3fbe142015-07-13 20:04:56 +0000630#if _LIBCPP_STD_VER >= 14
Eric Fiselier2e519572016-12-28 06:06:09 +0000631 _NOEXCEPT_DEBUG;
Marshall Clowe3fbe142015-07-13 20:04:56 +0000632#else
Eric Fiselier2e519572016-12-28 06:06:09 +0000633 _NOEXCEPT_DEBUG_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clowe3fbe142015-07-13 20:04:56 +0000634 __is_nothrow_swappable<allocator_type>::value);
635#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000636
Howard Hinnant848a5372010-09-22 16:48:34 +0000637 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000638 void __copy_assign_alloc(const __list_imp& __c)
639 {__copy_assign_alloc(__c, integral_constant<bool,
640 __node_alloc_traits::propagate_on_container_copy_assignment::value>());}
641
Howard Hinnant848a5372010-09-22 16:48:34 +0000642 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000643 void __move_assign_alloc(__list_imp& __c)
Howard Hinnant45900102011-06-03 17:30:28 +0000644 _NOEXCEPT_(
645 !__node_alloc_traits::propagate_on_container_move_assignment::value ||
646 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +0000647 {__move_assign_alloc(__c, integral_constant<bool,
648 __node_alloc_traits::propagate_on_container_move_assignment::value>());}
649
650private:
Howard Hinnant848a5372010-09-22 16:48:34 +0000651 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000652 void __copy_assign_alloc(const __list_imp& __c, true_type)
653 {
654 if (__node_alloc() != __c.__node_alloc())
655 clear();
656 __node_alloc() = __c.__node_alloc();
657 }
658
Howard Hinnant848a5372010-09-22 16:48:34 +0000659 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierfd838222016-12-23 23:37:52 +0000660 void __copy_assign_alloc(const __list_imp&, false_type)
Howard Hinnant3e519522010-05-11 19:42:16 +0000661 {}
662
Howard Hinnant848a5372010-09-22 16:48:34 +0000663 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant86681392011-09-02 20:42:31 +0000664 void __move_assign_alloc(__list_imp& __c, true_type)
Howard Hinnant45900102011-06-03 17:30:28 +0000665 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +0000666 {
Howard Hinnantce48a112011-06-30 21:18:19 +0000667 __node_alloc() = _VSTD::move(__c.__node_alloc());
Howard Hinnant3e519522010-05-11 19:42:16 +0000668 }
669
Howard Hinnant848a5372010-09-22 16:48:34 +0000670 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierfd838222016-12-23 23:37:52 +0000671 void __move_assign_alloc(__list_imp&, false_type)
Howard Hinnant45900102011-06-03 17:30:28 +0000672 _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000673 {}
Eric Fiselier2e519572016-12-28 06:06:09 +0000674
675 _LIBCPP_INLINE_VISIBILITY
676 void __invalidate_all_iterators() {
677#if _LIBCPP_DEBUG_LEVEL >= 2
678 __get_db()->__invalidate_all(this);
679#endif
680 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000681};
682
683// Unlink nodes [__f, __l]
684template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +0000685inline
Howard Hinnant3e519522010-05-11 19:42:16 +0000686void
Eric Fiselierb88ea352015-12-30 20:57:59 +0000687__list_imp<_Tp, _Alloc>::__unlink_nodes(__link_pointer __f, __link_pointer __l)
Howard Hinnant45900102011-06-03 17:30:28 +0000688 _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000689{
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000690 __f->__prev_->__next_ = __l->__next_;
691 __l->__next_->__prev_ = __f->__prev_;
Howard Hinnant3e519522010-05-11 19:42:16 +0000692}
693
694template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +0000695inline
Howard Hinnant3e519522010-05-11 19:42:16 +0000696__list_imp<_Tp, _Alloc>::__list_imp()
Howard Hinnant45900102011-06-03 17:30:28 +0000697 _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +0000698 : __size_alloc_(0)
699{
700}
701
702template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +0000703inline
Howard Hinnant3e519522010-05-11 19:42:16 +0000704__list_imp<_Tp, _Alloc>::__list_imp(const allocator_type& __a)
705 : __size_alloc_(0, __node_allocator(__a))
706{
707}
708
709template <class _Tp, class _Alloc>
710__list_imp<_Tp, _Alloc>::~__list_imp()
711{
712 clear();
Howard Hinnant920b56c2011-09-27 23:55:03 +0000713#if _LIBCPP_DEBUG_LEVEL >= 2
714 __get_db()->__erase_c(this);
715#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000716}
717
718template <class _Tp, class _Alloc>
719void
Howard Hinnant45900102011-06-03 17:30:28 +0000720__list_imp<_Tp, _Alloc>::clear() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000721{
722 if (!empty())
723 {
724 __node_allocator& __na = __node_alloc();
Eric Fiselierb88ea352015-12-30 20:57:59 +0000725 __link_pointer __f = __end_.__next_;
Eric Fiselier5243e192016-01-04 03:27:52 +0000726 __link_pointer __l = __end_as_link();
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000727 __unlink_nodes(__f, __l->__prev_);
Howard Hinnant3e519522010-05-11 19:42:16 +0000728 __sz() = 0;
729 while (__f != __l)
730 {
Eric Fiselierb88ea352015-12-30 20:57:59 +0000731 __node_pointer __np = __f->__as_node();
Howard Hinnant920b56c2011-09-27 23:55:03 +0000732 __f = __f->__next_;
Eric Fiselierb88ea352015-12-30 20:57:59 +0000733 __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
734 __node_alloc_traits::deallocate(__na, __np, 1);
Howard Hinnant3e519522010-05-11 19:42:16 +0000735 }
Eric Fiselier2e519572016-12-28 06:06:09 +0000736 __invalidate_all_iterators();
Howard Hinnant3e519522010-05-11 19:42:16 +0000737 }
738}
739
740template <class _Tp, class _Alloc>
741void
742__list_imp<_Tp, _Alloc>::swap(__list_imp& __c)
Marshall Clowe3fbe142015-07-13 20:04:56 +0000743#if _LIBCPP_STD_VER >= 14
Eric Fiselier2e519572016-12-28 06:06:09 +0000744 _NOEXCEPT_DEBUG
Marshall Clowe3fbe142015-07-13 20:04:56 +0000745#else
Eric Fiselier2e519572016-12-28 06:06:09 +0000746 _NOEXCEPT_DEBUG_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clowe3fbe142015-07-13 20:04:56 +0000747 __is_nothrow_swappable<allocator_type>::value)
748#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000749{
Howard Hinnant920b56c2011-09-27 23:55:03 +0000750 _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
751 this->__node_alloc() == __c.__node_alloc(),
752 "list::swap: Either propagate_on_container_swap must be true"
753 " or the allocators must compare equal");
Howard Hinnantce48a112011-06-30 21:18:19 +0000754 using _VSTD::swap;
Marshall Clowe3fbe142015-07-13 20:04:56 +0000755 __swap_allocator(__node_alloc(), __c.__node_alloc());
Howard Hinnant3e519522010-05-11 19:42:16 +0000756 swap(__sz(), __c.__sz());
757 swap(__end_, __c.__end_);
758 if (__sz() == 0)
Eric Fiselier5243e192016-01-04 03:27:52 +0000759 __end_.__next_ = __end_.__prev_ = __end_as_link();
Howard Hinnant3e519522010-05-11 19:42:16 +0000760 else
Eric Fiselier5243e192016-01-04 03:27:52 +0000761 __end_.__prev_->__next_ = __end_.__next_->__prev_ = __end_as_link();
Howard Hinnant3e519522010-05-11 19:42:16 +0000762 if (__c.__sz() == 0)
Eric Fiselier5243e192016-01-04 03:27:52 +0000763 __c.__end_.__next_ = __c.__end_.__prev_ = __c.__end_as_link();
Howard Hinnant3e519522010-05-11 19:42:16 +0000764 else
Eric Fiselier5243e192016-01-04 03:27:52 +0000765 __c.__end_.__prev_->__next_ = __c.__end_.__next_->__prev_ = __c.__end_as_link();
Marshall Clow28d65da2014-08-05 01:34:12 +0000766
Howard Hinnant920b56c2011-09-27 23:55:03 +0000767#if _LIBCPP_DEBUG_LEVEL >= 2
768 __libcpp_db* __db = __get_db();
769 __c_node* __cn1 = __db->__find_c_and_lock(this);
770 __c_node* __cn2 = __db->__find_c(&__c);
771 std::swap(__cn1->beg_, __cn2->beg_);
772 std::swap(__cn1->end_, __cn2->end_);
773 std::swap(__cn1->cap_, __cn2->cap_);
774 for (__i_node** __p = __cn1->end_; __p != __cn1->beg_;)
775 {
776 --__p;
777 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
Eric Fiselier5243e192016-01-04 03:27:52 +0000778 if (__i->__ptr_ == __c.__end_as_link())
Howard Hinnant920b56c2011-09-27 23:55:03 +0000779 {
780 __cn2->__add(*__p);
781 if (--__cn1->end_ != __p)
782 memmove(__p, __p+1, (__cn1->end_ - __p)*sizeof(__i_node*));
783 }
784 else
785 (*__p)->__c_ = __cn1;
786 }
787 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
788 {
789 --__p;
790 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
Eric Fiselier5243e192016-01-04 03:27:52 +0000791 if (__i->__ptr_ == __end_as_link())
Howard Hinnant920b56c2011-09-27 23:55:03 +0000792 {
793 __cn1->__add(*__p);
794 if (--__cn2->end_ != __p)
795 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
796 }
797 else
798 (*__p)->__c_ = __cn2;
799 }
800 __db->unlock();
801#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000802}
803
Marshall Clowb5d34aa2015-02-18 17:24:08 +0000804template <class _Tp, class _Alloc /*= allocator<_Tp>*/>
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +0000805class _LIBCPP_TEMPLATE_VIS list
Howard Hinnant3e519522010-05-11 19:42:16 +0000806 : private __list_imp<_Tp, _Alloc>
807{
808 typedef __list_imp<_Tp, _Alloc> base;
809 typedef typename base::__node __node;
810 typedef typename base::__node_allocator __node_allocator;
811 typedef typename base::__node_pointer __node_pointer;
812 typedef typename base::__node_alloc_traits __node_alloc_traits;
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000813 typedef typename base::__node_base __node_base;
814 typedef typename base::__node_base_pointer __node_base_pointer;
Eric Fiselierb88ea352015-12-30 20:57:59 +0000815 typedef typename base::__link_pointer __link_pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000816
817public:
818 typedef _Tp value_type;
819 typedef _Alloc allocator_type;
820 static_assert((is_same<value_type, typename allocator_type::value_type>::value),
821 "Invalid allocator::value_type");
822 typedef value_type& reference;
823 typedef const value_type& const_reference;
824 typedef typename base::pointer pointer;
825 typedef typename base::const_pointer const_pointer;
826 typedef typename base::size_type size_type;
827 typedef typename base::difference_type difference_type;
828 typedef typename base::iterator iterator;
829 typedef typename base::const_iterator const_iterator;
Howard Hinnantce48a112011-06-30 21:18:19 +0000830 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
831 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnant3e519522010-05-11 19:42:16 +0000832
Howard Hinnant848a5372010-09-22 16:48:34 +0000833 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000834 list()
835 _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value)
Howard Hinnant920b56c2011-09-27 23:55:03 +0000836 {
837#if _LIBCPP_DEBUG_LEVEL >= 2
838 __get_db()->__insert_c(this);
839#endif
840 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000841 _LIBCPP_INLINE_VISIBILITY
Marshall Clowfb829762013-09-08 19:11:51 +0000842 explicit list(const allocator_type& __a) : base(__a)
Howard Hinnant920b56c2011-09-27 23:55:03 +0000843 {
844#if _LIBCPP_DEBUG_LEVEL >= 2
845 __get_db()->__insert_c(this);
846#endif
847 }
Marshall Clowfb829762013-09-08 19:11:51 +0000848 explicit list(size_type __n);
849#if _LIBCPP_STD_VER > 11
850 explicit list(size_type __n, const allocator_type& __a);
851#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000852 list(size_type __n, const value_type& __x);
853 list(size_type __n, const value_type& __x, const allocator_type& __a);
854 template <class _InpIter>
855 list(_InpIter __f, _InpIter __l,
856 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
857 template <class _InpIter>
858 list(_InpIter __f, _InpIter __l, const allocator_type& __a,
859 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
860
861 list(const list& __c);
862 list(const list& __c, const allocator_type& __a);
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +0000863 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000864 list& operator=(const list& __c);
Eric Fiseliercf9ed002017-04-16 03:45:35 +0000865#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +0000866 list(initializer_list<value_type> __il);
867 list(initializer_list<value_type> __il, const allocator_type& __a);
Eric Fiseliercf9ed002017-04-16 03:45:35 +0000868
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +0000869 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000870 list(list&& __c)
871 _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value);
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +0000872 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000873 list(list&& __c, const allocator_type& __a);
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +0000874 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000875 list& operator=(list&& __c)
876 _NOEXCEPT_(
877 __node_alloc_traits::propagate_on_container_move_assignment::value &&
878 is_nothrow_move_assignable<__node_allocator>::value);
Eric Fiseliercf9ed002017-04-16 03:45:35 +0000879
Howard Hinnant848a5372010-09-22 16:48:34 +0000880 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000881 list& operator=(initializer_list<value_type> __il)
882 {assign(__il.begin(), __il.end()); return *this;}
Eric Fiseliercf9ed002017-04-16 03:45:35 +0000883
884 _LIBCPP_INLINE_VISIBILITY
885 void assign(initializer_list<value_type> __il)
886 {assign(__il.begin(), __il.end());}
887#endif // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +0000888
889 template <class _InpIter>
890 void assign(_InpIter __f, _InpIter __l,
891 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
892 void assign(size_type __n, const value_type& __x);
Howard Hinnant3e519522010-05-11 19:42:16 +0000893
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +0000894 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000895 allocator_type get_allocator() const _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +0000896
Howard Hinnant848a5372010-09-22 16:48:34 +0000897 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000898 size_type size() const _NOEXCEPT {return base::__sz();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000899 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000900 bool empty() const _NOEXCEPT {return base::empty();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000901 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000902 size_type max_size() const _NOEXCEPT
Eric Fiselier55b31b4e2016-11-23 01:18:56 +0000903 {
904 return std::min<size_type>(
905 base::__node_alloc_max_size(),
906 numeric_limits<difference_type >::max());
907 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000908
Howard Hinnant848a5372010-09-22 16:48:34 +0000909 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000910 iterator begin() _NOEXCEPT {return base::begin();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000911 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000912 const_iterator begin() const _NOEXCEPT {return base::begin();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000913 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000914 iterator end() _NOEXCEPT {return base::end();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000915 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000916 const_iterator end() const _NOEXCEPT {return base::end();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000917 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000918 const_iterator cbegin() const _NOEXCEPT {return base::begin();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000919 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000920 const_iterator cend() const _NOEXCEPT {return base::end();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000921
Howard Hinnant848a5372010-09-22 16:48:34 +0000922 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000923 reverse_iterator rbegin() _NOEXCEPT
924 {return reverse_iterator(end());}
Howard Hinnant848a5372010-09-22 16:48:34 +0000925 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000926 const_reverse_iterator rbegin() const _NOEXCEPT
927 {return const_reverse_iterator(end());}
Howard Hinnant848a5372010-09-22 16:48:34 +0000928 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000929 reverse_iterator rend() _NOEXCEPT
930 {return reverse_iterator(begin());}
Howard Hinnant848a5372010-09-22 16:48:34 +0000931 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000932 const_reverse_iterator rend() const _NOEXCEPT
933 {return const_reverse_iterator(begin());}
Howard Hinnant848a5372010-09-22 16:48:34 +0000934 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000935 const_reverse_iterator crbegin() const _NOEXCEPT
936 {return const_reverse_iterator(end());}
Howard Hinnant848a5372010-09-22 16:48:34 +0000937 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000938 const_reverse_iterator crend() const _NOEXCEPT
939 {return const_reverse_iterator(begin());}
Howard Hinnant3e519522010-05-11 19:42:16 +0000940
Howard Hinnant848a5372010-09-22 16:48:34 +0000941 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000942 reference front()
943 {
944 _LIBCPP_ASSERT(!empty(), "list::front called on empty list");
Eric Fiselierb88ea352015-12-30 20:57:59 +0000945 return base::__end_.__next_->__as_node()->__value_;
Howard Hinnant920b56c2011-09-27 23:55:03 +0000946 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000947 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000948 const_reference front() const
949 {
950 _LIBCPP_ASSERT(!empty(), "list::front called on empty list");
Eric Fiselierb88ea352015-12-30 20:57:59 +0000951 return base::__end_.__next_->__as_node()->__value_;
Howard Hinnant920b56c2011-09-27 23:55:03 +0000952 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000953 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000954 reference back()
955 {
956 _LIBCPP_ASSERT(!empty(), "list::back called on empty list");
Eric Fiselierb88ea352015-12-30 20:57:59 +0000957 return base::__end_.__prev_->__as_node()->__value_;
Howard Hinnant920b56c2011-09-27 23:55:03 +0000958 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000959 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000960 const_reference back() const
961 {
962 _LIBCPP_ASSERT(!empty(), "list::back called on empty list");
Eric Fiselierb88ea352015-12-30 20:57:59 +0000963 return base::__end_.__prev_->__as_node()->__value_;
Howard Hinnant920b56c2011-09-27 23:55:03 +0000964 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000965
Eric Fiseliercf9ed002017-04-16 03:45:35 +0000966#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +0000967 void push_front(value_type&& __x);
968 void push_back(value_type&& __x);
Eric Fiseliercf9ed002017-04-16 03:45:35 +0000969
Howard Hinnant3e519522010-05-11 19:42:16 +0000970 template <class... _Args>
Marshall Clow63b560b2017-01-24 23:09:12 +0000971#if _LIBCPP_STD_VER > 14
Eric Fiselier0e411642016-07-21 03:20:17 +0000972 reference emplace_front(_Args&&... __args);
Marshall Clow63b560b2017-01-24 23:09:12 +0000973#else
974 void emplace_front(_Args&&... __args);
975#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000976 template <class... _Args>
Marshall Clow63b560b2017-01-24 23:09:12 +0000977#if _LIBCPP_STD_VER > 14
Eric Fiselier0e411642016-07-21 03:20:17 +0000978 reference emplace_back(_Args&&... __args);
Marshall Clow63b560b2017-01-24 23:09:12 +0000979#else
980 void emplace_back(_Args&&... __args);
981#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000982 template <class... _Args>
983 iterator emplace(const_iterator __p, _Args&&... __args);
Eric Fiseliercf9ed002017-04-16 03:45:35 +0000984
Howard Hinnant3e519522010-05-11 19:42:16 +0000985 iterator insert(const_iterator __p, value_type&& __x);
Eric Fiseliercf9ed002017-04-16 03:45:35 +0000986
987 _LIBCPP_INLINE_VISIBILITY
988 iterator insert(const_iterator __p, initializer_list<value_type> __il)
989 {return insert(__p, __il.begin(), __il.end());}
990#endif // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +0000991
992 void push_front(const value_type& __x);
993 void push_back(const value_type& __x);
994
Eric Fiselier1c0cedc2017-10-17 13:03:17 +0000995#ifndef _LIBCPP_CXX03_LANG
996 template <class _Arg>
997 _LIBCPP_INLINE_VISIBILITY
998 void __emplace_back(_Arg&& __arg) { emplace_back(_VSTD::forward<_Arg>(__arg)); }
999#else
1000 _LIBCPP_INLINE_VISIBILITY
1001 void __emplace_back(value_type const& __arg) { push_back(__arg); }
1002#endif
1003
Howard Hinnant3e519522010-05-11 19:42:16 +00001004 iterator insert(const_iterator __p, const value_type& __x);
1005 iterator insert(const_iterator __p, size_type __n, const value_type& __x);
1006 template <class _InpIter>
1007 iterator insert(const_iterator __p, _InpIter __f, _InpIter __l,
1008 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
Howard Hinnant3e519522010-05-11 19:42:16 +00001009
Howard Hinnant848a5372010-09-22 16:48:34 +00001010 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +00001011 void swap(list& __c)
Marshall Clowe3fbe142015-07-13 20:04:56 +00001012#if _LIBCPP_STD_VER >= 14
Eric Fiselier2e519572016-12-28 06:06:09 +00001013 _NOEXCEPT_DEBUG
Marshall Clowe3fbe142015-07-13 20:04:56 +00001014#else
Eric Fiselier2e519572016-12-28 06:06:09 +00001015 _NOEXCEPT_DEBUG_(!__node_alloc_traits::propagate_on_container_swap::value ||
Howard Hinnant45900102011-06-03 17:30:28 +00001016 __is_nothrow_swappable<__node_allocator>::value)
Marshall Clowe3fbe142015-07-13 20:04:56 +00001017#endif
Howard Hinnant45900102011-06-03 17:30:28 +00001018 {base::swap(__c);}
Howard Hinnant848a5372010-09-22 16:48:34 +00001019 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +00001020 void clear() _NOEXCEPT {base::clear();}
Howard Hinnant3e519522010-05-11 19:42:16 +00001021
1022 void pop_front();
1023 void pop_back();
1024
1025 iterator erase(const_iterator __p);
1026 iterator erase(const_iterator __f, const_iterator __l);
1027
1028 void resize(size_type __n);
1029 void resize(size_type __n, const value_type& __x);
1030
1031 void splice(const_iterator __p, list& __c);
Eric Fiseliercf9ed002017-04-16 03:45:35 +00001032#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant848a5372010-09-22 16:48:34 +00001033 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001034 void splice(const_iterator __p, list&& __c) {splice(__p, __c);}
Howard Hinnant848a5372010-09-22 16:48:34 +00001035 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001036 void splice(const_iterator __p, list&& __c, const_iterator __i)
1037 {splice(__p, __c, __i);}
Howard Hinnant848a5372010-09-22 16:48:34 +00001038 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001039 void splice(const_iterator __p, list&& __c, const_iterator __f, const_iterator __l)
1040 {splice(__p, __c, __f, __l);}
Eric Fiseliercf9ed002017-04-16 03:45:35 +00001041#endif
1042 void splice(const_iterator __p, list& __c, const_iterator __i);
1043 void splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l);
Howard Hinnant3e519522010-05-11 19:42:16 +00001044
1045 void remove(const value_type& __x);
1046 template <class _Pred> void remove_if(_Pred __pred);
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001047 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001048 void unique();
1049 template <class _BinaryPred>
1050 void unique(_BinaryPred __binary_pred);
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001051 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001052 void merge(list& __c);
Eric Fiseliercf9ed002017-04-16 03:45:35 +00001053#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant848a5372010-09-22 16:48:34 +00001054 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001055 void merge(list&& __c) {merge(__c);}
Eric Fiseliercf9ed002017-04-16 03:45:35 +00001056
Howard Hinnant3e519522010-05-11 19:42:16 +00001057 template <class _Comp>
Howard Hinnant848a5372010-09-22 16:48:34 +00001058 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001059 void merge(list&& __c, _Comp __comp) {merge(__c, __comp);}
Eric Fiseliercf9ed002017-04-16 03:45:35 +00001060#endif
1061 template <class _Comp>
1062 void merge(list& __c, _Comp __comp);
1063
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001064 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001065 void sort();
1066 template <class _Comp>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001067 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001068 void sort(_Comp __comp);
1069
Howard Hinnant45900102011-06-03 17:30:28 +00001070 void reverse() _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +00001071
Howard Hinnant920b56c2011-09-27 23:55:03 +00001072 bool __invariants() const;
1073
1074#if _LIBCPP_DEBUG_LEVEL >= 2
1075
1076 bool __dereferenceable(const const_iterator* __i) const;
1077 bool __decrementable(const const_iterator* __i) const;
1078 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1079 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1080
1081#endif // _LIBCPP_DEBUG_LEVEL >= 2
1082
Howard Hinnant3e519522010-05-11 19:42:16 +00001083private:
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001084 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierb88ea352015-12-30 20:57:59 +00001085 static void __link_nodes (__link_pointer __p, __link_pointer __f, __link_pointer __l);
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001086 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierb88ea352015-12-30 20:57:59 +00001087 void __link_nodes_at_front(__link_pointer __f, __link_pointer __l);
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001088 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierb88ea352015-12-30 20:57:59 +00001089 void __link_nodes_at_back (__link_pointer __f, __link_pointer __l);
Howard Hinnant3e519522010-05-11 19:42:16 +00001090 iterator __iterator(size_type __n);
1091 template <class _Comp>
1092 static iterator __sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp);
1093
Howard Hinnant45900102011-06-03 17:30:28 +00001094 void __move_assign(list& __c, true_type)
1095 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value);
Howard Hinnant3e519522010-05-11 19:42:16 +00001096 void __move_assign(list& __c, false_type);
1097};
1098
1099// Link in nodes [__f, __l] just prior to __p
1100template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001101inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001102void
Eric Fiselierb88ea352015-12-30 20:57:59 +00001103list<_Tp, _Alloc>::__link_nodes(__link_pointer __p, __link_pointer __f, __link_pointer __l)
Howard Hinnant3e519522010-05-11 19:42:16 +00001104{
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001105 __p->__prev_->__next_ = __f;
1106 __f->__prev_ = __p->__prev_;
1107 __p->__prev_ = __l;
1108 __l->__next_ = __p;
Howard Hinnant3e519522010-05-11 19:42:16 +00001109}
1110
Marshall Clow28d65da2014-08-05 01:34:12 +00001111// Link in nodes [__f, __l] at the front of the list
1112template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001113inline
Marshall Clow28d65da2014-08-05 01:34:12 +00001114void
Eric Fiselierb88ea352015-12-30 20:57:59 +00001115list<_Tp, _Alloc>::__link_nodes_at_front(__link_pointer __f, __link_pointer __l)
Marshall Clow28d65da2014-08-05 01:34:12 +00001116{
Eric Fiselier5243e192016-01-04 03:27:52 +00001117 __f->__prev_ = base::__end_as_link();
Marshall Clowced70062014-08-08 15:35:52 +00001118 __l->__next_ = base::__end_.__next_;
1119 __l->__next_->__prev_ = __l;
1120 base::__end_.__next_ = __f;
Marshall Clow28d65da2014-08-05 01:34:12 +00001121}
1122
1123// Link in nodes [__f, __l] at the front of the list
1124template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001125inline
Marshall Clow28d65da2014-08-05 01:34:12 +00001126void
Eric Fiselierb88ea352015-12-30 20:57:59 +00001127list<_Tp, _Alloc>::__link_nodes_at_back(__link_pointer __f, __link_pointer __l)
Marshall Clow28d65da2014-08-05 01:34:12 +00001128{
Eric Fiselier5243e192016-01-04 03:27:52 +00001129 __l->__next_ = base::__end_as_link();
Marshall Clowced70062014-08-08 15:35:52 +00001130 __f->__prev_ = base::__end_.__prev_;
1131 __f->__prev_->__next_ = __f;
1132 base::__end_.__prev_ = __l;
Marshall Clow28d65da2014-08-05 01:34:12 +00001133}
1134
1135
Howard Hinnant3e519522010-05-11 19:42:16 +00001136template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001137inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001138typename list<_Tp, _Alloc>::iterator
1139list<_Tp, _Alloc>::__iterator(size_type __n)
1140{
Howard Hinnantce48a112011-06-30 21:18:19 +00001141 return __n <= base::__sz() / 2 ? _VSTD::next(begin(), __n)
1142 : _VSTD::prev(end(), base::__sz() - __n);
Howard Hinnant3e519522010-05-11 19:42:16 +00001143}
1144
1145template <class _Tp, class _Alloc>
1146list<_Tp, _Alloc>::list(size_type __n)
1147{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001148#if _LIBCPP_DEBUG_LEVEL >= 2
1149 __get_db()->__insert_c(this);
1150#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001151 for (; __n > 0; --__n)
Eric Fiseliercf9ed002017-04-16 03:45:35 +00001152#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001153 emplace_back();
1154#else
1155 push_back(value_type());
1156#endif
1157}
1158
Marshall Clowfb829762013-09-08 19:11:51 +00001159#if _LIBCPP_STD_VER > 11
1160template <class _Tp, class _Alloc>
1161list<_Tp, _Alloc>::list(size_type __n, const allocator_type& __a) : base(__a)
1162{
1163#if _LIBCPP_DEBUG_LEVEL >= 2
1164 __get_db()->__insert_c(this);
1165#endif
1166 for (; __n > 0; --__n)
Marshall Clowfb829762013-09-08 19:11:51 +00001167 emplace_back();
Marshall Clowfb829762013-09-08 19:11:51 +00001168}
1169#endif
1170
Howard Hinnant3e519522010-05-11 19:42:16 +00001171template <class _Tp, class _Alloc>
1172list<_Tp, _Alloc>::list(size_type __n, const value_type& __x)
1173{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001174#if _LIBCPP_DEBUG_LEVEL >= 2
1175 __get_db()->__insert_c(this);
1176#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001177 for (; __n > 0; --__n)
1178 push_back(__x);
1179}
1180
1181template <class _Tp, class _Alloc>
1182list<_Tp, _Alloc>::list(size_type __n, const value_type& __x, const allocator_type& __a)
1183 : base(__a)
1184{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001185#if _LIBCPP_DEBUG_LEVEL >= 2
1186 __get_db()->__insert_c(this);
1187#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001188 for (; __n > 0; --__n)
1189 push_back(__x);
1190}
1191
1192template <class _Tp, class _Alloc>
1193template <class _InpIter>
1194list<_Tp, _Alloc>::list(_InpIter __f, _InpIter __l,
1195 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1196{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001197#if _LIBCPP_DEBUG_LEVEL >= 2
1198 __get_db()->__insert_c(this);
1199#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001200 for (; __f != __l; ++__f)
Eric Fiselier1c0cedc2017-10-17 13:03:17 +00001201 __emplace_back(*__f);
Howard Hinnant3e519522010-05-11 19:42:16 +00001202}
1203
1204template <class _Tp, class _Alloc>
1205template <class _InpIter>
1206list<_Tp, _Alloc>::list(_InpIter __f, _InpIter __l, const allocator_type& __a,
1207 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1208 : base(__a)
1209{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001210#if _LIBCPP_DEBUG_LEVEL >= 2
1211 __get_db()->__insert_c(this);
1212#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001213 for (; __f != __l; ++__f)
Eric Fiselier1c0cedc2017-10-17 13:03:17 +00001214 __emplace_back(*__f);
Howard Hinnant3e519522010-05-11 19:42:16 +00001215}
1216
1217template <class _Tp, class _Alloc>
1218list<_Tp, _Alloc>::list(const list& __c)
1219 : base(allocator_type(
1220 __node_alloc_traits::select_on_container_copy_construction(
1221 __c.__node_alloc())))
1222{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001223#if _LIBCPP_DEBUG_LEVEL >= 2
1224 __get_db()->__insert_c(this);
1225#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001226 for (const_iterator __i = __c.begin(), __e = __c.end(); __i != __e; ++__i)
1227 push_back(*__i);
1228}
1229
1230template <class _Tp, class _Alloc>
1231list<_Tp, _Alloc>::list(const list& __c, const allocator_type& __a)
1232 : base(__a)
1233{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001234#if _LIBCPP_DEBUG_LEVEL >= 2
1235 __get_db()->__insert_c(this);
1236#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001237 for (const_iterator __i = __c.begin(), __e = __c.end(); __i != __e; ++__i)
1238 push_back(*__i);
1239}
1240
Eric Fiseliercf9ed002017-04-16 03:45:35 +00001241#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant54976f22011-08-12 21:56:02 +00001242
Howard Hinnant3e519522010-05-11 19:42:16 +00001243template <class _Tp, class _Alloc>
1244list<_Tp, _Alloc>::list(initializer_list<value_type> __il, const allocator_type& __a)
1245 : base(__a)
1246{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001247#if _LIBCPP_DEBUG_LEVEL >= 2
1248 __get_db()->__insert_c(this);
1249#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001250 for (typename initializer_list<value_type>::const_iterator __i = __il.begin(),
1251 __e = __il.end(); __i != __e; ++__i)
1252 push_back(*__i);
1253}
1254
1255template <class _Tp, class _Alloc>
1256list<_Tp, _Alloc>::list(initializer_list<value_type> __il)
1257{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001258#if _LIBCPP_DEBUG_LEVEL >= 2
1259 __get_db()->__insert_c(this);
1260#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001261 for (typename initializer_list<value_type>::const_iterator __i = __il.begin(),
1262 __e = __il.end(); __i != __e; ++__i)
1263 push_back(*__i);
1264}
1265
Howard Hinnant3e519522010-05-11 19:42:16 +00001266template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001267inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001268list<_Tp, _Alloc>::list(list&& __c)
Howard Hinnant45900102011-06-03 17:30:28 +00001269 _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value)
Howard Hinnantce48a112011-06-30 21:18:19 +00001270 : base(allocator_type(_VSTD::move(__c.__node_alloc())))
Howard Hinnant3e519522010-05-11 19:42:16 +00001271{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001272#if _LIBCPP_DEBUG_LEVEL >= 2
1273 __get_db()->__insert_c(this);
1274#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001275 splice(end(), __c);
1276}
1277
1278template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001279inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001280list<_Tp, _Alloc>::list(list&& __c, const allocator_type& __a)
1281 : base(__a)
1282{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001283#if _LIBCPP_DEBUG_LEVEL >= 2
1284 __get_db()->__insert_c(this);
1285#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001286 if (__a == __c.get_allocator())
1287 splice(end(), __c);
1288 else
1289 {
Howard Hinnantc003db12011-11-29 18:15:50 +00001290 typedef move_iterator<iterator> _Ip;
1291 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnant3e519522010-05-11 19:42:16 +00001292 }
1293}
1294
1295template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001296inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001297list<_Tp, _Alloc>&
1298list<_Tp, _Alloc>::operator=(list&& __c)
Howard Hinnant45900102011-06-03 17:30:28 +00001299 _NOEXCEPT_(
1300 __node_alloc_traits::propagate_on_container_move_assignment::value &&
1301 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +00001302{
1303 __move_assign(__c, integral_constant<bool,
1304 __node_alloc_traits::propagate_on_container_move_assignment::value>());
1305 return *this;
1306}
1307
1308template <class _Tp, class _Alloc>
1309void
1310list<_Tp, _Alloc>::__move_assign(list& __c, false_type)
1311{
1312 if (base::__node_alloc() != __c.__node_alloc())
1313 {
Howard Hinnantc003db12011-11-29 18:15:50 +00001314 typedef move_iterator<iterator> _Ip;
1315 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnant3e519522010-05-11 19:42:16 +00001316 }
1317 else
1318 __move_assign(__c, true_type());
1319}
1320
1321template <class _Tp, class _Alloc>
1322void
1323list<_Tp, _Alloc>::__move_assign(list& __c, true_type)
Howard Hinnant45900102011-06-03 17:30:28 +00001324 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +00001325{
1326 clear();
1327 base::__move_assign_alloc(__c);
1328 splice(end(), __c);
1329}
1330
Eric Fiseliercf9ed002017-04-16 03:45:35 +00001331#endif // _LIBCPP_CXX03_LANG
1332
1333template <class _Tp, class _Alloc>
1334inline
1335list<_Tp, _Alloc>&
1336list<_Tp, _Alloc>::operator=(const list& __c)
1337{
1338 if (this != &__c)
1339 {
1340 base::__copy_assign_alloc(__c);
1341 assign(__c.begin(), __c.end());
1342 }
1343 return *this;
1344}
Howard Hinnant3e519522010-05-11 19:42:16 +00001345
1346template <class _Tp, class _Alloc>
1347template <class _InpIter>
1348void
1349list<_Tp, _Alloc>::assign(_InpIter __f, _InpIter __l,
1350 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1351{
1352 iterator __i = begin();
1353 iterator __e = end();
1354 for (; __f != __l && __i != __e; ++__f, ++__i)
1355 *__i = *__f;
1356 if (__i == __e)
1357 insert(__e, __f, __l);
1358 else
1359 erase(__i, __e);
Eric Fiselier2e519572016-12-28 06:06:09 +00001360#if _LIBCPP_DEBUG_LEVEL >= 2
1361 __get_db()->__invalidate_all(this);
1362#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001363}
1364
1365template <class _Tp, class _Alloc>
1366void
1367list<_Tp, _Alloc>::assign(size_type __n, const value_type& __x)
1368{
1369 iterator __i = begin();
1370 iterator __e = end();
1371 for (; __n > 0 && __i != __e; --__n, ++__i)
1372 *__i = __x;
1373 if (__i == __e)
1374 insert(__e, __n, __x);
1375 else
1376 erase(__i, __e);
Eric Fiselier2e519572016-12-28 06:06:09 +00001377#if _LIBCPP_DEBUG_LEVEL >= 2
1378 __get_db()->__invalidate_all(this);
1379#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001380}
1381
1382template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001383inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001384_Alloc
Howard Hinnant45900102011-06-03 17:30:28 +00001385list<_Tp, _Alloc>::get_allocator() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001386{
1387 return allocator_type(base::__node_alloc());
1388}
1389
1390template <class _Tp, class _Alloc>
1391typename list<_Tp, _Alloc>::iterator
1392list<_Tp, _Alloc>::insert(const_iterator __p, const value_type& __x)
1393{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001394#if _LIBCPP_DEBUG_LEVEL >= 2
1395 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1396 "list::insert(iterator, x) called with an iterator not"
1397 " referring to this list");
1398#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001399 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001400 typedef __allocator_destructor<__node_allocator> _Dp;
1401 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant3e519522010-05-11 19:42:16 +00001402 __hold->__prev_ = 0;
Howard Hinnantce48a112011-06-30 21:18:19 +00001403 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001404 __link_nodes(__p.__ptr_, __hold->__as_link(), __hold->__as_link());
Howard Hinnant3e519522010-05-11 19:42:16 +00001405 ++base::__sz();
Howard Hinnantb0e4c9d2013-04-05 00:18:49 +00001406#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselierb88ea352015-12-30 20:57:59 +00001407 return iterator(__hold.release()->__as_link(), this);
Howard Hinnantb0e4c9d2013-04-05 00:18:49 +00001408#else
Eric Fiselierb88ea352015-12-30 20:57:59 +00001409 return iterator(__hold.release()->__as_link());
Howard Hinnantb0e4c9d2013-04-05 00:18:49 +00001410#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001411}
1412
1413template <class _Tp, class _Alloc>
1414typename list<_Tp, _Alloc>::iterator
1415list<_Tp, _Alloc>::insert(const_iterator __p, size_type __n, const value_type& __x)
1416{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001417#if _LIBCPP_DEBUG_LEVEL >= 2
1418 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1419 "list::insert(iterator, n, x) called with an iterator not"
1420 " referring to this list");
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001421 iterator __r(__p.__ptr_, this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001422#else
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001423 iterator __r(__p.__ptr_);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001424#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001425 if (__n > 0)
1426 {
1427 size_type __ds = 0;
1428 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001429 typedef __allocator_destructor<__node_allocator> _Dp;
1430 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant3e519522010-05-11 19:42:16 +00001431 __hold->__prev_ = 0;
Howard Hinnantce48a112011-06-30 21:18:19 +00001432 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnant3e519522010-05-11 19:42:16 +00001433 ++__ds;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001434#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselierb88ea352015-12-30 20:57:59 +00001435 __r = iterator(__hold->__as_link(), this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001436#else
Eric Fiselierb88ea352015-12-30 20:57:59 +00001437 __r = iterator(__hold->__as_link());
Howard Hinnant920b56c2011-09-27 23:55:03 +00001438#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001439 __hold.release();
1440 iterator __e = __r;
1441#ifndef _LIBCPP_NO_EXCEPTIONS
1442 try
1443 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001444#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001445 for (--__n; __n != 0; --__n, ++__e, ++__ds)
1446 {
1447 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001448 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001449 __e.__ptr_->__next_ = __hold->__as_link();
Howard Hinnant3e519522010-05-11 19:42:16 +00001450 __hold->__prev_ = __e.__ptr_;
1451 __hold.release();
1452 }
1453#ifndef _LIBCPP_NO_EXCEPTIONS
1454 }
1455 catch (...)
1456 {
1457 while (true)
1458 {
Howard Hinnantce48a112011-06-30 21:18:19 +00001459 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Eric Fiselierb88ea352015-12-30 20:57:59 +00001460 __link_pointer __prev = __e.__ptr_->__prev_;
1461 __node_alloc_traits::deallocate(__na, __e.__ptr_->__as_node(), 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001462 if (__prev == 0)
1463 break;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001464#if _LIBCPP_DEBUG_LEVEL >= 2
1465 __e = iterator(__prev, this);
1466#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001467 __e = iterator(__prev);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001468#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001469 }
1470 throw;
1471 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001472#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001473 __link_nodes(__p.__ptr_, __r.__ptr_, __e.__ptr_);
Howard Hinnant3e519522010-05-11 19:42:16 +00001474 base::__sz() += __ds;
1475 }
1476 return __r;
1477}
1478
1479template <class _Tp, class _Alloc>
1480template <class _InpIter>
1481typename list<_Tp, _Alloc>::iterator
1482list<_Tp, _Alloc>::insert(const_iterator __p, _InpIter __f, _InpIter __l,
1483 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1484{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001485#if _LIBCPP_DEBUG_LEVEL >= 2
1486 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1487 "list::insert(iterator, range) called with an iterator not"
1488 " referring to this list");
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001489 iterator __r(__p.__ptr_, this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001490#else
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001491 iterator __r(__p.__ptr_);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001492#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001493 if (__f != __l)
1494 {
1495 size_type __ds = 0;
1496 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001497 typedef __allocator_destructor<__node_allocator> _Dp;
1498 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant3e519522010-05-11 19:42:16 +00001499 __hold->__prev_ = 0;
Howard Hinnantce48a112011-06-30 21:18:19 +00001500 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), *__f);
Howard Hinnant3e519522010-05-11 19:42:16 +00001501 ++__ds;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001502#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselierb88ea352015-12-30 20:57:59 +00001503 __r = iterator(__hold.get()->__as_link(), this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001504#else
Eric Fiselierb88ea352015-12-30 20:57:59 +00001505 __r = iterator(__hold.get()->__as_link());
Howard Hinnant920b56c2011-09-27 23:55:03 +00001506#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001507 __hold.release();
1508 iterator __e = __r;
1509#ifndef _LIBCPP_NO_EXCEPTIONS
1510 try
1511 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001512#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiselier61bff612015-03-19 03:20:02 +00001513 for (++__f; __f != __l; ++__f, (void) ++__e, (void) ++__ds)
Howard Hinnant3e519522010-05-11 19:42:16 +00001514 {
1515 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001516 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), *__f);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001517 __e.__ptr_->__next_ = __hold.get()->__as_link();
Howard Hinnant3e519522010-05-11 19:42:16 +00001518 __hold->__prev_ = __e.__ptr_;
1519 __hold.release();
1520 }
1521#ifndef _LIBCPP_NO_EXCEPTIONS
1522 }
1523 catch (...)
1524 {
1525 while (true)
1526 {
Howard Hinnantce48a112011-06-30 21:18:19 +00001527 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Eric Fiselierb88ea352015-12-30 20:57:59 +00001528 __link_pointer __prev = __e.__ptr_->__prev_;
1529 __node_alloc_traits::deallocate(__na, __e.__ptr_->__as_node(), 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001530 if (__prev == 0)
1531 break;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001532#if _LIBCPP_DEBUG_LEVEL >= 2
1533 __e = iterator(__prev, this);
1534#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001535 __e = iterator(__prev);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001536#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001537 }
1538 throw;
1539 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001540#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001541 __link_nodes(__p.__ptr_, __r.__ptr_, __e.__ptr_);
Howard Hinnant3e519522010-05-11 19:42:16 +00001542 base::__sz() += __ds;
1543 }
1544 return __r;
1545}
1546
1547template <class _Tp, class _Alloc>
1548void
1549list<_Tp, _Alloc>::push_front(const value_type& __x)
1550{
1551 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001552 typedef __allocator_destructor<__node_allocator> _Dp;
1553 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001554 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001555 __link_pointer __nl = __hold->__as_link();
1556 __link_nodes_at_front(__nl, __nl);
Howard Hinnant3e519522010-05-11 19:42:16 +00001557 ++base::__sz();
1558 __hold.release();
1559}
1560
1561template <class _Tp, class _Alloc>
1562void
1563list<_Tp, _Alloc>::push_back(const value_type& __x)
1564{
1565 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001566 typedef __allocator_destructor<__node_allocator> _Dp;
1567 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001568 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001569 __link_nodes_at_back(__hold.get()->__as_link(), __hold.get()->__as_link());
Howard Hinnant3e519522010-05-11 19:42:16 +00001570 ++base::__sz();
1571 __hold.release();
1572}
1573
Eric Fiseliercf9ed002017-04-16 03:45:35 +00001574#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001575
1576template <class _Tp, class _Alloc>
1577void
1578list<_Tp, _Alloc>::push_front(value_type&& __x)
1579{
1580 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001581 typedef __allocator_destructor<__node_allocator> _Dp;
1582 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001583 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
Eric Fiselierb88ea352015-12-30 20:57:59 +00001584 __link_nodes_at_front(__hold.get()->__as_link(), __hold.get()->__as_link());
Howard Hinnant3e519522010-05-11 19:42:16 +00001585 ++base::__sz();
1586 __hold.release();
1587}
1588
1589template <class _Tp, class _Alloc>
1590void
1591list<_Tp, _Alloc>::push_back(value_type&& __x)
1592{
1593 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001594 typedef __allocator_destructor<__node_allocator> _Dp;
1595 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001596 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
Eric Fiselierb88ea352015-12-30 20:57:59 +00001597 __link_nodes_at_back(__hold.get()->__as_link(), __hold.get()->__as_link());
Howard Hinnant3e519522010-05-11 19:42:16 +00001598 ++base::__sz();
1599 __hold.release();
1600}
1601
1602template <class _Tp, class _Alloc>
1603template <class... _Args>
Marshall Clow63b560b2017-01-24 23:09:12 +00001604#if _LIBCPP_STD_VER > 14
Eric Fiselier0e411642016-07-21 03:20:17 +00001605typename list<_Tp, _Alloc>::reference
Marshall Clow63b560b2017-01-24 23:09:12 +00001606#else
1607void
1608#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001609list<_Tp, _Alloc>::emplace_front(_Args&&... __args)
1610{
1611 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001612 typedef __allocator_destructor<__node_allocator> _Dp;
1613 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001614 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001615 __link_nodes_at_front(__hold.get()->__as_link(), __hold.get()->__as_link());
Howard Hinnant3e519522010-05-11 19:42:16 +00001616 ++base::__sz();
Marshall Clow63b560b2017-01-24 23:09:12 +00001617#if _LIBCPP_STD_VER > 14
Eric Fiselier0e411642016-07-21 03:20:17 +00001618 return __hold.release()->__value_;
Marshall Clow63b560b2017-01-24 23:09:12 +00001619#else
1620 __hold.release();
1621#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001622}
1623
1624template <class _Tp, class _Alloc>
1625template <class... _Args>
Marshall Clow63b560b2017-01-24 23:09:12 +00001626#if _LIBCPP_STD_VER > 14
Eric Fiselier0e411642016-07-21 03:20:17 +00001627typename list<_Tp, _Alloc>::reference
Marshall Clow63b560b2017-01-24 23:09:12 +00001628#else
1629void
1630#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001631list<_Tp, _Alloc>::emplace_back(_Args&&... __args)
1632{
1633 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001634 typedef __allocator_destructor<__node_allocator> _Dp;
1635 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001636 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001637 __link_pointer __nl = __hold->__as_link();
1638 __link_nodes_at_back(__nl, __nl);
Howard Hinnant3e519522010-05-11 19:42:16 +00001639 ++base::__sz();
Marshall Clow63b560b2017-01-24 23:09:12 +00001640#if _LIBCPP_STD_VER > 14
Eric Fiselier0e411642016-07-21 03:20:17 +00001641 return __hold.release()->__value_;
Marshall Clow63b560b2017-01-24 23:09:12 +00001642#else
1643 __hold.release();
1644#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001645}
1646
1647template <class _Tp, class _Alloc>
1648template <class... _Args>
1649typename list<_Tp, _Alloc>::iterator
1650list<_Tp, _Alloc>::emplace(const_iterator __p, _Args&&... __args)
1651{
Howard Hinnantb0e4c9d2013-04-05 00:18:49 +00001652#if _LIBCPP_DEBUG_LEVEL >= 2
1653 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1654 "list::emplace(iterator, args...) called with an iterator not"
1655 " referring to this list");
1656#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001657 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001658 typedef __allocator_destructor<__node_allocator> _Dp;
1659 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant3e519522010-05-11 19:42:16 +00001660 __hold->__prev_ = 0;
Howard Hinnantce48a112011-06-30 21:18:19 +00001661 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001662 __link_pointer __nl = __hold.get()->__as_link();
1663 __link_nodes(__p.__ptr_, __nl, __nl);
Howard Hinnant3e519522010-05-11 19:42:16 +00001664 ++base::__sz();
Eric Fiselierb88ea352015-12-30 20:57:59 +00001665 __hold.release();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001666#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselierb88ea352015-12-30 20:57:59 +00001667 return iterator(__nl, this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001668#else
Eric Fiselierb88ea352015-12-30 20:57:59 +00001669 return iterator(__nl);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001670#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001671}
1672
1673template <class _Tp, class _Alloc>
1674typename list<_Tp, _Alloc>::iterator
1675list<_Tp, _Alloc>::insert(const_iterator __p, value_type&& __x)
1676{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001677#if _LIBCPP_DEBUG_LEVEL >= 2
1678 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1679 "list::insert(iterator, x) called with an iterator not"
1680 " referring to this list");
1681#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001682 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001683 typedef __allocator_destructor<__node_allocator> _Dp;
1684 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant3e519522010-05-11 19:42:16 +00001685 __hold->__prev_ = 0;
Howard Hinnantce48a112011-06-30 21:18:19 +00001686 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
Eric Fiselierb88ea352015-12-30 20:57:59 +00001687 __link_pointer __nl = __hold->__as_link();
1688 __link_nodes(__p.__ptr_, __nl, __nl);
Howard Hinnant3e519522010-05-11 19:42:16 +00001689 ++base::__sz();
Eric Fiselierb88ea352015-12-30 20:57:59 +00001690 __hold.release();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001691#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselierb88ea352015-12-30 20:57:59 +00001692 return iterator(__nl, this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001693#else
Eric Fiselierb88ea352015-12-30 20:57:59 +00001694 return iterator(__nl);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001695#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001696}
1697
Eric Fiseliercf9ed002017-04-16 03:45:35 +00001698#endif // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001699
1700template <class _Tp, class _Alloc>
1701void
1702list<_Tp, _Alloc>::pop_front()
1703{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001704 _LIBCPP_ASSERT(!empty(), "list::pop_front() called with empty list");
Howard Hinnant3e519522010-05-11 19:42:16 +00001705 __node_allocator& __na = base::__node_alloc();
Eric Fiselierb88ea352015-12-30 20:57:59 +00001706 __link_pointer __n = base::__end_.__next_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001707 base::__unlink_nodes(__n, __n);
1708 --base::__sz();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001709#if _LIBCPP_DEBUG_LEVEL >= 2
1710 __c_node* __c = __get_db()->__find_c_and_lock(this);
1711 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1712 {
1713 --__p;
1714 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001715 if (__i->__ptr_ == __n)
Howard Hinnant920b56c2011-09-27 23:55:03 +00001716 {
1717 (*__p)->__c_ = nullptr;
1718 if (--__c->end_ != __p)
1719 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1720 }
1721 }
1722 __get_db()->unlock();
1723#endif
Eric Fiselierb88ea352015-12-30 20:57:59 +00001724 __node_pointer __np = __n->__as_node();
1725 __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
1726 __node_alloc_traits::deallocate(__na, __np, 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001727}
1728
1729template <class _Tp, class _Alloc>
1730void
1731list<_Tp, _Alloc>::pop_back()
1732{
Dmitri Gribenkoc3f9c802013-06-24 06:15:57 +00001733 _LIBCPP_ASSERT(!empty(), "list::pop_back() called with empty list");
Howard Hinnant3e519522010-05-11 19:42:16 +00001734 __node_allocator& __na = base::__node_alloc();
Eric Fiselierb88ea352015-12-30 20:57:59 +00001735 __link_pointer __n = base::__end_.__prev_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001736 base::__unlink_nodes(__n, __n);
1737 --base::__sz();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001738#if _LIBCPP_DEBUG_LEVEL >= 2
1739 __c_node* __c = __get_db()->__find_c_and_lock(this);
1740 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1741 {
1742 --__p;
1743 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001744 if (__i->__ptr_ == __n)
Howard Hinnant920b56c2011-09-27 23:55:03 +00001745 {
1746 (*__p)->__c_ = nullptr;
1747 if (--__c->end_ != __p)
1748 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1749 }
1750 }
1751 __get_db()->unlock();
1752#endif
Eric Fiselierb88ea352015-12-30 20:57:59 +00001753 __node_pointer __np = __n->__as_node();
1754 __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
1755 __node_alloc_traits::deallocate(__na, __np, 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001756}
1757
1758template <class _Tp, class _Alloc>
1759typename list<_Tp, _Alloc>::iterator
1760list<_Tp, _Alloc>::erase(const_iterator __p)
1761{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001762#if _LIBCPP_DEBUG_LEVEL >= 2
1763 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1764 "list::erase(iterator) called with an iterator not"
1765 " referring to this list");
1766#endif
Howard Hinnantb0e4c9d2013-04-05 00:18:49 +00001767 _LIBCPP_ASSERT(__p != end(),
1768 "list::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnant3e519522010-05-11 19:42:16 +00001769 __node_allocator& __na = base::__node_alloc();
Eric Fiselierb88ea352015-12-30 20:57:59 +00001770 __link_pointer __n = __p.__ptr_;
1771 __link_pointer __r = __n->__next_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001772 base::__unlink_nodes(__n, __n);
1773 --base::__sz();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001774#if _LIBCPP_DEBUG_LEVEL >= 2
1775 __c_node* __c = __get_db()->__find_c_and_lock(this);
Eric Fiselier878e7e22016-10-23 19:26:39 +00001776 for (__i_node** __ip = __c->end_; __ip != __c->beg_; )
Howard Hinnant920b56c2011-09-27 23:55:03 +00001777 {
Eric Fiselier878e7e22016-10-23 19:26:39 +00001778 --__ip;
1779 iterator* __i = static_cast<iterator*>((*__ip)->__i_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001780 if (__i->__ptr_ == __n)
Howard Hinnant920b56c2011-09-27 23:55:03 +00001781 {
Eric Fiselier878e7e22016-10-23 19:26:39 +00001782 (*__ip)->__c_ = nullptr;
1783 if (--__c->end_ != __ip)
1784 memmove(__ip, __ip+1, (__c->end_ - __ip)*sizeof(__i_node*));
Howard Hinnant920b56c2011-09-27 23:55:03 +00001785 }
1786 }
1787 __get_db()->unlock();
1788#endif
Eric Fiselierb88ea352015-12-30 20:57:59 +00001789 __node_pointer __np = __n->__as_node();
1790 __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
1791 __node_alloc_traits::deallocate(__na, __np, 1);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001792#if _LIBCPP_DEBUG_LEVEL >= 2
1793 return iterator(__r, this);
1794#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001795 return iterator(__r);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001796#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001797}
1798
1799template <class _Tp, class _Alloc>
1800typename list<_Tp, _Alloc>::iterator
1801list<_Tp, _Alloc>::erase(const_iterator __f, const_iterator __l)
1802{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001803#if _LIBCPP_DEBUG_LEVEL >= 2
1804 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__f) == this,
1805 "list::erase(iterator, iterator) called with an iterator not"
1806 " referring to this list");
Eric Fiselier2e519572016-12-28 06:06:09 +00001807 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__l) == this,
1808 "list::erase(iterator, iterator) called with an iterator not"
1809 " referring to this list");
Howard Hinnant920b56c2011-09-27 23:55:03 +00001810#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001811 if (__f != __l)
1812 {
1813 __node_allocator& __na = base::__node_alloc();
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001814 base::__unlink_nodes(__f.__ptr_, __l.__ptr_->__prev_);
Howard Hinnant3e519522010-05-11 19:42:16 +00001815 while (__f != __l)
1816 {
Eric Fiselierb88ea352015-12-30 20:57:59 +00001817 __link_pointer __n = __f.__ptr_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001818 ++__f;
1819 --base::__sz();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001820#if _LIBCPP_DEBUG_LEVEL >= 2
1821 __c_node* __c = __get_db()->__find_c_and_lock(this);
1822 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1823 {
1824 --__p;
1825 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001826 if (__i->__ptr_ == __n)
Howard Hinnant920b56c2011-09-27 23:55:03 +00001827 {
1828 (*__p)->__c_ = nullptr;
1829 if (--__c->end_ != __p)
1830 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1831 }
1832 }
1833 __get_db()->unlock();
1834#endif
Eric Fiselierb88ea352015-12-30 20:57:59 +00001835 __node_pointer __np = __n->__as_node();
1836 __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
1837 __node_alloc_traits::deallocate(__na, __np, 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001838 }
1839 }
Howard Hinnant920b56c2011-09-27 23:55:03 +00001840#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001841 return iterator(__l.__ptr_, this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001842#else
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001843 return iterator(__l.__ptr_);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001844#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001845}
1846
1847template <class _Tp, class _Alloc>
1848void
1849list<_Tp, _Alloc>::resize(size_type __n)
1850{
1851 if (__n < base::__sz())
1852 erase(__iterator(__n), end());
1853 else if (__n > base::__sz())
1854 {
1855 __n -= base::__sz();
1856 size_type __ds = 0;
1857 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001858 typedef __allocator_destructor<__node_allocator> _Dp;
1859 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant3e519522010-05-11 19:42:16 +00001860 __hold->__prev_ = 0;
Howard Hinnantce48a112011-06-30 21:18:19 +00001861 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_));
Howard Hinnant3e519522010-05-11 19:42:16 +00001862 ++__ds;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001863#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselierb88ea352015-12-30 20:57:59 +00001864 iterator __r = iterator(__hold.release()->__as_link(), this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001865#else
Eric Fiselierb88ea352015-12-30 20:57:59 +00001866 iterator __r = iterator(__hold.release()->__as_link());
Howard Hinnant920b56c2011-09-27 23:55:03 +00001867#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001868 iterator __e = __r;
1869#ifndef _LIBCPP_NO_EXCEPTIONS
1870 try
1871 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001872#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001873 for (--__n; __n != 0; --__n, ++__e, ++__ds)
1874 {
1875 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001876 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_));
Eric Fiselierb88ea352015-12-30 20:57:59 +00001877 __e.__ptr_->__next_ = __hold.get()->__as_link();
Howard Hinnant3e519522010-05-11 19:42:16 +00001878 __hold->__prev_ = __e.__ptr_;
1879 __hold.release();
1880 }
1881#ifndef _LIBCPP_NO_EXCEPTIONS
1882 }
1883 catch (...)
1884 {
1885 while (true)
1886 {
Howard Hinnantce48a112011-06-30 21:18:19 +00001887 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Eric Fiselierb88ea352015-12-30 20:57:59 +00001888 __link_pointer __prev = __e.__ptr_->__prev_;
1889 __node_alloc_traits::deallocate(__na, __e.__ptr_->__as_node(), 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001890 if (__prev == 0)
1891 break;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001892#if _LIBCPP_DEBUG_LEVEL >= 2
1893 __e = iterator(__prev, this);
1894#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001895 __e = iterator(__prev);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001896#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001897 }
1898 throw;
1899 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001900#endif // _LIBCPP_NO_EXCEPTIONS
Marshall Clow28d65da2014-08-05 01:34:12 +00001901 __link_nodes_at_back(__r.__ptr_, __e.__ptr_);
Howard Hinnant3e519522010-05-11 19:42:16 +00001902 base::__sz() += __ds;
1903 }
1904}
1905
1906template <class _Tp, class _Alloc>
1907void
1908list<_Tp, _Alloc>::resize(size_type __n, const value_type& __x)
1909{
1910 if (__n < base::__sz())
1911 erase(__iterator(__n), end());
1912 else if (__n > base::__sz())
1913 {
1914 __n -= base::__sz();
1915 size_type __ds = 0;
1916 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001917 typedef __allocator_destructor<__node_allocator> _Dp;
1918 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant3e519522010-05-11 19:42:16 +00001919 __hold->__prev_ = 0;
Howard Hinnantce48a112011-06-30 21:18:19 +00001920 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnant3e519522010-05-11 19:42:16 +00001921 ++__ds;
Eric Fiselierb88ea352015-12-30 20:57:59 +00001922 __link_pointer __nl = __hold.release()->__as_link();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001923#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselierb88ea352015-12-30 20:57:59 +00001924 iterator __r = iterator(__nl, this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001925#else
Eric Fiselierb88ea352015-12-30 20:57:59 +00001926 iterator __r = iterator(__nl);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001927#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001928 iterator __e = __r;
1929#ifndef _LIBCPP_NO_EXCEPTIONS
1930 try
1931 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001932#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001933 for (--__n; __n != 0; --__n, ++__e, ++__ds)
1934 {
1935 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001936 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001937 __e.__ptr_->__next_ = __hold.get()->__as_link();
Howard Hinnant3e519522010-05-11 19:42:16 +00001938 __hold->__prev_ = __e.__ptr_;
1939 __hold.release();
1940 }
1941#ifndef _LIBCPP_NO_EXCEPTIONS
1942 }
1943 catch (...)
1944 {
1945 while (true)
1946 {
Howard Hinnantce48a112011-06-30 21:18:19 +00001947 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Eric Fiselierb88ea352015-12-30 20:57:59 +00001948 __link_pointer __prev = __e.__ptr_->__prev_;
1949 __node_alloc_traits::deallocate(__na, __e.__ptr_->__as_node(), 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001950 if (__prev == 0)
1951 break;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001952#if _LIBCPP_DEBUG_LEVEL >= 2
1953 __e = iterator(__prev, this);
1954#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001955 __e = iterator(__prev);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001956#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001957 }
1958 throw;
1959 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001960#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiselier5243e192016-01-04 03:27:52 +00001961 __link_nodes(base::__end_as_link(), __r.__ptr_, __e.__ptr_);
Howard Hinnant3e519522010-05-11 19:42:16 +00001962 base::__sz() += __ds;
Howard Hinnantb3371f62010-08-22 00:02:43 +00001963 }
Howard Hinnant3e519522010-05-11 19:42:16 +00001964}
1965
1966template <class _Tp, class _Alloc>
1967void
1968list<_Tp, _Alloc>::splice(const_iterator __p, list& __c)
1969{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001970 _LIBCPP_ASSERT(this != &__c,
1971 "list::splice(iterator, list) called with this == &list");
1972#if _LIBCPP_DEBUG_LEVEL >= 2
1973 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1974 "list::splice(iterator, list) called with an iterator not"
1975 " referring to this list");
1976#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001977 if (!__c.empty())
1978 {
Eric Fiselierb88ea352015-12-30 20:57:59 +00001979 __link_pointer __f = __c.__end_.__next_;
1980 __link_pointer __l = __c.__end_.__prev_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001981 base::__unlink_nodes(__f, __l);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001982 __link_nodes(__p.__ptr_, __f, __l);
Howard Hinnant3e519522010-05-11 19:42:16 +00001983 base::__sz() += __c.__sz();
1984 __c.__sz() = 0;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001985#if _LIBCPP_DEBUG_LEVEL >= 2
1986 __libcpp_db* __db = __get_db();
1987 __c_node* __cn1 = __db->__find_c_and_lock(this);
1988 __c_node* __cn2 = __db->__find_c(&__c);
Eric Fiselier878e7e22016-10-23 19:26:39 +00001989 for (__i_node** __ip = __cn2->end_; __ip != __cn2->beg_;)
Howard Hinnant920b56c2011-09-27 23:55:03 +00001990 {
Eric Fiselier878e7e22016-10-23 19:26:39 +00001991 --__ip;
1992 iterator* __i = static_cast<iterator*>((*__ip)->__i_);
Eric Fiselier5243e192016-01-04 03:27:52 +00001993 if (__i->__ptr_ != __c.__end_as_link())
Howard Hinnant920b56c2011-09-27 23:55:03 +00001994 {
Eric Fiselier878e7e22016-10-23 19:26:39 +00001995 __cn1->__add(*__ip);
1996 (*__ip)->__c_ = __cn1;
1997 if (--__cn2->end_ != __ip)
1998 memmove(__ip, __ip+1, (__cn2->end_ - __ip)*sizeof(__i_node*));
Howard Hinnant920b56c2011-09-27 23:55:03 +00001999 }
2000 }
2001 __db->unlock();
2002#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002003 }
2004}
2005
2006template <class _Tp, class _Alloc>
2007void
2008list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __i)
2009{
Howard Hinnant920b56c2011-09-27 23:55:03 +00002010#if _LIBCPP_DEBUG_LEVEL >= 2
2011 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2012 "list::splice(iterator, list, iterator) called with first iterator not"
2013 " referring to this list");
2014 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__i) == &__c,
2015 "list::splice(iterator, list, iterator) called with second iterator not"
2016 " referring to list argument");
2017 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(&__i),
2018 "list::splice(iterator, list, iterator) called with second iterator not"
2019 " derefereceable");
2020#endif
2021 if (__p.__ptr_ != __i.__ptr_ && __p.__ptr_ != __i.__ptr_->__next_)
Howard Hinnant3e519522010-05-11 19:42:16 +00002022 {
Eric Fiselierb88ea352015-12-30 20:57:59 +00002023 __link_pointer __f = __i.__ptr_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002024 base::__unlink_nodes(__f, __f);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002025 __link_nodes(__p.__ptr_, __f, __f);
Howard Hinnant3e519522010-05-11 19:42:16 +00002026 --__c.__sz();
2027 ++base::__sz();
Howard Hinnant920b56c2011-09-27 23:55:03 +00002028#if _LIBCPP_DEBUG_LEVEL >= 2
2029 __libcpp_db* __db = __get_db();
2030 __c_node* __cn1 = __db->__find_c_and_lock(this);
2031 __c_node* __cn2 = __db->__find_c(&__c);
Eric Fiselier878e7e22016-10-23 19:26:39 +00002032 for (__i_node** __ip = __cn2->end_; __ip != __cn2->beg_;)
Howard Hinnant920b56c2011-09-27 23:55:03 +00002033 {
Eric Fiselier878e7e22016-10-23 19:26:39 +00002034 --__ip;
2035 iterator* __j = static_cast<iterator*>((*__ip)->__i_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002036 if (__j->__ptr_ == __f)
Howard Hinnant920b56c2011-09-27 23:55:03 +00002037 {
Eric Fiselier878e7e22016-10-23 19:26:39 +00002038 __cn1->__add(*__ip);
2039 (*__ip)->__c_ = __cn1;
2040 if (--__cn2->end_ != __ip)
2041 memmove(__ip, __ip+1, (__cn2->end_ - __ip)*sizeof(__i_node*));
Howard Hinnant920b56c2011-09-27 23:55:03 +00002042 }
2043 }
2044 __db->unlock();
2045#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002046 }
2047}
2048
2049template <class _Tp, class _Alloc>
2050void
2051list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l)
2052{
Howard Hinnant920b56c2011-09-27 23:55:03 +00002053#if _LIBCPP_DEBUG_LEVEL >= 2
2054 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2055 "list::splice(iterator, list, iterator, iterator) called with first iterator not"
2056 " referring to this list");
2057 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__f) == &__c,
2058 "list::splice(iterator, list, iterator, iterator) called with second iterator not"
2059 " referring to list argument");
2060 if (this == &__c)
2061 {
2062 for (const_iterator __i = __f; __i != __l; ++__i)
2063 _LIBCPP_ASSERT(__i != __p,
2064 "list::splice(iterator, list, iterator, iterator)"
2065 " called with the first iterator within the range"
2066 " of the second and third iterators");
2067 }
2068#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002069 if (__f != __l)
2070 {
2071 if (this != &__c)
2072 {
Howard Hinnantce48a112011-06-30 21:18:19 +00002073 size_type __s = _VSTD::distance(__f, __l);
Howard Hinnant3e519522010-05-11 19:42:16 +00002074 __c.__sz() -= __s;
2075 base::__sz() += __s;
2076 }
Eric Fiselierb88ea352015-12-30 20:57:59 +00002077 __link_pointer __first = __f.__ptr_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002078 --__l;
Eric Fiselierb88ea352015-12-30 20:57:59 +00002079 __link_pointer __last = __l.__ptr_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002080 base::__unlink_nodes(__first, __last);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002081 __link_nodes(__p.__ptr_, __first, __last);
Howard Hinnant920b56c2011-09-27 23:55:03 +00002082#if _LIBCPP_DEBUG_LEVEL >= 2
2083 __libcpp_db* __db = __get_db();
2084 __c_node* __cn1 = __db->__find_c_and_lock(this);
2085 __c_node* __cn2 = __db->__find_c(&__c);
Eric Fiselier878e7e22016-10-23 19:26:39 +00002086 for (__i_node** __ip = __cn2->end_; __ip != __cn2->beg_;)
Howard Hinnant920b56c2011-09-27 23:55:03 +00002087 {
Eric Fiselier878e7e22016-10-23 19:26:39 +00002088 --__ip;
2089 iterator* __j = static_cast<iterator*>((*__ip)->__i_);
Eric Fiselierb88ea352015-12-30 20:57:59 +00002090 for (__link_pointer __k = __f.__ptr_;
Howard Hinnant920b56c2011-09-27 23:55:03 +00002091 __k != __l.__ptr_; __k = __k->__next_)
2092 {
2093 if (__j->__ptr_ == __k)
2094 {
Eric Fiselier878e7e22016-10-23 19:26:39 +00002095 __cn1->__add(*__ip);
2096 (*__ip)->__c_ = __cn1;
2097 if (--__cn2->end_ != __ip)
2098 memmove(__ip, __ip+1, (__cn2->end_ - __ip)*sizeof(__i_node*));
Howard Hinnant920b56c2011-09-27 23:55:03 +00002099 }
2100 }
2101 }
2102 __db->unlock();
2103#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002104 }
2105}
2106
2107template <class _Tp, class _Alloc>
2108void
2109list<_Tp, _Alloc>::remove(const value_type& __x)
2110{
Eric Fiselier5cac7752016-12-14 22:48:38 +00002111 list<_Tp, _Alloc> __deleted_nodes(get_allocator()); // collect the nodes we're removing
Marshall Clowced70062014-08-08 15:35:52 +00002112 for (const_iterator __i = begin(), __e = end(); __i != __e;)
Howard Hinnant3e519522010-05-11 19:42:16 +00002113 {
2114 if (*__i == __x)
2115 {
Marshall Clowced70062014-08-08 15:35:52 +00002116 const_iterator __j = _VSTD::next(__i);
Howard Hinnant3e519522010-05-11 19:42:16 +00002117 for (; __j != __e && *__j == __x; ++__j)
2118 ;
Marshall Clowced70062014-08-08 15:35:52 +00002119 __deleted_nodes.splice(__deleted_nodes.end(), *this, __i, __j);
2120 __i = __j;
Marshall Clow90ba05332014-08-04 17:32:25 +00002121 if (__i != __e)
Marshall Clow28d65da2014-08-05 01:34:12 +00002122 ++__i;
Howard Hinnant3e519522010-05-11 19:42:16 +00002123 }
2124 else
2125 ++__i;
2126 }
2127}
2128
2129template <class _Tp, class _Alloc>
2130template <class _Pred>
2131void
2132list<_Tp, _Alloc>::remove_if(_Pred __pred)
2133{
2134 for (iterator __i = begin(), __e = end(); __i != __e;)
2135 {
2136 if (__pred(*__i))
2137 {
Howard Hinnantce48a112011-06-30 21:18:19 +00002138 iterator __j = _VSTD::next(__i);
Howard Hinnant3e519522010-05-11 19:42:16 +00002139 for (; __j != __e && __pred(*__j); ++__j)
2140 ;
2141 __i = erase(__i, __j);
Marshall Clow90ba05332014-08-04 17:32:25 +00002142 if (__i != __e)
Marshall Clow28d65da2014-08-05 01:34:12 +00002143 ++__i;
Howard Hinnant3e519522010-05-11 19:42:16 +00002144 }
2145 else
2146 ++__i;
2147 }
2148}
2149
2150template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00002151inline
Howard Hinnant3e519522010-05-11 19:42:16 +00002152void
2153list<_Tp, _Alloc>::unique()
2154{
2155 unique(__equal_to<value_type>());
2156}
2157
2158template <class _Tp, class _Alloc>
2159template <class _BinaryPred>
2160void
2161list<_Tp, _Alloc>::unique(_BinaryPred __binary_pred)
2162{
2163 for (iterator __i = begin(), __e = end(); __i != __e;)
2164 {
Howard Hinnantce48a112011-06-30 21:18:19 +00002165 iterator __j = _VSTD::next(__i);
Howard Hinnant3e519522010-05-11 19:42:16 +00002166 for (; __j != __e && __binary_pred(*__i, *__j); ++__j)
2167 ;
2168 if (++__i != __j)
2169 __i = erase(__i, __j);
2170 }
2171}
2172
2173template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00002174inline
Howard Hinnant3e519522010-05-11 19:42:16 +00002175void
2176list<_Tp, _Alloc>::merge(list& __c)
2177{
2178 merge(__c, __less<value_type>());
2179}
2180
2181template <class _Tp, class _Alloc>
2182template <class _Comp>
2183void
2184list<_Tp, _Alloc>::merge(list& __c, _Comp __comp)
2185{
2186 if (this != &__c)
2187 {
2188 iterator __f1 = begin();
2189 iterator __e1 = end();
2190 iterator __f2 = __c.begin();
2191 iterator __e2 = __c.end();
2192 while (__f1 != __e1 && __f2 != __e2)
2193 {
2194 if (__comp(*__f2, *__f1))
2195 {
2196 size_type __ds = 1;
Howard Hinnantce48a112011-06-30 21:18:19 +00002197 iterator __m2 = _VSTD::next(__f2);
Howard Hinnant3e519522010-05-11 19:42:16 +00002198 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2, ++__ds)
2199 ;
2200 base::__sz() += __ds;
2201 __c.__sz() -= __ds;
Eric Fiselierb88ea352015-12-30 20:57:59 +00002202 __link_pointer __f = __f2.__ptr_;
2203 __link_pointer __l = __m2.__ptr_->__prev_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002204 __f2 = __m2;
2205 base::__unlink_nodes(__f, __l);
Howard Hinnantce48a112011-06-30 21:18:19 +00002206 __m2 = _VSTD::next(__f1);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002207 __link_nodes(__f1.__ptr_, __f, __l);
Howard Hinnant3e519522010-05-11 19:42:16 +00002208 __f1 = __m2;
2209 }
2210 else
2211 ++__f1;
2212 }
2213 splice(__e1, __c);
Howard Hinnant920b56c2011-09-27 23:55:03 +00002214#if _LIBCPP_DEBUG_LEVEL >= 2
2215 __libcpp_db* __db = __get_db();
2216 __c_node* __cn1 = __db->__find_c_and_lock(this);
2217 __c_node* __cn2 = __db->__find_c(&__c);
2218 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
2219 {
2220 --__p;
2221 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Eric Fiselier5243e192016-01-04 03:27:52 +00002222 if (__i->__ptr_ != __c.__end_as_link())
Howard Hinnant920b56c2011-09-27 23:55:03 +00002223 {
2224 __cn1->__add(*__p);
2225 (*__p)->__c_ = __cn1;
2226 if (--__cn2->end_ != __p)
2227 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
2228 }
2229 }
2230 __db->unlock();
2231#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002232 }
2233}
2234
2235template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00002236inline
Howard Hinnant3e519522010-05-11 19:42:16 +00002237void
2238list<_Tp, _Alloc>::sort()
2239{
2240 sort(__less<value_type>());
2241}
2242
2243template <class _Tp, class _Alloc>
2244template <class _Comp>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00002245inline
Howard Hinnant3e519522010-05-11 19:42:16 +00002246void
2247list<_Tp, _Alloc>::sort(_Comp __comp)
2248{
2249 __sort(begin(), end(), base::__sz(), __comp);
2250}
2251
2252template <class _Tp, class _Alloc>
2253template <class _Comp>
Howard Hinnant3e519522010-05-11 19:42:16 +00002254typename list<_Tp, _Alloc>::iterator
2255list<_Tp, _Alloc>::__sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp)
2256{
2257 switch (__n)
2258 {
2259 case 0:
2260 case 1:
2261 return __f1;
2262 case 2:
2263 if (__comp(*--__e2, *__f1))
2264 {
Eric Fiselierb88ea352015-12-30 20:57:59 +00002265 __link_pointer __f = __e2.__ptr_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002266 base::__unlink_nodes(__f, __f);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002267 __link_nodes(__f1.__ptr_, __f, __f);
Howard Hinnant3e519522010-05-11 19:42:16 +00002268 return __e2;
2269 }
2270 return __f1;
2271 }
2272 size_type __n2 = __n / 2;
Howard Hinnantce48a112011-06-30 21:18:19 +00002273 iterator __e1 = _VSTD::next(__f1, __n2);
Howard Hinnant3e519522010-05-11 19:42:16 +00002274 iterator __r = __f1 = __sort(__f1, __e1, __n2, __comp);
2275 iterator __f2 = __e1 = __sort(__e1, __e2, __n - __n2, __comp);
2276 if (__comp(*__f2, *__f1))
2277 {
Howard Hinnantce48a112011-06-30 21:18:19 +00002278 iterator __m2 = _VSTD::next(__f2);
Howard Hinnant3e519522010-05-11 19:42:16 +00002279 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2)
2280 ;
Eric Fiselierb88ea352015-12-30 20:57:59 +00002281 __link_pointer __f = __f2.__ptr_;
2282 __link_pointer __l = __m2.__ptr_->__prev_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002283 __r = __f2;
2284 __e1 = __f2 = __m2;
2285 base::__unlink_nodes(__f, __l);
Howard Hinnantce48a112011-06-30 21:18:19 +00002286 __m2 = _VSTD::next(__f1);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002287 __link_nodes(__f1.__ptr_, __f, __l);
Howard Hinnant3e519522010-05-11 19:42:16 +00002288 __f1 = __m2;
2289 }
2290 else
2291 ++__f1;
2292 while (__f1 != __e1 && __f2 != __e2)
2293 {
2294 if (__comp(*__f2, *__f1))
2295 {
Howard Hinnantce48a112011-06-30 21:18:19 +00002296 iterator __m2 = _VSTD::next(__f2);
Howard Hinnant3e519522010-05-11 19:42:16 +00002297 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2)
2298 ;
Eric Fiselierb88ea352015-12-30 20:57:59 +00002299 __link_pointer __f = __f2.__ptr_;
2300 __link_pointer __l = __m2.__ptr_->__prev_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002301 if (__e1 == __f2)
2302 __e1 = __m2;
2303 __f2 = __m2;
2304 base::__unlink_nodes(__f, __l);
Howard Hinnantce48a112011-06-30 21:18:19 +00002305 __m2 = _VSTD::next(__f1);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002306 __link_nodes(__f1.__ptr_, __f, __l);
Howard Hinnant3e519522010-05-11 19:42:16 +00002307 __f1 = __m2;
2308 }
2309 else
2310 ++__f1;
2311 }
2312 return __r;
2313}
2314
2315template <class _Tp, class _Alloc>
2316void
Howard Hinnant45900102011-06-03 17:30:28 +00002317list<_Tp, _Alloc>::reverse() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00002318{
2319 if (base::__sz() > 1)
2320 {
2321 iterator __e = end();
Howard Hinnant920b56c2011-09-27 23:55:03 +00002322 for (iterator __i = begin(); __i.__ptr_ != __e.__ptr_;)
2323 {
Howard Hinnantce48a112011-06-30 21:18:19 +00002324 _VSTD::swap(__i.__ptr_->__prev_, __i.__ptr_->__next_);
Howard Hinnant920b56c2011-09-27 23:55:03 +00002325 __i.__ptr_ = __i.__ptr_->__prev_;
2326 }
Howard Hinnantce48a112011-06-30 21:18:19 +00002327 _VSTD::swap(__e.__ptr_->__prev_, __e.__ptr_->__next_);
Howard Hinnant3e519522010-05-11 19:42:16 +00002328 }
2329}
2330
2331template <class _Tp, class _Alloc>
Howard Hinnant920b56c2011-09-27 23:55:03 +00002332bool
2333list<_Tp, _Alloc>::__invariants() const
2334{
2335 return size() == _VSTD::distance(begin(), end());
2336}
2337
2338#if _LIBCPP_DEBUG_LEVEL >= 2
2339
2340template <class _Tp, class _Alloc>
2341bool
2342list<_Tp, _Alloc>::__dereferenceable(const const_iterator* __i) const
2343{
Eric Fiselier5243e192016-01-04 03:27:52 +00002344 return __i->__ptr_ != this->__end_as_link();
Howard Hinnant920b56c2011-09-27 23:55:03 +00002345}
2346
2347template <class _Tp, class _Alloc>
2348bool
2349list<_Tp, _Alloc>::__decrementable(const const_iterator* __i) const
2350{
2351 return !empty() && __i->__ptr_ != base::__end_.__next_;
2352}
2353
2354template <class _Tp, class _Alloc>
2355bool
Eric Fiselierfd838222016-12-23 23:37:52 +00002356list<_Tp, _Alloc>::__addable(const const_iterator*, ptrdiff_t) const
Howard Hinnant920b56c2011-09-27 23:55:03 +00002357{
2358 return false;
2359}
2360
2361template <class _Tp, class _Alloc>
2362bool
Eric Fiselierfd838222016-12-23 23:37:52 +00002363list<_Tp, _Alloc>::__subscriptable(const const_iterator*, ptrdiff_t) const
Howard Hinnant920b56c2011-09-27 23:55:03 +00002364{
2365 return false;
2366}
2367
2368#endif // _LIBCPP_DEBUG_LEVEL >= 2
2369
2370template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00002371inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00002372bool
2373operator==(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2374{
Howard Hinnantce48a112011-06-30 21:18:19 +00002375 return __x.size() == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnant3e519522010-05-11 19:42:16 +00002376}
2377
2378template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00002379inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00002380bool
2381operator< (const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2382{
Howard Hinnantce48a112011-06-30 21:18:19 +00002383 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnant3e519522010-05-11 19:42:16 +00002384}
2385
2386template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00002387inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00002388bool
2389operator!=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2390{
2391 return !(__x == __y);
2392}
2393
2394template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00002395inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00002396bool
2397operator> (const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2398{
2399 return __y < __x;
2400}
2401
2402template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00002403inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00002404bool
2405operator>=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2406{
2407 return !(__x < __y);
2408}
2409
2410template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00002411inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00002412bool
2413operator<=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2414{
2415 return !(__y < __x);
2416}
2417
2418template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00002419inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00002420void
2421swap(list<_Tp, _Alloc>& __x, list<_Tp, _Alloc>& __y)
Howard Hinnant45900102011-06-03 17:30:28 +00002422 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnant3e519522010-05-11 19:42:16 +00002423{
2424 __x.swap(__y);
2425}
2426
2427_LIBCPP_END_NAMESPACE_STD
2428
Eric Fiseliera016efb2017-05-31 22:07:49 +00002429_LIBCPP_POP_MACROS
2430
Howard Hinnant3e519522010-05-11 19:42:16 +00002431#endif // _LIBCPP_LIST