blob: e44bcf26a4242252b61f4dc9932bbeff317d55a1 [file] [log] [blame]
Howard Hinnant3e519522010-05-11 19:42:16 +00001// -*- C++ -*-
2//===---------------------------- list ------------------------------------===//
3//
Howard Hinnant5b08a8a2010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnant3e519522010-05-11 19:42:16 +00005//
Howard Hinnant412dbeb2010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnant3e519522010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_LIST
12#define _LIBCPP_LIST
13
14/*
15 list synopsis
16
17namespace std
18{
19
20template <class T, class Alloc = allocator<T> >
21class list
22{
23public:
24
25 // types:
26 typedef T value_type;
27 typedef Alloc allocator_type;
28 typedef typename allocator_type::reference reference;
29 typedef typename allocator_type::const_reference const_reference;
30 typedef typename allocator_type::pointer pointer;
31 typedef typename allocator_type::const_pointer const_pointer;
32 typedef implementation-defined iterator;
33 typedef implementation-defined const_iterator;
34 typedef implementation-defined size_type;
35 typedef implementation-defined difference_type;
36 typedef reverse_iterator<iterator> reverse_iterator;
37 typedef reverse_iterator<const_iterator> const_reverse_iterator;
38
Howard Hinnant45900102011-06-03 17:30:28 +000039 list()
40 noexcept(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnant3e519522010-05-11 19:42:16 +000041 explicit list(const allocator_type& a);
42 explicit list(size_type n);
Marshall Clowf1b6d1b2013-09-09 18:19:45 +000043 explicit list(size_type n, const allocator_type& a); // C++14
Howard Hinnant3e519522010-05-11 19:42:16 +000044 list(size_type n, const value_type& value);
45 list(size_type n, const value_type& value, const allocator_type& a);
46 template <class Iter>
47 list(Iter first, Iter last);
48 template <class Iter>
49 list(Iter first, Iter last, const allocator_type& a);
50 list(const list& x);
51 list(const list&, const allocator_type& a);
Howard Hinnant45900102011-06-03 17:30:28 +000052 list(list&& x)
53 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnant3e519522010-05-11 19:42:16 +000054 list(list&&, const allocator_type& a);
55 list(initializer_list<value_type>);
56 list(initializer_list<value_type>, const allocator_type& a);
57
58 ~list();
59
60 list& operator=(const list& x);
Howard Hinnant45900102011-06-03 17:30:28 +000061 list& operator=(list&& x)
62 noexcept(
63 allocator_type::propagate_on_container_move_assignment::value &&
64 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant3e519522010-05-11 19:42:16 +000065 list& operator=(initializer_list<value_type>);
66 template <class Iter>
67 void assign(Iter first, Iter last);
68 void assign(size_type n, const value_type& t);
69 void assign(initializer_list<value_type>);
70
Howard Hinnant45900102011-06-03 17:30:28 +000071 allocator_type get_allocator() const noexcept;
Howard Hinnant3e519522010-05-11 19:42:16 +000072
Howard Hinnant45900102011-06-03 17:30:28 +000073 iterator begin() noexcept;
74 const_iterator begin() const noexcept;
75 iterator end() noexcept;
76 const_iterator end() const noexcept;
77 reverse_iterator rbegin() noexcept;
78 const_reverse_iterator rbegin() const noexcept;
79 reverse_iterator rend() noexcept;
80 const_reverse_iterator rend() const noexcept;
81 const_iterator cbegin() const noexcept;
82 const_iterator cend() const noexcept;
83 const_reverse_iterator crbegin() const noexcept;
84 const_reverse_iterator crend() const noexcept;
Howard Hinnant3e519522010-05-11 19:42:16 +000085
86 reference front();
87 const_reference front() const;
88 reference back();
89 const_reference back() const;
90
Howard Hinnant45900102011-06-03 17:30:28 +000091 bool empty() const noexcept;
92 size_type size() const noexcept;
93 size_type max_size() const noexcept;
Howard Hinnant3e519522010-05-11 19:42:16 +000094
95 template <class... Args>
Eric Fiselier0e411642016-07-21 03:20:17 +000096 reference emplace_front(Args&&... args);
Howard Hinnant3e519522010-05-11 19:42:16 +000097 void pop_front();
98 template <class... Args>
Eric Fiselier0e411642016-07-21 03:20:17 +000099 reference emplace_back(Args&&... args);
Howard Hinnant3e519522010-05-11 19:42:16 +0000100 void pop_back();
101 void push_front(const value_type& x);
102 void push_front(value_type&& x);
103 void push_back(const value_type& x);
104 void push_back(value_type&& x);
105 template <class... Args>
106 iterator emplace(const_iterator position, Args&&... args);
107 iterator insert(const_iterator position, const value_type& x);
108 iterator insert(const_iterator position, value_type&& x);
109 iterator insert(const_iterator position, size_type n, const value_type& x);
110 template <class Iter>
111 iterator insert(const_iterator position, Iter first, Iter last);
112 iterator insert(const_iterator position, initializer_list<value_type> il);
113
114 iterator erase(const_iterator position);
115 iterator erase(const_iterator position, const_iterator last);
116
117 void resize(size_type sz);
118 void resize(size_type sz, const value_type& c);
119
Howard Hinnant45900102011-06-03 17:30:28 +0000120 void swap(list&)
Marshall Clowe3fbe142015-07-13 20:04:56 +0000121 noexcept(allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnant45900102011-06-03 17:30:28 +0000122 void clear() noexcept;
Howard Hinnant3e519522010-05-11 19:42:16 +0000123
124 void splice(const_iterator position, list& x);
125 void splice(const_iterator position, list&& x);
126 void splice(const_iterator position, list& x, const_iterator i);
127 void splice(const_iterator position, list&& x, const_iterator i);
128 void splice(const_iterator position, list& x, const_iterator first,
129 const_iterator last);
130 void splice(const_iterator position, list&& x, const_iterator first,
131 const_iterator last);
132
133 void remove(const value_type& value);
134 template <class Pred> void remove_if(Pred pred);
135 void unique();
136 template <class BinaryPredicate>
137 void unique(BinaryPredicate binary_pred);
138 void merge(list& x);
139 void merge(list&& x);
140 template <class Compare>
141 void merge(list& x, Compare comp);
142 template <class Compare>
143 void merge(list&& x, Compare comp);
144 void sort();
145 template <class Compare>
146 void sort(Compare comp);
Howard Hinnant45900102011-06-03 17:30:28 +0000147 void reverse() noexcept;
Howard Hinnant3e519522010-05-11 19:42:16 +0000148};
149
150template <class T, class Alloc>
151 bool operator==(const list<T,Alloc>& x, const list<T,Alloc>& y);
152template <class T, class Alloc>
153 bool operator< (const list<T,Alloc>& x, const list<T,Alloc>& y);
154template <class T, class Alloc>
155 bool operator!=(const list<T,Alloc>& x, const list<T,Alloc>& y);
156template <class T, class Alloc>
157 bool operator> (const list<T,Alloc>& x, const list<T,Alloc>& y);
158template <class T, class Alloc>
159 bool operator>=(const list<T,Alloc>& x, const list<T,Alloc>& y);
160template <class T, class Alloc>
161 bool operator<=(const list<T,Alloc>& x, const list<T,Alloc>& y);
162
163template <class T, class Alloc>
Howard Hinnant45900102011-06-03 17:30:28 +0000164 void swap(list<T,Alloc>& x, list<T,Alloc>& y)
165 noexcept(noexcept(x.swap(y)));
Howard Hinnant3e519522010-05-11 19:42:16 +0000166
167} // std
168
169*/
170
171#include <__config>
172
173#include <memory>
174#include <limits>
175#include <initializer_list>
176#include <iterator>
177#include <algorithm>
Eric Fiselierb88ea352015-12-30 20:57:59 +0000178#include <type_traits>
Howard Hinnant3e519522010-05-11 19:42:16 +0000179
Howard Hinnantab4f4382011-11-29 16:45:27 +0000180#include <__undef_min_max>
181
Eric Fiselierc1bd9192014-08-10 23:53:08 +0000182#include <__debug>
Howard Hinnant42a30462013-08-02 00:26:35 +0000183
Howard Hinnant073458b2011-10-17 20:05:10 +0000184#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnant3e519522010-05-11 19:42:16 +0000185#pragma GCC system_header
Howard Hinnant073458b2011-10-17 20:05:10 +0000186#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000187
188_LIBCPP_BEGIN_NAMESPACE_STD
189
Howard Hinnantce534202011-06-14 19:58:17 +0000190template <class _Tp, class _VoidPtr> struct __list_node;
Eric Fiselierb88ea352015-12-30 20:57:59 +0000191template <class _Tp, class _VoidPtr> struct __list_node_base;
192
193template <class _Tp, class _VoidPtr>
194struct __list_node_pointer_traits {
195 typedef typename __rebind_pointer<_VoidPtr, __list_node<_Tp, _VoidPtr> >::type
196 __node_pointer;
197 typedef typename __rebind_pointer<_VoidPtr, __list_node_base<_Tp, _VoidPtr> >::type
198 __base_pointer;
199
200#if defined(_LIBCPP_ABI_LIST_REMOVE_NODE_POINTER_UB)
201 typedef __base_pointer __link_pointer;
202#else
203 typedef typename conditional<
204 is_pointer<_VoidPtr>::value,
205 __base_pointer,
206 __node_pointer
207 >::type __link_pointer;
208#endif
209
Eric Fiselier5243e192016-01-04 03:27:52 +0000210 typedef typename conditional<
211 is_same<__link_pointer, __node_pointer>::value,
212 __base_pointer,
213 __node_pointer
214 >::type __non_link_pointer;
215
216 static _LIBCPP_INLINE_VISIBILITY
217 __link_pointer __unsafe_link_pointer_cast(__link_pointer __p) {
218 return __p;
219 }
220
221 static _LIBCPP_INLINE_VISIBILITY
222 __link_pointer __unsafe_link_pointer_cast(__non_link_pointer __p) {
223 return static_cast<__link_pointer>(static_cast<_VoidPtr>(__p));
224 }
225
Eric Fiselierb88ea352015-12-30 20:57:59 +0000226};
Howard Hinnant3e519522010-05-11 19:42:16 +0000227
228template <class _Tp, class _VoidPtr>
229struct __list_node_base
230{
Eric Fiselierb88ea352015-12-30 20:57:59 +0000231 typedef __list_node_pointer_traits<_Tp, _VoidPtr> _NodeTraits;
232 typedef typename _NodeTraits::__node_pointer __node_pointer;
233 typedef typename _NodeTraits::__base_pointer __base_pointer;
234 typedef typename _NodeTraits::__link_pointer __link_pointer;
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000235
Eric Fiselierb88ea352015-12-30 20:57:59 +0000236 __link_pointer __prev_;
237 __link_pointer __next_;
Howard Hinnant3e519522010-05-11 19:42:16 +0000238
Howard Hinnant848a5372010-09-22 16:48:34 +0000239 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5243e192016-01-04 03:27:52 +0000240 __list_node_base() : __prev_(_NodeTraits::__unsafe_link_pointer_cast(__self())),
241 __next_(_NodeTraits::__unsafe_link_pointer_cast(__self())) {}
Marshall Clow28d65da2014-08-05 01:34:12 +0000242
243 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5243e192016-01-04 03:27:52 +0000244 __base_pointer __self() {
Eric Fiselierb88ea352015-12-30 20:57:59 +0000245 return pointer_traits<__base_pointer>::pointer_to(*this);
246 }
247
248 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierb88ea352015-12-30 20:57:59 +0000249 __node_pointer __as_node() {
Eric Fiselier5243e192016-01-04 03:27:52 +0000250 return static_cast<__node_pointer>(__self());
Marshall Clow28d65da2014-08-05 01:34:12 +0000251 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000252};
253
254template <class _Tp, class _VoidPtr>
255struct __list_node
256 : public __list_node_base<_Tp, _VoidPtr>
257{
258 _Tp __value_;
Eric Fiselier5243e192016-01-04 03:27:52 +0000259
260 typedef __list_node_base<_Tp, _VoidPtr> __base;
261 typedef typename __base::__link_pointer __link_pointer;
262
263 _LIBCPP_INLINE_VISIBILITY
264 __link_pointer __as_link() {
265 return static_cast<__link_pointer>(__base::__self());
266 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000267};
268
Marshall Clowb5d34aa2015-02-18 17:24:08 +0000269template <class _Tp, class _Alloc = allocator<_Tp> > class _LIBCPP_TYPE_VIS_ONLY list;
Howard Hinnantce534202011-06-14 19:58:17 +0000270template <class _Tp, class _Alloc> class __list_imp;
Howard Hinnantf0544c22013-08-12 18:38:34 +0000271template <class _Tp, class _VoidPtr> class _LIBCPP_TYPE_VIS_ONLY __list_const_iterator;
Howard Hinnant3e519522010-05-11 19:42:16 +0000272
273template <class _Tp, class _VoidPtr>
Howard Hinnantf0544c22013-08-12 18:38:34 +0000274class _LIBCPP_TYPE_VIS_ONLY __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>
Howard Hinnantf0544c22013-08-12 18:38:34 +0000397class _LIBCPP_TYPE_VIS_ONLY __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
629 _NOEXCEPT;
630#else
631 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
632 __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
Howard Hinnant3e519522010-05-11 19:42:16 +0000658 void __copy_assign_alloc(const __list_imp& __c, false_type)
659 {}
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
Howard Hinnant86681392011-09-02 20:42:31 +0000669 void __move_assign_alloc(__list_imp& __c, false_type)
Howard Hinnant45900102011-06-03 17:30:28 +0000670 _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000671 {}
672};
673
674// Unlink nodes [__f, __l]
675template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +0000676inline
Howard Hinnant3e519522010-05-11 19:42:16 +0000677void
Eric Fiselierb88ea352015-12-30 20:57:59 +0000678__list_imp<_Tp, _Alloc>::__unlink_nodes(__link_pointer __f, __link_pointer __l)
Howard Hinnant45900102011-06-03 17:30:28 +0000679 _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000680{
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000681 __f->__prev_->__next_ = __l->__next_;
682 __l->__next_->__prev_ = __f->__prev_;
Howard Hinnant3e519522010-05-11 19:42:16 +0000683}
684
685template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +0000686inline
Howard Hinnant3e519522010-05-11 19:42:16 +0000687__list_imp<_Tp, _Alloc>::__list_imp()
Howard Hinnant45900102011-06-03 17:30:28 +0000688 _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +0000689 : __size_alloc_(0)
690{
691}
692
693template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +0000694inline
Howard Hinnant3e519522010-05-11 19:42:16 +0000695__list_imp<_Tp, _Alloc>::__list_imp(const allocator_type& __a)
696 : __size_alloc_(0, __node_allocator(__a))
697{
698}
699
700template <class _Tp, class _Alloc>
701__list_imp<_Tp, _Alloc>::~__list_imp()
702{
703 clear();
Howard Hinnant920b56c2011-09-27 23:55:03 +0000704#if _LIBCPP_DEBUG_LEVEL >= 2
705 __get_db()->__erase_c(this);
706#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000707}
708
709template <class _Tp, class _Alloc>
710void
Howard Hinnant45900102011-06-03 17:30:28 +0000711__list_imp<_Tp, _Alloc>::clear() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000712{
713 if (!empty())
714 {
715 __node_allocator& __na = __node_alloc();
Eric Fiselierb88ea352015-12-30 20:57:59 +0000716 __link_pointer __f = __end_.__next_;
Eric Fiselier5243e192016-01-04 03:27:52 +0000717 __link_pointer __l = __end_as_link();
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000718 __unlink_nodes(__f, __l->__prev_);
Howard Hinnant3e519522010-05-11 19:42:16 +0000719 __sz() = 0;
720 while (__f != __l)
721 {
Eric Fiselierb88ea352015-12-30 20:57:59 +0000722 __node_pointer __np = __f->__as_node();
Howard Hinnant920b56c2011-09-27 23:55:03 +0000723 __f = __f->__next_;
Eric Fiselierb88ea352015-12-30 20:57:59 +0000724 __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
725 __node_alloc_traits::deallocate(__na, __np, 1);
Howard Hinnant3e519522010-05-11 19:42:16 +0000726 }
Howard Hinnant920b56c2011-09-27 23:55:03 +0000727#if _LIBCPP_DEBUG_LEVEL >= 2
728 __c_node* __c = __get_db()->__find_c_and_lock(this);
729 for (__i_node** __p = __c->end_; __p != __c->beg_; )
730 {
731 --__p;
732 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
733 if (__i->__ptr_ != __l)
734 {
735 (*__p)->__c_ = nullptr;
736 if (--__c->end_ != __p)
737 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
738 }
739 }
740 __get_db()->unlock();
741#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000742 }
743}
744
745template <class _Tp, class _Alloc>
746void
747__list_imp<_Tp, _Alloc>::swap(__list_imp& __c)
Marshall Clowe3fbe142015-07-13 20:04:56 +0000748#if _LIBCPP_STD_VER >= 14
749 _NOEXCEPT
750#else
751 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
752 __is_nothrow_swappable<allocator_type>::value)
753#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000754{
Howard Hinnant920b56c2011-09-27 23:55:03 +0000755 _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
756 this->__node_alloc() == __c.__node_alloc(),
757 "list::swap: Either propagate_on_container_swap must be true"
758 " or the allocators must compare equal");
Howard Hinnantce48a112011-06-30 21:18:19 +0000759 using _VSTD::swap;
Marshall Clowe3fbe142015-07-13 20:04:56 +0000760 __swap_allocator(__node_alloc(), __c.__node_alloc());
Howard Hinnant3e519522010-05-11 19:42:16 +0000761 swap(__sz(), __c.__sz());
762 swap(__end_, __c.__end_);
763 if (__sz() == 0)
Eric Fiselier5243e192016-01-04 03:27:52 +0000764 __end_.__next_ = __end_.__prev_ = __end_as_link();
Howard Hinnant3e519522010-05-11 19:42:16 +0000765 else
Eric Fiselier5243e192016-01-04 03:27:52 +0000766 __end_.__prev_->__next_ = __end_.__next_->__prev_ = __end_as_link();
Howard Hinnant3e519522010-05-11 19:42:16 +0000767 if (__c.__sz() == 0)
Eric Fiselier5243e192016-01-04 03:27:52 +0000768 __c.__end_.__next_ = __c.__end_.__prev_ = __c.__end_as_link();
Howard Hinnant3e519522010-05-11 19:42:16 +0000769 else
Eric Fiselier5243e192016-01-04 03:27:52 +0000770 __c.__end_.__prev_->__next_ = __c.__end_.__next_->__prev_ = __c.__end_as_link();
Marshall Clow28d65da2014-08-05 01:34:12 +0000771
Howard Hinnant920b56c2011-09-27 23:55:03 +0000772#if _LIBCPP_DEBUG_LEVEL >= 2
773 __libcpp_db* __db = __get_db();
774 __c_node* __cn1 = __db->__find_c_and_lock(this);
775 __c_node* __cn2 = __db->__find_c(&__c);
776 std::swap(__cn1->beg_, __cn2->beg_);
777 std::swap(__cn1->end_, __cn2->end_);
778 std::swap(__cn1->cap_, __cn2->cap_);
779 for (__i_node** __p = __cn1->end_; __p != __cn1->beg_;)
780 {
781 --__p;
782 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
Eric Fiselier5243e192016-01-04 03:27:52 +0000783 if (__i->__ptr_ == __c.__end_as_link())
Howard Hinnant920b56c2011-09-27 23:55:03 +0000784 {
785 __cn2->__add(*__p);
786 if (--__cn1->end_ != __p)
787 memmove(__p, __p+1, (__cn1->end_ - __p)*sizeof(__i_node*));
788 }
789 else
790 (*__p)->__c_ = __cn1;
791 }
792 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
793 {
794 --__p;
795 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
Eric Fiselier5243e192016-01-04 03:27:52 +0000796 if (__i->__ptr_ == __end_as_link())
Howard Hinnant920b56c2011-09-27 23:55:03 +0000797 {
798 __cn1->__add(*__p);
799 if (--__cn2->end_ != __p)
800 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
801 }
802 else
803 (*__p)->__c_ = __cn2;
804 }
805 __db->unlock();
806#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000807}
808
Marshall Clowb5d34aa2015-02-18 17:24:08 +0000809template <class _Tp, class _Alloc /*= allocator<_Tp>*/>
Howard Hinnantf0544c22013-08-12 18:38:34 +0000810class _LIBCPP_TYPE_VIS_ONLY list
Howard Hinnant3e519522010-05-11 19:42:16 +0000811 : private __list_imp<_Tp, _Alloc>
812{
813 typedef __list_imp<_Tp, _Alloc> base;
814 typedef typename base::__node __node;
815 typedef typename base::__node_allocator __node_allocator;
816 typedef typename base::__node_pointer __node_pointer;
817 typedef typename base::__node_alloc_traits __node_alloc_traits;
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000818 typedef typename base::__node_base __node_base;
819 typedef typename base::__node_base_pointer __node_base_pointer;
Eric Fiselierb88ea352015-12-30 20:57:59 +0000820 typedef typename base::__link_pointer __link_pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000821
822public:
823 typedef _Tp value_type;
824 typedef _Alloc allocator_type;
825 static_assert((is_same<value_type, typename allocator_type::value_type>::value),
826 "Invalid allocator::value_type");
827 typedef value_type& reference;
828 typedef const value_type& const_reference;
829 typedef typename base::pointer pointer;
830 typedef typename base::const_pointer const_pointer;
831 typedef typename base::size_type size_type;
832 typedef typename base::difference_type difference_type;
833 typedef typename base::iterator iterator;
834 typedef typename base::const_iterator const_iterator;
Howard Hinnantce48a112011-06-30 21:18:19 +0000835 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
836 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnant3e519522010-05-11 19:42:16 +0000837
Howard Hinnant848a5372010-09-22 16:48:34 +0000838 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000839 list()
840 _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value)
Howard Hinnant920b56c2011-09-27 23:55:03 +0000841 {
842#if _LIBCPP_DEBUG_LEVEL >= 2
843 __get_db()->__insert_c(this);
844#endif
845 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000846 _LIBCPP_INLINE_VISIBILITY
Marshall Clowfb829762013-09-08 19:11:51 +0000847 explicit list(const allocator_type& __a) : base(__a)
Howard Hinnant920b56c2011-09-27 23:55:03 +0000848 {
849#if _LIBCPP_DEBUG_LEVEL >= 2
850 __get_db()->__insert_c(this);
851#endif
852 }
Marshall Clowfb829762013-09-08 19:11:51 +0000853 explicit list(size_type __n);
854#if _LIBCPP_STD_VER > 11
855 explicit list(size_type __n, const allocator_type& __a);
856#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000857 list(size_type __n, const value_type& __x);
858 list(size_type __n, const value_type& __x, const allocator_type& __a);
859 template <class _InpIter>
860 list(_InpIter __f, _InpIter __l,
861 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
862 template <class _InpIter>
863 list(_InpIter __f, _InpIter __l, const allocator_type& __a,
864 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
865
866 list(const list& __c);
867 list(const list& __c, const allocator_type& __a);
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +0000868 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000869 list& operator=(const list& __c);
Howard Hinnant54976f22011-08-12 21:56:02 +0000870#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant3e519522010-05-11 19:42:16 +0000871 list(initializer_list<value_type> __il);
872 list(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnant54976f22011-08-12 21:56:02 +0000873#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000874#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +0000875 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000876 list(list&& __c)
877 _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value);
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +0000878 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000879 list(list&& __c, const allocator_type& __a);
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +0000880 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000881 list& operator=(list&& __c)
882 _NOEXCEPT_(
883 __node_alloc_traits::propagate_on_container_move_assignment::value &&
884 is_nothrow_move_assignable<__node_allocator>::value);
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000885#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54976f22011-08-12 21:56:02 +0000886#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant848a5372010-09-22 16:48:34 +0000887 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000888 list& operator=(initializer_list<value_type> __il)
889 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnant54976f22011-08-12 21:56:02 +0000890#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant3e519522010-05-11 19:42:16 +0000891
892 template <class _InpIter>
893 void assign(_InpIter __f, _InpIter __l,
894 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
895 void assign(size_type __n, const value_type& __x);
Howard Hinnant54976f22011-08-12 21:56:02 +0000896#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant848a5372010-09-22 16:48:34 +0000897 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000898 void assign(initializer_list<value_type> __il)
899 {assign(__il.begin(), __il.end());}
Howard Hinnant54976f22011-08-12 21:56:02 +0000900#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant3e519522010-05-11 19:42:16 +0000901
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +0000902 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000903 allocator_type get_allocator() const _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +0000904
Howard Hinnant848a5372010-09-22 16:48:34 +0000905 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000906 size_type size() const _NOEXCEPT {return base::__sz();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000907 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000908 bool empty() const _NOEXCEPT {return base::empty();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000909 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000910 size_type max_size() const _NOEXCEPT
Eric Fiselier55b31b4e2016-11-23 01:18:56 +0000911 {
912 return std::min<size_type>(
913 base::__node_alloc_max_size(),
914 numeric_limits<difference_type >::max());
915 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000916
Howard Hinnant848a5372010-09-22 16:48:34 +0000917 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000918 iterator begin() _NOEXCEPT {return base::begin();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000919 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000920 const_iterator begin() const _NOEXCEPT {return base::begin();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000921 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000922 iterator end() _NOEXCEPT {return base::end();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000923 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000924 const_iterator end() const _NOEXCEPT {return base::end();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000925 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000926 const_iterator cbegin() const _NOEXCEPT {return base::begin();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000927 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000928 const_iterator cend() const _NOEXCEPT {return base::end();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000929
Howard Hinnant848a5372010-09-22 16:48:34 +0000930 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000931 reverse_iterator rbegin() _NOEXCEPT
932 {return reverse_iterator(end());}
Howard Hinnant848a5372010-09-22 16:48:34 +0000933 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000934 const_reverse_iterator rbegin() const _NOEXCEPT
935 {return const_reverse_iterator(end());}
Howard Hinnant848a5372010-09-22 16:48:34 +0000936 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000937 reverse_iterator rend() _NOEXCEPT
938 {return reverse_iterator(begin());}
Howard Hinnant848a5372010-09-22 16:48:34 +0000939 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000940 const_reverse_iterator rend() const _NOEXCEPT
941 {return const_reverse_iterator(begin());}
Howard Hinnant848a5372010-09-22 16:48:34 +0000942 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000943 const_reverse_iterator crbegin() const _NOEXCEPT
944 {return const_reverse_iterator(end());}
Howard Hinnant848a5372010-09-22 16:48:34 +0000945 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000946 const_reverse_iterator crend() const _NOEXCEPT
947 {return const_reverse_iterator(begin());}
Howard Hinnant3e519522010-05-11 19:42:16 +0000948
Howard Hinnant848a5372010-09-22 16:48:34 +0000949 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000950 reference front()
951 {
952 _LIBCPP_ASSERT(!empty(), "list::front called on empty list");
Eric Fiselierb88ea352015-12-30 20:57:59 +0000953 return base::__end_.__next_->__as_node()->__value_;
Howard Hinnant920b56c2011-09-27 23:55:03 +0000954 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000955 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000956 const_reference front() const
957 {
958 _LIBCPP_ASSERT(!empty(), "list::front called on empty list");
Eric Fiselierb88ea352015-12-30 20:57:59 +0000959 return base::__end_.__next_->__as_node()->__value_;
Howard Hinnant920b56c2011-09-27 23:55:03 +0000960 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000961 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000962 reference back()
963 {
964 _LIBCPP_ASSERT(!empty(), "list::back called on empty list");
Eric Fiselierb88ea352015-12-30 20:57:59 +0000965 return base::__end_.__prev_->__as_node()->__value_;
Howard Hinnant920b56c2011-09-27 23:55:03 +0000966 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000967 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000968 const_reference back() const
969 {
970 _LIBCPP_ASSERT(!empty(), "list::back called on empty list");
Eric Fiselierb88ea352015-12-30 20:57:59 +0000971 return base::__end_.__prev_->__as_node()->__value_;
Howard Hinnant920b56c2011-09-27 23:55:03 +0000972 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000973
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000974#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +0000975 void push_front(value_type&& __x);
976 void push_back(value_type&& __x);
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000977#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnant3e519522010-05-11 19:42:16 +0000978 template <class... _Args>
Eric Fiselier0e411642016-07-21 03:20:17 +0000979 reference emplace_front(_Args&&... __args);
Howard Hinnant3e519522010-05-11 19:42:16 +0000980 template <class... _Args>
Eric Fiselier0e411642016-07-21 03:20:17 +0000981 reference emplace_back(_Args&&... __args);
Howard Hinnant3e519522010-05-11 19:42:16 +0000982 template <class... _Args>
983 iterator emplace(const_iterator __p, _Args&&... __args);
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000984#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnant3e519522010-05-11 19:42:16 +0000985 iterator insert(const_iterator __p, value_type&& __x);
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000986#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +0000987
988 void push_front(const value_type& __x);
989 void push_back(const value_type& __x);
990
991 iterator insert(const_iterator __p, const value_type& __x);
992 iterator insert(const_iterator __p, size_type __n, const value_type& __x);
993 template <class _InpIter>
994 iterator insert(const_iterator __p, _InpIter __f, _InpIter __l,
995 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
Howard Hinnant54976f22011-08-12 21:56:02 +0000996#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant848a5372010-09-22 16:48:34 +0000997 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000998 iterator insert(const_iterator __p, initializer_list<value_type> __il)
999 {return insert(__p, __il.begin(), __il.end());}
Howard Hinnant54976f22011-08-12 21:56:02 +00001000#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant3e519522010-05-11 19:42:16 +00001001
Howard Hinnant848a5372010-09-22 16:48:34 +00001002 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +00001003 void swap(list& __c)
Marshall Clowe3fbe142015-07-13 20:04:56 +00001004#if _LIBCPP_STD_VER >= 14
1005 _NOEXCEPT
1006#else
Howard Hinnant45900102011-06-03 17:30:28 +00001007 _NOEXCEPT_(!__node_alloc_traits::propagate_on_container_swap::value ||
1008 __is_nothrow_swappable<__node_allocator>::value)
Marshall Clowe3fbe142015-07-13 20:04:56 +00001009#endif
Howard Hinnant45900102011-06-03 17:30:28 +00001010 {base::swap(__c);}
Howard Hinnant848a5372010-09-22 16:48:34 +00001011 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +00001012 void clear() _NOEXCEPT {base::clear();}
Howard Hinnant3e519522010-05-11 19:42:16 +00001013
1014 void pop_front();
1015 void pop_back();
1016
1017 iterator erase(const_iterator __p);
1018 iterator erase(const_iterator __f, const_iterator __l);
1019
1020 void resize(size_type __n);
1021 void resize(size_type __n, const value_type& __x);
1022
1023 void splice(const_iterator __p, list& __c);
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001024#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant848a5372010-09-22 16:48:34 +00001025 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001026 void splice(const_iterator __p, list&& __c) {splice(__p, __c);}
1027#endif
1028 void splice(const_iterator __p, list& __c, const_iterator __i);
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001029#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant848a5372010-09-22 16:48:34 +00001030 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001031 void splice(const_iterator __p, list&& __c, const_iterator __i)
1032 {splice(__p, __c, __i);}
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001033#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +00001034 void splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l);
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001035#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant848a5372010-09-22 16:48:34 +00001036 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001037 void splice(const_iterator __p, list&& __c, const_iterator __f, const_iterator __l)
1038 {splice(__p, __c, __f, __l);}
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001039#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +00001040
1041 void remove(const value_type& __x);
1042 template <class _Pred> void remove_if(_Pred __pred);
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001043 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001044 void unique();
1045 template <class _BinaryPred>
1046 void unique(_BinaryPred __binary_pred);
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001047 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001048 void merge(list& __c);
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001049#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant848a5372010-09-22 16:48:34 +00001050 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001051 void merge(list&& __c) {merge(__c);}
1052#endif
1053 template <class _Comp>
1054 void merge(list& __c, _Comp __comp);
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001055#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +00001056 template <class _Comp>
Howard Hinnant848a5372010-09-22 16:48:34 +00001057 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001058 void merge(list&& __c, _Comp __comp) {merge(__c, __comp);}
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001059#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001060 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001061 void sort();
1062 template <class _Comp>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001063 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001064 void sort(_Comp __comp);
1065
Howard Hinnant45900102011-06-03 17:30:28 +00001066 void reverse() _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +00001067
Howard Hinnant920b56c2011-09-27 23:55:03 +00001068 bool __invariants() const;
1069
1070#if _LIBCPP_DEBUG_LEVEL >= 2
1071
1072 bool __dereferenceable(const const_iterator* __i) const;
1073 bool __decrementable(const const_iterator* __i) const;
1074 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1075 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1076
1077#endif // _LIBCPP_DEBUG_LEVEL >= 2
1078
Howard Hinnant3e519522010-05-11 19:42:16 +00001079private:
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001080 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierb88ea352015-12-30 20:57:59 +00001081 static void __link_nodes (__link_pointer __p, __link_pointer __f, __link_pointer __l);
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001082 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierb88ea352015-12-30 20:57:59 +00001083 void __link_nodes_at_front(__link_pointer __f, __link_pointer __l);
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001084 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierb88ea352015-12-30 20:57:59 +00001085 void __link_nodes_at_back (__link_pointer __f, __link_pointer __l);
Howard Hinnant3e519522010-05-11 19:42:16 +00001086 iterator __iterator(size_type __n);
1087 template <class _Comp>
1088 static iterator __sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp);
1089
Howard Hinnant45900102011-06-03 17:30:28 +00001090 void __move_assign(list& __c, true_type)
1091 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value);
Howard Hinnant3e519522010-05-11 19:42:16 +00001092 void __move_assign(list& __c, false_type);
1093};
1094
1095// Link in nodes [__f, __l] just prior to __p
1096template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001097inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001098void
Eric Fiselierb88ea352015-12-30 20:57:59 +00001099list<_Tp, _Alloc>::__link_nodes(__link_pointer __p, __link_pointer __f, __link_pointer __l)
Howard Hinnant3e519522010-05-11 19:42:16 +00001100{
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001101 __p->__prev_->__next_ = __f;
1102 __f->__prev_ = __p->__prev_;
1103 __p->__prev_ = __l;
1104 __l->__next_ = __p;
Howard Hinnant3e519522010-05-11 19:42:16 +00001105}
1106
Marshall Clow28d65da2014-08-05 01:34:12 +00001107// Link in nodes [__f, __l] at the front of the list
1108template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001109inline
Marshall Clow28d65da2014-08-05 01:34:12 +00001110void
Eric Fiselierb88ea352015-12-30 20:57:59 +00001111list<_Tp, _Alloc>::__link_nodes_at_front(__link_pointer __f, __link_pointer __l)
Marshall Clow28d65da2014-08-05 01:34:12 +00001112{
Eric Fiselier5243e192016-01-04 03:27:52 +00001113 __f->__prev_ = base::__end_as_link();
Marshall Clowced70062014-08-08 15:35:52 +00001114 __l->__next_ = base::__end_.__next_;
1115 __l->__next_->__prev_ = __l;
1116 base::__end_.__next_ = __f;
Marshall Clow28d65da2014-08-05 01:34:12 +00001117}
1118
1119// Link in nodes [__f, __l] at the front of the list
1120template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001121inline
Marshall Clow28d65da2014-08-05 01:34:12 +00001122void
Eric Fiselierb88ea352015-12-30 20:57:59 +00001123list<_Tp, _Alloc>::__link_nodes_at_back(__link_pointer __f, __link_pointer __l)
Marshall Clow28d65da2014-08-05 01:34:12 +00001124{
Eric Fiselier5243e192016-01-04 03:27:52 +00001125 __l->__next_ = base::__end_as_link();
Marshall Clowced70062014-08-08 15:35:52 +00001126 __f->__prev_ = base::__end_.__prev_;
1127 __f->__prev_->__next_ = __f;
1128 base::__end_.__prev_ = __l;
Marshall Clow28d65da2014-08-05 01:34:12 +00001129}
1130
1131
Howard Hinnant3e519522010-05-11 19:42:16 +00001132template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001133inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001134typename list<_Tp, _Alloc>::iterator
1135list<_Tp, _Alloc>::__iterator(size_type __n)
1136{
Howard Hinnantce48a112011-06-30 21:18:19 +00001137 return __n <= base::__sz() / 2 ? _VSTD::next(begin(), __n)
1138 : _VSTD::prev(end(), base::__sz() - __n);
Howard Hinnant3e519522010-05-11 19:42:16 +00001139}
1140
1141template <class _Tp, class _Alloc>
1142list<_Tp, _Alloc>::list(size_type __n)
1143{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001144#if _LIBCPP_DEBUG_LEVEL >= 2
1145 __get_db()->__insert_c(this);
1146#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001147 for (; __n > 0; --__n)
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001148#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +00001149 emplace_back();
1150#else
1151 push_back(value_type());
1152#endif
1153}
1154
Marshall Clowfb829762013-09-08 19:11:51 +00001155#if _LIBCPP_STD_VER > 11
1156template <class _Tp, class _Alloc>
1157list<_Tp, _Alloc>::list(size_type __n, const allocator_type& __a) : base(__a)
1158{
1159#if _LIBCPP_DEBUG_LEVEL >= 2
1160 __get_db()->__insert_c(this);
1161#endif
1162 for (; __n > 0; --__n)
1163#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1164 emplace_back();
1165#else
1166 push_back(value_type());
1167#endif
1168}
1169#endif
1170
Howard Hinnant3e519522010-05-11 19:42:16 +00001171template <class _Tp, class _Alloc>
1172list<_Tp, _Alloc>::list(size_type __n, const value_type& __x)
1173{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001174#if _LIBCPP_DEBUG_LEVEL >= 2
1175 __get_db()->__insert_c(this);
1176#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001177 for (; __n > 0; --__n)
1178 push_back(__x);
1179}
1180
1181template <class _Tp, class _Alloc>
1182list<_Tp, _Alloc>::list(size_type __n, const value_type& __x, const allocator_type& __a)
1183 : base(__a)
1184{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001185#if _LIBCPP_DEBUG_LEVEL >= 2
1186 __get_db()->__insert_c(this);
1187#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001188 for (; __n > 0; --__n)
1189 push_back(__x);
1190}
1191
1192template <class _Tp, class _Alloc>
1193template <class _InpIter>
1194list<_Tp, _Alloc>::list(_InpIter __f, _InpIter __l,
1195 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1196{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001197#if _LIBCPP_DEBUG_LEVEL >= 2
1198 __get_db()->__insert_c(this);
1199#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001200 for (; __f != __l; ++__f)
1201 push_back(*__f);
1202}
1203
1204template <class _Tp, class _Alloc>
1205template <class _InpIter>
1206list<_Tp, _Alloc>::list(_InpIter __f, _InpIter __l, const allocator_type& __a,
1207 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1208 : base(__a)
1209{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001210#if _LIBCPP_DEBUG_LEVEL >= 2
1211 __get_db()->__insert_c(this);
1212#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001213 for (; __f != __l; ++__f)
1214 push_back(*__f);
1215}
1216
1217template <class _Tp, class _Alloc>
1218list<_Tp, _Alloc>::list(const list& __c)
1219 : base(allocator_type(
1220 __node_alloc_traits::select_on_container_copy_construction(
1221 __c.__node_alloc())))
1222{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001223#if _LIBCPP_DEBUG_LEVEL >= 2
1224 __get_db()->__insert_c(this);
1225#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001226 for (const_iterator __i = __c.begin(), __e = __c.end(); __i != __e; ++__i)
1227 push_back(*__i);
1228}
1229
1230template <class _Tp, class _Alloc>
1231list<_Tp, _Alloc>::list(const list& __c, const allocator_type& __a)
1232 : base(__a)
1233{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001234#if _LIBCPP_DEBUG_LEVEL >= 2
1235 __get_db()->__insert_c(this);
1236#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001237 for (const_iterator __i = __c.begin(), __e = __c.end(); __i != __e; ++__i)
1238 push_back(*__i);
1239}
1240
Howard Hinnant54976f22011-08-12 21:56:02 +00001241#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1242
Howard Hinnant3e519522010-05-11 19:42:16 +00001243template <class _Tp, class _Alloc>
1244list<_Tp, _Alloc>::list(initializer_list<value_type> __il, const allocator_type& __a)
1245 : base(__a)
1246{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001247#if _LIBCPP_DEBUG_LEVEL >= 2
1248 __get_db()->__insert_c(this);
1249#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001250 for (typename initializer_list<value_type>::const_iterator __i = __il.begin(),
1251 __e = __il.end(); __i != __e; ++__i)
1252 push_back(*__i);
1253}
1254
1255template <class _Tp, class _Alloc>
1256list<_Tp, _Alloc>::list(initializer_list<value_type> __il)
1257{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001258#if _LIBCPP_DEBUG_LEVEL >= 2
1259 __get_db()->__insert_c(this);
1260#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001261 for (typename initializer_list<value_type>::const_iterator __i = __il.begin(),
1262 __e = __il.end(); __i != __e; ++__i)
1263 push_back(*__i);
1264}
1265
Howard Hinnant54976f22011-08-12 21:56:02 +00001266#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1267
Howard Hinnant3e519522010-05-11 19:42:16 +00001268template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001269inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001270list<_Tp, _Alloc>&
1271list<_Tp, _Alloc>::operator=(const list& __c)
1272{
1273 if (this != &__c)
1274 {
1275 base::__copy_assign_alloc(__c);
1276 assign(__c.begin(), __c.end());
1277 }
1278 return *this;
1279}
1280
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001281#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +00001282
1283template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001284inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001285list<_Tp, _Alloc>::list(list&& __c)
Howard Hinnant45900102011-06-03 17:30:28 +00001286 _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value)
Howard Hinnantce48a112011-06-30 21:18:19 +00001287 : base(allocator_type(_VSTD::move(__c.__node_alloc())))
Howard Hinnant3e519522010-05-11 19:42:16 +00001288{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001289#if _LIBCPP_DEBUG_LEVEL >= 2
1290 __get_db()->__insert_c(this);
1291#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001292 splice(end(), __c);
1293}
1294
1295template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001296inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001297list<_Tp, _Alloc>::list(list&& __c, const allocator_type& __a)
1298 : base(__a)
1299{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001300#if _LIBCPP_DEBUG_LEVEL >= 2
1301 __get_db()->__insert_c(this);
1302#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001303 if (__a == __c.get_allocator())
1304 splice(end(), __c);
1305 else
1306 {
Howard Hinnantc003db12011-11-29 18:15:50 +00001307 typedef move_iterator<iterator> _Ip;
1308 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnant3e519522010-05-11 19:42:16 +00001309 }
1310}
1311
1312template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001313inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001314list<_Tp, _Alloc>&
1315list<_Tp, _Alloc>::operator=(list&& __c)
Howard Hinnant45900102011-06-03 17:30:28 +00001316 _NOEXCEPT_(
1317 __node_alloc_traits::propagate_on_container_move_assignment::value &&
1318 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +00001319{
1320 __move_assign(__c, integral_constant<bool,
1321 __node_alloc_traits::propagate_on_container_move_assignment::value>());
1322 return *this;
1323}
1324
1325template <class _Tp, class _Alloc>
1326void
1327list<_Tp, _Alloc>::__move_assign(list& __c, false_type)
1328{
1329 if (base::__node_alloc() != __c.__node_alloc())
1330 {
Howard Hinnantc003db12011-11-29 18:15:50 +00001331 typedef move_iterator<iterator> _Ip;
1332 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnant3e519522010-05-11 19:42:16 +00001333 }
1334 else
1335 __move_assign(__c, true_type());
1336}
1337
1338template <class _Tp, class _Alloc>
1339void
1340list<_Tp, _Alloc>::__move_assign(list& __c, true_type)
Howard Hinnant45900102011-06-03 17:30:28 +00001341 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +00001342{
1343 clear();
1344 base::__move_assign_alloc(__c);
1345 splice(end(), __c);
1346}
1347
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001348#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +00001349
1350template <class _Tp, class _Alloc>
1351template <class _InpIter>
1352void
1353list<_Tp, _Alloc>::assign(_InpIter __f, _InpIter __l,
1354 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1355{
1356 iterator __i = begin();
1357 iterator __e = end();
1358 for (; __f != __l && __i != __e; ++__f, ++__i)
1359 *__i = *__f;
1360 if (__i == __e)
1361 insert(__e, __f, __l);
1362 else
1363 erase(__i, __e);
1364}
1365
1366template <class _Tp, class _Alloc>
1367void
1368list<_Tp, _Alloc>::assign(size_type __n, const value_type& __x)
1369{
1370 iterator __i = begin();
1371 iterator __e = end();
1372 for (; __n > 0 && __i != __e; --__n, ++__i)
1373 *__i = __x;
1374 if (__i == __e)
1375 insert(__e, __n, __x);
1376 else
1377 erase(__i, __e);
1378}
1379
1380template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001381inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001382_Alloc
Howard Hinnant45900102011-06-03 17:30:28 +00001383list<_Tp, _Alloc>::get_allocator() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001384{
1385 return allocator_type(base::__node_alloc());
1386}
1387
1388template <class _Tp, class _Alloc>
1389typename list<_Tp, _Alloc>::iterator
1390list<_Tp, _Alloc>::insert(const_iterator __p, const value_type& __x)
1391{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001392#if _LIBCPP_DEBUG_LEVEL >= 2
1393 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1394 "list::insert(iterator, x) called with an iterator not"
1395 " referring to this list");
1396#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001397 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001398 typedef __allocator_destructor<__node_allocator> _Dp;
1399 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant3e519522010-05-11 19:42:16 +00001400 __hold->__prev_ = 0;
Howard Hinnantce48a112011-06-30 21:18:19 +00001401 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001402 __link_nodes(__p.__ptr_, __hold->__as_link(), __hold->__as_link());
Howard Hinnant3e519522010-05-11 19:42:16 +00001403 ++base::__sz();
Howard Hinnantb0e4c9d2013-04-05 00:18:49 +00001404#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselierb88ea352015-12-30 20:57:59 +00001405 return iterator(__hold.release()->__as_link(), this);
Howard Hinnantb0e4c9d2013-04-05 00:18:49 +00001406#else
Eric Fiselierb88ea352015-12-30 20:57:59 +00001407 return iterator(__hold.release()->__as_link());
Howard Hinnantb0e4c9d2013-04-05 00:18:49 +00001408#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001409}
1410
1411template <class _Tp, class _Alloc>
1412typename list<_Tp, _Alloc>::iterator
1413list<_Tp, _Alloc>::insert(const_iterator __p, size_type __n, const value_type& __x)
1414{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001415#if _LIBCPP_DEBUG_LEVEL >= 2
1416 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1417 "list::insert(iterator, n, x) called with an iterator not"
1418 " referring to this list");
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001419 iterator __r(__p.__ptr_, this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001420#else
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001421 iterator __r(__p.__ptr_);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001422#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001423 if (__n > 0)
1424 {
1425 size_type __ds = 0;
1426 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001427 typedef __allocator_destructor<__node_allocator> _Dp;
1428 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant3e519522010-05-11 19:42:16 +00001429 __hold->__prev_ = 0;
Howard Hinnantce48a112011-06-30 21:18:19 +00001430 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnant3e519522010-05-11 19:42:16 +00001431 ++__ds;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001432#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselierb88ea352015-12-30 20:57:59 +00001433 __r = iterator(__hold->__as_link(), this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001434#else
Eric Fiselierb88ea352015-12-30 20:57:59 +00001435 __r = iterator(__hold->__as_link());
Howard Hinnant920b56c2011-09-27 23:55:03 +00001436#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001437 __hold.release();
1438 iterator __e = __r;
1439#ifndef _LIBCPP_NO_EXCEPTIONS
1440 try
1441 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001442#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001443 for (--__n; __n != 0; --__n, ++__e, ++__ds)
1444 {
1445 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001446 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001447 __e.__ptr_->__next_ = __hold->__as_link();
Howard Hinnant3e519522010-05-11 19:42:16 +00001448 __hold->__prev_ = __e.__ptr_;
1449 __hold.release();
1450 }
1451#ifndef _LIBCPP_NO_EXCEPTIONS
1452 }
1453 catch (...)
1454 {
1455 while (true)
1456 {
Howard Hinnantce48a112011-06-30 21:18:19 +00001457 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Eric Fiselierb88ea352015-12-30 20:57:59 +00001458 __link_pointer __prev = __e.__ptr_->__prev_;
1459 __node_alloc_traits::deallocate(__na, __e.__ptr_->__as_node(), 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001460 if (__prev == 0)
1461 break;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001462#if _LIBCPP_DEBUG_LEVEL >= 2
1463 __e = iterator(__prev, this);
1464#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001465 __e = iterator(__prev);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001466#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001467 }
1468 throw;
1469 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001470#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001471 __link_nodes(__p.__ptr_, __r.__ptr_, __e.__ptr_);
Howard Hinnant3e519522010-05-11 19:42:16 +00001472 base::__sz() += __ds;
1473 }
1474 return __r;
1475}
1476
1477template <class _Tp, class _Alloc>
1478template <class _InpIter>
1479typename list<_Tp, _Alloc>::iterator
1480list<_Tp, _Alloc>::insert(const_iterator __p, _InpIter __f, _InpIter __l,
1481 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1482{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001483#if _LIBCPP_DEBUG_LEVEL >= 2
1484 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1485 "list::insert(iterator, range) called with an iterator not"
1486 " referring to this list");
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001487 iterator __r(__p.__ptr_, this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001488#else
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001489 iterator __r(__p.__ptr_);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001490#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001491 if (__f != __l)
1492 {
1493 size_type __ds = 0;
1494 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001495 typedef __allocator_destructor<__node_allocator> _Dp;
1496 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant3e519522010-05-11 19:42:16 +00001497 __hold->__prev_ = 0;
Howard Hinnantce48a112011-06-30 21:18:19 +00001498 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), *__f);
Howard Hinnant3e519522010-05-11 19:42:16 +00001499 ++__ds;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001500#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselierb88ea352015-12-30 20:57:59 +00001501 __r = iterator(__hold.get()->__as_link(), this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001502#else
Eric Fiselierb88ea352015-12-30 20:57:59 +00001503 __r = iterator(__hold.get()->__as_link());
Howard Hinnant920b56c2011-09-27 23:55:03 +00001504#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001505 __hold.release();
1506 iterator __e = __r;
1507#ifndef _LIBCPP_NO_EXCEPTIONS
1508 try
1509 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001510#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiselier61bff612015-03-19 03:20:02 +00001511 for (++__f; __f != __l; ++__f, (void) ++__e, (void) ++__ds)
Howard Hinnant3e519522010-05-11 19:42:16 +00001512 {
1513 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001514 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), *__f);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001515 __e.__ptr_->__next_ = __hold.get()->__as_link();
Howard Hinnant3e519522010-05-11 19:42:16 +00001516 __hold->__prev_ = __e.__ptr_;
1517 __hold.release();
1518 }
1519#ifndef _LIBCPP_NO_EXCEPTIONS
1520 }
1521 catch (...)
1522 {
1523 while (true)
1524 {
Howard Hinnantce48a112011-06-30 21:18:19 +00001525 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Eric Fiselierb88ea352015-12-30 20:57:59 +00001526 __link_pointer __prev = __e.__ptr_->__prev_;
1527 __node_alloc_traits::deallocate(__na, __e.__ptr_->__as_node(), 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001528 if (__prev == 0)
1529 break;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001530#if _LIBCPP_DEBUG_LEVEL >= 2
1531 __e = iterator(__prev, this);
1532#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001533 __e = iterator(__prev);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001534#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001535 }
1536 throw;
1537 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001538#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001539 __link_nodes(__p.__ptr_, __r.__ptr_, __e.__ptr_);
Howard Hinnant3e519522010-05-11 19:42:16 +00001540 base::__sz() += __ds;
1541 }
1542 return __r;
1543}
1544
1545template <class _Tp, class _Alloc>
1546void
1547list<_Tp, _Alloc>::push_front(const value_type& __x)
1548{
1549 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001550 typedef __allocator_destructor<__node_allocator> _Dp;
1551 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001552 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001553 __link_pointer __nl = __hold->__as_link();
1554 __link_nodes_at_front(__nl, __nl);
Howard Hinnant3e519522010-05-11 19:42:16 +00001555 ++base::__sz();
1556 __hold.release();
1557}
1558
1559template <class _Tp, class _Alloc>
1560void
1561list<_Tp, _Alloc>::push_back(const value_type& __x)
1562{
1563 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001564 typedef __allocator_destructor<__node_allocator> _Dp;
1565 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001566 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001567 __link_nodes_at_back(__hold.get()->__as_link(), __hold.get()->__as_link());
Howard Hinnant3e519522010-05-11 19:42:16 +00001568 ++base::__sz();
1569 __hold.release();
1570}
1571
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001572#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +00001573
1574template <class _Tp, class _Alloc>
1575void
1576list<_Tp, _Alloc>::push_front(value_type&& __x)
1577{
1578 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001579 typedef __allocator_destructor<__node_allocator> _Dp;
1580 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001581 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
Eric Fiselierb88ea352015-12-30 20:57:59 +00001582 __link_nodes_at_front(__hold.get()->__as_link(), __hold.get()->__as_link());
Howard Hinnant3e519522010-05-11 19:42:16 +00001583 ++base::__sz();
1584 __hold.release();
1585}
1586
1587template <class _Tp, class _Alloc>
1588void
1589list<_Tp, _Alloc>::push_back(value_type&& __x)
1590{
1591 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001592 typedef __allocator_destructor<__node_allocator> _Dp;
1593 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001594 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
Eric Fiselierb88ea352015-12-30 20:57:59 +00001595 __link_nodes_at_back(__hold.get()->__as_link(), __hold.get()->__as_link());
Howard Hinnant3e519522010-05-11 19:42:16 +00001596 ++base::__sz();
1597 __hold.release();
1598}
1599
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001600#ifndef _LIBCPP_HAS_NO_VARIADICS
1601
Howard Hinnant3e519522010-05-11 19:42:16 +00001602template <class _Tp, class _Alloc>
1603template <class... _Args>
Eric Fiselier0e411642016-07-21 03:20:17 +00001604typename list<_Tp, _Alloc>::reference
Howard Hinnant3e519522010-05-11 19:42:16 +00001605list<_Tp, _Alloc>::emplace_front(_Args&&... __args)
1606{
1607 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001608 typedef __allocator_destructor<__node_allocator> _Dp;
1609 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001610 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001611 __link_nodes_at_front(__hold.get()->__as_link(), __hold.get()->__as_link());
Howard Hinnant3e519522010-05-11 19:42:16 +00001612 ++base::__sz();
Eric Fiselier0e411642016-07-21 03:20:17 +00001613 return __hold.release()->__value_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001614}
1615
1616template <class _Tp, class _Alloc>
1617template <class... _Args>
Eric Fiselier0e411642016-07-21 03:20:17 +00001618typename list<_Tp, _Alloc>::reference
Howard Hinnant3e519522010-05-11 19:42:16 +00001619list<_Tp, _Alloc>::emplace_back(_Args&&... __args)
1620{
1621 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001622 typedef __allocator_destructor<__node_allocator> _Dp;
1623 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001624 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001625 __link_pointer __nl = __hold->__as_link();
1626 __link_nodes_at_back(__nl, __nl);
Howard Hinnant3e519522010-05-11 19:42:16 +00001627 ++base::__sz();
Eric Fiselier0e411642016-07-21 03:20:17 +00001628 return __hold.release()->__value_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001629}
1630
1631template <class _Tp, class _Alloc>
1632template <class... _Args>
1633typename list<_Tp, _Alloc>::iterator
1634list<_Tp, _Alloc>::emplace(const_iterator __p, _Args&&... __args)
1635{
Howard Hinnantb0e4c9d2013-04-05 00:18:49 +00001636#if _LIBCPP_DEBUG_LEVEL >= 2
1637 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1638 "list::emplace(iterator, args...) called with an iterator not"
1639 " referring to this list");
1640#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001641 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001642 typedef __allocator_destructor<__node_allocator> _Dp;
1643 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant3e519522010-05-11 19:42:16 +00001644 __hold->__prev_ = 0;
Howard Hinnantce48a112011-06-30 21:18:19 +00001645 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001646 __link_pointer __nl = __hold.get()->__as_link();
1647 __link_nodes(__p.__ptr_, __nl, __nl);
Howard Hinnant3e519522010-05-11 19:42:16 +00001648 ++base::__sz();
Eric Fiselierb88ea352015-12-30 20:57:59 +00001649 __hold.release();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001650#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselierb88ea352015-12-30 20:57:59 +00001651 return iterator(__nl, this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001652#else
Eric Fiselierb88ea352015-12-30 20:57:59 +00001653 return iterator(__nl);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001654#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001655}
1656
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001657#endif // _LIBCPP_HAS_NO_VARIADICS
1658
Howard Hinnant3e519522010-05-11 19:42:16 +00001659template <class _Tp, class _Alloc>
1660typename list<_Tp, _Alloc>::iterator
1661list<_Tp, _Alloc>::insert(const_iterator __p, value_type&& __x)
1662{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001663#if _LIBCPP_DEBUG_LEVEL >= 2
1664 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1665 "list::insert(iterator, x) called with an iterator not"
1666 " referring to this list");
1667#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001668 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001669 typedef __allocator_destructor<__node_allocator> _Dp;
1670 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant3e519522010-05-11 19:42:16 +00001671 __hold->__prev_ = 0;
Howard Hinnantce48a112011-06-30 21:18:19 +00001672 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
Eric Fiselierb88ea352015-12-30 20:57:59 +00001673 __link_pointer __nl = __hold->__as_link();
1674 __link_nodes(__p.__ptr_, __nl, __nl);
Howard Hinnant3e519522010-05-11 19:42:16 +00001675 ++base::__sz();
Eric Fiselierb88ea352015-12-30 20:57:59 +00001676 __hold.release();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001677#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselierb88ea352015-12-30 20:57:59 +00001678 return iterator(__nl, this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001679#else
Eric Fiselierb88ea352015-12-30 20:57:59 +00001680 return iterator(__nl);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001681#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001682}
1683
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001684#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +00001685
1686template <class _Tp, class _Alloc>
1687void
1688list<_Tp, _Alloc>::pop_front()
1689{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001690 _LIBCPP_ASSERT(!empty(), "list::pop_front() called with empty list");
Howard Hinnant3e519522010-05-11 19:42:16 +00001691 __node_allocator& __na = base::__node_alloc();
Eric Fiselierb88ea352015-12-30 20:57:59 +00001692 __link_pointer __n = base::__end_.__next_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001693 base::__unlink_nodes(__n, __n);
1694 --base::__sz();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001695#if _LIBCPP_DEBUG_LEVEL >= 2
1696 __c_node* __c = __get_db()->__find_c_and_lock(this);
1697 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1698 {
1699 --__p;
1700 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001701 if (__i->__ptr_ == __n)
Howard Hinnant920b56c2011-09-27 23:55:03 +00001702 {
1703 (*__p)->__c_ = nullptr;
1704 if (--__c->end_ != __p)
1705 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1706 }
1707 }
1708 __get_db()->unlock();
1709#endif
Eric Fiselierb88ea352015-12-30 20:57:59 +00001710 __node_pointer __np = __n->__as_node();
1711 __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
1712 __node_alloc_traits::deallocate(__na, __np, 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001713}
1714
1715template <class _Tp, class _Alloc>
1716void
1717list<_Tp, _Alloc>::pop_back()
1718{
Dmitri Gribenkoc3f9c802013-06-24 06:15:57 +00001719 _LIBCPP_ASSERT(!empty(), "list::pop_back() called with empty list");
Howard Hinnant3e519522010-05-11 19:42:16 +00001720 __node_allocator& __na = base::__node_alloc();
Eric Fiselierb88ea352015-12-30 20:57:59 +00001721 __link_pointer __n = base::__end_.__prev_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001722 base::__unlink_nodes(__n, __n);
1723 --base::__sz();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001724#if _LIBCPP_DEBUG_LEVEL >= 2
1725 __c_node* __c = __get_db()->__find_c_and_lock(this);
1726 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1727 {
1728 --__p;
1729 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001730 if (__i->__ptr_ == __n)
Howard Hinnant920b56c2011-09-27 23:55:03 +00001731 {
1732 (*__p)->__c_ = nullptr;
1733 if (--__c->end_ != __p)
1734 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1735 }
1736 }
1737 __get_db()->unlock();
1738#endif
Eric Fiselierb88ea352015-12-30 20:57:59 +00001739 __node_pointer __np = __n->__as_node();
1740 __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
1741 __node_alloc_traits::deallocate(__na, __np, 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001742}
1743
1744template <class _Tp, class _Alloc>
1745typename list<_Tp, _Alloc>::iterator
1746list<_Tp, _Alloc>::erase(const_iterator __p)
1747{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001748#if _LIBCPP_DEBUG_LEVEL >= 2
1749 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1750 "list::erase(iterator) called with an iterator not"
1751 " referring to this list");
1752#endif
Howard Hinnantb0e4c9d2013-04-05 00:18:49 +00001753 _LIBCPP_ASSERT(__p != end(),
1754 "list::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnant3e519522010-05-11 19:42:16 +00001755 __node_allocator& __na = base::__node_alloc();
Eric Fiselierb88ea352015-12-30 20:57:59 +00001756 __link_pointer __n = __p.__ptr_;
1757 __link_pointer __r = __n->__next_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001758 base::__unlink_nodes(__n, __n);
1759 --base::__sz();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001760#if _LIBCPP_DEBUG_LEVEL >= 2
1761 __c_node* __c = __get_db()->__find_c_and_lock(this);
Eric Fiselier878e7e22016-10-23 19:26:39 +00001762 for (__i_node** __ip = __c->end_; __ip != __c->beg_; )
Howard Hinnant920b56c2011-09-27 23:55:03 +00001763 {
Eric Fiselier878e7e22016-10-23 19:26:39 +00001764 --__ip;
1765 iterator* __i = static_cast<iterator*>((*__ip)->__i_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001766 if (__i->__ptr_ == __n)
Howard Hinnant920b56c2011-09-27 23:55:03 +00001767 {
Eric Fiselier878e7e22016-10-23 19:26:39 +00001768 (*__ip)->__c_ = nullptr;
1769 if (--__c->end_ != __ip)
1770 memmove(__ip, __ip+1, (__c->end_ - __ip)*sizeof(__i_node*));
Howard Hinnant920b56c2011-09-27 23:55:03 +00001771 }
1772 }
1773 __get_db()->unlock();
1774#endif
Eric Fiselierb88ea352015-12-30 20:57:59 +00001775 __node_pointer __np = __n->__as_node();
1776 __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
1777 __node_alloc_traits::deallocate(__na, __np, 1);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001778#if _LIBCPP_DEBUG_LEVEL >= 2
1779 return iterator(__r, this);
1780#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001781 return iterator(__r);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001782#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001783}
1784
1785template <class _Tp, class _Alloc>
1786typename list<_Tp, _Alloc>::iterator
1787list<_Tp, _Alloc>::erase(const_iterator __f, const_iterator __l)
1788{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001789#if _LIBCPP_DEBUG_LEVEL >= 2
1790 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__f) == this,
1791 "list::erase(iterator, iterator) called with an iterator not"
1792 " referring to this list");
1793#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001794 if (__f != __l)
1795 {
1796 __node_allocator& __na = base::__node_alloc();
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001797 base::__unlink_nodes(__f.__ptr_, __l.__ptr_->__prev_);
Howard Hinnant3e519522010-05-11 19:42:16 +00001798 while (__f != __l)
1799 {
Eric Fiselierb88ea352015-12-30 20:57:59 +00001800 __link_pointer __n = __f.__ptr_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001801 ++__f;
1802 --base::__sz();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001803#if _LIBCPP_DEBUG_LEVEL >= 2
1804 __c_node* __c = __get_db()->__find_c_and_lock(this);
1805 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1806 {
1807 --__p;
1808 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001809 if (__i->__ptr_ == __n)
Howard Hinnant920b56c2011-09-27 23:55:03 +00001810 {
1811 (*__p)->__c_ = nullptr;
1812 if (--__c->end_ != __p)
1813 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1814 }
1815 }
1816 __get_db()->unlock();
1817#endif
Eric Fiselierb88ea352015-12-30 20:57:59 +00001818 __node_pointer __np = __n->__as_node();
1819 __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
1820 __node_alloc_traits::deallocate(__na, __np, 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001821 }
1822 }
Howard Hinnant920b56c2011-09-27 23:55:03 +00001823#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001824 return iterator(__l.__ptr_, this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001825#else
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001826 return iterator(__l.__ptr_);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001827#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001828}
1829
1830template <class _Tp, class _Alloc>
1831void
1832list<_Tp, _Alloc>::resize(size_type __n)
1833{
1834 if (__n < base::__sz())
1835 erase(__iterator(__n), end());
1836 else if (__n > base::__sz())
1837 {
1838 __n -= base::__sz();
1839 size_type __ds = 0;
1840 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001841 typedef __allocator_destructor<__node_allocator> _Dp;
1842 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant3e519522010-05-11 19:42:16 +00001843 __hold->__prev_ = 0;
Howard Hinnantce48a112011-06-30 21:18:19 +00001844 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_));
Howard Hinnant3e519522010-05-11 19:42:16 +00001845 ++__ds;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001846#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselierb88ea352015-12-30 20:57:59 +00001847 iterator __r = iterator(__hold.release()->__as_link(), this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001848#else
Eric Fiselierb88ea352015-12-30 20:57:59 +00001849 iterator __r = iterator(__hold.release()->__as_link());
Howard Hinnant920b56c2011-09-27 23:55:03 +00001850#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001851 iterator __e = __r;
1852#ifndef _LIBCPP_NO_EXCEPTIONS
1853 try
1854 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001855#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001856 for (--__n; __n != 0; --__n, ++__e, ++__ds)
1857 {
1858 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001859 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_));
Eric Fiselierb88ea352015-12-30 20:57:59 +00001860 __e.__ptr_->__next_ = __hold.get()->__as_link();
Howard Hinnant3e519522010-05-11 19:42:16 +00001861 __hold->__prev_ = __e.__ptr_;
1862 __hold.release();
1863 }
1864#ifndef _LIBCPP_NO_EXCEPTIONS
1865 }
1866 catch (...)
1867 {
1868 while (true)
1869 {
Howard Hinnantce48a112011-06-30 21:18:19 +00001870 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Eric Fiselierb88ea352015-12-30 20:57:59 +00001871 __link_pointer __prev = __e.__ptr_->__prev_;
1872 __node_alloc_traits::deallocate(__na, __e.__ptr_->__as_node(), 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001873 if (__prev == 0)
1874 break;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001875#if _LIBCPP_DEBUG_LEVEL >= 2
1876 __e = iterator(__prev, this);
1877#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001878 __e = iterator(__prev);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001879#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001880 }
1881 throw;
1882 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001883#endif // _LIBCPP_NO_EXCEPTIONS
Marshall Clow28d65da2014-08-05 01:34:12 +00001884 __link_nodes_at_back(__r.__ptr_, __e.__ptr_);
Howard Hinnant3e519522010-05-11 19:42:16 +00001885 base::__sz() += __ds;
1886 }
1887}
1888
1889template <class _Tp, class _Alloc>
1890void
1891list<_Tp, _Alloc>::resize(size_type __n, const value_type& __x)
1892{
1893 if (__n < base::__sz())
1894 erase(__iterator(__n), end());
1895 else if (__n > base::__sz())
1896 {
1897 __n -= base::__sz();
1898 size_type __ds = 0;
1899 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001900 typedef __allocator_destructor<__node_allocator> _Dp;
1901 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant3e519522010-05-11 19:42:16 +00001902 __hold->__prev_ = 0;
Howard Hinnantce48a112011-06-30 21:18:19 +00001903 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnant3e519522010-05-11 19:42:16 +00001904 ++__ds;
Eric Fiselierb88ea352015-12-30 20:57:59 +00001905 __link_pointer __nl = __hold.release()->__as_link();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001906#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselierb88ea352015-12-30 20:57:59 +00001907 iterator __r = iterator(__nl, this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001908#else
Eric Fiselierb88ea352015-12-30 20:57:59 +00001909 iterator __r = iterator(__nl);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001910#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001911 iterator __e = __r;
1912#ifndef _LIBCPP_NO_EXCEPTIONS
1913 try
1914 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001915#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001916 for (--__n; __n != 0; --__n, ++__e, ++__ds)
1917 {
1918 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001919 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001920 __e.__ptr_->__next_ = __hold.get()->__as_link();
Howard Hinnant3e519522010-05-11 19:42:16 +00001921 __hold->__prev_ = __e.__ptr_;
1922 __hold.release();
1923 }
1924#ifndef _LIBCPP_NO_EXCEPTIONS
1925 }
1926 catch (...)
1927 {
1928 while (true)
1929 {
Howard Hinnantce48a112011-06-30 21:18:19 +00001930 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Eric Fiselierb88ea352015-12-30 20:57:59 +00001931 __link_pointer __prev = __e.__ptr_->__prev_;
1932 __node_alloc_traits::deallocate(__na, __e.__ptr_->__as_node(), 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001933 if (__prev == 0)
1934 break;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001935#if _LIBCPP_DEBUG_LEVEL >= 2
1936 __e = iterator(__prev, this);
1937#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001938 __e = iterator(__prev);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001939#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001940 }
1941 throw;
1942 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001943#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiselier5243e192016-01-04 03:27:52 +00001944 __link_nodes(base::__end_as_link(), __r.__ptr_, __e.__ptr_);
Howard Hinnant3e519522010-05-11 19:42:16 +00001945 base::__sz() += __ds;
Howard Hinnantb3371f62010-08-22 00:02:43 +00001946 }
Howard Hinnant3e519522010-05-11 19:42:16 +00001947}
1948
1949template <class _Tp, class _Alloc>
1950void
1951list<_Tp, _Alloc>::splice(const_iterator __p, list& __c)
1952{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001953 _LIBCPP_ASSERT(this != &__c,
1954 "list::splice(iterator, list) called with this == &list");
1955#if _LIBCPP_DEBUG_LEVEL >= 2
1956 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1957 "list::splice(iterator, list) called with an iterator not"
1958 " referring to this list");
1959#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001960 if (!__c.empty())
1961 {
Eric Fiselierb88ea352015-12-30 20:57:59 +00001962 __link_pointer __f = __c.__end_.__next_;
1963 __link_pointer __l = __c.__end_.__prev_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001964 base::__unlink_nodes(__f, __l);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001965 __link_nodes(__p.__ptr_, __f, __l);
Howard Hinnant3e519522010-05-11 19:42:16 +00001966 base::__sz() += __c.__sz();
1967 __c.__sz() = 0;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001968#if _LIBCPP_DEBUG_LEVEL >= 2
1969 __libcpp_db* __db = __get_db();
1970 __c_node* __cn1 = __db->__find_c_and_lock(this);
1971 __c_node* __cn2 = __db->__find_c(&__c);
Eric Fiselier878e7e22016-10-23 19:26:39 +00001972 for (__i_node** __ip = __cn2->end_; __ip != __cn2->beg_;)
Howard Hinnant920b56c2011-09-27 23:55:03 +00001973 {
Eric Fiselier878e7e22016-10-23 19:26:39 +00001974 --__ip;
1975 iterator* __i = static_cast<iterator*>((*__ip)->__i_);
Eric Fiselier5243e192016-01-04 03:27:52 +00001976 if (__i->__ptr_ != __c.__end_as_link())
Howard Hinnant920b56c2011-09-27 23:55:03 +00001977 {
Eric Fiselier878e7e22016-10-23 19:26:39 +00001978 __cn1->__add(*__ip);
1979 (*__ip)->__c_ = __cn1;
1980 if (--__cn2->end_ != __ip)
1981 memmove(__ip, __ip+1, (__cn2->end_ - __ip)*sizeof(__i_node*));
Howard Hinnant920b56c2011-09-27 23:55:03 +00001982 }
1983 }
1984 __db->unlock();
1985#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001986 }
1987}
1988
1989template <class _Tp, class _Alloc>
1990void
1991list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __i)
1992{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001993#if _LIBCPP_DEBUG_LEVEL >= 2
1994 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1995 "list::splice(iterator, list, iterator) called with first iterator not"
1996 " referring to this list");
1997 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__i) == &__c,
1998 "list::splice(iterator, list, iterator) called with second iterator not"
1999 " referring to list argument");
2000 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(&__i),
2001 "list::splice(iterator, list, iterator) called with second iterator not"
2002 " derefereceable");
2003#endif
2004 if (__p.__ptr_ != __i.__ptr_ && __p.__ptr_ != __i.__ptr_->__next_)
Howard Hinnant3e519522010-05-11 19:42:16 +00002005 {
Eric Fiselierb88ea352015-12-30 20:57:59 +00002006 __link_pointer __f = __i.__ptr_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002007 base::__unlink_nodes(__f, __f);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002008 __link_nodes(__p.__ptr_, __f, __f);
Howard Hinnant3e519522010-05-11 19:42:16 +00002009 --__c.__sz();
2010 ++base::__sz();
Howard Hinnant920b56c2011-09-27 23:55:03 +00002011#if _LIBCPP_DEBUG_LEVEL >= 2
2012 __libcpp_db* __db = __get_db();
2013 __c_node* __cn1 = __db->__find_c_and_lock(this);
2014 __c_node* __cn2 = __db->__find_c(&__c);
Eric Fiselier878e7e22016-10-23 19:26:39 +00002015 for (__i_node** __ip = __cn2->end_; __ip != __cn2->beg_;)
Howard Hinnant920b56c2011-09-27 23:55:03 +00002016 {
Eric Fiselier878e7e22016-10-23 19:26:39 +00002017 --__ip;
2018 iterator* __j = static_cast<iterator*>((*__ip)->__i_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002019 if (__j->__ptr_ == __f)
Howard Hinnant920b56c2011-09-27 23:55:03 +00002020 {
Eric Fiselier878e7e22016-10-23 19:26:39 +00002021 __cn1->__add(*__ip);
2022 (*__ip)->__c_ = __cn1;
2023 if (--__cn2->end_ != __ip)
2024 memmove(__ip, __ip+1, (__cn2->end_ - __ip)*sizeof(__i_node*));
Howard Hinnant920b56c2011-09-27 23:55:03 +00002025 }
2026 }
2027 __db->unlock();
2028#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002029 }
2030}
2031
2032template <class _Tp, class _Alloc>
2033void
2034list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l)
2035{
Howard Hinnant920b56c2011-09-27 23:55:03 +00002036#if _LIBCPP_DEBUG_LEVEL >= 2
2037 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2038 "list::splice(iterator, list, iterator, iterator) called with first iterator not"
2039 " referring to this list");
2040 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__f) == &__c,
2041 "list::splice(iterator, list, iterator, iterator) called with second iterator not"
2042 " referring to list argument");
2043 if (this == &__c)
2044 {
2045 for (const_iterator __i = __f; __i != __l; ++__i)
2046 _LIBCPP_ASSERT(__i != __p,
2047 "list::splice(iterator, list, iterator, iterator)"
2048 " called with the first iterator within the range"
2049 " of the second and third iterators");
2050 }
2051#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002052 if (__f != __l)
2053 {
2054 if (this != &__c)
2055 {
Howard Hinnantce48a112011-06-30 21:18:19 +00002056 size_type __s = _VSTD::distance(__f, __l);
Howard Hinnant3e519522010-05-11 19:42:16 +00002057 __c.__sz() -= __s;
2058 base::__sz() += __s;
2059 }
Eric Fiselierb88ea352015-12-30 20:57:59 +00002060 __link_pointer __first = __f.__ptr_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002061 --__l;
Eric Fiselierb88ea352015-12-30 20:57:59 +00002062 __link_pointer __last = __l.__ptr_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002063 base::__unlink_nodes(__first, __last);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002064 __link_nodes(__p.__ptr_, __first, __last);
Howard Hinnant920b56c2011-09-27 23:55:03 +00002065#if _LIBCPP_DEBUG_LEVEL >= 2
2066 __libcpp_db* __db = __get_db();
2067 __c_node* __cn1 = __db->__find_c_and_lock(this);
2068 __c_node* __cn2 = __db->__find_c(&__c);
Eric Fiselier878e7e22016-10-23 19:26:39 +00002069 for (__i_node** __ip = __cn2->end_; __ip != __cn2->beg_;)
Howard Hinnant920b56c2011-09-27 23:55:03 +00002070 {
Eric Fiselier878e7e22016-10-23 19:26:39 +00002071 --__ip;
2072 iterator* __j = static_cast<iterator*>((*__ip)->__i_);
Eric Fiselierb88ea352015-12-30 20:57:59 +00002073 for (__link_pointer __k = __f.__ptr_;
Howard Hinnant920b56c2011-09-27 23:55:03 +00002074 __k != __l.__ptr_; __k = __k->__next_)
2075 {
2076 if (__j->__ptr_ == __k)
2077 {
Eric Fiselier878e7e22016-10-23 19:26:39 +00002078 __cn1->__add(*__ip);
2079 (*__ip)->__c_ = __cn1;
2080 if (--__cn2->end_ != __ip)
2081 memmove(__ip, __ip+1, (__cn2->end_ - __ip)*sizeof(__i_node*));
Howard Hinnant920b56c2011-09-27 23:55:03 +00002082 }
2083 }
2084 }
2085 __db->unlock();
2086#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002087 }
2088}
2089
2090template <class _Tp, class _Alloc>
2091void
2092list<_Tp, _Alloc>::remove(const value_type& __x)
2093{
Eric Fiselier5cac7752016-12-14 22:48:38 +00002094 list<_Tp, _Alloc> __deleted_nodes(get_allocator()); // collect the nodes we're removing
Marshall Clowced70062014-08-08 15:35:52 +00002095 for (const_iterator __i = begin(), __e = end(); __i != __e;)
Howard Hinnant3e519522010-05-11 19:42:16 +00002096 {
2097 if (*__i == __x)
2098 {
Marshall Clowced70062014-08-08 15:35:52 +00002099 const_iterator __j = _VSTD::next(__i);
Howard Hinnant3e519522010-05-11 19:42:16 +00002100 for (; __j != __e && *__j == __x; ++__j)
2101 ;
Marshall Clowced70062014-08-08 15:35:52 +00002102 __deleted_nodes.splice(__deleted_nodes.end(), *this, __i, __j);
2103 __i = __j;
Marshall Clow90ba05332014-08-04 17:32:25 +00002104 if (__i != __e)
Marshall Clow28d65da2014-08-05 01:34:12 +00002105 ++__i;
Howard Hinnant3e519522010-05-11 19:42:16 +00002106 }
2107 else
2108 ++__i;
2109 }
2110}
2111
2112template <class _Tp, class _Alloc>
2113template <class _Pred>
2114void
2115list<_Tp, _Alloc>::remove_if(_Pred __pred)
2116{
2117 for (iterator __i = begin(), __e = end(); __i != __e;)
2118 {
2119 if (__pred(*__i))
2120 {
Howard Hinnantce48a112011-06-30 21:18:19 +00002121 iterator __j = _VSTD::next(__i);
Howard Hinnant3e519522010-05-11 19:42:16 +00002122 for (; __j != __e && __pred(*__j); ++__j)
2123 ;
2124 __i = erase(__i, __j);
Marshall Clow90ba05332014-08-04 17:32:25 +00002125 if (__i != __e)
Marshall Clow28d65da2014-08-05 01:34:12 +00002126 ++__i;
Howard Hinnant3e519522010-05-11 19:42:16 +00002127 }
2128 else
2129 ++__i;
2130 }
2131}
2132
2133template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00002134inline
Howard Hinnant3e519522010-05-11 19:42:16 +00002135void
2136list<_Tp, _Alloc>::unique()
2137{
2138 unique(__equal_to<value_type>());
2139}
2140
2141template <class _Tp, class _Alloc>
2142template <class _BinaryPred>
2143void
2144list<_Tp, _Alloc>::unique(_BinaryPred __binary_pred)
2145{
2146 for (iterator __i = begin(), __e = end(); __i != __e;)
2147 {
Howard Hinnantce48a112011-06-30 21:18:19 +00002148 iterator __j = _VSTD::next(__i);
Howard Hinnant3e519522010-05-11 19:42:16 +00002149 for (; __j != __e && __binary_pred(*__i, *__j); ++__j)
2150 ;
2151 if (++__i != __j)
2152 __i = erase(__i, __j);
2153 }
2154}
2155
2156template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00002157inline
Howard Hinnant3e519522010-05-11 19:42:16 +00002158void
2159list<_Tp, _Alloc>::merge(list& __c)
2160{
2161 merge(__c, __less<value_type>());
2162}
2163
2164template <class _Tp, class _Alloc>
2165template <class _Comp>
2166void
2167list<_Tp, _Alloc>::merge(list& __c, _Comp __comp)
2168{
2169 if (this != &__c)
2170 {
2171 iterator __f1 = begin();
2172 iterator __e1 = end();
2173 iterator __f2 = __c.begin();
2174 iterator __e2 = __c.end();
2175 while (__f1 != __e1 && __f2 != __e2)
2176 {
2177 if (__comp(*__f2, *__f1))
2178 {
2179 size_type __ds = 1;
Howard Hinnantce48a112011-06-30 21:18:19 +00002180 iterator __m2 = _VSTD::next(__f2);
Howard Hinnant3e519522010-05-11 19:42:16 +00002181 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2, ++__ds)
2182 ;
2183 base::__sz() += __ds;
2184 __c.__sz() -= __ds;
Eric Fiselierb88ea352015-12-30 20:57:59 +00002185 __link_pointer __f = __f2.__ptr_;
2186 __link_pointer __l = __m2.__ptr_->__prev_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002187 __f2 = __m2;
2188 base::__unlink_nodes(__f, __l);
Howard Hinnantce48a112011-06-30 21:18:19 +00002189 __m2 = _VSTD::next(__f1);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002190 __link_nodes(__f1.__ptr_, __f, __l);
Howard Hinnant3e519522010-05-11 19:42:16 +00002191 __f1 = __m2;
2192 }
2193 else
2194 ++__f1;
2195 }
2196 splice(__e1, __c);
Howard Hinnant920b56c2011-09-27 23:55:03 +00002197#if _LIBCPP_DEBUG_LEVEL >= 2
2198 __libcpp_db* __db = __get_db();
2199 __c_node* __cn1 = __db->__find_c_and_lock(this);
2200 __c_node* __cn2 = __db->__find_c(&__c);
2201 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
2202 {
2203 --__p;
2204 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Eric Fiselier5243e192016-01-04 03:27:52 +00002205 if (__i->__ptr_ != __c.__end_as_link())
Howard Hinnant920b56c2011-09-27 23:55:03 +00002206 {
2207 __cn1->__add(*__p);
2208 (*__p)->__c_ = __cn1;
2209 if (--__cn2->end_ != __p)
2210 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
2211 }
2212 }
2213 __db->unlock();
2214#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002215 }
2216}
2217
2218template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00002219inline
Howard Hinnant3e519522010-05-11 19:42:16 +00002220void
2221list<_Tp, _Alloc>::sort()
2222{
2223 sort(__less<value_type>());
2224}
2225
2226template <class _Tp, class _Alloc>
2227template <class _Comp>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00002228inline
Howard Hinnant3e519522010-05-11 19:42:16 +00002229void
2230list<_Tp, _Alloc>::sort(_Comp __comp)
2231{
2232 __sort(begin(), end(), base::__sz(), __comp);
2233}
2234
2235template <class _Tp, class _Alloc>
2236template <class _Comp>
Howard Hinnant3e519522010-05-11 19:42:16 +00002237typename list<_Tp, _Alloc>::iterator
2238list<_Tp, _Alloc>::__sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp)
2239{
2240 switch (__n)
2241 {
2242 case 0:
2243 case 1:
2244 return __f1;
2245 case 2:
2246 if (__comp(*--__e2, *__f1))
2247 {
Eric Fiselierb88ea352015-12-30 20:57:59 +00002248 __link_pointer __f = __e2.__ptr_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002249 base::__unlink_nodes(__f, __f);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002250 __link_nodes(__f1.__ptr_, __f, __f);
Howard Hinnant3e519522010-05-11 19:42:16 +00002251 return __e2;
2252 }
2253 return __f1;
2254 }
2255 size_type __n2 = __n / 2;
Howard Hinnantce48a112011-06-30 21:18:19 +00002256 iterator __e1 = _VSTD::next(__f1, __n2);
Howard Hinnant3e519522010-05-11 19:42:16 +00002257 iterator __r = __f1 = __sort(__f1, __e1, __n2, __comp);
2258 iterator __f2 = __e1 = __sort(__e1, __e2, __n - __n2, __comp);
2259 if (__comp(*__f2, *__f1))
2260 {
Howard Hinnantce48a112011-06-30 21:18:19 +00002261 iterator __m2 = _VSTD::next(__f2);
Howard Hinnant3e519522010-05-11 19:42:16 +00002262 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2)
2263 ;
Eric Fiselierb88ea352015-12-30 20:57:59 +00002264 __link_pointer __f = __f2.__ptr_;
2265 __link_pointer __l = __m2.__ptr_->__prev_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002266 __r = __f2;
2267 __e1 = __f2 = __m2;
2268 base::__unlink_nodes(__f, __l);
Howard Hinnantce48a112011-06-30 21:18:19 +00002269 __m2 = _VSTD::next(__f1);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002270 __link_nodes(__f1.__ptr_, __f, __l);
Howard Hinnant3e519522010-05-11 19:42:16 +00002271 __f1 = __m2;
2272 }
2273 else
2274 ++__f1;
2275 while (__f1 != __e1 && __f2 != __e2)
2276 {
2277 if (__comp(*__f2, *__f1))
2278 {
Howard Hinnantce48a112011-06-30 21:18:19 +00002279 iterator __m2 = _VSTD::next(__f2);
Howard Hinnant3e519522010-05-11 19:42:16 +00002280 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2)
2281 ;
Eric Fiselierb88ea352015-12-30 20:57:59 +00002282 __link_pointer __f = __f2.__ptr_;
2283 __link_pointer __l = __m2.__ptr_->__prev_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002284 if (__e1 == __f2)
2285 __e1 = __m2;
2286 __f2 = __m2;
2287 base::__unlink_nodes(__f, __l);
Howard Hinnantce48a112011-06-30 21:18:19 +00002288 __m2 = _VSTD::next(__f1);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002289 __link_nodes(__f1.__ptr_, __f, __l);
Howard Hinnant3e519522010-05-11 19:42:16 +00002290 __f1 = __m2;
2291 }
2292 else
2293 ++__f1;
2294 }
2295 return __r;
2296}
2297
2298template <class _Tp, class _Alloc>
2299void
Howard Hinnant45900102011-06-03 17:30:28 +00002300list<_Tp, _Alloc>::reverse() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00002301{
2302 if (base::__sz() > 1)
2303 {
2304 iterator __e = end();
Howard Hinnant920b56c2011-09-27 23:55:03 +00002305 for (iterator __i = begin(); __i.__ptr_ != __e.__ptr_;)
2306 {
Howard Hinnantce48a112011-06-30 21:18:19 +00002307 _VSTD::swap(__i.__ptr_->__prev_, __i.__ptr_->__next_);
Howard Hinnant920b56c2011-09-27 23:55:03 +00002308 __i.__ptr_ = __i.__ptr_->__prev_;
2309 }
Howard Hinnantce48a112011-06-30 21:18:19 +00002310 _VSTD::swap(__e.__ptr_->__prev_, __e.__ptr_->__next_);
Howard Hinnant3e519522010-05-11 19:42:16 +00002311 }
2312}
2313
2314template <class _Tp, class _Alloc>
Howard Hinnant920b56c2011-09-27 23:55:03 +00002315bool
2316list<_Tp, _Alloc>::__invariants() const
2317{
2318 return size() == _VSTD::distance(begin(), end());
2319}
2320
2321#if _LIBCPP_DEBUG_LEVEL >= 2
2322
2323template <class _Tp, class _Alloc>
2324bool
2325list<_Tp, _Alloc>::__dereferenceable(const const_iterator* __i) const
2326{
Eric Fiselier5243e192016-01-04 03:27:52 +00002327 return __i->__ptr_ != this->__end_as_link();
Howard Hinnant920b56c2011-09-27 23:55:03 +00002328}
2329
2330template <class _Tp, class _Alloc>
2331bool
2332list<_Tp, _Alloc>::__decrementable(const const_iterator* __i) const
2333{
2334 return !empty() && __i->__ptr_ != base::__end_.__next_;
2335}
2336
2337template <class _Tp, class _Alloc>
2338bool
2339list<_Tp, _Alloc>::__addable(const const_iterator* __i, ptrdiff_t __n) const
2340{
2341 return false;
2342}
2343
2344template <class _Tp, class _Alloc>
2345bool
2346list<_Tp, _Alloc>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
2347{
2348 return false;
2349}
2350
2351#endif // _LIBCPP_DEBUG_LEVEL >= 2
2352
2353template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00002354inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00002355bool
2356operator==(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2357{
Howard Hinnantce48a112011-06-30 21:18:19 +00002358 return __x.size() == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnant3e519522010-05-11 19:42:16 +00002359}
2360
2361template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00002362inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00002363bool
2364operator< (const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2365{
Howard Hinnantce48a112011-06-30 21:18:19 +00002366 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnant3e519522010-05-11 19:42:16 +00002367}
2368
2369template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00002370inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00002371bool
2372operator!=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2373{
2374 return !(__x == __y);
2375}
2376
2377template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00002378inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00002379bool
2380operator> (const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2381{
2382 return __y < __x;
2383}
2384
2385template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00002386inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00002387bool
2388operator>=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2389{
2390 return !(__x < __y);
2391}
2392
2393template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00002394inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00002395bool
2396operator<=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2397{
2398 return !(__y < __x);
2399}
2400
2401template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00002402inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00002403void
2404swap(list<_Tp, _Alloc>& __x, list<_Tp, _Alloc>& __y)
Howard Hinnant45900102011-06-03 17:30:28 +00002405 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnant3e519522010-05-11 19:42:16 +00002406{
2407 __x.swap(__y);
2408}
2409
2410_LIBCPP_END_NAMESPACE_STD
2411
2412#endif // _LIBCPP_LIST