blob: 628a35bade7240c99b286b799db5a2b6c48a8606 [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
Howard Hinnant29f74322013-06-25 16:08:47 +0000199 typedef typename pointer_traits<_VoidPtr>::template
200#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
201 rebind<__list_node_base> __base_pointer;
202#else
203 rebind<__list_node_base>::other __base_pointer;
204#endif
205
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000206 pointer __prev_;
207 pointer __next_;
208
Howard Hinnant82894812010-09-22 16:48:34 +0000209 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000210 __list_node_base()
Howard Hinnant29f74322013-06-25 16:08:47 +0000211 : __prev_(static_cast<pointer>(pointer_traits<__base_pointer>::pointer_to(*this))),
212 __next_(static_cast<pointer>(pointer_traits<__base_pointer>::pointer_to(*this)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000213 {}
214};
215
216template <class _Tp, class _VoidPtr>
217struct __list_node
218 : public __list_node_base<_Tp, _VoidPtr>
219{
220 _Tp __value_;
221};
222
Howard Hinnant83eade62013-03-06 23:30:19 +0000223template <class _Tp, class _Alloc> class _LIBCPP_TYPE_VIS list;
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000224template <class _Tp, class _Alloc> class __list_imp;
Howard Hinnant83eade62013-03-06 23:30:19 +0000225template <class _Tp, class _VoidPtr> class _LIBCPP_TYPE_VIS __list_const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000226
227template <class _Tp, class _VoidPtr>
Howard Hinnant83eade62013-03-06 23:30:19 +0000228class _LIBCPP_TYPE_VIS __list_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000229{
230 typedef typename pointer_traits<_VoidPtr>::template
231#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
232 rebind<__list_node<_Tp, _VoidPtr> > __node_pointer;
233#else
234 rebind<__list_node<_Tp, _VoidPtr> >::other __node_pointer;
235#endif
236
237 __node_pointer __ptr_;
238
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000239#if _LIBCPP_DEBUG_LEVEL >= 2
240 _LIBCPP_INLINE_VISIBILITY
241 explicit __list_iterator(__node_pointer __p, const void* __c) _NOEXCEPT
242 : __ptr_(__p)
243 {
244 __get_db()->__insert_ic(this, __c);
245 }
246#else
Howard Hinnant82894812010-09-22 16:48:34 +0000247 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000248 explicit __list_iterator(__node_pointer __p) _NOEXCEPT : __ptr_(__p) {}
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000249#endif
250
251
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000252
253 template<class, class> friend class list;
254 template<class, class> friend class __list_imp;
255 template<class, class> friend class __list_const_iterator;
256public:
257 typedef bidirectional_iterator_tag iterator_category;
258 typedef _Tp value_type;
259 typedef value_type& reference;
260 typedef typename pointer_traits<_VoidPtr>::template
261#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
262 rebind<value_type>
263#else
264 rebind<value_type>::other
265#endif
266 pointer;
267 typedef typename pointer_traits<pointer>::difference_type difference_type;
268
Howard Hinnant82894812010-09-22 16:48:34 +0000269 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000270 __list_iterator() _NOEXCEPT
271 {
272#if _LIBCPP_DEBUG_LEVEL >= 2
273 __get_db()->__insert_i(this);
274#endif
275 }
276
277#if _LIBCPP_DEBUG_LEVEL >= 2
278
Howard Hinnant211f0ee2011-01-28 23:46:28 +0000279 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000280 __list_iterator(const __list_iterator& __p)
281 : __ptr_(__p.__ptr_)
282 {
283 __get_db()->__iterator_copy(this, &__p);
284 }
285
286 _LIBCPP_INLINE_VISIBILITY
287 ~__list_iterator()
288 {
289 __get_db()->__erase_i(this);
290 }
291
292 _LIBCPP_INLINE_VISIBILITY
293 __list_iterator& operator=(const __list_iterator& __p)
294 {
295 if (this != &__p)
296 {
297 __get_db()->__iterator_copy(this, &__p);
298 __ptr_ = __p.__ptr_;
299 }
300 return *this;
301 }
302
303#endif // _LIBCPP_DEBUG_LEVEL >= 2
304
305 _LIBCPP_INLINE_VISIBILITY
306 reference operator*() const
307 {
308#if _LIBCPP_DEBUG_LEVEL >= 2
309 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
310 "Attempted to dereference a non-dereferenceable list::iterator");
311#endif
312 return __ptr_->__value_;
313 }
Howard Hinnant82894812010-09-22 16:48:34 +0000314 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant29f74322013-06-25 16:08:47 +0000315 pointer operator->() const
316 {
317#if _LIBCPP_DEBUG_LEVEL >= 2
318 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
319 "Attempted to dereference a non-dereferenceable list::iterator");
320#endif
321 return pointer_traits<pointer>::pointer_to(__ptr_->__value_);
322 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000323
Howard Hinnant82894812010-09-22 16:48:34 +0000324 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000325 __list_iterator& operator++()
326 {
327#if _LIBCPP_DEBUG_LEVEL >= 2
328 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
329 "Attempted to increment non-incrementable list::iterator");
330#endif
331 __ptr_ = __ptr_->__next_;
332 return *this;
333 }
Howard Hinnant82894812010-09-22 16:48:34 +0000334 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000335 __list_iterator operator++(int) {__list_iterator __t(*this); ++(*this); return __t;}
336
Howard Hinnant82894812010-09-22 16:48:34 +0000337 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000338 __list_iterator& operator--()
339 {
340#if _LIBCPP_DEBUG_LEVEL >= 2
341 _LIBCPP_ASSERT(__get_const_db()->__decrementable(this),
342 "Attempted to decrement non-decrementable list::iterator");
343#endif
344 __ptr_ = __ptr_->__prev_;
345 return *this;
346 }
Howard Hinnant82894812010-09-22 16:48:34 +0000347 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000348 __list_iterator operator--(int) {__list_iterator __t(*this); --(*this); return __t;}
349
Howard Hinnant82894812010-09-22 16:48:34 +0000350 friend _LIBCPP_INLINE_VISIBILITY
351 bool operator==(const __list_iterator& __x, const __list_iterator& __y)
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000352 {
353#if _LIBCPP_DEBUG_LEVEL >= 2
354 _LIBCPP_ASSERT(__get_const_db()->__comparable(&__x, &__y),
355 "Attempted to compare non-comparable list::iterator");
356#endif
357 return __x.__ptr_ == __y.__ptr_;
358 }
Howard Hinnant82894812010-09-22 16:48:34 +0000359 friend _LIBCPP_INLINE_VISIBILITY
360 bool operator!=(const __list_iterator& __x, const __list_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000361 {return !(__x == __y);}
362};
363
364template <class _Tp, class _VoidPtr>
Howard Hinnant83eade62013-03-06 23:30:19 +0000365class _LIBCPP_TYPE_VIS __list_const_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000366{
367 typedef typename pointer_traits<_VoidPtr>::template
368#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnant29f74322013-06-25 16:08:47 +0000369 rebind<__list_node<_Tp, _VoidPtr> > __node_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000370#else
Howard Hinnant29f74322013-06-25 16:08:47 +0000371 rebind<__list_node<_Tp, _VoidPtr> >::other __node_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000372#endif
373
374 __node_pointer __ptr_;
375
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000376#if _LIBCPP_DEBUG_LEVEL >= 2
377 _LIBCPP_INLINE_VISIBILITY
378 explicit __list_const_iterator(__node_pointer __p, const void* __c) _NOEXCEPT
379 : __ptr_(__p)
380 {
381 __get_db()->__insert_ic(this, __c);
382 }
383#else
Howard Hinnant82894812010-09-22 16:48:34 +0000384 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000385 explicit __list_const_iterator(__node_pointer __p) _NOEXCEPT : __ptr_(__p) {}
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000386#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000387
388 template<class, class> friend class list;
389 template<class, class> friend class __list_imp;
390public:
391 typedef bidirectional_iterator_tag iterator_category;
392 typedef _Tp value_type;
393 typedef const value_type& reference;
394 typedef typename pointer_traits<_VoidPtr>::template
395#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
396 rebind<const value_type>
397#else
398 rebind<const value_type>::other
399#endif
400 pointer;
401 typedef typename pointer_traits<pointer>::difference_type difference_type;
402
Howard Hinnant82894812010-09-22 16:48:34 +0000403 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000404 __list_const_iterator() _NOEXCEPT
405 {
406#if _LIBCPP_DEBUG_LEVEL >= 2
407 __get_db()->__insert_i(this);
408#endif
409 }
Howard Hinnant211f0ee2011-01-28 23:46:28 +0000410 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6dcaf3e2013-04-05 17:58:52 +0000411 __list_const_iterator(const __list_iterator<_Tp, _VoidPtr>& __p) _NOEXCEPT
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000412 : __ptr_(__p.__ptr_)
413 {
414#if _LIBCPP_DEBUG_LEVEL >= 2
415 __get_db()->__iterator_copy(this, &__p);
416#endif
417 }
418
419#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000420
Howard Hinnant82894812010-09-22 16:48:34 +0000421 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000422 __list_const_iterator(const __list_const_iterator& __p)
423 : __ptr_(__p.__ptr_)
424 {
425 __get_db()->__iterator_copy(this, &__p);
426 }
427
428 _LIBCPP_INLINE_VISIBILITY
429 ~__list_const_iterator()
430 {
431 __get_db()->__erase_i(this);
432 }
433
434 _LIBCPP_INLINE_VISIBILITY
435 __list_const_iterator& operator=(const __list_const_iterator& __p)
436 {
437 if (this != &__p)
438 {
439 __get_db()->__iterator_copy(this, &__p);
440 __ptr_ = __p.__ptr_;
441 }
442 return *this;
443 }
444
445#endif // _LIBCPP_DEBUG_LEVEL >= 2
446 _LIBCPP_INLINE_VISIBILITY
447 reference operator*() const
448 {
449#if _LIBCPP_DEBUG_LEVEL >= 2
450 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
451 "Attempted to dereference a non-dereferenceable list::const_iterator");
452#endif
453 return __ptr_->__value_;
454 }
Howard Hinnant82894812010-09-22 16:48:34 +0000455 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant29f74322013-06-25 16:08:47 +0000456 pointer operator->() const
457 {
458#if _LIBCPP_DEBUG_LEVEL >= 2
459 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
460 "Attempted to dereference a non-dereferenceable list::iterator");
461#endif
462 return pointer_traits<pointer>::pointer_to(__ptr_->__value_);
463 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000464
Howard Hinnant82894812010-09-22 16:48:34 +0000465 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000466 __list_const_iterator& operator++()
467 {
468#if _LIBCPP_DEBUG_LEVEL >= 2
469 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
470 "Attempted to increment non-incrementable list::const_iterator");
471#endif
472 __ptr_ = __ptr_->__next_;
473 return *this;
474 }
Howard Hinnant82894812010-09-22 16:48:34 +0000475 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000476 __list_const_iterator operator++(int) {__list_const_iterator __t(*this); ++(*this); return __t;}
477
Howard Hinnant82894812010-09-22 16:48:34 +0000478 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000479 __list_const_iterator& operator--()
480 {
481#if _LIBCPP_DEBUG_LEVEL >= 2
482 _LIBCPP_ASSERT(__get_const_db()->__decrementable(this),
483 "Attempted to decrement non-decrementable list::const_iterator");
484#endif
485 __ptr_ = __ptr_->__prev_;
486 return *this;
487 }
Howard Hinnant82894812010-09-22 16:48:34 +0000488 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000489 __list_const_iterator operator--(int) {__list_const_iterator __t(*this); --(*this); return __t;}
490
Howard Hinnant82894812010-09-22 16:48:34 +0000491 friend _LIBCPP_INLINE_VISIBILITY
492 bool operator==(const __list_const_iterator& __x, const __list_const_iterator& __y)
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000493 {
494#if _LIBCPP_DEBUG_LEVEL >= 2
495 _LIBCPP_ASSERT(__get_const_db()->__comparable(&__x, &__y),
496 "Attempted to compare non-comparable list::const_iterator");
497#endif
498 return __x.__ptr_ == __y.__ptr_;
499 }
Howard Hinnant82894812010-09-22 16:48:34 +0000500 friend _LIBCPP_INLINE_VISIBILITY
501 bool operator!=(const __list_const_iterator& __x, const __list_const_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000502 {return !(__x == __y);}
503};
504
505template <class _Tp, class _Alloc>
506class __list_imp
507{
508 __list_imp(const __list_imp&);
509 __list_imp& operator=(const __list_imp&);
510protected:
511 typedef _Tp value_type;
512 typedef _Alloc allocator_type;
513 typedef allocator_traits<allocator_type> __alloc_traits;
514 typedef typename __alloc_traits::size_type size_type;
515 typedef typename __alloc_traits::void_pointer __void_pointer;
516 typedef __list_iterator<value_type, __void_pointer> iterator;
517 typedef __list_const_iterator<value_type, __void_pointer> const_iterator;
518 typedef __list_node_base<value_type, __void_pointer> __node_base;
519 typedef __list_node<value_type, __void_pointer> __node;
520 typedef typename __alloc_traits::template
521#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
522 rebind_alloc<__node>
523#else
524 rebind_alloc<__node>::other
525#endif
526 __node_allocator;
527 typedef allocator_traits<__node_allocator> __node_alloc_traits;
528 typedef typename __node_alloc_traits::pointer __node_pointer;
Howard Hinnant29f74322013-06-25 16:08:47 +0000529 typedef typename __node_alloc_traits::pointer __node_const_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000530 typedef typename __alloc_traits::pointer pointer;
531 typedef typename __alloc_traits::const_pointer const_pointer;
532 typedef typename __alloc_traits::difference_type difference_type;
533
Howard Hinnant29f74322013-06-25 16:08:47 +0000534 typedef typename __alloc_traits::template
535#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
536 rebind_alloc<__node_base>
537#else
538 rebind_alloc<__node_base>::other
539#endif
540 __node_base_allocator;
541 typedef typename allocator_traits<__node_base_allocator>::pointer __node_base_pointer;
542
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000543 __node_base __end_;
544 __compressed_pair<size_type, __node_allocator> __size_alloc_;
545
Howard Hinnant82894812010-09-22 16:48:34 +0000546 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000547 size_type& __sz() _NOEXCEPT {return __size_alloc_.first();}
Howard Hinnant82894812010-09-22 16:48:34 +0000548 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000549 const size_type& __sz() const _NOEXCEPT
550 {return __size_alloc_.first();}
Howard Hinnant82894812010-09-22 16:48:34 +0000551 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000552 __node_allocator& __node_alloc() _NOEXCEPT
553 {return __size_alloc_.second();}
Howard Hinnant82894812010-09-22 16:48:34 +0000554 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000555 const __node_allocator& __node_alloc() const _NOEXCEPT
556 {return __size_alloc_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000557
Howard Hinnant29f74322013-06-25 16:08:47 +0000558 static void __unlink_nodes(__node_pointer __f, __node_pointer __l) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000559
Howard Hinnantc5607272011-06-03 17:30:28 +0000560 __list_imp()
561 _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000562 __list_imp(const allocator_type& __a);
563 ~__list_imp();
Howard Hinnantc5607272011-06-03 17:30:28 +0000564 void clear() _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +0000565 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000566 bool empty() const _NOEXCEPT {return __sz() == 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000567
Howard Hinnant82894812010-09-22 16:48:34 +0000568 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000569 iterator begin() _NOEXCEPT
570 {
571#if _LIBCPP_DEBUG_LEVEL >= 2
572 return iterator(__end_.__next_, this);
573#else
574 return iterator(__end_.__next_);
575#endif
576 }
Howard Hinnant82894812010-09-22 16:48:34 +0000577 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000578 const_iterator begin() const _NOEXCEPT
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000579 {
580#if _LIBCPP_DEBUG_LEVEL >= 2
581 return const_iterator(__end_.__next_, this);
582#else
583 return const_iterator(__end_.__next_);
584#endif
585 }
Howard Hinnant82894812010-09-22 16:48:34 +0000586 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000587 iterator end() _NOEXCEPT
588 {
589#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant29f74322013-06-25 16:08:47 +0000590 return iterator(static_cast<__node_pointer>(
591 pointer_traits<__node_base_pointer>::pointer_to(__end_)), this);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000592#else
Howard Hinnant29f74322013-06-25 16:08:47 +0000593 return iterator(static_cast<__node_pointer>(
594 pointer_traits<__node_base_pointer>::pointer_to(__end_)));
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000595#endif
596 }
Howard Hinnant82894812010-09-22 16:48:34 +0000597 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000598 const_iterator end() const _NOEXCEPT
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000599 {
600#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant29f74322013-06-25 16:08:47 +0000601 return const_iterator(static_cast<__node_const_pointer>(
602 pointer_traits<__node_base_pointer>::pointer_to(const_cast<__node_base&>(__end_))), this);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000603#else
Howard Hinnant29f74322013-06-25 16:08:47 +0000604 return const_iterator(static_cast<__node_const_pointer>(
605 pointer_traits<__node_base_pointer>::pointer_to(const_cast<__node_base&>(__end_))));
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000606#endif
607 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000608
Howard Hinnantc5607272011-06-03 17:30:28 +0000609 void swap(__list_imp& __c)
610 _NOEXCEPT_(!__node_alloc_traits::propagate_on_container_swap::value ||
611 __is_nothrow_swappable<__node_allocator>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000612
Howard Hinnant82894812010-09-22 16:48:34 +0000613 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000614 void __copy_assign_alloc(const __list_imp& __c)
615 {__copy_assign_alloc(__c, integral_constant<bool,
616 __node_alloc_traits::propagate_on_container_copy_assignment::value>());}
617
Howard Hinnant82894812010-09-22 16:48:34 +0000618 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000619 void __move_assign_alloc(__list_imp& __c)
Howard Hinnantc5607272011-06-03 17:30:28 +0000620 _NOEXCEPT_(
621 !__node_alloc_traits::propagate_on_container_move_assignment::value ||
622 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000623 {__move_assign_alloc(__c, integral_constant<bool,
624 __node_alloc_traits::propagate_on_container_move_assignment::value>());}
625
626private:
Howard Hinnant82894812010-09-22 16:48:34 +0000627 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000628 static void __swap_alloc(__node_allocator& __x, __node_allocator& __y)
Howard Hinnantc5607272011-06-03 17:30:28 +0000629 _NOEXCEPT_(!__node_alloc_traits::propagate_on_container_swap::value ||
630 __is_nothrow_swappable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000631 {__swap_alloc(__x, __y, integral_constant<bool,
632 __node_alloc_traits::propagate_on_container_swap::value>());}
Howard Hinnant82894812010-09-22 16:48:34 +0000633 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000634 static void __swap_alloc(__node_allocator& __x, __node_allocator& __y, true_type)
Howard Hinnantc5607272011-06-03 17:30:28 +0000635 _NOEXCEPT_(__is_nothrow_swappable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000636 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000637 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000638 swap(__x, __y);
639 }
Howard Hinnant82894812010-09-22 16:48:34 +0000640 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000641 static void __swap_alloc(__node_allocator& __x, __node_allocator& __y, false_type)
Howard Hinnantc5607272011-06-03 17:30:28 +0000642 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000643 {}
644
Howard Hinnant82894812010-09-22 16:48:34 +0000645 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000646 void __copy_assign_alloc(const __list_imp& __c, true_type)
647 {
648 if (__node_alloc() != __c.__node_alloc())
649 clear();
650 __node_alloc() = __c.__node_alloc();
651 }
652
Howard Hinnant82894812010-09-22 16:48:34 +0000653 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000654 void __copy_assign_alloc(const __list_imp& __c, false_type)
655 {}
656
Howard Hinnant82894812010-09-22 16:48:34 +0000657 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31 +0000658 void __move_assign_alloc(__list_imp& __c, true_type)
Howard Hinnantc5607272011-06-03 17:30:28 +0000659 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000660 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000661 __node_alloc() = _VSTD::move(__c.__node_alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000662 }
663
Howard Hinnant82894812010-09-22 16:48:34 +0000664 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31 +0000665 void __move_assign_alloc(__list_imp& __c, false_type)
Howard Hinnantc5607272011-06-03 17:30:28 +0000666 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000667 {}
668};
669
670// Unlink nodes [__f, __l]
671template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +0000672inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000673void
Howard Hinnant29f74322013-06-25 16:08:47 +0000674__list_imp<_Tp, _Alloc>::__unlink_nodes(__node_pointer __f, __node_pointer __l)
Howard Hinnantc5607272011-06-03 17:30:28 +0000675 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000676{
Howard Hinnant29f74322013-06-25 16:08:47 +0000677 __f->__prev_->__next_ = __l->__next_;
678 __l->__next_->__prev_ = __f->__prev_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000679}
680
681template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +0000682inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000683__list_imp<_Tp, _Alloc>::__list_imp()
Howard Hinnantc5607272011-06-03 17:30:28 +0000684 _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000685 : __size_alloc_(0)
686{
687}
688
689template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +0000690inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000691__list_imp<_Tp, _Alloc>::__list_imp(const allocator_type& __a)
692 : __size_alloc_(0, __node_allocator(__a))
693{
694}
695
696template <class _Tp, class _Alloc>
697__list_imp<_Tp, _Alloc>::~__list_imp()
698{
699 clear();
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000700#if _LIBCPP_DEBUG_LEVEL >= 2
701 __get_db()->__erase_c(this);
702#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000703}
704
705template <class _Tp, class _Alloc>
706void
Howard Hinnantc5607272011-06-03 17:30:28 +0000707__list_imp<_Tp, _Alloc>::clear() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000708{
709 if (!empty())
710 {
711 __node_allocator& __na = __node_alloc();
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000712 __node_pointer __f = __end_.__next_;
Howard Hinnant29f74322013-06-25 16:08:47 +0000713 __node_pointer __l = static_cast<__node_pointer>(
714 pointer_traits<__node_base_pointer>::pointer_to(__end_));
715 __unlink_nodes(__f, __l->__prev_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000716 __sz() = 0;
717 while (__f != __l)
718 {
Howard Hinnant29f74322013-06-25 16:08:47 +0000719 __node_pointer __n = __f;
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000720 __f = __f->__next_;
Howard Hinnant29f74322013-06-25 16:08:47 +0000721 __node_alloc_traits::destroy(__na, _VSTD::addressof(__n->__value_));
722 __node_alloc_traits::deallocate(__na, __n, 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000723 }
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000724#if _LIBCPP_DEBUG_LEVEL >= 2
725 __c_node* __c = __get_db()->__find_c_and_lock(this);
726 for (__i_node** __p = __c->end_; __p != __c->beg_; )
727 {
728 --__p;
729 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
730 if (__i->__ptr_ != __l)
731 {
732 (*__p)->__c_ = nullptr;
733 if (--__c->end_ != __p)
734 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
735 }
736 }
737 __get_db()->unlock();
738#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000739 }
740}
741
742template <class _Tp, class _Alloc>
743void
744__list_imp<_Tp, _Alloc>::swap(__list_imp& __c)
Howard Hinnantc5607272011-06-03 17:30:28 +0000745 _NOEXCEPT_(!__node_alloc_traits::propagate_on_container_swap::value ||
746 __is_nothrow_swappable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000747{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000748 _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
749 this->__node_alloc() == __c.__node_alloc(),
750 "list::swap: Either propagate_on_container_swap must be true"
751 " or the allocators must compare equal");
Howard Hinnant0949eed2011-06-30 21:18:19 +0000752 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000753 __swap_alloc(__node_alloc(), __c.__node_alloc());
754 swap(__sz(), __c.__sz());
755 swap(__end_, __c.__end_);
756 if (__sz() == 0)
Howard Hinnant29f74322013-06-25 16:08:47 +0000757 __end_.__next_ = __end_.__prev_ = static_cast<__node_pointer>(
758 pointer_traits<__node_base_pointer>::pointer_to(__end_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000759 else
760 __end_.__prev_->__next_ = __end_.__next_->__prev_
Howard Hinnant29f74322013-06-25 16:08:47 +0000761 = static_cast<__node_pointer>(
762 pointer_traits<__node_base_pointer>::pointer_to(__end_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000763 if (__c.__sz() == 0)
764 __c.__end_.__next_ = __c.__end_.__prev_
Howard Hinnant29f74322013-06-25 16:08:47 +0000765 = static_cast<__node_pointer>(
766 pointer_traits<__node_base_pointer>::pointer_to(__c.__end_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000767 else
768 __c.__end_.__prev_->__next_ = __c.__end_.__next_->__prev_
Howard Hinnant29f74322013-06-25 16:08:47 +0000769 = static_cast<__node_pointer>(
770 pointer_traits<__node_base_pointer>::pointer_to(__c.__end_));
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000771#if _LIBCPP_DEBUG_LEVEL >= 2
772 __libcpp_db* __db = __get_db();
773 __c_node* __cn1 = __db->__find_c_and_lock(this);
774 __c_node* __cn2 = __db->__find_c(&__c);
775 std::swap(__cn1->beg_, __cn2->beg_);
776 std::swap(__cn1->end_, __cn2->end_);
777 std::swap(__cn1->cap_, __cn2->cap_);
778 for (__i_node** __p = __cn1->end_; __p != __cn1->beg_;)
779 {
780 --__p;
781 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
Howard Hinnant29f74322013-06-25 16:08:47 +0000782 if (__i->__ptr_ == static_cast<__node_pointer>(
783 pointer_traits<__node_base_pointer>::pointer_to(__c.__end_)))
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000784 {
785 __cn2->__add(*__p);
786 if (--__cn1->end_ != __p)
787 memmove(__p, __p+1, (__cn1->end_ - __p)*sizeof(__i_node*));
788 }
789 else
790 (*__p)->__c_ = __cn1;
791 }
792 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
793 {
794 --__p;
795 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
Howard Hinnant29f74322013-06-25 16:08:47 +0000796 if (__i->__ptr_ == static_cast<__node_pointer>(
797 pointer_traits<__node_base_pointer>::pointer_to(__end_)))
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000798 {
799 __cn1->__add(*__p);
800 if (--__cn2->end_ != __p)
801 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
802 }
803 else
804 (*__p)->__c_ = __cn2;
805 }
806 __db->unlock();
807#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000808}
809
810template <class _Tp, class _Alloc = allocator<_Tp> >
Howard Hinnant83eade62013-03-06 23:30:19 +0000811class _LIBCPP_TYPE_VIS list
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000812 : private __list_imp<_Tp, _Alloc>
813{
814 typedef __list_imp<_Tp, _Alloc> base;
815 typedef typename base::__node __node;
816 typedef typename base::__node_allocator __node_allocator;
817 typedef typename base::__node_pointer __node_pointer;
818 typedef typename base::__node_alloc_traits __node_alloc_traits;
Howard Hinnant29f74322013-06-25 16:08:47 +0000819 typedef typename base::__node_base __node_base;
820 typedef typename base::__node_base_pointer __node_base_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000821
822public:
823 typedef _Tp value_type;
824 typedef _Alloc allocator_type;
825 static_assert((is_same<value_type, typename allocator_type::value_type>::value),
826 "Invalid allocator::value_type");
827 typedef value_type& reference;
828 typedef const value_type& const_reference;
829 typedef typename base::pointer pointer;
830 typedef typename base::const_pointer const_pointer;
831 typedef typename base::size_type size_type;
832 typedef typename base::difference_type difference_type;
833 typedef typename base::iterator iterator;
834 typedef typename base::const_iterator const_iterator;
Howard Hinnant0949eed2011-06-30 21:18:19 +0000835 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
836 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000837
Howard Hinnant82894812010-09-22 16:48:34 +0000838 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000839 list()
840 _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value)
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000841 {
842#if _LIBCPP_DEBUG_LEVEL >= 2
843 __get_db()->__insert_c(this);
844#endif
845 }
Howard Hinnant82894812010-09-22 16:48:34 +0000846 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000847 list(const allocator_type& __a) : base(__a)
848 {
849#if _LIBCPP_DEBUG_LEVEL >= 2
850 __get_db()->__insert_c(this);
851#endif
852 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000853 list(size_type __n);
854 list(size_type __n, const value_type& __x);
855 list(size_type __n, const value_type& __x, const allocator_type& __a);
856 template <class _InpIter>
857 list(_InpIter __f, _InpIter __l,
858 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
859 template <class _InpIter>
860 list(_InpIter __f, _InpIter __l, const allocator_type& __a,
861 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
862
863 list(const list& __c);
864 list(const list& __c, const allocator_type& __a);
865 list& operator=(const list& __c);
Howard Hinnante3e32912011-08-12 21:56:02 +0000866#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000867 list(initializer_list<value_type> __il);
868 list(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnante3e32912011-08-12 21:56:02 +0000869#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant73d21a42010-09-04 23:28:19 +0000870#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc5607272011-06-03 17:30:28 +0000871 list(list&& __c)
872 _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000873 list(list&& __c, const allocator_type& __a);
Howard Hinnantc5607272011-06-03 17:30:28 +0000874 list& operator=(list&& __c)
875 _NOEXCEPT_(
876 __node_alloc_traits::propagate_on_container_move_assignment::value &&
877 is_nothrow_move_assignable<__node_allocator>::value);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000878#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnante3e32912011-08-12 21:56:02 +0000879#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant82894812010-09-22 16:48:34 +0000880 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000881 list& operator=(initializer_list<value_type> __il)
882 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnante3e32912011-08-12 21:56:02 +0000883#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000884
885 template <class _InpIter>
886 void assign(_InpIter __f, _InpIter __l,
887 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
888 void assign(size_type __n, const value_type& __x);
Howard Hinnante3e32912011-08-12 21:56:02 +0000889#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant82894812010-09-22 16:48:34 +0000890 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000891 void assign(initializer_list<value_type> __il)
892 {assign(__il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +0000893#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000894
Howard Hinnantc5607272011-06-03 17:30:28 +0000895 allocator_type get_allocator() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000896
Howard Hinnant82894812010-09-22 16:48:34 +0000897 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000898 size_type size() const _NOEXCEPT {return base::__sz();}
Howard Hinnant82894812010-09-22 16:48:34 +0000899 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000900 bool empty() const _NOEXCEPT {return base::empty();}
Howard Hinnant82894812010-09-22 16:48:34 +0000901 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000902 size_type max_size() const _NOEXCEPT
903 {return numeric_limits<difference_type>::max();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000904
Howard Hinnant82894812010-09-22 16:48:34 +0000905 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000906 iterator begin() _NOEXCEPT {return base::begin();}
Howard Hinnant82894812010-09-22 16:48:34 +0000907 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000908 const_iterator begin() const _NOEXCEPT {return base::begin();}
Howard Hinnant82894812010-09-22 16:48:34 +0000909 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000910 iterator end() _NOEXCEPT {return base::end();}
Howard Hinnant82894812010-09-22 16:48:34 +0000911 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000912 const_iterator end() const _NOEXCEPT {return base::end();}
Howard Hinnant82894812010-09-22 16:48:34 +0000913 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000914 const_iterator cbegin() const _NOEXCEPT {return base::begin();}
Howard Hinnant82894812010-09-22 16:48:34 +0000915 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000916 const_iterator cend() const _NOEXCEPT {return base::end();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000917
Howard Hinnant82894812010-09-22 16:48:34 +0000918 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000919 reverse_iterator rbegin() _NOEXCEPT
920 {return reverse_iterator(end());}
Howard Hinnant82894812010-09-22 16:48:34 +0000921 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000922 const_reverse_iterator rbegin() const _NOEXCEPT
923 {return const_reverse_iterator(end());}
Howard Hinnant82894812010-09-22 16:48:34 +0000924 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000925 reverse_iterator rend() _NOEXCEPT
926 {return reverse_iterator(begin());}
Howard Hinnant82894812010-09-22 16:48:34 +0000927 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000928 const_reverse_iterator rend() const _NOEXCEPT
929 {return const_reverse_iterator(begin());}
Howard Hinnant82894812010-09-22 16:48:34 +0000930 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000931 const_reverse_iterator crbegin() const _NOEXCEPT
932 {return const_reverse_iterator(end());}
Howard Hinnant82894812010-09-22 16:48:34 +0000933 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000934 const_reverse_iterator crend() const _NOEXCEPT
935 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000936
Howard Hinnant82894812010-09-22 16:48:34 +0000937 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000938 reference front()
939 {
940 _LIBCPP_ASSERT(!empty(), "list::front called on empty list");
941 return base::__end_.__next_->__value_;
942 }
Howard Hinnant82894812010-09-22 16:48:34 +0000943 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000944 const_reference front() const
945 {
946 _LIBCPP_ASSERT(!empty(), "list::front called on empty list");
947 return base::__end_.__next_->__value_;
948 }
Howard Hinnant82894812010-09-22 16:48:34 +0000949 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000950 reference back()
951 {
952 _LIBCPP_ASSERT(!empty(), "list::back called on empty list");
953 return base::__end_.__prev_->__value_;
954 }
Howard Hinnant82894812010-09-22 16:48:34 +0000955 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000956 const_reference back() const
957 {
958 _LIBCPP_ASSERT(!empty(), "list::back called on empty list");
959 return base::__end_.__prev_->__value_;
960 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000961
Howard Hinnant73d21a42010-09-04 23:28:19 +0000962#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000963 void push_front(value_type&& __x);
964 void push_back(value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000965#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000966 template <class... _Args>
967 void emplace_front(_Args&&... __args);
968 template <class... _Args>
969 void emplace_back(_Args&&... __args);
970 template <class... _Args>
971 iterator emplace(const_iterator __p, _Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000972#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000973 iterator insert(const_iterator __p, value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000974#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000975
976 void push_front(const value_type& __x);
977 void push_back(const value_type& __x);
978
979 iterator insert(const_iterator __p, const value_type& __x);
980 iterator insert(const_iterator __p, size_type __n, const value_type& __x);
981 template <class _InpIter>
982 iterator insert(const_iterator __p, _InpIter __f, _InpIter __l,
983 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
Howard Hinnante3e32912011-08-12 21:56:02 +0000984#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant82894812010-09-22 16:48:34 +0000985 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000986 iterator insert(const_iterator __p, initializer_list<value_type> __il)
987 {return insert(__p, __il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +0000988#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000989
Howard Hinnant82894812010-09-22 16:48:34 +0000990 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000991 void swap(list& __c)
992 _NOEXCEPT_(!__node_alloc_traits::propagate_on_container_swap::value ||
993 __is_nothrow_swappable<__node_allocator>::value)
994 {base::swap(__c);}
Howard Hinnant82894812010-09-22 16:48:34 +0000995 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000996 void clear() _NOEXCEPT {base::clear();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000997
998 void pop_front();
999 void pop_back();
1000
1001 iterator erase(const_iterator __p);
1002 iterator erase(const_iterator __f, const_iterator __l);
1003
1004 void resize(size_type __n);
1005 void resize(size_type __n, const value_type& __x);
1006
1007 void splice(const_iterator __p, list& __c);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001008#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant82894812010-09-22 16:48:34 +00001009 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001010 void splice(const_iterator __p, list&& __c) {splice(__p, __c);}
1011#endif
1012 void splice(const_iterator __p, list& __c, const_iterator __i);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001013#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant82894812010-09-22 16:48:34 +00001014 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001015 void splice(const_iterator __p, list&& __c, const_iterator __i)
1016 {splice(__p, __c, __i);}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001017#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001018 void splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001019#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant82894812010-09-22 16:48:34 +00001020 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001021 void splice(const_iterator __p, list&& __c, const_iterator __f, const_iterator __l)
1022 {splice(__p, __c, __f, __l);}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001023#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001024
1025 void remove(const value_type& __x);
1026 template <class _Pred> void remove_if(_Pred __pred);
1027 void unique();
1028 template <class _BinaryPred>
1029 void unique(_BinaryPred __binary_pred);
1030 void merge(list& __c);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001031#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant82894812010-09-22 16:48:34 +00001032 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001033 void merge(list&& __c) {merge(__c);}
1034#endif
1035 template <class _Comp>
1036 void merge(list& __c, _Comp __comp);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001037#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001038 template <class _Comp>
Howard Hinnant82894812010-09-22 16:48:34 +00001039 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001040 void merge(list&& __c, _Comp __comp) {merge(__c, __comp);}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001041#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001042 void sort();
1043 template <class _Comp>
1044 void sort(_Comp __comp);
1045
Howard Hinnantc5607272011-06-03 17:30:28 +00001046 void reverse() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001047
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001048 bool __invariants() const;
1049
1050#if _LIBCPP_DEBUG_LEVEL >= 2
1051
1052 bool __dereferenceable(const const_iterator* __i) const;
1053 bool __decrementable(const const_iterator* __i) const;
1054 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1055 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1056
1057#endif // _LIBCPP_DEBUG_LEVEL >= 2
1058
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001059private:
Howard Hinnant29f74322013-06-25 16:08:47 +00001060 static void __link_nodes(__node_pointer __p, __node_pointer __f, __node_pointer __l);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001061 iterator __iterator(size_type __n);
1062 template <class _Comp>
1063 static iterator __sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp);
1064
Howard Hinnantc5607272011-06-03 17:30:28 +00001065 void __move_assign(list& __c, true_type)
1066 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001067 void __move_assign(list& __c, false_type);
1068};
1069
1070// Link in nodes [__f, __l] just prior to __p
1071template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001072inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001073void
Howard Hinnant29f74322013-06-25 16:08:47 +00001074list<_Tp, _Alloc>::__link_nodes(__node_pointer __p, __node_pointer __f, __node_pointer __l)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001075{
Howard Hinnant29f74322013-06-25 16:08:47 +00001076 __p->__prev_->__next_ = __f;
1077 __f->__prev_ = __p->__prev_;
1078 __p->__prev_ = __l;
1079 __l->__next_ = __p;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001080}
1081
1082template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001083inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001084typename list<_Tp, _Alloc>::iterator
1085list<_Tp, _Alloc>::__iterator(size_type __n)
1086{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001087 return __n <= base::__sz() / 2 ? _VSTD::next(begin(), __n)
1088 : _VSTD::prev(end(), base::__sz() - __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001089}
1090
1091template <class _Tp, class _Alloc>
1092list<_Tp, _Alloc>::list(size_type __n)
1093{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001094#if _LIBCPP_DEBUG_LEVEL >= 2
1095 __get_db()->__insert_c(this);
1096#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001097 for (; __n > 0; --__n)
Howard Hinnant73d21a42010-09-04 23:28:19 +00001098#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001099 emplace_back();
1100#else
1101 push_back(value_type());
1102#endif
1103}
1104
1105template <class _Tp, class _Alloc>
1106list<_Tp, _Alloc>::list(size_type __n, const value_type& __x)
1107{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001108#if _LIBCPP_DEBUG_LEVEL >= 2
1109 __get_db()->__insert_c(this);
1110#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001111 for (; __n > 0; --__n)
1112 push_back(__x);
1113}
1114
1115template <class _Tp, class _Alloc>
1116list<_Tp, _Alloc>::list(size_type __n, const value_type& __x, const allocator_type& __a)
1117 : base(__a)
1118{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001119#if _LIBCPP_DEBUG_LEVEL >= 2
1120 __get_db()->__insert_c(this);
1121#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001122 for (; __n > 0; --__n)
1123 push_back(__x);
1124}
1125
1126template <class _Tp, class _Alloc>
1127template <class _InpIter>
1128list<_Tp, _Alloc>::list(_InpIter __f, _InpIter __l,
1129 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1130{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001131#if _LIBCPP_DEBUG_LEVEL >= 2
1132 __get_db()->__insert_c(this);
1133#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001134 for (; __f != __l; ++__f)
1135 push_back(*__f);
1136}
1137
1138template <class _Tp, class _Alloc>
1139template <class _InpIter>
1140list<_Tp, _Alloc>::list(_InpIter __f, _InpIter __l, const allocator_type& __a,
1141 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1142 : base(__a)
1143{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001144#if _LIBCPP_DEBUG_LEVEL >= 2
1145 __get_db()->__insert_c(this);
1146#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001147 for (; __f != __l; ++__f)
1148 push_back(*__f);
1149}
1150
1151template <class _Tp, class _Alloc>
1152list<_Tp, _Alloc>::list(const list& __c)
1153 : base(allocator_type(
1154 __node_alloc_traits::select_on_container_copy_construction(
1155 __c.__node_alloc())))
1156{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001157#if _LIBCPP_DEBUG_LEVEL >= 2
1158 __get_db()->__insert_c(this);
1159#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001160 for (const_iterator __i = __c.begin(), __e = __c.end(); __i != __e; ++__i)
1161 push_back(*__i);
1162}
1163
1164template <class _Tp, class _Alloc>
1165list<_Tp, _Alloc>::list(const list& __c, const allocator_type& __a)
1166 : base(__a)
1167{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001168#if _LIBCPP_DEBUG_LEVEL >= 2
1169 __get_db()->__insert_c(this);
1170#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001171 for (const_iterator __i = __c.begin(), __e = __c.end(); __i != __e; ++__i)
1172 push_back(*__i);
1173}
1174
Howard Hinnante3e32912011-08-12 21:56:02 +00001175#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1176
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001177template <class _Tp, class _Alloc>
1178list<_Tp, _Alloc>::list(initializer_list<value_type> __il, const allocator_type& __a)
1179 : base(__a)
1180{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001181#if _LIBCPP_DEBUG_LEVEL >= 2
1182 __get_db()->__insert_c(this);
1183#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001184 for (typename initializer_list<value_type>::const_iterator __i = __il.begin(),
1185 __e = __il.end(); __i != __e; ++__i)
1186 push_back(*__i);
1187}
1188
1189template <class _Tp, class _Alloc>
1190list<_Tp, _Alloc>::list(initializer_list<value_type> __il)
1191{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001192#if _LIBCPP_DEBUG_LEVEL >= 2
1193 __get_db()->__insert_c(this);
1194#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001195 for (typename initializer_list<value_type>::const_iterator __i = __il.begin(),
1196 __e = __il.end(); __i != __e; ++__i)
1197 push_back(*__i);
1198}
1199
Howard Hinnante3e32912011-08-12 21:56:02 +00001200#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1201
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001202template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001203inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001204list<_Tp, _Alloc>&
1205list<_Tp, _Alloc>::operator=(const list& __c)
1206{
1207 if (this != &__c)
1208 {
1209 base::__copy_assign_alloc(__c);
1210 assign(__c.begin(), __c.end());
1211 }
1212 return *this;
1213}
1214
Howard Hinnant73d21a42010-09-04 23:28:19 +00001215#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001216
1217template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001218inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001219list<_Tp, _Alloc>::list(list&& __c)
Howard Hinnantc5607272011-06-03 17:30:28 +00001220 _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001221 : base(allocator_type(_VSTD::move(__c.__node_alloc())))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001222{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001223#if _LIBCPP_DEBUG_LEVEL >= 2
1224 __get_db()->__insert_c(this);
1225#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001226 splice(end(), __c);
1227}
1228
1229template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001230inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001231list<_Tp, _Alloc>::list(list&& __c, const allocator_type& __a)
1232 : base(__a)
1233{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001234#if _LIBCPP_DEBUG_LEVEL >= 2
1235 __get_db()->__insert_c(this);
1236#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001237 if (__a == __c.get_allocator())
1238 splice(end(), __c);
1239 else
1240 {
Howard Hinnant99968442011-11-29 18:15:50 +00001241 typedef move_iterator<iterator> _Ip;
1242 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001243 }
1244}
1245
1246template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001247inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001248list<_Tp, _Alloc>&
1249list<_Tp, _Alloc>::operator=(list&& __c)
Howard Hinnantc5607272011-06-03 17:30:28 +00001250 _NOEXCEPT_(
1251 __node_alloc_traits::propagate_on_container_move_assignment::value &&
1252 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001253{
1254 __move_assign(__c, integral_constant<bool,
1255 __node_alloc_traits::propagate_on_container_move_assignment::value>());
1256 return *this;
1257}
1258
1259template <class _Tp, class _Alloc>
1260void
1261list<_Tp, _Alloc>::__move_assign(list& __c, false_type)
1262{
1263 if (base::__node_alloc() != __c.__node_alloc())
1264 {
Howard Hinnant99968442011-11-29 18:15:50 +00001265 typedef move_iterator<iterator> _Ip;
1266 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001267 }
1268 else
1269 __move_assign(__c, true_type());
1270}
1271
1272template <class _Tp, class _Alloc>
1273void
1274list<_Tp, _Alloc>::__move_assign(list& __c, true_type)
Howard Hinnantc5607272011-06-03 17:30:28 +00001275 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001276{
1277 clear();
1278 base::__move_assign_alloc(__c);
1279 splice(end(), __c);
1280}
1281
Howard Hinnant73d21a42010-09-04 23:28:19 +00001282#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001283
1284template <class _Tp, class _Alloc>
1285template <class _InpIter>
1286void
1287list<_Tp, _Alloc>::assign(_InpIter __f, _InpIter __l,
1288 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1289{
1290 iterator __i = begin();
1291 iterator __e = end();
1292 for (; __f != __l && __i != __e; ++__f, ++__i)
1293 *__i = *__f;
1294 if (__i == __e)
1295 insert(__e, __f, __l);
1296 else
1297 erase(__i, __e);
1298}
1299
1300template <class _Tp, class _Alloc>
1301void
1302list<_Tp, _Alloc>::assign(size_type __n, const value_type& __x)
1303{
1304 iterator __i = begin();
1305 iterator __e = end();
1306 for (; __n > 0 && __i != __e; --__n, ++__i)
1307 *__i = __x;
1308 if (__i == __e)
1309 insert(__e, __n, __x);
1310 else
1311 erase(__i, __e);
1312}
1313
1314template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001315inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001316_Alloc
Howard Hinnantc5607272011-06-03 17:30:28 +00001317list<_Tp, _Alloc>::get_allocator() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001318{
1319 return allocator_type(base::__node_alloc());
1320}
1321
1322template <class _Tp, class _Alloc>
1323typename list<_Tp, _Alloc>::iterator
1324list<_Tp, _Alloc>::insert(const_iterator __p, const value_type& __x)
1325{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001326#if _LIBCPP_DEBUG_LEVEL >= 2
1327 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1328 "list::insert(iterator, x) called with an iterator not"
1329 " referring to this list");
1330#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001331 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001332 typedef __allocator_destructor<__node_allocator> _Dp;
1333 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001334 __hold->__prev_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001335 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnant29f74322013-06-25 16:08:47 +00001336 __link_nodes(__p.__ptr_, __hold.get(), __hold.get());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001337 ++base::__sz();
Howard Hinnant79a35572013-04-05 00:18:49 +00001338#if _LIBCPP_DEBUG_LEVEL >= 2
1339 return iterator(__hold.release(), this);
1340#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001341 return iterator(__hold.release());
Howard Hinnant79a35572013-04-05 00:18:49 +00001342#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001343}
1344
1345template <class _Tp, class _Alloc>
1346typename list<_Tp, _Alloc>::iterator
1347list<_Tp, _Alloc>::insert(const_iterator __p, size_type __n, const value_type& __x)
1348{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001349#if _LIBCPP_DEBUG_LEVEL >= 2
1350 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1351 "list::insert(iterator, n, x) called with an iterator not"
1352 " referring to this list");
Howard Hinnant29f74322013-06-25 16:08:47 +00001353 iterator __r(__p.__ptr_, this);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001354#else
Howard Hinnant29f74322013-06-25 16:08:47 +00001355 iterator __r(__p.__ptr_);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001356#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001357 if (__n > 0)
1358 {
1359 size_type __ds = 0;
1360 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001361 typedef __allocator_destructor<__node_allocator> _Dp;
1362 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001363 __hold->__prev_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001364 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001365 ++__ds;
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001366#if _LIBCPP_DEBUG_LEVEL >= 2
1367 __r = iterator(__hold.get(), this);
1368#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001369 __r = iterator(__hold.get());
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001370#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001371 __hold.release();
1372 iterator __e = __r;
1373#ifndef _LIBCPP_NO_EXCEPTIONS
1374 try
1375 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001376#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001377 for (--__n; __n != 0; --__n, ++__e, ++__ds)
1378 {
1379 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001380 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001381 __e.__ptr_->__next_ = __hold.get();
1382 __hold->__prev_ = __e.__ptr_;
1383 __hold.release();
1384 }
1385#ifndef _LIBCPP_NO_EXCEPTIONS
1386 }
1387 catch (...)
1388 {
1389 while (true)
1390 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001391 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001392 __node_pointer __prev = __e.__ptr_->__prev_;
1393 __node_alloc_traits::deallocate(__na, __e.__ptr_, 1);
1394 if (__prev == 0)
1395 break;
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001396#if _LIBCPP_DEBUG_LEVEL >= 2
1397 __e = iterator(__prev, this);
1398#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001399 __e = iterator(__prev);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001400#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001401 }
1402 throw;
1403 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001404#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant29f74322013-06-25 16:08:47 +00001405 __link_nodes(__p.__ptr_, __r.__ptr_, __e.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001406 base::__sz() += __ds;
1407 }
1408 return __r;
1409}
1410
1411template <class _Tp, class _Alloc>
1412template <class _InpIter>
1413typename list<_Tp, _Alloc>::iterator
1414list<_Tp, _Alloc>::insert(const_iterator __p, _InpIter __f, _InpIter __l,
1415 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1416{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001417#if _LIBCPP_DEBUG_LEVEL >= 2
1418 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1419 "list::insert(iterator, range) called with an iterator not"
1420 " referring to this list");
Howard Hinnant29f74322013-06-25 16:08:47 +00001421 iterator __r(__p.__ptr_, this);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001422#else
Howard Hinnant29f74322013-06-25 16:08:47 +00001423 iterator __r(__p.__ptr_);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001424#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001425 if (__f != __l)
1426 {
1427 size_type __ds = 0;
1428 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001429 typedef __allocator_destructor<__node_allocator> _Dp;
1430 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001431 __hold->__prev_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001432 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), *__f);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001433 ++__ds;
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001434#if _LIBCPP_DEBUG_LEVEL >= 2
1435 __r = iterator(__hold.get(), this);
1436#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001437 __r = iterator(__hold.get());
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001438#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001439 __hold.release();
1440 iterator __e = __r;
1441#ifndef _LIBCPP_NO_EXCEPTIONS
1442 try
1443 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001444#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001445 for (++__f; __f != __l; ++__f, ++__e, ++__ds)
1446 {
1447 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001448 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), *__f);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001449 __e.__ptr_->__next_ = __hold.get();
1450 __hold->__prev_ = __e.__ptr_;
1451 __hold.release();
1452 }
1453#ifndef _LIBCPP_NO_EXCEPTIONS
1454 }
1455 catch (...)
1456 {
1457 while (true)
1458 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001459 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001460 __node_pointer __prev = __e.__ptr_->__prev_;
1461 __node_alloc_traits::deallocate(__na, __e.__ptr_, 1);
1462 if (__prev == 0)
1463 break;
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001464#if _LIBCPP_DEBUG_LEVEL >= 2
1465 __e = iterator(__prev, this);
1466#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001467 __e = iterator(__prev);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001468#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001469 }
1470 throw;
1471 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001472#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant29f74322013-06-25 16:08:47 +00001473 __link_nodes(__p.__ptr_, __r.__ptr_, __e.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001474 base::__sz() += __ds;
1475 }
1476 return __r;
1477}
1478
1479template <class _Tp, class _Alloc>
1480void
1481list<_Tp, _Alloc>::push_front(const value_type& __x)
1482{
1483 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001484 typedef __allocator_destructor<__node_allocator> _Dp;
1485 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001486 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnant29f74322013-06-25 16:08:47 +00001487 __link_nodes(base::__end_.__next_, __hold.get(), __hold.get());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001488 ++base::__sz();
1489 __hold.release();
1490}
1491
1492template <class _Tp, class _Alloc>
1493void
1494list<_Tp, _Alloc>::push_back(const value_type& __x)
1495{
1496 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001497 typedef __allocator_destructor<__node_allocator> _Dp;
1498 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001499 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnant29f74322013-06-25 16:08:47 +00001500 __link_nodes(static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::
1501 pointer_to(base::__end_)), __hold.get(), __hold.get());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001502 ++base::__sz();
1503 __hold.release();
1504}
1505
Howard Hinnant73d21a42010-09-04 23:28:19 +00001506#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001507
1508template <class _Tp, class _Alloc>
1509void
1510list<_Tp, _Alloc>::push_front(value_type&& __x)
1511{
1512 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001513 typedef __allocator_destructor<__node_allocator> _Dp;
1514 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001515 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
Howard Hinnant29f74322013-06-25 16:08:47 +00001516 __link_nodes(base::__end_.__next_, __hold.get(), __hold.get());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001517 ++base::__sz();
1518 __hold.release();
1519}
1520
1521template <class _Tp, class _Alloc>
1522void
1523list<_Tp, _Alloc>::push_back(value_type&& __x)
1524{
1525 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001526 typedef __allocator_destructor<__node_allocator> _Dp;
1527 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001528 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
Howard Hinnant29f74322013-06-25 16:08:47 +00001529 __link_nodes(static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::
1530 pointer_to(base::__end_)), __hold.get(), __hold.get());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001531 ++base::__sz();
1532 __hold.release();
1533}
1534
Howard Hinnant73d21a42010-09-04 23:28:19 +00001535#ifndef _LIBCPP_HAS_NO_VARIADICS
1536
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001537template <class _Tp, class _Alloc>
1538template <class... _Args>
1539void
1540list<_Tp, _Alloc>::emplace_front(_Args&&... __args)
1541{
1542 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001543 typedef __allocator_destructor<__node_allocator> _Dp;
1544 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001545 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
Howard Hinnant29f74322013-06-25 16:08:47 +00001546 __link_nodes(base::__end_.__next_, __hold.get(), __hold.get());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001547 ++base::__sz();
1548 __hold.release();
1549}
1550
1551template <class _Tp, class _Alloc>
1552template <class... _Args>
1553void
1554list<_Tp, _Alloc>::emplace_back(_Args&&... __args)
1555{
1556 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001557 typedef __allocator_destructor<__node_allocator> _Dp;
1558 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001559 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
Howard Hinnant29f74322013-06-25 16:08:47 +00001560 __link_nodes(static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::
1561 pointer_to(base::__end_)), __hold.get(), __hold.get());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001562 ++base::__sz();
1563 __hold.release();
1564}
1565
1566template <class _Tp, class _Alloc>
1567template <class... _Args>
1568typename list<_Tp, _Alloc>::iterator
1569list<_Tp, _Alloc>::emplace(const_iterator __p, _Args&&... __args)
1570{
Howard Hinnant79a35572013-04-05 00:18:49 +00001571#if _LIBCPP_DEBUG_LEVEL >= 2
1572 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1573 "list::emplace(iterator, args...) called with an iterator not"
1574 " referring to this list");
1575#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001576 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001577 typedef __allocator_destructor<__node_allocator> _Dp;
1578 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001579 __hold->__prev_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001580 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
Howard Hinnant29f74322013-06-25 16:08:47 +00001581 __link_nodes(__p.__ptr_, __hold.get(), __hold.get());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001582 ++base::__sz();
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001583#if _LIBCPP_DEBUG_LEVEL >= 2
1584 return iterator(__hold.release(), this);
1585#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001586 return iterator(__hold.release());
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001587#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001588}
1589
Howard Hinnant73d21a42010-09-04 23:28:19 +00001590#endif // _LIBCPP_HAS_NO_VARIADICS
1591
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001592template <class _Tp, class _Alloc>
1593typename list<_Tp, _Alloc>::iterator
1594list<_Tp, _Alloc>::insert(const_iterator __p, value_type&& __x)
1595{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001596#if _LIBCPP_DEBUG_LEVEL >= 2
1597 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1598 "list::insert(iterator, x) called with an iterator not"
1599 " referring to this list");
1600#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001601 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001602 typedef __allocator_destructor<__node_allocator> _Dp;
1603 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001604 __hold->__prev_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001605 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
Howard Hinnant29f74322013-06-25 16:08:47 +00001606 __link_nodes(__p.__ptr_, __hold.get(), __hold.get());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001607 ++base::__sz();
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001608#if _LIBCPP_DEBUG_LEVEL >= 2
1609 return iterator(__hold.release(), this);
1610#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001611 return iterator(__hold.release());
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001612#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001613}
1614
Howard Hinnant73d21a42010-09-04 23:28:19 +00001615#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001616
1617template <class _Tp, class _Alloc>
1618void
1619list<_Tp, _Alloc>::pop_front()
1620{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001621 _LIBCPP_ASSERT(!empty(), "list::pop_front() called with empty list");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001622 __node_allocator& __na = base::__node_alloc();
Howard Hinnant29f74322013-06-25 16:08:47 +00001623 __node_pointer __n = base::__end_.__next_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001624 base::__unlink_nodes(__n, __n);
1625 --base::__sz();
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001626#if _LIBCPP_DEBUG_LEVEL >= 2
1627 __c_node* __c = __get_db()->__find_c_and_lock(this);
1628 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1629 {
1630 --__p;
1631 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant29f74322013-06-25 16:08:47 +00001632 if (__i->__ptr_ == __n)
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001633 {
1634 (*__p)->__c_ = nullptr;
1635 if (--__c->end_ != __p)
1636 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1637 }
1638 }
1639 __get_db()->unlock();
1640#endif
Howard Hinnant29f74322013-06-25 16:08:47 +00001641 __node_alloc_traits::destroy(__na, _VSTD::addressof(__n->__value_));
1642 __node_alloc_traits::deallocate(__na, __n, 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001643}
1644
1645template <class _Tp, class _Alloc>
1646void
1647list<_Tp, _Alloc>::pop_back()
1648{
Dmitri Gribenkoc7a39cf2013-06-24 06:15:57 +00001649 _LIBCPP_ASSERT(!empty(), "list::pop_back() called with empty list");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001650 __node_allocator& __na = base::__node_alloc();
Howard Hinnant29f74322013-06-25 16:08:47 +00001651 __node_pointer __n = base::__end_.__prev_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001652 base::__unlink_nodes(__n, __n);
1653 --base::__sz();
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001654#if _LIBCPP_DEBUG_LEVEL >= 2
1655 __c_node* __c = __get_db()->__find_c_and_lock(this);
1656 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1657 {
1658 --__p;
1659 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant29f74322013-06-25 16:08:47 +00001660 if (__i->__ptr_ == __n)
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001661 {
1662 (*__p)->__c_ = nullptr;
1663 if (--__c->end_ != __p)
1664 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1665 }
1666 }
1667 __get_db()->unlock();
1668#endif
Howard Hinnant29f74322013-06-25 16:08:47 +00001669 __node_alloc_traits::destroy(__na, _VSTD::addressof(__n->__value_));
1670 __node_alloc_traits::deallocate(__na, __n, 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001671}
1672
1673template <class _Tp, class _Alloc>
1674typename list<_Tp, _Alloc>::iterator
1675list<_Tp, _Alloc>::erase(const_iterator __p)
1676{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001677#if _LIBCPP_DEBUG_LEVEL >= 2
1678 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1679 "list::erase(iterator) called with an iterator not"
1680 " referring to this list");
1681#endif
Howard Hinnant79a35572013-04-05 00:18:49 +00001682 _LIBCPP_ASSERT(__p != end(),
1683 "list::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001684 __node_allocator& __na = base::__node_alloc();
Howard Hinnant29f74322013-06-25 16:08:47 +00001685 __node_pointer __n = __p.__ptr_;
1686 __node_pointer __r = __n->__next_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001687 base::__unlink_nodes(__n, __n);
1688 --base::__sz();
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001689#if _LIBCPP_DEBUG_LEVEL >= 2
1690 __c_node* __c = __get_db()->__find_c_and_lock(this);
1691 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1692 {
1693 --__p;
1694 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant29f74322013-06-25 16:08:47 +00001695 if (__i->__ptr_ == __n)
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001696 {
1697 (*__p)->__c_ = nullptr;
1698 if (--__c->end_ != __p)
1699 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1700 }
1701 }
1702 __get_db()->unlock();
1703#endif
Howard Hinnant29f74322013-06-25 16:08:47 +00001704 __node_alloc_traits::destroy(__na, _VSTD::addressof(__n->__value_));
1705 __node_alloc_traits::deallocate(__na, __n, 1);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001706#if _LIBCPP_DEBUG_LEVEL >= 2
1707 return iterator(__r, this);
1708#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001709 return iterator(__r);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001710#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001711}
1712
1713template <class _Tp, class _Alloc>
1714typename list<_Tp, _Alloc>::iterator
1715list<_Tp, _Alloc>::erase(const_iterator __f, const_iterator __l)
1716{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001717#if _LIBCPP_DEBUG_LEVEL >= 2
1718 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__f) == this,
1719 "list::erase(iterator, iterator) called with an iterator not"
1720 " referring to this list");
1721#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001722 if (__f != __l)
1723 {
1724 __node_allocator& __na = base::__node_alloc();
Howard Hinnant29f74322013-06-25 16:08:47 +00001725 base::__unlink_nodes(__f.__ptr_, __l.__ptr_->__prev_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001726 while (__f != __l)
1727 {
Howard Hinnant29f74322013-06-25 16:08:47 +00001728 __node_pointer __n = __f.__ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001729 ++__f;
1730 --base::__sz();
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001731#if _LIBCPP_DEBUG_LEVEL >= 2
1732 __c_node* __c = __get_db()->__find_c_and_lock(this);
1733 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1734 {
1735 --__p;
1736 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant29f74322013-06-25 16:08:47 +00001737 if (__i->__ptr_ == __n)
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001738 {
1739 (*__p)->__c_ = nullptr;
1740 if (--__c->end_ != __p)
1741 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1742 }
1743 }
1744 __get_db()->unlock();
1745#endif
Howard Hinnant29f74322013-06-25 16:08:47 +00001746 __node_alloc_traits::destroy(__na, _VSTD::addressof(__n->__value_));
1747 __node_alloc_traits::deallocate(__na, __n, 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001748 }
1749 }
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001750#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant29f74322013-06-25 16:08:47 +00001751 return iterator(__l.__ptr_, this);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001752#else
Howard Hinnant29f74322013-06-25 16:08:47 +00001753 return iterator(__l.__ptr_);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001754#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001755}
1756
1757template <class _Tp, class _Alloc>
1758void
1759list<_Tp, _Alloc>::resize(size_type __n)
1760{
1761 if (__n < base::__sz())
1762 erase(__iterator(__n), end());
1763 else if (__n > base::__sz())
1764 {
1765 __n -= base::__sz();
1766 size_type __ds = 0;
1767 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001768 typedef __allocator_destructor<__node_allocator> _Dp;
1769 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001770 __hold->__prev_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001771 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001772 ++__ds;
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001773#if _LIBCPP_DEBUG_LEVEL >= 2
1774 iterator __r = iterator(__hold.release(), this);
1775#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001776 iterator __r = iterator(__hold.release());
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001777#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001778 iterator __e = __r;
1779#ifndef _LIBCPP_NO_EXCEPTIONS
1780 try
1781 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001782#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001783 for (--__n; __n != 0; --__n, ++__e, ++__ds)
1784 {
1785 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001786 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001787 __e.__ptr_->__next_ = __hold.get();
1788 __hold->__prev_ = __e.__ptr_;
1789 __hold.release();
1790 }
1791#ifndef _LIBCPP_NO_EXCEPTIONS
1792 }
1793 catch (...)
1794 {
1795 while (true)
1796 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001797 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001798 __node_pointer __prev = __e.__ptr_->__prev_;
1799 __node_alloc_traits::deallocate(__na, __e.__ptr_, 1);
1800 if (__prev == 0)
1801 break;
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001802#if _LIBCPP_DEBUG_LEVEL >= 2
1803 __e = iterator(__prev, this);
1804#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001805 __e = iterator(__prev);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001806#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001807 }
1808 throw;
1809 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001810#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant29f74322013-06-25 16:08:47 +00001811 __link_nodes(static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::
1812 pointer_to(base::__end_)), __r.__ptr_, __e.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001813 base::__sz() += __ds;
1814 }
1815}
1816
1817template <class _Tp, class _Alloc>
1818void
1819list<_Tp, _Alloc>::resize(size_type __n, const value_type& __x)
1820{
1821 if (__n < base::__sz())
1822 erase(__iterator(__n), end());
1823 else if (__n > base::__sz())
1824 {
1825 __n -= base::__sz();
1826 size_type __ds = 0;
1827 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001828 typedef __allocator_destructor<__node_allocator> _Dp;
1829 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001830 __hold->__prev_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001831 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001832 ++__ds;
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001833#if _LIBCPP_DEBUG_LEVEL >= 2
1834 iterator __r = iterator(__hold.release(), this);
1835#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001836 iterator __r = iterator(__hold.release());
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001837#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001838 iterator __e = __r;
1839#ifndef _LIBCPP_NO_EXCEPTIONS
1840 try
1841 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001842#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001843 for (--__n; __n != 0; --__n, ++__e, ++__ds)
1844 {
1845 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001846 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001847 __e.__ptr_->__next_ = __hold.get();
1848 __hold->__prev_ = __e.__ptr_;
1849 __hold.release();
1850 }
1851#ifndef _LIBCPP_NO_EXCEPTIONS
1852 }
1853 catch (...)
1854 {
1855 while (true)
1856 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001857 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001858 __node_pointer __prev = __e.__ptr_->__prev_;
1859 __node_alloc_traits::deallocate(__na, __e.__ptr_, 1);
1860 if (__prev == 0)
1861 break;
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001862#if _LIBCPP_DEBUG_LEVEL >= 2
1863 __e = iterator(__prev, this);
1864#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001865 __e = iterator(__prev);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001866#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001867 }
1868 throw;
1869 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001870#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant29f74322013-06-25 16:08:47 +00001871 __link_nodes(static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::
1872 pointer_to(base::__end_)), __r.__ptr_, __e.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001873 base::__sz() += __ds;
Howard Hinnant324bb032010-08-22 00:02:43 +00001874 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001875}
1876
1877template <class _Tp, class _Alloc>
1878void
1879list<_Tp, _Alloc>::splice(const_iterator __p, list& __c)
1880{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001881 _LIBCPP_ASSERT(this != &__c,
1882 "list::splice(iterator, list) called with this == &list");
1883#if _LIBCPP_DEBUG_LEVEL >= 2
1884 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1885 "list::splice(iterator, list) called with an iterator not"
1886 " referring to this list");
1887#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001888 if (!__c.empty())
1889 {
Howard Hinnant29f74322013-06-25 16:08:47 +00001890 __node_pointer __f = __c.__end_.__next_;
1891 __node_pointer __l = __c.__end_.__prev_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001892 base::__unlink_nodes(__f, __l);
Howard Hinnant29f74322013-06-25 16:08:47 +00001893 __link_nodes(__p.__ptr_, __f, __l);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001894 base::__sz() += __c.__sz();
1895 __c.__sz() = 0;
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001896#if _LIBCPP_DEBUG_LEVEL >= 2
1897 __libcpp_db* __db = __get_db();
1898 __c_node* __cn1 = __db->__find_c_and_lock(this);
1899 __c_node* __cn2 = __db->__find_c(&__c);
1900 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
1901 {
1902 --__p;
1903 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant29f74322013-06-25 16:08:47 +00001904 if (__i->__ptr_ != static_cast<__node_pointer>(
1905 pointer_traits<__node_base_pointer>::pointer_to(__c.__end_)))
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001906 {
1907 __cn1->__add(*__p);
1908 (*__p)->__c_ = __cn1;
1909 if (--__cn2->end_ != __p)
1910 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
1911 }
1912 }
1913 __db->unlock();
1914#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001915 }
1916}
1917
1918template <class _Tp, class _Alloc>
1919void
1920list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __i)
1921{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001922#if _LIBCPP_DEBUG_LEVEL >= 2
1923 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1924 "list::splice(iterator, list, iterator) called with first iterator not"
1925 " referring to this list");
1926 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__i) == &__c,
1927 "list::splice(iterator, list, iterator) called with second iterator not"
1928 " referring to list argument");
1929 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(&__i),
1930 "list::splice(iterator, list, iterator) called with second iterator not"
1931 " derefereceable");
1932#endif
1933 if (__p.__ptr_ != __i.__ptr_ && __p.__ptr_ != __i.__ptr_->__next_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001934 {
Howard Hinnant29f74322013-06-25 16:08:47 +00001935 __node_pointer __f = __i.__ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001936 base::__unlink_nodes(__f, __f);
Howard Hinnant29f74322013-06-25 16:08:47 +00001937 __link_nodes(__p.__ptr_, __f, __f);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001938 --__c.__sz();
1939 ++base::__sz();
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001940#if _LIBCPP_DEBUG_LEVEL >= 2
1941 __libcpp_db* __db = __get_db();
1942 __c_node* __cn1 = __db->__find_c_and_lock(this);
1943 __c_node* __cn2 = __db->__find_c(&__c);
1944 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
1945 {
1946 --__p;
1947 iterator* __j = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant29f74322013-06-25 16:08:47 +00001948 if (__j->__ptr_ == __f)
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001949 {
1950 __cn1->__add(*__p);
1951 (*__p)->__c_ = __cn1;
1952 if (--__cn2->end_ != __p)
1953 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
1954 }
1955 }
1956 __db->unlock();
1957#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001958 }
1959}
1960
1961template <class _Tp, class _Alloc>
1962void
1963list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l)
1964{
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001965#if _LIBCPP_DEBUG_LEVEL >= 2
1966 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1967 "list::splice(iterator, list, iterator, iterator) called with first iterator not"
1968 " referring to this list");
1969 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__f) == &__c,
1970 "list::splice(iterator, list, iterator, iterator) called with second iterator not"
1971 " referring to list argument");
1972 if (this == &__c)
1973 {
1974 for (const_iterator __i = __f; __i != __l; ++__i)
1975 _LIBCPP_ASSERT(__i != __p,
1976 "list::splice(iterator, list, iterator, iterator)"
1977 " called with the first iterator within the range"
1978 " of the second and third iterators");
1979 }
1980#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001981 if (__f != __l)
1982 {
1983 if (this != &__c)
1984 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001985 size_type __s = _VSTD::distance(__f, __l);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001986 __c.__sz() -= __s;
1987 base::__sz() += __s;
1988 }
Howard Hinnant29f74322013-06-25 16:08:47 +00001989 __node_pointer __first = __f.__ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001990 --__l;
Howard Hinnant29f74322013-06-25 16:08:47 +00001991 __node_pointer __last = __l.__ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001992 base::__unlink_nodes(__first, __last);
Howard Hinnant29f74322013-06-25 16:08:47 +00001993 __link_nodes(__p.__ptr_, __first, __last);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00001994#if _LIBCPP_DEBUG_LEVEL >= 2
1995 __libcpp_db* __db = __get_db();
1996 __c_node* __cn1 = __db->__find_c_and_lock(this);
1997 __c_node* __cn2 = __db->__find_c(&__c);
1998 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
1999 {
2000 --__p;
2001 iterator* __j = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant29f74322013-06-25 16:08:47 +00002002 for (__node_pointer __k = __f.__ptr_;
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00002003 __k != __l.__ptr_; __k = __k->__next_)
2004 {
2005 if (__j->__ptr_ == __k)
2006 {
2007 __cn1->__add(*__p);
2008 (*__p)->__c_ = __cn1;
2009 if (--__cn2->end_ != __p)
2010 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
2011 }
2012 }
2013 }
2014 __db->unlock();
2015#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002016 }
2017}
2018
2019template <class _Tp, class _Alloc>
2020void
2021list<_Tp, _Alloc>::remove(const value_type& __x)
2022{
2023 for (iterator __i = begin(), __e = end(); __i != __e;)
2024 {
2025 if (*__i == __x)
2026 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002027 iterator __j = _VSTD::next(__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002028 for (; __j != __e && *__j == __x; ++__j)
2029 ;
2030 __i = erase(__i, __j);
2031 }
2032 else
2033 ++__i;
2034 }
2035}
2036
2037template <class _Tp, class _Alloc>
2038template <class _Pred>
2039void
2040list<_Tp, _Alloc>::remove_if(_Pred __pred)
2041{
2042 for (iterator __i = begin(), __e = end(); __i != __e;)
2043 {
2044 if (__pred(*__i))
2045 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002046 iterator __j = _VSTD::next(__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002047 for (; __j != __e && __pred(*__j); ++__j)
2048 ;
2049 __i = erase(__i, __j);
2050 }
2051 else
2052 ++__i;
2053 }
2054}
2055
2056template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00002057inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002058void
2059list<_Tp, _Alloc>::unique()
2060{
2061 unique(__equal_to<value_type>());
2062}
2063
2064template <class _Tp, class _Alloc>
2065template <class _BinaryPred>
2066void
2067list<_Tp, _Alloc>::unique(_BinaryPred __binary_pred)
2068{
2069 for (iterator __i = begin(), __e = end(); __i != __e;)
2070 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002071 iterator __j = _VSTD::next(__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002072 for (; __j != __e && __binary_pred(*__i, *__j); ++__j)
2073 ;
2074 if (++__i != __j)
2075 __i = erase(__i, __j);
2076 }
2077}
2078
2079template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00002080inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002081void
2082list<_Tp, _Alloc>::merge(list& __c)
2083{
2084 merge(__c, __less<value_type>());
2085}
2086
2087template <class _Tp, class _Alloc>
2088template <class _Comp>
2089void
2090list<_Tp, _Alloc>::merge(list& __c, _Comp __comp)
2091{
2092 if (this != &__c)
2093 {
2094 iterator __f1 = begin();
2095 iterator __e1 = end();
2096 iterator __f2 = __c.begin();
2097 iterator __e2 = __c.end();
2098 while (__f1 != __e1 && __f2 != __e2)
2099 {
2100 if (__comp(*__f2, *__f1))
2101 {
2102 size_type __ds = 1;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002103 iterator __m2 = _VSTD::next(__f2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002104 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2, ++__ds)
2105 ;
2106 base::__sz() += __ds;
2107 __c.__sz() -= __ds;
Howard Hinnant29f74322013-06-25 16:08:47 +00002108 __node_pointer __f = __f2.__ptr_;
2109 __node_pointer __l = __m2.__ptr_->__prev_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002110 __f2 = __m2;
2111 base::__unlink_nodes(__f, __l);
Howard Hinnant0949eed2011-06-30 21:18:19 +00002112 __m2 = _VSTD::next(__f1);
Howard Hinnant29f74322013-06-25 16:08:47 +00002113 __link_nodes(__f1.__ptr_, __f, __l);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002114 __f1 = __m2;
2115 }
2116 else
2117 ++__f1;
2118 }
2119 splice(__e1, __c);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00002120#if _LIBCPP_DEBUG_LEVEL >= 2
2121 __libcpp_db* __db = __get_db();
2122 __c_node* __cn1 = __db->__find_c_and_lock(this);
2123 __c_node* __cn2 = __db->__find_c(&__c);
2124 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
2125 {
2126 --__p;
2127 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant29f74322013-06-25 16:08:47 +00002128 if (__i->__ptr_ != static_cast<__node_pointer>(
2129 pointer_traits<__node_base_pointer>::pointer_to(__c.__end_)))
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00002130 {
2131 __cn1->__add(*__p);
2132 (*__p)->__c_ = __cn1;
2133 if (--__cn2->end_ != __p)
2134 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
2135 }
2136 }
2137 __db->unlock();
2138#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002139 }
2140}
2141
2142template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00002143inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002144void
2145list<_Tp, _Alloc>::sort()
2146{
2147 sort(__less<value_type>());
2148}
2149
2150template <class _Tp, class _Alloc>
2151template <class _Comp>
Howard Hinnant82894812010-09-22 16:48:34 +00002152inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002153void
2154list<_Tp, _Alloc>::sort(_Comp __comp)
2155{
2156 __sort(begin(), end(), base::__sz(), __comp);
2157}
2158
2159template <class _Tp, class _Alloc>
2160template <class _Comp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002161typename list<_Tp, _Alloc>::iterator
2162list<_Tp, _Alloc>::__sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp)
2163{
2164 switch (__n)
2165 {
2166 case 0:
2167 case 1:
2168 return __f1;
2169 case 2:
2170 if (__comp(*--__e2, *__f1))
2171 {
Howard Hinnant29f74322013-06-25 16:08:47 +00002172 __node_pointer __f = __e2.__ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002173 base::__unlink_nodes(__f, __f);
Howard Hinnant29f74322013-06-25 16:08:47 +00002174 __link_nodes(__f1.__ptr_, __f, __f);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002175 return __e2;
2176 }
2177 return __f1;
2178 }
2179 size_type __n2 = __n / 2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002180 iterator __e1 = _VSTD::next(__f1, __n2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002181 iterator __r = __f1 = __sort(__f1, __e1, __n2, __comp);
2182 iterator __f2 = __e1 = __sort(__e1, __e2, __n - __n2, __comp);
2183 if (__comp(*__f2, *__f1))
2184 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002185 iterator __m2 = _VSTD::next(__f2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002186 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2)
2187 ;
Howard Hinnant29f74322013-06-25 16:08:47 +00002188 __node_pointer __f = __f2.__ptr_;
2189 __node_pointer __l = __m2.__ptr_->__prev_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002190 __r = __f2;
2191 __e1 = __f2 = __m2;
2192 base::__unlink_nodes(__f, __l);
Howard Hinnant0949eed2011-06-30 21:18:19 +00002193 __m2 = _VSTD::next(__f1);
Howard Hinnant29f74322013-06-25 16:08:47 +00002194 __link_nodes(__f1.__ptr_, __f, __l);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002195 __f1 = __m2;
2196 }
2197 else
2198 ++__f1;
2199 while (__f1 != __e1 && __f2 != __e2)
2200 {
2201 if (__comp(*__f2, *__f1))
2202 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002203 iterator __m2 = _VSTD::next(__f2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002204 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2)
2205 ;
Howard Hinnant29f74322013-06-25 16:08:47 +00002206 __node_pointer __f = __f2.__ptr_;
2207 __node_pointer __l = __m2.__ptr_->__prev_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002208 if (__e1 == __f2)
2209 __e1 = __m2;
2210 __f2 = __m2;
2211 base::__unlink_nodes(__f, __l);
Howard Hinnant0949eed2011-06-30 21:18:19 +00002212 __m2 = _VSTD::next(__f1);
Howard Hinnant29f74322013-06-25 16:08:47 +00002213 __link_nodes(__f1.__ptr_, __f, __l);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002214 __f1 = __m2;
2215 }
2216 else
2217 ++__f1;
2218 }
2219 return __r;
2220}
2221
2222template <class _Tp, class _Alloc>
2223void
Howard Hinnantc5607272011-06-03 17:30:28 +00002224list<_Tp, _Alloc>::reverse() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002225{
2226 if (base::__sz() > 1)
2227 {
2228 iterator __e = end();
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00002229 for (iterator __i = begin(); __i.__ptr_ != __e.__ptr_;)
2230 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002231 _VSTD::swap(__i.__ptr_->__prev_, __i.__ptr_->__next_);
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00002232 __i.__ptr_ = __i.__ptr_->__prev_;
2233 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002234 _VSTD::swap(__e.__ptr_->__prev_, __e.__ptr_->__next_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002235 }
2236}
2237
2238template <class _Tp, class _Alloc>
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00002239bool
2240list<_Tp, _Alloc>::__invariants() const
2241{
2242 return size() == _VSTD::distance(begin(), end());
2243}
2244
2245#if _LIBCPP_DEBUG_LEVEL >= 2
2246
2247template <class _Tp, class _Alloc>
2248bool
2249list<_Tp, _Alloc>::__dereferenceable(const const_iterator* __i) const
2250{
Howard Hinnant29f74322013-06-25 16:08:47 +00002251 return __i->__ptr_ != static_cast<__node_pointer>(
2252 pointer_traits<__node_base_pointer>::pointer_to(const_cast<__node_base&>(this->__end_)));
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +00002253}
2254
2255template <class _Tp, class _Alloc>
2256bool
2257list<_Tp, _Alloc>::__decrementable(const const_iterator* __i) const
2258{
2259 return !empty() && __i->__ptr_ != base::__end_.__next_;
2260}
2261
2262template <class _Tp, class _Alloc>
2263bool
2264list<_Tp, _Alloc>::__addable(const const_iterator* __i, ptrdiff_t __n) const
2265{
2266 return false;
2267}
2268
2269template <class _Tp, class _Alloc>
2270bool
2271list<_Tp, _Alloc>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
2272{
2273 return false;
2274}
2275
2276#endif // _LIBCPP_DEBUG_LEVEL >= 2
2277
2278template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00002279inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002280bool
2281operator==(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2282{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002283 return __x.size() == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002284}
2285
2286template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00002287inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002288bool
2289operator< (const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2290{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002291 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002292}
2293
2294template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00002295inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002296bool
2297operator!=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2298{
2299 return !(__x == __y);
2300}
2301
2302template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00002303inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002304bool
2305operator> (const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2306{
2307 return __y < __x;
2308}
2309
2310template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00002311inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002312bool
2313operator>=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2314{
2315 return !(__x < __y);
2316}
2317
2318template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00002319inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002320bool
2321operator<=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2322{
2323 return !(__y < __x);
2324}
2325
2326template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00002327inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002328void
2329swap(list<_Tp, _Alloc>& __x, list<_Tp, _Alloc>& __y)
Howard Hinnantc5607272011-06-03 17:30:28 +00002330 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002331{
2332 __x.swap(__y);
2333}
2334
2335_LIBCPP_END_NAMESPACE_STD
2336
2337#endif // _LIBCPP_LIST