blob: 800a1a3f5aa825d87acb559875e1764486dec659 [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
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000217 __list_node_base()
Howard Hinnant29f74322013-06-25 16:08:47 +0000218 : __prev_(static_cast<pointer>(pointer_traits<__base_pointer>::pointer_to(*this))),
219 __next_(static_cast<pointer>(pointer_traits<__base_pointer>::pointer_to(*this)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000220 {}
221};
222
223template <class _Tp, class _VoidPtr>
224struct __list_node
225 : public __list_node_base<_Tp, _VoidPtr>
226{
227 _Tp __value_;
228};
229
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000230template <class _Tp, class _Alloc> class _LIBCPP_TYPE_VIS_ONLY list;
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000231template <class _Tp, class _Alloc> class __list_imp;
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000232template <class _Tp, class _VoidPtr> class _LIBCPP_TYPE_VIS_ONLY __list_const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000233
234template <class _Tp, class _VoidPtr>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000235class _LIBCPP_TYPE_VIS_ONLY __list_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000236{
237 typedef typename pointer_traits<_VoidPtr>::template
238#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
239 rebind<__list_node<_Tp, _VoidPtr> > __node_pointer;
240#else
241 rebind<__list_node<_Tp, _VoidPtr> >::other __node_pointer;
242#endif
243
244 __node_pointer __ptr_;
245
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000246#if _LIBCPP_DEBUG_LEVEL >= 2
247 _LIBCPP_INLINE_VISIBILITY
248 explicit __list_iterator(__node_pointer __p, const void* __c) _NOEXCEPT
249 : __ptr_(__p)
250 {
251 __get_db()->__insert_ic(this, __c);
252 }
253#else
Howard Hinnant82894812010-09-22 16:48:34 +0000254 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000255 explicit __list_iterator(__node_pointer __p) _NOEXCEPT : __ptr_(__p) {}
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000256#endif
257
258
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000259
260 template<class, class> friend class list;
261 template<class, class> friend class __list_imp;
262 template<class, class> friend class __list_const_iterator;
263public:
264 typedef bidirectional_iterator_tag iterator_category;
265 typedef _Tp value_type;
266 typedef value_type& reference;
267 typedef typename pointer_traits<_VoidPtr>::template
268#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
269 rebind<value_type>
270#else
271 rebind<value_type>::other
272#endif
273 pointer;
274 typedef typename pointer_traits<pointer>::difference_type difference_type;
275
Howard Hinnant82894812010-09-22 16:48:34 +0000276 _LIBCPP_INLINE_VISIBILITY
Marshall Clow65d2e6a2013-08-05 21:23:28 +0000277 __list_iterator() _NOEXCEPT : __ptr_(nullptr)
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000278 {
279#if _LIBCPP_DEBUG_LEVEL >= 2
280 __get_db()->__insert_i(this);
281#endif
282 }
283
284#if _LIBCPP_DEBUG_LEVEL >= 2
285
Howard Hinnant211f0ee2011-01-28 23:46:28 +0000286 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000287 __list_iterator(const __list_iterator& __p)
288 : __ptr_(__p.__ptr_)
289 {
290 __get_db()->__iterator_copy(this, &__p);
291 }
292
293 _LIBCPP_INLINE_VISIBILITY
294 ~__list_iterator()
295 {
296 __get_db()->__erase_i(this);
297 }
298
299 _LIBCPP_INLINE_VISIBILITY
300 __list_iterator& operator=(const __list_iterator& __p)
301 {
302 if (this != &__p)
303 {
304 __get_db()->__iterator_copy(this, &__p);
305 __ptr_ = __p.__ptr_;
306 }
307 return *this;
308 }
309
310#endif // _LIBCPP_DEBUG_LEVEL >= 2
311
312 _LIBCPP_INLINE_VISIBILITY
313 reference operator*() const
314 {
315#if _LIBCPP_DEBUG_LEVEL >= 2
316 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
317 "Attempted to dereference a non-dereferenceable list::iterator");
318#endif
319 return __ptr_->__value_;
320 }
Howard Hinnant82894812010-09-22 16:48:34 +0000321 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant29f74322013-06-25 16:08:47 +0000322 pointer operator->() const
323 {
324#if _LIBCPP_DEBUG_LEVEL >= 2
325 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
326 "Attempted to dereference a non-dereferenceable list::iterator");
327#endif
328 return pointer_traits<pointer>::pointer_to(__ptr_->__value_);
329 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000330
Howard Hinnant82894812010-09-22 16:48:34 +0000331 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000332 __list_iterator& operator++()
333 {
334#if _LIBCPP_DEBUG_LEVEL >= 2
335 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
336 "Attempted to increment non-incrementable list::iterator");
337#endif
338 __ptr_ = __ptr_->__next_;
339 return *this;
340 }
Howard Hinnant82894812010-09-22 16:48:34 +0000341 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000342 __list_iterator operator++(int) {__list_iterator __t(*this); ++(*this); return __t;}
343
Howard Hinnant82894812010-09-22 16:48:34 +0000344 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000345 __list_iterator& operator--()
346 {
347#if _LIBCPP_DEBUG_LEVEL >= 2
348 _LIBCPP_ASSERT(__get_const_db()->__decrementable(this),
349 "Attempted to decrement non-decrementable list::iterator");
350#endif
351 __ptr_ = __ptr_->__prev_;
352 return *this;
353 }
Howard Hinnant82894812010-09-22 16:48:34 +0000354 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000355 __list_iterator operator--(int) {__list_iterator __t(*this); --(*this); return __t;}
356
Howard Hinnant82894812010-09-22 16:48:34 +0000357 friend _LIBCPP_INLINE_VISIBILITY
358 bool operator==(const __list_iterator& __x, const __list_iterator& __y)
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000359 {
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000360 return __x.__ptr_ == __y.__ptr_;
361 }
Howard Hinnant82894812010-09-22 16:48:34 +0000362 friend _LIBCPP_INLINE_VISIBILITY
363 bool operator!=(const __list_iterator& __x, const __list_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000364 {return !(__x == __y);}
365};
366
367template <class _Tp, class _VoidPtr>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000368class _LIBCPP_TYPE_VIS_ONLY __list_const_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000369{
370 typedef typename pointer_traits<_VoidPtr>::template
371#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnant29f74322013-06-25 16:08:47 +0000372 rebind<__list_node<_Tp, _VoidPtr> > __node_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000373#else
Howard Hinnant29f74322013-06-25 16:08:47 +0000374 rebind<__list_node<_Tp, _VoidPtr> >::other __node_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000375#endif
376
377 __node_pointer __ptr_;
378
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000379#if _LIBCPP_DEBUG_LEVEL >= 2
380 _LIBCPP_INLINE_VISIBILITY
381 explicit __list_const_iterator(__node_pointer __p, const void* __c) _NOEXCEPT
382 : __ptr_(__p)
383 {
384 __get_db()->__insert_ic(this, __c);
385 }
386#else
Howard Hinnant82894812010-09-22 16:48:34 +0000387 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000388 explicit __list_const_iterator(__node_pointer __p) _NOEXCEPT : __ptr_(__p) {}
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000389#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000390
391 template<class, class> friend class list;
392 template<class, class> friend class __list_imp;
393public:
394 typedef bidirectional_iterator_tag iterator_category;
395 typedef _Tp value_type;
396 typedef const value_type& reference;
397 typedef typename pointer_traits<_VoidPtr>::template
398#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
399 rebind<const value_type>
400#else
401 rebind<const value_type>::other
402#endif
403 pointer;
404 typedef typename pointer_traits<pointer>::difference_type difference_type;
405
Howard Hinnant82894812010-09-22 16:48:34 +0000406 _LIBCPP_INLINE_VISIBILITY
Marshall Clow65d2e6a2013-08-05 21:23:28 +0000407 __list_const_iterator() _NOEXCEPT : __ptr_(nullptr)
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000408 {
409#if _LIBCPP_DEBUG_LEVEL >= 2
410 __get_db()->__insert_i(this);
411#endif
412 }
Howard Hinnant211f0ee2011-01-28 23:46:28 +0000413 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6dcaf3e2013-04-05 17:58:52 +0000414 __list_const_iterator(const __list_iterator<_Tp, _VoidPtr>& __p) _NOEXCEPT
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000415 : __ptr_(__p.__ptr_)
416 {
417#if _LIBCPP_DEBUG_LEVEL >= 2
418 __get_db()->__iterator_copy(this, &__p);
419#endif
420 }
421
422#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000423
Howard Hinnant82894812010-09-22 16:48:34 +0000424 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000425 __list_const_iterator(const __list_const_iterator& __p)
426 : __ptr_(__p.__ptr_)
427 {
428 __get_db()->__iterator_copy(this, &__p);
429 }
430
431 _LIBCPP_INLINE_VISIBILITY
432 ~__list_const_iterator()
433 {
434 __get_db()->__erase_i(this);
435 }
436
437 _LIBCPP_INLINE_VISIBILITY
438 __list_const_iterator& operator=(const __list_const_iterator& __p)
439 {
440 if (this != &__p)
441 {
442 __get_db()->__iterator_copy(this, &__p);
443 __ptr_ = __p.__ptr_;
444 }
445 return *this;
446 }
447
448#endif // _LIBCPP_DEBUG_LEVEL >= 2
449 _LIBCPP_INLINE_VISIBILITY
450 reference operator*() const
451 {
452#if _LIBCPP_DEBUG_LEVEL >= 2
453 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
454 "Attempted to dereference a non-dereferenceable list::const_iterator");
455#endif
456 return __ptr_->__value_;
457 }
Howard Hinnant82894812010-09-22 16:48:34 +0000458 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant29f74322013-06-25 16:08:47 +0000459 pointer operator->() const
460 {
461#if _LIBCPP_DEBUG_LEVEL >= 2
462 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
463 "Attempted to dereference a non-dereferenceable list::iterator");
464#endif
465 return pointer_traits<pointer>::pointer_to(__ptr_->__value_);
466 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000467
Howard Hinnant82894812010-09-22 16:48:34 +0000468 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000469 __list_const_iterator& operator++()
470 {
471#if _LIBCPP_DEBUG_LEVEL >= 2
472 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
473 "Attempted to increment non-incrementable list::const_iterator");
474#endif
475 __ptr_ = __ptr_->__next_;
476 return *this;
477 }
Howard Hinnant82894812010-09-22 16:48:34 +0000478 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000479 __list_const_iterator operator++(int) {__list_const_iterator __t(*this); ++(*this); return __t;}
480
Howard Hinnant82894812010-09-22 16:48:34 +0000481 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000482 __list_const_iterator& operator--()
483 {
484#if _LIBCPP_DEBUG_LEVEL >= 2
485 _LIBCPP_ASSERT(__get_const_db()->__decrementable(this),
486 "Attempted to decrement non-decrementable list::const_iterator");
487#endif
488 __ptr_ = __ptr_->__prev_;
489 return *this;
490 }
Howard Hinnant82894812010-09-22 16:48:34 +0000491 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000492 __list_const_iterator operator--(int) {__list_const_iterator __t(*this); --(*this); return __t;}
493
Howard Hinnant82894812010-09-22 16:48:34 +0000494 friend _LIBCPP_INLINE_VISIBILITY
495 bool operator==(const __list_const_iterator& __x, const __list_const_iterator& __y)
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000496 {
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000497 return __x.__ptr_ == __y.__ptr_;
498 }
Howard Hinnant82894812010-09-22 16:48:34 +0000499 friend _LIBCPP_INLINE_VISIBILITY
500 bool operator!=(const __list_const_iterator& __x, const __list_const_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000501 {return !(__x == __y);}
502};
503
504template <class _Tp, class _Alloc>
505class __list_imp
506{
507 __list_imp(const __list_imp&);
508 __list_imp& operator=(const __list_imp&);
509protected:
510 typedef _Tp value_type;
511 typedef _Alloc allocator_type;
512 typedef allocator_traits<allocator_type> __alloc_traits;
513 typedef typename __alloc_traits::size_type size_type;
514 typedef typename __alloc_traits::void_pointer __void_pointer;
515 typedef __list_iterator<value_type, __void_pointer> iterator;
516 typedef __list_const_iterator<value_type, __void_pointer> const_iterator;
517 typedef __list_node_base<value_type, __void_pointer> __node_base;
518 typedef __list_node<value_type, __void_pointer> __node;
519 typedef typename __alloc_traits::template
520#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
521 rebind_alloc<__node>
522#else
523 rebind_alloc<__node>::other
524#endif
525 __node_allocator;
526 typedef allocator_traits<__node_allocator> __node_alloc_traits;
527 typedef typename __node_alloc_traits::pointer __node_pointer;
Howard Hinnant29f74322013-06-25 16:08:47 +0000528 typedef typename __node_alloc_traits::pointer __node_const_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000529 typedef typename __alloc_traits::pointer pointer;
530 typedef typename __alloc_traits::const_pointer const_pointer;
531 typedef typename __alloc_traits::difference_type difference_type;
532
Howard Hinnant29f74322013-06-25 16:08:47 +0000533 typedef typename __alloc_traits::template
534#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
535 rebind_alloc<__node_base>
536#else
537 rebind_alloc<__node_base>::other
538#endif
539 __node_base_allocator;
540 typedef typename allocator_traits<__node_base_allocator>::pointer __node_base_pointer;
541
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000542 __node_base __end_;
543 __compressed_pair<size_type, __node_allocator> __size_alloc_;
544
Howard Hinnant82894812010-09-22 16:48:34 +0000545 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000546 size_type& __sz() _NOEXCEPT {return __size_alloc_.first();}
Howard Hinnant82894812010-09-22 16:48:34 +0000547 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000548 const size_type& __sz() const _NOEXCEPT
549 {return __size_alloc_.first();}
Howard Hinnant82894812010-09-22 16:48:34 +0000550 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000551 __node_allocator& __node_alloc() _NOEXCEPT
552 {return __size_alloc_.second();}
Howard Hinnant82894812010-09-22 16:48:34 +0000553 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000554 const __node_allocator& __node_alloc() const _NOEXCEPT
555 {return __size_alloc_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000556
Howard Hinnant29f74322013-06-25 16:08:47 +0000557 static void __unlink_nodes(__node_pointer __f, __node_pointer __l) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000558
Howard Hinnantc5607272011-06-03 17:30:28 +0000559 __list_imp()
560 _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000561 __list_imp(const allocator_type& __a);
562 ~__list_imp();
Howard Hinnantc5607272011-06-03 17:30:28 +0000563 void clear() _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +0000564 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000565 bool empty() const _NOEXCEPT {return __sz() == 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000566
Howard Hinnant82894812010-09-22 16:48:34 +0000567 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000568 iterator begin() _NOEXCEPT
569 {
570#if _LIBCPP_DEBUG_LEVEL >= 2
571 return iterator(__end_.__next_, this);
572#else
573 return iterator(__end_.__next_);
574#endif
575 }
Howard Hinnant82894812010-09-22 16:48:34 +0000576 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000577 const_iterator begin() const _NOEXCEPT
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000578 {
579#if _LIBCPP_DEBUG_LEVEL >= 2
580 return const_iterator(__end_.__next_, this);
581#else
582 return const_iterator(__end_.__next_);
583#endif
584 }
Howard Hinnant82894812010-09-22 16:48:34 +0000585 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000586 iterator end() _NOEXCEPT
587 {
588#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant29f74322013-06-25 16:08:47 +0000589 return iterator(static_cast<__node_pointer>(
590 pointer_traits<__node_base_pointer>::pointer_to(__end_)), this);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000591#else
Howard Hinnant29f74322013-06-25 16:08:47 +0000592 return iterator(static_cast<__node_pointer>(
593 pointer_traits<__node_base_pointer>::pointer_to(__end_)));
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000594#endif
595 }
Howard Hinnant82894812010-09-22 16:48:34 +0000596 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000597 const_iterator end() const _NOEXCEPT
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000598 {
599#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant29f74322013-06-25 16:08:47 +0000600 return const_iterator(static_cast<__node_const_pointer>(
601 pointer_traits<__node_base_pointer>::pointer_to(const_cast<__node_base&>(__end_))), this);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000602#else
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_))));
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000605#endif
606 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000607
Howard Hinnantc5607272011-06-03 17:30:28 +0000608 void swap(__list_imp& __c)
609 _NOEXCEPT_(!__node_alloc_traits::propagate_on_container_swap::value ||
610 __is_nothrow_swappable<__node_allocator>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000611
Howard Hinnant82894812010-09-22 16:48:34 +0000612 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000613 void __copy_assign_alloc(const __list_imp& __c)
614 {__copy_assign_alloc(__c, integral_constant<bool,
615 __node_alloc_traits::propagate_on_container_copy_assignment::value>());}
616
Howard Hinnant82894812010-09-22 16:48:34 +0000617 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000618 void __move_assign_alloc(__list_imp& __c)
Howard Hinnantc5607272011-06-03 17:30:28 +0000619 _NOEXCEPT_(
620 !__node_alloc_traits::propagate_on_container_move_assignment::value ||
621 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000622 {__move_assign_alloc(__c, integral_constant<bool,
623 __node_alloc_traits::propagate_on_container_move_assignment::value>());}
624
625private:
Howard Hinnant82894812010-09-22 16:48:34 +0000626 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000627 static void __swap_alloc(__node_allocator& __x, __node_allocator& __y)
Howard Hinnantc5607272011-06-03 17:30:28 +0000628 _NOEXCEPT_(!__node_alloc_traits::propagate_on_container_swap::value ||
629 __is_nothrow_swappable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000630 {__swap_alloc(__x, __y, integral_constant<bool,
631 __node_alloc_traits::propagate_on_container_swap::value>());}
Howard Hinnant82894812010-09-22 16:48:34 +0000632 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000633 static void __swap_alloc(__node_allocator& __x, __node_allocator& __y, true_type)
Howard Hinnantc5607272011-06-03 17:30:28 +0000634 _NOEXCEPT_(__is_nothrow_swappable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000635 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000636 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000637 swap(__x, __y);
638 }
Howard Hinnant82894812010-09-22 16:48:34 +0000639 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000640 static void __swap_alloc(__node_allocator& __x, __node_allocator& __y, false_type)
Howard Hinnantc5607272011-06-03 17:30:28 +0000641 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000642 {}
643
Howard Hinnant82894812010-09-22 16:48:34 +0000644 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000645 void __copy_assign_alloc(const __list_imp& __c, true_type)
646 {
647 if (__node_alloc() != __c.__node_alloc())
648 clear();
649 __node_alloc() = __c.__node_alloc();
650 }
651
Howard Hinnant82894812010-09-22 16:48:34 +0000652 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000653 void __copy_assign_alloc(const __list_imp& __c, false_type)
654 {}
655
Howard Hinnant82894812010-09-22 16:48:34 +0000656 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31 +0000657 void __move_assign_alloc(__list_imp& __c, true_type)
Howard Hinnantc5607272011-06-03 17:30:28 +0000658 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000659 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000660 __node_alloc() = _VSTD::move(__c.__node_alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000661 }
662
Howard Hinnant82894812010-09-22 16:48:34 +0000663 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31 +0000664 void __move_assign_alloc(__list_imp& __c, false_type)
Howard Hinnantc5607272011-06-03 17:30:28 +0000665 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000666 {}
667};
668
669// Unlink nodes [__f, __l]
670template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +0000671inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000672void
Howard Hinnant29f74322013-06-25 16:08:47 +0000673__list_imp<_Tp, _Alloc>::__unlink_nodes(__node_pointer __f, __node_pointer __l)
Howard Hinnantc5607272011-06-03 17:30:28 +0000674 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000675{
Howard Hinnant29f74322013-06-25 16:08:47 +0000676 __f->__prev_->__next_ = __l->__next_;
677 __l->__next_->__prev_ = __f->__prev_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000678}
679
680template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +0000681inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000682__list_imp<_Tp, _Alloc>::__list_imp()
Howard Hinnantc5607272011-06-03 17:30:28 +0000683 _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000684 : __size_alloc_(0)
685{
686}
687
688template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +0000689inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000690__list_imp<_Tp, _Alloc>::__list_imp(const allocator_type& __a)
691 : __size_alloc_(0, __node_allocator(__a))
692{
693}
694
695template <class _Tp, class _Alloc>
696__list_imp<_Tp, _Alloc>::~__list_imp()
697{
698 clear();
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000699#if _LIBCPP_DEBUG_LEVEL >= 2
700 __get_db()->__erase_c(this);
701#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000702}
703
704template <class _Tp, class _Alloc>
705void
Howard Hinnantc5607272011-06-03 17:30:28 +0000706__list_imp<_Tp, _Alloc>::clear() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000707{
708 if (!empty())
709 {
710 __node_allocator& __na = __node_alloc();
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000711 __node_pointer __f = __end_.__next_;
Howard Hinnant29f74322013-06-25 16:08:47 +0000712 __node_pointer __l = static_cast<__node_pointer>(
713 pointer_traits<__node_base_pointer>::pointer_to(__end_));
714 __unlink_nodes(__f, __l->__prev_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000715 __sz() = 0;
716 while (__f != __l)
717 {
Howard Hinnant29f74322013-06-25 16:08:47 +0000718 __node_pointer __n = __f;
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000719 __f = __f->__next_;
Howard Hinnant29f74322013-06-25 16:08:47 +0000720 __node_alloc_traits::destroy(__na, _VSTD::addressof(__n->__value_));
721 __node_alloc_traits::deallocate(__na, __n, 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000722 }
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000723#if _LIBCPP_DEBUG_LEVEL >= 2
724 __c_node* __c = __get_db()->__find_c_and_lock(this);
725 for (__i_node** __p = __c->end_; __p != __c->beg_; )
726 {
727 --__p;
728 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
729 if (__i->__ptr_ != __l)
730 {
731 (*__p)->__c_ = nullptr;
732 if (--__c->end_ != __p)
733 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
734 }
735 }
736 __get_db()->unlock();
737#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000738 }
739}
740
741template <class _Tp, class _Alloc>
742void
743__list_imp<_Tp, _Alloc>::swap(__list_imp& __c)
Howard Hinnantc5607272011-06-03 17:30:28 +0000744 _NOEXCEPT_(!__node_alloc_traits::propagate_on_container_swap::value ||
745 __is_nothrow_swappable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000746{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000747 _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
748 this->__node_alloc() == __c.__node_alloc(),
749 "list::swap: Either propagate_on_container_swap must be true"
750 " or the allocators must compare equal");
Howard Hinnant0949eed2011-06-30 21:18:19 +0000751 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000752 __swap_alloc(__node_alloc(), __c.__node_alloc());
753 swap(__sz(), __c.__sz());
754 swap(__end_, __c.__end_);
755 if (__sz() == 0)
Howard Hinnant29f74322013-06-25 16:08:47 +0000756 __end_.__next_ = __end_.__prev_ = static_cast<__node_pointer>(
757 pointer_traits<__node_base_pointer>::pointer_to(__end_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000758 else
759 __end_.__prev_->__next_ = __end_.__next_->__prev_
Howard Hinnant29f74322013-06-25 16:08:47 +0000760 = static_cast<__node_pointer>(
761 pointer_traits<__node_base_pointer>::pointer_to(__end_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000762 if (__c.__sz() == 0)
763 __c.__end_.__next_ = __c.__end_.__prev_
Howard Hinnant29f74322013-06-25 16:08:47 +0000764 = static_cast<__node_pointer>(
765 pointer_traits<__node_base_pointer>::pointer_to(__c.__end_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000766 else
767 __c.__end_.__prev_->__next_ = __c.__end_.__next_->__prev_
Howard Hinnant29f74322013-06-25 16:08:47 +0000768 = static_cast<__node_pointer>(
769 pointer_traits<__node_base_pointer>::pointer_to(__c.__end_));
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000770#if _LIBCPP_DEBUG_LEVEL >= 2
771 __libcpp_db* __db = __get_db();
772 __c_node* __cn1 = __db->__find_c_and_lock(this);
773 __c_node* __cn2 = __db->__find_c(&__c);
774 std::swap(__cn1->beg_, __cn2->beg_);
775 std::swap(__cn1->end_, __cn2->end_);
776 std::swap(__cn1->cap_, __cn2->cap_);
777 for (__i_node** __p = __cn1->end_; __p != __cn1->beg_;)
778 {
779 --__p;
780 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
Howard Hinnant29f74322013-06-25 16:08:47 +0000781 if (__i->__ptr_ == static_cast<__node_pointer>(
782 pointer_traits<__node_base_pointer>::pointer_to(__c.__end_)))
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000783 {
784 __cn2->__add(*__p);
785 if (--__cn1->end_ != __p)
786 memmove(__p, __p+1, (__cn1->end_ - __p)*sizeof(__i_node*));
787 }
788 else
789 (*__p)->__c_ = __cn1;
790 }
791 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
792 {
793 --__p;
794 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
Howard Hinnant29f74322013-06-25 16:08:47 +0000795 if (__i->__ptr_ == static_cast<__node_pointer>(
796 pointer_traits<__node_base_pointer>::pointer_to(__end_)))
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000797 {
798 __cn1->__add(*__p);
799 if (--__cn2->end_ != __p)
800 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
801 }
802 else
803 (*__p)->__c_ = __cn2;
804 }
805 __db->unlock();
806#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000807}
808
809template <class _Tp, class _Alloc = allocator<_Tp> >
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000810class _LIBCPP_TYPE_VIS_ONLY list
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000811 : private __list_imp<_Tp, _Alloc>
812{
813 typedef __list_imp<_Tp, _Alloc> base;
814 typedef typename base::__node __node;
815 typedef typename base::__node_allocator __node_allocator;
816 typedef typename base::__node_pointer __node_pointer;
817 typedef typename base::__node_alloc_traits __node_alloc_traits;
Howard Hinnant29f74322013-06-25 16:08:47 +0000818 typedef typename base::__node_base __node_base;
819 typedef typename base::__node_base_pointer __node_base_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000820
821public:
822 typedef _Tp value_type;
823 typedef _Alloc allocator_type;
824 static_assert((is_same<value_type, typename allocator_type::value_type>::value),
825 "Invalid allocator::value_type");
826 typedef value_type& reference;
827 typedef const value_type& const_reference;
828 typedef typename base::pointer pointer;
829 typedef typename base::const_pointer const_pointer;
830 typedef typename base::size_type size_type;
831 typedef typename base::difference_type difference_type;
832 typedef typename base::iterator iterator;
833 typedef typename base::const_iterator const_iterator;
Howard Hinnant0949eed2011-06-30 21:18:19 +0000834 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
835 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000836
Howard Hinnant82894812010-09-22 16:48:34 +0000837 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000838 list()
839 _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value)
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000840 {
841#if _LIBCPP_DEBUG_LEVEL >= 2
842 __get_db()->__insert_c(this);
843#endif
844 }
Howard Hinnant82894812010-09-22 16:48:34 +0000845 _LIBCPP_INLINE_VISIBILITY
Marshall Clow955f2c82013-09-08 19:11:51 +0000846 explicit list(const allocator_type& __a) : base(__a)
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000847 {
848#if _LIBCPP_DEBUG_LEVEL >= 2
849 __get_db()->__insert_c(this);
850#endif
851 }
Marshall Clow955f2c82013-09-08 19:11:51 +0000852 explicit list(size_type __n);
853#if _LIBCPP_STD_VER > 11
854 explicit list(size_type __n, const allocator_type& __a);
855#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000856 list(size_type __n, const value_type& __x);
857 list(size_type __n, const value_type& __x, const allocator_type& __a);
858 template <class _InpIter>
859 list(_InpIter __f, _InpIter __l,
860 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
861 template <class _InpIter>
862 list(_InpIter __f, _InpIter __l, const allocator_type& __a,
863 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
864
865 list(const list& __c);
866 list(const list& __c, const allocator_type& __a);
867 list& operator=(const list& __c);
Howard Hinnante3e32912011-08-12 21:56:02 +0000868#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000869 list(initializer_list<value_type> __il);
870 list(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnante3e32912011-08-12 21:56:02 +0000871#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant73d21a42010-09-04 23:28:19 +0000872#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc5607272011-06-03 17:30:28 +0000873 list(list&& __c)
874 _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000875 list(list&& __c, const allocator_type& __a);
Howard Hinnantc5607272011-06-03 17:30:28 +0000876 list& operator=(list&& __c)
877 _NOEXCEPT_(
878 __node_alloc_traits::propagate_on_container_move_assignment::value &&
879 is_nothrow_move_assignable<__node_allocator>::value);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000880#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnante3e32912011-08-12 21:56:02 +0000881#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant82894812010-09-22 16:48:34 +0000882 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000883 list& operator=(initializer_list<value_type> __il)
884 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnante3e32912011-08-12 21:56:02 +0000885#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000886
887 template <class _InpIter>
888 void assign(_InpIter __f, _InpIter __l,
889 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
890 void assign(size_type __n, const value_type& __x);
Howard Hinnante3e32912011-08-12 21:56:02 +0000891#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant82894812010-09-22 16:48:34 +0000892 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000893 void assign(initializer_list<value_type> __il)
894 {assign(__il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +0000895#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000896
Howard Hinnantc5607272011-06-03 17:30:28 +0000897 allocator_type get_allocator() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000898
Howard Hinnant82894812010-09-22 16:48:34 +0000899 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000900 size_type size() const _NOEXCEPT {return base::__sz();}
Howard Hinnant82894812010-09-22 16:48:34 +0000901 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000902 bool empty() const _NOEXCEPT {return base::empty();}
Howard Hinnant82894812010-09-22 16:48:34 +0000903 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000904 size_type max_size() const _NOEXCEPT
905 {return numeric_limits<difference_type>::max();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000906
Howard Hinnant82894812010-09-22 16:48:34 +0000907 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000908 iterator begin() _NOEXCEPT {return base::begin();}
Howard Hinnant82894812010-09-22 16:48:34 +0000909 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000910 const_iterator begin() const _NOEXCEPT {return base::begin();}
Howard Hinnant82894812010-09-22 16:48:34 +0000911 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000912 iterator end() _NOEXCEPT {return base::end();}
Howard Hinnant82894812010-09-22 16:48:34 +0000913 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000914 const_iterator end() const _NOEXCEPT {return base::end();}
Howard Hinnant82894812010-09-22 16:48:34 +0000915 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000916 const_iterator cbegin() const _NOEXCEPT {return base::begin();}
Howard Hinnant82894812010-09-22 16:48:34 +0000917 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000918 const_iterator cend() const _NOEXCEPT {return base::end();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000919
Howard Hinnant82894812010-09-22 16:48:34 +0000920 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000921 reverse_iterator rbegin() _NOEXCEPT
922 {return reverse_iterator(end());}
Howard Hinnant82894812010-09-22 16:48:34 +0000923 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000924 const_reverse_iterator rbegin() const _NOEXCEPT
925 {return const_reverse_iterator(end());}
Howard Hinnant82894812010-09-22 16:48:34 +0000926 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000927 reverse_iterator rend() _NOEXCEPT
928 {return 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 rend() const _NOEXCEPT
931 {return const_reverse_iterator(begin());}
Howard Hinnant82894812010-09-22 16:48:34 +0000932 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000933 const_reverse_iterator crbegin() const _NOEXCEPT
934 {return const_reverse_iterator(end());}
Howard Hinnant82894812010-09-22 16:48:34 +0000935 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000936 const_reverse_iterator crend() const _NOEXCEPT
937 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000938
Howard Hinnant82894812010-09-22 16:48:34 +0000939 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000940 reference front()
941 {
942 _LIBCPP_ASSERT(!empty(), "list::front called on empty list");
943 return base::__end_.__next_->__value_;
944 }
Howard Hinnant82894812010-09-22 16:48:34 +0000945 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000946 const_reference front() const
947 {
948 _LIBCPP_ASSERT(!empty(), "list::front called on empty list");
949 return base::__end_.__next_->__value_;
950 }
Howard Hinnant82894812010-09-22 16:48:34 +0000951 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000952 reference back()
953 {
954 _LIBCPP_ASSERT(!empty(), "list::back called on empty list");
955 return base::__end_.__prev_->__value_;
956 }
Howard Hinnant82894812010-09-22 16:48:34 +0000957 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000958 const_reference back() const
959 {
960 _LIBCPP_ASSERT(!empty(), "list::back called on empty list");
961 return base::__end_.__prev_->__value_;
962 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000963
Howard Hinnant73d21a42010-09-04 23:28:19 +0000964#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000965 void push_front(value_type&& __x);
966 void push_back(value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000967#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000968 template <class... _Args>
969 void emplace_front(_Args&&... __args);
970 template <class... _Args>
971 void emplace_back(_Args&&... __args);
972 template <class... _Args>
973 iterator emplace(const_iterator __p, _Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000974#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000975 iterator insert(const_iterator __p, value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000976#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000977
978 void push_front(const value_type& __x);
979 void push_back(const value_type& __x);
980
981 iterator insert(const_iterator __p, const value_type& __x);
982 iterator insert(const_iterator __p, size_type __n, const value_type& __x);
983 template <class _InpIter>
984 iterator insert(const_iterator __p, _InpIter __f, _InpIter __l,
985 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
Howard Hinnante3e32912011-08-12 21:56:02 +0000986#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant82894812010-09-22 16:48:34 +0000987 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000988 iterator insert(const_iterator __p, initializer_list<value_type> __il)
989 {return insert(__p, __il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +0000990#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000991
Howard Hinnant82894812010-09-22 16:48:34 +0000992 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000993 void swap(list& __c)
994 _NOEXCEPT_(!__node_alloc_traits::propagate_on_container_swap::value ||
995 __is_nothrow_swappable<__node_allocator>::value)
996 {base::swap(__c);}
Howard Hinnant82894812010-09-22 16:48:34 +0000997 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000998 void clear() _NOEXCEPT {base::clear();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000999
1000 void pop_front();
1001 void pop_back();
1002
1003 iterator erase(const_iterator __p);
1004 iterator erase(const_iterator __f, const_iterator __l);
1005
1006 void resize(size_type __n);
1007 void resize(size_type __n, const value_type& __x);
1008
1009 void splice(const_iterator __p, list& __c);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001010#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant82894812010-09-22 16:48:34 +00001011 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001012 void splice(const_iterator __p, list&& __c) {splice(__p, __c);}
1013#endif
1014 void splice(const_iterator __p, list& __c, const_iterator __i);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001015#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant82894812010-09-22 16:48:34 +00001016 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001017 void splice(const_iterator __p, list&& __c, const_iterator __i)
1018 {splice(__p, __c, __i);}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001019#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001020 void splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001021#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant82894812010-09-22 16:48:34 +00001022 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001023 void splice(const_iterator __p, list&& __c, const_iterator __f, const_iterator __l)
1024 {splice(__p, __c, __f, __l);}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001025#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001026
1027 void remove(const value_type& __x);
1028 template <class _Pred> void remove_if(_Pred __pred);
1029 void unique();
1030 template <class _BinaryPred>
1031 void unique(_BinaryPred __binary_pred);
1032 void merge(list& __c);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001033#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant82894812010-09-22 16:48:34 +00001034 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001035 void merge(list&& __c) {merge(__c);}
1036#endif
1037 template <class _Comp>
1038 void merge(list& __c, _Comp __comp);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001039#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001040 template <class _Comp>
Howard Hinnant82894812010-09-22 16:48:34 +00001041 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001042 void merge(list&& __c, _Comp __comp) {merge(__c, __comp);}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001043#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001044 void sort();
1045 template <class _Comp>
1046 void sort(_Comp __comp);
1047
Howard Hinnantc5607272011-06-03 17:30:28 +00001048 void reverse() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001049
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001050 bool __invariants() const;
1051
1052#if _LIBCPP_DEBUG_LEVEL >= 2
1053
1054 bool __dereferenceable(const const_iterator* __i) const;
1055 bool __decrementable(const const_iterator* __i) const;
1056 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1057 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1058
1059#endif // _LIBCPP_DEBUG_LEVEL >= 2
1060
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001061private:
Howard Hinnant29f74322013-06-25 16:08:47 +00001062 static void __link_nodes(__node_pointer __p, __node_pointer __f, __node_pointer __l);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001063 iterator __iterator(size_type __n);
1064 template <class _Comp>
1065 static iterator __sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp);
1066
Howard Hinnantc5607272011-06-03 17:30:28 +00001067 void __move_assign(list& __c, true_type)
1068 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001069 void __move_assign(list& __c, false_type);
1070};
1071
1072// Link in nodes [__f, __l] just prior to __p
1073template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001074inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001075void
Howard Hinnant29f74322013-06-25 16:08:47 +00001076list<_Tp, _Alloc>::__link_nodes(__node_pointer __p, __node_pointer __f, __node_pointer __l)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001077{
Howard Hinnant29f74322013-06-25 16:08:47 +00001078 __p->__prev_->__next_ = __f;
1079 __f->__prev_ = __p->__prev_;
1080 __p->__prev_ = __l;
1081 __l->__next_ = __p;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001082}
1083
1084template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001085inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001086typename list<_Tp, _Alloc>::iterator
1087list<_Tp, _Alloc>::__iterator(size_type __n)
1088{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001089 return __n <= base::__sz() / 2 ? _VSTD::next(begin(), __n)
1090 : _VSTD::prev(end(), base::__sz() - __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001091}
1092
1093template <class _Tp, class _Alloc>
1094list<_Tp, _Alloc>::list(size_type __n)
1095{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001096#if _LIBCPP_DEBUG_LEVEL >= 2
1097 __get_db()->__insert_c(this);
1098#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001099 for (; __n > 0; --__n)
Howard Hinnant73d21a42010-09-04 23:28:19 +00001100#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001101 emplace_back();
1102#else
1103 push_back(value_type());
1104#endif
1105}
1106
Marshall Clow955f2c82013-09-08 19:11:51 +00001107#if _LIBCPP_STD_VER > 11
1108template <class _Tp, class _Alloc>
1109list<_Tp, _Alloc>::list(size_type __n, const allocator_type& __a) : base(__a)
1110{
1111#if _LIBCPP_DEBUG_LEVEL >= 2
1112 __get_db()->__insert_c(this);
1113#endif
1114 for (; __n > 0; --__n)
1115#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1116 emplace_back();
1117#else
1118 push_back(value_type());
1119#endif
1120}
1121#endif
1122
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001123template <class _Tp, class _Alloc>
1124list<_Tp, _Alloc>::list(size_type __n, const value_type& __x)
1125{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001126#if _LIBCPP_DEBUG_LEVEL >= 2
1127 __get_db()->__insert_c(this);
1128#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001129 for (; __n > 0; --__n)
1130 push_back(__x);
1131}
1132
1133template <class _Tp, class _Alloc>
1134list<_Tp, _Alloc>::list(size_type __n, const value_type& __x, const allocator_type& __a)
1135 : base(__a)
1136{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001137#if _LIBCPP_DEBUG_LEVEL >= 2
1138 __get_db()->__insert_c(this);
1139#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001140 for (; __n > 0; --__n)
1141 push_back(__x);
1142}
1143
1144template <class _Tp, class _Alloc>
1145template <class _InpIter>
1146list<_Tp, _Alloc>::list(_InpIter __f, _InpIter __l,
1147 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1148{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001149#if _LIBCPP_DEBUG_LEVEL >= 2
1150 __get_db()->__insert_c(this);
1151#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001152 for (; __f != __l; ++__f)
1153 push_back(*__f);
1154}
1155
1156template <class _Tp, class _Alloc>
1157template <class _InpIter>
1158list<_Tp, _Alloc>::list(_InpIter __f, _InpIter __l, const allocator_type& __a,
1159 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1160 : base(__a)
1161{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001162#if _LIBCPP_DEBUG_LEVEL >= 2
1163 __get_db()->__insert_c(this);
1164#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001165 for (; __f != __l; ++__f)
1166 push_back(*__f);
1167}
1168
1169template <class _Tp, class _Alloc>
1170list<_Tp, _Alloc>::list(const list& __c)
1171 : base(allocator_type(
1172 __node_alloc_traits::select_on_container_copy_construction(
1173 __c.__node_alloc())))
1174{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001175#if _LIBCPP_DEBUG_LEVEL >= 2
1176 __get_db()->__insert_c(this);
1177#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001178 for (const_iterator __i = __c.begin(), __e = __c.end(); __i != __e; ++__i)
1179 push_back(*__i);
1180}
1181
1182template <class _Tp, class _Alloc>
1183list<_Tp, _Alloc>::list(const list& __c, const allocator_type& __a)
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 (const_iterator __i = __c.begin(), __e = __c.end(); __i != __e; ++__i)
1190 push_back(*__i);
1191}
1192
Howard Hinnante3e32912011-08-12 21:56:02 +00001193#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1194
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001195template <class _Tp, class _Alloc>
1196list<_Tp, _Alloc>::list(initializer_list<value_type> __il, const allocator_type& __a)
1197 : base(__a)
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 (typename initializer_list<value_type>::const_iterator __i = __il.begin(),
1203 __e = __il.end(); __i != __e; ++__i)
1204 push_back(*__i);
1205}
1206
1207template <class _Tp, class _Alloc>
1208list<_Tp, _Alloc>::list(initializer_list<value_type> __il)
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 (typename initializer_list<value_type>::const_iterator __i = __il.begin(),
1214 __e = __il.end(); __i != __e; ++__i)
1215 push_back(*__i);
1216}
1217
Howard Hinnante3e32912011-08-12 21:56:02 +00001218#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1219
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001220template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001221inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001222list<_Tp, _Alloc>&
1223list<_Tp, _Alloc>::operator=(const list& __c)
1224{
1225 if (this != &__c)
1226 {
1227 base::__copy_assign_alloc(__c);
1228 assign(__c.begin(), __c.end());
1229 }
1230 return *this;
1231}
1232
Howard Hinnant73d21a42010-09-04 23:28:19 +00001233#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001234
1235template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001236inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001237list<_Tp, _Alloc>::list(list&& __c)
Howard Hinnantc5607272011-06-03 17:30:28 +00001238 _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001239 : base(allocator_type(_VSTD::move(__c.__node_alloc())))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001240{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001241#if _LIBCPP_DEBUG_LEVEL >= 2
1242 __get_db()->__insert_c(this);
1243#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001244 splice(end(), __c);
1245}
1246
1247template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001248inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001249list<_Tp, _Alloc>::list(list&& __c, const allocator_type& __a)
1250 : base(__a)
1251{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001252#if _LIBCPP_DEBUG_LEVEL >= 2
1253 __get_db()->__insert_c(this);
1254#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001255 if (__a == __c.get_allocator())
1256 splice(end(), __c);
1257 else
1258 {
Howard Hinnant99968442011-11-29 18:15:50 +00001259 typedef move_iterator<iterator> _Ip;
1260 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001261 }
1262}
1263
1264template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001265inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001266list<_Tp, _Alloc>&
1267list<_Tp, _Alloc>::operator=(list&& __c)
Howard Hinnantc5607272011-06-03 17:30:28 +00001268 _NOEXCEPT_(
1269 __node_alloc_traits::propagate_on_container_move_assignment::value &&
1270 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001271{
1272 __move_assign(__c, integral_constant<bool,
1273 __node_alloc_traits::propagate_on_container_move_assignment::value>());
1274 return *this;
1275}
1276
1277template <class _Tp, class _Alloc>
1278void
1279list<_Tp, _Alloc>::__move_assign(list& __c, false_type)
1280{
1281 if (base::__node_alloc() != __c.__node_alloc())
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 else
1287 __move_assign(__c, true_type());
1288}
1289
1290template <class _Tp, class _Alloc>
1291void
1292list<_Tp, _Alloc>::__move_assign(list& __c, true_type)
Howard Hinnantc5607272011-06-03 17:30:28 +00001293 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001294{
1295 clear();
1296 base::__move_assign_alloc(__c);
1297 splice(end(), __c);
1298}
1299
Howard Hinnant73d21a42010-09-04 23:28:19 +00001300#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001301
1302template <class _Tp, class _Alloc>
1303template <class _InpIter>
1304void
1305list<_Tp, _Alloc>::assign(_InpIter __f, _InpIter __l,
1306 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1307{
1308 iterator __i = begin();
1309 iterator __e = end();
1310 for (; __f != __l && __i != __e; ++__f, ++__i)
1311 *__i = *__f;
1312 if (__i == __e)
1313 insert(__e, __f, __l);
1314 else
1315 erase(__i, __e);
1316}
1317
1318template <class _Tp, class _Alloc>
1319void
1320list<_Tp, _Alloc>::assign(size_type __n, const value_type& __x)
1321{
1322 iterator __i = begin();
1323 iterator __e = end();
1324 for (; __n > 0 && __i != __e; --__n, ++__i)
1325 *__i = __x;
1326 if (__i == __e)
1327 insert(__e, __n, __x);
1328 else
1329 erase(__i, __e);
1330}
1331
1332template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001333inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001334_Alloc
Howard Hinnantc5607272011-06-03 17:30:28 +00001335list<_Tp, _Alloc>::get_allocator() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001336{
1337 return allocator_type(base::__node_alloc());
1338}
1339
1340template <class _Tp, class _Alloc>
1341typename list<_Tp, _Alloc>::iterator
1342list<_Tp, _Alloc>::insert(const_iterator __p, const value_type& __x)
1343{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001344#if _LIBCPP_DEBUG_LEVEL >= 2
1345 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1346 "list::insert(iterator, x) called with an iterator not"
1347 " referring to this list");
1348#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001349 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001350 typedef __allocator_destructor<__node_allocator> _Dp;
1351 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001352 __hold->__prev_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001353 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnant29f74322013-06-25 16:08:47 +00001354 __link_nodes(__p.__ptr_, __hold.get(), __hold.get());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001355 ++base::__sz();
Howard Hinnant79a35572013-04-05 00:18:49 +00001356#if _LIBCPP_DEBUG_LEVEL >= 2
1357 return iterator(__hold.release(), this);
1358#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001359 return iterator(__hold.release());
Howard Hinnant79a35572013-04-05 00:18:49 +00001360#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001361}
1362
1363template <class _Tp, class _Alloc>
1364typename list<_Tp, _Alloc>::iterator
1365list<_Tp, _Alloc>::insert(const_iterator __p, size_type __n, const value_type& __x)
1366{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001367#if _LIBCPP_DEBUG_LEVEL >= 2
1368 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1369 "list::insert(iterator, n, x) called with an iterator not"
1370 " referring to this list");
Howard Hinnant29f74322013-06-25 16:08:47 +00001371 iterator __r(__p.__ptr_, this);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001372#else
Howard Hinnant29f74322013-06-25 16:08:47 +00001373 iterator __r(__p.__ptr_);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001374#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001375 if (__n > 0)
1376 {
1377 size_type __ds = 0;
1378 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001379 typedef __allocator_destructor<__node_allocator> _Dp;
1380 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001381 __hold->__prev_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001382 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001383 ++__ds;
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001384#if _LIBCPP_DEBUG_LEVEL >= 2
1385 __r = iterator(__hold.get(), this);
1386#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001387 __r = iterator(__hold.get());
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001388#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001389 __hold.release();
1390 iterator __e = __r;
1391#ifndef _LIBCPP_NO_EXCEPTIONS
1392 try
1393 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001394#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001395 for (--__n; __n != 0; --__n, ++__e, ++__ds)
1396 {
1397 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001398 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001399 __e.__ptr_->__next_ = __hold.get();
1400 __hold->__prev_ = __e.__ptr_;
1401 __hold.release();
1402 }
1403#ifndef _LIBCPP_NO_EXCEPTIONS
1404 }
1405 catch (...)
1406 {
1407 while (true)
1408 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001409 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001410 __node_pointer __prev = __e.__ptr_->__prev_;
1411 __node_alloc_traits::deallocate(__na, __e.__ptr_, 1);
1412 if (__prev == 0)
1413 break;
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001414#if _LIBCPP_DEBUG_LEVEL >= 2
1415 __e = iterator(__prev, this);
1416#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001417 __e = iterator(__prev);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001418#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001419 }
1420 throw;
1421 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001422#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant29f74322013-06-25 16:08:47 +00001423 __link_nodes(__p.__ptr_, __r.__ptr_, __e.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001424 base::__sz() += __ds;
1425 }
1426 return __r;
1427}
1428
1429template <class _Tp, class _Alloc>
1430template <class _InpIter>
1431typename list<_Tp, _Alloc>::iterator
1432list<_Tp, _Alloc>::insert(const_iterator __p, _InpIter __f, _InpIter __l,
1433 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1434{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001435#if _LIBCPP_DEBUG_LEVEL >= 2
1436 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1437 "list::insert(iterator, range) called with an iterator not"
1438 " referring to this list");
Howard Hinnant29f74322013-06-25 16:08:47 +00001439 iterator __r(__p.__ptr_, this);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001440#else
Howard Hinnant29f74322013-06-25 16:08:47 +00001441 iterator __r(__p.__ptr_);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001442#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001443 if (__f != __l)
1444 {
1445 size_type __ds = 0;
1446 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001447 typedef __allocator_destructor<__node_allocator> _Dp;
1448 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001449 __hold->__prev_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001450 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), *__f);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001451 ++__ds;
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001452#if _LIBCPP_DEBUG_LEVEL >= 2
1453 __r = iterator(__hold.get(), this);
1454#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001455 __r = iterator(__hold.get());
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001456#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001457 __hold.release();
1458 iterator __e = __r;
1459#ifndef _LIBCPP_NO_EXCEPTIONS
1460 try
1461 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001462#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001463 for (++__f; __f != __l; ++__f, ++__e, ++__ds)
1464 {
1465 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001466 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), *__f);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001467 __e.__ptr_->__next_ = __hold.get();
1468 __hold->__prev_ = __e.__ptr_;
1469 __hold.release();
1470 }
1471#ifndef _LIBCPP_NO_EXCEPTIONS
1472 }
1473 catch (...)
1474 {
1475 while (true)
1476 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001477 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001478 __node_pointer __prev = __e.__ptr_->__prev_;
1479 __node_alloc_traits::deallocate(__na, __e.__ptr_, 1);
1480 if (__prev == 0)
1481 break;
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001482#if _LIBCPP_DEBUG_LEVEL >= 2
1483 __e = iterator(__prev, this);
1484#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001485 __e = iterator(__prev);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001486#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001487 }
1488 throw;
1489 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001490#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant29f74322013-06-25 16:08:47 +00001491 __link_nodes(__p.__ptr_, __r.__ptr_, __e.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001492 base::__sz() += __ds;
1493 }
1494 return __r;
1495}
1496
1497template <class _Tp, class _Alloc>
1498void
1499list<_Tp, _Alloc>::push_front(const value_type& __x)
1500{
1501 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001502 typedef __allocator_destructor<__node_allocator> _Dp;
1503 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001504 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnant29f74322013-06-25 16:08:47 +00001505 __link_nodes(base::__end_.__next_, __hold.get(), __hold.get());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001506 ++base::__sz();
1507 __hold.release();
1508}
1509
1510template <class _Tp, class _Alloc>
1511void
1512list<_Tp, _Alloc>::push_back(const value_type& __x)
1513{
1514 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001515 typedef __allocator_destructor<__node_allocator> _Dp;
1516 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001517 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnant29f74322013-06-25 16:08:47 +00001518 __link_nodes(static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::
1519 pointer_to(base::__end_)), __hold.get(), __hold.get());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001520 ++base::__sz();
1521 __hold.release();
1522}
1523
Howard Hinnant73d21a42010-09-04 23:28:19 +00001524#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001525
1526template <class _Tp, class _Alloc>
1527void
1528list<_Tp, _Alloc>::push_front(value_type&& __x)
1529{
1530 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001531 typedef __allocator_destructor<__node_allocator> _Dp;
1532 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001533 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
Howard Hinnant29f74322013-06-25 16:08:47 +00001534 __link_nodes(base::__end_.__next_, __hold.get(), __hold.get());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001535 ++base::__sz();
1536 __hold.release();
1537}
1538
1539template <class _Tp, class _Alloc>
1540void
1541list<_Tp, _Alloc>::push_back(value_type&& __x)
1542{
1543 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001544 typedef __allocator_destructor<__node_allocator> _Dp;
1545 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001546 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
Howard Hinnant29f74322013-06-25 16:08:47 +00001547 __link_nodes(static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::
1548 pointer_to(base::__end_)), __hold.get(), __hold.get());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001549 ++base::__sz();
1550 __hold.release();
1551}
1552
Howard Hinnant73d21a42010-09-04 23:28:19 +00001553#ifndef _LIBCPP_HAS_NO_VARIADICS
1554
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001555template <class _Tp, class _Alloc>
1556template <class... _Args>
1557void
1558list<_Tp, _Alloc>::emplace_front(_Args&&... __args)
1559{
1560 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001561 typedef __allocator_destructor<__node_allocator> _Dp;
1562 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001563 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
Howard Hinnant29f74322013-06-25 16:08:47 +00001564 __link_nodes(base::__end_.__next_, __hold.get(), __hold.get());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001565 ++base::__sz();
1566 __hold.release();
1567}
1568
1569template <class _Tp, class _Alloc>
1570template <class... _Args>
1571void
1572list<_Tp, _Alloc>::emplace_back(_Args&&... __args)
1573{
1574 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001575 typedef __allocator_destructor<__node_allocator> _Dp;
1576 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001577 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
Howard Hinnant29f74322013-06-25 16:08:47 +00001578 __link_nodes(static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::
1579 pointer_to(base::__end_)), __hold.get(), __hold.get());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001580 ++base::__sz();
1581 __hold.release();
1582}
1583
1584template <class _Tp, class _Alloc>
1585template <class... _Args>
1586typename list<_Tp, _Alloc>::iterator
1587list<_Tp, _Alloc>::emplace(const_iterator __p, _Args&&... __args)
1588{
Howard Hinnant79a35572013-04-05 00:18:49 +00001589#if _LIBCPP_DEBUG_LEVEL >= 2
1590 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1591 "list::emplace(iterator, args...) called with an iterator not"
1592 " referring to this list");
1593#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001594 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001595 typedef __allocator_destructor<__node_allocator> _Dp;
1596 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001597 __hold->__prev_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001598 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
Howard Hinnant29f74322013-06-25 16:08:47 +00001599 __link_nodes(__p.__ptr_, __hold.get(), __hold.get());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001600 ++base::__sz();
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001601#if _LIBCPP_DEBUG_LEVEL >= 2
1602 return iterator(__hold.release(), this);
1603#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001604 return iterator(__hold.release());
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001605#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001606}
1607
Howard Hinnant73d21a42010-09-04 23:28:19 +00001608#endif // _LIBCPP_HAS_NO_VARIADICS
1609
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001610template <class _Tp, class _Alloc>
1611typename list<_Tp, _Alloc>::iterator
1612list<_Tp, _Alloc>::insert(const_iterator __p, value_type&& __x)
1613{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001614#if _LIBCPP_DEBUG_LEVEL >= 2
1615 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1616 "list::insert(iterator, x) called with an iterator not"
1617 " referring to this list");
1618#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001619 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001620 typedef __allocator_destructor<__node_allocator> _Dp;
1621 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001622 __hold->__prev_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001623 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
Howard Hinnant29f74322013-06-25 16:08:47 +00001624 __link_nodes(__p.__ptr_, __hold.get(), __hold.get());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001625 ++base::__sz();
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001626#if _LIBCPP_DEBUG_LEVEL >= 2
1627 return iterator(__hold.release(), this);
1628#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001629 return iterator(__hold.release());
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001630#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001631}
1632
Howard Hinnant73d21a42010-09-04 23:28:19 +00001633#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001634
1635template <class _Tp, class _Alloc>
1636void
1637list<_Tp, _Alloc>::pop_front()
1638{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001639 _LIBCPP_ASSERT(!empty(), "list::pop_front() called with empty list");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001640 __node_allocator& __na = base::__node_alloc();
Howard Hinnant29f74322013-06-25 16:08:47 +00001641 __node_pointer __n = base::__end_.__next_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001642 base::__unlink_nodes(__n, __n);
1643 --base::__sz();
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001644#if _LIBCPP_DEBUG_LEVEL >= 2
1645 __c_node* __c = __get_db()->__find_c_and_lock(this);
1646 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1647 {
1648 --__p;
1649 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant29f74322013-06-25 16:08:47 +00001650 if (__i->__ptr_ == __n)
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001651 {
1652 (*__p)->__c_ = nullptr;
1653 if (--__c->end_ != __p)
1654 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1655 }
1656 }
1657 __get_db()->unlock();
1658#endif
Howard Hinnant29f74322013-06-25 16:08:47 +00001659 __node_alloc_traits::destroy(__na, _VSTD::addressof(__n->__value_));
1660 __node_alloc_traits::deallocate(__na, __n, 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001661}
1662
1663template <class _Tp, class _Alloc>
1664void
1665list<_Tp, _Alloc>::pop_back()
1666{
Dmitri Gribenkoc7a39cf2013-06-24 06:15:57 +00001667 _LIBCPP_ASSERT(!empty(), "list::pop_back() called with empty list");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001668 __node_allocator& __na = base::__node_alloc();
Howard Hinnant29f74322013-06-25 16:08:47 +00001669 __node_pointer __n = base::__end_.__prev_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001670 base::__unlink_nodes(__n, __n);
1671 --base::__sz();
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001672#if _LIBCPP_DEBUG_LEVEL >= 2
1673 __c_node* __c = __get_db()->__find_c_and_lock(this);
1674 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1675 {
1676 --__p;
1677 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant29f74322013-06-25 16:08:47 +00001678 if (__i->__ptr_ == __n)
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001679 {
1680 (*__p)->__c_ = nullptr;
1681 if (--__c->end_ != __p)
1682 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1683 }
1684 }
1685 __get_db()->unlock();
1686#endif
Howard Hinnant29f74322013-06-25 16:08:47 +00001687 __node_alloc_traits::destroy(__na, _VSTD::addressof(__n->__value_));
1688 __node_alloc_traits::deallocate(__na, __n, 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001689}
1690
1691template <class _Tp, class _Alloc>
1692typename list<_Tp, _Alloc>::iterator
1693list<_Tp, _Alloc>::erase(const_iterator __p)
1694{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001695#if _LIBCPP_DEBUG_LEVEL >= 2
1696 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1697 "list::erase(iterator) called with an iterator not"
1698 " referring to this list");
1699#endif
Howard Hinnant79a35572013-04-05 00:18:49 +00001700 _LIBCPP_ASSERT(__p != end(),
1701 "list::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001702 __node_allocator& __na = base::__node_alloc();
Howard Hinnant29f74322013-06-25 16:08:47 +00001703 __node_pointer __n = __p.__ptr_;
1704 __node_pointer __r = __n->__next_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001705 base::__unlink_nodes(__n, __n);
1706 --base::__sz();
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001707#if _LIBCPP_DEBUG_LEVEL >= 2
1708 __c_node* __c = __get_db()->__find_c_and_lock(this);
1709 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1710 {
1711 --__p;
1712 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant29f74322013-06-25 16:08:47 +00001713 if (__i->__ptr_ == __n)
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001714 {
1715 (*__p)->__c_ = nullptr;
1716 if (--__c->end_ != __p)
1717 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1718 }
1719 }
1720 __get_db()->unlock();
1721#endif
Howard Hinnant29f74322013-06-25 16:08:47 +00001722 __node_alloc_traits::destroy(__na, _VSTD::addressof(__n->__value_));
1723 __node_alloc_traits::deallocate(__na, __n, 1);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001724#if _LIBCPP_DEBUG_LEVEL >= 2
1725 return iterator(__r, this);
1726#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001727 return iterator(__r);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001728#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001729}
1730
1731template <class _Tp, class _Alloc>
1732typename list<_Tp, _Alloc>::iterator
1733list<_Tp, _Alloc>::erase(const_iterator __f, const_iterator __l)
1734{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001735#if _LIBCPP_DEBUG_LEVEL >= 2
1736 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__f) == this,
1737 "list::erase(iterator, iterator) called with an iterator not"
1738 " referring to this list");
1739#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001740 if (__f != __l)
1741 {
1742 __node_allocator& __na = base::__node_alloc();
Howard Hinnant29f74322013-06-25 16:08:47 +00001743 base::__unlink_nodes(__f.__ptr_, __l.__ptr_->__prev_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001744 while (__f != __l)
1745 {
Howard Hinnant29f74322013-06-25 16:08:47 +00001746 __node_pointer __n = __f.__ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001747 ++__f;
1748 --base::__sz();
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001749#if _LIBCPP_DEBUG_LEVEL >= 2
1750 __c_node* __c = __get_db()->__find_c_and_lock(this);
1751 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1752 {
1753 --__p;
1754 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant29f74322013-06-25 16:08:47 +00001755 if (__i->__ptr_ == __n)
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001756 {
1757 (*__p)->__c_ = nullptr;
1758 if (--__c->end_ != __p)
1759 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1760 }
1761 }
1762 __get_db()->unlock();
1763#endif
Howard Hinnant29f74322013-06-25 16:08:47 +00001764 __node_alloc_traits::destroy(__na, _VSTD::addressof(__n->__value_));
1765 __node_alloc_traits::deallocate(__na, __n, 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001766 }
1767 }
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001768#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant29f74322013-06-25 16:08:47 +00001769 return iterator(__l.__ptr_, this);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001770#else
Howard Hinnant29f74322013-06-25 16:08:47 +00001771 return iterator(__l.__ptr_);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001772#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001773}
1774
1775template <class _Tp, class _Alloc>
1776void
1777list<_Tp, _Alloc>::resize(size_type __n)
1778{
1779 if (__n < base::__sz())
1780 erase(__iterator(__n), end());
1781 else if (__n > base::__sz())
1782 {
1783 __n -= base::__sz();
1784 size_type __ds = 0;
1785 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001786 typedef __allocator_destructor<__node_allocator> _Dp;
1787 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001788 __hold->__prev_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001789 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001790 ++__ds;
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001791#if _LIBCPP_DEBUG_LEVEL >= 2
1792 iterator __r = iterator(__hold.release(), this);
1793#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001794 iterator __r = iterator(__hold.release());
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001795#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001796 iterator __e = __r;
1797#ifndef _LIBCPP_NO_EXCEPTIONS
1798 try
1799 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001800#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001801 for (--__n; __n != 0; --__n, ++__e, ++__ds)
1802 {
1803 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001804 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001805 __e.__ptr_->__next_ = __hold.get();
1806 __hold->__prev_ = __e.__ptr_;
1807 __hold.release();
1808 }
1809#ifndef _LIBCPP_NO_EXCEPTIONS
1810 }
1811 catch (...)
1812 {
1813 while (true)
1814 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001815 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001816 __node_pointer __prev = __e.__ptr_->__prev_;
1817 __node_alloc_traits::deallocate(__na, __e.__ptr_, 1);
1818 if (__prev == 0)
1819 break;
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001820#if _LIBCPP_DEBUG_LEVEL >= 2
1821 __e = iterator(__prev, this);
1822#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001823 __e = iterator(__prev);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001824#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001825 }
1826 throw;
1827 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001828#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant29f74322013-06-25 16:08:47 +00001829 __link_nodes(static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::
1830 pointer_to(base::__end_)), __r.__ptr_, __e.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001831 base::__sz() += __ds;
1832 }
1833}
1834
1835template <class _Tp, class _Alloc>
1836void
1837list<_Tp, _Alloc>::resize(size_type __n, const value_type& __x)
1838{
1839 if (__n < base::__sz())
1840 erase(__iterator(__n), end());
1841 else if (__n > base::__sz())
1842 {
1843 __n -= base::__sz();
1844 size_type __ds = 0;
1845 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001846 typedef __allocator_destructor<__node_allocator> _Dp;
1847 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001848 __hold->__prev_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001849 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001850 ++__ds;
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001851#if _LIBCPP_DEBUG_LEVEL >= 2
1852 iterator __r = iterator(__hold.release(), this);
1853#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001854 iterator __r = iterator(__hold.release());
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001855#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001856 iterator __e = __r;
1857#ifndef _LIBCPP_NO_EXCEPTIONS
1858 try
1859 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001860#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001861 for (--__n; __n != 0; --__n, ++__e, ++__ds)
1862 {
1863 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001864 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001865 __e.__ptr_->__next_ = __hold.get();
1866 __hold->__prev_ = __e.__ptr_;
1867 __hold.release();
1868 }
1869#ifndef _LIBCPP_NO_EXCEPTIONS
1870 }
1871 catch (...)
1872 {
1873 while (true)
1874 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001875 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001876 __node_pointer __prev = __e.__ptr_->__prev_;
1877 __node_alloc_traits::deallocate(__na, __e.__ptr_, 1);
1878 if (__prev == 0)
1879 break;
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001880#if _LIBCPP_DEBUG_LEVEL >= 2
1881 __e = iterator(__prev, this);
1882#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001883 __e = iterator(__prev);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001884#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001885 }
1886 throw;
1887 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001888#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant29f74322013-06-25 16:08:47 +00001889 __link_nodes(static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::
1890 pointer_to(base::__end_)), __r.__ptr_, __e.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001891 base::__sz() += __ds;
Howard Hinnant324bb032010-08-22 00:02:43 +00001892 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001893}
1894
1895template <class _Tp, class _Alloc>
1896void
1897list<_Tp, _Alloc>::splice(const_iterator __p, list& __c)
1898{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001899 _LIBCPP_ASSERT(this != &__c,
1900 "list::splice(iterator, list) called with this == &list");
1901#if _LIBCPP_DEBUG_LEVEL >= 2
1902 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1903 "list::splice(iterator, list) called with an iterator not"
1904 " referring to this list");
1905#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001906 if (!__c.empty())
1907 {
Howard Hinnant29f74322013-06-25 16:08:47 +00001908 __node_pointer __f = __c.__end_.__next_;
1909 __node_pointer __l = __c.__end_.__prev_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001910 base::__unlink_nodes(__f, __l);
Howard Hinnant29f74322013-06-25 16:08:47 +00001911 __link_nodes(__p.__ptr_, __f, __l);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001912 base::__sz() += __c.__sz();
1913 __c.__sz() = 0;
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001914#if _LIBCPP_DEBUG_LEVEL >= 2
1915 __libcpp_db* __db = __get_db();
1916 __c_node* __cn1 = __db->__find_c_and_lock(this);
1917 __c_node* __cn2 = __db->__find_c(&__c);
1918 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
1919 {
1920 --__p;
1921 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant29f74322013-06-25 16:08:47 +00001922 if (__i->__ptr_ != static_cast<__node_pointer>(
1923 pointer_traits<__node_base_pointer>::pointer_to(__c.__end_)))
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001924 {
1925 __cn1->__add(*__p);
1926 (*__p)->__c_ = __cn1;
1927 if (--__cn2->end_ != __p)
1928 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
1929 }
1930 }
1931 __db->unlock();
1932#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001933 }
1934}
1935
1936template <class _Tp, class _Alloc>
1937void
1938list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __i)
1939{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001940#if _LIBCPP_DEBUG_LEVEL >= 2
1941 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1942 "list::splice(iterator, list, iterator) called with first iterator not"
1943 " referring to this list");
1944 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__i) == &__c,
1945 "list::splice(iterator, list, iterator) called with second iterator not"
1946 " referring to list argument");
1947 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(&__i),
1948 "list::splice(iterator, list, iterator) called with second iterator not"
1949 " derefereceable");
1950#endif
1951 if (__p.__ptr_ != __i.__ptr_ && __p.__ptr_ != __i.__ptr_->__next_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001952 {
Howard Hinnant29f74322013-06-25 16:08:47 +00001953 __node_pointer __f = __i.__ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001954 base::__unlink_nodes(__f, __f);
Howard Hinnant29f74322013-06-25 16:08:47 +00001955 __link_nodes(__p.__ptr_, __f, __f);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001956 --__c.__sz();
1957 ++base::__sz();
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001958#if _LIBCPP_DEBUG_LEVEL >= 2
1959 __libcpp_db* __db = __get_db();
1960 __c_node* __cn1 = __db->__find_c_and_lock(this);
1961 __c_node* __cn2 = __db->__find_c(&__c);
1962 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
1963 {
1964 --__p;
1965 iterator* __j = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant29f74322013-06-25 16:08:47 +00001966 if (__j->__ptr_ == __f)
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001967 {
1968 __cn1->__add(*__p);
1969 (*__p)->__c_ = __cn1;
1970 if (--__cn2->end_ != __p)
1971 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
1972 }
1973 }
1974 __db->unlock();
1975#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001976 }
1977}
1978
1979template <class _Tp, class _Alloc>
1980void
1981list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l)
1982{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001983#if _LIBCPP_DEBUG_LEVEL >= 2
1984 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1985 "list::splice(iterator, list, iterator, iterator) called with first iterator not"
1986 " referring to this list");
1987 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__f) == &__c,
1988 "list::splice(iterator, list, iterator, iterator) called with second iterator not"
1989 " referring to list argument");
1990 if (this == &__c)
1991 {
1992 for (const_iterator __i = __f; __i != __l; ++__i)
1993 _LIBCPP_ASSERT(__i != __p,
1994 "list::splice(iterator, list, iterator, iterator)"
1995 " called with the first iterator within the range"
1996 " of the second and third iterators");
1997 }
1998#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001999 if (__f != __l)
2000 {
2001 if (this != &__c)
2002 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002003 size_type __s = _VSTD::distance(__f, __l);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002004 __c.__sz() -= __s;
2005 base::__sz() += __s;
2006 }
Howard Hinnant29f74322013-06-25 16:08:47 +00002007 __node_pointer __first = __f.__ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002008 --__l;
Howard Hinnant29f74322013-06-25 16:08:47 +00002009 __node_pointer __last = __l.__ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002010 base::__unlink_nodes(__first, __last);
Howard Hinnant29f74322013-06-25 16:08:47 +00002011 __link_nodes(__p.__ptr_, __first, __last);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00002012#if _LIBCPP_DEBUG_LEVEL >= 2
2013 __libcpp_db* __db = __get_db();
2014 __c_node* __cn1 = __db->__find_c_and_lock(this);
2015 __c_node* __cn2 = __db->__find_c(&__c);
2016 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
2017 {
2018 --__p;
2019 iterator* __j = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant29f74322013-06-25 16:08:47 +00002020 for (__node_pointer __k = __f.__ptr_;
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00002021 __k != __l.__ptr_; __k = __k->__next_)
2022 {
2023 if (__j->__ptr_ == __k)
2024 {
2025 __cn1->__add(*__p);
2026 (*__p)->__c_ = __cn1;
2027 if (--__cn2->end_ != __p)
2028 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
2029 }
2030 }
2031 }
2032 __db->unlock();
2033#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002034 }
2035}
2036
2037template <class _Tp, class _Alloc>
2038void
2039list<_Tp, _Alloc>::remove(const value_type& __x)
2040{
2041 for (iterator __i = begin(), __e = end(); __i != __e;)
2042 {
2043 if (*__i == __x)
2044 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002045 iterator __j = _VSTD::next(__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002046 for (; __j != __e && *__j == __x; ++__j)
2047 ;
2048 __i = erase(__i, __j);
2049 }
2050 else
2051 ++__i;
2052 }
2053}
2054
2055template <class _Tp, class _Alloc>
2056template <class _Pred>
2057void
2058list<_Tp, _Alloc>::remove_if(_Pred __pred)
2059{
2060 for (iterator __i = begin(), __e = end(); __i != __e;)
2061 {
2062 if (__pred(*__i))
2063 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002064 iterator __j = _VSTD::next(__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002065 for (; __j != __e && __pred(*__j); ++__j)
2066 ;
2067 __i = erase(__i, __j);
2068 }
2069 else
2070 ++__i;
2071 }
2072}
2073
2074template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00002075inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002076void
2077list<_Tp, _Alloc>::unique()
2078{
2079 unique(__equal_to<value_type>());
2080}
2081
2082template <class _Tp, class _Alloc>
2083template <class _BinaryPred>
2084void
2085list<_Tp, _Alloc>::unique(_BinaryPred __binary_pred)
2086{
2087 for (iterator __i = begin(), __e = end(); __i != __e;)
2088 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002089 iterator __j = _VSTD::next(__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002090 for (; __j != __e && __binary_pred(*__i, *__j); ++__j)
2091 ;
2092 if (++__i != __j)
2093 __i = erase(__i, __j);
2094 }
2095}
2096
2097template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00002098inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002099void
2100list<_Tp, _Alloc>::merge(list& __c)
2101{
2102 merge(__c, __less<value_type>());
2103}
2104
2105template <class _Tp, class _Alloc>
2106template <class _Comp>
2107void
2108list<_Tp, _Alloc>::merge(list& __c, _Comp __comp)
2109{
2110 if (this != &__c)
2111 {
2112 iterator __f1 = begin();
2113 iterator __e1 = end();
2114 iterator __f2 = __c.begin();
2115 iterator __e2 = __c.end();
2116 while (__f1 != __e1 && __f2 != __e2)
2117 {
2118 if (__comp(*__f2, *__f1))
2119 {
2120 size_type __ds = 1;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002121 iterator __m2 = _VSTD::next(__f2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002122 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2, ++__ds)
2123 ;
2124 base::__sz() += __ds;
2125 __c.__sz() -= __ds;
Howard Hinnant29f74322013-06-25 16:08:47 +00002126 __node_pointer __f = __f2.__ptr_;
2127 __node_pointer __l = __m2.__ptr_->__prev_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002128 __f2 = __m2;
2129 base::__unlink_nodes(__f, __l);
Howard Hinnant0949eed2011-06-30 21:18:19 +00002130 __m2 = _VSTD::next(__f1);
Howard Hinnant29f74322013-06-25 16:08:47 +00002131 __link_nodes(__f1.__ptr_, __f, __l);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002132 __f1 = __m2;
2133 }
2134 else
2135 ++__f1;
2136 }
2137 splice(__e1, __c);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00002138#if _LIBCPP_DEBUG_LEVEL >= 2
2139 __libcpp_db* __db = __get_db();
2140 __c_node* __cn1 = __db->__find_c_and_lock(this);
2141 __c_node* __cn2 = __db->__find_c(&__c);
2142 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
2143 {
2144 --__p;
2145 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant29f74322013-06-25 16:08:47 +00002146 if (__i->__ptr_ != static_cast<__node_pointer>(
2147 pointer_traits<__node_base_pointer>::pointer_to(__c.__end_)))
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00002148 {
2149 __cn1->__add(*__p);
2150 (*__p)->__c_ = __cn1;
2151 if (--__cn2->end_ != __p)
2152 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
2153 }
2154 }
2155 __db->unlock();
2156#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002157 }
2158}
2159
2160template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00002161inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002162void
2163list<_Tp, _Alloc>::sort()
2164{
2165 sort(__less<value_type>());
2166}
2167
2168template <class _Tp, class _Alloc>
2169template <class _Comp>
Howard Hinnant82894812010-09-22 16:48:34 +00002170inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002171void
2172list<_Tp, _Alloc>::sort(_Comp __comp)
2173{
2174 __sort(begin(), end(), base::__sz(), __comp);
2175}
2176
2177template <class _Tp, class _Alloc>
2178template <class _Comp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002179typename list<_Tp, _Alloc>::iterator
2180list<_Tp, _Alloc>::__sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp)
2181{
2182 switch (__n)
2183 {
2184 case 0:
2185 case 1:
2186 return __f1;
2187 case 2:
2188 if (__comp(*--__e2, *__f1))
2189 {
Howard Hinnant29f74322013-06-25 16:08:47 +00002190 __node_pointer __f = __e2.__ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002191 base::__unlink_nodes(__f, __f);
Howard Hinnant29f74322013-06-25 16:08:47 +00002192 __link_nodes(__f1.__ptr_, __f, __f);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002193 return __e2;
2194 }
2195 return __f1;
2196 }
2197 size_type __n2 = __n / 2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002198 iterator __e1 = _VSTD::next(__f1, __n2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002199 iterator __r = __f1 = __sort(__f1, __e1, __n2, __comp);
2200 iterator __f2 = __e1 = __sort(__e1, __e2, __n - __n2, __comp);
2201 if (__comp(*__f2, *__f1))
2202 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002203 iterator __m2 = _VSTD::next(__f2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002204 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2)
2205 ;
Howard Hinnant29f74322013-06-25 16:08:47 +00002206 __node_pointer __f = __f2.__ptr_;
2207 __node_pointer __l = __m2.__ptr_->__prev_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002208 __r = __f2;
2209 __e1 = __f2 = __m2;
2210 base::__unlink_nodes(__f, __l);
Howard Hinnant0949eed2011-06-30 21:18:19 +00002211 __m2 = _VSTD::next(__f1);
Howard Hinnant29f74322013-06-25 16:08:47 +00002212 __link_nodes(__f1.__ptr_, __f, __l);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002213 __f1 = __m2;
2214 }
2215 else
2216 ++__f1;
2217 while (__f1 != __e1 && __f2 != __e2)
2218 {
2219 if (__comp(*__f2, *__f1))
2220 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002221 iterator __m2 = _VSTD::next(__f2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002222 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2)
2223 ;
Howard Hinnant29f74322013-06-25 16:08:47 +00002224 __node_pointer __f = __f2.__ptr_;
2225 __node_pointer __l = __m2.__ptr_->__prev_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002226 if (__e1 == __f2)
2227 __e1 = __m2;
2228 __f2 = __m2;
2229 base::__unlink_nodes(__f, __l);
Howard Hinnant0949eed2011-06-30 21:18:19 +00002230 __m2 = _VSTD::next(__f1);
Howard Hinnant29f74322013-06-25 16:08:47 +00002231 __link_nodes(__f1.__ptr_, __f, __l);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002232 __f1 = __m2;
2233 }
2234 else
2235 ++__f1;
2236 }
2237 return __r;
2238}
2239
2240template <class _Tp, class _Alloc>
2241void
Howard Hinnantc5607272011-06-03 17:30:28 +00002242list<_Tp, _Alloc>::reverse() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002243{
2244 if (base::__sz() > 1)
2245 {
2246 iterator __e = end();
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00002247 for (iterator __i = begin(); __i.__ptr_ != __e.__ptr_;)
2248 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002249 _VSTD::swap(__i.__ptr_->__prev_, __i.__ptr_->__next_);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00002250 __i.__ptr_ = __i.__ptr_->__prev_;
2251 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002252 _VSTD::swap(__e.__ptr_->__prev_, __e.__ptr_->__next_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002253 }
2254}
2255
2256template <class _Tp, class _Alloc>
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00002257bool
2258list<_Tp, _Alloc>::__invariants() const
2259{
2260 return size() == _VSTD::distance(begin(), end());
2261}
2262
2263#if _LIBCPP_DEBUG_LEVEL >= 2
2264
2265template <class _Tp, class _Alloc>
2266bool
2267list<_Tp, _Alloc>::__dereferenceable(const const_iterator* __i) const
2268{
Howard Hinnant29f74322013-06-25 16:08:47 +00002269 return __i->__ptr_ != static_cast<__node_pointer>(
2270 pointer_traits<__node_base_pointer>::pointer_to(const_cast<__node_base&>(this->__end_)));
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00002271}
2272
2273template <class _Tp, class _Alloc>
2274bool
2275list<_Tp, _Alloc>::__decrementable(const const_iterator* __i) const
2276{
2277 return !empty() && __i->__ptr_ != base::__end_.__next_;
2278}
2279
2280template <class _Tp, class _Alloc>
2281bool
2282list<_Tp, _Alloc>::__addable(const const_iterator* __i, ptrdiff_t __n) const
2283{
2284 return false;
2285}
2286
2287template <class _Tp, class _Alloc>
2288bool
2289list<_Tp, _Alloc>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
2290{
2291 return false;
2292}
2293
2294#endif // _LIBCPP_DEBUG_LEVEL >= 2
2295
2296template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00002297inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002298bool
2299operator==(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2300{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002301 return __x.size() == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002302}
2303
2304template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00002305inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002306bool
2307operator< (const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2308{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002309 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002310}
2311
2312template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00002313inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002314bool
2315operator!=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2316{
2317 return !(__x == __y);
2318}
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{
2325 return __y < __x;
2326}
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{
2333 return !(__x < __y);
2334}
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 !(__y < __x);
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 +00002346void
2347swap(list<_Tp, _Alloc>& __x, list<_Tp, _Alloc>& __y)
Howard Hinnantc5607272011-06-03 17:30:28 +00002348 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002349{
2350 __x.swap(__y);
2351}
2352
2353_LIBCPP_END_NAMESPACE_STD
2354
2355#endif // _LIBCPP_LIST