blob: d2e78cd66afb8d2d5e1978844d6f902cf81366d4 [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
Marshall Clow4a227e52018-05-20 14:05:31 +0000150
151template <class InputIterator, class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
152 list(InputIterator, InputIterator, Allocator = Allocator())
153 -> list<typename iterator_traits<InputIterator>::value_type, Allocator>; // C++17
154
Howard Hinnant3e519522010-05-11 19:42:16 +0000155template <class T, class Alloc>
156 bool operator==(const list<T,Alloc>& x, const list<T,Alloc>& y);
157template <class T, class Alloc>
158 bool operator< (const list<T,Alloc>& x, const list<T,Alloc>& y);
159template <class T, class Alloc>
160 bool operator!=(const list<T,Alloc>& x, const list<T,Alloc>& y);
161template <class T, class Alloc>
162 bool operator> (const list<T,Alloc>& x, const list<T,Alloc>& y);
163template <class T, class Alloc>
164 bool operator>=(const list<T,Alloc>& x, const list<T,Alloc>& y);
165template <class T, class Alloc>
166 bool operator<=(const list<T,Alloc>& x, const list<T,Alloc>& y);
167
168template <class T, class Alloc>
Howard Hinnant45900102011-06-03 17:30:28 +0000169 void swap(list<T,Alloc>& x, list<T,Alloc>& y)
170 noexcept(noexcept(x.swap(y)));
Howard Hinnant3e519522010-05-11 19:42:16 +0000171
172} // std
173
174*/
175
176#include <__config>
177
178#include <memory>
179#include <limits>
180#include <initializer_list>
181#include <iterator>
182#include <algorithm>
Eric Fiselierb88ea352015-12-30 20:57:59 +0000183#include <type_traits>
Howard Hinnant3e519522010-05-11 19:42:16 +0000184
Eric Fiselierc1bd9192014-08-10 23:53:08 +0000185#include <__debug>
Howard Hinnant42a30462013-08-02 00:26:35 +0000186
Howard Hinnant073458b2011-10-17 20:05:10 +0000187#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnant3e519522010-05-11 19:42:16 +0000188#pragma GCC system_header
Howard Hinnant073458b2011-10-17 20:05:10 +0000189#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000190
Eric Fiseliera016efb2017-05-31 22:07:49 +0000191_LIBCPP_PUSH_MACROS
192#include <__undef_macros>
193
194
Howard Hinnant3e519522010-05-11 19:42:16 +0000195_LIBCPP_BEGIN_NAMESPACE_STD
196
Howard Hinnantce534202011-06-14 19:58:17 +0000197template <class _Tp, class _VoidPtr> struct __list_node;
Eric Fiselierb88ea352015-12-30 20:57:59 +0000198template <class _Tp, class _VoidPtr> struct __list_node_base;
199
200template <class _Tp, class _VoidPtr>
201struct __list_node_pointer_traits {
202 typedef typename __rebind_pointer<_VoidPtr, __list_node<_Tp, _VoidPtr> >::type
203 __node_pointer;
204 typedef typename __rebind_pointer<_VoidPtr, __list_node_base<_Tp, _VoidPtr> >::type
205 __base_pointer;
206
207#if defined(_LIBCPP_ABI_LIST_REMOVE_NODE_POINTER_UB)
208 typedef __base_pointer __link_pointer;
209#else
210 typedef typename conditional<
211 is_pointer<_VoidPtr>::value,
212 __base_pointer,
213 __node_pointer
214 >::type __link_pointer;
215#endif
216
Eric Fiselier5243e192016-01-04 03:27:52 +0000217 typedef typename conditional<
218 is_same<__link_pointer, __node_pointer>::value,
219 __base_pointer,
220 __node_pointer
221 >::type __non_link_pointer;
222
223 static _LIBCPP_INLINE_VISIBILITY
224 __link_pointer __unsafe_link_pointer_cast(__link_pointer __p) {
225 return __p;
226 }
227
228 static _LIBCPP_INLINE_VISIBILITY
229 __link_pointer __unsafe_link_pointer_cast(__non_link_pointer __p) {
230 return static_cast<__link_pointer>(static_cast<_VoidPtr>(__p));
231 }
232
Eric Fiselierb88ea352015-12-30 20:57:59 +0000233};
Howard Hinnant3e519522010-05-11 19:42:16 +0000234
235template <class _Tp, class _VoidPtr>
236struct __list_node_base
237{
Eric Fiselierb88ea352015-12-30 20:57:59 +0000238 typedef __list_node_pointer_traits<_Tp, _VoidPtr> _NodeTraits;
239 typedef typename _NodeTraits::__node_pointer __node_pointer;
240 typedef typename _NodeTraits::__base_pointer __base_pointer;
241 typedef typename _NodeTraits::__link_pointer __link_pointer;
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000242
Eric Fiselierb88ea352015-12-30 20:57:59 +0000243 __link_pointer __prev_;
244 __link_pointer __next_;
Howard Hinnant3e519522010-05-11 19:42:16 +0000245
Howard Hinnant848a5372010-09-22 16:48:34 +0000246 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5243e192016-01-04 03:27:52 +0000247 __list_node_base() : __prev_(_NodeTraits::__unsafe_link_pointer_cast(__self())),
248 __next_(_NodeTraits::__unsafe_link_pointer_cast(__self())) {}
Marshall Clow28d65da2014-08-05 01:34:12 +0000249
250 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5243e192016-01-04 03:27:52 +0000251 __base_pointer __self() {
Eric Fiselierb88ea352015-12-30 20:57:59 +0000252 return pointer_traits<__base_pointer>::pointer_to(*this);
253 }
254
255 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierb88ea352015-12-30 20:57:59 +0000256 __node_pointer __as_node() {
Eric Fiselier5243e192016-01-04 03:27:52 +0000257 return static_cast<__node_pointer>(__self());
Marshall Clow28d65da2014-08-05 01:34:12 +0000258 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000259};
260
261template <class _Tp, class _VoidPtr>
262struct __list_node
263 : public __list_node_base<_Tp, _VoidPtr>
264{
265 _Tp __value_;
Eric Fiselier5243e192016-01-04 03:27:52 +0000266
267 typedef __list_node_base<_Tp, _VoidPtr> __base;
268 typedef typename __base::__link_pointer __link_pointer;
269
270 _LIBCPP_INLINE_VISIBILITY
271 __link_pointer __as_link() {
272 return static_cast<__link_pointer>(__base::__self());
273 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000274};
275
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +0000276template <class _Tp, class _Alloc = allocator<_Tp> > class _LIBCPP_TEMPLATE_VIS list;
Howard Hinnantce534202011-06-14 19:58:17 +0000277template <class _Tp, class _Alloc> class __list_imp;
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +0000278template <class _Tp, class _VoidPtr> class _LIBCPP_TEMPLATE_VIS __list_const_iterator;
Howard Hinnant3e519522010-05-11 19:42:16 +0000279
280template <class _Tp, class _VoidPtr>
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +0000281class _LIBCPP_TEMPLATE_VIS __list_iterator
Howard Hinnant3e519522010-05-11 19:42:16 +0000282{
Eric Fiselierb88ea352015-12-30 20:57:59 +0000283 typedef __list_node_pointer_traits<_Tp, _VoidPtr> _NodeTraits;
284 typedef typename _NodeTraits::__link_pointer __link_pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000285
Eric Fiselierb88ea352015-12-30 20:57:59 +0000286 __link_pointer __ptr_;
Howard Hinnant3e519522010-05-11 19:42:16 +0000287
Howard Hinnant920b56c2011-09-27 23:55:03 +0000288#if _LIBCPP_DEBUG_LEVEL >= 2
289 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierb88ea352015-12-30 20:57:59 +0000290 explicit __list_iterator(__link_pointer __p, const void* __c) _NOEXCEPT
Howard Hinnant920b56c2011-09-27 23:55:03 +0000291 : __ptr_(__p)
292 {
293 __get_db()->__insert_ic(this, __c);
294 }
295#else
Howard Hinnant848a5372010-09-22 16:48:34 +0000296 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierb88ea352015-12-30 20:57:59 +0000297 explicit __list_iterator(__link_pointer __p) _NOEXCEPT : __ptr_(__p) {}
Howard Hinnant920b56c2011-09-27 23:55:03 +0000298#endif
299
300
Howard Hinnant3e519522010-05-11 19:42:16 +0000301
302 template<class, class> friend class list;
303 template<class, class> friend class __list_imp;
304 template<class, class> friend class __list_const_iterator;
305public:
306 typedef bidirectional_iterator_tag iterator_category;
307 typedef _Tp value_type;
308 typedef value_type& reference;
Eric Fiselier1c813402015-08-23 02:56:05 +0000309 typedef typename __rebind_pointer<_VoidPtr, value_type>::type pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000310 typedef typename pointer_traits<pointer>::difference_type difference_type;
311
Howard Hinnant848a5372010-09-22 16:48:34 +0000312 _LIBCPP_INLINE_VISIBILITY
Marshall Clow0c37cfd2013-08-05 21:23:28 +0000313 __list_iterator() _NOEXCEPT : __ptr_(nullptr)
Howard Hinnant920b56c2011-09-27 23:55:03 +0000314 {
315#if _LIBCPP_DEBUG_LEVEL >= 2
316 __get_db()->__insert_i(this);
317#endif
318 }
319
320#if _LIBCPP_DEBUG_LEVEL >= 2
321
Howard Hinnant27745452011-01-28 23:46:28 +0000322 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000323 __list_iterator(const __list_iterator& __p)
324 : __ptr_(__p.__ptr_)
325 {
326 __get_db()->__iterator_copy(this, &__p);
327 }
328
329 _LIBCPP_INLINE_VISIBILITY
330 ~__list_iterator()
331 {
332 __get_db()->__erase_i(this);
333 }
334
335 _LIBCPP_INLINE_VISIBILITY
336 __list_iterator& operator=(const __list_iterator& __p)
337 {
338 if (this != &__p)
339 {
340 __get_db()->__iterator_copy(this, &__p);
341 __ptr_ = __p.__ptr_;
342 }
343 return *this;
344 }
345
346#endif // _LIBCPP_DEBUG_LEVEL >= 2
347
348 _LIBCPP_INLINE_VISIBILITY
349 reference operator*() const
350 {
351#if _LIBCPP_DEBUG_LEVEL >= 2
352 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
353 "Attempted to dereference a non-dereferenceable list::iterator");
354#endif
Eric Fiselierb88ea352015-12-30 20:57:59 +0000355 return __ptr_->__as_node()->__value_;
Howard Hinnant920b56c2011-09-27 23:55:03 +0000356 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000357 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000358 pointer operator->() const
359 {
360#if _LIBCPP_DEBUG_LEVEL >= 2
361 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
362 "Attempted to dereference a non-dereferenceable list::iterator");
363#endif
Eric Fiselierb88ea352015-12-30 20:57:59 +0000364 return pointer_traits<pointer>::pointer_to(__ptr_->__as_node()->__value_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000365 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000366
Howard Hinnant848a5372010-09-22 16:48:34 +0000367 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000368 __list_iterator& operator++()
369 {
370#if _LIBCPP_DEBUG_LEVEL >= 2
371 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
372 "Attempted to increment non-incrementable list::iterator");
373#endif
374 __ptr_ = __ptr_->__next_;
375 return *this;
376 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000377 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000378 __list_iterator operator++(int) {__list_iterator __t(*this); ++(*this); return __t;}
379
Howard Hinnant848a5372010-09-22 16:48:34 +0000380 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000381 __list_iterator& operator--()
382 {
383#if _LIBCPP_DEBUG_LEVEL >= 2
384 _LIBCPP_ASSERT(__get_const_db()->__decrementable(this),
385 "Attempted to decrement non-decrementable list::iterator");
386#endif
387 __ptr_ = __ptr_->__prev_;
388 return *this;
389 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000390 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000391 __list_iterator operator--(int) {__list_iterator __t(*this); --(*this); return __t;}
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 Hinnant920b56c2011-09-27 23:55:03 +0000395 {
Howard Hinnant920b56c2011-09-27 23:55:03 +0000396 return __x.__ptr_ == __y.__ptr_;
397 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000398 friend _LIBCPP_INLINE_VISIBILITY
399 bool operator!=(const __list_iterator& __x, const __list_iterator& __y)
Howard Hinnant3e519522010-05-11 19:42:16 +0000400 {return !(__x == __y);}
401};
402
403template <class _Tp, class _VoidPtr>
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +0000404class _LIBCPP_TEMPLATE_VIS __list_const_iterator
Howard Hinnant3e519522010-05-11 19:42:16 +0000405{
Eric Fiselierb88ea352015-12-30 20:57:59 +0000406 typedef __list_node_pointer_traits<_Tp, _VoidPtr> _NodeTraits;
407 typedef typename _NodeTraits::__link_pointer __link_pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000408
Eric Fiselierb88ea352015-12-30 20:57:59 +0000409 __link_pointer __ptr_;
Howard Hinnant3e519522010-05-11 19:42:16 +0000410
Howard Hinnant920b56c2011-09-27 23:55:03 +0000411#if _LIBCPP_DEBUG_LEVEL >= 2
412 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierb88ea352015-12-30 20:57:59 +0000413 explicit __list_const_iterator(__link_pointer __p, const void* __c) _NOEXCEPT
Howard Hinnant920b56c2011-09-27 23:55:03 +0000414 : __ptr_(__p)
415 {
416 __get_db()->__insert_ic(this, __c);
417 }
418#else
Howard Hinnant848a5372010-09-22 16:48:34 +0000419 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierb88ea352015-12-30 20:57:59 +0000420 explicit __list_const_iterator(__link_pointer __p) _NOEXCEPT : __ptr_(__p) {}
Howard Hinnant920b56c2011-09-27 23:55:03 +0000421#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000422
423 template<class, class> friend class list;
424 template<class, class> friend class __list_imp;
425public:
426 typedef bidirectional_iterator_tag iterator_category;
427 typedef _Tp value_type;
428 typedef const value_type& reference;
Eric Fiselier1c813402015-08-23 02:56:05 +0000429 typedef typename __rebind_pointer<_VoidPtr, const value_type>::type pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000430 typedef typename pointer_traits<pointer>::difference_type difference_type;
431
Howard Hinnant848a5372010-09-22 16:48:34 +0000432 _LIBCPP_INLINE_VISIBILITY
Marshall Clow0c37cfd2013-08-05 21:23:28 +0000433 __list_const_iterator() _NOEXCEPT : __ptr_(nullptr)
Howard Hinnant920b56c2011-09-27 23:55:03 +0000434 {
435#if _LIBCPP_DEBUG_LEVEL >= 2
436 __get_db()->__insert_i(this);
437#endif
438 }
Howard Hinnant27745452011-01-28 23:46:28 +0000439 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf7509232013-04-05 17:58:52 +0000440 __list_const_iterator(const __list_iterator<_Tp, _VoidPtr>& __p) _NOEXCEPT
Howard Hinnant920b56c2011-09-27 23:55:03 +0000441 : __ptr_(__p.__ptr_)
442 {
443#if _LIBCPP_DEBUG_LEVEL >= 2
444 __get_db()->__iterator_copy(this, &__p);
445#endif
446 }
447
448#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant3e519522010-05-11 19:42:16 +0000449
Howard Hinnant848a5372010-09-22 16:48:34 +0000450 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000451 __list_const_iterator(const __list_const_iterator& __p)
452 : __ptr_(__p.__ptr_)
453 {
454 __get_db()->__iterator_copy(this, &__p);
455 }
456
457 _LIBCPP_INLINE_VISIBILITY
458 ~__list_const_iterator()
459 {
460 __get_db()->__erase_i(this);
461 }
462
463 _LIBCPP_INLINE_VISIBILITY
464 __list_const_iterator& operator=(const __list_const_iterator& __p)
465 {
466 if (this != &__p)
467 {
468 __get_db()->__iterator_copy(this, &__p);
469 __ptr_ = __p.__ptr_;
470 }
471 return *this;
472 }
473
474#endif // _LIBCPP_DEBUG_LEVEL >= 2
475 _LIBCPP_INLINE_VISIBILITY
476 reference operator*() const
477 {
478#if _LIBCPP_DEBUG_LEVEL >= 2
479 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
480 "Attempted to dereference a non-dereferenceable list::const_iterator");
481#endif
Eric Fiselierb88ea352015-12-30 20:57:59 +0000482 return __ptr_->__as_node()->__value_;
Howard Hinnant920b56c2011-09-27 23:55:03 +0000483 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000484 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000485 pointer operator->() const
486 {
487#if _LIBCPP_DEBUG_LEVEL >= 2
488 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
Marshall Clowed9010a2017-10-23 16:46:44 +0000489 "Attempted to dereference a non-dereferenceable list::const_iterator");
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000490#endif
Eric Fiselierb88ea352015-12-30 20:57:59 +0000491 return pointer_traits<pointer>::pointer_to(__ptr_->__as_node()->__value_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000492 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000493
Howard Hinnant848a5372010-09-22 16:48:34 +0000494 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000495 __list_const_iterator& operator++()
496 {
497#if _LIBCPP_DEBUG_LEVEL >= 2
498 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
499 "Attempted to increment non-incrementable list::const_iterator");
500#endif
501 __ptr_ = __ptr_->__next_;
502 return *this;
503 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000504 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000505 __list_const_iterator operator++(int) {__list_const_iterator __t(*this); ++(*this); return __t;}
506
Howard Hinnant848a5372010-09-22 16:48:34 +0000507 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000508 __list_const_iterator& operator--()
509 {
510#if _LIBCPP_DEBUG_LEVEL >= 2
511 _LIBCPP_ASSERT(__get_const_db()->__decrementable(this),
512 "Attempted to decrement non-decrementable list::const_iterator");
513#endif
514 __ptr_ = __ptr_->__prev_;
515 return *this;
516 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000517 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000518 __list_const_iterator operator--(int) {__list_const_iterator __t(*this); --(*this); return __t;}
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 Hinnant920b56c2011-09-27 23:55:03 +0000522 {
Howard Hinnant920b56c2011-09-27 23:55:03 +0000523 return __x.__ptr_ == __y.__ptr_;
524 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000525 friend _LIBCPP_INLINE_VISIBILITY
526 bool operator!=(const __list_const_iterator& __x, const __list_const_iterator& __y)
Howard Hinnant3e519522010-05-11 19:42:16 +0000527 {return !(__x == __y);}
528};
529
530template <class _Tp, class _Alloc>
531class __list_imp
532{
533 __list_imp(const __list_imp&);
534 __list_imp& operator=(const __list_imp&);
Marshall Clow4a227e52018-05-20 14:05:31 +0000535public:
Howard Hinnant3e519522010-05-11 19:42:16 +0000536 typedef _Alloc allocator_type;
537 typedef allocator_traits<allocator_type> __alloc_traits;
538 typedef typename __alloc_traits::size_type size_type;
Marshall Clow4a227e52018-05-20 14:05:31 +0000539protected:
540 typedef _Tp value_type;
Howard Hinnant3e519522010-05-11 19:42:16 +0000541 typedef typename __alloc_traits::void_pointer __void_pointer;
542 typedef __list_iterator<value_type, __void_pointer> iterator;
543 typedef __list_const_iterator<value_type, __void_pointer> const_iterator;
544 typedef __list_node_base<value_type, __void_pointer> __node_base;
545 typedef __list_node<value_type, __void_pointer> __node;
Marshall Clow1f508012015-04-07 05:21:38 +0000546 typedef typename __rebind_alloc_helper<__alloc_traits, __node>::type __node_allocator;
Howard Hinnant3e519522010-05-11 19:42:16 +0000547 typedef allocator_traits<__node_allocator> __node_alloc_traits;
548 typedef typename __node_alloc_traits::pointer __node_pointer;
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000549 typedef typename __node_alloc_traits::pointer __node_const_pointer;
Eric Fiselier5243e192016-01-04 03:27:52 +0000550 typedef __list_node_pointer_traits<value_type, __void_pointer> __node_pointer_traits;
551 typedef typename __node_pointer_traits::__link_pointer __link_pointer;
Eric Fiselierb88ea352015-12-30 20:57:59 +0000552 typedef __link_pointer __link_const_pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000553 typedef typename __alloc_traits::pointer pointer;
554 typedef typename __alloc_traits::const_pointer const_pointer;
555 typedef typename __alloc_traits::difference_type difference_type;
556
Marshall Clow1f508012015-04-07 05:21:38 +0000557 typedef typename __rebind_alloc_helper<__alloc_traits, __node_base>::type __node_base_allocator;
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000558 typedef typename allocator_traits<__node_base_allocator>::pointer __node_base_pointer;
Eric Fiselier8cef7fd2018-06-05 22:32:52 +0000559 static_assert((!is_same<allocator_type, __node_allocator>::value),
560 "internal allocator type must differ from user-specified "
561 "type; otherwise overload resolution breaks");
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000562
Howard Hinnant3e519522010-05-11 19:42:16 +0000563 __node_base __end_;
564 __compressed_pair<size_type, __node_allocator> __size_alloc_;
565
Howard Hinnant848a5372010-09-22 16:48:34 +0000566 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5243e192016-01-04 03:27:52 +0000567 __link_pointer __end_as_link() const _NOEXCEPT {
568 return __node_pointer_traits::__unsafe_link_pointer_cast(
569 const_cast<__node_base&>(__end_).__self());
570 }
571
572 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000573 size_type& __sz() _NOEXCEPT {return __size_alloc_.first();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000574 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000575 const size_type& __sz() const _NOEXCEPT
576 {return __size_alloc_.first();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000577 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000578 __node_allocator& __node_alloc() _NOEXCEPT
579 {return __size_alloc_.second();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000580 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000581 const __node_allocator& __node_alloc() const _NOEXCEPT
582 {return __size_alloc_.second();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000583
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +0000584 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier55b31b4e2016-11-23 01:18:56 +0000585 size_type __node_alloc_max_size() const _NOEXCEPT {
586 return __node_alloc_traits::max_size(__node_alloc());
587 }
588 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierb88ea352015-12-30 20:57:59 +0000589 static void __unlink_nodes(__link_pointer __f, __link_pointer __l) _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +0000590
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +0000591 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000592 __list_imp()
593 _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value);
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +0000594 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000595 __list_imp(const allocator_type& __a);
Eric Fiselier8cef7fd2018-06-05 22:32:52 +0000596 _LIBCPP_INLINE_VISIBILITY
597 __list_imp(const __node_allocator& __a);
598#ifndef _LIBCPP_CXX03_LANG
599 __list_imp(__node_allocator&& __a) _NOEXCEPT;
600#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000601 ~__list_imp();
Howard Hinnant45900102011-06-03 17:30:28 +0000602 void clear() _NOEXCEPT;
Howard Hinnant848a5372010-09-22 16:48:34 +0000603 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000604 bool empty() const _NOEXCEPT {return __sz() == 0;}
Howard Hinnant3e519522010-05-11 19:42:16 +0000605
Howard Hinnant848a5372010-09-22 16:48:34 +0000606 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000607 iterator begin() _NOEXCEPT
608 {
609#if _LIBCPP_DEBUG_LEVEL >= 2
610 return iterator(__end_.__next_, this);
611#else
612 return iterator(__end_.__next_);
613#endif
614 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000615 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000616 const_iterator begin() const _NOEXCEPT
Howard Hinnant920b56c2011-09-27 23:55:03 +0000617 {
618#if _LIBCPP_DEBUG_LEVEL >= 2
619 return const_iterator(__end_.__next_, this);
620#else
621 return const_iterator(__end_.__next_);
622#endif
623 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000624 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000625 iterator end() _NOEXCEPT
626 {
627#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselier5243e192016-01-04 03:27:52 +0000628 return iterator(__end_as_link(), this);
Howard Hinnant920b56c2011-09-27 23:55:03 +0000629#else
Eric Fiselier5243e192016-01-04 03:27:52 +0000630 return iterator(__end_as_link());
Howard Hinnant920b56c2011-09-27 23:55:03 +0000631#endif
632 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000633 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000634 const_iterator end() const _NOEXCEPT
Howard Hinnant920b56c2011-09-27 23:55:03 +0000635 {
636#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselier5243e192016-01-04 03:27:52 +0000637 return const_iterator(__end_as_link(), this);
Howard Hinnant920b56c2011-09-27 23:55:03 +0000638#else
Eric Fiselier5243e192016-01-04 03:27:52 +0000639 return const_iterator(__end_as_link());
Howard Hinnant920b56c2011-09-27 23:55:03 +0000640#endif
641 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000642
Howard Hinnant45900102011-06-03 17:30:28 +0000643 void swap(__list_imp& __c)
Marshall Clowe3fbe142015-07-13 20:04:56 +0000644#if _LIBCPP_STD_VER >= 14
Eric Fiselier2e519572016-12-28 06:06:09 +0000645 _NOEXCEPT_DEBUG;
Marshall Clowe3fbe142015-07-13 20:04:56 +0000646#else
Eric Fiselier2e519572016-12-28 06:06:09 +0000647 _NOEXCEPT_DEBUG_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clowe3fbe142015-07-13 20:04:56 +0000648 __is_nothrow_swappable<allocator_type>::value);
649#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000650
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)
653 {__copy_assign_alloc(__c, integral_constant<bool,
654 __node_alloc_traits::propagate_on_container_copy_assignment::value>());}
655
Howard Hinnant848a5372010-09-22 16:48:34 +0000656 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000657 void __move_assign_alloc(__list_imp& __c)
Howard Hinnant45900102011-06-03 17:30:28 +0000658 _NOEXCEPT_(
659 !__node_alloc_traits::propagate_on_container_move_assignment::value ||
660 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +0000661 {__move_assign_alloc(__c, integral_constant<bool,
662 __node_alloc_traits::propagate_on_container_move_assignment::value>());}
663
664private:
Howard Hinnant848a5372010-09-22 16:48:34 +0000665 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000666 void __copy_assign_alloc(const __list_imp& __c, true_type)
667 {
668 if (__node_alloc() != __c.__node_alloc())
669 clear();
670 __node_alloc() = __c.__node_alloc();
671 }
672
Howard Hinnant848a5372010-09-22 16:48:34 +0000673 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierfd838222016-12-23 23:37:52 +0000674 void __copy_assign_alloc(const __list_imp&, false_type)
Howard Hinnant3e519522010-05-11 19:42:16 +0000675 {}
676
Howard Hinnant848a5372010-09-22 16:48:34 +0000677 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant86681392011-09-02 20:42:31 +0000678 void __move_assign_alloc(__list_imp& __c, true_type)
Howard Hinnant45900102011-06-03 17:30:28 +0000679 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +0000680 {
Howard Hinnantce48a112011-06-30 21:18:19 +0000681 __node_alloc() = _VSTD::move(__c.__node_alloc());
Howard Hinnant3e519522010-05-11 19:42:16 +0000682 }
683
Howard Hinnant848a5372010-09-22 16:48:34 +0000684 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierfd838222016-12-23 23:37:52 +0000685 void __move_assign_alloc(__list_imp&, false_type)
Howard Hinnant45900102011-06-03 17:30:28 +0000686 _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000687 {}
Eric Fiselier2e519572016-12-28 06:06:09 +0000688
689 _LIBCPP_INLINE_VISIBILITY
690 void __invalidate_all_iterators() {
691#if _LIBCPP_DEBUG_LEVEL >= 2
692 __get_db()->__invalidate_all(this);
693#endif
694 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000695};
696
697// Unlink nodes [__f, __l]
698template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +0000699inline
Howard Hinnant3e519522010-05-11 19:42:16 +0000700void
Eric Fiselierb88ea352015-12-30 20:57:59 +0000701__list_imp<_Tp, _Alloc>::__unlink_nodes(__link_pointer __f, __link_pointer __l)
Howard Hinnant45900102011-06-03 17:30:28 +0000702 _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000703{
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000704 __f->__prev_->__next_ = __l->__next_;
705 __l->__next_->__prev_ = __f->__prev_;
Howard Hinnant3e519522010-05-11 19:42:16 +0000706}
707
708template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +0000709inline
Howard Hinnant3e519522010-05-11 19:42:16 +0000710__list_imp<_Tp, _Alloc>::__list_imp()
Howard Hinnant45900102011-06-03 17:30:28 +0000711 _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +0000712 : __size_alloc_(0)
713{
714}
715
716template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +0000717inline
Howard Hinnant3e519522010-05-11 19:42:16 +0000718__list_imp<_Tp, _Alloc>::__list_imp(const allocator_type& __a)
719 : __size_alloc_(0, __node_allocator(__a))
720{
721}
722
723template <class _Tp, class _Alloc>
Eric Fiselier8cef7fd2018-06-05 22:32:52 +0000724inline __list_imp<_Tp, _Alloc>::__list_imp(const __node_allocator& __a)
725 : __size_alloc_(0, __a) {}
726
727#ifndef _LIBCPP_CXX03_LANG
728template <class _Tp, class _Alloc>
729inline __list_imp<_Tp, _Alloc>::__list_imp(__node_allocator&& __a) _NOEXCEPT
730 : __size_alloc_(0, std::move(__a)) {}
731#endif
732
733template <class _Tp, class _Alloc>
734__list_imp<_Tp, _Alloc>::~__list_imp() {
735 clear();
Howard Hinnant920b56c2011-09-27 23:55:03 +0000736#if _LIBCPP_DEBUG_LEVEL >= 2
737 __get_db()->__erase_c(this);
738#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000739}
740
741template <class _Tp, class _Alloc>
742void
Howard Hinnant45900102011-06-03 17:30:28 +0000743__list_imp<_Tp, _Alloc>::clear() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000744{
745 if (!empty())
746 {
747 __node_allocator& __na = __node_alloc();
Eric Fiselierb88ea352015-12-30 20:57:59 +0000748 __link_pointer __f = __end_.__next_;
Eric Fiselier5243e192016-01-04 03:27:52 +0000749 __link_pointer __l = __end_as_link();
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000750 __unlink_nodes(__f, __l->__prev_);
Howard Hinnant3e519522010-05-11 19:42:16 +0000751 __sz() = 0;
752 while (__f != __l)
753 {
Eric Fiselierb88ea352015-12-30 20:57:59 +0000754 __node_pointer __np = __f->__as_node();
Howard Hinnant920b56c2011-09-27 23:55:03 +0000755 __f = __f->__next_;
Eric Fiselierb88ea352015-12-30 20:57:59 +0000756 __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
757 __node_alloc_traits::deallocate(__na, __np, 1);
Howard Hinnant3e519522010-05-11 19:42:16 +0000758 }
Eric Fiselier2e519572016-12-28 06:06:09 +0000759 __invalidate_all_iterators();
Howard Hinnant3e519522010-05-11 19:42:16 +0000760 }
761}
762
763template <class _Tp, class _Alloc>
764void
765__list_imp<_Tp, _Alloc>::swap(__list_imp& __c)
Marshall Clowe3fbe142015-07-13 20:04:56 +0000766#if _LIBCPP_STD_VER >= 14
Eric Fiselier2e519572016-12-28 06:06:09 +0000767 _NOEXCEPT_DEBUG
Marshall Clowe3fbe142015-07-13 20:04:56 +0000768#else
Eric Fiselier2e519572016-12-28 06:06:09 +0000769 _NOEXCEPT_DEBUG_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clowe3fbe142015-07-13 20:04:56 +0000770 __is_nothrow_swappable<allocator_type>::value)
771#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000772{
Howard Hinnant920b56c2011-09-27 23:55:03 +0000773 _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
774 this->__node_alloc() == __c.__node_alloc(),
775 "list::swap: Either propagate_on_container_swap must be true"
776 " or the allocators must compare equal");
Howard Hinnantce48a112011-06-30 21:18:19 +0000777 using _VSTD::swap;
Marshall Clowe3fbe142015-07-13 20:04:56 +0000778 __swap_allocator(__node_alloc(), __c.__node_alloc());
Howard Hinnant3e519522010-05-11 19:42:16 +0000779 swap(__sz(), __c.__sz());
780 swap(__end_, __c.__end_);
781 if (__sz() == 0)
Eric Fiselier5243e192016-01-04 03:27:52 +0000782 __end_.__next_ = __end_.__prev_ = __end_as_link();
Howard Hinnant3e519522010-05-11 19:42:16 +0000783 else
Eric Fiselier5243e192016-01-04 03:27:52 +0000784 __end_.__prev_->__next_ = __end_.__next_->__prev_ = __end_as_link();
Howard Hinnant3e519522010-05-11 19:42:16 +0000785 if (__c.__sz() == 0)
Eric Fiselier5243e192016-01-04 03:27:52 +0000786 __c.__end_.__next_ = __c.__end_.__prev_ = __c.__end_as_link();
Howard Hinnant3e519522010-05-11 19:42:16 +0000787 else
Eric Fiselier5243e192016-01-04 03:27:52 +0000788 __c.__end_.__prev_->__next_ = __c.__end_.__next_->__prev_ = __c.__end_as_link();
Marshall Clow28d65da2014-08-05 01:34:12 +0000789
Howard Hinnant920b56c2011-09-27 23:55:03 +0000790#if _LIBCPP_DEBUG_LEVEL >= 2
791 __libcpp_db* __db = __get_db();
792 __c_node* __cn1 = __db->__find_c_and_lock(this);
793 __c_node* __cn2 = __db->__find_c(&__c);
794 std::swap(__cn1->beg_, __cn2->beg_);
795 std::swap(__cn1->end_, __cn2->end_);
796 std::swap(__cn1->cap_, __cn2->cap_);
797 for (__i_node** __p = __cn1->end_; __p != __cn1->beg_;)
798 {
799 --__p;
800 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
Eric Fiselier5243e192016-01-04 03:27:52 +0000801 if (__i->__ptr_ == __c.__end_as_link())
Howard Hinnant920b56c2011-09-27 23:55:03 +0000802 {
803 __cn2->__add(*__p);
804 if (--__cn1->end_ != __p)
805 memmove(__p, __p+1, (__cn1->end_ - __p)*sizeof(__i_node*));
806 }
807 else
808 (*__p)->__c_ = __cn1;
809 }
810 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
811 {
812 --__p;
813 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
Eric Fiselier5243e192016-01-04 03:27:52 +0000814 if (__i->__ptr_ == __end_as_link())
Howard Hinnant920b56c2011-09-27 23:55:03 +0000815 {
816 __cn1->__add(*__p);
817 if (--__cn2->end_ != __p)
818 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
819 }
820 else
821 (*__p)->__c_ = __cn2;
822 }
823 __db->unlock();
824#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000825}
826
Marshall Clowb5d34aa2015-02-18 17:24:08 +0000827template <class _Tp, class _Alloc /*= allocator<_Tp>*/>
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +0000828class _LIBCPP_TEMPLATE_VIS list
Howard Hinnant3e519522010-05-11 19:42:16 +0000829 : private __list_imp<_Tp, _Alloc>
830{
831 typedef __list_imp<_Tp, _Alloc> base;
832 typedef typename base::__node __node;
833 typedef typename base::__node_allocator __node_allocator;
834 typedef typename base::__node_pointer __node_pointer;
835 typedef typename base::__node_alloc_traits __node_alloc_traits;
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000836 typedef typename base::__node_base __node_base;
837 typedef typename base::__node_base_pointer __node_base_pointer;
Eric Fiselierb88ea352015-12-30 20:57:59 +0000838 typedef typename base::__link_pointer __link_pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000839
840public:
841 typedef _Tp value_type;
842 typedef _Alloc allocator_type;
843 static_assert((is_same<value_type, typename allocator_type::value_type>::value),
844 "Invalid allocator::value_type");
845 typedef value_type& reference;
846 typedef const value_type& const_reference;
847 typedef typename base::pointer pointer;
848 typedef typename base::const_pointer const_pointer;
849 typedef typename base::size_type size_type;
850 typedef typename base::difference_type difference_type;
851 typedef typename base::iterator iterator;
852 typedef typename base::const_iterator const_iterator;
Howard Hinnantce48a112011-06-30 21:18:19 +0000853 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
854 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnant3e519522010-05-11 19:42:16 +0000855
Howard Hinnant848a5372010-09-22 16:48:34 +0000856 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000857 list()
858 _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value)
Howard Hinnant920b56c2011-09-27 23:55:03 +0000859 {
860#if _LIBCPP_DEBUG_LEVEL >= 2
861 __get_db()->__insert_c(this);
862#endif
863 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000864 _LIBCPP_INLINE_VISIBILITY
Marshall Clowfb829762013-09-08 19:11:51 +0000865 explicit list(const allocator_type& __a) : base(__a)
Howard Hinnant920b56c2011-09-27 23:55:03 +0000866 {
867#if _LIBCPP_DEBUG_LEVEL >= 2
868 __get_db()->__insert_c(this);
869#endif
870 }
Marshall Clowfb829762013-09-08 19:11:51 +0000871 explicit list(size_type __n);
872#if _LIBCPP_STD_VER > 11
873 explicit list(size_type __n, const allocator_type& __a);
874#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000875 list(size_type __n, const value_type& __x);
876 list(size_type __n, const value_type& __x, const allocator_type& __a);
877 template <class _InpIter>
878 list(_InpIter __f, _InpIter __l,
879 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
880 template <class _InpIter>
881 list(_InpIter __f, _InpIter __l, const allocator_type& __a,
882 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
883
884 list(const list& __c);
885 list(const list& __c, const allocator_type& __a);
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +0000886 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000887 list& operator=(const list& __c);
Eric Fiseliercf9ed002017-04-16 03:45:35 +0000888#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +0000889 list(initializer_list<value_type> __il);
890 list(initializer_list<value_type> __il, const allocator_type& __a);
Eric Fiseliercf9ed002017-04-16 03:45:35 +0000891
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +0000892 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000893 list(list&& __c)
894 _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value);
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +0000895 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000896 list(list&& __c, const allocator_type& __a);
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +0000897 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000898 list& operator=(list&& __c)
899 _NOEXCEPT_(
900 __node_alloc_traits::propagate_on_container_move_assignment::value &&
901 is_nothrow_move_assignable<__node_allocator>::value);
Eric Fiseliercf9ed002017-04-16 03:45:35 +0000902
Howard Hinnant848a5372010-09-22 16:48:34 +0000903 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000904 list& operator=(initializer_list<value_type> __il)
905 {assign(__il.begin(), __il.end()); return *this;}
Eric Fiseliercf9ed002017-04-16 03:45:35 +0000906
907 _LIBCPP_INLINE_VISIBILITY
908 void assign(initializer_list<value_type> __il)
909 {assign(__il.begin(), __il.end());}
910#endif // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +0000911
912 template <class _InpIter>
913 void assign(_InpIter __f, _InpIter __l,
914 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
915 void assign(size_type __n, const value_type& __x);
Howard Hinnant3e519522010-05-11 19:42:16 +0000916
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +0000917 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000918 allocator_type get_allocator() const _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +0000919
Howard Hinnant848a5372010-09-22 16:48:34 +0000920 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000921 size_type size() const _NOEXCEPT {return base::__sz();}
Marshall Clow72c8fad2017-11-15 05:51:26 +0000922 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000923 bool empty() const _NOEXCEPT {return base::empty();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000924 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000925 size_type max_size() const _NOEXCEPT
Eric Fiselier55b31b4e2016-11-23 01:18:56 +0000926 {
927 return std::min<size_type>(
928 base::__node_alloc_max_size(),
929 numeric_limits<difference_type >::max());
930 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000931
Howard Hinnant848a5372010-09-22 16:48:34 +0000932 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000933 iterator begin() _NOEXCEPT {return base::begin();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000934 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000935 const_iterator begin() const _NOEXCEPT {return base::begin();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000936 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000937 iterator end() _NOEXCEPT {return base::end();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000938 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000939 const_iterator end() const _NOEXCEPT {return base::end();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000940 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000941 const_iterator cbegin() const _NOEXCEPT {return base::begin();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000942 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000943 const_iterator cend() const _NOEXCEPT {return base::end();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000944
Howard Hinnant848a5372010-09-22 16:48:34 +0000945 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000946 reverse_iterator rbegin() _NOEXCEPT
947 {return reverse_iterator(end());}
Howard Hinnant848a5372010-09-22 16:48:34 +0000948 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000949 const_reverse_iterator rbegin() const _NOEXCEPT
950 {return const_reverse_iterator(end());}
Howard Hinnant848a5372010-09-22 16:48:34 +0000951 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000952 reverse_iterator rend() _NOEXCEPT
953 {return reverse_iterator(begin());}
Howard Hinnant848a5372010-09-22 16:48:34 +0000954 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000955 const_reverse_iterator rend() const _NOEXCEPT
956 {return const_reverse_iterator(begin());}
Howard Hinnant848a5372010-09-22 16:48:34 +0000957 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000958 const_reverse_iterator crbegin() const _NOEXCEPT
959 {return const_reverse_iterator(end());}
Howard Hinnant848a5372010-09-22 16:48:34 +0000960 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000961 const_reverse_iterator crend() const _NOEXCEPT
962 {return const_reverse_iterator(begin());}
Howard Hinnant3e519522010-05-11 19:42:16 +0000963
Howard Hinnant848a5372010-09-22 16:48:34 +0000964 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000965 reference front()
966 {
967 _LIBCPP_ASSERT(!empty(), "list::front called on empty list");
Eric Fiselierb88ea352015-12-30 20:57:59 +0000968 return base::__end_.__next_->__as_node()->__value_;
Howard Hinnant920b56c2011-09-27 23:55:03 +0000969 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000970 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000971 const_reference front() const
972 {
973 _LIBCPP_ASSERT(!empty(), "list::front called on empty list");
Eric Fiselierb88ea352015-12-30 20:57:59 +0000974 return base::__end_.__next_->__as_node()->__value_;
Howard Hinnant920b56c2011-09-27 23:55:03 +0000975 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000976 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000977 reference back()
978 {
979 _LIBCPP_ASSERT(!empty(), "list::back called on empty list");
Eric Fiselierb88ea352015-12-30 20:57:59 +0000980 return base::__end_.__prev_->__as_node()->__value_;
Howard Hinnant920b56c2011-09-27 23:55:03 +0000981 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000982 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000983 const_reference back() const
984 {
985 _LIBCPP_ASSERT(!empty(), "list::back called on empty list");
Eric Fiselierb88ea352015-12-30 20:57:59 +0000986 return base::__end_.__prev_->__as_node()->__value_;
Howard Hinnant920b56c2011-09-27 23:55:03 +0000987 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000988
Eric Fiseliercf9ed002017-04-16 03:45:35 +0000989#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +0000990 void push_front(value_type&& __x);
991 void push_back(value_type&& __x);
Eric Fiseliercf9ed002017-04-16 03:45:35 +0000992
Howard Hinnant3e519522010-05-11 19:42:16 +0000993 template <class... _Args>
Marshall Clow63b560b2017-01-24 23:09:12 +0000994#if _LIBCPP_STD_VER > 14
Eric Fiselier0e411642016-07-21 03:20:17 +0000995 reference emplace_front(_Args&&... __args);
Marshall Clow63b560b2017-01-24 23:09:12 +0000996#else
997 void emplace_front(_Args&&... __args);
998#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000999 template <class... _Args>
Marshall Clow63b560b2017-01-24 23:09:12 +00001000#if _LIBCPP_STD_VER > 14
Eric Fiselier0e411642016-07-21 03:20:17 +00001001 reference emplace_back(_Args&&... __args);
Marshall Clow63b560b2017-01-24 23:09:12 +00001002#else
1003 void emplace_back(_Args&&... __args);
1004#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001005 template <class... _Args>
1006 iterator emplace(const_iterator __p, _Args&&... __args);
Eric Fiseliercf9ed002017-04-16 03:45:35 +00001007
Howard Hinnant3e519522010-05-11 19:42:16 +00001008 iterator insert(const_iterator __p, value_type&& __x);
Eric Fiseliercf9ed002017-04-16 03:45:35 +00001009
1010 _LIBCPP_INLINE_VISIBILITY
1011 iterator insert(const_iterator __p, initializer_list<value_type> __il)
1012 {return insert(__p, __il.begin(), __il.end());}
1013#endif // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001014
1015 void push_front(const value_type& __x);
1016 void push_back(const value_type& __x);
1017
Eric Fiselier1c0cedc2017-10-17 13:03:17 +00001018#ifndef _LIBCPP_CXX03_LANG
1019 template <class _Arg>
1020 _LIBCPP_INLINE_VISIBILITY
1021 void __emplace_back(_Arg&& __arg) { emplace_back(_VSTD::forward<_Arg>(__arg)); }
1022#else
1023 _LIBCPP_INLINE_VISIBILITY
1024 void __emplace_back(value_type const& __arg) { push_back(__arg); }
1025#endif
1026
Howard Hinnant3e519522010-05-11 19:42:16 +00001027 iterator insert(const_iterator __p, const value_type& __x);
1028 iterator insert(const_iterator __p, size_type __n, const value_type& __x);
1029 template <class _InpIter>
1030 iterator insert(const_iterator __p, _InpIter __f, _InpIter __l,
1031 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
Howard Hinnant3e519522010-05-11 19:42:16 +00001032
Howard Hinnant848a5372010-09-22 16:48:34 +00001033 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +00001034 void swap(list& __c)
Marshall Clowe3fbe142015-07-13 20:04:56 +00001035#if _LIBCPP_STD_VER >= 14
Eric Fiselier2e519572016-12-28 06:06:09 +00001036 _NOEXCEPT_DEBUG
Marshall Clowe3fbe142015-07-13 20:04:56 +00001037#else
Eric Fiselier2e519572016-12-28 06:06:09 +00001038 _NOEXCEPT_DEBUG_(!__node_alloc_traits::propagate_on_container_swap::value ||
Howard Hinnant45900102011-06-03 17:30:28 +00001039 __is_nothrow_swappable<__node_allocator>::value)
Marshall Clowe3fbe142015-07-13 20:04:56 +00001040#endif
Howard Hinnant45900102011-06-03 17:30:28 +00001041 {base::swap(__c);}
Howard Hinnant848a5372010-09-22 16:48:34 +00001042 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +00001043 void clear() _NOEXCEPT {base::clear();}
Howard Hinnant3e519522010-05-11 19:42:16 +00001044
1045 void pop_front();
1046 void pop_back();
1047
1048 iterator erase(const_iterator __p);
1049 iterator erase(const_iterator __f, const_iterator __l);
1050
1051 void resize(size_type __n);
1052 void resize(size_type __n, const value_type& __x);
1053
1054 void splice(const_iterator __p, list& __c);
Eric Fiseliercf9ed002017-04-16 03:45:35 +00001055#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant848a5372010-09-22 16:48:34 +00001056 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001057 void splice(const_iterator __p, list&& __c) {splice(__p, __c);}
Howard Hinnant848a5372010-09-22 16:48:34 +00001058 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001059 void splice(const_iterator __p, list&& __c, const_iterator __i)
1060 {splice(__p, __c, __i);}
Howard Hinnant848a5372010-09-22 16:48:34 +00001061 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001062 void splice(const_iterator __p, list&& __c, const_iterator __f, const_iterator __l)
1063 {splice(__p, __c, __f, __l);}
Eric Fiseliercf9ed002017-04-16 03:45:35 +00001064#endif
1065 void splice(const_iterator __p, list& __c, const_iterator __i);
1066 void splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l);
Howard Hinnant3e519522010-05-11 19:42:16 +00001067
1068 void remove(const value_type& __x);
1069 template <class _Pred> void remove_if(_Pred __pred);
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001070 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001071 void unique();
1072 template <class _BinaryPred>
1073 void unique(_BinaryPred __binary_pred);
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001074 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001075 void merge(list& __c);
Eric Fiseliercf9ed002017-04-16 03:45:35 +00001076#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant848a5372010-09-22 16:48:34 +00001077 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001078 void merge(list&& __c) {merge(__c);}
Eric Fiseliercf9ed002017-04-16 03:45:35 +00001079
Howard Hinnant3e519522010-05-11 19:42:16 +00001080 template <class _Comp>
Howard Hinnant848a5372010-09-22 16:48:34 +00001081 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001082 void merge(list&& __c, _Comp __comp) {merge(__c, __comp);}
Eric Fiseliercf9ed002017-04-16 03:45:35 +00001083#endif
1084 template <class _Comp>
1085 void merge(list& __c, _Comp __comp);
1086
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001087 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001088 void sort();
1089 template <class _Comp>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001090 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001091 void sort(_Comp __comp);
1092
Howard Hinnant45900102011-06-03 17:30:28 +00001093 void reverse() _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +00001094
Howard Hinnant920b56c2011-09-27 23:55:03 +00001095 bool __invariants() const;
1096
Eric Fiselier0a412f42017-10-17 19:12:23 +00001097 typedef __allocator_destructor<__node_allocator> __node_destructor;
1098 typedef unique_ptr<__node, __node_destructor> __hold_pointer;
1099
1100 _LIBCPP_INLINE_VISIBILITY
1101 __hold_pointer __allocate_node(__node_allocator& __na) {
1102 __node_pointer __p = __node_alloc_traits::allocate(__na, 1);
1103 __p->__prev_ = nullptr;
1104 return __hold_pointer(__p, __node_destructor(__na, 1));
1105 }
1106
Howard Hinnant920b56c2011-09-27 23:55:03 +00001107#if _LIBCPP_DEBUG_LEVEL >= 2
1108
1109 bool __dereferenceable(const const_iterator* __i) const;
1110 bool __decrementable(const const_iterator* __i) const;
1111 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1112 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1113
1114#endif // _LIBCPP_DEBUG_LEVEL >= 2
1115
Howard Hinnant3e519522010-05-11 19:42:16 +00001116private:
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001117 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierb88ea352015-12-30 20:57:59 +00001118 static void __link_nodes (__link_pointer __p, __link_pointer __f, __link_pointer __l);
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001119 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierb88ea352015-12-30 20:57:59 +00001120 void __link_nodes_at_front(__link_pointer __f, __link_pointer __l);
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001121 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierb88ea352015-12-30 20:57:59 +00001122 void __link_nodes_at_back (__link_pointer __f, __link_pointer __l);
Howard Hinnant3e519522010-05-11 19:42:16 +00001123 iterator __iterator(size_type __n);
1124 template <class _Comp>
1125 static iterator __sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp);
1126
Howard Hinnant45900102011-06-03 17:30:28 +00001127 void __move_assign(list& __c, true_type)
1128 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value);
Howard Hinnant3e519522010-05-11 19:42:16 +00001129 void __move_assign(list& __c, false_type);
1130};
1131
Marshall Clow4a227e52018-05-20 14:05:31 +00001132#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
1133template<class _InputIterator,
1134 class _Alloc = typename std::allocator<typename iterator_traits<_InputIterator>::value_type>,
1135 class = typename enable_if<__is_allocator<_Alloc>::value, void>::type
1136 >
1137list(_InputIterator, _InputIterator)
1138 -> list<typename iterator_traits<_InputIterator>::value_type, _Alloc>;
1139
1140template<class _InputIterator,
1141 class _Alloc,
1142 class = typename enable_if<__is_allocator<_Alloc>::value, void>::type
1143 >
1144list(_InputIterator, _InputIterator, _Alloc)
1145 -> list<typename iterator_traits<_InputIterator>::value_type, _Alloc>;
1146#endif
1147
Howard Hinnant3e519522010-05-11 19:42:16 +00001148// Link in nodes [__f, __l] just prior to __p
1149template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001150inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001151void
Eric Fiselierb88ea352015-12-30 20:57:59 +00001152list<_Tp, _Alloc>::__link_nodes(__link_pointer __p, __link_pointer __f, __link_pointer __l)
Howard Hinnant3e519522010-05-11 19:42:16 +00001153{
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001154 __p->__prev_->__next_ = __f;
1155 __f->__prev_ = __p->__prev_;
1156 __p->__prev_ = __l;
1157 __l->__next_ = __p;
Howard Hinnant3e519522010-05-11 19:42:16 +00001158}
1159
Marshall Clow28d65da2014-08-05 01:34:12 +00001160// Link in nodes [__f, __l] at the front of the list
1161template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001162inline
Marshall Clow28d65da2014-08-05 01:34:12 +00001163void
Eric Fiselierb88ea352015-12-30 20:57:59 +00001164list<_Tp, _Alloc>::__link_nodes_at_front(__link_pointer __f, __link_pointer __l)
Marshall Clow28d65da2014-08-05 01:34:12 +00001165{
Eric Fiselier5243e192016-01-04 03:27:52 +00001166 __f->__prev_ = base::__end_as_link();
Marshall Clowced70062014-08-08 15:35:52 +00001167 __l->__next_ = base::__end_.__next_;
1168 __l->__next_->__prev_ = __l;
1169 base::__end_.__next_ = __f;
Marshall Clow28d65da2014-08-05 01:34:12 +00001170}
1171
1172// Link in nodes [__f, __l] at the front of the list
1173template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001174inline
Marshall Clow28d65da2014-08-05 01:34:12 +00001175void
Eric Fiselierb88ea352015-12-30 20:57:59 +00001176list<_Tp, _Alloc>::__link_nodes_at_back(__link_pointer __f, __link_pointer __l)
Marshall Clow28d65da2014-08-05 01:34:12 +00001177{
Eric Fiselier5243e192016-01-04 03:27:52 +00001178 __l->__next_ = base::__end_as_link();
Marshall Clowced70062014-08-08 15:35:52 +00001179 __f->__prev_ = base::__end_.__prev_;
1180 __f->__prev_->__next_ = __f;
1181 base::__end_.__prev_ = __l;
Marshall Clow28d65da2014-08-05 01:34:12 +00001182}
1183
1184
Howard Hinnant3e519522010-05-11 19:42:16 +00001185template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001186inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001187typename list<_Tp, _Alloc>::iterator
1188list<_Tp, _Alloc>::__iterator(size_type __n)
1189{
Howard Hinnantce48a112011-06-30 21:18:19 +00001190 return __n <= base::__sz() / 2 ? _VSTD::next(begin(), __n)
1191 : _VSTD::prev(end(), base::__sz() - __n);
Howard Hinnant3e519522010-05-11 19:42:16 +00001192}
1193
1194template <class _Tp, class _Alloc>
1195list<_Tp, _Alloc>::list(size_type __n)
1196{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001197#if _LIBCPP_DEBUG_LEVEL >= 2
1198 __get_db()->__insert_c(this);
1199#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001200 for (; __n > 0; --__n)
Eric Fiseliercf9ed002017-04-16 03:45:35 +00001201#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001202 emplace_back();
1203#else
1204 push_back(value_type());
1205#endif
1206}
1207
Marshall Clowfb829762013-09-08 19:11:51 +00001208#if _LIBCPP_STD_VER > 11
1209template <class _Tp, class _Alloc>
1210list<_Tp, _Alloc>::list(size_type __n, const allocator_type& __a) : base(__a)
1211{
1212#if _LIBCPP_DEBUG_LEVEL >= 2
1213 __get_db()->__insert_c(this);
1214#endif
1215 for (; __n > 0; --__n)
Marshall Clowfb829762013-09-08 19:11:51 +00001216 emplace_back();
Marshall Clowfb829762013-09-08 19:11:51 +00001217}
1218#endif
1219
Howard Hinnant3e519522010-05-11 19:42:16 +00001220template <class _Tp, class _Alloc>
1221list<_Tp, _Alloc>::list(size_type __n, const value_type& __x)
1222{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001223#if _LIBCPP_DEBUG_LEVEL >= 2
1224 __get_db()->__insert_c(this);
1225#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001226 for (; __n > 0; --__n)
1227 push_back(__x);
1228}
1229
1230template <class _Tp, class _Alloc>
1231list<_Tp, _Alloc>::list(size_type __n, const value_type& __x, const allocator_type& __a)
1232 : base(__a)
1233{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001234#if _LIBCPP_DEBUG_LEVEL >= 2
1235 __get_db()->__insert_c(this);
1236#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001237 for (; __n > 0; --__n)
1238 push_back(__x);
1239}
1240
1241template <class _Tp, class _Alloc>
1242template <class _InpIter>
1243list<_Tp, _Alloc>::list(_InpIter __f, _InpIter __l,
1244 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1245{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001246#if _LIBCPP_DEBUG_LEVEL >= 2
1247 __get_db()->__insert_c(this);
1248#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001249 for (; __f != __l; ++__f)
Eric Fiselier1c0cedc2017-10-17 13:03:17 +00001250 __emplace_back(*__f);
Howard Hinnant3e519522010-05-11 19:42:16 +00001251}
1252
1253template <class _Tp, class _Alloc>
1254template <class _InpIter>
1255list<_Tp, _Alloc>::list(_InpIter __f, _InpIter __l, const allocator_type& __a,
1256 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1257 : base(__a)
1258{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001259#if _LIBCPP_DEBUG_LEVEL >= 2
1260 __get_db()->__insert_c(this);
1261#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001262 for (; __f != __l; ++__f)
Eric Fiselier1c0cedc2017-10-17 13:03:17 +00001263 __emplace_back(*__f);
Howard Hinnant3e519522010-05-11 19:42:16 +00001264}
1265
1266template <class _Tp, class _Alloc>
1267list<_Tp, _Alloc>::list(const list& __c)
Eric Fiselier8cef7fd2018-06-05 22:32:52 +00001268 : base(__node_alloc_traits::select_on_container_copy_construction(
1269 __c.__node_alloc())) {
Howard Hinnant920b56c2011-09-27 23:55:03 +00001270#if _LIBCPP_DEBUG_LEVEL >= 2
1271 __get_db()->__insert_c(this);
1272#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001273 for (const_iterator __i = __c.begin(), __e = __c.end(); __i != __e; ++__i)
1274 push_back(*__i);
1275}
1276
1277template <class _Tp, class _Alloc>
1278list<_Tp, _Alloc>::list(const list& __c, const allocator_type& __a)
1279 : base(__a)
1280{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001281#if _LIBCPP_DEBUG_LEVEL >= 2
1282 __get_db()->__insert_c(this);
1283#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001284 for (const_iterator __i = __c.begin(), __e = __c.end(); __i != __e; ++__i)
1285 push_back(*__i);
1286}
1287
Eric Fiseliercf9ed002017-04-16 03:45:35 +00001288#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant54976f22011-08-12 21:56:02 +00001289
Howard Hinnant3e519522010-05-11 19:42:16 +00001290template <class _Tp, class _Alloc>
1291list<_Tp, _Alloc>::list(initializer_list<value_type> __il, const allocator_type& __a)
1292 : base(__a)
1293{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001294#if _LIBCPP_DEBUG_LEVEL >= 2
1295 __get_db()->__insert_c(this);
1296#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001297 for (typename initializer_list<value_type>::const_iterator __i = __il.begin(),
1298 __e = __il.end(); __i != __e; ++__i)
1299 push_back(*__i);
1300}
1301
1302template <class _Tp, class _Alloc>
1303list<_Tp, _Alloc>::list(initializer_list<value_type> __il)
1304{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001305#if _LIBCPP_DEBUG_LEVEL >= 2
1306 __get_db()->__insert_c(this);
1307#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001308 for (typename initializer_list<value_type>::const_iterator __i = __il.begin(),
1309 __e = __il.end(); __i != __e; ++__i)
1310 push_back(*__i);
1311}
1312
Howard Hinnant3e519522010-05-11 19:42:16 +00001313template <class _Tp, class _Alloc>
Eric Fiselier8cef7fd2018-06-05 22:32:52 +00001314inline list<_Tp, _Alloc>::list(list&& __c)
Howard Hinnant45900102011-06-03 17:30:28 +00001315 _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value)
Eric Fiselier8cef7fd2018-06-05 22:32:52 +00001316 : base(_VSTD::move(__c.__node_alloc())) {
Howard Hinnant920b56c2011-09-27 23:55:03 +00001317#if _LIBCPP_DEBUG_LEVEL >= 2
1318 __get_db()->__insert_c(this);
1319#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001320 splice(end(), __c);
1321}
1322
1323template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001324inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001325list<_Tp, _Alloc>::list(list&& __c, const allocator_type& __a)
1326 : base(__a)
1327{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001328#if _LIBCPP_DEBUG_LEVEL >= 2
1329 __get_db()->__insert_c(this);
1330#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001331 if (__a == __c.get_allocator())
1332 splice(end(), __c);
1333 else
1334 {
Howard Hinnantc003db12011-11-29 18:15:50 +00001335 typedef move_iterator<iterator> _Ip;
1336 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnant3e519522010-05-11 19:42:16 +00001337 }
1338}
1339
1340template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001341inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001342list<_Tp, _Alloc>&
1343list<_Tp, _Alloc>::operator=(list&& __c)
Howard Hinnant45900102011-06-03 17:30:28 +00001344 _NOEXCEPT_(
1345 __node_alloc_traits::propagate_on_container_move_assignment::value &&
1346 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +00001347{
1348 __move_assign(__c, integral_constant<bool,
1349 __node_alloc_traits::propagate_on_container_move_assignment::value>());
1350 return *this;
1351}
1352
1353template <class _Tp, class _Alloc>
1354void
1355list<_Tp, _Alloc>::__move_assign(list& __c, false_type)
1356{
1357 if (base::__node_alloc() != __c.__node_alloc())
1358 {
Howard Hinnantc003db12011-11-29 18:15:50 +00001359 typedef move_iterator<iterator> _Ip;
1360 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnant3e519522010-05-11 19:42:16 +00001361 }
1362 else
1363 __move_assign(__c, true_type());
1364}
1365
1366template <class _Tp, class _Alloc>
1367void
1368list<_Tp, _Alloc>::__move_assign(list& __c, true_type)
Howard Hinnant45900102011-06-03 17:30:28 +00001369 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +00001370{
1371 clear();
1372 base::__move_assign_alloc(__c);
1373 splice(end(), __c);
1374}
1375
Eric Fiseliercf9ed002017-04-16 03:45:35 +00001376#endif // _LIBCPP_CXX03_LANG
1377
1378template <class _Tp, class _Alloc>
1379inline
1380list<_Tp, _Alloc>&
1381list<_Tp, _Alloc>::operator=(const list& __c)
1382{
1383 if (this != &__c)
1384 {
1385 base::__copy_assign_alloc(__c);
1386 assign(__c.begin(), __c.end());
1387 }
1388 return *this;
1389}
Howard Hinnant3e519522010-05-11 19:42:16 +00001390
1391template <class _Tp, class _Alloc>
1392template <class _InpIter>
1393void
1394list<_Tp, _Alloc>::assign(_InpIter __f, _InpIter __l,
1395 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1396{
1397 iterator __i = begin();
1398 iterator __e = end();
1399 for (; __f != __l && __i != __e; ++__f, ++__i)
1400 *__i = *__f;
1401 if (__i == __e)
1402 insert(__e, __f, __l);
1403 else
1404 erase(__i, __e);
Eric Fiselier2e519572016-12-28 06:06:09 +00001405#if _LIBCPP_DEBUG_LEVEL >= 2
1406 __get_db()->__invalidate_all(this);
1407#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001408}
1409
1410template <class _Tp, class _Alloc>
1411void
1412list<_Tp, _Alloc>::assign(size_type __n, const value_type& __x)
1413{
1414 iterator __i = begin();
1415 iterator __e = end();
1416 for (; __n > 0 && __i != __e; --__n, ++__i)
1417 *__i = __x;
1418 if (__i == __e)
1419 insert(__e, __n, __x);
1420 else
1421 erase(__i, __e);
Eric Fiselier2e519572016-12-28 06:06:09 +00001422#if _LIBCPP_DEBUG_LEVEL >= 2
1423 __get_db()->__invalidate_all(this);
1424#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001425}
1426
1427template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001428inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001429_Alloc
Howard Hinnant45900102011-06-03 17:30:28 +00001430list<_Tp, _Alloc>::get_allocator() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001431{
1432 return allocator_type(base::__node_alloc());
1433}
1434
1435template <class _Tp, class _Alloc>
1436typename list<_Tp, _Alloc>::iterator
1437list<_Tp, _Alloc>::insert(const_iterator __p, const value_type& __x)
1438{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001439#if _LIBCPP_DEBUG_LEVEL >= 2
1440 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1441 "list::insert(iterator, x) called with an iterator not"
1442 " referring to this list");
1443#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001444 __node_allocator& __na = base::__node_alloc();
Eric Fiselier0a412f42017-10-17 19:12:23 +00001445 __hold_pointer __hold = __allocate_node(__na);
Howard Hinnantce48a112011-06-30 21:18:19 +00001446 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001447 __link_nodes(__p.__ptr_, __hold->__as_link(), __hold->__as_link());
Howard Hinnant3e519522010-05-11 19:42:16 +00001448 ++base::__sz();
Howard Hinnantb0e4c9d2013-04-05 00:18:49 +00001449#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselierb88ea352015-12-30 20:57:59 +00001450 return iterator(__hold.release()->__as_link(), this);
Howard Hinnantb0e4c9d2013-04-05 00:18:49 +00001451#else
Eric Fiselierb88ea352015-12-30 20:57:59 +00001452 return iterator(__hold.release()->__as_link());
Howard Hinnantb0e4c9d2013-04-05 00:18:49 +00001453#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001454}
1455
1456template <class _Tp, class _Alloc>
1457typename list<_Tp, _Alloc>::iterator
1458list<_Tp, _Alloc>::insert(const_iterator __p, size_type __n, const value_type& __x)
1459{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001460#if _LIBCPP_DEBUG_LEVEL >= 2
1461 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1462 "list::insert(iterator, n, x) called with an iterator not"
1463 " referring to this list");
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001464 iterator __r(__p.__ptr_, this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001465#else
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001466 iterator __r(__p.__ptr_);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001467#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001468 if (__n > 0)
1469 {
1470 size_type __ds = 0;
1471 __node_allocator& __na = base::__node_alloc();
Eric Fiselier0a412f42017-10-17 19:12:23 +00001472 __hold_pointer __hold = __allocate_node(__na);
Howard Hinnantce48a112011-06-30 21:18:19 +00001473 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnant3e519522010-05-11 19:42:16 +00001474 ++__ds;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001475#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselierb88ea352015-12-30 20:57:59 +00001476 __r = iterator(__hold->__as_link(), this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001477#else
Eric Fiselierb88ea352015-12-30 20:57:59 +00001478 __r = iterator(__hold->__as_link());
Howard Hinnant920b56c2011-09-27 23:55:03 +00001479#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001480 __hold.release();
1481 iterator __e = __r;
1482#ifndef _LIBCPP_NO_EXCEPTIONS
1483 try
1484 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001485#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001486 for (--__n; __n != 0; --__n, ++__e, ++__ds)
1487 {
1488 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001489 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001490 __e.__ptr_->__next_ = __hold->__as_link();
Howard Hinnant3e519522010-05-11 19:42:16 +00001491 __hold->__prev_ = __e.__ptr_;
1492 __hold.release();
1493 }
1494#ifndef _LIBCPP_NO_EXCEPTIONS
1495 }
1496 catch (...)
1497 {
1498 while (true)
1499 {
Howard Hinnantce48a112011-06-30 21:18:19 +00001500 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Eric Fiselierb88ea352015-12-30 20:57:59 +00001501 __link_pointer __prev = __e.__ptr_->__prev_;
1502 __node_alloc_traits::deallocate(__na, __e.__ptr_->__as_node(), 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001503 if (__prev == 0)
1504 break;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001505#if _LIBCPP_DEBUG_LEVEL >= 2
1506 __e = iterator(__prev, this);
1507#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001508 __e = iterator(__prev);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001509#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001510 }
1511 throw;
1512 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001513#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001514 __link_nodes(__p.__ptr_, __r.__ptr_, __e.__ptr_);
Howard Hinnant3e519522010-05-11 19:42:16 +00001515 base::__sz() += __ds;
1516 }
1517 return __r;
1518}
1519
1520template <class _Tp, class _Alloc>
1521template <class _InpIter>
1522typename list<_Tp, _Alloc>::iterator
1523list<_Tp, _Alloc>::insert(const_iterator __p, _InpIter __f, _InpIter __l,
1524 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1525{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001526#if _LIBCPP_DEBUG_LEVEL >= 2
1527 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1528 "list::insert(iterator, range) called with an iterator not"
1529 " referring to this list");
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001530 iterator __r(__p.__ptr_, this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001531#else
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001532 iterator __r(__p.__ptr_);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001533#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001534 if (__f != __l)
1535 {
1536 size_type __ds = 0;
1537 __node_allocator& __na = base::__node_alloc();
Eric Fiselier0a412f42017-10-17 19:12:23 +00001538 __hold_pointer __hold = __allocate_node(__na);
Howard Hinnantce48a112011-06-30 21:18:19 +00001539 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), *__f);
Howard Hinnant3e519522010-05-11 19:42:16 +00001540 ++__ds;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001541#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselierb88ea352015-12-30 20:57:59 +00001542 __r = iterator(__hold.get()->__as_link(), this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001543#else
Eric Fiselierb88ea352015-12-30 20:57:59 +00001544 __r = iterator(__hold.get()->__as_link());
Howard Hinnant920b56c2011-09-27 23:55:03 +00001545#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001546 __hold.release();
1547 iterator __e = __r;
1548#ifndef _LIBCPP_NO_EXCEPTIONS
1549 try
1550 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001551#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiselier61bff612015-03-19 03:20:02 +00001552 for (++__f; __f != __l; ++__f, (void) ++__e, (void) ++__ds)
Howard Hinnant3e519522010-05-11 19:42:16 +00001553 {
1554 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001555 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), *__f);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001556 __e.__ptr_->__next_ = __hold.get()->__as_link();
Howard Hinnant3e519522010-05-11 19:42:16 +00001557 __hold->__prev_ = __e.__ptr_;
1558 __hold.release();
1559 }
1560#ifndef _LIBCPP_NO_EXCEPTIONS
1561 }
1562 catch (...)
1563 {
1564 while (true)
1565 {
Howard Hinnantce48a112011-06-30 21:18:19 +00001566 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Eric Fiselierb88ea352015-12-30 20:57:59 +00001567 __link_pointer __prev = __e.__ptr_->__prev_;
1568 __node_alloc_traits::deallocate(__na, __e.__ptr_->__as_node(), 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001569 if (__prev == 0)
1570 break;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001571#if _LIBCPP_DEBUG_LEVEL >= 2
1572 __e = iterator(__prev, this);
1573#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001574 __e = iterator(__prev);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001575#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001576 }
1577 throw;
1578 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001579#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001580 __link_nodes(__p.__ptr_, __r.__ptr_, __e.__ptr_);
Howard Hinnant3e519522010-05-11 19:42:16 +00001581 base::__sz() += __ds;
1582 }
1583 return __r;
1584}
1585
1586template <class _Tp, class _Alloc>
1587void
1588list<_Tp, _Alloc>::push_front(const value_type& __x)
1589{
1590 __node_allocator& __na = base::__node_alloc();
Eric Fiselier0a412f42017-10-17 19:12:23 +00001591 __hold_pointer __hold = __allocate_node(__na);
Howard Hinnantce48a112011-06-30 21:18:19 +00001592 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001593 __link_pointer __nl = __hold->__as_link();
1594 __link_nodes_at_front(__nl, __nl);
Howard Hinnant3e519522010-05-11 19:42:16 +00001595 ++base::__sz();
1596 __hold.release();
1597}
1598
1599template <class _Tp, class _Alloc>
1600void
1601list<_Tp, _Alloc>::push_back(const value_type& __x)
1602{
1603 __node_allocator& __na = base::__node_alloc();
Eric Fiselier0a412f42017-10-17 19:12:23 +00001604 __hold_pointer __hold = __allocate_node(__na);
Howard Hinnantce48a112011-06-30 21:18:19 +00001605 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001606 __link_nodes_at_back(__hold.get()->__as_link(), __hold.get()->__as_link());
Howard Hinnant3e519522010-05-11 19:42:16 +00001607 ++base::__sz();
1608 __hold.release();
1609}
1610
Eric Fiseliercf9ed002017-04-16 03:45:35 +00001611#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001612
1613template <class _Tp, class _Alloc>
1614void
1615list<_Tp, _Alloc>::push_front(value_type&& __x)
1616{
1617 __node_allocator& __na = base::__node_alloc();
Eric Fiselier0a412f42017-10-17 19:12:23 +00001618 __hold_pointer __hold = __allocate_node(__na);
Howard Hinnantce48a112011-06-30 21:18:19 +00001619 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
Eric Fiselierb88ea352015-12-30 20:57:59 +00001620 __link_nodes_at_front(__hold.get()->__as_link(), __hold.get()->__as_link());
Howard Hinnant3e519522010-05-11 19:42:16 +00001621 ++base::__sz();
1622 __hold.release();
1623}
1624
1625template <class _Tp, class _Alloc>
1626void
1627list<_Tp, _Alloc>::push_back(value_type&& __x)
1628{
1629 __node_allocator& __na = base::__node_alloc();
Eric Fiselier0a412f42017-10-17 19:12:23 +00001630 __hold_pointer __hold = __allocate_node(__na);
Howard Hinnantce48a112011-06-30 21:18:19 +00001631 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
Eric Fiselierb88ea352015-12-30 20:57:59 +00001632 __link_nodes_at_back(__hold.get()->__as_link(), __hold.get()->__as_link());
Howard Hinnant3e519522010-05-11 19:42:16 +00001633 ++base::__sz();
1634 __hold.release();
1635}
1636
1637template <class _Tp, class _Alloc>
1638template <class... _Args>
Marshall Clow63b560b2017-01-24 23:09:12 +00001639#if _LIBCPP_STD_VER > 14
Eric Fiselier0e411642016-07-21 03:20:17 +00001640typename list<_Tp, _Alloc>::reference
Marshall Clow63b560b2017-01-24 23:09:12 +00001641#else
1642void
1643#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001644list<_Tp, _Alloc>::emplace_front(_Args&&... __args)
1645{
1646 __node_allocator& __na = base::__node_alloc();
Eric Fiselier0a412f42017-10-17 19:12:23 +00001647 __hold_pointer __hold = __allocate_node(__na);
Howard Hinnantce48a112011-06-30 21:18:19 +00001648 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001649 __link_nodes_at_front(__hold.get()->__as_link(), __hold.get()->__as_link());
Howard Hinnant3e519522010-05-11 19:42:16 +00001650 ++base::__sz();
Marshall Clow63b560b2017-01-24 23:09:12 +00001651#if _LIBCPP_STD_VER > 14
Eric Fiselier0e411642016-07-21 03:20:17 +00001652 return __hold.release()->__value_;
Marshall Clow63b560b2017-01-24 23:09:12 +00001653#else
1654 __hold.release();
1655#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001656}
1657
1658template <class _Tp, class _Alloc>
1659template <class... _Args>
Marshall Clow63b560b2017-01-24 23:09:12 +00001660#if _LIBCPP_STD_VER > 14
Eric Fiselier0e411642016-07-21 03:20:17 +00001661typename list<_Tp, _Alloc>::reference
Marshall Clow63b560b2017-01-24 23:09:12 +00001662#else
1663void
1664#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001665list<_Tp, _Alloc>::emplace_back(_Args&&... __args)
1666{
1667 __node_allocator& __na = base::__node_alloc();
Eric Fiselier0a412f42017-10-17 19:12:23 +00001668 __hold_pointer __hold = __allocate_node(__na);
Howard Hinnantce48a112011-06-30 21:18:19 +00001669 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001670 __link_pointer __nl = __hold->__as_link();
1671 __link_nodes_at_back(__nl, __nl);
Howard Hinnant3e519522010-05-11 19:42:16 +00001672 ++base::__sz();
Marshall Clow63b560b2017-01-24 23:09:12 +00001673#if _LIBCPP_STD_VER > 14
Eric Fiselier0e411642016-07-21 03:20:17 +00001674 return __hold.release()->__value_;
Marshall Clow63b560b2017-01-24 23:09:12 +00001675#else
1676 __hold.release();
1677#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001678}
1679
1680template <class _Tp, class _Alloc>
1681template <class... _Args>
1682typename list<_Tp, _Alloc>::iterator
1683list<_Tp, _Alloc>::emplace(const_iterator __p, _Args&&... __args)
1684{
Howard Hinnantb0e4c9d2013-04-05 00:18:49 +00001685#if _LIBCPP_DEBUG_LEVEL >= 2
1686 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1687 "list::emplace(iterator, args...) called with an iterator not"
1688 " referring to this list");
1689#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001690 __node_allocator& __na = base::__node_alloc();
Eric Fiselier0a412f42017-10-17 19:12:23 +00001691 __hold_pointer __hold = __allocate_node(__na);
Howard Hinnantce48a112011-06-30 21:18:19 +00001692 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001693 __link_pointer __nl = __hold.get()->__as_link();
1694 __link_nodes(__p.__ptr_, __nl, __nl);
Howard Hinnant3e519522010-05-11 19:42:16 +00001695 ++base::__sz();
Eric Fiselierb88ea352015-12-30 20:57:59 +00001696 __hold.release();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001697#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselierb88ea352015-12-30 20:57:59 +00001698 return iterator(__nl, this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001699#else
Eric Fiselierb88ea352015-12-30 20:57:59 +00001700 return iterator(__nl);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001701#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001702}
1703
1704template <class _Tp, class _Alloc>
1705typename list<_Tp, _Alloc>::iterator
1706list<_Tp, _Alloc>::insert(const_iterator __p, value_type&& __x)
1707{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001708#if _LIBCPP_DEBUG_LEVEL >= 2
1709 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1710 "list::insert(iterator, x) called with an iterator not"
1711 " referring to this list");
1712#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001713 __node_allocator& __na = base::__node_alloc();
Eric Fiselier0a412f42017-10-17 19:12:23 +00001714 __hold_pointer __hold = __allocate_node(__na);
Howard Hinnantce48a112011-06-30 21:18:19 +00001715 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
Eric Fiselierb88ea352015-12-30 20:57:59 +00001716 __link_pointer __nl = __hold->__as_link();
1717 __link_nodes(__p.__ptr_, __nl, __nl);
Howard Hinnant3e519522010-05-11 19:42:16 +00001718 ++base::__sz();
Eric Fiselierb88ea352015-12-30 20:57:59 +00001719 __hold.release();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001720#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselierb88ea352015-12-30 20:57:59 +00001721 return iterator(__nl, this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001722#else
Eric Fiselierb88ea352015-12-30 20:57:59 +00001723 return iterator(__nl);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001724#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001725}
1726
Eric Fiseliercf9ed002017-04-16 03:45:35 +00001727#endif // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001728
1729template <class _Tp, class _Alloc>
1730void
1731list<_Tp, _Alloc>::pop_front()
1732{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001733 _LIBCPP_ASSERT(!empty(), "list::pop_front() called with empty list");
Howard Hinnant3e519522010-05-11 19:42:16 +00001734 __node_allocator& __na = base::__node_alloc();
Eric Fiselierb88ea352015-12-30 20:57:59 +00001735 __link_pointer __n = base::__end_.__next_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001736 base::__unlink_nodes(__n, __n);
1737 --base::__sz();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001738#if _LIBCPP_DEBUG_LEVEL >= 2
1739 __c_node* __c = __get_db()->__find_c_and_lock(this);
1740 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1741 {
1742 --__p;
1743 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001744 if (__i->__ptr_ == __n)
Howard Hinnant920b56c2011-09-27 23:55:03 +00001745 {
1746 (*__p)->__c_ = nullptr;
1747 if (--__c->end_ != __p)
1748 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1749 }
1750 }
1751 __get_db()->unlock();
1752#endif
Eric Fiselierb88ea352015-12-30 20:57:59 +00001753 __node_pointer __np = __n->__as_node();
1754 __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
1755 __node_alloc_traits::deallocate(__na, __np, 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001756}
1757
1758template <class _Tp, class _Alloc>
1759void
1760list<_Tp, _Alloc>::pop_back()
1761{
Dmitri Gribenkoc3f9c802013-06-24 06:15:57 +00001762 _LIBCPP_ASSERT(!empty(), "list::pop_back() called with empty list");
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 = base::__end_.__prev_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001765 base::__unlink_nodes(__n, __n);
1766 --base::__sz();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001767#if _LIBCPP_DEBUG_LEVEL >= 2
1768 __c_node* __c = __get_db()->__find_c_and_lock(this);
1769 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1770 {
1771 --__p;
1772 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001773 if (__i->__ptr_ == __n)
Howard Hinnant920b56c2011-09-27 23:55:03 +00001774 {
1775 (*__p)->__c_ = nullptr;
1776 if (--__c->end_ != __p)
1777 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1778 }
1779 }
1780 __get_db()->unlock();
1781#endif
Eric Fiselierb88ea352015-12-30 20:57:59 +00001782 __node_pointer __np = __n->__as_node();
1783 __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
1784 __node_alloc_traits::deallocate(__na, __np, 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001785}
1786
1787template <class _Tp, class _Alloc>
1788typename list<_Tp, _Alloc>::iterator
1789list<_Tp, _Alloc>::erase(const_iterator __p)
1790{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001791#if _LIBCPP_DEBUG_LEVEL >= 2
1792 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1793 "list::erase(iterator) called with an iterator not"
1794 " referring to this list");
1795#endif
Howard Hinnantb0e4c9d2013-04-05 00:18:49 +00001796 _LIBCPP_ASSERT(__p != end(),
1797 "list::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnant3e519522010-05-11 19:42:16 +00001798 __node_allocator& __na = base::__node_alloc();
Eric Fiselierb88ea352015-12-30 20:57:59 +00001799 __link_pointer __n = __p.__ptr_;
1800 __link_pointer __r = __n->__next_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001801 base::__unlink_nodes(__n, __n);
1802 --base::__sz();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001803#if _LIBCPP_DEBUG_LEVEL >= 2
1804 __c_node* __c = __get_db()->__find_c_and_lock(this);
Eric Fiselier878e7e22016-10-23 19:26:39 +00001805 for (__i_node** __ip = __c->end_; __ip != __c->beg_; )
Howard Hinnant920b56c2011-09-27 23:55:03 +00001806 {
Eric Fiselier878e7e22016-10-23 19:26:39 +00001807 --__ip;
1808 iterator* __i = static_cast<iterator*>((*__ip)->__i_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001809 if (__i->__ptr_ == __n)
Howard Hinnant920b56c2011-09-27 23:55:03 +00001810 {
Eric Fiselier878e7e22016-10-23 19:26:39 +00001811 (*__ip)->__c_ = nullptr;
1812 if (--__c->end_ != __ip)
1813 memmove(__ip, __ip+1, (__c->end_ - __ip)*sizeof(__i_node*));
Howard Hinnant920b56c2011-09-27 23:55:03 +00001814 }
1815 }
1816 __get_db()->unlock();
1817#endif
Eric Fiselierb88ea352015-12-30 20:57:59 +00001818 __node_pointer __np = __n->__as_node();
1819 __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
1820 __node_alloc_traits::deallocate(__na, __np, 1);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001821#if _LIBCPP_DEBUG_LEVEL >= 2
1822 return iterator(__r, this);
1823#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001824 return iterator(__r);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001825#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001826}
1827
1828template <class _Tp, class _Alloc>
1829typename list<_Tp, _Alloc>::iterator
1830list<_Tp, _Alloc>::erase(const_iterator __f, const_iterator __l)
1831{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001832#if _LIBCPP_DEBUG_LEVEL >= 2
1833 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__f) == this,
1834 "list::erase(iterator, iterator) called with an iterator not"
1835 " referring to this list");
Eric Fiselier2e519572016-12-28 06:06:09 +00001836 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__l) == this,
1837 "list::erase(iterator, iterator) called with an iterator not"
1838 " referring to this list");
Howard Hinnant920b56c2011-09-27 23:55:03 +00001839#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001840 if (__f != __l)
1841 {
1842 __node_allocator& __na = base::__node_alloc();
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001843 base::__unlink_nodes(__f.__ptr_, __l.__ptr_->__prev_);
Howard Hinnant3e519522010-05-11 19:42:16 +00001844 while (__f != __l)
1845 {
Eric Fiselierb88ea352015-12-30 20:57:59 +00001846 __link_pointer __n = __f.__ptr_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001847 ++__f;
1848 --base::__sz();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001849#if _LIBCPP_DEBUG_LEVEL >= 2
1850 __c_node* __c = __get_db()->__find_c_and_lock(this);
1851 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1852 {
1853 --__p;
1854 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001855 if (__i->__ptr_ == __n)
Howard Hinnant920b56c2011-09-27 23:55:03 +00001856 {
1857 (*__p)->__c_ = nullptr;
1858 if (--__c->end_ != __p)
1859 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1860 }
1861 }
1862 __get_db()->unlock();
1863#endif
Eric Fiselierb88ea352015-12-30 20:57:59 +00001864 __node_pointer __np = __n->__as_node();
1865 __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
1866 __node_alloc_traits::deallocate(__na, __np, 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001867 }
1868 }
Howard Hinnant920b56c2011-09-27 23:55:03 +00001869#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001870 return iterator(__l.__ptr_, this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001871#else
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001872 return iterator(__l.__ptr_);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001873#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001874}
1875
1876template <class _Tp, class _Alloc>
1877void
1878list<_Tp, _Alloc>::resize(size_type __n)
1879{
1880 if (__n < base::__sz())
1881 erase(__iterator(__n), end());
1882 else if (__n > base::__sz())
1883 {
1884 __n -= base::__sz();
1885 size_type __ds = 0;
1886 __node_allocator& __na = base::__node_alloc();
Eric Fiselier0a412f42017-10-17 19:12:23 +00001887 __hold_pointer __hold = __allocate_node(__na);
Howard Hinnantce48a112011-06-30 21:18:19 +00001888 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_));
Howard Hinnant3e519522010-05-11 19:42:16 +00001889 ++__ds;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001890#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselierb88ea352015-12-30 20:57:59 +00001891 iterator __r = iterator(__hold.release()->__as_link(), this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001892#else
Eric Fiselierb88ea352015-12-30 20:57:59 +00001893 iterator __r = iterator(__hold.release()->__as_link());
Howard Hinnant920b56c2011-09-27 23:55:03 +00001894#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001895 iterator __e = __r;
1896#ifndef _LIBCPP_NO_EXCEPTIONS
1897 try
1898 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001899#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001900 for (--__n; __n != 0; --__n, ++__e, ++__ds)
1901 {
1902 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001903 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_));
Eric Fiselierb88ea352015-12-30 20:57:59 +00001904 __e.__ptr_->__next_ = __hold.get()->__as_link();
Howard Hinnant3e519522010-05-11 19:42:16 +00001905 __hold->__prev_ = __e.__ptr_;
1906 __hold.release();
1907 }
1908#ifndef _LIBCPP_NO_EXCEPTIONS
1909 }
1910 catch (...)
1911 {
1912 while (true)
1913 {
Howard Hinnantce48a112011-06-30 21:18:19 +00001914 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Eric Fiselierb88ea352015-12-30 20:57:59 +00001915 __link_pointer __prev = __e.__ptr_->__prev_;
1916 __node_alloc_traits::deallocate(__na, __e.__ptr_->__as_node(), 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001917 if (__prev == 0)
1918 break;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001919#if _LIBCPP_DEBUG_LEVEL >= 2
1920 __e = iterator(__prev, this);
1921#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001922 __e = iterator(__prev);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001923#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001924 }
1925 throw;
1926 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001927#endif // _LIBCPP_NO_EXCEPTIONS
Marshall Clow28d65da2014-08-05 01:34:12 +00001928 __link_nodes_at_back(__r.__ptr_, __e.__ptr_);
Howard Hinnant3e519522010-05-11 19:42:16 +00001929 base::__sz() += __ds;
1930 }
1931}
1932
1933template <class _Tp, class _Alloc>
1934void
1935list<_Tp, _Alloc>::resize(size_type __n, const value_type& __x)
1936{
1937 if (__n < base::__sz())
1938 erase(__iterator(__n), end());
1939 else if (__n > base::__sz())
1940 {
1941 __n -= base::__sz();
1942 size_type __ds = 0;
1943 __node_allocator& __na = base::__node_alloc();
Eric Fiselier0a412f42017-10-17 19:12:23 +00001944 __hold_pointer __hold = __allocate_node(__na);
Howard Hinnantce48a112011-06-30 21:18:19 +00001945 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnant3e519522010-05-11 19:42:16 +00001946 ++__ds;
Eric Fiselierb88ea352015-12-30 20:57:59 +00001947 __link_pointer __nl = __hold.release()->__as_link();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001948#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselierb88ea352015-12-30 20:57:59 +00001949 iterator __r = iterator(__nl, this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001950#else
Eric Fiselierb88ea352015-12-30 20:57:59 +00001951 iterator __r = iterator(__nl);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001952#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001953 iterator __e = __r;
1954#ifndef _LIBCPP_NO_EXCEPTIONS
1955 try
1956 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001957#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001958 for (--__n; __n != 0; --__n, ++__e, ++__ds)
1959 {
1960 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001961 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001962 __e.__ptr_->__next_ = __hold.get()->__as_link();
Howard Hinnant3e519522010-05-11 19:42:16 +00001963 __hold->__prev_ = __e.__ptr_;
1964 __hold.release();
1965 }
1966#ifndef _LIBCPP_NO_EXCEPTIONS
1967 }
1968 catch (...)
1969 {
1970 while (true)
1971 {
Howard Hinnantce48a112011-06-30 21:18:19 +00001972 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Eric Fiselierb88ea352015-12-30 20:57:59 +00001973 __link_pointer __prev = __e.__ptr_->__prev_;
1974 __node_alloc_traits::deallocate(__na, __e.__ptr_->__as_node(), 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001975 if (__prev == 0)
1976 break;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001977#if _LIBCPP_DEBUG_LEVEL >= 2
1978 __e = iterator(__prev, this);
1979#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001980 __e = iterator(__prev);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001981#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001982 }
1983 throw;
1984 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001985#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiselier5243e192016-01-04 03:27:52 +00001986 __link_nodes(base::__end_as_link(), __r.__ptr_, __e.__ptr_);
Howard Hinnant3e519522010-05-11 19:42:16 +00001987 base::__sz() += __ds;
Howard Hinnantb3371f62010-08-22 00:02:43 +00001988 }
Howard Hinnant3e519522010-05-11 19:42:16 +00001989}
1990
1991template <class _Tp, class _Alloc>
1992void
1993list<_Tp, _Alloc>::splice(const_iterator __p, list& __c)
1994{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001995 _LIBCPP_ASSERT(this != &__c,
1996 "list::splice(iterator, list) called with this == &list");
1997#if _LIBCPP_DEBUG_LEVEL >= 2
1998 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1999 "list::splice(iterator, list) called with an iterator not"
2000 " referring to this list");
2001#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002002 if (!__c.empty())
2003 {
Eric Fiselierb88ea352015-12-30 20:57:59 +00002004 __link_pointer __f = __c.__end_.__next_;
2005 __link_pointer __l = __c.__end_.__prev_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002006 base::__unlink_nodes(__f, __l);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002007 __link_nodes(__p.__ptr_, __f, __l);
Howard Hinnant3e519522010-05-11 19:42:16 +00002008 base::__sz() += __c.__sz();
2009 __c.__sz() = 0;
Howard Hinnant920b56c2011-09-27 23:55:03 +00002010#if _LIBCPP_DEBUG_LEVEL >= 2
2011 __libcpp_db* __db = __get_db();
2012 __c_node* __cn1 = __db->__find_c_and_lock(this);
2013 __c_node* __cn2 = __db->__find_c(&__c);
Eric Fiselier878e7e22016-10-23 19:26:39 +00002014 for (__i_node** __ip = __cn2->end_; __ip != __cn2->beg_;)
Howard Hinnant920b56c2011-09-27 23:55:03 +00002015 {
Eric Fiselier878e7e22016-10-23 19:26:39 +00002016 --__ip;
2017 iterator* __i = static_cast<iterator*>((*__ip)->__i_);
Eric Fiselier5243e192016-01-04 03:27:52 +00002018 if (__i->__ptr_ != __c.__end_as_link())
Howard Hinnant920b56c2011-09-27 23:55:03 +00002019 {
Eric Fiselier878e7e22016-10-23 19:26:39 +00002020 __cn1->__add(*__ip);
2021 (*__ip)->__c_ = __cn1;
2022 if (--__cn2->end_ != __ip)
2023 memmove(__ip, __ip+1, (__cn2->end_ - __ip)*sizeof(__i_node*));
Howard Hinnant920b56c2011-09-27 23:55:03 +00002024 }
2025 }
2026 __db->unlock();
2027#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002028 }
2029}
2030
2031template <class _Tp, class _Alloc>
2032void
2033list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __i)
2034{
Howard Hinnant920b56c2011-09-27 23:55:03 +00002035#if _LIBCPP_DEBUG_LEVEL >= 2
2036 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2037 "list::splice(iterator, list, iterator) called with first iterator not"
2038 " referring to this list");
2039 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__i) == &__c,
2040 "list::splice(iterator, list, iterator) called with second iterator not"
2041 " referring to list argument");
2042 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(&__i),
2043 "list::splice(iterator, list, iterator) called with second iterator not"
2044 " derefereceable");
2045#endif
2046 if (__p.__ptr_ != __i.__ptr_ && __p.__ptr_ != __i.__ptr_->__next_)
Howard Hinnant3e519522010-05-11 19:42:16 +00002047 {
Eric Fiselierb88ea352015-12-30 20:57:59 +00002048 __link_pointer __f = __i.__ptr_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002049 base::__unlink_nodes(__f, __f);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002050 __link_nodes(__p.__ptr_, __f, __f);
Howard Hinnant3e519522010-05-11 19:42:16 +00002051 --__c.__sz();
2052 ++base::__sz();
Howard Hinnant920b56c2011-09-27 23:55:03 +00002053#if _LIBCPP_DEBUG_LEVEL >= 2
2054 __libcpp_db* __db = __get_db();
2055 __c_node* __cn1 = __db->__find_c_and_lock(this);
2056 __c_node* __cn2 = __db->__find_c(&__c);
Eric Fiselier878e7e22016-10-23 19:26:39 +00002057 for (__i_node** __ip = __cn2->end_; __ip != __cn2->beg_;)
Howard Hinnant920b56c2011-09-27 23:55:03 +00002058 {
Eric Fiselier878e7e22016-10-23 19:26:39 +00002059 --__ip;
2060 iterator* __j = static_cast<iterator*>((*__ip)->__i_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002061 if (__j->__ptr_ == __f)
Howard Hinnant920b56c2011-09-27 23:55:03 +00002062 {
Eric Fiselier878e7e22016-10-23 19:26:39 +00002063 __cn1->__add(*__ip);
2064 (*__ip)->__c_ = __cn1;
2065 if (--__cn2->end_ != __ip)
2066 memmove(__ip, __ip+1, (__cn2->end_ - __ip)*sizeof(__i_node*));
Howard Hinnant920b56c2011-09-27 23:55:03 +00002067 }
2068 }
2069 __db->unlock();
2070#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002071 }
2072}
2073
2074template <class _Tp, class _Alloc>
2075void
2076list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l)
2077{
Howard Hinnant920b56c2011-09-27 23:55:03 +00002078#if _LIBCPP_DEBUG_LEVEL >= 2
2079 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2080 "list::splice(iterator, list, iterator, iterator) called with first iterator not"
2081 " referring to this list");
2082 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__f) == &__c,
2083 "list::splice(iterator, list, iterator, iterator) called with second iterator not"
2084 " referring to list argument");
2085 if (this == &__c)
2086 {
2087 for (const_iterator __i = __f; __i != __l; ++__i)
2088 _LIBCPP_ASSERT(__i != __p,
2089 "list::splice(iterator, list, iterator, iterator)"
2090 " called with the first iterator within the range"
2091 " of the second and third iterators");
2092 }
2093#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002094 if (__f != __l)
2095 {
Eric Fiselierb88ea352015-12-30 20:57:59 +00002096 __link_pointer __first = __f.__ptr_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002097 --__l;
Eric Fiselierb88ea352015-12-30 20:57:59 +00002098 __link_pointer __last = __l.__ptr_;
Eric Fiselierd053b592018-01-25 00:02:48 +00002099 if (this != &__c)
2100 {
2101 size_type __s = _VSTD::distance(__f, __l) + 1;
2102 __c.__sz() -= __s;
2103 base::__sz() += __s;
2104 }
Howard Hinnant3e519522010-05-11 19:42:16 +00002105 base::__unlink_nodes(__first, __last);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002106 __link_nodes(__p.__ptr_, __first, __last);
Howard Hinnant920b56c2011-09-27 23:55:03 +00002107#if _LIBCPP_DEBUG_LEVEL >= 2
2108 __libcpp_db* __db = __get_db();
2109 __c_node* __cn1 = __db->__find_c_and_lock(this);
2110 __c_node* __cn2 = __db->__find_c(&__c);
Eric Fiselier878e7e22016-10-23 19:26:39 +00002111 for (__i_node** __ip = __cn2->end_; __ip != __cn2->beg_;)
Howard Hinnant920b56c2011-09-27 23:55:03 +00002112 {
Eric Fiselier878e7e22016-10-23 19:26:39 +00002113 --__ip;
2114 iterator* __j = static_cast<iterator*>((*__ip)->__i_);
Eric Fiselierb88ea352015-12-30 20:57:59 +00002115 for (__link_pointer __k = __f.__ptr_;
Howard Hinnant920b56c2011-09-27 23:55:03 +00002116 __k != __l.__ptr_; __k = __k->__next_)
2117 {
2118 if (__j->__ptr_ == __k)
2119 {
Eric Fiselier878e7e22016-10-23 19:26:39 +00002120 __cn1->__add(*__ip);
2121 (*__ip)->__c_ = __cn1;
2122 if (--__cn2->end_ != __ip)
2123 memmove(__ip, __ip+1, (__cn2->end_ - __ip)*sizeof(__i_node*));
Howard Hinnant920b56c2011-09-27 23:55:03 +00002124 }
2125 }
2126 }
2127 __db->unlock();
2128#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002129 }
2130}
2131
2132template <class _Tp, class _Alloc>
2133void
2134list<_Tp, _Alloc>::remove(const value_type& __x)
2135{
Eric Fiselier5cac7752016-12-14 22:48:38 +00002136 list<_Tp, _Alloc> __deleted_nodes(get_allocator()); // collect the nodes we're removing
Marshall Clowced70062014-08-08 15:35:52 +00002137 for (const_iterator __i = begin(), __e = end(); __i != __e;)
Howard Hinnant3e519522010-05-11 19:42:16 +00002138 {
2139 if (*__i == __x)
2140 {
Marshall Clowced70062014-08-08 15:35:52 +00002141 const_iterator __j = _VSTD::next(__i);
Howard Hinnant3e519522010-05-11 19:42:16 +00002142 for (; __j != __e && *__j == __x; ++__j)
2143 ;
Marshall Clowced70062014-08-08 15:35:52 +00002144 __deleted_nodes.splice(__deleted_nodes.end(), *this, __i, __j);
2145 __i = __j;
Marshall Clow90ba05332014-08-04 17:32:25 +00002146 if (__i != __e)
Marshall Clow28d65da2014-08-05 01:34:12 +00002147 ++__i;
Howard Hinnant3e519522010-05-11 19:42:16 +00002148 }
2149 else
2150 ++__i;
2151 }
2152}
2153
2154template <class _Tp, class _Alloc>
2155template <class _Pred>
2156void
2157list<_Tp, _Alloc>::remove_if(_Pred __pred)
2158{
2159 for (iterator __i = begin(), __e = end(); __i != __e;)
2160 {
2161 if (__pred(*__i))
2162 {
Howard Hinnantce48a112011-06-30 21:18:19 +00002163 iterator __j = _VSTD::next(__i);
Howard Hinnant3e519522010-05-11 19:42:16 +00002164 for (; __j != __e && __pred(*__j); ++__j)
2165 ;
2166 __i = erase(__i, __j);
Marshall Clow90ba05332014-08-04 17:32:25 +00002167 if (__i != __e)
Marshall Clow28d65da2014-08-05 01:34:12 +00002168 ++__i;
Howard Hinnant3e519522010-05-11 19:42:16 +00002169 }
2170 else
2171 ++__i;
2172 }
2173}
2174
2175template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00002176inline
Howard Hinnant3e519522010-05-11 19:42:16 +00002177void
2178list<_Tp, _Alloc>::unique()
2179{
2180 unique(__equal_to<value_type>());
2181}
2182
2183template <class _Tp, class _Alloc>
2184template <class _BinaryPred>
2185void
2186list<_Tp, _Alloc>::unique(_BinaryPred __binary_pred)
2187{
2188 for (iterator __i = begin(), __e = end(); __i != __e;)
2189 {
Howard Hinnantce48a112011-06-30 21:18:19 +00002190 iterator __j = _VSTD::next(__i);
Howard Hinnant3e519522010-05-11 19:42:16 +00002191 for (; __j != __e && __binary_pred(*__i, *__j); ++__j)
2192 ;
2193 if (++__i != __j)
2194 __i = erase(__i, __j);
2195 }
2196}
2197
2198template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00002199inline
Howard Hinnant3e519522010-05-11 19:42:16 +00002200void
2201list<_Tp, _Alloc>::merge(list& __c)
2202{
2203 merge(__c, __less<value_type>());
2204}
2205
2206template <class _Tp, class _Alloc>
2207template <class _Comp>
2208void
2209list<_Tp, _Alloc>::merge(list& __c, _Comp __comp)
2210{
2211 if (this != &__c)
2212 {
2213 iterator __f1 = begin();
2214 iterator __e1 = end();
2215 iterator __f2 = __c.begin();
2216 iterator __e2 = __c.end();
2217 while (__f1 != __e1 && __f2 != __e2)
2218 {
2219 if (__comp(*__f2, *__f1))
2220 {
2221 size_type __ds = 1;
Howard Hinnantce48a112011-06-30 21:18:19 +00002222 iterator __m2 = _VSTD::next(__f2);
Howard Hinnant3e519522010-05-11 19:42:16 +00002223 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2, ++__ds)
2224 ;
2225 base::__sz() += __ds;
2226 __c.__sz() -= __ds;
Eric Fiselierb88ea352015-12-30 20:57:59 +00002227 __link_pointer __f = __f2.__ptr_;
2228 __link_pointer __l = __m2.__ptr_->__prev_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002229 __f2 = __m2;
2230 base::__unlink_nodes(__f, __l);
Howard Hinnantce48a112011-06-30 21:18:19 +00002231 __m2 = _VSTD::next(__f1);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002232 __link_nodes(__f1.__ptr_, __f, __l);
Howard Hinnant3e519522010-05-11 19:42:16 +00002233 __f1 = __m2;
2234 }
2235 else
2236 ++__f1;
2237 }
2238 splice(__e1, __c);
Howard Hinnant920b56c2011-09-27 23:55:03 +00002239#if _LIBCPP_DEBUG_LEVEL >= 2
2240 __libcpp_db* __db = __get_db();
2241 __c_node* __cn1 = __db->__find_c_and_lock(this);
2242 __c_node* __cn2 = __db->__find_c(&__c);
2243 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
2244 {
2245 --__p;
2246 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Eric Fiselier5243e192016-01-04 03:27:52 +00002247 if (__i->__ptr_ != __c.__end_as_link())
Howard Hinnant920b56c2011-09-27 23:55:03 +00002248 {
2249 __cn1->__add(*__p);
2250 (*__p)->__c_ = __cn1;
2251 if (--__cn2->end_ != __p)
2252 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
2253 }
2254 }
2255 __db->unlock();
2256#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002257 }
2258}
2259
2260template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00002261inline
Howard Hinnant3e519522010-05-11 19:42:16 +00002262void
2263list<_Tp, _Alloc>::sort()
2264{
2265 sort(__less<value_type>());
2266}
2267
2268template <class _Tp, class _Alloc>
2269template <class _Comp>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00002270inline
Howard Hinnant3e519522010-05-11 19:42:16 +00002271void
2272list<_Tp, _Alloc>::sort(_Comp __comp)
2273{
2274 __sort(begin(), end(), base::__sz(), __comp);
2275}
2276
2277template <class _Tp, class _Alloc>
2278template <class _Comp>
Howard Hinnant3e519522010-05-11 19:42:16 +00002279typename list<_Tp, _Alloc>::iterator
2280list<_Tp, _Alloc>::__sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp)
2281{
2282 switch (__n)
2283 {
2284 case 0:
2285 case 1:
2286 return __f1;
2287 case 2:
2288 if (__comp(*--__e2, *__f1))
2289 {
Eric Fiselierb88ea352015-12-30 20:57:59 +00002290 __link_pointer __f = __e2.__ptr_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002291 base::__unlink_nodes(__f, __f);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002292 __link_nodes(__f1.__ptr_, __f, __f);
Howard Hinnant3e519522010-05-11 19:42:16 +00002293 return __e2;
2294 }
2295 return __f1;
2296 }
2297 size_type __n2 = __n / 2;
Howard Hinnantce48a112011-06-30 21:18:19 +00002298 iterator __e1 = _VSTD::next(__f1, __n2);
Howard Hinnant3e519522010-05-11 19:42:16 +00002299 iterator __r = __f1 = __sort(__f1, __e1, __n2, __comp);
2300 iterator __f2 = __e1 = __sort(__e1, __e2, __n - __n2, __comp);
2301 if (__comp(*__f2, *__f1))
2302 {
Howard Hinnantce48a112011-06-30 21:18:19 +00002303 iterator __m2 = _VSTD::next(__f2);
Howard Hinnant3e519522010-05-11 19:42:16 +00002304 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2)
2305 ;
Eric Fiselierb88ea352015-12-30 20:57:59 +00002306 __link_pointer __f = __f2.__ptr_;
2307 __link_pointer __l = __m2.__ptr_->__prev_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002308 __r = __f2;
2309 __e1 = __f2 = __m2;
2310 base::__unlink_nodes(__f, __l);
Howard Hinnantce48a112011-06-30 21:18:19 +00002311 __m2 = _VSTD::next(__f1);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002312 __link_nodes(__f1.__ptr_, __f, __l);
Howard Hinnant3e519522010-05-11 19:42:16 +00002313 __f1 = __m2;
2314 }
2315 else
2316 ++__f1;
2317 while (__f1 != __e1 && __f2 != __e2)
2318 {
2319 if (__comp(*__f2, *__f1))
2320 {
Howard Hinnantce48a112011-06-30 21:18:19 +00002321 iterator __m2 = _VSTD::next(__f2);
Howard Hinnant3e519522010-05-11 19:42:16 +00002322 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2)
2323 ;
Eric Fiselierb88ea352015-12-30 20:57:59 +00002324 __link_pointer __f = __f2.__ptr_;
2325 __link_pointer __l = __m2.__ptr_->__prev_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002326 if (__e1 == __f2)
2327 __e1 = __m2;
2328 __f2 = __m2;
2329 base::__unlink_nodes(__f, __l);
Howard Hinnantce48a112011-06-30 21:18:19 +00002330 __m2 = _VSTD::next(__f1);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002331 __link_nodes(__f1.__ptr_, __f, __l);
Howard Hinnant3e519522010-05-11 19:42:16 +00002332 __f1 = __m2;
2333 }
2334 else
2335 ++__f1;
2336 }
2337 return __r;
2338}
2339
2340template <class _Tp, class _Alloc>
2341void
Howard Hinnant45900102011-06-03 17:30:28 +00002342list<_Tp, _Alloc>::reverse() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00002343{
2344 if (base::__sz() > 1)
2345 {
2346 iterator __e = end();
Howard Hinnant920b56c2011-09-27 23:55:03 +00002347 for (iterator __i = begin(); __i.__ptr_ != __e.__ptr_;)
2348 {
Howard Hinnantce48a112011-06-30 21:18:19 +00002349 _VSTD::swap(__i.__ptr_->__prev_, __i.__ptr_->__next_);
Howard Hinnant920b56c2011-09-27 23:55:03 +00002350 __i.__ptr_ = __i.__ptr_->__prev_;
2351 }
Howard Hinnantce48a112011-06-30 21:18:19 +00002352 _VSTD::swap(__e.__ptr_->__prev_, __e.__ptr_->__next_);
Howard Hinnant3e519522010-05-11 19:42:16 +00002353 }
2354}
2355
2356template <class _Tp, class _Alloc>
Howard Hinnant920b56c2011-09-27 23:55:03 +00002357bool
2358list<_Tp, _Alloc>::__invariants() const
2359{
2360 return size() == _VSTD::distance(begin(), end());
2361}
2362
2363#if _LIBCPP_DEBUG_LEVEL >= 2
2364
2365template <class _Tp, class _Alloc>
2366bool
2367list<_Tp, _Alloc>::__dereferenceable(const const_iterator* __i) const
2368{
Eric Fiselier5243e192016-01-04 03:27:52 +00002369 return __i->__ptr_ != this->__end_as_link();
Howard Hinnant920b56c2011-09-27 23:55:03 +00002370}
2371
2372template <class _Tp, class _Alloc>
2373bool
2374list<_Tp, _Alloc>::__decrementable(const const_iterator* __i) const
2375{
2376 return !empty() && __i->__ptr_ != base::__end_.__next_;
2377}
2378
2379template <class _Tp, class _Alloc>
2380bool
Eric Fiselierfd838222016-12-23 23:37:52 +00002381list<_Tp, _Alloc>::__addable(const const_iterator*, ptrdiff_t) const
Howard Hinnant920b56c2011-09-27 23:55:03 +00002382{
2383 return false;
2384}
2385
2386template <class _Tp, class _Alloc>
2387bool
Eric Fiselierfd838222016-12-23 23:37:52 +00002388list<_Tp, _Alloc>::__subscriptable(const const_iterator*, ptrdiff_t) const
Howard Hinnant920b56c2011-09-27 23:55:03 +00002389{
2390 return false;
2391}
2392
2393#endif // _LIBCPP_DEBUG_LEVEL >= 2
2394
2395template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00002396inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00002397bool
2398operator==(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2399{
Howard Hinnantce48a112011-06-30 21:18:19 +00002400 return __x.size() == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnant3e519522010-05-11 19:42:16 +00002401}
2402
2403template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00002404inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00002405bool
2406operator< (const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2407{
Howard Hinnantce48a112011-06-30 21:18:19 +00002408 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnant3e519522010-05-11 19:42:16 +00002409}
2410
2411template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00002412inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00002413bool
2414operator!=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2415{
2416 return !(__x == __y);
2417}
2418
2419template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00002420inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00002421bool
2422operator> (const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2423{
2424 return __y < __x;
2425}
2426
2427template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00002428inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00002429bool
2430operator>=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2431{
2432 return !(__x < __y);
2433}
2434
2435template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00002436inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00002437bool
2438operator<=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2439{
2440 return !(__y < __x);
2441}
2442
2443template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00002444inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00002445void
2446swap(list<_Tp, _Alloc>& __x, list<_Tp, _Alloc>& __y)
Howard Hinnant45900102011-06-03 17:30:28 +00002447 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnant3e519522010-05-11 19:42:16 +00002448{
2449 __x.swap(__y);
2450}
2451
2452_LIBCPP_END_NAMESPACE_STD
2453
Eric Fiseliera016efb2017-05-31 22:07:49 +00002454_LIBCPP_POP_MACROS
2455
Howard Hinnant3e519522010-05-11 19:42:16 +00002456#endif // _LIBCPP_LIST