blob: 28d505582c0d9d3465c2e62d37fcdd0c98c5827d [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>
178
Howard Hinnant66c6f972011-11-29 16:45:27179#include <__undef_min_max>
180
Eric Fiselierb9536102014-08-10 23:53:08181#include <__debug>
Howard Hinnant8b00e6c2013-08-02 00:26:35182
Howard Hinnant08e17472011-10-17 20:05:10183#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16184#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10185#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16186
187_LIBCPP_BEGIN_NAMESPACE_STD
188
Howard Hinnant2b1b2d42011-06-14 19:58:17189template <class _Tp, class _VoidPtr> struct __list_node;
Howard Hinnantbc8d3f92010-05-11 19:42:16190
191template <class _Tp, class _VoidPtr>
192struct __list_node_base
193{
Eric Fiselierbb2f28e2015-08-23 02:56:05194 typedef typename __rebind_pointer<_VoidPtr, __list_node<_Tp, _VoidPtr> >::type
195 pointer;
196 typedef typename __rebind_pointer<_VoidPtr, __list_node_base>::type
197 __base_pointer;
Howard Hinnant29f74322013-06-25 16:08:47198
Howard Hinnantbc8d3f92010-05-11 19:42:16199 pointer __prev_;
200 pointer __next_;
201
Howard Hinnant82894812010-09-22 16:48:34202 _LIBCPP_INLINE_VISIBILITY
Marshall Clowea8ed832014-08-05 01:34:12203 __list_node_base() : __prev_(__self()), __next_(__self()) {}
204
205 _LIBCPP_INLINE_VISIBILITY
206 pointer __self()
207 {
Marshall Clowfca038e2014-08-08 15:35:52208 return static_cast<pointer>(pointer_traits<__base_pointer>::pointer_to(*this));
Marshall Clowea8ed832014-08-05 01:34:12209 }
Howard Hinnantbc8d3f92010-05-11 19:42:16210};
211
212template <class _Tp, class _VoidPtr>
213struct __list_node
214 : public __list_node_base<_Tp, _VoidPtr>
215{
216 _Tp __value_;
217};
218
Marshall Clowceead9c2015-02-18 17:24:08219template <class _Tp, class _Alloc = allocator<_Tp> > class _LIBCPP_TYPE_VIS_ONLY list;
Howard Hinnant2b1b2d42011-06-14 19:58:17220template <class _Tp, class _Alloc> class __list_imp;
Howard Hinnant0f678bd2013-08-12 18:38:34221template <class _Tp, class _VoidPtr> class _LIBCPP_TYPE_VIS_ONLY __list_const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16222
223template <class _Tp, class _VoidPtr>
Howard Hinnant0f678bd2013-08-12 18:38:34224class _LIBCPP_TYPE_VIS_ONLY __list_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16225{
Eric Fiselierbb2f28e2015-08-23 02:56:05226 typedef typename __rebind_pointer<_VoidPtr, __list_node<_Tp, _VoidPtr> >::type
227 __node_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16228
229 __node_pointer __ptr_;
230
Howard Hinnant1c3ec6d2011-09-27 23:55:03231#if _LIBCPP_DEBUG_LEVEL >= 2
232 _LIBCPP_INLINE_VISIBILITY
233 explicit __list_iterator(__node_pointer __p, const void* __c) _NOEXCEPT
234 : __ptr_(__p)
235 {
236 __get_db()->__insert_ic(this, __c);
237 }
238#else
Howard Hinnant82894812010-09-22 16:48:34239 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28240 explicit __list_iterator(__node_pointer __p) _NOEXCEPT : __ptr_(__p) {}
Howard Hinnant1c3ec6d2011-09-27 23:55:03241#endif
242
243
Howard Hinnantbc8d3f92010-05-11 19:42:16244
245 template<class, class> friend class list;
246 template<class, class> friend class __list_imp;
247 template<class, class> friend class __list_const_iterator;
248public:
249 typedef bidirectional_iterator_tag iterator_category;
250 typedef _Tp value_type;
251 typedef value_type& reference;
Eric Fiselierbb2f28e2015-08-23 02:56:05252 typedef typename __rebind_pointer<_VoidPtr, value_type>::type pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16253 typedef typename pointer_traits<pointer>::difference_type difference_type;
254
Howard Hinnant82894812010-09-22 16:48:34255 _LIBCPP_INLINE_VISIBILITY
Marshall Clow65d2e6a2013-08-05 21:23:28256 __list_iterator() _NOEXCEPT : __ptr_(nullptr)
Howard Hinnant1c3ec6d2011-09-27 23:55:03257 {
258#if _LIBCPP_DEBUG_LEVEL >= 2
259 __get_db()->__insert_i(this);
260#endif
261 }
262
263#if _LIBCPP_DEBUG_LEVEL >= 2
264
Howard Hinnant211f0ee2011-01-28 23:46:28265 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03266 __list_iterator(const __list_iterator& __p)
267 : __ptr_(__p.__ptr_)
268 {
269 __get_db()->__iterator_copy(this, &__p);
270 }
271
272 _LIBCPP_INLINE_VISIBILITY
273 ~__list_iterator()
274 {
275 __get_db()->__erase_i(this);
276 }
277
278 _LIBCPP_INLINE_VISIBILITY
279 __list_iterator& operator=(const __list_iterator& __p)
280 {
281 if (this != &__p)
282 {
283 __get_db()->__iterator_copy(this, &__p);
284 __ptr_ = __p.__ptr_;
285 }
286 return *this;
287 }
288
289#endif // _LIBCPP_DEBUG_LEVEL >= 2
290
291 _LIBCPP_INLINE_VISIBILITY
292 reference operator*() const
293 {
294#if _LIBCPP_DEBUG_LEVEL >= 2
295 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
296 "Attempted to dereference a non-dereferenceable list::iterator");
297#endif
298 return __ptr_->__value_;
299 }
Howard Hinnant82894812010-09-22 16:48:34300 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant29f74322013-06-25 16:08:47301 pointer operator->() const
302 {
303#if _LIBCPP_DEBUG_LEVEL >= 2
304 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
305 "Attempted to dereference a non-dereferenceable list::iterator");
306#endif
307 return pointer_traits<pointer>::pointer_to(__ptr_->__value_);
308 }
Howard Hinnantbc8d3f92010-05-11 19:42:16309
Howard Hinnant82894812010-09-22 16:48:34310 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03311 __list_iterator& operator++()
312 {
313#if _LIBCPP_DEBUG_LEVEL >= 2
314 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
315 "Attempted to increment non-incrementable list::iterator");
316#endif
317 __ptr_ = __ptr_->__next_;
318 return *this;
319 }
Howard Hinnant82894812010-09-22 16:48:34320 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16321 __list_iterator operator++(int) {__list_iterator __t(*this); ++(*this); return __t;}
322
Howard Hinnant82894812010-09-22 16:48:34323 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03324 __list_iterator& operator--()
325 {
326#if _LIBCPP_DEBUG_LEVEL >= 2
327 _LIBCPP_ASSERT(__get_const_db()->__decrementable(this),
328 "Attempted to decrement non-decrementable list::iterator");
329#endif
330 __ptr_ = __ptr_->__prev_;
331 return *this;
332 }
Howard Hinnant82894812010-09-22 16:48:34333 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16334 __list_iterator operator--(int) {__list_iterator __t(*this); --(*this); return __t;}
335
Howard Hinnant82894812010-09-22 16:48:34336 friend _LIBCPP_INLINE_VISIBILITY
337 bool operator==(const __list_iterator& __x, const __list_iterator& __y)
Howard Hinnant1c3ec6d2011-09-27 23:55:03338 {
Howard Hinnant1c3ec6d2011-09-27 23:55:03339 return __x.__ptr_ == __y.__ptr_;
340 }
Howard Hinnant82894812010-09-22 16:48:34341 friend _LIBCPP_INLINE_VISIBILITY
342 bool operator!=(const __list_iterator& __x, const __list_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16343 {return !(__x == __y);}
344};
345
346template <class _Tp, class _VoidPtr>
Howard Hinnant0f678bd2013-08-12 18:38:34347class _LIBCPP_TYPE_VIS_ONLY __list_const_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16348{
Eric Fiselierbb2f28e2015-08-23 02:56:05349 typedef typename __rebind_pointer<_VoidPtr, __list_node<_Tp, _VoidPtr> >::type
350 __node_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16351
352 __node_pointer __ptr_;
353
Howard Hinnant1c3ec6d2011-09-27 23:55:03354#if _LIBCPP_DEBUG_LEVEL >= 2
355 _LIBCPP_INLINE_VISIBILITY
356 explicit __list_const_iterator(__node_pointer __p, const void* __c) _NOEXCEPT
357 : __ptr_(__p)
358 {
359 __get_db()->__insert_ic(this, __c);
360 }
361#else
Howard Hinnant82894812010-09-22 16:48:34362 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28363 explicit __list_const_iterator(__node_pointer __p) _NOEXCEPT : __ptr_(__p) {}
Howard Hinnant1c3ec6d2011-09-27 23:55:03364#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16365
366 template<class, class> friend class list;
367 template<class, class> friend class __list_imp;
368public:
369 typedef bidirectional_iterator_tag iterator_category;
370 typedef _Tp value_type;
371 typedef const value_type& reference;
Eric Fiselierbb2f28e2015-08-23 02:56:05372 typedef typename __rebind_pointer<_VoidPtr, const value_type>::type pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16373 typedef typename pointer_traits<pointer>::difference_type difference_type;
374
Howard Hinnant82894812010-09-22 16:48:34375 _LIBCPP_INLINE_VISIBILITY
Marshall Clow65d2e6a2013-08-05 21:23:28376 __list_const_iterator() _NOEXCEPT : __ptr_(nullptr)
Howard Hinnant1c3ec6d2011-09-27 23:55:03377 {
378#if _LIBCPP_DEBUG_LEVEL >= 2
379 __get_db()->__insert_i(this);
380#endif
381 }
Howard Hinnant211f0ee2011-01-28 23:46:28382 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6dcaf3e2013-04-05 17:58:52383 __list_const_iterator(const __list_iterator<_Tp, _VoidPtr>& __p) _NOEXCEPT
Howard Hinnant1c3ec6d2011-09-27 23:55:03384 : __ptr_(__p.__ptr_)
385 {
386#if _LIBCPP_DEBUG_LEVEL >= 2
387 __get_db()->__iterator_copy(this, &__p);
388#endif
389 }
390
391#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16392
Howard Hinnant82894812010-09-22 16:48:34393 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03394 __list_const_iterator(const __list_const_iterator& __p)
395 : __ptr_(__p.__ptr_)
396 {
397 __get_db()->__iterator_copy(this, &__p);
398 }
399
400 _LIBCPP_INLINE_VISIBILITY
401 ~__list_const_iterator()
402 {
403 __get_db()->__erase_i(this);
404 }
405
406 _LIBCPP_INLINE_VISIBILITY
407 __list_const_iterator& operator=(const __list_const_iterator& __p)
408 {
409 if (this != &__p)
410 {
411 __get_db()->__iterator_copy(this, &__p);
412 __ptr_ = __p.__ptr_;
413 }
414 return *this;
415 }
416
417#endif // _LIBCPP_DEBUG_LEVEL >= 2
418 _LIBCPP_INLINE_VISIBILITY
419 reference operator*() const
420 {
421#if _LIBCPP_DEBUG_LEVEL >= 2
422 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
423 "Attempted to dereference a non-dereferenceable list::const_iterator");
424#endif
425 return __ptr_->__value_;
426 }
Howard Hinnant82894812010-09-22 16:48:34427 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant29f74322013-06-25 16:08:47428 pointer operator->() const
429 {
430#if _LIBCPP_DEBUG_LEVEL >= 2
431 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
432 "Attempted to dereference a non-dereferenceable list::iterator");
433#endif
434 return pointer_traits<pointer>::pointer_to(__ptr_->__value_);
435 }
Howard Hinnantbc8d3f92010-05-11 19:42:16436
Howard Hinnant82894812010-09-22 16:48:34437 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03438 __list_const_iterator& operator++()
439 {
440#if _LIBCPP_DEBUG_LEVEL >= 2
441 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
442 "Attempted to increment non-incrementable list::const_iterator");
443#endif
444 __ptr_ = __ptr_->__next_;
445 return *this;
446 }
Howard Hinnant82894812010-09-22 16:48:34447 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16448 __list_const_iterator operator++(int) {__list_const_iterator __t(*this); ++(*this); return __t;}
449
Howard Hinnant82894812010-09-22 16:48:34450 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03451 __list_const_iterator& operator--()
452 {
453#if _LIBCPP_DEBUG_LEVEL >= 2
454 _LIBCPP_ASSERT(__get_const_db()->__decrementable(this),
455 "Attempted to decrement non-decrementable list::const_iterator");
456#endif
457 __ptr_ = __ptr_->__prev_;
458 return *this;
459 }
Howard Hinnant82894812010-09-22 16:48:34460 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16461 __list_const_iterator operator--(int) {__list_const_iterator __t(*this); --(*this); return __t;}
462
Howard Hinnant82894812010-09-22 16:48:34463 friend _LIBCPP_INLINE_VISIBILITY
464 bool operator==(const __list_const_iterator& __x, const __list_const_iterator& __y)
Howard Hinnant1c3ec6d2011-09-27 23:55:03465 {
Howard Hinnant1c3ec6d2011-09-27 23:55:03466 return __x.__ptr_ == __y.__ptr_;
467 }
Howard Hinnant82894812010-09-22 16:48:34468 friend _LIBCPP_INLINE_VISIBILITY
469 bool operator!=(const __list_const_iterator& __x, const __list_const_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16470 {return !(__x == __y);}
471};
472
473template <class _Tp, class _Alloc>
474class __list_imp
475{
476 __list_imp(const __list_imp&);
477 __list_imp& operator=(const __list_imp&);
478protected:
479 typedef _Tp value_type;
480 typedef _Alloc allocator_type;
481 typedef allocator_traits<allocator_type> __alloc_traits;
482 typedef typename __alloc_traits::size_type size_type;
483 typedef typename __alloc_traits::void_pointer __void_pointer;
484 typedef __list_iterator<value_type, __void_pointer> iterator;
485 typedef __list_const_iterator<value_type, __void_pointer> const_iterator;
486 typedef __list_node_base<value_type, __void_pointer> __node_base;
487 typedef __list_node<value_type, __void_pointer> __node;
Marshall Clow66302c62015-04-07 05:21:38488 typedef typename __rebind_alloc_helper<__alloc_traits, __node>::type __node_allocator;
Howard Hinnantbc8d3f92010-05-11 19:42:16489 typedef allocator_traits<__node_allocator> __node_alloc_traits;
490 typedef typename __node_alloc_traits::pointer __node_pointer;
Howard Hinnant29f74322013-06-25 16:08:47491 typedef typename __node_alloc_traits::pointer __node_const_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16492 typedef typename __alloc_traits::pointer pointer;
493 typedef typename __alloc_traits::const_pointer const_pointer;
494 typedef typename __alloc_traits::difference_type difference_type;
495
Marshall Clow66302c62015-04-07 05:21:38496 typedef typename __rebind_alloc_helper<__alloc_traits, __node_base>::type __node_base_allocator;
Howard Hinnant29f74322013-06-25 16:08:47497 typedef typename allocator_traits<__node_base_allocator>::pointer __node_base_pointer;
498
Howard Hinnantbc8d3f92010-05-11 19:42:16499 __node_base __end_;
500 __compressed_pair<size_type, __node_allocator> __size_alloc_;
501
Howard Hinnant82894812010-09-22 16:48:34502 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28503 size_type& __sz() _NOEXCEPT {return __size_alloc_.first();}
Howard Hinnant82894812010-09-22 16:48:34504 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28505 const size_type& __sz() const _NOEXCEPT
506 {return __size_alloc_.first();}
Howard Hinnant82894812010-09-22 16:48:34507 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28508 __node_allocator& __node_alloc() _NOEXCEPT
509 {return __size_alloc_.second();}
Howard Hinnant82894812010-09-22 16:48:34510 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28511 const __node_allocator& __node_alloc() const _NOEXCEPT
512 {return __size_alloc_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16513
Howard Hinnant29f74322013-06-25 16:08:47514 static void __unlink_nodes(__node_pointer __f, __node_pointer __l) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16515
Howard Hinnantc5607272011-06-03 17:30:28516 __list_imp()
517 _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16518 __list_imp(const allocator_type& __a);
519 ~__list_imp();
Howard Hinnantc5607272011-06-03 17:30:28520 void clear() _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34521 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28522 bool empty() const _NOEXCEPT {return __sz() == 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16523
Howard Hinnant82894812010-09-22 16:48:34524 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03525 iterator begin() _NOEXCEPT
526 {
527#if _LIBCPP_DEBUG_LEVEL >= 2
528 return iterator(__end_.__next_, this);
529#else
530 return iterator(__end_.__next_);
531#endif
532 }
Howard Hinnant82894812010-09-22 16:48:34533 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28534 const_iterator begin() const _NOEXCEPT
Howard Hinnant1c3ec6d2011-09-27 23:55:03535 {
536#if _LIBCPP_DEBUG_LEVEL >= 2
537 return const_iterator(__end_.__next_, this);
538#else
539 return const_iterator(__end_.__next_);
540#endif
541 }
Howard Hinnant82894812010-09-22 16:48:34542 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03543 iterator end() _NOEXCEPT
544 {
545#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant29f74322013-06-25 16:08:47546 return iterator(static_cast<__node_pointer>(
547 pointer_traits<__node_base_pointer>::pointer_to(__end_)), this);
Howard Hinnant1c3ec6d2011-09-27 23:55:03548#else
Howard Hinnant29f74322013-06-25 16:08:47549 return iterator(static_cast<__node_pointer>(
550 pointer_traits<__node_base_pointer>::pointer_to(__end_)));
Howard Hinnant1c3ec6d2011-09-27 23:55:03551#endif
552 }
Howard Hinnant82894812010-09-22 16:48:34553 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28554 const_iterator end() const _NOEXCEPT
Howard Hinnant1c3ec6d2011-09-27 23:55:03555 {
556#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant29f74322013-06-25 16:08:47557 return const_iterator(static_cast<__node_const_pointer>(
558 pointer_traits<__node_base_pointer>::pointer_to(const_cast<__node_base&>(__end_))), this);
Howard Hinnant1c3ec6d2011-09-27 23:55:03559#else
Howard Hinnant29f74322013-06-25 16:08:47560 return const_iterator(static_cast<__node_const_pointer>(
561 pointer_traits<__node_base_pointer>::pointer_to(const_cast<__node_base&>(__end_))));
Howard Hinnant1c3ec6d2011-09-27 23:55:03562#endif
563 }
Howard Hinnantbc8d3f92010-05-11 19:42:16564
Howard Hinnantc5607272011-06-03 17:30:28565 void swap(__list_imp& __c)
Marshall Clow7d914d12015-07-13 20:04:56566#if _LIBCPP_STD_VER >= 14
567 _NOEXCEPT;
568#else
569 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
570 __is_nothrow_swappable<allocator_type>::value);
571#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16572
Howard Hinnant82894812010-09-22 16:48:34573 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16574 void __copy_assign_alloc(const __list_imp& __c)
575 {__copy_assign_alloc(__c, integral_constant<bool,
576 __node_alloc_traits::propagate_on_container_copy_assignment::value>());}
577
Howard Hinnant82894812010-09-22 16:48:34578 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16579 void __move_assign_alloc(__list_imp& __c)
Howard Hinnantc5607272011-06-03 17:30:28580 _NOEXCEPT_(
581 !__node_alloc_traits::propagate_on_container_move_assignment::value ||
582 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16583 {__move_assign_alloc(__c, integral_constant<bool,
584 __node_alloc_traits::propagate_on_container_move_assignment::value>());}
585
586private:
Howard Hinnant82894812010-09-22 16:48:34587 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16588 void __copy_assign_alloc(const __list_imp& __c, true_type)
589 {
590 if (__node_alloc() != __c.__node_alloc())
591 clear();
592 __node_alloc() = __c.__node_alloc();
593 }
594
Howard Hinnant82894812010-09-22 16:48:34595 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16596 void __copy_assign_alloc(const __list_imp& __c, false_type)
597 {}
598
Howard Hinnant82894812010-09-22 16:48:34599 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31600 void __move_assign_alloc(__list_imp& __c, true_type)
Howard Hinnantc5607272011-06-03 17:30:28601 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16602 {
Howard Hinnant0949eed2011-06-30 21:18:19603 __node_alloc() = _VSTD::move(__c.__node_alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16604 }
605
Howard Hinnant82894812010-09-22 16:48:34606 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31607 void __move_assign_alloc(__list_imp& __c, false_type)
Howard Hinnantc5607272011-06-03 17:30:28608 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16609 {}
610};
611
612// Unlink nodes [__f, __l]
613template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34614inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16615void
Howard Hinnant29f74322013-06-25 16:08:47616__list_imp<_Tp, _Alloc>::__unlink_nodes(__node_pointer __f, __node_pointer __l)
Howard Hinnantc5607272011-06-03 17:30:28617 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16618{
Howard Hinnant29f74322013-06-25 16:08:47619 __f->__prev_->__next_ = __l->__next_;
620 __l->__next_->__prev_ = __f->__prev_;
Howard Hinnantbc8d3f92010-05-11 19:42:16621}
622
623template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34624inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16625__list_imp<_Tp, _Alloc>::__list_imp()
Howard Hinnantc5607272011-06-03 17:30:28626 _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16627 : __size_alloc_(0)
628{
629}
630
631template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34632inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16633__list_imp<_Tp, _Alloc>::__list_imp(const allocator_type& __a)
634 : __size_alloc_(0, __node_allocator(__a))
635{
636}
637
638template <class _Tp, class _Alloc>
639__list_imp<_Tp, _Alloc>::~__list_imp()
640{
641 clear();
Howard Hinnant1c3ec6d2011-09-27 23:55:03642#if _LIBCPP_DEBUG_LEVEL >= 2
643 __get_db()->__erase_c(this);
644#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16645}
646
647template <class _Tp, class _Alloc>
648void
Howard Hinnantc5607272011-06-03 17:30:28649__list_imp<_Tp, _Alloc>::clear() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16650{
651 if (!empty())
652 {
653 __node_allocator& __na = __node_alloc();
Howard Hinnant1c3ec6d2011-09-27 23:55:03654 __node_pointer __f = __end_.__next_;
Howard Hinnant29f74322013-06-25 16:08:47655 __node_pointer __l = static_cast<__node_pointer>(
656 pointer_traits<__node_base_pointer>::pointer_to(__end_));
657 __unlink_nodes(__f, __l->__prev_);
Howard Hinnantbc8d3f92010-05-11 19:42:16658 __sz() = 0;
659 while (__f != __l)
660 {
Howard Hinnant29f74322013-06-25 16:08:47661 __node_pointer __n = __f;
Howard Hinnant1c3ec6d2011-09-27 23:55:03662 __f = __f->__next_;
Howard Hinnant29f74322013-06-25 16:08:47663 __node_alloc_traits::destroy(__na, _VSTD::addressof(__n->__value_));
664 __node_alloc_traits::deallocate(__na, __n, 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16665 }
Howard Hinnant1c3ec6d2011-09-27 23:55:03666#if _LIBCPP_DEBUG_LEVEL >= 2
667 __c_node* __c = __get_db()->__find_c_and_lock(this);
668 for (__i_node** __p = __c->end_; __p != __c->beg_; )
669 {
670 --__p;
671 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
672 if (__i->__ptr_ != __l)
673 {
674 (*__p)->__c_ = nullptr;
675 if (--__c->end_ != __p)
676 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
677 }
678 }
679 __get_db()->unlock();
680#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16681 }
682}
683
684template <class _Tp, class _Alloc>
685void
686__list_imp<_Tp, _Alloc>::swap(__list_imp& __c)
Marshall Clow7d914d12015-07-13 20:04:56687#if _LIBCPP_STD_VER >= 14
688 _NOEXCEPT
689#else
690 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
691 __is_nothrow_swappable<allocator_type>::value)
692#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16693{
Howard Hinnant1c3ec6d2011-09-27 23:55:03694 _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
695 this->__node_alloc() == __c.__node_alloc(),
696 "list::swap: Either propagate_on_container_swap must be true"
697 " or the allocators must compare equal");
Howard Hinnant0949eed2011-06-30 21:18:19698 using _VSTD::swap;
Marshall Clow7d914d12015-07-13 20:04:56699 __swap_allocator(__node_alloc(), __c.__node_alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16700 swap(__sz(), __c.__sz());
701 swap(__end_, __c.__end_);
702 if (__sz() == 0)
Marshall Clowea8ed832014-08-05 01:34:12703 __end_.__next_ = __end_.__prev_ = __end_.__self();
Howard Hinnantbc8d3f92010-05-11 19:42:16704 else
Marshall Clowea8ed832014-08-05 01:34:12705 __end_.__prev_->__next_ = __end_.__next_->__prev_ = __end_.__self();
Howard Hinnantbc8d3f92010-05-11 19:42:16706 if (__c.__sz() == 0)
Marshall Clowea8ed832014-08-05 01:34:12707 __c.__end_.__next_ = __c.__end_.__prev_ = __c.__end_.__self();
Howard Hinnantbc8d3f92010-05-11 19:42:16708 else
Marshall Clowea8ed832014-08-05 01:34:12709 __c.__end_.__prev_->__next_ = __c.__end_.__next_->__prev_ = __c.__end_.__self();
710
Howard Hinnant1c3ec6d2011-09-27 23:55:03711#if _LIBCPP_DEBUG_LEVEL >= 2
712 __libcpp_db* __db = __get_db();
713 __c_node* __cn1 = __db->__find_c_and_lock(this);
714 __c_node* __cn2 = __db->__find_c(&__c);
715 std::swap(__cn1->beg_, __cn2->beg_);
716 std::swap(__cn1->end_, __cn2->end_);
717 std::swap(__cn1->cap_, __cn2->cap_);
718 for (__i_node** __p = __cn1->end_; __p != __cn1->beg_;)
719 {
720 --__p;
721 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
Howard Hinnant29f74322013-06-25 16:08:47722 if (__i->__ptr_ == static_cast<__node_pointer>(
723 pointer_traits<__node_base_pointer>::pointer_to(__c.__end_)))
Howard Hinnant1c3ec6d2011-09-27 23:55:03724 {
725 __cn2->__add(*__p);
726 if (--__cn1->end_ != __p)
727 memmove(__p, __p+1, (__cn1->end_ - __p)*sizeof(__i_node*));
728 }
729 else
730 (*__p)->__c_ = __cn1;
731 }
732 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
733 {
734 --__p;
735 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
Howard Hinnant29f74322013-06-25 16:08:47736 if (__i->__ptr_ == static_cast<__node_pointer>(
737 pointer_traits<__node_base_pointer>::pointer_to(__end_)))
Howard Hinnant1c3ec6d2011-09-27 23:55:03738 {
739 __cn1->__add(*__p);
740 if (--__cn2->end_ != __p)
741 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
742 }
743 else
744 (*__p)->__c_ = __cn2;
745 }
746 __db->unlock();
747#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16748}
749
Marshall Clowceead9c2015-02-18 17:24:08750template <class _Tp, class _Alloc /*= allocator<_Tp>*/>
Howard Hinnant0f678bd2013-08-12 18:38:34751class _LIBCPP_TYPE_VIS_ONLY list
Howard Hinnantbc8d3f92010-05-11 19:42:16752 : private __list_imp<_Tp, _Alloc>
753{
754 typedef __list_imp<_Tp, _Alloc> base;
755 typedef typename base::__node __node;
756 typedef typename base::__node_allocator __node_allocator;
757 typedef typename base::__node_pointer __node_pointer;
758 typedef typename base::__node_alloc_traits __node_alloc_traits;
Howard Hinnant29f74322013-06-25 16:08:47759 typedef typename base::__node_base __node_base;
760 typedef typename base::__node_base_pointer __node_base_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16761
762public:
763 typedef _Tp value_type;
764 typedef _Alloc allocator_type;
765 static_assert((is_same<value_type, typename allocator_type::value_type>::value),
766 "Invalid allocator::value_type");
767 typedef value_type& reference;
768 typedef const value_type& const_reference;
769 typedef typename base::pointer pointer;
770 typedef typename base::const_pointer const_pointer;
771 typedef typename base::size_type size_type;
772 typedef typename base::difference_type difference_type;
773 typedef typename base::iterator iterator;
774 typedef typename base::const_iterator const_iterator;
Howard Hinnant0949eed2011-06-30 21:18:19775 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
776 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16777
Howard Hinnant82894812010-09-22 16:48:34778 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28779 list()
780 _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value)
Howard Hinnant1c3ec6d2011-09-27 23:55:03781 {
782#if _LIBCPP_DEBUG_LEVEL >= 2
783 __get_db()->__insert_c(this);
784#endif
785 }
Howard Hinnant82894812010-09-22 16:48:34786 _LIBCPP_INLINE_VISIBILITY
Marshall Clow955f2c82013-09-08 19:11:51787 explicit list(const allocator_type& __a) : base(__a)
Howard Hinnant1c3ec6d2011-09-27 23:55:03788 {
789#if _LIBCPP_DEBUG_LEVEL >= 2
790 __get_db()->__insert_c(this);
791#endif
792 }
Marshall Clow955f2c82013-09-08 19:11:51793 explicit list(size_type __n);
794#if _LIBCPP_STD_VER > 11
795 explicit list(size_type __n, const allocator_type& __a);
796#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16797 list(size_type __n, const value_type& __x);
798 list(size_type __n, const value_type& __x, const allocator_type& __a);
799 template <class _InpIter>
800 list(_InpIter __f, _InpIter __l,
801 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
802 template <class _InpIter>
803 list(_InpIter __f, _InpIter __l, const allocator_type& __a,
804 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
805
806 list(const list& __c);
807 list(const list& __c, const allocator_type& __a);
808 list& operator=(const list& __c);
Howard Hinnante3e32912011-08-12 21:56:02809#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16810 list(initializer_list<value_type> __il);
811 list(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnante3e32912011-08-12 21:56:02812#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant73d21a42010-09-04 23:28:19813#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc5607272011-06-03 17:30:28814 list(list&& __c)
815 _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16816 list(list&& __c, const allocator_type& __a);
Howard Hinnantc5607272011-06-03 17:30:28817 list& operator=(list&& __c)
818 _NOEXCEPT_(
819 __node_alloc_traits::propagate_on_container_move_assignment::value &&
820 is_nothrow_move_assignable<__node_allocator>::value);
Howard Hinnant73d21a42010-09-04 23:28:19821#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnante3e32912011-08-12 21:56:02822#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant82894812010-09-22 16:48:34823 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16824 list& operator=(initializer_list<value_type> __il)
825 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnante3e32912011-08-12 21:56:02826#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16827
828 template <class _InpIter>
829 void assign(_InpIter __f, _InpIter __l,
830 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
831 void assign(size_type __n, const value_type& __x);
Howard Hinnante3e32912011-08-12 21:56:02832#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant82894812010-09-22 16:48:34833 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16834 void assign(initializer_list<value_type> __il)
835 {assign(__il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02836#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16837
Howard Hinnantc5607272011-06-03 17:30:28838 allocator_type get_allocator() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16839
Howard Hinnant82894812010-09-22 16:48:34840 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28841 size_type size() const _NOEXCEPT {return base::__sz();}
Howard Hinnant82894812010-09-22 16:48:34842 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28843 bool empty() const _NOEXCEPT {return base::empty();}
Howard Hinnant82894812010-09-22 16:48:34844 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28845 size_type max_size() const _NOEXCEPT
846 {return numeric_limits<difference_type>::max();}
Howard Hinnantbc8d3f92010-05-11 19:42:16847
Howard Hinnant82894812010-09-22 16:48:34848 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28849 iterator begin() _NOEXCEPT {return base::begin();}
Howard Hinnant82894812010-09-22 16:48:34850 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28851 const_iterator begin() const _NOEXCEPT {return base::begin();}
Howard Hinnant82894812010-09-22 16:48:34852 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28853 iterator end() _NOEXCEPT {return base::end();}
Howard Hinnant82894812010-09-22 16:48:34854 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28855 const_iterator end() const _NOEXCEPT {return base::end();}
Howard Hinnant82894812010-09-22 16:48:34856 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28857 const_iterator cbegin() const _NOEXCEPT {return base::begin();}
Howard Hinnant82894812010-09-22 16:48:34858 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28859 const_iterator cend() const _NOEXCEPT {return base::end();}
Howard Hinnantbc8d3f92010-05-11 19:42:16860
Howard Hinnant82894812010-09-22 16:48:34861 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28862 reverse_iterator rbegin() _NOEXCEPT
863 {return reverse_iterator(end());}
Howard Hinnant82894812010-09-22 16:48:34864 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28865 const_reverse_iterator rbegin() const _NOEXCEPT
866 {return const_reverse_iterator(end());}
Howard Hinnant82894812010-09-22 16:48:34867 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28868 reverse_iterator rend() _NOEXCEPT
869 {return reverse_iterator(begin());}
Howard Hinnant82894812010-09-22 16:48:34870 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28871 const_reverse_iterator rend() const _NOEXCEPT
872 {return const_reverse_iterator(begin());}
Howard Hinnant82894812010-09-22 16:48:34873 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28874 const_reverse_iterator crbegin() const _NOEXCEPT
875 {return const_reverse_iterator(end());}
Howard Hinnant82894812010-09-22 16:48:34876 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28877 const_reverse_iterator crend() const _NOEXCEPT
878 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16879
Howard Hinnant82894812010-09-22 16:48:34880 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03881 reference front()
882 {
883 _LIBCPP_ASSERT(!empty(), "list::front called on empty list");
884 return base::__end_.__next_->__value_;
885 }
Howard Hinnant82894812010-09-22 16:48:34886 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03887 const_reference front() const
888 {
889 _LIBCPP_ASSERT(!empty(), "list::front called on empty list");
890 return base::__end_.__next_->__value_;
891 }
Howard Hinnant82894812010-09-22 16:48:34892 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03893 reference back()
894 {
895 _LIBCPP_ASSERT(!empty(), "list::back called on empty list");
896 return base::__end_.__prev_->__value_;
897 }
Howard Hinnant82894812010-09-22 16:48:34898 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c3ec6d2011-09-27 23:55:03899 const_reference back() const
900 {
901 _LIBCPP_ASSERT(!empty(), "list::back called on empty list");
902 return base::__end_.__prev_->__value_;
903 }
Howard Hinnantbc8d3f92010-05-11 19:42:16904
Howard Hinnant73d21a42010-09-04 23:28:19905#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16906 void push_front(value_type&& __x);
907 void push_back(value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19908#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16909 template <class... _Args>
910 void emplace_front(_Args&&... __args);
911 template <class... _Args>
912 void emplace_back(_Args&&... __args);
913 template <class... _Args>
914 iterator emplace(const_iterator __p, _Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19915#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16916 iterator insert(const_iterator __p, value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19917#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16918
919 void push_front(const value_type& __x);
920 void push_back(const value_type& __x);
921
922 iterator insert(const_iterator __p, const value_type& __x);
923 iterator insert(const_iterator __p, size_type __n, const value_type& __x);
924 template <class _InpIter>
925 iterator insert(const_iterator __p, _InpIter __f, _InpIter __l,
926 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
Howard Hinnante3e32912011-08-12 21:56:02927#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant82894812010-09-22 16:48:34928 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16929 iterator insert(const_iterator __p, initializer_list<value_type> __il)
930 {return insert(__p, __il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02931#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16932
Howard Hinnant82894812010-09-22 16:48:34933 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28934 void swap(list& __c)
Marshall Clow7d914d12015-07-13 20:04:56935#if _LIBCPP_STD_VER >= 14
936 _NOEXCEPT
937#else
Howard Hinnantc5607272011-06-03 17:30:28938 _NOEXCEPT_(!__node_alloc_traits::propagate_on_container_swap::value ||
939 __is_nothrow_swappable<__node_allocator>::value)
Marshall Clow7d914d12015-07-13 20:04:56940#endif
Howard Hinnantc5607272011-06-03 17:30:28941 {base::swap(__c);}
Howard Hinnant82894812010-09-22 16:48:34942 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28943 void clear() _NOEXCEPT {base::clear();}
Howard Hinnantbc8d3f92010-05-11 19:42:16944
945 void pop_front();
946 void pop_back();
947
948 iterator erase(const_iterator __p);
949 iterator erase(const_iterator __f, const_iterator __l);
950
951 void resize(size_type __n);
952 void resize(size_type __n, const value_type& __x);
953
954 void splice(const_iterator __p, list& __c);
Howard Hinnant73d21a42010-09-04 23:28:19955#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant82894812010-09-22 16:48:34956 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16957 void splice(const_iterator __p, list&& __c) {splice(__p, __c);}
958#endif
959 void splice(const_iterator __p, list& __c, const_iterator __i);
Howard Hinnant73d21a42010-09-04 23:28:19960#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant82894812010-09-22 16:48:34961 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16962 void splice(const_iterator __p, list&& __c, const_iterator __i)
963 {splice(__p, __c, __i);}
Howard Hinnant73d21a42010-09-04 23:28:19964#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16965 void splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l);
Howard Hinnant73d21a42010-09-04 23:28:19966#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant82894812010-09-22 16:48:34967 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16968 void splice(const_iterator __p, list&& __c, const_iterator __f, const_iterator __l)
969 {splice(__p, __c, __f, __l);}
Howard Hinnant73d21a42010-09-04 23:28:19970#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16971
972 void remove(const value_type& __x);
973 template <class _Pred> void remove_if(_Pred __pred);
974 void unique();
975 template <class _BinaryPred>
976 void unique(_BinaryPred __binary_pred);
977 void merge(list& __c);
Howard Hinnant73d21a42010-09-04 23:28:19978#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant82894812010-09-22 16:48:34979 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16980 void merge(list&& __c) {merge(__c);}
981#endif
982 template <class _Comp>
983 void merge(list& __c, _Comp __comp);
Howard Hinnant73d21a42010-09-04 23:28:19984#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16985 template <class _Comp>
Howard Hinnant82894812010-09-22 16:48:34986 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16987 void merge(list&& __c, _Comp __comp) {merge(__c, __comp);}
Howard Hinnant73d21a42010-09-04 23:28:19988#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16989 void sort();
990 template <class _Comp>
991 void sort(_Comp __comp);
992
Howard Hinnantc5607272011-06-03 17:30:28993 void reverse() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16994
Howard Hinnant1c3ec6d2011-09-27 23:55:03995 bool __invariants() const;
996
997#if _LIBCPP_DEBUG_LEVEL >= 2
998
999 bool __dereferenceable(const const_iterator* __i) const;
1000 bool __decrementable(const const_iterator* __i) const;
1001 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1002 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1003
1004#endif // _LIBCPP_DEBUG_LEVEL >= 2
1005
Howard Hinnantbc8d3f92010-05-11 19:42:161006private:
Marshall Clowea8ed832014-08-05 01:34:121007 static void __link_nodes (__node_pointer __p, __node_pointer __f, __node_pointer __l);
1008 void __link_nodes_at_front(__node_pointer __f, __node_pointer __l);
1009 void __link_nodes_at_back (__node_pointer __f, __node_pointer __l);
Howard Hinnantbc8d3f92010-05-11 19:42:161010 iterator __iterator(size_type __n);
1011 template <class _Comp>
1012 static iterator __sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp);
1013
Howard Hinnantc5607272011-06-03 17:30:281014 void __move_assign(list& __c, true_type)
1015 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:161016 void __move_assign(list& __c, false_type);
1017};
1018
1019// Link in nodes [__f, __l] just prior to __p
1020template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:341021inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161022void
Howard Hinnant29f74322013-06-25 16:08:471023list<_Tp, _Alloc>::__link_nodes(__node_pointer __p, __node_pointer __f, __node_pointer __l)
Howard Hinnantbc8d3f92010-05-11 19:42:161024{
Howard Hinnant29f74322013-06-25 16:08:471025 __p->__prev_->__next_ = __f;
1026 __f->__prev_ = __p->__prev_;
1027 __p->__prev_ = __l;
1028 __l->__next_ = __p;
Howard Hinnantbc8d3f92010-05-11 19:42:161029}
1030
Marshall Clowea8ed832014-08-05 01:34:121031// Link in nodes [__f, __l] at the front of the list
1032template <class _Tp, class _Alloc>
1033inline _LIBCPP_INLINE_VISIBILITY
1034void
1035list<_Tp, _Alloc>::__link_nodes_at_front(__node_pointer __f, __node_pointer __l)
1036{
Marshall Clowfca038e2014-08-08 15:35:521037 __f->__prev_ = base::__end_.__self();
1038 __l->__next_ = base::__end_.__next_;
1039 __l->__next_->__prev_ = __l;
1040 base::__end_.__next_ = __f;
Marshall Clowea8ed832014-08-05 01:34:121041}
1042
1043// Link in nodes [__f, __l] at the front of the list
1044template <class _Tp, class _Alloc>
1045inline _LIBCPP_INLINE_VISIBILITY
1046void
1047list<_Tp, _Alloc>::__link_nodes_at_back(__node_pointer __f, __node_pointer __l)
1048{
Marshall Clowfca038e2014-08-08 15:35:521049 __l->__next_ = base::__end_.__self();
1050 __f->__prev_ = base::__end_.__prev_;
1051 __f->__prev_->__next_ = __f;
1052 base::__end_.__prev_ = __l;
Marshall Clowea8ed832014-08-05 01:34:121053}
1054
1055
Howard Hinnantbc8d3f92010-05-11 19:42:161056template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:341057inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161058typename list<_Tp, _Alloc>::iterator
1059list<_Tp, _Alloc>::__iterator(size_type __n)
1060{
Howard Hinnant0949eed2011-06-30 21:18:191061 return __n <= base::__sz() / 2 ? _VSTD::next(begin(), __n)
1062 : _VSTD::prev(end(), base::__sz() - __n);
Howard Hinnantbc8d3f92010-05-11 19:42:161063}
1064
1065template <class _Tp, class _Alloc>
1066list<_Tp, _Alloc>::list(size_type __n)
1067{
Howard Hinnant1c3ec6d2011-09-27 23:55:031068#if _LIBCPP_DEBUG_LEVEL >= 2
1069 __get_db()->__insert_c(this);
1070#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161071 for (; __n > 0; --__n)
Howard Hinnant73d21a42010-09-04 23:28:191072#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:161073 emplace_back();
1074#else
1075 push_back(value_type());
1076#endif
1077}
1078
Marshall Clow955f2c82013-09-08 19:11:511079#if _LIBCPP_STD_VER > 11
1080template <class _Tp, class _Alloc>
1081list<_Tp, _Alloc>::list(size_type __n, const allocator_type& __a) : base(__a)
1082{
1083#if _LIBCPP_DEBUG_LEVEL >= 2
1084 __get_db()->__insert_c(this);
1085#endif
1086 for (; __n > 0; --__n)
1087#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1088 emplace_back();
1089#else
1090 push_back(value_type());
1091#endif
1092}
1093#endif
1094
Howard Hinnantbc8d3f92010-05-11 19:42:161095template <class _Tp, class _Alloc>
1096list<_Tp, _Alloc>::list(size_type __n, const value_type& __x)
1097{
Howard Hinnant1c3ec6d2011-09-27 23:55:031098#if _LIBCPP_DEBUG_LEVEL >= 2
1099 __get_db()->__insert_c(this);
1100#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161101 for (; __n > 0; --__n)
1102 push_back(__x);
1103}
1104
1105template <class _Tp, class _Alloc>
1106list<_Tp, _Alloc>::list(size_type __n, const value_type& __x, const allocator_type& __a)
1107 : base(__a)
1108{
Howard Hinnant1c3ec6d2011-09-27 23:55:031109#if _LIBCPP_DEBUG_LEVEL >= 2
1110 __get_db()->__insert_c(this);
1111#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161112 for (; __n > 0; --__n)
1113 push_back(__x);
1114}
1115
1116template <class _Tp, class _Alloc>
1117template <class _InpIter>
1118list<_Tp, _Alloc>::list(_InpIter __f, _InpIter __l,
1119 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1120{
Howard Hinnant1c3ec6d2011-09-27 23:55:031121#if _LIBCPP_DEBUG_LEVEL >= 2
1122 __get_db()->__insert_c(this);
1123#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161124 for (; __f != __l; ++__f)
1125 push_back(*__f);
1126}
1127
1128template <class _Tp, class _Alloc>
1129template <class _InpIter>
1130list<_Tp, _Alloc>::list(_InpIter __f, _InpIter __l, const allocator_type& __a,
1131 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1132 : base(__a)
1133{
Howard Hinnant1c3ec6d2011-09-27 23:55:031134#if _LIBCPP_DEBUG_LEVEL >= 2
1135 __get_db()->__insert_c(this);
1136#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161137 for (; __f != __l; ++__f)
1138 push_back(*__f);
1139}
1140
1141template <class _Tp, class _Alloc>
1142list<_Tp, _Alloc>::list(const list& __c)
1143 : base(allocator_type(
1144 __node_alloc_traits::select_on_container_copy_construction(
1145 __c.__node_alloc())))
1146{
Howard Hinnant1c3ec6d2011-09-27 23:55:031147#if _LIBCPP_DEBUG_LEVEL >= 2
1148 __get_db()->__insert_c(this);
1149#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161150 for (const_iterator __i = __c.begin(), __e = __c.end(); __i != __e; ++__i)
1151 push_back(*__i);
1152}
1153
1154template <class _Tp, class _Alloc>
1155list<_Tp, _Alloc>::list(const list& __c, const allocator_type& __a)
1156 : base(__a)
1157{
Howard Hinnant1c3ec6d2011-09-27 23:55:031158#if _LIBCPP_DEBUG_LEVEL >= 2
1159 __get_db()->__insert_c(this);
1160#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161161 for (const_iterator __i = __c.begin(), __e = __c.end(); __i != __e; ++__i)
1162 push_back(*__i);
1163}
1164
Howard Hinnante3e32912011-08-12 21:56:021165#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1166
Howard Hinnantbc8d3f92010-05-11 19:42:161167template <class _Tp, class _Alloc>
1168list<_Tp, _Alloc>::list(initializer_list<value_type> __il, const allocator_type& __a)
1169 : base(__a)
1170{
Howard Hinnant1c3ec6d2011-09-27 23:55:031171#if _LIBCPP_DEBUG_LEVEL >= 2
1172 __get_db()->__insert_c(this);
1173#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161174 for (typename initializer_list<value_type>::const_iterator __i = __il.begin(),
1175 __e = __il.end(); __i != __e; ++__i)
1176 push_back(*__i);
1177}
1178
1179template <class _Tp, class _Alloc>
1180list<_Tp, _Alloc>::list(initializer_list<value_type> __il)
1181{
Howard Hinnant1c3ec6d2011-09-27 23:55:031182#if _LIBCPP_DEBUG_LEVEL >= 2
1183 __get_db()->__insert_c(this);
1184#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161185 for (typename initializer_list<value_type>::const_iterator __i = __il.begin(),
1186 __e = __il.end(); __i != __e; ++__i)
1187 push_back(*__i);
1188}
1189
Howard Hinnante3e32912011-08-12 21:56:021190#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1191
Howard Hinnantbc8d3f92010-05-11 19:42:161192template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:341193inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161194list<_Tp, _Alloc>&
1195list<_Tp, _Alloc>::operator=(const list& __c)
1196{
1197 if (this != &__c)
1198 {
1199 base::__copy_assign_alloc(__c);
1200 assign(__c.begin(), __c.end());
1201 }
1202 return *this;
1203}
1204
Howard Hinnant73d21a42010-09-04 23:28:191205#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:161206
1207template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:341208inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161209list<_Tp, _Alloc>::list(list&& __c)
Howard Hinnantc5607272011-06-03 17:30:281210 _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value)
Howard Hinnant0949eed2011-06-30 21:18:191211 : base(allocator_type(_VSTD::move(__c.__node_alloc())))
Howard Hinnantbc8d3f92010-05-11 19:42:161212{
Howard Hinnant1c3ec6d2011-09-27 23:55:031213#if _LIBCPP_DEBUG_LEVEL >= 2
1214 __get_db()->__insert_c(this);
1215#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161216 splice(end(), __c);
1217}
1218
1219template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:341220inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161221list<_Tp, _Alloc>::list(list&& __c, const allocator_type& __a)
1222 : base(__a)
1223{
Howard Hinnant1c3ec6d2011-09-27 23:55:031224#if _LIBCPP_DEBUG_LEVEL >= 2
1225 __get_db()->__insert_c(this);
1226#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161227 if (__a == __c.get_allocator())
1228 splice(end(), __c);
1229 else
1230 {
Howard Hinnant99968442011-11-29 18:15:501231 typedef move_iterator<iterator> _Ip;
1232 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:161233 }
1234}
1235
1236template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:341237inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161238list<_Tp, _Alloc>&
1239list<_Tp, _Alloc>::operator=(list&& __c)
Howard Hinnantc5607272011-06-03 17:30:281240 _NOEXCEPT_(
1241 __node_alloc_traits::propagate_on_container_move_assignment::value &&
1242 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:161243{
1244 __move_assign(__c, integral_constant<bool,
1245 __node_alloc_traits::propagate_on_container_move_assignment::value>());
1246 return *this;
1247}
1248
1249template <class _Tp, class _Alloc>
1250void
1251list<_Tp, _Alloc>::__move_assign(list& __c, false_type)
1252{
1253 if (base::__node_alloc() != __c.__node_alloc())
1254 {
Howard Hinnant99968442011-11-29 18:15:501255 typedef move_iterator<iterator> _Ip;
1256 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:161257 }
1258 else
1259 __move_assign(__c, true_type());
1260}
1261
1262template <class _Tp, class _Alloc>
1263void
1264list<_Tp, _Alloc>::__move_assign(list& __c, true_type)
Howard Hinnantc5607272011-06-03 17:30:281265 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:161266{
1267 clear();
1268 base::__move_assign_alloc(__c);
1269 splice(end(), __c);
1270}
1271
Howard Hinnant73d21a42010-09-04 23:28:191272#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:161273
1274template <class _Tp, class _Alloc>
1275template <class _InpIter>
1276void
1277list<_Tp, _Alloc>::assign(_InpIter __f, _InpIter __l,
1278 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1279{
1280 iterator __i = begin();
1281 iterator __e = end();
1282 for (; __f != __l && __i != __e; ++__f, ++__i)
1283 *__i = *__f;
1284 if (__i == __e)
1285 insert(__e, __f, __l);
1286 else
1287 erase(__i, __e);
1288}
1289
1290template <class _Tp, class _Alloc>
1291void
1292list<_Tp, _Alloc>::assign(size_type __n, const value_type& __x)
1293{
1294 iterator __i = begin();
1295 iterator __e = end();
1296 for (; __n > 0 && __i != __e; --__n, ++__i)
1297 *__i = __x;
1298 if (__i == __e)
1299 insert(__e, __n, __x);
1300 else
1301 erase(__i, __e);
1302}
1303
1304template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:341305inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161306_Alloc
Howard Hinnantc5607272011-06-03 17:30:281307list<_Tp, _Alloc>::get_allocator() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161308{
1309 return allocator_type(base::__node_alloc());
1310}
1311
1312template <class _Tp, class _Alloc>
1313typename list<_Tp, _Alloc>::iterator
1314list<_Tp, _Alloc>::insert(const_iterator __p, const value_type& __x)
1315{
Howard Hinnant1c3ec6d2011-09-27 23:55:031316#if _LIBCPP_DEBUG_LEVEL >= 2
1317 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1318 "list::insert(iterator, x) called with an iterator not"
1319 " referring to this list");
1320#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161321 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:501322 typedef __allocator_destructor<__node_allocator> _Dp;
1323 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:161324 __hold->__prev_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:191325 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnant29f74322013-06-25 16:08:471326 __link_nodes(__p.__ptr_, __hold.get(), __hold.get());
Howard Hinnantbc8d3f92010-05-11 19:42:161327 ++base::__sz();
Howard Hinnant79a35572013-04-05 00:18:491328#if _LIBCPP_DEBUG_LEVEL >= 2
1329 return iterator(__hold.release(), this);
1330#else
Howard Hinnantbc8d3f92010-05-11 19:42:161331 return iterator(__hold.release());
Howard Hinnant79a35572013-04-05 00:18:491332#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161333}
1334
1335template <class _Tp, class _Alloc>
1336typename list<_Tp, _Alloc>::iterator
1337list<_Tp, _Alloc>::insert(const_iterator __p, size_type __n, const value_type& __x)
1338{
Howard Hinnant1c3ec6d2011-09-27 23:55:031339#if _LIBCPP_DEBUG_LEVEL >= 2
1340 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1341 "list::insert(iterator, n, x) called with an iterator not"
1342 " referring to this list");
Howard Hinnant29f74322013-06-25 16:08:471343 iterator __r(__p.__ptr_, this);
Howard Hinnant1c3ec6d2011-09-27 23:55:031344#else
Howard Hinnant29f74322013-06-25 16:08:471345 iterator __r(__p.__ptr_);
Howard Hinnant1c3ec6d2011-09-27 23:55:031346#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161347 if (__n > 0)
1348 {
1349 size_type __ds = 0;
1350 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:501351 typedef __allocator_destructor<__node_allocator> _Dp;
1352 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:161353 __hold->__prev_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:191354 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:161355 ++__ds;
Howard Hinnant1c3ec6d2011-09-27 23:55:031356#if _LIBCPP_DEBUG_LEVEL >= 2
1357 __r = iterator(__hold.get(), this);
1358#else
Howard Hinnantbc8d3f92010-05-11 19:42:161359 __r = iterator(__hold.get());
Howard Hinnant1c3ec6d2011-09-27 23:55:031360#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161361 __hold.release();
1362 iterator __e = __r;
1363#ifndef _LIBCPP_NO_EXCEPTIONS
1364 try
1365 {
Howard Hinnant324bb032010-08-22 00:02:431366#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:161367 for (--__n; __n != 0; --__n, ++__e, ++__ds)
1368 {
1369 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:191370 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:161371 __e.__ptr_->__next_ = __hold.get();
1372 __hold->__prev_ = __e.__ptr_;
1373 __hold.release();
1374 }
1375#ifndef _LIBCPP_NO_EXCEPTIONS
1376 }
1377 catch (...)
1378 {
1379 while (true)
1380 {
Howard Hinnant0949eed2011-06-30 21:18:191381 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Howard Hinnantbc8d3f92010-05-11 19:42:161382 __node_pointer __prev = __e.__ptr_->__prev_;
1383 __node_alloc_traits::deallocate(__na, __e.__ptr_, 1);
1384 if (__prev == 0)
1385 break;
Howard Hinnant1c3ec6d2011-09-27 23:55:031386#if _LIBCPP_DEBUG_LEVEL >= 2
1387 __e = iterator(__prev, this);
1388#else
Howard Hinnantbc8d3f92010-05-11 19:42:161389 __e = iterator(__prev);
Howard Hinnant1c3ec6d2011-09-27 23:55:031390#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161391 }
1392 throw;
1393 }
Howard Hinnant324bb032010-08-22 00:02:431394#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant29f74322013-06-25 16:08:471395 __link_nodes(__p.__ptr_, __r.__ptr_, __e.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:161396 base::__sz() += __ds;
1397 }
1398 return __r;
1399}
1400
1401template <class _Tp, class _Alloc>
1402template <class _InpIter>
1403typename list<_Tp, _Alloc>::iterator
1404list<_Tp, _Alloc>::insert(const_iterator __p, _InpIter __f, _InpIter __l,
1405 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
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, range) 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 (__f != __l)
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_), *__f);
Howard Hinnantbc8d3f92010-05-11 19:42:161423 ++__ds;
Howard Hinnant1c3ec6d2011-09-27 23:55:031424#if _LIBCPP_DEBUG_LEVEL >= 2
1425 __r = iterator(__hold.get(), this);
1426#else
Howard Hinnantbc8d3f92010-05-11 19:42:161427 __r = iterator(__hold.get());
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
Eric Fiselier537876b2015-03-19 03:20:021435 for (++__f; __f != __l; ++__f, (void) ++__e, (void) ++__ds)
Howard Hinnantbc8d3f92010-05-11 19:42:161436 {
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_), *__f);
Howard Hinnantbc8d3f92010-05-11 19:42:161439 __e.__ptr_->__next_ = __hold.get();
1440 __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));
Howard Hinnantbc8d3f92010-05-11 19:42:161450 __node_pointer __prev = __e.__ptr_->__prev_;
1451 __node_alloc_traits::deallocate(__na, __e.__ptr_, 1);
1452 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>
1470void
1471list<_Tp, _Alloc>::push_front(const value_type& __x)
1472{
1473 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:501474 typedef __allocator_destructor<__node_allocator> _Dp;
1475 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:191476 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Marshall Clowea8ed832014-08-05 01:34:121477 __link_nodes_at_front(__hold.get(), __hold.get());
Howard Hinnantbc8d3f92010-05-11 19:42:161478 ++base::__sz();
1479 __hold.release();
1480}
1481
1482template <class _Tp, class _Alloc>
1483void
1484list<_Tp, _Alloc>::push_back(const value_type& __x)
1485{
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 Hinnant0949eed2011-06-30 21:18:191489 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Marshall Clowea8ed832014-08-05 01:34:121490 __link_nodes_at_back(__hold.get(), __hold.get());
Howard Hinnantbc8d3f92010-05-11 19:42:161491 ++base::__sz();
1492 __hold.release();
1493}
1494
Howard Hinnant73d21a42010-09-04 23:28:191495#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:161496
1497template <class _Tp, class _Alloc>
1498void
1499list<_Tp, _Alloc>::push_front(value_type&& __x)
1500{
1501 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:501502 typedef __allocator_destructor<__node_allocator> _Dp;
1503 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:191504 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
Marshall Clowea8ed832014-08-05 01:34:121505 __link_nodes_at_front(__hold.get(), __hold.get());
Howard Hinnantbc8d3f92010-05-11 19:42:161506 ++base::__sz();
1507 __hold.release();
1508}
1509
1510template <class _Tp, class _Alloc>
1511void
1512list<_Tp, _Alloc>::push_back(value_type&& __x)
1513{
1514 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:501515 typedef __allocator_destructor<__node_allocator> _Dp;
1516 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:191517 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
Marshall Clowea8ed832014-08-05 01:34:121518 __link_nodes_at_back(__hold.get(), __hold.get());
Howard Hinnantbc8d3f92010-05-11 19:42:161519 ++base::__sz();
1520 __hold.release();
1521}
1522
Howard Hinnant73d21a42010-09-04 23:28:191523#ifndef _LIBCPP_HAS_NO_VARIADICS
1524
Howard Hinnantbc8d3f92010-05-11 19:42:161525template <class _Tp, class _Alloc>
1526template <class... _Args>
1527void
1528list<_Tp, _Alloc>::emplace_front(_Args&&... __args)
1529{
1530 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:501531 typedef __allocator_destructor<__node_allocator> _Dp;
1532 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:191533 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
Marshall Clowea8ed832014-08-05 01:34:121534 __link_nodes_at_front(__hold.get(), __hold.get());
Howard Hinnantbc8d3f92010-05-11 19:42:161535 ++base::__sz();
1536 __hold.release();
1537}
1538
1539template <class _Tp, class _Alloc>
1540template <class... _Args>
1541void
1542list<_Tp, _Alloc>::emplace_back(_Args&&... __args)
1543{
1544 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:501545 typedef __allocator_destructor<__node_allocator> _Dp;
1546 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:191547 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
Marshall Clowea8ed832014-08-05 01:34:121548 __link_nodes_at_back(__hold.get(), __hold.get());
Howard Hinnantbc8d3f92010-05-11 19:42:161549 ++base::__sz();
1550 __hold.release();
1551}
1552
1553template <class _Tp, class _Alloc>
1554template <class... _Args>
1555typename list<_Tp, _Alloc>::iterator
1556list<_Tp, _Alloc>::emplace(const_iterator __p, _Args&&... __args)
1557{
Howard Hinnant79a35572013-04-05 00:18:491558#if _LIBCPP_DEBUG_LEVEL >= 2
1559 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1560 "list::emplace(iterator, args...) called with an iterator not"
1561 " referring to this list");
1562#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161563 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:501564 typedef __allocator_destructor<__node_allocator> _Dp;
1565 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:161566 __hold->__prev_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:191567 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
Howard Hinnant29f74322013-06-25 16:08:471568 __link_nodes(__p.__ptr_, __hold.get(), __hold.get());
Howard Hinnantbc8d3f92010-05-11 19:42:161569 ++base::__sz();
Howard Hinnant1c3ec6d2011-09-27 23:55:031570#if _LIBCPP_DEBUG_LEVEL >= 2
1571 return iterator(__hold.release(), this);
1572#else
Howard Hinnantbc8d3f92010-05-11 19:42:161573 return iterator(__hold.release());
Howard Hinnant1c3ec6d2011-09-27 23:55:031574#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161575}
1576
Howard Hinnant73d21a42010-09-04 23:28:191577#endif // _LIBCPP_HAS_NO_VARIADICS
1578
Howard Hinnantbc8d3f92010-05-11 19:42:161579template <class _Tp, class _Alloc>
1580typename list<_Tp, _Alloc>::iterator
1581list<_Tp, _Alloc>::insert(const_iterator __p, value_type&& __x)
1582{
Howard Hinnant1c3ec6d2011-09-27 23:55:031583#if _LIBCPP_DEBUG_LEVEL >= 2
1584 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1585 "list::insert(iterator, x) called with an iterator not"
1586 " referring to this list");
1587#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161588 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:501589 typedef __allocator_destructor<__node_allocator> _Dp;
1590 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:161591 __hold->__prev_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:191592 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
Howard Hinnant29f74322013-06-25 16:08:471593 __link_nodes(__p.__ptr_, __hold.get(), __hold.get());
Howard Hinnantbc8d3f92010-05-11 19:42:161594 ++base::__sz();
Howard Hinnant1c3ec6d2011-09-27 23:55:031595#if _LIBCPP_DEBUG_LEVEL >= 2
1596 return iterator(__hold.release(), this);
1597#else
Howard Hinnantbc8d3f92010-05-11 19:42:161598 return iterator(__hold.release());
Howard Hinnant1c3ec6d2011-09-27 23:55:031599#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161600}
1601
Howard Hinnant73d21a42010-09-04 23:28:191602#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:161603
1604template <class _Tp, class _Alloc>
1605void
1606list<_Tp, _Alloc>::pop_front()
1607{
Howard Hinnant1c3ec6d2011-09-27 23:55:031608 _LIBCPP_ASSERT(!empty(), "list::pop_front() called with empty list");
Howard Hinnantbc8d3f92010-05-11 19:42:161609 __node_allocator& __na = base::__node_alloc();
Howard Hinnant29f74322013-06-25 16:08:471610 __node_pointer __n = base::__end_.__next_;
Howard Hinnantbc8d3f92010-05-11 19:42:161611 base::__unlink_nodes(__n, __n);
1612 --base::__sz();
Howard Hinnant1c3ec6d2011-09-27 23:55:031613#if _LIBCPP_DEBUG_LEVEL >= 2
1614 __c_node* __c = __get_db()->__find_c_and_lock(this);
1615 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1616 {
1617 --__p;
1618 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant29f74322013-06-25 16:08:471619 if (__i->__ptr_ == __n)
Howard Hinnant1c3ec6d2011-09-27 23:55:031620 {
1621 (*__p)->__c_ = nullptr;
1622 if (--__c->end_ != __p)
1623 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1624 }
1625 }
1626 __get_db()->unlock();
1627#endif
Howard Hinnant29f74322013-06-25 16:08:471628 __node_alloc_traits::destroy(__na, _VSTD::addressof(__n->__value_));
1629 __node_alloc_traits::deallocate(__na, __n, 1);
Howard Hinnantbc8d3f92010-05-11 19:42:161630}
1631
1632template <class _Tp, class _Alloc>
1633void
1634list<_Tp, _Alloc>::pop_back()
1635{
Dmitri Gribenkoc7a39cf2013-06-24 06:15:571636 _LIBCPP_ASSERT(!empty(), "list::pop_back() called with empty list");
Howard Hinnantbc8d3f92010-05-11 19:42:161637 __node_allocator& __na = base::__node_alloc();
Howard Hinnant29f74322013-06-25 16:08:471638 __node_pointer __n = base::__end_.__prev_;
Howard Hinnantbc8d3f92010-05-11 19:42:161639 base::__unlink_nodes(__n, __n);
1640 --base::__sz();
Howard Hinnant1c3ec6d2011-09-27 23:55:031641#if _LIBCPP_DEBUG_LEVEL >= 2
1642 __c_node* __c = __get_db()->__find_c_and_lock(this);
1643 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1644 {
1645 --__p;
1646 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant29f74322013-06-25 16:08:471647 if (__i->__ptr_ == __n)
Howard Hinnant1c3ec6d2011-09-27 23:55:031648 {
1649 (*__p)->__c_ = nullptr;
1650 if (--__c->end_ != __p)
1651 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1652 }
1653 }
1654 __get_db()->unlock();
1655#endif
Howard Hinnant29f74322013-06-25 16:08:471656 __node_alloc_traits::destroy(__na, _VSTD::addressof(__n->__value_));
1657 __node_alloc_traits::deallocate(__na, __n, 1);
Howard Hinnantbc8d3f92010-05-11 19:42:161658}
1659
1660template <class _Tp, class _Alloc>
1661typename list<_Tp, _Alloc>::iterator
1662list<_Tp, _Alloc>::erase(const_iterator __p)
1663{
Howard Hinnant1c3ec6d2011-09-27 23:55:031664#if _LIBCPP_DEBUG_LEVEL >= 2
1665 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1666 "list::erase(iterator) called with an iterator not"
1667 " referring to this list");
1668#endif
Howard Hinnant79a35572013-04-05 00:18:491669 _LIBCPP_ASSERT(__p != end(),
1670 "list::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnantbc8d3f92010-05-11 19:42:161671 __node_allocator& __na = base::__node_alloc();
Howard Hinnant29f74322013-06-25 16:08:471672 __node_pointer __n = __p.__ptr_;
1673 __node_pointer __r = __n->__next_;
Howard Hinnantbc8d3f92010-05-11 19:42:161674 base::__unlink_nodes(__n, __n);
1675 --base::__sz();
Howard Hinnant1c3ec6d2011-09-27 23:55:031676#if _LIBCPP_DEBUG_LEVEL >= 2
1677 __c_node* __c = __get_db()->__find_c_and_lock(this);
1678 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1679 {
1680 --__p;
1681 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant29f74322013-06-25 16:08:471682 if (__i->__ptr_ == __n)
Howard Hinnant1c3ec6d2011-09-27 23:55:031683 {
1684 (*__p)->__c_ = nullptr;
1685 if (--__c->end_ != __p)
1686 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1687 }
1688 }
1689 __get_db()->unlock();
1690#endif
Howard Hinnant29f74322013-06-25 16:08:471691 __node_alloc_traits::destroy(__na, _VSTD::addressof(__n->__value_));
1692 __node_alloc_traits::deallocate(__na, __n, 1);
Howard Hinnant1c3ec6d2011-09-27 23:55:031693#if _LIBCPP_DEBUG_LEVEL >= 2
1694 return iterator(__r, this);
1695#else
Howard Hinnantbc8d3f92010-05-11 19:42:161696 return iterator(__r);
Howard Hinnant1c3ec6d2011-09-27 23:55:031697#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161698}
1699
1700template <class _Tp, class _Alloc>
1701typename list<_Tp, _Alloc>::iterator
1702list<_Tp, _Alloc>::erase(const_iterator __f, const_iterator __l)
1703{
Howard Hinnant1c3ec6d2011-09-27 23:55:031704#if _LIBCPP_DEBUG_LEVEL >= 2
1705 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__f) == this,
1706 "list::erase(iterator, iterator) called with an iterator not"
1707 " referring to this list");
1708#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161709 if (__f != __l)
1710 {
1711 __node_allocator& __na = base::__node_alloc();
Howard Hinnant29f74322013-06-25 16:08:471712 base::__unlink_nodes(__f.__ptr_, __l.__ptr_->__prev_);
Howard Hinnantbc8d3f92010-05-11 19:42:161713 while (__f != __l)
1714 {
Howard Hinnant29f74322013-06-25 16:08:471715 __node_pointer __n = __f.__ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:161716 ++__f;
1717 --base::__sz();
Howard Hinnant1c3ec6d2011-09-27 23:55:031718#if _LIBCPP_DEBUG_LEVEL >= 2
1719 __c_node* __c = __get_db()->__find_c_and_lock(this);
1720 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1721 {
1722 --__p;
1723 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant29f74322013-06-25 16:08:471724 if (__i->__ptr_ == __n)
Howard Hinnant1c3ec6d2011-09-27 23:55:031725 {
1726 (*__p)->__c_ = nullptr;
1727 if (--__c->end_ != __p)
1728 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1729 }
1730 }
1731 __get_db()->unlock();
1732#endif
Howard Hinnant29f74322013-06-25 16:08:471733 __node_alloc_traits::destroy(__na, _VSTD::addressof(__n->__value_));
1734 __node_alloc_traits::deallocate(__na, __n, 1);
Howard Hinnantbc8d3f92010-05-11 19:42:161735 }
1736 }
Howard Hinnant1c3ec6d2011-09-27 23:55:031737#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant29f74322013-06-25 16:08:471738 return iterator(__l.__ptr_, this);
Howard Hinnant1c3ec6d2011-09-27 23:55:031739#else
Howard Hinnant29f74322013-06-25 16:08:471740 return iterator(__l.__ptr_);
Howard Hinnant1c3ec6d2011-09-27 23:55:031741#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161742}
1743
1744template <class _Tp, class _Alloc>
1745void
1746list<_Tp, _Alloc>::resize(size_type __n)
1747{
1748 if (__n < base::__sz())
1749 erase(__iterator(__n), end());
1750 else if (__n > base::__sz())
1751 {
1752 __n -= base::__sz();
1753 size_type __ds = 0;
1754 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:501755 typedef __allocator_destructor<__node_allocator> _Dp;
1756 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:161757 __hold->__prev_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:191758 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:161759 ++__ds;
Howard Hinnant1c3ec6d2011-09-27 23:55:031760#if _LIBCPP_DEBUG_LEVEL >= 2
1761 iterator __r = iterator(__hold.release(), this);
1762#else
Howard Hinnantbc8d3f92010-05-11 19:42:161763 iterator __r = iterator(__hold.release());
Howard Hinnant1c3ec6d2011-09-27 23:55:031764#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161765 iterator __e = __r;
1766#ifndef _LIBCPP_NO_EXCEPTIONS
1767 try
1768 {
Howard Hinnant324bb032010-08-22 00:02:431769#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:161770 for (--__n; __n != 0; --__n, ++__e, ++__ds)
1771 {
1772 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:191773 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:161774 __e.__ptr_->__next_ = __hold.get();
1775 __hold->__prev_ = __e.__ptr_;
1776 __hold.release();
1777 }
1778#ifndef _LIBCPP_NO_EXCEPTIONS
1779 }
1780 catch (...)
1781 {
1782 while (true)
1783 {
Howard Hinnant0949eed2011-06-30 21:18:191784 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Howard Hinnantbc8d3f92010-05-11 19:42:161785 __node_pointer __prev = __e.__ptr_->__prev_;
1786 __node_alloc_traits::deallocate(__na, __e.__ptr_, 1);
1787 if (__prev == 0)
1788 break;
Howard Hinnant1c3ec6d2011-09-27 23:55:031789#if _LIBCPP_DEBUG_LEVEL >= 2
1790 __e = iterator(__prev, this);
1791#else
Howard Hinnantbc8d3f92010-05-11 19:42:161792 __e = iterator(__prev);
Howard Hinnant1c3ec6d2011-09-27 23:55:031793#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161794 }
1795 throw;
1796 }
Howard Hinnant324bb032010-08-22 00:02:431797#endif // _LIBCPP_NO_EXCEPTIONS
Marshall Clowea8ed832014-08-05 01:34:121798 __link_nodes_at_back(__r.__ptr_, __e.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:161799 base::__sz() += __ds;
1800 }
1801}
1802
1803template <class _Tp, class _Alloc>
1804void
1805list<_Tp, _Alloc>::resize(size_type __n, const value_type& __x)
1806{
1807 if (__n < base::__sz())
1808 erase(__iterator(__n), end());
1809 else if (__n > base::__sz())
1810 {
1811 __n -= base::__sz();
1812 size_type __ds = 0;
1813 __node_allocator& __na = base::__node_alloc();
Howard Hinnant99968442011-11-29 18:15:501814 typedef __allocator_destructor<__node_allocator> _Dp;
1815 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:161816 __hold->__prev_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:191817 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:161818 ++__ds;
Howard Hinnant1c3ec6d2011-09-27 23:55:031819#if _LIBCPP_DEBUG_LEVEL >= 2
1820 iterator __r = iterator(__hold.release(), this);
1821#else
Howard Hinnantbc8d3f92010-05-11 19:42:161822 iterator __r = iterator(__hold.release());
Howard Hinnant1c3ec6d2011-09-27 23:55:031823#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161824 iterator __e = __r;
1825#ifndef _LIBCPP_NO_EXCEPTIONS
1826 try
1827 {
Howard Hinnant324bb032010-08-22 00:02:431828#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:161829 for (--__n; __n != 0; --__n, ++__e, ++__ds)
1830 {
1831 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:191832 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:161833 __e.__ptr_->__next_ = __hold.get();
1834 __hold->__prev_ = __e.__ptr_;
1835 __hold.release();
1836 }
1837#ifndef _LIBCPP_NO_EXCEPTIONS
1838 }
1839 catch (...)
1840 {
1841 while (true)
1842 {
Howard Hinnant0949eed2011-06-30 21:18:191843 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Howard Hinnantbc8d3f92010-05-11 19:42:161844 __node_pointer __prev = __e.__ptr_->__prev_;
1845 __node_alloc_traits::deallocate(__na, __e.__ptr_, 1);
1846 if (__prev == 0)
1847 break;
Howard Hinnant1c3ec6d2011-09-27 23:55:031848#if _LIBCPP_DEBUG_LEVEL >= 2
1849 __e = iterator(__prev, this);
1850#else
Howard Hinnantbc8d3f92010-05-11 19:42:161851 __e = iterator(__prev);
Howard Hinnant1c3ec6d2011-09-27 23:55:031852#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161853 }
1854 throw;
1855 }
Howard Hinnant324bb032010-08-22 00:02:431856#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant29f74322013-06-25 16:08:471857 __link_nodes(static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::
1858 pointer_to(base::__end_)), __r.__ptr_, __e.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:161859 base::__sz() += __ds;
Howard Hinnant324bb032010-08-22 00:02:431860 }
Howard Hinnantbc8d3f92010-05-11 19:42:161861}
1862
1863template <class _Tp, class _Alloc>
1864void
1865list<_Tp, _Alloc>::splice(const_iterator __p, list& __c)
1866{
Howard Hinnant1c3ec6d2011-09-27 23:55:031867 _LIBCPP_ASSERT(this != &__c,
1868 "list::splice(iterator, list) called with this == &list");
1869#if _LIBCPP_DEBUG_LEVEL >= 2
1870 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1871 "list::splice(iterator, list) called with an iterator not"
1872 " referring to this list");
1873#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161874 if (!__c.empty())
1875 {
Howard Hinnant29f74322013-06-25 16:08:471876 __node_pointer __f = __c.__end_.__next_;
1877 __node_pointer __l = __c.__end_.__prev_;
Howard Hinnantbc8d3f92010-05-11 19:42:161878 base::__unlink_nodes(__f, __l);
Howard Hinnant29f74322013-06-25 16:08:471879 __link_nodes(__p.__ptr_, __f, __l);
Howard Hinnantbc8d3f92010-05-11 19:42:161880 base::__sz() += __c.__sz();
1881 __c.__sz() = 0;
Howard Hinnant1c3ec6d2011-09-27 23:55:031882#if _LIBCPP_DEBUG_LEVEL >= 2
1883 __libcpp_db* __db = __get_db();
1884 __c_node* __cn1 = __db->__find_c_and_lock(this);
1885 __c_node* __cn2 = __db->__find_c(&__c);
1886 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
1887 {
1888 --__p;
1889 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant29f74322013-06-25 16:08:471890 if (__i->__ptr_ != static_cast<__node_pointer>(
1891 pointer_traits<__node_base_pointer>::pointer_to(__c.__end_)))
Howard Hinnant1c3ec6d2011-09-27 23:55:031892 {
1893 __cn1->__add(*__p);
1894 (*__p)->__c_ = __cn1;
1895 if (--__cn2->end_ != __p)
1896 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
1897 }
1898 }
1899 __db->unlock();
1900#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161901 }
1902}
1903
1904template <class _Tp, class _Alloc>
1905void
1906list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __i)
1907{
Howard Hinnant1c3ec6d2011-09-27 23:55:031908#if _LIBCPP_DEBUG_LEVEL >= 2
1909 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1910 "list::splice(iterator, list, iterator) called with first iterator not"
1911 " referring to this list");
1912 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__i) == &__c,
1913 "list::splice(iterator, list, iterator) called with second iterator not"
1914 " referring to list argument");
1915 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(&__i),
1916 "list::splice(iterator, list, iterator) called with second iterator not"
1917 " derefereceable");
1918#endif
1919 if (__p.__ptr_ != __i.__ptr_ && __p.__ptr_ != __i.__ptr_->__next_)
Howard Hinnantbc8d3f92010-05-11 19:42:161920 {
Howard Hinnant29f74322013-06-25 16:08:471921 __node_pointer __f = __i.__ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:161922 base::__unlink_nodes(__f, __f);
Howard Hinnant29f74322013-06-25 16:08:471923 __link_nodes(__p.__ptr_, __f, __f);
Howard Hinnantbc8d3f92010-05-11 19:42:161924 --__c.__sz();
1925 ++base::__sz();
Howard Hinnant1c3ec6d2011-09-27 23:55:031926#if _LIBCPP_DEBUG_LEVEL >= 2
1927 __libcpp_db* __db = __get_db();
1928 __c_node* __cn1 = __db->__find_c_and_lock(this);
1929 __c_node* __cn2 = __db->__find_c(&__c);
1930 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
1931 {
1932 --__p;
1933 iterator* __j = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant29f74322013-06-25 16:08:471934 if (__j->__ptr_ == __f)
Howard Hinnant1c3ec6d2011-09-27 23:55:031935 {
1936 __cn1->__add(*__p);
1937 (*__p)->__c_ = __cn1;
1938 if (--__cn2->end_ != __p)
1939 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
1940 }
1941 }
1942 __db->unlock();
1943#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161944 }
1945}
1946
1947template <class _Tp, class _Alloc>
1948void
1949list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l)
1950{
Howard Hinnant1c3ec6d2011-09-27 23:55:031951#if _LIBCPP_DEBUG_LEVEL >= 2
1952 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1953 "list::splice(iterator, list, iterator, iterator) called with first iterator not"
1954 " referring to this list");
1955 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__f) == &__c,
1956 "list::splice(iterator, list, iterator, iterator) called with second iterator not"
1957 " referring to list argument");
1958 if (this == &__c)
1959 {
1960 for (const_iterator __i = __f; __i != __l; ++__i)
1961 _LIBCPP_ASSERT(__i != __p,
1962 "list::splice(iterator, list, iterator, iterator)"
1963 " called with the first iterator within the range"
1964 " of the second and third iterators");
1965 }
1966#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161967 if (__f != __l)
1968 {
1969 if (this != &__c)
1970 {
Howard Hinnant0949eed2011-06-30 21:18:191971 size_type __s = _VSTD::distance(__f, __l);
Howard Hinnantbc8d3f92010-05-11 19:42:161972 __c.__sz() -= __s;
1973 base::__sz() += __s;
1974 }
Howard Hinnant29f74322013-06-25 16:08:471975 __node_pointer __first = __f.__ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:161976 --__l;
Howard Hinnant29f74322013-06-25 16:08:471977 __node_pointer __last = __l.__ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:161978 base::__unlink_nodes(__first, __last);
Howard Hinnant29f74322013-06-25 16:08:471979 __link_nodes(__p.__ptr_, __first, __last);
Howard Hinnant1c3ec6d2011-09-27 23:55:031980#if _LIBCPP_DEBUG_LEVEL >= 2
1981 __libcpp_db* __db = __get_db();
1982 __c_node* __cn1 = __db->__find_c_and_lock(this);
1983 __c_node* __cn2 = __db->__find_c(&__c);
1984 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
1985 {
1986 --__p;
1987 iterator* __j = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant29f74322013-06-25 16:08:471988 for (__node_pointer __k = __f.__ptr_;
Howard Hinnant1c3ec6d2011-09-27 23:55:031989 __k != __l.__ptr_; __k = __k->__next_)
1990 {
1991 if (__j->__ptr_ == __k)
1992 {
1993 __cn1->__add(*__p);
1994 (*__p)->__c_ = __cn1;
1995 if (--__cn2->end_ != __p)
1996 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
1997 }
1998 }
1999 }
2000 __db->unlock();
2001#endif
Howard Hinnantbc8d3f92010-05-11 19:42:162002 }
2003}
2004
2005template <class _Tp, class _Alloc>
2006void
2007list<_Tp, _Alloc>::remove(const value_type& __x)
2008{
Marshall Clowfca038e2014-08-08 15:35:522009 list<_Tp, _Alloc> __deleted_nodes; // collect the nodes we're removing
2010 for (const_iterator __i = begin(), __e = end(); __i != __e;)
Howard Hinnantbc8d3f92010-05-11 19:42:162011 {
2012 if (*__i == __x)
2013 {
Marshall Clowfca038e2014-08-08 15:35:522014 const_iterator __j = _VSTD::next(__i);
Howard Hinnantbc8d3f92010-05-11 19:42:162015 for (; __j != __e && *__j == __x; ++__j)
2016 ;
Marshall Clowfca038e2014-08-08 15:35:522017 __deleted_nodes.splice(__deleted_nodes.end(), *this, __i, __j);
2018 __i = __j;
Marshall Clowf0f1bca2014-08-04 17:32:252019 if (__i != __e)
Marshall Clowea8ed832014-08-05 01:34:122020 ++__i;
Howard Hinnantbc8d3f92010-05-11 19:42:162021 }
2022 else
2023 ++__i;
2024 }
2025}
2026
2027template <class _Tp, class _Alloc>
2028template <class _Pred>
2029void
2030list<_Tp, _Alloc>::remove_if(_Pred __pred)
2031{
2032 for (iterator __i = begin(), __e = end(); __i != __e;)
2033 {
2034 if (__pred(*__i))
2035 {
Howard Hinnant0949eed2011-06-30 21:18:192036 iterator __j = _VSTD::next(__i);
Howard Hinnantbc8d3f92010-05-11 19:42:162037 for (; __j != __e && __pred(*__j); ++__j)
2038 ;
2039 __i = erase(__i, __j);
Marshall Clowf0f1bca2014-08-04 17:32:252040 if (__i != __e)
Marshall Clowea8ed832014-08-05 01:34:122041 ++__i;
Howard Hinnantbc8d3f92010-05-11 19:42:162042 }
2043 else
2044 ++__i;
2045 }
2046}
2047
2048template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:342049inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162050void
2051list<_Tp, _Alloc>::unique()
2052{
2053 unique(__equal_to<value_type>());
2054}
2055
2056template <class _Tp, class _Alloc>
2057template <class _BinaryPred>
2058void
2059list<_Tp, _Alloc>::unique(_BinaryPred __binary_pred)
2060{
2061 for (iterator __i = begin(), __e = end(); __i != __e;)
2062 {
Howard Hinnant0949eed2011-06-30 21:18:192063 iterator __j = _VSTD::next(__i);
Howard Hinnantbc8d3f92010-05-11 19:42:162064 for (; __j != __e && __binary_pred(*__i, *__j); ++__j)
2065 ;
2066 if (++__i != __j)
2067 __i = erase(__i, __j);
2068 }
2069}
2070
2071template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:342072inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162073void
2074list<_Tp, _Alloc>::merge(list& __c)
2075{
2076 merge(__c, __less<value_type>());
2077}
2078
2079template <class _Tp, class _Alloc>
2080template <class _Comp>
2081void
2082list<_Tp, _Alloc>::merge(list& __c, _Comp __comp)
2083{
2084 if (this != &__c)
2085 {
2086 iterator __f1 = begin();
2087 iterator __e1 = end();
2088 iterator __f2 = __c.begin();
2089 iterator __e2 = __c.end();
2090 while (__f1 != __e1 && __f2 != __e2)
2091 {
2092 if (__comp(*__f2, *__f1))
2093 {
2094 size_type __ds = 1;
Howard Hinnant0949eed2011-06-30 21:18:192095 iterator __m2 = _VSTD::next(__f2);
Howard Hinnantbc8d3f92010-05-11 19:42:162096 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2, ++__ds)
2097 ;
2098 base::__sz() += __ds;
2099 __c.__sz() -= __ds;
Howard Hinnant29f74322013-06-25 16:08:472100 __node_pointer __f = __f2.__ptr_;
2101 __node_pointer __l = __m2.__ptr_->__prev_;
Howard Hinnantbc8d3f92010-05-11 19:42:162102 __f2 = __m2;
2103 base::__unlink_nodes(__f, __l);
Howard Hinnant0949eed2011-06-30 21:18:192104 __m2 = _VSTD::next(__f1);
Howard Hinnant29f74322013-06-25 16:08:472105 __link_nodes(__f1.__ptr_, __f, __l);
Howard Hinnantbc8d3f92010-05-11 19:42:162106 __f1 = __m2;
2107 }
2108 else
2109 ++__f1;
2110 }
2111 splice(__e1, __c);
Howard Hinnant1c3ec6d2011-09-27 23:55:032112#if _LIBCPP_DEBUG_LEVEL >= 2
2113 __libcpp_db* __db = __get_db();
2114 __c_node* __cn1 = __db->__find_c_and_lock(this);
2115 __c_node* __cn2 = __db->__find_c(&__c);
2116 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
2117 {
2118 --__p;
2119 iterator* __i = static_cast<iterator*>((*__p)->__i_);
Howard Hinnant29f74322013-06-25 16:08:472120 if (__i->__ptr_ != static_cast<__node_pointer>(
2121 pointer_traits<__node_base_pointer>::pointer_to(__c.__end_)))
Howard Hinnant1c3ec6d2011-09-27 23:55:032122 {
2123 __cn1->__add(*__p);
2124 (*__p)->__c_ = __cn1;
2125 if (--__cn2->end_ != __p)
2126 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
2127 }
2128 }
2129 __db->unlock();
2130#endif
Howard Hinnantbc8d3f92010-05-11 19:42:162131 }
2132}
2133
2134template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:342135inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162136void
2137list<_Tp, _Alloc>::sort()
2138{
2139 sort(__less<value_type>());
2140}
2141
2142template <class _Tp, class _Alloc>
2143template <class _Comp>
Howard Hinnant82894812010-09-22 16:48:342144inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162145void
2146list<_Tp, _Alloc>::sort(_Comp __comp)
2147{
2148 __sort(begin(), end(), base::__sz(), __comp);
2149}
2150
2151template <class _Tp, class _Alloc>
2152template <class _Comp>
Howard Hinnantbc8d3f92010-05-11 19:42:162153typename list<_Tp, _Alloc>::iterator
2154list<_Tp, _Alloc>::__sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp)
2155{
2156 switch (__n)
2157 {
2158 case 0:
2159 case 1:
2160 return __f1;
2161 case 2:
2162 if (__comp(*--__e2, *__f1))
2163 {
Howard Hinnant29f74322013-06-25 16:08:472164 __node_pointer __f = __e2.__ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:162165 base::__unlink_nodes(__f, __f);
Howard Hinnant29f74322013-06-25 16:08:472166 __link_nodes(__f1.__ptr_, __f, __f);
Howard Hinnantbc8d3f92010-05-11 19:42:162167 return __e2;
2168 }
2169 return __f1;
2170 }
2171 size_type __n2 = __n / 2;
Howard Hinnant0949eed2011-06-30 21:18:192172 iterator __e1 = _VSTD::next(__f1, __n2);
Howard Hinnantbc8d3f92010-05-11 19:42:162173 iterator __r = __f1 = __sort(__f1, __e1, __n2, __comp);
2174 iterator __f2 = __e1 = __sort(__e1, __e2, __n - __n2, __comp);
2175 if (__comp(*__f2, *__f1))
2176 {
Howard Hinnant0949eed2011-06-30 21:18:192177 iterator __m2 = _VSTD::next(__f2);
Howard Hinnantbc8d3f92010-05-11 19:42:162178 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2)
2179 ;
Howard Hinnant29f74322013-06-25 16:08:472180 __node_pointer __f = __f2.__ptr_;
2181 __node_pointer __l = __m2.__ptr_->__prev_;
Howard Hinnantbc8d3f92010-05-11 19:42:162182 __r = __f2;
2183 __e1 = __f2 = __m2;
2184 base::__unlink_nodes(__f, __l);
Howard Hinnant0949eed2011-06-30 21:18:192185 __m2 = _VSTD::next(__f1);
Howard Hinnant29f74322013-06-25 16:08:472186 __link_nodes(__f1.__ptr_, __f, __l);
Howard Hinnantbc8d3f92010-05-11 19:42:162187 __f1 = __m2;
2188 }
2189 else
2190 ++__f1;
2191 while (__f1 != __e1 && __f2 != __e2)
2192 {
2193 if (__comp(*__f2, *__f1))
2194 {
Howard Hinnant0949eed2011-06-30 21:18:192195 iterator __m2 = _VSTD::next(__f2);
Howard Hinnantbc8d3f92010-05-11 19:42:162196 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2)
2197 ;
Howard Hinnant29f74322013-06-25 16:08:472198 __node_pointer __f = __f2.__ptr_;
2199 __node_pointer __l = __m2.__ptr_->__prev_;
Howard Hinnantbc8d3f92010-05-11 19:42:162200 if (__e1 == __f2)
2201 __e1 = __m2;
2202 __f2 = __m2;
2203 base::__unlink_nodes(__f, __l);
Howard Hinnant0949eed2011-06-30 21:18:192204 __m2 = _VSTD::next(__f1);
Howard Hinnant29f74322013-06-25 16:08:472205 __link_nodes(__f1.__ptr_, __f, __l);
Howard Hinnantbc8d3f92010-05-11 19:42:162206 __f1 = __m2;
2207 }
2208 else
2209 ++__f1;
2210 }
2211 return __r;
2212}
2213
2214template <class _Tp, class _Alloc>
2215void
Howard Hinnantc5607272011-06-03 17:30:282216list<_Tp, _Alloc>::reverse() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:162217{
2218 if (base::__sz() > 1)
2219 {
2220 iterator __e = end();
Howard Hinnant1c3ec6d2011-09-27 23:55:032221 for (iterator __i = begin(); __i.__ptr_ != __e.__ptr_;)
2222 {
Howard Hinnant0949eed2011-06-30 21:18:192223 _VSTD::swap(__i.__ptr_->__prev_, __i.__ptr_->__next_);
Howard Hinnant1c3ec6d2011-09-27 23:55:032224 __i.__ptr_ = __i.__ptr_->__prev_;
2225 }
Howard Hinnant0949eed2011-06-30 21:18:192226 _VSTD::swap(__e.__ptr_->__prev_, __e.__ptr_->__next_);
Howard Hinnantbc8d3f92010-05-11 19:42:162227 }
2228}
2229
2230template <class _Tp, class _Alloc>
Howard Hinnant1c3ec6d2011-09-27 23:55:032231bool
2232list<_Tp, _Alloc>::__invariants() const
2233{
2234 return size() == _VSTD::distance(begin(), end());
2235}
2236
2237#if _LIBCPP_DEBUG_LEVEL >= 2
2238
2239template <class _Tp, class _Alloc>
2240bool
2241list<_Tp, _Alloc>::__dereferenceable(const const_iterator* __i) const
2242{
Howard Hinnant29f74322013-06-25 16:08:472243 return __i->__ptr_ != static_cast<__node_pointer>(
2244 pointer_traits<__node_base_pointer>::pointer_to(const_cast<__node_base&>(this->__end_)));
Howard Hinnant1c3ec6d2011-09-27 23:55:032245}
2246
2247template <class _Tp, class _Alloc>
2248bool
2249list<_Tp, _Alloc>::__decrementable(const const_iterator* __i) const
2250{
2251 return !empty() && __i->__ptr_ != base::__end_.__next_;
2252}
2253
2254template <class _Tp, class _Alloc>
2255bool
2256list<_Tp, _Alloc>::__addable(const const_iterator* __i, ptrdiff_t __n) const
2257{
2258 return false;
2259}
2260
2261template <class _Tp, class _Alloc>
2262bool
2263list<_Tp, _Alloc>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
2264{
2265 return false;
2266}
2267
2268#endif // _LIBCPP_DEBUG_LEVEL >= 2
2269
2270template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:342271inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162272bool
2273operator==(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2274{
Howard Hinnant0949eed2011-06-30 21:18:192275 return __x.size() == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantbc8d3f92010-05-11 19:42:162276}
2277
2278template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:342279inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162280bool
2281operator< (const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2282{
Howard Hinnant0949eed2011-06-30 21:18:192283 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantbc8d3f92010-05-11 19:42:162284}
2285
2286template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:342287inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162288bool
2289operator!=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2290{
2291 return !(__x == __y);
2292}
2293
2294template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:342295inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162296bool
2297operator> (const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2298{
2299 return __y < __x;
2300}
2301
2302template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:342303inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162304bool
2305operator>=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2306{
2307 return !(__x < __y);
2308}
2309
2310template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:342311inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162312bool
2313operator<=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2314{
2315 return !(__y < __x);
2316}
2317
2318template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:342319inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162320void
2321swap(list<_Tp, _Alloc>& __x, list<_Tp, _Alloc>& __y)
Howard Hinnantc5607272011-06-03 17:30:282322 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantbc8d3f92010-05-11 19:42:162323{
2324 __x.swap(__y);
2325}
2326
2327_LIBCPP_END_NAMESPACE_STD
2328
2329#endif // _LIBCPP_LIST