blob: cff0a853eff9c6ef3852f736c789a96a6ab222f7 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:161// -*- C++ -*-
2//===---------------------------- list ------------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:014// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:165//
Howard Hinnantb64f8b02010-11-16 22:09:026// 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:168//
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:2839 list()
40 noexcept(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:1641 explicit list(const allocator_type& a);
42 explicit list(size_type n);
Marshall Clowe00f53b2013-09-09 18:19:4543 explicit list(size_type n, const allocator_type& a); // C++14
Howard Hinnantbc8d3f92010-05-11 19:42:1644 list(size_type n, const value_type& value);
45 list(size_type n, const value_type& value, const allocator_type& a);
46 template <class Iter>
47 list(Iter first, Iter last);
48 template <class Iter>
49 list(Iter first, Iter last, const allocator_type& a);
50 list(const list& x);
51 list(const list&, const allocator_type& a);
Howard Hinnantc5607272011-06-03 17:30:2852 list(list&& x)
53 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:1654 list(list&&, const allocator_type& a);
55 list(initializer_list<value_type>);
56 list(initializer_list<value_type>, const allocator_type& a);
57
58 ~list();
59
60 list& operator=(const list& x);
Howard Hinnantc5607272011-06-03 17:30:2861 list& operator=(list&& x)
62 noexcept(
63 allocator_type::propagate_on_container_move_assignment::value &&
64 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:1665 list& operator=(initializer_list<value_type>);
66 template <class Iter>
67 void assign(Iter first, Iter last);
68 void assign(size_type n, const value_type& t);
69 void assign(initializer_list<value_type>);
70
Howard Hinnantc5607272011-06-03 17:30:2871 allocator_type get_allocator() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1672
Howard Hinnantc5607272011-06-03 17:30:2873 iterator begin() noexcept;
74 const_iterator begin() const noexcept;
75 iterator end() noexcept;
76 const_iterator end() const noexcept;
77 reverse_iterator rbegin() noexcept;
78 const_reverse_iterator rbegin() const noexcept;
79 reverse_iterator rend() noexcept;
80 const_reverse_iterator rend() const noexcept;
81 const_iterator cbegin() const noexcept;
82 const_iterator cend() const noexcept;
83 const_reverse_iterator crbegin() const noexcept;
84 const_reverse_iterator crend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1685
86 reference front();
87 const_reference front() const;
88 reference back();
89 const_reference back() const;
90
Howard Hinnantc5607272011-06-03 17:30:2891 bool empty() const noexcept;
92 size_type size() const noexcept;
93 size_type max_size() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1694
95 template <class... Args>
96 void emplace_front(Args&&... args);
97 void pop_front();
98 template <class... Args>
99 void emplace_back(Args&&... args);
100 void pop_back();
101 void push_front(const value_type& x);
102 void push_front(value_type&& x);
103 void push_back(const value_type& x);
104 void push_back(value_type&& x);
105 template <class... Args>
106 iterator emplace(const_iterator position, Args&&... args);
107 iterator insert(const_iterator position, const value_type& x);
108 iterator insert(const_iterator position, value_type&& x);
109 iterator insert(const_iterator position, size_type n, const value_type& x);
110 template <class Iter>
111 iterator insert(const_iterator position, Iter first, Iter last);
112 iterator insert(const_iterator position, initializer_list<value_type> il);
113
114 iterator erase(const_iterator position);
115 iterator erase(const_iterator position, const_iterator last);
116
117 void resize(size_type sz);
118 void resize(size_type sz, const value_type& c);
119
Howard Hinnantc5607272011-06-03 17:30:28120 void swap(list&)
Marshall Clow7d914d12015-07-13 20:04:56121 noexcept(allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnantc5607272011-06-03 17:30:28122 void clear() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16123
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:28147 void reverse() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16148};
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:28164 void swap(list<T,Alloc>& x, list<T,Alloc>& y)
165 noexcept(noexcept(x.swap(y)));
Howard Hinnantbc8d3f92010-05-11 19:42:16166
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>
Eric Fiselier5c74b482015-12-30 20:57:59178#include <type_traits>
Howard Hinnantbc8d3f92010-05-11 19:42:16179
Howard Hinnant66c6f972011-11-29 16:45:27180#include <__undef_min_max>
181
Eric Fiselierb9536102014-08-10 23:53:08182#include <__debug>
Howard Hinnant8b00e6c2013-08-02 00:26:35183
Howard Hinnant08e17472011-10-17 20:05:10184#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16185#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10186#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16187
188_LIBCPP_BEGIN_NAMESPACE_STD
189
Howard Hinnant2b1b2d42011-06-14 19:58:17190template <class _Tp, class _VoidPtr> struct __list_node;
Eric Fiselier5c74b482015-12-30 20:57:59191template <class _Tp, class _VoidPtr> struct __list_node_base;
192
193template <class _Tp, class _VoidPtr>
194struct __list_node_pointer_traits {
195 typedef typename __rebind_pointer<_VoidPtr, __list_node<_Tp, _VoidPtr> >::type
196 __node_pointer;
197 typedef typename __rebind_pointer<_VoidPtr, __list_node_base<_Tp, _VoidPtr> >::type
198 __base_pointer;
199
200#if defined(_LIBCPP_ABI_LIST_REMOVE_NODE_POINTER_UB)
201 typedef __base_pointer __link_pointer;
202#else
203 typedef typename conditional<
204 is_pointer<_VoidPtr>::value,
205 __base_pointer,
206 __node_pointer
207 >::type __link_pointer;
208#endif
209
Eric Fiselier8e7bd4f2016-01-04 03:27:52210 typedef typename conditional<
211 is_same<__link_pointer, __node_pointer>::value,
212 __base_pointer,
213 __node_pointer
214 >::type __non_link_pointer;
215
216 static _LIBCPP_INLINE_VISIBILITY
217 __link_pointer __unsafe_link_pointer_cast(__link_pointer __p) {
218 return __p;
219 }
220
221 static _LIBCPP_INLINE_VISIBILITY
222 __link_pointer __unsafe_link_pointer_cast(__non_link_pointer __p) {
223 return static_cast<__link_pointer>(static_cast<_VoidPtr>(__p));
224 }
225
Eric Fiselier5c74b482015-12-30 20:57:59226};
Howard Hinnantbc8d3f92010-05-11 19:42:16227
228template <class _Tp, class _VoidPtr>
229struct __list_node_base
230{
Eric Fiselier5c74b482015-12-30 20:57:59231 typedef __list_node_pointer_traits<_Tp, _VoidPtr> _NodeTraits;
232 typedef typename _NodeTraits::__node_pointer __node_pointer;
233 typedef typename _NodeTraits::__base_pointer __base_pointer;
234 typedef typename _NodeTraits::__link_pointer __link_pointer;
Howard Hinnant29f74322013-06-25 16:08:47235
Eric Fiselier5c74b482015-12-30 20:57:59236 __link_pointer __prev_;
237 __link_pointer __next_;
Howard Hinnantbc8d3f92010-05-11 19:42:16238
Howard Hinnant82894812010-09-22 16:48:34239 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier8e7bd4f2016-01-04 03:27:52240 __list_node_base() : __prev_(_NodeTraits::__unsafe_link_pointer_cast(__self())),
241 __next_(_NodeTraits::__unsafe_link_pointer_cast(__self())) {}
Marshall Clowea8ed832014-08-05 01:34:12242
243 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier8e7bd4f2016-01-04 03:27:52244 __base_pointer __self() {
Eric Fiselier5c74b482015-12-30 20:57:59245 return pointer_traits<__base_pointer>::pointer_to(*this);
246 }
247
248 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5c74b482015-12-30 20:57:59249 __node_pointer __as_node() {
Eric Fiselier8e7bd4f2016-01-04 03:27:52250 return static_cast<__node_pointer>(__self());
Marshall Clowea8ed832014-08-05 01:34:12251 }
Howard Hinnantbc8d3f92010-05-11 19:42:16252};
253
254template <class _Tp, class _VoidPtr>
255struct __list_node
256 : public __list_node_base<_Tp, _VoidPtr>
257{
258 _Tp __value_;
Eric Fiselier8e7bd4f2016-01-04 03:27:52259
260 typedef __list_node_base<_Tp, _VoidPtr> __base;
261 typedef typename __base::__link_pointer __link_pointer;
262
263 _LIBCPP_INLINE_VISIBILITY
264 __link_pointer __as_link() {
265 return static_cast<__link_pointer>(__base::__self());
266 }
Howard Hinnantbc8d3f92010-05-11 19:42:16267};
268
Marshall Clowceead9c2015-02-18 17:24:08269template <class _Tp, class _Alloc = allocator<_Tp> > class _LIBCPP_TYPE_VIS_ONLY list;
Howard Hinnant2b1b2d42011-06-14 19:58:17270template <class _Tp, class _Alloc> class __list_imp;
Howard Hinnant0f678bd2013-08-12 18:38:34271template <class _Tp, class _VoidPtr> class _LIBCPP_TYPE_VIS_ONLY __list_const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16272
273template <class _Tp, class _VoidPtr>
Howard Hinnant0f678bd2013-08-12 18:38:34274class _LIBCPP_TYPE_VIS_ONLY __list_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16275{
Eric Fiselier5c74b482015-12-30 20:57:59276 typedef __list_node_pointer_traits<_Tp, _VoidPtr> _NodeTraits;
277 typedef typename _NodeTraits::__link_pointer __link_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16278
Eric Fiselier5c74b482015-12-30 20:57:59279 __link_pointer __ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:16280
Howard Hinnant1c3ec6d2011-09-27 23:55:03281#if _LIBCPP_DEBUG_LEVEL >= 2
282 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5c74b482015-12-30 20:57:59283 explicit __list_iterator(__link_pointer __p, const void* __c) _NOEXCEPT
Howard Hinnant1c3ec6d2011-09-27 23:55:03284 : __ptr_(__p)
285 {
286 __get_db()->__insert_ic(this, __c);
287 }
288#else
Howard Hinnant82894812010-09-22 16:48:34289 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5c74b482015-12-30 20:57:59290 explicit __list_iterator(__link_pointer __p) _NOEXCEPT : __ptr_(__p) {}
Howard Hinnant1c3ec6d2011-09-27 23:55:03291#endif
292
293
Howard Hinnantbc8d3f92010-05-11 19:42:16294
295 template<class, class> friend class list;
296 template<class, class> friend class __list_imp;
297 template<class, class> friend class __list_const_iterator;
298public:
299 typedef bidirectional_iterator_tag iterator_category;
300 typedef _Tp value_type;
301 typedef value_type& reference;
Eric Fiselierbb2f28e2015-08-23 02:56:05302 typedef typename __rebind_pointer<_VoidPtr, value_type>::type pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16303 typedef typename pointer_traits<pointer>::difference_type difference_type;
304
Howard Hinnant82894812010-09-22 16:48:34305 _LIBCPP_INLINE_VISIBILITY
Marshall Clow65d2e6a2013-08-05 21:23:28306 __list_iterator() _NOEXCEPT : __ptr_(nullptr)
Howard Hinnant1c3ec6d2011-09-27 23:55:03307 {
308#if _LIBCPP_DEBUG_LEVEL >= 2
309 __get_db()->__insert_i(this);
310#endif
311 }
312
313#if _LIBCPP_DEBUG_LEVEL >= 2
314
Howard Hinnant211f0ee2011-01-28 23:46:28315 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03316 __list_iterator(const __list_iterator& __p)
317 : __ptr_(__p.__ptr_)
318 {
319 __get_db()->__iterator_copy(this, &__p);
320 }
321
322 _LIBCPP_INLINE_VISIBILITY
323 ~__list_iterator()
324 {
325 __get_db()->__erase_i(this);
326 }
327
328 _LIBCPP_INLINE_VISIBILITY
329 __list_iterator& operator=(const __list_iterator& __p)
330 {
331 if (this != &__p)
332 {
333 __get_db()->__iterator_copy(this, &__p);
334 __ptr_ = __p.__ptr_;
335 }
336 return *this;
337 }
338
339#endif // _LIBCPP_DEBUG_LEVEL >= 2
340
341 _LIBCPP_INLINE_VISIBILITY
342 reference operator*() const
343 {
344#if _LIBCPP_DEBUG_LEVEL >= 2
345 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
346 "Attempted to dereference a non-dereferenceable list::iterator");
347#endif
Eric Fiselier5c74b482015-12-30 20:57:59348 return __ptr_->__as_node()->__value_;
Howard Hinnant1c3ec6d2011-09-27 23:55:03349 }
Howard Hinnant82894812010-09-22 16:48:34350 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant29f74322013-06-25 16:08:47351 pointer operator->() const
352 {
353#if _LIBCPP_DEBUG_LEVEL >= 2
354 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
355 "Attempted to dereference a non-dereferenceable list::iterator");
356#endif
Eric Fiselier5c74b482015-12-30 20:57:59357 return pointer_traits<pointer>::pointer_to(__ptr_->__as_node()->__value_);
Howard Hinnant29f74322013-06-25 16:08:47358 }
Howard Hinnantbc8d3f92010-05-11 19:42:16359
Howard Hinnant82894812010-09-22 16:48:34360 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03361 __list_iterator& operator++()
362 {
363#if _LIBCPP_DEBUG_LEVEL >= 2
364 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
365 "Attempted to increment non-incrementable list::iterator");
366#endif
367 __ptr_ = __ptr_->__next_;
368 return *this;
369 }
Howard Hinnant82894812010-09-22 16:48:34370 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16371 __list_iterator operator++(int) {__list_iterator __t(*this); ++(*this); return __t;}
372
Howard Hinnant82894812010-09-22 16:48:34373 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03374 __list_iterator& operator--()
375 {
376#if _LIBCPP_DEBUG_LEVEL >= 2
377 _LIBCPP_ASSERT(__get_const_db()->__decrementable(this),
378 "Attempted to decrement non-decrementable list::iterator");
379#endif
380 __ptr_ = __ptr_->__prev_;
381 return *this;
382 }
Howard Hinnant82894812010-09-22 16:48:34383 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16384 __list_iterator operator--(int) {__list_iterator __t(*this); --(*this); return __t;}
385
Howard Hinnant82894812010-09-22 16:48:34386 friend _LIBCPP_INLINE_VISIBILITY
387 bool operator==(const __list_iterator& __x, const __list_iterator& __y)
Howard Hinnant1c3ec6d2011-09-27 23:55:03388 {
Howard Hinnant1c3ec6d2011-09-27 23:55:03389 return __x.__ptr_ == __y.__ptr_;
390 }
Howard Hinnant82894812010-09-22 16:48:34391 friend _LIBCPP_INLINE_VISIBILITY
392 bool operator!=(const __list_iterator& __x, const __list_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16393 {return !(__x == __y);}
394};
395
396template <class _Tp, class _VoidPtr>
Howard Hinnant0f678bd2013-08-12 18:38:34397class _LIBCPP_TYPE_VIS_ONLY __list_const_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16398{
Eric Fiselier5c74b482015-12-30 20:57:59399 typedef __list_node_pointer_traits<_Tp, _VoidPtr> _NodeTraits;
400 typedef typename _NodeTraits::__link_pointer __link_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16401
Eric Fiselier5c74b482015-12-30 20:57:59402 __link_pointer __ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:16403
Howard Hinnant1c3ec6d2011-09-27 23:55:03404#if _LIBCPP_DEBUG_LEVEL >= 2
405 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5c74b482015-12-30 20:57:59406 explicit __list_const_iterator(__link_pointer __p, const void* __c) _NOEXCEPT
Howard Hinnant1c3ec6d2011-09-27 23:55:03407 : __ptr_(__p)
408 {
409 __get_db()->__insert_ic(this, __c);
410 }
411#else
Howard Hinnant82894812010-09-22 16:48:34412 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5c74b482015-12-30 20:57:59413 explicit __list_const_iterator(__link_pointer __p) _NOEXCEPT : __ptr_(__p) {}
Howard Hinnant1c3ec6d2011-09-27 23:55:03414#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16415
416 template<class, class> friend class list;
417 template<class, class> friend class __list_imp;
418public:
419 typedef bidirectional_iterator_tag iterator_category;
420 typedef _Tp value_type;
421 typedef const value_type& reference;
Eric Fiselierbb2f28e2015-08-23 02:56:05422 typedef typename __rebind_pointer<_VoidPtr, const value_type>::type pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16423 typedef typename pointer_traits<pointer>::difference_type difference_type;
424
Howard Hinnant82894812010-09-22 16:48:34425 _LIBCPP_INLINE_VISIBILITY
Marshall Clow65d2e6a2013-08-05 21:23:28426 __list_const_iterator() _NOEXCEPT : __ptr_(nullptr)
Howard Hinnant1c3ec6d2011-09-27 23:55:03427 {
428#if _LIBCPP_DEBUG_LEVEL >= 2
429 __get_db()->__insert_i(this);
430#endif
431 }
Howard Hinnant211f0ee2011-01-28 23:46:28432 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6dcaf3e2013-04-05 17:58:52433 __list_const_iterator(const __list_iterator<_Tp, _VoidPtr>& __p) _NOEXCEPT
Howard Hinnant1c3ec6d2011-09-27 23:55:03434 : __ptr_(__p.__ptr_)
435 {
436#if _LIBCPP_DEBUG_LEVEL >= 2
437 __get_db()->__iterator_copy(this, &__p);
438#endif
439 }
440
441#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16442
Howard Hinnant82894812010-09-22 16:48:34443 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03444 __list_const_iterator(const __list_const_iterator& __p)
445 : __ptr_(__p.__ptr_)
446 {
447 __get_db()->__iterator_copy(this, &__p);
448 }
449
450 _LIBCPP_INLINE_VISIBILITY
451 ~__list_const_iterator()
452 {
453 __get_db()->__erase_i(this);
454 }
455
456 _LIBCPP_INLINE_VISIBILITY
457 __list_const_iterator& operator=(const __list_const_iterator& __p)
458 {
459 if (this != &__p)
460 {
461 __get_db()->__iterator_copy(this, &__p);
462 __ptr_ = __p.__ptr_;
463 }
464 return *this;
465 }
466
467#endif // _LIBCPP_DEBUG_LEVEL >= 2
468 _LIBCPP_INLINE_VISIBILITY
469 reference operator*() const
470 {
471#if _LIBCPP_DEBUG_LEVEL >= 2
472 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
473 "Attempted to dereference a non-dereferenceable list::const_iterator");
474#endif
Eric Fiselier5c74b482015-12-30 20:57:59475 return __ptr_->__as_node()->__value_;
Howard Hinnant1c3ec6d2011-09-27 23:55:03476 }
Howard Hinnant82894812010-09-22 16:48:34477 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant29f74322013-06-25 16:08:47478 pointer operator->() const
479 {
480#if _LIBCPP_DEBUG_LEVEL >= 2
481 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
482 "Attempted to dereference a non-dereferenceable list::iterator");
483#endif
Eric Fiselier5c74b482015-12-30 20:57:59484 return pointer_traits<pointer>::pointer_to(__ptr_->__as_node()->__value_);
Howard Hinnant29f74322013-06-25 16:08:47485 }
Howard Hinnantbc8d3f92010-05-11 19:42:16486
Howard Hinnant82894812010-09-22 16:48:34487 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03488 __list_const_iterator& operator++()
489 {
490#if _LIBCPP_DEBUG_LEVEL >= 2
491 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
492 "Attempted to increment non-incrementable list::const_iterator");
493#endif
494 __ptr_ = __ptr_->__next_;
495 return *this;
496 }
Howard Hinnant82894812010-09-22 16:48:34497 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16498 __list_const_iterator operator++(int) {__list_const_iterator __t(*this); ++(*this); return __t;}
499
Howard Hinnant82894812010-09-22 16:48:34500 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03501 __list_const_iterator& operator--()
502 {
503#if _LIBCPP_DEBUG_LEVEL >= 2
504 _LIBCPP_ASSERT(__get_const_db()->__decrementable(this),
505 "Attempted to decrement non-decrementable list::const_iterator");
506#endif
507 __ptr_ = __ptr_->__prev_;
508 return *this;
509 }
Howard Hinnant82894812010-09-22 16:48:34510 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16511 __list_const_iterator operator--(int) {__list_const_iterator __t(*this); --(*this); return __t;}
512
Howard Hinnant82894812010-09-22 16:48:34513 friend _LIBCPP_INLINE_VISIBILITY
514 bool operator==(const __list_const_iterator& __x, const __list_const_iterator& __y)
Howard Hinnant1c3ec6d2011-09-27 23:55:03515 {
Howard Hinnant1c3ec6d2011-09-27 23:55:03516 return __x.__ptr_ == __y.__ptr_;
517 }
Howard Hinnant82894812010-09-22 16:48:34518 friend _LIBCPP_INLINE_VISIBILITY
519 bool operator!=(const __list_const_iterator& __x, const __list_const_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16520 {return !(__x == __y);}
521};
522
523template <class _Tp, class _Alloc>
524class __list_imp
525{
526 __list_imp(const __list_imp&);
527 __list_imp& operator=(const __list_imp&);
528protected:
529 typedef _Tp value_type;
530 typedef _Alloc allocator_type;
531 typedef allocator_traits<allocator_type> __alloc_traits;
532 typedef typename __alloc_traits::size_type size_type;
533 typedef typename __alloc_traits::void_pointer __void_pointer;
534 typedef __list_iterator<value_type, __void_pointer> iterator;
535 typedef __list_const_iterator<value_type, __void_pointer> const_iterator;
536 typedef __list_node_base<value_type, __void_pointer> __node_base;
537 typedef __list_node<value_type, __void_pointer> __node;
Marshall Clow66302c62015-04-07 05:21:38538 typedef typename __rebind_alloc_helper<__alloc_traits, __node>::type __node_allocator;
Howard Hinnantbc8d3f92010-05-11 19:42:16539 typedef allocator_traits<__node_allocator> __node_alloc_traits;
540 typedef typename __node_alloc_traits::pointer __node_pointer;
Howard Hinnant29f74322013-06-25 16:08:47541 typedef typename __node_alloc_traits::pointer __node_const_pointer;
Eric Fiselier8e7bd4f2016-01-04 03:27:52542 typedef __list_node_pointer_traits<value_type, __void_pointer> __node_pointer_traits;
543 typedef typename __node_pointer_traits::__link_pointer __link_pointer;
Eric Fiselier5c74b482015-12-30 20:57:59544 typedef __link_pointer __link_const_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16545 typedef typename __alloc_traits::pointer pointer;
546 typedef typename __alloc_traits::const_pointer const_pointer;
547 typedef typename __alloc_traits::difference_type difference_type;
548
Marshall Clow66302c62015-04-07 05:21:38549 typedef typename __rebind_alloc_helper<__alloc_traits, __node_base>::type __node_base_allocator;
Howard Hinnant29f74322013-06-25 16:08:47550 typedef typename allocator_traits<__node_base_allocator>::pointer __node_base_pointer;
551
Howard Hinnantbc8d3f92010-05-11 19:42:16552 __node_base __end_;
553 __compressed_pair<size_type, __node_allocator> __size_alloc_;
554
Howard Hinnant82894812010-09-22 16:48:34555 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier8e7bd4f2016-01-04 03:27:52556 __link_pointer __end_as_link() const _NOEXCEPT {
557 return __node_pointer_traits::__unsafe_link_pointer_cast(
558 const_cast<__node_base&>(__end_).__self());
559 }
560
561 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28562 size_type& __sz() _NOEXCEPT {return __size_alloc_.first();}
Howard Hinnant82894812010-09-22 16:48:34563 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28564 const size_type& __sz() const _NOEXCEPT
565 {return __size_alloc_.first();}
Howard Hinnant82894812010-09-22 16:48:34566 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28567 __node_allocator& __node_alloc() _NOEXCEPT
568 {return __size_alloc_.second();}
Howard Hinnant82894812010-09-22 16:48:34569 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28570 const __node_allocator& __node_alloc() const _NOEXCEPT
571 {return __size_alloc_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16572
Evgeniy Stepanov9341a8a2016-04-22 01:04:55573 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5c74b482015-12-30 20:57:59574 static void __unlink_nodes(__link_pointer __f, __link_pointer __l) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16575
Evgeniy Stepanov9341a8a2016-04-22 01:04:55576 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28577 __list_imp()
578 _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value);
Evgeniy Stepanov9341a8a2016-04-22 01:04:55579 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16580 __list_imp(const allocator_type& __a);
581 ~__list_imp();
Howard Hinnantc5607272011-06-03 17:30:28582 void clear() _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34583 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28584 bool empty() const _NOEXCEPT {return __sz() == 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16585
Howard Hinnant82894812010-09-22 16:48:34586 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03587 iterator begin() _NOEXCEPT
588 {
589#if _LIBCPP_DEBUG_LEVEL >= 2
590 return iterator(__end_.__next_, this);
591#else
592 return iterator(__end_.__next_);
593#endif
594 }
Howard Hinnant82894812010-09-22 16:48:34595 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28596 const_iterator begin() const _NOEXCEPT
Howard Hinnant1c3ec6d2011-09-27 23:55:03597 {
598#if _LIBCPP_DEBUG_LEVEL >= 2
599 return const_iterator(__end_.__next_, this);
600#else
601 return const_iterator(__end_.__next_);
602#endif
603 }
Howard Hinnant82894812010-09-22 16:48:34604 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03605 iterator end() _NOEXCEPT
606 {
607#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselier8e7bd4f2016-01-04 03:27:52608 return iterator(__end_as_link(), this);
Howard Hinnant1c3ec6d2011-09-27 23:55:03609#else
Eric Fiselier8e7bd4f2016-01-04 03:27:52610 return iterator(__end_as_link());
Howard Hinnant1c3ec6d2011-09-27 23:55:03611#endif
612 }
Howard Hinnant82894812010-09-22 16:48:34613 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28614 const_iterator end() const _NOEXCEPT
Howard Hinnant1c3ec6d2011-09-27 23:55:03615 {
616#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselier8e7bd4f2016-01-04 03:27:52617 return const_iterator(__end_as_link(), this);
Howard Hinnant1c3ec6d2011-09-27 23:55:03618#else
Eric Fiselier8e7bd4f2016-01-04 03:27:52619 return const_iterator(__end_as_link());
Howard Hinnant1c3ec6d2011-09-27 23:55:03620#endif
621 }
Howard Hinnantbc8d3f92010-05-11 19:42:16622
Howard Hinnantc5607272011-06-03 17:30:28623 void swap(__list_imp& __c)
Marshall Clow7d914d12015-07-13 20:04:56624#if _LIBCPP_STD_VER >= 14
625 _NOEXCEPT;
626#else
627 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
628 __is_nothrow_swappable<allocator_type>::value);
629#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16630
Howard Hinnant82894812010-09-22 16:48:34631 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16632 void __copy_assign_alloc(const __list_imp& __c)
633 {__copy_assign_alloc(__c, integral_constant<bool,
634 __node_alloc_traits::propagate_on_container_copy_assignment::value>());}
635
Howard Hinnant82894812010-09-22 16:48:34636 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16637 void __move_assign_alloc(__list_imp& __c)
Howard Hinnantc5607272011-06-03 17:30:28638 _NOEXCEPT_(
639 !__node_alloc_traits::propagate_on_container_move_assignment::value ||
640 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16641 {__move_assign_alloc(__c, integral_constant<bool,
642 __node_alloc_traits::propagate_on_container_move_assignment::value>());}
643
644private:
Howard Hinnant82894812010-09-22 16:48:34645 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16646 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:34653 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16654 void __copy_assign_alloc(const __list_imp& __c, false_type)
655 {}
656
Howard Hinnant82894812010-09-22 16:48:34657 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31658 void __move_assign_alloc(__list_imp& __c, true_type)
Howard Hinnantc5607272011-06-03 17:30:28659 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16660 {
Howard Hinnant0949eed2011-06-30 21:18:19661 __node_alloc() = _VSTD::move(__c.__node_alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16662 }
663
Howard Hinnant82894812010-09-22 16:48:34664 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31665 void __move_assign_alloc(__list_imp& __c, false_type)
Howard Hinnantc5607272011-06-03 17:30:28666 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16667 {}
668};
669
670// Unlink nodes [__f, __l]
671template <class _Tp, class _Alloc>
Evgeniy Stepanov9341a8a2016-04-22 01:04:55672inline
Howard Hinnantbc8d3f92010-05-11 19:42:16673void
Eric Fiselier5c74b482015-12-30 20:57:59674__list_imp<_Tp, _Alloc>::__unlink_nodes(__link_pointer __f, __link_pointer __l)
Howard Hinnantc5607272011-06-03 17:30:28675 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16676{
Howard Hinnant29f74322013-06-25 16:08:47677 __f->__prev_->__next_ = __l->__next_;
678 __l->__next_->__prev_ = __f->__prev_;
Howard Hinnantbc8d3f92010-05-11 19:42:16679}
680
681template <class _Tp, class _Alloc>
Evgeniy Stepanov9341a8a2016-04-22 01:04:55682inline
Howard Hinnantbc8d3f92010-05-11 19:42:16683__list_imp<_Tp, _Alloc>::__list_imp()
Howard Hinnantc5607272011-06-03 17:30:28684 _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16685 : __size_alloc_(0)
686{
687}
688
689template <class _Tp, class _Alloc>
Evgeniy Stepanov9341a8a2016-04-22 01:04:55690inline
Howard Hinnantbc8d3f92010-05-11 19:42:16691__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:03700#if _LIBCPP_DEBUG_LEVEL >= 2
701 __get_db()->__erase_c(this);
702#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16703}
704
705template <class _Tp, class _Alloc>
706void
Howard Hinnantc5607272011-06-03 17:30:28707__list_imp<_Tp, _Alloc>::clear() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16708{
709 if (!empty())
710 {
711 __node_allocator& __na = __node_alloc();
Eric Fiselier5c74b482015-12-30 20:57:59712 __link_pointer __f = __end_.__next_;
Eric Fiselier8e7bd4f2016-01-04 03:27:52713 __link_pointer __l = __end_as_link();
Howard Hinnant29f74322013-06-25 16:08:47714 __unlink_nodes(__f, __l->__prev_);
Howard Hinnantbc8d3f92010-05-11 19:42:16715 __sz() = 0;
716 while (__f != __l)
717 {
Eric Fiselier5c74b482015-12-30 20:57:59718 __node_pointer __np = __f->__as_node();
Howard Hinnant1c3ec6d2011-09-27 23:55:03719 __f = __f->__next_;
Eric Fiselier5c74b482015-12-30 20:57:59720 __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
721 __node_alloc_traits::deallocate(__na, __np, 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16722 }
Howard Hinnant1c3ec6d2011-09-27 23:55:03723#if _LIBCPP_DEBUG_LEVEL >= 2
724 __c_node* __c = __get_db()->__find_c_and_lock(this);
725 for (__i_node** __p = __c->end_; __p != __c->beg_; )
726 {
727 --__p;
728 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
729 if (__i->__ptr_ != __l)
730 {
731 (*__p)->__c_ = nullptr;
732 if (--__c->end_ != __p)
733 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
734 }
735 }
736 __get_db()->unlock();
737#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16738 }
739}
740
741template <class _Tp, class _Alloc>
742void
743__list_imp<_Tp, _Alloc>::swap(__list_imp& __c)
Marshall Clow7d914d12015-07-13 20:04:56744#if _LIBCPP_STD_VER >= 14
745 _NOEXCEPT
746#else
747 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
748 __is_nothrow_swappable<allocator_type>::value)
749#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16750{
Howard Hinnant1c3ec6d2011-09-27 23:55:03751 _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
752 this->__node_alloc() == __c.__node_alloc(),
753 "list::swap: Either propagate_on_container_swap must be true"
754 " or the allocators must compare equal");
Howard Hinnant0949eed2011-06-30 21:18:19755 using _VSTD::swap;
Marshall Clow7d914d12015-07-13 20:04:56756 __swap_allocator(__node_alloc(), __c.__node_alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16757 swap(__sz(), __c.__sz());
758 swap(__end_, __c.__end_);
759 if (__sz() == 0)
Eric Fiselier8e7bd4f2016-01-04 03:27:52760 __end_.__next_ = __end_.__prev_ = __end_as_link();
Howard Hinnantbc8d3f92010-05-11 19:42:16761 else
Eric Fiselier8e7bd4f2016-01-04 03:27:52762 __end_.__prev_->__next_ = __end_.__next_->__prev_ = __end_as_link();
Howard Hinnantbc8d3f92010-05-11 19:42:16763 if (__c.__sz() == 0)
Eric Fiselier8e7bd4f2016-01-04 03:27:52764 __c.__end_.__next_ = __c.__end_.__prev_ = __c.__end_as_link();
Howard Hinnantbc8d3f92010-05-11 19:42:16765 else
Eric Fiselier8e7bd4f2016-01-04 03:27:52766 __c.__end_.__prev_->__next_ = __c.__end_.__next_->__prev_ = __c.__end_as_link();
Marshall Clowea8ed832014-08-05 01:34:12767
Howard Hinnant1c3ec6d2011-09-27 23:55:03768#if _LIBCPP_DEBUG_LEVEL >= 2
769 __libcpp_db* __db = __get_db();
770 __c_node* __cn1 = __db->__find_c_and_lock(this);
771 __c_node* __cn2 = __db->__find_c(&__c);
772 std::swap(__cn1->beg_, __cn2->beg_);
773 std::swap(__cn1->end_, __cn2->end_);
774 std::swap(__cn1->cap_, __cn2->cap_);
775 for (__i_node** __p = __cn1->end_; __p != __cn1->beg_;)
776 {
777 --__p;
778 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
Eric Fiselier8e7bd4f2016-01-04 03:27:52779 if (__i->__ptr_ == __c.__end_as_link())
Howard Hinnant1c3ec6d2011-09-27 23:55:03780 {
781 __cn2->__add(*__p);
782 if (--__cn1->end_ != __p)
783 memmove(__p, __p+1, (__cn1->end_ - __p)*sizeof(__i_node*));
784 }
785 else
786 (*__p)->__c_ = __cn1;
787 }
788 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
789 {
790 --__p;
791 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
Eric Fiselier8e7bd4f2016-01-04 03:27:52792 if (__i->__ptr_ == __end_as_link())
Howard Hinnant1c3ec6d2011-09-27 23:55:03793 {
794 __cn1->__add(*__p);
795 if (--__cn2->end_ != __p)
796 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
797 }
798 else
799 (*__p)->__c_ = __cn2;
800 }
801 __db->unlock();
802#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16803}
804
Marshall Clowceead9c2015-02-18 17:24:08805template <class _Tp, class _Alloc /*= allocator<_Tp>*/>
Howard Hinnant0f678bd2013-08-12 18:38:34806class _LIBCPP_TYPE_VIS_ONLY list
Howard Hinnantbc8d3f92010-05-11 19:42:16807 : private __list_imp<_Tp, _Alloc>
808{
809 typedef __list_imp<_Tp, _Alloc> base;
810 typedef typename base::__node __node;
811 typedef typename base::__node_allocator __node_allocator;
812 typedef typename base::__node_pointer __node_pointer;
813 typedef typename base::__node_alloc_traits __node_alloc_traits;
Howard Hinnant29f74322013-06-25 16:08:47814 typedef typename base::__node_base __node_base;
815 typedef typename base::__node_base_pointer __node_base_pointer;
Eric Fiselier5c74b482015-12-30 20:57:59816 typedef typename base::__link_pointer __link_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16817
818public:
819 typedef _Tp value_type;
820 typedef _Alloc allocator_type;
821 static_assert((is_same<value_type, typename allocator_type::value_type>::value),
822 "Invalid allocator::value_type");
823 typedef value_type& reference;
824 typedef const value_type& const_reference;
825 typedef typename base::pointer pointer;
826 typedef typename base::const_pointer const_pointer;
827 typedef typename base::size_type size_type;
828 typedef typename base::difference_type difference_type;
829 typedef typename base::iterator iterator;
830 typedef typename base::const_iterator const_iterator;
Howard Hinnant0949eed2011-06-30 21:18:19831 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
832 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16833
Howard Hinnant82894812010-09-22 16:48:34834 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28835 list()
836 _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value)
Howard Hinnant1c3ec6d2011-09-27 23:55:03837 {
838#if _LIBCPP_DEBUG_LEVEL >= 2
839 __get_db()->__insert_c(this);
840#endif
841 }
Howard Hinnant82894812010-09-22 16:48:34842 _LIBCPP_INLINE_VISIBILITY
Marshall Clow955f2c82013-09-08 19:11:51843 explicit list(const allocator_type& __a) : base(__a)
Howard Hinnant1c3ec6d2011-09-27 23:55:03844 {
845#if _LIBCPP_DEBUG_LEVEL >= 2
846 __get_db()->__insert_c(this);
847#endif
848 }
Marshall Clow955f2c82013-09-08 19:11:51849 explicit list(size_type __n);
850#if _LIBCPP_STD_VER > 11
851 explicit list(size_type __n, const allocator_type& __a);
852#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16853 list(size_type __n, const value_type& __x);
854 list(size_type __n, const value_type& __x, const allocator_type& __a);
855 template <class _InpIter>
856 list(_InpIter __f, _InpIter __l,
857 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
858 template <class _InpIter>
859 list(_InpIter __f, _InpIter __l, const allocator_type& __a,
860 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
861
862 list(const list& __c);
863 list(const list& __c, const allocator_type& __a);
Evgeniy Stepanov9341a8a2016-04-22 01:04:55864 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16865 list& operator=(const list& __c);
Howard Hinnante3e32912011-08-12 21:56:02866#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16867 list(initializer_list<value_type> __il);
868 list(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnante3e32912011-08-12 21:56:02869#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant73d21a42010-09-04 23:28:19870#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov9341a8a2016-04-22 01:04:55871 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28872 list(list&& __c)
873 _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value);
Evgeniy Stepanov9341a8a2016-04-22 01:04:55874 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16875 list(list&& __c, const allocator_type& __a);
Evgeniy Stepanov9341a8a2016-04-22 01:04:55876 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28877 list& operator=(list&& __c)
878 _NOEXCEPT_(
879 __node_alloc_traits::propagate_on_container_move_assignment::value &&
880 is_nothrow_move_assignable<__node_allocator>::value);
Howard Hinnant73d21a42010-09-04 23:28:19881#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnante3e32912011-08-12 21:56:02882#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant82894812010-09-22 16:48:34883 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16884 list& operator=(initializer_list<value_type> __il)
885 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnante3e32912011-08-12 21:56:02886#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16887
888 template <class _InpIter>
889 void assign(_InpIter __f, _InpIter __l,
890 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
891 void assign(size_type __n, const value_type& __x);
Howard Hinnante3e32912011-08-12 21:56:02892#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant82894812010-09-22 16:48:34893 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16894 void assign(initializer_list<value_type> __il)
895 {assign(__il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02896#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16897
Evgeniy Stepanov9341a8a2016-04-22 01:04:55898 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28899 allocator_type get_allocator() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16900
Howard Hinnant82894812010-09-22 16:48:34901 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28902 size_type size() const _NOEXCEPT {return base::__sz();}
Howard Hinnant82894812010-09-22 16:48:34903 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28904 bool empty() const _NOEXCEPT {return base::empty();}
Howard Hinnant82894812010-09-22 16:48:34905 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28906 size_type max_size() const _NOEXCEPT
907 {return numeric_limits<difference_type>::max();}
Howard Hinnantbc8d3f92010-05-11 19:42:16908
Howard Hinnant82894812010-09-22 16:48:34909 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28910 iterator begin() _NOEXCEPT {return base::begin();}
Howard Hinnant82894812010-09-22 16:48:34911 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28912 const_iterator begin() const _NOEXCEPT {return base::begin();}
Howard Hinnant82894812010-09-22 16:48:34913 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28914 iterator end() _NOEXCEPT {return base::end();}
Howard Hinnant82894812010-09-22 16:48:34915 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28916 const_iterator end() const _NOEXCEPT {return base::end();}
Howard Hinnant82894812010-09-22 16:48:34917 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28918 const_iterator cbegin() const _NOEXCEPT {return base::begin();}
Howard Hinnant82894812010-09-22 16:48:34919 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28920 const_iterator cend() const _NOEXCEPT {return base::end();}
Howard Hinnantbc8d3f92010-05-11 19:42:16921
Howard Hinnant82894812010-09-22 16:48:34922 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28923 reverse_iterator rbegin() _NOEXCEPT
924 {return reverse_iterator(end());}
Howard Hinnant82894812010-09-22 16:48:34925 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28926 const_reverse_iterator rbegin() const _NOEXCEPT
927 {return const_reverse_iterator(end());}
Howard Hinnant82894812010-09-22 16:48:34928 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28929 reverse_iterator rend() _NOEXCEPT
930 {return reverse_iterator(begin());}
Howard Hinnant82894812010-09-22 16:48:34931 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28932 const_reverse_iterator rend() const _NOEXCEPT
933 {return const_reverse_iterator(begin());}
Howard Hinnant82894812010-09-22 16:48:34934 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28935 const_reverse_iterator crbegin() const _NOEXCEPT
936 {return const_reverse_iterator(end());}
Howard Hinnant82894812010-09-22 16:48:34937 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28938 const_reverse_iterator crend() const _NOEXCEPT
939 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16940
Howard Hinnant82894812010-09-22 16:48:34941 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03942 reference front()
943 {
944 _LIBCPP_ASSERT(!empty(), "list::front called on empty list");
Eric Fiselier5c74b482015-12-30 20:57:59945 return base::__end_.__next_->__as_node()->__value_;
Howard Hinnant1c3ec6d2011-09-27 23:55:03946 }
Howard Hinnant82894812010-09-22 16:48:34947 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03948 const_reference front() const
949 {
950 _LIBCPP_ASSERT(!empty(), "list::front called on empty list");
Eric Fiselier5c74b482015-12-30 20:57:59951 return base::__end_.__next_->__as_node()->__value_;
Howard Hinnant1c3ec6d2011-09-27 23:55:03952 }
Howard Hinnant82894812010-09-22 16:48:34953 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03954 reference back()
955 {
956 _LIBCPP_ASSERT(!empty(), "list::back called on empty list");
Eric Fiselier5c74b482015-12-30 20:57:59957 return base::__end_.__prev_->__as_node()->__value_;
Howard Hinnant1c3ec6d2011-09-27 23:55:03958 }
Howard Hinnant82894812010-09-22 16:48:34959 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03960 const_reference back() const
961 {
962 _LIBCPP_ASSERT(!empty(), "list::back called on empty list");
Eric Fiselier5c74b482015-12-30 20:57:59963 return base::__end_.__prev_->__as_node()->__value_;
Howard Hinnant1c3ec6d2011-09-27 23:55:03964 }
Howard Hinnantbc8d3f92010-05-11 19:42:16965
Howard Hinnant73d21a42010-09-04 23:28:19966#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16967 void push_front(value_type&& __x);
968 void push_back(value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19969#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16970 template <class... _Args>
971 void emplace_front(_Args&&... __args);
972 template <class... _Args>
973 void emplace_back(_Args&&... __args);
974 template <class... _Args>
975 iterator emplace(const_iterator __p, _Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19976#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16977 iterator insert(const_iterator __p, value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19978#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16979
980 void push_front(const value_type& __x);
981 void push_back(const value_type& __x);
982
983 iterator insert(const_iterator __p, const value_type& __x);
984 iterator insert(const_iterator __p, size_type __n, const value_type& __x);
985 template <class _InpIter>
986 iterator insert(const_iterator __p, _InpIter __f, _InpIter __l,
987 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
Howard Hinnante3e32912011-08-12 21:56:02988#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant82894812010-09-22 16:48:34989 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16990 iterator insert(const_iterator __p, initializer_list<value_type> __il)
991 {return insert(__p, __il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02992#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16993
Howard Hinnant82894812010-09-22 16:48:34994 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28995 void swap(list& __c)
Marshall Clow7d914d12015-07-13 20:04:56996#if _LIBCPP_STD_VER >= 14
997 _NOEXCEPT
998#else
Howard Hinnantc5607272011-06-03 17:30:28999 _NOEXCEPT_(!__node_alloc_traits::propagate_on_container_swap::value ||
1000 __is_nothrow_swappable<__node_allocator>::value)
Marshall Clow7d914d12015-07-13 20:04:561001#endif
Howard Hinnantc5607272011-06-03 17:30:281002 {base::swap(__c);}
Howard Hinnant82894812010-09-22 16:48:341003 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:281004 void clear() _NOEXCEPT {base::clear();}
Howard Hinnantbc8d3f92010-05-11 19:42:161005
1006 void pop_front();
1007 void pop_back();
1008
1009 iterator erase(const_iterator __p);
1010 iterator erase(const_iterator __f, const_iterator __l);
1011
1012 void resize(size_type __n);
1013 void resize(size_type __n, const value_type& __x);
1014
1015 void splice(const_iterator __p, list& __c);
Howard Hinnant73d21a42010-09-04 23:28:191016#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant82894812010-09-22 16:48:341017 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161018 void splice(const_iterator __p, list&& __c) {splice(__p, __c);}
1019#endif
1020 void splice(const_iterator __p, list& __c, const_iterator __i);
Howard Hinnant73d21a42010-09-04 23:28:191021#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant82894812010-09-22 16:48:341022 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161023 void splice(const_iterator __p, list&& __c, const_iterator __i)
1024 {splice(__p, __c, __i);}
Howard Hinnant73d21a42010-09-04 23:28:191025#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:161026 void splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l);
Howard Hinnant73d21a42010-09-04 23:28:191027#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant82894812010-09-22 16:48:341028 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161029 void splice(const_iterator __p, list&& __c, const_iterator __f, const_iterator __l)
1030 {splice(__p, __c, __f, __l);}
Howard Hinnant73d21a42010-09-04 23:28:191031#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:161032
1033 void remove(const value_type& __x);
1034 template <class _Pred> void remove_if(_Pred __pred);
Evgeniy Stepanov9341a8a2016-04-22 01:04:551035 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161036 void unique();
1037 template <class _BinaryPred>
1038 void unique(_BinaryPred __binary_pred);
Evgeniy Stepanov9341a8a2016-04-22 01:04:551039 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161040 void merge(list& __c);
Howard Hinnant73d21a42010-09-04 23:28:191041#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant82894812010-09-22 16:48:341042 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161043 void merge(list&& __c) {merge(__c);}
1044#endif
1045 template <class _Comp>
1046 void merge(list& __c, _Comp __comp);
Howard Hinnant73d21a42010-09-04 23:28:191047#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:161048 template <class _Comp>
Howard Hinnant82894812010-09-22 16:48:341049 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161050 void merge(list&& __c, _Comp __comp) {merge(__c, __comp);}
Howard Hinnant73d21a42010-09-04 23:28:191051#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov9341a8a2016-04-22 01:04:551052 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161053 void sort();
1054 template <class _Comp>
Evgeniy Stepanov9341a8a2016-04-22 01:04:551055 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161056 void sort(_Comp __comp);
1057
Howard Hinnantc5607272011-06-03 17:30:281058 void reverse() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:161059
Howard Hinnant1c3ec6d2011-09-27 23:55:031060 bool __invariants() const;
1061
1062#if _LIBCPP_DEBUG_LEVEL >= 2
1063
1064 bool __dereferenceable(const const_iterator* __i) const;
1065 bool __decrementable(const const_iterator* __i) const;
1066 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1067 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1068
1069#endif // _LIBCPP_DEBUG_LEVEL >= 2
1070
Howard Hinnantbc8d3f92010-05-11 19:42:161071private:
Evgeniy Stepanov9341a8a2016-04-22 01:04:551072 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5c74b482015-12-30 20:57:591073 static void __link_nodes (__link_pointer __p, __link_pointer __f, __link_pointer __l);
Evgeniy Stepanov9341a8a2016-04-22 01:04:551074 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5c74b482015-12-30 20:57:591075 void __link_nodes_at_front(__link_pointer __f, __link_pointer __l);
Evgeniy Stepanov9341a8a2016-04-22 01:04:551076 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5c74b482015-12-30 20:57:591077 void __link_nodes_at_back (__link_pointer __f, __link_pointer __l);
Howard Hinnantbc8d3f92010-05-11 19:42:161078 iterator __iterator(size_type __n);
1079 template <class _Comp>
1080 static iterator __sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp);
1081
Howard Hinnantc5607272011-06-03 17:30:281082 void __move_assign(list& __c, true_type)
1083 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:161084 void __move_assign(list& __c, false_type);
1085};
1086
1087// Link in nodes [__f, __l] just prior to __p
1088template <class _Tp, class _Alloc>
Evgeniy Stepanov9341a8a2016-04-22 01:04:551089inline
Howard Hinnantbc8d3f92010-05-11 19:42:161090void
Eric Fiselier5c74b482015-12-30 20:57:591091list<_Tp, _Alloc>::__link_nodes(__link_pointer __p, __link_pointer __f, __link_pointer __l)
Howard Hinnantbc8d3f92010-05-11 19:42:161092{
Howard Hinnant29f74322013-06-25 16:08:471093 __p->__prev_->__next_ = __f;
1094 __f->__prev_ = __p->__prev_;
1095 __p->__prev_ = __l;
1096 __l->__next_ = __p;
Howard Hinnantbc8d3f92010-05-11 19:42:161097}
1098
Marshall Clowea8ed832014-08-05 01:34:121099// Link in nodes [__f, __l] at the front of the list
1100template <class _Tp, class _Alloc>
Evgeniy Stepanov9341a8a2016-04-22 01:04:551101inline
Marshall Clowea8ed832014-08-05 01:34:121102void
Eric Fiselier5c74b482015-12-30 20:57:591103list<_Tp, _Alloc>::__link_nodes_at_front(__link_pointer __f, __link_pointer __l)
Marshall Clowea8ed832014-08-05 01:34:121104{
Eric Fiselier8e7bd4f2016-01-04 03:27:521105 __f->__prev_ = base::__end_as_link();
Marshall Clowfca038e2014-08-08 15:35:521106 __l->__next_ = base::__end_.__next_;
1107 __l->__next_->__prev_ = __l;
1108 base::__end_.__next_ = __f;
Marshall Clowea8ed832014-08-05 01:34:121109}
1110
1111// Link in nodes [__f, __l] at the front of the list
1112template <class _Tp, class _Alloc>
Evgeniy Stepanov9341a8a2016-04-22 01:04:551113inline
Marshall Clowea8ed832014-08-05 01:34:121114void
Eric Fiselier5c74b482015-12-30 20:57:591115list<_Tp, _Alloc>::__link_nodes_at_back(__link_pointer __f, __link_pointer __l)
Marshall Clowea8ed832014-08-05 01:34:121116{
Eric Fiselier8e7bd4f2016-01-04 03:27:521117 __l->__next_ = base::__end_as_link();
Marshall Clowfca038e2014-08-08 15:35:521118 __f->__prev_ = base::__end_.__prev_;
1119 __f->__prev_->__next_ = __f;
1120 base::__end_.__prev_ = __l;
Marshall Clowea8ed832014-08-05 01:34:121121}
1122
1123
Howard Hinnantbc8d3f92010-05-11 19:42:161124template <class _Tp, class _Alloc>
Evgeniy Stepanov9341a8a2016-04-22 01:04:551125inline
Howard Hinnantbc8d3f92010-05-11 19:42:161126typename list<_Tp, _Alloc>::iterator
1127list<_Tp, _Alloc>::__iterator(size_type __n)
1128{
Howard Hinnant0949eed2011-06-30 21:18:191129 return __n <= base::__sz() / 2 ? _VSTD::next(begin(), __n)
1130 : _VSTD::prev(end(), base::__sz() - __n);
Howard Hinnantbc8d3f92010-05-11 19:42:161131}
1132
1133template <class _Tp, class _Alloc>
1134list<_Tp, _Alloc>::list(size_type __n)
1135{
Howard Hinnant1c3ec6d2011-09-27 23:55:031136#if _LIBCPP_DEBUG_LEVEL >= 2
1137 __get_db()->__insert_c(this);
1138#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161139 for (; __n > 0; --__n)
Howard Hinnant73d21a42010-09-04 23:28:191140#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:161141 emplace_back();
1142#else
1143 push_back(value_type());
1144#endif
1145}
1146
Marshall Clow955f2c82013-09-08 19:11:511147#if _LIBCPP_STD_VER > 11
1148template <class _Tp, class _Alloc>
1149list<_Tp, _Alloc>::list(size_type __n, const allocator_type& __a) : base(__a)
1150{
1151#if _LIBCPP_DEBUG_LEVEL >= 2
1152 __get_db()->__insert_c(this);
1153#endif
1154 for (; __n > 0; --__n)
1155#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1156 emplace_back();
1157#else
1158 push_back(value_type());
1159#endif
1160}
1161#endif
1162
Howard Hinnantbc8d3f92010-05-11 19:42:161163template <class _Tp, class _Alloc>
1164list<_Tp, _Alloc>::list(size_type __n, const value_type& __x)
1165{
Howard Hinnant1c3ec6d2011-09-27 23:55:031166#if _LIBCPP_DEBUG_LEVEL >= 2
1167 __get_db()->__insert_c(this);
1168#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161169 for (; __n > 0; --__n)
1170 push_back(__x);
1171}
1172
1173template <class _Tp, class _Alloc>
1174list<_Tp, _Alloc>::list(size_type __n, const value_type& __x, const allocator_type& __a)
1175 : base(__a)
1176{
Howard Hinnant1c3ec6d2011-09-27 23:55:031177#if _LIBCPP_DEBUG_LEVEL >= 2
1178 __get_db()->__insert_c(this);
1179#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161180 for (; __n > 0; --__n)
1181 push_back(__x);
1182}
1183
1184template <class _Tp, class _Alloc>
1185template <class _InpIter>
1186list<_Tp, _Alloc>::list(_InpIter __f, _InpIter __l,
1187 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1188{
Howard Hinnant1c3ec6d2011-09-27 23:55:031189#if _LIBCPP_DEBUG_LEVEL >= 2
1190 __get_db()->__insert_c(this);
1191#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161192 for (; __f != __l; ++__f)
1193 push_back(*__f);
1194}
1195
1196template <class _Tp, class _Alloc>
1197template <class _InpIter>
1198list<_Tp, _Alloc>::list(_InpIter __f, _InpIter __l, const allocator_type& __a,
1199 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1200 : base(__a)
1201{
Howard Hinnant1c3ec6d2011-09-27 23:55:031202#if _LIBCPP_DEBUG_LEVEL >= 2
1203 __get_db()->__insert_c(this);
1204#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161205 for (; __f != __l; ++__f)
1206 push_back(*__f);
1207}
1208
1209template <class _Tp, class _Alloc>
1210list<_Tp, _Alloc>::list(const list& __c)
1211 : base(allocator_type(
1212 __node_alloc_traits::select_on_container_copy_construction(
1213 __c.__node_alloc())))
1214{
Howard Hinnant1c3ec6d2011-09-27 23:55:031215#if _LIBCPP_DEBUG_LEVEL >= 2
1216 __get_db()->__insert_c(this);
1217#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161218 for (const_iterator __i = __c.begin(), __e = __c.end(); __i != __e; ++__i)
1219 push_back(*__i);
1220}
1221
1222template <class _Tp, class _Alloc>
1223list<_Tp, _Alloc>::list(const list& __c, const allocator_type& __a)
1224 : base(__a)
1225{
Howard Hinnant1c3ec6d2011-09-27 23:55:031226#if _LIBCPP_DEBUG_LEVEL >= 2
1227 __get_db()->__insert_c(this);
1228#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161229 for (const_iterator __i = __c.begin(), __e = __c.end(); __i != __e; ++__i)
1230 push_back(*__i);
1231}
1232
Howard Hinnante3e32912011-08-12 21:56:021233#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1234
Howard Hinnantbc8d3f92010-05-11 19:42:161235template <class _Tp, class _Alloc>
1236list<_Tp, _Alloc>::list(initializer_list<value_type> __il, const allocator_type& __a)
1237 : base(__a)
1238{
Howard Hinnant1c3ec6d2011-09-27 23:55:031239#if _LIBCPP_DEBUG_LEVEL >= 2
1240 __get_db()->__insert_c(this);
1241#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161242 for (typename initializer_list<value_type>::const_iterator __i = __il.begin(),
1243 __e = __il.end(); __i != __e; ++__i)
1244 push_back(*__i);
1245}
1246
1247template <class _Tp, class _Alloc>
1248list<_Tp, _Alloc>::list(initializer_list<value_type> __il)
1249{
Howard Hinnant1c3ec6d2011-09-27 23:55:031250#if _LIBCPP_DEBUG_LEVEL >= 2
1251 __get_db()->__insert_c(this);
1252#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161253 for (typename initializer_list<value_type>::const_iterator __i = __il.begin(),
1254 __e = __il.end(); __i != __e; ++__i)
1255 push_back(*__i);
1256}
1257
Howard Hinnante3e32912011-08-12 21:56:021258#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1259
Howard Hinnantbc8d3f92010-05-11 19:42:161260template <class _Tp, class _Alloc>
Evgeniy Stepanov9341a8a2016-04-22 01:04:551261inline
Howard Hinnantbc8d3f92010-05-11 19:42:161262list<_Tp, _Alloc>&
1263list<_Tp, _Alloc>::operator=(const list& __c)
1264{
1265 if (this != &__c)
1266 {
1267 base::__copy_assign_alloc(__c);
1268 assign(__c.begin(), __c.end());
1269 }
1270 return *this;
1271}
1272
Howard Hinnant73d21a42010-09-04 23:28:191273#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:161274
1275template <class _Tp, class _Alloc>
Evgeniy Stepanov9341a8a2016-04-22 01:04:551276inline
Howard Hinnantbc8d3f92010-05-11 19:42:161277list<_Tp, _Alloc>::list(list&& __c)
Howard Hinnantc5607272011-06-03 17:30:281278 _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value)
Howard Hinnant0949eed2011-06-30 21:18:191279 : base(allocator_type(_VSTD::move(__c.__node_alloc())))
Howard Hinnantbc8d3f92010-05-11 19:42:161280{
Howard Hinnant1c3ec6d2011-09-27 23:55:031281#if _LIBCPP_DEBUG_LEVEL >= 2
1282 __get_db()->__insert_c(this);
1283#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161284 splice(end(), __c);
1285}
1286
1287template <class _Tp, class _Alloc>
Evgeniy Stepanov9341a8a2016-04-22 01:04:551288inline
Howard Hinnantbc8d3f92010-05-11 19:42:161289list<_Tp, _Alloc>::list(list&& __c, const allocator_type& __a)
1290 : base(__a)
1291{
Howard Hinnant1c3ec6d2011-09-27 23:55:031292#if _LIBCPP_DEBUG_LEVEL >= 2
1293 __get_db()->__insert_c(this);
1294#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161295 if (__a == __c.get_allocator())
1296 splice(end(), __c);
1297 else
1298 {
Howard Hinnant99968442011-11-29 18:15:501299 typedef move_iterator<iterator> _Ip;
1300 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:161301 }
1302}
1303
1304template <class _Tp, class _Alloc>
Evgeniy Stepanov9341a8a2016-04-22 01:04:551305inline
Howard Hinnantbc8d3f92010-05-11 19:42:161306list<_Tp, _Alloc>&
1307list<_Tp, _Alloc>::operator=(list&& __c)
Howard Hinnantc5607272011-06-03 17:30:281308 _NOEXCEPT_(
1309 __node_alloc_traits::propagate_on_container_move_assignment::value &&
1310 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:161311{
1312 __move_assign(__c, integral_constant<bool,
1313 __node_alloc_traits::propagate_on_container_move_assignment::value>());
1314 return *this;
1315}
1316
1317template <class _Tp, class _Alloc>
1318void
1319list<_Tp, _Alloc>::__move_assign(list& __c, false_type)
1320{
1321 if (base::__node_alloc() != __c.__node_alloc())
1322 {
Howard Hinnant99968442011-11-29 18:15:501323 typedef move_iterator<iterator> _Ip;
1324 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:161325 }
1326 else
1327 __move_assign(__c, true_type());
1328}
1329
1330template <class _Tp, class _Alloc>
1331void
1332list<_Tp, _Alloc>::__move_assign(list& __c, true_type)
Howard Hinnantc5607272011-06-03 17:30:281333 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:161334{
1335 clear();
1336 base::__move_assign_alloc(__c);
1337 splice(end(), __c);
1338}
1339
Howard Hinnant73d21a42010-09-04 23:28:191340#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:161341
1342template <class _Tp, class _Alloc>
1343template <class _InpIter>
1344void
1345list<_Tp, _Alloc>::assign(_InpIter __f, _InpIter __l,
1346 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1347{
1348 iterator __i = begin();
1349 iterator __e = end();
1350 for (; __f != __l && __i != __e; ++__f, ++__i)
1351 *__i = *__f;
1352 if (__i == __e)
1353 insert(__e, __f, __l);
1354 else
1355 erase(__i, __e);
1356}
1357
1358template <class _Tp, class _Alloc>
1359void
1360list<_Tp, _Alloc>::assign(size_type __n, const value_type& __x)
1361{
1362 iterator __i = begin();
1363 iterator __e = end();
1364 for (; __n > 0 && __i != __e; --__n, ++__i)
1365 *__i = __x;
1366 if (__i == __e)
1367 insert(__e, __n, __x);
1368 else
1369 erase(__i, __e);
1370}
1371
1372template <class _Tp, class _Alloc>
Evgeniy Stepanov9341a8a2016-04-22 01:04:551373inline
Howard Hinnantbc8d3f92010-05-11 19:42:161374_Alloc
Howard Hinnantc5607272011-06-03 17:30:281375list<_Tp, _Alloc>::get_allocator() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161376{
1377 return allocator_type(base::__node_alloc());
1378}
1379
1380template <class _Tp, class _Alloc>
1381typename list<_Tp, _Alloc>::iterator
1382list<_Tp, _Alloc>::insert(const_iterator __p, const value_type& __x)
1383{
Howard Hinnant1c3ec6d2011-09-27 23:55:031384#if _LIBCPP_DEBUG_LEVEL >= 2
1385 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1386 "list::insert(iterator, x) called with an iterator not"
1387 " referring to this list");
1388#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161389 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:501390 typedef __allocator_destructor<__node_allocator> _Dp;
1391 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:161392 __hold->__prev_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:191393 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Eric Fiselier5c74b482015-12-30 20:57:591394 __link_nodes(__p.__ptr_, __hold->__as_link(), __hold->__as_link());
Howard Hinnantbc8d3f92010-05-11 19:42:161395 ++base::__sz();
Howard Hinnant79a35572013-04-05 00:18:491396#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselier5c74b482015-12-30 20:57:591397 return iterator(__hold.release()->__as_link(), this);
Howard Hinnant79a35572013-04-05 00:18:491398#else
Eric Fiselier5c74b482015-12-30 20:57:591399 return iterator(__hold.release()->__as_link());
Howard Hinnant79a35572013-04-05 00:18:491400#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161401}
1402
1403template <class _Tp, class _Alloc>
1404typename list<_Tp, _Alloc>::iterator
1405list<_Tp, _Alloc>::insert(const_iterator __p, size_type __n, const value_type& __x)
1406{
Howard Hinnant1c3ec6d2011-09-27 23:55:031407#if _LIBCPP_DEBUG_LEVEL >= 2
1408 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1409 "list::insert(iterator, n, x) called with an iterator not"
1410 " referring to this list");
Howard Hinnant29f74322013-06-25 16:08:471411 iterator __r(__p.__ptr_, this);
Howard Hinnant1c3ec6d2011-09-27 23:55:031412#else
Howard Hinnant29f74322013-06-25 16:08:471413 iterator __r(__p.__ptr_);
Howard Hinnant1c3ec6d2011-09-27 23:55:031414#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161415 if (__n > 0)
1416 {
1417 size_type __ds = 0;
1418 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:501419 typedef __allocator_destructor<__node_allocator> _Dp;
1420 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:161421 __hold->__prev_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:191422 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:161423 ++__ds;
Howard Hinnant1c3ec6d2011-09-27 23:55:031424#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselier5c74b482015-12-30 20:57:591425 __r = iterator(__hold->__as_link(), this);
Howard Hinnant1c3ec6d2011-09-27 23:55:031426#else
Eric Fiselier5c74b482015-12-30 20:57:591427 __r = iterator(__hold->__as_link());
Howard Hinnant1c3ec6d2011-09-27 23:55:031428#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161429 __hold.release();
1430 iterator __e = __r;
1431#ifndef _LIBCPP_NO_EXCEPTIONS
1432 try
1433 {
Howard Hinnant324bb032010-08-22 00:02:431434#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:161435 for (--__n; __n != 0; --__n, ++__e, ++__ds)
1436 {
1437 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:191438 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Eric Fiselier5c74b482015-12-30 20:57:591439 __e.__ptr_->__next_ = __hold->__as_link();
Howard Hinnantbc8d3f92010-05-11 19:42:161440 __hold->__prev_ = __e.__ptr_;
1441 __hold.release();
1442 }
1443#ifndef _LIBCPP_NO_EXCEPTIONS
1444 }
1445 catch (...)
1446 {
1447 while (true)
1448 {
Howard Hinnant0949eed2011-06-30 21:18:191449 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Eric Fiselier5c74b482015-12-30 20:57:591450 __link_pointer __prev = __e.__ptr_->__prev_;
1451 __node_alloc_traits::deallocate(__na, __e.__ptr_->__as_node(), 1);
Howard Hinnantbc8d3f92010-05-11 19:42:161452 if (__prev == 0)
1453 break;
Howard Hinnant1c3ec6d2011-09-27 23:55:031454#if _LIBCPP_DEBUG_LEVEL >= 2
1455 __e = iterator(__prev, this);
1456#else
Howard Hinnantbc8d3f92010-05-11 19:42:161457 __e = iterator(__prev);
Howard Hinnant1c3ec6d2011-09-27 23:55:031458#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161459 }
1460 throw;
1461 }
Howard Hinnant324bb032010-08-22 00:02:431462#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant29f74322013-06-25 16:08:471463 __link_nodes(__p.__ptr_, __r.__ptr_, __e.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:161464 base::__sz() += __ds;
1465 }
1466 return __r;
1467}
1468
1469template <class _Tp, class _Alloc>
1470template <class _InpIter>
1471typename list<_Tp, _Alloc>::iterator
1472list<_Tp, _Alloc>::insert(const_iterator __p, _InpIter __f, _InpIter __l,
1473 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1474{
Howard Hinnant1c3ec6d2011-09-27 23:55:031475#if _LIBCPP_DEBUG_LEVEL >= 2
1476 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1477 "list::insert(iterator, range) called with an iterator not"
1478 " referring to this list");
Howard Hinnant29f74322013-06-25 16:08:471479 iterator __r(__p.__ptr_, this);
Howard Hinnant1c3ec6d2011-09-27 23:55:031480#else
Howard Hinnant29f74322013-06-25 16:08:471481 iterator __r(__p.__ptr_);
Howard Hinnant1c3ec6d2011-09-27 23:55:031482#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161483 if (__f != __l)
1484 {
1485 size_type __ds = 0;
1486 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:501487 typedef __allocator_destructor<__node_allocator> _Dp;
1488 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:161489 __hold->__prev_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:191490 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), *__f);
Howard Hinnantbc8d3f92010-05-11 19:42:161491 ++__ds;
Howard Hinnant1c3ec6d2011-09-27 23:55:031492#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselier5c74b482015-12-30 20:57:591493 __r = iterator(__hold.get()->__as_link(), this);
Howard Hinnant1c3ec6d2011-09-27 23:55:031494#else
Eric Fiselier5c74b482015-12-30 20:57:591495 __r = iterator(__hold.get()->__as_link());
Howard Hinnant1c3ec6d2011-09-27 23:55:031496#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161497 __hold.release();
1498 iterator __e = __r;
1499#ifndef _LIBCPP_NO_EXCEPTIONS
1500 try
1501 {
Howard Hinnant324bb032010-08-22 00:02:431502#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiselier537876b2015-03-19 03:20:021503 for (++__f; __f != __l; ++__f, (void) ++__e, (void) ++__ds)
Howard Hinnantbc8d3f92010-05-11 19:42:161504 {
1505 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:191506 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), *__f);
Eric Fiselier5c74b482015-12-30 20:57:591507 __e.__ptr_->__next_ = __hold.get()->__as_link();
Howard Hinnantbc8d3f92010-05-11 19:42:161508 __hold->__prev_ = __e.__ptr_;
1509 __hold.release();
1510 }
1511#ifndef _LIBCPP_NO_EXCEPTIONS
1512 }
1513 catch (...)
1514 {
1515 while (true)
1516 {
Howard Hinnant0949eed2011-06-30 21:18:191517 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Eric Fiselier5c74b482015-12-30 20:57:591518 __link_pointer __prev = __e.__ptr_->__prev_;
1519 __node_alloc_traits::deallocate(__na, __e.__ptr_->__as_node(), 1);
Howard Hinnantbc8d3f92010-05-11 19:42:161520 if (__prev == 0)
1521 break;
Howard Hinnant1c3ec6d2011-09-27 23:55:031522#if _LIBCPP_DEBUG_LEVEL >= 2
1523 __e = iterator(__prev, this);
1524#else
Howard Hinnantbc8d3f92010-05-11 19:42:161525 __e = iterator(__prev);
Howard Hinnant1c3ec6d2011-09-27 23:55:031526#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161527 }
1528 throw;
1529 }
Howard Hinnant324bb032010-08-22 00:02:431530#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant29f74322013-06-25 16:08:471531 __link_nodes(__p.__ptr_, __r.__ptr_, __e.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:161532 base::__sz() += __ds;
1533 }
1534 return __r;
1535}
1536
1537template <class _Tp, class _Alloc>
1538void
1539list<_Tp, _Alloc>::push_front(const value_type& __x)
1540{
1541 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:501542 typedef __allocator_destructor<__node_allocator> _Dp;
1543 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:191544 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Eric Fiselier5c74b482015-12-30 20:57:591545 __link_pointer __nl = __hold->__as_link();
1546 __link_nodes_at_front(__nl, __nl);
Howard Hinnantbc8d3f92010-05-11 19:42:161547 ++base::__sz();
1548 __hold.release();
1549}
1550
1551template <class _Tp, class _Alloc>
1552void
1553list<_Tp, _Alloc>::push_back(const value_type& __x)
1554{
1555 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:501556 typedef __allocator_destructor<__node_allocator> _Dp;
1557 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:191558 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Eric Fiselier5c74b482015-12-30 20:57:591559 __link_nodes_at_back(__hold.get()->__as_link(), __hold.get()->__as_link());
Howard Hinnantbc8d3f92010-05-11 19:42:161560 ++base::__sz();
1561 __hold.release();
1562}
1563
Howard Hinnant73d21a42010-09-04 23:28:191564#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:161565
1566template <class _Tp, class _Alloc>
1567void
1568list<_Tp, _Alloc>::push_front(value_type&& __x)
1569{
1570 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:501571 typedef __allocator_destructor<__node_allocator> _Dp;
1572 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:191573 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
Eric Fiselier5c74b482015-12-30 20:57:591574 __link_nodes_at_front(__hold.get()->__as_link(), __hold.get()->__as_link());
Howard Hinnantbc8d3f92010-05-11 19:42:161575 ++base::__sz();
1576 __hold.release();
1577}
1578
1579template <class _Tp, class _Alloc>
1580void
1581list<_Tp, _Alloc>::push_back(value_type&& __x)
1582{
1583 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:501584 typedef __allocator_destructor<__node_allocator> _Dp;
1585 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:191586 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
Eric Fiselier5c74b482015-12-30 20:57:591587 __link_nodes_at_back(__hold.get()->__as_link(), __hold.get()->__as_link());
Howard Hinnantbc8d3f92010-05-11 19:42:161588 ++base::__sz();
1589 __hold.release();
1590}
1591
Howard Hinnant73d21a42010-09-04 23:28:191592#ifndef _LIBCPP_HAS_NO_VARIADICS
1593
Howard Hinnantbc8d3f92010-05-11 19:42:161594template <class _Tp, class _Alloc>
1595template <class... _Args>
1596void
1597list<_Tp, _Alloc>::emplace_front(_Args&&... __args)
1598{
1599 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:501600 typedef __allocator_destructor<__node_allocator> _Dp;
1601 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:191602 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
Eric Fiselier5c74b482015-12-30 20:57:591603 __link_nodes_at_front(__hold.get()->__as_link(), __hold.get()->__as_link());
Howard Hinnantbc8d3f92010-05-11 19:42:161604 ++base::__sz();
1605 __hold.release();
1606}
1607
1608template <class _Tp, class _Alloc>
1609template <class... _Args>
1610void
1611list<_Tp, _Alloc>::emplace_back(_Args&&... __args)
1612{
1613 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:501614 typedef __allocator_destructor<__node_allocator> _Dp;
1615 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:191616 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
Eric Fiselier5c74b482015-12-30 20:57:591617 __link_pointer __nl = __hold->__as_link();
1618 __link_nodes_at_back(__nl, __nl);
Howard Hinnantbc8d3f92010-05-11 19:42:161619 ++base::__sz();
1620 __hold.release();
1621}
1622
1623template <class _Tp, class _Alloc>
1624template <class... _Args>
1625typename list<_Tp, _Alloc>::iterator
1626list<_Tp, _Alloc>::emplace(const_iterator __p, _Args&&... __args)
1627{
Howard Hinnant79a35572013-04-05 00:18:491628#if _LIBCPP_DEBUG_LEVEL >= 2
1629 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1630 "list::emplace(iterator, args...) called with an iterator not"
1631 " referring to this list");
1632#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161633 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:501634 typedef __allocator_destructor<__node_allocator> _Dp;
1635 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:161636 __hold->__prev_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:191637 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
Eric Fiselier5c74b482015-12-30 20:57:591638 __link_pointer __nl = __hold.get()->__as_link();
1639 __link_nodes(__p.__ptr_, __nl, __nl);
Howard Hinnantbc8d3f92010-05-11 19:42:161640 ++base::__sz();
Eric Fiselier5c74b482015-12-30 20:57:591641 __hold.release();
Howard Hinnant1c3ec6d2011-09-27 23:55:031642#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselier5c74b482015-12-30 20:57:591643 return iterator(__nl, this);
Howard Hinnant1c3ec6d2011-09-27 23:55:031644#else
Eric Fiselier5c74b482015-12-30 20:57:591645 return iterator(__nl);
Howard Hinnant1c3ec6d2011-09-27 23:55:031646#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161647}
1648
Howard Hinnant73d21a42010-09-04 23:28:191649#endif // _LIBCPP_HAS_NO_VARIADICS
1650
Howard Hinnantbc8d3f92010-05-11 19:42:161651template <class _Tp, class _Alloc>
1652typename list<_Tp, _Alloc>::iterator
1653list<_Tp, _Alloc>::insert(const_iterator __p, value_type&& __x)
1654{
Howard Hinnant1c3ec6d2011-09-27 23:55:031655#if _LIBCPP_DEBUG_LEVEL >= 2
1656 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1657 "list::insert(iterator, x) called with an iterator not"
1658 " referring to this list");
1659#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161660 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:501661 typedef __allocator_destructor<__node_allocator> _Dp;
1662 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:161663 __hold->__prev_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:191664 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
Eric Fiselier5c74b482015-12-30 20:57:591665 __link_pointer __nl = __hold->__as_link();
1666 __link_nodes(__p.__ptr_, __nl, __nl);
Howard Hinnantbc8d3f92010-05-11 19:42:161667 ++base::__sz();
Eric Fiselier5c74b482015-12-30 20:57:591668 __hold.release();
Howard Hinnant1c3ec6d2011-09-27 23:55:031669#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselier5c74b482015-12-30 20:57:591670 return iterator(__nl, this);
Howard Hinnant1c3ec6d2011-09-27 23:55:031671#else
Eric Fiselier5c74b482015-12-30 20:57:591672 return iterator(__nl);
Howard Hinnant1c3ec6d2011-09-27 23:55:031673#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161674}
1675
Howard Hinnant73d21a42010-09-04 23:28:191676#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:161677
1678template <class _Tp, class _Alloc>
1679void
1680list<_Tp, _Alloc>::pop_front()
1681{
Howard Hinnant1c3ec6d2011-09-27 23:55:031682 _LIBCPP_ASSERT(!empty(), "list::pop_front() called with empty list");
Howard Hinnantbc8d3f92010-05-11 19:42:161683 __node_allocator& __na = base::__node_alloc();
Eric Fiselier5c74b482015-12-30 20:57:591684 __link_pointer __n = base::__end_.__next_;
Howard Hinnantbc8d3f92010-05-11 19:42:161685 base::__unlink_nodes(__n, __n);
1686 --base::__sz();
Howard Hinnant1c3ec6d2011-09-27 23:55:031687#if _LIBCPP_DEBUG_LEVEL >= 2
1688 __c_node* __c = __get_db()->__find_c_and_lock(this);
1689 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1690 {
1691 --__p;
1692 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant29f74322013-06-25 16:08:471693 if (__i->__ptr_ == __n)
Howard Hinnant1c3ec6d2011-09-27 23:55:031694 {
1695 (*__p)->__c_ = nullptr;
1696 if (--__c->end_ != __p)
1697 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1698 }
1699 }
1700 __get_db()->unlock();
1701#endif
Eric Fiselier5c74b482015-12-30 20:57:591702 __node_pointer __np = __n->__as_node();
1703 __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
1704 __node_alloc_traits::deallocate(__na, __np, 1);
Howard Hinnantbc8d3f92010-05-11 19:42:161705}
1706
1707template <class _Tp, class _Alloc>
1708void
1709list<_Tp, _Alloc>::pop_back()
1710{
Dmitri Gribenkoc7a39cf2013-06-24 06:15:571711 _LIBCPP_ASSERT(!empty(), "list::pop_back() called with empty list");
Howard Hinnantbc8d3f92010-05-11 19:42:161712 __node_allocator& __na = base::__node_alloc();
Eric Fiselier5c74b482015-12-30 20:57:591713 __link_pointer __n = base::__end_.__prev_;
Howard Hinnantbc8d3f92010-05-11 19:42:161714 base::__unlink_nodes(__n, __n);
1715 --base::__sz();
Howard Hinnant1c3ec6d2011-09-27 23:55:031716#if _LIBCPP_DEBUG_LEVEL >= 2
1717 __c_node* __c = __get_db()->__find_c_and_lock(this);
1718 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1719 {
1720 --__p;
1721 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant29f74322013-06-25 16:08:471722 if (__i->__ptr_ == __n)
Howard Hinnant1c3ec6d2011-09-27 23:55:031723 {
1724 (*__p)->__c_ = nullptr;
1725 if (--__c->end_ != __p)
1726 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1727 }
1728 }
1729 __get_db()->unlock();
1730#endif
Eric Fiselier5c74b482015-12-30 20:57:591731 __node_pointer __np = __n->__as_node();
1732 __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
1733 __node_alloc_traits::deallocate(__na, __np, 1);
Howard Hinnantbc8d3f92010-05-11 19:42:161734}
1735
1736template <class _Tp, class _Alloc>
1737typename list<_Tp, _Alloc>::iterator
1738list<_Tp, _Alloc>::erase(const_iterator __p)
1739{
Howard Hinnant1c3ec6d2011-09-27 23:55:031740#if _LIBCPP_DEBUG_LEVEL >= 2
1741 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1742 "list::erase(iterator) called with an iterator not"
1743 " referring to this list");
1744#endif
Howard Hinnant79a35572013-04-05 00:18:491745 _LIBCPP_ASSERT(__p != end(),
1746 "list::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnantbc8d3f92010-05-11 19:42:161747 __node_allocator& __na = base::__node_alloc();
Eric Fiselier5c74b482015-12-30 20:57:591748 __link_pointer __n = __p.__ptr_;
1749 __link_pointer __r = __n->__next_;
Howard Hinnantbc8d3f92010-05-11 19:42:161750 base::__unlink_nodes(__n, __n);
1751 --base::__sz();
Howard Hinnant1c3ec6d2011-09-27 23:55:031752#if _LIBCPP_DEBUG_LEVEL >= 2
1753 __c_node* __c = __get_db()->__find_c_and_lock(this);
1754 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1755 {
1756 --__p;
1757 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant29f74322013-06-25 16:08:471758 if (__i->__ptr_ == __n)
Howard Hinnant1c3ec6d2011-09-27 23:55:031759 {
1760 (*__p)->__c_ = nullptr;
1761 if (--__c->end_ != __p)
1762 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1763 }
1764 }
1765 __get_db()->unlock();
1766#endif
Eric Fiselier5c74b482015-12-30 20:57:591767 __node_pointer __np = __n->__as_node();
1768 __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
1769 __node_alloc_traits::deallocate(__na, __np, 1);
Howard Hinnant1c3ec6d2011-09-27 23:55:031770#if _LIBCPP_DEBUG_LEVEL >= 2
1771 return iterator(__r, this);
1772#else
Howard Hinnantbc8d3f92010-05-11 19:42:161773 return iterator(__r);
Howard Hinnant1c3ec6d2011-09-27 23:55:031774#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161775}
1776
1777template <class _Tp, class _Alloc>
1778typename list<_Tp, _Alloc>::iterator
1779list<_Tp, _Alloc>::erase(const_iterator __f, const_iterator __l)
1780{
Howard Hinnant1c3ec6d2011-09-27 23:55:031781#if _LIBCPP_DEBUG_LEVEL >= 2
1782 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__f) == this,
1783 "list::erase(iterator, iterator) called with an iterator not"
1784 " referring to this list");
1785#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161786 if (__f != __l)
1787 {
1788 __node_allocator& __na = base::__node_alloc();
Howard Hinnant29f74322013-06-25 16:08:471789 base::__unlink_nodes(__f.__ptr_, __l.__ptr_->__prev_);
Howard Hinnantbc8d3f92010-05-11 19:42:161790 while (__f != __l)
1791 {
Eric Fiselier5c74b482015-12-30 20:57:591792 __link_pointer __n = __f.__ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:161793 ++__f;
1794 --base::__sz();
Howard Hinnant1c3ec6d2011-09-27 23:55:031795#if _LIBCPP_DEBUG_LEVEL >= 2
1796 __c_node* __c = __get_db()->__find_c_and_lock(this);
1797 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1798 {
1799 --__p;
1800 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant29f74322013-06-25 16:08:471801 if (__i->__ptr_ == __n)
Howard Hinnant1c3ec6d2011-09-27 23:55:031802 {
1803 (*__p)->__c_ = nullptr;
1804 if (--__c->end_ != __p)
1805 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1806 }
1807 }
1808 __get_db()->unlock();
1809#endif
Eric Fiselier5c74b482015-12-30 20:57:591810 __node_pointer __np = __n->__as_node();
1811 __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
1812 __node_alloc_traits::deallocate(__na, __np, 1);
Howard Hinnantbc8d3f92010-05-11 19:42:161813 }
1814 }
Howard Hinnant1c3ec6d2011-09-27 23:55:031815#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant29f74322013-06-25 16:08:471816 return iterator(__l.__ptr_, this);
Howard Hinnant1c3ec6d2011-09-27 23:55:031817#else
Howard Hinnant29f74322013-06-25 16:08:471818 return iterator(__l.__ptr_);
Howard Hinnant1c3ec6d2011-09-27 23:55:031819#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161820}
1821
1822template <class _Tp, class _Alloc>
1823void
1824list<_Tp, _Alloc>::resize(size_type __n)
1825{
1826 if (__n < base::__sz())
1827 erase(__iterator(__n), end());
1828 else if (__n > base::__sz())
1829 {
1830 __n -= base::__sz();
1831 size_type __ds = 0;
1832 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:501833 typedef __allocator_destructor<__node_allocator> _Dp;
1834 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:161835 __hold->__prev_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:191836 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:161837 ++__ds;
Howard Hinnant1c3ec6d2011-09-27 23:55:031838#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselier5c74b482015-12-30 20:57:591839 iterator __r = iterator(__hold.release()->__as_link(), this);
Howard Hinnant1c3ec6d2011-09-27 23:55:031840#else
Eric Fiselier5c74b482015-12-30 20:57:591841 iterator __r = iterator(__hold.release()->__as_link());
Howard Hinnant1c3ec6d2011-09-27 23:55:031842#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161843 iterator __e = __r;
1844#ifndef _LIBCPP_NO_EXCEPTIONS
1845 try
1846 {
Howard Hinnant324bb032010-08-22 00:02:431847#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:161848 for (--__n; __n != 0; --__n, ++__e, ++__ds)
1849 {
1850 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:191851 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_));
Eric Fiselier5c74b482015-12-30 20:57:591852 __e.__ptr_->__next_ = __hold.get()->__as_link();
Howard Hinnantbc8d3f92010-05-11 19:42:161853 __hold->__prev_ = __e.__ptr_;
1854 __hold.release();
1855 }
1856#ifndef _LIBCPP_NO_EXCEPTIONS
1857 }
1858 catch (...)
1859 {
1860 while (true)
1861 {
Howard Hinnant0949eed2011-06-30 21:18:191862 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Eric Fiselier5c74b482015-12-30 20:57:591863 __link_pointer __prev = __e.__ptr_->__prev_;
1864 __node_alloc_traits::deallocate(__na, __e.__ptr_->__as_node(), 1);
Howard Hinnantbc8d3f92010-05-11 19:42:161865 if (__prev == 0)
1866 break;
Howard Hinnant1c3ec6d2011-09-27 23:55:031867#if _LIBCPP_DEBUG_LEVEL >= 2
1868 __e = iterator(__prev, this);
1869#else
Howard Hinnantbc8d3f92010-05-11 19:42:161870 __e = iterator(__prev);
Howard Hinnant1c3ec6d2011-09-27 23:55:031871#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161872 }
1873 throw;
1874 }
Howard Hinnant324bb032010-08-22 00:02:431875#endif // _LIBCPP_NO_EXCEPTIONS
Marshall Clowea8ed832014-08-05 01:34:121876 __link_nodes_at_back(__r.__ptr_, __e.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:161877 base::__sz() += __ds;
1878 }
1879}
1880
1881template <class _Tp, class _Alloc>
1882void
1883list<_Tp, _Alloc>::resize(size_type __n, const value_type& __x)
1884{
1885 if (__n < base::__sz())
1886 erase(__iterator(__n), end());
1887 else if (__n > base::__sz())
1888 {
1889 __n -= base::__sz();
1890 size_type __ds = 0;
1891 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:501892 typedef __allocator_destructor<__node_allocator> _Dp;
1893 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:161894 __hold->__prev_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:191895 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:161896 ++__ds;
Eric Fiselier5c74b482015-12-30 20:57:591897 __link_pointer __nl = __hold.release()->__as_link();
Howard Hinnant1c3ec6d2011-09-27 23:55:031898#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselier5c74b482015-12-30 20:57:591899 iterator __r = iterator(__nl, this);
Howard Hinnant1c3ec6d2011-09-27 23:55:031900#else
Eric Fiselier5c74b482015-12-30 20:57:591901 iterator __r = iterator(__nl);
Howard Hinnant1c3ec6d2011-09-27 23:55:031902#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161903 iterator __e = __r;
1904#ifndef _LIBCPP_NO_EXCEPTIONS
1905 try
1906 {
Howard Hinnant324bb032010-08-22 00:02:431907#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:161908 for (--__n; __n != 0; --__n, ++__e, ++__ds)
1909 {
1910 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:191911 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Eric Fiselier5c74b482015-12-30 20:57:591912 __e.__ptr_->__next_ = __hold.get()->__as_link();
Howard Hinnantbc8d3f92010-05-11 19:42:161913 __hold->__prev_ = __e.__ptr_;
1914 __hold.release();
1915 }
1916#ifndef _LIBCPP_NO_EXCEPTIONS
1917 }
1918 catch (...)
1919 {
1920 while (true)
1921 {
Howard Hinnant0949eed2011-06-30 21:18:191922 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Eric Fiselier5c74b482015-12-30 20:57:591923 __link_pointer __prev = __e.__ptr_->__prev_;
1924 __node_alloc_traits::deallocate(__na, __e.__ptr_->__as_node(), 1);
Howard Hinnantbc8d3f92010-05-11 19:42:161925 if (__prev == 0)
1926 break;
Howard Hinnant1c3ec6d2011-09-27 23:55:031927#if _LIBCPP_DEBUG_LEVEL >= 2
1928 __e = iterator(__prev, this);
1929#else
Howard Hinnantbc8d3f92010-05-11 19:42:161930 __e = iterator(__prev);
Howard Hinnant1c3ec6d2011-09-27 23:55:031931#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161932 }
1933 throw;
1934 }
Howard Hinnant324bb032010-08-22 00:02:431935#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiselier8e7bd4f2016-01-04 03:27:521936 __link_nodes(base::__end_as_link(), __r.__ptr_, __e.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:161937 base::__sz() += __ds;
Howard Hinnant324bb032010-08-22 00:02:431938 }
Howard Hinnantbc8d3f92010-05-11 19:42:161939}
1940
1941template <class _Tp, class _Alloc>
1942void
1943list<_Tp, _Alloc>::splice(const_iterator __p, list& __c)
1944{
Howard Hinnant1c3ec6d2011-09-27 23:55:031945 _LIBCPP_ASSERT(this != &__c,
1946 "list::splice(iterator, list) called with this == &list");
1947#if _LIBCPP_DEBUG_LEVEL >= 2
1948 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1949 "list::splice(iterator, list) called with an iterator not"
1950 " referring to this list");
1951#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161952 if (!__c.empty())
1953 {
Eric Fiselier5c74b482015-12-30 20:57:591954 __link_pointer __f = __c.__end_.__next_;
1955 __link_pointer __l = __c.__end_.__prev_;
Howard Hinnantbc8d3f92010-05-11 19:42:161956 base::__unlink_nodes(__f, __l);
Howard Hinnant29f74322013-06-25 16:08:471957 __link_nodes(__p.__ptr_, __f, __l);
Howard Hinnantbc8d3f92010-05-11 19:42:161958 base::__sz() += __c.__sz();
1959 __c.__sz() = 0;
Howard Hinnant1c3ec6d2011-09-27 23:55:031960#if _LIBCPP_DEBUG_LEVEL >= 2
1961 __libcpp_db* __db = __get_db();
1962 __c_node* __cn1 = __db->__find_c_and_lock(this);
1963 __c_node* __cn2 = __db->__find_c(&__c);
1964 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
1965 {
1966 --__p;
1967 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Eric Fiselier8e7bd4f2016-01-04 03:27:521968 if (__i->__ptr_ != __c.__end_as_link())
Howard Hinnant1c3ec6d2011-09-27 23:55:031969 {
1970 __cn1->__add(*__p);
1971 (*__p)->__c_ = __cn1;
1972 if (--__cn2->end_ != __p)
1973 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
1974 }
1975 }
1976 __db->unlock();
1977#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161978 }
1979}
1980
1981template <class _Tp, class _Alloc>
1982void
1983list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __i)
1984{
Howard Hinnant1c3ec6d2011-09-27 23:55:031985#if _LIBCPP_DEBUG_LEVEL >= 2
1986 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1987 "list::splice(iterator, list, iterator) called with first iterator not"
1988 " referring to this list");
1989 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__i) == &__c,
1990 "list::splice(iterator, list, iterator) called with second iterator not"
1991 " referring to list argument");
1992 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(&__i),
1993 "list::splice(iterator, list, iterator) called with second iterator not"
1994 " derefereceable");
1995#endif
1996 if (__p.__ptr_ != __i.__ptr_ && __p.__ptr_ != __i.__ptr_->__next_)
Howard Hinnantbc8d3f92010-05-11 19:42:161997 {
Eric Fiselier5c74b482015-12-30 20:57:591998 __link_pointer __f = __i.__ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:161999 base::__unlink_nodes(__f, __f);
Howard Hinnant29f74322013-06-25 16:08:472000 __link_nodes(__p.__ptr_, __f, __f);
Howard Hinnantbc8d3f92010-05-11 19:42:162001 --__c.__sz();
2002 ++base::__sz();
Howard Hinnant1c3ec6d2011-09-27 23:55:032003#if _LIBCPP_DEBUG_LEVEL >= 2
2004 __libcpp_db* __db = __get_db();
2005 __c_node* __cn1 = __db->__find_c_and_lock(this);
2006 __c_node* __cn2 = __db->__find_c(&__c);
2007 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
2008 {
2009 --__p;
2010 iterator* __j = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant29f74322013-06-25 16:08:472011 if (__j->__ptr_ == __f)
Howard Hinnant1c3ec6d2011-09-27 23:55:032012 {
2013 __cn1->__add(*__p);
2014 (*__p)->__c_ = __cn1;
2015 if (--__cn2->end_ != __p)
2016 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
2017 }
2018 }
2019 __db->unlock();
2020#endif
Howard Hinnantbc8d3f92010-05-11 19:42:162021 }
2022}
2023
2024template <class _Tp, class _Alloc>
2025void
2026list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l)
2027{
Howard Hinnant1c3ec6d2011-09-27 23:55:032028#if _LIBCPP_DEBUG_LEVEL >= 2
2029 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2030 "list::splice(iterator, list, iterator, iterator) called with first iterator not"
2031 " referring to this list");
2032 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__f) == &__c,
2033 "list::splice(iterator, list, iterator, iterator) called with second iterator not"
2034 " referring to list argument");
2035 if (this == &__c)
2036 {
2037 for (const_iterator __i = __f; __i != __l; ++__i)
2038 _LIBCPP_ASSERT(__i != __p,
2039 "list::splice(iterator, list, iterator, iterator)"
2040 " called with the first iterator within the range"
2041 " of the second and third iterators");
2042 }
2043#endif
Howard Hinnantbc8d3f92010-05-11 19:42:162044 if (__f != __l)
2045 {
2046 if (this != &__c)
2047 {
Howard Hinnant0949eed2011-06-30 21:18:192048 size_type __s = _VSTD::distance(__f, __l);
Howard Hinnantbc8d3f92010-05-11 19:42:162049 __c.__sz() -= __s;
2050 base::__sz() += __s;
2051 }
Eric Fiselier5c74b482015-12-30 20:57:592052 __link_pointer __first = __f.__ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:162053 --__l;
Eric Fiselier5c74b482015-12-30 20:57:592054 __link_pointer __last = __l.__ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:162055 base::__unlink_nodes(__first, __last);
Howard Hinnant29f74322013-06-25 16:08:472056 __link_nodes(__p.__ptr_, __first, __last);
Howard Hinnant1c3ec6d2011-09-27 23:55:032057#if _LIBCPP_DEBUG_LEVEL >= 2
2058 __libcpp_db* __db = __get_db();
2059 __c_node* __cn1 = __db->__find_c_and_lock(this);
2060 __c_node* __cn2 = __db->__find_c(&__c);
2061 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
2062 {
2063 --__p;
2064 iterator* __j = static_cast<iterator*>((*__p)->__i_);
Eric Fiselier5c74b482015-12-30 20:57:592065 for (__link_pointer __k = __f.__ptr_;
Howard Hinnant1c3ec6d2011-09-27 23:55:032066 __k != __l.__ptr_; __k = __k->__next_)
2067 {
2068 if (__j->__ptr_ == __k)
2069 {
2070 __cn1->__add(*__p);
2071 (*__p)->__c_ = __cn1;
2072 if (--__cn2->end_ != __p)
2073 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
2074 }
2075 }
2076 }
2077 __db->unlock();
2078#endif
Howard Hinnantbc8d3f92010-05-11 19:42:162079 }
2080}
2081
2082template <class _Tp, class _Alloc>
2083void
2084list<_Tp, _Alloc>::remove(const value_type& __x)
2085{
Marshall Clowfca038e2014-08-08 15:35:522086 list<_Tp, _Alloc> __deleted_nodes; // collect the nodes we're removing
2087 for (const_iterator __i = begin(), __e = end(); __i != __e;)
Howard Hinnantbc8d3f92010-05-11 19:42:162088 {
2089 if (*__i == __x)
2090 {
Marshall Clowfca038e2014-08-08 15:35:522091 const_iterator __j = _VSTD::next(__i);
Howard Hinnantbc8d3f92010-05-11 19:42:162092 for (; __j != __e && *__j == __x; ++__j)
2093 ;
Marshall Clowfca038e2014-08-08 15:35:522094 __deleted_nodes.splice(__deleted_nodes.end(), *this, __i, __j);
2095 __i = __j;
Marshall Clowf0f1bca2014-08-04 17:32:252096 if (__i != __e)
Marshall Clowea8ed832014-08-05 01:34:122097 ++__i;
Howard Hinnantbc8d3f92010-05-11 19:42:162098 }
2099 else
2100 ++__i;
2101 }
2102}
2103
2104template <class _Tp, class _Alloc>
2105template <class _Pred>
2106void
2107list<_Tp, _Alloc>::remove_if(_Pred __pred)
2108{
2109 for (iterator __i = begin(), __e = end(); __i != __e;)
2110 {
2111 if (__pred(*__i))
2112 {
Howard Hinnant0949eed2011-06-30 21:18:192113 iterator __j = _VSTD::next(__i);
Howard Hinnantbc8d3f92010-05-11 19:42:162114 for (; __j != __e && __pred(*__j); ++__j)
2115 ;
2116 __i = erase(__i, __j);
Marshall Clowf0f1bca2014-08-04 17:32:252117 if (__i != __e)
Marshall Clowea8ed832014-08-05 01:34:122118 ++__i;
Howard Hinnantbc8d3f92010-05-11 19:42:162119 }
2120 else
2121 ++__i;
2122 }
2123}
2124
2125template <class _Tp, class _Alloc>
Evgeniy Stepanov9341a8a2016-04-22 01:04:552126inline
Howard Hinnantbc8d3f92010-05-11 19:42:162127void
2128list<_Tp, _Alloc>::unique()
2129{
2130 unique(__equal_to<value_type>());
2131}
2132
2133template <class _Tp, class _Alloc>
2134template <class _BinaryPred>
2135void
2136list<_Tp, _Alloc>::unique(_BinaryPred __binary_pred)
2137{
2138 for (iterator __i = begin(), __e = end(); __i != __e;)
2139 {
Howard Hinnant0949eed2011-06-30 21:18:192140 iterator __j = _VSTD::next(__i);
Howard Hinnantbc8d3f92010-05-11 19:42:162141 for (; __j != __e && __binary_pred(*__i, *__j); ++__j)
2142 ;
2143 if (++__i != __j)
2144 __i = erase(__i, __j);
2145 }
2146}
2147
2148template <class _Tp, class _Alloc>
Evgeniy Stepanov9341a8a2016-04-22 01:04:552149inline
Howard Hinnantbc8d3f92010-05-11 19:42:162150void
2151list<_Tp, _Alloc>::merge(list& __c)
2152{
2153 merge(__c, __less<value_type>());
2154}
2155
2156template <class _Tp, class _Alloc>
2157template <class _Comp>
2158void
2159list<_Tp, _Alloc>::merge(list& __c, _Comp __comp)
2160{
2161 if (this != &__c)
2162 {
2163 iterator __f1 = begin();
2164 iterator __e1 = end();
2165 iterator __f2 = __c.begin();
2166 iterator __e2 = __c.end();
2167 while (__f1 != __e1 && __f2 != __e2)
2168 {
2169 if (__comp(*__f2, *__f1))
2170 {
2171 size_type __ds = 1;
Howard Hinnant0949eed2011-06-30 21:18:192172 iterator __m2 = _VSTD::next(__f2);
Howard Hinnantbc8d3f92010-05-11 19:42:162173 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2, ++__ds)
2174 ;
2175 base::__sz() += __ds;
2176 __c.__sz() -= __ds;
Eric Fiselier5c74b482015-12-30 20:57:592177 __link_pointer __f = __f2.__ptr_;
2178 __link_pointer __l = __m2.__ptr_->__prev_;
Howard Hinnantbc8d3f92010-05-11 19:42:162179 __f2 = __m2;
2180 base::__unlink_nodes(__f, __l);
Howard Hinnant0949eed2011-06-30 21:18:192181 __m2 = _VSTD::next(__f1);
Howard Hinnant29f74322013-06-25 16:08:472182 __link_nodes(__f1.__ptr_, __f, __l);
Howard Hinnantbc8d3f92010-05-11 19:42:162183 __f1 = __m2;
2184 }
2185 else
2186 ++__f1;
2187 }
2188 splice(__e1, __c);
Howard Hinnant1c3ec6d2011-09-27 23:55:032189#if _LIBCPP_DEBUG_LEVEL >= 2
2190 __libcpp_db* __db = __get_db();
2191 __c_node* __cn1 = __db->__find_c_and_lock(this);
2192 __c_node* __cn2 = __db->__find_c(&__c);
2193 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
2194 {
2195 --__p;
2196 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Eric Fiselier8e7bd4f2016-01-04 03:27:522197 if (__i->__ptr_ != __c.__end_as_link())
Howard Hinnant1c3ec6d2011-09-27 23:55:032198 {
2199 __cn1->__add(*__p);
2200 (*__p)->__c_ = __cn1;
2201 if (--__cn2->end_ != __p)
2202 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
2203 }
2204 }
2205 __db->unlock();
2206#endif
Howard Hinnantbc8d3f92010-05-11 19:42:162207 }
2208}
2209
2210template <class _Tp, class _Alloc>
Evgeniy Stepanov9341a8a2016-04-22 01:04:552211inline
Howard Hinnantbc8d3f92010-05-11 19:42:162212void
2213list<_Tp, _Alloc>::sort()
2214{
2215 sort(__less<value_type>());
2216}
2217
2218template <class _Tp, class _Alloc>
2219template <class _Comp>
Evgeniy Stepanov9341a8a2016-04-22 01:04:552220inline
Howard Hinnantbc8d3f92010-05-11 19:42:162221void
2222list<_Tp, _Alloc>::sort(_Comp __comp)
2223{
2224 __sort(begin(), end(), base::__sz(), __comp);
2225}
2226
2227template <class _Tp, class _Alloc>
2228template <class _Comp>
Howard Hinnantbc8d3f92010-05-11 19:42:162229typename list<_Tp, _Alloc>::iterator
2230list<_Tp, _Alloc>::__sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp)
2231{
2232 switch (__n)
2233 {
2234 case 0:
2235 case 1:
2236 return __f1;
2237 case 2:
2238 if (__comp(*--__e2, *__f1))
2239 {
Eric Fiselier5c74b482015-12-30 20:57:592240 __link_pointer __f = __e2.__ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:162241 base::__unlink_nodes(__f, __f);
Howard Hinnant29f74322013-06-25 16:08:472242 __link_nodes(__f1.__ptr_, __f, __f);
Howard Hinnantbc8d3f92010-05-11 19:42:162243 return __e2;
2244 }
2245 return __f1;
2246 }
2247 size_type __n2 = __n / 2;
Howard Hinnant0949eed2011-06-30 21:18:192248 iterator __e1 = _VSTD::next(__f1, __n2);
Howard Hinnantbc8d3f92010-05-11 19:42:162249 iterator __r = __f1 = __sort(__f1, __e1, __n2, __comp);
2250 iterator __f2 = __e1 = __sort(__e1, __e2, __n - __n2, __comp);
2251 if (__comp(*__f2, *__f1))
2252 {
Howard Hinnant0949eed2011-06-30 21:18:192253 iterator __m2 = _VSTD::next(__f2);
Howard Hinnantbc8d3f92010-05-11 19:42:162254 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2)
2255 ;
Eric Fiselier5c74b482015-12-30 20:57:592256 __link_pointer __f = __f2.__ptr_;
2257 __link_pointer __l = __m2.__ptr_->__prev_;
Howard Hinnantbc8d3f92010-05-11 19:42:162258 __r = __f2;
2259 __e1 = __f2 = __m2;
2260 base::__unlink_nodes(__f, __l);
Howard Hinnant0949eed2011-06-30 21:18:192261 __m2 = _VSTD::next(__f1);
Howard Hinnant29f74322013-06-25 16:08:472262 __link_nodes(__f1.__ptr_, __f, __l);
Howard Hinnantbc8d3f92010-05-11 19:42:162263 __f1 = __m2;
2264 }
2265 else
2266 ++__f1;
2267 while (__f1 != __e1 && __f2 != __e2)
2268 {
2269 if (__comp(*__f2, *__f1))
2270 {
Howard Hinnant0949eed2011-06-30 21:18:192271 iterator __m2 = _VSTD::next(__f2);
Howard Hinnantbc8d3f92010-05-11 19:42:162272 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2)
2273 ;
Eric Fiselier5c74b482015-12-30 20:57:592274 __link_pointer __f = __f2.__ptr_;
2275 __link_pointer __l = __m2.__ptr_->__prev_;
Howard Hinnantbc8d3f92010-05-11 19:42:162276 if (__e1 == __f2)
2277 __e1 = __m2;
2278 __f2 = __m2;
2279 base::__unlink_nodes(__f, __l);
Howard Hinnant0949eed2011-06-30 21:18:192280 __m2 = _VSTD::next(__f1);
Howard Hinnant29f74322013-06-25 16:08:472281 __link_nodes(__f1.__ptr_, __f, __l);
Howard Hinnantbc8d3f92010-05-11 19:42:162282 __f1 = __m2;
2283 }
2284 else
2285 ++__f1;
2286 }
2287 return __r;
2288}
2289
2290template <class _Tp, class _Alloc>
2291void
Howard Hinnantc5607272011-06-03 17:30:282292list<_Tp, _Alloc>::reverse() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:162293{
2294 if (base::__sz() > 1)
2295 {
2296 iterator __e = end();
Howard Hinnant1c3ec6d2011-09-27 23:55:032297 for (iterator __i = begin(); __i.__ptr_ != __e.__ptr_;)
2298 {
Howard Hinnant0949eed2011-06-30 21:18:192299 _VSTD::swap(__i.__ptr_->__prev_, __i.__ptr_->__next_);
Howard Hinnant1c3ec6d2011-09-27 23:55:032300 __i.__ptr_ = __i.__ptr_->__prev_;
2301 }
Howard Hinnant0949eed2011-06-30 21:18:192302 _VSTD::swap(__e.__ptr_->__prev_, __e.__ptr_->__next_);
Howard Hinnantbc8d3f92010-05-11 19:42:162303 }
2304}
2305
2306template <class _Tp, class _Alloc>
Howard Hinnant1c3ec6d2011-09-27 23:55:032307bool
2308list<_Tp, _Alloc>::__invariants() const
2309{
2310 return size() == _VSTD::distance(begin(), end());
2311}
2312
2313#if _LIBCPP_DEBUG_LEVEL >= 2
2314
2315template <class _Tp, class _Alloc>
2316bool
2317list<_Tp, _Alloc>::__dereferenceable(const const_iterator* __i) const
2318{
Eric Fiselier8e7bd4f2016-01-04 03:27:522319 return __i->__ptr_ != this->__end_as_link();
Howard Hinnant1c3ec6d2011-09-27 23:55:032320}
2321
2322template <class _Tp, class _Alloc>
2323bool
2324list<_Tp, _Alloc>::__decrementable(const const_iterator* __i) const
2325{
2326 return !empty() && __i->__ptr_ != base::__end_.__next_;
2327}
2328
2329template <class _Tp, class _Alloc>
2330bool
2331list<_Tp, _Alloc>::__addable(const const_iterator* __i, ptrdiff_t __n) const
2332{
2333 return false;
2334}
2335
2336template <class _Tp, class _Alloc>
2337bool
2338list<_Tp, _Alloc>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
2339{
2340 return false;
2341}
2342
2343#endif // _LIBCPP_DEBUG_LEVEL >= 2
2344
2345template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:342346inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162347bool
2348operator==(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2349{
Howard Hinnant0949eed2011-06-30 21:18:192350 return __x.size() == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantbc8d3f92010-05-11 19:42:162351}
2352
2353template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:342354inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162355bool
2356operator< (const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2357{
Howard Hinnant0949eed2011-06-30 21:18:192358 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantbc8d3f92010-05-11 19:42:162359}
2360
2361template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:342362inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162363bool
2364operator!=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2365{
2366 return !(__x == __y);
2367}
2368
2369template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:342370inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162371bool
2372operator> (const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2373{
2374 return __y < __x;
2375}
2376
2377template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:342378inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162379bool
2380operator>=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2381{
2382 return !(__x < __y);
2383}
2384
2385template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:342386inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162387bool
2388operator<=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2389{
2390 return !(__y < __x);
2391}
2392
2393template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:342394inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162395void
2396swap(list<_Tp, _Alloc>& __x, list<_Tp, _Alloc>& __y)
Howard Hinnantc5607272011-06-03 17:30:282397 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantbc8d3f92010-05-11 19:42:162398{
2399 __x.swap(__y);
2400}
2401
2402_LIBCPP_END_NAMESPACE_STD
2403
2404#endif // _LIBCPP_LIST