blob: d39f076711d94c68c376a450e96b559e3ce75aac [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&)
121 noexcept(!allocator_type::propagate_on_container_swap::value ||
122 __is_nothrow_swappable<allocator_type>::value);
123 void clear() noexcept;
Howard Hinnant3e519522010-05-11 19:42:16 +0000124
125 void splice(const_iterator position, list& x);
126 void splice(const_iterator position, list&& x);
127 void splice(const_iterator position, list& x, const_iterator i);
128 void splice(const_iterator position, list&& x, const_iterator i);
129 void splice(const_iterator position, list& x, const_iterator first,
130 const_iterator last);
131 void splice(const_iterator position, list&& x, const_iterator first,
132 const_iterator last);
133
134 void remove(const value_type& value);
135 template <class Pred> void remove_if(Pred pred);
136 void unique();
137 template <class BinaryPredicate>
138 void unique(BinaryPredicate binary_pred);
139 void merge(list& x);
140 void merge(list&& x);
141 template <class Compare>
142 void merge(list& x, Compare comp);
143 template <class Compare>
144 void merge(list&& x, Compare comp);
145 void sort();
146 template <class Compare>
147 void sort(Compare comp);
Howard Hinnant45900102011-06-03 17:30:28 +0000148 void reverse() noexcept;
Howard Hinnant3e519522010-05-11 19:42:16 +0000149};
150
151template <class T, class Alloc>
152 bool operator==(const list<T,Alloc>& x, const list<T,Alloc>& y);
153template <class T, class Alloc>
154 bool operator< (const list<T,Alloc>& x, const list<T,Alloc>& y);
155template <class T, class Alloc>
156 bool operator!=(const list<T,Alloc>& x, const list<T,Alloc>& y);
157template <class T, class Alloc>
158 bool operator> (const list<T,Alloc>& x, const list<T,Alloc>& y);
159template <class T, class Alloc>
160 bool operator>=(const list<T,Alloc>& x, const list<T,Alloc>& y);
161template <class T, class Alloc>
162 bool operator<=(const list<T,Alloc>& x, const list<T,Alloc>& y);
163
164template <class T, class Alloc>
Howard Hinnant45900102011-06-03 17:30:28 +0000165 void swap(list<T,Alloc>& x, list<T,Alloc>& y)
166 noexcept(noexcept(x.swap(y)));
Howard Hinnant3e519522010-05-11 19:42:16 +0000167
168} // std
169
170*/
171
172#include <__config>
173
174#include <memory>
175#include <limits>
176#include <initializer_list>
177#include <iterator>
178#include <algorithm>
179
Howard Hinnantab4f4382011-11-29 16:45:27 +0000180#include <__undef_min_max>
181
Eric Fiselierc1bd9192014-08-10 23:53:08 +0000182#include <__debug>
Howard Hinnant42a30462013-08-02 00:26:35 +0000183
Howard Hinnant073458b2011-10-17 20:05:10 +0000184#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnant3e519522010-05-11 19:42:16 +0000185#pragma GCC system_header
Howard Hinnant073458b2011-10-17 20:05:10 +0000186#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000187
188_LIBCPP_BEGIN_NAMESPACE_STD
189
Howard Hinnantce534202011-06-14 19:58:17 +0000190template <class _Tp, class _VoidPtr> struct __list_node;
Howard Hinnant3e519522010-05-11 19:42:16 +0000191
192template <class _Tp, class _VoidPtr>
193struct __list_node_base
194{
195 typedef typename pointer_traits<_VoidPtr>::template
196#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
197 rebind<__list_node<_Tp, _VoidPtr> > pointer;
198#else
199 rebind<__list_node<_Tp, _VoidPtr> >::other pointer;
200#endif
201
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000202 typedef typename pointer_traits<_VoidPtr>::template
203#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
204 rebind<__list_node_base> __base_pointer;
205#else
206 rebind<__list_node_base>::other __base_pointer;
207#endif
208
Howard Hinnant3e519522010-05-11 19:42:16 +0000209 pointer __prev_;
210 pointer __next_;
211
Howard Hinnant848a5372010-09-22 16:48:34 +0000212 _LIBCPP_INLINE_VISIBILITY
Marshall Clow28d65da2014-08-05 01:34:12 +0000213 __list_node_base() : __prev_(__self()), __next_(__self()) {}
214
215 _LIBCPP_INLINE_VISIBILITY
216 pointer __self()
217 {
Marshall Clowced70062014-08-08 15:35:52 +0000218 return static_cast<pointer>(pointer_traits<__base_pointer>::pointer_to(*this));
Marshall Clow28d65da2014-08-05 01:34:12 +0000219 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000220};
221
222template <class _Tp, class _VoidPtr>
223struct __list_node
224 : public __list_node_base<_Tp, _VoidPtr>
225{
226 _Tp __value_;
227};
228
Marshall Clowb5d34aa2015-02-18 17:24:08 +0000229template <class _Tp, class _Alloc = allocator<_Tp> > class _LIBCPP_TYPE_VIS_ONLY list;
Howard Hinnantce534202011-06-14 19:58:17 +0000230template <class _Tp, class _Alloc> class __list_imp;
Howard Hinnantf0544c22013-08-12 18:38:34 +0000231template <class _Tp, class _VoidPtr> class _LIBCPP_TYPE_VIS_ONLY __list_const_iterator;
Howard Hinnant3e519522010-05-11 19:42:16 +0000232
233template <class _Tp, class _VoidPtr>
Howard Hinnantf0544c22013-08-12 18:38:34 +0000234class _LIBCPP_TYPE_VIS_ONLY __list_iterator
Howard Hinnant3e519522010-05-11 19:42:16 +0000235{
236 typedef typename pointer_traits<_VoidPtr>::template
237#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
238 rebind<__list_node<_Tp, _VoidPtr> > __node_pointer;
239#else
240 rebind<__list_node<_Tp, _VoidPtr> >::other __node_pointer;
241#endif
242
243 __node_pointer __ptr_;
244
Howard Hinnant920b56c2011-09-27 23:55:03 +0000245#if _LIBCPP_DEBUG_LEVEL >= 2
246 _LIBCPP_INLINE_VISIBILITY
247 explicit __list_iterator(__node_pointer __p, const void* __c) _NOEXCEPT
248 : __ptr_(__p)
249 {
250 __get_db()->__insert_ic(this, __c);
251 }
252#else
Howard Hinnant848a5372010-09-22 16:48:34 +0000253 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000254 explicit __list_iterator(__node_pointer __p) _NOEXCEPT : __ptr_(__p) {}
Howard Hinnant920b56c2011-09-27 23:55:03 +0000255#endif
256
257
Howard Hinnant3e519522010-05-11 19:42:16 +0000258
259 template<class, class> friend class list;
260 template<class, class> friend class __list_imp;
261 template<class, class> friend class __list_const_iterator;
262public:
263 typedef bidirectional_iterator_tag iterator_category;
264 typedef _Tp value_type;
265 typedef value_type& reference;
266 typedef typename pointer_traits<_VoidPtr>::template
267#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
268 rebind<value_type>
269#else
270 rebind<value_type>::other
271#endif
272 pointer;
273 typedef typename pointer_traits<pointer>::difference_type difference_type;
274
Howard Hinnant848a5372010-09-22 16:48:34 +0000275 _LIBCPP_INLINE_VISIBILITY
Marshall Clow0c37cfd2013-08-05 21:23:28 +0000276 __list_iterator() _NOEXCEPT : __ptr_(nullptr)
Howard Hinnant920b56c2011-09-27 23:55:03 +0000277 {
278#if _LIBCPP_DEBUG_LEVEL >= 2
279 __get_db()->__insert_i(this);
280#endif
281 }
282
283#if _LIBCPP_DEBUG_LEVEL >= 2
284
Howard Hinnant27745452011-01-28 23:46:28 +0000285 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000286 __list_iterator(const __list_iterator& __p)
287 : __ptr_(__p.__ptr_)
288 {
289 __get_db()->__iterator_copy(this, &__p);
290 }
291
292 _LIBCPP_INLINE_VISIBILITY
293 ~__list_iterator()
294 {
295 __get_db()->__erase_i(this);
296 }
297
298 _LIBCPP_INLINE_VISIBILITY
299 __list_iterator& operator=(const __list_iterator& __p)
300 {
301 if (this != &__p)
302 {
303 __get_db()->__iterator_copy(this, &__p);
304 __ptr_ = __p.__ptr_;
305 }
306 return *this;
307 }
308
309#endif // _LIBCPP_DEBUG_LEVEL >= 2
310
311 _LIBCPP_INLINE_VISIBILITY
312 reference operator*() const
313 {
314#if _LIBCPP_DEBUG_LEVEL >= 2
315 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
316 "Attempted to dereference a non-dereferenceable list::iterator");
317#endif
318 return __ptr_->__value_;
319 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000320 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000321 pointer operator->() const
322 {
323#if _LIBCPP_DEBUG_LEVEL >= 2
324 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
325 "Attempted to dereference a non-dereferenceable list::iterator");
326#endif
327 return pointer_traits<pointer>::pointer_to(__ptr_->__value_);
328 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000329
Howard Hinnant848a5372010-09-22 16:48:34 +0000330 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000331 __list_iterator& operator++()
332 {
333#if _LIBCPP_DEBUG_LEVEL >= 2
334 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
335 "Attempted to increment non-incrementable list::iterator");
336#endif
337 __ptr_ = __ptr_->__next_;
338 return *this;
339 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000340 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000341 __list_iterator operator++(int) {__list_iterator __t(*this); ++(*this); return __t;}
342
Howard Hinnant848a5372010-09-22 16:48:34 +0000343 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000344 __list_iterator& operator--()
345 {
346#if _LIBCPP_DEBUG_LEVEL >= 2
347 _LIBCPP_ASSERT(__get_const_db()->__decrementable(this),
348 "Attempted to decrement non-decrementable list::iterator");
349#endif
350 __ptr_ = __ptr_->__prev_;
351 return *this;
352 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000353 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000354 __list_iterator operator--(int) {__list_iterator __t(*this); --(*this); return __t;}
355
Howard Hinnant848a5372010-09-22 16:48:34 +0000356 friend _LIBCPP_INLINE_VISIBILITY
357 bool operator==(const __list_iterator& __x, const __list_iterator& __y)
Howard Hinnant920b56c2011-09-27 23:55:03 +0000358 {
Howard Hinnant920b56c2011-09-27 23:55:03 +0000359 return __x.__ptr_ == __y.__ptr_;
360 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000361 friend _LIBCPP_INLINE_VISIBILITY
362 bool operator!=(const __list_iterator& __x, const __list_iterator& __y)
Howard Hinnant3e519522010-05-11 19:42:16 +0000363 {return !(__x == __y);}
364};
365
366template <class _Tp, class _VoidPtr>
Howard Hinnantf0544c22013-08-12 18:38:34 +0000367class _LIBCPP_TYPE_VIS_ONLY __list_const_iterator
Howard Hinnant3e519522010-05-11 19:42:16 +0000368{
369 typedef typename pointer_traits<_VoidPtr>::template
370#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000371 rebind<__list_node<_Tp, _VoidPtr> > __node_pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000372#else
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000373 rebind<__list_node<_Tp, _VoidPtr> >::other __node_pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000374#endif
375
376 __node_pointer __ptr_;
377
Howard Hinnant920b56c2011-09-27 23:55:03 +0000378#if _LIBCPP_DEBUG_LEVEL >= 2
379 _LIBCPP_INLINE_VISIBILITY
380 explicit __list_const_iterator(__node_pointer __p, const void* __c) _NOEXCEPT
381 : __ptr_(__p)
382 {
383 __get_db()->__insert_ic(this, __c);
384 }
385#else
Howard Hinnant848a5372010-09-22 16:48:34 +0000386 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000387 explicit __list_const_iterator(__node_pointer __p) _NOEXCEPT : __ptr_(__p) {}
Howard Hinnant920b56c2011-09-27 23:55:03 +0000388#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000389
390 template<class, class> friend class list;
391 template<class, class> friend class __list_imp;
392public:
393 typedef bidirectional_iterator_tag iterator_category;
394 typedef _Tp value_type;
395 typedef const value_type& reference;
396 typedef typename pointer_traits<_VoidPtr>::template
397#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
398 rebind<const value_type>
399#else
400 rebind<const value_type>::other
401#endif
402 pointer;
403 typedef typename pointer_traits<pointer>::difference_type difference_type;
404
Howard Hinnant848a5372010-09-22 16:48:34 +0000405 _LIBCPP_INLINE_VISIBILITY
Marshall Clow0c37cfd2013-08-05 21:23:28 +0000406 __list_const_iterator() _NOEXCEPT : __ptr_(nullptr)
Howard Hinnant920b56c2011-09-27 23:55:03 +0000407 {
408#if _LIBCPP_DEBUG_LEVEL >= 2
409 __get_db()->__insert_i(this);
410#endif
411 }
Howard Hinnant27745452011-01-28 23:46:28 +0000412 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf7509232013-04-05 17:58:52 +0000413 __list_const_iterator(const __list_iterator<_Tp, _VoidPtr>& __p) _NOEXCEPT
Howard Hinnant920b56c2011-09-27 23:55:03 +0000414 : __ptr_(__p.__ptr_)
415 {
416#if _LIBCPP_DEBUG_LEVEL >= 2
417 __get_db()->__iterator_copy(this, &__p);
418#endif
419 }
420
421#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant3e519522010-05-11 19:42:16 +0000422
Howard Hinnant848a5372010-09-22 16:48:34 +0000423 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000424 __list_const_iterator(const __list_const_iterator& __p)
425 : __ptr_(__p.__ptr_)
426 {
427 __get_db()->__iterator_copy(this, &__p);
428 }
429
430 _LIBCPP_INLINE_VISIBILITY
431 ~__list_const_iterator()
432 {
433 __get_db()->__erase_i(this);
434 }
435
436 _LIBCPP_INLINE_VISIBILITY
437 __list_const_iterator& operator=(const __list_const_iterator& __p)
438 {
439 if (this != &__p)
440 {
441 __get_db()->__iterator_copy(this, &__p);
442 __ptr_ = __p.__ptr_;
443 }
444 return *this;
445 }
446
447#endif // _LIBCPP_DEBUG_LEVEL >= 2
448 _LIBCPP_INLINE_VISIBILITY
449 reference operator*() const
450 {
451#if _LIBCPP_DEBUG_LEVEL >= 2
452 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
453 "Attempted to dereference a non-dereferenceable list::const_iterator");
454#endif
455 return __ptr_->__value_;
456 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000457 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000458 pointer operator->() const
459 {
460#if _LIBCPP_DEBUG_LEVEL >= 2
461 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
462 "Attempted to dereference a non-dereferenceable list::iterator");
463#endif
464 return pointer_traits<pointer>::pointer_to(__ptr_->__value_);
465 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000466
Howard Hinnant848a5372010-09-22 16:48:34 +0000467 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000468 __list_const_iterator& operator++()
469 {
470#if _LIBCPP_DEBUG_LEVEL >= 2
471 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
472 "Attempted to increment non-incrementable list::const_iterator");
473#endif
474 __ptr_ = __ptr_->__next_;
475 return *this;
476 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000477 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000478 __list_const_iterator operator++(int) {__list_const_iterator __t(*this); ++(*this); return __t;}
479
Howard Hinnant848a5372010-09-22 16:48:34 +0000480 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000481 __list_const_iterator& operator--()
482 {
483#if _LIBCPP_DEBUG_LEVEL >= 2
484 _LIBCPP_ASSERT(__get_const_db()->__decrementable(this),
485 "Attempted to decrement non-decrementable list::const_iterator");
486#endif
487 __ptr_ = __ptr_->__prev_;
488 return *this;
489 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000490 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000491 __list_const_iterator operator--(int) {__list_const_iterator __t(*this); --(*this); return __t;}
492
Howard Hinnant848a5372010-09-22 16:48:34 +0000493 friend _LIBCPP_INLINE_VISIBILITY
494 bool operator==(const __list_const_iterator& __x, const __list_const_iterator& __y)
Howard Hinnant920b56c2011-09-27 23:55:03 +0000495 {
Howard Hinnant920b56c2011-09-27 23:55:03 +0000496 return __x.__ptr_ == __y.__ptr_;
497 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000498 friend _LIBCPP_INLINE_VISIBILITY
499 bool operator!=(const __list_const_iterator& __x, const __list_const_iterator& __y)
Howard Hinnant3e519522010-05-11 19:42:16 +0000500 {return !(__x == __y);}
501};
502
503template <class _Tp, class _Alloc>
504class __list_imp
505{
506 __list_imp(const __list_imp&);
507 __list_imp& operator=(const __list_imp&);
508protected:
509 typedef _Tp value_type;
510 typedef _Alloc allocator_type;
511 typedef allocator_traits<allocator_type> __alloc_traits;
512 typedef typename __alloc_traits::size_type size_type;
513 typedef typename __alloc_traits::void_pointer __void_pointer;
514 typedef __list_iterator<value_type, __void_pointer> iterator;
515 typedef __list_const_iterator<value_type, __void_pointer> const_iterator;
516 typedef __list_node_base<value_type, __void_pointer> __node_base;
517 typedef __list_node<value_type, __void_pointer> __node;
Marshall Clow1f508012015-04-07 05:21:38 +0000518 typedef typename __rebind_alloc_helper<__alloc_traits, __node>::type __node_allocator;
Howard Hinnant3e519522010-05-11 19:42:16 +0000519 typedef allocator_traits<__node_allocator> __node_alloc_traits;
520 typedef typename __node_alloc_traits::pointer __node_pointer;
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000521 typedef typename __node_alloc_traits::pointer __node_const_pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000522 typedef typename __alloc_traits::pointer pointer;
523 typedef typename __alloc_traits::const_pointer const_pointer;
524 typedef typename __alloc_traits::difference_type difference_type;
525
Marshall Clow1f508012015-04-07 05:21:38 +0000526 typedef typename __rebind_alloc_helper<__alloc_traits, __node_base>::type __node_base_allocator;
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000527 typedef typename allocator_traits<__node_base_allocator>::pointer __node_base_pointer;
528
Howard Hinnant3e519522010-05-11 19:42:16 +0000529 __node_base __end_;
530 __compressed_pair<size_type, __node_allocator> __size_alloc_;
531
Howard Hinnant848a5372010-09-22 16:48:34 +0000532 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000533 size_type& __sz() _NOEXCEPT {return __size_alloc_.first();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000534 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000535 const size_type& __sz() const _NOEXCEPT
536 {return __size_alloc_.first();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000537 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000538 __node_allocator& __node_alloc() _NOEXCEPT
539 {return __size_alloc_.second();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000540 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000541 const __node_allocator& __node_alloc() const _NOEXCEPT
542 {return __size_alloc_.second();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000543
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000544 static void __unlink_nodes(__node_pointer __f, __node_pointer __l) _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +0000545
Howard Hinnant45900102011-06-03 17:30:28 +0000546 __list_imp()
547 _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value);
Howard Hinnant3e519522010-05-11 19:42:16 +0000548 __list_imp(const allocator_type& __a);
549 ~__list_imp();
Howard Hinnant45900102011-06-03 17:30:28 +0000550 void clear() _NOEXCEPT;
Howard Hinnant848a5372010-09-22 16:48:34 +0000551 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000552 bool empty() const _NOEXCEPT {return __sz() == 0;}
Howard Hinnant3e519522010-05-11 19:42:16 +0000553
Howard Hinnant848a5372010-09-22 16:48:34 +0000554 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000555 iterator begin() _NOEXCEPT
556 {
557#if _LIBCPP_DEBUG_LEVEL >= 2
558 return iterator(__end_.__next_, this);
559#else
560 return iterator(__end_.__next_);
561#endif
562 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000563 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000564 const_iterator begin() const _NOEXCEPT
Howard Hinnant920b56c2011-09-27 23:55:03 +0000565 {
566#if _LIBCPP_DEBUG_LEVEL >= 2
567 return const_iterator(__end_.__next_, this);
568#else
569 return const_iterator(__end_.__next_);
570#endif
571 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000572 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000573 iterator end() _NOEXCEPT
574 {
575#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000576 return iterator(static_cast<__node_pointer>(
577 pointer_traits<__node_base_pointer>::pointer_to(__end_)), this);
Howard Hinnant920b56c2011-09-27 23:55:03 +0000578#else
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000579 return iterator(static_cast<__node_pointer>(
580 pointer_traits<__node_base_pointer>::pointer_to(__end_)));
Howard Hinnant920b56c2011-09-27 23:55:03 +0000581#endif
582 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000583 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000584 const_iterator end() const _NOEXCEPT
Howard Hinnant920b56c2011-09-27 23:55:03 +0000585 {
586#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000587 return const_iterator(static_cast<__node_const_pointer>(
588 pointer_traits<__node_base_pointer>::pointer_to(const_cast<__node_base&>(__end_))), this);
Howard Hinnant920b56c2011-09-27 23:55:03 +0000589#else
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000590 return const_iterator(static_cast<__node_const_pointer>(
591 pointer_traits<__node_base_pointer>::pointer_to(const_cast<__node_base&>(__end_))));
Howard Hinnant920b56c2011-09-27 23:55:03 +0000592#endif
593 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000594
Howard Hinnant45900102011-06-03 17:30:28 +0000595 void swap(__list_imp& __c)
596 _NOEXCEPT_(!__node_alloc_traits::propagate_on_container_swap::value ||
597 __is_nothrow_swappable<__node_allocator>::value);
Howard Hinnant3e519522010-05-11 19:42:16 +0000598
Howard Hinnant848a5372010-09-22 16:48:34 +0000599 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000600 void __copy_assign_alloc(const __list_imp& __c)
601 {__copy_assign_alloc(__c, integral_constant<bool,
602 __node_alloc_traits::propagate_on_container_copy_assignment::value>());}
603
Howard Hinnant848a5372010-09-22 16:48:34 +0000604 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000605 void __move_assign_alloc(__list_imp& __c)
Howard Hinnant45900102011-06-03 17:30:28 +0000606 _NOEXCEPT_(
607 !__node_alloc_traits::propagate_on_container_move_assignment::value ||
608 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +0000609 {__move_assign_alloc(__c, integral_constant<bool,
610 __node_alloc_traits::propagate_on_container_move_assignment::value>());}
611
612private:
Howard Hinnant848a5372010-09-22 16:48:34 +0000613 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000614 static void __swap_alloc(__node_allocator& __x, __node_allocator& __y)
Howard Hinnant45900102011-06-03 17:30:28 +0000615 _NOEXCEPT_(!__node_alloc_traits::propagate_on_container_swap::value ||
616 __is_nothrow_swappable<__node_allocator>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +0000617 {__swap_alloc(__x, __y, integral_constant<bool,
618 __node_alloc_traits::propagate_on_container_swap::value>());}
Howard Hinnant848a5372010-09-22 16:48:34 +0000619 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000620 static void __swap_alloc(__node_allocator& __x, __node_allocator& __y, true_type)
Howard Hinnant45900102011-06-03 17:30:28 +0000621 _NOEXCEPT_(__is_nothrow_swappable<__node_allocator>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +0000622 {
Howard Hinnantce48a112011-06-30 21:18:19 +0000623 using _VSTD::swap;
Howard Hinnant3e519522010-05-11 19:42:16 +0000624 swap(__x, __y);
625 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000626 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000627 static void __swap_alloc(__node_allocator& __x, __node_allocator& __y, false_type)
Howard Hinnant45900102011-06-03 17:30:28 +0000628 _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000629 {}
630
Howard Hinnant848a5372010-09-22 16:48:34 +0000631 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000632 void __copy_assign_alloc(const __list_imp& __c, true_type)
633 {
634 if (__node_alloc() != __c.__node_alloc())
635 clear();
636 __node_alloc() = __c.__node_alloc();
637 }
638
Howard Hinnant848a5372010-09-22 16:48:34 +0000639 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000640 void __copy_assign_alloc(const __list_imp& __c, false_type)
641 {}
642
Howard Hinnant848a5372010-09-22 16:48:34 +0000643 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant86681392011-09-02 20:42:31 +0000644 void __move_assign_alloc(__list_imp& __c, true_type)
Howard Hinnant45900102011-06-03 17:30:28 +0000645 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +0000646 {
Howard Hinnantce48a112011-06-30 21:18:19 +0000647 __node_alloc() = _VSTD::move(__c.__node_alloc());
Howard Hinnant3e519522010-05-11 19:42:16 +0000648 }
649
Howard Hinnant848a5372010-09-22 16:48:34 +0000650 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant86681392011-09-02 20:42:31 +0000651 void __move_assign_alloc(__list_imp& __c, false_type)
Howard Hinnant45900102011-06-03 17:30:28 +0000652 _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000653 {}
654};
655
656// Unlink nodes [__f, __l]
657template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +0000658inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000659void
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000660__list_imp<_Tp, _Alloc>::__unlink_nodes(__node_pointer __f, __node_pointer __l)
Howard Hinnant45900102011-06-03 17:30:28 +0000661 _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000662{
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000663 __f->__prev_->__next_ = __l->__next_;
664 __l->__next_->__prev_ = __f->__prev_;
Howard Hinnant3e519522010-05-11 19:42:16 +0000665}
666
667template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +0000668inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000669__list_imp<_Tp, _Alloc>::__list_imp()
Howard Hinnant45900102011-06-03 17:30:28 +0000670 _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +0000671 : __size_alloc_(0)
672{
673}
674
675template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +0000676inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000677__list_imp<_Tp, _Alloc>::__list_imp(const allocator_type& __a)
678 : __size_alloc_(0, __node_allocator(__a))
679{
680}
681
682template <class _Tp, class _Alloc>
683__list_imp<_Tp, _Alloc>::~__list_imp()
684{
685 clear();
Howard Hinnant920b56c2011-09-27 23:55:03 +0000686#if _LIBCPP_DEBUG_LEVEL >= 2
687 __get_db()->__erase_c(this);
688#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000689}
690
691template <class _Tp, class _Alloc>
692void
Howard Hinnant45900102011-06-03 17:30:28 +0000693__list_imp<_Tp, _Alloc>::clear() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000694{
695 if (!empty())
696 {
697 __node_allocator& __na = __node_alloc();
Howard Hinnant920b56c2011-09-27 23:55:03 +0000698 __node_pointer __f = __end_.__next_;
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000699 __node_pointer __l = static_cast<__node_pointer>(
700 pointer_traits<__node_base_pointer>::pointer_to(__end_));
701 __unlink_nodes(__f, __l->__prev_);
Howard Hinnant3e519522010-05-11 19:42:16 +0000702 __sz() = 0;
703 while (__f != __l)
704 {
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000705 __node_pointer __n = __f;
Howard Hinnant920b56c2011-09-27 23:55:03 +0000706 __f = __f->__next_;
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000707 __node_alloc_traits::destroy(__na, _VSTD::addressof(__n->__value_));
708 __node_alloc_traits::deallocate(__na, __n, 1);
Howard Hinnant3e519522010-05-11 19:42:16 +0000709 }
Howard Hinnant920b56c2011-09-27 23:55:03 +0000710#if _LIBCPP_DEBUG_LEVEL >= 2
711 __c_node* __c = __get_db()->__find_c_and_lock(this);
712 for (__i_node** __p = __c->end_; __p != __c->beg_; )
713 {
714 --__p;
715 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
716 if (__i->__ptr_ != __l)
717 {
718 (*__p)->__c_ = nullptr;
719 if (--__c->end_ != __p)
720 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
721 }
722 }
723 __get_db()->unlock();
724#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000725 }
726}
727
728template <class _Tp, class _Alloc>
729void
730__list_imp<_Tp, _Alloc>::swap(__list_imp& __c)
Howard Hinnant45900102011-06-03 17:30:28 +0000731 _NOEXCEPT_(!__node_alloc_traits::propagate_on_container_swap::value ||
732 __is_nothrow_swappable<__node_allocator>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +0000733{
Howard Hinnant920b56c2011-09-27 23:55:03 +0000734 _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
735 this->__node_alloc() == __c.__node_alloc(),
736 "list::swap: Either propagate_on_container_swap must be true"
737 " or the allocators must compare equal");
Howard Hinnantce48a112011-06-30 21:18:19 +0000738 using _VSTD::swap;
Howard Hinnant3e519522010-05-11 19:42:16 +0000739 __swap_alloc(__node_alloc(), __c.__node_alloc());
740 swap(__sz(), __c.__sz());
741 swap(__end_, __c.__end_);
742 if (__sz() == 0)
Marshall Clow28d65da2014-08-05 01:34:12 +0000743 __end_.__next_ = __end_.__prev_ = __end_.__self();
Howard Hinnant3e519522010-05-11 19:42:16 +0000744 else
Marshall Clow28d65da2014-08-05 01:34:12 +0000745 __end_.__prev_->__next_ = __end_.__next_->__prev_ = __end_.__self();
Howard Hinnant3e519522010-05-11 19:42:16 +0000746 if (__c.__sz() == 0)
Marshall Clow28d65da2014-08-05 01:34:12 +0000747 __c.__end_.__next_ = __c.__end_.__prev_ = __c.__end_.__self();
Howard Hinnant3e519522010-05-11 19:42:16 +0000748 else
Marshall Clow28d65da2014-08-05 01:34:12 +0000749 __c.__end_.__prev_->__next_ = __c.__end_.__next_->__prev_ = __c.__end_.__self();
750
Howard Hinnant920b56c2011-09-27 23:55:03 +0000751#if _LIBCPP_DEBUG_LEVEL >= 2
752 __libcpp_db* __db = __get_db();
753 __c_node* __cn1 = __db->__find_c_and_lock(this);
754 __c_node* __cn2 = __db->__find_c(&__c);
755 std::swap(__cn1->beg_, __cn2->beg_);
756 std::swap(__cn1->end_, __cn2->end_);
757 std::swap(__cn1->cap_, __cn2->cap_);
758 for (__i_node** __p = __cn1->end_; __p != __cn1->beg_;)
759 {
760 --__p;
761 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000762 if (__i->__ptr_ == static_cast<__node_pointer>(
763 pointer_traits<__node_base_pointer>::pointer_to(__c.__end_)))
Howard Hinnant920b56c2011-09-27 23:55:03 +0000764 {
765 __cn2->__add(*__p);
766 if (--__cn1->end_ != __p)
767 memmove(__p, __p+1, (__cn1->end_ - __p)*sizeof(__i_node*));
768 }
769 else
770 (*__p)->__c_ = __cn1;
771 }
772 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
773 {
774 --__p;
775 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000776 if (__i->__ptr_ == static_cast<__node_pointer>(
777 pointer_traits<__node_base_pointer>::pointer_to(__end_)))
Howard Hinnant920b56c2011-09-27 23:55:03 +0000778 {
779 __cn1->__add(*__p);
780 if (--__cn2->end_ != __p)
781 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
782 }
783 else
784 (*__p)->__c_ = __cn2;
785 }
786 __db->unlock();
787#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000788}
789
Marshall Clowb5d34aa2015-02-18 17:24:08 +0000790template <class _Tp, class _Alloc /*= allocator<_Tp>*/>
Howard Hinnantf0544c22013-08-12 18:38:34 +0000791class _LIBCPP_TYPE_VIS_ONLY list
Howard Hinnant3e519522010-05-11 19:42:16 +0000792 : private __list_imp<_Tp, _Alloc>
793{
794 typedef __list_imp<_Tp, _Alloc> base;
795 typedef typename base::__node __node;
796 typedef typename base::__node_allocator __node_allocator;
797 typedef typename base::__node_pointer __node_pointer;
798 typedef typename base::__node_alloc_traits __node_alloc_traits;
Howard Hinnant866d4ef2013-06-25 16:08:47 +0000799 typedef typename base::__node_base __node_base;
800 typedef typename base::__node_base_pointer __node_base_pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000801
802public:
803 typedef _Tp value_type;
804 typedef _Alloc allocator_type;
805 static_assert((is_same<value_type, typename allocator_type::value_type>::value),
806 "Invalid allocator::value_type");
807 typedef value_type& reference;
808 typedef const value_type& const_reference;
809 typedef typename base::pointer pointer;
810 typedef typename base::const_pointer const_pointer;
811 typedef typename base::size_type size_type;
812 typedef typename base::difference_type difference_type;
813 typedef typename base::iterator iterator;
814 typedef typename base::const_iterator const_iterator;
Howard Hinnantce48a112011-06-30 21:18:19 +0000815 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
816 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnant3e519522010-05-11 19:42:16 +0000817
Howard Hinnant848a5372010-09-22 16:48:34 +0000818 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000819 list()
820 _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value)
Howard Hinnant920b56c2011-09-27 23:55:03 +0000821 {
822#if _LIBCPP_DEBUG_LEVEL >= 2
823 __get_db()->__insert_c(this);
824#endif
825 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000826 _LIBCPP_INLINE_VISIBILITY
Marshall Clowfb829762013-09-08 19:11:51 +0000827 explicit list(const allocator_type& __a) : base(__a)
Howard Hinnant920b56c2011-09-27 23:55:03 +0000828 {
829#if _LIBCPP_DEBUG_LEVEL >= 2
830 __get_db()->__insert_c(this);
831#endif
832 }
Marshall Clowfb829762013-09-08 19:11:51 +0000833 explicit list(size_type __n);
834#if _LIBCPP_STD_VER > 11
835 explicit list(size_type __n, const allocator_type& __a);
836#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000837 list(size_type __n, const value_type& __x);
838 list(size_type __n, const value_type& __x, const allocator_type& __a);
839 template <class _InpIter>
840 list(_InpIter __f, _InpIter __l,
841 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
842 template <class _InpIter>
843 list(_InpIter __f, _InpIter __l, const allocator_type& __a,
844 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
845
846 list(const list& __c);
847 list(const list& __c, const allocator_type& __a);
848 list& operator=(const list& __c);
Howard Hinnant54976f22011-08-12 21:56:02 +0000849#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant3e519522010-05-11 19:42:16 +0000850 list(initializer_list<value_type> __il);
851 list(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnant54976f22011-08-12 21:56:02 +0000852#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000853#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant45900102011-06-03 17:30:28 +0000854 list(list&& __c)
855 _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value);
Howard Hinnant3e519522010-05-11 19:42:16 +0000856 list(list&& __c, const allocator_type& __a);
Howard Hinnant45900102011-06-03 17:30:28 +0000857 list& operator=(list&& __c)
858 _NOEXCEPT_(
859 __node_alloc_traits::propagate_on_container_move_assignment::value &&
860 is_nothrow_move_assignable<__node_allocator>::value);
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000861#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54976f22011-08-12 21:56:02 +0000862#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant848a5372010-09-22 16:48:34 +0000863 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000864 list& operator=(initializer_list<value_type> __il)
865 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnant54976f22011-08-12 21:56:02 +0000866#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant3e519522010-05-11 19:42:16 +0000867
868 template <class _InpIter>
869 void assign(_InpIter __f, _InpIter __l,
870 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
871 void assign(size_type __n, const value_type& __x);
Howard Hinnant54976f22011-08-12 21:56:02 +0000872#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant848a5372010-09-22 16:48:34 +0000873 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000874 void assign(initializer_list<value_type> __il)
875 {assign(__il.begin(), __il.end());}
Howard Hinnant54976f22011-08-12 21:56:02 +0000876#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant3e519522010-05-11 19:42:16 +0000877
Howard Hinnant45900102011-06-03 17:30:28 +0000878 allocator_type get_allocator() const _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +0000879
Howard Hinnant848a5372010-09-22 16:48:34 +0000880 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000881 size_type size() const _NOEXCEPT {return base::__sz();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000882 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000883 bool empty() const _NOEXCEPT {return base::empty();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000884 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000885 size_type max_size() const _NOEXCEPT
886 {return numeric_limits<difference_type>::max();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000887
Howard Hinnant848a5372010-09-22 16:48:34 +0000888 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000889 iterator begin() _NOEXCEPT {return base::begin();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000890 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000891 const_iterator begin() const _NOEXCEPT {return base::begin();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000892 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000893 iterator end() _NOEXCEPT {return base::end();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000894 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000895 const_iterator end() const _NOEXCEPT {return base::end();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000896 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000897 const_iterator cbegin() const _NOEXCEPT {return base::begin();}
Howard Hinnant848a5372010-09-22 16:48:34 +0000898 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000899 const_iterator cend() const _NOEXCEPT {return base::end();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000900
Howard Hinnant848a5372010-09-22 16:48:34 +0000901 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000902 reverse_iterator rbegin() _NOEXCEPT
903 {return reverse_iterator(end());}
Howard Hinnant848a5372010-09-22 16:48:34 +0000904 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000905 const_reverse_iterator rbegin() const _NOEXCEPT
906 {return const_reverse_iterator(end());}
Howard Hinnant848a5372010-09-22 16:48:34 +0000907 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000908 reverse_iterator rend() _NOEXCEPT
909 {return reverse_iterator(begin());}
Howard Hinnant848a5372010-09-22 16:48:34 +0000910 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000911 const_reverse_iterator rend() const _NOEXCEPT
912 {return const_reverse_iterator(begin());}
Howard Hinnant848a5372010-09-22 16:48:34 +0000913 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000914 const_reverse_iterator crbegin() const _NOEXCEPT
915 {return const_reverse_iterator(end());}
Howard Hinnant848a5372010-09-22 16:48:34 +0000916 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000917 const_reverse_iterator crend() const _NOEXCEPT
918 {return const_reverse_iterator(begin());}
Howard Hinnant3e519522010-05-11 19:42:16 +0000919
Howard Hinnant848a5372010-09-22 16:48:34 +0000920 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000921 reference front()
922 {
923 _LIBCPP_ASSERT(!empty(), "list::front called on empty list");
924 return base::__end_.__next_->__value_;
925 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000926 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000927 const_reference front() const
928 {
929 _LIBCPP_ASSERT(!empty(), "list::front called on empty list");
930 return base::__end_.__next_->__value_;
931 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000932 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000933 reference back()
934 {
935 _LIBCPP_ASSERT(!empty(), "list::back called on empty list");
936 return base::__end_.__prev_->__value_;
937 }
Howard Hinnant848a5372010-09-22 16:48:34 +0000938 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant920b56c2011-09-27 23:55:03 +0000939 const_reference back() const
940 {
941 _LIBCPP_ASSERT(!empty(), "list::back called on empty list");
942 return base::__end_.__prev_->__value_;
943 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000944
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000945#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +0000946 void push_front(value_type&& __x);
947 void push_back(value_type&& __x);
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000948#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnant3e519522010-05-11 19:42:16 +0000949 template <class... _Args>
950 void emplace_front(_Args&&... __args);
951 template <class... _Args>
952 void emplace_back(_Args&&... __args);
953 template <class... _Args>
954 iterator emplace(const_iterator __p, _Args&&... __args);
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000955#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnant3e519522010-05-11 19:42:16 +0000956 iterator insert(const_iterator __p, value_type&& __x);
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000957#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +0000958
959 void push_front(const value_type& __x);
960 void push_back(const value_type& __x);
961
962 iterator insert(const_iterator __p, const value_type& __x);
963 iterator insert(const_iterator __p, size_type __n, const value_type& __x);
964 template <class _InpIter>
965 iterator insert(const_iterator __p, _InpIter __f, _InpIter __l,
966 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
Howard Hinnant54976f22011-08-12 21:56:02 +0000967#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant848a5372010-09-22 16:48:34 +0000968 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000969 iterator insert(const_iterator __p, initializer_list<value_type> __il)
970 {return insert(__p, __il.begin(), __il.end());}
Howard Hinnant54976f22011-08-12 21:56:02 +0000971#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant3e519522010-05-11 19:42:16 +0000972
Howard Hinnant848a5372010-09-22 16:48:34 +0000973 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000974 void swap(list& __c)
975 _NOEXCEPT_(!__node_alloc_traits::propagate_on_container_swap::value ||
976 __is_nothrow_swappable<__node_allocator>::value)
977 {base::swap(__c);}
Howard Hinnant848a5372010-09-22 16:48:34 +0000978 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant45900102011-06-03 17:30:28 +0000979 void clear() _NOEXCEPT {base::clear();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000980
981 void pop_front();
982 void pop_back();
983
984 iterator erase(const_iterator __p);
985 iterator erase(const_iterator __f, const_iterator __l);
986
987 void resize(size_type __n);
988 void resize(size_type __n, const value_type& __x);
989
990 void splice(const_iterator __p, list& __c);
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000991#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant848a5372010-09-22 16:48:34 +0000992 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000993 void splice(const_iterator __p, list&& __c) {splice(__p, __c);}
994#endif
995 void splice(const_iterator __p, list& __c, const_iterator __i);
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000996#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant848a5372010-09-22 16:48:34 +0000997 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000998 void splice(const_iterator __p, list&& __c, const_iterator __i)
999 {splice(__p, __c, __i);}
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001000#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +00001001 void splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l);
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001002#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant848a5372010-09-22 16:48:34 +00001003 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001004 void splice(const_iterator __p, list&& __c, const_iterator __f, const_iterator __l)
1005 {splice(__p, __c, __f, __l);}
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001006#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +00001007
1008 void remove(const value_type& __x);
1009 template <class _Pred> void remove_if(_Pred __pred);
1010 void unique();
1011 template <class _BinaryPred>
1012 void unique(_BinaryPred __binary_pred);
1013 void merge(list& __c);
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001014#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant848a5372010-09-22 16:48:34 +00001015 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001016 void merge(list&& __c) {merge(__c);}
1017#endif
1018 template <class _Comp>
1019 void merge(list& __c, _Comp __comp);
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001020#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +00001021 template <class _Comp>
Howard Hinnant848a5372010-09-22 16:48:34 +00001022 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001023 void merge(list&& __c, _Comp __comp) {merge(__c, __comp);}
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001024#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +00001025 void sort();
1026 template <class _Comp>
1027 void sort(_Comp __comp);
1028
Howard Hinnant45900102011-06-03 17:30:28 +00001029 void reverse() _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +00001030
Howard Hinnant920b56c2011-09-27 23:55:03 +00001031 bool __invariants() const;
1032
1033#if _LIBCPP_DEBUG_LEVEL >= 2
1034
1035 bool __dereferenceable(const const_iterator* __i) const;
1036 bool __decrementable(const const_iterator* __i) const;
1037 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1038 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1039
1040#endif // _LIBCPP_DEBUG_LEVEL >= 2
1041
Howard Hinnant3e519522010-05-11 19:42:16 +00001042private:
Marshall Clow28d65da2014-08-05 01:34:12 +00001043 static void __link_nodes (__node_pointer __p, __node_pointer __f, __node_pointer __l);
1044 void __link_nodes_at_front(__node_pointer __f, __node_pointer __l);
1045 void __link_nodes_at_back (__node_pointer __f, __node_pointer __l);
Howard Hinnant3e519522010-05-11 19:42:16 +00001046 iterator __iterator(size_type __n);
1047 template <class _Comp>
1048 static iterator __sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp);
1049
Howard Hinnant45900102011-06-03 17:30:28 +00001050 void __move_assign(list& __c, true_type)
1051 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value);
Howard Hinnant3e519522010-05-11 19:42:16 +00001052 void __move_assign(list& __c, false_type);
1053};
1054
1055// Link in nodes [__f, __l] just prior to __p
1056template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00001057inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001058void
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001059list<_Tp, _Alloc>::__link_nodes(__node_pointer __p, __node_pointer __f, __node_pointer __l)
Howard Hinnant3e519522010-05-11 19:42:16 +00001060{
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001061 __p->__prev_->__next_ = __f;
1062 __f->__prev_ = __p->__prev_;
1063 __p->__prev_ = __l;
1064 __l->__next_ = __p;
Howard Hinnant3e519522010-05-11 19:42:16 +00001065}
1066
Marshall Clow28d65da2014-08-05 01:34:12 +00001067// Link in nodes [__f, __l] at the front of the list
1068template <class _Tp, class _Alloc>
1069inline _LIBCPP_INLINE_VISIBILITY
1070void
1071list<_Tp, _Alloc>::__link_nodes_at_front(__node_pointer __f, __node_pointer __l)
1072{
Marshall Clowced70062014-08-08 15:35:52 +00001073 __f->__prev_ = base::__end_.__self();
1074 __l->__next_ = base::__end_.__next_;
1075 __l->__next_->__prev_ = __l;
1076 base::__end_.__next_ = __f;
Marshall Clow28d65da2014-08-05 01:34:12 +00001077}
1078
1079// Link in nodes [__f, __l] at the front of the list
1080template <class _Tp, class _Alloc>
1081inline _LIBCPP_INLINE_VISIBILITY
1082void
1083list<_Tp, _Alloc>::__link_nodes_at_back(__node_pointer __f, __node_pointer __l)
1084{
Marshall Clowced70062014-08-08 15:35:52 +00001085 __l->__next_ = base::__end_.__self();
1086 __f->__prev_ = base::__end_.__prev_;
1087 __f->__prev_->__next_ = __f;
1088 base::__end_.__prev_ = __l;
Marshall Clow28d65da2014-08-05 01:34:12 +00001089}
1090
1091
Howard Hinnant3e519522010-05-11 19:42:16 +00001092template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00001093inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001094typename list<_Tp, _Alloc>::iterator
1095list<_Tp, _Alloc>::__iterator(size_type __n)
1096{
Howard Hinnantce48a112011-06-30 21:18:19 +00001097 return __n <= base::__sz() / 2 ? _VSTD::next(begin(), __n)
1098 : _VSTD::prev(end(), base::__sz() - __n);
Howard Hinnant3e519522010-05-11 19:42:16 +00001099}
1100
1101template <class _Tp, class _Alloc>
1102list<_Tp, _Alloc>::list(size_type __n)
1103{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001104#if _LIBCPP_DEBUG_LEVEL >= 2
1105 __get_db()->__insert_c(this);
1106#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001107 for (; __n > 0; --__n)
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001108#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +00001109 emplace_back();
1110#else
1111 push_back(value_type());
1112#endif
1113}
1114
Marshall Clowfb829762013-09-08 19:11:51 +00001115#if _LIBCPP_STD_VER > 11
1116template <class _Tp, class _Alloc>
1117list<_Tp, _Alloc>::list(size_type __n, const allocator_type& __a) : base(__a)
1118{
1119#if _LIBCPP_DEBUG_LEVEL >= 2
1120 __get_db()->__insert_c(this);
1121#endif
1122 for (; __n > 0; --__n)
1123#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1124 emplace_back();
1125#else
1126 push_back(value_type());
1127#endif
1128}
1129#endif
1130
Howard Hinnant3e519522010-05-11 19:42:16 +00001131template <class _Tp, class _Alloc>
1132list<_Tp, _Alloc>::list(size_type __n, const value_type& __x)
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 (; __n > 0; --__n)
1138 push_back(__x);
1139}
1140
1141template <class _Tp, class _Alloc>
1142list<_Tp, _Alloc>::list(size_type __n, const value_type& __x, const allocator_type& __a)
1143 : base(__a)
1144{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001145#if _LIBCPP_DEBUG_LEVEL >= 2
1146 __get_db()->__insert_c(this);
1147#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001148 for (; __n > 0; --__n)
1149 push_back(__x);
1150}
1151
1152template <class _Tp, class _Alloc>
1153template <class _InpIter>
1154list<_Tp, _Alloc>::list(_InpIter __f, _InpIter __l,
1155 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1156{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001157#if _LIBCPP_DEBUG_LEVEL >= 2
1158 __get_db()->__insert_c(this);
1159#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001160 for (; __f != __l; ++__f)
1161 push_back(*__f);
1162}
1163
1164template <class _Tp, class _Alloc>
1165template <class _InpIter>
1166list<_Tp, _Alloc>::list(_InpIter __f, _InpIter __l, const allocator_type& __a,
1167 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1168 : base(__a)
1169{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001170#if _LIBCPP_DEBUG_LEVEL >= 2
1171 __get_db()->__insert_c(this);
1172#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001173 for (; __f != __l; ++__f)
1174 push_back(*__f);
1175}
1176
1177template <class _Tp, class _Alloc>
1178list<_Tp, _Alloc>::list(const list& __c)
1179 : base(allocator_type(
1180 __node_alloc_traits::select_on_container_copy_construction(
1181 __c.__node_alloc())))
1182{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001183#if _LIBCPP_DEBUG_LEVEL >= 2
1184 __get_db()->__insert_c(this);
1185#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001186 for (const_iterator __i = __c.begin(), __e = __c.end(); __i != __e; ++__i)
1187 push_back(*__i);
1188}
1189
1190template <class _Tp, class _Alloc>
1191list<_Tp, _Alloc>::list(const list& __c, const allocator_type& __a)
1192 : base(__a)
1193{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001194#if _LIBCPP_DEBUG_LEVEL >= 2
1195 __get_db()->__insert_c(this);
1196#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001197 for (const_iterator __i = __c.begin(), __e = __c.end(); __i != __e; ++__i)
1198 push_back(*__i);
1199}
1200
Howard Hinnant54976f22011-08-12 21:56:02 +00001201#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1202
Howard Hinnant3e519522010-05-11 19:42:16 +00001203template <class _Tp, class _Alloc>
1204list<_Tp, _Alloc>::list(initializer_list<value_type> __il, const allocator_type& __a)
1205 : base(__a)
1206{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001207#if _LIBCPP_DEBUG_LEVEL >= 2
1208 __get_db()->__insert_c(this);
1209#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001210 for (typename initializer_list<value_type>::const_iterator __i = __il.begin(),
1211 __e = __il.end(); __i != __e; ++__i)
1212 push_back(*__i);
1213}
1214
1215template <class _Tp, class _Alloc>
1216list<_Tp, _Alloc>::list(initializer_list<value_type> __il)
1217{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001218#if _LIBCPP_DEBUG_LEVEL >= 2
1219 __get_db()->__insert_c(this);
1220#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001221 for (typename initializer_list<value_type>::const_iterator __i = __il.begin(),
1222 __e = __il.end(); __i != __e; ++__i)
1223 push_back(*__i);
1224}
1225
Howard Hinnant54976f22011-08-12 21:56:02 +00001226#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1227
Howard Hinnant3e519522010-05-11 19:42:16 +00001228template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00001229inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001230list<_Tp, _Alloc>&
1231list<_Tp, _Alloc>::operator=(const list& __c)
1232{
1233 if (this != &__c)
1234 {
1235 base::__copy_assign_alloc(__c);
1236 assign(__c.begin(), __c.end());
1237 }
1238 return *this;
1239}
1240
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001241#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +00001242
1243template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00001244inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001245list<_Tp, _Alloc>::list(list&& __c)
Howard Hinnant45900102011-06-03 17:30:28 +00001246 _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value)
Howard Hinnantce48a112011-06-30 21:18:19 +00001247 : base(allocator_type(_VSTD::move(__c.__node_alloc())))
Howard Hinnant3e519522010-05-11 19:42:16 +00001248{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001249#if _LIBCPP_DEBUG_LEVEL >= 2
1250 __get_db()->__insert_c(this);
1251#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001252 splice(end(), __c);
1253}
1254
1255template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00001256inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001257list<_Tp, _Alloc>::list(list&& __c, const allocator_type& __a)
1258 : base(__a)
1259{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001260#if _LIBCPP_DEBUG_LEVEL >= 2
1261 __get_db()->__insert_c(this);
1262#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001263 if (__a == __c.get_allocator())
1264 splice(end(), __c);
1265 else
1266 {
Howard Hinnantc003db12011-11-29 18:15:50 +00001267 typedef move_iterator<iterator> _Ip;
1268 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnant3e519522010-05-11 19:42:16 +00001269 }
1270}
1271
1272template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00001273inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001274list<_Tp, _Alloc>&
1275list<_Tp, _Alloc>::operator=(list&& __c)
Howard Hinnant45900102011-06-03 17:30:28 +00001276 _NOEXCEPT_(
1277 __node_alloc_traits::propagate_on_container_move_assignment::value &&
1278 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +00001279{
1280 __move_assign(__c, integral_constant<bool,
1281 __node_alloc_traits::propagate_on_container_move_assignment::value>());
1282 return *this;
1283}
1284
1285template <class _Tp, class _Alloc>
1286void
1287list<_Tp, _Alloc>::__move_assign(list& __c, false_type)
1288{
1289 if (base::__node_alloc() != __c.__node_alloc())
1290 {
Howard Hinnantc003db12011-11-29 18:15:50 +00001291 typedef move_iterator<iterator> _Ip;
1292 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnant3e519522010-05-11 19:42:16 +00001293 }
1294 else
1295 __move_assign(__c, true_type());
1296}
1297
1298template <class _Tp, class _Alloc>
1299void
1300list<_Tp, _Alloc>::__move_assign(list& __c, true_type)
Howard Hinnant45900102011-06-03 17:30:28 +00001301 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +00001302{
1303 clear();
1304 base::__move_assign_alloc(__c);
1305 splice(end(), __c);
1306}
1307
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001308#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +00001309
1310template <class _Tp, class _Alloc>
1311template <class _InpIter>
1312void
1313list<_Tp, _Alloc>::assign(_InpIter __f, _InpIter __l,
1314 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1315{
1316 iterator __i = begin();
1317 iterator __e = end();
1318 for (; __f != __l && __i != __e; ++__f, ++__i)
1319 *__i = *__f;
1320 if (__i == __e)
1321 insert(__e, __f, __l);
1322 else
1323 erase(__i, __e);
1324}
1325
1326template <class _Tp, class _Alloc>
1327void
1328list<_Tp, _Alloc>::assign(size_type __n, const value_type& __x)
1329{
1330 iterator __i = begin();
1331 iterator __e = end();
1332 for (; __n > 0 && __i != __e; --__n, ++__i)
1333 *__i = __x;
1334 if (__i == __e)
1335 insert(__e, __n, __x);
1336 else
1337 erase(__i, __e);
1338}
1339
1340template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00001341inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001342_Alloc
Howard Hinnant45900102011-06-03 17:30:28 +00001343list<_Tp, _Alloc>::get_allocator() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001344{
1345 return allocator_type(base::__node_alloc());
1346}
1347
1348template <class _Tp, class _Alloc>
1349typename list<_Tp, _Alloc>::iterator
1350list<_Tp, _Alloc>::insert(const_iterator __p, const value_type& __x)
1351{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001352#if _LIBCPP_DEBUG_LEVEL >= 2
1353 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1354 "list::insert(iterator, x) called with an iterator not"
1355 " referring to this list");
1356#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001357 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001358 typedef __allocator_destructor<__node_allocator> _Dp;
1359 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant3e519522010-05-11 19:42:16 +00001360 __hold->__prev_ = 0;
Howard Hinnantce48a112011-06-30 21:18:19 +00001361 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001362 __link_nodes(__p.__ptr_, __hold.get(), __hold.get());
Howard Hinnant3e519522010-05-11 19:42:16 +00001363 ++base::__sz();
Howard Hinnantb0e4c9d2013-04-05 00:18:49 +00001364#if _LIBCPP_DEBUG_LEVEL >= 2
1365 return iterator(__hold.release(), this);
1366#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001367 return iterator(__hold.release());
Howard Hinnantb0e4c9d2013-04-05 00:18:49 +00001368#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001369}
1370
1371template <class _Tp, class _Alloc>
1372typename list<_Tp, _Alloc>::iterator
1373list<_Tp, _Alloc>::insert(const_iterator __p, size_type __n, const value_type& __x)
1374{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001375#if _LIBCPP_DEBUG_LEVEL >= 2
1376 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1377 "list::insert(iterator, n, x) called with an iterator not"
1378 " referring to this list");
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001379 iterator __r(__p.__ptr_, this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001380#else
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001381 iterator __r(__p.__ptr_);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001382#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001383 if (__n > 0)
1384 {
1385 size_type __ds = 0;
1386 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001387 typedef __allocator_destructor<__node_allocator> _Dp;
1388 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant3e519522010-05-11 19:42:16 +00001389 __hold->__prev_ = 0;
Howard Hinnantce48a112011-06-30 21:18:19 +00001390 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnant3e519522010-05-11 19:42:16 +00001391 ++__ds;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001392#if _LIBCPP_DEBUG_LEVEL >= 2
1393 __r = iterator(__hold.get(), this);
1394#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001395 __r = iterator(__hold.get());
Howard Hinnant920b56c2011-09-27 23:55:03 +00001396#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001397 __hold.release();
1398 iterator __e = __r;
1399#ifndef _LIBCPP_NO_EXCEPTIONS
1400 try
1401 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001402#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001403 for (--__n; __n != 0; --__n, ++__e, ++__ds)
1404 {
1405 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001406 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnant3e519522010-05-11 19:42:16 +00001407 __e.__ptr_->__next_ = __hold.get();
1408 __hold->__prev_ = __e.__ptr_;
1409 __hold.release();
1410 }
1411#ifndef _LIBCPP_NO_EXCEPTIONS
1412 }
1413 catch (...)
1414 {
1415 while (true)
1416 {
Howard Hinnantce48a112011-06-30 21:18:19 +00001417 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Howard Hinnant3e519522010-05-11 19:42:16 +00001418 __node_pointer __prev = __e.__ptr_->__prev_;
1419 __node_alloc_traits::deallocate(__na, __e.__ptr_, 1);
1420 if (__prev == 0)
1421 break;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001422#if _LIBCPP_DEBUG_LEVEL >= 2
1423 __e = iterator(__prev, this);
1424#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001425 __e = iterator(__prev);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001426#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001427 }
1428 throw;
1429 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001430#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001431 __link_nodes(__p.__ptr_, __r.__ptr_, __e.__ptr_);
Howard Hinnant3e519522010-05-11 19:42:16 +00001432 base::__sz() += __ds;
1433 }
1434 return __r;
1435}
1436
1437template <class _Tp, class _Alloc>
1438template <class _InpIter>
1439typename list<_Tp, _Alloc>::iterator
1440list<_Tp, _Alloc>::insert(const_iterator __p, _InpIter __f, _InpIter __l,
1441 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1442{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001443#if _LIBCPP_DEBUG_LEVEL >= 2
1444 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1445 "list::insert(iterator, range) called with an iterator not"
1446 " referring to this list");
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001447 iterator __r(__p.__ptr_, this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001448#else
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001449 iterator __r(__p.__ptr_);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001450#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001451 if (__f != __l)
1452 {
1453 size_type __ds = 0;
1454 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001455 typedef __allocator_destructor<__node_allocator> _Dp;
1456 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant3e519522010-05-11 19:42:16 +00001457 __hold->__prev_ = 0;
Howard Hinnantce48a112011-06-30 21:18:19 +00001458 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), *__f);
Howard Hinnant3e519522010-05-11 19:42:16 +00001459 ++__ds;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001460#if _LIBCPP_DEBUG_LEVEL >= 2
1461 __r = iterator(__hold.get(), this);
1462#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001463 __r = iterator(__hold.get());
Howard Hinnant920b56c2011-09-27 23:55:03 +00001464#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001465 __hold.release();
1466 iterator __e = __r;
1467#ifndef _LIBCPP_NO_EXCEPTIONS
1468 try
1469 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001470#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiselier61bff612015-03-19 03:20:02 +00001471 for (++__f; __f != __l; ++__f, (void) ++__e, (void) ++__ds)
Howard Hinnant3e519522010-05-11 19:42:16 +00001472 {
1473 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001474 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), *__f);
Howard Hinnant3e519522010-05-11 19:42:16 +00001475 __e.__ptr_->__next_ = __hold.get();
1476 __hold->__prev_ = __e.__ptr_;
1477 __hold.release();
1478 }
1479#ifndef _LIBCPP_NO_EXCEPTIONS
1480 }
1481 catch (...)
1482 {
1483 while (true)
1484 {
Howard Hinnantce48a112011-06-30 21:18:19 +00001485 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Howard Hinnant3e519522010-05-11 19:42:16 +00001486 __node_pointer __prev = __e.__ptr_->__prev_;
1487 __node_alloc_traits::deallocate(__na, __e.__ptr_, 1);
1488 if (__prev == 0)
1489 break;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001490#if _LIBCPP_DEBUG_LEVEL >= 2
1491 __e = iterator(__prev, this);
1492#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001493 __e = iterator(__prev);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001494#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001495 }
1496 throw;
1497 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001498#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001499 __link_nodes(__p.__ptr_, __r.__ptr_, __e.__ptr_);
Howard Hinnant3e519522010-05-11 19:42:16 +00001500 base::__sz() += __ds;
1501 }
1502 return __r;
1503}
1504
1505template <class _Tp, class _Alloc>
1506void
1507list<_Tp, _Alloc>::push_front(const value_type& __x)
1508{
1509 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001510 typedef __allocator_destructor<__node_allocator> _Dp;
1511 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001512 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Marshall Clow28d65da2014-08-05 01:34:12 +00001513 __link_nodes_at_front(__hold.get(), __hold.get());
Howard Hinnant3e519522010-05-11 19:42:16 +00001514 ++base::__sz();
1515 __hold.release();
1516}
1517
1518template <class _Tp, class _Alloc>
1519void
1520list<_Tp, _Alloc>::push_back(const value_type& __x)
1521{
1522 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001523 typedef __allocator_destructor<__node_allocator> _Dp;
1524 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001525 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Marshall Clow28d65da2014-08-05 01:34:12 +00001526 __link_nodes_at_back(__hold.get(), __hold.get());
Howard Hinnant3e519522010-05-11 19:42:16 +00001527 ++base::__sz();
1528 __hold.release();
1529}
1530
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001531#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +00001532
1533template <class _Tp, class _Alloc>
1534void
1535list<_Tp, _Alloc>::push_front(value_type&& __x)
1536{
1537 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001538 typedef __allocator_destructor<__node_allocator> _Dp;
1539 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001540 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
Marshall Clow28d65da2014-08-05 01:34:12 +00001541 __link_nodes_at_front(__hold.get(), __hold.get());
Howard Hinnant3e519522010-05-11 19:42:16 +00001542 ++base::__sz();
1543 __hold.release();
1544}
1545
1546template <class _Tp, class _Alloc>
1547void
1548list<_Tp, _Alloc>::push_back(value_type&& __x)
1549{
1550 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001551 typedef __allocator_destructor<__node_allocator> _Dp;
1552 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001553 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
Marshall Clow28d65da2014-08-05 01:34:12 +00001554 __link_nodes_at_back(__hold.get(), __hold.get());
Howard Hinnant3e519522010-05-11 19:42:16 +00001555 ++base::__sz();
1556 __hold.release();
1557}
1558
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001559#ifndef _LIBCPP_HAS_NO_VARIADICS
1560
Howard Hinnant3e519522010-05-11 19:42:16 +00001561template <class _Tp, class _Alloc>
1562template <class... _Args>
1563void
1564list<_Tp, _Alloc>::emplace_front(_Args&&... __args)
1565{
1566 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001567 typedef __allocator_destructor<__node_allocator> _Dp;
1568 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001569 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
Marshall Clow28d65da2014-08-05 01:34:12 +00001570 __link_nodes_at_front(__hold.get(), __hold.get());
Howard Hinnant3e519522010-05-11 19:42:16 +00001571 ++base::__sz();
1572 __hold.release();
1573}
1574
1575template <class _Tp, class _Alloc>
1576template <class... _Args>
1577void
1578list<_Tp, _Alloc>::emplace_back(_Args&&... __args)
1579{
1580 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001581 typedef __allocator_destructor<__node_allocator> _Dp;
1582 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001583 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
Marshall Clow28d65da2014-08-05 01:34:12 +00001584 __link_nodes_at_back(__hold.get(), __hold.get());
Howard Hinnant3e519522010-05-11 19:42:16 +00001585 ++base::__sz();
1586 __hold.release();
1587}
1588
1589template <class _Tp, class _Alloc>
1590template <class... _Args>
1591typename list<_Tp, _Alloc>::iterator
1592list<_Tp, _Alloc>::emplace(const_iterator __p, _Args&&... __args)
1593{
Howard Hinnantb0e4c9d2013-04-05 00:18:49 +00001594#if _LIBCPP_DEBUG_LEVEL >= 2
1595 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1596 "list::emplace(iterator, args...) called with an iterator not"
1597 " referring to this list");
1598#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001599 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001600 typedef __allocator_destructor<__node_allocator> _Dp;
1601 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant3e519522010-05-11 19:42:16 +00001602 __hold->__prev_ = 0;
Howard Hinnantce48a112011-06-30 21:18:19 +00001603 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001604 __link_nodes(__p.__ptr_, __hold.get(), __hold.get());
Howard Hinnant3e519522010-05-11 19:42:16 +00001605 ++base::__sz();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001606#if _LIBCPP_DEBUG_LEVEL >= 2
1607 return iterator(__hold.release(), this);
1608#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001609 return iterator(__hold.release());
Howard Hinnant920b56c2011-09-27 23:55:03 +00001610#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001611}
1612
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001613#endif // _LIBCPP_HAS_NO_VARIADICS
1614
Howard Hinnant3e519522010-05-11 19:42:16 +00001615template <class _Tp, class _Alloc>
1616typename list<_Tp, _Alloc>::iterator
1617list<_Tp, _Alloc>::insert(const_iterator __p, value_type&& __x)
1618{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001619#if _LIBCPP_DEBUG_LEVEL >= 2
1620 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1621 "list::insert(iterator, x) called with an iterator not"
1622 " referring to this list");
1623#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001624 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001625 typedef __allocator_destructor<__node_allocator> _Dp;
1626 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant3e519522010-05-11 19:42:16 +00001627 __hold->__prev_ = 0;
Howard Hinnantce48a112011-06-30 21:18:19 +00001628 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001629 __link_nodes(__p.__ptr_, __hold.get(), __hold.get());
Howard Hinnant3e519522010-05-11 19:42:16 +00001630 ++base::__sz();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001631#if _LIBCPP_DEBUG_LEVEL >= 2
1632 return iterator(__hold.release(), this);
1633#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001634 return iterator(__hold.release());
Howard Hinnant920b56c2011-09-27 23:55:03 +00001635#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001636}
1637
Howard Hinnant7609c9b2010-09-04 23:28:19 +00001638#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +00001639
1640template <class _Tp, class _Alloc>
1641void
1642list<_Tp, _Alloc>::pop_front()
1643{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001644 _LIBCPP_ASSERT(!empty(), "list::pop_front() called with empty list");
Howard Hinnant3e519522010-05-11 19:42:16 +00001645 __node_allocator& __na = base::__node_alloc();
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001646 __node_pointer __n = base::__end_.__next_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001647 base::__unlink_nodes(__n, __n);
1648 --base::__sz();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001649#if _LIBCPP_DEBUG_LEVEL >= 2
1650 __c_node* __c = __get_db()->__find_c_and_lock(this);
1651 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1652 {
1653 --__p;
1654 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001655 if (__i->__ptr_ == __n)
Howard Hinnant920b56c2011-09-27 23:55:03 +00001656 {
1657 (*__p)->__c_ = nullptr;
1658 if (--__c->end_ != __p)
1659 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1660 }
1661 }
1662 __get_db()->unlock();
1663#endif
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001664 __node_alloc_traits::destroy(__na, _VSTD::addressof(__n->__value_));
1665 __node_alloc_traits::deallocate(__na, __n, 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001666}
1667
1668template <class _Tp, class _Alloc>
1669void
1670list<_Tp, _Alloc>::pop_back()
1671{
Dmitri Gribenkoc3f9c802013-06-24 06:15:57 +00001672 _LIBCPP_ASSERT(!empty(), "list::pop_back() called with empty list");
Howard Hinnant3e519522010-05-11 19:42:16 +00001673 __node_allocator& __na = base::__node_alloc();
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001674 __node_pointer __n = base::__end_.__prev_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001675 base::__unlink_nodes(__n, __n);
1676 --base::__sz();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001677#if _LIBCPP_DEBUG_LEVEL >= 2
1678 __c_node* __c = __get_db()->__find_c_and_lock(this);
1679 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1680 {
1681 --__p;
1682 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001683 if (__i->__ptr_ == __n)
Howard Hinnant920b56c2011-09-27 23:55:03 +00001684 {
1685 (*__p)->__c_ = nullptr;
1686 if (--__c->end_ != __p)
1687 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1688 }
1689 }
1690 __get_db()->unlock();
1691#endif
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001692 __node_alloc_traits::destroy(__na, _VSTD::addressof(__n->__value_));
1693 __node_alloc_traits::deallocate(__na, __n, 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001694}
1695
1696template <class _Tp, class _Alloc>
1697typename list<_Tp, _Alloc>::iterator
1698list<_Tp, _Alloc>::erase(const_iterator __p)
1699{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001700#if _LIBCPP_DEBUG_LEVEL >= 2
1701 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1702 "list::erase(iterator) called with an iterator not"
1703 " referring to this list");
1704#endif
Howard Hinnantb0e4c9d2013-04-05 00:18:49 +00001705 _LIBCPP_ASSERT(__p != end(),
1706 "list::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnant3e519522010-05-11 19:42:16 +00001707 __node_allocator& __na = base::__node_alloc();
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001708 __node_pointer __n = __p.__ptr_;
1709 __node_pointer __r = __n->__next_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001710 base::__unlink_nodes(__n, __n);
1711 --base::__sz();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001712#if _LIBCPP_DEBUG_LEVEL >= 2
1713 __c_node* __c = __get_db()->__find_c_and_lock(this);
1714 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1715 {
1716 --__p;
1717 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001718 if (__i->__ptr_ == __n)
Howard Hinnant920b56c2011-09-27 23:55:03 +00001719 {
1720 (*__p)->__c_ = nullptr;
1721 if (--__c->end_ != __p)
1722 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1723 }
1724 }
1725 __get_db()->unlock();
1726#endif
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001727 __node_alloc_traits::destroy(__na, _VSTD::addressof(__n->__value_));
1728 __node_alloc_traits::deallocate(__na, __n, 1);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001729#if _LIBCPP_DEBUG_LEVEL >= 2
1730 return iterator(__r, this);
1731#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001732 return iterator(__r);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001733#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001734}
1735
1736template <class _Tp, class _Alloc>
1737typename list<_Tp, _Alloc>::iterator
1738list<_Tp, _Alloc>::erase(const_iterator __f, const_iterator __l)
1739{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001740#if _LIBCPP_DEBUG_LEVEL >= 2
1741 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__f) == this,
1742 "list::erase(iterator, iterator) called with an iterator not"
1743 " referring to this list");
1744#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001745 if (__f != __l)
1746 {
1747 __node_allocator& __na = base::__node_alloc();
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001748 base::__unlink_nodes(__f.__ptr_, __l.__ptr_->__prev_);
Howard Hinnant3e519522010-05-11 19:42:16 +00001749 while (__f != __l)
1750 {
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001751 __node_pointer __n = __f.__ptr_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001752 ++__f;
1753 --base::__sz();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001754#if _LIBCPP_DEBUG_LEVEL >= 2
1755 __c_node* __c = __get_db()->__find_c_and_lock(this);
1756 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1757 {
1758 --__p;
1759 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001760 if (__i->__ptr_ == __n)
Howard Hinnant920b56c2011-09-27 23:55:03 +00001761 {
1762 (*__p)->__c_ = nullptr;
1763 if (--__c->end_ != __p)
1764 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1765 }
1766 }
1767 __get_db()->unlock();
1768#endif
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001769 __node_alloc_traits::destroy(__na, _VSTD::addressof(__n->__value_));
1770 __node_alloc_traits::deallocate(__na, __n, 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001771 }
1772 }
Howard Hinnant920b56c2011-09-27 23:55:03 +00001773#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001774 return iterator(__l.__ptr_, this);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001775#else
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001776 return iterator(__l.__ptr_);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001777#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001778}
1779
1780template <class _Tp, class _Alloc>
1781void
1782list<_Tp, _Alloc>::resize(size_type __n)
1783{
1784 if (__n < base::__sz())
1785 erase(__iterator(__n), end());
1786 else if (__n > base::__sz())
1787 {
1788 __n -= base::__sz();
1789 size_type __ds = 0;
1790 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001791 typedef __allocator_destructor<__node_allocator> _Dp;
1792 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant3e519522010-05-11 19:42:16 +00001793 __hold->__prev_ = 0;
Howard Hinnantce48a112011-06-30 21:18:19 +00001794 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_));
Howard Hinnant3e519522010-05-11 19:42:16 +00001795 ++__ds;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001796#if _LIBCPP_DEBUG_LEVEL >= 2
1797 iterator __r = iterator(__hold.release(), this);
1798#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001799 iterator __r = iterator(__hold.release());
Howard Hinnant920b56c2011-09-27 23:55:03 +00001800#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001801 iterator __e = __r;
1802#ifndef _LIBCPP_NO_EXCEPTIONS
1803 try
1804 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001805#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001806 for (--__n; __n != 0; --__n, ++__e, ++__ds)
1807 {
1808 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001809 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_));
Howard Hinnant3e519522010-05-11 19:42:16 +00001810 __e.__ptr_->__next_ = __hold.get();
1811 __hold->__prev_ = __e.__ptr_;
1812 __hold.release();
1813 }
1814#ifndef _LIBCPP_NO_EXCEPTIONS
1815 }
1816 catch (...)
1817 {
1818 while (true)
1819 {
Howard Hinnantce48a112011-06-30 21:18:19 +00001820 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Howard Hinnant3e519522010-05-11 19:42:16 +00001821 __node_pointer __prev = __e.__ptr_->__prev_;
1822 __node_alloc_traits::deallocate(__na, __e.__ptr_, 1);
1823 if (__prev == 0)
1824 break;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001825#if _LIBCPP_DEBUG_LEVEL >= 2
1826 __e = iterator(__prev, this);
1827#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001828 __e = iterator(__prev);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001829#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001830 }
1831 throw;
1832 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001833#endif // _LIBCPP_NO_EXCEPTIONS
Marshall Clow28d65da2014-08-05 01:34:12 +00001834 __link_nodes_at_back(__r.__ptr_, __e.__ptr_);
Howard Hinnant3e519522010-05-11 19:42:16 +00001835 base::__sz() += __ds;
1836 }
1837}
1838
1839template <class _Tp, class _Alloc>
1840void
1841list<_Tp, _Alloc>::resize(size_type __n, const value_type& __x)
1842{
1843 if (__n < base::__sz())
1844 erase(__iterator(__n), end());
1845 else if (__n > base::__sz())
1846 {
1847 __n -= base::__sz();
1848 size_type __ds = 0;
1849 __node_allocator& __na = base::__node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00001850 typedef __allocator_destructor<__node_allocator> _Dp;
1851 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant3e519522010-05-11 19:42:16 +00001852 __hold->__prev_ = 0;
Howard Hinnantce48a112011-06-30 21:18:19 +00001853 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnant3e519522010-05-11 19:42:16 +00001854 ++__ds;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001855#if _LIBCPP_DEBUG_LEVEL >= 2
1856 iterator __r = iterator(__hold.release(), this);
1857#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001858 iterator __r = iterator(__hold.release());
Howard Hinnant920b56c2011-09-27 23:55:03 +00001859#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001860 iterator __e = __r;
1861#ifndef _LIBCPP_NO_EXCEPTIONS
1862 try
1863 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001864#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001865 for (--__n; __n != 0; --__n, ++__e, ++__ds)
1866 {
1867 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnantce48a112011-06-30 21:18:19 +00001868 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnant3e519522010-05-11 19:42:16 +00001869 __e.__ptr_->__next_ = __hold.get();
1870 __hold->__prev_ = __e.__ptr_;
1871 __hold.release();
1872 }
1873#ifndef _LIBCPP_NO_EXCEPTIONS
1874 }
1875 catch (...)
1876 {
1877 while (true)
1878 {
Howard Hinnantce48a112011-06-30 21:18:19 +00001879 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Howard Hinnant3e519522010-05-11 19:42:16 +00001880 __node_pointer __prev = __e.__ptr_->__prev_;
1881 __node_alloc_traits::deallocate(__na, __e.__ptr_, 1);
1882 if (__prev == 0)
1883 break;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001884#if _LIBCPP_DEBUG_LEVEL >= 2
1885 __e = iterator(__prev, this);
1886#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001887 __e = iterator(__prev);
Howard Hinnant920b56c2011-09-27 23:55:03 +00001888#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001889 }
1890 throw;
1891 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001892#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001893 __link_nodes(static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::
1894 pointer_to(base::__end_)), __r.__ptr_, __e.__ptr_);
Howard Hinnant3e519522010-05-11 19:42:16 +00001895 base::__sz() += __ds;
Howard Hinnantb3371f62010-08-22 00:02:43 +00001896 }
Howard Hinnant3e519522010-05-11 19:42:16 +00001897}
1898
1899template <class _Tp, class _Alloc>
1900void
1901list<_Tp, _Alloc>::splice(const_iterator __p, list& __c)
1902{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001903 _LIBCPP_ASSERT(this != &__c,
1904 "list::splice(iterator, list) called with this == &list");
1905#if _LIBCPP_DEBUG_LEVEL >= 2
1906 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1907 "list::splice(iterator, list) called with an iterator not"
1908 " referring to this list");
1909#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001910 if (!__c.empty())
1911 {
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001912 __node_pointer __f = __c.__end_.__next_;
1913 __node_pointer __l = __c.__end_.__prev_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001914 base::__unlink_nodes(__f, __l);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001915 __link_nodes(__p.__ptr_, __f, __l);
Howard Hinnant3e519522010-05-11 19:42:16 +00001916 base::__sz() += __c.__sz();
1917 __c.__sz() = 0;
Howard Hinnant920b56c2011-09-27 23:55:03 +00001918#if _LIBCPP_DEBUG_LEVEL >= 2
1919 __libcpp_db* __db = __get_db();
1920 __c_node* __cn1 = __db->__find_c_and_lock(this);
1921 __c_node* __cn2 = __db->__find_c(&__c);
1922 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
1923 {
1924 --__p;
1925 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001926 if (__i->__ptr_ != static_cast<__node_pointer>(
1927 pointer_traits<__node_base_pointer>::pointer_to(__c.__end_)))
Howard Hinnant920b56c2011-09-27 23:55:03 +00001928 {
1929 __cn1->__add(*__p);
1930 (*__p)->__c_ = __cn1;
1931 if (--__cn2->end_ != __p)
1932 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
1933 }
1934 }
1935 __db->unlock();
1936#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001937 }
1938}
1939
1940template <class _Tp, class _Alloc>
1941void
1942list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __i)
1943{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001944#if _LIBCPP_DEBUG_LEVEL >= 2
1945 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1946 "list::splice(iterator, list, iterator) called with first iterator not"
1947 " referring to this list");
1948 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__i) == &__c,
1949 "list::splice(iterator, list, iterator) called with second iterator not"
1950 " referring to list argument");
1951 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(&__i),
1952 "list::splice(iterator, list, iterator) called with second iterator not"
1953 " derefereceable");
1954#endif
1955 if (__p.__ptr_ != __i.__ptr_ && __p.__ptr_ != __i.__ptr_->__next_)
Howard Hinnant3e519522010-05-11 19:42:16 +00001956 {
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001957 __node_pointer __f = __i.__ptr_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001958 base::__unlink_nodes(__f, __f);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001959 __link_nodes(__p.__ptr_, __f, __f);
Howard Hinnant3e519522010-05-11 19:42:16 +00001960 --__c.__sz();
1961 ++base::__sz();
Howard Hinnant920b56c2011-09-27 23:55:03 +00001962#if _LIBCPP_DEBUG_LEVEL >= 2
1963 __libcpp_db* __db = __get_db();
1964 __c_node* __cn1 = __db->__find_c_and_lock(this);
1965 __c_node* __cn2 = __db->__find_c(&__c);
1966 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
1967 {
1968 --__p;
1969 iterator* __j = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00001970 if (__j->__ptr_ == __f)
Howard Hinnant920b56c2011-09-27 23:55:03 +00001971 {
1972 __cn1->__add(*__p);
1973 (*__p)->__c_ = __cn1;
1974 if (--__cn2->end_ != __p)
1975 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
1976 }
1977 }
1978 __db->unlock();
1979#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001980 }
1981}
1982
1983template <class _Tp, class _Alloc>
1984void
1985list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l)
1986{
Howard Hinnant920b56c2011-09-27 23:55:03 +00001987#if _LIBCPP_DEBUG_LEVEL >= 2
1988 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1989 "list::splice(iterator, list, iterator, iterator) called with first iterator not"
1990 " referring to this list");
1991 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__f) == &__c,
1992 "list::splice(iterator, list, iterator, iterator) called with second iterator not"
1993 " referring to list argument");
1994 if (this == &__c)
1995 {
1996 for (const_iterator __i = __f; __i != __l; ++__i)
1997 _LIBCPP_ASSERT(__i != __p,
1998 "list::splice(iterator, list, iterator, iterator)"
1999 " called with the first iterator within the range"
2000 " of the second and third iterators");
2001 }
2002#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002003 if (__f != __l)
2004 {
2005 if (this != &__c)
2006 {
Howard Hinnantce48a112011-06-30 21:18:19 +00002007 size_type __s = _VSTD::distance(__f, __l);
Howard Hinnant3e519522010-05-11 19:42:16 +00002008 __c.__sz() -= __s;
2009 base::__sz() += __s;
2010 }
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002011 __node_pointer __first = __f.__ptr_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002012 --__l;
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002013 __node_pointer __last = __l.__ptr_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002014 base::__unlink_nodes(__first, __last);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002015 __link_nodes(__p.__ptr_, __first, __last);
Howard Hinnant920b56c2011-09-27 23:55:03 +00002016#if _LIBCPP_DEBUG_LEVEL >= 2
2017 __libcpp_db* __db = __get_db();
2018 __c_node* __cn1 = __db->__find_c_and_lock(this);
2019 __c_node* __cn2 = __db->__find_c(&__c);
2020 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
2021 {
2022 --__p;
2023 iterator* __j = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002024 for (__node_pointer __k = __f.__ptr_;
Howard Hinnant920b56c2011-09-27 23:55:03 +00002025 __k != __l.__ptr_; __k = __k->__next_)
2026 {
2027 if (__j->__ptr_ == __k)
2028 {
2029 __cn1->__add(*__p);
2030 (*__p)->__c_ = __cn1;
2031 if (--__cn2->end_ != __p)
2032 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
2033 }
2034 }
2035 }
2036 __db->unlock();
2037#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002038 }
2039}
2040
2041template <class _Tp, class _Alloc>
2042void
2043list<_Tp, _Alloc>::remove(const value_type& __x)
2044{
Marshall Clowced70062014-08-08 15:35:52 +00002045 list<_Tp, _Alloc> __deleted_nodes; // collect the nodes we're removing
2046 for (const_iterator __i = begin(), __e = end(); __i != __e;)
Howard Hinnant3e519522010-05-11 19:42:16 +00002047 {
2048 if (*__i == __x)
2049 {
Marshall Clowced70062014-08-08 15:35:52 +00002050 const_iterator __j = _VSTD::next(__i);
Howard Hinnant3e519522010-05-11 19:42:16 +00002051 for (; __j != __e && *__j == __x; ++__j)
2052 ;
Marshall Clowced70062014-08-08 15:35:52 +00002053 __deleted_nodes.splice(__deleted_nodes.end(), *this, __i, __j);
2054 __i = __j;
Marshall Clow90ba05332014-08-04 17:32:25 +00002055 if (__i != __e)
Marshall Clow28d65da2014-08-05 01:34:12 +00002056 ++__i;
Howard Hinnant3e519522010-05-11 19:42:16 +00002057 }
2058 else
2059 ++__i;
2060 }
2061}
2062
2063template <class _Tp, class _Alloc>
2064template <class _Pred>
2065void
2066list<_Tp, _Alloc>::remove_if(_Pred __pred)
2067{
2068 for (iterator __i = begin(), __e = end(); __i != __e;)
2069 {
2070 if (__pred(*__i))
2071 {
Howard Hinnantce48a112011-06-30 21:18:19 +00002072 iterator __j = _VSTD::next(__i);
Howard Hinnant3e519522010-05-11 19:42:16 +00002073 for (; __j != __e && __pred(*__j); ++__j)
2074 ;
2075 __i = erase(__i, __j);
Marshall Clow90ba05332014-08-04 17:32:25 +00002076 if (__i != __e)
Marshall Clow28d65da2014-08-05 01:34:12 +00002077 ++__i;
Howard Hinnant3e519522010-05-11 19:42:16 +00002078 }
2079 else
2080 ++__i;
2081 }
2082}
2083
2084template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00002085inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00002086void
2087list<_Tp, _Alloc>::unique()
2088{
2089 unique(__equal_to<value_type>());
2090}
2091
2092template <class _Tp, class _Alloc>
2093template <class _BinaryPred>
2094void
2095list<_Tp, _Alloc>::unique(_BinaryPred __binary_pred)
2096{
2097 for (iterator __i = begin(), __e = end(); __i != __e;)
2098 {
Howard Hinnantce48a112011-06-30 21:18:19 +00002099 iterator __j = _VSTD::next(__i);
Howard Hinnant3e519522010-05-11 19:42:16 +00002100 for (; __j != __e && __binary_pred(*__i, *__j); ++__j)
2101 ;
2102 if (++__i != __j)
2103 __i = erase(__i, __j);
2104 }
2105}
2106
2107template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00002108inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00002109void
2110list<_Tp, _Alloc>::merge(list& __c)
2111{
2112 merge(__c, __less<value_type>());
2113}
2114
2115template <class _Tp, class _Alloc>
2116template <class _Comp>
2117void
2118list<_Tp, _Alloc>::merge(list& __c, _Comp __comp)
2119{
2120 if (this != &__c)
2121 {
2122 iterator __f1 = begin();
2123 iterator __e1 = end();
2124 iterator __f2 = __c.begin();
2125 iterator __e2 = __c.end();
2126 while (__f1 != __e1 && __f2 != __e2)
2127 {
2128 if (__comp(*__f2, *__f1))
2129 {
2130 size_type __ds = 1;
Howard Hinnantce48a112011-06-30 21:18:19 +00002131 iterator __m2 = _VSTD::next(__f2);
Howard Hinnant3e519522010-05-11 19:42:16 +00002132 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2, ++__ds)
2133 ;
2134 base::__sz() += __ds;
2135 __c.__sz() -= __ds;
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002136 __node_pointer __f = __f2.__ptr_;
2137 __node_pointer __l = __m2.__ptr_->__prev_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002138 __f2 = __m2;
2139 base::__unlink_nodes(__f, __l);
Howard Hinnantce48a112011-06-30 21:18:19 +00002140 __m2 = _VSTD::next(__f1);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002141 __link_nodes(__f1.__ptr_, __f, __l);
Howard Hinnant3e519522010-05-11 19:42:16 +00002142 __f1 = __m2;
2143 }
2144 else
2145 ++__f1;
2146 }
2147 splice(__e1, __c);
Howard Hinnant920b56c2011-09-27 23:55:03 +00002148#if _LIBCPP_DEBUG_LEVEL >= 2
2149 __libcpp_db* __db = __get_db();
2150 __c_node* __cn1 = __db->__find_c_and_lock(this);
2151 __c_node* __cn2 = __db->__find_c(&__c);
2152 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
2153 {
2154 --__p;
2155 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002156 if (__i->__ptr_ != static_cast<__node_pointer>(
2157 pointer_traits<__node_base_pointer>::pointer_to(__c.__end_)))
Howard Hinnant920b56c2011-09-27 23:55:03 +00002158 {
2159 __cn1->__add(*__p);
2160 (*__p)->__c_ = __cn1;
2161 if (--__cn2->end_ != __p)
2162 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
2163 }
2164 }
2165 __db->unlock();
2166#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002167 }
2168}
2169
2170template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00002171inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00002172void
2173list<_Tp, _Alloc>::sort()
2174{
2175 sort(__less<value_type>());
2176}
2177
2178template <class _Tp, class _Alloc>
2179template <class _Comp>
Howard Hinnant848a5372010-09-22 16:48:34 +00002180inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00002181void
2182list<_Tp, _Alloc>::sort(_Comp __comp)
2183{
2184 __sort(begin(), end(), base::__sz(), __comp);
2185}
2186
2187template <class _Tp, class _Alloc>
2188template <class _Comp>
Howard Hinnant3e519522010-05-11 19:42:16 +00002189typename list<_Tp, _Alloc>::iterator
2190list<_Tp, _Alloc>::__sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp)
2191{
2192 switch (__n)
2193 {
2194 case 0:
2195 case 1:
2196 return __f1;
2197 case 2:
2198 if (__comp(*--__e2, *__f1))
2199 {
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002200 __node_pointer __f = __e2.__ptr_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002201 base::__unlink_nodes(__f, __f);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002202 __link_nodes(__f1.__ptr_, __f, __f);
Howard Hinnant3e519522010-05-11 19:42:16 +00002203 return __e2;
2204 }
2205 return __f1;
2206 }
2207 size_type __n2 = __n / 2;
Howard Hinnantce48a112011-06-30 21:18:19 +00002208 iterator __e1 = _VSTD::next(__f1, __n2);
Howard Hinnant3e519522010-05-11 19:42:16 +00002209 iterator __r = __f1 = __sort(__f1, __e1, __n2, __comp);
2210 iterator __f2 = __e1 = __sort(__e1, __e2, __n - __n2, __comp);
2211 if (__comp(*__f2, *__f1))
2212 {
Howard Hinnantce48a112011-06-30 21:18:19 +00002213 iterator __m2 = _VSTD::next(__f2);
Howard Hinnant3e519522010-05-11 19:42:16 +00002214 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2)
2215 ;
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002216 __node_pointer __f = __f2.__ptr_;
2217 __node_pointer __l = __m2.__ptr_->__prev_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002218 __r = __f2;
2219 __e1 = __f2 = __m2;
2220 base::__unlink_nodes(__f, __l);
Howard Hinnantce48a112011-06-30 21:18:19 +00002221 __m2 = _VSTD::next(__f1);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002222 __link_nodes(__f1.__ptr_, __f, __l);
Howard Hinnant3e519522010-05-11 19:42:16 +00002223 __f1 = __m2;
2224 }
2225 else
2226 ++__f1;
2227 while (__f1 != __e1 && __f2 != __e2)
2228 {
2229 if (__comp(*__f2, *__f1))
2230 {
Howard Hinnantce48a112011-06-30 21:18:19 +00002231 iterator __m2 = _VSTD::next(__f2);
Howard Hinnant3e519522010-05-11 19:42:16 +00002232 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2)
2233 ;
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002234 __node_pointer __f = __f2.__ptr_;
2235 __node_pointer __l = __m2.__ptr_->__prev_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002236 if (__e1 == __f2)
2237 __e1 = __m2;
2238 __f2 = __m2;
2239 base::__unlink_nodes(__f, __l);
Howard Hinnantce48a112011-06-30 21:18:19 +00002240 __m2 = _VSTD::next(__f1);
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002241 __link_nodes(__f1.__ptr_, __f, __l);
Howard Hinnant3e519522010-05-11 19:42:16 +00002242 __f1 = __m2;
2243 }
2244 else
2245 ++__f1;
2246 }
2247 return __r;
2248}
2249
2250template <class _Tp, class _Alloc>
2251void
Howard Hinnant45900102011-06-03 17:30:28 +00002252list<_Tp, _Alloc>::reverse() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00002253{
2254 if (base::__sz() > 1)
2255 {
2256 iterator __e = end();
Howard Hinnant920b56c2011-09-27 23:55:03 +00002257 for (iterator __i = begin(); __i.__ptr_ != __e.__ptr_;)
2258 {
Howard Hinnantce48a112011-06-30 21:18:19 +00002259 _VSTD::swap(__i.__ptr_->__prev_, __i.__ptr_->__next_);
Howard Hinnant920b56c2011-09-27 23:55:03 +00002260 __i.__ptr_ = __i.__ptr_->__prev_;
2261 }
Howard Hinnantce48a112011-06-30 21:18:19 +00002262 _VSTD::swap(__e.__ptr_->__prev_, __e.__ptr_->__next_);
Howard Hinnant3e519522010-05-11 19:42:16 +00002263 }
2264}
2265
2266template <class _Tp, class _Alloc>
Howard Hinnant920b56c2011-09-27 23:55:03 +00002267bool
2268list<_Tp, _Alloc>::__invariants() const
2269{
2270 return size() == _VSTD::distance(begin(), end());
2271}
2272
2273#if _LIBCPP_DEBUG_LEVEL >= 2
2274
2275template <class _Tp, class _Alloc>
2276bool
2277list<_Tp, _Alloc>::__dereferenceable(const const_iterator* __i) const
2278{
Howard Hinnant866d4ef2013-06-25 16:08:47 +00002279 return __i->__ptr_ != static_cast<__node_pointer>(
2280 pointer_traits<__node_base_pointer>::pointer_to(const_cast<__node_base&>(this->__end_)));
Howard Hinnant920b56c2011-09-27 23:55:03 +00002281}
2282
2283template <class _Tp, class _Alloc>
2284bool
2285list<_Tp, _Alloc>::__decrementable(const const_iterator* __i) const
2286{
2287 return !empty() && __i->__ptr_ != base::__end_.__next_;
2288}
2289
2290template <class _Tp, class _Alloc>
2291bool
2292list<_Tp, _Alloc>::__addable(const const_iterator* __i, ptrdiff_t __n) const
2293{
2294 return false;
2295}
2296
2297template <class _Tp, class _Alloc>
2298bool
2299list<_Tp, _Alloc>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
2300{
2301 return false;
2302}
2303
2304#endif // _LIBCPP_DEBUG_LEVEL >= 2
2305
2306template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00002307inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00002308bool
2309operator==(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2310{
Howard Hinnantce48a112011-06-30 21:18:19 +00002311 return __x.size() == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnant3e519522010-05-11 19:42:16 +00002312}
2313
2314template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00002315inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00002316bool
2317operator< (const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2318{
Howard Hinnantce48a112011-06-30 21:18:19 +00002319 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnant3e519522010-05-11 19:42:16 +00002320}
2321
2322template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00002323inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00002324bool
2325operator!=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2326{
2327 return !(__x == __y);
2328}
2329
2330template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00002331inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00002332bool
2333operator> (const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2334{
2335 return __y < __x;
2336}
2337
2338template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00002339inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00002340bool
2341operator>=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2342{
2343 return !(__x < __y);
2344}
2345
2346template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00002347inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00002348bool
2349operator<=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2350{
2351 return !(__y < __x);
2352}
2353
2354template <class _Tp, class _Alloc>
Howard Hinnant848a5372010-09-22 16:48:34 +00002355inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00002356void
2357swap(list<_Tp, _Alloc>& __x, list<_Tp, _Alloc>& __y)
Howard Hinnant45900102011-06-03 17:30:28 +00002358 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnant3e519522010-05-11 19:42:16 +00002359{
2360 __x.swap(__y);
2361}
2362
2363_LIBCPP_END_NAMESPACE_STD
2364
2365#endif // _LIBCPP_LIST