blob: 76c50a156093a6c796fb8584f0cd2890026905e7 [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>
Eric Fiselier0e411642016-07-21 03:20:17 +000096 reference emplace_front(Args&&... args);
Howard Hinnant3e519522010-05-11 19:42:16 +000097 void pop_front();
98 template <class... Args>
Eric Fiselier0e411642016-07-21 03:20:17 +000099 reference emplace_back(Args&&... args);
Howard Hinnant3e519522010-05-11 19:42:16 +0000100 void pop_back();
101 void push_front(const value_type& x);
102 void push_front(value_type&& x);
103 void push_back(const value_type& x);
104 void push_back(value_type&& x);
105 template <class... Args>
106 iterator emplace(const_iterator position, Args&&... args);
107 iterator insert(const_iterator position, const value_type& x);
108 iterator insert(const_iterator position, value_type&& x);
109 iterator insert(const_iterator position, size_type n, const value_type& x);
110 template <class Iter>
111 iterator insert(const_iterator position, Iter first, Iter last);
112 iterator insert(const_iterator position, initializer_list<value_type> il);
113
114 iterator erase(const_iterator position);
115 iterator erase(const_iterator position, const_iterator last);
116
117 void resize(size_type sz);
118 void resize(size_type sz, const value_type& c);
119
Howard Hinnant45900102011-06-03 17:30:28 +0000120 void swap(list&)
Marshall Clowe3fbe142015-07-13 20:04:56 +0000121 noexcept(allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnant45900102011-06-03 17:30:28 +0000122 void clear() noexcept;
Howard Hinnant3e519522010-05-11 19:42:16 +0000123
124 void splice(const_iterator position, list& x);
125 void splice(const_iterator position, list&& x);
126 void splice(const_iterator position, list& x, const_iterator i);
127 void splice(const_iterator position, list&& x, const_iterator i);
128 void splice(const_iterator position, list& x, const_iterator first,
129 const_iterator last);
130 void splice(const_iterator position, list&& x, const_iterator first,
131 const_iterator last);
132
133 void remove(const value_type& value);
134 template <class Pred> void remove_if(Pred pred);
135 void unique();
136 template <class BinaryPredicate>
137 void unique(BinaryPredicate binary_pred);
138 void merge(list& x);
139 void merge(list&& x);
140 template <class Compare>
141 void merge(list& x, Compare comp);
142 template <class Compare>
143 void merge(list&& x, Compare comp);
144 void sort();
145 template <class Compare>
146 void sort(Compare comp);
Howard Hinnant45900102011-06-03 17:30:28 +0000147 void reverse() noexcept;
Howard Hinnant3e519522010-05-11 19:42:16 +0000148};
149
150template <class T, class Alloc>
151 bool operator==(const list<T,Alloc>& x, const list<T,Alloc>& y);
152template <class T, class Alloc>
153 bool operator< (const list<T,Alloc>& x, const list<T,Alloc>& y);
154template <class T, class Alloc>
155 bool operator!=(const list<T,Alloc>& x, const list<T,Alloc>& y);
156template <class T, class Alloc>
157 bool operator> (const list<T,Alloc>& x, const list<T,Alloc>& y);
158template <class T, class Alloc>
159 bool operator>=(const list<T,Alloc>& x, const list<T,Alloc>& y);
160template <class T, class Alloc>
161 bool operator<=(const list<T,Alloc>& x, const list<T,Alloc>& y);
162
163template <class T, class Alloc>
Howard Hinnant45900102011-06-03 17:30:28 +0000164 void swap(list<T,Alloc>& x, list<T,Alloc>& y)
165 noexcept(noexcept(x.swap(y)));
Howard Hinnant3e519522010-05-11 19:42:16 +0000166
167} // std
168
169*/
170
171#include <__config>
172
173#include <memory>
174#include <limits>
175#include <initializer_list>
176#include <iterator>
177#include <algorithm>
Eric Fiselierb88ea352015-12-30 20:57:59 +0000178#include <type_traits>
Howard Hinnant3e519522010-05-11 19:42:16 +0000179
Howard Hinnantab4f4382011-11-29 16:45:27 +0000180#include <__undef_min_max>
181
Eric Fiselierc1bd9192014-08-10 23:53:08 +0000182#include <__debug>
Howard Hinnant42a30462013-08-02 00:26:35 +0000183
Howard Hinnant073458b2011-10-17 20:05:10 +0000184#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnant3e519522010-05-11 19:42:16 +0000185#pragma GCC system_header
Howard Hinnant073458b2011-10-17 20:05:10 +0000186#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000187
188_LIBCPP_BEGIN_NAMESPACE_STD
189
Howard Hinnantce534202011-06-14 19:58:17 +0000190template <class _Tp, class _VoidPtr> struct __list_node;
Eric Fiselierb88ea352015-12-30 20:57:59 +0000191template <class _Tp, class _VoidPtr> struct __list_node_base;
192
193template <class _Tp, class _VoidPtr>
194struct __list_node_pointer_traits {
195 typedef typename __rebind_pointer<_VoidPtr, __list_node<_Tp, _VoidPtr> >::type
196 __node_pointer;
197 typedef typename __rebind_pointer<_VoidPtr, __list_node_base<_Tp, _VoidPtr> >::type
198 __base_pointer;
199
200#if defined(_LIBCPP_ABI_LIST_REMOVE_NODE_POINTER_UB)
201 typedef __base_pointer __link_pointer;
202#else
203 typedef typename conditional<
204 is_pointer<_VoidPtr>::value,
205 __base_pointer,
206 __node_pointer
207 >::type __link_pointer;
208#endif
209
Eric Fiselier5243e192016-01-04 03:27:52 +0000210 typedef typename conditional<
211 is_same<__link_pointer, __node_pointer>::value,
212 __base_pointer,
213 __node_pointer
214 >::type __non_link_pointer;
215
216 static _LIBCPP_INLINE_VISIBILITY
217 __link_pointer __unsafe_link_pointer_cast(__link_pointer __p) {
218 return __p;
219 }
220
221 static _LIBCPP_INLINE_VISIBILITY
222 __link_pointer __unsafe_link_pointer_cast(__non_link_pointer __p) {
223 return static_cast<__link_pointer>(static_cast<_VoidPtr>(__p));
224 }
225
Eric Fiselierb88ea352015-12-30 20:57:59 +0000226};
Howard Hinnant3e519522010-05-11 19:42:16 +0000227
228template <class _Tp, class _VoidPtr>
229struct __list_node_base
230{
Eric Fiselierb88ea352015-12-30 20:57:59 +0000231 typedef __list_node_pointer_traits<_Tp, _VoidPtr> _NodeTraits;
232 typedef typename _NodeTraits::__node_pointer __node_pointer;
233 typedef typename _NodeTraits::__base_pointer __base_pointer;
234 typedef typename _NodeTraits::__link_pointer __link_pointer;
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000235
Eric Fiselierb88ea352015-12-30 20:57:59 +0000236 __link_pointer __prev_;
237 __link_pointer __next_;
Howard Hinnant3e519522010-05-11 19:42:16 +0000238
Howard Hinnant848a5372010-09-22 16:48:34 +0000239 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5243e192016-01-04 03:27:52 +0000240 __list_node_base() : __prev_(_NodeTraits::__unsafe_link_pointer_cast(__self())),
241 __next_(_NodeTraits::__unsafe_link_pointer_cast(__self())) {}
Marshall Clow28d65da2014-08-05 01:34:12 +0000242
243 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5243e192016-01-04 03:27:52 +0000244 __base_pointer __self() {
Eric Fiselierb88ea352015-12-30 20:57:59 +0000245 return pointer_traits<__base_pointer>::pointer_to(*this);
246 }
247
248 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierb88ea352015-12-30 20:57:59 +0000249 __node_pointer __as_node() {
Eric Fiselier5243e192016-01-04 03:27:52 +0000250 return static_cast<__node_pointer>(__self());
Marshall Clow28d65da2014-08-05 01:34:12 +0000251 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000252};
253
254template <class _Tp, class _VoidPtr>
255struct __list_node
256 : public __list_node_base<_Tp, _VoidPtr>
257{
258 _Tp __value_;
Eric Fiselier5243e192016-01-04 03:27:52 +0000259
260 typedef __list_node_base<_Tp, _VoidPtr> __base;
261 typedef typename __base::__link_pointer __link_pointer;
262
263 _LIBCPP_INLINE_VISIBILITY
264 __link_pointer __as_link() {
265 return static_cast<__link_pointer>(__base::__self());
266 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000267};
268
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +0000269template <class _Tp, class _Alloc = allocator<_Tp> > class _LIBCPP_TEMPLATE_VIS list;
Howard Hinnantce534202011-06-14 19:58:17 +0000270template <class _Tp, class _Alloc> class __list_imp;
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +0000271template <class _Tp, class _VoidPtr> class _LIBCPP_TEMPLATE_VIS __list_const_iterator;
Howard Hinnant3e519522010-05-11 19:42:16 +0000272
273template <class _Tp, class _VoidPtr>
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +0000274class _LIBCPP_TEMPLATE_VIS __list_iterator
Howard Hinnant3e519522010-05-11 19:42:16 +0000275{
Eric Fiselierb88ea352015-12-30 20:57:59 +0000276 typedef __list_node_pointer_traits<_Tp, _VoidPtr> _NodeTraits;
277 typedef typename _NodeTraits::__link_pointer __link_pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000278
Eric Fiselierb88ea352015-12-30 20:57:59 +0000279 __link_pointer __ptr_;
Howard Hinnant3e519522010-05-11 19:42:16 +0000280
Howard Hinnant920b56c2011-09-27 23:55:03 +0000281#if _LIBCPP_DEBUG_LEVEL >= 2
282 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierb88ea352015-12-30 20:57:59 +0000283 explicit __list_iterator(__link_pointer __p, const void* __c) _NOEXCEPT
Howard Hinnant920b56c2011-09-27 23:55:03 +0000284 : __ptr_(__p)
285 {
286 __get_db()->__insert_ic(this, __c);
287 }
288#else
Howard Hinnant848a5372010-09-22 16:48:34 +0000289 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierb88ea352015-12-30 20:57:59 +0000290 explicit __list_iterator(__link_pointer __p) _NOEXCEPT : __ptr_(__p) {}
Howard Hinnant920b56c2011-09-27 23:55:03 +0000291#endif
292
293
Howard Hinnant3e519522010-05-11 19:42:16 +0000294
295 template<class, class> friend class list;
296 template<class, class> friend class __list_imp;
297 template<class, class> friend class __list_const_iterator;
298public:
299 typedef bidirectional_iterator_tag iterator_category;
300 typedef _Tp value_type;
301 typedef value_type& reference;
Eric Fiselier1c813402015-08-23 02:56:05 +0000302 typedef typename __rebind_pointer<_VoidPtr, value_type>::type pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000303 typedef typename pointer_traits<pointer>::difference_type difference_type;
304
Howard Hinnant848a5372010-09-22 16:48:34 +0000305 _LIBCPP_INLINE_VISIBILITY
Marshall Clow0c37cfd2013-08-05 21:23:28 +0000306 __list_iterator() _NOEXCEPT : __ptr_(nullptr)
Howard Hinnant920b56c2011-09-27 23:55:03 +0000307 {
308#if _LIBCPP_DEBUG_LEVEL >= 2
309 __get_db()->__insert_i(this);
310#endif
311 }
312
313#if _LIBCPP_DEBUG_LEVEL >= 2
314
Howard Hinnant27745452011-01-28 23:46:28 +0000315 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000316 __list_iterator(const __list_iterator& __p)
317 : __ptr_(__p.__ptr_)
318 {
319 __get_db()->__iterator_copy(this, &__p);
320 }
321
322 _LIBCPP_INLINE_VISIBILITY
323 ~__list_iterator()
324 {
325 __get_db()->__erase_i(this);
326 }
327
328 _LIBCPP_INLINE_VISIBILITY
329 __list_iterator& operator=(const __list_iterator& __p)
330 {
331 if (this != &__p)
332 {
333 __get_db()->__iterator_copy(this, &__p);
334 __ptr_ = __p.__ptr_;
335 }
336 return *this;
337 }
338
339#endif // _LIBCPP_DEBUG_LEVEL >= 2
340
341 _LIBCPP_INLINE_VISIBILITY
342 reference operator*() const
343 {
344#if _LIBCPP_DEBUG_LEVEL >= 2
345 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
346 "Attempted to dereference a non-dereferenceable list::iterator");
347#endif
Eric Fiselierb88ea352015-12-30 20:57:59 +0000348 return __ptr_->__as_node()->__value_;
Howard Hinnant920b56c2011-09-27 23:55:03 +0000349 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000350 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000351 pointer operator->() const
352 {
353#if _LIBCPP_DEBUG_LEVEL >= 2
354 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
355 "Attempted to dereference a non-dereferenceable list::iterator");
356#endif
Eric Fiselierb88ea352015-12-30 20:57:59 +0000357 return pointer_traits<pointer>::pointer_to(__ptr_->__as_node()->__value_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000358 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000359
Howard Hinnant848a5372010-09-22 16:48:34 +0000360 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000361 __list_iterator& operator++()
362 {
363#if _LIBCPP_DEBUG_LEVEL >= 2
364 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
365 "Attempted to increment non-incrementable list::iterator");
366#endif
367 __ptr_ = __ptr_->__next_;
368 return *this;
369 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000370 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000371 __list_iterator operator++(int) {__list_iterator __t(*this); ++(*this); return __t;}
372
Howard Hinnant848a5372010-09-22 16:48:34 +0000373 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000374 __list_iterator& operator--()
375 {
376#if _LIBCPP_DEBUG_LEVEL >= 2
377 _LIBCPP_ASSERT(__get_const_db()->__decrementable(this),
378 "Attempted to decrement non-decrementable list::iterator");
379#endif
380 __ptr_ = __ptr_->__prev_;
381 return *this;
382 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000383 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000384 __list_iterator operator--(int) {__list_iterator __t(*this); --(*this); return __t;}
385
Howard Hinnant848a5372010-09-22 16:48:34 +0000386 friend _LIBCPP_INLINE_VISIBILITY
387 bool operator==(const __list_iterator& __x, const __list_iterator& __y)
Howard Hinnant920b56c2011-09-27 23:55:03 +0000388 {
Howard Hinnant920b56c2011-09-27 23:55:03 +0000389 return __x.__ptr_ == __y.__ptr_;
390 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000391 friend _LIBCPP_INLINE_VISIBILITY
392 bool operator!=(const __list_iterator& __x, const __list_iterator& __y)
Howard Hinnant3e519522010-05-11 19:42:16 +0000393 {return !(__x == __y);}
394};
395
396template <class _Tp, class _VoidPtr>
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +0000397class _LIBCPP_TEMPLATE_VIS __list_const_iterator
Howard Hinnant3e519522010-05-11 19:42:16 +0000398{
Eric Fiselierb88ea352015-12-30 20:57:59 +0000399 typedef __list_node_pointer_traits<_Tp, _VoidPtr> _NodeTraits;
400 typedef typename _NodeTraits::__link_pointer __link_pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000401
Eric Fiselierb88ea352015-12-30 20:57:59 +0000402 __link_pointer __ptr_;
Howard Hinnant3e519522010-05-11 19:42:16 +0000403
Howard Hinnant920b56c2011-09-27 23:55:03 +0000404#if _LIBCPP_DEBUG_LEVEL >= 2
405 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierb88ea352015-12-30 20:57:59 +0000406 explicit __list_const_iterator(__link_pointer __p, const void* __c) _NOEXCEPT
Howard Hinnant920b56c2011-09-27 23:55:03 +0000407 : __ptr_(__p)
408 {
409 __get_db()->__insert_ic(this, __c);
410 }
411#else
Howard Hinnant848a5372010-09-22 16:48:34 +0000412 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierb88ea352015-12-30 20:57:59 +0000413 explicit __list_const_iterator(__link_pointer __p) _NOEXCEPT : __ptr_(__p) {}
Howard Hinnant920b56c2011-09-27 23:55:03 +0000414#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000415
416 template<class, class> friend class list;
417 template<class, class> friend class __list_imp;
418public:
419 typedef bidirectional_iterator_tag iterator_category;
420 typedef _Tp value_type;
421 typedef const value_type& reference;
Eric Fiselier1c813402015-08-23 02:56:05 +0000422 typedef typename __rebind_pointer<_VoidPtr, const value_type>::type pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000423 typedef typename pointer_traits<pointer>::difference_type difference_type;
424
Howard Hinnant848a5372010-09-22 16:48:34 +0000425 _LIBCPP_INLINE_VISIBILITY
Marshall Clow0c37cfd2013-08-05 21:23:28 +0000426 __list_const_iterator() _NOEXCEPT : __ptr_(nullptr)
Howard Hinnant920b56c2011-09-27 23:55:03 +0000427 {
428#if _LIBCPP_DEBUG_LEVEL >= 2
429 __get_db()->__insert_i(this);
430#endif
431 }
Howard Hinnant27745452011-01-28 23:46:28 +0000432 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf7509232013-04-05 17:58:52 +0000433 __list_const_iterator(const __list_iterator<_Tp, _VoidPtr>& __p) _NOEXCEPT
Howard Hinnant920b56c2011-09-27 23:55:03 +0000434 : __ptr_(__p.__ptr_)
435 {
436#if _LIBCPP_DEBUG_LEVEL >= 2
437 __get_db()->__iterator_copy(this, &__p);
438#endif
439 }
440
441#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant3e519522010-05-11 19:42:16 +0000442
Howard Hinnant848a5372010-09-22 16:48:34 +0000443 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000444 __list_const_iterator(const __list_const_iterator& __p)
445 : __ptr_(__p.__ptr_)
446 {
447 __get_db()->__iterator_copy(this, &__p);
448 }
449
450 _LIBCPP_INLINE_VISIBILITY
451 ~__list_const_iterator()
452 {
453 __get_db()->__erase_i(this);
454 }
455
456 _LIBCPP_INLINE_VISIBILITY
457 __list_const_iterator& operator=(const __list_const_iterator& __p)
458 {
459 if (this != &__p)
460 {
461 __get_db()->__iterator_copy(this, &__p);
462 __ptr_ = __p.__ptr_;
463 }
464 return *this;
465 }
466
467#endif // _LIBCPP_DEBUG_LEVEL >= 2
468 _LIBCPP_INLINE_VISIBILITY
469 reference operator*() const
470 {
471#if _LIBCPP_DEBUG_LEVEL >= 2
472 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
473 "Attempted to dereference a non-dereferenceable list::const_iterator");
474#endif
Eric Fiselierb88ea352015-12-30 20:57:59 +0000475 return __ptr_->__as_node()->__value_;
Howard Hinnant920b56c2011-09-27 23:55:03 +0000476 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000477 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000478 pointer operator->() const
479 {
480#if _LIBCPP_DEBUG_LEVEL >= 2
481 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
482 "Attempted to dereference a non-dereferenceable list::iterator");
483#endif
Eric Fiselierb88ea352015-12-30 20:57:59 +0000484 return pointer_traits<pointer>::pointer_to(__ptr_->__as_node()->__value_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000485 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000486
Howard Hinnant848a5372010-09-22 16:48:34 +0000487 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000488 __list_const_iterator& operator++()
489 {
490#if _LIBCPP_DEBUG_LEVEL >= 2
491 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
492 "Attempted to increment non-incrementable list::const_iterator");
493#endif
494 __ptr_ = __ptr_->__next_;
495 return *this;
496 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000497 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000498 __list_const_iterator operator++(int) {__list_const_iterator __t(*this); ++(*this); return __t;}
499
Howard Hinnant848a5372010-09-22 16:48:34 +0000500 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000501 __list_const_iterator& operator--()
502 {
503#if _LIBCPP_DEBUG_LEVEL >= 2
504 _LIBCPP_ASSERT(__get_const_db()->__decrementable(this),
505 "Attempted to decrement non-decrementable list::const_iterator");
506#endif
507 __ptr_ = __ptr_->__prev_;
508 return *this;
509 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000510 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000511 __list_const_iterator operator--(int) {__list_const_iterator __t(*this); --(*this); return __t;}
512
Howard Hinnant848a5372010-09-22 16:48:34 +0000513 friend _LIBCPP_INLINE_VISIBILITY
514 bool operator==(const __list_const_iterator& __x, const __list_const_iterator& __y)
Howard Hinnant920b56c2011-09-27 23:55:03 +0000515 {
Howard Hinnant920b56c2011-09-27 23:55:03 +0000516 return __x.__ptr_ == __y.__ptr_;
517 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000518 friend _LIBCPP_INLINE_VISIBILITY
519 bool operator!=(const __list_const_iterator& __x, const __list_const_iterator& __y)
Howard Hinnant3e519522010-05-11 19:42:16 +0000520 {return !(__x == __y);}
521};
522
523template <class _Tp, class _Alloc>
524class __list_imp
525{
526 __list_imp(const __list_imp&);
527 __list_imp& operator=(const __list_imp&);
528protected:
529 typedef _Tp value_type;
530 typedef _Alloc allocator_type;
531 typedef allocator_traits<allocator_type> __alloc_traits;
532 typedef typename __alloc_traits::size_type size_type;
533 typedef typename __alloc_traits::void_pointer __void_pointer;
534 typedef __list_iterator<value_type, __void_pointer> iterator;
535 typedef __list_const_iterator<value_type, __void_pointer> const_iterator;
536 typedef __list_node_base<value_type, __void_pointer> __node_base;
537 typedef __list_node<value_type, __void_pointer> __node;
Marshall Clow1f508012015-04-07 05:21:38 +0000538 typedef typename __rebind_alloc_helper<__alloc_traits, __node>::type __node_allocator;
Howard Hinnant3e519522010-05-11 19:42:16 +0000539 typedef allocator_traits<__node_allocator> __node_alloc_traits;
540 typedef typename __node_alloc_traits::pointer __node_pointer;
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000541 typedef typename __node_alloc_traits::pointer __node_const_pointer;
Eric Fiselier5243e192016-01-04 03:27:52 +0000542 typedef __list_node_pointer_traits<value_type, __void_pointer> __node_pointer_traits;
543 typedef typename __node_pointer_traits::__link_pointer __link_pointer;
Eric Fiselierb88ea352015-12-30 20:57:59 +0000544 typedef __link_pointer __link_const_pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000545 typedef typename __alloc_traits::pointer pointer;
546 typedef typename __alloc_traits::const_pointer const_pointer;
547 typedef typename __alloc_traits::difference_type difference_type;
548
Marshall Clow1f508012015-04-07 05:21:38 +0000549 typedef typename __rebind_alloc_helper<__alloc_traits, __node_base>::type __node_base_allocator;
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000550 typedef typename allocator_traits<__node_base_allocator>::pointer __node_base_pointer;
551
Howard Hinnant3e519522010-05-11 19:42:16 +0000552 __node_base __end_;
553 __compressed_pair<size_type, __node_allocator> __size_alloc_;
554
Howard Hinnant848a5372010-09-22 16:48:34 +0000555 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5243e192016-01-04 03:27:52 +0000556 __link_pointer __end_as_link() const _NOEXCEPT {
557 return __node_pointer_traits::__unsafe_link_pointer_cast(
558 const_cast<__node_base&>(__end_).__self());
559 }
560
561 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000562 size_type& __sz() _NOEXCEPT {return __size_alloc_.first();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000563 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000564 const size_type& __sz() const _NOEXCEPT
565 {return __size_alloc_.first();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000566 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000567 __node_allocator& __node_alloc() _NOEXCEPT
568 {return __size_alloc_.second();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000569 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000570 const __node_allocator& __node_alloc() const _NOEXCEPT
571 {return __size_alloc_.second();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000572
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +0000573 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier55b31b4e2016-11-23 01:18:56 +0000574 size_type __node_alloc_max_size() const _NOEXCEPT {
575 return __node_alloc_traits::max_size(__node_alloc());
576 }
577 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierb88ea352015-12-30 20:57:59 +0000578 static void __unlink_nodes(__link_pointer __f, __link_pointer __l) _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +0000579
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +0000580 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000581 __list_imp()
582 _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value);
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +0000583 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000584 __list_imp(const allocator_type& __a);
585 ~__list_imp();
Howard Hinnant45900102011-06-03 17:30:28 +0000586 void clear() _NOEXCEPT;
Howard Hinnant848a5372010-09-22 16:48:34 +0000587 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000588 bool empty() const _NOEXCEPT {return __sz() == 0;}
Howard Hinnant3e519522010-05-11 19:42:16 +0000589
Howard Hinnant848a5372010-09-22 16:48:34 +0000590 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000591 iterator begin() _NOEXCEPT
592 {
593#if _LIBCPP_DEBUG_LEVEL >= 2
594 return iterator(__end_.__next_, this);
595#else
596 return iterator(__end_.__next_);
597#endif
598 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000599 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000600 const_iterator begin() const _NOEXCEPT
Howard Hinnant920b56c2011-09-27 23:55:03 +0000601 {
602#if _LIBCPP_DEBUG_LEVEL >= 2
603 return const_iterator(__end_.__next_, this);
604#else
605 return const_iterator(__end_.__next_);
606#endif
607 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000608 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000609 iterator end() _NOEXCEPT
610 {
611#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselier5243e192016-01-04 03:27:52 +0000612 return iterator(__end_as_link(), this);
Howard Hinnant920b56c2011-09-27 23:55:03 +0000613#else
Eric Fiselier5243e192016-01-04 03:27:52 +0000614 return iterator(__end_as_link());
Howard Hinnant920b56c2011-09-27 23:55:03 +0000615#endif
616 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000617 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000618 const_iterator end() const _NOEXCEPT
Howard Hinnant920b56c2011-09-27 23:55:03 +0000619 {
620#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselier5243e192016-01-04 03:27:52 +0000621 return const_iterator(__end_as_link(), this);
Howard Hinnant920b56c2011-09-27 23:55:03 +0000622#else
Eric Fiselier5243e192016-01-04 03:27:52 +0000623 return const_iterator(__end_as_link());
Howard Hinnant920b56c2011-09-27 23:55:03 +0000624#endif
625 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000626
Howard Hinnant45900102011-06-03 17:30:28 +0000627 void swap(__list_imp& __c)
Marshall Clowe3fbe142015-07-13 20:04:56 +0000628#if _LIBCPP_STD_VER >= 14
Eric Fiselier2e519572016-12-28 06:06:09 +0000629 _NOEXCEPT_DEBUG;
Marshall Clowe3fbe142015-07-13 20:04:56 +0000630#else
Eric Fiselier2e519572016-12-28 06:06:09 +0000631 _NOEXCEPT_DEBUG_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clowe3fbe142015-07-13 20:04:56 +0000632 __is_nothrow_swappable<allocator_type>::value);
633#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000634
Howard Hinnant848a5372010-09-22 16:48:34 +0000635 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000636 void __copy_assign_alloc(const __list_imp& __c)
637 {__copy_assign_alloc(__c, integral_constant<bool,
638 __node_alloc_traits::propagate_on_container_copy_assignment::value>());}
639
Howard Hinnant848a5372010-09-22 16:48:34 +0000640 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000641 void __move_assign_alloc(__list_imp& __c)
Howard Hinnant45900102011-06-03 17:30:28 +0000642 _NOEXCEPT_(
643 !__node_alloc_traits::propagate_on_container_move_assignment::value ||
644 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +0000645 {__move_assign_alloc(__c, integral_constant<bool,
646 __node_alloc_traits::propagate_on_container_move_assignment::value>());}
647
648private:
Howard Hinnant848a5372010-09-22 16:48:34 +0000649 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000650 void __copy_assign_alloc(const __list_imp& __c, true_type)
651 {
652 if (__node_alloc() != __c.__node_alloc())
653 clear();
654 __node_alloc() = __c.__node_alloc();
655 }
656
Howard Hinnant848a5372010-09-22 16:48:34 +0000657 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierfd838222016-12-23 23:37:52 +0000658 void __copy_assign_alloc(const __list_imp&, false_type)
Howard Hinnant3e519522010-05-11 19:42:16 +0000659 {}
660
Howard Hinnant848a5372010-09-22 16:48:34 +0000661 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant86681392011-09-02 20:42:31 +0000662 void __move_assign_alloc(__list_imp& __c, true_type)
Howard Hinnant45900102011-06-03 17:30:28 +0000663 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +0000664 {
Howard Hinnantce48a112011-06-30 21:18:19 +0000665 __node_alloc() = _VSTD::move(__c.__node_alloc());
Howard Hinnant3e519522010-05-11 19:42:16 +0000666 }
667
Howard Hinnant848a5372010-09-22 16:48:34 +0000668 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierfd838222016-12-23 23:37:52 +0000669 void __move_assign_alloc(__list_imp&, false_type)
Howard Hinnant45900102011-06-03 17:30:28 +0000670 _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000671 {}
Eric Fiselier2e519572016-12-28 06:06:09 +0000672
673 _LIBCPP_INLINE_VISIBILITY
674 void __invalidate_all_iterators() {
675#if _LIBCPP_DEBUG_LEVEL >= 2
676 __get_db()->__invalidate_all(this);
677#endif
678 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000679};
680
681// Unlink nodes [__f, __l]
682template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +0000683inline
Howard Hinnant3e519522010-05-11 19:42:16 +0000684void
Eric Fiselierb88ea352015-12-30 20:57:59 +0000685__list_imp<_Tp, _Alloc>::__unlink_nodes(__link_pointer __f, __link_pointer __l)
Howard Hinnant45900102011-06-03 17:30:28 +0000686 _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000687{
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000688 __f->__prev_->__next_ = __l->__next_;
689 __l->__next_->__prev_ = __f->__prev_;
Howard Hinnant3e519522010-05-11 19:42:16 +0000690}
691
692template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +0000693inline
Howard Hinnant3e519522010-05-11 19:42:16 +0000694__list_imp<_Tp, _Alloc>::__list_imp()
Howard Hinnant45900102011-06-03 17:30:28 +0000695 _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +0000696 : __size_alloc_(0)
697{
698}
699
700template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +0000701inline
Howard Hinnant3e519522010-05-11 19:42:16 +0000702__list_imp<_Tp, _Alloc>::__list_imp(const allocator_type& __a)
703 : __size_alloc_(0, __node_allocator(__a))
704{
705}
706
707template <class _Tp, class _Alloc>
708__list_imp<_Tp, _Alloc>::~__list_imp()
709{
710 clear();
Howard Hinnant920b56c2011-09-27 23:55:03 +0000711#if _LIBCPP_DEBUG_LEVEL >= 2
712 __get_db()->__erase_c(this);
713#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000714}
715
716template <class _Tp, class _Alloc>
717void
Howard Hinnant45900102011-06-03 17:30:28 +0000718__list_imp<_Tp, _Alloc>::clear() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000719{
720 if (!empty())
721 {
722 __node_allocator& __na = __node_alloc();
Eric Fiselierb88ea352015-12-30 20:57:59 +0000723 __link_pointer __f = __end_.__next_;
Eric Fiselier5243e192016-01-04 03:27:52 +0000724 __link_pointer __l = __end_as_link();
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000725 __unlink_nodes(__f, __l->__prev_);
Howard Hinnant3e519522010-05-11 19:42:16 +0000726 __sz() = 0;
727 while (__f != __l)
728 {
Eric Fiselierb88ea352015-12-30 20:57:59 +0000729 __node_pointer __np = __f->__as_node();
Howard Hinnant920b56c2011-09-27 23:55:03 +0000730 __f = __f->__next_;
Eric Fiselierb88ea352015-12-30 20:57:59 +0000731 __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
732 __node_alloc_traits::deallocate(__na, __np, 1);
Howard Hinnant3e519522010-05-11 19:42:16 +0000733 }
Eric Fiselier2e519572016-12-28 06:06:09 +0000734 __invalidate_all_iterators();
Howard Hinnant3e519522010-05-11 19:42:16 +0000735 }
736}
737
738template <class _Tp, class _Alloc>
739void
740__list_imp<_Tp, _Alloc>::swap(__list_imp& __c)
Marshall Clowe3fbe142015-07-13 20:04:56 +0000741#if _LIBCPP_STD_VER >= 14
Eric Fiselier2e519572016-12-28 06:06:09 +0000742 _NOEXCEPT_DEBUG
Marshall Clowe3fbe142015-07-13 20:04:56 +0000743#else
Eric Fiselier2e519572016-12-28 06:06:09 +0000744 _NOEXCEPT_DEBUG_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clowe3fbe142015-07-13 20:04:56 +0000745 __is_nothrow_swappable<allocator_type>::value)
746#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000747{
Howard Hinnant920b56c2011-09-27 23:55:03 +0000748 _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
749 this->__node_alloc() == __c.__node_alloc(),
750 "list::swap: Either propagate_on_container_swap must be true"
751 " or the allocators must compare equal");
Howard Hinnantce48a112011-06-30 21:18:19 +0000752 using _VSTD::swap;
Marshall Clowe3fbe142015-07-13 20:04:56 +0000753 __swap_allocator(__node_alloc(), __c.__node_alloc());
Howard Hinnant3e519522010-05-11 19:42:16 +0000754 swap(__sz(), __c.__sz());
755 swap(__end_, __c.__end_);
756 if (__sz() == 0)
Eric Fiselier5243e192016-01-04 03:27:52 +0000757 __end_.__next_ = __end_.__prev_ = __end_as_link();
Howard Hinnant3e519522010-05-11 19:42:16 +0000758 else
Eric Fiselier5243e192016-01-04 03:27:52 +0000759 __end_.__prev_->__next_ = __end_.__next_->__prev_ = __end_as_link();
Howard Hinnant3e519522010-05-11 19:42:16 +0000760 if (__c.__sz() == 0)
Eric Fiselier5243e192016-01-04 03:27:52 +0000761 __c.__end_.__next_ = __c.__end_.__prev_ = __c.__end_as_link();
Howard Hinnant3e519522010-05-11 19:42:16 +0000762 else
Eric Fiselier5243e192016-01-04 03:27:52 +0000763 __c.__end_.__prev_->__next_ = __c.__end_.__next_->__prev_ = __c.__end_as_link();
Marshall Clow28d65da2014-08-05 01:34:12 +0000764
Howard Hinnant920b56c2011-09-27 23:55:03 +0000765#if _LIBCPP_DEBUG_LEVEL >= 2
766 __libcpp_db* __db = __get_db();
767 __c_node* __cn1 = __db->__find_c_and_lock(this);
768 __c_node* __cn2 = __db->__find_c(&__c);
769 std::swap(__cn1->beg_, __cn2->beg_);
770 std::swap(__cn1->end_, __cn2->end_);
771 std::swap(__cn1->cap_, __cn2->cap_);
772 for (__i_node** __p = __cn1->end_; __p != __cn1->beg_;)
773 {
774 --__p;
775 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
Eric Fiselier5243e192016-01-04 03:27:52 +0000776 if (__i->__ptr_ == __c.__end_as_link())
Howard Hinnant920b56c2011-09-27 23:55:03 +0000777 {
778 __cn2->__add(*__p);
779 if (--__cn1->end_ != __p)
780 memmove(__p, __p+1, (__cn1->end_ - __p)*sizeof(__i_node*));
781 }
782 else
783 (*__p)->__c_ = __cn1;
784 }
785 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
786 {
787 --__p;
788 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
Eric Fiselier5243e192016-01-04 03:27:52 +0000789 if (__i->__ptr_ == __end_as_link())
Howard Hinnant920b56c2011-09-27 23:55:03 +0000790 {
791 __cn1->__add(*__p);
792 if (--__cn2->end_ != __p)
793 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
794 }
795 else
796 (*__p)->__c_ = __cn2;
797 }
798 __db->unlock();
799#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000800}
801
Marshall Clowb5d34aa2015-02-18 17:24:08 +0000802template <class _Tp, class _Alloc /*= allocator<_Tp>*/>
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +0000803class _LIBCPP_TEMPLATE_VIS list
Howard Hinnant3e519522010-05-11 19:42:16 +0000804 : private __list_imp<_Tp, _Alloc>
805{
806 typedef __list_imp<_Tp, _Alloc> base;
807 typedef typename base::__node __node;
808 typedef typename base::__node_allocator __node_allocator;
809 typedef typename base::__node_pointer __node_pointer;
810 typedef typename base::__node_alloc_traits __node_alloc_traits;
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000811 typedef typename base::__node_base __node_base;
812 typedef typename base::__node_base_pointer __node_base_pointer;
Eric Fiselierb88ea352015-12-30 20:57:59 +0000813 typedef typename base::__link_pointer __link_pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000814
815public:
816 typedef _Tp value_type;
817 typedef _Alloc allocator_type;
818 static_assert((is_same<value_type, typename allocator_type::value_type>::value),
819 "Invalid allocator::value_type");
820 typedef value_type& reference;
821 typedef const value_type& const_reference;
822 typedef typename base::pointer pointer;
823 typedef typename base::const_pointer const_pointer;
824 typedef typename base::size_type size_type;
825 typedef typename base::difference_type difference_type;
826 typedef typename base::iterator iterator;
827 typedef typename base::const_iterator const_iterator;
Howard Hinnantce48a112011-06-30 21:18:19 +0000828 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
829 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnant3e519522010-05-11 19:42:16 +0000830
Howard Hinnant848a5372010-09-22 16:48:34 +0000831 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000832 list()
833 _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value)
Howard Hinnant920b56c2011-09-27 23:55:03 +0000834 {
835#if _LIBCPP_DEBUG_LEVEL >= 2
836 __get_db()->__insert_c(this);
837#endif
838 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000839 _LIBCPP_INLINE_VISIBILITY
Marshall Clowfb829762013-09-08 19:11:51 +0000840 explicit list(const allocator_type& __a) : base(__a)
Howard Hinnant920b56c2011-09-27 23:55:03 +0000841 {
842#if _LIBCPP_DEBUG_LEVEL >= 2
843 __get_db()->__insert_c(this);
844#endif
845 }
Marshall Clowfb829762013-09-08 19:11:51 +0000846 explicit list(size_type __n);
847#if _LIBCPP_STD_VER > 11
848 explicit list(size_type __n, const allocator_type& __a);
849#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000850 list(size_type __n, const value_type& __x);
851 list(size_type __n, const value_type& __x, const allocator_type& __a);
852 template <class _InpIter>
853 list(_InpIter __f, _InpIter __l,
854 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
855 template <class _InpIter>
856 list(_InpIter __f, _InpIter __l, const allocator_type& __a,
857 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
858
859 list(const list& __c);
860 list(const list& __c, const allocator_type& __a);
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +0000861 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000862 list& operator=(const list& __c);
Howard Hinnant54976f22011-08-12 21:56:02 +0000863#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant3e519522010-05-11 19:42:16 +0000864 list(initializer_list<value_type> __il);
865 list(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnant54976f22011-08-12 21:56:02 +0000866#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000867#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +0000868 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000869 list(list&& __c)
870 _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value);
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +0000871 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000872 list(list&& __c, const allocator_type& __a);
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +0000873 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000874 list& operator=(list&& __c)
875 _NOEXCEPT_(
876 __node_alloc_traits::propagate_on_container_move_assignment::value &&
877 is_nothrow_move_assignable<__node_allocator>::value);
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000878#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54976f22011-08-12 21:56:02 +0000879#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant848a5372010-09-22 16:48:34 +0000880 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000881 list& operator=(initializer_list<value_type> __il)
882 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnant54976f22011-08-12 21:56:02 +0000883#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant3e519522010-05-11 19:42:16 +0000884
885 template <class _InpIter>
886 void assign(_InpIter __f, _InpIter __l,
887 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
888 void assign(size_type __n, const value_type& __x);
Howard Hinnant54976f22011-08-12 21:56:02 +0000889#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant848a5372010-09-22 16:48:34 +0000890 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000891 void assign(initializer_list<value_type> __il)
892 {assign(__il.begin(), __il.end());}
Howard Hinnant54976f22011-08-12 21:56:02 +0000893#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant3e519522010-05-11 19:42:16 +0000894
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +0000895 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000896 allocator_type get_allocator() const _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +0000897
Howard Hinnant848a5372010-09-22 16:48:34 +0000898 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000899 size_type size() const _NOEXCEPT {return base::__sz();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000900 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000901 bool empty() const _NOEXCEPT {return base::empty();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000902 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000903 size_type max_size() const _NOEXCEPT
Eric Fiselier55b31b4e2016-11-23 01:18:56 +0000904 {
905 return std::min<size_type>(
906 base::__node_alloc_max_size(),
907 numeric_limits<difference_type >::max());
908 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000909
Howard Hinnant848a5372010-09-22 16:48:34 +0000910 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000911 iterator begin() _NOEXCEPT {return base::begin();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000912 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000913 const_iterator begin() const _NOEXCEPT {return base::begin();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000914 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000915 iterator end() _NOEXCEPT {return base::end();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000916 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000917 const_iterator end() const _NOEXCEPT {return base::end();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000918 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000919 const_iterator cbegin() const _NOEXCEPT {return base::begin();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000920 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000921 const_iterator cend() const _NOEXCEPT {return base::end();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000922
Howard Hinnant848a5372010-09-22 16:48:34 +0000923 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000924 reverse_iterator rbegin() _NOEXCEPT
925 {return reverse_iterator(end());}
Howard Hinnant848a5372010-09-22 16:48:34 +0000926 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000927 const_reverse_iterator rbegin() const _NOEXCEPT
928 {return const_reverse_iterator(end());}
Howard Hinnant848a5372010-09-22 16:48:34 +0000929 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000930 reverse_iterator rend() _NOEXCEPT
931 {return reverse_iterator(begin());}
Howard Hinnant848a5372010-09-22 16:48:34 +0000932 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000933 const_reverse_iterator rend() const _NOEXCEPT
934 {return const_reverse_iterator(begin());}
Howard Hinnant848a5372010-09-22 16:48:34 +0000935 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000936 const_reverse_iterator crbegin() const _NOEXCEPT
937 {return const_reverse_iterator(end());}
Howard Hinnant848a5372010-09-22 16:48:34 +0000938 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000939 const_reverse_iterator crend() const _NOEXCEPT
940 {return const_reverse_iterator(begin());}
Howard Hinnant3e519522010-05-11 19:42:16 +0000941
Howard Hinnant848a5372010-09-22 16:48:34 +0000942 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000943 reference front()
944 {
945 _LIBCPP_ASSERT(!empty(), "list::front called on empty list");
Eric Fiselierb88ea352015-12-30 20:57:59 +0000946 return base::__end_.__next_->__as_node()->__value_;
Howard Hinnant920b56c2011-09-27 23:55:03 +0000947 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000948 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000949 const_reference front() const
950 {
951 _LIBCPP_ASSERT(!empty(), "list::front called on empty list");
Eric Fiselierb88ea352015-12-30 20:57:59 +0000952 return base::__end_.__next_->__as_node()->__value_;
Howard Hinnant920b56c2011-09-27 23:55:03 +0000953 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000954 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000955 reference back()
956 {
957 _LIBCPP_ASSERT(!empty(), "list::back called on empty list");
Eric Fiselierb88ea352015-12-30 20:57:59 +0000958 return base::__end_.__prev_->__as_node()->__value_;
Howard Hinnant920b56c2011-09-27 23:55:03 +0000959 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000960 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000961 const_reference back() const
962 {
963 _LIBCPP_ASSERT(!empty(), "list::back called on empty list");
Eric Fiselierb88ea352015-12-30 20:57:59 +0000964 return base::__end_.__prev_->__as_node()->__value_;
Howard Hinnant920b56c2011-09-27 23:55:03 +0000965 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000966
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000967#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +0000968 void push_front(value_type&& __x);
969 void push_back(value_type&& __x);
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000970#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnant3e519522010-05-11 19:42:16 +0000971 template <class... _Args>
Eric Fiselier0e411642016-07-21 03:20:17 +0000972 reference emplace_front(_Args&&... __args);
Howard Hinnant3e519522010-05-11 19:42:16 +0000973 template <class... _Args>
Eric Fiselier0e411642016-07-21 03:20:17 +0000974 reference emplace_back(_Args&&... __args);
Howard Hinnant3e519522010-05-11 19:42:16 +0000975 template <class... _Args>
976 iterator emplace(const_iterator __p, _Args&&... __args);
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000977#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnant3e519522010-05-11 19:42:16 +0000978 iterator insert(const_iterator __p, value_type&& __x);
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000979#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +0000980
981 void push_front(const value_type& __x);
982 void push_back(const value_type& __x);
983
984 iterator insert(const_iterator __p, const value_type& __x);
985 iterator insert(const_iterator __p, size_type __n, const value_type& __x);
986 template <class _InpIter>
987 iterator insert(const_iterator __p, _InpIter __f, _InpIter __l,
988 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
Howard Hinnant54976f22011-08-12 21:56:02 +0000989#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant848a5372010-09-22 16:48:34 +0000990 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000991 iterator insert(const_iterator __p, initializer_list<value_type> __il)
992 {return insert(__p, __il.begin(), __il.end());}
Howard Hinnant54976f22011-08-12 21:56:02 +0000993#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant3e519522010-05-11 19:42:16 +0000994
Howard Hinnant848a5372010-09-22 16:48:34 +0000995 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000996 void swap(list& __c)
Marshall Clowe3fbe142015-07-13 20:04:56 +0000997#if _LIBCPP_STD_VER >= 14
Eric Fiselier2e519572016-12-28 06:06:09 +0000998 _NOEXCEPT_DEBUG
Marshall Clowe3fbe142015-07-13 20:04:56 +0000999#else
Eric Fiselier2e519572016-12-28 06:06:09 +00001000 _NOEXCEPT_DEBUG_(!__node_alloc_traits::propagate_on_container_swap::value ||
Howard Hinnant45900102011-06-03 17:30:28 +00001001 __is_nothrow_swappable<__node_allocator>::value)
Marshall Clowe3fbe142015-07-13 20:04:56 +00001002#endif
Howard Hinnant45900102011-06-03 17:30:28 +00001003 {base::swap(__c);}
Howard Hinnant848a5372010-09-22 16:48:34 +00001004 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +00001005 void clear() _NOEXCEPT {base::clear();}
Howard Hinnant3e519522010-05-11 19:42:16 +00001006
1007 void pop_front();
1008 void pop_back();
1009
1010 iterator erase(const_iterator __p);
1011 iterator erase(const_iterator __f, const_iterator __l);
1012
1013 void resize(size_type __n);
1014 void resize(size_type __n, const value_type& __x);
1015
1016 void splice(const_iterator __p, list& __c);
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001017#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant848a5372010-09-22 16:48:34 +00001018 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001019 void splice(const_iterator __p, list&& __c) {splice(__p, __c);}
1020#endif
1021 void splice(const_iterator __p, list& __c, const_iterator __i);
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001022#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant848a5372010-09-22 16:48:34 +00001023 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001024 void splice(const_iterator __p, list&& __c, const_iterator __i)
1025 {splice(__p, __c, __i);}
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001026#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +00001027 void splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l);
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001028#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant848a5372010-09-22 16:48:34 +00001029 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001030 void splice(const_iterator __p, list&& __c, const_iterator __f, const_iterator __l)
1031 {splice(__p, __c, __f, __l);}
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001032#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +00001033
1034 void remove(const value_type& __x);
1035 template <class _Pred> void remove_if(_Pred __pred);
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001036 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001037 void unique();
1038 template <class _BinaryPred>
1039 void unique(_BinaryPred __binary_pred);
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001040 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001041 void merge(list& __c);
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001042#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant848a5372010-09-22 16:48:34 +00001043 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001044 void merge(list&& __c) {merge(__c);}
1045#endif
1046 template <class _Comp>
1047 void merge(list& __c, _Comp __comp);
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001048#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +00001049 template <class _Comp>
Howard Hinnant848a5372010-09-22 16:48:34 +00001050 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001051 void merge(list&& __c, _Comp __comp) {merge(__c, __comp);}
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001052#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001053 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001054 void sort();
1055 template <class _Comp>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001056 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001057 void sort(_Comp __comp);
1058
Howard Hinnant45900102011-06-03 17:30:28 +00001059 void reverse() _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +00001060
Howard Hinnant920b56c2011-09-27 23:55:03 +00001061 bool __invariants() const;
1062
1063#if _LIBCPP_DEBUG_LEVEL >= 2
1064
1065 bool __dereferenceable(const const_iterator* __i) const;
1066 bool __decrementable(const const_iterator* __i) const;
1067 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1068 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1069
1070#endif // _LIBCPP_DEBUG_LEVEL >= 2
1071
Howard Hinnant3e519522010-05-11 19:42:16 +00001072private:
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001073 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierb88ea352015-12-30 20:57:59 +00001074 static void __link_nodes (__link_pointer __p, __link_pointer __f, __link_pointer __l);
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001075 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierb88ea352015-12-30 20:57:59 +00001076 void __link_nodes_at_front(__link_pointer __f, __link_pointer __l);
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001077 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierb88ea352015-12-30 20:57:59 +00001078 void __link_nodes_at_back (__link_pointer __f, __link_pointer __l);
Howard Hinnant3e519522010-05-11 19:42:16 +00001079 iterator __iterator(size_type __n);
1080 template <class _Comp>
1081 static iterator __sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp);
1082
Howard Hinnant45900102011-06-03 17:30:28 +00001083 void __move_assign(list& __c, true_type)
1084 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value);
Howard Hinnant3e519522010-05-11 19:42:16 +00001085 void __move_assign(list& __c, false_type);
1086};
1087
1088// Link in nodes [__f, __l] just prior to __p
1089template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001090inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001091void
Eric Fiselierb88ea352015-12-30 20:57:59 +00001092list<_Tp, _Alloc>::__link_nodes(__link_pointer __p, __link_pointer __f, __link_pointer __l)
Howard Hinnant3e519522010-05-11 19:42:16 +00001093{
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001094 __p->__prev_->__next_ = __f;
1095 __f->__prev_ = __p->__prev_;
1096 __p->__prev_ = __l;
1097 __l->__next_ = __p;
Howard Hinnant3e519522010-05-11 19:42:16 +00001098}
1099
Marshall Clow28d65da2014-08-05 01:34:12 +00001100// Link in nodes [__f, __l] at the front of the list
1101template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001102inline
Marshall Clow28d65da2014-08-05 01:34:12 +00001103void
Eric Fiselierb88ea352015-12-30 20:57:59 +00001104list<_Tp, _Alloc>::__link_nodes_at_front(__link_pointer __f, __link_pointer __l)
Marshall Clow28d65da2014-08-05 01:34:12 +00001105{
Eric Fiselier5243e192016-01-04 03:27:52 +00001106 __f->__prev_ = base::__end_as_link();
Marshall Clowced70062014-08-08 15:35:52 +00001107 __l->__next_ = base::__end_.__next_;
1108 __l->__next_->__prev_ = __l;
1109 base::__end_.__next_ = __f;
Marshall Clow28d65da2014-08-05 01:34:12 +00001110}
1111
1112// Link in nodes [__f, __l] at the front of the list
1113template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001114inline
Marshall Clow28d65da2014-08-05 01:34:12 +00001115void
Eric Fiselierb88ea352015-12-30 20:57:59 +00001116list<_Tp, _Alloc>::__link_nodes_at_back(__link_pointer __f, __link_pointer __l)
Marshall Clow28d65da2014-08-05 01:34:12 +00001117{
Eric Fiselier5243e192016-01-04 03:27:52 +00001118 __l->__next_ = base::__end_as_link();
Marshall Clowced70062014-08-08 15:35:52 +00001119 __f->__prev_ = base::__end_.__prev_;
1120 __f->__prev_->__next_ = __f;
1121 base::__end_.__prev_ = __l;
Marshall Clow28d65da2014-08-05 01:34:12 +00001122}
1123
1124
Howard Hinnant3e519522010-05-11 19:42:16 +00001125template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001126inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001127typename list<_Tp, _Alloc>::iterator
1128list<_Tp, _Alloc>::__iterator(size_type __n)
1129{
Howard Hinnantce48a112011-06-30 21:18:19 +00001130 return __n <= base::__sz() / 2 ? _VSTD::next(begin(), __n)
1131 : _VSTD::prev(end(), base::__sz() - __n);
Howard Hinnant3e519522010-05-11 19:42:16 +00001132}
1133
1134template <class _Tp, class _Alloc>
1135list<_Tp, _Alloc>::list(size_type __n)
1136{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001137#if _LIBCPP_DEBUG_LEVEL >= 2
1138 __get_db()->__insert_c(this);
1139#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001140 for (; __n > 0; --__n)
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001141#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +00001142 emplace_back();
1143#else
1144 push_back(value_type());
1145#endif
1146}
1147
Marshall Clowfb829762013-09-08 19:11:51 +00001148#if _LIBCPP_STD_VER > 11
1149template <class _Tp, class _Alloc>
1150list<_Tp, _Alloc>::list(size_type __n, const allocator_type& __a) : base(__a)
1151{
1152#if _LIBCPP_DEBUG_LEVEL >= 2
1153 __get_db()->__insert_c(this);
1154#endif
1155 for (; __n > 0; --__n)
1156#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1157 emplace_back();
1158#else
1159 push_back(value_type());
1160#endif
1161}
1162#endif
1163
Howard Hinnant3e519522010-05-11 19:42:16 +00001164template <class _Tp, class _Alloc>
1165list<_Tp, _Alloc>::list(size_type __n, const value_type& __x)
1166{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001167#if _LIBCPP_DEBUG_LEVEL >= 2
1168 __get_db()->__insert_c(this);
1169#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001170 for (; __n > 0; --__n)
1171 push_back(__x);
1172}
1173
1174template <class _Tp, class _Alloc>
1175list<_Tp, _Alloc>::list(size_type __n, const value_type& __x, const allocator_type& __a)
1176 : base(__a)
1177{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001178#if _LIBCPP_DEBUG_LEVEL >= 2
1179 __get_db()->__insert_c(this);
1180#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001181 for (; __n > 0; --__n)
1182 push_back(__x);
1183}
1184
1185template <class _Tp, class _Alloc>
1186template <class _InpIter>
1187list<_Tp, _Alloc>::list(_InpIter __f, _InpIter __l,
1188 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1189{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001190#if _LIBCPP_DEBUG_LEVEL >= 2
1191 __get_db()->__insert_c(this);
1192#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001193 for (; __f != __l; ++__f)
1194 push_back(*__f);
1195}
1196
1197template <class _Tp, class _Alloc>
1198template <class _InpIter>
1199list<_Tp, _Alloc>::list(_InpIter __f, _InpIter __l, const allocator_type& __a,
1200 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1201 : base(__a)
1202{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001203#if _LIBCPP_DEBUG_LEVEL >= 2
1204 __get_db()->__insert_c(this);
1205#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001206 for (; __f != __l; ++__f)
1207 push_back(*__f);
1208}
1209
1210template <class _Tp, class _Alloc>
1211list<_Tp, _Alloc>::list(const list& __c)
1212 : base(allocator_type(
1213 __node_alloc_traits::select_on_container_copy_construction(
1214 __c.__node_alloc())))
1215{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001216#if _LIBCPP_DEBUG_LEVEL >= 2
1217 __get_db()->__insert_c(this);
1218#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001219 for (const_iterator __i = __c.begin(), __e = __c.end(); __i != __e; ++__i)
1220 push_back(*__i);
1221}
1222
1223template <class _Tp, class _Alloc>
1224list<_Tp, _Alloc>::list(const list& __c, const allocator_type& __a)
1225 : base(__a)
1226{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001227#if _LIBCPP_DEBUG_LEVEL >= 2
1228 __get_db()->__insert_c(this);
1229#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001230 for (const_iterator __i = __c.begin(), __e = __c.end(); __i != __e; ++__i)
1231 push_back(*__i);
1232}
1233
Howard Hinnant54976f22011-08-12 21:56:02 +00001234#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1235
Howard Hinnant3e519522010-05-11 19:42:16 +00001236template <class _Tp, class _Alloc>
1237list<_Tp, _Alloc>::list(initializer_list<value_type> __il, const allocator_type& __a)
1238 : base(__a)
1239{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001240#if _LIBCPP_DEBUG_LEVEL >= 2
1241 __get_db()->__insert_c(this);
1242#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001243 for (typename initializer_list<value_type>::const_iterator __i = __il.begin(),
1244 __e = __il.end(); __i != __e; ++__i)
1245 push_back(*__i);
1246}
1247
1248template <class _Tp, class _Alloc>
1249list<_Tp, _Alloc>::list(initializer_list<value_type> __il)
1250{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001251#if _LIBCPP_DEBUG_LEVEL >= 2
1252 __get_db()->__insert_c(this);
1253#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001254 for (typename initializer_list<value_type>::const_iterator __i = __il.begin(),
1255 __e = __il.end(); __i != __e; ++__i)
1256 push_back(*__i);
1257}
1258
Howard Hinnant54976f22011-08-12 21:56:02 +00001259#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1260
Howard Hinnant3e519522010-05-11 19:42:16 +00001261template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001262inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001263list<_Tp, _Alloc>&
1264list<_Tp, _Alloc>::operator=(const list& __c)
1265{
1266 if (this != &__c)
1267 {
1268 base::__copy_assign_alloc(__c);
1269 assign(__c.begin(), __c.end());
1270 }
1271 return *this;
1272}
1273
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001274#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +00001275
1276template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001277inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001278list<_Tp, _Alloc>::list(list&& __c)
Howard Hinnant45900102011-06-03 17:30:28 +00001279 _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value)
Howard Hinnantce48a112011-06-30 21:18:19 +00001280 : base(allocator_type(_VSTD::move(__c.__node_alloc())))
Howard Hinnant3e519522010-05-11 19:42:16 +00001281{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001282#if _LIBCPP_DEBUG_LEVEL >= 2
1283 __get_db()->__insert_c(this);
1284#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001285 splice(end(), __c);
1286}
1287
1288template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001289inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001290list<_Tp, _Alloc>::list(list&& __c, const allocator_type& __a)
1291 : base(__a)
1292{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001293#if _LIBCPP_DEBUG_LEVEL >= 2
1294 __get_db()->__insert_c(this);
1295#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001296 if (__a == __c.get_allocator())
1297 splice(end(), __c);
1298 else
1299 {
Howard Hinnantc003db12011-11-29 18:15:50 +00001300 typedef move_iterator<iterator> _Ip;
1301 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnant3e519522010-05-11 19:42:16 +00001302 }
1303}
1304
1305template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001306inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001307list<_Tp, _Alloc>&
1308list<_Tp, _Alloc>::operator=(list&& __c)
Howard Hinnant45900102011-06-03 17:30:28 +00001309 _NOEXCEPT_(
1310 __node_alloc_traits::propagate_on_container_move_assignment::value &&
1311 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +00001312{
1313 __move_assign(__c, integral_constant<bool,
1314 __node_alloc_traits::propagate_on_container_move_assignment::value>());
1315 return *this;
1316}
1317
1318template <class _Tp, class _Alloc>
1319void
1320list<_Tp, _Alloc>::__move_assign(list& __c, false_type)
1321{
1322 if (base::__node_alloc() != __c.__node_alloc())
1323 {
Howard Hinnantc003db12011-11-29 18:15:50 +00001324 typedef move_iterator<iterator> _Ip;
1325 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnant3e519522010-05-11 19:42:16 +00001326 }
1327 else
1328 __move_assign(__c, true_type());
1329}
1330
1331template <class _Tp, class _Alloc>
1332void
1333list<_Tp, _Alloc>::__move_assign(list& __c, true_type)
Howard Hinnant45900102011-06-03 17:30:28 +00001334 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +00001335{
1336 clear();
1337 base::__move_assign_alloc(__c);
1338 splice(end(), __c);
1339}
1340
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001341#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +00001342
1343template <class _Tp, class _Alloc>
1344template <class _InpIter>
1345void
1346list<_Tp, _Alloc>::assign(_InpIter __f, _InpIter __l,
1347 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1348{
1349 iterator __i = begin();
1350 iterator __e = end();
1351 for (; __f != __l && __i != __e; ++__f, ++__i)
1352 *__i = *__f;
1353 if (__i == __e)
1354 insert(__e, __f, __l);
1355 else
1356 erase(__i, __e);
Eric Fiselier2e519572016-12-28 06:06:09 +00001357#if _LIBCPP_DEBUG_LEVEL >= 2
1358 __get_db()->__invalidate_all(this);
1359#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001360}
1361
1362template <class _Tp, class _Alloc>
1363void
1364list<_Tp, _Alloc>::assign(size_type __n, const value_type& __x)
1365{
1366 iterator __i = begin();
1367 iterator __e = end();
1368 for (; __n > 0 && __i != __e; --__n, ++__i)
1369 *__i = __x;
1370 if (__i == __e)
1371 insert(__e, __n, __x);
1372 else
1373 erase(__i, __e);
Eric Fiselier2e519572016-12-28 06:06:09 +00001374#if _LIBCPP_DEBUG_LEVEL >= 2
1375 __get_db()->__invalidate_all(this);
1376#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001377}
1378
1379template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001380inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001381_Alloc
Howard Hinnant45900102011-06-03 17:30:28 +00001382list<_Tp, _Alloc>::get_allocator() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001383{
1384 return allocator_type(base::__node_alloc());
1385}
1386
1387template <class _Tp, class _Alloc>
1388typename list<_Tp, _Alloc>::iterator
1389list<_Tp, _Alloc>::insert(const_iterator __p, const value_type& __x)
1390{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001391#if _LIBCPP_DEBUG_LEVEL >= 2
1392 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1393 "list::insert(iterator, x) called with an iterator not"
1394 " referring to this list");
1395#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001396 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001397 typedef __allocator_destructor<__node_allocator> _Dp;
1398 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant3e519522010-05-11 19:42:16 +00001399 __hold->__prev_ = 0;
Howard Hinnantce48a112011-06-30 21:18:19 +00001400 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001401 __link_nodes(__p.__ptr_, __hold->__as_link(), __hold->__as_link());
Howard Hinnant3e519522010-05-11 19:42:16 +00001402 ++base::__sz();
Howard Hinnantb0e4c9d2013-04-05 00:18:49 +00001403#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselierb88ea352015-12-30 20:57:59 +00001404 return iterator(__hold.release()->__as_link(), this);
Howard Hinnantb0e4c9d2013-04-05 00:18:49 +00001405#else
Eric Fiselierb88ea352015-12-30 20:57:59 +00001406 return iterator(__hold.release()->__as_link());
Howard Hinnantb0e4c9d2013-04-05 00:18:49 +00001407#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001408}
1409
1410template <class _Tp, class _Alloc>
1411typename list<_Tp, _Alloc>::iterator
1412list<_Tp, _Alloc>::insert(const_iterator __p, size_type __n, const value_type& __x)
1413{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001414#if _LIBCPP_DEBUG_LEVEL >= 2
1415 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1416 "list::insert(iterator, n, x) called with an iterator not"
1417 " referring to this list");
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001418 iterator __r(__p.__ptr_, this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001419#else
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001420 iterator __r(__p.__ptr_);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001421#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001422 if (__n > 0)
1423 {
1424 size_type __ds = 0;
1425 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001426 typedef __allocator_destructor<__node_allocator> _Dp;
1427 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant3e519522010-05-11 19:42:16 +00001428 __hold->__prev_ = 0;
Howard Hinnantce48a112011-06-30 21:18:19 +00001429 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnant3e519522010-05-11 19:42:16 +00001430 ++__ds;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001431#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselierb88ea352015-12-30 20:57:59 +00001432 __r = iterator(__hold->__as_link(), this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001433#else
Eric Fiselierb88ea352015-12-30 20:57:59 +00001434 __r = iterator(__hold->__as_link());
Howard Hinnant920b56c2011-09-27 23:55:03 +00001435#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001436 __hold.release();
1437 iterator __e = __r;
1438#ifndef _LIBCPP_NO_EXCEPTIONS
1439 try
1440 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001441#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001442 for (--__n; __n != 0; --__n, ++__e, ++__ds)
1443 {
1444 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001445 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001446 __e.__ptr_->__next_ = __hold->__as_link();
Howard Hinnant3e519522010-05-11 19:42:16 +00001447 __hold->__prev_ = __e.__ptr_;
1448 __hold.release();
1449 }
1450#ifndef _LIBCPP_NO_EXCEPTIONS
1451 }
1452 catch (...)
1453 {
1454 while (true)
1455 {
Howard Hinnantce48a112011-06-30 21:18:19 +00001456 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Eric Fiselierb88ea352015-12-30 20:57:59 +00001457 __link_pointer __prev = __e.__ptr_->__prev_;
1458 __node_alloc_traits::deallocate(__na, __e.__ptr_->__as_node(), 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001459 if (__prev == 0)
1460 break;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001461#if _LIBCPP_DEBUG_LEVEL >= 2
1462 __e = iterator(__prev, this);
1463#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001464 __e = iterator(__prev);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001465#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001466 }
1467 throw;
1468 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001469#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001470 __link_nodes(__p.__ptr_, __r.__ptr_, __e.__ptr_);
Howard Hinnant3e519522010-05-11 19:42:16 +00001471 base::__sz() += __ds;
1472 }
1473 return __r;
1474}
1475
1476template <class _Tp, class _Alloc>
1477template <class _InpIter>
1478typename list<_Tp, _Alloc>::iterator
1479list<_Tp, _Alloc>::insert(const_iterator __p, _InpIter __f, _InpIter __l,
1480 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1481{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001482#if _LIBCPP_DEBUG_LEVEL >= 2
1483 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1484 "list::insert(iterator, range) called with an iterator not"
1485 " referring to this list");
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001486 iterator __r(__p.__ptr_, this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001487#else
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001488 iterator __r(__p.__ptr_);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001489#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001490 if (__f != __l)
1491 {
1492 size_type __ds = 0;
1493 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001494 typedef __allocator_destructor<__node_allocator> _Dp;
1495 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant3e519522010-05-11 19:42:16 +00001496 __hold->__prev_ = 0;
Howard Hinnantce48a112011-06-30 21:18:19 +00001497 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), *__f);
Howard Hinnant3e519522010-05-11 19:42:16 +00001498 ++__ds;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001499#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselierb88ea352015-12-30 20:57:59 +00001500 __r = iterator(__hold.get()->__as_link(), this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001501#else
Eric Fiselierb88ea352015-12-30 20:57:59 +00001502 __r = iterator(__hold.get()->__as_link());
Howard Hinnant920b56c2011-09-27 23:55:03 +00001503#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001504 __hold.release();
1505 iterator __e = __r;
1506#ifndef _LIBCPP_NO_EXCEPTIONS
1507 try
1508 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001509#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiselier61bff612015-03-19 03:20:02 +00001510 for (++__f; __f != __l; ++__f, (void) ++__e, (void) ++__ds)
Howard Hinnant3e519522010-05-11 19:42:16 +00001511 {
1512 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001513 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), *__f);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001514 __e.__ptr_->__next_ = __hold.get()->__as_link();
Howard Hinnant3e519522010-05-11 19:42:16 +00001515 __hold->__prev_ = __e.__ptr_;
1516 __hold.release();
1517 }
1518#ifndef _LIBCPP_NO_EXCEPTIONS
1519 }
1520 catch (...)
1521 {
1522 while (true)
1523 {
Howard Hinnantce48a112011-06-30 21:18:19 +00001524 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Eric Fiselierb88ea352015-12-30 20:57:59 +00001525 __link_pointer __prev = __e.__ptr_->__prev_;
1526 __node_alloc_traits::deallocate(__na, __e.__ptr_->__as_node(), 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001527 if (__prev == 0)
1528 break;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001529#if _LIBCPP_DEBUG_LEVEL >= 2
1530 __e = iterator(__prev, this);
1531#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001532 __e = iterator(__prev);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001533#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001534 }
1535 throw;
1536 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001537#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001538 __link_nodes(__p.__ptr_, __r.__ptr_, __e.__ptr_);
Howard Hinnant3e519522010-05-11 19:42:16 +00001539 base::__sz() += __ds;
1540 }
1541 return __r;
1542}
1543
1544template <class _Tp, class _Alloc>
1545void
1546list<_Tp, _Alloc>::push_front(const value_type& __x)
1547{
1548 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001549 typedef __allocator_destructor<__node_allocator> _Dp;
1550 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001551 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001552 __link_pointer __nl = __hold->__as_link();
1553 __link_nodes_at_front(__nl, __nl);
Howard Hinnant3e519522010-05-11 19:42:16 +00001554 ++base::__sz();
1555 __hold.release();
1556}
1557
1558template <class _Tp, class _Alloc>
1559void
1560list<_Tp, _Alloc>::push_back(const value_type& __x)
1561{
1562 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001563 typedef __allocator_destructor<__node_allocator> _Dp;
1564 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001565 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001566 __link_nodes_at_back(__hold.get()->__as_link(), __hold.get()->__as_link());
Howard Hinnant3e519522010-05-11 19:42:16 +00001567 ++base::__sz();
1568 __hold.release();
1569}
1570
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001571#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +00001572
1573template <class _Tp, class _Alloc>
1574void
1575list<_Tp, _Alloc>::push_front(value_type&& __x)
1576{
1577 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001578 typedef __allocator_destructor<__node_allocator> _Dp;
1579 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001580 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
Eric Fiselierb88ea352015-12-30 20:57:59 +00001581 __link_nodes_at_front(__hold.get()->__as_link(), __hold.get()->__as_link());
Howard Hinnant3e519522010-05-11 19:42:16 +00001582 ++base::__sz();
1583 __hold.release();
1584}
1585
1586template <class _Tp, class _Alloc>
1587void
1588list<_Tp, _Alloc>::push_back(value_type&& __x)
1589{
1590 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001591 typedef __allocator_destructor<__node_allocator> _Dp;
1592 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001593 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
Eric Fiselierb88ea352015-12-30 20:57:59 +00001594 __link_nodes_at_back(__hold.get()->__as_link(), __hold.get()->__as_link());
Howard Hinnant3e519522010-05-11 19:42:16 +00001595 ++base::__sz();
1596 __hold.release();
1597}
1598
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001599#ifndef _LIBCPP_HAS_NO_VARIADICS
1600
Howard Hinnant3e519522010-05-11 19:42:16 +00001601template <class _Tp, class _Alloc>
1602template <class... _Args>
Eric Fiselier0e411642016-07-21 03:20:17 +00001603typename list<_Tp, _Alloc>::reference
Howard Hinnant3e519522010-05-11 19:42:16 +00001604list<_Tp, _Alloc>::emplace_front(_Args&&... __args)
1605{
1606 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001607 typedef __allocator_destructor<__node_allocator> _Dp;
1608 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001609 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001610 __link_nodes_at_front(__hold.get()->__as_link(), __hold.get()->__as_link());
Howard Hinnant3e519522010-05-11 19:42:16 +00001611 ++base::__sz();
Eric Fiselier0e411642016-07-21 03:20:17 +00001612 return __hold.release()->__value_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001613}
1614
1615template <class _Tp, class _Alloc>
1616template <class... _Args>
Eric Fiselier0e411642016-07-21 03:20:17 +00001617typename list<_Tp, _Alloc>::reference
Howard Hinnant3e519522010-05-11 19:42:16 +00001618list<_Tp, _Alloc>::emplace_back(_Args&&... __args)
1619{
1620 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001621 typedef __allocator_destructor<__node_allocator> _Dp;
1622 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001623 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001624 __link_pointer __nl = __hold->__as_link();
1625 __link_nodes_at_back(__nl, __nl);
Howard Hinnant3e519522010-05-11 19:42:16 +00001626 ++base::__sz();
Eric Fiselier0e411642016-07-21 03:20:17 +00001627 return __hold.release()->__value_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001628}
1629
1630template <class _Tp, class _Alloc>
1631template <class... _Args>
1632typename list<_Tp, _Alloc>::iterator
1633list<_Tp, _Alloc>::emplace(const_iterator __p, _Args&&... __args)
1634{
Howard Hinnantb0e4c9d2013-04-05 00:18:49 +00001635#if _LIBCPP_DEBUG_LEVEL >= 2
1636 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1637 "list::emplace(iterator, args...) called with an iterator not"
1638 " referring to this list");
1639#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001640 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001641 typedef __allocator_destructor<__node_allocator> _Dp;
1642 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant3e519522010-05-11 19:42:16 +00001643 __hold->__prev_ = 0;
Howard Hinnantce48a112011-06-30 21:18:19 +00001644 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001645 __link_pointer __nl = __hold.get()->__as_link();
1646 __link_nodes(__p.__ptr_, __nl, __nl);
Howard Hinnant3e519522010-05-11 19:42:16 +00001647 ++base::__sz();
Eric Fiselierb88ea352015-12-30 20:57:59 +00001648 __hold.release();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001649#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselierb88ea352015-12-30 20:57:59 +00001650 return iterator(__nl, this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001651#else
Eric Fiselierb88ea352015-12-30 20:57:59 +00001652 return iterator(__nl);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001653#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001654}
1655
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001656#endif // _LIBCPP_HAS_NO_VARIADICS
1657
Howard Hinnant3e519522010-05-11 19:42:16 +00001658template <class _Tp, class _Alloc>
1659typename list<_Tp, _Alloc>::iterator
1660list<_Tp, _Alloc>::insert(const_iterator __p, value_type&& __x)
1661{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001662#if _LIBCPP_DEBUG_LEVEL >= 2
1663 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1664 "list::insert(iterator, x) called with an iterator not"
1665 " referring to this list");
1666#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001667 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001668 typedef __allocator_destructor<__node_allocator> _Dp;
1669 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant3e519522010-05-11 19:42:16 +00001670 __hold->__prev_ = 0;
Howard Hinnantce48a112011-06-30 21:18:19 +00001671 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
Eric Fiselierb88ea352015-12-30 20:57:59 +00001672 __link_pointer __nl = __hold->__as_link();
1673 __link_nodes(__p.__ptr_, __nl, __nl);
Howard Hinnant3e519522010-05-11 19:42:16 +00001674 ++base::__sz();
Eric Fiselierb88ea352015-12-30 20:57:59 +00001675 __hold.release();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001676#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselierb88ea352015-12-30 20:57:59 +00001677 return iterator(__nl, this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001678#else
Eric Fiselierb88ea352015-12-30 20:57:59 +00001679 return iterator(__nl);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001680#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001681}
1682
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001683#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +00001684
1685template <class _Tp, class _Alloc>
1686void
1687list<_Tp, _Alloc>::pop_front()
1688{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001689 _LIBCPP_ASSERT(!empty(), "list::pop_front() called with empty list");
Howard Hinnant3e519522010-05-11 19:42:16 +00001690 __node_allocator& __na = base::__node_alloc();
Eric Fiselierb88ea352015-12-30 20:57:59 +00001691 __link_pointer __n = base::__end_.__next_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001692 base::__unlink_nodes(__n, __n);
1693 --base::__sz();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001694#if _LIBCPP_DEBUG_LEVEL >= 2
1695 __c_node* __c = __get_db()->__find_c_and_lock(this);
1696 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1697 {
1698 --__p;
1699 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001700 if (__i->__ptr_ == __n)
Howard Hinnant920b56c2011-09-27 23:55:03 +00001701 {
1702 (*__p)->__c_ = nullptr;
1703 if (--__c->end_ != __p)
1704 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1705 }
1706 }
1707 __get_db()->unlock();
1708#endif
Eric Fiselierb88ea352015-12-30 20:57:59 +00001709 __node_pointer __np = __n->__as_node();
1710 __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
1711 __node_alloc_traits::deallocate(__na, __np, 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001712}
1713
1714template <class _Tp, class _Alloc>
1715void
1716list<_Tp, _Alloc>::pop_back()
1717{
Dmitri Gribenkoc3f9c802013-06-24 06:15:57 +00001718 _LIBCPP_ASSERT(!empty(), "list::pop_back() called with empty list");
Howard Hinnant3e519522010-05-11 19:42:16 +00001719 __node_allocator& __na = base::__node_alloc();
Eric Fiselierb88ea352015-12-30 20:57:59 +00001720 __link_pointer __n = base::__end_.__prev_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001721 base::__unlink_nodes(__n, __n);
1722 --base::__sz();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001723#if _LIBCPP_DEBUG_LEVEL >= 2
1724 __c_node* __c = __get_db()->__find_c_and_lock(this);
1725 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1726 {
1727 --__p;
1728 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001729 if (__i->__ptr_ == __n)
Howard Hinnant920b56c2011-09-27 23:55:03 +00001730 {
1731 (*__p)->__c_ = nullptr;
1732 if (--__c->end_ != __p)
1733 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1734 }
1735 }
1736 __get_db()->unlock();
1737#endif
Eric Fiselierb88ea352015-12-30 20:57:59 +00001738 __node_pointer __np = __n->__as_node();
1739 __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
1740 __node_alloc_traits::deallocate(__na, __np, 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001741}
1742
1743template <class _Tp, class _Alloc>
1744typename list<_Tp, _Alloc>::iterator
1745list<_Tp, _Alloc>::erase(const_iterator __p)
1746{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001747#if _LIBCPP_DEBUG_LEVEL >= 2
1748 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1749 "list::erase(iterator) called with an iterator not"
1750 " referring to this list");
1751#endif
Howard Hinnantb0e4c9d2013-04-05 00:18:49 +00001752 _LIBCPP_ASSERT(__p != end(),
1753 "list::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnant3e519522010-05-11 19:42:16 +00001754 __node_allocator& __na = base::__node_alloc();
Eric Fiselierb88ea352015-12-30 20:57:59 +00001755 __link_pointer __n = __p.__ptr_;
1756 __link_pointer __r = __n->__next_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001757 base::__unlink_nodes(__n, __n);
1758 --base::__sz();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001759#if _LIBCPP_DEBUG_LEVEL >= 2
1760 __c_node* __c = __get_db()->__find_c_and_lock(this);
Eric Fiselier878e7e22016-10-23 19:26:39 +00001761 for (__i_node** __ip = __c->end_; __ip != __c->beg_; )
Howard Hinnant920b56c2011-09-27 23:55:03 +00001762 {
Eric Fiselier878e7e22016-10-23 19:26:39 +00001763 --__ip;
1764 iterator* __i = static_cast<iterator*>((*__ip)->__i_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001765 if (__i->__ptr_ == __n)
Howard Hinnant920b56c2011-09-27 23:55:03 +00001766 {
Eric Fiselier878e7e22016-10-23 19:26:39 +00001767 (*__ip)->__c_ = nullptr;
1768 if (--__c->end_ != __ip)
1769 memmove(__ip, __ip+1, (__c->end_ - __ip)*sizeof(__i_node*));
Howard Hinnant920b56c2011-09-27 23:55:03 +00001770 }
1771 }
1772 __get_db()->unlock();
1773#endif
Eric Fiselierb88ea352015-12-30 20:57:59 +00001774 __node_pointer __np = __n->__as_node();
1775 __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
1776 __node_alloc_traits::deallocate(__na, __np, 1);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001777#if _LIBCPP_DEBUG_LEVEL >= 2
1778 return iterator(__r, this);
1779#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001780 return iterator(__r);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001781#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001782}
1783
1784template <class _Tp, class _Alloc>
1785typename list<_Tp, _Alloc>::iterator
1786list<_Tp, _Alloc>::erase(const_iterator __f, const_iterator __l)
1787{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001788#if _LIBCPP_DEBUG_LEVEL >= 2
1789 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__f) == this,
1790 "list::erase(iterator, iterator) called with an iterator not"
1791 " referring to this list");
Eric Fiselier2e519572016-12-28 06:06:09 +00001792 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__l) == this,
1793 "list::erase(iterator, iterator) called with an iterator not"
1794 " referring to this list");
Howard Hinnant920b56c2011-09-27 23:55:03 +00001795#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001796 if (__f != __l)
1797 {
1798 __node_allocator& __na = base::__node_alloc();
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001799 base::__unlink_nodes(__f.__ptr_, __l.__ptr_->__prev_);
Howard Hinnant3e519522010-05-11 19:42:16 +00001800 while (__f != __l)
1801 {
Eric Fiselierb88ea352015-12-30 20:57:59 +00001802 __link_pointer __n = __f.__ptr_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001803 ++__f;
1804 --base::__sz();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001805#if _LIBCPP_DEBUG_LEVEL >= 2
1806 __c_node* __c = __get_db()->__find_c_and_lock(this);
1807 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1808 {
1809 --__p;
1810 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001811 if (__i->__ptr_ == __n)
Howard Hinnant920b56c2011-09-27 23:55:03 +00001812 {
1813 (*__p)->__c_ = nullptr;
1814 if (--__c->end_ != __p)
1815 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1816 }
1817 }
1818 __get_db()->unlock();
1819#endif
Eric Fiselierb88ea352015-12-30 20:57:59 +00001820 __node_pointer __np = __n->__as_node();
1821 __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
1822 __node_alloc_traits::deallocate(__na, __np, 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001823 }
1824 }
Howard Hinnant920b56c2011-09-27 23:55:03 +00001825#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001826 return iterator(__l.__ptr_, this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001827#else
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001828 return iterator(__l.__ptr_);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001829#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001830}
1831
1832template <class _Tp, class _Alloc>
1833void
1834list<_Tp, _Alloc>::resize(size_type __n)
1835{
1836 if (__n < base::__sz())
1837 erase(__iterator(__n), end());
1838 else if (__n > base::__sz())
1839 {
1840 __n -= base::__sz();
1841 size_type __ds = 0;
1842 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001843 typedef __allocator_destructor<__node_allocator> _Dp;
1844 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant3e519522010-05-11 19:42:16 +00001845 __hold->__prev_ = 0;
Howard Hinnantce48a112011-06-30 21:18:19 +00001846 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_));
Howard Hinnant3e519522010-05-11 19:42:16 +00001847 ++__ds;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001848#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselierb88ea352015-12-30 20:57:59 +00001849 iterator __r = iterator(__hold.release()->__as_link(), this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001850#else
Eric Fiselierb88ea352015-12-30 20:57:59 +00001851 iterator __r = iterator(__hold.release()->__as_link());
Howard Hinnant920b56c2011-09-27 23:55:03 +00001852#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001853 iterator __e = __r;
1854#ifndef _LIBCPP_NO_EXCEPTIONS
1855 try
1856 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001857#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001858 for (--__n; __n != 0; --__n, ++__e, ++__ds)
1859 {
1860 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001861 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_));
Eric Fiselierb88ea352015-12-30 20:57:59 +00001862 __e.__ptr_->__next_ = __hold.get()->__as_link();
Howard Hinnant3e519522010-05-11 19:42:16 +00001863 __hold->__prev_ = __e.__ptr_;
1864 __hold.release();
1865 }
1866#ifndef _LIBCPP_NO_EXCEPTIONS
1867 }
1868 catch (...)
1869 {
1870 while (true)
1871 {
Howard Hinnantce48a112011-06-30 21:18:19 +00001872 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Eric Fiselierb88ea352015-12-30 20:57:59 +00001873 __link_pointer __prev = __e.__ptr_->__prev_;
1874 __node_alloc_traits::deallocate(__na, __e.__ptr_->__as_node(), 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001875 if (__prev == 0)
1876 break;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001877#if _LIBCPP_DEBUG_LEVEL >= 2
1878 __e = iterator(__prev, this);
1879#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001880 __e = iterator(__prev);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001881#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001882 }
1883 throw;
1884 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001885#endif // _LIBCPP_NO_EXCEPTIONS
Marshall Clow28d65da2014-08-05 01:34:12 +00001886 __link_nodes_at_back(__r.__ptr_, __e.__ptr_);
Howard Hinnant3e519522010-05-11 19:42:16 +00001887 base::__sz() += __ds;
1888 }
1889}
1890
1891template <class _Tp, class _Alloc>
1892void
1893list<_Tp, _Alloc>::resize(size_type __n, const value_type& __x)
1894{
1895 if (__n < base::__sz())
1896 erase(__iterator(__n), end());
1897 else if (__n > base::__sz())
1898 {
1899 __n -= base::__sz();
1900 size_type __ds = 0;
1901 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001902 typedef __allocator_destructor<__node_allocator> _Dp;
1903 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant3e519522010-05-11 19:42:16 +00001904 __hold->__prev_ = 0;
Howard Hinnantce48a112011-06-30 21:18:19 +00001905 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnant3e519522010-05-11 19:42:16 +00001906 ++__ds;
Eric Fiselierb88ea352015-12-30 20:57:59 +00001907 __link_pointer __nl = __hold.release()->__as_link();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001908#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselierb88ea352015-12-30 20:57:59 +00001909 iterator __r = iterator(__nl, this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001910#else
Eric Fiselierb88ea352015-12-30 20:57:59 +00001911 iterator __r = iterator(__nl);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001912#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001913 iterator __e = __r;
1914#ifndef _LIBCPP_NO_EXCEPTIONS
1915 try
1916 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001917#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001918 for (--__n; __n != 0; --__n, ++__e, ++__ds)
1919 {
1920 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001921 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001922 __e.__ptr_->__next_ = __hold.get()->__as_link();
Howard Hinnant3e519522010-05-11 19:42:16 +00001923 __hold->__prev_ = __e.__ptr_;
1924 __hold.release();
1925 }
1926#ifndef _LIBCPP_NO_EXCEPTIONS
1927 }
1928 catch (...)
1929 {
1930 while (true)
1931 {
Howard Hinnantce48a112011-06-30 21:18:19 +00001932 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Eric Fiselierb88ea352015-12-30 20:57:59 +00001933 __link_pointer __prev = __e.__ptr_->__prev_;
1934 __node_alloc_traits::deallocate(__na, __e.__ptr_->__as_node(), 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001935 if (__prev == 0)
1936 break;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001937#if _LIBCPP_DEBUG_LEVEL >= 2
1938 __e = iterator(__prev, this);
1939#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001940 __e = iterator(__prev);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001941#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001942 }
1943 throw;
1944 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001945#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiselier5243e192016-01-04 03:27:52 +00001946 __link_nodes(base::__end_as_link(), __r.__ptr_, __e.__ptr_);
Howard Hinnant3e519522010-05-11 19:42:16 +00001947 base::__sz() += __ds;
Howard Hinnantb3371f62010-08-22 00:02:43 +00001948 }
Howard Hinnant3e519522010-05-11 19:42:16 +00001949}
1950
1951template <class _Tp, class _Alloc>
1952void
1953list<_Tp, _Alloc>::splice(const_iterator __p, list& __c)
1954{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001955 _LIBCPP_ASSERT(this != &__c,
1956 "list::splice(iterator, list) called with this == &list");
1957#if _LIBCPP_DEBUG_LEVEL >= 2
1958 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1959 "list::splice(iterator, list) called with an iterator not"
1960 " referring to this list");
1961#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001962 if (!__c.empty())
1963 {
Eric Fiselierb88ea352015-12-30 20:57:59 +00001964 __link_pointer __f = __c.__end_.__next_;
1965 __link_pointer __l = __c.__end_.__prev_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001966 base::__unlink_nodes(__f, __l);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001967 __link_nodes(__p.__ptr_, __f, __l);
Howard Hinnant3e519522010-05-11 19:42:16 +00001968 base::__sz() += __c.__sz();
1969 __c.__sz() = 0;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001970#if _LIBCPP_DEBUG_LEVEL >= 2
1971 __libcpp_db* __db = __get_db();
1972 __c_node* __cn1 = __db->__find_c_and_lock(this);
1973 __c_node* __cn2 = __db->__find_c(&__c);
Eric Fiselier878e7e22016-10-23 19:26:39 +00001974 for (__i_node** __ip = __cn2->end_; __ip != __cn2->beg_;)
Howard Hinnant920b56c2011-09-27 23:55:03 +00001975 {
Eric Fiselier878e7e22016-10-23 19:26:39 +00001976 --__ip;
1977 iterator* __i = static_cast<iterator*>((*__ip)->__i_);
Eric Fiselier5243e192016-01-04 03:27:52 +00001978 if (__i->__ptr_ != __c.__end_as_link())
Howard Hinnant920b56c2011-09-27 23:55:03 +00001979 {
Eric Fiselier878e7e22016-10-23 19:26:39 +00001980 __cn1->__add(*__ip);
1981 (*__ip)->__c_ = __cn1;
1982 if (--__cn2->end_ != __ip)
1983 memmove(__ip, __ip+1, (__cn2->end_ - __ip)*sizeof(__i_node*));
Howard Hinnant920b56c2011-09-27 23:55:03 +00001984 }
1985 }
1986 __db->unlock();
1987#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001988 }
1989}
1990
1991template <class _Tp, class _Alloc>
1992void
1993list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __i)
1994{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001995#if _LIBCPP_DEBUG_LEVEL >= 2
1996 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1997 "list::splice(iterator, list, iterator) called with first iterator not"
1998 " referring to this list");
1999 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__i) == &__c,
2000 "list::splice(iterator, list, iterator) called with second iterator not"
2001 " referring to list argument");
2002 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(&__i),
2003 "list::splice(iterator, list, iterator) called with second iterator not"
2004 " derefereceable");
2005#endif
2006 if (__p.__ptr_ != __i.__ptr_ && __p.__ptr_ != __i.__ptr_->__next_)
Howard Hinnant3e519522010-05-11 19:42:16 +00002007 {
Eric Fiselierb88ea352015-12-30 20:57:59 +00002008 __link_pointer __f = __i.__ptr_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002009 base::__unlink_nodes(__f, __f);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002010 __link_nodes(__p.__ptr_, __f, __f);
Howard Hinnant3e519522010-05-11 19:42:16 +00002011 --__c.__sz();
2012 ++base::__sz();
Howard Hinnant920b56c2011-09-27 23:55:03 +00002013#if _LIBCPP_DEBUG_LEVEL >= 2
2014 __libcpp_db* __db = __get_db();
2015 __c_node* __cn1 = __db->__find_c_and_lock(this);
2016 __c_node* __cn2 = __db->__find_c(&__c);
Eric Fiselier878e7e22016-10-23 19:26:39 +00002017 for (__i_node** __ip = __cn2->end_; __ip != __cn2->beg_;)
Howard Hinnant920b56c2011-09-27 23:55:03 +00002018 {
Eric Fiselier878e7e22016-10-23 19:26:39 +00002019 --__ip;
2020 iterator* __j = static_cast<iterator*>((*__ip)->__i_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002021 if (__j->__ptr_ == __f)
Howard Hinnant920b56c2011-09-27 23:55:03 +00002022 {
Eric Fiselier878e7e22016-10-23 19:26:39 +00002023 __cn1->__add(*__ip);
2024 (*__ip)->__c_ = __cn1;
2025 if (--__cn2->end_ != __ip)
2026 memmove(__ip, __ip+1, (__cn2->end_ - __ip)*sizeof(__i_node*));
Howard Hinnant920b56c2011-09-27 23:55:03 +00002027 }
2028 }
2029 __db->unlock();
2030#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002031 }
2032}
2033
2034template <class _Tp, class _Alloc>
2035void
2036list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l)
2037{
Howard Hinnant920b56c2011-09-27 23:55:03 +00002038#if _LIBCPP_DEBUG_LEVEL >= 2
2039 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2040 "list::splice(iterator, list, iterator, iterator) called with first iterator not"
2041 " referring to this list");
2042 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__f) == &__c,
2043 "list::splice(iterator, list, iterator, iterator) called with second iterator not"
2044 " referring to list argument");
2045 if (this == &__c)
2046 {
2047 for (const_iterator __i = __f; __i != __l; ++__i)
2048 _LIBCPP_ASSERT(__i != __p,
2049 "list::splice(iterator, list, iterator, iterator)"
2050 " called with the first iterator within the range"
2051 " of the second and third iterators");
2052 }
2053#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002054 if (__f != __l)
2055 {
2056 if (this != &__c)
2057 {
Howard Hinnantce48a112011-06-30 21:18:19 +00002058 size_type __s = _VSTD::distance(__f, __l);
Howard Hinnant3e519522010-05-11 19:42:16 +00002059 __c.__sz() -= __s;
2060 base::__sz() += __s;
2061 }
Eric Fiselierb88ea352015-12-30 20:57:59 +00002062 __link_pointer __first = __f.__ptr_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002063 --__l;
Eric Fiselierb88ea352015-12-30 20:57:59 +00002064 __link_pointer __last = __l.__ptr_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002065 base::__unlink_nodes(__first, __last);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002066 __link_nodes(__p.__ptr_, __first, __last);
Howard Hinnant920b56c2011-09-27 23:55:03 +00002067#if _LIBCPP_DEBUG_LEVEL >= 2
2068 __libcpp_db* __db = __get_db();
2069 __c_node* __cn1 = __db->__find_c_and_lock(this);
2070 __c_node* __cn2 = __db->__find_c(&__c);
Eric Fiselier878e7e22016-10-23 19:26:39 +00002071 for (__i_node** __ip = __cn2->end_; __ip != __cn2->beg_;)
Howard Hinnant920b56c2011-09-27 23:55:03 +00002072 {
Eric Fiselier878e7e22016-10-23 19:26:39 +00002073 --__ip;
2074 iterator* __j = static_cast<iterator*>((*__ip)->__i_);
Eric Fiselierb88ea352015-12-30 20:57:59 +00002075 for (__link_pointer __k = __f.__ptr_;
Howard Hinnant920b56c2011-09-27 23:55:03 +00002076 __k != __l.__ptr_; __k = __k->__next_)
2077 {
2078 if (__j->__ptr_ == __k)
2079 {
Eric Fiselier878e7e22016-10-23 19:26:39 +00002080 __cn1->__add(*__ip);
2081 (*__ip)->__c_ = __cn1;
2082 if (--__cn2->end_ != __ip)
2083 memmove(__ip, __ip+1, (__cn2->end_ - __ip)*sizeof(__i_node*));
Howard Hinnant920b56c2011-09-27 23:55:03 +00002084 }
2085 }
2086 }
2087 __db->unlock();
2088#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002089 }
2090}
2091
2092template <class _Tp, class _Alloc>
2093void
2094list<_Tp, _Alloc>::remove(const value_type& __x)
2095{
Eric Fiselier5cac7752016-12-14 22:48:38 +00002096 list<_Tp, _Alloc> __deleted_nodes(get_allocator()); // collect the nodes we're removing
Marshall Clowced70062014-08-08 15:35:52 +00002097 for (const_iterator __i = begin(), __e = end(); __i != __e;)
Howard Hinnant3e519522010-05-11 19:42:16 +00002098 {
2099 if (*__i == __x)
2100 {
Marshall Clowced70062014-08-08 15:35:52 +00002101 const_iterator __j = _VSTD::next(__i);
Howard Hinnant3e519522010-05-11 19:42:16 +00002102 for (; __j != __e && *__j == __x; ++__j)
2103 ;
Marshall Clowced70062014-08-08 15:35:52 +00002104 __deleted_nodes.splice(__deleted_nodes.end(), *this, __i, __j);
2105 __i = __j;
Marshall Clow90ba05332014-08-04 17:32:25 +00002106 if (__i != __e)
Marshall Clow28d65da2014-08-05 01:34:12 +00002107 ++__i;
Howard Hinnant3e519522010-05-11 19:42:16 +00002108 }
2109 else
2110 ++__i;
2111 }
2112}
2113
2114template <class _Tp, class _Alloc>
2115template <class _Pred>
2116void
2117list<_Tp, _Alloc>::remove_if(_Pred __pred)
2118{
2119 for (iterator __i = begin(), __e = end(); __i != __e;)
2120 {
2121 if (__pred(*__i))
2122 {
Howard Hinnantce48a112011-06-30 21:18:19 +00002123 iterator __j = _VSTD::next(__i);
Howard Hinnant3e519522010-05-11 19:42:16 +00002124 for (; __j != __e && __pred(*__j); ++__j)
2125 ;
2126 __i = erase(__i, __j);
Marshall Clow90ba05332014-08-04 17:32:25 +00002127 if (__i != __e)
Marshall Clow28d65da2014-08-05 01:34:12 +00002128 ++__i;
Howard Hinnant3e519522010-05-11 19:42:16 +00002129 }
2130 else
2131 ++__i;
2132 }
2133}
2134
2135template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00002136inline
Howard Hinnant3e519522010-05-11 19:42:16 +00002137void
2138list<_Tp, _Alloc>::unique()
2139{
2140 unique(__equal_to<value_type>());
2141}
2142
2143template <class _Tp, class _Alloc>
2144template <class _BinaryPred>
2145void
2146list<_Tp, _Alloc>::unique(_BinaryPred __binary_pred)
2147{
2148 for (iterator __i = begin(), __e = end(); __i != __e;)
2149 {
Howard Hinnantce48a112011-06-30 21:18:19 +00002150 iterator __j = _VSTD::next(__i);
Howard Hinnant3e519522010-05-11 19:42:16 +00002151 for (; __j != __e && __binary_pred(*__i, *__j); ++__j)
2152 ;
2153 if (++__i != __j)
2154 __i = erase(__i, __j);
2155 }
2156}
2157
2158template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00002159inline
Howard Hinnant3e519522010-05-11 19:42:16 +00002160void
2161list<_Tp, _Alloc>::merge(list& __c)
2162{
2163 merge(__c, __less<value_type>());
2164}
2165
2166template <class _Tp, class _Alloc>
2167template <class _Comp>
2168void
2169list<_Tp, _Alloc>::merge(list& __c, _Comp __comp)
2170{
2171 if (this != &__c)
2172 {
2173 iterator __f1 = begin();
2174 iterator __e1 = end();
2175 iterator __f2 = __c.begin();
2176 iterator __e2 = __c.end();
2177 while (__f1 != __e1 && __f2 != __e2)
2178 {
2179 if (__comp(*__f2, *__f1))
2180 {
2181 size_type __ds = 1;
Howard Hinnantce48a112011-06-30 21:18:19 +00002182 iterator __m2 = _VSTD::next(__f2);
Howard Hinnant3e519522010-05-11 19:42:16 +00002183 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2, ++__ds)
2184 ;
2185 base::__sz() += __ds;
2186 __c.__sz() -= __ds;
Eric Fiselierb88ea352015-12-30 20:57:59 +00002187 __link_pointer __f = __f2.__ptr_;
2188 __link_pointer __l = __m2.__ptr_->__prev_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002189 __f2 = __m2;
2190 base::__unlink_nodes(__f, __l);
Howard Hinnantce48a112011-06-30 21:18:19 +00002191 __m2 = _VSTD::next(__f1);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002192 __link_nodes(__f1.__ptr_, __f, __l);
Howard Hinnant3e519522010-05-11 19:42:16 +00002193 __f1 = __m2;
2194 }
2195 else
2196 ++__f1;
2197 }
2198 splice(__e1, __c);
Howard Hinnant920b56c2011-09-27 23:55:03 +00002199#if _LIBCPP_DEBUG_LEVEL >= 2
2200 __libcpp_db* __db = __get_db();
2201 __c_node* __cn1 = __db->__find_c_and_lock(this);
2202 __c_node* __cn2 = __db->__find_c(&__c);
2203 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
2204 {
2205 --__p;
2206 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Eric Fiselier5243e192016-01-04 03:27:52 +00002207 if (__i->__ptr_ != __c.__end_as_link())
Howard Hinnant920b56c2011-09-27 23:55:03 +00002208 {
2209 __cn1->__add(*__p);
2210 (*__p)->__c_ = __cn1;
2211 if (--__cn2->end_ != __p)
2212 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
2213 }
2214 }
2215 __db->unlock();
2216#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002217 }
2218}
2219
2220template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00002221inline
Howard Hinnant3e519522010-05-11 19:42:16 +00002222void
2223list<_Tp, _Alloc>::sort()
2224{
2225 sort(__less<value_type>());
2226}
2227
2228template <class _Tp, class _Alloc>
2229template <class _Comp>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00002230inline
Howard Hinnant3e519522010-05-11 19:42:16 +00002231void
2232list<_Tp, _Alloc>::sort(_Comp __comp)
2233{
2234 __sort(begin(), end(), base::__sz(), __comp);
2235}
2236
2237template <class _Tp, class _Alloc>
2238template <class _Comp>
Howard Hinnant3e519522010-05-11 19:42:16 +00002239typename list<_Tp, _Alloc>::iterator
2240list<_Tp, _Alloc>::__sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp)
2241{
2242 switch (__n)
2243 {
2244 case 0:
2245 case 1:
2246 return __f1;
2247 case 2:
2248 if (__comp(*--__e2, *__f1))
2249 {
Eric Fiselierb88ea352015-12-30 20:57:59 +00002250 __link_pointer __f = __e2.__ptr_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002251 base::__unlink_nodes(__f, __f);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002252 __link_nodes(__f1.__ptr_, __f, __f);
Howard Hinnant3e519522010-05-11 19:42:16 +00002253 return __e2;
2254 }
2255 return __f1;
2256 }
2257 size_type __n2 = __n / 2;
Howard Hinnantce48a112011-06-30 21:18:19 +00002258 iterator __e1 = _VSTD::next(__f1, __n2);
Howard Hinnant3e519522010-05-11 19:42:16 +00002259 iterator __r = __f1 = __sort(__f1, __e1, __n2, __comp);
2260 iterator __f2 = __e1 = __sort(__e1, __e2, __n - __n2, __comp);
2261 if (__comp(*__f2, *__f1))
2262 {
Howard Hinnantce48a112011-06-30 21:18:19 +00002263 iterator __m2 = _VSTD::next(__f2);
Howard Hinnant3e519522010-05-11 19:42:16 +00002264 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2)
2265 ;
Eric Fiselierb88ea352015-12-30 20:57:59 +00002266 __link_pointer __f = __f2.__ptr_;
2267 __link_pointer __l = __m2.__ptr_->__prev_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002268 __r = __f2;
2269 __e1 = __f2 = __m2;
2270 base::__unlink_nodes(__f, __l);
Howard Hinnantce48a112011-06-30 21:18:19 +00002271 __m2 = _VSTD::next(__f1);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002272 __link_nodes(__f1.__ptr_, __f, __l);
Howard Hinnant3e519522010-05-11 19:42:16 +00002273 __f1 = __m2;
2274 }
2275 else
2276 ++__f1;
2277 while (__f1 != __e1 && __f2 != __e2)
2278 {
2279 if (__comp(*__f2, *__f1))
2280 {
Howard Hinnantce48a112011-06-30 21:18:19 +00002281 iterator __m2 = _VSTD::next(__f2);
Howard Hinnant3e519522010-05-11 19:42:16 +00002282 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2)
2283 ;
Eric Fiselierb88ea352015-12-30 20:57:59 +00002284 __link_pointer __f = __f2.__ptr_;
2285 __link_pointer __l = __m2.__ptr_->__prev_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002286 if (__e1 == __f2)
2287 __e1 = __m2;
2288 __f2 = __m2;
2289 base::__unlink_nodes(__f, __l);
Howard Hinnantce48a112011-06-30 21:18:19 +00002290 __m2 = _VSTD::next(__f1);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002291 __link_nodes(__f1.__ptr_, __f, __l);
Howard Hinnant3e519522010-05-11 19:42:16 +00002292 __f1 = __m2;
2293 }
2294 else
2295 ++__f1;
2296 }
2297 return __r;
2298}
2299
2300template <class _Tp, class _Alloc>
2301void
Howard Hinnant45900102011-06-03 17:30:28 +00002302list<_Tp, _Alloc>::reverse() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00002303{
2304 if (base::__sz() > 1)
2305 {
2306 iterator __e = end();
Howard Hinnant920b56c2011-09-27 23:55:03 +00002307 for (iterator __i = begin(); __i.__ptr_ != __e.__ptr_;)
2308 {
Howard Hinnantce48a112011-06-30 21:18:19 +00002309 _VSTD::swap(__i.__ptr_->__prev_, __i.__ptr_->__next_);
Howard Hinnant920b56c2011-09-27 23:55:03 +00002310 __i.__ptr_ = __i.__ptr_->__prev_;
2311 }
Howard Hinnantce48a112011-06-30 21:18:19 +00002312 _VSTD::swap(__e.__ptr_->__prev_, __e.__ptr_->__next_);
Howard Hinnant3e519522010-05-11 19:42:16 +00002313 }
2314}
2315
2316template <class _Tp, class _Alloc>
Howard Hinnant920b56c2011-09-27 23:55:03 +00002317bool
2318list<_Tp, _Alloc>::__invariants() const
2319{
2320 return size() == _VSTD::distance(begin(), end());
2321}
2322
2323#if _LIBCPP_DEBUG_LEVEL >= 2
2324
2325template <class _Tp, class _Alloc>
2326bool
2327list<_Tp, _Alloc>::__dereferenceable(const const_iterator* __i) const
2328{
Eric Fiselier5243e192016-01-04 03:27:52 +00002329 return __i->__ptr_ != this->__end_as_link();
Howard Hinnant920b56c2011-09-27 23:55:03 +00002330}
2331
2332template <class _Tp, class _Alloc>
2333bool
2334list<_Tp, _Alloc>::__decrementable(const const_iterator* __i) const
2335{
2336 return !empty() && __i->__ptr_ != base::__end_.__next_;
2337}
2338
2339template <class _Tp, class _Alloc>
2340bool
Eric Fiselierfd838222016-12-23 23:37:52 +00002341list<_Tp, _Alloc>::__addable(const const_iterator*, ptrdiff_t) const
Howard Hinnant920b56c2011-09-27 23:55:03 +00002342{
2343 return false;
2344}
2345
2346template <class _Tp, class _Alloc>
2347bool
Eric Fiselierfd838222016-12-23 23:37:52 +00002348list<_Tp, _Alloc>::__subscriptable(const const_iterator*, ptrdiff_t) const
Howard Hinnant920b56c2011-09-27 23:55:03 +00002349{
2350 return false;
2351}
2352
2353#endif // _LIBCPP_DEBUG_LEVEL >= 2
2354
2355template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00002356inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00002357bool
2358operator==(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2359{
Howard Hinnantce48a112011-06-30 21:18:19 +00002360 return __x.size() == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnant3e519522010-05-11 19:42:16 +00002361}
2362
2363template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00002364inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00002365bool
2366operator< (const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2367{
Howard Hinnantce48a112011-06-30 21:18:19 +00002368 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnant3e519522010-05-11 19:42:16 +00002369}
2370
2371template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00002372inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00002373bool
2374operator!=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2375{
2376 return !(__x == __y);
2377}
2378
2379template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00002380inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00002381bool
2382operator> (const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2383{
2384 return __y < __x;
2385}
2386
2387template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00002388inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00002389bool
2390operator>=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2391{
2392 return !(__x < __y);
2393}
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{
2400 return !(__y < __x);
2401}
2402
2403template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00002404inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00002405void
2406swap(list<_Tp, _Alloc>& __x, list<_Tp, _Alloc>& __y)
Howard Hinnant45900102011-06-03 17:30:28 +00002407 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnant3e519522010-05-11 19:42:16 +00002408{
2409 __x.swap(__y);
2410}
2411
2412_LIBCPP_END_NAMESPACE_STD
2413
2414#endif // _LIBCPP_LIST