blob: 6040e7a4cc90b9f23904beca7136c135df8b7865 [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 Hinnant08e17472011-10-17 20:05:10 +0000181#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000182#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000183#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000184
185_LIBCPP_BEGIN_NAMESPACE_STD
186
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000187template <class _Tp, class _VoidPtr> struct __list_node;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000188
189template <class _Tp, class _VoidPtr>
190struct __list_node_base
191{
192 typedef typename pointer_traits<_VoidPtr>::template
193#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
194 rebind<__list_node<_Tp, _VoidPtr> > pointer;
195#else
196 rebind<__list_node<_Tp, _VoidPtr> >::other pointer;
197#endif
198
199 pointer __prev_;
200 pointer __next_;
201
Howard Hinnant82894812010-09-22 16:48:34 +0000202 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000203 __list_node_base()
204 : __prev_(static_cast<pointer>(this)),
205 __next_(static_cast<pointer>(this))
206 {}
207};
208
209template <class _Tp, class _VoidPtr>
210struct __list_node
211 : public __list_node_base<_Tp, _VoidPtr>
212{
213 _Tp __value_;
214};
215
Howard Hinnant83eade62013-03-06 23:30:19 +0000216template <class _Tp, class _Alloc> class _LIBCPP_TYPE_VIS list;
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000217template <class _Tp, class _Alloc> class __list_imp;
Howard Hinnant83eade62013-03-06 23:30:19 +0000218template <class _Tp, class _VoidPtr> class _LIBCPP_TYPE_VIS __list_const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000219
220template <class _Tp, class _VoidPtr>
Howard Hinnant83eade62013-03-06 23:30:19 +0000221class _LIBCPP_TYPE_VIS __list_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000222{
223 typedef typename pointer_traits<_VoidPtr>::template
224#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
225 rebind<__list_node<_Tp, _VoidPtr> > __node_pointer;
226#else
227 rebind<__list_node<_Tp, _VoidPtr> >::other __node_pointer;
228#endif
229
230 __node_pointer __ptr_;
231
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000232#if _LIBCPP_DEBUG_LEVEL >= 2
233 _LIBCPP_INLINE_VISIBILITY
234 explicit __list_iterator(__node_pointer __p, const void* __c) _NOEXCEPT
235 : __ptr_(__p)
236 {
237 __get_db()->__insert_ic(this, __c);
238 }
239#else
Howard Hinnant82894812010-09-22 16:48:34 +0000240 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000241 explicit __list_iterator(__node_pointer __p) _NOEXCEPT : __ptr_(__p) {}
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000242#endif
243
244
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000245
246 template<class, class> friend class list;
247 template<class, class> friend class __list_imp;
248 template<class, class> friend class __list_const_iterator;
249public:
250 typedef bidirectional_iterator_tag iterator_category;
251 typedef _Tp value_type;
252 typedef value_type& reference;
253 typedef typename pointer_traits<_VoidPtr>::template
254#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
255 rebind<value_type>
256#else
257 rebind<value_type>::other
258#endif
259 pointer;
260 typedef typename pointer_traits<pointer>::difference_type difference_type;
261
Howard Hinnant82894812010-09-22 16:48:34 +0000262 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000263 __list_iterator() _NOEXCEPT
264 {
265#if _LIBCPP_DEBUG_LEVEL >= 2
266 __get_db()->__insert_i(this);
267#endif
268 }
269
270#if _LIBCPP_DEBUG_LEVEL >= 2
271
Howard Hinnant211f0ee2011-01-28 23:46:28 +0000272 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000273 __list_iterator(const __list_iterator& __p)
274 : __ptr_(__p.__ptr_)
275 {
276 __get_db()->__iterator_copy(this, &__p);
277 }
278
279 _LIBCPP_INLINE_VISIBILITY
280 ~__list_iterator()
281 {
282 __get_db()->__erase_i(this);
283 }
284
285 _LIBCPP_INLINE_VISIBILITY
286 __list_iterator& operator=(const __list_iterator& __p)
287 {
288 if (this != &__p)
289 {
290 __get_db()->__iterator_copy(this, &__p);
291 __ptr_ = __p.__ptr_;
292 }
293 return *this;
294 }
295
296#endif // _LIBCPP_DEBUG_LEVEL >= 2
297
298 _LIBCPP_INLINE_VISIBILITY
299 reference operator*() const
300 {
301#if _LIBCPP_DEBUG_LEVEL >= 2
302 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
303 "Attempted to dereference a non-dereferenceable list::iterator");
304#endif
305 return __ptr_->__value_;
306 }
Howard Hinnant82894812010-09-22 16:48:34 +0000307 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000308 pointer operator->() const {return &(operator*());}
309
Howard Hinnant82894812010-09-22 16:48:34 +0000310 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000311 __list_iterator& operator++()
312 {
313#if _LIBCPP_DEBUG_LEVEL >= 2
314 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
315 "Attempted to increment non-incrementable list::iterator");
316#endif
317 __ptr_ = __ptr_->__next_;
318 return *this;
319 }
Howard Hinnant82894812010-09-22 16:48:34 +0000320 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000321 __list_iterator operator++(int) {__list_iterator __t(*this); ++(*this); return __t;}
322
Howard Hinnant82894812010-09-22 16:48:34 +0000323 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000324 __list_iterator& operator--()
325 {
326#if _LIBCPP_DEBUG_LEVEL >= 2
327 _LIBCPP_ASSERT(__get_const_db()->__decrementable(this),
328 "Attempted to decrement non-decrementable list::iterator");
329#endif
330 __ptr_ = __ptr_->__prev_;
331 return *this;
332 }
Howard Hinnant82894812010-09-22 16:48:34 +0000333 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000334 __list_iterator operator--(int) {__list_iterator __t(*this); --(*this); return __t;}
335
Howard Hinnant82894812010-09-22 16:48:34 +0000336 friend _LIBCPP_INLINE_VISIBILITY
337 bool operator==(const __list_iterator& __x, const __list_iterator& __y)
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000338 {
339#if _LIBCPP_DEBUG_LEVEL >= 2
340 _LIBCPP_ASSERT(__get_const_db()->__comparable(&__x, &__y),
341 "Attempted to compare non-comparable list::iterator");
342#endif
343 return __x.__ptr_ == __y.__ptr_;
344 }
Howard Hinnant82894812010-09-22 16:48:34 +0000345 friend _LIBCPP_INLINE_VISIBILITY
346 bool operator!=(const __list_iterator& __x, const __list_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000347 {return !(__x == __y);}
348};
349
350template <class _Tp, class _VoidPtr>
Howard Hinnant83eade62013-03-06 23:30:19 +0000351class _LIBCPP_TYPE_VIS __list_const_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000352{
353 typedef typename pointer_traits<_VoidPtr>::template
354#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
355 rebind<const __list_node<_Tp, _VoidPtr> > __node_pointer;
356#else
357 rebind<const __list_node<_Tp, _VoidPtr> >::other __node_pointer;
358#endif
359
360 __node_pointer __ptr_;
361
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000362#if _LIBCPP_DEBUG_LEVEL >= 2
363 _LIBCPP_INLINE_VISIBILITY
364 explicit __list_const_iterator(__node_pointer __p, const void* __c) _NOEXCEPT
365 : __ptr_(__p)
366 {
367 __get_db()->__insert_ic(this, __c);
368 }
369#else
Howard Hinnant82894812010-09-22 16:48:34 +0000370 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000371 explicit __list_const_iterator(__node_pointer __p) _NOEXCEPT : __ptr_(__p) {}
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000372#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000373
374 template<class, class> friend class list;
375 template<class, class> friend class __list_imp;
376public:
377 typedef bidirectional_iterator_tag iterator_category;
378 typedef _Tp value_type;
379 typedef const value_type& reference;
380 typedef typename pointer_traits<_VoidPtr>::template
381#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
382 rebind<const value_type>
383#else
384 rebind<const value_type>::other
385#endif
386 pointer;
387 typedef typename pointer_traits<pointer>::difference_type difference_type;
388
Howard Hinnant82894812010-09-22 16:48:34 +0000389 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000390 __list_const_iterator() _NOEXCEPT
391 {
392#if _LIBCPP_DEBUG_LEVEL >= 2
393 __get_db()->__insert_i(this);
394#endif
395 }
Howard Hinnant211f0ee2011-01-28 23:46:28 +0000396 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000397 __list_const_iterator(__list_iterator<_Tp, _VoidPtr> __p) _NOEXCEPT
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000398 : __ptr_(__p.__ptr_)
399 {
400#if _LIBCPP_DEBUG_LEVEL >= 2
401 __get_db()->__iterator_copy(this, &__p);
402#endif
403 }
404
405#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000406
Howard Hinnant82894812010-09-22 16:48:34 +0000407 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000408 __list_const_iterator(const __list_const_iterator& __p)
409 : __ptr_(__p.__ptr_)
410 {
411 __get_db()->__iterator_copy(this, &__p);
412 }
413
414 _LIBCPP_INLINE_VISIBILITY
415 ~__list_const_iterator()
416 {
417 __get_db()->__erase_i(this);
418 }
419
420 _LIBCPP_INLINE_VISIBILITY
421 __list_const_iterator& operator=(const __list_const_iterator& __p)
422 {
423 if (this != &__p)
424 {
425 __get_db()->__iterator_copy(this, &__p);
426 __ptr_ = __p.__ptr_;
427 }
428 return *this;
429 }
430
431#endif // _LIBCPP_DEBUG_LEVEL >= 2
432 _LIBCPP_INLINE_VISIBILITY
433 reference operator*() const
434 {
435#if _LIBCPP_DEBUG_LEVEL >= 2
436 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
437 "Attempted to dereference a non-dereferenceable list::const_iterator");
438#endif
439 return __ptr_->__value_;
440 }
Howard Hinnant82894812010-09-22 16:48:34 +0000441 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000442 pointer operator->() const {return &(operator*());}
443
Howard Hinnant82894812010-09-22 16:48:34 +0000444 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000445 __list_const_iterator& operator++()
446 {
447#if _LIBCPP_DEBUG_LEVEL >= 2
448 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
449 "Attempted to increment non-incrementable list::const_iterator");
450#endif
451 __ptr_ = __ptr_->__next_;
452 return *this;
453 }
Howard Hinnant82894812010-09-22 16:48:34 +0000454 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000455 __list_const_iterator operator++(int) {__list_const_iterator __t(*this); ++(*this); return __t;}
456
Howard Hinnant82894812010-09-22 16:48:34 +0000457 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000458 __list_const_iterator& operator--()
459 {
460#if _LIBCPP_DEBUG_LEVEL >= 2
461 _LIBCPP_ASSERT(__get_const_db()->__decrementable(this),
462 "Attempted to decrement non-decrementable list::const_iterator");
463#endif
464 __ptr_ = __ptr_->__prev_;
465 return *this;
466 }
Howard Hinnant82894812010-09-22 16:48:34 +0000467 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000468 __list_const_iterator operator--(int) {__list_const_iterator __t(*this); --(*this); return __t;}
469
Howard Hinnant82894812010-09-22 16:48:34 +0000470 friend _LIBCPP_INLINE_VISIBILITY
471 bool operator==(const __list_const_iterator& __x, const __list_const_iterator& __y)
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000472 {
473#if _LIBCPP_DEBUG_LEVEL >= 2
474 _LIBCPP_ASSERT(__get_const_db()->__comparable(&__x, &__y),
475 "Attempted to compare non-comparable list::const_iterator");
476#endif
477 return __x.__ptr_ == __y.__ptr_;
478 }
Howard Hinnant82894812010-09-22 16:48:34 +0000479 friend _LIBCPP_INLINE_VISIBILITY
480 bool operator!=(const __list_const_iterator& __x, const __list_const_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000481 {return !(__x == __y);}
482};
483
484template <class _Tp, class _Alloc>
485class __list_imp
486{
487 __list_imp(const __list_imp&);
488 __list_imp& operator=(const __list_imp&);
489protected:
490 typedef _Tp value_type;
491 typedef _Alloc allocator_type;
492 typedef allocator_traits<allocator_type> __alloc_traits;
493 typedef typename __alloc_traits::size_type size_type;
494 typedef typename __alloc_traits::void_pointer __void_pointer;
495 typedef __list_iterator<value_type, __void_pointer> iterator;
496 typedef __list_const_iterator<value_type, __void_pointer> const_iterator;
497 typedef __list_node_base<value_type, __void_pointer> __node_base;
498 typedef __list_node<value_type, __void_pointer> __node;
499 typedef typename __alloc_traits::template
500#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
501 rebind_alloc<__node>
502#else
503 rebind_alloc<__node>::other
504#endif
505 __node_allocator;
506 typedef allocator_traits<__node_allocator> __node_alloc_traits;
507 typedef typename __node_alloc_traits::pointer __node_pointer;
508 typedef typename __node_alloc_traits::const_pointer __node_const_pointer;
509 typedef typename __alloc_traits::pointer pointer;
510 typedef typename __alloc_traits::const_pointer const_pointer;
511 typedef typename __alloc_traits::difference_type difference_type;
512
513 __node_base __end_;
514 __compressed_pair<size_type, __node_allocator> __size_alloc_;
515
Howard Hinnant82894812010-09-22 16:48:34 +0000516 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000517 size_type& __sz() _NOEXCEPT {return __size_alloc_.first();}
Howard Hinnant82894812010-09-22 16:48:34 +0000518 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000519 const size_type& __sz() const _NOEXCEPT
520 {return __size_alloc_.first();}
Howard Hinnant82894812010-09-22 16:48:34 +0000521 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000522 __node_allocator& __node_alloc() _NOEXCEPT
523 {return __size_alloc_.second();}
Howard Hinnant82894812010-09-22 16:48:34 +0000524 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000525 const __node_allocator& __node_alloc() const _NOEXCEPT
526 {return __size_alloc_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000527
Howard Hinnantc5607272011-06-03 17:30:28 +0000528 static void __unlink_nodes(__node_base& __f, __node_base& __l) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000529
Howard Hinnantc5607272011-06-03 17:30:28 +0000530 __list_imp()
531 _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000532 __list_imp(const allocator_type& __a);
533 ~__list_imp();
Howard Hinnantc5607272011-06-03 17:30:28 +0000534 void clear() _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +0000535 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000536 bool empty() const _NOEXCEPT {return __sz() == 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000537
Howard Hinnant82894812010-09-22 16:48:34 +0000538 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000539 iterator begin() _NOEXCEPT
540 {
541#if _LIBCPP_DEBUG_LEVEL >= 2
542 return iterator(__end_.__next_, this);
543#else
544 return iterator(__end_.__next_);
545#endif
546 }
Howard Hinnant82894812010-09-22 16:48:34 +0000547 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000548 const_iterator begin() const _NOEXCEPT
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000549 {
550#if _LIBCPP_DEBUG_LEVEL >= 2
551 return const_iterator(__end_.__next_, this);
552#else
553 return const_iterator(__end_.__next_);
554#endif
555 }
Howard Hinnant82894812010-09-22 16:48:34 +0000556 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000557 iterator end() _NOEXCEPT
558 {
559#if _LIBCPP_DEBUG_LEVEL >= 2
560 return iterator(static_cast<__node_pointer>(&__end_), this);
561#else
562 return iterator(static_cast<__node_pointer>(&__end_));
563#endif
564 }
Howard Hinnant82894812010-09-22 16:48:34 +0000565 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000566 const_iterator end() const _NOEXCEPT
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000567 {
568#if _LIBCPP_DEBUG_LEVEL >= 2
569 return const_iterator(static_cast<__node_const_pointer>(&__end_), this);
570#else
571 return const_iterator(static_cast<__node_const_pointer>(&__end_));
572#endif
573 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000574
Howard Hinnantc5607272011-06-03 17:30:28 +0000575 void swap(__list_imp& __c)
576 _NOEXCEPT_(!__node_alloc_traits::propagate_on_container_swap::value ||
577 __is_nothrow_swappable<__node_allocator>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000578
Howard Hinnant82894812010-09-22 16:48:34 +0000579 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000580 void __copy_assign_alloc(const __list_imp& __c)
581 {__copy_assign_alloc(__c, integral_constant<bool,
582 __node_alloc_traits::propagate_on_container_copy_assignment::value>());}
583
Howard Hinnant82894812010-09-22 16:48:34 +0000584 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000585 void __move_assign_alloc(__list_imp& __c)
Howard Hinnantc5607272011-06-03 17:30:28 +0000586 _NOEXCEPT_(
587 !__node_alloc_traits::propagate_on_container_move_assignment::value ||
588 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000589 {__move_assign_alloc(__c, integral_constant<bool,
590 __node_alloc_traits::propagate_on_container_move_assignment::value>());}
591
592private:
Howard Hinnant82894812010-09-22 16:48:34 +0000593 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000594 static void __swap_alloc(__node_allocator& __x, __node_allocator& __y)
Howard Hinnantc5607272011-06-03 17:30:28 +0000595 _NOEXCEPT_(!__node_alloc_traits::propagate_on_container_swap::value ||
596 __is_nothrow_swappable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000597 {__swap_alloc(__x, __y, integral_constant<bool,
598 __node_alloc_traits::propagate_on_container_swap::value>());}
Howard Hinnant82894812010-09-22 16:48:34 +0000599 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000600 static void __swap_alloc(__node_allocator& __x, __node_allocator& __y, true_type)
Howard Hinnantc5607272011-06-03 17:30:28 +0000601 _NOEXCEPT_(__is_nothrow_swappable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000602 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000603 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000604 swap(__x, __y);
605 }
Howard Hinnant82894812010-09-22 16:48:34 +0000606 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000607 static void __swap_alloc(__node_allocator& __x, __node_allocator& __y, false_type)
Howard Hinnantc5607272011-06-03 17:30:28 +0000608 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000609 {}
610
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, true_type)
613 {
614 if (__node_alloc() != __c.__node_alloc())
615 clear();
616 __node_alloc() = __c.__node_alloc();
617 }
618
Howard Hinnant82894812010-09-22 16:48:34 +0000619 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000620 void __copy_assign_alloc(const __list_imp& __c, false_type)
621 {}
622
Howard Hinnant82894812010-09-22 16:48:34 +0000623 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31 +0000624 void __move_assign_alloc(__list_imp& __c, true_type)
Howard Hinnantc5607272011-06-03 17:30:28 +0000625 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000626 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000627 __node_alloc() = _VSTD::move(__c.__node_alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000628 }
629
Howard Hinnant82894812010-09-22 16:48:34 +0000630 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31 +0000631 void __move_assign_alloc(__list_imp& __c, false_type)
Howard Hinnantc5607272011-06-03 17:30:28 +0000632 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000633 {}
634};
635
636// Unlink nodes [__f, __l]
637template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +0000638inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000639void
640__list_imp<_Tp, _Alloc>::__unlink_nodes(__node_base& __f, __node_base& __l)
Howard Hinnantc5607272011-06-03 17:30:28 +0000641 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000642{
643 __f.__prev_->__next_ = __l.__next_;
644 __l.__next_->__prev_ = __f.__prev_;
645}
646
647template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +0000648inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000649__list_imp<_Tp, _Alloc>::__list_imp()
Howard Hinnantc5607272011-06-03 17:30:28 +0000650 _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000651 : __size_alloc_(0)
652{
653}
654
655template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +0000656inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000657__list_imp<_Tp, _Alloc>::__list_imp(const allocator_type& __a)
658 : __size_alloc_(0, __node_allocator(__a))
659{
660}
661
662template <class _Tp, class _Alloc>
663__list_imp<_Tp, _Alloc>::~__list_imp()
664{
665 clear();
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000666#if _LIBCPP_DEBUG_LEVEL >= 2
667 __get_db()->__erase_c(this);
668#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000669}
670
671template <class _Tp, class _Alloc>
672void
Howard Hinnantc5607272011-06-03 17:30:28 +0000673__list_imp<_Tp, _Alloc>::clear() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000674{
675 if (!empty())
676 {
677 __node_allocator& __na = __node_alloc();
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000678 __node_pointer __f = __end_.__next_;
679 __node_pointer __l = static_cast<__node_pointer>(&__end_);
680 __unlink_nodes(*__f, *__l->__prev_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000681 __sz() = 0;
682 while (__f != __l)
683 {
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000684 __node& __n = *__f;
685 __f = __f->__next_;
Howard Hinnant0949eed2011-06-30 21:18:19 +0000686 __node_alloc_traits::destroy(__na, _VSTD::addressof(__n.__value_));
687 __node_alloc_traits::deallocate(__na, _VSTD::addressof(__n), 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000688 }
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000689#if _LIBCPP_DEBUG_LEVEL >= 2
690 __c_node* __c = __get_db()->__find_c_and_lock(this);
691 for (__i_node** __p = __c->end_; __p != __c->beg_; )
692 {
693 --__p;
694 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
695 if (__i->__ptr_ != __l)
696 {
697 (*__p)->__c_ = nullptr;
698 if (--__c->end_ != __p)
699 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
700 }
701 }
702 __get_db()->unlock();
703#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000704 }
705}
706
707template <class _Tp, class _Alloc>
708void
709__list_imp<_Tp, _Alloc>::swap(__list_imp& __c)
Howard Hinnantc5607272011-06-03 17:30:28 +0000710 _NOEXCEPT_(!__node_alloc_traits::propagate_on_container_swap::value ||
711 __is_nothrow_swappable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000712{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000713 _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
714 this->__node_alloc() == __c.__node_alloc(),
715 "list::swap: Either propagate_on_container_swap must be true"
716 " or the allocators must compare equal");
Howard Hinnant0949eed2011-06-30 21:18:19 +0000717 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000718 __swap_alloc(__node_alloc(), __c.__node_alloc());
719 swap(__sz(), __c.__sz());
720 swap(__end_, __c.__end_);
721 if (__sz() == 0)
722 __end_.__next_ = __end_.__prev_ = &static_cast<__node&>(__end_);
723 else
724 __end_.__prev_->__next_ = __end_.__next_->__prev_
725 = &static_cast<__node&>(__end_);
726 if (__c.__sz() == 0)
727 __c.__end_.__next_ = __c.__end_.__prev_
728 = &static_cast<__node&>(__c.__end_);
729 else
730 __c.__end_.__prev_->__next_ = __c.__end_.__next_->__prev_
731 = &static_cast<__node&>(__c.__end_);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000732#if _LIBCPP_DEBUG_LEVEL >= 2
733 __libcpp_db* __db = __get_db();
734 __c_node* __cn1 = __db->__find_c_and_lock(this);
735 __c_node* __cn2 = __db->__find_c(&__c);
736 std::swap(__cn1->beg_, __cn2->beg_);
737 std::swap(__cn1->end_, __cn2->end_);
738 std::swap(__cn1->cap_, __cn2->cap_);
739 for (__i_node** __p = __cn1->end_; __p != __cn1->beg_;)
740 {
741 --__p;
742 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
743 if (__i->__ptr_ == static_cast<__node_pointer>(&__c.__end_))
744 {
745 __cn2->__add(*__p);
746 if (--__cn1->end_ != __p)
747 memmove(__p, __p+1, (__cn1->end_ - __p)*sizeof(__i_node*));
748 }
749 else
750 (*__p)->__c_ = __cn1;
751 }
752 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
753 {
754 --__p;
755 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
756 if (__i->__ptr_ == static_cast<__node_pointer>(&__end_))
757 {
758 __cn1->__add(*__p);
759 if (--__cn2->end_ != __p)
760 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
761 }
762 else
763 (*__p)->__c_ = __cn2;
764 }
765 __db->unlock();
766#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000767}
768
769template <class _Tp, class _Alloc = allocator<_Tp> >
Howard Hinnant83eade62013-03-06 23:30:19 +0000770class _LIBCPP_TYPE_VIS list
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000771 : private __list_imp<_Tp, _Alloc>
772{
773 typedef __list_imp<_Tp, _Alloc> base;
774 typedef typename base::__node __node;
775 typedef typename base::__node_allocator __node_allocator;
776 typedef typename base::__node_pointer __node_pointer;
777 typedef typename base::__node_alloc_traits __node_alloc_traits;
778
779public:
780 typedef _Tp value_type;
781 typedef _Alloc allocator_type;
782 static_assert((is_same<value_type, typename allocator_type::value_type>::value),
783 "Invalid allocator::value_type");
784 typedef value_type& reference;
785 typedef const value_type& const_reference;
786 typedef typename base::pointer pointer;
787 typedef typename base::const_pointer const_pointer;
788 typedef typename base::size_type size_type;
789 typedef typename base::difference_type difference_type;
790 typedef typename base::iterator iterator;
791 typedef typename base::const_iterator const_iterator;
Howard Hinnant0949eed2011-06-30 21:18:19 +0000792 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
793 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000794
Howard Hinnant82894812010-09-22 16:48:34 +0000795 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000796 list()
797 _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value)
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000798 {
799#if _LIBCPP_DEBUG_LEVEL >= 2
800 __get_db()->__insert_c(this);
801#endif
802 }
Howard Hinnant82894812010-09-22 16:48:34 +0000803 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000804 list(const allocator_type& __a) : base(__a)
805 {
806#if _LIBCPP_DEBUG_LEVEL >= 2
807 __get_db()->__insert_c(this);
808#endif
809 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000810 list(size_type __n);
811 list(size_type __n, const value_type& __x);
812 list(size_type __n, const value_type& __x, const allocator_type& __a);
813 template <class _InpIter>
814 list(_InpIter __f, _InpIter __l,
815 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
816 template <class _InpIter>
817 list(_InpIter __f, _InpIter __l, const allocator_type& __a,
818 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
819
820 list(const list& __c);
821 list(const list& __c, const allocator_type& __a);
822 list& operator=(const list& __c);
Howard Hinnante3e32912011-08-12 21:56:02 +0000823#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000824 list(initializer_list<value_type> __il);
825 list(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnante3e32912011-08-12 21:56:02 +0000826#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant73d21a42010-09-04 23:28:19 +0000827#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc5607272011-06-03 17:30:28 +0000828 list(list&& __c)
829 _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000830 list(list&& __c, const allocator_type& __a);
Howard Hinnantc5607272011-06-03 17:30:28 +0000831 list& operator=(list&& __c)
832 _NOEXCEPT_(
833 __node_alloc_traits::propagate_on_container_move_assignment::value &&
834 is_nothrow_move_assignable<__node_allocator>::value);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000835#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnante3e32912011-08-12 21:56:02 +0000836#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant82894812010-09-22 16:48:34 +0000837 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000838 list& operator=(initializer_list<value_type> __il)
839 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnante3e32912011-08-12 21:56:02 +0000840#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000841
842 template <class _InpIter>
843 void assign(_InpIter __f, _InpIter __l,
844 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
845 void assign(size_type __n, const value_type& __x);
Howard Hinnante3e32912011-08-12 21:56:02 +0000846#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant82894812010-09-22 16:48:34 +0000847 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000848 void assign(initializer_list<value_type> __il)
849 {assign(__il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +0000850#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000851
Howard Hinnantc5607272011-06-03 17:30:28 +0000852 allocator_type get_allocator() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000853
Howard Hinnant82894812010-09-22 16:48:34 +0000854 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000855 size_type size() const _NOEXCEPT {return base::__sz();}
Howard Hinnant82894812010-09-22 16:48:34 +0000856 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000857 bool empty() const _NOEXCEPT {return base::empty();}
Howard Hinnant82894812010-09-22 16:48:34 +0000858 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000859 size_type max_size() const _NOEXCEPT
860 {return numeric_limits<difference_type>::max();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000861
Howard Hinnant82894812010-09-22 16:48:34 +0000862 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000863 iterator begin() _NOEXCEPT {return base::begin();}
Howard Hinnant82894812010-09-22 16:48:34 +0000864 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000865 const_iterator begin() const _NOEXCEPT {return base::begin();}
Howard Hinnant82894812010-09-22 16:48:34 +0000866 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000867 iterator end() _NOEXCEPT {return base::end();}
Howard Hinnant82894812010-09-22 16:48:34 +0000868 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000869 const_iterator end() const _NOEXCEPT {return base::end();}
Howard Hinnant82894812010-09-22 16:48:34 +0000870 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000871 const_iterator cbegin() const _NOEXCEPT {return base::begin();}
Howard Hinnant82894812010-09-22 16:48:34 +0000872 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000873 const_iterator cend() const _NOEXCEPT {return base::end();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000874
Howard Hinnant82894812010-09-22 16:48:34 +0000875 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000876 reverse_iterator rbegin() _NOEXCEPT
877 {return reverse_iterator(end());}
Howard Hinnant82894812010-09-22 16:48:34 +0000878 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000879 const_reverse_iterator rbegin() const _NOEXCEPT
880 {return const_reverse_iterator(end());}
Howard Hinnant82894812010-09-22 16:48:34 +0000881 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000882 reverse_iterator rend() _NOEXCEPT
883 {return reverse_iterator(begin());}
Howard Hinnant82894812010-09-22 16:48:34 +0000884 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000885 const_reverse_iterator rend() const _NOEXCEPT
886 {return const_reverse_iterator(begin());}
Howard Hinnant82894812010-09-22 16:48:34 +0000887 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000888 const_reverse_iterator crbegin() const _NOEXCEPT
889 {return const_reverse_iterator(end());}
Howard Hinnant82894812010-09-22 16:48:34 +0000890 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000891 const_reverse_iterator crend() const _NOEXCEPT
892 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000893
Howard Hinnant82894812010-09-22 16:48:34 +0000894 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000895 reference front()
896 {
897 _LIBCPP_ASSERT(!empty(), "list::front called on empty list");
898 return base::__end_.__next_->__value_;
899 }
Howard Hinnant82894812010-09-22 16:48:34 +0000900 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000901 const_reference front() const
902 {
903 _LIBCPP_ASSERT(!empty(), "list::front called on empty list");
904 return base::__end_.__next_->__value_;
905 }
Howard Hinnant82894812010-09-22 16:48:34 +0000906 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000907 reference back()
908 {
909 _LIBCPP_ASSERT(!empty(), "list::back called on empty list");
910 return base::__end_.__prev_->__value_;
911 }
Howard Hinnant82894812010-09-22 16:48:34 +0000912 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000913 const_reference back() const
914 {
915 _LIBCPP_ASSERT(!empty(), "list::back called on empty list");
916 return base::__end_.__prev_->__value_;
917 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000918
Howard Hinnant73d21a42010-09-04 23:28:19 +0000919#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000920 void push_front(value_type&& __x);
921 void push_back(value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000922#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000923 template <class... _Args>
924 void emplace_front(_Args&&... __args);
925 template <class... _Args>
926 void emplace_back(_Args&&... __args);
927 template <class... _Args>
928 iterator emplace(const_iterator __p, _Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000929#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000930 iterator insert(const_iterator __p, value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000931#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000932
933 void push_front(const value_type& __x);
934 void push_back(const value_type& __x);
935
936 iterator insert(const_iterator __p, const value_type& __x);
937 iterator insert(const_iterator __p, size_type __n, const value_type& __x);
938 template <class _InpIter>
939 iterator insert(const_iterator __p, _InpIter __f, _InpIter __l,
940 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
Howard Hinnante3e32912011-08-12 21:56:02 +0000941#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant82894812010-09-22 16:48:34 +0000942 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000943 iterator insert(const_iterator __p, initializer_list<value_type> __il)
944 {return insert(__p, __il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +0000945#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000946
Howard Hinnant82894812010-09-22 16:48:34 +0000947 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000948 void swap(list& __c)
949 _NOEXCEPT_(!__node_alloc_traits::propagate_on_container_swap::value ||
950 __is_nothrow_swappable<__node_allocator>::value)
951 {base::swap(__c);}
Howard Hinnant82894812010-09-22 16:48:34 +0000952 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000953 void clear() _NOEXCEPT {base::clear();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000954
955 void pop_front();
956 void pop_back();
957
958 iterator erase(const_iterator __p);
959 iterator erase(const_iterator __f, const_iterator __l);
960
961 void resize(size_type __n);
962 void resize(size_type __n, const value_type& __x);
963
964 void splice(const_iterator __p, list& __c);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000965#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant82894812010-09-22 16:48:34 +0000966 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000967 void splice(const_iterator __p, list&& __c) {splice(__p, __c);}
968#endif
969 void splice(const_iterator __p, list& __c, const_iterator __i);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000970#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant82894812010-09-22 16:48:34 +0000971 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000972 void splice(const_iterator __p, list&& __c, const_iterator __i)
973 {splice(__p, __c, __i);}
Howard Hinnant73d21a42010-09-04 23:28:19 +0000974#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000975 void splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000976#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant82894812010-09-22 16:48:34 +0000977 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000978 void splice(const_iterator __p, list&& __c, const_iterator __f, const_iterator __l)
979 {splice(__p, __c, __f, __l);}
Howard Hinnant73d21a42010-09-04 23:28:19 +0000980#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000981
982 void remove(const value_type& __x);
983 template <class _Pred> void remove_if(_Pred __pred);
984 void unique();
985 template <class _BinaryPred>
986 void unique(_BinaryPred __binary_pred);
987 void merge(list& __c);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000988#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant82894812010-09-22 16:48:34 +0000989 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000990 void merge(list&& __c) {merge(__c);}
991#endif
992 template <class _Comp>
993 void merge(list& __c, _Comp __comp);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000994#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000995 template <class _Comp>
Howard Hinnant82894812010-09-22 16:48:34 +0000996 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000997 void merge(list&& __c, _Comp __comp) {merge(__c, __comp);}
Howard Hinnant73d21a42010-09-04 23:28:19 +0000998#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000999 void sort();
1000 template <class _Comp>
1001 void sort(_Comp __comp);
1002
Howard Hinnantc5607272011-06-03 17:30:28 +00001003 void reverse() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001004
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001005 bool __invariants() const;
1006
1007#if _LIBCPP_DEBUG_LEVEL >= 2
1008
1009 bool __dereferenceable(const const_iterator* __i) const;
1010 bool __decrementable(const const_iterator* __i) const;
1011 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1012 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1013
1014#endif // _LIBCPP_DEBUG_LEVEL >= 2
1015
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001016private:
1017 static void __link_nodes(__node& __p, __node& __f, __node& __l);
1018 iterator __iterator(size_type __n);
1019 template <class _Comp>
1020 static iterator __sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp);
1021
Howard Hinnantc5607272011-06-03 17:30:28 +00001022 void __move_assign(list& __c, true_type)
1023 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001024 void __move_assign(list& __c, false_type);
1025};
1026
1027// Link in nodes [__f, __l] just prior to __p
1028template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001029inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001030void
1031list<_Tp, _Alloc>::__link_nodes(__node& __p, __node& __f, __node& __l)
1032{
1033 __p.__prev_->__next_ = &__f;
1034 __f.__prev_ = __p.__prev_;
1035 __p.__prev_ = &__l;
1036 __l.__next_ = &__p;
1037}
1038
1039template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001040inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001041typename list<_Tp, _Alloc>::iterator
1042list<_Tp, _Alloc>::__iterator(size_type __n)
1043{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001044 return __n <= base::__sz() / 2 ? _VSTD::next(begin(), __n)
1045 : _VSTD::prev(end(), base::__sz() - __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001046}
1047
1048template <class _Tp, class _Alloc>
1049list<_Tp, _Alloc>::list(size_type __n)
1050{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001051#if _LIBCPP_DEBUG_LEVEL >= 2
1052 __get_db()->__insert_c(this);
1053#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001054 for (; __n > 0; --__n)
Howard Hinnant73d21a42010-09-04 23:28:19 +00001055#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001056 emplace_back();
1057#else
1058 push_back(value_type());
1059#endif
1060}
1061
1062template <class _Tp, class _Alloc>
1063list<_Tp, _Alloc>::list(size_type __n, const value_type& __x)
1064{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001065#if _LIBCPP_DEBUG_LEVEL >= 2
1066 __get_db()->__insert_c(this);
1067#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001068 for (; __n > 0; --__n)
1069 push_back(__x);
1070}
1071
1072template <class _Tp, class _Alloc>
1073list<_Tp, _Alloc>::list(size_type __n, const value_type& __x, const allocator_type& __a)
1074 : base(__a)
1075{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001076#if _LIBCPP_DEBUG_LEVEL >= 2
1077 __get_db()->__insert_c(this);
1078#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001079 for (; __n > 0; --__n)
1080 push_back(__x);
1081}
1082
1083template <class _Tp, class _Alloc>
1084template <class _InpIter>
1085list<_Tp, _Alloc>::list(_InpIter __f, _InpIter __l,
1086 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1087{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001088#if _LIBCPP_DEBUG_LEVEL >= 2
1089 __get_db()->__insert_c(this);
1090#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001091 for (; __f != __l; ++__f)
1092 push_back(*__f);
1093}
1094
1095template <class _Tp, class _Alloc>
1096template <class _InpIter>
1097list<_Tp, _Alloc>::list(_InpIter __f, _InpIter __l, const allocator_type& __a,
1098 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1099 : base(__a)
1100{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001101#if _LIBCPP_DEBUG_LEVEL >= 2
1102 __get_db()->__insert_c(this);
1103#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001104 for (; __f != __l; ++__f)
1105 push_back(*__f);
1106}
1107
1108template <class _Tp, class _Alloc>
1109list<_Tp, _Alloc>::list(const list& __c)
1110 : base(allocator_type(
1111 __node_alloc_traits::select_on_container_copy_construction(
1112 __c.__node_alloc())))
1113{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001114#if _LIBCPP_DEBUG_LEVEL >= 2
1115 __get_db()->__insert_c(this);
1116#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001117 for (const_iterator __i = __c.begin(), __e = __c.end(); __i != __e; ++__i)
1118 push_back(*__i);
1119}
1120
1121template <class _Tp, class _Alloc>
1122list<_Tp, _Alloc>::list(const list& __c, const allocator_type& __a)
1123 : base(__a)
1124{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001125#if _LIBCPP_DEBUG_LEVEL >= 2
1126 __get_db()->__insert_c(this);
1127#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001128 for (const_iterator __i = __c.begin(), __e = __c.end(); __i != __e; ++__i)
1129 push_back(*__i);
1130}
1131
Howard Hinnante3e32912011-08-12 21:56:02 +00001132#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1133
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001134template <class _Tp, class _Alloc>
1135list<_Tp, _Alloc>::list(initializer_list<value_type> __il, const allocator_type& __a)
1136 : base(__a)
1137{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001138#if _LIBCPP_DEBUG_LEVEL >= 2
1139 __get_db()->__insert_c(this);
1140#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001141 for (typename initializer_list<value_type>::const_iterator __i = __il.begin(),
1142 __e = __il.end(); __i != __e; ++__i)
1143 push_back(*__i);
1144}
1145
1146template <class _Tp, class _Alloc>
1147list<_Tp, _Alloc>::list(initializer_list<value_type> __il)
1148{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001149#if _LIBCPP_DEBUG_LEVEL >= 2
1150 __get_db()->__insert_c(this);
1151#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001152 for (typename initializer_list<value_type>::const_iterator __i = __il.begin(),
1153 __e = __il.end(); __i != __e; ++__i)
1154 push_back(*__i);
1155}
1156
Howard Hinnante3e32912011-08-12 21:56:02 +00001157#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1158
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001159template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001160inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001161list<_Tp, _Alloc>&
1162list<_Tp, _Alloc>::operator=(const list& __c)
1163{
1164 if (this != &__c)
1165 {
1166 base::__copy_assign_alloc(__c);
1167 assign(__c.begin(), __c.end());
1168 }
1169 return *this;
1170}
1171
Howard Hinnant73d21a42010-09-04 23:28:19 +00001172#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001173
1174template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001175inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001176list<_Tp, _Alloc>::list(list&& __c)
Howard Hinnantc5607272011-06-03 17:30:28 +00001177 _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001178 : base(allocator_type(_VSTD::move(__c.__node_alloc())))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001179{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001180#if _LIBCPP_DEBUG_LEVEL >= 2
1181 __get_db()->__insert_c(this);
1182#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001183 splice(end(), __c);
1184}
1185
1186template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001187inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001188list<_Tp, _Alloc>::list(list&& __c, const allocator_type& __a)
1189 : base(__a)
1190{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001191#if _LIBCPP_DEBUG_LEVEL >= 2
1192 __get_db()->__insert_c(this);
1193#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001194 if (__a == __c.get_allocator())
1195 splice(end(), __c);
1196 else
1197 {
Howard Hinnant99968442011-11-29 18:15:50 +00001198 typedef move_iterator<iterator> _Ip;
1199 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001200 }
1201}
1202
1203template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001204inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001205list<_Tp, _Alloc>&
1206list<_Tp, _Alloc>::operator=(list&& __c)
Howard Hinnantc5607272011-06-03 17:30:28 +00001207 _NOEXCEPT_(
1208 __node_alloc_traits::propagate_on_container_move_assignment::value &&
1209 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001210{
1211 __move_assign(__c, integral_constant<bool,
1212 __node_alloc_traits::propagate_on_container_move_assignment::value>());
1213 return *this;
1214}
1215
1216template <class _Tp, class _Alloc>
1217void
1218list<_Tp, _Alloc>::__move_assign(list& __c, false_type)
1219{
1220 if (base::__node_alloc() != __c.__node_alloc())
1221 {
Howard Hinnant99968442011-11-29 18:15:50 +00001222 typedef move_iterator<iterator> _Ip;
1223 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001224 }
1225 else
1226 __move_assign(__c, true_type());
1227}
1228
1229template <class _Tp, class _Alloc>
1230void
1231list<_Tp, _Alloc>::__move_assign(list& __c, true_type)
Howard Hinnantc5607272011-06-03 17:30:28 +00001232 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001233{
1234 clear();
1235 base::__move_assign_alloc(__c);
1236 splice(end(), __c);
1237}
1238
Howard Hinnant73d21a42010-09-04 23:28:19 +00001239#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001240
1241template <class _Tp, class _Alloc>
1242template <class _InpIter>
1243void
1244list<_Tp, _Alloc>::assign(_InpIter __f, _InpIter __l,
1245 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1246{
1247 iterator __i = begin();
1248 iterator __e = end();
1249 for (; __f != __l && __i != __e; ++__f, ++__i)
1250 *__i = *__f;
1251 if (__i == __e)
1252 insert(__e, __f, __l);
1253 else
1254 erase(__i, __e);
1255}
1256
1257template <class _Tp, class _Alloc>
1258void
1259list<_Tp, _Alloc>::assign(size_type __n, const value_type& __x)
1260{
1261 iterator __i = begin();
1262 iterator __e = end();
1263 for (; __n > 0 && __i != __e; --__n, ++__i)
1264 *__i = __x;
1265 if (__i == __e)
1266 insert(__e, __n, __x);
1267 else
1268 erase(__i, __e);
1269}
1270
1271template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001272inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001273_Alloc
Howard Hinnantc5607272011-06-03 17:30:28 +00001274list<_Tp, _Alloc>::get_allocator() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001275{
1276 return allocator_type(base::__node_alloc());
1277}
1278
1279template <class _Tp, class _Alloc>
1280typename list<_Tp, _Alloc>::iterator
1281list<_Tp, _Alloc>::insert(const_iterator __p, const value_type& __x)
1282{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001283#if _LIBCPP_DEBUG_LEVEL >= 2
1284 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1285 "list::insert(iterator, x) called with an iterator not"
1286 " referring to this list");
1287#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001288 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001289 typedef __allocator_destructor<__node_allocator> _Dp;
1290 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001291 __hold->__prev_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001292 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001293 __link_nodes(const_cast<__node&>(*__p.__ptr_), *__hold, *__hold);
1294 ++base::__sz();
Howard Hinnant79a35572013-04-05 00:18:49 +00001295#if _LIBCPP_DEBUG_LEVEL >= 2
1296 return iterator(__hold.release(), this);
1297#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001298 return iterator(__hold.release());
Howard Hinnant79a35572013-04-05 00:18:49 +00001299#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001300}
1301
1302template <class _Tp, class _Alloc>
1303typename list<_Tp, _Alloc>::iterator
1304list<_Tp, _Alloc>::insert(const_iterator __p, size_type __n, const value_type& __x)
1305{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001306#if _LIBCPP_DEBUG_LEVEL >= 2
1307 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1308 "list::insert(iterator, n, x) called with an iterator not"
1309 " referring to this list");
1310 iterator __r(const_cast<__node_pointer>(__p.__ptr_), this);
1311#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001312 iterator __r(const_cast<__node_pointer>(__p.__ptr_));
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001313#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001314 if (__n > 0)
1315 {
1316 size_type __ds = 0;
1317 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001318 typedef __allocator_destructor<__node_allocator> _Dp;
1319 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001320 __hold->__prev_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001321 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001322 ++__ds;
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001323#if _LIBCPP_DEBUG_LEVEL >= 2
1324 __r = iterator(__hold.get(), this);
1325#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001326 __r = iterator(__hold.get());
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001327#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001328 __hold.release();
1329 iterator __e = __r;
1330#ifndef _LIBCPP_NO_EXCEPTIONS
1331 try
1332 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001333#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001334 for (--__n; __n != 0; --__n, ++__e, ++__ds)
1335 {
1336 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001337 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001338 __e.__ptr_->__next_ = __hold.get();
1339 __hold->__prev_ = __e.__ptr_;
1340 __hold.release();
1341 }
1342#ifndef _LIBCPP_NO_EXCEPTIONS
1343 }
1344 catch (...)
1345 {
1346 while (true)
1347 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001348 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001349 __node_pointer __prev = __e.__ptr_->__prev_;
1350 __node_alloc_traits::deallocate(__na, __e.__ptr_, 1);
1351 if (__prev == 0)
1352 break;
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001353#if _LIBCPP_DEBUG_LEVEL >= 2
1354 __e = iterator(__prev, this);
1355#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001356 __e = iterator(__prev);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001357#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001358 }
1359 throw;
1360 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001361#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001362 __link_nodes(const_cast<__node&>(*__p.__ptr_), *__r.__ptr_, *__e.__ptr_);
1363 base::__sz() += __ds;
1364 }
1365 return __r;
1366}
1367
1368template <class _Tp, class _Alloc>
1369template <class _InpIter>
1370typename list<_Tp, _Alloc>::iterator
1371list<_Tp, _Alloc>::insert(const_iterator __p, _InpIter __f, _InpIter __l,
1372 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1373{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001374#if _LIBCPP_DEBUG_LEVEL >= 2
1375 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1376 "list::insert(iterator, range) called with an iterator not"
1377 " referring to this list");
1378 iterator __r(const_cast<__node_pointer>(__p.__ptr_), this);
1379#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001380 iterator __r(const_cast<__node_pointer>(__p.__ptr_));
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001381#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001382 if (__f != __l)
1383 {
1384 size_type __ds = 0;
1385 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001386 typedef __allocator_destructor<__node_allocator> _Dp;
1387 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001388 __hold->__prev_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001389 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), *__f);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001390 ++__ds;
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001391#if _LIBCPP_DEBUG_LEVEL >= 2
1392 __r = iterator(__hold.get(), this);
1393#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001394 __r = iterator(__hold.get());
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001395#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001396 __hold.release();
1397 iterator __e = __r;
1398#ifndef _LIBCPP_NO_EXCEPTIONS
1399 try
1400 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001401#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001402 for (++__f; __f != __l; ++__f, ++__e, ++__ds)
1403 {
1404 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001405 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), *__f);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001406 __e.__ptr_->__next_ = __hold.get();
1407 __hold->__prev_ = __e.__ptr_;
1408 __hold.release();
1409 }
1410#ifndef _LIBCPP_NO_EXCEPTIONS
1411 }
1412 catch (...)
1413 {
1414 while (true)
1415 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001416 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001417 __node_pointer __prev = __e.__ptr_->__prev_;
1418 __node_alloc_traits::deallocate(__na, __e.__ptr_, 1);
1419 if (__prev == 0)
1420 break;
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001421#if _LIBCPP_DEBUG_LEVEL >= 2
1422 __e = iterator(__prev, this);
1423#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001424 __e = iterator(__prev);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001425#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001426 }
1427 throw;
1428 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001429#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001430 __link_nodes(const_cast<__node&>(*__p.__ptr_), *__r.__ptr_, *__e.__ptr_);
1431 base::__sz() += __ds;
1432 }
1433 return __r;
1434}
1435
1436template <class _Tp, class _Alloc>
1437void
1438list<_Tp, _Alloc>::push_front(const value_type& __x)
1439{
1440 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001441 typedef __allocator_destructor<__node_allocator> _Dp;
1442 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001443 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001444 __link_nodes(*base::__end_.__next_, *__hold, *__hold);
1445 ++base::__sz();
1446 __hold.release();
1447}
1448
1449template <class _Tp, class _Alloc>
1450void
1451list<_Tp, _Alloc>::push_back(const value_type& __x)
1452{
1453 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001454 typedef __allocator_destructor<__node_allocator> _Dp;
1455 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001456 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001457 __link_nodes(static_cast<__node&>(base::__end_), *__hold, *__hold);
1458 ++base::__sz();
1459 __hold.release();
1460}
1461
Howard Hinnant73d21a42010-09-04 23:28:19 +00001462#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001463
1464template <class _Tp, class _Alloc>
1465void
1466list<_Tp, _Alloc>::push_front(value_type&& __x)
1467{
1468 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001469 typedef __allocator_destructor<__node_allocator> _Dp;
1470 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001471 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001472 __link_nodes(*base::__end_.__next_, *__hold, *__hold);
1473 ++base::__sz();
1474 __hold.release();
1475}
1476
1477template <class _Tp, class _Alloc>
1478void
1479list<_Tp, _Alloc>::push_back(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_), _VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001485 __link_nodes(static_cast<__node&>(base::__end_), *__hold, *__hold);
1486 ++base::__sz();
1487 __hold.release();
1488}
1489
Howard Hinnant73d21a42010-09-04 23:28:19 +00001490#ifndef _LIBCPP_HAS_NO_VARIADICS
1491
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001492template <class _Tp, class _Alloc>
1493template <class... _Args>
1494void
1495list<_Tp, _Alloc>::emplace_front(_Args&&... __args)
1496{
1497 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001498 typedef __allocator_destructor<__node_allocator> _Dp;
1499 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001500 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001501 __link_nodes(*base::__end_.__next_, *__hold, *__hold);
1502 ++base::__sz();
1503 __hold.release();
1504}
1505
1506template <class _Tp, class _Alloc>
1507template <class... _Args>
1508void
1509list<_Tp, _Alloc>::emplace_back(_Args&&... __args)
1510{
1511 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001512 typedef __allocator_destructor<__node_allocator> _Dp;
1513 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001514 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001515 __link_nodes(static_cast<__node&>(base::__end_), *__hold, *__hold);
1516 ++base::__sz();
1517 __hold.release();
1518}
1519
1520template <class _Tp, class _Alloc>
1521template <class... _Args>
1522typename list<_Tp, _Alloc>::iterator
1523list<_Tp, _Alloc>::emplace(const_iterator __p, _Args&&... __args)
1524{
Howard Hinnant79a35572013-04-05 00:18:49 +00001525#if _LIBCPP_DEBUG_LEVEL >= 2
1526 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1527 "list::emplace(iterator, args...) called with an iterator not"
1528 " referring to this list");
1529#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001530 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001531 typedef __allocator_destructor<__node_allocator> _Dp;
1532 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001533 __hold->__prev_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001534 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001535 __link_nodes(const_cast<__node&>(*__p.__ptr_), *__hold, *__hold);
1536 ++base::__sz();
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001537#if _LIBCPP_DEBUG_LEVEL >= 2
1538 return iterator(__hold.release(), this);
1539#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001540 return iterator(__hold.release());
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001541#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001542}
1543
Howard Hinnant73d21a42010-09-04 23:28:19 +00001544#endif // _LIBCPP_HAS_NO_VARIADICS
1545
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001546template <class _Tp, class _Alloc>
1547typename list<_Tp, _Alloc>::iterator
1548list<_Tp, _Alloc>::insert(const_iterator __p, value_type&& __x)
1549{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001550#if _LIBCPP_DEBUG_LEVEL >= 2
1551 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1552 "list::insert(iterator, x) called with an iterator not"
1553 " referring to this list");
1554#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001555 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001556 typedef __allocator_destructor<__node_allocator> _Dp;
1557 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001558 __hold->__prev_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001559 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001560 __link_nodes(const_cast<__node&>(*__p.__ptr_), *__hold, *__hold);
1561 ++base::__sz();
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001562#if _LIBCPP_DEBUG_LEVEL >= 2
1563 return iterator(__hold.release(), this);
1564#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001565 return iterator(__hold.release());
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001566#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001567}
1568
Howard Hinnant73d21a42010-09-04 23:28:19 +00001569#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001570
1571template <class _Tp, class _Alloc>
1572void
1573list<_Tp, _Alloc>::pop_front()
1574{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001575 _LIBCPP_ASSERT(!empty(), "list::pop_front() called with empty list");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001576 __node_allocator& __na = base::__node_alloc();
1577 __node& __n = *base::__end_.__next_;
1578 base::__unlink_nodes(__n, __n);
1579 --base::__sz();
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001580#if _LIBCPP_DEBUG_LEVEL >= 2
1581 __c_node* __c = __get_db()->__find_c_and_lock(this);
1582 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1583 {
1584 --__p;
1585 iterator* __i = static_cast<iterator*>((*__p)->__i_);
1586 if (__i->__ptr_ == &__n)
1587 {
1588 (*__p)->__c_ = nullptr;
1589 if (--__c->end_ != __p)
1590 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1591 }
1592 }
1593 __get_db()->unlock();
1594#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00001595 __node_alloc_traits::destroy(__na, _VSTD::addressof(__n.__value_));
1596 __node_alloc_traits::deallocate(__na, _VSTD::addressof(__n), 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001597}
1598
1599template <class _Tp, class _Alloc>
1600void
1601list<_Tp, _Alloc>::pop_back()
1602{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001603 _LIBCPP_ASSERT(!empty(), "list::pop_front() called with empty list");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001604 __node_allocator& __na = base::__node_alloc();
1605 __node& __n = *base::__end_.__prev_;
1606 base::__unlink_nodes(__n, __n);
1607 --base::__sz();
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001608#if _LIBCPP_DEBUG_LEVEL >= 2
1609 __c_node* __c = __get_db()->__find_c_and_lock(this);
1610 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1611 {
1612 --__p;
1613 iterator* __i = static_cast<iterator*>((*__p)->__i_);
1614 if (__i->__ptr_ == &__n)
1615 {
1616 (*__p)->__c_ = nullptr;
1617 if (--__c->end_ != __p)
1618 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1619 }
1620 }
1621 __get_db()->unlock();
1622#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00001623 __node_alloc_traits::destroy(__na, _VSTD::addressof(__n.__value_));
1624 __node_alloc_traits::deallocate(__na, _VSTD::addressof(__n), 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001625}
1626
1627template <class _Tp, class _Alloc>
1628typename list<_Tp, _Alloc>::iterator
1629list<_Tp, _Alloc>::erase(const_iterator __p)
1630{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001631#if _LIBCPP_DEBUG_LEVEL >= 2
1632 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1633 "list::erase(iterator) called with an iterator not"
1634 " referring to this list");
1635#endif
Howard Hinnant79a35572013-04-05 00:18:49 +00001636 _LIBCPP_ASSERT(__p != end(),
1637 "list::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001638 __node_allocator& __na = base::__node_alloc();
1639 __node& __n = const_cast<__node&>(*__p.__ptr_);
1640 __node_pointer __r = __n.__next_;
1641 base::__unlink_nodes(__n, __n);
1642 --base::__sz();
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001643#if _LIBCPP_DEBUG_LEVEL >= 2
1644 __c_node* __c = __get_db()->__find_c_and_lock(this);
1645 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1646 {
1647 --__p;
1648 iterator* __i = static_cast<iterator*>((*__p)->__i_);
1649 if (__i->__ptr_ == &__n)
1650 {
1651 (*__p)->__c_ = nullptr;
1652 if (--__c->end_ != __p)
1653 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1654 }
1655 }
1656 __get_db()->unlock();
1657#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00001658 __node_alloc_traits::destroy(__na, _VSTD::addressof(__n.__value_));
1659 __node_alloc_traits::deallocate(__na, _VSTD::addressof(__n), 1);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001660#if _LIBCPP_DEBUG_LEVEL >= 2
1661 return iterator(__r, this);
1662#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001663 return iterator(__r);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001664#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001665}
1666
1667template <class _Tp, class _Alloc>
1668typename list<_Tp, _Alloc>::iterator
1669list<_Tp, _Alloc>::erase(const_iterator __f, const_iterator __l)
1670{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001671#if _LIBCPP_DEBUG_LEVEL >= 2
1672 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__f) == this,
1673 "list::erase(iterator, iterator) called with an iterator not"
1674 " referring to this list");
1675#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001676 if (__f != __l)
1677 {
1678 __node_allocator& __na = base::__node_alloc();
1679 base::__unlink_nodes(const_cast<__node&>(*__f.__ptr_), *__l.__ptr_->__prev_);
1680 while (__f != __l)
1681 {
1682 __node& __n = const_cast<__node&>(*__f.__ptr_);
1683 ++__f;
1684 --base::__sz();
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001685#if _LIBCPP_DEBUG_LEVEL >= 2
1686 __c_node* __c = __get_db()->__find_c_and_lock(this);
1687 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1688 {
1689 --__p;
1690 iterator* __i = static_cast<iterator*>((*__p)->__i_);
1691 if (__i->__ptr_ == &__n)
1692 {
1693 (*__p)->__c_ = nullptr;
1694 if (--__c->end_ != __p)
1695 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1696 }
1697 }
1698 __get_db()->unlock();
1699#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00001700 __node_alloc_traits::destroy(__na, _VSTD::addressof(__n.__value_));
1701 __node_alloc_traits::deallocate(__na, _VSTD::addressof(__n), 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001702 }
1703 }
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001704#if _LIBCPP_DEBUG_LEVEL >= 2
1705 return iterator(const_cast<__node_pointer>(__l.__ptr_), this);
1706#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001707 return iterator(const_cast<__node_pointer>(__l.__ptr_));
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001708#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001709}
1710
1711template <class _Tp, class _Alloc>
1712void
1713list<_Tp, _Alloc>::resize(size_type __n)
1714{
1715 if (__n < base::__sz())
1716 erase(__iterator(__n), end());
1717 else if (__n > base::__sz())
1718 {
1719 __n -= base::__sz();
1720 size_type __ds = 0;
1721 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001722 typedef __allocator_destructor<__node_allocator> _Dp;
1723 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001724 __hold->__prev_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001725 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001726 ++__ds;
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001727#if _LIBCPP_DEBUG_LEVEL >= 2
1728 iterator __r = iterator(__hold.release(), this);
1729#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001730 iterator __r = iterator(__hold.release());
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001731#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001732 iterator __e = __r;
1733#ifndef _LIBCPP_NO_EXCEPTIONS
1734 try
1735 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001736#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001737 for (--__n; __n != 0; --__n, ++__e, ++__ds)
1738 {
1739 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001740 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001741 __e.__ptr_->__next_ = __hold.get();
1742 __hold->__prev_ = __e.__ptr_;
1743 __hold.release();
1744 }
1745#ifndef _LIBCPP_NO_EXCEPTIONS
1746 }
1747 catch (...)
1748 {
1749 while (true)
1750 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001751 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001752 __node_pointer __prev = __e.__ptr_->__prev_;
1753 __node_alloc_traits::deallocate(__na, __e.__ptr_, 1);
1754 if (__prev == 0)
1755 break;
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001756#if _LIBCPP_DEBUG_LEVEL >= 2
1757 __e = iterator(__prev, this);
1758#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001759 __e = iterator(__prev);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001760#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001761 }
1762 throw;
1763 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001764#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001765 __link_nodes(static_cast<__node&>(base::__end_), *__r.__ptr_, *__e.__ptr_);
1766 base::__sz() += __ds;
1767 }
1768}
1769
1770template <class _Tp, class _Alloc>
1771void
1772list<_Tp, _Alloc>::resize(size_type __n, const value_type& __x)
1773{
1774 if (__n < base::__sz())
1775 erase(__iterator(__n), end());
1776 else if (__n > base::__sz())
1777 {
1778 __n -= base::__sz();
1779 size_type __ds = 0;
1780 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001781 typedef __allocator_destructor<__node_allocator> _Dp;
1782 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001783 __hold->__prev_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001784 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001785 ++__ds;
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001786#if _LIBCPP_DEBUG_LEVEL >= 2
1787 iterator __r = iterator(__hold.release(), this);
1788#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001789 iterator __r = iterator(__hold.release());
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001790#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001791 iterator __e = __r;
1792#ifndef _LIBCPP_NO_EXCEPTIONS
1793 try
1794 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001795#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001796 for (--__n; __n != 0; --__n, ++__e, ++__ds)
1797 {
1798 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001799 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001800 __e.__ptr_->__next_ = __hold.get();
1801 __hold->__prev_ = __e.__ptr_;
1802 __hold.release();
1803 }
1804#ifndef _LIBCPP_NO_EXCEPTIONS
1805 }
1806 catch (...)
1807 {
1808 while (true)
1809 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001810 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001811 __node_pointer __prev = __e.__ptr_->__prev_;
1812 __node_alloc_traits::deallocate(__na, __e.__ptr_, 1);
1813 if (__prev == 0)
1814 break;
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001815#if _LIBCPP_DEBUG_LEVEL >= 2
1816 __e = iterator(__prev, this);
1817#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001818 __e = iterator(__prev);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001819#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001820 }
1821 throw;
1822 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001823#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001824 __link_nodes(static_cast<__node&>(base::__end_), *__r.__ptr_, *__e.__ptr_);
1825 base::__sz() += __ds;
Howard Hinnant324bb032010-08-22 00:02:43 +00001826 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001827}
1828
1829template <class _Tp, class _Alloc>
1830void
1831list<_Tp, _Alloc>::splice(const_iterator __p, list& __c)
1832{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001833 _LIBCPP_ASSERT(this != &__c,
1834 "list::splice(iterator, list) called with this == &list");
1835#if _LIBCPP_DEBUG_LEVEL >= 2
1836 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1837 "list::splice(iterator, list) called with an iterator not"
1838 " referring to this list");
1839#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001840 if (!__c.empty())
1841 {
1842 __node& __f = *__c.__end_.__next_;
1843 __node& __l = *__c.__end_.__prev_;
1844 base::__unlink_nodes(__f, __l);
1845 __link_nodes(const_cast<__node&>(*__p.__ptr_), __f, __l);
1846 base::__sz() += __c.__sz();
1847 __c.__sz() = 0;
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001848#if _LIBCPP_DEBUG_LEVEL >= 2
1849 __libcpp_db* __db = __get_db();
1850 __c_node* __cn1 = __db->__find_c_and_lock(this);
1851 __c_node* __cn2 = __db->__find_c(&__c);
1852 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
1853 {
1854 --__p;
1855 iterator* __i = static_cast<iterator*>((*__p)->__i_);
1856 if (__i->__ptr_ != static_cast<__node_pointer>(&__c.__end_))
1857 {
1858 __cn1->__add(*__p);
1859 (*__p)->__c_ = __cn1;
1860 if (--__cn2->end_ != __p)
1861 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
1862 }
1863 }
1864 __db->unlock();
1865#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001866 }
1867}
1868
1869template <class _Tp, class _Alloc>
1870void
1871list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __i)
1872{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001873#if _LIBCPP_DEBUG_LEVEL >= 2
1874 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1875 "list::splice(iterator, list, iterator) called with first iterator not"
1876 " referring to this list");
1877 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__i) == &__c,
1878 "list::splice(iterator, list, iterator) called with second iterator not"
1879 " referring to list argument");
1880 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(&__i),
1881 "list::splice(iterator, list, iterator) called with second iterator not"
1882 " derefereceable");
1883#endif
1884 if (__p.__ptr_ != __i.__ptr_ && __p.__ptr_ != __i.__ptr_->__next_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001885 {
1886 __node& __f = const_cast<__node&>(*__i.__ptr_);
1887 base::__unlink_nodes(__f, __f);
1888 __link_nodes(const_cast<__node&>(*__p.__ptr_), __f, __f);
1889 --__c.__sz();
1890 ++base::__sz();
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001891#if _LIBCPP_DEBUG_LEVEL >= 2
1892 __libcpp_db* __db = __get_db();
1893 __c_node* __cn1 = __db->__find_c_and_lock(this);
1894 __c_node* __cn2 = __db->__find_c(&__c);
1895 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
1896 {
1897 --__p;
1898 iterator* __j = static_cast<iterator*>((*__p)->__i_);
1899 if (__j->__ptr_ == &__f)
1900 {
1901 __cn1->__add(*__p);
1902 (*__p)->__c_ = __cn1;
1903 if (--__cn2->end_ != __p)
1904 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
1905 }
1906 }
1907 __db->unlock();
1908#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001909 }
1910}
1911
1912template <class _Tp, class _Alloc>
1913void
1914list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l)
1915{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001916#if _LIBCPP_DEBUG_LEVEL >= 2
1917 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1918 "list::splice(iterator, list, iterator, iterator) called with first iterator not"
1919 " referring to this list");
1920 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__f) == &__c,
1921 "list::splice(iterator, list, iterator, iterator) called with second iterator not"
1922 " referring to list argument");
1923 if (this == &__c)
1924 {
1925 for (const_iterator __i = __f; __i != __l; ++__i)
1926 _LIBCPP_ASSERT(__i != __p,
1927 "list::splice(iterator, list, iterator, iterator)"
1928 " called with the first iterator within the range"
1929 " of the second and third iterators");
1930 }
1931#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001932 if (__f != __l)
1933 {
1934 if (this != &__c)
1935 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001936 size_type __s = _VSTD::distance(__f, __l);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001937 __c.__sz() -= __s;
1938 base::__sz() += __s;
1939 }
1940 __node& __first = const_cast<__node&>(*__f.__ptr_);
1941 --__l;
1942 __node& __last = const_cast<__node&>(*__l.__ptr_);
1943 base::__unlink_nodes(__first, __last);
1944 __link_nodes(const_cast<__node&>(*__p.__ptr_), __first, __last);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001945#if _LIBCPP_DEBUG_LEVEL >= 2
1946 __libcpp_db* __db = __get_db();
1947 __c_node* __cn1 = __db->__find_c_and_lock(this);
1948 __c_node* __cn2 = __db->__find_c(&__c);
1949 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
1950 {
1951 --__p;
1952 iterator* __j = static_cast<iterator*>((*__p)->__i_);
1953 for (__node_pointer __k = const_cast<__node_pointer>(__f.__ptr_);
1954 __k != __l.__ptr_; __k = __k->__next_)
1955 {
1956 if (__j->__ptr_ == __k)
1957 {
1958 __cn1->__add(*__p);
1959 (*__p)->__c_ = __cn1;
1960 if (--__cn2->end_ != __p)
1961 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
1962 }
1963 }
1964 }
1965 __db->unlock();
1966#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001967 }
1968}
1969
1970template <class _Tp, class _Alloc>
1971void
1972list<_Tp, _Alloc>::remove(const value_type& __x)
1973{
1974 for (iterator __i = begin(), __e = end(); __i != __e;)
1975 {
1976 if (*__i == __x)
1977 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001978 iterator __j = _VSTD::next(__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001979 for (; __j != __e && *__j == __x; ++__j)
1980 ;
1981 __i = erase(__i, __j);
1982 }
1983 else
1984 ++__i;
1985 }
1986}
1987
1988template <class _Tp, class _Alloc>
1989template <class _Pred>
1990void
1991list<_Tp, _Alloc>::remove_if(_Pred __pred)
1992{
1993 for (iterator __i = begin(), __e = end(); __i != __e;)
1994 {
1995 if (__pred(*__i))
1996 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001997 iterator __j = _VSTD::next(__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001998 for (; __j != __e && __pred(*__j); ++__j)
1999 ;
2000 __i = erase(__i, __j);
2001 }
2002 else
2003 ++__i;
2004 }
2005}
2006
2007template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00002008inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002009void
2010list<_Tp, _Alloc>::unique()
2011{
2012 unique(__equal_to<value_type>());
2013}
2014
2015template <class _Tp, class _Alloc>
2016template <class _BinaryPred>
2017void
2018list<_Tp, _Alloc>::unique(_BinaryPred __binary_pred)
2019{
2020 for (iterator __i = begin(), __e = end(); __i != __e;)
2021 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002022 iterator __j = _VSTD::next(__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002023 for (; __j != __e && __binary_pred(*__i, *__j); ++__j)
2024 ;
2025 if (++__i != __j)
2026 __i = erase(__i, __j);
2027 }
2028}
2029
2030template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00002031inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002032void
2033list<_Tp, _Alloc>::merge(list& __c)
2034{
2035 merge(__c, __less<value_type>());
2036}
2037
2038template <class _Tp, class _Alloc>
2039template <class _Comp>
2040void
2041list<_Tp, _Alloc>::merge(list& __c, _Comp __comp)
2042{
2043 if (this != &__c)
2044 {
2045 iterator __f1 = begin();
2046 iterator __e1 = end();
2047 iterator __f2 = __c.begin();
2048 iterator __e2 = __c.end();
2049 while (__f1 != __e1 && __f2 != __e2)
2050 {
2051 if (__comp(*__f2, *__f1))
2052 {
2053 size_type __ds = 1;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002054 iterator __m2 = _VSTD::next(__f2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002055 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2, ++__ds)
2056 ;
2057 base::__sz() += __ds;
2058 __c.__sz() -= __ds;
2059 __node& __f = *__f2.__ptr_;
2060 __node& __l = *__m2.__ptr_->__prev_;
2061 __f2 = __m2;
2062 base::__unlink_nodes(__f, __l);
Howard Hinnant0949eed2011-06-30 21:18:19 +00002063 __m2 = _VSTD::next(__f1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002064 __link_nodes(*__f1.__ptr_, __f, __l);
2065 __f1 = __m2;
2066 }
2067 else
2068 ++__f1;
2069 }
2070 splice(__e1, __c);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00002071#if _LIBCPP_DEBUG_LEVEL >= 2
2072 __libcpp_db* __db = __get_db();
2073 __c_node* __cn1 = __db->__find_c_and_lock(this);
2074 __c_node* __cn2 = __db->__find_c(&__c);
2075 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
2076 {
2077 --__p;
2078 iterator* __i = static_cast<iterator*>((*__p)->__i_);
2079 if (__i->__ptr_ != static_cast<__node_pointer>(&__c.__end_))
2080 {
2081 __cn1->__add(*__p);
2082 (*__p)->__c_ = __cn1;
2083 if (--__cn2->end_ != __p)
2084 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
2085 }
2086 }
2087 __db->unlock();
2088#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002089 }
2090}
2091
2092template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00002093inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002094void
2095list<_Tp, _Alloc>::sort()
2096{
2097 sort(__less<value_type>());
2098}
2099
2100template <class _Tp, class _Alloc>
2101template <class _Comp>
Howard Hinnant82894812010-09-22 16:48:34 +00002102inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002103void
2104list<_Tp, _Alloc>::sort(_Comp __comp)
2105{
2106 __sort(begin(), end(), base::__sz(), __comp);
2107}
2108
2109template <class _Tp, class _Alloc>
2110template <class _Comp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002111typename list<_Tp, _Alloc>::iterator
2112list<_Tp, _Alloc>::__sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp)
2113{
2114 switch (__n)
2115 {
2116 case 0:
2117 case 1:
2118 return __f1;
2119 case 2:
2120 if (__comp(*--__e2, *__f1))
2121 {
2122 __node& __f = *__e2.__ptr_;
2123 base::__unlink_nodes(__f, __f);
2124 __link_nodes(*__f1.__ptr_, __f, __f);
2125 return __e2;
2126 }
2127 return __f1;
2128 }
2129 size_type __n2 = __n / 2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002130 iterator __e1 = _VSTD::next(__f1, __n2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002131 iterator __r = __f1 = __sort(__f1, __e1, __n2, __comp);
2132 iterator __f2 = __e1 = __sort(__e1, __e2, __n - __n2, __comp);
2133 if (__comp(*__f2, *__f1))
2134 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002135 iterator __m2 = _VSTD::next(__f2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002136 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2)
2137 ;
2138 __node& __f = *__f2.__ptr_;
2139 __node& __l = *__m2.__ptr_->__prev_;
2140 __r = __f2;
2141 __e1 = __f2 = __m2;
2142 base::__unlink_nodes(__f, __l);
Howard Hinnant0949eed2011-06-30 21:18:19 +00002143 __m2 = _VSTD::next(__f1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002144 __link_nodes(*__f1.__ptr_, __f, __l);
2145 __f1 = __m2;
2146 }
2147 else
2148 ++__f1;
2149 while (__f1 != __e1 && __f2 != __e2)
2150 {
2151 if (__comp(*__f2, *__f1))
2152 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002153 iterator __m2 = _VSTD::next(__f2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002154 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2)
2155 ;
2156 __node& __f = *__f2.__ptr_;
2157 __node& __l = *__m2.__ptr_->__prev_;
2158 if (__e1 == __f2)
2159 __e1 = __m2;
2160 __f2 = __m2;
2161 base::__unlink_nodes(__f, __l);
Howard Hinnant0949eed2011-06-30 21:18:19 +00002162 __m2 = _VSTD::next(__f1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002163 __link_nodes(*__f1.__ptr_, __f, __l);
2164 __f1 = __m2;
2165 }
2166 else
2167 ++__f1;
2168 }
2169 return __r;
2170}
2171
2172template <class _Tp, class _Alloc>
2173void
Howard Hinnantc5607272011-06-03 17:30:28 +00002174list<_Tp, _Alloc>::reverse() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002175{
2176 if (base::__sz() > 1)
2177 {
2178 iterator __e = end();
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00002179 for (iterator __i = begin(); __i.__ptr_ != __e.__ptr_;)
2180 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002181 _VSTD::swap(__i.__ptr_->__prev_, __i.__ptr_->__next_);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00002182 __i.__ptr_ = __i.__ptr_->__prev_;
2183 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002184 _VSTD::swap(__e.__ptr_->__prev_, __e.__ptr_->__next_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002185 }
2186}
2187
2188template <class _Tp, class _Alloc>
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00002189bool
2190list<_Tp, _Alloc>::__invariants() const
2191{
2192 return size() == _VSTD::distance(begin(), end());
2193}
2194
2195#if _LIBCPP_DEBUG_LEVEL >= 2
2196
2197template <class _Tp, class _Alloc>
2198bool
2199list<_Tp, _Alloc>::__dereferenceable(const const_iterator* __i) const
2200{
2201 return __i->__ptr_ != &this->__end_;
2202}
2203
2204template <class _Tp, class _Alloc>
2205bool
2206list<_Tp, _Alloc>::__decrementable(const const_iterator* __i) const
2207{
2208 return !empty() && __i->__ptr_ != base::__end_.__next_;
2209}
2210
2211template <class _Tp, class _Alloc>
2212bool
2213list<_Tp, _Alloc>::__addable(const const_iterator* __i, ptrdiff_t __n) const
2214{
2215 return false;
2216}
2217
2218template <class _Tp, class _Alloc>
2219bool
2220list<_Tp, _Alloc>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
2221{
2222 return false;
2223}
2224
2225#endif // _LIBCPP_DEBUG_LEVEL >= 2
2226
2227template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00002228inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002229bool
2230operator==(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2231{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002232 return __x.size() == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002233}
2234
2235template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00002236inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002237bool
2238operator< (const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2239{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002240 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002241}
2242
2243template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00002244inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002245bool
2246operator!=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2247{
2248 return !(__x == __y);
2249}
2250
2251template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00002252inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002253bool
2254operator> (const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2255{
2256 return __y < __x;
2257}
2258
2259template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00002260inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002261bool
2262operator>=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2263{
2264 return !(__x < __y);
2265}
2266
2267template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00002268inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002269bool
2270operator<=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2271{
2272 return !(__y < __x);
2273}
2274
2275template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00002276inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002277void
2278swap(list<_Tp, _Alloc>& __x, list<_Tp, _Alloc>& __y)
Howard Hinnantc5607272011-06-03 17:30:28 +00002279 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002280{
2281 __x.swap(__y);
2282}
2283
2284_LIBCPP_END_NAMESPACE_STD
2285
2286#endif // _LIBCPP_LIST