blob: cff0a853eff9c6ef3852f736c789a96a6ab222f7 [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>
96 void emplace_front(Args&&... args);
97 void pop_front();
98 template <class... Args>
99 void emplace_back(Args&&... args);
100 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 Fiselierb88ea352015-12-30 20:57:59 +0000574 static void __unlink_nodes(__link_pointer __f, __link_pointer __l) _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +0000575
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +0000576 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000577 __list_imp()
578 _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value);
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +0000579 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000580 __list_imp(const allocator_type& __a);
581 ~__list_imp();
Howard Hinnant45900102011-06-03 17:30:28 +0000582 void clear() _NOEXCEPT;
Howard Hinnant848a5372010-09-22 16:48:34 +0000583 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000584 bool empty() const _NOEXCEPT {return __sz() == 0;}
Howard Hinnant3e519522010-05-11 19:42:16 +0000585
Howard Hinnant848a5372010-09-22 16:48:34 +0000586 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000587 iterator begin() _NOEXCEPT
588 {
589#if _LIBCPP_DEBUG_LEVEL >= 2
590 return iterator(__end_.__next_, this);
591#else
592 return iterator(__end_.__next_);
593#endif
594 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000595 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000596 const_iterator begin() const _NOEXCEPT
Howard Hinnant920b56c2011-09-27 23:55:03 +0000597 {
598#if _LIBCPP_DEBUG_LEVEL >= 2
599 return const_iterator(__end_.__next_, this);
600#else
601 return const_iterator(__end_.__next_);
602#endif
603 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000604 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000605 iterator end() _NOEXCEPT
606 {
607#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselier5243e192016-01-04 03:27:52 +0000608 return iterator(__end_as_link(), this);
Howard Hinnant920b56c2011-09-27 23:55:03 +0000609#else
Eric Fiselier5243e192016-01-04 03:27:52 +0000610 return iterator(__end_as_link());
Howard Hinnant920b56c2011-09-27 23:55:03 +0000611#endif
612 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000613 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000614 const_iterator end() const _NOEXCEPT
Howard Hinnant920b56c2011-09-27 23:55:03 +0000615 {
616#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselier5243e192016-01-04 03:27:52 +0000617 return const_iterator(__end_as_link(), this);
Howard Hinnant920b56c2011-09-27 23:55:03 +0000618#else
Eric Fiselier5243e192016-01-04 03:27:52 +0000619 return const_iterator(__end_as_link());
Howard Hinnant920b56c2011-09-27 23:55:03 +0000620#endif
621 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000622
Howard Hinnant45900102011-06-03 17:30:28 +0000623 void swap(__list_imp& __c)
Marshall Clowe3fbe142015-07-13 20:04:56 +0000624#if _LIBCPP_STD_VER >= 14
625 _NOEXCEPT;
626#else
627 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
628 __is_nothrow_swappable<allocator_type>::value);
629#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000630
Howard Hinnant848a5372010-09-22 16:48:34 +0000631 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000632 void __copy_assign_alloc(const __list_imp& __c)
633 {__copy_assign_alloc(__c, integral_constant<bool,
634 __node_alloc_traits::propagate_on_container_copy_assignment::value>());}
635
Howard Hinnant848a5372010-09-22 16:48:34 +0000636 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000637 void __move_assign_alloc(__list_imp& __c)
Howard Hinnant45900102011-06-03 17:30:28 +0000638 _NOEXCEPT_(
639 !__node_alloc_traits::propagate_on_container_move_assignment::value ||
640 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +0000641 {__move_assign_alloc(__c, integral_constant<bool,
642 __node_alloc_traits::propagate_on_container_move_assignment::value>());}
643
644private:
Howard Hinnant848a5372010-09-22 16:48:34 +0000645 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000646 void __copy_assign_alloc(const __list_imp& __c, true_type)
647 {
648 if (__node_alloc() != __c.__node_alloc())
649 clear();
650 __node_alloc() = __c.__node_alloc();
651 }
652
Howard Hinnant848a5372010-09-22 16:48:34 +0000653 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000654 void __copy_assign_alloc(const __list_imp& __c, false_type)
655 {}
656
Howard Hinnant848a5372010-09-22 16:48:34 +0000657 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant86681392011-09-02 20:42:31 +0000658 void __move_assign_alloc(__list_imp& __c, true_type)
Howard Hinnant45900102011-06-03 17:30:28 +0000659 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +0000660 {
Howard Hinnantce48a112011-06-30 21:18:19 +0000661 __node_alloc() = _VSTD::move(__c.__node_alloc());
Howard Hinnant3e519522010-05-11 19:42:16 +0000662 }
663
Howard Hinnant848a5372010-09-22 16:48:34 +0000664 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant86681392011-09-02 20:42:31 +0000665 void __move_assign_alloc(__list_imp& __c, false_type)
Howard Hinnant45900102011-06-03 17:30:28 +0000666 _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000667 {}
668};
669
670// Unlink nodes [__f, __l]
671template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +0000672inline
Howard Hinnant3e519522010-05-11 19:42:16 +0000673void
Eric Fiselierb88ea352015-12-30 20:57:59 +0000674__list_imp<_Tp, _Alloc>::__unlink_nodes(__link_pointer __f, __link_pointer __l)
Howard Hinnant45900102011-06-03 17:30:28 +0000675 _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000676{
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000677 __f->__prev_->__next_ = __l->__next_;
678 __l->__next_->__prev_ = __f->__prev_;
Howard Hinnant3e519522010-05-11 19:42:16 +0000679}
680
681template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +0000682inline
Howard Hinnant3e519522010-05-11 19:42:16 +0000683__list_imp<_Tp, _Alloc>::__list_imp()
Howard Hinnant45900102011-06-03 17:30:28 +0000684 _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +0000685 : __size_alloc_(0)
686{
687}
688
689template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +0000690inline
Howard Hinnant3e519522010-05-11 19:42:16 +0000691__list_imp<_Tp, _Alloc>::__list_imp(const allocator_type& __a)
692 : __size_alloc_(0, __node_allocator(__a))
693{
694}
695
696template <class _Tp, class _Alloc>
697__list_imp<_Tp, _Alloc>::~__list_imp()
698{
699 clear();
Howard Hinnant920b56c2011-09-27 23:55:03 +0000700#if _LIBCPP_DEBUG_LEVEL >= 2
701 __get_db()->__erase_c(this);
702#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000703}
704
705template <class _Tp, class _Alloc>
706void
Howard Hinnant45900102011-06-03 17:30:28 +0000707__list_imp<_Tp, _Alloc>::clear() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000708{
709 if (!empty())
710 {
711 __node_allocator& __na = __node_alloc();
Eric Fiselierb88ea352015-12-30 20:57:59 +0000712 __link_pointer __f = __end_.__next_;
Eric Fiselier5243e192016-01-04 03:27:52 +0000713 __link_pointer __l = __end_as_link();
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000714 __unlink_nodes(__f, __l->__prev_);
Howard Hinnant3e519522010-05-11 19:42:16 +0000715 __sz() = 0;
716 while (__f != __l)
717 {
Eric Fiselierb88ea352015-12-30 20:57:59 +0000718 __node_pointer __np = __f->__as_node();
Howard Hinnant920b56c2011-09-27 23:55:03 +0000719 __f = __f->__next_;
Eric Fiselierb88ea352015-12-30 20:57:59 +0000720 __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
721 __node_alloc_traits::deallocate(__na, __np, 1);
Howard Hinnant3e519522010-05-11 19:42:16 +0000722 }
Howard Hinnant920b56c2011-09-27 23:55:03 +0000723#if _LIBCPP_DEBUG_LEVEL >= 2
724 __c_node* __c = __get_db()->__find_c_and_lock(this);
725 for (__i_node** __p = __c->end_; __p != __c->beg_; )
726 {
727 --__p;
728 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
729 if (__i->__ptr_ != __l)
730 {
731 (*__p)->__c_ = nullptr;
732 if (--__c->end_ != __p)
733 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
734 }
735 }
736 __get_db()->unlock();
737#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000738 }
739}
740
741template <class _Tp, class _Alloc>
742void
743__list_imp<_Tp, _Alloc>::swap(__list_imp& __c)
Marshall Clowe3fbe142015-07-13 20:04:56 +0000744#if _LIBCPP_STD_VER >= 14
745 _NOEXCEPT
746#else
747 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
748 __is_nothrow_swappable<allocator_type>::value)
749#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000750{
Howard Hinnant920b56c2011-09-27 23:55:03 +0000751 _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
752 this->__node_alloc() == __c.__node_alloc(),
753 "list::swap: Either propagate_on_container_swap must be true"
754 " or the allocators must compare equal");
Howard Hinnantce48a112011-06-30 21:18:19 +0000755 using _VSTD::swap;
Marshall Clowe3fbe142015-07-13 20:04:56 +0000756 __swap_allocator(__node_alloc(), __c.__node_alloc());
Howard Hinnant3e519522010-05-11 19:42:16 +0000757 swap(__sz(), __c.__sz());
758 swap(__end_, __c.__end_);
759 if (__sz() == 0)
Eric Fiselier5243e192016-01-04 03:27:52 +0000760 __end_.__next_ = __end_.__prev_ = __end_as_link();
Howard Hinnant3e519522010-05-11 19:42:16 +0000761 else
Eric Fiselier5243e192016-01-04 03:27:52 +0000762 __end_.__prev_->__next_ = __end_.__next_->__prev_ = __end_as_link();
Howard Hinnant3e519522010-05-11 19:42:16 +0000763 if (__c.__sz() == 0)
Eric Fiselier5243e192016-01-04 03:27:52 +0000764 __c.__end_.__next_ = __c.__end_.__prev_ = __c.__end_as_link();
Howard Hinnant3e519522010-05-11 19:42:16 +0000765 else
Eric Fiselier5243e192016-01-04 03:27:52 +0000766 __c.__end_.__prev_->__next_ = __c.__end_.__next_->__prev_ = __c.__end_as_link();
Marshall Clow28d65da2014-08-05 01:34:12 +0000767
Howard Hinnant920b56c2011-09-27 23:55:03 +0000768#if _LIBCPP_DEBUG_LEVEL >= 2
769 __libcpp_db* __db = __get_db();
770 __c_node* __cn1 = __db->__find_c_and_lock(this);
771 __c_node* __cn2 = __db->__find_c(&__c);
772 std::swap(__cn1->beg_, __cn2->beg_);
773 std::swap(__cn1->end_, __cn2->end_);
774 std::swap(__cn1->cap_, __cn2->cap_);
775 for (__i_node** __p = __cn1->end_; __p != __cn1->beg_;)
776 {
777 --__p;
778 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
Eric Fiselier5243e192016-01-04 03:27:52 +0000779 if (__i->__ptr_ == __c.__end_as_link())
Howard Hinnant920b56c2011-09-27 23:55:03 +0000780 {
781 __cn2->__add(*__p);
782 if (--__cn1->end_ != __p)
783 memmove(__p, __p+1, (__cn1->end_ - __p)*sizeof(__i_node*));
784 }
785 else
786 (*__p)->__c_ = __cn1;
787 }
788 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
789 {
790 --__p;
791 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
Eric Fiselier5243e192016-01-04 03:27:52 +0000792 if (__i->__ptr_ == __end_as_link())
Howard Hinnant920b56c2011-09-27 23:55:03 +0000793 {
794 __cn1->__add(*__p);
795 if (--__cn2->end_ != __p)
796 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
797 }
798 else
799 (*__p)->__c_ = __cn2;
800 }
801 __db->unlock();
802#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000803}
804
Marshall Clowb5d34aa2015-02-18 17:24:08 +0000805template <class _Tp, class _Alloc /*= allocator<_Tp>*/>
Howard Hinnantf0544c22013-08-12 18:38:34 +0000806class _LIBCPP_TYPE_VIS_ONLY list
Howard Hinnant3e519522010-05-11 19:42:16 +0000807 : private __list_imp<_Tp, _Alloc>
808{
809 typedef __list_imp<_Tp, _Alloc> base;
810 typedef typename base::__node __node;
811 typedef typename base::__node_allocator __node_allocator;
812 typedef typename base::__node_pointer __node_pointer;
813 typedef typename base::__node_alloc_traits __node_alloc_traits;
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000814 typedef typename base::__node_base __node_base;
815 typedef typename base::__node_base_pointer __node_base_pointer;
Eric Fiselierb88ea352015-12-30 20:57:59 +0000816 typedef typename base::__link_pointer __link_pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000817
818public:
819 typedef _Tp value_type;
820 typedef _Alloc allocator_type;
821 static_assert((is_same<value_type, typename allocator_type::value_type>::value),
822 "Invalid allocator::value_type");
823 typedef value_type& reference;
824 typedef const value_type& const_reference;
825 typedef typename base::pointer pointer;
826 typedef typename base::const_pointer const_pointer;
827 typedef typename base::size_type size_type;
828 typedef typename base::difference_type difference_type;
829 typedef typename base::iterator iterator;
830 typedef typename base::const_iterator const_iterator;
Howard Hinnantce48a112011-06-30 21:18:19 +0000831 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
832 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnant3e519522010-05-11 19:42:16 +0000833
Howard Hinnant848a5372010-09-22 16:48:34 +0000834 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000835 list()
836 _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value)
Howard Hinnant920b56c2011-09-27 23:55:03 +0000837 {
838#if _LIBCPP_DEBUG_LEVEL >= 2
839 __get_db()->__insert_c(this);
840#endif
841 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000842 _LIBCPP_INLINE_VISIBILITY
Marshall Clowfb829762013-09-08 19:11:51 +0000843 explicit list(const allocator_type& __a) : base(__a)
Howard Hinnant920b56c2011-09-27 23:55:03 +0000844 {
845#if _LIBCPP_DEBUG_LEVEL >= 2
846 __get_db()->__insert_c(this);
847#endif
848 }
Marshall Clowfb829762013-09-08 19:11:51 +0000849 explicit list(size_type __n);
850#if _LIBCPP_STD_VER > 11
851 explicit list(size_type __n, const allocator_type& __a);
852#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000853 list(size_type __n, const value_type& __x);
854 list(size_type __n, const value_type& __x, const allocator_type& __a);
855 template <class _InpIter>
856 list(_InpIter __f, _InpIter __l,
857 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
858 template <class _InpIter>
859 list(_InpIter __f, _InpIter __l, const allocator_type& __a,
860 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
861
862 list(const list& __c);
863 list(const list& __c, const allocator_type& __a);
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +0000864 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000865 list& operator=(const list& __c);
Howard Hinnant54976f22011-08-12 21:56:02 +0000866#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant3e519522010-05-11 19:42:16 +0000867 list(initializer_list<value_type> __il);
868 list(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnant54976f22011-08-12 21:56:02 +0000869#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000870#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +0000871 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000872 list(list&& __c)
873 _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value);
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +0000874 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000875 list(list&& __c, const allocator_type& __a);
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +0000876 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000877 list& operator=(list&& __c)
878 _NOEXCEPT_(
879 __node_alloc_traits::propagate_on_container_move_assignment::value &&
880 is_nothrow_move_assignable<__node_allocator>::value);
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000881#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54976f22011-08-12 21:56:02 +0000882#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant848a5372010-09-22 16:48:34 +0000883 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000884 list& operator=(initializer_list<value_type> __il)
885 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnant54976f22011-08-12 21:56:02 +0000886#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant3e519522010-05-11 19:42:16 +0000887
888 template <class _InpIter>
889 void assign(_InpIter __f, _InpIter __l,
890 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
891 void assign(size_type __n, const value_type& __x);
Howard Hinnant54976f22011-08-12 21:56:02 +0000892#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant848a5372010-09-22 16:48:34 +0000893 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000894 void assign(initializer_list<value_type> __il)
895 {assign(__il.begin(), __il.end());}
Howard Hinnant54976f22011-08-12 21:56:02 +0000896#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant3e519522010-05-11 19:42:16 +0000897
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +0000898 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000899 allocator_type get_allocator() const _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +0000900
Howard Hinnant848a5372010-09-22 16:48:34 +0000901 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000902 size_type size() const _NOEXCEPT {return base::__sz();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000903 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000904 bool empty() const _NOEXCEPT {return base::empty();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000905 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000906 size_type max_size() const _NOEXCEPT
907 {return numeric_limits<difference_type>::max();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000908
Howard Hinnant848a5372010-09-22 16:48:34 +0000909 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000910 iterator begin() _NOEXCEPT {return base::begin();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000911 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000912 const_iterator begin() const _NOEXCEPT {return base::begin();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000913 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000914 iterator end() _NOEXCEPT {return base::end();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000915 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000916 const_iterator end() const _NOEXCEPT {return base::end();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000917 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000918 const_iterator cbegin() const _NOEXCEPT {return base::begin();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000919 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000920 const_iterator cend() const _NOEXCEPT {return base::end();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000921
Howard Hinnant848a5372010-09-22 16:48:34 +0000922 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000923 reverse_iterator rbegin() _NOEXCEPT
924 {return reverse_iterator(end());}
Howard Hinnant848a5372010-09-22 16:48:34 +0000925 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000926 const_reverse_iterator rbegin() const _NOEXCEPT
927 {return const_reverse_iterator(end());}
Howard Hinnant848a5372010-09-22 16:48:34 +0000928 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000929 reverse_iterator rend() _NOEXCEPT
930 {return reverse_iterator(begin());}
Howard Hinnant848a5372010-09-22 16:48:34 +0000931 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000932 const_reverse_iterator rend() const _NOEXCEPT
933 {return const_reverse_iterator(begin());}
Howard Hinnant848a5372010-09-22 16:48:34 +0000934 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000935 const_reverse_iterator crbegin() const _NOEXCEPT
936 {return const_reverse_iterator(end());}
Howard Hinnant848a5372010-09-22 16:48:34 +0000937 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000938 const_reverse_iterator crend() const _NOEXCEPT
939 {return const_reverse_iterator(begin());}
Howard Hinnant3e519522010-05-11 19:42:16 +0000940
Howard Hinnant848a5372010-09-22 16:48:34 +0000941 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000942 reference front()
943 {
944 _LIBCPP_ASSERT(!empty(), "list::front called on empty list");
Eric Fiselierb88ea352015-12-30 20:57:59 +0000945 return base::__end_.__next_->__as_node()->__value_;
Howard Hinnant920b56c2011-09-27 23:55:03 +0000946 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000947 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000948 const_reference front() const
949 {
950 _LIBCPP_ASSERT(!empty(), "list::front called on empty list");
Eric Fiselierb88ea352015-12-30 20:57:59 +0000951 return base::__end_.__next_->__as_node()->__value_;
Howard Hinnant920b56c2011-09-27 23:55:03 +0000952 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000953 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000954 reference back()
955 {
956 _LIBCPP_ASSERT(!empty(), "list::back called on empty list");
Eric Fiselierb88ea352015-12-30 20:57:59 +0000957 return base::__end_.__prev_->__as_node()->__value_;
Howard Hinnant920b56c2011-09-27 23:55:03 +0000958 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000959 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000960 const_reference back() const
961 {
962 _LIBCPP_ASSERT(!empty(), "list::back called on empty list");
Eric Fiselierb88ea352015-12-30 20:57:59 +0000963 return base::__end_.__prev_->__as_node()->__value_;
Howard Hinnant920b56c2011-09-27 23:55:03 +0000964 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000965
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000966#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +0000967 void push_front(value_type&& __x);
968 void push_back(value_type&& __x);
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000969#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnant3e519522010-05-11 19:42:16 +0000970 template <class... _Args>
971 void emplace_front(_Args&&... __args);
972 template <class... _Args>
973 void emplace_back(_Args&&... __args);
974 template <class... _Args>
975 iterator emplace(const_iterator __p, _Args&&... __args);
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000976#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnant3e519522010-05-11 19:42:16 +0000977 iterator insert(const_iterator __p, value_type&& __x);
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000978#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +0000979
980 void push_front(const value_type& __x);
981 void push_back(const value_type& __x);
982
983 iterator insert(const_iterator __p, const value_type& __x);
984 iterator insert(const_iterator __p, size_type __n, const value_type& __x);
985 template <class _InpIter>
986 iterator insert(const_iterator __p, _InpIter __f, _InpIter __l,
987 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
Howard Hinnant54976f22011-08-12 21:56:02 +0000988#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant848a5372010-09-22 16:48:34 +0000989 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000990 iterator insert(const_iterator __p, initializer_list<value_type> __il)
991 {return insert(__p, __il.begin(), __il.end());}
Howard Hinnant54976f22011-08-12 21:56:02 +0000992#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant3e519522010-05-11 19:42:16 +0000993
Howard Hinnant848a5372010-09-22 16:48:34 +0000994 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000995 void swap(list& __c)
Marshall Clowe3fbe142015-07-13 20:04:56 +0000996#if _LIBCPP_STD_VER >= 14
997 _NOEXCEPT
998#else
Howard Hinnant45900102011-06-03 17:30:28 +0000999 _NOEXCEPT_(!__node_alloc_traits::propagate_on_container_swap::value ||
1000 __is_nothrow_swappable<__node_allocator>::value)
Marshall Clowe3fbe142015-07-13 20:04:56 +00001001#endif
Howard Hinnant45900102011-06-03 17:30:28 +00001002 {base::swap(__c);}
Howard Hinnant848a5372010-09-22 16:48:34 +00001003 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +00001004 void clear() _NOEXCEPT {base::clear();}
Howard Hinnant3e519522010-05-11 19:42:16 +00001005
1006 void pop_front();
1007 void pop_back();
1008
1009 iterator erase(const_iterator __p);
1010 iterator erase(const_iterator __f, const_iterator __l);
1011
1012 void resize(size_type __n);
1013 void resize(size_type __n, const value_type& __x);
1014
1015 void splice(const_iterator __p, list& __c);
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001016#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant848a5372010-09-22 16:48:34 +00001017 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001018 void splice(const_iterator __p, list&& __c) {splice(__p, __c);}
1019#endif
1020 void splice(const_iterator __p, list& __c, const_iterator __i);
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001021#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant848a5372010-09-22 16:48:34 +00001022 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001023 void splice(const_iterator __p, list&& __c, const_iterator __i)
1024 {splice(__p, __c, __i);}
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001025#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +00001026 void splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l);
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001027#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant848a5372010-09-22 16:48:34 +00001028 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001029 void splice(const_iterator __p, list&& __c, const_iterator __f, const_iterator __l)
1030 {splice(__p, __c, __f, __l);}
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001031#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +00001032
1033 void remove(const value_type& __x);
1034 template <class _Pred> void remove_if(_Pred __pred);
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001035 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001036 void unique();
1037 template <class _BinaryPred>
1038 void unique(_BinaryPred __binary_pred);
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001039 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001040 void merge(list& __c);
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001041#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant848a5372010-09-22 16:48:34 +00001042 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001043 void merge(list&& __c) {merge(__c);}
1044#endif
1045 template <class _Comp>
1046 void merge(list& __c, _Comp __comp);
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001047#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +00001048 template <class _Comp>
Howard Hinnant848a5372010-09-22 16:48:34 +00001049 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001050 void merge(list&& __c, _Comp __comp) {merge(__c, __comp);}
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001051#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001052 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001053 void sort();
1054 template <class _Comp>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001055 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001056 void sort(_Comp __comp);
1057
Howard Hinnant45900102011-06-03 17:30:28 +00001058 void reverse() _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +00001059
Howard Hinnant920b56c2011-09-27 23:55:03 +00001060 bool __invariants() const;
1061
1062#if _LIBCPP_DEBUG_LEVEL >= 2
1063
1064 bool __dereferenceable(const const_iterator* __i) const;
1065 bool __decrementable(const const_iterator* __i) const;
1066 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1067 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1068
1069#endif // _LIBCPP_DEBUG_LEVEL >= 2
1070
Howard Hinnant3e519522010-05-11 19:42:16 +00001071private:
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001072 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierb88ea352015-12-30 20:57:59 +00001073 static void __link_nodes (__link_pointer __p, __link_pointer __f, __link_pointer __l);
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001074 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierb88ea352015-12-30 20:57:59 +00001075 void __link_nodes_at_front(__link_pointer __f, __link_pointer __l);
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001076 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierb88ea352015-12-30 20:57:59 +00001077 void __link_nodes_at_back (__link_pointer __f, __link_pointer __l);
Howard Hinnant3e519522010-05-11 19:42:16 +00001078 iterator __iterator(size_type __n);
1079 template <class _Comp>
1080 static iterator __sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp);
1081
Howard Hinnant45900102011-06-03 17:30:28 +00001082 void __move_assign(list& __c, true_type)
1083 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value);
Howard Hinnant3e519522010-05-11 19:42:16 +00001084 void __move_assign(list& __c, false_type);
1085};
1086
1087// Link in nodes [__f, __l] just prior to __p
1088template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001089inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001090void
Eric Fiselierb88ea352015-12-30 20:57:59 +00001091list<_Tp, _Alloc>::__link_nodes(__link_pointer __p, __link_pointer __f, __link_pointer __l)
Howard Hinnant3e519522010-05-11 19:42:16 +00001092{
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001093 __p->__prev_->__next_ = __f;
1094 __f->__prev_ = __p->__prev_;
1095 __p->__prev_ = __l;
1096 __l->__next_ = __p;
Howard Hinnant3e519522010-05-11 19:42:16 +00001097}
1098
Marshall Clow28d65da2014-08-05 01:34:12 +00001099// Link in nodes [__f, __l] at the front of the list
1100template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001101inline
Marshall Clow28d65da2014-08-05 01:34:12 +00001102void
Eric Fiselierb88ea352015-12-30 20:57:59 +00001103list<_Tp, _Alloc>::__link_nodes_at_front(__link_pointer __f, __link_pointer __l)
Marshall Clow28d65da2014-08-05 01:34:12 +00001104{
Eric Fiselier5243e192016-01-04 03:27:52 +00001105 __f->__prev_ = base::__end_as_link();
Marshall Clowced70062014-08-08 15:35:52 +00001106 __l->__next_ = base::__end_.__next_;
1107 __l->__next_->__prev_ = __l;
1108 base::__end_.__next_ = __f;
Marshall Clow28d65da2014-08-05 01:34:12 +00001109}
1110
1111// Link in nodes [__f, __l] at the front of the list
1112template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001113inline
Marshall Clow28d65da2014-08-05 01:34:12 +00001114void
Eric Fiselierb88ea352015-12-30 20:57:59 +00001115list<_Tp, _Alloc>::__link_nodes_at_back(__link_pointer __f, __link_pointer __l)
Marshall Clow28d65da2014-08-05 01:34:12 +00001116{
Eric Fiselier5243e192016-01-04 03:27:52 +00001117 __l->__next_ = base::__end_as_link();
Marshall Clowced70062014-08-08 15:35:52 +00001118 __f->__prev_ = base::__end_.__prev_;
1119 __f->__prev_->__next_ = __f;
1120 base::__end_.__prev_ = __l;
Marshall Clow28d65da2014-08-05 01:34:12 +00001121}
1122
1123
Howard Hinnant3e519522010-05-11 19:42:16 +00001124template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001125inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001126typename list<_Tp, _Alloc>::iterator
1127list<_Tp, _Alloc>::__iterator(size_type __n)
1128{
Howard Hinnantce48a112011-06-30 21:18:19 +00001129 return __n <= base::__sz() / 2 ? _VSTD::next(begin(), __n)
1130 : _VSTD::prev(end(), base::__sz() - __n);
Howard Hinnant3e519522010-05-11 19:42:16 +00001131}
1132
1133template <class _Tp, class _Alloc>
1134list<_Tp, _Alloc>::list(size_type __n)
1135{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001136#if _LIBCPP_DEBUG_LEVEL >= 2
1137 __get_db()->__insert_c(this);
1138#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001139 for (; __n > 0; --__n)
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001140#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +00001141 emplace_back();
1142#else
1143 push_back(value_type());
1144#endif
1145}
1146
Marshall Clowfb829762013-09-08 19:11:51 +00001147#if _LIBCPP_STD_VER > 11
1148template <class _Tp, class _Alloc>
1149list<_Tp, _Alloc>::list(size_type __n, const allocator_type& __a) : base(__a)
1150{
1151#if _LIBCPP_DEBUG_LEVEL >= 2
1152 __get_db()->__insert_c(this);
1153#endif
1154 for (; __n > 0; --__n)
1155#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1156 emplace_back();
1157#else
1158 push_back(value_type());
1159#endif
1160}
1161#endif
1162
Howard Hinnant3e519522010-05-11 19:42:16 +00001163template <class _Tp, class _Alloc>
1164list<_Tp, _Alloc>::list(size_type __n, const value_type& __x)
1165{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001166#if _LIBCPP_DEBUG_LEVEL >= 2
1167 __get_db()->__insert_c(this);
1168#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001169 for (; __n > 0; --__n)
1170 push_back(__x);
1171}
1172
1173template <class _Tp, class _Alloc>
1174list<_Tp, _Alloc>::list(size_type __n, const value_type& __x, const allocator_type& __a)
1175 : base(__a)
1176{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001177#if _LIBCPP_DEBUG_LEVEL >= 2
1178 __get_db()->__insert_c(this);
1179#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001180 for (; __n > 0; --__n)
1181 push_back(__x);
1182}
1183
1184template <class _Tp, class _Alloc>
1185template <class _InpIter>
1186list<_Tp, _Alloc>::list(_InpIter __f, _InpIter __l,
1187 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1188{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001189#if _LIBCPP_DEBUG_LEVEL >= 2
1190 __get_db()->__insert_c(this);
1191#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001192 for (; __f != __l; ++__f)
1193 push_back(*__f);
1194}
1195
1196template <class _Tp, class _Alloc>
1197template <class _InpIter>
1198list<_Tp, _Alloc>::list(_InpIter __f, _InpIter __l, const allocator_type& __a,
1199 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1200 : base(__a)
1201{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001202#if _LIBCPP_DEBUG_LEVEL >= 2
1203 __get_db()->__insert_c(this);
1204#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001205 for (; __f != __l; ++__f)
1206 push_back(*__f);
1207}
1208
1209template <class _Tp, class _Alloc>
1210list<_Tp, _Alloc>::list(const list& __c)
1211 : base(allocator_type(
1212 __node_alloc_traits::select_on_container_copy_construction(
1213 __c.__node_alloc())))
1214{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001215#if _LIBCPP_DEBUG_LEVEL >= 2
1216 __get_db()->__insert_c(this);
1217#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001218 for (const_iterator __i = __c.begin(), __e = __c.end(); __i != __e; ++__i)
1219 push_back(*__i);
1220}
1221
1222template <class _Tp, class _Alloc>
1223list<_Tp, _Alloc>::list(const list& __c, const allocator_type& __a)
1224 : base(__a)
1225{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001226#if _LIBCPP_DEBUG_LEVEL >= 2
1227 __get_db()->__insert_c(this);
1228#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001229 for (const_iterator __i = __c.begin(), __e = __c.end(); __i != __e; ++__i)
1230 push_back(*__i);
1231}
1232
Howard Hinnant54976f22011-08-12 21:56:02 +00001233#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1234
Howard Hinnant3e519522010-05-11 19:42:16 +00001235template <class _Tp, class _Alloc>
1236list<_Tp, _Alloc>::list(initializer_list<value_type> __il, const allocator_type& __a)
1237 : base(__a)
1238{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001239#if _LIBCPP_DEBUG_LEVEL >= 2
1240 __get_db()->__insert_c(this);
1241#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001242 for (typename initializer_list<value_type>::const_iterator __i = __il.begin(),
1243 __e = __il.end(); __i != __e; ++__i)
1244 push_back(*__i);
1245}
1246
1247template <class _Tp, class _Alloc>
1248list<_Tp, _Alloc>::list(initializer_list<value_type> __il)
1249{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001250#if _LIBCPP_DEBUG_LEVEL >= 2
1251 __get_db()->__insert_c(this);
1252#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001253 for (typename initializer_list<value_type>::const_iterator __i = __il.begin(),
1254 __e = __il.end(); __i != __e; ++__i)
1255 push_back(*__i);
1256}
1257
Howard Hinnant54976f22011-08-12 21:56:02 +00001258#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1259
Howard Hinnant3e519522010-05-11 19:42:16 +00001260template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001261inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001262list<_Tp, _Alloc>&
1263list<_Tp, _Alloc>::operator=(const list& __c)
1264{
1265 if (this != &__c)
1266 {
1267 base::__copy_assign_alloc(__c);
1268 assign(__c.begin(), __c.end());
1269 }
1270 return *this;
1271}
1272
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001273#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +00001274
1275template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001276inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001277list<_Tp, _Alloc>::list(list&& __c)
Howard Hinnant45900102011-06-03 17:30:28 +00001278 _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value)
Howard Hinnantce48a112011-06-30 21:18:19 +00001279 : base(allocator_type(_VSTD::move(__c.__node_alloc())))
Howard Hinnant3e519522010-05-11 19:42:16 +00001280{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001281#if _LIBCPP_DEBUG_LEVEL >= 2
1282 __get_db()->__insert_c(this);
1283#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001284 splice(end(), __c);
1285}
1286
1287template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001288inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001289list<_Tp, _Alloc>::list(list&& __c, const allocator_type& __a)
1290 : base(__a)
1291{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001292#if _LIBCPP_DEBUG_LEVEL >= 2
1293 __get_db()->__insert_c(this);
1294#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001295 if (__a == __c.get_allocator())
1296 splice(end(), __c);
1297 else
1298 {
Howard Hinnantc003db12011-11-29 18:15:50 +00001299 typedef move_iterator<iterator> _Ip;
1300 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnant3e519522010-05-11 19:42:16 +00001301 }
1302}
1303
1304template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001305inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001306list<_Tp, _Alloc>&
1307list<_Tp, _Alloc>::operator=(list&& __c)
Howard Hinnant45900102011-06-03 17:30:28 +00001308 _NOEXCEPT_(
1309 __node_alloc_traits::propagate_on_container_move_assignment::value &&
1310 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +00001311{
1312 __move_assign(__c, integral_constant<bool,
1313 __node_alloc_traits::propagate_on_container_move_assignment::value>());
1314 return *this;
1315}
1316
1317template <class _Tp, class _Alloc>
1318void
1319list<_Tp, _Alloc>::__move_assign(list& __c, false_type)
1320{
1321 if (base::__node_alloc() != __c.__node_alloc())
1322 {
Howard Hinnantc003db12011-11-29 18:15:50 +00001323 typedef move_iterator<iterator> _Ip;
1324 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnant3e519522010-05-11 19:42:16 +00001325 }
1326 else
1327 __move_assign(__c, true_type());
1328}
1329
1330template <class _Tp, class _Alloc>
1331void
1332list<_Tp, _Alloc>::__move_assign(list& __c, true_type)
Howard Hinnant45900102011-06-03 17:30:28 +00001333 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +00001334{
1335 clear();
1336 base::__move_assign_alloc(__c);
1337 splice(end(), __c);
1338}
1339
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001340#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +00001341
1342template <class _Tp, class _Alloc>
1343template <class _InpIter>
1344void
1345list<_Tp, _Alloc>::assign(_InpIter __f, _InpIter __l,
1346 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1347{
1348 iterator __i = begin();
1349 iterator __e = end();
1350 for (; __f != __l && __i != __e; ++__f, ++__i)
1351 *__i = *__f;
1352 if (__i == __e)
1353 insert(__e, __f, __l);
1354 else
1355 erase(__i, __e);
1356}
1357
1358template <class _Tp, class _Alloc>
1359void
1360list<_Tp, _Alloc>::assign(size_type __n, const value_type& __x)
1361{
1362 iterator __i = begin();
1363 iterator __e = end();
1364 for (; __n > 0 && __i != __e; --__n, ++__i)
1365 *__i = __x;
1366 if (__i == __e)
1367 insert(__e, __n, __x);
1368 else
1369 erase(__i, __e);
1370}
1371
1372template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00001373inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001374_Alloc
Howard Hinnant45900102011-06-03 17:30:28 +00001375list<_Tp, _Alloc>::get_allocator() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001376{
1377 return allocator_type(base::__node_alloc());
1378}
1379
1380template <class _Tp, class _Alloc>
1381typename list<_Tp, _Alloc>::iterator
1382list<_Tp, _Alloc>::insert(const_iterator __p, const value_type& __x)
1383{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001384#if _LIBCPP_DEBUG_LEVEL >= 2
1385 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1386 "list::insert(iterator, x) called with an iterator not"
1387 " referring to this list");
1388#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001389 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001390 typedef __allocator_destructor<__node_allocator> _Dp;
1391 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant3e519522010-05-11 19:42:16 +00001392 __hold->__prev_ = 0;
Howard Hinnantce48a112011-06-30 21:18:19 +00001393 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001394 __link_nodes(__p.__ptr_, __hold->__as_link(), __hold->__as_link());
Howard Hinnant3e519522010-05-11 19:42:16 +00001395 ++base::__sz();
Howard Hinnantb0e4c9d2013-04-05 00:18:49 +00001396#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselierb88ea352015-12-30 20:57:59 +00001397 return iterator(__hold.release()->__as_link(), this);
Howard Hinnantb0e4c9d2013-04-05 00:18:49 +00001398#else
Eric Fiselierb88ea352015-12-30 20:57:59 +00001399 return iterator(__hold.release()->__as_link());
Howard Hinnantb0e4c9d2013-04-05 00:18:49 +00001400#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001401}
1402
1403template <class _Tp, class _Alloc>
1404typename list<_Tp, _Alloc>::iterator
1405list<_Tp, _Alloc>::insert(const_iterator __p, size_type __n, const value_type& __x)
1406{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001407#if _LIBCPP_DEBUG_LEVEL >= 2
1408 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1409 "list::insert(iterator, n, x) called with an iterator not"
1410 " referring to this list");
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001411 iterator __r(__p.__ptr_, this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001412#else
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001413 iterator __r(__p.__ptr_);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001414#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001415 if (__n > 0)
1416 {
1417 size_type __ds = 0;
1418 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001419 typedef __allocator_destructor<__node_allocator> _Dp;
1420 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant3e519522010-05-11 19:42:16 +00001421 __hold->__prev_ = 0;
Howard Hinnantce48a112011-06-30 21:18:19 +00001422 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnant3e519522010-05-11 19:42:16 +00001423 ++__ds;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001424#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselierb88ea352015-12-30 20:57:59 +00001425 __r = iterator(__hold->__as_link(), this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001426#else
Eric Fiselierb88ea352015-12-30 20:57:59 +00001427 __r = iterator(__hold->__as_link());
Howard Hinnant920b56c2011-09-27 23:55:03 +00001428#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001429 __hold.release();
1430 iterator __e = __r;
1431#ifndef _LIBCPP_NO_EXCEPTIONS
1432 try
1433 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001434#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001435 for (--__n; __n != 0; --__n, ++__e, ++__ds)
1436 {
1437 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001438 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001439 __e.__ptr_->__next_ = __hold->__as_link();
Howard Hinnant3e519522010-05-11 19:42:16 +00001440 __hold->__prev_ = __e.__ptr_;
1441 __hold.release();
1442 }
1443#ifndef _LIBCPP_NO_EXCEPTIONS
1444 }
1445 catch (...)
1446 {
1447 while (true)
1448 {
Howard Hinnantce48a112011-06-30 21:18:19 +00001449 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Eric Fiselierb88ea352015-12-30 20:57:59 +00001450 __link_pointer __prev = __e.__ptr_->__prev_;
1451 __node_alloc_traits::deallocate(__na, __e.__ptr_->__as_node(), 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001452 if (__prev == 0)
1453 break;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001454#if _LIBCPP_DEBUG_LEVEL >= 2
1455 __e = iterator(__prev, this);
1456#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001457 __e = iterator(__prev);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001458#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001459 }
1460 throw;
1461 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001462#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001463 __link_nodes(__p.__ptr_, __r.__ptr_, __e.__ptr_);
Howard Hinnant3e519522010-05-11 19:42:16 +00001464 base::__sz() += __ds;
1465 }
1466 return __r;
1467}
1468
1469template <class _Tp, class _Alloc>
1470template <class _InpIter>
1471typename list<_Tp, _Alloc>::iterator
1472list<_Tp, _Alloc>::insert(const_iterator __p, _InpIter __f, _InpIter __l,
1473 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1474{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001475#if _LIBCPP_DEBUG_LEVEL >= 2
1476 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1477 "list::insert(iterator, range) called with an iterator not"
1478 " referring to this list");
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001479 iterator __r(__p.__ptr_, this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001480#else
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001481 iterator __r(__p.__ptr_);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001482#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001483 if (__f != __l)
1484 {
1485 size_type __ds = 0;
1486 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001487 typedef __allocator_destructor<__node_allocator> _Dp;
1488 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant3e519522010-05-11 19:42:16 +00001489 __hold->__prev_ = 0;
Howard Hinnantce48a112011-06-30 21:18:19 +00001490 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), *__f);
Howard Hinnant3e519522010-05-11 19:42:16 +00001491 ++__ds;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001492#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselierb88ea352015-12-30 20:57:59 +00001493 __r = iterator(__hold.get()->__as_link(), this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001494#else
Eric Fiselierb88ea352015-12-30 20:57:59 +00001495 __r = iterator(__hold.get()->__as_link());
Howard Hinnant920b56c2011-09-27 23:55:03 +00001496#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001497 __hold.release();
1498 iterator __e = __r;
1499#ifndef _LIBCPP_NO_EXCEPTIONS
1500 try
1501 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001502#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiselier61bff612015-03-19 03:20:02 +00001503 for (++__f; __f != __l; ++__f, (void) ++__e, (void) ++__ds)
Howard Hinnant3e519522010-05-11 19:42:16 +00001504 {
1505 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001506 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), *__f);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001507 __e.__ptr_->__next_ = __hold.get()->__as_link();
Howard Hinnant3e519522010-05-11 19:42:16 +00001508 __hold->__prev_ = __e.__ptr_;
1509 __hold.release();
1510 }
1511#ifndef _LIBCPP_NO_EXCEPTIONS
1512 }
1513 catch (...)
1514 {
1515 while (true)
1516 {
Howard Hinnantce48a112011-06-30 21:18:19 +00001517 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Eric Fiselierb88ea352015-12-30 20:57:59 +00001518 __link_pointer __prev = __e.__ptr_->__prev_;
1519 __node_alloc_traits::deallocate(__na, __e.__ptr_->__as_node(), 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001520 if (__prev == 0)
1521 break;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001522#if _LIBCPP_DEBUG_LEVEL >= 2
1523 __e = iterator(__prev, this);
1524#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001525 __e = iterator(__prev);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001526#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001527 }
1528 throw;
1529 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001530#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001531 __link_nodes(__p.__ptr_, __r.__ptr_, __e.__ptr_);
Howard Hinnant3e519522010-05-11 19:42:16 +00001532 base::__sz() += __ds;
1533 }
1534 return __r;
1535}
1536
1537template <class _Tp, class _Alloc>
1538void
1539list<_Tp, _Alloc>::push_front(const value_type& __x)
1540{
1541 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001542 typedef __allocator_destructor<__node_allocator> _Dp;
1543 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001544 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001545 __link_pointer __nl = __hold->__as_link();
1546 __link_nodes_at_front(__nl, __nl);
Howard Hinnant3e519522010-05-11 19:42:16 +00001547 ++base::__sz();
1548 __hold.release();
1549}
1550
1551template <class _Tp, class _Alloc>
1552void
1553list<_Tp, _Alloc>::push_back(const value_type& __x)
1554{
1555 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001556 typedef __allocator_destructor<__node_allocator> _Dp;
1557 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001558 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001559 __link_nodes_at_back(__hold.get()->__as_link(), __hold.get()->__as_link());
Howard Hinnant3e519522010-05-11 19:42:16 +00001560 ++base::__sz();
1561 __hold.release();
1562}
1563
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001564#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +00001565
1566template <class _Tp, class _Alloc>
1567void
1568list<_Tp, _Alloc>::push_front(value_type&& __x)
1569{
1570 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001571 typedef __allocator_destructor<__node_allocator> _Dp;
1572 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001573 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
Eric Fiselierb88ea352015-12-30 20:57:59 +00001574 __link_nodes_at_front(__hold.get()->__as_link(), __hold.get()->__as_link());
Howard Hinnant3e519522010-05-11 19:42:16 +00001575 ++base::__sz();
1576 __hold.release();
1577}
1578
1579template <class _Tp, class _Alloc>
1580void
1581list<_Tp, _Alloc>::push_back(value_type&& __x)
1582{
1583 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001584 typedef __allocator_destructor<__node_allocator> _Dp;
1585 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001586 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
Eric Fiselierb88ea352015-12-30 20:57:59 +00001587 __link_nodes_at_back(__hold.get()->__as_link(), __hold.get()->__as_link());
Howard Hinnant3e519522010-05-11 19:42:16 +00001588 ++base::__sz();
1589 __hold.release();
1590}
1591
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001592#ifndef _LIBCPP_HAS_NO_VARIADICS
1593
Howard Hinnant3e519522010-05-11 19:42:16 +00001594template <class _Tp, class _Alloc>
1595template <class... _Args>
1596void
1597list<_Tp, _Alloc>::emplace_front(_Args&&... __args)
1598{
1599 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001600 typedef __allocator_destructor<__node_allocator> _Dp;
1601 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001602 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001603 __link_nodes_at_front(__hold.get()->__as_link(), __hold.get()->__as_link());
Howard Hinnant3e519522010-05-11 19:42:16 +00001604 ++base::__sz();
1605 __hold.release();
1606}
1607
1608template <class _Tp, class _Alloc>
1609template <class... _Args>
1610void
1611list<_Tp, _Alloc>::emplace_back(_Args&&... __args)
1612{
1613 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001614 typedef __allocator_destructor<__node_allocator> _Dp;
1615 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001616 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001617 __link_pointer __nl = __hold->__as_link();
1618 __link_nodes_at_back(__nl, __nl);
Howard Hinnant3e519522010-05-11 19:42:16 +00001619 ++base::__sz();
1620 __hold.release();
1621}
1622
1623template <class _Tp, class _Alloc>
1624template <class... _Args>
1625typename list<_Tp, _Alloc>::iterator
1626list<_Tp, _Alloc>::emplace(const_iterator __p, _Args&&... __args)
1627{
Howard Hinnantb0e4c9d2013-04-05 00:18:49 +00001628#if _LIBCPP_DEBUG_LEVEL >= 2
1629 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1630 "list::emplace(iterator, args...) called with an iterator not"
1631 " referring to this list");
1632#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001633 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001634 typedef __allocator_destructor<__node_allocator> _Dp;
1635 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant3e519522010-05-11 19:42:16 +00001636 __hold->__prev_ = 0;
Howard Hinnantce48a112011-06-30 21:18:19 +00001637 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001638 __link_pointer __nl = __hold.get()->__as_link();
1639 __link_nodes(__p.__ptr_, __nl, __nl);
Howard Hinnant3e519522010-05-11 19:42:16 +00001640 ++base::__sz();
Eric Fiselierb88ea352015-12-30 20:57:59 +00001641 __hold.release();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001642#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselierb88ea352015-12-30 20:57:59 +00001643 return iterator(__nl, this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001644#else
Eric Fiselierb88ea352015-12-30 20:57:59 +00001645 return iterator(__nl);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001646#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001647}
1648
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001649#endif // _LIBCPP_HAS_NO_VARIADICS
1650
Howard Hinnant3e519522010-05-11 19:42:16 +00001651template <class _Tp, class _Alloc>
1652typename list<_Tp, _Alloc>::iterator
1653list<_Tp, _Alloc>::insert(const_iterator __p, value_type&& __x)
1654{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001655#if _LIBCPP_DEBUG_LEVEL >= 2
1656 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1657 "list::insert(iterator, x) called with an iterator not"
1658 " referring to this list");
1659#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001660 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001661 typedef __allocator_destructor<__node_allocator> _Dp;
1662 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant3e519522010-05-11 19:42:16 +00001663 __hold->__prev_ = 0;
Howard Hinnantce48a112011-06-30 21:18:19 +00001664 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
Eric Fiselierb88ea352015-12-30 20:57:59 +00001665 __link_pointer __nl = __hold->__as_link();
1666 __link_nodes(__p.__ptr_, __nl, __nl);
Howard Hinnant3e519522010-05-11 19:42:16 +00001667 ++base::__sz();
Eric Fiselierb88ea352015-12-30 20:57:59 +00001668 __hold.release();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001669#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselierb88ea352015-12-30 20:57:59 +00001670 return iterator(__nl, this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001671#else
Eric Fiselierb88ea352015-12-30 20:57:59 +00001672 return iterator(__nl);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001673#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001674}
1675
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001676#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +00001677
1678template <class _Tp, class _Alloc>
1679void
1680list<_Tp, _Alloc>::pop_front()
1681{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001682 _LIBCPP_ASSERT(!empty(), "list::pop_front() called with empty list");
Howard Hinnant3e519522010-05-11 19:42:16 +00001683 __node_allocator& __na = base::__node_alloc();
Eric Fiselierb88ea352015-12-30 20:57:59 +00001684 __link_pointer __n = base::__end_.__next_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001685 base::__unlink_nodes(__n, __n);
1686 --base::__sz();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001687#if _LIBCPP_DEBUG_LEVEL >= 2
1688 __c_node* __c = __get_db()->__find_c_and_lock(this);
1689 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1690 {
1691 --__p;
1692 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001693 if (__i->__ptr_ == __n)
Howard Hinnant920b56c2011-09-27 23:55:03 +00001694 {
1695 (*__p)->__c_ = nullptr;
1696 if (--__c->end_ != __p)
1697 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1698 }
1699 }
1700 __get_db()->unlock();
1701#endif
Eric Fiselierb88ea352015-12-30 20:57:59 +00001702 __node_pointer __np = __n->__as_node();
1703 __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
1704 __node_alloc_traits::deallocate(__na, __np, 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001705}
1706
1707template <class _Tp, class _Alloc>
1708void
1709list<_Tp, _Alloc>::pop_back()
1710{
Dmitri Gribenkoc3f9c802013-06-24 06:15:57 +00001711 _LIBCPP_ASSERT(!empty(), "list::pop_back() called with empty list");
Howard Hinnant3e519522010-05-11 19:42:16 +00001712 __node_allocator& __na = base::__node_alloc();
Eric Fiselierb88ea352015-12-30 20:57:59 +00001713 __link_pointer __n = base::__end_.__prev_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001714 base::__unlink_nodes(__n, __n);
1715 --base::__sz();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001716#if _LIBCPP_DEBUG_LEVEL >= 2
1717 __c_node* __c = __get_db()->__find_c_and_lock(this);
1718 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1719 {
1720 --__p;
1721 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001722 if (__i->__ptr_ == __n)
Howard Hinnant920b56c2011-09-27 23:55:03 +00001723 {
1724 (*__p)->__c_ = nullptr;
1725 if (--__c->end_ != __p)
1726 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1727 }
1728 }
1729 __get_db()->unlock();
1730#endif
Eric Fiselierb88ea352015-12-30 20:57:59 +00001731 __node_pointer __np = __n->__as_node();
1732 __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
1733 __node_alloc_traits::deallocate(__na, __np, 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001734}
1735
1736template <class _Tp, class _Alloc>
1737typename list<_Tp, _Alloc>::iterator
1738list<_Tp, _Alloc>::erase(const_iterator __p)
1739{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001740#if _LIBCPP_DEBUG_LEVEL >= 2
1741 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1742 "list::erase(iterator) called with an iterator not"
1743 " referring to this list");
1744#endif
Howard Hinnantb0e4c9d2013-04-05 00:18:49 +00001745 _LIBCPP_ASSERT(__p != end(),
1746 "list::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnant3e519522010-05-11 19:42:16 +00001747 __node_allocator& __na = base::__node_alloc();
Eric Fiselierb88ea352015-12-30 20:57:59 +00001748 __link_pointer __n = __p.__ptr_;
1749 __link_pointer __r = __n->__next_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001750 base::__unlink_nodes(__n, __n);
1751 --base::__sz();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001752#if _LIBCPP_DEBUG_LEVEL >= 2
1753 __c_node* __c = __get_db()->__find_c_and_lock(this);
1754 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1755 {
1756 --__p;
1757 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001758 if (__i->__ptr_ == __n)
Howard Hinnant920b56c2011-09-27 23:55:03 +00001759 {
1760 (*__p)->__c_ = nullptr;
1761 if (--__c->end_ != __p)
1762 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1763 }
1764 }
1765 __get_db()->unlock();
1766#endif
Eric Fiselierb88ea352015-12-30 20:57:59 +00001767 __node_pointer __np = __n->__as_node();
1768 __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
1769 __node_alloc_traits::deallocate(__na, __np, 1);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001770#if _LIBCPP_DEBUG_LEVEL >= 2
1771 return iterator(__r, this);
1772#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001773 return iterator(__r);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001774#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001775}
1776
1777template <class _Tp, class _Alloc>
1778typename list<_Tp, _Alloc>::iterator
1779list<_Tp, _Alloc>::erase(const_iterator __f, const_iterator __l)
1780{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001781#if _LIBCPP_DEBUG_LEVEL >= 2
1782 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__f) == this,
1783 "list::erase(iterator, iterator) called with an iterator not"
1784 " referring to this list");
1785#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001786 if (__f != __l)
1787 {
1788 __node_allocator& __na = base::__node_alloc();
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001789 base::__unlink_nodes(__f.__ptr_, __l.__ptr_->__prev_);
Howard Hinnant3e519522010-05-11 19:42:16 +00001790 while (__f != __l)
1791 {
Eric Fiselierb88ea352015-12-30 20:57:59 +00001792 __link_pointer __n = __f.__ptr_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001793 ++__f;
1794 --base::__sz();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001795#if _LIBCPP_DEBUG_LEVEL >= 2
1796 __c_node* __c = __get_db()->__find_c_and_lock(this);
1797 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1798 {
1799 --__p;
1800 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001801 if (__i->__ptr_ == __n)
Howard Hinnant920b56c2011-09-27 23:55:03 +00001802 {
1803 (*__p)->__c_ = nullptr;
1804 if (--__c->end_ != __p)
1805 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1806 }
1807 }
1808 __get_db()->unlock();
1809#endif
Eric Fiselierb88ea352015-12-30 20:57:59 +00001810 __node_pointer __np = __n->__as_node();
1811 __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
1812 __node_alloc_traits::deallocate(__na, __np, 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001813 }
1814 }
Howard Hinnant920b56c2011-09-27 23:55:03 +00001815#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001816 return iterator(__l.__ptr_, this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001817#else
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001818 return iterator(__l.__ptr_);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001819#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001820}
1821
1822template <class _Tp, class _Alloc>
1823void
1824list<_Tp, _Alloc>::resize(size_type __n)
1825{
1826 if (__n < base::__sz())
1827 erase(__iterator(__n), end());
1828 else if (__n > base::__sz())
1829 {
1830 __n -= base::__sz();
1831 size_type __ds = 0;
1832 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001833 typedef __allocator_destructor<__node_allocator> _Dp;
1834 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant3e519522010-05-11 19:42:16 +00001835 __hold->__prev_ = 0;
Howard Hinnantce48a112011-06-30 21:18:19 +00001836 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_));
Howard Hinnant3e519522010-05-11 19:42:16 +00001837 ++__ds;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001838#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselierb88ea352015-12-30 20:57:59 +00001839 iterator __r = iterator(__hold.release()->__as_link(), this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001840#else
Eric Fiselierb88ea352015-12-30 20:57:59 +00001841 iterator __r = iterator(__hold.release()->__as_link());
Howard Hinnant920b56c2011-09-27 23:55:03 +00001842#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001843 iterator __e = __r;
1844#ifndef _LIBCPP_NO_EXCEPTIONS
1845 try
1846 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001847#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001848 for (--__n; __n != 0; --__n, ++__e, ++__ds)
1849 {
1850 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001851 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_));
Eric Fiselierb88ea352015-12-30 20:57:59 +00001852 __e.__ptr_->__next_ = __hold.get()->__as_link();
Howard Hinnant3e519522010-05-11 19:42:16 +00001853 __hold->__prev_ = __e.__ptr_;
1854 __hold.release();
1855 }
1856#ifndef _LIBCPP_NO_EXCEPTIONS
1857 }
1858 catch (...)
1859 {
1860 while (true)
1861 {
Howard Hinnantce48a112011-06-30 21:18:19 +00001862 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Eric Fiselierb88ea352015-12-30 20:57:59 +00001863 __link_pointer __prev = __e.__ptr_->__prev_;
1864 __node_alloc_traits::deallocate(__na, __e.__ptr_->__as_node(), 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001865 if (__prev == 0)
1866 break;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001867#if _LIBCPP_DEBUG_LEVEL >= 2
1868 __e = iterator(__prev, this);
1869#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001870 __e = iterator(__prev);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001871#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001872 }
1873 throw;
1874 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001875#endif // _LIBCPP_NO_EXCEPTIONS
Marshall Clow28d65da2014-08-05 01:34:12 +00001876 __link_nodes_at_back(__r.__ptr_, __e.__ptr_);
Howard Hinnant3e519522010-05-11 19:42:16 +00001877 base::__sz() += __ds;
1878 }
1879}
1880
1881template <class _Tp, class _Alloc>
1882void
1883list<_Tp, _Alloc>::resize(size_type __n, const value_type& __x)
1884{
1885 if (__n < base::__sz())
1886 erase(__iterator(__n), end());
1887 else if (__n > base::__sz())
1888 {
1889 __n -= base::__sz();
1890 size_type __ds = 0;
1891 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001892 typedef __allocator_destructor<__node_allocator> _Dp;
1893 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant3e519522010-05-11 19:42:16 +00001894 __hold->__prev_ = 0;
Howard Hinnantce48a112011-06-30 21:18:19 +00001895 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnant3e519522010-05-11 19:42:16 +00001896 ++__ds;
Eric Fiselierb88ea352015-12-30 20:57:59 +00001897 __link_pointer __nl = __hold.release()->__as_link();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001898#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselierb88ea352015-12-30 20:57:59 +00001899 iterator __r = iterator(__nl, this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001900#else
Eric Fiselierb88ea352015-12-30 20:57:59 +00001901 iterator __r = iterator(__nl);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001902#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001903 iterator __e = __r;
1904#ifndef _LIBCPP_NO_EXCEPTIONS
1905 try
1906 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001907#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001908 for (--__n; __n != 0; --__n, ++__e, ++__ds)
1909 {
1910 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001911 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Eric Fiselierb88ea352015-12-30 20:57:59 +00001912 __e.__ptr_->__next_ = __hold.get()->__as_link();
Howard Hinnant3e519522010-05-11 19:42:16 +00001913 __hold->__prev_ = __e.__ptr_;
1914 __hold.release();
1915 }
1916#ifndef _LIBCPP_NO_EXCEPTIONS
1917 }
1918 catch (...)
1919 {
1920 while (true)
1921 {
Howard Hinnantce48a112011-06-30 21:18:19 +00001922 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Eric Fiselierb88ea352015-12-30 20:57:59 +00001923 __link_pointer __prev = __e.__ptr_->__prev_;
1924 __node_alloc_traits::deallocate(__na, __e.__ptr_->__as_node(), 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001925 if (__prev == 0)
1926 break;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001927#if _LIBCPP_DEBUG_LEVEL >= 2
1928 __e = iterator(__prev, this);
1929#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001930 __e = iterator(__prev);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001931#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001932 }
1933 throw;
1934 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001935#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiselier5243e192016-01-04 03:27:52 +00001936 __link_nodes(base::__end_as_link(), __r.__ptr_, __e.__ptr_);
Howard Hinnant3e519522010-05-11 19:42:16 +00001937 base::__sz() += __ds;
Howard Hinnantb3371f62010-08-22 00:02:43 +00001938 }
Howard Hinnant3e519522010-05-11 19:42:16 +00001939}
1940
1941template <class _Tp, class _Alloc>
1942void
1943list<_Tp, _Alloc>::splice(const_iterator __p, list& __c)
1944{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001945 _LIBCPP_ASSERT(this != &__c,
1946 "list::splice(iterator, list) called with this == &list");
1947#if _LIBCPP_DEBUG_LEVEL >= 2
1948 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1949 "list::splice(iterator, list) called with an iterator not"
1950 " referring to this list");
1951#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001952 if (!__c.empty())
1953 {
Eric Fiselierb88ea352015-12-30 20:57:59 +00001954 __link_pointer __f = __c.__end_.__next_;
1955 __link_pointer __l = __c.__end_.__prev_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001956 base::__unlink_nodes(__f, __l);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001957 __link_nodes(__p.__ptr_, __f, __l);
Howard Hinnant3e519522010-05-11 19:42:16 +00001958 base::__sz() += __c.__sz();
1959 __c.__sz() = 0;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001960#if _LIBCPP_DEBUG_LEVEL >= 2
1961 __libcpp_db* __db = __get_db();
1962 __c_node* __cn1 = __db->__find_c_and_lock(this);
1963 __c_node* __cn2 = __db->__find_c(&__c);
1964 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
1965 {
1966 --__p;
1967 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Eric Fiselier5243e192016-01-04 03:27:52 +00001968 if (__i->__ptr_ != __c.__end_as_link())
Howard Hinnant920b56c2011-09-27 23:55:03 +00001969 {
1970 __cn1->__add(*__p);
1971 (*__p)->__c_ = __cn1;
1972 if (--__cn2->end_ != __p)
1973 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
1974 }
1975 }
1976 __db->unlock();
1977#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001978 }
1979}
1980
1981template <class _Tp, class _Alloc>
1982void
1983list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __i)
1984{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001985#if _LIBCPP_DEBUG_LEVEL >= 2
1986 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1987 "list::splice(iterator, list, iterator) called with first iterator not"
1988 " referring to this list");
1989 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__i) == &__c,
1990 "list::splice(iterator, list, iterator) called with second iterator not"
1991 " referring to list argument");
1992 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(&__i),
1993 "list::splice(iterator, list, iterator) called with second iterator not"
1994 " derefereceable");
1995#endif
1996 if (__p.__ptr_ != __i.__ptr_ && __p.__ptr_ != __i.__ptr_->__next_)
Howard Hinnant3e519522010-05-11 19:42:16 +00001997 {
Eric Fiselierb88ea352015-12-30 20:57:59 +00001998 __link_pointer __f = __i.__ptr_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001999 base::__unlink_nodes(__f, __f);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002000 __link_nodes(__p.__ptr_, __f, __f);
Howard Hinnant3e519522010-05-11 19:42:16 +00002001 --__c.__sz();
2002 ++base::__sz();
Howard Hinnant920b56c2011-09-27 23:55:03 +00002003#if _LIBCPP_DEBUG_LEVEL >= 2
2004 __libcpp_db* __db = __get_db();
2005 __c_node* __cn1 = __db->__find_c_and_lock(this);
2006 __c_node* __cn2 = __db->__find_c(&__c);
2007 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
2008 {
2009 --__p;
2010 iterator* __j = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002011 if (__j->__ptr_ == __f)
Howard Hinnant920b56c2011-09-27 23:55:03 +00002012 {
2013 __cn1->__add(*__p);
2014 (*__p)->__c_ = __cn1;
2015 if (--__cn2->end_ != __p)
2016 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
2017 }
2018 }
2019 __db->unlock();
2020#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002021 }
2022}
2023
2024template <class _Tp, class _Alloc>
2025void
2026list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l)
2027{
Howard Hinnant920b56c2011-09-27 23:55:03 +00002028#if _LIBCPP_DEBUG_LEVEL >= 2
2029 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2030 "list::splice(iterator, list, iterator, iterator) called with first iterator not"
2031 " referring to this list");
2032 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__f) == &__c,
2033 "list::splice(iterator, list, iterator, iterator) called with second iterator not"
2034 " referring to list argument");
2035 if (this == &__c)
2036 {
2037 for (const_iterator __i = __f; __i != __l; ++__i)
2038 _LIBCPP_ASSERT(__i != __p,
2039 "list::splice(iterator, list, iterator, iterator)"
2040 " called with the first iterator within the range"
2041 " of the second and third iterators");
2042 }
2043#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002044 if (__f != __l)
2045 {
2046 if (this != &__c)
2047 {
Howard Hinnantce48a112011-06-30 21:18:19 +00002048 size_type __s = _VSTD::distance(__f, __l);
Howard Hinnant3e519522010-05-11 19:42:16 +00002049 __c.__sz() -= __s;
2050 base::__sz() += __s;
2051 }
Eric Fiselierb88ea352015-12-30 20:57:59 +00002052 __link_pointer __first = __f.__ptr_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002053 --__l;
Eric Fiselierb88ea352015-12-30 20:57:59 +00002054 __link_pointer __last = __l.__ptr_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002055 base::__unlink_nodes(__first, __last);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002056 __link_nodes(__p.__ptr_, __first, __last);
Howard Hinnant920b56c2011-09-27 23:55:03 +00002057#if _LIBCPP_DEBUG_LEVEL >= 2
2058 __libcpp_db* __db = __get_db();
2059 __c_node* __cn1 = __db->__find_c_and_lock(this);
2060 __c_node* __cn2 = __db->__find_c(&__c);
2061 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
2062 {
2063 --__p;
2064 iterator* __j = static_cast<iterator*>((*__p)->__i_);
Eric Fiselierb88ea352015-12-30 20:57:59 +00002065 for (__link_pointer __k = __f.__ptr_;
Howard Hinnant920b56c2011-09-27 23:55:03 +00002066 __k != __l.__ptr_; __k = __k->__next_)
2067 {
2068 if (__j->__ptr_ == __k)
2069 {
2070 __cn1->__add(*__p);
2071 (*__p)->__c_ = __cn1;
2072 if (--__cn2->end_ != __p)
2073 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
2074 }
2075 }
2076 }
2077 __db->unlock();
2078#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002079 }
2080}
2081
2082template <class _Tp, class _Alloc>
2083void
2084list<_Tp, _Alloc>::remove(const value_type& __x)
2085{
Marshall Clowced70062014-08-08 15:35:52 +00002086 list<_Tp, _Alloc> __deleted_nodes; // collect the nodes we're removing
2087 for (const_iterator __i = begin(), __e = end(); __i != __e;)
Howard Hinnant3e519522010-05-11 19:42:16 +00002088 {
2089 if (*__i == __x)
2090 {
Marshall Clowced70062014-08-08 15:35:52 +00002091 const_iterator __j = _VSTD::next(__i);
Howard Hinnant3e519522010-05-11 19:42:16 +00002092 for (; __j != __e && *__j == __x; ++__j)
2093 ;
Marshall Clowced70062014-08-08 15:35:52 +00002094 __deleted_nodes.splice(__deleted_nodes.end(), *this, __i, __j);
2095 __i = __j;
Marshall Clow90ba05332014-08-04 17:32:25 +00002096 if (__i != __e)
Marshall Clow28d65da2014-08-05 01:34:12 +00002097 ++__i;
Howard Hinnant3e519522010-05-11 19:42:16 +00002098 }
2099 else
2100 ++__i;
2101 }
2102}
2103
2104template <class _Tp, class _Alloc>
2105template <class _Pred>
2106void
2107list<_Tp, _Alloc>::remove_if(_Pred __pred)
2108{
2109 for (iterator __i = begin(), __e = end(); __i != __e;)
2110 {
2111 if (__pred(*__i))
2112 {
Howard Hinnantce48a112011-06-30 21:18:19 +00002113 iterator __j = _VSTD::next(__i);
Howard Hinnant3e519522010-05-11 19:42:16 +00002114 for (; __j != __e && __pred(*__j); ++__j)
2115 ;
2116 __i = erase(__i, __j);
Marshall Clow90ba05332014-08-04 17:32:25 +00002117 if (__i != __e)
Marshall Clow28d65da2014-08-05 01:34:12 +00002118 ++__i;
Howard Hinnant3e519522010-05-11 19:42:16 +00002119 }
2120 else
2121 ++__i;
2122 }
2123}
2124
2125template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00002126inline
Howard Hinnant3e519522010-05-11 19:42:16 +00002127void
2128list<_Tp, _Alloc>::unique()
2129{
2130 unique(__equal_to<value_type>());
2131}
2132
2133template <class _Tp, class _Alloc>
2134template <class _BinaryPred>
2135void
2136list<_Tp, _Alloc>::unique(_BinaryPred __binary_pred)
2137{
2138 for (iterator __i = begin(), __e = end(); __i != __e;)
2139 {
Howard Hinnantce48a112011-06-30 21:18:19 +00002140 iterator __j = _VSTD::next(__i);
Howard Hinnant3e519522010-05-11 19:42:16 +00002141 for (; __j != __e && __binary_pred(*__i, *__j); ++__j)
2142 ;
2143 if (++__i != __j)
2144 __i = erase(__i, __j);
2145 }
2146}
2147
2148template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00002149inline
Howard Hinnant3e519522010-05-11 19:42:16 +00002150void
2151list<_Tp, _Alloc>::merge(list& __c)
2152{
2153 merge(__c, __less<value_type>());
2154}
2155
2156template <class _Tp, class _Alloc>
2157template <class _Comp>
2158void
2159list<_Tp, _Alloc>::merge(list& __c, _Comp __comp)
2160{
2161 if (this != &__c)
2162 {
2163 iterator __f1 = begin();
2164 iterator __e1 = end();
2165 iterator __f2 = __c.begin();
2166 iterator __e2 = __c.end();
2167 while (__f1 != __e1 && __f2 != __e2)
2168 {
2169 if (__comp(*__f2, *__f1))
2170 {
2171 size_type __ds = 1;
Howard Hinnantce48a112011-06-30 21:18:19 +00002172 iterator __m2 = _VSTD::next(__f2);
Howard Hinnant3e519522010-05-11 19:42:16 +00002173 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2, ++__ds)
2174 ;
2175 base::__sz() += __ds;
2176 __c.__sz() -= __ds;
Eric Fiselierb88ea352015-12-30 20:57:59 +00002177 __link_pointer __f = __f2.__ptr_;
2178 __link_pointer __l = __m2.__ptr_->__prev_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002179 __f2 = __m2;
2180 base::__unlink_nodes(__f, __l);
Howard Hinnantce48a112011-06-30 21:18:19 +00002181 __m2 = _VSTD::next(__f1);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002182 __link_nodes(__f1.__ptr_, __f, __l);
Howard Hinnant3e519522010-05-11 19:42:16 +00002183 __f1 = __m2;
2184 }
2185 else
2186 ++__f1;
2187 }
2188 splice(__e1, __c);
Howard Hinnant920b56c2011-09-27 23:55:03 +00002189#if _LIBCPP_DEBUG_LEVEL >= 2
2190 __libcpp_db* __db = __get_db();
2191 __c_node* __cn1 = __db->__find_c_and_lock(this);
2192 __c_node* __cn2 = __db->__find_c(&__c);
2193 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
2194 {
2195 --__p;
2196 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Eric Fiselier5243e192016-01-04 03:27:52 +00002197 if (__i->__ptr_ != __c.__end_as_link())
Howard Hinnant920b56c2011-09-27 23:55:03 +00002198 {
2199 __cn1->__add(*__p);
2200 (*__p)->__c_ = __cn1;
2201 if (--__cn2->end_ != __p)
2202 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
2203 }
2204 }
2205 __db->unlock();
2206#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002207 }
2208}
2209
2210template <class _Tp, class _Alloc>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00002211inline
Howard Hinnant3e519522010-05-11 19:42:16 +00002212void
2213list<_Tp, _Alloc>::sort()
2214{
2215 sort(__less<value_type>());
2216}
2217
2218template <class _Tp, class _Alloc>
2219template <class _Comp>
Evgeniy Stepanovcd31b432016-04-22 01:04:55 +00002220inline
Howard Hinnant3e519522010-05-11 19:42:16 +00002221void
2222list<_Tp, _Alloc>::sort(_Comp __comp)
2223{
2224 __sort(begin(), end(), base::__sz(), __comp);
2225}
2226
2227template <class _Tp, class _Alloc>
2228template <class _Comp>
Howard Hinnant3e519522010-05-11 19:42:16 +00002229typename list<_Tp, _Alloc>::iterator
2230list<_Tp, _Alloc>::__sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp)
2231{
2232 switch (__n)
2233 {
2234 case 0:
2235 case 1:
2236 return __f1;
2237 case 2:
2238 if (__comp(*--__e2, *__f1))
2239 {
Eric Fiselierb88ea352015-12-30 20:57:59 +00002240 __link_pointer __f = __e2.__ptr_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002241 base::__unlink_nodes(__f, __f);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002242 __link_nodes(__f1.__ptr_, __f, __f);
Howard Hinnant3e519522010-05-11 19:42:16 +00002243 return __e2;
2244 }
2245 return __f1;
2246 }
2247 size_type __n2 = __n / 2;
Howard Hinnantce48a112011-06-30 21:18:19 +00002248 iterator __e1 = _VSTD::next(__f1, __n2);
Howard Hinnant3e519522010-05-11 19:42:16 +00002249 iterator __r = __f1 = __sort(__f1, __e1, __n2, __comp);
2250 iterator __f2 = __e1 = __sort(__e1, __e2, __n - __n2, __comp);
2251 if (__comp(*__f2, *__f1))
2252 {
Howard Hinnantce48a112011-06-30 21:18:19 +00002253 iterator __m2 = _VSTD::next(__f2);
Howard Hinnant3e519522010-05-11 19:42:16 +00002254 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2)
2255 ;
Eric Fiselierb88ea352015-12-30 20:57:59 +00002256 __link_pointer __f = __f2.__ptr_;
2257 __link_pointer __l = __m2.__ptr_->__prev_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002258 __r = __f2;
2259 __e1 = __f2 = __m2;
2260 base::__unlink_nodes(__f, __l);
Howard Hinnantce48a112011-06-30 21:18:19 +00002261 __m2 = _VSTD::next(__f1);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002262 __link_nodes(__f1.__ptr_, __f, __l);
Howard Hinnant3e519522010-05-11 19:42:16 +00002263 __f1 = __m2;
2264 }
2265 else
2266 ++__f1;
2267 while (__f1 != __e1 && __f2 != __e2)
2268 {
2269 if (__comp(*__f2, *__f1))
2270 {
Howard Hinnantce48a112011-06-30 21:18:19 +00002271 iterator __m2 = _VSTD::next(__f2);
Howard Hinnant3e519522010-05-11 19:42:16 +00002272 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2)
2273 ;
Eric Fiselierb88ea352015-12-30 20:57:59 +00002274 __link_pointer __f = __f2.__ptr_;
2275 __link_pointer __l = __m2.__ptr_->__prev_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002276 if (__e1 == __f2)
2277 __e1 = __m2;
2278 __f2 = __m2;
2279 base::__unlink_nodes(__f, __l);
Howard Hinnantce48a112011-06-30 21:18:19 +00002280 __m2 = _VSTD::next(__f1);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002281 __link_nodes(__f1.__ptr_, __f, __l);
Howard Hinnant3e519522010-05-11 19:42:16 +00002282 __f1 = __m2;
2283 }
2284 else
2285 ++__f1;
2286 }
2287 return __r;
2288}
2289
2290template <class _Tp, class _Alloc>
2291void
Howard Hinnant45900102011-06-03 17:30:28 +00002292list<_Tp, _Alloc>::reverse() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00002293{
2294 if (base::__sz() > 1)
2295 {
2296 iterator __e = end();
Howard Hinnant920b56c2011-09-27 23:55:03 +00002297 for (iterator __i = begin(); __i.__ptr_ != __e.__ptr_;)
2298 {
Howard Hinnantce48a112011-06-30 21:18:19 +00002299 _VSTD::swap(__i.__ptr_->__prev_, __i.__ptr_->__next_);
Howard Hinnant920b56c2011-09-27 23:55:03 +00002300 __i.__ptr_ = __i.__ptr_->__prev_;
2301 }
Howard Hinnantce48a112011-06-30 21:18:19 +00002302 _VSTD::swap(__e.__ptr_->__prev_, __e.__ptr_->__next_);
Howard Hinnant3e519522010-05-11 19:42:16 +00002303 }
2304}
2305
2306template <class _Tp, class _Alloc>
Howard Hinnant920b56c2011-09-27 23:55:03 +00002307bool
2308list<_Tp, _Alloc>::__invariants() const
2309{
2310 return size() == _VSTD::distance(begin(), end());
2311}
2312
2313#if _LIBCPP_DEBUG_LEVEL >= 2
2314
2315template <class _Tp, class _Alloc>
2316bool
2317list<_Tp, _Alloc>::__dereferenceable(const const_iterator* __i) const
2318{
Eric Fiselier5243e192016-01-04 03:27:52 +00002319 return __i->__ptr_ != this->__end_as_link();
Howard Hinnant920b56c2011-09-27 23:55:03 +00002320}
2321
2322template <class _Tp, class _Alloc>
2323bool
2324list<_Tp, _Alloc>::__decrementable(const const_iterator* __i) const
2325{
2326 return !empty() && __i->__ptr_ != base::__end_.__next_;
2327}
2328
2329template <class _Tp, class _Alloc>
2330bool
2331list<_Tp, _Alloc>::__addable(const const_iterator* __i, ptrdiff_t __n) const
2332{
2333 return false;
2334}
2335
2336template <class _Tp, class _Alloc>
2337bool
2338list<_Tp, _Alloc>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
2339{
2340 return false;
2341}
2342
2343#endif // _LIBCPP_DEBUG_LEVEL >= 2
2344
2345template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00002346inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00002347bool
2348operator==(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2349{
Howard Hinnantce48a112011-06-30 21:18:19 +00002350 return __x.size() == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnant3e519522010-05-11 19:42:16 +00002351}
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 _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
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{
2366 return !(__x == __y);
2367}
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 __y < __x;
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 !(__x < __y);
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 !(__y < __x);
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 +00002395void
2396swap(list<_Tp, _Alloc>& __x, list<_Tp, _Alloc>& __y)
Howard Hinnant45900102011-06-03 17:30:28 +00002397 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnant3e519522010-05-11 19:42:16 +00002398{
2399 __x.swap(__y);
2400}
2401
2402_LIBCPP_END_NAMESPACE_STD
2403
2404#endif // _LIBCPP_LIST