blob: 28d505582c0d9d3465c2e62d37fcdd0c98c5827d [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>
178
Howard Hinnantab4f4382011-11-29 16:45:27 +0000179#include <__undef_min_max>
180
Eric Fiselierc1bd9192014-08-10 23:53:08 +0000181#include <__debug>
Howard Hinnant42a30462013-08-02 00:26:35 +0000182
Howard Hinnant073458b2011-10-17 20:05:10 +0000183#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnant3e519522010-05-11 19:42:16 +0000184#pragma GCC system_header
Howard Hinnant073458b2011-10-17 20:05:10 +0000185#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000186
187_LIBCPP_BEGIN_NAMESPACE_STD
188
Howard Hinnantce534202011-06-14 19:58:17 +0000189template <class _Tp, class _VoidPtr> struct __list_node;
Howard Hinnant3e519522010-05-11 19:42:16 +0000190
191template <class _Tp, class _VoidPtr>
192struct __list_node_base
193{
Eric Fiselier1c813402015-08-23 02:56:05 +0000194 typedef typename __rebind_pointer<_VoidPtr, __list_node<_Tp, _VoidPtr> >::type
195 pointer;
196 typedef typename __rebind_pointer<_VoidPtr, __list_node_base>::type
197 __base_pointer;
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000198
Howard Hinnant3e519522010-05-11 19:42:16 +0000199 pointer __prev_;
200 pointer __next_;
201
Howard Hinnant848a5372010-09-22 16:48:34 +0000202 _LIBCPP_INLINE_VISIBILITY
Marshall Clow28d65da2014-08-05 01:34:12 +0000203 __list_node_base() : __prev_(__self()), __next_(__self()) {}
204
205 _LIBCPP_INLINE_VISIBILITY
206 pointer __self()
207 {
Marshall Clowced70062014-08-08 15:35:52 +0000208 return static_cast<pointer>(pointer_traits<__base_pointer>::pointer_to(*this));
Marshall Clow28d65da2014-08-05 01:34:12 +0000209 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000210};
211
212template <class _Tp, class _VoidPtr>
213struct __list_node
214 : public __list_node_base<_Tp, _VoidPtr>
215{
216 _Tp __value_;
217};
218
Marshall Clowb5d34aa2015-02-18 17:24:08 +0000219template <class _Tp, class _Alloc = allocator<_Tp> > class _LIBCPP_TYPE_VIS_ONLY list;
Howard Hinnantce534202011-06-14 19:58:17 +0000220template <class _Tp, class _Alloc> class __list_imp;
Howard Hinnantf0544c22013-08-12 18:38:34 +0000221template <class _Tp, class _VoidPtr> class _LIBCPP_TYPE_VIS_ONLY __list_const_iterator;
Howard Hinnant3e519522010-05-11 19:42:16 +0000222
223template <class _Tp, class _VoidPtr>
Howard Hinnantf0544c22013-08-12 18:38:34 +0000224class _LIBCPP_TYPE_VIS_ONLY __list_iterator
Howard Hinnant3e519522010-05-11 19:42:16 +0000225{
Eric Fiselier1c813402015-08-23 02:56:05 +0000226 typedef typename __rebind_pointer<_VoidPtr, __list_node<_Tp, _VoidPtr> >::type
227 __node_pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000228
229 __node_pointer __ptr_;
230
Howard Hinnant920b56c2011-09-27 23:55:03 +0000231#if _LIBCPP_DEBUG_LEVEL >= 2
232 _LIBCPP_INLINE_VISIBILITY
233 explicit __list_iterator(__node_pointer __p, const void* __c) _NOEXCEPT
234 : __ptr_(__p)
235 {
236 __get_db()->__insert_ic(this, __c);
237 }
238#else
Howard Hinnant848a5372010-09-22 16:48:34 +0000239 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000240 explicit __list_iterator(__node_pointer __p) _NOEXCEPT : __ptr_(__p) {}
Howard Hinnant920b56c2011-09-27 23:55:03 +0000241#endif
242
243
Howard Hinnant3e519522010-05-11 19:42:16 +0000244
245 template<class, class> friend class list;
246 template<class, class> friend class __list_imp;
247 template<class, class> friend class __list_const_iterator;
248public:
249 typedef bidirectional_iterator_tag iterator_category;
250 typedef _Tp value_type;
251 typedef value_type& reference;
Eric Fiselier1c813402015-08-23 02:56:05 +0000252 typedef typename __rebind_pointer<_VoidPtr, value_type>::type pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000253 typedef typename pointer_traits<pointer>::difference_type difference_type;
254
Howard Hinnant848a5372010-09-22 16:48:34 +0000255 _LIBCPP_INLINE_VISIBILITY
Marshall Clow0c37cfd2013-08-05 21:23:28 +0000256 __list_iterator() _NOEXCEPT : __ptr_(nullptr)
Howard Hinnant920b56c2011-09-27 23:55:03 +0000257 {
258#if _LIBCPP_DEBUG_LEVEL >= 2
259 __get_db()->__insert_i(this);
260#endif
261 }
262
263#if _LIBCPP_DEBUG_LEVEL >= 2
264
Howard Hinnant27745452011-01-28 23:46:28 +0000265 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000266 __list_iterator(const __list_iterator& __p)
267 : __ptr_(__p.__ptr_)
268 {
269 __get_db()->__iterator_copy(this, &__p);
270 }
271
272 _LIBCPP_INLINE_VISIBILITY
273 ~__list_iterator()
274 {
275 __get_db()->__erase_i(this);
276 }
277
278 _LIBCPP_INLINE_VISIBILITY
279 __list_iterator& operator=(const __list_iterator& __p)
280 {
281 if (this != &__p)
282 {
283 __get_db()->__iterator_copy(this, &__p);
284 __ptr_ = __p.__ptr_;
285 }
286 return *this;
287 }
288
289#endif // _LIBCPP_DEBUG_LEVEL >= 2
290
291 _LIBCPP_INLINE_VISIBILITY
292 reference operator*() const
293 {
294#if _LIBCPP_DEBUG_LEVEL >= 2
295 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
296 "Attempted to dereference a non-dereferenceable list::iterator");
297#endif
298 return __ptr_->__value_;
299 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000300 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000301 pointer operator->() const
302 {
303#if _LIBCPP_DEBUG_LEVEL >= 2
304 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
305 "Attempted to dereference a non-dereferenceable list::iterator");
306#endif
307 return pointer_traits<pointer>::pointer_to(__ptr_->__value_);
308 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000309
Howard Hinnant848a5372010-09-22 16:48:34 +0000310 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000311 __list_iterator& operator++()
312 {
313#if _LIBCPP_DEBUG_LEVEL >= 2
314 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
315 "Attempted to increment non-incrementable list::iterator");
316#endif
317 __ptr_ = __ptr_->__next_;
318 return *this;
319 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000320 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000321 __list_iterator operator++(int) {__list_iterator __t(*this); ++(*this); return __t;}
322
Howard Hinnant848a5372010-09-22 16:48:34 +0000323 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000324 __list_iterator& operator--()
325 {
326#if _LIBCPP_DEBUG_LEVEL >= 2
327 _LIBCPP_ASSERT(__get_const_db()->__decrementable(this),
328 "Attempted to decrement non-decrementable list::iterator");
329#endif
330 __ptr_ = __ptr_->__prev_;
331 return *this;
332 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000333 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000334 __list_iterator operator--(int) {__list_iterator __t(*this); --(*this); return __t;}
335
Howard Hinnant848a5372010-09-22 16:48:34 +0000336 friend _LIBCPP_INLINE_VISIBILITY
337 bool operator==(const __list_iterator& __x, const __list_iterator& __y)
Howard Hinnant920b56c2011-09-27 23:55:03 +0000338 {
Howard Hinnant920b56c2011-09-27 23:55:03 +0000339 return __x.__ptr_ == __y.__ptr_;
340 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000341 friend _LIBCPP_INLINE_VISIBILITY
342 bool operator!=(const __list_iterator& __x, const __list_iterator& __y)
Howard Hinnant3e519522010-05-11 19:42:16 +0000343 {return !(__x == __y);}
344};
345
346template <class _Tp, class _VoidPtr>
Howard Hinnantf0544c22013-08-12 18:38:34 +0000347class _LIBCPP_TYPE_VIS_ONLY __list_const_iterator
Howard Hinnant3e519522010-05-11 19:42:16 +0000348{
Eric Fiselier1c813402015-08-23 02:56:05 +0000349 typedef typename __rebind_pointer<_VoidPtr, __list_node<_Tp, _VoidPtr> >::type
350 __node_pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000351
352 __node_pointer __ptr_;
353
Howard Hinnant920b56c2011-09-27 23:55:03 +0000354#if _LIBCPP_DEBUG_LEVEL >= 2
355 _LIBCPP_INLINE_VISIBILITY
356 explicit __list_const_iterator(__node_pointer __p, const void* __c) _NOEXCEPT
357 : __ptr_(__p)
358 {
359 __get_db()->__insert_ic(this, __c);
360 }
361#else
Howard Hinnant848a5372010-09-22 16:48:34 +0000362 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000363 explicit __list_const_iterator(__node_pointer __p) _NOEXCEPT : __ptr_(__p) {}
Howard Hinnant920b56c2011-09-27 23:55:03 +0000364#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000365
366 template<class, class> friend class list;
367 template<class, class> friend class __list_imp;
368public:
369 typedef bidirectional_iterator_tag iterator_category;
370 typedef _Tp value_type;
371 typedef const value_type& reference;
Eric Fiselier1c813402015-08-23 02:56:05 +0000372 typedef typename __rebind_pointer<_VoidPtr, const value_type>::type pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000373 typedef typename pointer_traits<pointer>::difference_type difference_type;
374
Howard Hinnant848a5372010-09-22 16:48:34 +0000375 _LIBCPP_INLINE_VISIBILITY
Marshall Clow0c37cfd2013-08-05 21:23:28 +0000376 __list_const_iterator() _NOEXCEPT : __ptr_(nullptr)
Howard Hinnant920b56c2011-09-27 23:55:03 +0000377 {
378#if _LIBCPP_DEBUG_LEVEL >= 2
379 __get_db()->__insert_i(this);
380#endif
381 }
Howard Hinnant27745452011-01-28 23:46:28 +0000382 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf7509232013-04-05 17:58:52 +0000383 __list_const_iterator(const __list_iterator<_Tp, _VoidPtr>& __p) _NOEXCEPT
Howard Hinnant920b56c2011-09-27 23:55:03 +0000384 : __ptr_(__p.__ptr_)
385 {
386#if _LIBCPP_DEBUG_LEVEL >= 2
387 __get_db()->__iterator_copy(this, &__p);
388#endif
389 }
390
391#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant3e519522010-05-11 19:42:16 +0000392
Howard Hinnant848a5372010-09-22 16:48:34 +0000393 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000394 __list_const_iterator(const __list_const_iterator& __p)
395 : __ptr_(__p.__ptr_)
396 {
397 __get_db()->__iterator_copy(this, &__p);
398 }
399
400 _LIBCPP_INLINE_VISIBILITY
401 ~__list_const_iterator()
402 {
403 __get_db()->__erase_i(this);
404 }
405
406 _LIBCPP_INLINE_VISIBILITY
407 __list_const_iterator& operator=(const __list_const_iterator& __p)
408 {
409 if (this != &__p)
410 {
411 __get_db()->__iterator_copy(this, &__p);
412 __ptr_ = __p.__ptr_;
413 }
414 return *this;
415 }
416
417#endif // _LIBCPP_DEBUG_LEVEL >= 2
418 _LIBCPP_INLINE_VISIBILITY
419 reference operator*() const
420 {
421#if _LIBCPP_DEBUG_LEVEL >= 2
422 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
423 "Attempted to dereference a non-dereferenceable list::const_iterator");
424#endif
425 return __ptr_->__value_;
426 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000427 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000428 pointer operator->() const
429 {
430#if _LIBCPP_DEBUG_LEVEL >= 2
431 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
432 "Attempted to dereference a non-dereferenceable list::iterator");
433#endif
434 return pointer_traits<pointer>::pointer_to(__ptr_->__value_);
435 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000436
Howard Hinnant848a5372010-09-22 16:48:34 +0000437 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000438 __list_const_iterator& operator++()
439 {
440#if _LIBCPP_DEBUG_LEVEL >= 2
441 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
442 "Attempted to increment non-incrementable list::const_iterator");
443#endif
444 __ptr_ = __ptr_->__next_;
445 return *this;
446 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000447 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000448 __list_const_iterator operator++(int) {__list_const_iterator __t(*this); ++(*this); return __t;}
449
Howard Hinnant848a5372010-09-22 16:48:34 +0000450 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000451 __list_const_iterator& operator--()
452 {
453#if _LIBCPP_DEBUG_LEVEL >= 2
454 _LIBCPP_ASSERT(__get_const_db()->__decrementable(this),
455 "Attempted to decrement non-decrementable list::const_iterator");
456#endif
457 __ptr_ = __ptr_->__prev_;
458 return *this;
459 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000460 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000461 __list_const_iterator operator--(int) {__list_const_iterator __t(*this); --(*this); return __t;}
462
Howard Hinnant848a5372010-09-22 16:48:34 +0000463 friend _LIBCPP_INLINE_VISIBILITY
464 bool operator==(const __list_const_iterator& __x, const __list_const_iterator& __y)
Howard Hinnant920b56c2011-09-27 23:55:03 +0000465 {
Howard Hinnant920b56c2011-09-27 23:55:03 +0000466 return __x.__ptr_ == __y.__ptr_;
467 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000468 friend _LIBCPP_INLINE_VISIBILITY
469 bool operator!=(const __list_const_iterator& __x, const __list_const_iterator& __y)
Howard Hinnant3e519522010-05-11 19:42:16 +0000470 {return !(__x == __y);}
471};
472
473template <class _Tp, class _Alloc>
474class __list_imp
475{
476 __list_imp(const __list_imp&);
477 __list_imp& operator=(const __list_imp&);
478protected:
479 typedef _Tp value_type;
480 typedef _Alloc allocator_type;
481 typedef allocator_traits<allocator_type> __alloc_traits;
482 typedef typename __alloc_traits::size_type size_type;
483 typedef typename __alloc_traits::void_pointer __void_pointer;
484 typedef __list_iterator<value_type, __void_pointer> iterator;
485 typedef __list_const_iterator<value_type, __void_pointer> const_iterator;
486 typedef __list_node_base<value_type, __void_pointer> __node_base;
487 typedef __list_node<value_type, __void_pointer> __node;
Marshall Clow1f508012015-04-07 05:21:38 +0000488 typedef typename __rebind_alloc_helper<__alloc_traits, __node>::type __node_allocator;
Howard Hinnant3e519522010-05-11 19:42:16 +0000489 typedef allocator_traits<__node_allocator> __node_alloc_traits;
490 typedef typename __node_alloc_traits::pointer __node_pointer;
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000491 typedef typename __node_alloc_traits::pointer __node_const_pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000492 typedef typename __alloc_traits::pointer pointer;
493 typedef typename __alloc_traits::const_pointer const_pointer;
494 typedef typename __alloc_traits::difference_type difference_type;
495
Marshall Clow1f508012015-04-07 05:21:38 +0000496 typedef typename __rebind_alloc_helper<__alloc_traits, __node_base>::type __node_base_allocator;
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000497 typedef typename allocator_traits<__node_base_allocator>::pointer __node_base_pointer;
498
Howard Hinnant3e519522010-05-11 19:42:16 +0000499 __node_base __end_;
500 __compressed_pair<size_type, __node_allocator> __size_alloc_;
501
Howard Hinnant848a5372010-09-22 16:48:34 +0000502 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000503 size_type& __sz() _NOEXCEPT {return __size_alloc_.first();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000504 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000505 const size_type& __sz() const _NOEXCEPT
506 {return __size_alloc_.first();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000507 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000508 __node_allocator& __node_alloc() _NOEXCEPT
509 {return __size_alloc_.second();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000510 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000511 const __node_allocator& __node_alloc() const _NOEXCEPT
512 {return __size_alloc_.second();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000513
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000514 static void __unlink_nodes(__node_pointer __f, __node_pointer __l) _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +0000515
Howard Hinnant45900102011-06-03 17:30:28 +0000516 __list_imp()
517 _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value);
Howard Hinnant3e519522010-05-11 19:42:16 +0000518 __list_imp(const allocator_type& __a);
519 ~__list_imp();
Howard Hinnant45900102011-06-03 17:30:28 +0000520 void clear() _NOEXCEPT;
Howard Hinnant848a5372010-09-22 16:48:34 +0000521 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000522 bool empty() const _NOEXCEPT {return __sz() == 0;}
Howard Hinnant3e519522010-05-11 19:42:16 +0000523
Howard Hinnant848a5372010-09-22 16:48:34 +0000524 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000525 iterator begin() _NOEXCEPT
526 {
527#if _LIBCPP_DEBUG_LEVEL >= 2
528 return iterator(__end_.__next_, this);
529#else
530 return iterator(__end_.__next_);
531#endif
532 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000533 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000534 const_iterator begin() const _NOEXCEPT
Howard Hinnant920b56c2011-09-27 23:55:03 +0000535 {
536#if _LIBCPP_DEBUG_LEVEL >= 2
537 return const_iterator(__end_.__next_, this);
538#else
539 return const_iterator(__end_.__next_);
540#endif
541 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000542 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000543 iterator end() _NOEXCEPT
544 {
545#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000546 return iterator(static_cast<__node_pointer>(
547 pointer_traits<__node_base_pointer>::pointer_to(__end_)), this);
Howard Hinnant920b56c2011-09-27 23:55:03 +0000548#else
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000549 return iterator(static_cast<__node_pointer>(
550 pointer_traits<__node_base_pointer>::pointer_to(__end_)));
Howard Hinnant920b56c2011-09-27 23:55:03 +0000551#endif
552 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000553 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000554 const_iterator end() const _NOEXCEPT
Howard Hinnant920b56c2011-09-27 23:55:03 +0000555 {
556#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000557 return const_iterator(static_cast<__node_const_pointer>(
558 pointer_traits<__node_base_pointer>::pointer_to(const_cast<__node_base&>(__end_))), this);
Howard Hinnant920b56c2011-09-27 23:55:03 +0000559#else
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000560 return const_iterator(static_cast<__node_const_pointer>(
561 pointer_traits<__node_base_pointer>::pointer_to(const_cast<__node_base&>(__end_))));
Howard Hinnant920b56c2011-09-27 23:55:03 +0000562#endif
563 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000564
Howard Hinnant45900102011-06-03 17:30:28 +0000565 void swap(__list_imp& __c)
Marshall Clowe3fbe142015-07-13 20:04:56 +0000566#if _LIBCPP_STD_VER >= 14
567 _NOEXCEPT;
568#else
569 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
570 __is_nothrow_swappable<allocator_type>::value);
571#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000572
Howard Hinnant848a5372010-09-22 16:48:34 +0000573 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000574 void __copy_assign_alloc(const __list_imp& __c)
575 {__copy_assign_alloc(__c, integral_constant<bool,
576 __node_alloc_traits::propagate_on_container_copy_assignment::value>());}
577
Howard Hinnant848a5372010-09-22 16:48:34 +0000578 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000579 void __move_assign_alloc(__list_imp& __c)
Howard Hinnant45900102011-06-03 17:30:28 +0000580 _NOEXCEPT_(
581 !__node_alloc_traits::propagate_on_container_move_assignment::value ||
582 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +0000583 {__move_assign_alloc(__c, integral_constant<bool,
584 __node_alloc_traits::propagate_on_container_move_assignment::value>());}
585
586private:
Howard Hinnant848a5372010-09-22 16:48:34 +0000587 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000588 void __copy_assign_alloc(const __list_imp& __c, true_type)
589 {
590 if (__node_alloc() != __c.__node_alloc())
591 clear();
592 __node_alloc() = __c.__node_alloc();
593 }
594
Howard Hinnant848a5372010-09-22 16:48:34 +0000595 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000596 void __copy_assign_alloc(const __list_imp& __c, false_type)
597 {}
598
Howard Hinnant848a5372010-09-22 16:48:34 +0000599 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant86681392011-09-02 20:42:31 +0000600 void __move_assign_alloc(__list_imp& __c, true_type)
Howard Hinnant45900102011-06-03 17:30:28 +0000601 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +0000602 {
Howard Hinnantce48a112011-06-30 21:18:19 +0000603 __node_alloc() = _VSTD::move(__c.__node_alloc());
Howard Hinnant3e519522010-05-11 19:42:16 +0000604 }
605
Howard Hinnant848a5372010-09-22 16:48:34 +0000606 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant86681392011-09-02 20:42:31 +0000607 void __move_assign_alloc(__list_imp& __c, false_type)
Howard Hinnant45900102011-06-03 17:30:28 +0000608 _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000609 {}
610};
611
612// Unlink nodes [__f, __l]
613template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +0000614inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000615void
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000616__list_imp<_Tp, _Alloc>::__unlink_nodes(__node_pointer __f, __node_pointer __l)
Howard Hinnant45900102011-06-03 17:30:28 +0000617 _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000618{
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000619 __f->__prev_->__next_ = __l->__next_;
620 __l->__next_->__prev_ = __f->__prev_;
Howard Hinnant3e519522010-05-11 19:42:16 +0000621}
622
623template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +0000624inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000625__list_imp<_Tp, _Alloc>::__list_imp()
Howard Hinnant45900102011-06-03 17:30:28 +0000626 _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +0000627 : __size_alloc_(0)
628{
629}
630
631template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +0000632inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000633__list_imp<_Tp, _Alloc>::__list_imp(const allocator_type& __a)
634 : __size_alloc_(0, __node_allocator(__a))
635{
636}
637
638template <class _Tp, class _Alloc>
639__list_imp<_Tp, _Alloc>::~__list_imp()
640{
641 clear();
Howard Hinnant920b56c2011-09-27 23:55:03 +0000642#if _LIBCPP_DEBUG_LEVEL >= 2
643 __get_db()->__erase_c(this);
644#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000645}
646
647template <class _Tp, class _Alloc>
648void
Howard Hinnant45900102011-06-03 17:30:28 +0000649__list_imp<_Tp, _Alloc>::clear() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000650{
651 if (!empty())
652 {
653 __node_allocator& __na = __node_alloc();
Howard Hinnant920b56c2011-09-27 23:55:03 +0000654 __node_pointer __f = __end_.__next_;
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000655 __node_pointer __l = static_cast<__node_pointer>(
656 pointer_traits<__node_base_pointer>::pointer_to(__end_));
657 __unlink_nodes(__f, __l->__prev_);
Howard Hinnant3e519522010-05-11 19:42:16 +0000658 __sz() = 0;
659 while (__f != __l)
660 {
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000661 __node_pointer __n = __f;
Howard Hinnant920b56c2011-09-27 23:55:03 +0000662 __f = __f->__next_;
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000663 __node_alloc_traits::destroy(__na, _VSTD::addressof(__n->__value_));
664 __node_alloc_traits::deallocate(__na, __n, 1);
Howard Hinnant3e519522010-05-11 19:42:16 +0000665 }
Howard Hinnant920b56c2011-09-27 23:55:03 +0000666#if _LIBCPP_DEBUG_LEVEL >= 2
667 __c_node* __c = __get_db()->__find_c_and_lock(this);
668 for (__i_node** __p = __c->end_; __p != __c->beg_; )
669 {
670 --__p;
671 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
672 if (__i->__ptr_ != __l)
673 {
674 (*__p)->__c_ = nullptr;
675 if (--__c->end_ != __p)
676 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
677 }
678 }
679 __get_db()->unlock();
680#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000681 }
682}
683
684template <class _Tp, class _Alloc>
685void
686__list_imp<_Tp, _Alloc>::swap(__list_imp& __c)
Marshall Clowe3fbe142015-07-13 20:04:56 +0000687#if _LIBCPP_STD_VER >= 14
688 _NOEXCEPT
689#else
690 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
691 __is_nothrow_swappable<allocator_type>::value)
692#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000693{
Howard Hinnant920b56c2011-09-27 23:55:03 +0000694 _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
695 this->__node_alloc() == __c.__node_alloc(),
696 "list::swap: Either propagate_on_container_swap must be true"
697 " or the allocators must compare equal");
Howard Hinnantce48a112011-06-30 21:18:19 +0000698 using _VSTD::swap;
Marshall Clowe3fbe142015-07-13 20:04:56 +0000699 __swap_allocator(__node_alloc(), __c.__node_alloc());
Howard Hinnant3e519522010-05-11 19:42:16 +0000700 swap(__sz(), __c.__sz());
701 swap(__end_, __c.__end_);
702 if (__sz() == 0)
Marshall Clow28d65da2014-08-05 01:34:12 +0000703 __end_.__next_ = __end_.__prev_ = __end_.__self();
Howard Hinnant3e519522010-05-11 19:42:16 +0000704 else
Marshall Clow28d65da2014-08-05 01:34:12 +0000705 __end_.__prev_->__next_ = __end_.__next_->__prev_ = __end_.__self();
Howard Hinnant3e519522010-05-11 19:42:16 +0000706 if (__c.__sz() == 0)
Marshall Clow28d65da2014-08-05 01:34:12 +0000707 __c.__end_.__next_ = __c.__end_.__prev_ = __c.__end_.__self();
Howard Hinnant3e519522010-05-11 19:42:16 +0000708 else
Marshall Clow28d65da2014-08-05 01:34:12 +0000709 __c.__end_.__prev_->__next_ = __c.__end_.__next_->__prev_ = __c.__end_.__self();
710
Howard Hinnant920b56c2011-09-27 23:55:03 +0000711#if _LIBCPP_DEBUG_LEVEL >= 2
712 __libcpp_db* __db = __get_db();
713 __c_node* __cn1 = __db->__find_c_and_lock(this);
714 __c_node* __cn2 = __db->__find_c(&__c);
715 std::swap(__cn1->beg_, __cn2->beg_);
716 std::swap(__cn1->end_, __cn2->end_);
717 std::swap(__cn1->cap_, __cn2->cap_);
718 for (__i_node** __p = __cn1->end_; __p != __cn1->beg_;)
719 {
720 --__p;
721 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000722 if (__i->__ptr_ == static_cast<__node_pointer>(
723 pointer_traits<__node_base_pointer>::pointer_to(__c.__end_)))
Howard Hinnant920b56c2011-09-27 23:55:03 +0000724 {
725 __cn2->__add(*__p);
726 if (--__cn1->end_ != __p)
727 memmove(__p, __p+1, (__cn1->end_ - __p)*sizeof(__i_node*));
728 }
729 else
730 (*__p)->__c_ = __cn1;
731 }
732 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
733 {
734 --__p;
735 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000736 if (__i->__ptr_ == static_cast<__node_pointer>(
737 pointer_traits<__node_base_pointer>::pointer_to(__end_)))
Howard Hinnant920b56c2011-09-27 23:55:03 +0000738 {
739 __cn1->__add(*__p);
740 if (--__cn2->end_ != __p)
741 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
742 }
743 else
744 (*__p)->__c_ = __cn2;
745 }
746 __db->unlock();
747#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000748}
749
Marshall Clowb5d34aa2015-02-18 17:24:08 +0000750template <class _Tp, class _Alloc /*= allocator<_Tp>*/>
Howard Hinnantf0544c22013-08-12 18:38:34 +0000751class _LIBCPP_TYPE_VIS_ONLY list
Howard Hinnant3e519522010-05-11 19:42:16 +0000752 : private __list_imp<_Tp, _Alloc>
753{
754 typedef __list_imp<_Tp, _Alloc> base;
755 typedef typename base::__node __node;
756 typedef typename base::__node_allocator __node_allocator;
757 typedef typename base::__node_pointer __node_pointer;
758 typedef typename base::__node_alloc_traits __node_alloc_traits;
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000759 typedef typename base::__node_base __node_base;
760 typedef typename base::__node_base_pointer __node_base_pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000761
762public:
763 typedef _Tp value_type;
764 typedef _Alloc allocator_type;
765 static_assert((is_same<value_type, typename allocator_type::value_type>::value),
766 "Invalid allocator::value_type");
767 typedef value_type& reference;
768 typedef const value_type& const_reference;
769 typedef typename base::pointer pointer;
770 typedef typename base::const_pointer const_pointer;
771 typedef typename base::size_type size_type;
772 typedef typename base::difference_type difference_type;
773 typedef typename base::iterator iterator;
774 typedef typename base::const_iterator const_iterator;
Howard Hinnantce48a112011-06-30 21:18:19 +0000775 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
776 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnant3e519522010-05-11 19:42:16 +0000777
Howard Hinnant848a5372010-09-22 16:48:34 +0000778 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000779 list()
780 _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value)
Howard Hinnant920b56c2011-09-27 23:55:03 +0000781 {
782#if _LIBCPP_DEBUG_LEVEL >= 2
783 __get_db()->__insert_c(this);
784#endif
785 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000786 _LIBCPP_INLINE_VISIBILITY
Marshall Clowfb829762013-09-08 19:11:51 +0000787 explicit list(const allocator_type& __a) : base(__a)
Howard Hinnant920b56c2011-09-27 23:55:03 +0000788 {
789#if _LIBCPP_DEBUG_LEVEL >= 2
790 __get_db()->__insert_c(this);
791#endif
792 }
Marshall Clowfb829762013-09-08 19:11:51 +0000793 explicit list(size_type __n);
794#if _LIBCPP_STD_VER > 11
795 explicit list(size_type __n, const allocator_type& __a);
796#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000797 list(size_type __n, const value_type& __x);
798 list(size_type __n, const value_type& __x, const allocator_type& __a);
799 template <class _InpIter>
800 list(_InpIter __f, _InpIter __l,
801 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
802 template <class _InpIter>
803 list(_InpIter __f, _InpIter __l, const allocator_type& __a,
804 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
805
806 list(const list& __c);
807 list(const list& __c, const allocator_type& __a);
808 list& operator=(const list& __c);
Howard Hinnant54976f22011-08-12 21:56:02 +0000809#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant3e519522010-05-11 19:42:16 +0000810 list(initializer_list<value_type> __il);
811 list(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnant54976f22011-08-12 21:56:02 +0000812#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000813#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant45900102011-06-03 17:30:28 +0000814 list(list&& __c)
815 _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value);
Howard Hinnant3e519522010-05-11 19:42:16 +0000816 list(list&& __c, const allocator_type& __a);
Howard Hinnant45900102011-06-03 17:30:28 +0000817 list& operator=(list&& __c)
818 _NOEXCEPT_(
819 __node_alloc_traits::propagate_on_container_move_assignment::value &&
820 is_nothrow_move_assignable<__node_allocator>::value);
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000821#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54976f22011-08-12 21:56:02 +0000822#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant848a5372010-09-22 16:48:34 +0000823 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000824 list& operator=(initializer_list<value_type> __il)
825 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnant54976f22011-08-12 21:56:02 +0000826#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant3e519522010-05-11 19:42:16 +0000827
828 template <class _InpIter>
829 void assign(_InpIter __f, _InpIter __l,
830 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
831 void assign(size_type __n, const value_type& __x);
Howard Hinnant54976f22011-08-12 21:56:02 +0000832#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant848a5372010-09-22 16:48:34 +0000833 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000834 void assign(initializer_list<value_type> __il)
835 {assign(__il.begin(), __il.end());}
Howard Hinnant54976f22011-08-12 21:56:02 +0000836#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant3e519522010-05-11 19:42:16 +0000837
Howard Hinnant45900102011-06-03 17:30:28 +0000838 allocator_type get_allocator() const _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +0000839
Howard Hinnant848a5372010-09-22 16:48:34 +0000840 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000841 size_type size() const _NOEXCEPT {return base::__sz();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000842 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000843 bool empty() const _NOEXCEPT {return base::empty();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000844 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000845 size_type max_size() const _NOEXCEPT
846 {return numeric_limits<difference_type>::max();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000847
Howard Hinnant848a5372010-09-22 16:48:34 +0000848 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000849 iterator begin() _NOEXCEPT {return base::begin();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000850 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000851 const_iterator begin() const _NOEXCEPT {return base::begin();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000852 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000853 iterator end() _NOEXCEPT {return base::end();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000854 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000855 const_iterator end() const _NOEXCEPT {return base::end();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000856 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000857 const_iterator cbegin() const _NOEXCEPT {return base::begin();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000858 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000859 const_iterator cend() const _NOEXCEPT {return base::end();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000860
Howard Hinnant848a5372010-09-22 16:48:34 +0000861 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000862 reverse_iterator rbegin() _NOEXCEPT
863 {return reverse_iterator(end());}
Howard Hinnant848a5372010-09-22 16:48:34 +0000864 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000865 const_reverse_iterator rbegin() const _NOEXCEPT
866 {return const_reverse_iterator(end());}
Howard Hinnant848a5372010-09-22 16:48:34 +0000867 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000868 reverse_iterator rend() _NOEXCEPT
869 {return reverse_iterator(begin());}
Howard Hinnant848a5372010-09-22 16:48:34 +0000870 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000871 const_reverse_iterator rend() const _NOEXCEPT
872 {return const_reverse_iterator(begin());}
Howard Hinnant848a5372010-09-22 16:48:34 +0000873 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000874 const_reverse_iterator crbegin() const _NOEXCEPT
875 {return const_reverse_iterator(end());}
Howard Hinnant848a5372010-09-22 16:48:34 +0000876 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000877 const_reverse_iterator crend() const _NOEXCEPT
878 {return const_reverse_iterator(begin());}
Howard Hinnant3e519522010-05-11 19:42:16 +0000879
Howard Hinnant848a5372010-09-22 16:48:34 +0000880 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000881 reference front()
882 {
883 _LIBCPP_ASSERT(!empty(), "list::front called on empty list");
884 return base::__end_.__next_->__value_;
885 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000886 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000887 const_reference front() const
888 {
889 _LIBCPP_ASSERT(!empty(), "list::front called on empty list");
890 return base::__end_.__next_->__value_;
891 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000892 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000893 reference back()
894 {
895 _LIBCPP_ASSERT(!empty(), "list::back called on empty list");
896 return base::__end_.__prev_->__value_;
897 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000898 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000899 const_reference back() const
900 {
901 _LIBCPP_ASSERT(!empty(), "list::back called on empty list");
902 return base::__end_.__prev_->__value_;
903 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000904
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000905#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +0000906 void push_front(value_type&& __x);
907 void push_back(value_type&& __x);
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000908#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnant3e519522010-05-11 19:42:16 +0000909 template <class... _Args>
910 void emplace_front(_Args&&... __args);
911 template <class... _Args>
912 void emplace_back(_Args&&... __args);
913 template <class... _Args>
914 iterator emplace(const_iterator __p, _Args&&... __args);
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000915#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnant3e519522010-05-11 19:42:16 +0000916 iterator insert(const_iterator __p, value_type&& __x);
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000917#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +0000918
919 void push_front(const value_type& __x);
920 void push_back(const value_type& __x);
921
922 iterator insert(const_iterator __p, const value_type& __x);
923 iterator insert(const_iterator __p, size_type __n, const value_type& __x);
924 template <class _InpIter>
925 iterator insert(const_iterator __p, _InpIter __f, _InpIter __l,
926 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
Howard Hinnant54976f22011-08-12 21:56:02 +0000927#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant848a5372010-09-22 16:48:34 +0000928 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000929 iterator insert(const_iterator __p, initializer_list<value_type> __il)
930 {return insert(__p, __il.begin(), __il.end());}
Howard Hinnant54976f22011-08-12 21:56:02 +0000931#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant3e519522010-05-11 19:42:16 +0000932
Howard Hinnant848a5372010-09-22 16:48:34 +0000933 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000934 void swap(list& __c)
Marshall Clowe3fbe142015-07-13 20:04:56 +0000935#if _LIBCPP_STD_VER >= 14
936 _NOEXCEPT
937#else
Howard Hinnant45900102011-06-03 17:30:28 +0000938 _NOEXCEPT_(!__node_alloc_traits::propagate_on_container_swap::value ||
939 __is_nothrow_swappable<__node_allocator>::value)
Marshall Clowe3fbe142015-07-13 20:04:56 +0000940#endif
Howard Hinnant45900102011-06-03 17:30:28 +0000941 {base::swap(__c);}
Howard Hinnant848a5372010-09-22 16:48:34 +0000942 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000943 void clear() _NOEXCEPT {base::clear();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000944
945 void pop_front();
946 void pop_back();
947
948 iterator erase(const_iterator __p);
949 iterator erase(const_iterator __f, const_iterator __l);
950
951 void resize(size_type __n);
952 void resize(size_type __n, const value_type& __x);
953
954 void splice(const_iterator __p, list& __c);
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000955#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant848a5372010-09-22 16:48:34 +0000956 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000957 void splice(const_iterator __p, list&& __c) {splice(__p, __c);}
958#endif
959 void splice(const_iterator __p, list& __c, const_iterator __i);
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000960#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant848a5372010-09-22 16:48:34 +0000961 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000962 void splice(const_iterator __p, list&& __c, const_iterator __i)
963 {splice(__p, __c, __i);}
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000964#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +0000965 void splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l);
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000966#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant848a5372010-09-22 16:48:34 +0000967 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000968 void splice(const_iterator __p, list&& __c, const_iterator __f, const_iterator __l)
969 {splice(__p, __c, __f, __l);}
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000970#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +0000971
972 void remove(const value_type& __x);
973 template <class _Pred> void remove_if(_Pred __pred);
974 void unique();
975 template <class _BinaryPred>
976 void unique(_BinaryPred __binary_pred);
977 void merge(list& __c);
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000978#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant848a5372010-09-22 16:48:34 +0000979 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000980 void merge(list&& __c) {merge(__c);}
981#endif
982 template <class _Comp>
983 void merge(list& __c, _Comp __comp);
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000984#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +0000985 template <class _Comp>
Howard Hinnant848a5372010-09-22 16:48:34 +0000986 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000987 void merge(list&& __c, _Comp __comp) {merge(__c, __comp);}
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000988#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +0000989 void sort();
990 template <class _Comp>
991 void sort(_Comp __comp);
992
Howard Hinnant45900102011-06-03 17:30:28 +0000993 void reverse() _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +0000994
Howard Hinnant920b56c2011-09-27 23:55:03 +0000995 bool __invariants() const;
996
997#if _LIBCPP_DEBUG_LEVEL >= 2
998
999 bool __dereferenceable(const const_iterator* __i) const;
1000 bool __decrementable(const const_iterator* __i) const;
1001 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1002 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1003
1004#endif // _LIBCPP_DEBUG_LEVEL >= 2
1005
Howard Hinnant3e519522010-05-11 19:42:16 +00001006private:
Marshall Clow28d65da2014-08-05 01:34:12 +00001007 static void __link_nodes (__node_pointer __p, __node_pointer __f, __node_pointer __l);
1008 void __link_nodes_at_front(__node_pointer __f, __node_pointer __l);
1009 void __link_nodes_at_back (__node_pointer __f, __node_pointer __l);
Howard Hinnant3e519522010-05-11 19:42:16 +00001010 iterator __iterator(size_type __n);
1011 template <class _Comp>
1012 static iterator __sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp);
1013
Howard Hinnant45900102011-06-03 17:30:28 +00001014 void __move_assign(list& __c, true_type)
1015 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value);
Howard Hinnant3e519522010-05-11 19:42:16 +00001016 void __move_assign(list& __c, false_type);
1017};
1018
1019// Link in nodes [__f, __l] just prior to __p
1020template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00001021inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001022void
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001023list<_Tp, _Alloc>::__link_nodes(__node_pointer __p, __node_pointer __f, __node_pointer __l)
Howard Hinnant3e519522010-05-11 19:42:16 +00001024{
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001025 __p->__prev_->__next_ = __f;
1026 __f->__prev_ = __p->__prev_;
1027 __p->__prev_ = __l;
1028 __l->__next_ = __p;
Howard Hinnant3e519522010-05-11 19:42:16 +00001029}
1030
Marshall Clow28d65da2014-08-05 01:34:12 +00001031// Link in nodes [__f, __l] at the front of the list
1032template <class _Tp, class _Alloc>
1033inline _LIBCPP_INLINE_VISIBILITY
1034void
1035list<_Tp, _Alloc>::__link_nodes_at_front(__node_pointer __f, __node_pointer __l)
1036{
Marshall Clowced70062014-08-08 15:35:52 +00001037 __f->__prev_ = base::__end_.__self();
1038 __l->__next_ = base::__end_.__next_;
1039 __l->__next_->__prev_ = __l;
1040 base::__end_.__next_ = __f;
Marshall Clow28d65da2014-08-05 01:34:12 +00001041}
1042
1043// Link in nodes [__f, __l] at the front of the list
1044template <class _Tp, class _Alloc>
1045inline _LIBCPP_INLINE_VISIBILITY
1046void
1047list<_Tp, _Alloc>::__link_nodes_at_back(__node_pointer __f, __node_pointer __l)
1048{
Marshall Clowced70062014-08-08 15:35:52 +00001049 __l->__next_ = base::__end_.__self();
1050 __f->__prev_ = base::__end_.__prev_;
1051 __f->__prev_->__next_ = __f;
1052 base::__end_.__prev_ = __l;
Marshall Clow28d65da2014-08-05 01:34:12 +00001053}
1054
1055
Howard Hinnant3e519522010-05-11 19:42:16 +00001056template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00001057inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001058typename list<_Tp, _Alloc>::iterator
1059list<_Tp, _Alloc>::__iterator(size_type __n)
1060{
Howard Hinnantce48a112011-06-30 21:18:19 +00001061 return __n <= base::__sz() / 2 ? _VSTD::next(begin(), __n)
1062 : _VSTD::prev(end(), base::__sz() - __n);
Howard Hinnant3e519522010-05-11 19:42:16 +00001063}
1064
1065template <class _Tp, class _Alloc>
1066list<_Tp, _Alloc>::list(size_type __n)
1067{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001068#if _LIBCPP_DEBUG_LEVEL >= 2
1069 __get_db()->__insert_c(this);
1070#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001071 for (; __n > 0; --__n)
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001072#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +00001073 emplace_back();
1074#else
1075 push_back(value_type());
1076#endif
1077}
1078
Marshall Clowfb829762013-09-08 19:11:51 +00001079#if _LIBCPP_STD_VER > 11
1080template <class _Tp, class _Alloc>
1081list<_Tp, _Alloc>::list(size_type __n, const allocator_type& __a) : base(__a)
1082{
1083#if _LIBCPP_DEBUG_LEVEL >= 2
1084 __get_db()->__insert_c(this);
1085#endif
1086 for (; __n > 0; --__n)
1087#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1088 emplace_back();
1089#else
1090 push_back(value_type());
1091#endif
1092}
1093#endif
1094
Howard Hinnant3e519522010-05-11 19:42:16 +00001095template <class _Tp, class _Alloc>
1096list<_Tp, _Alloc>::list(size_type __n, const value_type& __x)
1097{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001098#if _LIBCPP_DEBUG_LEVEL >= 2
1099 __get_db()->__insert_c(this);
1100#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001101 for (; __n > 0; --__n)
1102 push_back(__x);
1103}
1104
1105template <class _Tp, class _Alloc>
1106list<_Tp, _Alloc>::list(size_type __n, const value_type& __x, const allocator_type& __a)
1107 : base(__a)
1108{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001109#if _LIBCPP_DEBUG_LEVEL >= 2
1110 __get_db()->__insert_c(this);
1111#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001112 for (; __n > 0; --__n)
1113 push_back(__x);
1114}
1115
1116template <class _Tp, class _Alloc>
1117template <class _InpIter>
1118list<_Tp, _Alloc>::list(_InpIter __f, _InpIter __l,
1119 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1120{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001121#if _LIBCPP_DEBUG_LEVEL >= 2
1122 __get_db()->__insert_c(this);
1123#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001124 for (; __f != __l; ++__f)
1125 push_back(*__f);
1126}
1127
1128template <class _Tp, class _Alloc>
1129template <class _InpIter>
1130list<_Tp, _Alloc>::list(_InpIter __f, _InpIter __l, const allocator_type& __a,
1131 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1132 : base(__a)
1133{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001134#if _LIBCPP_DEBUG_LEVEL >= 2
1135 __get_db()->__insert_c(this);
1136#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001137 for (; __f != __l; ++__f)
1138 push_back(*__f);
1139}
1140
1141template <class _Tp, class _Alloc>
1142list<_Tp, _Alloc>::list(const list& __c)
1143 : base(allocator_type(
1144 __node_alloc_traits::select_on_container_copy_construction(
1145 __c.__node_alloc())))
1146{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001147#if _LIBCPP_DEBUG_LEVEL >= 2
1148 __get_db()->__insert_c(this);
1149#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001150 for (const_iterator __i = __c.begin(), __e = __c.end(); __i != __e; ++__i)
1151 push_back(*__i);
1152}
1153
1154template <class _Tp, class _Alloc>
1155list<_Tp, _Alloc>::list(const list& __c, const allocator_type& __a)
1156 : base(__a)
1157{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001158#if _LIBCPP_DEBUG_LEVEL >= 2
1159 __get_db()->__insert_c(this);
1160#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001161 for (const_iterator __i = __c.begin(), __e = __c.end(); __i != __e; ++__i)
1162 push_back(*__i);
1163}
1164
Howard Hinnant54976f22011-08-12 21:56:02 +00001165#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1166
Howard Hinnant3e519522010-05-11 19:42:16 +00001167template <class _Tp, class _Alloc>
1168list<_Tp, _Alloc>::list(initializer_list<value_type> __il, const allocator_type& __a)
1169 : base(__a)
1170{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001171#if _LIBCPP_DEBUG_LEVEL >= 2
1172 __get_db()->__insert_c(this);
1173#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001174 for (typename initializer_list<value_type>::const_iterator __i = __il.begin(),
1175 __e = __il.end(); __i != __e; ++__i)
1176 push_back(*__i);
1177}
1178
1179template <class _Tp, class _Alloc>
1180list<_Tp, _Alloc>::list(initializer_list<value_type> __il)
1181{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001182#if _LIBCPP_DEBUG_LEVEL >= 2
1183 __get_db()->__insert_c(this);
1184#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001185 for (typename initializer_list<value_type>::const_iterator __i = __il.begin(),
1186 __e = __il.end(); __i != __e; ++__i)
1187 push_back(*__i);
1188}
1189
Howard Hinnant54976f22011-08-12 21:56:02 +00001190#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1191
Howard Hinnant3e519522010-05-11 19:42:16 +00001192template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00001193inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001194list<_Tp, _Alloc>&
1195list<_Tp, _Alloc>::operator=(const list& __c)
1196{
1197 if (this != &__c)
1198 {
1199 base::__copy_assign_alloc(__c);
1200 assign(__c.begin(), __c.end());
1201 }
1202 return *this;
1203}
1204
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001205#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +00001206
1207template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00001208inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001209list<_Tp, _Alloc>::list(list&& __c)
Howard Hinnant45900102011-06-03 17:30:28 +00001210 _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value)
Howard Hinnantce48a112011-06-30 21:18:19 +00001211 : base(allocator_type(_VSTD::move(__c.__node_alloc())))
Howard Hinnant3e519522010-05-11 19:42:16 +00001212{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001213#if _LIBCPP_DEBUG_LEVEL >= 2
1214 __get_db()->__insert_c(this);
1215#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001216 splice(end(), __c);
1217}
1218
1219template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00001220inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001221list<_Tp, _Alloc>::list(list&& __c, const allocator_type& __a)
1222 : base(__a)
1223{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001224#if _LIBCPP_DEBUG_LEVEL >= 2
1225 __get_db()->__insert_c(this);
1226#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001227 if (__a == __c.get_allocator())
1228 splice(end(), __c);
1229 else
1230 {
Howard Hinnantc003db12011-11-29 18:15:50 +00001231 typedef move_iterator<iterator> _Ip;
1232 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnant3e519522010-05-11 19:42:16 +00001233 }
1234}
1235
1236template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00001237inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001238list<_Tp, _Alloc>&
1239list<_Tp, _Alloc>::operator=(list&& __c)
Howard Hinnant45900102011-06-03 17:30:28 +00001240 _NOEXCEPT_(
1241 __node_alloc_traits::propagate_on_container_move_assignment::value &&
1242 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +00001243{
1244 __move_assign(__c, integral_constant<bool,
1245 __node_alloc_traits::propagate_on_container_move_assignment::value>());
1246 return *this;
1247}
1248
1249template <class _Tp, class _Alloc>
1250void
1251list<_Tp, _Alloc>::__move_assign(list& __c, false_type)
1252{
1253 if (base::__node_alloc() != __c.__node_alloc())
1254 {
Howard Hinnantc003db12011-11-29 18:15:50 +00001255 typedef move_iterator<iterator> _Ip;
1256 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnant3e519522010-05-11 19:42:16 +00001257 }
1258 else
1259 __move_assign(__c, true_type());
1260}
1261
1262template <class _Tp, class _Alloc>
1263void
1264list<_Tp, _Alloc>::__move_assign(list& __c, true_type)
Howard Hinnant45900102011-06-03 17:30:28 +00001265 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +00001266{
1267 clear();
1268 base::__move_assign_alloc(__c);
1269 splice(end(), __c);
1270}
1271
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001272#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +00001273
1274template <class _Tp, class _Alloc>
1275template <class _InpIter>
1276void
1277list<_Tp, _Alloc>::assign(_InpIter __f, _InpIter __l,
1278 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1279{
1280 iterator __i = begin();
1281 iterator __e = end();
1282 for (; __f != __l && __i != __e; ++__f, ++__i)
1283 *__i = *__f;
1284 if (__i == __e)
1285 insert(__e, __f, __l);
1286 else
1287 erase(__i, __e);
1288}
1289
1290template <class _Tp, class _Alloc>
1291void
1292list<_Tp, _Alloc>::assign(size_type __n, const value_type& __x)
1293{
1294 iterator __i = begin();
1295 iterator __e = end();
1296 for (; __n > 0 && __i != __e; --__n, ++__i)
1297 *__i = __x;
1298 if (__i == __e)
1299 insert(__e, __n, __x);
1300 else
1301 erase(__i, __e);
1302}
1303
1304template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00001305inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001306_Alloc
Howard Hinnant45900102011-06-03 17:30:28 +00001307list<_Tp, _Alloc>::get_allocator() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001308{
1309 return allocator_type(base::__node_alloc());
1310}
1311
1312template <class _Tp, class _Alloc>
1313typename list<_Tp, _Alloc>::iterator
1314list<_Tp, _Alloc>::insert(const_iterator __p, const value_type& __x)
1315{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001316#if _LIBCPP_DEBUG_LEVEL >= 2
1317 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1318 "list::insert(iterator, x) called with an iterator not"
1319 " referring to this list");
1320#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001321 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001322 typedef __allocator_destructor<__node_allocator> _Dp;
1323 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant3e519522010-05-11 19:42:16 +00001324 __hold->__prev_ = 0;
Howard Hinnantce48a112011-06-30 21:18:19 +00001325 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001326 __link_nodes(__p.__ptr_, __hold.get(), __hold.get());
Howard Hinnant3e519522010-05-11 19:42:16 +00001327 ++base::__sz();
Howard Hinnantb0e4c9d2013-04-05 00:18:49 +00001328#if _LIBCPP_DEBUG_LEVEL >= 2
1329 return iterator(__hold.release(), this);
1330#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001331 return iterator(__hold.release());
Howard Hinnantb0e4c9d2013-04-05 00:18:49 +00001332#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001333}
1334
1335template <class _Tp, class _Alloc>
1336typename list<_Tp, _Alloc>::iterator
1337list<_Tp, _Alloc>::insert(const_iterator __p, size_type __n, const value_type& __x)
1338{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001339#if _LIBCPP_DEBUG_LEVEL >= 2
1340 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1341 "list::insert(iterator, n, x) called with an iterator not"
1342 " referring to this list");
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001343 iterator __r(__p.__ptr_, this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001344#else
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001345 iterator __r(__p.__ptr_);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001346#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001347 if (__n > 0)
1348 {
1349 size_type __ds = 0;
1350 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001351 typedef __allocator_destructor<__node_allocator> _Dp;
1352 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant3e519522010-05-11 19:42:16 +00001353 __hold->__prev_ = 0;
Howard Hinnantce48a112011-06-30 21:18:19 +00001354 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnant3e519522010-05-11 19:42:16 +00001355 ++__ds;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001356#if _LIBCPP_DEBUG_LEVEL >= 2
1357 __r = iterator(__hold.get(), this);
1358#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001359 __r = iterator(__hold.get());
Howard Hinnant920b56c2011-09-27 23:55:03 +00001360#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001361 __hold.release();
1362 iterator __e = __r;
1363#ifndef _LIBCPP_NO_EXCEPTIONS
1364 try
1365 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001366#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001367 for (--__n; __n != 0; --__n, ++__e, ++__ds)
1368 {
1369 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001370 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnant3e519522010-05-11 19:42:16 +00001371 __e.__ptr_->__next_ = __hold.get();
1372 __hold->__prev_ = __e.__ptr_;
1373 __hold.release();
1374 }
1375#ifndef _LIBCPP_NO_EXCEPTIONS
1376 }
1377 catch (...)
1378 {
1379 while (true)
1380 {
Howard Hinnantce48a112011-06-30 21:18:19 +00001381 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Howard Hinnant3e519522010-05-11 19:42:16 +00001382 __node_pointer __prev = __e.__ptr_->__prev_;
1383 __node_alloc_traits::deallocate(__na, __e.__ptr_, 1);
1384 if (__prev == 0)
1385 break;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001386#if _LIBCPP_DEBUG_LEVEL >= 2
1387 __e = iterator(__prev, this);
1388#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001389 __e = iterator(__prev);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001390#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001391 }
1392 throw;
1393 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001394#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001395 __link_nodes(__p.__ptr_, __r.__ptr_, __e.__ptr_);
Howard Hinnant3e519522010-05-11 19:42:16 +00001396 base::__sz() += __ds;
1397 }
1398 return __r;
1399}
1400
1401template <class _Tp, class _Alloc>
1402template <class _InpIter>
1403typename list<_Tp, _Alloc>::iterator
1404list<_Tp, _Alloc>::insert(const_iterator __p, _InpIter __f, _InpIter __l,
1405 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
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, range) 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 (__f != __l)
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_), *__f);
Howard Hinnant3e519522010-05-11 19:42:16 +00001423 ++__ds;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001424#if _LIBCPP_DEBUG_LEVEL >= 2
1425 __r = iterator(__hold.get(), this);
1426#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001427 __r = iterator(__hold.get());
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
Eric Fiselier61bff612015-03-19 03:20:02 +00001435 for (++__f; __f != __l; ++__f, (void) ++__e, (void) ++__ds)
Howard Hinnant3e519522010-05-11 19:42:16 +00001436 {
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_), *__f);
Howard Hinnant3e519522010-05-11 19:42:16 +00001439 __e.__ptr_->__next_ = __hold.get();
1440 __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));
Howard Hinnant3e519522010-05-11 19:42:16 +00001450 __node_pointer __prev = __e.__ptr_->__prev_;
1451 __node_alloc_traits::deallocate(__na, __e.__ptr_, 1);
1452 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>
1470void
1471list<_Tp, _Alloc>::push_front(const value_type& __x)
1472{
1473 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001474 typedef __allocator_destructor<__node_allocator> _Dp;
1475 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001476 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Marshall Clow28d65da2014-08-05 01:34:12 +00001477 __link_nodes_at_front(__hold.get(), __hold.get());
Howard Hinnant3e519522010-05-11 19:42:16 +00001478 ++base::__sz();
1479 __hold.release();
1480}
1481
1482template <class _Tp, class _Alloc>
1483void
1484list<_Tp, _Alloc>::push_back(const value_type& __x)
1485{
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 Hinnantce48a112011-06-30 21:18:19 +00001489 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Marshall Clow28d65da2014-08-05 01:34:12 +00001490 __link_nodes_at_back(__hold.get(), __hold.get());
Howard Hinnant3e519522010-05-11 19:42:16 +00001491 ++base::__sz();
1492 __hold.release();
1493}
1494
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001495#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +00001496
1497template <class _Tp, class _Alloc>
1498void
1499list<_Tp, _Alloc>::push_front(value_type&& __x)
1500{
1501 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001502 typedef __allocator_destructor<__node_allocator> _Dp;
1503 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001504 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
Marshall Clow28d65da2014-08-05 01:34:12 +00001505 __link_nodes_at_front(__hold.get(), __hold.get());
Howard Hinnant3e519522010-05-11 19:42:16 +00001506 ++base::__sz();
1507 __hold.release();
1508}
1509
1510template <class _Tp, class _Alloc>
1511void
1512list<_Tp, _Alloc>::push_back(value_type&& __x)
1513{
1514 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001515 typedef __allocator_destructor<__node_allocator> _Dp;
1516 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001517 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
Marshall Clow28d65da2014-08-05 01:34:12 +00001518 __link_nodes_at_back(__hold.get(), __hold.get());
Howard Hinnant3e519522010-05-11 19:42:16 +00001519 ++base::__sz();
1520 __hold.release();
1521}
1522
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001523#ifndef _LIBCPP_HAS_NO_VARIADICS
1524
Howard Hinnant3e519522010-05-11 19:42:16 +00001525template <class _Tp, class _Alloc>
1526template <class... _Args>
1527void
1528list<_Tp, _Alloc>::emplace_front(_Args&&... __args)
1529{
1530 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001531 typedef __allocator_destructor<__node_allocator> _Dp;
1532 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001533 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
Marshall Clow28d65da2014-08-05 01:34:12 +00001534 __link_nodes_at_front(__hold.get(), __hold.get());
Howard Hinnant3e519522010-05-11 19:42:16 +00001535 ++base::__sz();
1536 __hold.release();
1537}
1538
1539template <class _Tp, class _Alloc>
1540template <class... _Args>
1541void
1542list<_Tp, _Alloc>::emplace_back(_Args&&... __args)
1543{
1544 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001545 typedef __allocator_destructor<__node_allocator> _Dp;
1546 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001547 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
Marshall Clow28d65da2014-08-05 01:34:12 +00001548 __link_nodes_at_back(__hold.get(), __hold.get());
Howard Hinnant3e519522010-05-11 19:42:16 +00001549 ++base::__sz();
1550 __hold.release();
1551}
1552
1553template <class _Tp, class _Alloc>
1554template <class... _Args>
1555typename list<_Tp, _Alloc>::iterator
1556list<_Tp, _Alloc>::emplace(const_iterator __p, _Args&&... __args)
1557{
Howard Hinnantb0e4c9d2013-04-05 00:18:49 +00001558#if _LIBCPP_DEBUG_LEVEL >= 2
1559 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1560 "list::emplace(iterator, args...) called with an iterator not"
1561 " referring to this list");
1562#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001563 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001564 typedef __allocator_destructor<__node_allocator> _Dp;
1565 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant3e519522010-05-11 19:42:16 +00001566 __hold->__prev_ = 0;
Howard Hinnantce48a112011-06-30 21:18:19 +00001567 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001568 __link_nodes(__p.__ptr_, __hold.get(), __hold.get());
Howard Hinnant3e519522010-05-11 19:42:16 +00001569 ++base::__sz();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001570#if _LIBCPP_DEBUG_LEVEL >= 2
1571 return iterator(__hold.release(), this);
1572#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001573 return iterator(__hold.release());
Howard Hinnant920b56c2011-09-27 23:55:03 +00001574#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001575}
1576
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001577#endif // _LIBCPP_HAS_NO_VARIADICS
1578
Howard Hinnant3e519522010-05-11 19:42:16 +00001579template <class _Tp, class _Alloc>
1580typename list<_Tp, _Alloc>::iterator
1581list<_Tp, _Alloc>::insert(const_iterator __p, value_type&& __x)
1582{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001583#if _LIBCPP_DEBUG_LEVEL >= 2
1584 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1585 "list::insert(iterator, x) called with an iterator not"
1586 " referring to this list");
1587#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001588 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001589 typedef __allocator_destructor<__node_allocator> _Dp;
1590 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant3e519522010-05-11 19:42:16 +00001591 __hold->__prev_ = 0;
Howard Hinnantce48a112011-06-30 21:18:19 +00001592 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001593 __link_nodes(__p.__ptr_, __hold.get(), __hold.get());
Howard Hinnant3e519522010-05-11 19:42:16 +00001594 ++base::__sz();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001595#if _LIBCPP_DEBUG_LEVEL >= 2
1596 return iterator(__hold.release(), this);
1597#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001598 return iterator(__hold.release());
Howard Hinnant920b56c2011-09-27 23:55:03 +00001599#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001600}
1601
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001602#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +00001603
1604template <class _Tp, class _Alloc>
1605void
1606list<_Tp, _Alloc>::pop_front()
1607{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001608 _LIBCPP_ASSERT(!empty(), "list::pop_front() called with empty list");
Howard Hinnant3e519522010-05-11 19:42:16 +00001609 __node_allocator& __na = base::__node_alloc();
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001610 __node_pointer __n = base::__end_.__next_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001611 base::__unlink_nodes(__n, __n);
1612 --base::__sz();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001613#if _LIBCPP_DEBUG_LEVEL >= 2
1614 __c_node* __c = __get_db()->__find_c_and_lock(this);
1615 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1616 {
1617 --__p;
1618 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001619 if (__i->__ptr_ == __n)
Howard Hinnant920b56c2011-09-27 23:55:03 +00001620 {
1621 (*__p)->__c_ = nullptr;
1622 if (--__c->end_ != __p)
1623 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1624 }
1625 }
1626 __get_db()->unlock();
1627#endif
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001628 __node_alloc_traits::destroy(__na, _VSTD::addressof(__n->__value_));
1629 __node_alloc_traits::deallocate(__na, __n, 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001630}
1631
1632template <class _Tp, class _Alloc>
1633void
1634list<_Tp, _Alloc>::pop_back()
1635{
Dmitri Gribenkoc3f9c802013-06-24 06:15:57 +00001636 _LIBCPP_ASSERT(!empty(), "list::pop_back() called with empty list");
Howard Hinnant3e519522010-05-11 19:42:16 +00001637 __node_allocator& __na = base::__node_alloc();
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001638 __node_pointer __n = base::__end_.__prev_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001639 base::__unlink_nodes(__n, __n);
1640 --base::__sz();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001641#if _LIBCPP_DEBUG_LEVEL >= 2
1642 __c_node* __c = __get_db()->__find_c_and_lock(this);
1643 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1644 {
1645 --__p;
1646 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001647 if (__i->__ptr_ == __n)
Howard Hinnant920b56c2011-09-27 23:55:03 +00001648 {
1649 (*__p)->__c_ = nullptr;
1650 if (--__c->end_ != __p)
1651 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1652 }
1653 }
1654 __get_db()->unlock();
1655#endif
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001656 __node_alloc_traits::destroy(__na, _VSTD::addressof(__n->__value_));
1657 __node_alloc_traits::deallocate(__na, __n, 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001658}
1659
1660template <class _Tp, class _Alloc>
1661typename list<_Tp, _Alloc>::iterator
1662list<_Tp, _Alloc>::erase(const_iterator __p)
1663{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001664#if _LIBCPP_DEBUG_LEVEL >= 2
1665 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1666 "list::erase(iterator) called with an iterator not"
1667 " referring to this list");
1668#endif
Howard Hinnantb0e4c9d2013-04-05 00:18:49 +00001669 _LIBCPP_ASSERT(__p != end(),
1670 "list::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnant3e519522010-05-11 19:42:16 +00001671 __node_allocator& __na = base::__node_alloc();
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001672 __node_pointer __n = __p.__ptr_;
1673 __node_pointer __r = __n->__next_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001674 base::__unlink_nodes(__n, __n);
1675 --base::__sz();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001676#if _LIBCPP_DEBUG_LEVEL >= 2
1677 __c_node* __c = __get_db()->__find_c_and_lock(this);
1678 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1679 {
1680 --__p;
1681 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001682 if (__i->__ptr_ == __n)
Howard Hinnant920b56c2011-09-27 23:55:03 +00001683 {
1684 (*__p)->__c_ = nullptr;
1685 if (--__c->end_ != __p)
1686 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1687 }
1688 }
1689 __get_db()->unlock();
1690#endif
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001691 __node_alloc_traits::destroy(__na, _VSTD::addressof(__n->__value_));
1692 __node_alloc_traits::deallocate(__na, __n, 1);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001693#if _LIBCPP_DEBUG_LEVEL >= 2
1694 return iterator(__r, this);
1695#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001696 return iterator(__r);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001697#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001698}
1699
1700template <class _Tp, class _Alloc>
1701typename list<_Tp, _Alloc>::iterator
1702list<_Tp, _Alloc>::erase(const_iterator __f, const_iterator __l)
1703{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001704#if _LIBCPP_DEBUG_LEVEL >= 2
1705 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__f) == this,
1706 "list::erase(iterator, iterator) called with an iterator not"
1707 " referring to this list");
1708#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001709 if (__f != __l)
1710 {
1711 __node_allocator& __na = base::__node_alloc();
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001712 base::__unlink_nodes(__f.__ptr_, __l.__ptr_->__prev_);
Howard Hinnant3e519522010-05-11 19:42:16 +00001713 while (__f != __l)
1714 {
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001715 __node_pointer __n = __f.__ptr_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001716 ++__f;
1717 --base::__sz();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001718#if _LIBCPP_DEBUG_LEVEL >= 2
1719 __c_node* __c = __get_db()->__find_c_and_lock(this);
1720 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1721 {
1722 --__p;
1723 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001724 if (__i->__ptr_ == __n)
Howard Hinnant920b56c2011-09-27 23:55:03 +00001725 {
1726 (*__p)->__c_ = nullptr;
1727 if (--__c->end_ != __p)
1728 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1729 }
1730 }
1731 __get_db()->unlock();
1732#endif
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001733 __node_alloc_traits::destroy(__na, _VSTD::addressof(__n->__value_));
1734 __node_alloc_traits::deallocate(__na, __n, 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001735 }
1736 }
Howard Hinnant920b56c2011-09-27 23:55:03 +00001737#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001738 return iterator(__l.__ptr_, this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001739#else
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001740 return iterator(__l.__ptr_);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001741#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001742}
1743
1744template <class _Tp, class _Alloc>
1745void
1746list<_Tp, _Alloc>::resize(size_type __n)
1747{
1748 if (__n < base::__sz())
1749 erase(__iterator(__n), end());
1750 else if (__n > base::__sz())
1751 {
1752 __n -= base::__sz();
1753 size_type __ds = 0;
1754 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001755 typedef __allocator_destructor<__node_allocator> _Dp;
1756 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant3e519522010-05-11 19:42:16 +00001757 __hold->__prev_ = 0;
Howard Hinnantce48a112011-06-30 21:18:19 +00001758 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_));
Howard Hinnant3e519522010-05-11 19:42:16 +00001759 ++__ds;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001760#if _LIBCPP_DEBUG_LEVEL >= 2
1761 iterator __r = iterator(__hold.release(), this);
1762#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001763 iterator __r = iterator(__hold.release());
Howard Hinnant920b56c2011-09-27 23:55:03 +00001764#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001765 iterator __e = __r;
1766#ifndef _LIBCPP_NO_EXCEPTIONS
1767 try
1768 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001769#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001770 for (--__n; __n != 0; --__n, ++__e, ++__ds)
1771 {
1772 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001773 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_));
Howard Hinnant3e519522010-05-11 19:42:16 +00001774 __e.__ptr_->__next_ = __hold.get();
1775 __hold->__prev_ = __e.__ptr_;
1776 __hold.release();
1777 }
1778#ifndef _LIBCPP_NO_EXCEPTIONS
1779 }
1780 catch (...)
1781 {
1782 while (true)
1783 {
Howard Hinnantce48a112011-06-30 21:18:19 +00001784 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Howard Hinnant3e519522010-05-11 19:42:16 +00001785 __node_pointer __prev = __e.__ptr_->__prev_;
1786 __node_alloc_traits::deallocate(__na, __e.__ptr_, 1);
1787 if (__prev == 0)
1788 break;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001789#if _LIBCPP_DEBUG_LEVEL >= 2
1790 __e = iterator(__prev, this);
1791#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001792 __e = iterator(__prev);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001793#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001794 }
1795 throw;
1796 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001797#endif // _LIBCPP_NO_EXCEPTIONS
Marshall Clow28d65da2014-08-05 01:34:12 +00001798 __link_nodes_at_back(__r.__ptr_, __e.__ptr_);
Howard Hinnant3e519522010-05-11 19:42:16 +00001799 base::__sz() += __ds;
1800 }
1801}
1802
1803template <class _Tp, class _Alloc>
1804void
1805list<_Tp, _Alloc>::resize(size_type __n, const value_type& __x)
1806{
1807 if (__n < base::__sz())
1808 erase(__iterator(__n), end());
1809 else if (__n > base::__sz())
1810 {
1811 __n -= base::__sz();
1812 size_type __ds = 0;
1813 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001814 typedef __allocator_destructor<__node_allocator> _Dp;
1815 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant3e519522010-05-11 19:42:16 +00001816 __hold->__prev_ = 0;
Howard Hinnantce48a112011-06-30 21:18:19 +00001817 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnant3e519522010-05-11 19:42:16 +00001818 ++__ds;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001819#if _LIBCPP_DEBUG_LEVEL >= 2
1820 iterator __r = iterator(__hold.release(), this);
1821#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001822 iterator __r = iterator(__hold.release());
Howard Hinnant920b56c2011-09-27 23:55:03 +00001823#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001824 iterator __e = __r;
1825#ifndef _LIBCPP_NO_EXCEPTIONS
1826 try
1827 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001828#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001829 for (--__n; __n != 0; --__n, ++__e, ++__ds)
1830 {
1831 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001832 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnant3e519522010-05-11 19:42:16 +00001833 __e.__ptr_->__next_ = __hold.get();
1834 __hold->__prev_ = __e.__ptr_;
1835 __hold.release();
1836 }
1837#ifndef _LIBCPP_NO_EXCEPTIONS
1838 }
1839 catch (...)
1840 {
1841 while (true)
1842 {
Howard Hinnantce48a112011-06-30 21:18:19 +00001843 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Howard Hinnant3e519522010-05-11 19:42:16 +00001844 __node_pointer __prev = __e.__ptr_->__prev_;
1845 __node_alloc_traits::deallocate(__na, __e.__ptr_, 1);
1846 if (__prev == 0)
1847 break;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001848#if _LIBCPP_DEBUG_LEVEL >= 2
1849 __e = iterator(__prev, this);
1850#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001851 __e = iterator(__prev);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001852#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001853 }
1854 throw;
1855 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001856#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001857 __link_nodes(static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::
1858 pointer_to(base::__end_)), __r.__ptr_, __e.__ptr_);
Howard Hinnant3e519522010-05-11 19:42:16 +00001859 base::__sz() += __ds;
Howard Hinnantb3371f62010-08-22 00:02:43 +00001860 }
Howard Hinnant3e519522010-05-11 19:42:16 +00001861}
1862
1863template <class _Tp, class _Alloc>
1864void
1865list<_Tp, _Alloc>::splice(const_iterator __p, list& __c)
1866{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001867 _LIBCPP_ASSERT(this != &__c,
1868 "list::splice(iterator, list) called with this == &list");
1869#if _LIBCPP_DEBUG_LEVEL >= 2
1870 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1871 "list::splice(iterator, list) called with an iterator not"
1872 " referring to this list");
1873#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001874 if (!__c.empty())
1875 {
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001876 __node_pointer __f = __c.__end_.__next_;
1877 __node_pointer __l = __c.__end_.__prev_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001878 base::__unlink_nodes(__f, __l);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001879 __link_nodes(__p.__ptr_, __f, __l);
Howard Hinnant3e519522010-05-11 19:42:16 +00001880 base::__sz() += __c.__sz();
1881 __c.__sz() = 0;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001882#if _LIBCPP_DEBUG_LEVEL >= 2
1883 __libcpp_db* __db = __get_db();
1884 __c_node* __cn1 = __db->__find_c_and_lock(this);
1885 __c_node* __cn2 = __db->__find_c(&__c);
1886 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
1887 {
1888 --__p;
1889 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001890 if (__i->__ptr_ != static_cast<__node_pointer>(
1891 pointer_traits<__node_base_pointer>::pointer_to(__c.__end_)))
Howard Hinnant920b56c2011-09-27 23:55:03 +00001892 {
1893 __cn1->__add(*__p);
1894 (*__p)->__c_ = __cn1;
1895 if (--__cn2->end_ != __p)
1896 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
1897 }
1898 }
1899 __db->unlock();
1900#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001901 }
1902}
1903
1904template <class _Tp, class _Alloc>
1905void
1906list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __i)
1907{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001908#if _LIBCPP_DEBUG_LEVEL >= 2
1909 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1910 "list::splice(iterator, list, iterator) called with first iterator not"
1911 " referring to this list");
1912 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__i) == &__c,
1913 "list::splice(iterator, list, iterator) called with second iterator not"
1914 " referring to list argument");
1915 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(&__i),
1916 "list::splice(iterator, list, iterator) called with second iterator not"
1917 " derefereceable");
1918#endif
1919 if (__p.__ptr_ != __i.__ptr_ && __p.__ptr_ != __i.__ptr_->__next_)
Howard Hinnant3e519522010-05-11 19:42:16 +00001920 {
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001921 __node_pointer __f = __i.__ptr_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001922 base::__unlink_nodes(__f, __f);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001923 __link_nodes(__p.__ptr_, __f, __f);
Howard Hinnant3e519522010-05-11 19:42:16 +00001924 --__c.__sz();
1925 ++base::__sz();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001926#if _LIBCPP_DEBUG_LEVEL >= 2
1927 __libcpp_db* __db = __get_db();
1928 __c_node* __cn1 = __db->__find_c_and_lock(this);
1929 __c_node* __cn2 = __db->__find_c(&__c);
1930 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
1931 {
1932 --__p;
1933 iterator* __j = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001934 if (__j->__ptr_ == __f)
Howard Hinnant920b56c2011-09-27 23:55:03 +00001935 {
1936 __cn1->__add(*__p);
1937 (*__p)->__c_ = __cn1;
1938 if (--__cn2->end_ != __p)
1939 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
1940 }
1941 }
1942 __db->unlock();
1943#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001944 }
1945}
1946
1947template <class _Tp, class _Alloc>
1948void
1949list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l)
1950{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001951#if _LIBCPP_DEBUG_LEVEL >= 2
1952 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1953 "list::splice(iterator, list, iterator, iterator) called with first iterator not"
1954 " referring to this list");
1955 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__f) == &__c,
1956 "list::splice(iterator, list, iterator, iterator) called with second iterator not"
1957 " referring to list argument");
1958 if (this == &__c)
1959 {
1960 for (const_iterator __i = __f; __i != __l; ++__i)
1961 _LIBCPP_ASSERT(__i != __p,
1962 "list::splice(iterator, list, iterator, iterator)"
1963 " called with the first iterator within the range"
1964 " of the second and third iterators");
1965 }
1966#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001967 if (__f != __l)
1968 {
1969 if (this != &__c)
1970 {
Howard Hinnantce48a112011-06-30 21:18:19 +00001971 size_type __s = _VSTD::distance(__f, __l);
Howard Hinnant3e519522010-05-11 19:42:16 +00001972 __c.__sz() -= __s;
1973 base::__sz() += __s;
1974 }
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001975 __node_pointer __first = __f.__ptr_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001976 --__l;
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001977 __node_pointer __last = __l.__ptr_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001978 base::__unlink_nodes(__first, __last);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001979 __link_nodes(__p.__ptr_, __first, __last);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001980#if _LIBCPP_DEBUG_LEVEL >= 2
1981 __libcpp_db* __db = __get_db();
1982 __c_node* __cn1 = __db->__find_c_and_lock(this);
1983 __c_node* __cn2 = __db->__find_c(&__c);
1984 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
1985 {
1986 --__p;
1987 iterator* __j = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001988 for (__node_pointer __k = __f.__ptr_;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001989 __k != __l.__ptr_; __k = __k->__next_)
1990 {
1991 if (__j->__ptr_ == __k)
1992 {
1993 __cn1->__add(*__p);
1994 (*__p)->__c_ = __cn1;
1995 if (--__cn2->end_ != __p)
1996 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
1997 }
1998 }
1999 }
2000 __db->unlock();
2001#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002002 }
2003}
2004
2005template <class _Tp, class _Alloc>
2006void
2007list<_Tp, _Alloc>::remove(const value_type& __x)
2008{
Marshall Clowced70062014-08-08 15:35:52 +00002009 list<_Tp, _Alloc> __deleted_nodes; // collect the nodes we're removing
2010 for (const_iterator __i = begin(), __e = end(); __i != __e;)
Howard Hinnant3e519522010-05-11 19:42:16 +00002011 {
2012 if (*__i == __x)
2013 {
Marshall Clowced70062014-08-08 15:35:52 +00002014 const_iterator __j = _VSTD::next(__i);
Howard Hinnant3e519522010-05-11 19:42:16 +00002015 for (; __j != __e && *__j == __x; ++__j)
2016 ;
Marshall Clowced70062014-08-08 15:35:52 +00002017 __deleted_nodes.splice(__deleted_nodes.end(), *this, __i, __j);
2018 __i = __j;
Marshall Clow90ba05332014-08-04 17:32:25 +00002019 if (__i != __e)
Marshall Clow28d65da2014-08-05 01:34:12 +00002020 ++__i;
Howard Hinnant3e519522010-05-11 19:42:16 +00002021 }
2022 else
2023 ++__i;
2024 }
2025}
2026
2027template <class _Tp, class _Alloc>
2028template <class _Pred>
2029void
2030list<_Tp, _Alloc>::remove_if(_Pred __pred)
2031{
2032 for (iterator __i = begin(), __e = end(); __i != __e;)
2033 {
2034 if (__pred(*__i))
2035 {
Howard Hinnantce48a112011-06-30 21:18:19 +00002036 iterator __j = _VSTD::next(__i);
Howard Hinnant3e519522010-05-11 19:42:16 +00002037 for (; __j != __e && __pred(*__j); ++__j)
2038 ;
2039 __i = erase(__i, __j);
Marshall Clow90ba05332014-08-04 17:32:25 +00002040 if (__i != __e)
Marshall Clow28d65da2014-08-05 01:34:12 +00002041 ++__i;
Howard Hinnant3e519522010-05-11 19:42:16 +00002042 }
2043 else
2044 ++__i;
2045 }
2046}
2047
2048template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00002049inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00002050void
2051list<_Tp, _Alloc>::unique()
2052{
2053 unique(__equal_to<value_type>());
2054}
2055
2056template <class _Tp, class _Alloc>
2057template <class _BinaryPred>
2058void
2059list<_Tp, _Alloc>::unique(_BinaryPred __binary_pred)
2060{
2061 for (iterator __i = begin(), __e = end(); __i != __e;)
2062 {
Howard Hinnantce48a112011-06-30 21:18:19 +00002063 iterator __j = _VSTD::next(__i);
Howard Hinnant3e519522010-05-11 19:42:16 +00002064 for (; __j != __e && __binary_pred(*__i, *__j); ++__j)
2065 ;
2066 if (++__i != __j)
2067 __i = erase(__i, __j);
2068 }
2069}
2070
2071template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00002072inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00002073void
2074list<_Tp, _Alloc>::merge(list& __c)
2075{
2076 merge(__c, __less<value_type>());
2077}
2078
2079template <class _Tp, class _Alloc>
2080template <class _Comp>
2081void
2082list<_Tp, _Alloc>::merge(list& __c, _Comp __comp)
2083{
2084 if (this != &__c)
2085 {
2086 iterator __f1 = begin();
2087 iterator __e1 = end();
2088 iterator __f2 = __c.begin();
2089 iterator __e2 = __c.end();
2090 while (__f1 != __e1 && __f2 != __e2)
2091 {
2092 if (__comp(*__f2, *__f1))
2093 {
2094 size_type __ds = 1;
Howard Hinnantce48a112011-06-30 21:18:19 +00002095 iterator __m2 = _VSTD::next(__f2);
Howard Hinnant3e519522010-05-11 19:42:16 +00002096 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2, ++__ds)
2097 ;
2098 base::__sz() += __ds;
2099 __c.__sz() -= __ds;
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002100 __node_pointer __f = __f2.__ptr_;
2101 __node_pointer __l = __m2.__ptr_->__prev_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002102 __f2 = __m2;
2103 base::__unlink_nodes(__f, __l);
Howard Hinnantce48a112011-06-30 21:18:19 +00002104 __m2 = _VSTD::next(__f1);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002105 __link_nodes(__f1.__ptr_, __f, __l);
Howard Hinnant3e519522010-05-11 19:42:16 +00002106 __f1 = __m2;
2107 }
2108 else
2109 ++__f1;
2110 }
2111 splice(__e1, __c);
Howard Hinnant920b56c2011-09-27 23:55:03 +00002112#if _LIBCPP_DEBUG_LEVEL >= 2
2113 __libcpp_db* __db = __get_db();
2114 __c_node* __cn1 = __db->__find_c_and_lock(this);
2115 __c_node* __cn2 = __db->__find_c(&__c);
2116 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
2117 {
2118 --__p;
2119 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002120 if (__i->__ptr_ != static_cast<__node_pointer>(
2121 pointer_traits<__node_base_pointer>::pointer_to(__c.__end_)))
Howard Hinnant920b56c2011-09-27 23:55:03 +00002122 {
2123 __cn1->__add(*__p);
2124 (*__p)->__c_ = __cn1;
2125 if (--__cn2->end_ != __p)
2126 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
2127 }
2128 }
2129 __db->unlock();
2130#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002131 }
2132}
2133
2134template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00002135inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00002136void
2137list<_Tp, _Alloc>::sort()
2138{
2139 sort(__less<value_type>());
2140}
2141
2142template <class _Tp, class _Alloc>
2143template <class _Comp>
Howard Hinnant848a5372010-09-22 16:48:34 +00002144inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00002145void
2146list<_Tp, _Alloc>::sort(_Comp __comp)
2147{
2148 __sort(begin(), end(), base::__sz(), __comp);
2149}
2150
2151template <class _Tp, class _Alloc>
2152template <class _Comp>
Howard Hinnant3e519522010-05-11 19:42:16 +00002153typename list<_Tp, _Alloc>::iterator
2154list<_Tp, _Alloc>::__sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp)
2155{
2156 switch (__n)
2157 {
2158 case 0:
2159 case 1:
2160 return __f1;
2161 case 2:
2162 if (__comp(*--__e2, *__f1))
2163 {
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002164 __node_pointer __f = __e2.__ptr_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002165 base::__unlink_nodes(__f, __f);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002166 __link_nodes(__f1.__ptr_, __f, __f);
Howard Hinnant3e519522010-05-11 19:42:16 +00002167 return __e2;
2168 }
2169 return __f1;
2170 }
2171 size_type __n2 = __n / 2;
Howard Hinnantce48a112011-06-30 21:18:19 +00002172 iterator __e1 = _VSTD::next(__f1, __n2);
Howard Hinnant3e519522010-05-11 19:42:16 +00002173 iterator __r = __f1 = __sort(__f1, __e1, __n2, __comp);
2174 iterator __f2 = __e1 = __sort(__e1, __e2, __n - __n2, __comp);
2175 if (__comp(*__f2, *__f1))
2176 {
Howard Hinnantce48a112011-06-30 21:18:19 +00002177 iterator __m2 = _VSTD::next(__f2);
Howard Hinnant3e519522010-05-11 19:42:16 +00002178 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2)
2179 ;
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002180 __node_pointer __f = __f2.__ptr_;
2181 __node_pointer __l = __m2.__ptr_->__prev_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002182 __r = __f2;
2183 __e1 = __f2 = __m2;
2184 base::__unlink_nodes(__f, __l);
Howard Hinnantce48a112011-06-30 21:18:19 +00002185 __m2 = _VSTD::next(__f1);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002186 __link_nodes(__f1.__ptr_, __f, __l);
Howard Hinnant3e519522010-05-11 19:42:16 +00002187 __f1 = __m2;
2188 }
2189 else
2190 ++__f1;
2191 while (__f1 != __e1 && __f2 != __e2)
2192 {
2193 if (__comp(*__f2, *__f1))
2194 {
Howard Hinnantce48a112011-06-30 21:18:19 +00002195 iterator __m2 = _VSTD::next(__f2);
Howard Hinnant3e519522010-05-11 19:42:16 +00002196 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2)
2197 ;
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002198 __node_pointer __f = __f2.__ptr_;
2199 __node_pointer __l = __m2.__ptr_->__prev_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002200 if (__e1 == __f2)
2201 __e1 = __m2;
2202 __f2 = __m2;
2203 base::__unlink_nodes(__f, __l);
Howard Hinnantce48a112011-06-30 21:18:19 +00002204 __m2 = _VSTD::next(__f1);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002205 __link_nodes(__f1.__ptr_, __f, __l);
Howard Hinnant3e519522010-05-11 19:42:16 +00002206 __f1 = __m2;
2207 }
2208 else
2209 ++__f1;
2210 }
2211 return __r;
2212}
2213
2214template <class _Tp, class _Alloc>
2215void
Howard Hinnant45900102011-06-03 17:30:28 +00002216list<_Tp, _Alloc>::reverse() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00002217{
2218 if (base::__sz() > 1)
2219 {
2220 iterator __e = end();
Howard Hinnant920b56c2011-09-27 23:55:03 +00002221 for (iterator __i = begin(); __i.__ptr_ != __e.__ptr_;)
2222 {
Howard Hinnantce48a112011-06-30 21:18:19 +00002223 _VSTD::swap(__i.__ptr_->__prev_, __i.__ptr_->__next_);
Howard Hinnant920b56c2011-09-27 23:55:03 +00002224 __i.__ptr_ = __i.__ptr_->__prev_;
2225 }
Howard Hinnantce48a112011-06-30 21:18:19 +00002226 _VSTD::swap(__e.__ptr_->__prev_, __e.__ptr_->__next_);
Howard Hinnant3e519522010-05-11 19:42:16 +00002227 }
2228}
2229
2230template <class _Tp, class _Alloc>
Howard Hinnant920b56c2011-09-27 23:55:03 +00002231bool
2232list<_Tp, _Alloc>::__invariants() const
2233{
2234 return size() == _VSTD::distance(begin(), end());
2235}
2236
2237#if _LIBCPP_DEBUG_LEVEL >= 2
2238
2239template <class _Tp, class _Alloc>
2240bool
2241list<_Tp, _Alloc>::__dereferenceable(const const_iterator* __i) const
2242{
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002243 return __i->__ptr_ != static_cast<__node_pointer>(
2244 pointer_traits<__node_base_pointer>::pointer_to(const_cast<__node_base&>(this->__end_)));
Howard Hinnant920b56c2011-09-27 23:55:03 +00002245}
2246
2247template <class _Tp, class _Alloc>
2248bool
2249list<_Tp, _Alloc>::__decrementable(const const_iterator* __i) const
2250{
2251 return !empty() && __i->__ptr_ != base::__end_.__next_;
2252}
2253
2254template <class _Tp, class _Alloc>
2255bool
2256list<_Tp, _Alloc>::__addable(const const_iterator* __i, ptrdiff_t __n) const
2257{
2258 return false;
2259}
2260
2261template <class _Tp, class _Alloc>
2262bool
2263list<_Tp, _Alloc>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
2264{
2265 return false;
2266}
2267
2268#endif // _LIBCPP_DEBUG_LEVEL >= 2
2269
2270template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00002271inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00002272bool
2273operator==(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2274{
Howard Hinnantce48a112011-06-30 21:18:19 +00002275 return __x.size() == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnant3e519522010-05-11 19:42:16 +00002276}
2277
2278template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00002279inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00002280bool
2281operator< (const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2282{
Howard Hinnantce48a112011-06-30 21:18:19 +00002283 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnant3e519522010-05-11 19:42:16 +00002284}
2285
2286template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00002287inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00002288bool
2289operator!=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2290{
2291 return !(__x == __y);
2292}
2293
2294template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00002295inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00002296bool
2297operator> (const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2298{
2299 return __y < __x;
2300}
2301
2302template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00002303inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00002304bool
2305operator>=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2306{
2307 return !(__x < __y);
2308}
2309
2310template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00002311inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00002312bool
2313operator<=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2314{
2315 return !(__y < __x);
2316}
2317
2318template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00002319inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00002320void
2321swap(list<_Tp, _Alloc>& __x, list<_Tp, _Alloc>& __y)
Howard Hinnant45900102011-06-03 17:30:28 +00002322 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnant3e519522010-05-11 19:42:16 +00002323{
2324 __x.swap(__y);
2325}
2326
2327_LIBCPP_END_NAMESPACE_STD
2328
2329#endif // _LIBCPP_LIST