2 //===---------------------------- list ------------------------------------===//
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
19 template <class T, class Alloc = allocator<T> >
26 typedef Alloc allocator_type;
27 typedef typename allocator_type::reference reference;
28 typedef typename allocator_type::const_reference const_reference;
29 typedef typename allocator_type::pointer pointer;
30 typedef typename allocator_type::const_pointer const_pointer;
31 typedef implementation-defined iterator;
32 typedef implementation-defined const_iterator;
33 typedef implementation-defined size_type;
34 typedef implementation-defined difference_type;
35 typedef reverse_iterator<iterator> reverse_iterator;
36 typedef reverse_iterator<const_iterator> const_reverse_iterator;
39 noexcept(is_nothrow_default_constructible<allocator_type>::value);
40 explicit list(const allocator_type& a);
41 explicit list(size_type n);
42 explicit list(size_type n, const allocator_type& a); // C++14
43 list(size_type n, const value_type& value);
44 list(size_type n, const value_type& value, const allocator_type& a);
46 list(Iter first, Iter last);
48 list(Iter first, Iter last, const allocator_type& a);
50 list(const list&, const allocator_type& a);
52 noexcept(is_nothrow_move_constructible<allocator_type>::value);
53 list(list&&, const allocator_type& a);
54 list(initializer_list<value_type>);
55 list(initializer_list<value_type>, const allocator_type& a);
59 list& operator=(const list& x);
60 list& operator=(list&& x)
62 allocator_type::propagate_on_container_move_assignment::value &&
63 is_nothrow_move_assignable<allocator_type>::value);
64 list& operator=(initializer_list<value_type>);
66 void assign(Iter first, Iter last);
67 void assign(size_type n, const value_type& t);
68 void assign(initializer_list<value_type>);
70 allocator_type get_allocator() const noexcept;
72 iterator begin() noexcept;
73 const_iterator begin() const noexcept;
74 iterator end() noexcept;
75 const_iterator end() const noexcept;
76 reverse_iterator rbegin() noexcept;
77 const_reverse_iterator rbegin() const noexcept;
78 reverse_iterator rend() noexcept;
79 const_reverse_iterator rend() const noexcept;
80 const_iterator cbegin() const noexcept;
81 const_iterator cend() const noexcept;
82 const_reverse_iterator crbegin() const noexcept;
83 const_reverse_iterator crend() const noexcept;
86 const_reference front() const;
88 const_reference back() const;
90 bool empty() const noexcept;
91 size_type size() const noexcept;
92 size_type max_size() const noexcept;
94 template <class... Args>
95 reference emplace_front(Args&&... args); // reference in C++17
97 template <class... Args>
98 reference emplace_back(Args&&... args); // reference in C++17
100 void push_front(const value_type& x);
101 void push_front(value_type&& x);
102 void push_back(const value_type& x);
103 void push_back(value_type&& x);
104 template <class... Args>
105 iterator emplace(const_iterator position, Args&&... args);
106 iterator insert(const_iterator position, const value_type& x);
107 iterator insert(const_iterator position, value_type&& x);
108 iterator insert(const_iterator position, size_type n, const value_type& x);
109 template <class Iter>
110 iterator insert(const_iterator position, Iter first, Iter last);
111 iterator insert(const_iterator position, initializer_list<value_type> il);
113 iterator erase(const_iterator position);
114 iterator erase(const_iterator position, const_iterator last);
116 void resize(size_type sz);
117 void resize(size_type sz, const value_type& c);
120 noexcept(allocator_traits<allocator_type>::is_always_equal::value); // C++17
121 void clear() noexcept;
123 void splice(const_iterator position, list& x);
124 void splice(const_iterator position, list&& x);
125 void splice(const_iterator position, list& x, const_iterator i);
126 void splice(const_iterator position, list&& x, const_iterator i);
127 void splice(const_iterator position, list& x, const_iterator first,
128 const_iterator last);
129 void splice(const_iterator position, list&& x, const_iterator first,
130 const_iterator last);
132 size_type remove(const value_type& value); // void before C++20
133 template <class Pred>
134 size_type remove_if(Pred pred); // void before C++20
135 size_type unique(); // void before C++20
136 template <class BinaryPredicate>
137 size_type unique(BinaryPredicate binary_pred); // void before C++20
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);
145 template <class Compare>
146 void sort(Compare comp);
147 void reverse() noexcept;
151 template <class InputIterator, class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
152 list(InputIterator, InputIterator, Allocator = Allocator())
153 -> list<typename iterator_traits<InputIterator>::value_type, Allocator>; // C++17
155 template <class T, class Alloc>
156 bool operator==(const list<T,Alloc>& x, const list<T,Alloc>& y);
157 template <class T, class Alloc>
158 bool operator< (const list<T,Alloc>& x, const list<T,Alloc>& y);
159 template <class T, class Alloc>
160 bool operator!=(const list<T,Alloc>& x, const list<T,Alloc>& y);
161 template <class T, class Alloc>
162 bool operator> (const list<T,Alloc>& x, const list<T,Alloc>& y);
163 template <class T, class Alloc>
164 bool operator>=(const list<T,Alloc>& x, const list<T,Alloc>& y);
165 template <class T, class Alloc>
166 bool operator<=(const list<T,Alloc>& x, const list<T,Alloc>& y);
168 template <class T, class Alloc>
169 void swap(list<T,Alloc>& x, list<T,Alloc>& y)
170 noexcept(noexcept(x.swap(y)));
172 template <class T, class Allocator, class U>
173 void erase(list<T, Allocator>& c, const U& value); // C++20
174 template <class T, class Allocator, class Predicate>
175 void erase_if(list<T, Allocator>& c, Predicate pred); // C++20
185 #include <initializer_list>
188 #include <type_traits>
193 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
194 #pragma GCC system_header
198 #include <__undef_macros>
201 _LIBCPP_BEGIN_NAMESPACE_STD
203 template <class _Tp, class _VoidPtr> struct __list_node;
204 template <class _Tp, class _VoidPtr> struct __list_node_base;
206 template <class _Tp, class _VoidPtr>
207 struct __list_node_pointer_traits {
208 typedef typename __rebind_pointer<_VoidPtr, __list_node<_Tp, _VoidPtr> >::type
210 typedef typename __rebind_pointer<_VoidPtr, __list_node_base<_Tp, _VoidPtr> >::type
213 #if defined(_LIBCPP_ABI_LIST_REMOVE_NODE_POINTER_UB)
214 typedef __base_pointer __link_pointer;
216 typedef typename conditional<
217 is_pointer<_VoidPtr>::value,
220 >::type __link_pointer;
223 typedef typename conditional<
224 is_same<__link_pointer, __node_pointer>::value,
227 >::type __non_link_pointer;
229 static _LIBCPP_INLINE_VISIBILITY
230 __link_pointer __unsafe_link_pointer_cast(__link_pointer __p) {
234 static _LIBCPP_INLINE_VISIBILITY
235 __link_pointer __unsafe_link_pointer_cast(__non_link_pointer __p) {
236 return static_cast<__link_pointer>(static_cast<_VoidPtr>(__p));
241 template <class _Tp, class _VoidPtr>
242 struct __list_node_base
244 typedef __list_node_pointer_traits<_Tp, _VoidPtr> _NodeTraits;
245 typedef typename _NodeTraits::__node_pointer __node_pointer;
246 typedef typename _NodeTraits::__base_pointer __base_pointer;
247 typedef typename _NodeTraits::__link_pointer __link_pointer;
249 __link_pointer __prev_;
250 __link_pointer __next_;
252 _LIBCPP_INLINE_VISIBILITY
253 __list_node_base() : __prev_(_NodeTraits::__unsafe_link_pointer_cast(__self())),
254 __next_(_NodeTraits::__unsafe_link_pointer_cast(__self())) {}
256 _LIBCPP_INLINE_VISIBILITY
257 __base_pointer __self() {
258 return pointer_traits<__base_pointer>::pointer_to(*this);
261 _LIBCPP_INLINE_VISIBILITY
262 __node_pointer __as_node() {
263 return static_cast<__node_pointer>(__self());
267 template <class _Tp, class _VoidPtr>
269 : public __list_node_base<_Tp, _VoidPtr>
273 typedef __list_node_base<_Tp, _VoidPtr> __base;
274 typedef typename __base::__link_pointer __link_pointer;
276 _LIBCPP_INLINE_VISIBILITY
277 __link_pointer __as_link() {
278 return static_cast<__link_pointer>(__base::__self());
282 template <class _Tp, class _Alloc = allocator<_Tp> > class _LIBCPP_TEMPLATE_VIS list;
283 template <class _Tp, class _Alloc> class __list_imp;
284 template <class _Tp, class _VoidPtr> class _LIBCPP_TEMPLATE_VIS __list_const_iterator;
286 template <class _Tp, class _VoidPtr>
287 class _LIBCPP_TEMPLATE_VIS __list_iterator
289 typedef __list_node_pointer_traits<_Tp, _VoidPtr> _NodeTraits;
290 typedef typename _NodeTraits::__link_pointer __link_pointer;
292 __link_pointer __ptr_;
294 #if _LIBCPP_DEBUG_LEVEL >= 2
295 _LIBCPP_INLINE_VISIBILITY
296 explicit __list_iterator(__link_pointer __p, const void* __c) _NOEXCEPT
299 __get_db()->__insert_ic(this, __c);
302 _LIBCPP_INLINE_VISIBILITY
303 explicit __list_iterator(__link_pointer __p) _NOEXCEPT : __ptr_(__p) {}
308 template<class, class> friend class list;
309 template<class, class> friend class __list_imp;
310 template<class, class> friend class __list_const_iterator;
312 typedef bidirectional_iterator_tag iterator_category;
313 typedef _Tp value_type;
314 typedef value_type& reference;
315 typedef typename __rebind_pointer<_VoidPtr, value_type>::type pointer;
316 typedef typename pointer_traits<pointer>::difference_type difference_type;
318 _LIBCPP_INLINE_VISIBILITY
319 __list_iterator() _NOEXCEPT : __ptr_(nullptr)
321 #if _LIBCPP_DEBUG_LEVEL >= 2
322 __get_db()->__insert_i(this);
326 #if _LIBCPP_DEBUG_LEVEL >= 2
328 _LIBCPP_INLINE_VISIBILITY
329 __list_iterator(const __list_iterator& __p)
332 __get_db()->__iterator_copy(this, &__p);
335 _LIBCPP_INLINE_VISIBILITY
338 __get_db()->__erase_i(this);
341 _LIBCPP_INLINE_VISIBILITY
342 __list_iterator& operator=(const __list_iterator& __p)
346 __get_db()->__iterator_copy(this, &__p);
352 #endif // _LIBCPP_DEBUG_LEVEL >= 2
354 _LIBCPP_INLINE_VISIBILITY
355 reference operator*() const
357 #if _LIBCPP_DEBUG_LEVEL >= 2
358 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
359 "Attempted to dereference a non-dereferenceable list::iterator");
361 return __ptr_->__as_node()->__value_;
363 _LIBCPP_INLINE_VISIBILITY
364 pointer operator->() const
366 #if _LIBCPP_DEBUG_LEVEL >= 2
367 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
368 "Attempted to dereference a non-dereferenceable list::iterator");
370 return pointer_traits<pointer>::pointer_to(__ptr_->__as_node()->__value_);
373 _LIBCPP_INLINE_VISIBILITY
374 __list_iterator& operator++()
376 #if _LIBCPP_DEBUG_LEVEL >= 2
377 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
378 "Attempted to increment non-incrementable list::iterator");
380 __ptr_ = __ptr_->__next_;
383 _LIBCPP_INLINE_VISIBILITY
384 __list_iterator operator++(int) {__list_iterator __t(*this); ++(*this); return __t;}
386 _LIBCPP_INLINE_VISIBILITY
387 __list_iterator& operator--()
389 #if _LIBCPP_DEBUG_LEVEL >= 2
390 _LIBCPP_ASSERT(__get_const_db()->__decrementable(this),
391 "Attempted to decrement non-decrementable list::iterator");
393 __ptr_ = __ptr_->__prev_;
396 _LIBCPP_INLINE_VISIBILITY
397 __list_iterator operator--(int) {__list_iterator __t(*this); --(*this); return __t;}
399 friend _LIBCPP_INLINE_VISIBILITY
400 bool operator==(const __list_iterator& __x, const __list_iterator& __y)
402 return __x.__ptr_ == __y.__ptr_;
404 friend _LIBCPP_INLINE_VISIBILITY
405 bool operator!=(const __list_iterator& __x, const __list_iterator& __y)
406 {return !(__x == __y);}
409 template <class _Tp, class _VoidPtr>
410 class _LIBCPP_TEMPLATE_VIS __list_const_iterator
412 typedef __list_node_pointer_traits<_Tp, _VoidPtr> _NodeTraits;
413 typedef typename _NodeTraits::__link_pointer __link_pointer;
415 __link_pointer __ptr_;
417 #if _LIBCPP_DEBUG_LEVEL >= 2
418 _LIBCPP_INLINE_VISIBILITY
419 explicit __list_const_iterator(__link_pointer __p, const void* __c) _NOEXCEPT
422 __get_db()->__insert_ic(this, __c);
425 _LIBCPP_INLINE_VISIBILITY
426 explicit __list_const_iterator(__link_pointer __p) _NOEXCEPT : __ptr_(__p) {}
429 template<class, class> friend class list;
430 template<class, class> friend class __list_imp;
432 typedef bidirectional_iterator_tag iterator_category;
433 typedef _Tp value_type;
434 typedef const value_type& reference;
435 typedef typename __rebind_pointer<_VoidPtr, const value_type>::type pointer;
436 typedef typename pointer_traits<pointer>::difference_type difference_type;
438 _LIBCPP_INLINE_VISIBILITY
439 __list_const_iterator() _NOEXCEPT : __ptr_(nullptr)
441 #if _LIBCPP_DEBUG_LEVEL >= 2
442 __get_db()->__insert_i(this);
445 _LIBCPP_INLINE_VISIBILITY
446 __list_const_iterator(const __list_iterator<_Tp, _VoidPtr>& __p) _NOEXCEPT
449 #if _LIBCPP_DEBUG_LEVEL >= 2
450 __get_db()->__iterator_copy(this, &__p);
454 #if _LIBCPP_DEBUG_LEVEL >= 2
456 _LIBCPP_INLINE_VISIBILITY
457 __list_const_iterator(const __list_const_iterator& __p)
460 __get_db()->__iterator_copy(this, &__p);
463 _LIBCPP_INLINE_VISIBILITY
464 ~__list_const_iterator()
466 __get_db()->__erase_i(this);
469 _LIBCPP_INLINE_VISIBILITY
470 __list_const_iterator& operator=(const __list_const_iterator& __p)
474 __get_db()->__iterator_copy(this, &__p);
480 #endif // _LIBCPP_DEBUG_LEVEL >= 2
481 _LIBCPP_INLINE_VISIBILITY
482 reference operator*() const
484 #if _LIBCPP_DEBUG_LEVEL >= 2
485 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
486 "Attempted to dereference a non-dereferenceable list::const_iterator");
488 return __ptr_->__as_node()->__value_;
490 _LIBCPP_INLINE_VISIBILITY
491 pointer operator->() const
493 #if _LIBCPP_DEBUG_LEVEL >= 2
494 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
495 "Attempted to dereference a non-dereferenceable list::const_iterator");
497 return pointer_traits<pointer>::pointer_to(__ptr_->__as_node()->__value_);
500 _LIBCPP_INLINE_VISIBILITY
501 __list_const_iterator& operator++()
503 #if _LIBCPP_DEBUG_LEVEL >= 2
504 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
505 "Attempted to increment non-incrementable list::const_iterator");
507 __ptr_ = __ptr_->__next_;
510 _LIBCPP_INLINE_VISIBILITY
511 __list_const_iterator operator++(int) {__list_const_iterator __t(*this); ++(*this); return __t;}
513 _LIBCPP_INLINE_VISIBILITY
514 __list_const_iterator& operator--()
516 #if _LIBCPP_DEBUG_LEVEL >= 2
517 _LIBCPP_ASSERT(__get_const_db()->__decrementable(this),
518 "Attempted to decrement non-decrementable list::const_iterator");
520 __ptr_ = __ptr_->__prev_;
523 _LIBCPP_INLINE_VISIBILITY
524 __list_const_iterator operator--(int) {__list_const_iterator __t(*this); --(*this); return __t;}
526 friend _LIBCPP_INLINE_VISIBILITY
527 bool operator==(const __list_const_iterator& __x, const __list_const_iterator& __y)
529 return __x.__ptr_ == __y.__ptr_;
531 friend _LIBCPP_INLINE_VISIBILITY
532 bool operator!=(const __list_const_iterator& __x, const __list_const_iterator& __y)
533 {return !(__x == __y);}
536 template <class _Tp, class _Alloc>
539 __list_imp(const __list_imp&);
540 __list_imp& operator=(const __list_imp&);
542 typedef _Alloc allocator_type;
543 typedef allocator_traits<allocator_type> __alloc_traits;
544 typedef typename __alloc_traits::size_type size_type;
546 typedef _Tp value_type;
547 typedef typename __alloc_traits::void_pointer __void_pointer;
548 typedef __list_iterator<value_type, __void_pointer> iterator;
549 typedef __list_const_iterator<value_type, __void_pointer> const_iterator;
550 typedef __list_node_base<value_type, __void_pointer> __node_base;
551 typedef __list_node<value_type, __void_pointer> __node;
552 typedef typename __rebind_alloc_helper<__alloc_traits, __node>::type __node_allocator;
553 typedef allocator_traits<__node_allocator> __node_alloc_traits;
554 typedef typename __node_alloc_traits::pointer __node_pointer;
555 typedef typename __node_alloc_traits::pointer __node_const_pointer;
556 typedef __list_node_pointer_traits<value_type, __void_pointer> __node_pointer_traits;
557 typedef typename __node_pointer_traits::__link_pointer __link_pointer;
558 typedef __link_pointer __link_const_pointer;
559 typedef typename __alloc_traits::pointer pointer;
560 typedef typename __alloc_traits::const_pointer const_pointer;
561 typedef typename __alloc_traits::difference_type difference_type;
563 typedef typename __rebind_alloc_helper<__alloc_traits, __node_base>::type __node_base_allocator;
564 typedef typename allocator_traits<__node_base_allocator>::pointer __node_base_pointer;
565 static_assert((!is_same<allocator_type, __node_allocator>::value),
566 "internal allocator type must differ from user-specified "
567 "type; otherwise overload resolution breaks");
570 __compressed_pair<size_type, __node_allocator> __size_alloc_;
572 _LIBCPP_INLINE_VISIBILITY
573 __link_pointer __end_as_link() const _NOEXCEPT {
574 return __node_pointer_traits::__unsafe_link_pointer_cast(
575 const_cast<__node_base&>(__end_).__self());
578 _LIBCPP_INLINE_VISIBILITY
579 size_type& __sz() _NOEXCEPT {return __size_alloc_.first();}
580 _LIBCPP_INLINE_VISIBILITY
581 const size_type& __sz() const _NOEXCEPT
582 {return __size_alloc_.first();}
583 _LIBCPP_INLINE_VISIBILITY
584 __node_allocator& __node_alloc() _NOEXCEPT
585 {return __size_alloc_.second();}
586 _LIBCPP_INLINE_VISIBILITY
587 const __node_allocator& __node_alloc() const _NOEXCEPT
588 {return __size_alloc_.second();}
590 _LIBCPP_INLINE_VISIBILITY
591 size_type __node_alloc_max_size() const _NOEXCEPT {
592 return __node_alloc_traits::max_size(__node_alloc());
594 _LIBCPP_INLINE_VISIBILITY
595 static void __unlink_nodes(__link_pointer __f, __link_pointer __l) _NOEXCEPT;
597 _LIBCPP_INLINE_VISIBILITY
599 _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value);
600 _LIBCPP_INLINE_VISIBILITY
601 __list_imp(const allocator_type& __a);
602 _LIBCPP_INLINE_VISIBILITY
603 __list_imp(const __node_allocator& __a);
604 #ifndef _LIBCPP_CXX03_LANG
605 __list_imp(__node_allocator&& __a) _NOEXCEPT;
608 void clear() _NOEXCEPT;
609 _LIBCPP_INLINE_VISIBILITY
610 bool empty() const _NOEXCEPT {return __sz() == 0;}
612 _LIBCPP_INLINE_VISIBILITY
613 iterator begin() _NOEXCEPT
615 #if _LIBCPP_DEBUG_LEVEL >= 2
616 return iterator(__end_.__next_, this);
618 return iterator(__end_.__next_);
621 _LIBCPP_INLINE_VISIBILITY
622 const_iterator begin() const _NOEXCEPT
624 #if _LIBCPP_DEBUG_LEVEL >= 2
625 return const_iterator(__end_.__next_, this);
627 return const_iterator(__end_.__next_);
630 _LIBCPP_INLINE_VISIBILITY
631 iterator end() _NOEXCEPT
633 #if _LIBCPP_DEBUG_LEVEL >= 2
634 return iterator(__end_as_link(), this);
636 return iterator(__end_as_link());
639 _LIBCPP_INLINE_VISIBILITY
640 const_iterator end() const _NOEXCEPT
642 #if _LIBCPP_DEBUG_LEVEL >= 2
643 return const_iterator(__end_as_link(), this);
645 return const_iterator(__end_as_link());
649 void swap(__list_imp& __c)
650 #if _LIBCPP_STD_VER >= 14
653 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
654 __is_nothrow_swappable<allocator_type>::value);
657 _LIBCPP_INLINE_VISIBILITY
658 void __copy_assign_alloc(const __list_imp& __c)
659 {__copy_assign_alloc(__c, integral_constant<bool,
660 __node_alloc_traits::propagate_on_container_copy_assignment::value>());}
662 _LIBCPP_INLINE_VISIBILITY
663 void __move_assign_alloc(__list_imp& __c)
665 !__node_alloc_traits::propagate_on_container_move_assignment::value ||
666 is_nothrow_move_assignable<__node_allocator>::value)
667 {__move_assign_alloc(__c, integral_constant<bool,
668 __node_alloc_traits::propagate_on_container_move_assignment::value>());}
671 _LIBCPP_INLINE_VISIBILITY
672 void __copy_assign_alloc(const __list_imp& __c, true_type)
674 if (__node_alloc() != __c.__node_alloc())
676 __node_alloc() = __c.__node_alloc();
679 _LIBCPP_INLINE_VISIBILITY
680 void __copy_assign_alloc(const __list_imp&, false_type)
683 _LIBCPP_INLINE_VISIBILITY
684 void __move_assign_alloc(__list_imp& __c, true_type)
685 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
687 __node_alloc() = _VSTD::move(__c.__node_alloc());
690 _LIBCPP_INLINE_VISIBILITY
691 void __move_assign_alloc(__list_imp&, false_type)
695 _LIBCPP_INLINE_VISIBILITY
696 void __invalidate_all_iterators() {
697 #if _LIBCPP_DEBUG_LEVEL >= 2
698 __get_db()->__invalidate_all(this);
703 // Unlink nodes [__f, __l]
704 template <class _Tp, class _Alloc>
707 __list_imp<_Tp, _Alloc>::__unlink_nodes(__link_pointer __f, __link_pointer __l)
710 __f->__prev_->__next_ = __l->__next_;
711 __l->__next_->__prev_ = __f->__prev_;
714 template <class _Tp, class _Alloc>
716 __list_imp<_Tp, _Alloc>::__list_imp()
717 _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value)
722 template <class _Tp, class _Alloc>
724 __list_imp<_Tp, _Alloc>::__list_imp(const allocator_type& __a)
725 : __size_alloc_(0, __node_allocator(__a))
729 template <class _Tp, class _Alloc>
730 inline __list_imp<_Tp, _Alloc>::__list_imp(const __node_allocator& __a)
731 : __size_alloc_(0, __a) {}
733 #ifndef _LIBCPP_CXX03_LANG
734 template <class _Tp, class _Alloc>
735 inline __list_imp<_Tp, _Alloc>::__list_imp(__node_allocator&& __a) _NOEXCEPT
736 : __size_alloc_(0, std::move(__a)) {}
739 template <class _Tp, class _Alloc>
740 __list_imp<_Tp, _Alloc>::~__list_imp() {
742 #if _LIBCPP_DEBUG_LEVEL >= 2
743 __get_db()->__erase_c(this);
747 template <class _Tp, class _Alloc>
749 __list_imp<_Tp, _Alloc>::clear() _NOEXCEPT
753 __node_allocator& __na = __node_alloc();
754 __link_pointer __f = __end_.__next_;
755 __link_pointer __l = __end_as_link();
756 __unlink_nodes(__f, __l->__prev_);
760 __node_pointer __np = __f->__as_node();
762 __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
763 __node_alloc_traits::deallocate(__na, __np, 1);
765 __invalidate_all_iterators();
769 template <class _Tp, class _Alloc>
771 __list_imp<_Tp, _Alloc>::swap(__list_imp& __c)
772 #if _LIBCPP_STD_VER >= 14
775 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
776 __is_nothrow_swappable<allocator_type>::value)
779 _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
780 this->__node_alloc() == __c.__node_alloc(),
781 "list::swap: Either propagate_on_container_swap must be true"
782 " or the allocators must compare equal");
784 __swap_allocator(__node_alloc(), __c.__node_alloc());
785 swap(__sz(), __c.__sz());
786 swap(__end_, __c.__end_);
788 __end_.__next_ = __end_.__prev_ = __end_as_link();
790 __end_.__prev_->__next_ = __end_.__next_->__prev_ = __end_as_link();
792 __c.__end_.__next_ = __c.__end_.__prev_ = __c.__end_as_link();
794 __c.__end_.__prev_->__next_ = __c.__end_.__next_->__prev_ = __c.__end_as_link();
796 #if _LIBCPP_DEBUG_LEVEL >= 2
797 __libcpp_db* __db = __get_db();
798 __c_node* __cn1 = __db->__find_c_and_lock(this);
799 __c_node* __cn2 = __db->__find_c(&__c);
800 std::swap(__cn1->beg_, __cn2->beg_);
801 std::swap(__cn1->end_, __cn2->end_);
802 std::swap(__cn1->cap_, __cn2->cap_);
803 for (__i_node** __p = __cn1->end_; __p != __cn1->beg_;)
806 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
807 if (__i->__ptr_ == __c.__end_as_link())
810 if (--__cn1->end_ != __p)
811 memmove(__p, __p+1, (__cn1->end_ - __p)*sizeof(__i_node*));
814 (*__p)->__c_ = __cn1;
816 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
819 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
820 if (__i->__ptr_ == __end_as_link())
823 if (--__cn2->end_ != __p)
824 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
827 (*__p)->__c_ = __cn2;
833 template <class _Tp, class _Alloc /*= allocator<_Tp>*/>
834 class _LIBCPP_TEMPLATE_VIS list
835 : private __list_imp<_Tp, _Alloc>
837 typedef __list_imp<_Tp, _Alloc> base;
838 typedef typename base::__node __node;
839 typedef typename base::__node_allocator __node_allocator;
840 typedef typename base::__node_pointer __node_pointer;
841 typedef typename base::__node_alloc_traits __node_alloc_traits;
842 typedef typename base::__node_base __node_base;
843 typedef typename base::__node_base_pointer __node_base_pointer;
844 typedef typename base::__link_pointer __link_pointer;
847 typedef _Tp value_type;
848 typedef _Alloc allocator_type;
849 static_assert((is_same<value_type, typename allocator_type::value_type>::value),
850 "Invalid allocator::value_type");
851 typedef value_type& reference;
852 typedef const value_type& const_reference;
853 typedef typename base::pointer pointer;
854 typedef typename base::const_pointer const_pointer;
855 typedef typename base::size_type size_type;
856 typedef typename base::difference_type difference_type;
857 typedef typename base::iterator iterator;
858 typedef typename base::const_iterator const_iterator;
859 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
860 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
861 #if _LIBCPP_STD_VER > 17
862 typedef size_type __remove_return_type;
864 typedef void __remove_return_type;
867 _LIBCPP_INLINE_VISIBILITY
869 _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value)
871 #if _LIBCPP_DEBUG_LEVEL >= 2
872 __get_db()->__insert_c(this);
875 _LIBCPP_INLINE_VISIBILITY
876 explicit list(const allocator_type& __a) : base(__a)
878 #if _LIBCPP_DEBUG_LEVEL >= 2
879 __get_db()->__insert_c(this);
882 explicit list(size_type __n);
883 #if _LIBCPP_STD_VER > 11
884 explicit list(size_type __n, const allocator_type& __a);
886 list(size_type __n, const value_type& __x);
887 list(size_type __n, const value_type& __x, const allocator_type& __a);
888 template <class _InpIter>
889 list(_InpIter __f, _InpIter __l,
890 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
891 template <class _InpIter>
892 list(_InpIter __f, _InpIter __l, const allocator_type& __a,
893 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
895 list(const list& __c);
896 list(const list& __c, const allocator_type& __a);
897 _LIBCPP_INLINE_VISIBILITY
898 list& operator=(const list& __c);
899 #ifndef _LIBCPP_CXX03_LANG
900 list(initializer_list<value_type> __il);
901 list(initializer_list<value_type> __il, const allocator_type& __a);
903 _LIBCPP_INLINE_VISIBILITY
905 _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value);
906 _LIBCPP_INLINE_VISIBILITY
907 list(list&& __c, const allocator_type& __a);
908 _LIBCPP_INLINE_VISIBILITY
909 list& operator=(list&& __c)
911 __node_alloc_traits::propagate_on_container_move_assignment::value &&
912 is_nothrow_move_assignable<__node_allocator>::value);
914 _LIBCPP_INLINE_VISIBILITY
915 list& operator=(initializer_list<value_type> __il)
916 {assign(__il.begin(), __il.end()); return *this;}
918 _LIBCPP_INLINE_VISIBILITY
919 void assign(initializer_list<value_type> __il)
920 {assign(__il.begin(), __il.end());}
921 #endif // _LIBCPP_CXX03_LANG
923 template <class _InpIter>
924 void assign(_InpIter __f, _InpIter __l,
925 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
926 void assign(size_type __n, const value_type& __x);
928 _LIBCPP_INLINE_VISIBILITY
929 allocator_type get_allocator() const _NOEXCEPT;
931 _LIBCPP_INLINE_VISIBILITY
932 size_type size() const _NOEXCEPT {return base::__sz();}
933 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
934 bool empty() const _NOEXCEPT {return base::empty();}
935 _LIBCPP_INLINE_VISIBILITY
936 size_type max_size() const _NOEXCEPT
938 return std::min<size_type>(
939 base::__node_alloc_max_size(),
940 numeric_limits<difference_type >::max());
943 _LIBCPP_INLINE_VISIBILITY
944 iterator begin() _NOEXCEPT {return base::begin();}
945 _LIBCPP_INLINE_VISIBILITY
946 const_iterator begin() const _NOEXCEPT {return base::begin();}
947 _LIBCPP_INLINE_VISIBILITY
948 iterator end() _NOEXCEPT {return base::end();}
949 _LIBCPP_INLINE_VISIBILITY
950 const_iterator end() const _NOEXCEPT {return base::end();}
951 _LIBCPP_INLINE_VISIBILITY
952 const_iterator cbegin() const _NOEXCEPT {return base::begin();}
953 _LIBCPP_INLINE_VISIBILITY
954 const_iterator cend() const _NOEXCEPT {return base::end();}
956 _LIBCPP_INLINE_VISIBILITY
957 reverse_iterator rbegin() _NOEXCEPT
958 {return reverse_iterator(end());}
959 _LIBCPP_INLINE_VISIBILITY
960 const_reverse_iterator rbegin() const _NOEXCEPT
961 {return const_reverse_iterator(end());}
962 _LIBCPP_INLINE_VISIBILITY
963 reverse_iterator rend() _NOEXCEPT
964 {return reverse_iterator(begin());}
965 _LIBCPP_INLINE_VISIBILITY
966 const_reverse_iterator rend() const _NOEXCEPT
967 {return const_reverse_iterator(begin());}
968 _LIBCPP_INLINE_VISIBILITY
969 const_reverse_iterator crbegin() const _NOEXCEPT
970 {return const_reverse_iterator(end());}
971 _LIBCPP_INLINE_VISIBILITY
972 const_reverse_iterator crend() const _NOEXCEPT
973 {return const_reverse_iterator(begin());}
975 _LIBCPP_INLINE_VISIBILITY
978 _LIBCPP_ASSERT(!empty(), "list::front called on empty list");
979 return base::__end_.__next_->__as_node()->__value_;
981 _LIBCPP_INLINE_VISIBILITY
982 const_reference front() const
984 _LIBCPP_ASSERT(!empty(), "list::front called on empty list");
985 return base::__end_.__next_->__as_node()->__value_;
987 _LIBCPP_INLINE_VISIBILITY
990 _LIBCPP_ASSERT(!empty(), "list::back called on empty list");
991 return base::__end_.__prev_->__as_node()->__value_;
993 _LIBCPP_INLINE_VISIBILITY
994 const_reference back() const
996 _LIBCPP_ASSERT(!empty(), "list::back called on empty list");
997 return base::__end_.__prev_->__as_node()->__value_;
1000 #ifndef _LIBCPP_CXX03_LANG
1001 void push_front(value_type&& __x);
1002 void push_back(value_type&& __x);
1004 template <class... _Args>
1005 #if _LIBCPP_STD_VER > 14
1006 reference emplace_front(_Args&&... __args);
1008 void emplace_front(_Args&&... __args);
1010 template <class... _Args>
1011 #if _LIBCPP_STD_VER > 14
1012 reference emplace_back(_Args&&... __args);
1014 void emplace_back(_Args&&... __args);
1016 template <class... _Args>
1017 iterator emplace(const_iterator __p, _Args&&... __args);
1019 iterator insert(const_iterator __p, value_type&& __x);
1021 _LIBCPP_INLINE_VISIBILITY
1022 iterator insert(const_iterator __p, initializer_list<value_type> __il)
1023 {return insert(__p, __il.begin(), __il.end());}
1024 #endif // _LIBCPP_CXX03_LANG
1026 void push_front(const value_type& __x);
1027 void push_back(const value_type& __x);
1029 #ifndef _LIBCPP_CXX03_LANG
1030 template <class _Arg>
1031 _LIBCPP_INLINE_VISIBILITY
1032 void __emplace_back(_Arg&& __arg) { emplace_back(_VSTD::forward<_Arg>(__arg)); }
1034 _LIBCPP_INLINE_VISIBILITY
1035 void __emplace_back(value_type const& __arg) { push_back(__arg); }
1038 iterator insert(const_iterator __p, const value_type& __x);
1039 iterator insert(const_iterator __p, size_type __n, const value_type& __x);
1040 template <class _InpIter>
1041 iterator insert(const_iterator __p, _InpIter __f, _InpIter __l,
1042 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
1044 _LIBCPP_INLINE_VISIBILITY
1045 void swap(list& __c)
1046 #if _LIBCPP_STD_VER >= 14
1049 _NOEXCEPT_(!__node_alloc_traits::propagate_on_container_swap::value ||
1050 __is_nothrow_swappable<__node_allocator>::value)
1053 _LIBCPP_INLINE_VISIBILITY
1054 void clear() _NOEXCEPT {base::clear();}
1059 iterator erase(const_iterator __p);
1060 iterator erase(const_iterator __f, const_iterator __l);
1062 void resize(size_type __n);
1063 void resize(size_type __n, const value_type& __x);
1065 void splice(const_iterator __p, list& __c);
1066 #ifndef _LIBCPP_CXX03_LANG
1067 _LIBCPP_INLINE_VISIBILITY
1068 void splice(const_iterator __p, list&& __c) {splice(__p, __c);}
1069 _LIBCPP_INLINE_VISIBILITY
1070 void splice(const_iterator __p, list&& __c, const_iterator __i)
1071 {splice(__p, __c, __i);}
1072 _LIBCPP_INLINE_VISIBILITY
1073 void splice(const_iterator __p, list&& __c, const_iterator __f, const_iterator __l)
1074 {splice(__p, __c, __f, __l);}
1076 void splice(const_iterator __p, list& __c, const_iterator __i);
1077 void splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l);
1079 __remove_return_type remove(const value_type& __x);
1080 template <class _Pred> __remove_return_type remove_if(_Pred __pred);
1081 _LIBCPP_INLINE_VISIBILITY
1082 __remove_return_type unique() { return unique(__equal_to<value_type>()); }
1083 template <class _BinaryPred>
1084 __remove_return_type unique(_BinaryPred __binary_pred);
1085 _LIBCPP_INLINE_VISIBILITY
1086 void merge(list& __c);
1087 #ifndef _LIBCPP_CXX03_LANG
1088 _LIBCPP_INLINE_VISIBILITY
1089 void merge(list&& __c) {merge(__c);}
1091 template <class _Comp>
1092 _LIBCPP_INLINE_VISIBILITY
1093 void merge(list&& __c, _Comp __comp) {merge(__c, __comp);}
1095 template <class _Comp>
1096 void merge(list& __c, _Comp __comp);
1098 _LIBCPP_INLINE_VISIBILITY
1100 template <class _Comp>
1101 _LIBCPP_INLINE_VISIBILITY
1102 void sort(_Comp __comp);
1104 void reverse() _NOEXCEPT;
1106 bool __invariants() const;
1108 typedef __allocator_destructor<__node_allocator> __node_destructor;
1109 typedef unique_ptr<__node, __node_destructor> __hold_pointer;
1111 _LIBCPP_INLINE_VISIBILITY
1112 __hold_pointer __allocate_node(__node_allocator& __na) {
1113 __node_pointer __p = __node_alloc_traits::allocate(__na, 1);
1114 __p->__prev_ = nullptr;
1115 return __hold_pointer(__p, __node_destructor(__na, 1));
1118 #if _LIBCPP_DEBUG_LEVEL >= 2
1120 bool __dereferenceable(const const_iterator* __i) const;
1121 bool __decrementable(const const_iterator* __i) const;
1122 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1123 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1125 #endif // _LIBCPP_DEBUG_LEVEL >= 2
1128 _LIBCPP_INLINE_VISIBILITY
1129 static void __link_nodes (__link_pointer __p, __link_pointer __f, __link_pointer __l);
1130 _LIBCPP_INLINE_VISIBILITY
1131 void __link_nodes_at_front(__link_pointer __f, __link_pointer __l);
1132 _LIBCPP_INLINE_VISIBILITY
1133 void __link_nodes_at_back (__link_pointer __f, __link_pointer __l);
1134 iterator __iterator(size_type __n);
1135 template <class _Comp>
1136 static iterator __sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp);
1138 void __move_assign(list& __c, true_type)
1139 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value);
1140 void __move_assign(list& __c, false_type);
1143 #ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
1144 template<class _InputIterator,
1145 class _Alloc = typename std::allocator<typename iterator_traits<_InputIterator>::value_type>,
1146 class = typename enable_if<__is_allocator<_Alloc>::value, void>::type
1148 list(_InputIterator, _InputIterator)
1149 -> list<typename iterator_traits<_InputIterator>::value_type, _Alloc>;
1151 template<class _InputIterator,
1153 class = typename enable_if<__is_allocator<_Alloc>::value, void>::type
1155 list(_InputIterator, _InputIterator, _Alloc)
1156 -> list<typename iterator_traits<_InputIterator>::value_type, _Alloc>;
1159 // Link in nodes [__f, __l] just prior to __p
1160 template <class _Tp, class _Alloc>
1163 list<_Tp, _Alloc>::__link_nodes(__link_pointer __p, __link_pointer __f, __link_pointer __l)
1165 __p->__prev_->__next_ = __f;
1166 __f->__prev_ = __p->__prev_;
1171 // Link in nodes [__f, __l] at the front of the list
1172 template <class _Tp, class _Alloc>
1175 list<_Tp, _Alloc>::__link_nodes_at_front(__link_pointer __f, __link_pointer __l)
1177 __f->__prev_ = base::__end_as_link();
1178 __l->__next_ = base::__end_.__next_;
1179 __l->__next_->__prev_ = __l;
1180 base::__end_.__next_ = __f;
1183 // Link in nodes [__f, __l] at the back of the list
1184 template <class _Tp, class _Alloc>
1187 list<_Tp, _Alloc>::__link_nodes_at_back(__link_pointer __f, __link_pointer __l)
1189 __l->__next_ = base::__end_as_link();
1190 __f->__prev_ = base::__end_.__prev_;
1191 __f->__prev_->__next_ = __f;
1192 base::__end_.__prev_ = __l;
1196 template <class _Tp, class _Alloc>
1198 typename list<_Tp, _Alloc>::iterator
1199 list<_Tp, _Alloc>::__iterator(size_type __n)
1201 return __n <= base::__sz() / 2 ? _VSTD::next(begin(), __n)
1202 : _VSTD::prev(end(), base::__sz() - __n);
1205 template <class _Tp, class _Alloc>
1206 list<_Tp, _Alloc>::list(size_type __n)
1208 #if _LIBCPP_DEBUG_LEVEL >= 2
1209 __get_db()->__insert_c(this);
1211 for (; __n > 0; --__n)
1212 #ifndef _LIBCPP_CXX03_LANG
1215 push_back(value_type());
1219 #if _LIBCPP_STD_VER > 11
1220 template <class _Tp, class _Alloc>
1221 list<_Tp, _Alloc>::list(size_type __n, const allocator_type& __a) : base(__a)
1223 #if _LIBCPP_DEBUG_LEVEL >= 2
1224 __get_db()->__insert_c(this);
1226 for (; __n > 0; --__n)
1231 template <class _Tp, class _Alloc>
1232 list<_Tp, _Alloc>::list(size_type __n, const value_type& __x)
1234 #if _LIBCPP_DEBUG_LEVEL >= 2
1235 __get_db()->__insert_c(this);
1237 for (; __n > 0; --__n)
1241 template <class _Tp, class _Alloc>
1242 list<_Tp, _Alloc>::list(size_type __n, const value_type& __x, const allocator_type& __a)
1245 #if _LIBCPP_DEBUG_LEVEL >= 2
1246 __get_db()->__insert_c(this);
1248 for (; __n > 0; --__n)
1252 template <class _Tp, class _Alloc>
1253 template <class _InpIter>
1254 list<_Tp, _Alloc>::list(_InpIter __f, _InpIter __l,
1255 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1257 #if _LIBCPP_DEBUG_LEVEL >= 2
1258 __get_db()->__insert_c(this);
1260 for (; __f != __l; ++__f)
1261 __emplace_back(*__f);
1264 template <class _Tp, class _Alloc>
1265 template <class _InpIter>
1266 list<_Tp, _Alloc>::list(_InpIter __f, _InpIter __l, const allocator_type& __a,
1267 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1270 #if _LIBCPP_DEBUG_LEVEL >= 2
1271 __get_db()->__insert_c(this);
1273 for (; __f != __l; ++__f)
1274 __emplace_back(*__f);
1277 template <class _Tp, class _Alloc>
1278 list<_Tp, _Alloc>::list(const list& __c)
1279 : base(__node_alloc_traits::select_on_container_copy_construction(
1280 __c.__node_alloc())) {
1281 #if _LIBCPP_DEBUG_LEVEL >= 2
1282 __get_db()->__insert_c(this);
1284 for (const_iterator __i = __c.begin(), __e = __c.end(); __i != __e; ++__i)
1288 template <class _Tp, class _Alloc>
1289 list<_Tp, _Alloc>::list(const list& __c, const allocator_type& __a)
1292 #if _LIBCPP_DEBUG_LEVEL >= 2
1293 __get_db()->__insert_c(this);
1295 for (const_iterator __i = __c.begin(), __e = __c.end(); __i != __e; ++__i)
1299 #ifndef _LIBCPP_CXX03_LANG
1301 template <class _Tp, class _Alloc>
1302 list<_Tp, _Alloc>::list(initializer_list<value_type> __il, const allocator_type& __a)
1305 #if _LIBCPP_DEBUG_LEVEL >= 2
1306 __get_db()->__insert_c(this);
1308 for (typename initializer_list<value_type>::const_iterator __i = __il.begin(),
1309 __e = __il.end(); __i != __e; ++__i)
1313 template <class _Tp, class _Alloc>
1314 list<_Tp, _Alloc>::list(initializer_list<value_type> __il)
1316 #if _LIBCPP_DEBUG_LEVEL >= 2
1317 __get_db()->__insert_c(this);
1319 for (typename initializer_list<value_type>::const_iterator __i = __il.begin(),
1320 __e = __il.end(); __i != __e; ++__i)
1324 template <class _Tp, class _Alloc>
1325 inline list<_Tp, _Alloc>::list(list&& __c)
1326 _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value)
1327 : base(_VSTD::move(__c.__node_alloc())) {
1328 #if _LIBCPP_DEBUG_LEVEL >= 2
1329 __get_db()->__insert_c(this);
1334 template <class _Tp, class _Alloc>
1336 list<_Tp, _Alloc>::list(list&& __c, const allocator_type& __a)
1339 #if _LIBCPP_DEBUG_LEVEL >= 2
1340 __get_db()->__insert_c(this);
1342 if (__a == __c.get_allocator())
1346 typedef move_iterator<iterator> _Ip;
1347 assign(_Ip(__c.begin()), _Ip(__c.end()));
1351 template <class _Tp, class _Alloc>
1354 list<_Tp, _Alloc>::operator=(list&& __c)
1356 __node_alloc_traits::propagate_on_container_move_assignment::value &&
1357 is_nothrow_move_assignable<__node_allocator>::value)
1359 __move_assign(__c, integral_constant<bool,
1360 __node_alloc_traits::propagate_on_container_move_assignment::value>());
1364 template <class _Tp, class _Alloc>
1366 list<_Tp, _Alloc>::__move_assign(list& __c, false_type)
1368 if (base::__node_alloc() != __c.__node_alloc())
1370 typedef move_iterator<iterator> _Ip;
1371 assign(_Ip(__c.begin()), _Ip(__c.end()));
1374 __move_assign(__c, true_type());
1377 template <class _Tp, class _Alloc>
1379 list<_Tp, _Alloc>::__move_assign(list& __c, true_type)
1380 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
1383 base::__move_assign_alloc(__c);
1387 #endif // _LIBCPP_CXX03_LANG
1389 template <class _Tp, class _Alloc>
1392 list<_Tp, _Alloc>::operator=(const list& __c)
1396 base::__copy_assign_alloc(__c);
1397 assign(__c.begin(), __c.end());
1402 template <class _Tp, class _Alloc>
1403 template <class _InpIter>
1405 list<_Tp, _Alloc>::assign(_InpIter __f, _InpIter __l,
1406 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1408 iterator __i = begin();
1409 iterator __e = end();
1410 for (; __f != __l && __i != __e; ++__f, ++__i)
1413 insert(__e, __f, __l);
1416 #if _LIBCPP_DEBUG_LEVEL >= 2
1417 __get_db()->__invalidate_all(this);
1421 template <class _Tp, class _Alloc>
1423 list<_Tp, _Alloc>::assign(size_type __n, const value_type& __x)
1425 iterator __i = begin();
1426 iterator __e = end();
1427 for (; __n > 0 && __i != __e; --__n, ++__i)
1430 insert(__e, __n, __x);
1433 #if _LIBCPP_DEBUG_LEVEL >= 2
1434 __get_db()->__invalidate_all(this);
1438 template <class _Tp, class _Alloc>
1441 list<_Tp, _Alloc>::get_allocator() const _NOEXCEPT
1443 return allocator_type(base::__node_alloc());
1446 template <class _Tp, class _Alloc>
1447 typename list<_Tp, _Alloc>::iterator
1448 list<_Tp, _Alloc>::insert(const_iterator __p, const value_type& __x)
1450 #if _LIBCPP_DEBUG_LEVEL >= 2
1451 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1452 "list::insert(iterator, x) called with an iterator not"
1453 " referring to this list");
1455 __node_allocator& __na = base::__node_alloc();
1456 __hold_pointer __hold = __allocate_node(__na);
1457 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
1458 __link_nodes(__p.__ptr_, __hold->__as_link(), __hold->__as_link());
1460 #if _LIBCPP_DEBUG_LEVEL >= 2
1461 return iterator(__hold.release()->__as_link(), this);
1463 return iterator(__hold.release()->__as_link());
1467 template <class _Tp, class _Alloc>
1468 typename list<_Tp, _Alloc>::iterator
1469 list<_Tp, _Alloc>::insert(const_iterator __p, size_type __n, const value_type& __x)
1471 #if _LIBCPP_DEBUG_LEVEL >= 2
1472 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1473 "list::insert(iterator, n, x) called with an iterator not"
1474 " referring to this list");
1475 iterator __r(__p.__ptr_, this);
1477 iterator __r(__p.__ptr_);
1482 __node_allocator& __na = base::__node_alloc();
1483 __hold_pointer __hold = __allocate_node(__na);
1484 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
1486 #if _LIBCPP_DEBUG_LEVEL >= 2
1487 __r = iterator(__hold->__as_link(), this);
1489 __r = iterator(__hold->__as_link());
1493 #ifndef _LIBCPP_NO_EXCEPTIONS
1496 #endif // _LIBCPP_NO_EXCEPTIONS
1497 for (--__n; __n != 0; --__n, ++__e, ++__ds)
1499 __hold.reset(__node_alloc_traits::allocate(__na, 1));
1500 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
1501 __e.__ptr_->__next_ = __hold->__as_link();
1502 __hold->__prev_ = __e.__ptr_;
1505 #ifndef _LIBCPP_NO_EXCEPTIONS
1511 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
1512 __link_pointer __prev = __e.__ptr_->__prev_;
1513 __node_alloc_traits::deallocate(__na, __e.__ptr_->__as_node(), 1);
1516 #if _LIBCPP_DEBUG_LEVEL >= 2
1517 __e = iterator(__prev, this);
1519 __e = iterator(__prev);
1524 #endif // _LIBCPP_NO_EXCEPTIONS
1525 __link_nodes(__p.__ptr_, __r.__ptr_, __e.__ptr_);
1526 base::__sz() += __ds;
1531 template <class _Tp, class _Alloc>
1532 template <class _InpIter>
1533 typename list<_Tp, _Alloc>::iterator
1534 list<_Tp, _Alloc>::insert(const_iterator __p, _InpIter __f, _InpIter __l,
1535 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1537 #if _LIBCPP_DEBUG_LEVEL >= 2
1538 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1539 "list::insert(iterator, range) called with an iterator not"
1540 " referring to this list");
1541 iterator __r(__p.__ptr_, this);
1543 iterator __r(__p.__ptr_);
1548 __node_allocator& __na = base::__node_alloc();
1549 __hold_pointer __hold = __allocate_node(__na);
1550 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), *__f);
1552 #if _LIBCPP_DEBUG_LEVEL >= 2
1553 __r = iterator(__hold.get()->__as_link(), this);
1555 __r = iterator(__hold.get()->__as_link());
1559 #ifndef _LIBCPP_NO_EXCEPTIONS
1562 #endif // _LIBCPP_NO_EXCEPTIONS
1563 for (++__f; __f != __l; ++__f, (void) ++__e, (void) ++__ds)
1565 __hold.reset(__node_alloc_traits::allocate(__na, 1));
1566 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), *__f);
1567 __e.__ptr_->__next_ = __hold.get()->__as_link();
1568 __hold->__prev_ = __e.__ptr_;
1571 #ifndef _LIBCPP_NO_EXCEPTIONS
1577 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
1578 __link_pointer __prev = __e.__ptr_->__prev_;
1579 __node_alloc_traits::deallocate(__na, __e.__ptr_->__as_node(), 1);
1582 #if _LIBCPP_DEBUG_LEVEL >= 2
1583 __e = iterator(__prev, this);
1585 __e = iterator(__prev);
1590 #endif // _LIBCPP_NO_EXCEPTIONS
1591 __link_nodes(__p.__ptr_, __r.__ptr_, __e.__ptr_);
1592 base::__sz() += __ds;
1597 template <class _Tp, class _Alloc>
1599 list<_Tp, _Alloc>::push_front(const value_type& __x)
1601 __node_allocator& __na = base::__node_alloc();
1602 __hold_pointer __hold = __allocate_node(__na);
1603 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
1604 __link_pointer __nl = __hold->__as_link();
1605 __link_nodes_at_front(__nl, __nl);
1610 template <class _Tp, class _Alloc>
1612 list<_Tp, _Alloc>::push_back(const value_type& __x)
1614 __node_allocator& __na = base::__node_alloc();
1615 __hold_pointer __hold = __allocate_node(__na);
1616 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
1617 __link_nodes_at_back(__hold.get()->__as_link(), __hold.get()->__as_link());
1622 #ifndef _LIBCPP_CXX03_LANG
1624 template <class _Tp, class _Alloc>
1626 list<_Tp, _Alloc>::push_front(value_type&& __x)
1628 __node_allocator& __na = base::__node_alloc();
1629 __hold_pointer __hold = __allocate_node(__na);
1630 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
1631 __link_nodes_at_front(__hold.get()->__as_link(), __hold.get()->__as_link());
1636 template <class _Tp, class _Alloc>
1638 list<_Tp, _Alloc>::push_back(value_type&& __x)
1640 __node_allocator& __na = base::__node_alloc();
1641 __hold_pointer __hold = __allocate_node(__na);
1642 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
1643 __link_nodes_at_back(__hold.get()->__as_link(), __hold.get()->__as_link());
1648 template <class _Tp, class _Alloc>
1649 template <class... _Args>
1650 #if _LIBCPP_STD_VER > 14
1651 typename list<_Tp, _Alloc>::reference
1655 list<_Tp, _Alloc>::emplace_front(_Args&&... __args)
1657 __node_allocator& __na = base::__node_alloc();
1658 __hold_pointer __hold = __allocate_node(__na);
1659 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
1660 __link_nodes_at_front(__hold.get()->__as_link(), __hold.get()->__as_link());
1662 #if _LIBCPP_STD_VER > 14
1663 return __hold.release()->__value_;
1669 template <class _Tp, class _Alloc>
1670 template <class... _Args>
1671 #if _LIBCPP_STD_VER > 14
1672 typename list<_Tp, _Alloc>::reference
1676 list<_Tp, _Alloc>::emplace_back(_Args&&... __args)
1678 __node_allocator& __na = base::__node_alloc();
1679 __hold_pointer __hold = __allocate_node(__na);
1680 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
1681 __link_pointer __nl = __hold->__as_link();
1682 __link_nodes_at_back(__nl, __nl);
1684 #if _LIBCPP_STD_VER > 14
1685 return __hold.release()->__value_;
1691 template <class _Tp, class _Alloc>
1692 template <class... _Args>
1693 typename list<_Tp, _Alloc>::iterator
1694 list<_Tp, _Alloc>::emplace(const_iterator __p, _Args&&... __args)
1696 #if _LIBCPP_DEBUG_LEVEL >= 2
1697 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1698 "list::emplace(iterator, args...) called with an iterator not"
1699 " referring to this list");
1701 __node_allocator& __na = base::__node_alloc();
1702 __hold_pointer __hold = __allocate_node(__na);
1703 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
1704 __link_pointer __nl = __hold.get()->__as_link();
1705 __link_nodes(__p.__ptr_, __nl, __nl);
1708 #if _LIBCPP_DEBUG_LEVEL >= 2
1709 return iterator(__nl, this);
1711 return iterator(__nl);
1715 template <class _Tp, class _Alloc>
1716 typename list<_Tp, _Alloc>::iterator
1717 list<_Tp, _Alloc>::insert(const_iterator __p, value_type&& __x)
1719 #if _LIBCPP_DEBUG_LEVEL >= 2
1720 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1721 "list::insert(iterator, x) called with an iterator not"
1722 " referring to this list");
1724 __node_allocator& __na = base::__node_alloc();
1725 __hold_pointer __hold = __allocate_node(__na);
1726 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
1727 __link_pointer __nl = __hold->__as_link();
1728 __link_nodes(__p.__ptr_, __nl, __nl);
1731 #if _LIBCPP_DEBUG_LEVEL >= 2
1732 return iterator(__nl, this);
1734 return iterator(__nl);
1738 #endif // _LIBCPP_CXX03_LANG
1740 template <class _Tp, class _Alloc>
1742 list<_Tp, _Alloc>::pop_front()
1744 _LIBCPP_ASSERT(!empty(), "list::pop_front() called with empty list");
1745 __node_allocator& __na = base::__node_alloc();
1746 __link_pointer __n = base::__end_.__next_;
1747 base::__unlink_nodes(__n, __n);
1749 #if _LIBCPP_DEBUG_LEVEL >= 2
1750 __c_node* __c = __get_db()->__find_c_and_lock(this);
1751 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1754 iterator* __i = static_cast<iterator*>((*__p)->__i_);
1755 if (__i->__ptr_ == __n)
1757 (*__p)->__c_ = nullptr;
1758 if (--__c->end_ != __p)
1759 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1762 __get_db()->unlock();
1764 __node_pointer __np = __n->__as_node();
1765 __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
1766 __node_alloc_traits::deallocate(__na, __np, 1);
1769 template <class _Tp, class _Alloc>
1771 list<_Tp, _Alloc>::pop_back()
1773 _LIBCPP_ASSERT(!empty(), "list::pop_back() called with empty list");
1774 __node_allocator& __na = base::__node_alloc();
1775 __link_pointer __n = base::__end_.__prev_;
1776 base::__unlink_nodes(__n, __n);
1778 #if _LIBCPP_DEBUG_LEVEL >= 2
1779 __c_node* __c = __get_db()->__find_c_and_lock(this);
1780 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1783 iterator* __i = static_cast<iterator*>((*__p)->__i_);
1784 if (__i->__ptr_ == __n)
1786 (*__p)->__c_ = nullptr;
1787 if (--__c->end_ != __p)
1788 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1791 __get_db()->unlock();
1793 __node_pointer __np = __n->__as_node();
1794 __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
1795 __node_alloc_traits::deallocate(__na, __np, 1);
1798 template <class _Tp, class _Alloc>
1799 typename list<_Tp, _Alloc>::iterator
1800 list<_Tp, _Alloc>::erase(const_iterator __p)
1802 #if _LIBCPP_DEBUG_LEVEL >= 2
1803 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1804 "list::erase(iterator) called with an iterator not"
1805 " referring to this list");
1807 _LIBCPP_ASSERT(__p != end(),
1808 "list::erase(iterator) called with a non-dereferenceable iterator");
1809 __node_allocator& __na = base::__node_alloc();
1810 __link_pointer __n = __p.__ptr_;
1811 __link_pointer __r = __n->__next_;
1812 base::__unlink_nodes(__n, __n);
1814 #if _LIBCPP_DEBUG_LEVEL >= 2
1815 __c_node* __c = __get_db()->__find_c_and_lock(this);
1816 for (__i_node** __ip = __c->end_; __ip != __c->beg_; )
1819 iterator* __i = static_cast<iterator*>((*__ip)->__i_);
1820 if (__i->__ptr_ == __n)
1822 (*__ip)->__c_ = nullptr;
1823 if (--__c->end_ != __ip)
1824 memmove(__ip, __ip+1, (__c->end_ - __ip)*sizeof(__i_node*));
1827 __get_db()->unlock();
1829 __node_pointer __np = __n->__as_node();
1830 __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
1831 __node_alloc_traits::deallocate(__na, __np, 1);
1832 #if _LIBCPP_DEBUG_LEVEL >= 2
1833 return iterator(__r, this);
1835 return iterator(__r);
1839 template <class _Tp, class _Alloc>
1840 typename list<_Tp, _Alloc>::iterator
1841 list<_Tp, _Alloc>::erase(const_iterator __f, const_iterator __l)
1843 #if _LIBCPP_DEBUG_LEVEL >= 2
1844 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__f) == this,
1845 "list::erase(iterator, iterator) called with an iterator not"
1846 " referring to this list");
1847 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__l) == this,
1848 "list::erase(iterator, iterator) called with an iterator not"
1849 " referring to this list");
1853 __node_allocator& __na = base::__node_alloc();
1854 base::__unlink_nodes(__f.__ptr_, __l.__ptr_->__prev_);
1857 __link_pointer __n = __f.__ptr_;
1860 #if _LIBCPP_DEBUG_LEVEL >= 2
1861 __c_node* __c = __get_db()->__find_c_and_lock(this);
1862 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1865 iterator* __i = static_cast<iterator*>((*__p)->__i_);
1866 if (__i->__ptr_ == __n)
1868 (*__p)->__c_ = nullptr;
1869 if (--__c->end_ != __p)
1870 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1873 __get_db()->unlock();
1875 __node_pointer __np = __n->__as_node();
1876 __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
1877 __node_alloc_traits::deallocate(__na, __np, 1);
1880 #if _LIBCPP_DEBUG_LEVEL >= 2
1881 return iterator(__l.__ptr_, this);
1883 return iterator(__l.__ptr_);
1887 template <class _Tp, class _Alloc>
1889 list<_Tp, _Alloc>::resize(size_type __n)
1891 if (__n < base::__sz())
1892 erase(__iterator(__n), end());
1893 else if (__n > base::__sz())
1895 __n -= base::__sz();
1897 __node_allocator& __na = base::__node_alloc();
1898 __hold_pointer __hold = __allocate_node(__na);
1899 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_));
1901 #if _LIBCPP_DEBUG_LEVEL >= 2
1902 iterator __r = iterator(__hold.release()->__as_link(), this);
1904 iterator __r = iterator(__hold.release()->__as_link());
1907 #ifndef _LIBCPP_NO_EXCEPTIONS
1910 #endif // _LIBCPP_NO_EXCEPTIONS
1911 for (--__n; __n != 0; --__n, ++__e, ++__ds)
1913 __hold.reset(__node_alloc_traits::allocate(__na, 1));
1914 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_));
1915 __e.__ptr_->__next_ = __hold.get()->__as_link();
1916 __hold->__prev_ = __e.__ptr_;
1919 #ifndef _LIBCPP_NO_EXCEPTIONS
1925 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
1926 __link_pointer __prev = __e.__ptr_->__prev_;
1927 __node_alloc_traits::deallocate(__na, __e.__ptr_->__as_node(), 1);
1930 #if _LIBCPP_DEBUG_LEVEL >= 2
1931 __e = iterator(__prev, this);
1933 __e = iterator(__prev);
1938 #endif // _LIBCPP_NO_EXCEPTIONS
1939 __link_nodes_at_back(__r.__ptr_, __e.__ptr_);
1940 base::__sz() += __ds;
1944 template <class _Tp, class _Alloc>
1946 list<_Tp, _Alloc>::resize(size_type __n, const value_type& __x)
1948 if (__n < base::__sz())
1949 erase(__iterator(__n), end());
1950 else if (__n > base::__sz())
1952 __n -= base::__sz();
1954 __node_allocator& __na = base::__node_alloc();
1955 __hold_pointer __hold = __allocate_node(__na);
1956 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
1958 __link_pointer __nl = __hold.release()->__as_link();
1959 #if _LIBCPP_DEBUG_LEVEL >= 2
1960 iterator __r = iterator(__nl, this);
1962 iterator __r = iterator(__nl);
1965 #ifndef _LIBCPP_NO_EXCEPTIONS
1968 #endif // _LIBCPP_NO_EXCEPTIONS
1969 for (--__n; __n != 0; --__n, ++__e, ++__ds)
1971 __hold.reset(__node_alloc_traits::allocate(__na, 1));
1972 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
1973 __e.__ptr_->__next_ = __hold.get()->__as_link();
1974 __hold->__prev_ = __e.__ptr_;
1977 #ifndef _LIBCPP_NO_EXCEPTIONS
1983 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
1984 __link_pointer __prev = __e.__ptr_->__prev_;
1985 __node_alloc_traits::deallocate(__na, __e.__ptr_->__as_node(), 1);
1988 #if _LIBCPP_DEBUG_LEVEL >= 2
1989 __e = iterator(__prev, this);
1991 __e = iterator(__prev);
1996 #endif // _LIBCPP_NO_EXCEPTIONS
1997 __link_nodes(base::__end_as_link(), __r.__ptr_, __e.__ptr_);
1998 base::__sz() += __ds;
2002 template <class _Tp, class _Alloc>
2004 list<_Tp, _Alloc>::splice(const_iterator __p, list& __c)
2006 _LIBCPP_ASSERT(this != &__c,
2007 "list::splice(iterator, list) called with this == &list");
2008 #if _LIBCPP_DEBUG_LEVEL >= 2
2009 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2010 "list::splice(iterator, list) called with an iterator not"
2011 " referring to this list");
2015 __link_pointer __f = __c.__end_.__next_;
2016 __link_pointer __l = __c.__end_.__prev_;
2017 base::__unlink_nodes(__f, __l);
2018 __link_nodes(__p.__ptr_, __f, __l);
2019 base::__sz() += __c.__sz();
2021 #if _LIBCPP_DEBUG_LEVEL >= 2
2023 __libcpp_db* __db = __get_db();
2024 __c_node* __cn1 = __db->__find_c_and_lock(this);
2025 __c_node* __cn2 = __db->__find_c(&__c);
2026 for (__i_node** __ip = __cn2->end_; __ip != __cn2->beg_;)
2029 iterator* __i = static_cast<iterator*>((*__ip)->__i_);
2030 if (__i->__ptr_ != __c.__end_as_link())
2032 __cn1->__add(*__ip);
2033 (*__ip)->__c_ = __cn1;
2034 if (--__cn2->end_ != __ip)
2035 memmove(__ip, __ip+1, (__cn2->end_ - __ip)*sizeof(__i_node*));
2044 template <class _Tp, class _Alloc>
2046 list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __i)
2048 #if _LIBCPP_DEBUG_LEVEL >= 2
2049 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2050 "list::splice(iterator, list, iterator) called with first iterator not"
2051 " referring to this list");
2052 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__i) == &__c,
2053 "list::splice(iterator, list, iterator) called with second iterator not"
2054 " referring to list argument");
2055 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(&__i),
2056 "list::splice(iterator, list, iterator) called with second iterator not"
2059 if (__p.__ptr_ != __i.__ptr_ && __p.__ptr_ != __i.__ptr_->__next_)
2061 __link_pointer __f = __i.__ptr_;
2062 base::__unlink_nodes(__f, __f);
2063 __link_nodes(__p.__ptr_, __f, __f);
2066 #if _LIBCPP_DEBUG_LEVEL >= 2
2068 __libcpp_db* __db = __get_db();
2069 __c_node* __cn1 = __db->__find_c_and_lock(this);
2070 __c_node* __cn2 = __db->__find_c(&__c);
2071 for (__i_node** __ip = __cn2->end_; __ip != __cn2->beg_;)
2074 iterator* __j = static_cast<iterator*>((*__ip)->__i_);
2075 if (__j->__ptr_ == __f)
2077 __cn1->__add(*__ip);
2078 (*__ip)->__c_ = __cn1;
2079 if (--__cn2->end_ != __ip)
2080 memmove(__ip, __ip+1, (__cn2->end_ - __ip)*sizeof(__i_node*));
2089 template <class _Tp, class _Alloc>
2091 list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l)
2093 #if _LIBCPP_DEBUG_LEVEL >= 2
2094 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2095 "list::splice(iterator, list, iterator, iterator) called with first iterator not"
2096 " referring to this list");
2097 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__f) == &__c,
2098 "list::splice(iterator, list, iterator, iterator) called with second iterator not"
2099 " referring to list argument");
2102 for (const_iterator __i = __f; __i != __l; ++__i)
2103 _LIBCPP_ASSERT(__i != __p,
2104 "list::splice(iterator, list, iterator, iterator)"
2105 " called with the first iterator within the range"
2106 " of the second and third iterators");
2111 __link_pointer __first = __f.__ptr_;
2113 __link_pointer __last = __l.__ptr_;
2116 size_type __s = _VSTD::distance(__f, __l) + 1;
2118 base::__sz() += __s;
2120 base::__unlink_nodes(__first, __last);
2121 __link_nodes(__p.__ptr_, __first, __last);
2122 #if _LIBCPP_DEBUG_LEVEL >= 2
2124 __libcpp_db* __db = __get_db();
2125 __c_node* __cn1 = __db->__find_c_and_lock(this);
2126 __c_node* __cn2 = __db->__find_c(&__c);
2127 for (__i_node** __ip = __cn2->end_; __ip != __cn2->beg_;)
2130 iterator* __j = static_cast<iterator*>((*__ip)->__i_);
2131 for (__link_pointer __k = __f.__ptr_;
2132 __k != __l.__ptr_; __k = __k->__next_)
2134 if (__j->__ptr_ == __k)
2136 __cn1->__add(*__ip);
2137 (*__ip)->__c_ = __cn1;
2138 if (--__cn2->end_ != __ip)
2139 memmove(__ip, __ip+1, (__cn2->end_ - __ip)*sizeof(__i_node*));
2149 template <class _Tp, class _Alloc>
2150 typename list<_Tp, _Alloc>::__remove_return_type
2151 list<_Tp, _Alloc>::remove(const value_type& __x)
2153 list<_Tp, _Alloc> __deleted_nodes(get_allocator()); // collect the nodes we're removing
2154 for (const_iterator __i = begin(), __e = end(); __i != __e;)
2158 const_iterator __j = _VSTD::next(__i);
2159 for (; __j != __e && *__j == __x; ++__j)
2161 __deleted_nodes.splice(__deleted_nodes.end(), *this, __i, __j);
2170 return (__remove_return_type) __deleted_nodes.size();
2173 template <class _Tp, class _Alloc>
2174 template <class _Pred>
2175 typename list<_Tp, _Alloc>::__remove_return_type
2176 list<_Tp, _Alloc>::remove_if(_Pred __pred)
2178 list<_Tp, _Alloc> __deleted_nodes(get_allocator()); // collect the nodes we're removing
2179 for (iterator __i = begin(), __e = end(); __i != __e;)
2183 iterator __j = _VSTD::next(__i);
2184 for (; __j != __e && __pred(*__j); ++__j)
2186 __deleted_nodes.splice(__deleted_nodes.end(), *this, __i, __j);
2195 return (__remove_return_type) __deleted_nodes.size();
2198 template <class _Tp, class _Alloc>
2199 template <class _BinaryPred>
2200 typename list<_Tp, _Alloc>::__remove_return_type
2201 list<_Tp, _Alloc>::unique(_BinaryPred __binary_pred)
2203 list<_Tp, _Alloc> __deleted_nodes(get_allocator()); // collect the nodes we're removing
2204 for (iterator __i = begin(), __e = end(); __i != __e;)
2206 iterator __j = _VSTD::next(__i);
2207 for (; __j != __e && __binary_pred(*__i, *__j); ++__j)
2210 __deleted_nodes.splice(__deleted_nodes.end(), *this, __i, __j);
2215 return (__remove_return_type) __deleted_nodes.size();
2218 template <class _Tp, class _Alloc>
2221 list<_Tp, _Alloc>::merge(list& __c)
2223 merge(__c, __less<value_type>());
2226 template <class _Tp, class _Alloc>
2227 template <class _Comp>
2229 list<_Tp, _Alloc>::merge(list& __c, _Comp __comp)
2231 if (this != _VSTD::addressof(__c))
2233 iterator __f1 = begin();
2234 iterator __e1 = end();
2235 iterator __f2 = __c.begin();
2236 iterator __e2 = __c.end();
2237 while (__f1 != __e1 && __f2 != __e2)
2239 if (__comp(*__f2, *__f1))
2242 iterator __m2 = _VSTD::next(__f2);
2243 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2, ++__ds)
2245 base::__sz() += __ds;
2247 __link_pointer __f = __f2.__ptr_;
2248 __link_pointer __l = __m2.__ptr_->__prev_;
2250 base::__unlink_nodes(__f, __l);
2251 __m2 = _VSTD::next(__f1);
2252 __link_nodes(__f1.__ptr_, __f, __l);
2259 #if _LIBCPP_DEBUG_LEVEL >= 2
2260 __libcpp_db* __db = __get_db();
2261 __c_node* __cn1 = __db->__find_c_and_lock(this);
2262 __c_node* __cn2 = __db->__find_c(&__c);
2263 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
2266 iterator* __i = static_cast<iterator*>((*__p)->__i_);
2267 if (__i->__ptr_ != __c.__end_as_link())
2270 (*__p)->__c_ = __cn1;
2271 if (--__cn2->end_ != __p)
2272 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
2280 template <class _Tp, class _Alloc>
2283 list<_Tp, _Alloc>::sort()
2285 sort(__less<value_type>());
2288 template <class _Tp, class _Alloc>
2289 template <class _Comp>
2292 list<_Tp, _Alloc>::sort(_Comp __comp)
2294 __sort(begin(), end(), base::__sz(), __comp);
2297 template <class _Tp, class _Alloc>
2298 template <class _Comp>
2299 typename list<_Tp, _Alloc>::iterator
2300 list<_Tp, _Alloc>::__sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp)
2308 if (__comp(*--__e2, *__f1))
2310 __link_pointer __f = __e2.__ptr_;
2311 base::__unlink_nodes(__f, __f);
2312 __link_nodes(__f1.__ptr_, __f, __f);
2317 size_type __n2 = __n / 2;
2318 iterator __e1 = _VSTD::next(__f1, __n2);
2319 iterator __r = __f1 = __sort(__f1, __e1, __n2, __comp);
2320 iterator __f2 = __e1 = __sort(__e1, __e2, __n - __n2, __comp);
2321 if (__comp(*__f2, *__f1))
2323 iterator __m2 = _VSTD::next(__f2);
2324 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2)
2326 __link_pointer __f = __f2.__ptr_;
2327 __link_pointer __l = __m2.__ptr_->__prev_;
2330 base::__unlink_nodes(__f, __l);
2331 __m2 = _VSTD::next(__f1);
2332 __link_nodes(__f1.__ptr_, __f, __l);
2337 while (__f1 != __e1 && __f2 != __e2)
2339 if (__comp(*__f2, *__f1))
2341 iterator __m2 = _VSTD::next(__f2);
2342 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2)
2344 __link_pointer __f = __f2.__ptr_;
2345 __link_pointer __l = __m2.__ptr_->__prev_;
2349 base::__unlink_nodes(__f, __l);
2350 __m2 = _VSTD::next(__f1);
2351 __link_nodes(__f1.__ptr_, __f, __l);
2360 template <class _Tp, class _Alloc>
2362 list<_Tp, _Alloc>::reverse() _NOEXCEPT
2364 if (base::__sz() > 1)
2366 iterator __e = end();
2367 for (iterator __i = begin(); __i.__ptr_ != __e.__ptr_;)
2369 _VSTD::swap(__i.__ptr_->__prev_, __i.__ptr_->__next_);
2370 __i.__ptr_ = __i.__ptr_->__prev_;
2372 _VSTD::swap(__e.__ptr_->__prev_, __e.__ptr_->__next_);
2376 template <class _Tp, class _Alloc>
2378 list<_Tp, _Alloc>::__invariants() const
2380 return size() == _VSTD::distance(begin(), end());
2383 #if _LIBCPP_DEBUG_LEVEL >= 2
2385 template <class _Tp, class _Alloc>
2387 list<_Tp, _Alloc>::__dereferenceable(const const_iterator* __i) const
2389 return __i->__ptr_ != this->__end_as_link();
2392 template <class _Tp, class _Alloc>
2394 list<_Tp, _Alloc>::__decrementable(const const_iterator* __i) const
2396 return !empty() && __i->__ptr_ != base::__end_.__next_;
2399 template <class _Tp, class _Alloc>
2401 list<_Tp, _Alloc>::__addable(const const_iterator*, ptrdiff_t) const
2406 template <class _Tp, class _Alloc>
2408 list<_Tp, _Alloc>::__subscriptable(const const_iterator*, ptrdiff_t) const
2413 #endif // _LIBCPP_DEBUG_LEVEL >= 2
2415 template <class _Tp, class _Alloc>
2416 inline _LIBCPP_INLINE_VISIBILITY
2418 operator==(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2420 return __x.size() == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
2423 template <class _Tp, class _Alloc>
2424 inline _LIBCPP_INLINE_VISIBILITY
2426 operator< (const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2428 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
2431 template <class _Tp, class _Alloc>
2432 inline _LIBCPP_INLINE_VISIBILITY
2434 operator!=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2436 return !(__x == __y);
2439 template <class _Tp, class _Alloc>
2440 inline _LIBCPP_INLINE_VISIBILITY
2442 operator> (const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2447 template <class _Tp, class _Alloc>
2448 inline _LIBCPP_INLINE_VISIBILITY
2450 operator>=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2452 return !(__x < __y);
2455 template <class _Tp, class _Alloc>
2456 inline _LIBCPP_INLINE_VISIBILITY
2458 operator<=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2460 return !(__y < __x);
2463 template <class _Tp, class _Alloc>
2464 inline _LIBCPP_INLINE_VISIBILITY
2466 swap(list<_Tp, _Alloc>& __x, list<_Tp, _Alloc>& __y)
2467 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
2472 #if _LIBCPP_STD_VER > 17
2473 template <class _Tp, class _Allocator, class _Predicate>
2474 inline _LIBCPP_INLINE_VISIBILITY
2475 void erase_if(list<_Tp, _Allocator>& __c, _Predicate __pred)
2476 { __c.remove_if(__pred); }
2478 template <class _Tp, class _Allocator, class _Up>
2479 inline _LIBCPP_INLINE_VISIBILITY
2480 void erase(list<_Tp, _Allocator>& __c, const _Up& __v)
2481 { _VSTD::erase_if(__c, [&](auto& __elem) { return __elem == __v; }); }
2484 _LIBCPP_END_NAMESPACE_STD
2488 #endif // _LIBCPP_LIST