blob: 7827b96f2aa0715e5269c215d74f0b0bf54ee668 [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);
43 list(size_type n, const value_type& value);
44 list(size_type n, const value_type& value, const allocator_type& a);
45 template <class Iter>
46 list(Iter first, Iter last);
47 template <class Iter>
48 list(Iter first, Iter last, const allocator_type& a);
49 list(const list& x);
50 list(const list&, const allocator_type& a);
Howard Hinnantc5607272011-06-03 17:30:28 +000051 list(list&& x)
52 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000053 list(list&&, const allocator_type& a);
54 list(initializer_list<value_type>);
55 list(initializer_list<value_type>, const allocator_type& a);
56
57 ~list();
58
59 list& operator=(const list& x);
Howard Hinnantc5607272011-06-03 17:30:28 +000060 list& operator=(list&& x)
61 noexcept(
62 allocator_type::propagate_on_container_move_assignment::value &&
63 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000064 list& operator=(initializer_list<value_type>);
65 template <class Iter>
66 void assign(Iter first, Iter last);
67 void assign(size_type n, const value_type& t);
68 void assign(initializer_list<value_type>);
69
Howard Hinnantc5607272011-06-03 17:30:28 +000070 allocator_type get_allocator() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000071
Howard Hinnantc5607272011-06-03 17:30:28 +000072 iterator begin() noexcept;
73 const_iterator begin() const noexcept;
74 iterator end() noexcept;
75 const_iterator end() const noexcept;
76 reverse_iterator rbegin() noexcept;
77 const_reverse_iterator rbegin() const noexcept;
78 reverse_iterator rend() noexcept;
79 const_reverse_iterator rend() const noexcept;
80 const_iterator cbegin() const noexcept;
81 const_iterator cend() const noexcept;
82 const_reverse_iterator crbegin() const noexcept;
83 const_reverse_iterator crend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000084
85 reference front();
86 const_reference front() const;
87 reference back();
88 const_reference back() const;
89
Howard Hinnantc5607272011-06-03 17:30:28 +000090 bool empty() const noexcept;
91 size_type size() const noexcept;
92 size_type max_size() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000093
94 template <class... Args>
95 void emplace_front(Args&&... args);
96 void pop_front();
97 template <class... Args>
98 void emplace_back(Args&&... args);
99 void pop_back();
100 void push_front(const value_type& x);
101 void push_front(value_type&& x);
102 void push_back(const value_type& x);
103 void push_back(value_type&& x);
104 template <class... Args>
105 iterator emplace(const_iterator position, Args&&... args);
106 iterator insert(const_iterator position, const value_type& x);
107 iterator insert(const_iterator position, value_type&& x);
108 iterator insert(const_iterator position, size_type n, const value_type& x);
109 template <class Iter>
110 iterator insert(const_iterator position, Iter first, Iter last);
111 iterator insert(const_iterator position, initializer_list<value_type> il);
112
113 iterator erase(const_iterator position);
114 iterator erase(const_iterator position, const_iterator last);
115
116 void resize(size_type sz);
117 void resize(size_type sz, const value_type& c);
118
Howard Hinnantc5607272011-06-03 17:30:28 +0000119 void swap(list&)
120 noexcept(!allocator_type::propagate_on_container_swap::value ||
121 __is_nothrow_swappable<allocator_type>::value);
122 void clear() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000123
124 void splice(const_iterator position, list& x);
125 void splice(const_iterator position, list&& x);
126 void splice(const_iterator position, list& x, const_iterator i);
127 void splice(const_iterator position, list&& x, const_iterator i);
128 void splice(const_iterator position, list& x, const_iterator first,
129 const_iterator last);
130 void splice(const_iterator position, list&& x, const_iterator first,
131 const_iterator last);
132
133 void remove(const value_type& value);
134 template <class Pred> void remove_if(Pred pred);
135 void unique();
136 template <class BinaryPredicate>
137 void unique(BinaryPredicate binary_pred);
138 void merge(list& x);
139 void merge(list&& x);
140 template <class Compare>
141 void merge(list& x, Compare comp);
142 template <class Compare>
143 void merge(list&& x, Compare comp);
144 void sort();
145 template <class Compare>
146 void sort(Compare comp);
Howard Hinnantc5607272011-06-03 17:30:28 +0000147 void reverse() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000148};
149
150template <class T, class Alloc>
151 bool operator==(const list<T,Alloc>& x, const list<T,Alloc>& y);
152template <class T, class Alloc>
153 bool operator< (const list<T,Alloc>& x, const list<T,Alloc>& y);
154template <class T, class Alloc>
155 bool operator!=(const list<T,Alloc>& x, const list<T,Alloc>& y);
156template <class T, class Alloc>
157 bool operator> (const list<T,Alloc>& x, const list<T,Alloc>& y);
158template <class T, class Alloc>
159 bool operator>=(const list<T,Alloc>& x, const list<T,Alloc>& y);
160template <class T, class Alloc>
161 bool operator<=(const list<T,Alloc>& x, const list<T,Alloc>& y);
162
163template <class T, class Alloc>
Howard Hinnantc5607272011-06-03 17:30:28 +0000164 void swap(list<T,Alloc>& x, list<T,Alloc>& y)
165 noexcept(noexcept(x.swap(y)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000166
167} // std
168
169*/
170
171#include <__config>
172
173#include <memory>
174#include <limits>
175#include <initializer_list>
176#include <iterator>
177#include <algorithm>
178
Howard Hinnant66c6f972011-11-29 16:45:27 +0000179#include <__undef_min_max>
180
Howard Hinnant5e571422013-08-23 20:10:18 +0000181#ifdef _LIBCPP_DEBUG
Howard Hinnant8b00e6c2013-08-02 00:26:35 +0000182# include <__debug>
183#else
184# define _LIBCPP_ASSERT(x, m) ((void)0)
185#endif
186
Howard Hinnant08e17472011-10-17 20:05:10 +0000187#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000188#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000189#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000190
191_LIBCPP_BEGIN_NAMESPACE_STD
192
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000193template <class _Tp, class _VoidPtr> struct __list_node;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000194
195template <class _Tp, class _VoidPtr>
196struct __list_node_base
197{
198 typedef typename pointer_traits<_VoidPtr>::template
199#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
200 rebind<__list_node<_Tp, _VoidPtr> > pointer;
201#else
202 rebind<__list_node<_Tp, _VoidPtr> >::other pointer;
203#endif
204
Howard Hinnant29f74322013-06-25 16:08:47 +0000205 typedef typename pointer_traits<_VoidPtr>::template
206#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
207 rebind<__list_node_base> __base_pointer;
208#else
209 rebind<__list_node_base>::other __base_pointer;
210#endif
211
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000212 pointer __prev_;
213 pointer __next_;
214
Howard Hinnant82894812010-09-22 16:48:34 +0000215 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000216 __list_node_base()
Howard Hinnant29f74322013-06-25 16:08:47 +0000217 : __prev_(static_cast<pointer>(pointer_traits<__base_pointer>::pointer_to(*this))),
218 __next_(static_cast<pointer>(pointer_traits<__base_pointer>::pointer_to(*this)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000219 {}
220};
221
222template <class _Tp, class _VoidPtr>
223struct __list_node
224 : public __list_node_base<_Tp, _VoidPtr>
225{
226 _Tp __value_;
227};
228
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000229template <class _Tp, class _Alloc> class _LIBCPP_TYPE_VIS_ONLY list;
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000230template <class _Tp, class _Alloc> class __list_imp;
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000231template <class _Tp, class _VoidPtr> class _LIBCPP_TYPE_VIS_ONLY __list_const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000232
233template <class _Tp, class _VoidPtr>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000234class _LIBCPP_TYPE_VIS_ONLY __list_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000235{
236 typedef typename pointer_traits<_VoidPtr>::template
237#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
238 rebind<__list_node<_Tp, _VoidPtr> > __node_pointer;
239#else
240 rebind<__list_node<_Tp, _VoidPtr> >::other __node_pointer;
241#endif
242
243 __node_pointer __ptr_;
244
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000245#if _LIBCPP_DEBUG_LEVEL >= 2
246 _LIBCPP_INLINE_VISIBILITY
247 explicit __list_iterator(__node_pointer __p, const void* __c) _NOEXCEPT
248 : __ptr_(__p)
249 {
250 __get_db()->__insert_ic(this, __c);
251 }
252#else
Howard Hinnant82894812010-09-22 16:48:34 +0000253 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000254 explicit __list_iterator(__node_pointer __p) _NOEXCEPT : __ptr_(__p) {}
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000255#endif
256
257
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000258
259 template<class, class> friend class list;
260 template<class, class> friend class __list_imp;
261 template<class, class> friend class __list_const_iterator;
262public:
263 typedef bidirectional_iterator_tag iterator_category;
264 typedef _Tp value_type;
265 typedef value_type& reference;
266 typedef typename pointer_traits<_VoidPtr>::template
267#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
268 rebind<value_type>
269#else
270 rebind<value_type>::other
271#endif
272 pointer;
273 typedef typename pointer_traits<pointer>::difference_type difference_type;
274
Howard Hinnant82894812010-09-22 16:48:34 +0000275 _LIBCPP_INLINE_VISIBILITY
Marshall Clow65d2e6a2013-08-05 21:23:28 +0000276 __list_iterator() _NOEXCEPT : __ptr_(nullptr)
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000277 {
278#if _LIBCPP_DEBUG_LEVEL >= 2
279 __get_db()->__insert_i(this);
280#endif
281 }
282
283#if _LIBCPP_DEBUG_LEVEL >= 2
284
Howard Hinnant211f0ee2011-01-28 23:46:28 +0000285 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000286 __list_iterator(const __list_iterator& __p)
287 : __ptr_(__p.__ptr_)
288 {
289 __get_db()->__iterator_copy(this, &__p);
290 }
291
292 _LIBCPP_INLINE_VISIBILITY
293 ~__list_iterator()
294 {
295 __get_db()->__erase_i(this);
296 }
297
298 _LIBCPP_INLINE_VISIBILITY
299 __list_iterator& operator=(const __list_iterator& __p)
300 {
301 if (this != &__p)
302 {
303 __get_db()->__iterator_copy(this, &__p);
304 __ptr_ = __p.__ptr_;
305 }
306 return *this;
307 }
308
309#endif // _LIBCPP_DEBUG_LEVEL >= 2
310
311 _LIBCPP_INLINE_VISIBILITY
312 reference operator*() const
313 {
314#if _LIBCPP_DEBUG_LEVEL >= 2
315 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
316 "Attempted to dereference a non-dereferenceable list::iterator");
317#endif
318 return __ptr_->__value_;
319 }
Howard Hinnant82894812010-09-22 16:48:34 +0000320 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant29f74322013-06-25 16:08:47 +0000321 pointer operator->() const
322 {
323#if _LIBCPP_DEBUG_LEVEL >= 2
324 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
325 "Attempted to dereference a non-dereferenceable list::iterator");
326#endif
327 return pointer_traits<pointer>::pointer_to(__ptr_->__value_);
328 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000329
Howard Hinnant82894812010-09-22 16:48:34 +0000330 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000331 __list_iterator& operator++()
332 {
333#if _LIBCPP_DEBUG_LEVEL >= 2
334 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
335 "Attempted to increment non-incrementable list::iterator");
336#endif
337 __ptr_ = __ptr_->__next_;
338 return *this;
339 }
Howard Hinnant82894812010-09-22 16:48:34 +0000340 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000341 __list_iterator operator++(int) {__list_iterator __t(*this); ++(*this); return __t;}
342
Howard Hinnant82894812010-09-22 16:48:34 +0000343 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000344 __list_iterator& operator--()
345 {
346#if _LIBCPP_DEBUG_LEVEL >= 2
347 _LIBCPP_ASSERT(__get_const_db()->__decrementable(this),
348 "Attempted to decrement non-decrementable list::iterator");
349#endif
350 __ptr_ = __ptr_->__prev_;
351 return *this;
352 }
Howard Hinnant82894812010-09-22 16:48:34 +0000353 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000354 __list_iterator operator--(int) {__list_iterator __t(*this); --(*this); return __t;}
355
Howard Hinnant82894812010-09-22 16:48:34 +0000356 friend _LIBCPP_INLINE_VISIBILITY
357 bool operator==(const __list_iterator& __x, const __list_iterator& __y)
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000358 {
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000359 return __x.__ptr_ == __y.__ptr_;
360 }
Howard Hinnant82894812010-09-22 16:48:34 +0000361 friend _LIBCPP_INLINE_VISIBILITY
362 bool operator!=(const __list_iterator& __x, const __list_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000363 {return !(__x == __y);}
364};
365
366template <class _Tp, class _VoidPtr>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000367class _LIBCPP_TYPE_VIS_ONLY __list_const_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000368{
369 typedef typename pointer_traits<_VoidPtr>::template
370#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnant29f74322013-06-25 16:08:47 +0000371 rebind<__list_node<_Tp, _VoidPtr> > __node_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000372#else
Howard Hinnant29f74322013-06-25 16:08:47 +0000373 rebind<__list_node<_Tp, _VoidPtr> >::other __node_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000374#endif
375
376 __node_pointer __ptr_;
377
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000378#if _LIBCPP_DEBUG_LEVEL >= 2
379 _LIBCPP_INLINE_VISIBILITY
380 explicit __list_const_iterator(__node_pointer __p, const void* __c) _NOEXCEPT
381 : __ptr_(__p)
382 {
383 __get_db()->__insert_ic(this, __c);
384 }
385#else
Howard Hinnant82894812010-09-22 16:48:34 +0000386 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000387 explicit __list_const_iterator(__node_pointer __p) _NOEXCEPT : __ptr_(__p) {}
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000388#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000389
390 template<class, class> friend class list;
391 template<class, class> friend class __list_imp;
392public:
393 typedef bidirectional_iterator_tag iterator_category;
394 typedef _Tp value_type;
395 typedef const value_type& reference;
396 typedef typename pointer_traits<_VoidPtr>::template
397#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
398 rebind<const value_type>
399#else
400 rebind<const value_type>::other
401#endif
402 pointer;
403 typedef typename pointer_traits<pointer>::difference_type difference_type;
404
Howard Hinnant82894812010-09-22 16:48:34 +0000405 _LIBCPP_INLINE_VISIBILITY
Marshall Clow65d2e6a2013-08-05 21:23:28 +0000406 __list_const_iterator() _NOEXCEPT : __ptr_(nullptr)
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000407 {
408#if _LIBCPP_DEBUG_LEVEL >= 2
409 __get_db()->__insert_i(this);
410#endif
411 }
Howard Hinnant211f0ee2011-01-28 23:46:28 +0000412 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6dcaf3e2013-04-05 17:58:52 +0000413 __list_const_iterator(const __list_iterator<_Tp, _VoidPtr>& __p) _NOEXCEPT
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000414 : __ptr_(__p.__ptr_)
415 {
416#if _LIBCPP_DEBUG_LEVEL >= 2
417 __get_db()->__iterator_copy(this, &__p);
418#endif
419 }
420
421#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000422
Howard Hinnant82894812010-09-22 16:48:34 +0000423 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000424 __list_const_iterator(const __list_const_iterator& __p)
425 : __ptr_(__p.__ptr_)
426 {
427 __get_db()->__iterator_copy(this, &__p);
428 }
429
430 _LIBCPP_INLINE_VISIBILITY
431 ~__list_const_iterator()
432 {
433 __get_db()->__erase_i(this);
434 }
435
436 _LIBCPP_INLINE_VISIBILITY
437 __list_const_iterator& operator=(const __list_const_iterator& __p)
438 {
439 if (this != &__p)
440 {
441 __get_db()->__iterator_copy(this, &__p);
442 __ptr_ = __p.__ptr_;
443 }
444 return *this;
445 }
446
447#endif // _LIBCPP_DEBUG_LEVEL >= 2
448 _LIBCPP_INLINE_VISIBILITY
449 reference operator*() const
450 {
451#if _LIBCPP_DEBUG_LEVEL >= 2
452 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
453 "Attempted to dereference a non-dereferenceable list::const_iterator");
454#endif
455 return __ptr_->__value_;
456 }
Howard Hinnant82894812010-09-22 16:48:34 +0000457 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant29f74322013-06-25 16:08:47 +0000458 pointer operator->() const
459 {
460#if _LIBCPP_DEBUG_LEVEL >= 2
461 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
462 "Attempted to dereference a non-dereferenceable list::iterator");
463#endif
464 return pointer_traits<pointer>::pointer_to(__ptr_->__value_);
465 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000466
Howard Hinnant82894812010-09-22 16:48:34 +0000467 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000468 __list_const_iterator& operator++()
469 {
470#if _LIBCPP_DEBUG_LEVEL >= 2
471 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
472 "Attempted to increment non-incrementable list::const_iterator");
473#endif
474 __ptr_ = __ptr_->__next_;
475 return *this;
476 }
Howard Hinnant82894812010-09-22 16:48:34 +0000477 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000478 __list_const_iterator operator++(int) {__list_const_iterator __t(*this); ++(*this); return __t;}
479
Howard Hinnant82894812010-09-22 16:48:34 +0000480 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000481 __list_const_iterator& operator--()
482 {
483#if _LIBCPP_DEBUG_LEVEL >= 2
484 _LIBCPP_ASSERT(__get_const_db()->__decrementable(this),
485 "Attempted to decrement non-decrementable list::const_iterator");
486#endif
487 __ptr_ = __ptr_->__prev_;
488 return *this;
489 }
Howard Hinnant82894812010-09-22 16:48:34 +0000490 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000491 __list_const_iterator operator--(int) {__list_const_iterator __t(*this); --(*this); return __t;}
492
Howard Hinnant82894812010-09-22 16:48:34 +0000493 friend _LIBCPP_INLINE_VISIBILITY
494 bool operator==(const __list_const_iterator& __x, const __list_const_iterator& __y)
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000495 {
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000496 return __x.__ptr_ == __y.__ptr_;
497 }
Howard Hinnant82894812010-09-22 16:48:34 +0000498 friend _LIBCPP_INLINE_VISIBILITY
499 bool operator!=(const __list_const_iterator& __x, const __list_const_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000500 {return !(__x == __y);}
501};
502
503template <class _Tp, class _Alloc>
504class __list_imp
505{
506 __list_imp(const __list_imp&);
507 __list_imp& operator=(const __list_imp&);
508protected:
509 typedef _Tp value_type;
510 typedef _Alloc allocator_type;
511 typedef allocator_traits<allocator_type> __alloc_traits;
512 typedef typename __alloc_traits::size_type size_type;
513 typedef typename __alloc_traits::void_pointer __void_pointer;
514 typedef __list_iterator<value_type, __void_pointer> iterator;
515 typedef __list_const_iterator<value_type, __void_pointer> const_iterator;
516 typedef __list_node_base<value_type, __void_pointer> __node_base;
517 typedef __list_node<value_type, __void_pointer> __node;
518 typedef typename __alloc_traits::template
519#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
520 rebind_alloc<__node>
521#else
522 rebind_alloc<__node>::other
523#endif
524 __node_allocator;
525 typedef allocator_traits<__node_allocator> __node_alloc_traits;
526 typedef typename __node_alloc_traits::pointer __node_pointer;
Howard Hinnant29f74322013-06-25 16:08:47 +0000527 typedef typename __node_alloc_traits::pointer __node_const_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000528 typedef typename __alloc_traits::pointer pointer;
529 typedef typename __alloc_traits::const_pointer const_pointer;
530 typedef typename __alloc_traits::difference_type difference_type;
531
Howard Hinnant29f74322013-06-25 16:08:47 +0000532 typedef typename __alloc_traits::template
533#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
534 rebind_alloc<__node_base>
535#else
536 rebind_alloc<__node_base>::other
537#endif
538 __node_base_allocator;
539 typedef typename allocator_traits<__node_base_allocator>::pointer __node_base_pointer;
540
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000541 __node_base __end_;
542 __compressed_pair<size_type, __node_allocator> __size_alloc_;
543
Howard Hinnant82894812010-09-22 16:48:34 +0000544 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000545 size_type& __sz() _NOEXCEPT {return __size_alloc_.first();}
Howard Hinnant82894812010-09-22 16:48:34 +0000546 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000547 const size_type& __sz() const _NOEXCEPT
548 {return __size_alloc_.first();}
Howard Hinnant82894812010-09-22 16:48:34 +0000549 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000550 __node_allocator& __node_alloc() _NOEXCEPT
551 {return __size_alloc_.second();}
Howard Hinnant82894812010-09-22 16:48:34 +0000552 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000553 const __node_allocator& __node_alloc() const _NOEXCEPT
554 {return __size_alloc_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000555
Howard Hinnant29f74322013-06-25 16:08:47 +0000556 static void __unlink_nodes(__node_pointer __f, __node_pointer __l) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000557
Howard Hinnantc5607272011-06-03 17:30:28 +0000558 __list_imp()
559 _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000560 __list_imp(const allocator_type& __a);
561 ~__list_imp();
Howard Hinnantc5607272011-06-03 17:30:28 +0000562 void clear() _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +0000563 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000564 bool empty() const _NOEXCEPT {return __sz() == 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000565
Howard Hinnant82894812010-09-22 16:48:34 +0000566 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000567 iterator begin() _NOEXCEPT
568 {
569#if _LIBCPP_DEBUG_LEVEL >= 2
570 return iterator(__end_.__next_, this);
571#else
572 return iterator(__end_.__next_);
573#endif
574 }
Howard Hinnant82894812010-09-22 16:48:34 +0000575 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000576 const_iterator begin() const _NOEXCEPT
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000577 {
578#if _LIBCPP_DEBUG_LEVEL >= 2
579 return const_iterator(__end_.__next_, this);
580#else
581 return const_iterator(__end_.__next_);
582#endif
583 }
Howard Hinnant82894812010-09-22 16:48:34 +0000584 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000585 iterator end() _NOEXCEPT
586 {
587#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant29f74322013-06-25 16:08:47 +0000588 return iterator(static_cast<__node_pointer>(
589 pointer_traits<__node_base_pointer>::pointer_to(__end_)), this);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000590#else
Howard Hinnant29f74322013-06-25 16:08:47 +0000591 return iterator(static_cast<__node_pointer>(
592 pointer_traits<__node_base_pointer>::pointer_to(__end_)));
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000593#endif
594 }
Howard Hinnant82894812010-09-22 16:48:34 +0000595 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000596 const_iterator end() const _NOEXCEPT
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000597 {
598#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant29f74322013-06-25 16:08:47 +0000599 return const_iterator(static_cast<__node_const_pointer>(
600 pointer_traits<__node_base_pointer>::pointer_to(const_cast<__node_base&>(__end_))), this);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000601#else
Howard Hinnant29f74322013-06-25 16:08:47 +0000602 return const_iterator(static_cast<__node_const_pointer>(
603 pointer_traits<__node_base_pointer>::pointer_to(const_cast<__node_base&>(__end_))));
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000604#endif
605 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000606
Howard Hinnantc5607272011-06-03 17:30:28 +0000607 void swap(__list_imp& __c)
608 _NOEXCEPT_(!__node_alloc_traits::propagate_on_container_swap::value ||
609 __is_nothrow_swappable<__node_allocator>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000610
Howard Hinnant82894812010-09-22 16:48:34 +0000611 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000612 void __copy_assign_alloc(const __list_imp& __c)
613 {__copy_assign_alloc(__c, integral_constant<bool,
614 __node_alloc_traits::propagate_on_container_copy_assignment::value>());}
615
Howard Hinnant82894812010-09-22 16:48:34 +0000616 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000617 void __move_assign_alloc(__list_imp& __c)
Howard Hinnantc5607272011-06-03 17:30:28 +0000618 _NOEXCEPT_(
619 !__node_alloc_traits::propagate_on_container_move_assignment::value ||
620 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000621 {__move_assign_alloc(__c, integral_constant<bool,
622 __node_alloc_traits::propagate_on_container_move_assignment::value>());}
623
624private:
Howard Hinnant82894812010-09-22 16:48:34 +0000625 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000626 static void __swap_alloc(__node_allocator& __x, __node_allocator& __y)
Howard Hinnantc5607272011-06-03 17:30:28 +0000627 _NOEXCEPT_(!__node_alloc_traits::propagate_on_container_swap::value ||
628 __is_nothrow_swappable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000629 {__swap_alloc(__x, __y, integral_constant<bool,
630 __node_alloc_traits::propagate_on_container_swap::value>());}
Howard Hinnant82894812010-09-22 16:48:34 +0000631 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000632 static void __swap_alloc(__node_allocator& __x, __node_allocator& __y, true_type)
Howard Hinnantc5607272011-06-03 17:30:28 +0000633 _NOEXCEPT_(__is_nothrow_swappable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000634 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000635 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000636 swap(__x, __y);
637 }
Howard Hinnant82894812010-09-22 16:48:34 +0000638 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000639 static void __swap_alloc(__node_allocator& __x, __node_allocator& __y, false_type)
Howard Hinnantc5607272011-06-03 17:30:28 +0000640 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000641 {}
642
Howard Hinnant82894812010-09-22 16:48:34 +0000643 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000644 void __copy_assign_alloc(const __list_imp& __c, true_type)
645 {
646 if (__node_alloc() != __c.__node_alloc())
647 clear();
648 __node_alloc() = __c.__node_alloc();
649 }
650
Howard Hinnant82894812010-09-22 16:48:34 +0000651 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000652 void __copy_assign_alloc(const __list_imp& __c, false_type)
653 {}
654
Howard Hinnant82894812010-09-22 16:48:34 +0000655 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31 +0000656 void __move_assign_alloc(__list_imp& __c, true_type)
Howard Hinnantc5607272011-06-03 17:30:28 +0000657 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000658 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000659 __node_alloc() = _VSTD::move(__c.__node_alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000660 }
661
Howard Hinnant82894812010-09-22 16:48:34 +0000662 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31 +0000663 void __move_assign_alloc(__list_imp& __c, false_type)
Howard Hinnantc5607272011-06-03 17:30:28 +0000664 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000665 {}
666};
667
668// Unlink nodes [__f, __l]
669template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +0000670inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000671void
Howard Hinnant29f74322013-06-25 16:08:47 +0000672__list_imp<_Tp, _Alloc>::__unlink_nodes(__node_pointer __f, __node_pointer __l)
Howard Hinnantc5607272011-06-03 17:30:28 +0000673 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000674{
Howard Hinnant29f74322013-06-25 16:08:47 +0000675 __f->__prev_->__next_ = __l->__next_;
676 __l->__next_->__prev_ = __f->__prev_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000677}
678
679template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +0000680inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000681__list_imp<_Tp, _Alloc>::__list_imp()
Howard Hinnantc5607272011-06-03 17:30:28 +0000682 _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000683 : __size_alloc_(0)
684{
685}
686
687template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +0000688inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000689__list_imp<_Tp, _Alloc>::__list_imp(const allocator_type& __a)
690 : __size_alloc_(0, __node_allocator(__a))
691{
692}
693
694template <class _Tp, class _Alloc>
695__list_imp<_Tp, _Alloc>::~__list_imp()
696{
697 clear();
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000698#if _LIBCPP_DEBUG_LEVEL >= 2
699 __get_db()->__erase_c(this);
700#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000701}
702
703template <class _Tp, class _Alloc>
704void
Howard Hinnantc5607272011-06-03 17:30:28 +0000705__list_imp<_Tp, _Alloc>::clear() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000706{
707 if (!empty())
708 {
709 __node_allocator& __na = __node_alloc();
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000710 __node_pointer __f = __end_.__next_;
Howard Hinnant29f74322013-06-25 16:08:47 +0000711 __node_pointer __l = static_cast<__node_pointer>(
712 pointer_traits<__node_base_pointer>::pointer_to(__end_));
713 __unlink_nodes(__f, __l->__prev_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000714 __sz() = 0;
715 while (__f != __l)
716 {
Howard Hinnant29f74322013-06-25 16:08:47 +0000717 __node_pointer __n = __f;
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000718 __f = __f->__next_;
Howard Hinnant29f74322013-06-25 16:08:47 +0000719 __node_alloc_traits::destroy(__na, _VSTD::addressof(__n->__value_));
720 __node_alloc_traits::deallocate(__na, __n, 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000721 }
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000722#if _LIBCPP_DEBUG_LEVEL >= 2
723 __c_node* __c = __get_db()->__find_c_and_lock(this);
724 for (__i_node** __p = __c->end_; __p != __c->beg_; )
725 {
726 --__p;
727 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
728 if (__i->__ptr_ != __l)
729 {
730 (*__p)->__c_ = nullptr;
731 if (--__c->end_ != __p)
732 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
733 }
734 }
735 __get_db()->unlock();
736#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000737 }
738}
739
740template <class _Tp, class _Alloc>
741void
742__list_imp<_Tp, _Alloc>::swap(__list_imp& __c)
Howard Hinnantc5607272011-06-03 17:30:28 +0000743 _NOEXCEPT_(!__node_alloc_traits::propagate_on_container_swap::value ||
744 __is_nothrow_swappable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000745{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000746 _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
747 this->__node_alloc() == __c.__node_alloc(),
748 "list::swap: Either propagate_on_container_swap must be true"
749 " or the allocators must compare equal");
Howard Hinnant0949eed2011-06-30 21:18:19 +0000750 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000751 __swap_alloc(__node_alloc(), __c.__node_alloc());
752 swap(__sz(), __c.__sz());
753 swap(__end_, __c.__end_);
754 if (__sz() == 0)
Howard Hinnant29f74322013-06-25 16:08:47 +0000755 __end_.__next_ = __end_.__prev_ = static_cast<__node_pointer>(
756 pointer_traits<__node_base_pointer>::pointer_to(__end_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000757 else
758 __end_.__prev_->__next_ = __end_.__next_->__prev_
Howard Hinnant29f74322013-06-25 16:08:47 +0000759 = static_cast<__node_pointer>(
760 pointer_traits<__node_base_pointer>::pointer_to(__end_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000761 if (__c.__sz() == 0)
762 __c.__end_.__next_ = __c.__end_.__prev_
Howard Hinnant29f74322013-06-25 16:08:47 +0000763 = static_cast<__node_pointer>(
764 pointer_traits<__node_base_pointer>::pointer_to(__c.__end_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000765 else
766 __c.__end_.__prev_->__next_ = __c.__end_.__next_->__prev_
Howard Hinnant29f74322013-06-25 16:08:47 +0000767 = static_cast<__node_pointer>(
768 pointer_traits<__node_base_pointer>::pointer_to(__c.__end_));
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000769#if _LIBCPP_DEBUG_LEVEL >= 2
770 __libcpp_db* __db = __get_db();
771 __c_node* __cn1 = __db->__find_c_and_lock(this);
772 __c_node* __cn2 = __db->__find_c(&__c);
773 std::swap(__cn1->beg_, __cn2->beg_);
774 std::swap(__cn1->end_, __cn2->end_);
775 std::swap(__cn1->cap_, __cn2->cap_);
776 for (__i_node** __p = __cn1->end_; __p != __cn1->beg_;)
777 {
778 --__p;
779 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
Howard Hinnant29f74322013-06-25 16:08:47 +0000780 if (__i->__ptr_ == static_cast<__node_pointer>(
781 pointer_traits<__node_base_pointer>::pointer_to(__c.__end_)))
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000782 {
783 __cn2->__add(*__p);
784 if (--__cn1->end_ != __p)
785 memmove(__p, __p+1, (__cn1->end_ - __p)*sizeof(__i_node*));
786 }
787 else
788 (*__p)->__c_ = __cn1;
789 }
790 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
791 {
792 --__p;
793 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
Howard Hinnant29f74322013-06-25 16:08:47 +0000794 if (__i->__ptr_ == static_cast<__node_pointer>(
795 pointer_traits<__node_base_pointer>::pointer_to(__end_)))
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000796 {
797 __cn1->__add(*__p);
798 if (--__cn2->end_ != __p)
799 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
800 }
801 else
802 (*__p)->__c_ = __cn2;
803 }
804 __db->unlock();
805#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000806}
807
808template <class _Tp, class _Alloc = allocator<_Tp> >
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000809class _LIBCPP_TYPE_VIS_ONLY list
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000810 : private __list_imp<_Tp, _Alloc>
811{
812 typedef __list_imp<_Tp, _Alloc> base;
813 typedef typename base::__node __node;
814 typedef typename base::__node_allocator __node_allocator;
815 typedef typename base::__node_pointer __node_pointer;
816 typedef typename base::__node_alloc_traits __node_alloc_traits;
Howard Hinnant29f74322013-06-25 16:08:47 +0000817 typedef typename base::__node_base __node_base;
818 typedef typename base::__node_base_pointer __node_base_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000819
820public:
821 typedef _Tp value_type;
822 typedef _Alloc allocator_type;
823 static_assert((is_same<value_type, typename allocator_type::value_type>::value),
824 "Invalid allocator::value_type");
825 typedef value_type& reference;
826 typedef const value_type& const_reference;
827 typedef typename base::pointer pointer;
828 typedef typename base::const_pointer const_pointer;
829 typedef typename base::size_type size_type;
830 typedef typename base::difference_type difference_type;
831 typedef typename base::iterator iterator;
832 typedef typename base::const_iterator const_iterator;
Howard Hinnant0949eed2011-06-30 21:18:19 +0000833 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
834 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000835
Howard Hinnant82894812010-09-22 16:48:34 +0000836 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000837 list()
838 _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value)
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000839 {
840#if _LIBCPP_DEBUG_LEVEL >= 2
841 __get_db()->__insert_c(this);
842#endif
843 }
Howard Hinnant82894812010-09-22 16:48:34 +0000844 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000845 list(const allocator_type& __a) : base(__a)
846 {
847#if _LIBCPP_DEBUG_LEVEL >= 2
848 __get_db()->__insert_c(this);
849#endif
850 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000851 list(size_type __n);
852 list(size_type __n, const value_type& __x);
853 list(size_type __n, const value_type& __x, const allocator_type& __a);
854 template <class _InpIter>
855 list(_InpIter __f, _InpIter __l,
856 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
857 template <class _InpIter>
858 list(_InpIter __f, _InpIter __l, const allocator_type& __a,
859 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
860
861 list(const list& __c);
862 list(const list& __c, const allocator_type& __a);
863 list& operator=(const list& __c);
Howard Hinnante3e32912011-08-12 21:56:02 +0000864#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000865 list(initializer_list<value_type> __il);
866 list(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnante3e32912011-08-12 21:56:02 +0000867#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant73d21a42010-09-04 23:28:19 +0000868#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc5607272011-06-03 17:30:28 +0000869 list(list&& __c)
870 _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000871 list(list&& __c, const allocator_type& __a);
Howard Hinnantc5607272011-06-03 17:30:28 +0000872 list& operator=(list&& __c)
873 _NOEXCEPT_(
874 __node_alloc_traits::propagate_on_container_move_assignment::value &&
875 is_nothrow_move_assignable<__node_allocator>::value);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000876#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnante3e32912011-08-12 21:56:02 +0000877#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant82894812010-09-22 16:48:34 +0000878 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000879 list& operator=(initializer_list<value_type> __il)
880 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnante3e32912011-08-12 21:56:02 +0000881#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000882
883 template <class _InpIter>
884 void assign(_InpIter __f, _InpIter __l,
885 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
886 void assign(size_type __n, const value_type& __x);
Howard Hinnante3e32912011-08-12 21:56:02 +0000887#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant82894812010-09-22 16:48:34 +0000888 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000889 void assign(initializer_list<value_type> __il)
890 {assign(__il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +0000891#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000892
Howard Hinnantc5607272011-06-03 17:30:28 +0000893 allocator_type get_allocator() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000894
Howard Hinnant82894812010-09-22 16:48:34 +0000895 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000896 size_type size() const _NOEXCEPT {return base::__sz();}
Howard Hinnant82894812010-09-22 16:48:34 +0000897 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000898 bool empty() const _NOEXCEPT {return base::empty();}
Howard Hinnant82894812010-09-22 16:48:34 +0000899 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000900 size_type max_size() const _NOEXCEPT
901 {return numeric_limits<difference_type>::max();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000902
Howard Hinnant82894812010-09-22 16:48:34 +0000903 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000904 iterator begin() _NOEXCEPT {return base::begin();}
Howard Hinnant82894812010-09-22 16:48:34 +0000905 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000906 const_iterator begin() const _NOEXCEPT {return base::begin();}
Howard Hinnant82894812010-09-22 16:48:34 +0000907 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000908 iterator end() _NOEXCEPT {return base::end();}
Howard Hinnant82894812010-09-22 16:48:34 +0000909 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000910 const_iterator end() const _NOEXCEPT {return base::end();}
Howard Hinnant82894812010-09-22 16:48:34 +0000911 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000912 const_iterator cbegin() const _NOEXCEPT {return base::begin();}
Howard Hinnant82894812010-09-22 16:48:34 +0000913 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000914 const_iterator cend() const _NOEXCEPT {return base::end();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000915
Howard Hinnant82894812010-09-22 16:48:34 +0000916 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000917 reverse_iterator rbegin() _NOEXCEPT
918 {return reverse_iterator(end());}
Howard Hinnant82894812010-09-22 16:48:34 +0000919 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000920 const_reverse_iterator rbegin() const _NOEXCEPT
921 {return const_reverse_iterator(end());}
Howard Hinnant82894812010-09-22 16:48:34 +0000922 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000923 reverse_iterator rend() _NOEXCEPT
924 {return reverse_iterator(begin());}
Howard Hinnant82894812010-09-22 16:48:34 +0000925 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000926 const_reverse_iterator rend() const _NOEXCEPT
927 {return const_reverse_iterator(begin());}
Howard Hinnant82894812010-09-22 16:48:34 +0000928 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000929 const_reverse_iterator crbegin() const _NOEXCEPT
930 {return const_reverse_iterator(end());}
Howard Hinnant82894812010-09-22 16:48:34 +0000931 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000932 const_reverse_iterator crend() const _NOEXCEPT
933 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000934
Howard Hinnant82894812010-09-22 16:48:34 +0000935 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000936 reference front()
937 {
938 _LIBCPP_ASSERT(!empty(), "list::front called on empty list");
939 return base::__end_.__next_->__value_;
940 }
Howard Hinnant82894812010-09-22 16:48:34 +0000941 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000942 const_reference front() const
943 {
944 _LIBCPP_ASSERT(!empty(), "list::front called on empty list");
945 return base::__end_.__next_->__value_;
946 }
Howard Hinnant82894812010-09-22 16:48:34 +0000947 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000948 reference back()
949 {
950 _LIBCPP_ASSERT(!empty(), "list::back called on empty list");
951 return base::__end_.__prev_->__value_;
952 }
Howard Hinnant82894812010-09-22 16:48:34 +0000953 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000954 const_reference back() const
955 {
956 _LIBCPP_ASSERT(!empty(), "list::back called on empty list");
957 return base::__end_.__prev_->__value_;
958 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000959
Howard Hinnant73d21a42010-09-04 23:28:19 +0000960#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000961 void push_front(value_type&& __x);
962 void push_back(value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000963#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000964 template <class... _Args>
965 void emplace_front(_Args&&... __args);
966 template <class... _Args>
967 void emplace_back(_Args&&... __args);
968 template <class... _Args>
969 iterator emplace(const_iterator __p, _Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000970#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000971 iterator insert(const_iterator __p, value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000972#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000973
974 void push_front(const value_type& __x);
975 void push_back(const value_type& __x);
976
977 iterator insert(const_iterator __p, const value_type& __x);
978 iterator insert(const_iterator __p, size_type __n, const value_type& __x);
979 template <class _InpIter>
980 iterator insert(const_iterator __p, _InpIter __f, _InpIter __l,
981 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
Howard Hinnante3e32912011-08-12 21:56:02 +0000982#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant82894812010-09-22 16:48:34 +0000983 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000984 iterator insert(const_iterator __p, initializer_list<value_type> __il)
985 {return insert(__p, __il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +0000986#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000987
Howard Hinnant82894812010-09-22 16:48:34 +0000988 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000989 void swap(list& __c)
990 _NOEXCEPT_(!__node_alloc_traits::propagate_on_container_swap::value ||
991 __is_nothrow_swappable<__node_allocator>::value)
992 {base::swap(__c);}
Howard Hinnant82894812010-09-22 16:48:34 +0000993 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000994 void clear() _NOEXCEPT {base::clear();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000995
996 void pop_front();
997 void pop_back();
998
999 iterator erase(const_iterator __p);
1000 iterator erase(const_iterator __f, const_iterator __l);
1001
1002 void resize(size_type __n);
1003 void resize(size_type __n, const value_type& __x);
1004
1005 void splice(const_iterator __p, list& __c);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001006#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant82894812010-09-22 16:48:34 +00001007 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001008 void splice(const_iterator __p, list&& __c) {splice(__p, __c);}
1009#endif
1010 void splice(const_iterator __p, list& __c, const_iterator __i);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001011#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant82894812010-09-22 16:48:34 +00001012 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001013 void splice(const_iterator __p, list&& __c, const_iterator __i)
1014 {splice(__p, __c, __i);}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001015#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001016 void splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001017#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant82894812010-09-22 16:48:34 +00001018 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001019 void splice(const_iterator __p, list&& __c, const_iterator __f, const_iterator __l)
1020 {splice(__p, __c, __f, __l);}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001021#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001022
1023 void remove(const value_type& __x);
1024 template <class _Pred> void remove_if(_Pred __pred);
1025 void unique();
1026 template <class _BinaryPred>
1027 void unique(_BinaryPred __binary_pred);
1028 void merge(list& __c);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001029#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant82894812010-09-22 16:48:34 +00001030 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001031 void merge(list&& __c) {merge(__c);}
1032#endif
1033 template <class _Comp>
1034 void merge(list& __c, _Comp __comp);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001035#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001036 template <class _Comp>
Howard Hinnant82894812010-09-22 16:48:34 +00001037 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001038 void merge(list&& __c, _Comp __comp) {merge(__c, __comp);}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001039#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001040 void sort();
1041 template <class _Comp>
1042 void sort(_Comp __comp);
1043
Howard Hinnantc5607272011-06-03 17:30:28 +00001044 void reverse() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001045
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001046 bool __invariants() const;
1047
1048#if _LIBCPP_DEBUG_LEVEL >= 2
1049
1050 bool __dereferenceable(const const_iterator* __i) const;
1051 bool __decrementable(const const_iterator* __i) const;
1052 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1053 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1054
1055#endif // _LIBCPP_DEBUG_LEVEL >= 2
1056
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001057private:
Howard Hinnant29f74322013-06-25 16:08:47 +00001058 static void __link_nodes(__node_pointer __p, __node_pointer __f, __node_pointer __l);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001059 iterator __iterator(size_type __n);
1060 template <class _Comp>
1061 static iterator __sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp);
1062
Howard Hinnantc5607272011-06-03 17:30:28 +00001063 void __move_assign(list& __c, true_type)
1064 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001065 void __move_assign(list& __c, false_type);
1066};
1067
1068// Link in nodes [__f, __l] just prior to __p
1069template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001070inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001071void
Howard Hinnant29f74322013-06-25 16:08:47 +00001072list<_Tp, _Alloc>::__link_nodes(__node_pointer __p, __node_pointer __f, __node_pointer __l)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001073{
Howard Hinnant29f74322013-06-25 16:08:47 +00001074 __p->__prev_->__next_ = __f;
1075 __f->__prev_ = __p->__prev_;
1076 __p->__prev_ = __l;
1077 __l->__next_ = __p;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001078}
1079
1080template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001081inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001082typename list<_Tp, _Alloc>::iterator
1083list<_Tp, _Alloc>::__iterator(size_type __n)
1084{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001085 return __n <= base::__sz() / 2 ? _VSTD::next(begin(), __n)
1086 : _VSTD::prev(end(), base::__sz() - __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001087}
1088
1089template <class _Tp, class _Alloc>
1090list<_Tp, _Alloc>::list(size_type __n)
1091{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001092#if _LIBCPP_DEBUG_LEVEL >= 2
1093 __get_db()->__insert_c(this);
1094#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001095 for (; __n > 0; --__n)
Howard Hinnant73d21a42010-09-04 23:28:19 +00001096#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001097 emplace_back();
1098#else
1099 push_back(value_type());
1100#endif
1101}
1102
1103template <class _Tp, class _Alloc>
1104list<_Tp, _Alloc>::list(size_type __n, const value_type& __x)
1105{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001106#if _LIBCPP_DEBUG_LEVEL >= 2
1107 __get_db()->__insert_c(this);
1108#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001109 for (; __n > 0; --__n)
1110 push_back(__x);
1111}
1112
1113template <class _Tp, class _Alloc>
1114list<_Tp, _Alloc>::list(size_type __n, const value_type& __x, const allocator_type& __a)
1115 : base(__a)
1116{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001117#if _LIBCPP_DEBUG_LEVEL >= 2
1118 __get_db()->__insert_c(this);
1119#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001120 for (; __n > 0; --__n)
1121 push_back(__x);
1122}
1123
1124template <class _Tp, class _Alloc>
1125template <class _InpIter>
1126list<_Tp, _Alloc>::list(_InpIter __f, _InpIter __l,
1127 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1128{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001129#if _LIBCPP_DEBUG_LEVEL >= 2
1130 __get_db()->__insert_c(this);
1131#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001132 for (; __f != __l; ++__f)
1133 push_back(*__f);
1134}
1135
1136template <class _Tp, class _Alloc>
1137template <class _InpIter>
1138list<_Tp, _Alloc>::list(_InpIter __f, _InpIter __l, const allocator_type& __a,
1139 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1140 : base(__a)
1141{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001142#if _LIBCPP_DEBUG_LEVEL >= 2
1143 __get_db()->__insert_c(this);
1144#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001145 for (; __f != __l; ++__f)
1146 push_back(*__f);
1147}
1148
1149template <class _Tp, class _Alloc>
1150list<_Tp, _Alloc>::list(const list& __c)
1151 : base(allocator_type(
1152 __node_alloc_traits::select_on_container_copy_construction(
1153 __c.__node_alloc())))
1154{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001155#if _LIBCPP_DEBUG_LEVEL >= 2
1156 __get_db()->__insert_c(this);
1157#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001158 for (const_iterator __i = __c.begin(), __e = __c.end(); __i != __e; ++__i)
1159 push_back(*__i);
1160}
1161
1162template <class _Tp, class _Alloc>
1163list<_Tp, _Alloc>::list(const list& __c, const allocator_type& __a)
1164 : base(__a)
1165{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001166#if _LIBCPP_DEBUG_LEVEL >= 2
1167 __get_db()->__insert_c(this);
1168#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001169 for (const_iterator __i = __c.begin(), __e = __c.end(); __i != __e; ++__i)
1170 push_back(*__i);
1171}
1172
Howard Hinnante3e32912011-08-12 21:56:02 +00001173#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1174
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001175template <class _Tp, class _Alloc>
1176list<_Tp, _Alloc>::list(initializer_list<value_type> __il, const allocator_type& __a)
1177 : base(__a)
1178{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001179#if _LIBCPP_DEBUG_LEVEL >= 2
1180 __get_db()->__insert_c(this);
1181#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001182 for (typename initializer_list<value_type>::const_iterator __i = __il.begin(),
1183 __e = __il.end(); __i != __e; ++__i)
1184 push_back(*__i);
1185}
1186
1187template <class _Tp, class _Alloc>
1188list<_Tp, _Alloc>::list(initializer_list<value_type> __il)
1189{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001190#if _LIBCPP_DEBUG_LEVEL >= 2
1191 __get_db()->__insert_c(this);
1192#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001193 for (typename initializer_list<value_type>::const_iterator __i = __il.begin(),
1194 __e = __il.end(); __i != __e; ++__i)
1195 push_back(*__i);
1196}
1197
Howard Hinnante3e32912011-08-12 21:56:02 +00001198#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1199
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001200template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001201inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001202list<_Tp, _Alloc>&
1203list<_Tp, _Alloc>::operator=(const list& __c)
1204{
1205 if (this != &__c)
1206 {
1207 base::__copy_assign_alloc(__c);
1208 assign(__c.begin(), __c.end());
1209 }
1210 return *this;
1211}
1212
Howard Hinnant73d21a42010-09-04 23:28:19 +00001213#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001214
1215template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001216inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001217list<_Tp, _Alloc>::list(list&& __c)
Howard Hinnantc5607272011-06-03 17:30:28 +00001218 _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001219 : base(allocator_type(_VSTD::move(__c.__node_alloc())))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001220{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001221#if _LIBCPP_DEBUG_LEVEL >= 2
1222 __get_db()->__insert_c(this);
1223#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001224 splice(end(), __c);
1225}
1226
1227template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001228inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001229list<_Tp, _Alloc>::list(list&& __c, const allocator_type& __a)
1230 : base(__a)
1231{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001232#if _LIBCPP_DEBUG_LEVEL >= 2
1233 __get_db()->__insert_c(this);
1234#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001235 if (__a == __c.get_allocator())
1236 splice(end(), __c);
1237 else
1238 {
Howard Hinnant99968442011-11-29 18:15:50 +00001239 typedef move_iterator<iterator> _Ip;
1240 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001241 }
1242}
1243
1244template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001245inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001246list<_Tp, _Alloc>&
1247list<_Tp, _Alloc>::operator=(list&& __c)
Howard Hinnantc5607272011-06-03 17:30:28 +00001248 _NOEXCEPT_(
1249 __node_alloc_traits::propagate_on_container_move_assignment::value &&
1250 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001251{
1252 __move_assign(__c, integral_constant<bool,
1253 __node_alloc_traits::propagate_on_container_move_assignment::value>());
1254 return *this;
1255}
1256
1257template <class _Tp, class _Alloc>
1258void
1259list<_Tp, _Alloc>::__move_assign(list& __c, false_type)
1260{
1261 if (base::__node_alloc() != __c.__node_alloc())
1262 {
Howard Hinnant99968442011-11-29 18:15:50 +00001263 typedef move_iterator<iterator> _Ip;
1264 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001265 }
1266 else
1267 __move_assign(__c, true_type());
1268}
1269
1270template <class _Tp, class _Alloc>
1271void
1272list<_Tp, _Alloc>::__move_assign(list& __c, true_type)
Howard Hinnantc5607272011-06-03 17:30:28 +00001273 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001274{
1275 clear();
1276 base::__move_assign_alloc(__c);
1277 splice(end(), __c);
1278}
1279
Howard Hinnant73d21a42010-09-04 23:28:19 +00001280#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001281
1282template <class _Tp, class _Alloc>
1283template <class _InpIter>
1284void
1285list<_Tp, _Alloc>::assign(_InpIter __f, _InpIter __l,
1286 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1287{
1288 iterator __i = begin();
1289 iterator __e = end();
1290 for (; __f != __l && __i != __e; ++__f, ++__i)
1291 *__i = *__f;
1292 if (__i == __e)
1293 insert(__e, __f, __l);
1294 else
1295 erase(__i, __e);
1296}
1297
1298template <class _Tp, class _Alloc>
1299void
1300list<_Tp, _Alloc>::assign(size_type __n, const value_type& __x)
1301{
1302 iterator __i = begin();
1303 iterator __e = end();
1304 for (; __n > 0 && __i != __e; --__n, ++__i)
1305 *__i = __x;
1306 if (__i == __e)
1307 insert(__e, __n, __x);
1308 else
1309 erase(__i, __e);
1310}
1311
1312template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001313inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001314_Alloc
Howard Hinnantc5607272011-06-03 17:30:28 +00001315list<_Tp, _Alloc>::get_allocator() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001316{
1317 return allocator_type(base::__node_alloc());
1318}
1319
1320template <class _Tp, class _Alloc>
1321typename list<_Tp, _Alloc>::iterator
1322list<_Tp, _Alloc>::insert(const_iterator __p, const value_type& __x)
1323{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001324#if _LIBCPP_DEBUG_LEVEL >= 2
1325 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1326 "list::insert(iterator, x) called with an iterator not"
1327 " referring to this list");
1328#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001329 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001330 typedef __allocator_destructor<__node_allocator> _Dp;
1331 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001332 __hold->__prev_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001333 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnant29f74322013-06-25 16:08:47 +00001334 __link_nodes(__p.__ptr_, __hold.get(), __hold.get());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001335 ++base::__sz();
Howard Hinnant79a35572013-04-05 00:18:49 +00001336#if _LIBCPP_DEBUG_LEVEL >= 2
1337 return iterator(__hold.release(), this);
1338#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001339 return iterator(__hold.release());
Howard Hinnant79a35572013-04-05 00:18:49 +00001340#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001341}
1342
1343template <class _Tp, class _Alloc>
1344typename list<_Tp, _Alloc>::iterator
1345list<_Tp, _Alloc>::insert(const_iterator __p, size_type __n, const value_type& __x)
1346{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001347#if _LIBCPP_DEBUG_LEVEL >= 2
1348 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1349 "list::insert(iterator, n, x) called with an iterator not"
1350 " referring to this list");
Howard Hinnant29f74322013-06-25 16:08:47 +00001351 iterator __r(__p.__ptr_, this);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001352#else
Howard Hinnant29f74322013-06-25 16:08:47 +00001353 iterator __r(__p.__ptr_);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001354#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001355 if (__n > 0)
1356 {
1357 size_type __ds = 0;
1358 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001359 typedef __allocator_destructor<__node_allocator> _Dp;
1360 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001361 __hold->__prev_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001362 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001363 ++__ds;
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001364#if _LIBCPP_DEBUG_LEVEL >= 2
1365 __r = iterator(__hold.get(), this);
1366#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001367 __r = iterator(__hold.get());
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001368#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001369 __hold.release();
1370 iterator __e = __r;
1371#ifndef _LIBCPP_NO_EXCEPTIONS
1372 try
1373 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001374#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001375 for (--__n; __n != 0; --__n, ++__e, ++__ds)
1376 {
1377 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001378 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001379 __e.__ptr_->__next_ = __hold.get();
1380 __hold->__prev_ = __e.__ptr_;
1381 __hold.release();
1382 }
1383#ifndef _LIBCPP_NO_EXCEPTIONS
1384 }
1385 catch (...)
1386 {
1387 while (true)
1388 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001389 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001390 __node_pointer __prev = __e.__ptr_->__prev_;
1391 __node_alloc_traits::deallocate(__na, __e.__ptr_, 1);
1392 if (__prev == 0)
1393 break;
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001394#if _LIBCPP_DEBUG_LEVEL >= 2
1395 __e = iterator(__prev, this);
1396#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001397 __e = iterator(__prev);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001398#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001399 }
1400 throw;
1401 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001402#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant29f74322013-06-25 16:08:47 +00001403 __link_nodes(__p.__ptr_, __r.__ptr_, __e.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001404 base::__sz() += __ds;
1405 }
1406 return __r;
1407}
1408
1409template <class _Tp, class _Alloc>
1410template <class _InpIter>
1411typename list<_Tp, _Alloc>::iterator
1412list<_Tp, _Alloc>::insert(const_iterator __p, _InpIter __f, _InpIter __l,
1413 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1414{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001415#if _LIBCPP_DEBUG_LEVEL >= 2
1416 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1417 "list::insert(iterator, range) called with an iterator not"
1418 " referring to this list");
Howard Hinnant29f74322013-06-25 16:08:47 +00001419 iterator __r(__p.__ptr_, this);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001420#else
Howard Hinnant29f74322013-06-25 16:08:47 +00001421 iterator __r(__p.__ptr_);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001422#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001423 if (__f != __l)
1424 {
1425 size_type __ds = 0;
1426 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001427 typedef __allocator_destructor<__node_allocator> _Dp;
1428 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001429 __hold->__prev_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001430 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), *__f);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001431 ++__ds;
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001432#if _LIBCPP_DEBUG_LEVEL >= 2
1433 __r = iterator(__hold.get(), this);
1434#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001435 __r = iterator(__hold.get());
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001436#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001437 __hold.release();
1438 iterator __e = __r;
1439#ifndef _LIBCPP_NO_EXCEPTIONS
1440 try
1441 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001442#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001443 for (++__f; __f != __l; ++__f, ++__e, ++__ds)
1444 {
1445 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001446 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), *__f);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001447 __e.__ptr_->__next_ = __hold.get();
1448 __hold->__prev_ = __e.__ptr_;
1449 __hold.release();
1450 }
1451#ifndef _LIBCPP_NO_EXCEPTIONS
1452 }
1453 catch (...)
1454 {
1455 while (true)
1456 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001457 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001458 __node_pointer __prev = __e.__ptr_->__prev_;
1459 __node_alloc_traits::deallocate(__na, __e.__ptr_, 1);
1460 if (__prev == 0)
1461 break;
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001462#if _LIBCPP_DEBUG_LEVEL >= 2
1463 __e = iterator(__prev, this);
1464#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001465 __e = iterator(__prev);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001466#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001467 }
1468 throw;
1469 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001470#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant29f74322013-06-25 16:08:47 +00001471 __link_nodes(__p.__ptr_, __r.__ptr_, __e.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001472 base::__sz() += __ds;
1473 }
1474 return __r;
1475}
1476
1477template <class _Tp, class _Alloc>
1478void
1479list<_Tp, _Alloc>::push_front(const value_type& __x)
1480{
1481 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001482 typedef __allocator_destructor<__node_allocator> _Dp;
1483 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001484 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnant29f74322013-06-25 16:08:47 +00001485 __link_nodes(base::__end_.__next_, __hold.get(), __hold.get());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001486 ++base::__sz();
1487 __hold.release();
1488}
1489
1490template <class _Tp, class _Alloc>
1491void
1492list<_Tp, _Alloc>::push_back(const value_type& __x)
1493{
1494 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001495 typedef __allocator_destructor<__node_allocator> _Dp;
1496 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001497 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnant29f74322013-06-25 16:08:47 +00001498 __link_nodes(static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::
1499 pointer_to(base::__end_)), __hold.get(), __hold.get());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001500 ++base::__sz();
1501 __hold.release();
1502}
1503
Howard Hinnant73d21a42010-09-04 23:28:19 +00001504#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001505
1506template <class _Tp, class _Alloc>
1507void
1508list<_Tp, _Alloc>::push_front(value_type&& __x)
1509{
1510 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001511 typedef __allocator_destructor<__node_allocator> _Dp;
1512 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001513 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
Howard Hinnant29f74322013-06-25 16:08:47 +00001514 __link_nodes(base::__end_.__next_, __hold.get(), __hold.get());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001515 ++base::__sz();
1516 __hold.release();
1517}
1518
1519template <class _Tp, class _Alloc>
1520void
1521list<_Tp, _Alloc>::push_back(value_type&& __x)
1522{
1523 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001524 typedef __allocator_destructor<__node_allocator> _Dp;
1525 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001526 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
Howard Hinnant29f74322013-06-25 16:08:47 +00001527 __link_nodes(static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::
1528 pointer_to(base::__end_)), __hold.get(), __hold.get());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001529 ++base::__sz();
1530 __hold.release();
1531}
1532
Howard Hinnant73d21a42010-09-04 23:28:19 +00001533#ifndef _LIBCPP_HAS_NO_VARIADICS
1534
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001535template <class _Tp, class _Alloc>
1536template <class... _Args>
1537void
1538list<_Tp, _Alloc>::emplace_front(_Args&&... __args)
1539{
1540 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001541 typedef __allocator_destructor<__node_allocator> _Dp;
1542 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001543 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
Howard Hinnant29f74322013-06-25 16:08:47 +00001544 __link_nodes(base::__end_.__next_, __hold.get(), __hold.get());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001545 ++base::__sz();
1546 __hold.release();
1547}
1548
1549template <class _Tp, class _Alloc>
1550template <class... _Args>
1551void
1552list<_Tp, _Alloc>::emplace_back(_Args&&... __args)
1553{
1554 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001555 typedef __allocator_destructor<__node_allocator> _Dp;
1556 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001557 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
Howard Hinnant29f74322013-06-25 16:08:47 +00001558 __link_nodes(static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::
1559 pointer_to(base::__end_)), __hold.get(), __hold.get());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001560 ++base::__sz();
1561 __hold.release();
1562}
1563
1564template <class _Tp, class _Alloc>
1565template <class... _Args>
1566typename list<_Tp, _Alloc>::iterator
1567list<_Tp, _Alloc>::emplace(const_iterator __p, _Args&&... __args)
1568{
Howard Hinnant79a35572013-04-05 00:18:49 +00001569#if _LIBCPP_DEBUG_LEVEL >= 2
1570 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1571 "list::emplace(iterator, args...) called with an iterator not"
1572 " referring to this list");
1573#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001574 __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 Hinnantbc8d3f92010-05-11 19:42:16 +00001577 __hold->__prev_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001578 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
Howard Hinnant29f74322013-06-25 16:08:47 +00001579 __link_nodes(__p.__ptr_, __hold.get(), __hold.get());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001580 ++base::__sz();
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001581#if _LIBCPP_DEBUG_LEVEL >= 2
1582 return iterator(__hold.release(), this);
1583#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001584 return iterator(__hold.release());
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001585#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001586}
1587
Howard Hinnant73d21a42010-09-04 23:28:19 +00001588#endif // _LIBCPP_HAS_NO_VARIADICS
1589
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001590template <class _Tp, class _Alloc>
1591typename list<_Tp, _Alloc>::iterator
1592list<_Tp, _Alloc>::insert(const_iterator __p, value_type&& __x)
1593{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001594#if _LIBCPP_DEBUG_LEVEL >= 2
1595 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1596 "list::insert(iterator, x) called with an iterator not"
1597 " referring to this list");
1598#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001599 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001600 typedef __allocator_destructor<__node_allocator> _Dp;
1601 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001602 __hold->__prev_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001603 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
Howard Hinnant29f74322013-06-25 16:08:47 +00001604 __link_nodes(__p.__ptr_, __hold.get(), __hold.get());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001605 ++base::__sz();
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001606#if _LIBCPP_DEBUG_LEVEL >= 2
1607 return iterator(__hold.release(), this);
1608#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001609 return iterator(__hold.release());
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001610#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001611}
1612
Howard Hinnant73d21a42010-09-04 23:28:19 +00001613#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001614
1615template <class _Tp, class _Alloc>
1616void
1617list<_Tp, _Alloc>::pop_front()
1618{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001619 _LIBCPP_ASSERT(!empty(), "list::pop_front() called with empty list");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001620 __node_allocator& __na = base::__node_alloc();
Howard Hinnant29f74322013-06-25 16:08:47 +00001621 __node_pointer __n = base::__end_.__next_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001622 base::__unlink_nodes(__n, __n);
1623 --base::__sz();
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001624#if _LIBCPP_DEBUG_LEVEL >= 2
1625 __c_node* __c = __get_db()->__find_c_and_lock(this);
1626 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1627 {
1628 --__p;
1629 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant29f74322013-06-25 16:08:47 +00001630 if (__i->__ptr_ == __n)
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001631 {
1632 (*__p)->__c_ = nullptr;
1633 if (--__c->end_ != __p)
1634 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1635 }
1636 }
1637 __get_db()->unlock();
1638#endif
Howard Hinnant29f74322013-06-25 16:08:47 +00001639 __node_alloc_traits::destroy(__na, _VSTD::addressof(__n->__value_));
1640 __node_alloc_traits::deallocate(__na, __n, 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001641}
1642
1643template <class _Tp, class _Alloc>
1644void
1645list<_Tp, _Alloc>::pop_back()
1646{
Dmitri Gribenkoc7a39cf2013-06-24 06:15:57 +00001647 _LIBCPP_ASSERT(!empty(), "list::pop_back() called with empty list");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001648 __node_allocator& __na = base::__node_alloc();
Howard Hinnant29f74322013-06-25 16:08:47 +00001649 __node_pointer __n = base::__end_.__prev_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001650 base::__unlink_nodes(__n, __n);
1651 --base::__sz();
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001652#if _LIBCPP_DEBUG_LEVEL >= 2
1653 __c_node* __c = __get_db()->__find_c_and_lock(this);
1654 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1655 {
1656 --__p;
1657 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant29f74322013-06-25 16:08:47 +00001658 if (__i->__ptr_ == __n)
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001659 {
1660 (*__p)->__c_ = nullptr;
1661 if (--__c->end_ != __p)
1662 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1663 }
1664 }
1665 __get_db()->unlock();
1666#endif
Howard Hinnant29f74322013-06-25 16:08:47 +00001667 __node_alloc_traits::destroy(__na, _VSTD::addressof(__n->__value_));
1668 __node_alloc_traits::deallocate(__na, __n, 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001669}
1670
1671template <class _Tp, class _Alloc>
1672typename list<_Tp, _Alloc>::iterator
1673list<_Tp, _Alloc>::erase(const_iterator __p)
1674{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001675#if _LIBCPP_DEBUG_LEVEL >= 2
1676 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1677 "list::erase(iterator) called with an iterator not"
1678 " referring to this list");
1679#endif
Howard Hinnant79a35572013-04-05 00:18:49 +00001680 _LIBCPP_ASSERT(__p != end(),
1681 "list::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001682 __node_allocator& __na = base::__node_alloc();
Howard Hinnant29f74322013-06-25 16:08:47 +00001683 __node_pointer __n = __p.__ptr_;
1684 __node_pointer __r = __n->__next_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001685 base::__unlink_nodes(__n, __n);
1686 --base::__sz();
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001687#if _LIBCPP_DEBUG_LEVEL >= 2
1688 __c_node* __c = __get_db()->__find_c_and_lock(this);
1689 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1690 {
1691 --__p;
1692 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant29f74322013-06-25 16:08:47 +00001693 if (__i->__ptr_ == __n)
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001694 {
1695 (*__p)->__c_ = nullptr;
1696 if (--__c->end_ != __p)
1697 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1698 }
1699 }
1700 __get_db()->unlock();
1701#endif
Howard Hinnant29f74322013-06-25 16:08:47 +00001702 __node_alloc_traits::destroy(__na, _VSTD::addressof(__n->__value_));
1703 __node_alloc_traits::deallocate(__na, __n, 1);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001704#if _LIBCPP_DEBUG_LEVEL >= 2
1705 return iterator(__r, this);
1706#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001707 return iterator(__r);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001708#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001709}
1710
1711template <class _Tp, class _Alloc>
1712typename list<_Tp, _Alloc>::iterator
1713list<_Tp, _Alloc>::erase(const_iterator __f, const_iterator __l)
1714{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001715#if _LIBCPP_DEBUG_LEVEL >= 2
1716 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__f) == this,
1717 "list::erase(iterator, iterator) called with an iterator not"
1718 " referring to this list");
1719#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001720 if (__f != __l)
1721 {
1722 __node_allocator& __na = base::__node_alloc();
Howard Hinnant29f74322013-06-25 16:08:47 +00001723 base::__unlink_nodes(__f.__ptr_, __l.__ptr_->__prev_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001724 while (__f != __l)
1725 {
Howard Hinnant29f74322013-06-25 16:08:47 +00001726 __node_pointer __n = __f.__ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001727 ++__f;
1728 --base::__sz();
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001729#if _LIBCPP_DEBUG_LEVEL >= 2
1730 __c_node* __c = __get_db()->__find_c_and_lock(this);
1731 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1732 {
1733 --__p;
1734 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant29f74322013-06-25 16:08:47 +00001735 if (__i->__ptr_ == __n)
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001736 {
1737 (*__p)->__c_ = nullptr;
1738 if (--__c->end_ != __p)
1739 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1740 }
1741 }
1742 __get_db()->unlock();
1743#endif
Howard Hinnant29f74322013-06-25 16:08:47 +00001744 __node_alloc_traits::destroy(__na, _VSTD::addressof(__n->__value_));
1745 __node_alloc_traits::deallocate(__na, __n, 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001746 }
1747 }
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001748#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant29f74322013-06-25 16:08:47 +00001749 return iterator(__l.__ptr_, this);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001750#else
Howard Hinnant29f74322013-06-25 16:08:47 +00001751 return iterator(__l.__ptr_);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001752#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001753}
1754
1755template <class _Tp, class _Alloc>
1756void
1757list<_Tp, _Alloc>::resize(size_type __n)
1758{
1759 if (__n < base::__sz())
1760 erase(__iterator(__n), end());
1761 else if (__n > base::__sz())
1762 {
1763 __n -= base::__sz();
1764 size_type __ds = 0;
1765 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001766 typedef __allocator_destructor<__node_allocator> _Dp;
1767 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001768 __hold->__prev_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001769 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001770 ++__ds;
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001771#if _LIBCPP_DEBUG_LEVEL >= 2
1772 iterator __r = iterator(__hold.release(), this);
1773#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001774 iterator __r = iterator(__hold.release());
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001775#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001776 iterator __e = __r;
1777#ifndef _LIBCPP_NO_EXCEPTIONS
1778 try
1779 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001780#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001781 for (--__n; __n != 0; --__n, ++__e, ++__ds)
1782 {
1783 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001784 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001785 __e.__ptr_->__next_ = __hold.get();
1786 __hold->__prev_ = __e.__ptr_;
1787 __hold.release();
1788 }
1789#ifndef _LIBCPP_NO_EXCEPTIONS
1790 }
1791 catch (...)
1792 {
1793 while (true)
1794 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001795 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001796 __node_pointer __prev = __e.__ptr_->__prev_;
1797 __node_alloc_traits::deallocate(__na, __e.__ptr_, 1);
1798 if (__prev == 0)
1799 break;
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001800#if _LIBCPP_DEBUG_LEVEL >= 2
1801 __e = iterator(__prev, this);
1802#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001803 __e = iterator(__prev);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001804#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001805 }
1806 throw;
1807 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001808#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant29f74322013-06-25 16:08:47 +00001809 __link_nodes(static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::
1810 pointer_to(base::__end_)), __r.__ptr_, __e.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001811 base::__sz() += __ds;
1812 }
1813}
1814
1815template <class _Tp, class _Alloc>
1816void
1817list<_Tp, _Alloc>::resize(size_type __n, const value_type& __x)
1818{
1819 if (__n < base::__sz())
1820 erase(__iterator(__n), end());
1821 else if (__n > base::__sz())
1822 {
1823 __n -= base::__sz();
1824 size_type __ds = 0;
1825 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001826 typedef __allocator_destructor<__node_allocator> _Dp;
1827 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001828 __hold->__prev_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001829 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001830 ++__ds;
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001831#if _LIBCPP_DEBUG_LEVEL >= 2
1832 iterator __r = iterator(__hold.release(), this);
1833#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001834 iterator __r = iterator(__hold.release());
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001835#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001836 iterator __e = __r;
1837#ifndef _LIBCPP_NO_EXCEPTIONS
1838 try
1839 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001840#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001841 for (--__n; __n != 0; --__n, ++__e, ++__ds)
1842 {
1843 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001844 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001845 __e.__ptr_->__next_ = __hold.get();
1846 __hold->__prev_ = __e.__ptr_;
1847 __hold.release();
1848 }
1849#ifndef _LIBCPP_NO_EXCEPTIONS
1850 }
1851 catch (...)
1852 {
1853 while (true)
1854 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001855 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001856 __node_pointer __prev = __e.__ptr_->__prev_;
1857 __node_alloc_traits::deallocate(__na, __e.__ptr_, 1);
1858 if (__prev == 0)
1859 break;
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001860#if _LIBCPP_DEBUG_LEVEL >= 2
1861 __e = iterator(__prev, this);
1862#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001863 __e = iterator(__prev);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001864#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001865 }
1866 throw;
1867 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001868#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant29f74322013-06-25 16:08:47 +00001869 __link_nodes(static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::
1870 pointer_to(base::__end_)), __r.__ptr_, __e.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001871 base::__sz() += __ds;
Howard Hinnant324bb032010-08-22 00:02:43 +00001872 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001873}
1874
1875template <class _Tp, class _Alloc>
1876void
1877list<_Tp, _Alloc>::splice(const_iterator __p, list& __c)
1878{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001879 _LIBCPP_ASSERT(this != &__c,
1880 "list::splice(iterator, list) called with this == &list");
1881#if _LIBCPP_DEBUG_LEVEL >= 2
1882 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1883 "list::splice(iterator, list) called with an iterator not"
1884 " referring to this list");
1885#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001886 if (!__c.empty())
1887 {
Howard Hinnant29f74322013-06-25 16:08:47 +00001888 __node_pointer __f = __c.__end_.__next_;
1889 __node_pointer __l = __c.__end_.__prev_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001890 base::__unlink_nodes(__f, __l);
Howard Hinnant29f74322013-06-25 16:08:47 +00001891 __link_nodes(__p.__ptr_, __f, __l);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001892 base::__sz() += __c.__sz();
1893 __c.__sz() = 0;
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001894#if _LIBCPP_DEBUG_LEVEL >= 2
1895 __libcpp_db* __db = __get_db();
1896 __c_node* __cn1 = __db->__find_c_and_lock(this);
1897 __c_node* __cn2 = __db->__find_c(&__c);
1898 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
1899 {
1900 --__p;
1901 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant29f74322013-06-25 16:08:47 +00001902 if (__i->__ptr_ != static_cast<__node_pointer>(
1903 pointer_traits<__node_base_pointer>::pointer_to(__c.__end_)))
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001904 {
1905 __cn1->__add(*__p);
1906 (*__p)->__c_ = __cn1;
1907 if (--__cn2->end_ != __p)
1908 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
1909 }
1910 }
1911 __db->unlock();
1912#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001913 }
1914}
1915
1916template <class _Tp, class _Alloc>
1917void
1918list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __i)
1919{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001920#if _LIBCPP_DEBUG_LEVEL >= 2
1921 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1922 "list::splice(iterator, list, iterator) called with first iterator not"
1923 " referring to this list");
1924 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__i) == &__c,
1925 "list::splice(iterator, list, iterator) called with second iterator not"
1926 " referring to list argument");
1927 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(&__i),
1928 "list::splice(iterator, list, iterator) called with second iterator not"
1929 " derefereceable");
1930#endif
1931 if (__p.__ptr_ != __i.__ptr_ && __p.__ptr_ != __i.__ptr_->__next_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001932 {
Howard Hinnant29f74322013-06-25 16:08:47 +00001933 __node_pointer __f = __i.__ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001934 base::__unlink_nodes(__f, __f);
Howard Hinnant29f74322013-06-25 16:08:47 +00001935 __link_nodes(__p.__ptr_, __f, __f);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001936 --__c.__sz();
1937 ++base::__sz();
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001938#if _LIBCPP_DEBUG_LEVEL >= 2
1939 __libcpp_db* __db = __get_db();
1940 __c_node* __cn1 = __db->__find_c_and_lock(this);
1941 __c_node* __cn2 = __db->__find_c(&__c);
1942 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
1943 {
1944 --__p;
1945 iterator* __j = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant29f74322013-06-25 16:08:47 +00001946 if (__j->__ptr_ == __f)
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001947 {
1948 __cn1->__add(*__p);
1949 (*__p)->__c_ = __cn1;
1950 if (--__cn2->end_ != __p)
1951 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
1952 }
1953 }
1954 __db->unlock();
1955#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001956 }
1957}
1958
1959template <class _Tp, class _Alloc>
1960void
1961list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l)
1962{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001963#if _LIBCPP_DEBUG_LEVEL >= 2
1964 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1965 "list::splice(iterator, list, iterator, iterator) called with first iterator not"
1966 " referring to this list");
1967 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__f) == &__c,
1968 "list::splice(iterator, list, iterator, iterator) called with second iterator not"
1969 " referring to list argument");
1970 if (this == &__c)
1971 {
1972 for (const_iterator __i = __f; __i != __l; ++__i)
1973 _LIBCPP_ASSERT(__i != __p,
1974 "list::splice(iterator, list, iterator, iterator)"
1975 " called with the first iterator within the range"
1976 " of the second and third iterators");
1977 }
1978#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001979 if (__f != __l)
1980 {
1981 if (this != &__c)
1982 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001983 size_type __s = _VSTD::distance(__f, __l);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001984 __c.__sz() -= __s;
1985 base::__sz() += __s;
1986 }
Howard Hinnant29f74322013-06-25 16:08:47 +00001987 __node_pointer __first = __f.__ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001988 --__l;
Howard Hinnant29f74322013-06-25 16:08:47 +00001989 __node_pointer __last = __l.__ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001990 base::__unlink_nodes(__first, __last);
Howard Hinnant29f74322013-06-25 16:08:47 +00001991 __link_nodes(__p.__ptr_, __first, __last);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001992#if _LIBCPP_DEBUG_LEVEL >= 2
1993 __libcpp_db* __db = __get_db();
1994 __c_node* __cn1 = __db->__find_c_and_lock(this);
1995 __c_node* __cn2 = __db->__find_c(&__c);
1996 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
1997 {
1998 --__p;
1999 iterator* __j = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant29f74322013-06-25 16:08:47 +00002000 for (__node_pointer __k = __f.__ptr_;
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00002001 __k != __l.__ptr_; __k = __k->__next_)
2002 {
2003 if (__j->__ptr_ == __k)
2004 {
2005 __cn1->__add(*__p);
2006 (*__p)->__c_ = __cn1;
2007 if (--__cn2->end_ != __p)
2008 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
2009 }
2010 }
2011 }
2012 __db->unlock();
2013#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002014 }
2015}
2016
2017template <class _Tp, class _Alloc>
2018void
2019list<_Tp, _Alloc>::remove(const value_type& __x)
2020{
2021 for (iterator __i = begin(), __e = end(); __i != __e;)
2022 {
2023 if (*__i == __x)
2024 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002025 iterator __j = _VSTD::next(__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002026 for (; __j != __e && *__j == __x; ++__j)
2027 ;
2028 __i = erase(__i, __j);
2029 }
2030 else
2031 ++__i;
2032 }
2033}
2034
2035template <class _Tp, class _Alloc>
2036template <class _Pred>
2037void
2038list<_Tp, _Alloc>::remove_if(_Pred __pred)
2039{
2040 for (iterator __i = begin(), __e = end(); __i != __e;)
2041 {
2042 if (__pred(*__i))
2043 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002044 iterator __j = _VSTD::next(__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002045 for (; __j != __e && __pred(*__j); ++__j)
2046 ;
2047 __i = erase(__i, __j);
2048 }
2049 else
2050 ++__i;
2051 }
2052}
2053
2054template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00002055inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002056void
2057list<_Tp, _Alloc>::unique()
2058{
2059 unique(__equal_to<value_type>());
2060}
2061
2062template <class _Tp, class _Alloc>
2063template <class _BinaryPred>
2064void
2065list<_Tp, _Alloc>::unique(_BinaryPred __binary_pred)
2066{
2067 for (iterator __i = begin(), __e = end(); __i != __e;)
2068 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002069 iterator __j = _VSTD::next(__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002070 for (; __j != __e && __binary_pred(*__i, *__j); ++__j)
2071 ;
2072 if (++__i != __j)
2073 __i = erase(__i, __j);
2074 }
2075}
2076
2077template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00002078inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002079void
2080list<_Tp, _Alloc>::merge(list& __c)
2081{
2082 merge(__c, __less<value_type>());
2083}
2084
2085template <class _Tp, class _Alloc>
2086template <class _Comp>
2087void
2088list<_Tp, _Alloc>::merge(list& __c, _Comp __comp)
2089{
2090 if (this != &__c)
2091 {
2092 iterator __f1 = begin();
2093 iterator __e1 = end();
2094 iterator __f2 = __c.begin();
2095 iterator __e2 = __c.end();
2096 while (__f1 != __e1 && __f2 != __e2)
2097 {
2098 if (__comp(*__f2, *__f1))
2099 {
2100 size_type __ds = 1;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002101 iterator __m2 = _VSTD::next(__f2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002102 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2, ++__ds)
2103 ;
2104 base::__sz() += __ds;
2105 __c.__sz() -= __ds;
Howard Hinnant29f74322013-06-25 16:08:47 +00002106 __node_pointer __f = __f2.__ptr_;
2107 __node_pointer __l = __m2.__ptr_->__prev_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002108 __f2 = __m2;
2109 base::__unlink_nodes(__f, __l);
Howard Hinnant0949eed2011-06-30 21:18:19 +00002110 __m2 = _VSTD::next(__f1);
Howard Hinnant29f74322013-06-25 16:08:47 +00002111 __link_nodes(__f1.__ptr_, __f, __l);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002112 __f1 = __m2;
2113 }
2114 else
2115 ++__f1;
2116 }
2117 splice(__e1, __c);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00002118#if _LIBCPP_DEBUG_LEVEL >= 2
2119 __libcpp_db* __db = __get_db();
2120 __c_node* __cn1 = __db->__find_c_and_lock(this);
2121 __c_node* __cn2 = __db->__find_c(&__c);
2122 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
2123 {
2124 --__p;
2125 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant29f74322013-06-25 16:08:47 +00002126 if (__i->__ptr_ != static_cast<__node_pointer>(
2127 pointer_traits<__node_base_pointer>::pointer_to(__c.__end_)))
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00002128 {
2129 __cn1->__add(*__p);
2130 (*__p)->__c_ = __cn1;
2131 if (--__cn2->end_ != __p)
2132 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
2133 }
2134 }
2135 __db->unlock();
2136#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002137 }
2138}
2139
2140template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00002141inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002142void
2143list<_Tp, _Alloc>::sort()
2144{
2145 sort(__less<value_type>());
2146}
2147
2148template <class _Tp, class _Alloc>
2149template <class _Comp>
Howard Hinnant82894812010-09-22 16:48:34 +00002150inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002151void
2152list<_Tp, _Alloc>::sort(_Comp __comp)
2153{
2154 __sort(begin(), end(), base::__sz(), __comp);
2155}
2156
2157template <class _Tp, class _Alloc>
2158template <class _Comp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002159typename list<_Tp, _Alloc>::iterator
2160list<_Tp, _Alloc>::__sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp)
2161{
2162 switch (__n)
2163 {
2164 case 0:
2165 case 1:
2166 return __f1;
2167 case 2:
2168 if (__comp(*--__e2, *__f1))
2169 {
Howard Hinnant29f74322013-06-25 16:08:47 +00002170 __node_pointer __f = __e2.__ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002171 base::__unlink_nodes(__f, __f);
Howard Hinnant29f74322013-06-25 16:08:47 +00002172 __link_nodes(__f1.__ptr_, __f, __f);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002173 return __e2;
2174 }
2175 return __f1;
2176 }
2177 size_type __n2 = __n / 2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002178 iterator __e1 = _VSTD::next(__f1, __n2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002179 iterator __r = __f1 = __sort(__f1, __e1, __n2, __comp);
2180 iterator __f2 = __e1 = __sort(__e1, __e2, __n - __n2, __comp);
2181 if (__comp(*__f2, *__f1))
2182 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002183 iterator __m2 = _VSTD::next(__f2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002184 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2)
2185 ;
Howard Hinnant29f74322013-06-25 16:08:47 +00002186 __node_pointer __f = __f2.__ptr_;
2187 __node_pointer __l = __m2.__ptr_->__prev_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002188 __r = __f2;
2189 __e1 = __f2 = __m2;
2190 base::__unlink_nodes(__f, __l);
Howard Hinnant0949eed2011-06-30 21:18:19 +00002191 __m2 = _VSTD::next(__f1);
Howard Hinnant29f74322013-06-25 16:08:47 +00002192 __link_nodes(__f1.__ptr_, __f, __l);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002193 __f1 = __m2;
2194 }
2195 else
2196 ++__f1;
2197 while (__f1 != __e1 && __f2 != __e2)
2198 {
2199 if (__comp(*__f2, *__f1))
2200 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002201 iterator __m2 = _VSTD::next(__f2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002202 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2)
2203 ;
Howard Hinnant29f74322013-06-25 16:08:47 +00002204 __node_pointer __f = __f2.__ptr_;
2205 __node_pointer __l = __m2.__ptr_->__prev_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002206 if (__e1 == __f2)
2207 __e1 = __m2;
2208 __f2 = __m2;
2209 base::__unlink_nodes(__f, __l);
Howard Hinnant0949eed2011-06-30 21:18:19 +00002210 __m2 = _VSTD::next(__f1);
Howard Hinnant29f74322013-06-25 16:08:47 +00002211 __link_nodes(__f1.__ptr_, __f, __l);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002212 __f1 = __m2;
2213 }
2214 else
2215 ++__f1;
2216 }
2217 return __r;
2218}
2219
2220template <class _Tp, class _Alloc>
2221void
Howard Hinnantc5607272011-06-03 17:30:28 +00002222list<_Tp, _Alloc>::reverse() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002223{
2224 if (base::__sz() > 1)
2225 {
2226 iterator __e = end();
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00002227 for (iterator __i = begin(); __i.__ptr_ != __e.__ptr_;)
2228 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002229 _VSTD::swap(__i.__ptr_->__prev_, __i.__ptr_->__next_);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00002230 __i.__ptr_ = __i.__ptr_->__prev_;
2231 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002232 _VSTD::swap(__e.__ptr_->__prev_, __e.__ptr_->__next_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002233 }
2234}
2235
2236template <class _Tp, class _Alloc>
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00002237bool
2238list<_Tp, _Alloc>::__invariants() const
2239{
2240 return size() == _VSTD::distance(begin(), end());
2241}
2242
2243#if _LIBCPP_DEBUG_LEVEL >= 2
2244
2245template <class _Tp, class _Alloc>
2246bool
2247list<_Tp, _Alloc>::__dereferenceable(const const_iterator* __i) const
2248{
Howard Hinnant29f74322013-06-25 16:08:47 +00002249 return __i->__ptr_ != static_cast<__node_pointer>(
2250 pointer_traits<__node_base_pointer>::pointer_to(const_cast<__node_base&>(this->__end_)));
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00002251}
2252
2253template <class _Tp, class _Alloc>
2254bool
2255list<_Tp, _Alloc>::__decrementable(const const_iterator* __i) const
2256{
2257 return !empty() && __i->__ptr_ != base::__end_.__next_;
2258}
2259
2260template <class _Tp, class _Alloc>
2261bool
2262list<_Tp, _Alloc>::__addable(const const_iterator* __i, ptrdiff_t __n) const
2263{
2264 return false;
2265}
2266
2267template <class _Tp, class _Alloc>
2268bool
2269list<_Tp, _Alloc>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
2270{
2271 return false;
2272}
2273
2274#endif // _LIBCPP_DEBUG_LEVEL >= 2
2275
2276template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00002277inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002278bool
2279operator==(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2280{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002281 return __x.size() == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002282}
2283
2284template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00002285inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002286bool
2287operator< (const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2288{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002289 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002290}
2291
2292template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00002293inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002294bool
2295operator!=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2296{
2297 return !(__x == __y);
2298}
2299
2300template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00002301inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002302bool
2303operator> (const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2304{
2305 return __y < __x;
2306}
2307
2308template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00002309inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002310bool
2311operator>=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2312{
2313 return !(__x < __y);
2314}
2315
2316template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00002317inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002318bool
2319operator<=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2320{
2321 return !(__y < __x);
2322}
2323
2324template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00002325inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002326void
2327swap(list<_Tp, _Alloc>& __x, list<_Tp, _Alloc>& __y)
Howard Hinnantc5607272011-06-03 17:30:28 +00002328 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002329{
2330 __x.swap(__y);
2331}
2332
2333_LIBCPP_END_NAMESPACE_STD
2334
2335#endif // _LIBCPP_LIST