blob: fa148db516bc9933c2acb65eb8e30368661f3599 [file] [log] [blame]
Howard Hinnant3e519522010-05-11 19:42:16 +00001// -*- C++ -*-
2//===---------------------------- list ------------------------------------===//
3//
Howard Hinnant5b08a8a2010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnant3e519522010-05-11 19:42:16 +00005//
Howard Hinnant412dbeb2010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnant3e519522010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_LIST
12#define _LIBCPP_LIST
13
14/*
15 list synopsis
16
17namespace std
18{
19
20template <class T, class Alloc = allocator<T> >
21class list
22{
23public:
24
25 // types:
26 typedef T value_type;
27 typedef Alloc allocator_type;
28 typedef typename allocator_type::reference reference;
29 typedef typename allocator_type::const_reference const_reference;
30 typedef typename allocator_type::pointer pointer;
31 typedef typename allocator_type::const_pointer const_pointer;
32 typedef implementation-defined iterator;
33 typedef implementation-defined const_iterator;
34 typedef implementation-defined size_type;
35 typedef implementation-defined difference_type;
36 typedef reverse_iterator<iterator> reverse_iterator;
37 typedef reverse_iterator<const_iterator> const_reverse_iterator;
38
Howard Hinnant45900102011-06-03 17:30:28 +000039 list()
40 noexcept(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnant3e519522010-05-11 19:42:16 +000041 explicit list(const allocator_type& a);
42 explicit list(size_type n);
Marshall Clowf1b6d1b2013-09-09 18:19:45 +000043 explicit list(size_type n, const allocator_type& a); // C++14
Howard Hinnant3e519522010-05-11 19:42:16 +000044 list(size_type n, const value_type& value);
45 list(size_type n, const value_type& value, const allocator_type& a);
46 template <class Iter>
47 list(Iter first, Iter last);
48 template <class Iter>
49 list(Iter first, Iter last, const allocator_type& a);
50 list(const list& x);
51 list(const list&, const allocator_type& a);
Howard Hinnant45900102011-06-03 17:30:28 +000052 list(list&& x)
53 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnant3e519522010-05-11 19:42:16 +000054 list(list&&, const allocator_type& a);
55 list(initializer_list<value_type>);
56 list(initializer_list<value_type>, const allocator_type& a);
57
58 ~list();
59
60 list& operator=(const list& x);
Howard Hinnant45900102011-06-03 17:30:28 +000061 list& operator=(list&& x)
62 noexcept(
63 allocator_type::propagate_on_container_move_assignment::value &&
64 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant3e519522010-05-11 19:42:16 +000065 list& operator=(initializer_list<value_type>);
66 template <class Iter>
67 void assign(Iter first, Iter last);
68 void assign(size_type n, const value_type& t);
69 void assign(initializer_list<value_type>);
70
Howard Hinnant45900102011-06-03 17:30:28 +000071 allocator_type get_allocator() const noexcept;
Howard Hinnant3e519522010-05-11 19:42:16 +000072
Howard Hinnant45900102011-06-03 17:30:28 +000073 iterator begin() noexcept;
74 const_iterator begin() const noexcept;
75 iterator end() noexcept;
76 const_iterator end() const noexcept;
77 reverse_iterator rbegin() noexcept;
78 const_reverse_iterator rbegin() const noexcept;
79 reverse_iterator rend() noexcept;
80 const_reverse_iterator rend() const noexcept;
81 const_iterator cbegin() const noexcept;
82 const_iterator cend() const noexcept;
83 const_reverse_iterator crbegin() const noexcept;
84 const_reverse_iterator crend() const noexcept;
Howard Hinnant3e519522010-05-11 19:42:16 +000085
86 reference front();
87 const_reference front() const;
88 reference back();
89 const_reference back() const;
90
Howard Hinnant45900102011-06-03 17:30:28 +000091 bool empty() const noexcept;
92 size_type size() const noexcept;
93 size_type max_size() const noexcept;
Howard Hinnant3e519522010-05-11 19:42:16 +000094
95 template <class... Args>
Marshall Clow63b560b2017-01-24 23:09:12 +000096 reference emplace_front(Args&&... args); // reference in C++17
Howard Hinnant3e519522010-05-11 19:42:16 +000097 void pop_front();
98 template <class... Args>
Marshall Clow63b560b2017-01-24 23:09:12 +000099 reference emplace_back(Args&&... args); // reference in C++17
Howard Hinnant3e519522010-05-11 19:42:16 +0000100 void pop_back();
101 void push_front(const value_type& x);
102 void push_front(value_type&& x);
103 void push_back(const value_type& x);
104 void push_back(value_type&& x);
105 template <class... Args>
106 iterator emplace(const_iterator position, Args&&... args);
107 iterator insert(const_iterator position, const value_type& x);
108 iterator insert(const_iterator position, value_type&& x);
109 iterator insert(const_iterator position, size_type n, const value_type& x);
110 template <class Iter>
111 iterator insert(const_iterator position, Iter first, Iter last);
112 iterator insert(const_iterator position, initializer_list<value_type> il);
113
114 iterator erase(const_iterator position);
115 iterator erase(const_iterator position, const_iterator last);
116
117 void resize(size_type sz);
118 void resize(size_type sz, const value_type& c);
119
Howard Hinnant45900102011-06-03 17:30:28 +0000120 void swap(list&)
Marshall Clowe3fbe142015-07-13 20:04:56 +0000121 noexcept(allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnant45900102011-06-03 17:30:28 +0000122 void clear() noexcept;
Howard Hinnant3e519522010-05-11 19:42:16 +0000123
124 void splice(const_iterator position, list& x);
125 void splice(const_iterator position, list&& x);
126 void splice(const_iterator position, list& x, const_iterator i);
127 void splice(const_iterator position, list&& x, const_iterator i);
128 void splice(const_iterator position, list& x, const_iterator first,
129 const_iterator last);
130 void splice(const_iterator position, list&& x, const_iterator first,
131 const_iterator last);
132
133 void remove(const value_type& value);
134 template <class Pred> void remove_if(Pred pred);
135 void unique();
136 template <class BinaryPredicate>
137 void unique(BinaryPredicate binary_pred);
138 void merge(list& x);
139 void merge(list&& x);
140 template <class Compare>
141 void merge(list& x, Compare comp);
142 template <class Compare>
143 void merge(list&& x, Compare comp);
144 void sort();
145 template <class Compare>
146 void sort(Compare comp);
Howard Hinnant45900102011-06-03 17:30:28 +0000147 void reverse() noexcept;
Howard Hinnant3e519522010-05-11 19:42:16 +0000148};
149
150template <class T, class Alloc>
151 bool operator==(const list<T,Alloc>& x, const list<T,Alloc>& y);
152template <class T, class Alloc>
153 bool operator< (const list<T,Alloc>& x, const list<T,Alloc>& y);
154template <class T, class Alloc>
155 bool operator!=(const list<T,Alloc>& x, const list<T,Alloc>& y);
156template <class T, class Alloc>
157 bool operator> (const list<T,Alloc>& x, const list<T,Alloc>& y);
158template <class T, class Alloc>
159 bool operator>=(const list<T,Alloc>& x, const list<T,Alloc>& y);
160template <class T, class Alloc>
161 bool operator<=(const list<T,Alloc>& x, const list<T,Alloc>& y);
162
163template <class T, class Alloc>
Howard Hinnant45900102011-06-03 17:30:28 +0000164 void swap(list<T,Alloc>& x, list<T,Alloc>& y)
165 noexcept(noexcept(x.swap(y)));
Howard Hinnant3e519522010-05-11 19:42:16 +0000166
167} // std
168
169*/
170
171#include <__config>
172
173#include <memory>
174#include <limits>
175#include <initializer_list>
176#include <iterator>
177#include <algorithm>
Eric Fiselierb88ea352015-12-30 20:57:59 +0000178#include <type_traits>
Howard Hinnant3e519522010-05-11 19:42:16 +0000179
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>
Marshall Clow63b560b2017-01-24 23:09:12 +0000972#if _LIBCPP_STD_VER > 14
Eric Fiselier0e411642016-07-21 03:20:17 +0000973 reference emplace_front(_Args&&... __args);
Marshall Clow63b560b2017-01-24 23:09:12 +0000974#else
975 void emplace_front(_Args&&... __args);
976#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000977 template <class... _Args>
Marshall Clow63b560b2017-01-24 23:09:12 +0000978#if _LIBCPP_STD_VER > 14
Eric Fiselier0e411642016-07-21 03:20:17 +0000979 reference emplace_back(_Args&&... __args);
Marshall Clow63b560b2017-01-24 23:09:12 +0000980#else
981 void emplace_back(_Args&&... __args);
982#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000983 template <class... _Args>
984 iterator emplace(const_iterator __p, _Args&&... __args);
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000985#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnant3e519522010-05-11 19:42:16 +0000986 iterator insert(const_iterator __p, value_type&& __x);
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000987#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +0000988
989 void push_front(const value_type& __x);
990 void push_back(const value_type& __x);
991
992 iterator insert(const_iterator __p, const value_type& __x);
993 iterator insert(const_iterator __p, size_type __n, const value_type& __x);
994 template <class _InpIter>
995 iterator insert(const_iterator __p, _InpIter __f, _InpIter __l,
996 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
Howard Hinnant54976f22011-08-12 21:56:02 +0000997#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant848a5372010-09-22 16:48:34 +0000998 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000999 iterator insert(const_iterator __p, initializer_list<value_type> __il)
1000 {return insert(__p, __il.begin(), __il.end());}
Howard Hinnant54976f22011-08-12 21:56:02 +00001001#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant3e519522010-05-11 19:42:16 +00001002
Howard Hinnant848a5372010-09-22 16:48:34 +00001003 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +00001004 void swap(list& __c)
Marshall Clowe3fbe142015-07-13 20:04:56 +00001005#if _LIBCPP_STD_VER >= 14
Eric Fiselier2e519572016-12-28 06:06:09 +00001006 _NOEXCEPT_DEBUG
Marshall Clowe3fbe142015-07-13 20:04:56 +00001007#else
Eric Fiselier2e519572016-12-28 06:06:09 +00001008 _NOEXCEPT_DEBUG_(!__node_alloc_traits::propagate_on_container_swap::value ||
Howard Hinnant45900102011-06-03 17:30:28 +00001009 __is_nothrow_swappable<__node_allocator>::value)
Marshall Clowe3fbe142015-07-13 20:04:56 +00001010#endif
Howard Hinnant45900102011-06-03 17:30:28 +00001011 {base::swap(__c);}
Howard Hinnant848a5372010-09-22 16:48:34 +00001012 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +00001013 void clear() _NOEXCEPT {base::clear();}
Howard Hinnant3e519522010-05-11 19:42:16 +00001014
1015 void pop_front();
1016 void pop_back();
1017
1018 iterator erase(const_iterator __p);
1019 iterator erase(const_iterator __f, const_iterator __l);
1020
1021 void resize(size_type __n);
1022 void resize(size_type __n, const value_type& __x);
1023
1024 void splice(const_iterator __p, list& __c);
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001025#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant848a5372010-09-22 16:48:34 +00001026 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001027 void splice(const_iterator __p, list&& __c) {splice(__p, __c);}
1028#endif
1029 void splice(const_iterator __p, list& __c, const_iterator __i);
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001030#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant848a5372010-09-22 16:48:34 +00001031 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001032 void splice(const_iterator __p, list&& __c, const_iterator __i)
1033 {splice(__p, __c, __i);}
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001034#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +00001035 void splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l);
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001036#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant848a5372010-09-22 16:48:34 +00001037 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001038 void splice(const_iterator __p, list&& __c, const_iterator __f, const_iterator __l)
1039 {splice(__p, __c, __f, __l);}
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001040#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +00001041
1042 void remove(const value_type& __x);
1043 template <class _Pred> void remove_if(_Pred __pred);
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001044 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001045 void unique();
1046 template <class _BinaryPred>
1047 void unique(_BinaryPred __binary_pred);
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001048 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001049 void merge(list& __c);
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001050#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant848a5372010-09-22 16:48:34 +00001051 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001052 void merge(list&& __c) {merge(__c);}
1053#endif
1054 template <class _Comp>
1055 void merge(list& __c, _Comp __comp);
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001056#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +00001057 template <class _Comp>
Howard Hinnant848a5372010-09-22 16:48:34 +00001058 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001059 void merge(list&& __c, _Comp __comp) {merge(__c, __comp);}
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001060#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001061 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001062 void sort();
1063 template <class _Comp>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001064 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001065 void sort(_Comp __comp);
1066
Howard Hinnant45900102011-06-03 17:30:28 +00001067 void reverse() _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +00001068
Howard Hinnant920b56c2011-09-27 23:55:03 +00001069 bool __invariants() const;
1070
1071#if _LIBCPP_DEBUG_LEVEL >= 2
1072
1073 bool __dereferenceable(const const_iterator* __i) const;
1074 bool __decrementable(const const_iterator* __i) const;
1075 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1076 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1077
1078#endif // _LIBCPP_DEBUG_LEVEL >= 2
1079
Howard Hinnant3e519522010-05-11 19:42:16 +00001080private:
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001081 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierb88ea352015-12-30 20:57:59 +00001082 static void __link_nodes (__link_pointer __p, __link_pointer __f, __link_pointer __l);
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001083 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierb88ea352015-12-30 20:57:59 +00001084 void __link_nodes_at_front(__link_pointer __f, __link_pointer __l);
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001085 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierb88ea352015-12-30 20:57:59 +00001086 void __link_nodes_at_back (__link_pointer __f, __link_pointer __l);
Howard Hinnant3e519522010-05-11 19:42:16 +00001087 iterator __iterator(size_type __n);
1088 template <class _Comp>
1089 static iterator __sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp);
1090
Howard Hinnant45900102011-06-03 17:30:28 +00001091 void __move_assign(list& __c, true_type)
1092 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value);
Howard Hinnant3e519522010-05-11 19:42:16 +00001093 void __move_assign(list& __c, false_type);
1094};
1095
1096// Link in nodes [__f, __l] just prior to __p
1097template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001098inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001099void
Eric Fiselierb88ea352015-12-30 20:57:59 +00001100list<_Tp, _Alloc>::__link_nodes(__link_pointer __p, __link_pointer __f, __link_pointer __l)
Howard Hinnant3e519522010-05-11 19:42:16 +00001101{
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001102 __p->__prev_->__next_ = __f;
1103 __f->__prev_ = __p->__prev_;
1104 __p->__prev_ = __l;
1105 __l->__next_ = __p;
Howard Hinnant3e519522010-05-11 19:42:16 +00001106}
1107
Marshall Clow28d65da2014-08-05 01:34:12 +00001108// Link in nodes [__f, __l] at the front of the list
1109template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001110inline
Marshall Clow28d65da2014-08-05 01:34:12 +00001111void
Eric Fiselierb88ea352015-12-30 20:57:59 +00001112list<_Tp, _Alloc>::__link_nodes_at_front(__link_pointer __f, __link_pointer __l)
Marshall Clow28d65da2014-08-05 01:34:12 +00001113{
Eric Fiselier5243e192016-01-04 03:27:52 +00001114 __f->__prev_ = base::__end_as_link();
Marshall Clowced70062014-08-08 15:35:52 +00001115 __l->__next_ = base::__end_.__next_;
1116 __l->__next_->__prev_ = __l;
1117 base::__end_.__next_ = __f;
Marshall Clow28d65da2014-08-05 01:34:12 +00001118}
1119
1120// Link in nodes [__f, __l] at the front of the list
1121template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001122inline
Marshall Clow28d65da2014-08-05 01:34:12 +00001123void
Eric Fiselierb88ea352015-12-30 20:57:59 +00001124list<_Tp, _Alloc>::__link_nodes_at_back(__link_pointer __f, __link_pointer __l)
Marshall Clow28d65da2014-08-05 01:34:12 +00001125{
Eric Fiselier5243e192016-01-04 03:27:52 +00001126 __l->__next_ = base::__end_as_link();
Marshall Clowced70062014-08-08 15:35:52 +00001127 __f->__prev_ = base::__end_.__prev_;
1128 __f->__prev_->__next_ = __f;
1129 base::__end_.__prev_ = __l;
Marshall Clow28d65da2014-08-05 01:34:12 +00001130}
1131
1132
Howard Hinnant3e519522010-05-11 19:42:16 +00001133template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001134inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001135typename list<_Tp, _Alloc>::iterator
1136list<_Tp, _Alloc>::__iterator(size_type __n)
1137{
Howard Hinnantce48a112011-06-30 21:18:19 +00001138 return __n <= base::__sz() / 2 ? _VSTD::next(begin(), __n)
1139 : _VSTD::prev(end(), base::__sz() - __n);
Howard Hinnant3e519522010-05-11 19:42:16 +00001140}
1141
1142template <class _Tp, class _Alloc>
1143list<_Tp, _Alloc>::list(size_type __n)
1144{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001145#if _LIBCPP_DEBUG_LEVEL >= 2
1146 __get_db()->__insert_c(this);
1147#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001148 for (; __n > 0; --__n)
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001149#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +00001150 emplace_back();
1151#else
1152 push_back(value_type());
1153#endif
1154}
1155
Marshall Clowfb829762013-09-08 19:11:51 +00001156#if _LIBCPP_STD_VER > 11
1157template <class _Tp, class _Alloc>
1158list<_Tp, _Alloc>::list(size_type __n, const allocator_type& __a) : base(__a)
1159{
1160#if _LIBCPP_DEBUG_LEVEL >= 2
1161 __get_db()->__insert_c(this);
1162#endif
1163 for (; __n > 0; --__n)
1164#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1165 emplace_back();
1166#else
1167 push_back(value_type());
1168#endif
1169}
1170#endif
1171
Howard Hinnant3e519522010-05-11 19:42:16 +00001172template <class _Tp, class _Alloc>
1173list<_Tp, _Alloc>::list(size_type __n, const value_type& __x)
1174{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001175#if _LIBCPP_DEBUG_LEVEL >= 2
1176 __get_db()->__insert_c(this);
1177#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001178 for (; __n > 0; --__n)
1179 push_back(__x);
1180}
1181
1182template <class _Tp, class _Alloc>
1183list<_Tp, _Alloc>::list(size_type __n, const value_type& __x, const allocator_type& __a)
1184 : base(__a)
1185{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001186#if _LIBCPP_DEBUG_LEVEL >= 2
1187 __get_db()->__insert_c(this);
1188#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001189 for (; __n > 0; --__n)
1190 push_back(__x);
1191}
1192
1193template <class _Tp, class _Alloc>
1194template <class _InpIter>
1195list<_Tp, _Alloc>::list(_InpIter __f, _InpIter __l,
1196 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1197{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001198#if _LIBCPP_DEBUG_LEVEL >= 2
1199 __get_db()->__insert_c(this);
1200#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001201 for (; __f != __l; ++__f)
1202 push_back(*__f);
1203}
1204
1205template <class _Tp, class _Alloc>
1206template <class _InpIter>
1207list<_Tp, _Alloc>::list(_InpIter __f, _InpIter __l, const allocator_type& __a,
1208 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1209 : base(__a)
1210{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001211#if _LIBCPP_DEBUG_LEVEL >= 2
1212 __get_db()->__insert_c(this);
1213#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001214 for (; __f != __l; ++__f)
1215 push_back(*__f);
1216}
1217
1218template <class _Tp, class _Alloc>
1219list<_Tp, _Alloc>::list(const list& __c)
1220 : base(allocator_type(
1221 __node_alloc_traits::select_on_container_copy_construction(
1222 __c.__node_alloc())))
1223{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001224#if _LIBCPP_DEBUG_LEVEL >= 2
1225 __get_db()->__insert_c(this);
1226#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001227 for (const_iterator __i = __c.begin(), __e = __c.end(); __i != __e; ++__i)
1228 push_back(*__i);
1229}
1230
1231template <class _Tp, class _Alloc>
1232list<_Tp, _Alloc>::list(const list& __c, const allocator_type& __a)
1233 : base(__a)
1234{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001235#if _LIBCPP_DEBUG_LEVEL >= 2
1236 __get_db()->__insert_c(this);
1237#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001238 for (const_iterator __i = __c.begin(), __e = __c.end(); __i != __e; ++__i)
1239 push_back(*__i);
1240}
1241
Howard Hinnant54976f22011-08-12 21:56:02 +00001242#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1243
Howard Hinnant3e519522010-05-11 19:42:16 +00001244template <class _Tp, class _Alloc>
1245list<_Tp, _Alloc>::list(initializer_list<value_type> __il, const allocator_type& __a)
1246 : base(__a)
1247{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001248#if _LIBCPP_DEBUG_LEVEL >= 2
1249 __get_db()->__insert_c(this);
1250#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001251 for (typename initializer_list<value_type>::const_iterator __i = __il.begin(),
1252 __e = __il.end(); __i != __e; ++__i)
1253 push_back(*__i);
1254}
1255
1256template <class _Tp, class _Alloc>
1257list<_Tp, _Alloc>::list(initializer_list<value_type> __il)
1258{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001259#if _LIBCPP_DEBUG_LEVEL >= 2
1260 __get_db()->__insert_c(this);
1261#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001262 for (typename initializer_list<value_type>::const_iterator __i = __il.begin(),
1263 __e = __il.end(); __i != __e; ++__i)
1264 push_back(*__i);
1265}
1266
Howard Hinnant54976f22011-08-12 21:56:02 +00001267#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1268
Howard Hinnant3e519522010-05-11 19:42:16 +00001269template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001270inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001271list<_Tp, _Alloc>&
1272list<_Tp, _Alloc>::operator=(const list& __c)
1273{
1274 if (this != &__c)
1275 {
1276 base::__copy_assign_alloc(__c);
1277 assign(__c.begin(), __c.end());
1278 }
1279 return *this;
1280}
1281
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001282#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +00001283
1284template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001285inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001286list<_Tp, _Alloc>::list(list&& __c)
Howard Hinnant45900102011-06-03 17:30:28 +00001287 _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value)
Howard Hinnantce48a112011-06-30 21:18:19 +00001288 : base(allocator_type(_VSTD::move(__c.__node_alloc())))
Howard Hinnant3e519522010-05-11 19:42:16 +00001289{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001290#if _LIBCPP_DEBUG_LEVEL >= 2
1291 __get_db()->__insert_c(this);
1292#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001293 splice(end(), __c);
1294}
1295
1296template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001297inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001298list<_Tp, _Alloc>::list(list&& __c, const allocator_type& __a)
1299 : base(__a)
1300{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001301#if _LIBCPP_DEBUG_LEVEL >= 2
1302 __get_db()->__insert_c(this);
1303#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001304 if (__a == __c.get_allocator())
1305 splice(end(), __c);
1306 else
1307 {
Howard Hinnantc003db12011-11-29 18:15:50 +00001308 typedef move_iterator<iterator> _Ip;
1309 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnant3e519522010-05-11 19:42:16 +00001310 }
1311}
1312
1313template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001314inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001315list<_Tp, _Alloc>&
1316list<_Tp, _Alloc>::operator=(list&& __c)
Howard Hinnant45900102011-06-03 17:30:28 +00001317 _NOEXCEPT_(
1318 __node_alloc_traits::propagate_on_container_move_assignment::value &&
1319 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +00001320{
1321 __move_assign(__c, integral_constant<bool,
1322 __node_alloc_traits::propagate_on_container_move_assignment::value>());
1323 return *this;
1324}
1325
1326template <class _Tp, class _Alloc>
1327void
1328list<_Tp, _Alloc>::__move_assign(list& __c, false_type)
1329{
1330 if (base::__node_alloc() != __c.__node_alloc())
1331 {
Howard Hinnantc003db12011-11-29 18:15:50 +00001332 typedef move_iterator<iterator> _Ip;
1333 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnant3e519522010-05-11 19:42:16 +00001334 }
1335 else
1336 __move_assign(__c, true_type());
1337}
1338
1339template <class _Tp, class _Alloc>
1340void
1341list<_Tp, _Alloc>::__move_assign(list& __c, true_type)
Howard Hinnant45900102011-06-03 17:30:28 +00001342 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +00001343{
1344 clear();
1345 base::__move_assign_alloc(__c);
1346 splice(end(), __c);
1347}
1348
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001349#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +00001350
1351template <class _Tp, class _Alloc>
1352template <class _InpIter>
1353void
1354list<_Tp, _Alloc>::assign(_InpIter __f, _InpIter __l,
1355 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1356{
1357 iterator __i = begin();
1358 iterator __e = end();
1359 for (; __f != __l && __i != __e; ++__f, ++__i)
1360 *__i = *__f;
1361 if (__i == __e)
1362 insert(__e, __f, __l);
1363 else
1364 erase(__i, __e);
Eric Fiselier2e519572016-12-28 06:06:09 +00001365#if _LIBCPP_DEBUG_LEVEL >= 2
1366 __get_db()->__invalidate_all(this);
1367#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001368}
1369
1370template <class _Tp, class _Alloc>
1371void
1372list<_Tp, _Alloc>::assign(size_type __n, const value_type& __x)
1373{
1374 iterator __i = begin();
1375 iterator __e = end();
1376 for (; __n > 0 && __i != __e; --__n, ++__i)
1377 *__i = __x;
1378 if (__i == __e)
1379 insert(__e, __n, __x);
1380 else
1381 erase(__i, __e);
Eric Fiselier2e519572016-12-28 06:06:09 +00001382#if _LIBCPP_DEBUG_LEVEL >= 2
1383 __get_db()->__invalidate_all(this);
1384#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001385}
1386
1387template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001388inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001389_Alloc
Howard Hinnant45900102011-06-03 17:30:28 +00001390list<_Tp, _Alloc>::get_allocator() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001391{
1392 return allocator_type(base::__node_alloc());
1393}
1394
1395template <class _Tp, class _Alloc>
1396typename list<_Tp, _Alloc>::iterator
1397list<_Tp, _Alloc>::insert(const_iterator __p, const value_type& __x)
1398{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001399#if _LIBCPP_DEBUG_LEVEL >= 2
1400 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1401 "list::insert(iterator, x) called with an iterator not"
1402 " referring to this list");
1403#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001404 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001405 typedef __allocator_destructor<__node_allocator> _Dp;
1406 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant3e519522010-05-11 19:42:16 +00001407 __hold->__prev_ = 0;
Howard Hinnantce48a112011-06-30 21:18:19 +00001408 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001409 __link_nodes(__p.__ptr_, __hold->__as_link(), __hold->__as_link());
Howard Hinnant3e519522010-05-11 19:42:16 +00001410 ++base::__sz();
Howard Hinnantb0e4c9d2013-04-05 00:18:49 +00001411#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselierb88ea352015-12-30 20:57:59 +00001412 return iterator(__hold.release()->__as_link(), this);
Howard Hinnantb0e4c9d2013-04-05 00:18:49 +00001413#else
Eric Fiselierb88ea352015-12-30 20:57:59 +00001414 return iterator(__hold.release()->__as_link());
Howard Hinnantb0e4c9d2013-04-05 00:18:49 +00001415#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001416}
1417
1418template <class _Tp, class _Alloc>
1419typename list<_Tp, _Alloc>::iterator
1420list<_Tp, _Alloc>::insert(const_iterator __p, size_type __n, const value_type& __x)
1421{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001422#if _LIBCPP_DEBUG_LEVEL >= 2
1423 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1424 "list::insert(iterator, n, x) called with an iterator not"
1425 " referring to this list");
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001426 iterator __r(__p.__ptr_, this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001427#else
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001428 iterator __r(__p.__ptr_);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001429#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001430 if (__n > 0)
1431 {
1432 size_type __ds = 0;
1433 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001434 typedef __allocator_destructor<__node_allocator> _Dp;
1435 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant3e519522010-05-11 19:42:16 +00001436 __hold->__prev_ = 0;
Howard Hinnantce48a112011-06-30 21:18:19 +00001437 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnant3e519522010-05-11 19:42:16 +00001438 ++__ds;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001439#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselierb88ea352015-12-30 20:57:59 +00001440 __r = iterator(__hold->__as_link(), this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001441#else
Eric Fiselierb88ea352015-12-30 20:57:59 +00001442 __r = iterator(__hold->__as_link());
Howard Hinnant920b56c2011-09-27 23:55:03 +00001443#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001444 __hold.release();
1445 iterator __e = __r;
1446#ifndef _LIBCPP_NO_EXCEPTIONS
1447 try
1448 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001449#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001450 for (--__n; __n != 0; --__n, ++__e, ++__ds)
1451 {
1452 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001453 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001454 __e.__ptr_->__next_ = __hold->__as_link();
Howard Hinnant3e519522010-05-11 19:42:16 +00001455 __hold->__prev_ = __e.__ptr_;
1456 __hold.release();
1457 }
1458#ifndef _LIBCPP_NO_EXCEPTIONS
1459 }
1460 catch (...)
1461 {
1462 while (true)
1463 {
Howard Hinnantce48a112011-06-30 21:18:19 +00001464 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Eric Fiselierb88ea352015-12-30 20:57:59 +00001465 __link_pointer __prev = __e.__ptr_->__prev_;
1466 __node_alloc_traits::deallocate(__na, __e.__ptr_->__as_node(), 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001467 if (__prev == 0)
1468 break;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001469#if _LIBCPP_DEBUG_LEVEL >= 2
1470 __e = iterator(__prev, this);
1471#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001472 __e = iterator(__prev);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001473#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001474 }
1475 throw;
1476 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001477#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001478 __link_nodes(__p.__ptr_, __r.__ptr_, __e.__ptr_);
Howard Hinnant3e519522010-05-11 19:42:16 +00001479 base::__sz() += __ds;
1480 }
1481 return __r;
1482}
1483
1484template <class _Tp, class _Alloc>
1485template <class _InpIter>
1486typename list<_Tp, _Alloc>::iterator
1487list<_Tp, _Alloc>::insert(const_iterator __p, _InpIter __f, _InpIter __l,
1488 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1489{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001490#if _LIBCPP_DEBUG_LEVEL >= 2
1491 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1492 "list::insert(iterator, range) called with an iterator not"
1493 " referring to this list");
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001494 iterator __r(__p.__ptr_, this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001495#else
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001496 iterator __r(__p.__ptr_);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001497#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001498 if (__f != __l)
1499 {
1500 size_type __ds = 0;
1501 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001502 typedef __allocator_destructor<__node_allocator> _Dp;
1503 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant3e519522010-05-11 19:42:16 +00001504 __hold->__prev_ = 0;
Howard Hinnantce48a112011-06-30 21:18:19 +00001505 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), *__f);
Howard Hinnant3e519522010-05-11 19:42:16 +00001506 ++__ds;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001507#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselierb88ea352015-12-30 20:57:59 +00001508 __r = iterator(__hold.get()->__as_link(), this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001509#else
Eric Fiselierb88ea352015-12-30 20:57:59 +00001510 __r = iterator(__hold.get()->__as_link());
Howard Hinnant920b56c2011-09-27 23:55:03 +00001511#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001512 __hold.release();
1513 iterator __e = __r;
1514#ifndef _LIBCPP_NO_EXCEPTIONS
1515 try
1516 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001517#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiselier61bff612015-03-19 03:20:02 +00001518 for (++__f; __f != __l; ++__f, (void) ++__e, (void) ++__ds)
Howard Hinnant3e519522010-05-11 19:42:16 +00001519 {
1520 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001521 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), *__f);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001522 __e.__ptr_->__next_ = __hold.get()->__as_link();
Howard Hinnant3e519522010-05-11 19:42:16 +00001523 __hold->__prev_ = __e.__ptr_;
1524 __hold.release();
1525 }
1526#ifndef _LIBCPP_NO_EXCEPTIONS
1527 }
1528 catch (...)
1529 {
1530 while (true)
1531 {
Howard Hinnantce48a112011-06-30 21:18:19 +00001532 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Eric Fiselierb88ea352015-12-30 20:57:59 +00001533 __link_pointer __prev = __e.__ptr_->__prev_;
1534 __node_alloc_traits::deallocate(__na, __e.__ptr_->__as_node(), 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001535 if (__prev == 0)
1536 break;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001537#if _LIBCPP_DEBUG_LEVEL >= 2
1538 __e = iterator(__prev, this);
1539#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001540 __e = iterator(__prev);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001541#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001542 }
1543 throw;
1544 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001545#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001546 __link_nodes(__p.__ptr_, __r.__ptr_, __e.__ptr_);
Howard Hinnant3e519522010-05-11 19:42:16 +00001547 base::__sz() += __ds;
1548 }
1549 return __r;
1550}
1551
1552template <class _Tp, class _Alloc>
1553void
1554list<_Tp, _Alloc>::push_front(const value_type& __x)
1555{
1556 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001557 typedef __allocator_destructor<__node_allocator> _Dp;
1558 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001559 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001560 __link_pointer __nl = __hold->__as_link();
1561 __link_nodes_at_front(__nl, __nl);
Howard Hinnant3e519522010-05-11 19:42:16 +00001562 ++base::__sz();
1563 __hold.release();
1564}
1565
1566template <class _Tp, class _Alloc>
1567void
1568list<_Tp, _Alloc>::push_back(const value_type& __x)
1569{
1570 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001571 typedef __allocator_destructor<__node_allocator> _Dp;
1572 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001573 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001574 __link_nodes_at_back(__hold.get()->__as_link(), __hold.get()->__as_link());
Howard Hinnant3e519522010-05-11 19:42:16 +00001575 ++base::__sz();
1576 __hold.release();
1577}
1578
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001579#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +00001580
1581template <class _Tp, class _Alloc>
1582void
1583list<_Tp, _Alloc>::push_front(value_type&& __x)
1584{
1585 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001586 typedef __allocator_destructor<__node_allocator> _Dp;
1587 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001588 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
Eric Fiselierb88ea352015-12-30 20:57:59 +00001589 __link_nodes_at_front(__hold.get()->__as_link(), __hold.get()->__as_link());
Howard Hinnant3e519522010-05-11 19:42:16 +00001590 ++base::__sz();
1591 __hold.release();
1592}
1593
1594template <class _Tp, class _Alloc>
1595void
1596list<_Tp, _Alloc>::push_back(value_type&& __x)
1597{
1598 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001599 typedef __allocator_destructor<__node_allocator> _Dp;
1600 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001601 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
Eric Fiselierb88ea352015-12-30 20:57:59 +00001602 __link_nodes_at_back(__hold.get()->__as_link(), __hold.get()->__as_link());
Howard Hinnant3e519522010-05-11 19:42:16 +00001603 ++base::__sz();
1604 __hold.release();
1605}
1606
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001607#ifndef _LIBCPP_HAS_NO_VARIADICS
1608
Howard Hinnant3e519522010-05-11 19:42:16 +00001609template <class _Tp, class _Alloc>
1610template <class... _Args>
Marshall Clow63b560b2017-01-24 23:09:12 +00001611#if _LIBCPP_STD_VER > 14
Eric Fiselier0e411642016-07-21 03:20:17 +00001612typename list<_Tp, _Alloc>::reference
Marshall Clow63b560b2017-01-24 23:09:12 +00001613#else
1614void
1615#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001616list<_Tp, _Alloc>::emplace_front(_Args&&... __args)
1617{
1618 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001619 typedef __allocator_destructor<__node_allocator> _Dp;
1620 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001621 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001622 __link_nodes_at_front(__hold.get()->__as_link(), __hold.get()->__as_link());
Howard Hinnant3e519522010-05-11 19:42:16 +00001623 ++base::__sz();
Marshall Clow63b560b2017-01-24 23:09:12 +00001624#if _LIBCPP_STD_VER > 14
Eric Fiselier0e411642016-07-21 03:20:17 +00001625 return __hold.release()->__value_;
Marshall Clow63b560b2017-01-24 23:09:12 +00001626#else
1627 __hold.release();
1628#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001629}
1630
1631template <class _Tp, class _Alloc>
1632template <class... _Args>
Marshall Clow63b560b2017-01-24 23:09:12 +00001633#if _LIBCPP_STD_VER > 14
Eric Fiselier0e411642016-07-21 03:20:17 +00001634typename list<_Tp, _Alloc>::reference
Marshall Clow63b560b2017-01-24 23:09:12 +00001635#else
1636void
1637#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001638list<_Tp, _Alloc>::emplace_back(_Args&&... __args)
1639{
1640 __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 Hinnantce48a112011-06-30 21:18:19 +00001643 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001644 __link_pointer __nl = __hold->__as_link();
1645 __link_nodes_at_back(__nl, __nl);
Howard Hinnant3e519522010-05-11 19:42:16 +00001646 ++base::__sz();
Marshall Clow63b560b2017-01-24 23:09:12 +00001647#if _LIBCPP_STD_VER > 14
Eric Fiselier0e411642016-07-21 03:20:17 +00001648 return __hold.release()->__value_;
Marshall Clow63b560b2017-01-24 23:09:12 +00001649#else
1650 __hold.release();
1651#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001652}
1653
1654template <class _Tp, class _Alloc>
1655template <class... _Args>
1656typename list<_Tp, _Alloc>::iterator
1657list<_Tp, _Alloc>::emplace(const_iterator __p, _Args&&... __args)
1658{
Howard Hinnantb0e4c9d2013-04-05 00:18:49 +00001659#if _LIBCPP_DEBUG_LEVEL >= 2
1660 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1661 "list::emplace(iterator, args...) called with an iterator not"
1662 " referring to this list");
1663#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001664 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001665 typedef __allocator_destructor<__node_allocator> _Dp;
1666 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant3e519522010-05-11 19:42:16 +00001667 __hold->__prev_ = 0;
Howard Hinnantce48a112011-06-30 21:18:19 +00001668 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001669 __link_pointer __nl = __hold.get()->__as_link();
1670 __link_nodes(__p.__ptr_, __nl, __nl);
Howard Hinnant3e519522010-05-11 19:42:16 +00001671 ++base::__sz();
Eric Fiselierb88ea352015-12-30 20:57:59 +00001672 __hold.release();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001673#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselierb88ea352015-12-30 20:57:59 +00001674 return iterator(__nl, this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001675#else
Eric Fiselierb88ea352015-12-30 20:57:59 +00001676 return iterator(__nl);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001677#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001678}
1679
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001680#endif // _LIBCPP_HAS_NO_VARIADICS
1681
Howard Hinnant3e519522010-05-11 19:42:16 +00001682template <class _Tp, class _Alloc>
1683typename list<_Tp, _Alloc>::iterator
1684list<_Tp, _Alloc>::insert(const_iterator __p, value_type&& __x)
1685{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001686#if _LIBCPP_DEBUG_LEVEL >= 2
1687 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1688 "list::insert(iterator, x) called with an iterator not"
1689 " referring to this list");
1690#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001691 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001692 typedef __allocator_destructor<__node_allocator> _Dp;
1693 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant3e519522010-05-11 19:42:16 +00001694 __hold->__prev_ = 0;
Howard Hinnantce48a112011-06-30 21:18:19 +00001695 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
Eric Fiselierb88ea352015-12-30 20:57:59 +00001696 __link_pointer __nl = __hold->__as_link();
1697 __link_nodes(__p.__ptr_, __nl, __nl);
Howard Hinnant3e519522010-05-11 19:42:16 +00001698 ++base::__sz();
Eric Fiselierb88ea352015-12-30 20:57:59 +00001699 __hold.release();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001700#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselierb88ea352015-12-30 20:57:59 +00001701 return iterator(__nl, this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001702#else
Eric Fiselierb88ea352015-12-30 20:57:59 +00001703 return iterator(__nl);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001704#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001705}
1706
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001707#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +00001708
1709template <class _Tp, class _Alloc>
1710void
1711list<_Tp, _Alloc>::pop_front()
1712{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001713 _LIBCPP_ASSERT(!empty(), "list::pop_front() called with empty list");
Howard Hinnant3e519522010-05-11 19:42:16 +00001714 __node_allocator& __na = base::__node_alloc();
Eric Fiselierb88ea352015-12-30 20:57:59 +00001715 __link_pointer __n = base::__end_.__next_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001716 base::__unlink_nodes(__n, __n);
1717 --base::__sz();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001718#if _LIBCPP_DEBUG_LEVEL >= 2
1719 __c_node* __c = __get_db()->__find_c_and_lock(this);
1720 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1721 {
1722 --__p;
1723 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001724 if (__i->__ptr_ == __n)
Howard Hinnant920b56c2011-09-27 23:55:03 +00001725 {
1726 (*__p)->__c_ = nullptr;
1727 if (--__c->end_ != __p)
1728 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1729 }
1730 }
1731 __get_db()->unlock();
1732#endif
Eric Fiselierb88ea352015-12-30 20:57:59 +00001733 __node_pointer __np = __n->__as_node();
1734 __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
1735 __node_alloc_traits::deallocate(__na, __np, 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001736}
1737
1738template <class _Tp, class _Alloc>
1739void
1740list<_Tp, _Alloc>::pop_back()
1741{
Dmitri Gribenkoc3f9c802013-06-24 06:15:57 +00001742 _LIBCPP_ASSERT(!empty(), "list::pop_back() called with empty list");
Howard Hinnant3e519522010-05-11 19:42:16 +00001743 __node_allocator& __na = base::__node_alloc();
Eric Fiselierb88ea352015-12-30 20:57:59 +00001744 __link_pointer __n = base::__end_.__prev_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001745 base::__unlink_nodes(__n, __n);
1746 --base::__sz();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001747#if _LIBCPP_DEBUG_LEVEL >= 2
1748 __c_node* __c = __get_db()->__find_c_and_lock(this);
1749 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1750 {
1751 --__p;
1752 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001753 if (__i->__ptr_ == __n)
Howard Hinnant920b56c2011-09-27 23:55:03 +00001754 {
1755 (*__p)->__c_ = nullptr;
1756 if (--__c->end_ != __p)
1757 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1758 }
1759 }
1760 __get_db()->unlock();
1761#endif
Eric Fiselierb88ea352015-12-30 20:57:59 +00001762 __node_pointer __np = __n->__as_node();
1763 __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
1764 __node_alloc_traits::deallocate(__na, __np, 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001765}
1766
1767template <class _Tp, class _Alloc>
1768typename list<_Tp, _Alloc>::iterator
1769list<_Tp, _Alloc>::erase(const_iterator __p)
1770{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001771#if _LIBCPP_DEBUG_LEVEL >= 2
1772 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1773 "list::erase(iterator) called with an iterator not"
1774 " referring to this list");
1775#endif
Howard Hinnantb0e4c9d2013-04-05 00:18:49 +00001776 _LIBCPP_ASSERT(__p != end(),
1777 "list::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnant3e519522010-05-11 19:42:16 +00001778 __node_allocator& __na = base::__node_alloc();
Eric Fiselierb88ea352015-12-30 20:57:59 +00001779 __link_pointer __n = __p.__ptr_;
1780 __link_pointer __r = __n->__next_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001781 base::__unlink_nodes(__n, __n);
1782 --base::__sz();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001783#if _LIBCPP_DEBUG_LEVEL >= 2
1784 __c_node* __c = __get_db()->__find_c_and_lock(this);
Eric Fiselier878e7e22016-10-23 19:26:39 +00001785 for (__i_node** __ip = __c->end_; __ip != __c->beg_; )
Howard Hinnant920b56c2011-09-27 23:55:03 +00001786 {
Eric Fiselier878e7e22016-10-23 19:26:39 +00001787 --__ip;
1788 iterator* __i = static_cast<iterator*>((*__ip)->__i_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001789 if (__i->__ptr_ == __n)
Howard Hinnant920b56c2011-09-27 23:55:03 +00001790 {
Eric Fiselier878e7e22016-10-23 19:26:39 +00001791 (*__ip)->__c_ = nullptr;
1792 if (--__c->end_ != __ip)
1793 memmove(__ip, __ip+1, (__c->end_ - __ip)*sizeof(__i_node*));
Howard Hinnant920b56c2011-09-27 23:55:03 +00001794 }
1795 }
1796 __get_db()->unlock();
1797#endif
Eric Fiselierb88ea352015-12-30 20:57:59 +00001798 __node_pointer __np = __n->__as_node();
1799 __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
1800 __node_alloc_traits::deallocate(__na, __np, 1);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001801#if _LIBCPP_DEBUG_LEVEL >= 2
1802 return iterator(__r, this);
1803#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001804 return iterator(__r);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001805#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001806}
1807
1808template <class _Tp, class _Alloc>
1809typename list<_Tp, _Alloc>::iterator
1810list<_Tp, _Alloc>::erase(const_iterator __f, const_iterator __l)
1811{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001812#if _LIBCPP_DEBUG_LEVEL >= 2
1813 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__f) == this,
1814 "list::erase(iterator, iterator) called with an iterator not"
1815 " referring to this list");
Eric Fiselier2e519572016-12-28 06:06:09 +00001816 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__l) == this,
1817 "list::erase(iterator, iterator) called with an iterator not"
1818 " referring to this list");
Howard Hinnant920b56c2011-09-27 23:55:03 +00001819#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001820 if (__f != __l)
1821 {
1822 __node_allocator& __na = base::__node_alloc();
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001823 base::__unlink_nodes(__f.__ptr_, __l.__ptr_->__prev_);
Howard Hinnant3e519522010-05-11 19:42:16 +00001824 while (__f != __l)
1825 {
Eric Fiselierb88ea352015-12-30 20:57:59 +00001826 __link_pointer __n = __f.__ptr_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001827 ++__f;
1828 --base::__sz();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001829#if _LIBCPP_DEBUG_LEVEL >= 2
1830 __c_node* __c = __get_db()->__find_c_and_lock(this);
1831 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1832 {
1833 --__p;
1834 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001835 if (__i->__ptr_ == __n)
Howard Hinnant920b56c2011-09-27 23:55:03 +00001836 {
1837 (*__p)->__c_ = nullptr;
1838 if (--__c->end_ != __p)
1839 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1840 }
1841 }
1842 __get_db()->unlock();
1843#endif
Eric Fiselierb88ea352015-12-30 20:57:59 +00001844 __node_pointer __np = __n->__as_node();
1845 __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
1846 __node_alloc_traits::deallocate(__na, __np, 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001847 }
1848 }
Howard Hinnant920b56c2011-09-27 23:55:03 +00001849#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001850 return iterator(__l.__ptr_, this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001851#else
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001852 return iterator(__l.__ptr_);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001853#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001854}
1855
1856template <class _Tp, class _Alloc>
1857void
1858list<_Tp, _Alloc>::resize(size_type __n)
1859{
1860 if (__n < base::__sz())
1861 erase(__iterator(__n), end());
1862 else if (__n > base::__sz())
1863 {
1864 __n -= base::__sz();
1865 size_type __ds = 0;
1866 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001867 typedef __allocator_destructor<__node_allocator> _Dp;
1868 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant3e519522010-05-11 19:42:16 +00001869 __hold->__prev_ = 0;
Howard Hinnantce48a112011-06-30 21:18:19 +00001870 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_));
Howard Hinnant3e519522010-05-11 19:42:16 +00001871 ++__ds;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001872#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselierb88ea352015-12-30 20:57:59 +00001873 iterator __r = iterator(__hold.release()->__as_link(), this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001874#else
Eric Fiselierb88ea352015-12-30 20:57:59 +00001875 iterator __r = iterator(__hold.release()->__as_link());
Howard Hinnant920b56c2011-09-27 23:55:03 +00001876#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001877 iterator __e = __r;
1878#ifndef _LIBCPP_NO_EXCEPTIONS
1879 try
1880 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001881#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001882 for (--__n; __n != 0; --__n, ++__e, ++__ds)
1883 {
1884 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001885 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_));
Eric Fiselierb88ea352015-12-30 20:57:59 +00001886 __e.__ptr_->__next_ = __hold.get()->__as_link();
Howard Hinnant3e519522010-05-11 19:42:16 +00001887 __hold->__prev_ = __e.__ptr_;
1888 __hold.release();
1889 }
1890#ifndef _LIBCPP_NO_EXCEPTIONS
1891 }
1892 catch (...)
1893 {
1894 while (true)
1895 {
Howard Hinnantce48a112011-06-30 21:18:19 +00001896 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Eric Fiselierb88ea352015-12-30 20:57:59 +00001897 __link_pointer __prev = __e.__ptr_->__prev_;
1898 __node_alloc_traits::deallocate(__na, __e.__ptr_->__as_node(), 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001899 if (__prev == 0)
1900 break;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001901#if _LIBCPP_DEBUG_LEVEL >= 2
1902 __e = iterator(__prev, this);
1903#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001904 __e = iterator(__prev);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001905#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001906 }
1907 throw;
1908 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001909#endif // _LIBCPP_NO_EXCEPTIONS
Marshall Clow28d65da2014-08-05 01:34:12 +00001910 __link_nodes_at_back(__r.__ptr_, __e.__ptr_);
Howard Hinnant3e519522010-05-11 19:42:16 +00001911 base::__sz() += __ds;
1912 }
1913}
1914
1915template <class _Tp, class _Alloc>
1916void
1917list<_Tp, _Alloc>::resize(size_type __n, const value_type& __x)
1918{
1919 if (__n < base::__sz())
1920 erase(__iterator(__n), end());
1921 else if (__n > base::__sz())
1922 {
1923 __n -= base::__sz();
1924 size_type __ds = 0;
1925 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001926 typedef __allocator_destructor<__node_allocator> _Dp;
1927 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant3e519522010-05-11 19:42:16 +00001928 __hold->__prev_ = 0;
Howard Hinnantce48a112011-06-30 21:18:19 +00001929 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnant3e519522010-05-11 19:42:16 +00001930 ++__ds;
Eric Fiselierb88ea352015-12-30 20:57:59 +00001931 __link_pointer __nl = __hold.release()->__as_link();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001932#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselierb88ea352015-12-30 20:57:59 +00001933 iterator __r = iterator(__nl, this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001934#else
Eric Fiselierb88ea352015-12-30 20:57:59 +00001935 iterator __r = iterator(__nl);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001936#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001937 iterator __e = __r;
1938#ifndef _LIBCPP_NO_EXCEPTIONS
1939 try
1940 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001941#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001942 for (--__n; __n != 0; --__n, ++__e, ++__ds)
1943 {
1944 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001945 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001946 __e.__ptr_->__next_ = __hold.get()->__as_link();
Howard Hinnant3e519522010-05-11 19:42:16 +00001947 __hold->__prev_ = __e.__ptr_;
1948 __hold.release();
1949 }
1950#ifndef _LIBCPP_NO_EXCEPTIONS
1951 }
1952 catch (...)
1953 {
1954 while (true)
1955 {
Howard Hinnantce48a112011-06-30 21:18:19 +00001956 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Eric Fiselierb88ea352015-12-30 20:57:59 +00001957 __link_pointer __prev = __e.__ptr_->__prev_;
1958 __node_alloc_traits::deallocate(__na, __e.__ptr_->__as_node(), 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001959 if (__prev == 0)
1960 break;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001961#if _LIBCPP_DEBUG_LEVEL >= 2
1962 __e = iterator(__prev, this);
1963#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001964 __e = iterator(__prev);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001965#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001966 }
1967 throw;
1968 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001969#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiselier5243e192016-01-04 03:27:52 +00001970 __link_nodes(base::__end_as_link(), __r.__ptr_, __e.__ptr_);
Howard Hinnant3e519522010-05-11 19:42:16 +00001971 base::__sz() += __ds;
Howard Hinnantb3371f62010-08-22 00:02:43 +00001972 }
Howard Hinnant3e519522010-05-11 19:42:16 +00001973}
1974
1975template <class _Tp, class _Alloc>
1976void
1977list<_Tp, _Alloc>::splice(const_iterator __p, list& __c)
1978{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001979 _LIBCPP_ASSERT(this != &__c,
1980 "list::splice(iterator, list) called with this == &list");
1981#if _LIBCPP_DEBUG_LEVEL >= 2
1982 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1983 "list::splice(iterator, list) called with an iterator not"
1984 " referring to this list");
1985#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001986 if (!__c.empty())
1987 {
Eric Fiselierb88ea352015-12-30 20:57:59 +00001988 __link_pointer __f = __c.__end_.__next_;
1989 __link_pointer __l = __c.__end_.__prev_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001990 base::__unlink_nodes(__f, __l);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001991 __link_nodes(__p.__ptr_, __f, __l);
Howard Hinnant3e519522010-05-11 19:42:16 +00001992 base::__sz() += __c.__sz();
1993 __c.__sz() = 0;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001994#if _LIBCPP_DEBUG_LEVEL >= 2
1995 __libcpp_db* __db = __get_db();
1996 __c_node* __cn1 = __db->__find_c_and_lock(this);
1997 __c_node* __cn2 = __db->__find_c(&__c);
Eric Fiselier878e7e22016-10-23 19:26:39 +00001998 for (__i_node** __ip = __cn2->end_; __ip != __cn2->beg_;)
Howard Hinnant920b56c2011-09-27 23:55:03 +00001999 {
Eric Fiselier878e7e22016-10-23 19:26:39 +00002000 --__ip;
2001 iterator* __i = static_cast<iterator*>((*__ip)->__i_);
Eric Fiselier5243e192016-01-04 03:27:52 +00002002 if (__i->__ptr_ != __c.__end_as_link())
Howard Hinnant920b56c2011-09-27 23:55:03 +00002003 {
Eric Fiselier878e7e22016-10-23 19:26:39 +00002004 __cn1->__add(*__ip);
2005 (*__ip)->__c_ = __cn1;
2006 if (--__cn2->end_ != __ip)
2007 memmove(__ip, __ip+1, (__cn2->end_ - __ip)*sizeof(__i_node*));
Howard Hinnant920b56c2011-09-27 23:55:03 +00002008 }
2009 }
2010 __db->unlock();
2011#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002012 }
2013}
2014
2015template <class _Tp, class _Alloc>
2016void
2017list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __i)
2018{
Howard Hinnant920b56c2011-09-27 23:55:03 +00002019#if _LIBCPP_DEBUG_LEVEL >= 2
2020 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2021 "list::splice(iterator, list, iterator) called with first iterator not"
2022 " referring to this list");
2023 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__i) == &__c,
2024 "list::splice(iterator, list, iterator) called with second iterator not"
2025 " referring to list argument");
2026 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(&__i),
2027 "list::splice(iterator, list, iterator) called with second iterator not"
2028 " derefereceable");
2029#endif
2030 if (__p.__ptr_ != __i.__ptr_ && __p.__ptr_ != __i.__ptr_->__next_)
Howard Hinnant3e519522010-05-11 19:42:16 +00002031 {
Eric Fiselierb88ea352015-12-30 20:57:59 +00002032 __link_pointer __f = __i.__ptr_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002033 base::__unlink_nodes(__f, __f);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002034 __link_nodes(__p.__ptr_, __f, __f);
Howard Hinnant3e519522010-05-11 19:42:16 +00002035 --__c.__sz();
2036 ++base::__sz();
Howard Hinnant920b56c2011-09-27 23:55:03 +00002037#if _LIBCPP_DEBUG_LEVEL >= 2
2038 __libcpp_db* __db = __get_db();
2039 __c_node* __cn1 = __db->__find_c_and_lock(this);
2040 __c_node* __cn2 = __db->__find_c(&__c);
Eric Fiselier878e7e22016-10-23 19:26:39 +00002041 for (__i_node** __ip = __cn2->end_; __ip != __cn2->beg_;)
Howard Hinnant920b56c2011-09-27 23:55:03 +00002042 {
Eric Fiselier878e7e22016-10-23 19:26:39 +00002043 --__ip;
2044 iterator* __j = static_cast<iterator*>((*__ip)->__i_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002045 if (__j->__ptr_ == __f)
Howard Hinnant920b56c2011-09-27 23:55:03 +00002046 {
Eric Fiselier878e7e22016-10-23 19:26:39 +00002047 __cn1->__add(*__ip);
2048 (*__ip)->__c_ = __cn1;
2049 if (--__cn2->end_ != __ip)
2050 memmove(__ip, __ip+1, (__cn2->end_ - __ip)*sizeof(__i_node*));
Howard Hinnant920b56c2011-09-27 23:55:03 +00002051 }
2052 }
2053 __db->unlock();
2054#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002055 }
2056}
2057
2058template <class _Tp, class _Alloc>
2059void
2060list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l)
2061{
Howard Hinnant920b56c2011-09-27 23:55:03 +00002062#if _LIBCPP_DEBUG_LEVEL >= 2
2063 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2064 "list::splice(iterator, list, iterator, iterator) called with first iterator not"
2065 " referring to this list");
2066 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__f) == &__c,
2067 "list::splice(iterator, list, iterator, iterator) called with second iterator not"
2068 " referring to list argument");
2069 if (this == &__c)
2070 {
2071 for (const_iterator __i = __f; __i != __l; ++__i)
2072 _LIBCPP_ASSERT(__i != __p,
2073 "list::splice(iterator, list, iterator, iterator)"
2074 " called with the first iterator within the range"
2075 " of the second and third iterators");
2076 }
2077#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002078 if (__f != __l)
2079 {
2080 if (this != &__c)
2081 {
Howard Hinnantce48a112011-06-30 21:18:19 +00002082 size_type __s = _VSTD::distance(__f, __l);
Howard Hinnant3e519522010-05-11 19:42:16 +00002083 __c.__sz() -= __s;
2084 base::__sz() += __s;
2085 }
Eric Fiselierb88ea352015-12-30 20:57:59 +00002086 __link_pointer __first = __f.__ptr_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002087 --__l;
Eric Fiselierb88ea352015-12-30 20:57:59 +00002088 __link_pointer __last = __l.__ptr_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002089 base::__unlink_nodes(__first, __last);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002090 __link_nodes(__p.__ptr_, __first, __last);
Howard Hinnant920b56c2011-09-27 23:55:03 +00002091#if _LIBCPP_DEBUG_LEVEL >= 2
2092 __libcpp_db* __db = __get_db();
2093 __c_node* __cn1 = __db->__find_c_and_lock(this);
2094 __c_node* __cn2 = __db->__find_c(&__c);
Eric Fiselier878e7e22016-10-23 19:26:39 +00002095 for (__i_node** __ip = __cn2->end_; __ip != __cn2->beg_;)
Howard Hinnant920b56c2011-09-27 23:55:03 +00002096 {
Eric Fiselier878e7e22016-10-23 19:26:39 +00002097 --__ip;
2098 iterator* __j = static_cast<iterator*>((*__ip)->__i_);
Eric Fiselierb88ea352015-12-30 20:57:59 +00002099 for (__link_pointer __k = __f.__ptr_;
Howard Hinnant920b56c2011-09-27 23:55:03 +00002100 __k != __l.__ptr_; __k = __k->__next_)
2101 {
2102 if (__j->__ptr_ == __k)
2103 {
Eric Fiselier878e7e22016-10-23 19:26:39 +00002104 __cn1->__add(*__ip);
2105 (*__ip)->__c_ = __cn1;
2106 if (--__cn2->end_ != __ip)
2107 memmove(__ip, __ip+1, (__cn2->end_ - __ip)*sizeof(__i_node*));
Howard Hinnant920b56c2011-09-27 23:55:03 +00002108 }
2109 }
2110 }
2111 __db->unlock();
2112#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002113 }
2114}
2115
2116template <class _Tp, class _Alloc>
2117void
2118list<_Tp, _Alloc>::remove(const value_type& __x)
2119{
Eric Fiselier5cac7752016-12-14 22:48:38 +00002120 list<_Tp, _Alloc> __deleted_nodes(get_allocator()); // collect the nodes we're removing
Marshall Clowced70062014-08-08 15:35:52 +00002121 for (const_iterator __i = begin(), __e = end(); __i != __e;)
Howard Hinnant3e519522010-05-11 19:42:16 +00002122 {
2123 if (*__i == __x)
2124 {
Marshall Clowced70062014-08-08 15:35:52 +00002125 const_iterator __j = _VSTD::next(__i);
Howard Hinnant3e519522010-05-11 19:42:16 +00002126 for (; __j != __e && *__j == __x; ++__j)
2127 ;
Marshall Clowced70062014-08-08 15:35:52 +00002128 __deleted_nodes.splice(__deleted_nodes.end(), *this, __i, __j);
2129 __i = __j;
Marshall Clow90ba05332014-08-04 17:32:25 +00002130 if (__i != __e)
Marshall Clow28d65da2014-08-05 01:34:12 +00002131 ++__i;
Howard Hinnant3e519522010-05-11 19:42:16 +00002132 }
2133 else
2134 ++__i;
2135 }
2136}
2137
2138template <class _Tp, class _Alloc>
2139template <class _Pred>
2140void
2141list<_Tp, _Alloc>::remove_if(_Pred __pred)
2142{
2143 for (iterator __i = begin(), __e = end(); __i != __e;)
2144 {
2145 if (__pred(*__i))
2146 {
Howard Hinnantce48a112011-06-30 21:18:19 +00002147 iterator __j = _VSTD::next(__i);
Howard Hinnant3e519522010-05-11 19:42:16 +00002148 for (; __j != __e && __pred(*__j); ++__j)
2149 ;
2150 __i = erase(__i, __j);
Marshall Clow90ba05332014-08-04 17:32:25 +00002151 if (__i != __e)
Marshall Clow28d65da2014-08-05 01:34:12 +00002152 ++__i;
Howard Hinnant3e519522010-05-11 19:42:16 +00002153 }
2154 else
2155 ++__i;
2156 }
2157}
2158
2159template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00002160inline
Howard Hinnant3e519522010-05-11 19:42:16 +00002161void
2162list<_Tp, _Alloc>::unique()
2163{
2164 unique(__equal_to<value_type>());
2165}
2166
2167template <class _Tp, class _Alloc>
2168template <class _BinaryPred>
2169void
2170list<_Tp, _Alloc>::unique(_BinaryPred __binary_pred)
2171{
2172 for (iterator __i = begin(), __e = end(); __i != __e;)
2173 {
Howard Hinnantce48a112011-06-30 21:18:19 +00002174 iterator __j = _VSTD::next(__i);
Howard Hinnant3e519522010-05-11 19:42:16 +00002175 for (; __j != __e && __binary_pred(*__i, *__j); ++__j)
2176 ;
2177 if (++__i != __j)
2178 __i = erase(__i, __j);
2179 }
2180}
2181
2182template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00002183inline
Howard Hinnant3e519522010-05-11 19:42:16 +00002184void
2185list<_Tp, _Alloc>::merge(list& __c)
2186{
2187 merge(__c, __less<value_type>());
2188}
2189
2190template <class _Tp, class _Alloc>
2191template <class _Comp>
2192void
2193list<_Tp, _Alloc>::merge(list& __c, _Comp __comp)
2194{
2195 if (this != &__c)
2196 {
2197 iterator __f1 = begin();
2198 iterator __e1 = end();
2199 iterator __f2 = __c.begin();
2200 iterator __e2 = __c.end();
2201 while (__f1 != __e1 && __f2 != __e2)
2202 {
2203 if (__comp(*__f2, *__f1))
2204 {
2205 size_type __ds = 1;
Howard Hinnantce48a112011-06-30 21:18:19 +00002206 iterator __m2 = _VSTD::next(__f2);
Howard Hinnant3e519522010-05-11 19:42:16 +00002207 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2, ++__ds)
2208 ;
2209 base::__sz() += __ds;
2210 __c.__sz() -= __ds;
Eric Fiselierb88ea352015-12-30 20:57:59 +00002211 __link_pointer __f = __f2.__ptr_;
2212 __link_pointer __l = __m2.__ptr_->__prev_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002213 __f2 = __m2;
2214 base::__unlink_nodes(__f, __l);
Howard Hinnantce48a112011-06-30 21:18:19 +00002215 __m2 = _VSTD::next(__f1);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002216 __link_nodes(__f1.__ptr_, __f, __l);
Howard Hinnant3e519522010-05-11 19:42:16 +00002217 __f1 = __m2;
2218 }
2219 else
2220 ++__f1;
2221 }
2222 splice(__e1, __c);
Howard Hinnant920b56c2011-09-27 23:55:03 +00002223#if _LIBCPP_DEBUG_LEVEL >= 2
2224 __libcpp_db* __db = __get_db();
2225 __c_node* __cn1 = __db->__find_c_and_lock(this);
2226 __c_node* __cn2 = __db->__find_c(&__c);
2227 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
2228 {
2229 --__p;
2230 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Eric Fiselier5243e192016-01-04 03:27:52 +00002231 if (__i->__ptr_ != __c.__end_as_link())
Howard Hinnant920b56c2011-09-27 23:55:03 +00002232 {
2233 __cn1->__add(*__p);
2234 (*__p)->__c_ = __cn1;
2235 if (--__cn2->end_ != __p)
2236 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
2237 }
2238 }
2239 __db->unlock();
2240#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002241 }
2242}
2243
2244template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00002245inline
Howard Hinnant3e519522010-05-11 19:42:16 +00002246void
2247list<_Tp, _Alloc>::sort()
2248{
2249 sort(__less<value_type>());
2250}
2251
2252template <class _Tp, class _Alloc>
2253template <class _Comp>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00002254inline
Howard Hinnant3e519522010-05-11 19:42:16 +00002255void
2256list<_Tp, _Alloc>::sort(_Comp __comp)
2257{
2258 __sort(begin(), end(), base::__sz(), __comp);
2259}
2260
2261template <class _Tp, class _Alloc>
2262template <class _Comp>
Howard Hinnant3e519522010-05-11 19:42:16 +00002263typename list<_Tp, _Alloc>::iterator
2264list<_Tp, _Alloc>::__sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp)
2265{
2266 switch (__n)
2267 {
2268 case 0:
2269 case 1:
2270 return __f1;
2271 case 2:
2272 if (__comp(*--__e2, *__f1))
2273 {
Eric Fiselierb88ea352015-12-30 20:57:59 +00002274 __link_pointer __f = __e2.__ptr_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002275 base::__unlink_nodes(__f, __f);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002276 __link_nodes(__f1.__ptr_, __f, __f);
Howard Hinnant3e519522010-05-11 19:42:16 +00002277 return __e2;
2278 }
2279 return __f1;
2280 }
2281 size_type __n2 = __n / 2;
Howard Hinnantce48a112011-06-30 21:18:19 +00002282 iterator __e1 = _VSTD::next(__f1, __n2);
Howard Hinnant3e519522010-05-11 19:42:16 +00002283 iterator __r = __f1 = __sort(__f1, __e1, __n2, __comp);
2284 iterator __f2 = __e1 = __sort(__e1, __e2, __n - __n2, __comp);
2285 if (__comp(*__f2, *__f1))
2286 {
Howard Hinnantce48a112011-06-30 21:18:19 +00002287 iterator __m2 = _VSTD::next(__f2);
Howard Hinnant3e519522010-05-11 19:42:16 +00002288 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2)
2289 ;
Eric Fiselierb88ea352015-12-30 20:57:59 +00002290 __link_pointer __f = __f2.__ptr_;
2291 __link_pointer __l = __m2.__ptr_->__prev_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002292 __r = __f2;
2293 __e1 = __f2 = __m2;
2294 base::__unlink_nodes(__f, __l);
Howard Hinnantce48a112011-06-30 21:18:19 +00002295 __m2 = _VSTD::next(__f1);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002296 __link_nodes(__f1.__ptr_, __f, __l);
Howard Hinnant3e519522010-05-11 19:42:16 +00002297 __f1 = __m2;
2298 }
2299 else
2300 ++__f1;
2301 while (__f1 != __e1 && __f2 != __e2)
2302 {
2303 if (__comp(*__f2, *__f1))
2304 {
Howard Hinnantce48a112011-06-30 21:18:19 +00002305 iterator __m2 = _VSTD::next(__f2);
Howard Hinnant3e519522010-05-11 19:42:16 +00002306 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2)
2307 ;
Eric Fiselierb88ea352015-12-30 20:57:59 +00002308 __link_pointer __f = __f2.__ptr_;
2309 __link_pointer __l = __m2.__ptr_->__prev_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002310 if (__e1 == __f2)
2311 __e1 = __m2;
2312 __f2 = __m2;
2313 base::__unlink_nodes(__f, __l);
Howard Hinnantce48a112011-06-30 21:18:19 +00002314 __m2 = _VSTD::next(__f1);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002315 __link_nodes(__f1.__ptr_, __f, __l);
Howard Hinnant3e519522010-05-11 19:42:16 +00002316 __f1 = __m2;
2317 }
2318 else
2319 ++__f1;
2320 }
2321 return __r;
2322}
2323
2324template <class _Tp, class _Alloc>
2325void
Howard Hinnant45900102011-06-03 17:30:28 +00002326list<_Tp, _Alloc>::reverse() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00002327{
2328 if (base::__sz() > 1)
2329 {
2330 iterator __e = end();
Howard Hinnant920b56c2011-09-27 23:55:03 +00002331 for (iterator __i = begin(); __i.__ptr_ != __e.__ptr_;)
2332 {
Howard Hinnantce48a112011-06-30 21:18:19 +00002333 _VSTD::swap(__i.__ptr_->__prev_, __i.__ptr_->__next_);
Howard Hinnant920b56c2011-09-27 23:55:03 +00002334 __i.__ptr_ = __i.__ptr_->__prev_;
2335 }
Howard Hinnantce48a112011-06-30 21:18:19 +00002336 _VSTD::swap(__e.__ptr_->__prev_, __e.__ptr_->__next_);
Howard Hinnant3e519522010-05-11 19:42:16 +00002337 }
2338}
2339
2340template <class _Tp, class _Alloc>
Howard Hinnant920b56c2011-09-27 23:55:03 +00002341bool
2342list<_Tp, _Alloc>::__invariants() const
2343{
2344 return size() == _VSTD::distance(begin(), end());
2345}
2346
2347#if _LIBCPP_DEBUG_LEVEL >= 2
2348
2349template <class _Tp, class _Alloc>
2350bool
2351list<_Tp, _Alloc>::__dereferenceable(const const_iterator* __i) const
2352{
Eric Fiselier5243e192016-01-04 03:27:52 +00002353 return __i->__ptr_ != this->__end_as_link();
Howard Hinnant920b56c2011-09-27 23:55:03 +00002354}
2355
2356template <class _Tp, class _Alloc>
2357bool
2358list<_Tp, _Alloc>::__decrementable(const const_iterator* __i) const
2359{
2360 return !empty() && __i->__ptr_ != base::__end_.__next_;
2361}
2362
2363template <class _Tp, class _Alloc>
2364bool
Eric Fiselierfd838222016-12-23 23:37:52 +00002365list<_Tp, _Alloc>::__addable(const const_iterator*, ptrdiff_t) const
Howard Hinnant920b56c2011-09-27 23:55:03 +00002366{
2367 return false;
2368}
2369
2370template <class _Tp, class _Alloc>
2371bool
Eric Fiselierfd838222016-12-23 23:37:52 +00002372list<_Tp, _Alloc>::__subscriptable(const const_iterator*, ptrdiff_t) const
Howard Hinnant920b56c2011-09-27 23:55:03 +00002373{
2374 return false;
2375}
2376
2377#endif // _LIBCPP_DEBUG_LEVEL >= 2
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{
Howard Hinnantce48a112011-06-30 21:18:19 +00002384 return __x.size() == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnant3e519522010-05-11 19:42:16 +00002385}
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{
Howard Hinnantce48a112011-06-30 21:18:19 +00002392 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnant3e519522010-05-11 19:42:16 +00002393}
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 !(__x == __y);
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 +00002405bool
2406operator> (const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2407{
2408 return __y < __x;
2409}
2410
2411template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00002412inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00002413bool
2414operator>=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2415{
2416 return !(__x < __y);
2417}
2418
2419template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00002420inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00002421bool
2422operator<=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2423{
2424 return !(__y < __x);
2425}
2426
2427template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00002428inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00002429void
2430swap(list<_Tp, _Alloc>& __x, list<_Tp, _Alloc>& __y)
Howard Hinnant45900102011-06-03 17:30:28 +00002431 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnant3e519522010-05-11 19:42:16 +00002432{
2433 __x.swap(__y);
2434}
2435
2436_LIBCPP_END_NAMESPACE_STD
2437
2438#endif // _LIBCPP_LIST