blob: f884b168126f4724c8a3822b5f60101f4cfcd4eb [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),
Marshall Clowed9010a2017-10-23 16:46:44 +0000484 "Attempted to dereference a non-dereferenceable list::const_iterator");
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000485#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();}
Marshall Clow72c8fad2017-11-15 05:51:26 +0000899 _LIBCPP_NODISCARD_AFTER_CXX17 _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
Eric Fiselier0a412f42017-10-17 19:12:23 +00001074 typedef __allocator_destructor<__node_allocator> __node_destructor;
1075 typedef unique_ptr<__node, __node_destructor> __hold_pointer;
1076
1077 _LIBCPP_INLINE_VISIBILITY
1078 __hold_pointer __allocate_node(__node_allocator& __na) {
1079 __node_pointer __p = __node_alloc_traits::allocate(__na, 1);
1080 __p->__prev_ = nullptr;
1081 return __hold_pointer(__p, __node_destructor(__na, 1));
1082 }
1083
Howard Hinnant920b56c2011-09-27 23:55:03 +00001084#if _LIBCPP_DEBUG_LEVEL >= 2
1085
1086 bool __dereferenceable(const const_iterator* __i) const;
1087 bool __decrementable(const const_iterator* __i) const;
1088 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1089 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1090
1091#endif // _LIBCPP_DEBUG_LEVEL >= 2
1092
Howard Hinnant3e519522010-05-11 19:42:16 +00001093private:
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001094 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierb88ea352015-12-30 20:57:59 +00001095 static void __link_nodes (__link_pointer __p, __link_pointer __f, __link_pointer __l);
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001096 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierb88ea352015-12-30 20:57:59 +00001097 void __link_nodes_at_front(__link_pointer __f, __link_pointer __l);
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001098 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierb88ea352015-12-30 20:57:59 +00001099 void __link_nodes_at_back (__link_pointer __f, __link_pointer __l);
Howard Hinnant3e519522010-05-11 19:42:16 +00001100 iterator __iterator(size_type __n);
1101 template <class _Comp>
1102 static iterator __sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp);
1103
Howard Hinnant45900102011-06-03 17:30:28 +00001104 void __move_assign(list& __c, true_type)
1105 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value);
Howard Hinnant3e519522010-05-11 19:42:16 +00001106 void __move_assign(list& __c, false_type);
1107};
1108
1109// Link in nodes [__f, __l] just prior to __p
1110template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001111inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001112void
Eric Fiselierb88ea352015-12-30 20:57:59 +00001113list<_Tp, _Alloc>::__link_nodes(__link_pointer __p, __link_pointer __f, __link_pointer __l)
Howard Hinnant3e519522010-05-11 19:42:16 +00001114{
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001115 __p->__prev_->__next_ = __f;
1116 __f->__prev_ = __p->__prev_;
1117 __p->__prev_ = __l;
1118 __l->__next_ = __p;
Howard Hinnant3e519522010-05-11 19:42:16 +00001119}
1120
Marshall Clow28d65da2014-08-05 01:34:12 +00001121// Link in nodes [__f, __l] at the front of the list
1122template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001123inline
Marshall Clow28d65da2014-08-05 01:34:12 +00001124void
Eric Fiselierb88ea352015-12-30 20:57:59 +00001125list<_Tp, _Alloc>::__link_nodes_at_front(__link_pointer __f, __link_pointer __l)
Marshall Clow28d65da2014-08-05 01:34:12 +00001126{
Eric Fiselier5243e192016-01-04 03:27:52 +00001127 __f->__prev_ = base::__end_as_link();
Marshall Clowced70062014-08-08 15:35:52 +00001128 __l->__next_ = base::__end_.__next_;
1129 __l->__next_->__prev_ = __l;
1130 base::__end_.__next_ = __f;
Marshall Clow28d65da2014-08-05 01:34:12 +00001131}
1132
1133// Link in nodes [__f, __l] at the front of the list
1134template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001135inline
Marshall Clow28d65da2014-08-05 01:34:12 +00001136void
Eric Fiselierb88ea352015-12-30 20:57:59 +00001137list<_Tp, _Alloc>::__link_nodes_at_back(__link_pointer __f, __link_pointer __l)
Marshall Clow28d65da2014-08-05 01:34:12 +00001138{
Eric Fiselier5243e192016-01-04 03:27:52 +00001139 __l->__next_ = base::__end_as_link();
Marshall Clowced70062014-08-08 15:35:52 +00001140 __f->__prev_ = base::__end_.__prev_;
1141 __f->__prev_->__next_ = __f;
1142 base::__end_.__prev_ = __l;
Marshall Clow28d65da2014-08-05 01:34:12 +00001143}
1144
1145
Howard Hinnant3e519522010-05-11 19:42:16 +00001146template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001147inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001148typename list<_Tp, _Alloc>::iterator
1149list<_Tp, _Alloc>::__iterator(size_type __n)
1150{
Howard Hinnantce48a112011-06-30 21:18:19 +00001151 return __n <= base::__sz() / 2 ? _VSTD::next(begin(), __n)
1152 : _VSTD::prev(end(), base::__sz() - __n);
Howard Hinnant3e519522010-05-11 19:42:16 +00001153}
1154
1155template <class _Tp, class _Alloc>
1156list<_Tp, _Alloc>::list(size_type __n)
1157{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001158#if _LIBCPP_DEBUG_LEVEL >= 2
1159 __get_db()->__insert_c(this);
1160#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001161 for (; __n > 0; --__n)
Eric Fiseliercf9ed002017-04-16 03:45:35 +00001162#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001163 emplace_back();
1164#else
1165 push_back(value_type());
1166#endif
1167}
1168
Marshall Clowfb829762013-09-08 19:11:51 +00001169#if _LIBCPP_STD_VER > 11
1170template <class _Tp, class _Alloc>
1171list<_Tp, _Alloc>::list(size_type __n, const allocator_type& __a) : base(__a)
1172{
1173#if _LIBCPP_DEBUG_LEVEL >= 2
1174 __get_db()->__insert_c(this);
1175#endif
1176 for (; __n > 0; --__n)
Marshall Clowfb829762013-09-08 19:11:51 +00001177 emplace_back();
Marshall Clowfb829762013-09-08 19:11:51 +00001178}
1179#endif
1180
Howard Hinnant3e519522010-05-11 19:42:16 +00001181template <class _Tp, class _Alloc>
1182list<_Tp, _Alloc>::list(size_type __n, const value_type& __x)
1183{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001184#if _LIBCPP_DEBUG_LEVEL >= 2
1185 __get_db()->__insert_c(this);
1186#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001187 for (; __n > 0; --__n)
1188 push_back(__x);
1189}
1190
1191template <class _Tp, class _Alloc>
1192list<_Tp, _Alloc>::list(size_type __n, const value_type& __x, const allocator_type& __a)
1193 : base(__a)
1194{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001195#if _LIBCPP_DEBUG_LEVEL >= 2
1196 __get_db()->__insert_c(this);
1197#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001198 for (; __n > 0; --__n)
1199 push_back(__x);
1200}
1201
1202template <class _Tp, class _Alloc>
1203template <class _InpIter>
1204list<_Tp, _Alloc>::list(_InpIter __f, _InpIter __l,
1205 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1206{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001207#if _LIBCPP_DEBUG_LEVEL >= 2
1208 __get_db()->__insert_c(this);
1209#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001210 for (; __f != __l; ++__f)
Eric Fiselier1c0cedc2017-10-17 13:03:17 +00001211 __emplace_back(*__f);
Howard Hinnant3e519522010-05-11 19:42:16 +00001212}
1213
1214template <class _Tp, class _Alloc>
1215template <class _InpIter>
1216list<_Tp, _Alloc>::list(_InpIter __f, _InpIter __l, const allocator_type& __a,
1217 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1218 : base(__a)
1219{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001220#if _LIBCPP_DEBUG_LEVEL >= 2
1221 __get_db()->__insert_c(this);
1222#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001223 for (; __f != __l; ++__f)
Eric Fiselier1c0cedc2017-10-17 13:03:17 +00001224 __emplace_back(*__f);
Howard Hinnant3e519522010-05-11 19:42:16 +00001225}
1226
1227template <class _Tp, class _Alloc>
1228list<_Tp, _Alloc>::list(const list& __c)
1229 : base(allocator_type(
1230 __node_alloc_traits::select_on_container_copy_construction(
1231 __c.__node_alloc())))
1232{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001233#if _LIBCPP_DEBUG_LEVEL >= 2
1234 __get_db()->__insert_c(this);
1235#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001236 for (const_iterator __i = __c.begin(), __e = __c.end(); __i != __e; ++__i)
1237 push_back(*__i);
1238}
1239
1240template <class _Tp, class _Alloc>
1241list<_Tp, _Alloc>::list(const list& __c, const allocator_type& __a)
1242 : base(__a)
1243{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001244#if _LIBCPP_DEBUG_LEVEL >= 2
1245 __get_db()->__insert_c(this);
1246#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001247 for (const_iterator __i = __c.begin(), __e = __c.end(); __i != __e; ++__i)
1248 push_back(*__i);
1249}
1250
Eric Fiseliercf9ed002017-04-16 03:45:35 +00001251#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant54976f22011-08-12 21:56:02 +00001252
Howard Hinnant3e519522010-05-11 19:42:16 +00001253template <class _Tp, class _Alloc>
1254list<_Tp, _Alloc>::list(initializer_list<value_type> __il, const allocator_type& __a)
1255 : base(__a)
1256{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001257#if _LIBCPP_DEBUG_LEVEL >= 2
1258 __get_db()->__insert_c(this);
1259#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001260 for (typename initializer_list<value_type>::const_iterator __i = __il.begin(),
1261 __e = __il.end(); __i != __e; ++__i)
1262 push_back(*__i);
1263}
1264
1265template <class _Tp, class _Alloc>
1266list<_Tp, _Alloc>::list(initializer_list<value_type> __il)
1267{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001268#if _LIBCPP_DEBUG_LEVEL >= 2
1269 __get_db()->__insert_c(this);
1270#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001271 for (typename initializer_list<value_type>::const_iterator __i = __il.begin(),
1272 __e = __il.end(); __i != __e; ++__i)
1273 push_back(*__i);
1274}
1275
Howard Hinnant3e519522010-05-11 19:42:16 +00001276template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001277inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001278list<_Tp, _Alloc>::list(list&& __c)
Howard Hinnant45900102011-06-03 17:30:28 +00001279 _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value)
Howard Hinnantce48a112011-06-30 21:18:19 +00001280 : base(allocator_type(_VSTD::move(__c.__node_alloc())))
Howard Hinnant3e519522010-05-11 19:42:16 +00001281{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001282#if _LIBCPP_DEBUG_LEVEL >= 2
1283 __get_db()->__insert_c(this);
1284#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001285 splice(end(), __c);
1286}
1287
1288template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001289inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001290list<_Tp, _Alloc>::list(list&& __c, const allocator_type& __a)
1291 : base(__a)
1292{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001293#if _LIBCPP_DEBUG_LEVEL >= 2
1294 __get_db()->__insert_c(this);
1295#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001296 if (__a == __c.get_allocator())
1297 splice(end(), __c);
1298 else
1299 {
Howard Hinnantc003db12011-11-29 18:15:50 +00001300 typedef move_iterator<iterator> _Ip;
1301 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnant3e519522010-05-11 19:42:16 +00001302 }
1303}
1304
1305template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001306inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001307list<_Tp, _Alloc>&
1308list<_Tp, _Alloc>::operator=(list&& __c)
Howard Hinnant45900102011-06-03 17:30:28 +00001309 _NOEXCEPT_(
1310 __node_alloc_traits::propagate_on_container_move_assignment::value &&
1311 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +00001312{
1313 __move_assign(__c, integral_constant<bool,
1314 __node_alloc_traits::propagate_on_container_move_assignment::value>());
1315 return *this;
1316}
1317
1318template <class _Tp, class _Alloc>
1319void
1320list<_Tp, _Alloc>::__move_assign(list& __c, false_type)
1321{
1322 if (base::__node_alloc() != __c.__node_alloc())
1323 {
Howard Hinnantc003db12011-11-29 18:15:50 +00001324 typedef move_iterator<iterator> _Ip;
1325 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnant3e519522010-05-11 19:42:16 +00001326 }
1327 else
1328 __move_assign(__c, true_type());
1329}
1330
1331template <class _Tp, class _Alloc>
1332void
1333list<_Tp, _Alloc>::__move_assign(list& __c, true_type)
Howard Hinnant45900102011-06-03 17:30:28 +00001334 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +00001335{
1336 clear();
1337 base::__move_assign_alloc(__c);
1338 splice(end(), __c);
1339}
1340
Eric Fiseliercf9ed002017-04-16 03:45:35 +00001341#endif // _LIBCPP_CXX03_LANG
1342
1343template <class _Tp, class _Alloc>
1344inline
1345list<_Tp, _Alloc>&
1346list<_Tp, _Alloc>::operator=(const list& __c)
1347{
1348 if (this != &__c)
1349 {
1350 base::__copy_assign_alloc(__c);
1351 assign(__c.begin(), __c.end());
1352 }
1353 return *this;
1354}
Howard Hinnant3e519522010-05-11 19:42:16 +00001355
1356template <class _Tp, class _Alloc>
1357template <class _InpIter>
1358void
1359list<_Tp, _Alloc>::assign(_InpIter __f, _InpIter __l,
1360 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1361{
1362 iterator __i = begin();
1363 iterator __e = end();
1364 for (; __f != __l && __i != __e; ++__f, ++__i)
1365 *__i = *__f;
1366 if (__i == __e)
1367 insert(__e, __f, __l);
1368 else
1369 erase(__i, __e);
Eric Fiselier2e519572016-12-28 06:06:09 +00001370#if _LIBCPP_DEBUG_LEVEL >= 2
1371 __get_db()->__invalidate_all(this);
1372#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001373}
1374
1375template <class _Tp, class _Alloc>
1376void
1377list<_Tp, _Alloc>::assign(size_type __n, const value_type& __x)
1378{
1379 iterator __i = begin();
1380 iterator __e = end();
1381 for (; __n > 0 && __i != __e; --__n, ++__i)
1382 *__i = __x;
1383 if (__i == __e)
1384 insert(__e, __n, __x);
1385 else
1386 erase(__i, __e);
Eric Fiselier2e519572016-12-28 06:06:09 +00001387#if _LIBCPP_DEBUG_LEVEL >= 2
1388 __get_db()->__invalidate_all(this);
1389#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001390}
1391
1392template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001393inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001394_Alloc
Howard Hinnant45900102011-06-03 17:30:28 +00001395list<_Tp, _Alloc>::get_allocator() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001396{
1397 return allocator_type(base::__node_alloc());
1398}
1399
1400template <class _Tp, class _Alloc>
1401typename list<_Tp, _Alloc>::iterator
1402list<_Tp, _Alloc>::insert(const_iterator __p, const value_type& __x)
1403{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001404#if _LIBCPP_DEBUG_LEVEL >= 2
1405 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1406 "list::insert(iterator, x) called with an iterator not"
1407 " referring to this list");
1408#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001409 __node_allocator& __na = base::__node_alloc();
Eric Fiselier0a412f42017-10-17 19:12:23 +00001410 __hold_pointer __hold = __allocate_node(__na);
Howard Hinnantce48a112011-06-30 21:18:19 +00001411 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001412 __link_nodes(__p.__ptr_, __hold->__as_link(), __hold->__as_link());
Howard Hinnant3e519522010-05-11 19:42:16 +00001413 ++base::__sz();
Howard Hinnantb0e4c9d2013-04-05 00:18:49 +00001414#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselierb88ea352015-12-30 20:57:59 +00001415 return iterator(__hold.release()->__as_link(), this);
Howard Hinnantb0e4c9d2013-04-05 00:18:49 +00001416#else
Eric Fiselierb88ea352015-12-30 20:57:59 +00001417 return iterator(__hold.release()->__as_link());
Howard Hinnantb0e4c9d2013-04-05 00:18:49 +00001418#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001419}
1420
1421template <class _Tp, class _Alloc>
1422typename list<_Tp, _Alloc>::iterator
1423list<_Tp, _Alloc>::insert(const_iterator __p, size_type __n, const value_type& __x)
1424{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001425#if _LIBCPP_DEBUG_LEVEL >= 2
1426 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1427 "list::insert(iterator, n, x) called with an iterator not"
1428 " referring to this list");
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001429 iterator __r(__p.__ptr_, this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001430#else
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001431 iterator __r(__p.__ptr_);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001432#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001433 if (__n > 0)
1434 {
1435 size_type __ds = 0;
1436 __node_allocator& __na = base::__node_alloc();
Eric Fiselier0a412f42017-10-17 19:12:23 +00001437 __hold_pointer __hold = __allocate_node(__na);
Howard Hinnantce48a112011-06-30 21:18:19 +00001438 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnant3e519522010-05-11 19:42:16 +00001439 ++__ds;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001440#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselierb88ea352015-12-30 20:57:59 +00001441 __r = iterator(__hold->__as_link(), this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001442#else
Eric Fiselierb88ea352015-12-30 20:57:59 +00001443 __r = iterator(__hold->__as_link());
Howard Hinnant920b56c2011-09-27 23:55:03 +00001444#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001445 __hold.release();
1446 iterator __e = __r;
1447#ifndef _LIBCPP_NO_EXCEPTIONS
1448 try
1449 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001450#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001451 for (--__n; __n != 0; --__n, ++__e, ++__ds)
1452 {
1453 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001454 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001455 __e.__ptr_->__next_ = __hold->__as_link();
Howard Hinnant3e519522010-05-11 19:42:16 +00001456 __hold->__prev_ = __e.__ptr_;
1457 __hold.release();
1458 }
1459#ifndef _LIBCPP_NO_EXCEPTIONS
1460 }
1461 catch (...)
1462 {
1463 while (true)
1464 {
Howard Hinnantce48a112011-06-30 21:18:19 +00001465 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Eric Fiselierb88ea352015-12-30 20:57:59 +00001466 __link_pointer __prev = __e.__ptr_->__prev_;
1467 __node_alloc_traits::deallocate(__na, __e.__ptr_->__as_node(), 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001468 if (__prev == 0)
1469 break;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001470#if _LIBCPP_DEBUG_LEVEL >= 2
1471 __e = iterator(__prev, this);
1472#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001473 __e = iterator(__prev);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001474#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001475 }
1476 throw;
1477 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001478#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001479 __link_nodes(__p.__ptr_, __r.__ptr_, __e.__ptr_);
Howard Hinnant3e519522010-05-11 19:42:16 +00001480 base::__sz() += __ds;
1481 }
1482 return __r;
1483}
1484
1485template <class _Tp, class _Alloc>
1486template <class _InpIter>
1487typename list<_Tp, _Alloc>::iterator
1488list<_Tp, _Alloc>::insert(const_iterator __p, _InpIter __f, _InpIter __l,
1489 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1490{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001491#if _LIBCPP_DEBUG_LEVEL >= 2
1492 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1493 "list::insert(iterator, range) called with an iterator not"
1494 " referring to this list");
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001495 iterator __r(__p.__ptr_, this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001496#else
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001497 iterator __r(__p.__ptr_);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001498#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001499 if (__f != __l)
1500 {
1501 size_type __ds = 0;
1502 __node_allocator& __na = base::__node_alloc();
Eric Fiselier0a412f42017-10-17 19:12:23 +00001503 __hold_pointer __hold = __allocate_node(__na);
Howard Hinnantce48a112011-06-30 21:18:19 +00001504 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), *__f);
Howard Hinnant3e519522010-05-11 19:42:16 +00001505 ++__ds;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001506#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselierb88ea352015-12-30 20:57:59 +00001507 __r = iterator(__hold.get()->__as_link(), this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001508#else
Eric Fiselierb88ea352015-12-30 20:57:59 +00001509 __r = iterator(__hold.get()->__as_link());
Howard Hinnant920b56c2011-09-27 23:55:03 +00001510#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001511 __hold.release();
1512 iterator __e = __r;
1513#ifndef _LIBCPP_NO_EXCEPTIONS
1514 try
1515 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001516#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiselier61bff612015-03-19 03:20:02 +00001517 for (++__f; __f != __l; ++__f, (void) ++__e, (void) ++__ds)
Howard Hinnant3e519522010-05-11 19:42:16 +00001518 {
1519 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001520 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), *__f);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001521 __e.__ptr_->__next_ = __hold.get()->__as_link();
Howard Hinnant3e519522010-05-11 19:42:16 +00001522 __hold->__prev_ = __e.__ptr_;
1523 __hold.release();
1524 }
1525#ifndef _LIBCPP_NO_EXCEPTIONS
1526 }
1527 catch (...)
1528 {
1529 while (true)
1530 {
Howard Hinnantce48a112011-06-30 21:18:19 +00001531 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Eric Fiselierb88ea352015-12-30 20:57:59 +00001532 __link_pointer __prev = __e.__ptr_->__prev_;
1533 __node_alloc_traits::deallocate(__na, __e.__ptr_->__as_node(), 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001534 if (__prev == 0)
1535 break;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001536#if _LIBCPP_DEBUG_LEVEL >= 2
1537 __e = iterator(__prev, this);
1538#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001539 __e = iterator(__prev);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001540#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001541 }
1542 throw;
1543 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001544#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001545 __link_nodes(__p.__ptr_, __r.__ptr_, __e.__ptr_);
Howard Hinnant3e519522010-05-11 19:42:16 +00001546 base::__sz() += __ds;
1547 }
1548 return __r;
1549}
1550
1551template <class _Tp, class _Alloc>
1552void
1553list<_Tp, _Alloc>::push_front(const value_type& __x)
1554{
1555 __node_allocator& __na = base::__node_alloc();
Eric Fiselier0a412f42017-10-17 19:12:23 +00001556 __hold_pointer __hold = __allocate_node(__na);
Howard Hinnantce48a112011-06-30 21:18:19 +00001557 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001558 __link_pointer __nl = __hold->__as_link();
1559 __link_nodes_at_front(__nl, __nl);
Howard Hinnant3e519522010-05-11 19:42:16 +00001560 ++base::__sz();
1561 __hold.release();
1562}
1563
1564template <class _Tp, class _Alloc>
1565void
1566list<_Tp, _Alloc>::push_back(const value_type& __x)
1567{
1568 __node_allocator& __na = base::__node_alloc();
Eric Fiselier0a412f42017-10-17 19:12:23 +00001569 __hold_pointer __hold = __allocate_node(__na);
Howard Hinnantce48a112011-06-30 21:18:19 +00001570 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001571 __link_nodes_at_back(__hold.get()->__as_link(), __hold.get()->__as_link());
Howard Hinnant3e519522010-05-11 19:42:16 +00001572 ++base::__sz();
1573 __hold.release();
1574}
1575
Eric Fiseliercf9ed002017-04-16 03:45:35 +00001576#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001577
1578template <class _Tp, class _Alloc>
1579void
1580list<_Tp, _Alloc>::push_front(value_type&& __x)
1581{
1582 __node_allocator& __na = base::__node_alloc();
Eric Fiselier0a412f42017-10-17 19:12:23 +00001583 __hold_pointer __hold = __allocate_node(__na);
Howard Hinnantce48a112011-06-30 21:18:19 +00001584 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
Eric Fiselierb88ea352015-12-30 20:57:59 +00001585 __link_nodes_at_front(__hold.get()->__as_link(), __hold.get()->__as_link());
Howard Hinnant3e519522010-05-11 19:42:16 +00001586 ++base::__sz();
1587 __hold.release();
1588}
1589
1590template <class _Tp, class _Alloc>
1591void
1592list<_Tp, _Alloc>::push_back(value_type&& __x)
1593{
1594 __node_allocator& __na = base::__node_alloc();
Eric Fiselier0a412f42017-10-17 19:12:23 +00001595 __hold_pointer __hold = __allocate_node(__na);
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();
Eric Fiselier0a412f42017-10-17 19:12:23 +00001612 __hold_pointer __hold = __allocate_node(__na);
Howard Hinnantce48a112011-06-30 21:18:19 +00001613 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001614 __link_nodes_at_front(__hold.get()->__as_link(), __hold.get()->__as_link());
Howard Hinnant3e519522010-05-11 19:42:16 +00001615 ++base::__sz();
Marshall Clow63b560b2017-01-24 23:09:12 +00001616#if _LIBCPP_STD_VER > 14
Eric Fiselier0e411642016-07-21 03:20:17 +00001617 return __hold.release()->__value_;
Marshall Clow63b560b2017-01-24 23:09:12 +00001618#else
1619 __hold.release();
1620#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001621}
1622
1623template <class _Tp, class _Alloc>
1624template <class... _Args>
Marshall Clow63b560b2017-01-24 23:09:12 +00001625#if _LIBCPP_STD_VER > 14
Eric Fiselier0e411642016-07-21 03:20:17 +00001626typename list<_Tp, _Alloc>::reference
Marshall Clow63b560b2017-01-24 23:09:12 +00001627#else
1628void
1629#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001630list<_Tp, _Alloc>::emplace_back(_Args&&... __args)
1631{
1632 __node_allocator& __na = base::__node_alloc();
Eric Fiselier0a412f42017-10-17 19:12:23 +00001633 __hold_pointer __hold = __allocate_node(__na);
Howard Hinnantce48a112011-06-30 21:18:19 +00001634 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001635 __link_pointer __nl = __hold->__as_link();
1636 __link_nodes_at_back(__nl, __nl);
Howard Hinnant3e519522010-05-11 19:42:16 +00001637 ++base::__sz();
Marshall Clow63b560b2017-01-24 23:09:12 +00001638#if _LIBCPP_STD_VER > 14
Eric Fiselier0e411642016-07-21 03:20:17 +00001639 return __hold.release()->__value_;
Marshall Clow63b560b2017-01-24 23:09:12 +00001640#else
1641 __hold.release();
1642#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001643}
1644
1645template <class _Tp, class _Alloc>
1646template <class... _Args>
1647typename list<_Tp, _Alloc>::iterator
1648list<_Tp, _Alloc>::emplace(const_iterator __p, _Args&&... __args)
1649{
Howard Hinnantb0e4c9d2013-04-05 00:18:49 +00001650#if _LIBCPP_DEBUG_LEVEL >= 2
1651 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1652 "list::emplace(iterator, args...) called with an iterator not"
1653 " referring to this list");
1654#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001655 __node_allocator& __na = base::__node_alloc();
Eric Fiselier0a412f42017-10-17 19:12:23 +00001656 __hold_pointer __hold = __allocate_node(__na);
Howard Hinnantce48a112011-06-30 21:18:19 +00001657 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001658 __link_pointer __nl = __hold.get()->__as_link();
1659 __link_nodes(__p.__ptr_, __nl, __nl);
Howard Hinnant3e519522010-05-11 19:42:16 +00001660 ++base::__sz();
Eric Fiselierb88ea352015-12-30 20:57:59 +00001661 __hold.release();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001662#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselierb88ea352015-12-30 20:57:59 +00001663 return iterator(__nl, this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001664#else
Eric Fiselierb88ea352015-12-30 20:57:59 +00001665 return iterator(__nl);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001666#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001667}
1668
1669template <class _Tp, class _Alloc>
1670typename list<_Tp, _Alloc>::iterator
1671list<_Tp, _Alloc>::insert(const_iterator __p, value_type&& __x)
1672{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001673#if _LIBCPP_DEBUG_LEVEL >= 2
1674 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1675 "list::insert(iterator, x) called with an iterator not"
1676 " referring to this list");
1677#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001678 __node_allocator& __na = base::__node_alloc();
Eric Fiselier0a412f42017-10-17 19:12:23 +00001679 __hold_pointer __hold = __allocate_node(__na);
Howard Hinnantce48a112011-06-30 21:18:19 +00001680 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
Eric Fiselierb88ea352015-12-30 20:57:59 +00001681 __link_pointer __nl = __hold->__as_link();
1682 __link_nodes(__p.__ptr_, __nl, __nl);
Howard Hinnant3e519522010-05-11 19:42:16 +00001683 ++base::__sz();
Eric Fiselierb88ea352015-12-30 20:57:59 +00001684 __hold.release();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001685#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselierb88ea352015-12-30 20:57:59 +00001686 return iterator(__nl, this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001687#else
Eric Fiselierb88ea352015-12-30 20:57:59 +00001688 return iterator(__nl);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001689#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001690}
1691
Eric Fiseliercf9ed002017-04-16 03:45:35 +00001692#endif // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001693
1694template <class _Tp, class _Alloc>
1695void
1696list<_Tp, _Alloc>::pop_front()
1697{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001698 _LIBCPP_ASSERT(!empty(), "list::pop_front() called with empty list");
Howard Hinnant3e519522010-05-11 19:42:16 +00001699 __node_allocator& __na = base::__node_alloc();
Eric Fiselierb88ea352015-12-30 20:57:59 +00001700 __link_pointer __n = base::__end_.__next_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001701 base::__unlink_nodes(__n, __n);
1702 --base::__sz();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001703#if _LIBCPP_DEBUG_LEVEL >= 2
1704 __c_node* __c = __get_db()->__find_c_and_lock(this);
1705 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1706 {
1707 --__p;
1708 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001709 if (__i->__ptr_ == __n)
Howard Hinnant920b56c2011-09-27 23:55:03 +00001710 {
1711 (*__p)->__c_ = nullptr;
1712 if (--__c->end_ != __p)
1713 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1714 }
1715 }
1716 __get_db()->unlock();
1717#endif
Eric Fiselierb88ea352015-12-30 20:57:59 +00001718 __node_pointer __np = __n->__as_node();
1719 __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
1720 __node_alloc_traits::deallocate(__na, __np, 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001721}
1722
1723template <class _Tp, class _Alloc>
1724void
1725list<_Tp, _Alloc>::pop_back()
1726{
Dmitri Gribenkoc3f9c802013-06-24 06:15:57 +00001727 _LIBCPP_ASSERT(!empty(), "list::pop_back() called with empty list");
Howard Hinnant3e519522010-05-11 19:42:16 +00001728 __node_allocator& __na = base::__node_alloc();
Eric Fiselierb88ea352015-12-30 20:57:59 +00001729 __link_pointer __n = base::__end_.__prev_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001730 base::__unlink_nodes(__n, __n);
1731 --base::__sz();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001732#if _LIBCPP_DEBUG_LEVEL >= 2
1733 __c_node* __c = __get_db()->__find_c_and_lock(this);
1734 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1735 {
1736 --__p;
1737 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001738 if (__i->__ptr_ == __n)
Howard Hinnant920b56c2011-09-27 23:55:03 +00001739 {
1740 (*__p)->__c_ = nullptr;
1741 if (--__c->end_ != __p)
1742 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1743 }
1744 }
1745 __get_db()->unlock();
1746#endif
Eric Fiselierb88ea352015-12-30 20:57:59 +00001747 __node_pointer __np = __n->__as_node();
1748 __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
1749 __node_alloc_traits::deallocate(__na, __np, 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001750}
1751
1752template <class _Tp, class _Alloc>
1753typename list<_Tp, _Alloc>::iterator
1754list<_Tp, _Alloc>::erase(const_iterator __p)
1755{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001756#if _LIBCPP_DEBUG_LEVEL >= 2
1757 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1758 "list::erase(iterator) called with an iterator not"
1759 " referring to this list");
1760#endif
Howard Hinnantb0e4c9d2013-04-05 00:18:49 +00001761 _LIBCPP_ASSERT(__p != end(),
1762 "list::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnant3e519522010-05-11 19:42:16 +00001763 __node_allocator& __na = base::__node_alloc();
Eric Fiselierb88ea352015-12-30 20:57:59 +00001764 __link_pointer __n = __p.__ptr_;
1765 __link_pointer __r = __n->__next_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001766 base::__unlink_nodes(__n, __n);
1767 --base::__sz();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001768#if _LIBCPP_DEBUG_LEVEL >= 2
1769 __c_node* __c = __get_db()->__find_c_and_lock(this);
Eric Fiselier878e7e22016-10-23 19:26:39 +00001770 for (__i_node** __ip = __c->end_; __ip != __c->beg_; )
Howard Hinnant920b56c2011-09-27 23:55:03 +00001771 {
Eric Fiselier878e7e22016-10-23 19:26:39 +00001772 --__ip;
1773 iterator* __i = static_cast<iterator*>((*__ip)->__i_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001774 if (__i->__ptr_ == __n)
Howard Hinnant920b56c2011-09-27 23:55:03 +00001775 {
Eric Fiselier878e7e22016-10-23 19:26:39 +00001776 (*__ip)->__c_ = nullptr;
1777 if (--__c->end_ != __ip)
1778 memmove(__ip, __ip+1, (__c->end_ - __ip)*sizeof(__i_node*));
Howard Hinnant920b56c2011-09-27 23:55:03 +00001779 }
1780 }
1781 __get_db()->unlock();
1782#endif
Eric Fiselierb88ea352015-12-30 20:57:59 +00001783 __node_pointer __np = __n->__as_node();
1784 __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
1785 __node_alloc_traits::deallocate(__na, __np, 1);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001786#if _LIBCPP_DEBUG_LEVEL >= 2
1787 return iterator(__r, this);
1788#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001789 return iterator(__r);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001790#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001791}
1792
1793template <class _Tp, class _Alloc>
1794typename list<_Tp, _Alloc>::iterator
1795list<_Tp, _Alloc>::erase(const_iterator __f, const_iterator __l)
1796{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001797#if _LIBCPP_DEBUG_LEVEL >= 2
1798 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__f) == this,
1799 "list::erase(iterator, iterator) called with an iterator not"
1800 " referring to this list");
Eric Fiselier2e519572016-12-28 06:06:09 +00001801 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__l) == this,
1802 "list::erase(iterator, iterator) called with an iterator not"
1803 " referring to this list");
Howard Hinnant920b56c2011-09-27 23:55:03 +00001804#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001805 if (__f != __l)
1806 {
1807 __node_allocator& __na = base::__node_alloc();
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001808 base::__unlink_nodes(__f.__ptr_, __l.__ptr_->__prev_);
Howard Hinnant3e519522010-05-11 19:42:16 +00001809 while (__f != __l)
1810 {
Eric Fiselierb88ea352015-12-30 20:57:59 +00001811 __link_pointer __n = __f.__ptr_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001812 ++__f;
1813 --base::__sz();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001814#if _LIBCPP_DEBUG_LEVEL >= 2
1815 __c_node* __c = __get_db()->__find_c_and_lock(this);
1816 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1817 {
1818 --__p;
1819 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001820 if (__i->__ptr_ == __n)
Howard Hinnant920b56c2011-09-27 23:55:03 +00001821 {
1822 (*__p)->__c_ = nullptr;
1823 if (--__c->end_ != __p)
1824 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1825 }
1826 }
1827 __get_db()->unlock();
1828#endif
Eric Fiselierb88ea352015-12-30 20:57:59 +00001829 __node_pointer __np = __n->__as_node();
1830 __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
1831 __node_alloc_traits::deallocate(__na, __np, 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001832 }
1833 }
Howard Hinnant920b56c2011-09-27 23:55:03 +00001834#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001835 return iterator(__l.__ptr_, this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001836#else
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001837 return iterator(__l.__ptr_);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001838#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001839}
1840
1841template <class _Tp, class _Alloc>
1842void
1843list<_Tp, _Alloc>::resize(size_type __n)
1844{
1845 if (__n < base::__sz())
1846 erase(__iterator(__n), end());
1847 else if (__n > base::__sz())
1848 {
1849 __n -= base::__sz();
1850 size_type __ds = 0;
1851 __node_allocator& __na = base::__node_alloc();
Eric Fiselier0a412f42017-10-17 19:12:23 +00001852 __hold_pointer __hold = __allocate_node(__na);
Howard Hinnantce48a112011-06-30 21:18:19 +00001853 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_));
Howard Hinnant3e519522010-05-11 19:42:16 +00001854 ++__ds;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001855#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselierb88ea352015-12-30 20:57:59 +00001856 iterator __r = iterator(__hold.release()->__as_link(), this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001857#else
Eric Fiselierb88ea352015-12-30 20:57:59 +00001858 iterator __r = iterator(__hold.release()->__as_link());
Howard Hinnant920b56c2011-09-27 23:55:03 +00001859#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001860 iterator __e = __r;
1861#ifndef _LIBCPP_NO_EXCEPTIONS
1862 try
1863 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001864#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001865 for (--__n; __n != 0; --__n, ++__e, ++__ds)
1866 {
1867 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001868 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_));
Eric Fiselierb88ea352015-12-30 20:57:59 +00001869 __e.__ptr_->__next_ = __hold.get()->__as_link();
Howard Hinnant3e519522010-05-11 19:42:16 +00001870 __hold->__prev_ = __e.__ptr_;
1871 __hold.release();
1872 }
1873#ifndef _LIBCPP_NO_EXCEPTIONS
1874 }
1875 catch (...)
1876 {
1877 while (true)
1878 {
Howard Hinnantce48a112011-06-30 21:18:19 +00001879 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Eric Fiselierb88ea352015-12-30 20:57:59 +00001880 __link_pointer __prev = __e.__ptr_->__prev_;
1881 __node_alloc_traits::deallocate(__na, __e.__ptr_->__as_node(), 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001882 if (__prev == 0)
1883 break;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001884#if _LIBCPP_DEBUG_LEVEL >= 2
1885 __e = iterator(__prev, this);
1886#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001887 __e = iterator(__prev);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001888#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001889 }
1890 throw;
1891 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001892#endif // _LIBCPP_NO_EXCEPTIONS
Marshall Clow28d65da2014-08-05 01:34:12 +00001893 __link_nodes_at_back(__r.__ptr_, __e.__ptr_);
Howard Hinnant3e519522010-05-11 19:42:16 +00001894 base::__sz() += __ds;
1895 }
1896}
1897
1898template <class _Tp, class _Alloc>
1899void
1900list<_Tp, _Alloc>::resize(size_type __n, const value_type& __x)
1901{
1902 if (__n < base::__sz())
1903 erase(__iterator(__n), end());
1904 else if (__n > base::__sz())
1905 {
1906 __n -= base::__sz();
1907 size_type __ds = 0;
1908 __node_allocator& __na = base::__node_alloc();
Eric Fiselier0a412f42017-10-17 19:12:23 +00001909 __hold_pointer __hold = __allocate_node(__na);
Howard Hinnantce48a112011-06-30 21:18:19 +00001910 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnant3e519522010-05-11 19:42:16 +00001911 ++__ds;
Eric Fiselierb88ea352015-12-30 20:57:59 +00001912 __link_pointer __nl = __hold.release()->__as_link();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001913#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselierb88ea352015-12-30 20:57:59 +00001914 iterator __r = iterator(__nl, this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001915#else
Eric Fiselierb88ea352015-12-30 20:57:59 +00001916 iterator __r = iterator(__nl);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001917#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001918 iterator __e = __r;
1919#ifndef _LIBCPP_NO_EXCEPTIONS
1920 try
1921 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001922#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001923 for (--__n; __n != 0; --__n, ++__e, ++__ds)
1924 {
1925 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001926 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001927 __e.__ptr_->__next_ = __hold.get()->__as_link();
Howard Hinnant3e519522010-05-11 19:42:16 +00001928 __hold->__prev_ = __e.__ptr_;
1929 __hold.release();
1930 }
1931#ifndef _LIBCPP_NO_EXCEPTIONS
1932 }
1933 catch (...)
1934 {
1935 while (true)
1936 {
Howard Hinnantce48a112011-06-30 21:18:19 +00001937 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Eric Fiselierb88ea352015-12-30 20:57:59 +00001938 __link_pointer __prev = __e.__ptr_->__prev_;
1939 __node_alloc_traits::deallocate(__na, __e.__ptr_->__as_node(), 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001940 if (__prev == 0)
1941 break;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001942#if _LIBCPP_DEBUG_LEVEL >= 2
1943 __e = iterator(__prev, this);
1944#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001945 __e = iterator(__prev);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001946#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001947 }
1948 throw;
1949 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001950#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiselier5243e192016-01-04 03:27:52 +00001951 __link_nodes(base::__end_as_link(), __r.__ptr_, __e.__ptr_);
Howard Hinnant3e519522010-05-11 19:42:16 +00001952 base::__sz() += __ds;
Howard Hinnantb3371f62010-08-22 00:02:43 +00001953 }
Howard Hinnant3e519522010-05-11 19:42:16 +00001954}
1955
1956template <class _Tp, class _Alloc>
1957void
1958list<_Tp, _Alloc>::splice(const_iterator __p, list& __c)
1959{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001960 _LIBCPP_ASSERT(this != &__c,
1961 "list::splice(iterator, list) called with this == &list");
1962#if _LIBCPP_DEBUG_LEVEL >= 2
1963 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1964 "list::splice(iterator, list) called with an iterator not"
1965 " referring to this list");
1966#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001967 if (!__c.empty())
1968 {
Eric Fiselierb88ea352015-12-30 20:57:59 +00001969 __link_pointer __f = __c.__end_.__next_;
1970 __link_pointer __l = __c.__end_.__prev_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001971 base::__unlink_nodes(__f, __l);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001972 __link_nodes(__p.__ptr_, __f, __l);
Howard Hinnant3e519522010-05-11 19:42:16 +00001973 base::__sz() += __c.__sz();
1974 __c.__sz() = 0;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001975#if _LIBCPP_DEBUG_LEVEL >= 2
1976 __libcpp_db* __db = __get_db();
1977 __c_node* __cn1 = __db->__find_c_and_lock(this);
1978 __c_node* __cn2 = __db->__find_c(&__c);
Eric Fiselier878e7e22016-10-23 19:26:39 +00001979 for (__i_node** __ip = __cn2->end_; __ip != __cn2->beg_;)
Howard Hinnant920b56c2011-09-27 23:55:03 +00001980 {
Eric Fiselier878e7e22016-10-23 19:26:39 +00001981 --__ip;
1982 iterator* __i = static_cast<iterator*>((*__ip)->__i_);
Eric Fiselier5243e192016-01-04 03:27:52 +00001983 if (__i->__ptr_ != __c.__end_as_link())
Howard Hinnant920b56c2011-09-27 23:55:03 +00001984 {
Eric Fiselier878e7e22016-10-23 19:26:39 +00001985 __cn1->__add(*__ip);
1986 (*__ip)->__c_ = __cn1;
1987 if (--__cn2->end_ != __ip)
1988 memmove(__ip, __ip+1, (__cn2->end_ - __ip)*sizeof(__i_node*));
Howard Hinnant920b56c2011-09-27 23:55:03 +00001989 }
1990 }
1991 __db->unlock();
1992#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001993 }
1994}
1995
1996template <class _Tp, class _Alloc>
1997void
1998list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __i)
1999{
Howard Hinnant920b56c2011-09-27 23:55:03 +00002000#if _LIBCPP_DEBUG_LEVEL >= 2
2001 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2002 "list::splice(iterator, list, iterator) called with first iterator not"
2003 " referring to this list");
2004 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__i) == &__c,
2005 "list::splice(iterator, list, iterator) called with second iterator not"
2006 " referring to list argument");
2007 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(&__i),
2008 "list::splice(iterator, list, iterator) called with second iterator not"
2009 " derefereceable");
2010#endif
2011 if (__p.__ptr_ != __i.__ptr_ && __p.__ptr_ != __i.__ptr_->__next_)
Howard Hinnant3e519522010-05-11 19:42:16 +00002012 {
Eric Fiselierb88ea352015-12-30 20:57:59 +00002013 __link_pointer __f = __i.__ptr_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002014 base::__unlink_nodes(__f, __f);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002015 __link_nodes(__p.__ptr_, __f, __f);
Howard Hinnant3e519522010-05-11 19:42:16 +00002016 --__c.__sz();
2017 ++base::__sz();
Howard Hinnant920b56c2011-09-27 23:55:03 +00002018#if _LIBCPP_DEBUG_LEVEL >= 2
2019 __libcpp_db* __db = __get_db();
2020 __c_node* __cn1 = __db->__find_c_and_lock(this);
2021 __c_node* __cn2 = __db->__find_c(&__c);
Eric Fiselier878e7e22016-10-23 19:26:39 +00002022 for (__i_node** __ip = __cn2->end_; __ip != __cn2->beg_;)
Howard Hinnant920b56c2011-09-27 23:55:03 +00002023 {
Eric Fiselier878e7e22016-10-23 19:26:39 +00002024 --__ip;
2025 iterator* __j = static_cast<iterator*>((*__ip)->__i_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002026 if (__j->__ptr_ == __f)
Howard Hinnant920b56c2011-09-27 23:55:03 +00002027 {
Eric Fiselier878e7e22016-10-23 19:26:39 +00002028 __cn1->__add(*__ip);
2029 (*__ip)->__c_ = __cn1;
2030 if (--__cn2->end_ != __ip)
2031 memmove(__ip, __ip+1, (__cn2->end_ - __ip)*sizeof(__i_node*));
Howard Hinnant920b56c2011-09-27 23:55:03 +00002032 }
2033 }
2034 __db->unlock();
2035#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002036 }
2037}
2038
2039template <class _Tp, class _Alloc>
2040void
2041list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l)
2042{
Howard Hinnant920b56c2011-09-27 23:55:03 +00002043#if _LIBCPP_DEBUG_LEVEL >= 2
2044 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2045 "list::splice(iterator, list, iterator, iterator) called with first iterator not"
2046 " referring to this list");
2047 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__f) == &__c,
2048 "list::splice(iterator, list, iterator, iterator) called with second iterator not"
2049 " referring to list argument");
2050 if (this == &__c)
2051 {
2052 for (const_iterator __i = __f; __i != __l; ++__i)
2053 _LIBCPP_ASSERT(__i != __p,
2054 "list::splice(iterator, list, iterator, iterator)"
2055 " called with the first iterator within the range"
2056 " of the second and third iterators");
2057 }
2058#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002059 if (__f != __l)
2060 {
Eric Fiselierb88ea352015-12-30 20:57:59 +00002061 __link_pointer __first = __f.__ptr_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002062 --__l;
Eric Fiselierb88ea352015-12-30 20:57:59 +00002063 __link_pointer __last = __l.__ptr_;
Eric Fiselierd053b592018-01-25 00:02:48 +00002064 if (this != &__c)
2065 {
2066 size_type __s = _VSTD::distance(__f, __l) + 1;
2067 __c.__sz() -= __s;
2068 base::__sz() += __s;
2069 }
Howard Hinnant3e519522010-05-11 19:42:16 +00002070 base::__unlink_nodes(__first, __last);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002071 __link_nodes(__p.__ptr_, __first, __last);
Howard Hinnant920b56c2011-09-27 23:55:03 +00002072#if _LIBCPP_DEBUG_LEVEL >= 2
2073 __libcpp_db* __db = __get_db();
2074 __c_node* __cn1 = __db->__find_c_and_lock(this);
2075 __c_node* __cn2 = __db->__find_c(&__c);
Eric Fiselier878e7e22016-10-23 19:26:39 +00002076 for (__i_node** __ip = __cn2->end_; __ip != __cn2->beg_;)
Howard Hinnant920b56c2011-09-27 23:55:03 +00002077 {
Eric Fiselier878e7e22016-10-23 19:26:39 +00002078 --__ip;
2079 iterator* __j = static_cast<iterator*>((*__ip)->__i_);
Eric Fiselierb88ea352015-12-30 20:57:59 +00002080 for (__link_pointer __k = __f.__ptr_;
Howard Hinnant920b56c2011-09-27 23:55:03 +00002081 __k != __l.__ptr_; __k = __k->__next_)
2082 {
2083 if (__j->__ptr_ == __k)
2084 {
Eric Fiselier878e7e22016-10-23 19:26:39 +00002085 __cn1->__add(*__ip);
2086 (*__ip)->__c_ = __cn1;
2087 if (--__cn2->end_ != __ip)
2088 memmove(__ip, __ip+1, (__cn2->end_ - __ip)*sizeof(__i_node*));
Howard Hinnant920b56c2011-09-27 23:55:03 +00002089 }
2090 }
2091 }
2092 __db->unlock();
2093#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002094 }
2095}
2096
2097template <class _Tp, class _Alloc>
2098void
2099list<_Tp, _Alloc>::remove(const value_type& __x)
2100{
Eric Fiselier5cac7752016-12-14 22:48:38 +00002101 list<_Tp, _Alloc> __deleted_nodes(get_allocator()); // collect the nodes we're removing
Marshall Clowced70062014-08-08 15:35:52 +00002102 for (const_iterator __i = begin(), __e = end(); __i != __e;)
Howard Hinnant3e519522010-05-11 19:42:16 +00002103 {
2104 if (*__i == __x)
2105 {
Marshall Clowced70062014-08-08 15:35:52 +00002106 const_iterator __j = _VSTD::next(__i);
Howard Hinnant3e519522010-05-11 19:42:16 +00002107 for (; __j != __e && *__j == __x; ++__j)
2108 ;
Marshall Clowced70062014-08-08 15:35:52 +00002109 __deleted_nodes.splice(__deleted_nodes.end(), *this, __i, __j);
2110 __i = __j;
Marshall Clow90ba05332014-08-04 17:32:25 +00002111 if (__i != __e)
Marshall Clow28d65da2014-08-05 01:34:12 +00002112 ++__i;
Howard Hinnant3e519522010-05-11 19:42:16 +00002113 }
2114 else
2115 ++__i;
2116 }
2117}
2118
2119template <class _Tp, class _Alloc>
2120template <class _Pred>
2121void
2122list<_Tp, _Alloc>::remove_if(_Pred __pred)
2123{
2124 for (iterator __i = begin(), __e = end(); __i != __e;)
2125 {
2126 if (__pred(*__i))
2127 {
Howard Hinnantce48a112011-06-30 21:18:19 +00002128 iterator __j = _VSTD::next(__i);
Howard Hinnant3e519522010-05-11 19:42:16 +00002129 for (; __j != __e && __pred(*__j); ++__j)
2130 ;
2131 __i = erase(__i, __j);
Marshall Clow90ba05332014-08-04 17:32:25 +00002132 if (__i != __e)
Marshall Clow28d65da2014-08-05 01:34:12 +00002133 ++__i;
Howard Hinnant3e519522010-05-11 19:42:16 +00002134 }
2135 else
2136 ++__i;
2137 }
2138}
2139
2140template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00002141inline
Howard Hinnant3e519522010-05-11 19:42:16 +00002142void
2143list<_Tp, _Alloc>::unique()
2144{
2145 unique(__equal_to<value_type>());
2146}
2147
2148template <class _Tp, class _Alloc>
2149template <class _BinaryPred>
2150void
2151list<_Tp, _Alloc>::unique(_BinaryPred __binary_pred)
2152{
2153 for (iterator __i = begin(), __e = end(); __i != __e;)
2154 {
Howard Hinnantce48a112011-06-30 21:18:19 +00002155 iterator __j = _VSTD::next(__i);
Howard Hinnant3e519522010-05-11 19:42:16 +00002156 for (; __j != __e && __binary_pred(*__i, *__j); ++__j)
2157 ;
2158 if (++__i != __j)
2159 __i = erase(__i, __j);
2160 }
2161}
2162
2163template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00002164inline
Howard Hinnant3e519522010-05-11 19:42:16 +00002165void
2166list<_Tp, _Alloc>::merge(list& __c)
2167{
2168 merge(__c, __less<value_type>());
2169}
2170
2171template <class _Tp, class _Alloc>
2172template <class _Comp>
2173void
2174list<_Tp, _Alloc>::merge(list& __c, _Comp __comp)
2175{
2176 if (this != &__c)
2177 {
2178 iterator __f1 = begin();
2179 iterator __e1 = end();
2180 iterator __f2 = __c.begin();
2181 iterator __e2 = __c.end();
2182 while (__f1 != __e1 && __f2 != __e2)
2183 {
2184 if (__comp(*__f2, *__f1))
2185 {
2186 size_type __ds = 1;
Howard Hinnantce48a112011-06-30 21:18:19 +00002187 iterator __m2 = _VSTD::next(__f2);
Howard Hinnant3e519522010-05-11 19:42:16 +00002188 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2, ++__ds)
2189 ;
2190 base::__sz() += __ds;
2191 __c.__sz() -= __ds;
Eric Fiselierb88ea352015-12-30 20:57:59 +00002192 __link_pointer __f = __f2.__ptr_;
2193 __link_pointer __l = __m2.__ptr_->__prev_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002194 __f2 = __m2;
2195 base::__unlink_nodes(__f, __l);
Howard Hinnantce48a112011-06-30 21:18:19 +00002196 __m2 = _VSTD::next(__f1);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002197 __link_nodes(__f1.__ptr_, __f, __l);
Howard Hinnant3e519522010-05-11 19:42:16 +00002198 __f1 = __m2;
2199 }
2200 else
2201 ++__f1;
2202 }
2203 splice(__e1, __c);
Howard Hinnant920b56c2011-09-27 23:55:03 +00002204#if _LIBCPP_DEBUG_LEVEL >= 2
2205 __libcpp_db* __db = __get_db();
2206 __c_node* __cn1 = __db->__find_c_and_lock(this);
2207 __c_node* __cn2 = __db->__find_c(&__c);
2208 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
2209 {
2210 --__p;
2211 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Eric Fiselier5243e192016-01-04 03:27:52 +00002212 if (__i->__ptr_ != __c.__end_as_link())
Howard Hinnant920b56c2011-09-27 23:55:03 +00002213 {
2214 __cn1->__add(*__p);
2215 (*__p)->__c_ = __cn1;
2216 if (--__cn2->end_ != __p)
2217 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
2218 }
2219 }
2220 __db->unlock();
2221#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002222 }
2223}
2224
2225template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00002226inline
Howard Hinnant3e519522010-05-11 19:42:16 +00002227void
2228list<_Tp, _Alloc>::sort()
2229{
2230 sort(__less<value_type>());
2231}
2232
2233template <class _Tp, class _Alloc>
2234template <class _Comp>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00002235inline
Howard Hinnant3e519522010-05-11 19:42:16 +00002236void
2237list<_Tp, _Alloc>::sort(_Comp __comp)
2238{
2239 __sort(begin(), end(), base::__sz(), __comp);
2240}
2241
2242template <class _Tp, class _Alloc>
2243template <class _Comp>
Howard Hinnant3e519522010-05-11 19:42:16 +00002244typename list<_Tp, _Alloc>::iterator
2245list<_Tp, _Alloc>::__sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp)
2246{
2247 switch (__n)
2248 {
2249 case 0:
2250 case 1:
2251 return __f1;
2252 case 2:
2253 if (__comp(*--__e2, *__f1))
2254 {
Eric Fiselierb88ea352015-12-30 20:57:59 +00002255 __link_pointer __f = __e2.__ptr_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002256 base::__unlink_nodes(__f, __f);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002257 __link_nodes(__f1.__ptr_, __f, __f);
Howard Hinnant3e519522010-05-11 19:42:16 +00002258 return __e2;
2259 }
2260 return __f1;
2261 }
2262 size_type __n2 = __n / 2;
Howard Hinnantce48a112011-06-30 21:18:19 +00002263 iterator __e1 = _VSTD::next(__f1, __n2);
Howard Hinnant3e519522010-05-11 19:42:16 +00002264 iterator __r = __f1 = __sort(__f1, __e1, __n2, __comp);
2265 iterator __f2 = __e1 = __sort(__e1, __e2, __n - __n2, __comp);
2266 if (__comp(*__f2, *__f1))
2267 {
Howard Hinnantce48a112011-06-30 21:18:19 +00002268 iterator __m2 = _VSTD::next(__f2);
Howard Hinnant3e519522010-05-11 19:42:16 +00002269 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2)
2270 ;
Eric Fiselierb88ea352015-12-30 20:57:59 +00002271 __link_pointer __f = __f2.__ptr_;
2272 __link_pointer __l = __m2.__ptr_->__prev_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002273 __r = __f2;
2274 __e1 = __f2 = __m2;
2275 base::__unlink_nodes(__f, __l);
Howard Hinnantce48a112011-06-30 21:18:19 +00002276 __m2 = _VSTD::next(__f1);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002277 __link_nodes(__f1.__ptr_, __f, __l);
Howard Hinnant3e519522010-05-11 19:42:16 +00002278 __f1 = __m2;
2279 }
2280 else
2281 ++__f1;
2282 while (__f1 != __e1 && __f2 != __e2)
2283 {
2284 if (__comp(*__f2, *__f1))
2285 {
Howard Hinnantce48a112011-06-30 21:18:19 +00002286 iterator __m2 = _VSTD::next(__f2);
Howard Hinnant3e519522010-05-11 19:42:16 +00002287 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2)
2288 ;
Eric Fiselierb88ea352015-12-30 20:57:59 +00002289 __link_pointer __f = __f2.__ptr_;
2290 __link_pointer __l = __m2.__ptr_->__prev_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002291 if (__e1 == __f2)
2292 __e1 = __m2;
2293 __f2 = __m2;
2294 base::__unlink_nodes(__f, __l);
Howard Hinnantce48a112011-06-30 21:18:19 +00002295 __m2 = _VSTD::next(__f1);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002296 __link_nodes(__f1.__ptr_, __f, __l);
Howard Hinnant3e519522010-05-11 19:42:16 +00002297 __f1 = __m2;
2298 }
2299 else
2300 ++__f1;
2301 }
2302 return __r;
2303}
2304
2305template <class _Tp, class _Alloc>
2306void
Howard Hinnant45900102011-06-03 17:30:28 +00002307list<_Tp, _Alloc>::reverse() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00002308{
2309 if (base::__sz() > 1)
2310 {
2311 iterator __e = end();
Howard Hinnant920b56c2011-09-27 23:55:03 +00002312 for (iterator __i = begin(); __i.__ptr_ != __e.__ptr_;)
2313 {
Howard Hinnantce48a112011-06-30 21:18:19 +00002314 _VSTD::swap(__i.__ptr_->__prev_, __i.__ptr_->__next_);
Howard Hinnant920b56c2011-09-27 23:55:03 +00002315 __i.__ptr_ = __i.__ptr_->__prev_;
2316 }
Howard Hinnantce48a112011-06-30 21:18:19 +00002317 _VSTD::swap(__e.__ptr_->__prev_, __e.__ptr_->__next_);
Howard Hinnant3e519522010-05-11 19:42:16 +00002318 }
2319}
2320
2321template <class _Tp, class _Alloc>
Howard Hinnant920b56c2011-09-27 23:55:03 +00002322bool
2323list<_Tp, _Alloc>::__invariants() const
2324{
2325 return size() == _VSTD::distance(begin(), end());
2326}
2327
2328#if _LIBCPP_DEBUG_LEVEL >= 2
2329
2330template <class _Tp, class _Alloc>
2331bool
2332list<_Tp, _Alloc>::__dereferenceable(const const_iterator* __i) const
2333{
Eric Fiselier5243e192016-01-04 03:27:52 +00002334 return __i->__ptr_ != this->__end_as_link();
Howard Hinnant920b56c2011-09-27 23:55:03 +00002335}
2336
2337template <class _Tp, class _Alloc>
2338bool
2339list<_Tp, _Alloc>::__decrementable(const const_iterator* __i) const
2340{
2341 return !empty() && __i->__ptr_ != base::__end_.__next_;
2342}
2343
2344template <class _Tp, class _Alloc>
2345bool
Eric Fiselierfd838222016-12-23 23:37:52 +00002346list<_Tp, _Alloc>::__addable(const const_iterator*, ptrdiff_t) const
Howard Hinnant920b56c2011-09-27 23:55:03 +00002347{
2348 return false;
2349}
2350
2351template <class _Tp, class _Alloc>
2352bool
Eric Fiselierfd838222016-12-23 23:37:52 +00002353list<_Tp, _Alloc>::__subscriptable(const const_iterator*, ptrdiff_t) const
Howard Hinnant920b56c2011-09-27 23:55:03 +00002354{
2355 return false;
2356}
2357
2358#endif // _LIBCPP_DEBUG_LEVEL >= 2
2359
2360template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00002361inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00002362bool
2363operator==(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2364{
Howard Hinnantce48a112011-06-30 21:18:19 +00002365 return __x.size() == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnant3e519522010-05-11 19:42:16 +00002366}
2367
2368template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00002369inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00002370bool
2371operator< (const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2372{
Howard Hinnantce48a112011-06-30 21:18:19 +00002373 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnant3e519522010-05-11 19:42:16 +00002374}
2375
2376template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00002377inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00002378bool
2379operator!=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2380{
2381 return !(__x == __y);
2382}
2383
2384template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00002385inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00002386bool
2387operator> (const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2388{
2389 return __y < __x;
2390}
2391
2392template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00002393inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00002394bool
2395operator>=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2396{
2397 return !(__x < __y);
2398}
2399
2400template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00002401inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00002402bool
2403operator<=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2404{
2405 return !(__y < __x);
2406}
2407
2408template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00002409inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00002410void
2411swap(list<_Tp, _Alloc>& __x, list<_Tp, _Alloc>& __y)
Howard Hinnant45900102011-06-03 17:30:28 +00002412 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnant3e519522010-05-11 19:42:16 +00002413{
2414 __x.swap(__y);
2415}
2416
2417_LIBCPP_END_NAMESPACE_STD
2418
Eric Fiseliera016efb2017-05-31 22:07:49 +00002419_LIBCPP_POP_MACROS
2420
Howard Hinnant3e519522010-05-11 19:42:16 +00002421#endif // _LIBCPP_LIST