blob: 1d21fd6f1181054c94df7c99a1847c6ff9dd05d1 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===---------------------------- list ------------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-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 Hinnantbc8d3f92010-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 Hinnantc5607272011-06-03 17:30:28 +000039 list()
40 noexcept(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000041 explicit list(const allocator_type& a);
42 explicit list(size_type n);
Marshall Clowe00f53b2013-09-09 18:19:45 +000043 explicit list(size_type n, const allocator_type& a); // C++14
Howard Hinnantbc8d3f92010-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 Hinnantc5607272011-06-03 17:30:28 +000052 list(list&& x)
53 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-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 Hinnantc5607272011-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 Hinnantbc8d3f92010-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 Hinnantc5607272011-06-03 17:30:28 +000071 allocator_type get_allocator() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000072
Howard Hinnantc5607272011-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 Hinnantbc8d3f92010-05-11 19:42:16 +000085
86 reference front();
87 const_reference front() const;
88 reference back();
89 const_reference back() const;
90
Howard Hinnantc5607272011-06-03 17:30:28 +000091 bool empty() const noexcept;
92 size_type size() const noexcept;
93 size_type max_size() const noexcept;
Howard Hinnantbc8d3f92010-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 Hinnantc5607272011-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 Hinnantbc8d3f92010-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 Hinnantc5607272011-06-03 17:30:28 +0000148 void reverse() noexcept;
Howard Hinnantbc8d3f92010-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 Hinnantc5607272011-06-03 17:30:28 +0000165 void swap(list<T,Alloc>& x, list<T,Alloc>& y)
166 noexcept(noexcept(x.swap(y)));
Howard Hinnantbc8d3f92010-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 Hinnant66c6f972011-11-29 16:45:27 +0000180#include <__undef_min_max>
181
Howard Hinnant5e571422013-08-23 20:10:18 +0000182#ifdef _LIBCPP_DEBUG
Howard Hinnant8b00e6c2013-08-02 00:26:35 +0000183# include <__debug>
184#else
185# define _LIBCPP_ASSERT(x, m) ((void)0)
186#endif
187
Howard Hinnant08e17472011-10-17 20:05:10 +0000188#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000189#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000190#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000191
192_LIBCPP_BEGIN_NAMESPACE_STD
193
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000194template <class _Tp, class _VoidPtr> struct __list_node;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000195
196template <class _Tp, class _VoidPtr>
197struct __list_node_base
198{
199 typedef typename pointer_traits<_VoidPtr>::template
200#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
201 rebind<__list_node<_Tp, _VoidPtr> > pointer;
202#else
203 rebind<__list_node<_Tp, _VoidPtr> >::other pointer;
204#endif
205
Howard Hinnant29f74322013-06-25 16:08:47 +0000206 typedef typename pointer_traits<_VoidPtr>::template
207#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
208 rebind<__list_node_base> __base_pointer;
209#else
210 rebind<__list_node_base>::other __base_pointer;
211#endif
212
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000213 pointer __prev_;
214 pointer __next_;
215
Howard Hinnant82894812010-09-22 16:48:34 +0000216 _LIBCPP_INLINE_VISIBILITY
Marshall Clowea8ed832014-08-05 01:34:12 +0000217 __list_node_base() : __prev_(__self()), __next_(__self()) {}
218
219 _LIBCPP_INLINE_VISIBILITY
220 pointer __self()
221 {
222 return static_cast<pointer>(pointer_traits<__base_pointer>::pointer_to(*this));
223 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000224};
225
226template <class _Tp, class _VoidPtr>
227struct __list_node
228 : public __list_node_base<_Tp, _VoidPtr>
229{
230 _Tp __value_;
231};
232
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000233template <class _Tp, class _Alloc> class _LIBCPP_TYPE_VIS_ONLY list;
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000234template <class _Tp, class _Alloc> class __list_imp;
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000235template <class _Tp, class _VoidPtr> class _LIBCPP_TYPE_VIS_ONLY __list_const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000236
237template <class _Tp, class _VoidPtr>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000238class _LIBCPP_TYPE_VIS_ONLY __list_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000239{
240 typedef typename pointer_traits<_VoidPtr>::template
241#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
242 rebind<__list_node<_Tp, _VoidPtr> > __node_pointer;
243#else
244 rebind<__list_node<_Tp, _VoidPtr> >::other __node_pointer;
245#endif
246
247 __node_pointer __ptr_;
248
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000249#if _LIBCPP_DEBUG_LEVEL >= 2
250 _LIBCPP_INLINE_VISIBILITY
251 explicit __list_iterator(__node_pointer __p, const void* __c) _NOEXCEPT
252 : __ptr_(__p)
253 {
254 __get_db()->__insert_ic(this, __c);
255 }
256#else
Howard Hinnant82894812010-09-22 16:48:34 +0000257 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000258 explicit __list_iterator(__node_pointer __p) _NOEXCEPT : __ptr_(__p) {}
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000259#endif
260
261
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000262
263 template<class, class> friend class list;
264 template<class, class> friend class __list_imp;
265 template<class, class> friend class __list_const_iterator;
266public:
267 typedef bidirectional_iterator_tag iterator_category;
268 typedef _Tp value_type;
269 typedef value_type& reference;
270 typedef typename pointer_traits<_VoidPtr>::template
271#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
272 rebind<value_type>
273#else
274 rebind<value_type>::other
275#endif
276 pointer;
277 typedef typename pointer_traits<pointer>::difference_type difference_type;
278
Howard Hinnant82894812010-09-22 16:48:34 +0000279 _LIBCPP_INLINE_VISIBILITY
Marshall Clow65d2e6a2013-08-05 21:23:28 +0000280 __list_iterator() _NOEXCEPT : __ptr_(nullptr)
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000281 {
282#if _LIBCPP_DEBUG_LEVEL >= 2
283 __get_db()->__insert_i(this);
284#endif
285 }
286
287#if _LIBCPP_DEBUG_LEVEL >= 2
288
Howard Hinnant211f0ee2011-01-28 23:46:28 +0000289 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000290 __list_iterator(const __list_iterator& __p)
291 : __ptr_(__p.__ptr_)
292 {
293 __get_db()->__iterator_copy(this, &__p);
294 }
295
296 _LIBCPP_INLINE_VISIBILITY
297 ~__list_iterator()
298 {
299 __get_db()->__erase_i(this);
300 }
301
302 _LIBCPP_INLINE_VISIBILITY
303 __list_iterator& operator=(const __list_iterator& __p)
304 {
305 if (this != &__p)
306 {
307 __get_db()->__iterator_copy(this, &__p);
308 __ptr_ = __p.__ptr_;
309 }
310 return *this;
311 }
312
313#endif // _LIBCPP_DEBUG_LEVEL >= 2
314
315 _LIBCPP_INLINE_VISIBILITY
316 reference operator*() const
317 {
318#if _LIBCPP_DEBUG_LEVEL >= 2
319 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
320 "Attempted to dereference a non-dereferenceable list::iterator");
321#endif
322 return __ptr_->__value_;
323 }
Howard Hinnant82894812010-09-22 16:48:34 +0000324 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant29f74322013-06-25 16:08:47 +0000325 pointer operator->() const
326 {
327#if _LIBCPP_DEBUG_LEVEL >= 2
328 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
329 "Attempted to dereference a non-dereferenceable list::iterator");
330#endif
331 return pointer_traits<pointer>::pointer_to(__ptr_->__value_);
332 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000333
Howard Hinnant82894812010-09-22 16:48:34 +0000334 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000335 __list_iterator& operator++()
336 {
337#if _LIBCPP_DEBUG_LEVEL >= 2
338 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
339 "Attempted to increment non-incrementable list::iterator");
340#endif
341 __ptr_ = __ptr_->__next_;
342 return *this;
343 }
Howard Hinnant82894812010-09-22 16:48:34 +0000344 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000345 __list_iterator operator++(int) {__list_iterator __t(*this); ++(*this); return __t;}
346
Howard Hinnant82894812010-09-22 16:48:34 +0000347 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000348 __list_iterator& operator--()
349 {
350#if _LIBCPP_DEBUG_LEVEL >= 2
351 _LIBCPP_ASSERT(__get_const_db()->__decrementable(this),
352 "Attempted to decrement non-decrementable list::iterator");
353#endif
354 __ptr_ = __ptr_->__prev_;
355 return *this;
356 }
Howard Hinnant82894812010-09-22 16:48:34 +0000357 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000358 __list_iterator operator--(int) {__list_iterator __t(*this); --(*this); return __t;}
359
Howard Hinnant82894812010-09-22 16:48:34 +0000360 friend _LIBCPP_INLINE_VISIBILITY
361 bool operator==(const __list_iterator& __x, const __list_iterator& __y)
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000362 {
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000363 return __x.__ptr_ == __y.__ptr_;
364 }
Howard Hinnant82894812010-09-22 16:48:34 +0000365 friend _LIBCPP_INLINE_VISIBILITY
366 bool operator!=(const __list_iterator& __x, const __list_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000367 {return !(__x == __y);}
368};
369
370template <class _Tp, class _VoidPtr>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000371class _LIBCPP_TYPE_VIS_ONLY __list_const_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000372{
373 typedef typename pointer_traits<_VoidPtr>::template
374#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnant29f74322013-06-25 16:08:47 +0000375 rebind<__list_node<_Tp, _VoidPtr> > __node_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000376#else
Howard Hinnant29f74322013-06-25 16:08:47 +0000377 rebind<__list_node<_Tp, _VoidPtr> >::other __node_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000378#endif
379
380 __node_pointer __ptr_;
381
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000382#if _LIBCPP_DEBUG_LEVEL >= 2
383 _LIBCPP_INLINE_VISIBILITY
384 explicit __list_const_iterator(__node_pointer __p, const void* __c) _NOEXCEPT
385 : __ptr_(__p)
386 {
387 __get_db()->__insert_ic(this, __c);
388 }
389#else
Howard Hinnant82894812010-09-22 16:48:34 +0000390 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000391 explicit __list_const_iterator(__node_pointer __p) _NOEXCEPT : __ptr_(__p) {}
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000392#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000393
394 template<class, class> friend class list;
395 template<class, class> friend class __list_imp;
396public:
397 typedef bidirectional_iterator_tag iterator_category;
398 typedef _Tp value_type;
399 typedef const value_type& reference;
400 typedef typename pointer_traits<_VoidPtr>::template
401#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
402 rebind<const value_type>
403#else
404 rebind<const value_type>::other
405#endif
406 pointer;
407 typedef typename pointer_traits<pointer>::difference_type difference_type;
408
Howard Hinnant82894812010-09-22 16:48:34 +0000409 _LIBCPP_INLINE_VISIBILITY
Marshall Clow65d2e6a2013-08-05 21:23:28 +0000410 __list_const_iterator() _NOEXCEPT : __ptr_(nullptr)
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000411 {
412#if _LIBCPP_DEBUG_LEVEL >= 2
413 __get_db()->__insert_i(this);
414#endif
415 }
Howard Hinnant211f0ee2011-01-28 23:46:28 +0000416 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6dcaf3e2013-04-05 17:58:52 +0000417 __list_const_iterator(const __list_iterator<_Tp, _VoidPtr>& __p) _NOEXCEPT
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000418 : __ptr_(__p.__ptr_)
419 {
420#if _LIBCPP_DEBUG_LEVEL >= 2
421 __get_db()->__iterator_copy(this, &__p);
422#endif
423 }
424
425#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000426
Howard Hinnant82894812010-09-22 16:48:34 +0000427 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000428 __list_const_iterator(const __list_const_iterator& __p)
429 : __ptr_(__p.__ptr_)
430 {
431 __get_db()->__iterator_copy(this, &__p);
432 }
433
434 _LIBCPP_INLINE_VISIBILITY
435 ~__list_const_iterator()
436 {
437 __get_db()->__erase_i(this);
438 }
439
440 _LIBCPP_INLINE_VISIBILITY
441 __list_const_iterator& operator=(const __list_const_iterator& __p)
442 {
443 if (this != &__p)
444 {
445 __get_db()->__iterator_copy(this, &__p);
446 __ptr_ = __p.__ptr_;
447 }
448 return *this;
449 }
450
451#endif // _LIBCPP_DEBUG_LEVEL >= 2
452 _LIBCPP_INLINE_VISIBILITY
453 reference operator*() const
454 {
455#if _LIBCPP_DEBUG_LEVEL >= 2
456 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
457 "Attempted to dereference a non-dereferenceable list::const_iterator");
458#endif
459 return __ptr_->__value_;
460 }
Howard Hinnant82894812010-09-22 16:48:34 +0000461 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant29f74322013-06-25 16:08:47 +0000462 pointer operator->() const
463 {
464#if _LIBCPP_DEBUG_LEVEL >= 2
465 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
466 "Attempted to dereference a non-dereferenceable list::iterator");
467#endif
468 return pointer_traits<pointer>::pointer_to(__ptr_->__value_);
469 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000470
Howard Hinnant82894812010-09-22 16:48:34 +0000471 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000472 __list_const_iterator& operator++()
473 {
474#if _LIBCPP_DEBUG_LEVEL >= 2
475 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
476 "Attempted to increment non-incrementable list::const_iterator");
477#endif
478 __ptr_ = __ptr_->__next_;
479 return *this;
480 }
Howard Hinnant82894812010-09-22 16:48:34 +0000481 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000482 __list_const_iterator operator++(int) {__list_const_iterator __t(*this); ++(*this); return __t;}
483
Howard Hinnant82894812010-09-22 16:48:34 +0000484 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000485 __list_const_iterator& operator--()
486 {
487#if _LIBCPP_DEBUG_LEVEL >= 2
488 _LIBCPP_ASSERT(__get_const_db()->__decrementable(this),
489 "Attempted to decrement non-decrementable list::const_iterator");
490#endif
491 __ptr_ = __ptr_->__prev_;
492 return *this;
493 }
Howard Hinnant82894812010-09-22 16:48:34 +0000494 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000495 __list_const_iterator operator--(int) {__list_const_iterator __t(*this); --(*this); return __t;}
496
Howard Hinnant82894812010-09-22 16:48:34 +0000497 friend _LIBCPP_INLINE_VISIBILITY
498 bool operator==(const __list_const_iterator& __x, const __list_const_iterator& __y)
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000499 {
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000500 return __x.__ptr_ == __y.__ptr_;
501 }
Howard Hinnant82894812010-09-22 16:48:34 +0000502 friend _LIBCPP_INLINE_VISIBILITY
503 bool operator!=(const __list_const_iterator& __x, const __list_const_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000504 {return !(__x == __y);}
505};
506
507template <class _Tp, class _Alloc>
508class __list_imp
509{
510 __list_imp(const __list_imp&);
511 __list_imp& operator=(const __list_imp&);
512protected:
513 typedef _Tp value_type;
514 typedef _Alloc allocator_type;
515 typedef allocator_traits<allocator_type> __alloc_traits;
516 typedef typename __alloc_traits::size_type size_type;
517 typedef typename __alloc_traits::void_pointer __void_pointer;
518 typedef __list_iterator<value_type, __void_pointer> iterator;
519 typedef __list_const_iterator<value_type, __void_pointer> const_iterator;
520 typedef __list_node_base<value_type, __void_pointer> __node_base;
521 typedef __list_node<value_type, __void_pointer> __node;
522 typedef typename __alloc_traits::template
523#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
524 rebind_alloc<__node>
525#else
526 rebind_alloc<__node>::other
527#endif
528 __node_allocator;
529 typedef allocator_traits<__node_allocator> __node_alloc_traits;
530 typedef typename __node_alloc_traits::pointer __node_pointer;
Howard Hinnant29f74322013-06-25 16:08:47 +0000531 typedef typename __node_alloc_traits::pointer __node_const_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000532 typedef typename __alloc_traits::pointer pointer;
533 typedef typename __alloc_traits::const_pointer const_pointer;
534 typedef typename __alloc_traits::difference_type difference_type;
535
Howard Hinnant29f74322013-06-25 16:08:47 +0000536 typedef typename __alloc_traits::template
537#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
538 rebind_alloc<__node_base>
539#else
540 rebind_alloc<__node_base>::other
541#endif
542 __node_base_allocator;
543 typedef typename allocator_traits<__node_base_allocator>::pointer __node_base_pointer;
544
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000545 __node_base __end_;
546 __compressed_pair<size_type, __node_allocator> __size_alloc_;
547
Howard Hinnant82894812010-09-22 16:48:34 +0000548 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000549 size_type& __sz() _NOEXCEPT {return __size_alloc_.first();}
Howard Hinnant82894812010-09-22 16:48:34 +0000550 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000551 const size_type& __sz() const _NOEXCEPT
552 {return __size_alloc_.first();}
Howard Hinnant82894812010-09-22 16:48:34 +0000553 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000554 __node_allocator& __node_alloc() _NOEXCEPT
555 {return __size_alloc_.second();}
Howard Hinnant82894812010-09-22 16:48:34 +0000556 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000557 const __node_allocator& __node_alloc() const _NOEXCEPT
558 {return __size_alloc_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000559
Howard Hinnant29f74322013-06-25 16:08:47 +0000560 static void __unlink_nodes(__node_pointer __f, __node_pointer __l) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000561
Howard Hinnantc5607272011-06-03 17:30:28 +0000562 __list_imp()
563 _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000564 __list_imp(const allocator_type& __a);
565 ~__list_imp();
Howard Hinnantc5607272011-06-03 17:30:28 +0000566 void clear() _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +0000567 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000568 bool empty() const _NOEXCEPT {return __sz() == 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000569
Howard Hinnant82894812010-09-22 16:48:34 +0000570 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000571 iterator begin() _NOEXCEPT
572 {
573#if _LIBCPP_DEBUG_LEVEL >= 2
574 return iterator(__end_.__next_, this);
575#else
576 return iterator(__end_.__next_);
577#endif
578 }
Howard Hinnant82894812010-09-22 16:48:34 +0000579 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000580 const_iterator begin() const _NOEXCEPT
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000581 {
582#if _LIBCPP_DEBUG_LEVEL >= 2
583 return const_iterator(__end_.__next_, this);
584#else
585 return const_iterator(__end_.__next_);
586#endif
587 }
Howard Hinnant82894812010-09-22 16:48:34 +0000588 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000589 iterator end() _NOEXCEPT
590 {
591#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant29f74322013-06-25 16:08:47 +0000592 return iterator(static_cast<__node_pointer>(
593 pointer_traits<__node_base_pointer>::pointer_to(__end_)), this);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000594#else
Howard Hinnant29f74322013-06-25 16:08:47 +0000595 return iterator(static_cast<__node_pointer>(
596 pointer_traits<__node_base_pointer>::pointer_to(__end_)));
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000597#endif
598 }
Howard Hinnant82894812010-09-22 16:48:34 +0000599 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000600 const_iterator end() const _NOEXCEPT
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000601 {
602#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant29f74322013-06-25 16:08:47 +0000603 return const_iterator(static_cast<__node_const_pointer>(
604 pointer_traits<__node_base_pointer>::pointer_to(const_cast<__node_base&>(__end_))), this);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000605#else
Howard Hinnant29f74322013-06-25 16:08:47 +0000606 return const_iterator(static_cast<__node_const_pointer>(
607 pointer_traits<__node_base_pointer>::pointer_to(const_cast<__node_base&>(__end_))));
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000608#endif
609 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000610
Howard Hinnantc5607272011-06-03 17:30:28 +0000611 void swap(__list_imp& __c)
612 _NOEXCEPT_(!__node_alloc_traits::propagate_on_container_swap::value ||
613 __is_nothrow_swappable<__node_allocator>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000614
Howard Hinnant82894812010-09-22 16:48:34 +0000615 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000616 void __copy_assign_alloc(const __list_imp& __c)
617 {__copy_assign_alloc(__c, integral_constant<bool,
618 __node_alloc_traits::propagate_on_container_copy_assignment::value>());}
619
Howard Hinnant82894812010-09-22 16:48:34 +0000620 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000621 void __move_assign_alloc(__list_imp& __c)
Howard Hinnantc5607272011-06-03 17:30:28 +0000622 _NOEXCEPT_(
623 !__node_alloc_traits::propagate_on_container_move_assignment::value ||
624 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000625 {__move_assign_alloc(__c, integral_constant<bool,
626 __node_alloc_traits::propagate_on_container_move_assignment::value>());}
627
628private:
Howard Hinnant82894812010-09-22 16:48:34 +0000629 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000630 static void __swap_alloc(__node_allocator& __x, __node_allocator& __y)
Howard Hinnantc5607272011-06-03 17:30:28 +0000631 _NOEXCEPT_(!__node_alloc_traits::propagate_on_container_swap::value ||
632 __is_nothrow_swappable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000633 {__swap_alloc(__x, __y, integral_constant<bool,
634 __node_alloc_traits::propagate_on_container_swap::value>());}
Howard Hinnant82894812010-09-22 16:48:34 +0000635 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000636 static void __swap_alloc(__node_allocator& __x, __node_allocator& __y, true_type)
Howard Hinnantc5607272011-06-03 17:30:28 +0000637 _NOEXCEPT_(__is_nothrow_swappable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000638 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000639 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000640 swap(__x, __y);
641 }
Howard Hinnant82894812010-09-22 16:48:34 +0000642 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000643 static void __swap_alloc(__node_allocator& __x, __node_allocator& __y, false_type)
Howard Hinnantc5607272011-06-03 17:30:28 +0000644 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000645 {}
646
Howard Hinnant82894812010-09-22 16:48:34 +0000647 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000648 void __copy_assign_alloc(const __list_imp& __c, true_type)
649 {
650 if (__node_alloc() != __c.__node_alloc())
651 clear();
652 __node_alloc() = __c.__node_alloc();
653 }
654
Howard Hinnant82894812010-09-22 16:48:34 +0000655 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000656 void __copy_assign_alloc(const __list_imp& __c, false_type)
657 {}
658
Howard Hinnant82894812010-09-22 16:48:34 +0000659 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31 +0000660 void __move_assign_alloc(__list_imp& __c, true_type)
Howard Hinnantc5607272011-06-03 17:30:28 +0000661 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000662 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000663 __node_alloc() = _VSTD::move(__c.__node_alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000664 }
665
Howard Hinnant82894812010-09-22 16:48:34 +0000666 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31 +0000667 void __move_assign_alloc(__list_imp& __c, false_type)
Howard Hinnantc5607272011-06-03 17:30:28 +0000668 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000669 {}
670};
671
672// Unlink nodes [__f, __l]
673template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +0000674inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000675void
Howard Hinnant29f74322013-06-25 16:08:47 +0000676__list_imp<_Tp, _Alloc>::__unlink_nodes(__node_pointer __f, __node_pointer __l)
Howard Hinnantc5607272011-06-03 17:30:28 +0000677 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000678{
Howard Hinnant29f74322013-06-25 16:08:47 +0000679 __f->__prev_->__next_ = __l->__next_;
680 __l->__next_->__prev_ = __f->__prev_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000681}
682
683template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +0000684inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000685__list_imp<_Tp, _Alloc>::__list_imp()
Howard Hinnantc5607272011-06-03 17:30:28 +0000686 _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000687 : __size_alloc_(0)
688{
689}
690
691template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +0000692inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000693__list_imp<_Tp, _Alloc>::__list_imp(const allocator_type& __a)
694 : __size_alloc_(0, __node_allocator(__a))
695{
696}
697
698template <class _Tp, class _Alloc>
699__list_imp<_Tp, _Alloc>::~__list_imp()
700{
701 clear();
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000702#if _LIBCPP_DEBUG_LEVEL >= 2
703 __get_db()->__erase_c(this);
704#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000705}
706
707template <class _Tp, class _Alloc>
708void
Howard Hinnantc5607272011-06-03 17:30:28 +0000709__list_imp<_Tp, _Alloc>::clear() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000710{
711 if (!empty())
712 {
713 __node_allocator& __na = __node_alloc();
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000714 __node_pointer __f = __end_.__next_;
Howard Hinnant29f74322013-06-25 16:08:47 +0000715 __node_pointer __l = static_cast<__node_pointer>(
716 pointer_traits<__node_base_pointer>::pointer_to(__end_));
717 __unlink_nodes(__f, __l->__prev_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000718 __sz() = 0;
719 while (__f != __l)
720 {
Howard Hinnant29f74322013-06-25 16:08:47 +0000721 __node_pointer __n = __f;
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000722 __f = __f->__next_;
Howard Hinnant29f74322013-06-25 16:08:47 +0000723 __node_alloc_traits::destroy(__na, _VSTD::addressof(__n->__value_));
724 __node_alloc_traits::deallocate(__na, __n, 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000725 }
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000726#if _LIBCPP_DEBUG_LEVEL >= 2
727 __c_node* __c = __get_db()->__find_c_and_lock(this);
728 for (__i_node** __p = __c->end_; __p != __c->beg_; )
729 {
730 --__p;
731 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
732 if (__i->__ptr_ != __l)
733 {
734 (*__p)->__c_ = nullptr;
735 if (--__c->end_ != __p)
736 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
737 }
738 }
739 __get_db()->unlock();
740#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000741 }
742}
743
744template <class _Tp, class _Alloc>
745void
746__list_imp<_Tp, _Alloc>::swap(__list_imp& __c)
Howard Hinnantc5607272011-06-03 17:30:28 +0000747 _NOEXCEPT_(!__node_alloc_traits::propagate_on_container_swap::value ||
748 __is_nothrow_swappable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000749{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000750 _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
751 this->__node_alloc() == __c.__node_alloc(),
752 "list::swap: Either propagate_on_container_swap must be true"
753 " or the allocators must compare equal");
Howard Hinnant0949eed2011-06-30 21:18:19 +0000754 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000755 __swap_alloc(__node_alloc(), __c.__node_alloc());
756 swap(__sz(), __c.__sz());
757 swap(__end_, __c.__end_);
758 if (__sz() == 0)
Marshall Clowea8ed832014-08-05 01:34:12 +0000759 __end_.__next_ = __end_.__prev_ = __end_.__self();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000760 else
Marshall Clowea8ed832014-08-05 01:34:12 +0000761 __end_.__prev_->__next_ = __end_.__next_->__prev_ = __end_.__self();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000762 if (__c.__sz() == 0)
Marshall Clowea8ed832014-08-05 01:34:12 +0000763 __c.__end_.__next_ = __c.__end_.__prev_ = __c.__end_.__self();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000764 else
Marshall Clowea8ed832014-08-05 01:34:12 +0000765 __c.__end_.__prev_->__next_ = __c.__end_.__next_->__prev_ = __c.__end_.__self();
766
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000767#if _LIBCPP_DEBUG_LEVEL >= 2
768 __libcpp_db* __db = __get_db();
769 __c_node* __cn1 = __db->__find_c_and_lock(this);
770 __c_node* __cn2 = __db->__find_c(&__c);
771 std::swap(__cn1->beg_, __cn2->beg_);
772 std::swap(__cn1->end_, __cn2->end_);
773 std::swap(__cn1->cap_, __cn2->cap_);
774 for (__i_node** __p = __cn1->end_; __p != __cn1->beg_;)
775 {
776 --__p;
777 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
Howard Hinnant29f74322013-06-25 16:08:47 +0000778 if (__i->__ptr_ == static_cast<__node_pointer>(
779 pointer_traits<__node_base_pointer>::pointer_to(__c.__end_)))
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000780 {
781 __cn2->__add(*__p);
782 if (--__cn1->end_ != __p)
783 memmove(__p, __p+1, (__cn1->end_ - __p)*sizeof(__i_node*));
784 }
785 else
786 (*__p)->__c_ = __cn1;
787 }
788 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
789 {
790 --__p;
791 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
Howard Hinnant29f74322013-06-25 16:08:47 +0000792 if (__i->__ptr_ == static_cast<__node_pointer>(
793 pointer_traits<__node_base_pointer>::pointer_to(__end_)))
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000794 {
795 __cn1->__add(*__p);
796 if (--__cn2->end_ != __p)
797 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
798 }
799 else
800 (*__p)->__c_ = __cn2;
801 }
802 __db->unlock();
803#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000804}
805
806template <class _Tp, class _Alloc = allocator<_Tp> >
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000807class _LIBCPP_TYPE_VIS_ONLY list
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000808 : private __list_imp<_Tp, _Alloc>
809{
810 typedef __list_imp<_Tp, _Alloc> base;
811 typedef typename base::__node __node;
812 typedef typename base::__node_allocator __node_allocator;
813 typedef typename base::__node_pointer __node_pointer;
814 typedef typename base::__node_alloc_traits __node_alloc_traits;
Howard Hinnant29f74322013-06-25 16:08:47 +0000815 typedef typename base::__node_base __node_base;
816 typedef typename base::__node_base_pointer __node_base_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000817
818public:
819 typedef _Tp value_type;
820 typedef _Alloc allocator_type;
821 static_assert((is_same<value_type, typename allocator_type::value_type>::value),
822 "Invalid allocator::value_type");
823 typedef value_type& reference;
824 typedef const value_type& const_reference;
825 typedef typename base::pointer pointer;
826 typedef typename base::const_pointer const_pointer;
827 typedef typename base::size_type size_type;
828 typedef typename base::difference_type difference_type;
829 typedef typename base::iterator iterator;
830 typedef typename base::const_iterator const_iterator;
Howard Hinnant0949eed2011-06-30 21:18:19 +0000831 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
832 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000833
Howard Hinnant82894812010-09-22 16:48:34 +0000834 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000835 list()
836 _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value)
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000837 {
838#if _LIBCPP_DEBUG_LEVEL >= 2
839 __get_db()->__insert_c(this);
840#endif
841 }
Howard Hinnant82894812010-09-22 16:48:34 +0000842 _LIBCPP_INLINE_VISIBILITY
Marshall Clow955f2c82013-09-08 19:11:51 +0000843 explicit list(const allocator_type& __a) : base(__a)
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000844 {
845#if _LIBCPP_DEBUG_LEVEL >= 2
846 __get_db()->__insert_c(this);
847#endif
848 }
Marshall Clow955f2c82013-09-08 19:11:51 +0000849 explicit list(size_type __n);
850#if _LIBCPP_STD_VER > 11
851 explicit list(size_type __n, const allocator_type& __a);
852#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000853 list(size_type __n, const value_type& __x);
854 list(size_type __n, const value_type& __x, const allocator_type& __a);
855 template <class _InpIter>
856 list(_InpIter __f, _InpIter __l,
857 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
858 template <class _InpIter>
859 list(_InpIter __f, _InpIter __l, const allocator_type& __a,
860 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
861
862 list(const list& __c);
863 list(const list& __c, const allocator_type& __a);
864 list& operator=(const list& __c);
Howard Hinnante3e32912011-08-12 21:56:02 +0000865#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000866 list(initializer_list<value_type> __il);
867 list(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnante3e32912011-08-12 21:56:02 +0000868#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant73d21a42010-09-04 23:28:19 +0000869#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc5607272011-06-03 17:30:28 +0000870 list(list&& __c)
871 _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000872 list(list&& __c, const allocator_type& __a);
Howard Hinnantc5607272011-06-03 17:30:28 +0000873 list& operator=(list&& __c)
874 _NOEXCEPT_(
875 __node_alloc_traits::propagate_on_container_move_assignment::value &&
876 is_nothrow_move_assignable<__node_allocator>::value);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000877#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnante3e32912011-08-12 21:56:02 +0000878#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant82894812010-09-22 16:48:34 +0000879 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000880 list& operator=(initializer_list<value_type> __il)
881 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnante3e32912011-08-12 21:56:02 +0000882#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000883
884 template <class _InpIter>
885 void assign(_InpIter __f, _InpIter __l,
886 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
887 void assign(size_type __n, const value_type& __x);
Howard Hinnante3e32912011-08-12 21:56:02 +0000888#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant82894812010-09-22 16:48:34 +0000889 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000890 void assign(initializer_list<value_type> __il)
891 {assign(__il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +0000892#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000893
Howard Hinnantc5607272011-06-03 17:30:28 +0000894 allocator_type get_allocator() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000895
Howard Hinnant82894812010-09-22 16:48:34 +0000896 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000897 size_type size() const _NOEXCEPT {return base::__sz();}
Howard Hinnant82894812010-09-22 16:48:34 +0000898 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000899 bool empty() const _NOEXCEPT {return base::empty();}
Howard Hinnant82894812010-09-22 16:48:34 +0000900 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000901 size_type max_size() const _NOEXCEPT
902 {return numeric_limits<difference_type>::max();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000903
Howard Hinnant82894812010-09-22 16:48:34 +0000904 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000905 iterator begin() _NOEXCEPT {return base::begin();}
Howard Hinnant82894812010-09-22 16:48:34 +0000906 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000907 const_iterator begin() const _NOEXCEPT {return base::begin();}
Howard Hinnant82894812010-09-22 16:48:34 +0000908 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000909 iterator end() _NOEXCEPT {return base::end();}
Howard Hinnant82894812010-09-22 16:48:34 +0000910 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000911 const_iterator end() const _NOEXCEPT {return base::end();}
Howard Hinnant82894812010-09-22 16:48:34 +0000912 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000913 const_iterator cbegin() const _NOEXCEPT {return base::begin();}
Howard Hinnant82894812010-09-22 16:48:34 +0000914 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000915 const_iterator cend() const _NOEXCEPT {return base::end();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000916
Howard Hinnant82894812010-09-22 16:48:34 +0000917 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000918 reverse_iterator rbegin() _NOEXCEPT
919 {return reverse_iterator(end());}
Howard Hinnant82894812010-09-22 16:48:34 +0000920 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000921 const_reverse_iterator rbegin() const _NOEXCEPT
922 {return const_reverse_iterator(end());}
Howard Hinnant82894812010-09-22 16:48:34 +0000923 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000924 reverse_iterator rend() _NOEXCEPT
925 {return reverse_iterator(begin());}
Howard Hinnant82894812010-09-22 16:48:34 +0000926 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000927 const_reverse_iterator rend() const _NOEXCEPT
928 {return const_reverse_iterator(begin());}
Howard Hinnant82894812010-09-22 16:48:34 +0000929 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000930 const_reverse_iterator crbegin() const _NOEXCEPT
931 {return const_reverse_iterator(end());}
Howard Hinnant82894812010-09-22 16:48:34 +0000932 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000933 const_reverse_iterator crend() const _NOEXCEPT
934 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000935
Howard Hinnant82894812010-09-22 16:48:34 +0000936 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000937 reference front()
938 {
939 _LIBCPP_ASSERT(!empty(), "list::front called on empty list");
940 return base::__end_.__next_->__value_;
941 }
Howard Hinnant82894812010-09-22 16:48:34 +0000942 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000943 const_reference front() const
944 {
945 _LIBCPP_ASSERT(!empty(), "list::front called on empty list");
946 return base::__end_.__next_->__value_;
947 }
Howard Hinnant82894812010-09-22 16:48:34 +0000948 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000949 reference back()
950 {
951 _LIBCPP_ASSERT(!empty(), "list::back called on empty list");
952 return base::__end_.__prev_->__value_;
953 }
Howard Hinnant82894812010-09-22 16:48:34 +0000954 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000955 const_reference back() const
956 {
957 _LIBCPP_ASSERT(!empty(), "list::back called on empty list");
958 return base::__end_.__prev_->__value_;
959 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000960
Howard Hinnant73d21a42010-09-04 23:28:19 +0000961#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000962 void push_front(value_type&& __x);
963 void push_back(value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000964#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000965 template <class... _Args>
966 void emplace_front(_Args&&... __args);
967 template <class... _Args>
968 void emplace_back(_Args&&... __args);
969 template <class... _Args>
970 iterator emplace(const_iterator __p, _Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000971#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000972 iterator insert(const_iterator __p, value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000973#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000974
975 void push_front(const value_type& __x);
976 void push_back(const value_type& __x);
977
978 iterator insert(const_iterator __p, const value_type& __x);
979 iterator insert(const_iterator __p, size_type __n, const value_type& __x);
980 template <class _InpIter>
981 iterator insert(const_iterator __p, _InpIter __f, _InpIter __l,
982 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
Howard Hinnante3e32912011-08-12 21:56:02 +0000983#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant82894812010-09-22 16:48:34 +0000984 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000985 iterator insert(const_iterator __p, initializer_list<value_type> __il)
986 {return insert(__p, __il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +0000987#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000988
Howard Hinnant82894812010-09-22 16:48:34 +0000989 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000990 void swap(list& __c)
991 _NOEXCEPT_(!__node_alloc_traits::propagate_on_container_swap::value ||
992 __is_nothrow_swappable<__node_allocator>::value)
993 {base::swap(__c);}
Howard Hinnant82894812010-09-22 16:48:34 +0000994 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000995 void clear() _NOEXCEPT {base::clear();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000996
997 void pop_front();
998 void pop_back();
999
1000 iterator erase(const_iterator __p);
1001 iterator erase(const_iterator __f, const_iterator __l);
1002
1003 void resize(size_type __n);
1004 void resize(size_type __n, const value_type& __x);
1005
1006 void splice(const_iterator __p, list& __c);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001007#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant82894812010-09-22 16:48:34 +00001008 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001009 void splice(const_iterator __p, list&& __c) {splice(__p, __c);}
1010#endif
1011 void splice(const_iterator __p, list& __c, const_iterator __i);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001012#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant82894812010-09-22 16:48:34 +00001013 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001014 void splice(const_iterator __p, list&& __c, const_iterator __i)
1015 {splice(__p, __c, __i);}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001016#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001017 void splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001018#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant82894812010-09-22 16:48:34 +00001019 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001020 void splice(const_iterator __p, list&& __c, const_iterator __f, const_iterator __l)
1021 {splice(__p, __c, __f, __l);}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001022#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001023
1024 void remove(const value_type& __x);
1025 template <class _Pred> void remove_if(_Pred __pred);
1026 void unique();
1027 template <class _BinaryPred>
1028 void unique(_BinaryPred __binary_pred);
1029 void merge(list& __c);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001030#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant82894812010-09-22 16:48:34 +00001031 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001032 void merge(list&& __c) {merge(__c);}
1033#endif
1034 template <class _Comp>
1035 void merge(list& __c, _Comp __comp);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001036#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001037 template <class _Comp>
Howard Hinnant82894812010-09-22 16:48:34 +00001038 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001039 void merge(list&& __c, _Comp __comp) {merge(__c, __comp);}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001040#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001041 void sort();
1042 template <class _Comp>
1043 void sort(_Comp __comp);
1044
Howard Hinnantc5607272011-06-03 17:30:28 +00001045 void reverse() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001046
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001047 bool __invariants() const;
1048
1049#if _LIBCPP_DEBUG_LEVEL >= 2
1050
1051 bool __dereferenceable(const const_iterator* __i) const;
1052 bool __decrementable(const const_iterator* __i) const;
1053 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1054 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1055
1056#endif // _LIBCPP_DEBUG_LEVEL >= 2
1057
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001058private:
Marshall Clowea8ed832014-08-05 01:34:12 +00001059 static void __link_nodes (__node_pointer __p, __node_pointer __f, __node_pointer __l);
1060 void __link_nodes_at_front(__node_pointer __f, __node_pointer __l);
1061 void __link_nodes_at_back (__node_pointer __f, __node_pointer __l);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001062 iterator __iterator(size_type __n);
1063 template <class _Comp>
1064 static iterator __sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp);
1065
Howard Hinnantc5607272011-06-03 17:30:28 +00001066 void __move_assign(list& __c, true_type)
1067 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001068 void __move_assign(list& __c, false_type);
1069};
1070
1071// Link in nodes [__f, __l] just prior to __p
1072template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001073inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001074void
Howard Hinnant29f74322013-06-25 16:08:47 +00001075list<_Tp, _Alloc>::__link_nodes(__node_pointer __p, __node_pointer __f, __node_pointer __l)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001076{
Howard Hinnant29f74322013-06-25 16:08:47 +00001077 __p->__prev_->__next_ = __f;
1078 __f->__prev_ = __p->__prev_;
1079 __p->__prev_ = __l;
1080 __l->__next_ = __p;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001081}
1082
Marshall Clowea8ed832014-08-05 01:34:12 +00001083// Link in nodes [__f, __l] at the front of the list
1084template <class _Tp, class _Alloc>
1085inline _LIBCPP_INLINE_VISIBILITY
1086void
1087list<_Tp, _Alloc>::__link_nodes_at_front(__node_pointer __f, __node_pointer __l)
1088{
1089 __f->__prev_ = base::__end_.__self();
1090 __l->__next_ = base::__end_.__next_;
1091 __l->__next_->__prev_ = __l;
1092 base::__end_.__next_ = __f;
1093}
1094
1095// Link in nodes [__f, __l] at the front of the list
1096template <class _Tp, class _Alloc>
1097inline _LIBCPP_INLINE_VISIBILITY
1098void
1099list<_Tp, _Alloc>::__link_nodes_at_back(__node_pointer __f, __node_pointer __l)
1100{
1101 __l->__next_ = base::__end_.__self();
1102 __f->__prev_ = base::__end_.__prev_;
1103 __f->__prev_->__next_ = __f;
1104 base::__end_.__prev_ = __l;
1105}
1106
1107
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001108template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001109inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001110typename list<_Tp, _Alloc>::iterator
1111list<_Tp, _Alloc>::__iterator(size_type __n)
1112{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001113 return __n <= base::__sz() / 2 ? _VSTD::next(begin(), __n)
1114 : _VSTD::prev(end(), base::__sz() - __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001115}
1116
1117template <class _Tp, class _Alloc>
1118list<_Tp, _Alloc>::list(size_type __n)
1119{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001120#if _LIBCPP_DEBUG_LEVEL >= 2
1121 __get_db()->__insert_c(this);
1122#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001123 for (; __n > 0; --__n)
Howard Hinnant73d21a42010-09-04 23:28:19 +00001124#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001125 emplace_back();
1126#else
1127 push_back(value_type());
1128#endif
1129}
1130
Marshall Clow955f2c82013-09-08 19:11:51 +00001131#if _LIBCPP_STD_VER > 11
1132template <class _Tp, class _Alloc>
1133list<_Tp, _Alloc>::list(size_type __n, const allocator_type& __a) : base(__a)
1134{
1135#if _LIBCPP_DEBUG_LEVEL >= 2
1136 __get_db()->__insert_c(this);
1137#endif
1138 for (; __n > 0; --__n)
1139#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1140 emplace_back();
1141#else
1142 push_back(value_type());
1143#endif
1144}
1145#endif
1146
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001147template <class _Tp, class _Alloc>
1148list<_Tp, _Alloc>::list(size_type __n, const value_type& __x)
1149{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001150#if _LIBCPP_DEBUG_LEVEL >= 2
1151 __get_db()->__insert_c(this);
1152#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001153 for (; __n > 0; --__n)
1154 push_back(__x);
1155}
1156
1157template <class _Tp, class _Alloc>
1158list<_Tp, _Alloc>::list(size_type __n, const value_type& __x, const allocator_type& __a)
1159 : base(__a)
1160{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001161#if _LIBCPP_DEBUG_LEVEL >= 2
1162 __get_db()->__insert_c(this);
1163#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001164 for (; __n > 0; --__n)
1165 push_back(__x);
1166}
1167
1168template <class _Tp, class _Alloc>
1169template <class _InpIter>
1170list<_Tp, _Alloc>::list(_InpIter __f, _InpIter __l,
1171 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1172{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001173#if _LIBCPP_DEBUG_LEVEL >= 2
1174 __get_db()->__insert_c(this);
1175#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001176 for (; __f != __l; ++__f)
1177 push_back(*__f);
1178}
1179
1180template <class _Tp, class _Alloc>
1181template <class _InpIter>
1182list<_Tp, _Alloc>::list(_InpIter __f, _InpIter __l, const allocator_type& __a,
1183 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1184 : base(__a)
1185{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001186#if _LIBCPP_DEBUG_LEVEL >= 2
1187 __get_db()->__insert_c(this);
1188#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001189 for (; __f != __l; ++__f)
1190 push_back(*__f);
1191}
1192
1193template <class _Tp, class _Alloc>
1194list<_Tp, _Alloc>::list(const list& __c)
1195 : base(allocator_type(
1196 __node_alloc_traits::select_on_container_copy_construction(
1197 __c.__node_alloc())))
1198{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001199#if _LIBCPP_DEBUG_LEVEL >= 2
1200 __get_db()->__insert_c(this);
1201#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001202 for (const_iterator __i = __c.begin(), __e = __c.end(); __i != __e; ++__i)
1203 push_back(*__i);
1204}
1205
1206template <class _Tp, class _Alloc>
1207list<_Tp, _Alloc>::list(const list& __c, const allocator_type& __a)
1208 : base(__a)
1209{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001210#if _LIBCPP_DEBUG_LEVEL >= 2
1211 __get_db()->__insert_c(this);
1212#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001213 for (const_iterator __i = __c.begin(), __e = __c.end(); __i != __e; ++__i)
1214 push_back(*__i);
1215}
1216
Howard Hinnante3e32912011-08-12 21:56:02 +00001217#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1218
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001219template <class _Tp, class _Alloc>
1220list<_Tp, _Alloc>::list(initializer_list<value_type> __il, const allocator_type& __a)
1221 : base(__a)
1222{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001223#if _LIBCPP_DEBUG_LEVEL >= 2
1224 __get_db()->__insert_c(this);
1225#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001226 for (typename initializer_list<value_type>::const_iterator __i = __il.begin(),
1227 __e = __il.end(); __i != __e; ++__i)
1228 push_back(*__i);
1229}
1230
1231template <class _Tp, class _Alloc>
1232list<_Tp, _Alloc>::list(initializer_list<value_type> __il)
1233{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001234#if _LIBCPP_DEBUG_LEVEL >= 2
1235 __get_db()->__insert_c(this);
1236#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001237 for (typename initializer_list<value_type>::const_iterator __i = __il.begin(),
1238 __e = __il.end(); __i != __e; ++__i)
1239 push_back(*__i);
1240}
1241
Howard Hinnante3e32912011-08-12 21:56:02 +00001242#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1243
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001244template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001245inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001246list<_Tp, _Alloc>&
1247list<_Tp, _Alloc>::operator=(const list& __c)
1248{
1249 if (this != &__c)
1250 {
1251 base::__copy_assign_alloc(__c);
1252 assign(__c.begin(), __c.end());
1253 }
1254 return *this;
1255}
1256
Howard Hinnant73d21a42010-09-04 23:28:19 +00001257#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001258
1259template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001260inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001261list<_Tp, _Alloc>::list(list&& __c)
Howard Hinnantc5607272011-06-03 17:30:28 +00001262 _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001263 : base(allocator_type(_VSTD::move(__c.__node_alloc())))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001264{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001265#if _LIBCPP_DEBUG_LEVEL >= 2
1266 __get_db()->__insert_c(this);
1267#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001268 splice(end(), __c);
1269}
1270
1271template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001272inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001273list<_Tp, _Alloc>::list(list&& __c, const allocator_type& __a)
1274 : base(__a)
1275{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001276#if _LIBCPP_DEBUG_LEVEL >= 2
1277 __get_db()->__insert_c(this);
1278#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001279 if (__a == __c.get_allocator())
1280 splice(end(), __c);
1281 else
1282 {
Howard Hinnant99968442011-11-29 18:15:50 +00001283 typedef move_iterator<iterator> _Ip;
1284 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001285 }
1286}
1287
1288template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001289inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001290list<_Tp, _Alloc>&
1291list<_Tp, _Alloc>::operator=(list&& __c)
Howard Hinnantc5607272011-06-03 17:30:28 +00001292 _NOEXCEPT_(
1293 __node_alloc_traits::propagate_on_container_move_assignment::value &&
1294 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001295{
1296 __move_assign(__c, integral_constant<bool,
1297 __node_alloc_traits::propagate_on_container_move_assignment::value>());
1298 return *this;
1299}
1300
1301template <class _Tp, class _Alloc>
1302void
1303list<_Tp, _Alloc>::__move_assign(list& __c, false_type)
1304{
1305 if (base::__node_alloc() != __c.__node_alloc())
1306 {
Howard Hinnant99968442011-11-29 18:15:50 +00001307 typedef move_iterator<iterator> _Ip;
1308 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001309 }
1310 else
1311 __move_assign(__c, true_type());
1312}
1313
1314template <class _Tp, class _Alloc>
1315void
1316list<_Tp, _Alloc>::__move_assign(list& __c, true_type)
Howard Hinnantc5607272011-06-03 17:30:28 +00001317 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001318{
1319 clear();
1320 base::__move_assign_alloc(__c);
1321 splice(end(), __c);
1322}
1323
Howard Hinnant73d21a42010-09-04 23:28:19 +00001324#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001325
1326template <class _Tp, class _Alloc>
1327template <class _InpIter>
1328void
1329list<_Tp, _Alloc>::assign(_InpIter __f, _InpIter __l,
1330 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1331{
1332 iterator __i = begin();
1333 iterator __e = end();
1334 for (; __f != __l && __i != __e; ++__f, ++__i)
1335 *__i = *__f;
1336 if (__i == __e)
1337 insert(__e, __f, __l);
1338 else
1339 erase(__i, __e);
1340}
1341
1342template <class _Tp, class _Alloc>
1343void
1344list<_Tp, _Alloc>::assign(size_type __n, const value_type& __x)
1345{
1346 iterator __i = begin();
1347 iterator __e = end();
1348 for (; __n > 0 && __i != __e; --__n, ++__i)
1349 *__i = __x;
1350 if (__i == __e)
1351 insert(__e, __n, __x);
1352 else
1353 erase(__i, __e);
1354}
1355
1356template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001357inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001358_Alloc
Howard Hinnantc5607272011-06-03 17:30:28 +00001359list<_Tp, _Alloc>::get_allocator() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001360{
1361 return allocator_type(base::__node_alloc());
1362}
1363
1364template <class _Tp, class _Alloc>
1365typename list<_Tp, _Alloc>::iterator
1366list<_Tp, _Alloc>::insert(const_iterator __p, const value_type& __x)
1367{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001368#if _LIBCPP_DEBUG_LEVEL >= 2
1369 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1370 "list::insert(iterator, x) called with an iterator not"
1371 " referring to this list");
1372#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001373 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001374 typedef __allocator_destructor<__node_allocator> _Dp;
1375 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001376 __hold->__prev_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001377 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnant29f74322013-06-25 16:08:47 +00001378 __link_nodes(__p.__ptr_, __hold.get(), __hold.get());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001379 ++base::__sz();
Howard Hinnant79a35572013-04-05 00:18:49 +00001380#if _LIBCPP_DEBUG_LEVEL >= 2
1381 return iterator(__hold.release(), this);
1382#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001383 return iterator(__hold.release());
Howard Hinnant79a35572013-04-05 00:18:49 +00001384#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001385}
1386
1387template <class _Tp, class _Alloc>
1388typename list<_Tp, _Alloc>::iterator
1389list<_Tp, _Alloc>::insert(const_iterator __p, size_type __n, const value_type& __x)
1390{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001391#if _LIBCPP_DEBUG_LEVEL >= 2
1392 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1393 "list::insert(iterator, n, x) called with an iterator not"
1394 " referring to this list");
Howard Hinnant29f74322013-06-25 16:08:47 +00001395 iterator __r(__p.__ptr_, this);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001396#else
Howard Hinnant29f74322013-06-25 16:08:47 +00001397 iterator __r(__p.__ptr_);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001398#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001399 if (__n > 0)
1400 {
1401 size_type __ds = 0;
1402 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001403 typedef __allocator_destructor<__node_allocator> _Dp;
1404 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001405 __hold->__prev_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001406 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001407 ++__ds;
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001408#if _LIBCPP_DEBUG_LEVEL >= 2
1409 __r = iterator(__hold.get(), this);
1410#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001411 __r = iterator(__hold.get());
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001412#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001413 __hold.release();
1414 iterator __e = __r;
1415#ifndef _LIBCPP_NO_EXCEPTIONS
1416 try
1417 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001418#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001419 for (--__n; __n != 0; --__n, ++__e, ++__ds)
1420 {
1421 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001422 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001423 __e.__ptr_->__next_ = __hold.get();
1424 __hold->__prev_ = __e.__ptr_;
1425 __hold.release();
1426 }
1427#ifndef _LIBCPP_NO_EXCEPTIONS
1428 }
1429 catch (...)
1430 {
1431 while (true)
1432 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001433 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001434 __node_pointer __prev = __e.__ptr_->__prev_;
1435 __node_alloc_traits::deallocate(__na, __e.__ptr_, 1);
1436 if (__prev == 0)
1437 break;
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001438#if _LIBCPP_DEBUG_LEVEL >= 2
1439 __e = iterator(__prev, this);
1440#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001441 __e = iterator(__prev);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001442#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001443 }
1444 throw;
1445 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001446#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant29f74322013-06-25 16:08:47 +00001447 __link_nodes(__p.__ptr_, __r.__ptr_, __e.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001448 base::__sz() += __ds;
1449 }
1450 return __r;
1451}
1452
1453template <class _Tp, class _Alloc>
1454template <class _InpIter>
1455typename list<_Tp, _Alloc>::iterator
1456list<_Tp, _Alloc>::insert(const_iterator __p, _InpIter __f, _InpIter __l,
1457 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1458{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001459#if _LIBCPP_DEBUG_LEVEL >= 2
1460 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1461 "list::insert(iterator, range) called with an iterator not"
1462 " referring to this list");
Howard Hinnant29f74322013-06-25 16:08:47 +00001463 iterator __r(__p.__ptr_, this);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001464#else
Howard Hinnant29f74322013-06-25 16:08:47 +00001465 iterator __r(__p.__ptr_);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001466#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001467 if (__f != __l)
1468 {
1469 size_type __ds = 0;
1470 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001471 typedef __allocator_destructor<__node_allocator> _Dp;
1472 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001473 __hold->__prev_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001474 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), *__f);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001475 ++__ds;
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001476#if _LIBCPP_DEBUG_LEVEL >= 2
1477 __r = iterator(__hold.get(), this);
1478#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001479 __r = iterator(__hold.get());
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001480#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001481 __hold.release();
1482 iterator __e = __r;
1483#ifndef _LIBCPP_NO_EXCEPTIONS
1484 try
1485 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001486#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001487 for (++__f; __f != __l; ++__f, ++__e, ++__ds)
1488 {
1489 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001490 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), *__f);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001491 __e.__ptr_->__next_ = __hold.get();
1492 __hold->__prev_ = __e.__ptr_;
1493 __hold.release();
1494 }
1495#ifndef _LIBCPP_NO_EXCEPTIONS
1496 }
1497 catch (...)
1498 {
1499 while (true)
1500 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001501 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001502 __node_pointer __prev = __e.__ptr_->__prev_;
1503 __node_alloc_traits::deallocate(__na, __e.__ptr_, 1);
1504 if (__prev == 0)
1505 break;
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001506#if _LIBCPP_DEBUG_LEVEL >= 2
1507 __e = iterator(__prev, this);
1508#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001509 __e = iterator(__prev);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001510#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001511 }
1512 throw;
1513 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001514#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant29f74322013-06-25 16:08:47 +00001515 __link_nodes(__p.__ptr_, __r.__ptr_, __e.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001516 base::__sz() += __ds;
1517 }
1518 return __r;
1519}
1520
1521template <class _Tp, class _Alloc>
1522void
1523list<_Tp, _Alloc>::push_front(const value_type& __x)
1524{
1525 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001526 typedef __allocator_destructor<__node_allocator> _Dp;
1527 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001528 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Marshall Clowea8ed832014-08-05 01:34:12 +00001529 __link_nodes_at_front(__hold.get(), __hold.get());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001530 ++base::__sz();
1531 __hold.release();
1532}
1533
1534template <class _Tp, class _Alloc>
1535void
1536list<_Tp, _Alloc>::push_back(const value_type& __x)
1537{
1538 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001539 typedef __allocator_destructor<__node_allocator> _Dp;
1540 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001541 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Marshall Clowea8ed832014-08-05 01:34:12 +00001542 __link_nodes_at_back(__hold.get(), __hold.get());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001543 ++base::__sz();
1544 __hold.release();
1545}
1546
Howard Hinnant73d21a42010-09-04 23:28:19 +00001547#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001548
1549template <class _Tp, class _Alloc>
1550void
1551list<_Tp, _Alloc>::push_front(value_type&& __x)
1552{
1553 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001554 typedef __allocator_destructor<__node_allocator> _Dp;
1555 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001556 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
Marshall Clowea8ed832014-08-05 01:34:12 +00001557 __link_nodes_at_front(__hold.get(), __hold.get());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001558 ++base::__sz();
1559 __hold.release();
1560}
1561
1562template <class _Tp, class _Alloc>
1563void
1564list<_Tp, _Alloc>::push_back(value_type&& __x)
1565{
1566 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-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 Hinnant0949eed2011-06-30 21:18:19 +00001569 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
Marshall Clowea8ed832014-08-05 01:34:12 +00001570 __link_nodes_at_back(__hold.get(), __hold.get());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001571 ++base::__sz();
1572 __hold.release();
1573}
1574
Howard Hinnant73d21a42010-09-04 23:28:19 +00001575#ifndef _LIBCPP_HAS_NO_VARIADICS
1576
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001577template <class _Tp, class _Alloc>
1578template <class... _Args>
1579void
1580list<_Tp, _Alloc>::emplace_front(_Args&&... __args)
1581{
1582 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001583 typedef __allocator_destructor<__node_allocator> _Dp;
1584 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001585 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
Marshall Clowea8ed832014-08-05 01:34:12 +00001586 __link_nodes_at_front(__hold.get(), __hold.get());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001587 ++base::__sz();
1588 __hold.release();
1589}
1590
1591template <class _Tp, class _Alloc>
1592template <class... _Args>
1593void
1594list<_Tp, _Alloc>::emplace_back(_Args&&... __args)
1595{
1596 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001597 typedef __allocator_destructor<__node_allocator> _Dp;
1598 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001599 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
Marshall Clowea8ed832014-08-05 01:34:12 +00001600 __link_nodes_at_back(__hold.get(), __hold.get());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001601 ++base::__sz();
1602 __hold.release();
1603}
1604
1605template <class _Tp, class _Alloc>
1606template <class... _Args>
1607typename list<_Tp, _Alloc>::iterator
1608list<_Tp, _Alloc>::emplace(const_iterator __p, _Args&&... __args)
1609{
Howard Hinnant79a35572013-04-05 00:18:49 +00001610#if _LIBCPP_DEBUG_LEVEL >= 2
1611 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1612 "list::emplace(iterator, args...) called with an iterator not"
1613 " referring to this list");
1614#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001615 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001616 typedef __allocator_destructor<__node_allocator> _Dp;
1617 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001618 __hold->__prev_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001619 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
Howard Hinnant29f74322013-06-25 16:08:47 +00001620 __link_nodes(__p.__ptr_, __hold.get(), __hold.get());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001621 ++base::__sz();
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001622#if _LIBCPP_DEBUG_LEVEL >= 2
1623 return iterator(__hold.release(), this);
1624#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001625 return iterator(__hold.release());
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001626#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001627}
1628
Howard Hinnant73d21a42010-09-04 23:28:19 +00001629#endif // _LIBCPP_HAS_NO_VARIADICS
1630
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001631template <class _Tp, class _Alloc>
1632typename list<_Tp, _Alloc>::iterator
1633list<_Tp, _Alloc>::insert(const_iterator __p, value_type&& __x)
1634{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001635#if _LIBCPP_DEBUG_LEVEL >= 2
1636 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1637 "list::insert(iterator, x) called with an iterator not"
1638 " referring to this list");
1639#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001640 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001641 typedef __allocator_destructor<__node_allocator> _Dp;
1642 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001643 __hold->__prev_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001644 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
Howard Hinnant29f74322013-06-25 16:08:47 +00001645 __link_nodes(__p.__ptr_, __hold.get(), __hold.get());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001646 ++base::__sz();
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001647#if _LIBCPP_DEBUG_LEVEL >= 2
1648 return iterator(__hold.release(), this);
1649#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001650 return iterator(__hold.release());
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001651#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001652}
1653
Howard Hinnant73d21a42010-09-04 23:28:19 +00001654#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001655
1656template <class _Tp, class _Alloc>
1657void
1658list<_Tp, _Alloc>::pop_front()
1659{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001660 _LIBCPP_ASSERT(!empty(), "list::pop_front() called with empty list");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001661 __node_allocator& __na = base::__node_alloc();
Howard Hinnant29f74322013-06-25 16:08:47 +00001662 __node_pointer __n = base::__end_.__next_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001663 base::__unlink_nodes(__n, __n);
1664 --base::__sz();
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001665#if _LIBCPP_DEBUG_LEVEL >= 2
1666 __c_node* __c = __get_db()->__find_c_and_lock(this);
1667 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1668 {
1669 --__p;
1670 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant29f74322013-06-25 16:08:47 +00001671 if (__i->__ptr_ == __n)
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001672 {
1673 (*__p)->__c_ = nullptr;
1674 if (--__c->end_ != __p)
1675 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1676 }
1677 }
1678 __get_db()->unlock();
1679#endif
Howard Hinnant29f74322013-06-25 16:08:47 +00001680 __node_alloc_traits::destroy(__na, _VSTD::addressof(__n->__value_));
1681 __node_alloc_traits::deallocate(__na, __n, 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001682}
1683
1684template <class _Tp, class _Alloc>
1685void
1686list<_Tp, _Alloc>::pop_back()
1687{
Dmitri Gribenkoc7a39cf2013-06-24 06:15:57 +00001688 _LIBCPP_ASSERT(!empty(), "list::pop_back() called with empty list");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001689 __node_allocator& __na = base::__node_alloc();
Howard Hinnant29f74322013-06-25 16:08:47 +00001690 __node_pointer __n = base::__end_.__prev_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001691 base::__unlink_nodes(__n, __n);
1692 --base::__sz();
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001693#if _LIBCPP_DEBUG_LEVEL >= 2
1694 __c_node* __c = __get_db()->__find_c_and_lock(this);
1695 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1696 {
1697 --__p;
1698 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant29f74322013-06-25 16:08:47 +00001699 if (__i->__ptr_ == __n)
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001700 {
1701 (*__p)->__c_ = nullptr;
1702 if (--__c->end_ != __p)
1703 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1704 }
1705 }
1706 __get_db()->unlock();
1707#endif
Howard Hinnant29f74322013-06-25 16:08:47 +00001708 __node_alloc_traits::destroy(__na, _VSTD::addressof(__n->__value_));
1709 __node_alloc_traits::deallocate(__na, __n, 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001710}
1711
1712template <class _Tp, class _Alloc>
1713typename list<_Tp, _Alloc>::iterator
1714list<_Tp, _Alloc>::erase(const_iterator __p)
1715{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001716#if _LIBCPP_DEBUG_LEVEL >= 2
1717 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1718 "list::erase(iterator) called with an iterator not"
1719 " referring to this list");
1720#endif
Howard Hinnant79a35572013-04-05 00:18:49 +00001721 _LIBCPP_ASSERT(__p != end(),
1722 "list::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001723 __node_allocator& __na = base::__node_alloc();
Howard Hinnant29f74322013-06-25 16:08:47 +00001724 __node_pointer __n = __p.__ptr_;
1725 __node_pointer __r = __n->__next_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001726 base::__unlink_nodes(__n, __n);
1727 --base::__sz();
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001728#if _LIBCPP_DEBUG_LEVEL >= 2
1729 __c_node* __c = __get_db()->__find_c_and_lock(this);
1730 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1731 {
1732 --__p;
1733 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant29f74322013-06-25 16:08:47 +00001734 if (__i->__ptr_ == __n)
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001735 {
1736 (*__p)->__c_ = nullptr;
1737 if (--__c->end_ != __p)
1738 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1739 }
1740 }
1741 __get_db()->unlock();
1742#endif
Howard Hinnant29f74322013-06-25 16:08:47 +00001743 __node_alloc_traits::destroy(__na, _VSTD::addressof(__n->__value_));
1744 __node_alloc_traits::deallocate(__na, __n, 1);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001745#if _LIBCPP_DEBUG_LEVEL >= 2
1746 return iterator(__r, this);
1747#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001748 return iterator(__r);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001749#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001750}
1751
1752template <class _Tp, class _Alloc>
1753typename list<_Tp, _Alloc>::iterator
1754list<_Tp, _Alloc>::erase(const_iterator __f, const_iterator __l)
1755{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001756#if _LIBCPP_DEBUG_LEVEL >= 2
1757 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__f) == this,
1758 "list::erase(iterator, iterator) called with an iterator not"
1759 " referring to this list");
1760#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001761 if (__f != __l)
1762 {
1763 __node_allocator& __na = base::__node_alloc();
Howard Hinnant29f74322013-06-25 16:08:47 +00001764 base::__unlink_nodes(__f.__ptr_, __l.__ptr_->__prev_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001765 while (__f != __l)
1766 {
Howard Hinnant29f74322013-06-25 16:08:47 +00001767 __node_pointer __n = __f.__ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001768 ++__f;
1769 --base::__sz();
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001770#if _LIBCPP_DEBUG_LEVEL >= 2
1771 __c_node* __c = __get_db()->__find_c_and_lock(this);
1772 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1773 {
1774 --__p;
1775 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant29f74322013-06-25 16:08:47 +00001776 if (__i->__ptr_ == __n)
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001777 {
1778 (*__p)->__c_ = nullptr;
1779 if (--__c->end_ != __p)
1780 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1781 }
1782 }
1783 __get_db()->unlock();
1784#endif
Howard Hinnant29f74322013-06-25 16:08:47 +00001785 __node_alloc_traits::destroy(__na, _VSTD::addressof(__n->__value_));
1786 __node_alloc_traits::deallocate(__na, __n, 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001787 }
1788 }
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001789#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant29f74322013-06-25 16:08:47 +00001790 return iterator(__l.__ptr_, this);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001791#else
Howard Hinnant29f74322013-06-25 16:08:47 +00001792 return iterator(__l.__ptr_);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001793#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001794}
1795
1796template <class _Tp, class _Alloc>
1797void
1798list<_Tp, _Alloc>::resize(size_type __n)
1799{
1800 if (__n < base::__sz())
1801 erase(__iterator(__n), end());
1802 else if (__n > base::__sz())
1803 {
1804 __n -= base::__sz();
1805 size_type __ds = 0;
1806 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001807 typedef __allocator_destructor<__node_allocator> _Dp;
1808 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001809 __hold->__prev_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001810 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001811 ++__ds;
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001812#if _LIBCPP_DEBUG_LEVEL >= 2
1813 iterator __r = iterator(__hold.release(), this);
1814#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001815 iterator __r = iterator(__hold.release());
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001816#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001817 iterator __e = __r;
1818#ifndef _LIBCPP_NO_EXCEPTIONS
1819 try
1820 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001821#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001822 for (--__n; __n != 0; --__n, ++__e, ++__ds)
1823 {
1824 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001825 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001826 __e.__ptr_->__next_ = __hold.get();
1827 __hold->__prev_ = __e.__ptr_;
1828 __hold.release();
1829 }
1830#ifndef _LIBCPP_NO_EXCEPTIONS
1831 }
1832 catch (...)
1833 {
1834 while (true)
1835 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001836 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001837 __node_pointer __prev = __e.__ptr_->__prev_;
1838 __node_alloc_traits::deallocate(__na, __e.__ptr_, 1);
1839 if (__prev == 0)
1840 break;
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001841#if _LIBCPP_DEBUG_LEVEL >= 2
1842 __e = iterator(__prev, this);
1843#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001844 __e = iterator(__prev);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001845#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001846 }
1847 throw;
1848 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001849#endif // _LIBCPP_NO_EXCEPTIONS
Marshall Clowea8ed832014-08-05 01:34:12 +00001850 __link_nodes_at_back(__r.__ptr_, __e.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001851 base::__sz() += __ds;
1852 }
1853}
1854
1855template <class _Tp, class _Alloc>
1856void
1857list<_Tp, _Alloc>::resize(size_type __n, const value_type& __x)
1858{
1859 if (__n < base::__sz())
1860 erase(__iterator(__n), end());
1861 else if (__n > base::__sz())
1862 {
1863 __n -= base::__sz();
1864 size_type __ds = 0;
1865 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001866 typedef __allocator_destructor<__node_allocator> _Dp;
1867 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001868 __hold->__prev_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001869 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001870 ++__ds;
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001871#if _LIBCPP_DEBUG_LEVEL >= 2
1872 iterator __r = iterator(__hold.release(), this);
1873#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001874 iterator __r = iterator(__hold.release());
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001875#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001876 iterator __e = __r;
1877#ifndef _LIBCPP_NO_EXCEPTIONS
1878 try
1879 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001880#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001881 for (--__n; __n != 0; --__n, ++__e, ++__ds)
1882 {
1883 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001884 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001885 __e.__ptr_->__next_ = __hold.get();
1886 __hold->__prev_ = __e.__ptr_;
1887 __hold.release();
1888 }
1889#ifndef _LIBCPP_NO_EXCEPTIONS
1890 }
1891 catch (...)
1892 {
1893 while (true)
1894 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001895 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001896 __node_pointer __prev = __e.__ptr_->__prev_;
1897 __node_alloc_traits::deallocate(__na, __e.__ptr_, 1);
1898 if (__prev == 0)
1899 break;
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001900#if _LIBCPP_DEBUG_LEVEL >= 2
1901 __e = iterator(__prev, this);
1902#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001903 __e = iterator(__prev);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001904#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001905 }
1906 throw;
1907 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001908#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant29f74322013-06-25 16:08:47 +00001909 __link_nodes(static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::
1910 pointer_to(base::__end_)), __r.__ptr_, __e.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001911 base::__sz() += __ds;
Howard Hinnant324bb032010-08-22 00:02:43 +00001912 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001913}
1914
1915template <class _Tp, class _Alloc>
1916void
1917list<_Tp, _Alloc>::splice(const_iterator __p, list& __c)
1918{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001919 _LIBCPP_ASSERT(this != &__c,
1920 "list::splice(iterator, list) called with this == &list");
1921#if _LIBCPP_DEBUG_LEVEL >= 2
1922 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1923 "list::splice(iterator, list) called with an iterator not"
1924 " referring to this list");
1925#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001926 if (!__c.empty())
1927 {
Howard Hinnant29f74322013-06-25 16:08:47 +00001928 __node_pointer __f = __c.__end_.__next_;
1929 __node_pointer __l = __c.__end_.__prev_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001930 base::__unlink_nodes(__f, __l);
Howard Hinnant29f74322013-06-25 16:08:47 +00001931 __link_nodes(__p.__ptr_, __f, __l);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001932 base::__sz() += __c.__sz();
1933 __c.__sz() = 0;
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001934#if _LIBCPP_DEBUG_LEVEL >= 2
1935 __libcpp_db* __db = __get_db();
1936 __c_node* __cn1 = __db->__find_c_and_lock(this);
1937 __c_node* __cn2 = __db->__find_c(&__c);
1938 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
1939 {
1940 --__p;
1941 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant29f74322013-06-25 16:08:47 +00001942 if (__i->__ptr_ != static_cast<__node_pointer>(
1943 pointer_traits<__node_base_pointer>::pointer_to(__c.__end_)))
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001944 {
1945 __cn1->__add(*__p);
1946 (*__p)->__c_ = __cn1;
1947 if (--__cn2->end_ != __p)
1948 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
1949 }
1950 }
1951 __db->unlock();
1952#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001953 }
1954}
1955
1956template <class _Tp, class _Alloc>
1957void
1958list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __i)
1959{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001960#if _LIBCPP_DEBUG_LEVEL >= 2
1961 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1962 "list::splice(iterator, list, iterator) called with first iterator not"
1963 " referring to this list");
1964 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__i) == &__c,
1965 "list::splice(iterator, list, iterator) called with second iterator not"
1966 " referring to list argument");
1967 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(&__i),
1968 "list::splice(iterator, list, iterator) called with second iterator not"
1969 " derefereceable");
1970#endif
1971 if (__p.__ptr_ != __i.__ptr_ && __p.__ptr_ != __i.__ptr_->__next_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001972 {
Howard Hinnant29f74322013-06-25 16:08:47 +00001973 __node_pointer __f = __i.__ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001974 base::__unlink_nodes(__f, __f);
Howard Hinnant29f74322013-06-25 16:08:47 +00001975 __link_nodes(__p.__ptr_, __f, __f);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001976 --__c.__sz();
1977 ++base::__sz();
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001978#if _LIBCPP_DEBUG_LEVEL >= 2
1979 __libcpp_db* __db = __get_db();
1980 __c_node* __cn1 = __db->__find_c_and_lock(this);
1981 __c_node* __cn2 = __db->__find_c(&__c);
1982 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
1983 {
1984 --__p;
1985 iterator* __j = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant29f74322013-06-25 16:08:47 +00001986 if (__j->__ptr_ == __f)
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001987 {
1988 __cn1->__add(*__p);
1989 (*__p)->__c_ = __cn1;
1990 if (--__cn2->end_ != __p)
1991 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
1992 }
1993 }
1994 __db->unlock();
1995#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001996 }
1997}
1998
1999template <class _Tp, class _Alloc>
2000void
2001list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l)
2002{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00002003#if _LIBCPP_DEBUG_LEVEL >= 2
2004 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2005 "list::splice(iterator, list, iterator, iterator) called with first iterator not"
2006 " referring to this list");
2007 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__f) == &__c,
2008 "list::splice(iterator, list, iterator, iterator) called with second iterator not"
2009 " referring to list argument");
2010 if (this == &__c)
2011 {
2012 for (const_iterator __i = __f; __i != __l; ++__i)
2013 _LIBCPP_ASSERT(__i != __p,
2014 "list::splice(iterator, list, iterator, iterator)"
2015 " called with the first iterator within the range"
2016 " of the second and third iterators");
2017 }
2018#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002019 if (__f != __l)
2020 {
2021 if (this != &__c)
2022 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002023 size_type __s = _VSTD::distance(__f, __l);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002024 __c.__sz() -= __s;
2025 base::__sz() += __s;
2026 }
Howard Hinnant29f74322013-06-25 16:08:47 +00002027 __node_pointer __first = __f.__ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002028 --__l;
Howard Hinnant29f74322013-06-25 16:08:47 +00002029 __node_pointer __last = __l.__ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002030 base::__unlink_nodes(__first, __last);
Howard Hinnant29f74322013-06-25 16:08:47 +00002031 __link_nodes(__p.__ptr_, __first, __last);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00002032#if _LIBCPP_DEBUG_LEVEL >= 2
2033 __libcpp_db* __db = __get_db();
2034 __c_node* __cn1 = __db->__find_c_and_lock(this);
2035 __c_node* __cn2 = __db->__find_c(&__c);
2036 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
2037 {
2038 --__p;
2039 iterator* __j = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant29f74322013-06-25 16:08:47 +00002040 for (__node_pointer __k = __f.__ptr_;
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00002041 __k != __l.__ptr_; __k = __k->__next_)
2042 {
2043 if (__j->__ptr_ == __k)
2044 {
2045 __cn1->__add(*__p);
2046 (*__p)->__c_ = __cn1;
2047 if (--__cn2->end_ != __p)
2048 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
2049 }
2050 }
2051 }
2052 __db->unlock();
2053#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002054 }
2055}
2056
2057template <class _Tp, class _Alloc>
2058void
2059list<_Tp, _Alloc>::remove(const value_type& __x)
2060{
2061 for (iterator __i = begin(), __e = end(); __i != __e;)
2062 {
2063 if (*__i == __x)
2064 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002065 iterator __j = _VSTD::next(__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002066 for (; __j != __e && *__j == __x; ++__j)
2067 ;
2068 __i = erase(__i, __j);
Marshall Clowf0f1bca2014-08-04 17:32:25 +00002069 if (__i != __e)
Marshall Clowea8ed832014-08-05 01:34:12 +00002070 ++__i;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002071 }
2072 else
2073 ++__i;
2074 }
2075}
2076
2077template <class _Tp, class _Alloc>
2078template <class _Pred>
2079void
2080list<_Tp, _Alloc>::remove_if(_Pred __pred)
2081{
2082 for (iterator __i = begin(), __e = end(); __i != __e;)
2083 {
2084 if (__pred(*__i))
2085 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002086 iterator __j = _VSTD::next(__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002087 for (; __j != __e && __pred(*__j); ++__j)
2088 ;
2089 __i = erase(__i, __j);
Marshall Clowf0f1bca2014-08-04 17:32:25 +00002090 if (__i != __e)
Marshall Clowea8ed832014-08-05 01:34:12 +00002091 ++__i;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002092 }
2093 else
2094 ++__i;
2095 }
2096}
2097
2098template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00002099inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002100void
2101list<_Tp, _Alloc>::unique()
2102{
2103 unique(__equal_to<value_type>());
2104}
2105
2106template <class _Tp, class _Alloc>
2107template <class _BinaryPred>
2108void
2109list<_Tp, _Alloc>::unique(_BinaryPred __binary_pred)
2110{
2111 for (iterator __i = begin(), __e = end(); __i != __e;)
2112 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002113 iterator __j = _VSTD::next(__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002114 for (; __j != __e && __binary_pred(*__i, *__j); ++__j)
2115 ;
2116 if (++__i != __j)
2117 __i = erase(__i, __j);
2118 }
2119}
2120
2121template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00002122inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002123void
2124list<_Tp, _Alloc>::merge(list& __c)
2125{
2126 merge(__c, __less<value_type>());
2127}
2128
2129template <class _Tp, class _Alloc>
2130template <class _Comp>
2131void
2132list<_Tp, _Alloc>::merge(list& __c, _Comp __comp)
2133{
2134 if (this != &__c)
2135 {
2136 iterator __f1 = begin();
2137 iterator __e1 = end();
2138 iterator __f2 = __c.begin();
2139 iterator __e2 = __c.end();
2140 while (__f1 != __e1 && __f2 != __e2)
2141 {
2142 if (__comp(*__f2, *__f1))
2143 {
2144 size_type __ds = 1;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002145 iterator __m2 = _VSTD::next(__f2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002146 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2, ++__ds)
2147 ;
2148 base::__sz() += __ds;
2149 __c.__sz() -= __ds;
Howard Hinnant29f74322013-06-25 16:08:47 +00002150 __node_pointer __f = __f2.__ptr_;
2151 __node_pointer __l = __m2.__ptr_->__prev_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002152 __f2 = __m2;
2153 base::__unlink_nodes(__f, __l);
Howard Hinnant0949eed2011-06-30 21:18:19 +00002154 __m2 = _VSTD::next(__f1);
Howard Hinnant29f74322013-06-25 16:08:47 +00002155 __link_nodes(__f1.__ptr_, __f, __l);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002156 __f1 = __m2;
2157 }
2158 else
2159 ++__f1;
2160 }
2161 splice(__e1, __c);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00002162#if _LIBCPP_DEBUG_LEVEL >= 2
2163 __libcpp_db* __db = __get_db();
2164 __c_node* __cn1 = __db->__find_c_and_lock(this);
2165 __c_node* __cn2 = __db->__find_c(&__c);
2166 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
2167 {
2168 --__p;
2169 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant29f74322013-06-25 16:08:47 +00002170 if (__i->__ptr_ != static_cast<__node_pointer>(
2171 pointer_traits<__node_base_pointer>::pointer_to(__c.__end_)))
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00002172 {
2173 __cn1->__add(*__p);
2174 (*__p)->__c_ = __cn1;
2175 if (--__cn2->end_ != __p)
2176 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
2177 }
2178 }
2179 __db->unlock();
2180#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002181 }
2182}
2183
2184template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00002185inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002186void
2187list<_Tp, _Alloc>::sort()
2188{
2189 sort(__less<value_type>());
2190}
2191
2192template <class _Tp, class _Alloc>
2193template <class _Comp>
Howard Hinnant82894812010-09-22 16:48:34 +00002194inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002195void
2196list<_Tp, _Alloc>::sort(_Comp __comp)
2197{
2198 __sort(begin(), end(), base::__sz(), __comp);
2199}
2200
2201template <class _Tp, class _Alloc>
2202template <class _Comp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002203typename list<_Tp, _Alloc>::iterator
2204list<_Tp, _Alloc>::__sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp)
2205{
2206 switch (__n)
2207 {
2208 case 0:
2209 case 1:
2210 return __f1;
2211 case 2:
2212 if (__comp(*--__e2, *__f1))
2213 {
Howard Hinnant29f74322013-06-25 16:08:47 +00002214 __node_pointer __f = __e2.__ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002215 base::__unlink_nodes(__f, __f);
Howard Hinnant29f74322013-06-25 16:08:47 +00002216 __link_nodes(__f1.__ptr_, __f, __f);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002217 return __e2;
2218 }
2219 return __f1;
2220 }
2221 size_type __n2 = __n / 2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002222 iterator __e1 = _VSTD::next(__f1, __n2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002223 iterator __r = __f1 = __sort(__f1, __e1, __n2, __comp);
2224 iterator __f2 = __e1 = __sort(__e1, __e2, __n - __n2, __comp);
2225 if (__comp(*__f2, *__f1))
2226 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002227 iterator __m2 = _VSTD::next(__f2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002228 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2)
2229 ;
Howard Hinnant29f74322013-06-25 16:08:47 +00002230 __node_pointer __f = __f2.__ptr_;
2231 __node_pointer __l = __m2.__ptr_->__prev_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002232 __r = __f2;
2233 __e1 = __f2 = __m2;
2234 base::__unlink_nodes(__f, __l);
Howard Hinnant0949eed2011-06-30 21:18:19 +00002235 __m2 = _VSTD::next(__f1);
Howard Hinnant29f74322013-06-25 16:08:47 +00002236 __link_nodes(__f1.__ptr_, __f, __l);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002237 __f1 = __m2;
2238 }
2239 else
2240 ++__f1;
2241 while (__f1 != __e1 && __f2 != __e2)
2242 {
2243 if (__comp(*__f2, *__f1))
2244 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002245 iterator __m2 = _VSTD::next(__f2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002246 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2)
2247 ;
Howard Hinnant29f74322013-06-25 16:08:47 +00002248 __node_pointer __f = __f2.__ptr_;
2249 __node_pointer __l = __m2.__ptr_->__prev_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002250 if (__e1 == __f2)
2251 __e1 = __m2;
2252 __f2 = __m2;
2253 base::__unlink_nodes(__f, __l);
Howard Hinnant0949eed2011-06-30 21:18:19 +00002254 __m2 = _VSTD::next(__f1);
Howard Hinnant29f74322013-06-25 16:08:47 +00002255 __link_nodes(__f1.__ptr_, __f, __l);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002256 __f1 = __m2;
2257 }
2258 else
2259 ++__f1;
2260 }
2261 return __r;
2262}
2263
2264template <class _Tp, class _Alloc>
2265void
Howard Hinnantc5607272011-06-03 17:30:28 +00002266list<_Tp, _Alloc>::reverse() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002267{
2268 if (base::__sz() > 1)
2269 {
2270 iterator __e = end();
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00002271 for (iterator __i = begin(); __i.__ptr_ != __e.__ptr_;)
2272 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002273 _VSTD::swap(__i.__ptr_->__prev_, __i.__ptr_->__next_);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00002274 __i.__ptr_ = __i.__ptr_->__prev_;
2275 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002276 _VSTD::swap(__e.__ptr_->__prev_, __e.__ptr_->__next_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002277 }
2278}
2279
2280template <class _Tp, class _Alloc>
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00002281bool
2282list<_Tp, _Alloc>::__invariants() const
2283{
2284 return size() == _VSTD::distance(begin(), end());
2285}
2286
2287#if _LIBCPP_DEBUG_LEVEL >= 2
2288
2289template <class _Tp, class _Alloc>
2290bool
2291list<_Tp, _Alloc>::__dereferenceable(const const_iterator* __i) const
2292{
Howard Hinnant29f74322013-06-25 16:08:47 +00002293 return __i->__ptr_ != static_cast<__node_pointer>(
2294 pointer_traits<__node_base_pointer>::pointer_to(const_cast<__node_base&>(this->__end_)));
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00002295}
2296
2297template <class _Tp, class _Alloc>
2298bool
2299list<_Tp, _Alloc>::__decrementable(const const_iterator* __i) const
2300{
2301 return !empty() && __i->__ptr_ != base::__end_.__next_;
2302}
2303
2304template <class _Tp, class _Alloc>
2305bool
2306list<_Tp, _Alloc>::__addable(const const_iterator* __i, ptrdiff_t __n) const
2307{
2308 return false;
2309}
2310
2311template <class _Tp, class _Alloc>
2312bool
2313list<_Tp, _Alloc>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
2314{
2315 return false;
2316}
2317
2318#endif // _LIBCPP_DEBUG_LEVEL >= 2
2319
2320template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00002321inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002322bool
2323operator==(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2324{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002325 return __x.size() == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002326}
2327
2328template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00002329inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002330bool
2331operator< (const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2332{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002333 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002334}
2335
2336template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00002337inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002338bool
2339operator!=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2340{
2341 return !(__x == __y);
2342}
2343
2344template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00002345inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002346bool
2347operator> (const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2348{
2349 return __y < __x;
2350}
2351
2352template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00002353inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002354bool
2355operator>=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2356{
2357 return !(__x < __y);
2358}
2359
2360template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00002361inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002362bool
2363operator<=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2364{
2365 return !(__y < __x);
2366}
2367
2368template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00002369inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002370void
2371swap(list<_Tp, _Alloc>& __x, list<_Tp, _Alloc>& __y)
Howard Hinnantc5607272011-06-03 17:30:28 +00002372 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002373{
2374 __x.swap(__y);
2375}
2376
2377_LIBCPP_END_NAMESPACE_STD
2378
2379#endif // _LIBCPP_LIST