2 //===---------------------------- list ------------------------------------===//
4 // The LLVM Compiler Infrastructure
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
20 template <class T, class Alloc = allocator<T> >
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;
40 noexcept(is_nothrow_default_constructible<allocator_type>::value);
41 explicit list(const allocator_type& a);
42 explicit list(size_type n);
43 explicit list(size_type n, const allocator_type& a); // C++14
44 list(size_type n, const value_type& value);
45 list(size_type n, const value_type& value, const allocator_type& a);
47 list(Iter first, Iter last);
49 list(Iter first, Iter last, const allocator_type& a);
51 list(const list&, const allocator_type& a);
53 noexcept(is_nothrow_move_constructible<allocator_type>::value);
54 list(list&&, const allocator_type& a);
55 list(initializer_list<value_type>);
56 list(initializer_list<value_type>, const allocator_type& a);
60 list& operator=(const list& x);
61 list& operator=(list&& x)
63 allocator_type::propagate_on_container_move_assignment::value &&
64 is_nothrow_move_assignable<allocator_type>::value);
65 list& operator=(initializer_list<value_type>);
67 void assign(Iter first, Iter last);
68 void assign(size_type n, const value_type& t);
69 void assign(initializer_list<value_type>);
71 allocator_type get_allocator() const noexcept;
73 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;
87 const_reference front() const;
89 const_reference back() const;
91 bool empty() const noexcept;
92 size_type size() const noexcept;
93 size_type max_size() const noexcept;
95 template <class... Args>
96 reference emplace_front(Args&&... args);
98 template <class... Args>
99 reference emplace_back(Args&&... args);
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);
114 iterator erase(const_iterator position);
115 iterator erase(const_iterator position, const_iterator last);
117 void resize(size_type sz);
118 void resize(size_type sz, const value_type& c);
121 noexcept(allocator_traits<allocator_type>::is_always_equal::value); // C++17
122 void clear() noexcept;
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);
133 void remove(const value_type& value);
134 template <class Pred> void remove_if(Pred pred);
136 template <class BinaryPredicate>
137 void unique(BinaryPredicate binary_pred);
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;
150 template <class T, class Alloc>
151 bool operator==(const list<T,Alloc>& x, const list<T,Alloc>& y);
152 template <class T, class Alloc>
153 bool operator< (const list<T,Alloc>& x, const list<T,Alloc>& y);
154 template <class T, class Alloc>
155 bool operator!=(const list<T,Alloc>& x, const list<T,Alloc>& y);
156 template <class T, class Alloc>
157 bool operator> (const list<T,Alloc>& x, const list<T,Alloc>& y);
158 template <class T, class Alloc>
159 bool operator>=(const list<T,Alloc>& x, const list<T,Alloc>& y);
160 template <class T, class Alloc>
161 bool operator<=(const list<T,Alloc>& x, const list<T,Alloc>& y);
163 template <class T, class Alloc>
164 void swap(list<T,Alloc>& x, list<T,Alloc>& y)
165 noexcept(noexcept(x.swap(y)));
175 #include <initializer_list>
178 #include <type_traits>
180 #include <__undef_min_max>
184 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
185 #pragma GCC system_header
188 _LIBCPP_BEGIN_NAMESPACE_STD
190 template <class _Tp, class _VoidPtr> struct __list_node;
191 template <class _Tp, class _VoidPtr> struct __list_node_base;
193 template <class _Tp, class _VoidPtr>
194 struct __list_node_pointer_traits {
195 typedef typename __rebind_pointer<_VoidPtr, __list_node<_Tp, _VoidPtr> >::type
197 typedef typename __rebind_pointer<_VoidPtr, __list_node_base<_Tp, _VoidPtr> >::type
200 #if defined(_LIBCPP_ABI_LIST_REMOVE_NODE_POINTER_UB)
201 typedef __base_pointer __link_pointer;
203 typedef typename conditional<
204 is_pointer<_VoidPtr>::value,
207 >::type __link_pointer;
210 typedef typename conditional<
211 is_same<__link_pointer, __node_pointer>::value,
214 >::type __non_link_pointer;
216 static _LIBCPP_INLINE_VISIBILITY
217 __link_pointer __unsafe_link_pointer_cast(__link_pointer __p) {
221 static _LIBCPP_INLINE_VISIBILITY
222 __link_pointer __unsafe_link_pointer_cast(__non_link_pointer __p) {
223 return static_cast<__link_pointer>(static_cast<_VoidPtr>(__p));
228 template <class _Tp, class _VoidPtr>
229 struct __list_node_base
231 typedef __list_node_pointer_traits<_Tp, _VoidPtr> _NodeTraits;
232 typedef typename _NodeTraits::__node_pointer __node_pointer;
233 typedef typename _NodeTraits::__base_pointer __base_pointer;
234 typedef typename _NodeTraits::__link_pointer __link_pointer;
236 __link_pointer __prev_;
237 __link_pointer __next_;
239 _LIBCPP_INLINE_VISIBILITY
240 __list_node_base() : __prev_(_NodeTraits::__unsafe_link_pointer_cast(__self())),
241 __next_(_NodeTraits::__unsafe_link_pointer_cast(__self())) {}
243 _LIBCPP_INLINE_VISIBILITY
244 __base_pointer __self() {
245 return pointer_traits<__base_pointer>::pointer_to(*this);
248 _LIBCPP_INLINE_VISIBILITY
249 __node_pointer __as_node() {
250 return static_cast<__node_pointer>(__self());
254 template <class _Tp, class _VoidPtr>
256 : public __list_node_base<_Tp, _VoidPtr>
260 typedef __list_node_base<_Tp, _VoidPtr> __base;
261 typedef typename __base::__link_pointer __link_pointer;
263 _LIBCPP_INLINE_VISIBILITY
264 __link_pointer __as_link() {
265 return static_cast<__link_pointer>(__base::__self());
269 template <class _Tp, class _Alloc = allocator<_Tp> > class _LIBCPP_TEMPLATE_VIS list;
270 template <class _Tp, class _Alloc> class __list_imp;
271 template <class _Tp, class _VoidPtr> class _LIBCPP_TEMPLATE_VIS __list_const_iterator;
273 template <class _Tp, class _VoidPtr>
274 class _LIBCPP_TEMPLATE_VIS __list_iterator
276 typedef __list_node_pointer_traits<_Tp, _VoidPtr> _NodeTraits;
277 typedef typename _NodeTraits::__link_pointer __link_pointer;
279 __link_pointer __ptr_;
281 #if _LIBCPP_DEBUG_LEVEL >= 2
282 _LIBCPP_INLINE_VISIBILITY
283 explicit __list_iterator(__link_pointer __p, const void* __c) _NOEXCEPT
286 __get_db()->__insert_ic(this, __c);
289 _LIBCPP_INLINE_VISIBILITY
290 explicit __list_iterator(__link_pointer __p) _NOEXCEPT : __ptr_(__p) {}
295 template<class, class> friend class list;
296 template<class, class> friend class __list_imp;
297 template<class, class> friend class __list_const_iterator;
299 typedef bidirectional_iterator_tag iterator_category;
300 typedef _Tp value_type;
301 typedef value_type& reference;
302 typedef typename __rebind_pointer<_VoidPtr, value_type>::type pointer;
303 typedef typename pointer_traits<pointer>::difference_type difference_type;
305 _LIBCPP_INLINE_VISIBILITY
306 __list_iterator() _NOEXCEPT : __ptr_(nullptr)
308 #if _LIBCPP_DEBUG_LEVEL >= 2
309 __get_db()->__insert_i(this);
313 #if _LIBCPP_DEBUG_LEVEL >= 2
315 _LIBCPP_INLINE_VISIBILITY
316 __list_iterator(const __list_iterator& __p)
319 __get_db()->__iterator_copy(this, &__p);
322 _LIBCPP_INLINE_VISIBILITY
325 __get_db()->__erase_i(this);
328 _LIBCPP_INLINE_VISIBILITY
329 __list_iterator& operator=(const __list_iterator& __p)
333 __get_db()->__iterator_copy(this, &__p);
339 #endif // _LIBCPP_DEBUG_LEVEL >= 2
341 _LIBCPP_INLINE_VISIBILITY
342 reference operator*() const
344 #if _LIBCPP_DEBUG_LEVEL >= 2
345 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
346 "Attempted to dereference a non-dereferenceable list::iterator");
348 return __ptr_->__as_node()->__value_;
350 _LIBCPP_INLINE_VISIBILITY
351 pointer operator->() const
353 #if _LIBCPP_DEBUG_LEVEL >= 2
354 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
355 "Attempted to dereference a non-dereferenceable list::iterator");
357 return pointer_traits<pointer>::pointer_to(__ptr_->__as_node()->__value_);
360 _LIBCPP_INLINE_VISIBILITY
361 __list_iterator& operator++()
363 #if _LIBCPP_DEBUG_LEVEL >= 2
364 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
365 "Attempted to increment non-incrementable list::iterator");
367 __ptr_ = __ptr_->__next_;
370 _LIBCPP_INLINE_VISIBILITY
371 __list_iterator operator++(int) {__list_iterator __t(*this); ++(*this); return __t;}
373 _LIBCPP_INLINE_VISIBILITY
374 __list_iterator& operator--()
376 #if _LIBCPP_DEBUG_LEVEL >= 2
377 _LIBCPP_ASSERT(__get_const_db()->__decrementable(this),
378 "Attempted to decrement non-decrementable list::iterator");
380 __ptr_ = __ptr_->__prev_;
383 _LIBCPP_INLINE_VISIBILITY
384 __list_iterator operator--(int) {__list_iterator __t(*this); --(*this); return __t;}
386 friend _LIBCPP_INLINE_VISIBILITY
387 bool operator==(const __list_iterator& __x, const __list_iterator& __y)
389 return __x.__ptr_ == __y.__ptr_;
391 friend _LIBCPP_INLINE_VISIBILITY
392 bool operator!=(const __list_iterator& __x, const __list_iterator& __y)
393 {return !(__x == __y);}
396 template <class _Tp, class _VoidPtr>
397 class _LIBCPP_TEMPLATE_VIS __list_const_iterator
399 typedef __list_node_pointer_traits<_Tp, _VoidPtr> _NodeTraits;
400 typedef typename _NodeTraits::__link_pointer __link_pointer;
402 __link_pointer __ptr_;
404 #if _LIBCPP_DEBUG_LEVEL >= 2
405 _LIBCPP_INLINE_VISIBILITY
406 explicit __list_const_iterator(__link_pointer __p, const void* __c) _NOEXCEPT
409 __get_db()->__insert_ic(this, __c);
412 _LIBCPP_INLINE_VISIBILITY
413 explicit __list_const_iterator(__link_pointer __p) _NOEXCEPT : __ptr_(__p) {}
416 template<class, class> friend class list;
417 template<class, class> friend class __list_imp;
419 typedef bidirectional_iterator_tag iterator_category;
420 typedef _Tp value_type;
421 typedef const value_type& reference;
422 typedef typename __rebind_pointer<_VoidPtr, const value_type>::type pointer;
423 typedef typename pointer_traits<pointer>::difference_type difference_type;
425 _LIBCPP_INLINE_VISIBILITY
426 __list_const_iterator() _NOEXCEPT : __ptr_(nullptr)
428 #if _LIBCPP_DEBUG_LEVEL >= 2
429 __get_db()->__insert_i(this);
432 _LIBCPP_INLINE_VISIBILITY
433 __list_const_iterator(const __list_iterator<_Tp, _VoidPtr>& __p) _NOEXCEPT
436 #if _LIBCPP_DEBUG_LEVEL >= 2
437 __get_db()->__iterator_copy(this, &__p);
441 #if _LIBCPP_DEBUG_LEVEL >= 2
443 _LIBCPP_INLINE_VISIBILITY
444 __list_const_iterator(const __list_const_iterator& __p)
447 __get_db()->__iterator_copy(this, &__p);
450 _LIBCPP_INLINE_VISIBILITY
451 ~__list_const_iterator()
453 __get_db()->__erase_i(this);
456 _LIBCPP_INLINE_VISIBILITY
457 __list_const_iterator& operator=(const __list_const_iterator& __p)
461 __get_db()->__iterator_copy(this, &__p);
467 #endif // _LIBCPP_DEBUG_LEVEL >= 2
468 _LIBCPP_INLINE_VISIBILITY
469 reference operator*() const
471 #if _LIBCPP_DEBUG_LEVEL >= 2
472 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
473 "Attempted to dereference a non-dereferenceable list::const_iterator");
475 return __ptr_->__as_node()->__value_;
477 _LIBCPP_INLINE_VISIBILITY
478 pointer operator->() const
480 #if _LIBCPP_DEBUG_LEVEL >= 2
481 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
482 "Attempted to dereference a non-dereferenceable list::iterator");
484 return pointer_traits<pointer>::pointer_to(__ptr_->__as_node()->__value_);
487 _LIBCPP_INLINE_VISIBILITY
488 __list_const_iterator& operator++()
490 #if _LIBCPP_DEBUG_LEVEL >= 2
491 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
492 "Attempted to increment non-incrementable list::const_iterator");
494 __ptr_ = __ptr_->__next_;
497 _LIBCPP_INLINE_VISIBILITY
498 __list_const_iterator operator++(int) {__list_const_iterator __t(*this); ++(*this); return __t;}
500 _LIBCPP_INLINE_VISIBILITY
501 __list_const_iterator& operator--()
503 #if _LIBCPP_DEBUG_LEVEL >= 2
504 _LIBCPP_ASSERT(__get_const_db()->__decrementable(this),
505 "Attempted to decrement non-decrementable list::const_iterator");
507 __ptr_ = __ptr_->__prev_;
510 _LIBCPP_INLINE_VISIBILITY
511 __list_const_iterator operator--(int) {__list_const_iterator __t(*this); --(*this); return __t;}
513 friend _LIBCPP_INLINE_VISIBILITY
514 bool operator==(const __list_const_iterator& __x, const __list_const_iterator& __y)
516 return __x.__ptr_ == __y.__ptr_;
518 friend _LIBCPP_INLINE_VISIBILITY
519 bool operator!=(const __list_const_iterator& __x, const __list_const_iterator& __y)
520 {return !(__x == __y);}
523 template <class _Tp, class _Alloc>
526 __list_imp(const __list_imp&);
527 __list_imp& operator=(const __list_imp&);
529 typedef _Tp value_type;
530 typedef _Alloc allocator_type;
531 typedef allocator_traits<allocator_type> __alloc_traits;
532 typedef typename __alloc_traits::size_type size_type;
533 typedef typename __alloc_traits::void_pointer __void_pointer;
534 typedef __list_iterator<value_type, __void_pointer> iterator;
535 typedef __list_const_iterator<value_type, __void_pointer> const_iterator;
536 typedef __list_node_base<value_type, __void_pointer> __node_base;
537 typedef __list_node<value_type, __void_pointer> __node;
538 typedef typename __rebind_alloc_helper<__alloc_traits, __node>::type __node_allocator;
539 typedef allocator_traits<__node_allocator> __node_alloc_traits;
540 typedef typename __node_alloc_traits::pointer __node_pointer;
541 typedef typename __node_alloc_traits::pointer __node_const_pointer;
542 typedef __list_node_pointer_traits<value_type, __void_pointer> __node_pointer_traits;
543 typedef typename __node_pointer_traits::__link_pointer __link_pointer;
544 typedef __link_pointer __link_const_pointer;
545 typedef typename __alloc_traits::pointer pointer;
546 typedef typename __alloc_traits::const_pointer const_pointer;
547 typedef typename __alloc_traits::difference_type difference_type;
549 typedef typename __rebind_alloc_helper<__alloc_traits, __node_base>::type __node_base_allocator;
550 typedef typename allocator_traits<__node_base_allocator>::pointer __node_base_pointer;
553 __compressed_pair<size_type, __node_allocator> __size_alloc_;
555 _LIBCPP_INLINE_VISIBILITY
556 __link_pointer __end_as_link() const _NOEXCEPT {
557 return __node_pointer_traits::__unsafe_link_pointer_cast(
558 const_cast<__node_base&>(__end_).__self());
561 _LIBCPP_INLINE_VISIBILITY
562 size_type& __sz() _NOEXCEPT {return __size_alloc_.first();}
563 _LIBCPP_INLINE_VISIBILITY
564 const size_type& __sz() const _NOEXCEPT
565 {return __size_alloc_.first();}
566 _LIBCPP_INLINE_VISIBILITY
567 __node_allocator& __node_alloc() _NOEXCEPT
568 {return __size_alloc_.second();}
569 _LIBCPP_INLINE_VISIBILITY
570 const __node_allocator& __node_alloc() const _NOEXCEPT
571 {return __size_alloc_.second();}
573 _LIBCPP_INLINE_VISIBILITY
574 size_type __node_alloc_max_size() const _NOEXCEPT {
575 return __node_alloc_traits::max_size(__node_alloc());
577 _LIBCPP_INLINE_VISIBILITY
578 static void __unlink_nodes(__link_pointer __f, __link_pointer __l) _NOEXCEPT;
580 _LIBCPP_INLINE_VISIBILITY
582 _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value);
583 _LIBCPP_INLINE_VISIBILITY
584 __list_imp(const allocator_type& __a);
586 void clear() _NOEXCEPT;
587 _LIBCPP_INLINE_VISIBILITY
588 bool empty() const _NOEXCEPT {return __sz() == 0;}
590 _LIBCPP_INLINE_VISIBILITY
591 iterator begin() _NOEXCEPT
593 #if _LIBCPP_DEBUG_LEVEL >= 2
594 return iterator(__end_.__next_, this);
596 return iterator(__end_.__next_);
599 _LIBCPP_INLINE_VISIBILITY
600 const_iterator begin() const _NOEXCEPT
602 #if _LIBCPP_DEBUG_LEVEL >= 2
603 return const_iterator(__end_.__next_, this);
605 return const_iterator(__end_.__next_);
608 _LIBCPP_INLINE_VISIBILITY
609 iterator end() _NOEXCEPT
611 #if _LIBCPP_DEBUG_LEVEL >= 2
612 return iterator(__end_as_link(), this);
614 return iterator(__end_as_link());
617 _LIBCPP_INLINE_VISIBILITY
618 const_iterator end() const _NOEXCEPT
620 #if _LIBCPP_DEBUG_LEVEL >= 2
621 return const_iterator(__end_as_link(), this);
623 return const_iterator(__end_as_link());
627 void swap(__list_imp& __c)
628 #if _LIBCPP_STD_VER >= 14
631 _NOEXCEPT_DEBUG_(!__alloc_traits::propagate_on_container_swap::value ||
632 __is_nothrow_swappable<allocator_type>::value);
635 _LIBCPP_INLINE_VISIBILITY
636 void __copy_assign_alloc(const __list_imp& __c)
637 {__copy_assign_alloc(__c, integral_constant<bool,
638 __node_alloc_traits::propagate_on_container_copy_assignment::value>());}
640 _LIBCPP_INLINE_VISIBILITY
641 void __move_assign_alloc(__list_imp& __c)
643 !__node_alloc_traits::propagate_on_container_move_assignment::value ||
644 is_nothrow_move_assignable<__node_allocator>::value)
645 {__move_assign_alloc(__c, integral_constant<bool,
646 __node_alloc_traits::propagate_on_container_move_assignment::value>());}
649 _LIBCPP_INLINE_VISIBILITY
650 void __copy_assign_alloc(const __list_imp& __c, true_type)
652 if (__node_alloc() != __c.__node_alloc())
654 __node_alloc() = __c.__node_alloc();
657 _LIBCPP_INLINE_VISIBILITY
658 void __copy_assign_alloc(const __list_imp&, false_type)
661 _LIBCPP_INLINE_VISIBILITY
662 void __move_assign_alloc(__list_imp& __c, true_type)
663 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
665 __node_alloc() = _VSTD::move(__c.__node_alloc());
668 _LIBCPP_INLINE_VISIBILITY
669 void __move_assign_alloc(__list_imp&, false_type)
673 _LIBCPP_INLINE_VISIBILITY
674 void __invalidate_all_iterators() {
675 #if _LIBCPP_DEBUG_LEVEL >= 2
676 __get_db()->__invalidate_all(this);
681 // Unlink nodes [__f, __l]
682 template <class _Tp, class _Alloc>
685 __list_imp<_Tp, _Alloc>::__unlink_nodes(__link_pointer __f, __link_pointer __l)
688 __f->__prev_->__next_ = __l->__next_;
689 __l->__next_->__prev_ = __f->__prev_;
692 template <class _Tp, class _Alloc>
694 __list_imp<_Tp, _Alloc>::__list_imp()
695 _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value)
700 template <class _Tp, class _Alloc>
702 __list_imp<_Tp, _Alloc>::__list_imp(const allocator_type& __a)
703 : __size_alloc_(0, __node_allocator(__a))
707 template <class _Tp, class _Alloc>
708 __list_imp<_Tp, _Alloc>::~__list_imp()
711 #if _LIBCPP_DEBUG_LEVEL >= 2
712 __get_db()->__erase_c(this);
716 template <class _Tp, class _Alloc>
718 __list_imp<_Tp, _Alloc>::clear() _NOEXCEPT
722 __node_allocator& __na = __node_alloc();
723 __link_pointer __f = __end_.__next_;
724 __link_pointer __l = __end_as_link();
725 __unlink_nodes(__f, __l->__prev_);
729 __node_pointer __np = __f->__as_node();
731 __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
732 __node_alloc_traits::deallocate(__na, __np, 1);
734 __invalidate_all_iterators();
738 template <class _Tp, class _Alloc>
740 __list_imp<_Tp, _Alloc>::swap(__list_imp& __c)
741 #if _LIBCPP_STD_VER >= 14
744 _NOEXCEPT_DEBUG_(!__alloc_traits::propagate_on_container_swap::value ||
745 __is_nothrow_swappable<allocator_type>::value)
748 _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
749 this->__node_alloc() == __c.__node_alloc(),
750 "list::swap: Either propagate_on_container_swap must be true"
751 " or the allocators must compare equal");
753 __swap_allocator(__node_alloc(), __c.__node_alloc());
754 swap(__sz(), __c.__sz());
755 swap(__end_, __c.__end_);
757 __end_.__next_ = __end_.__prev_ = __end_as_link();
759 __end_.__prev_->__next_ = __end_.__next_->__prev_ = __end_as_link();
761 __c.__end_.__next_ = __c.__end_.__prev_ = __c.__end_as_link();
763 __c.__end_.__prev_->__next_ = __c.__end_.__next_->__prev_ = __c.__end_as_link();
765 #if _LIBCPP_DEBUG_LEVEL >= 2
766 __libcpp_db* __db = __get_db();
767 __c_node* __cn1 = __db->__find_c_and_lock(this);
768 __c_node* __cn2 = __db->__find_c(&__c);
769 std::swap(__cn1->beg_, __cn2->beg_);
770 std::swap(__cn1->end_, __cn2->end_);
771 std::swap(__cn1->cap_, __cn2->cap_);
772 for (__i_node** __p = __cn1->end_; __p != __cn1->beg_;)
775 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
776 if (__i->__ptr_ == __c.__end_as_link())
779 if (--__cn1->end_ != __p)
780 memmove(__p, __p+1, (__cn1->end_ - __p)*sizeof(__i_node*));
783 (*__p)->__c_ = __cn1;
785 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
788 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
789 if (__i->__ptr_ == __end_as_link())
792 if (--__cn2->end_ != __p)
793 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
796 (*__p)->__c_ = __cn2;
802 template <class _Tp, class _Alloc /*= allocator<_Tp>*/>
803 class _LIBCPP_TEMPLATE_VIS list
804 : private __list_imp<_Tp, _Alloc>
806 typedef __list_imp<_Tp, _Alloc> base;
807 typedef typename base::__node __node;
808 typedef typename base::__node_allocator __node_allocator;
809 typedef typename base::__node_pointer __node_pointer;
810 typedef typename base::__node_alloc_traits __node_alloc_traits;
811 typedef typename base::__node_base __node_base;
812 typedef typename base::__node_base_pointer __node_base_pointer;
813 typedef typename base::__link_pointer __link_pointer;
816 typedef _Tp value_type;
817 typedef _Alloc allocator_type;
818 static_assert((is_same<value_type, typename allocator_type::value_type>::value),
819 "Invalid allocator::value_type");
820 typedef value_type& reference;
821 typedef const value_type& const_reference;
822 typedef typename base::pointer pointer;
823 typedef typename base::const_pointer const_pointer;
824 typedef typename base::size_type size_type;
825 typedef typename base::difference_type difference_type;
826 typedef typename base::iterator iterator;
827 typedef typename base::const_iterator const_iterator;
828 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
829 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
831 _LIBCPP_INLINE_VISIBILITY
833 _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value)
835 #if _LIBCPP_DEBUG_LEVEL >= 2
836 __get_db()->__insert_c(this);
839 _LIBCPP_INLINE_VISIBILITY
840 explicit list(const allocator_type& __a) : base(__a)
842 #if _LIBCPP_DEBUG_LEVEL >= 2
843 __get_db()->__insert_c(this);
846 explicit list(size_type __n);
847 #if _LIBCPP_STD_VER > 11
848 explicit list(size_type __n, const allocator_type& __a);
850 list(size_type __n, const value_type& __x);
851 list(size_type __n, const value_type& __x, const allocator_type& __a);
852 template <class _InpIter>
853 list(_InpIter __f, _InpIter __l,
854 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
855 template <class _InpIter>
856 list(_InpIter __f, _InpIter __l, const allocator_type& __a,
857 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
859 list(const list& __c);
860 list(const list& __c, const allocator_type& __a);
861 _LIBCPP_INLINE_VISIBILITY
862 list& operator=(const list& __c);
863 #ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
864 list(initializer_list<value_type> __il);
865 list(initializer_list<value_type> __il, const allocator_type& __a);
866 #endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
867 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
868 _LIBCPP_INLINE_VISIBILITY
870 _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value);
871 _LIBCPP_INLINE_VISIBILITY
872 list(list&& __c, const allocator_type& __a);
873 _LIBCPP_INLINE_VISIBILITY
874 list& operator=(list&& __c)
876 __node_alloc_traits::propagate_on_container_move_assignment::value &&
877 is_nothrow_move_assignable<__node_allocator>::value);
878 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
879 #ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
880 _LIBCPP_INLINE_VISIBILITY
881 list& operator=(initializer_list<value_type> __il)
882 {assign(__il.begin(), __il.end()); return *this;}
883 #endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
885 template <class _InpIter>
886 void assign(_InpIter __f, _InpIter __l,
887 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
888 void assign(size_type __n, const value_type& __x);
889 #ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
890 _LIBCPP_INLINE_VISIBILITY
891 void assign(initializer_list<value_type> __il)
892 {assign(__il.begin(), __il.end());}
893 #endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
895 _LIBCPP_INLINE_VISIBILITY
896 allocator_type get_allocator() const _NOEXCEPT;
898 _LIBCPP_INLINE_VISIBILITY
899 size_type size() const _NOEXCEPT {return base::__sz();}
900 _LIBCPP_INLINE_VISIBILITY
901 bool empty() const _NOEXCEPT {return base::empty();}
902 _LIBCPP_INLINE_VISIBILITY
903 size_type max_size() const _NOEXCEPT
905 return std::min<size_type>(
906 base::__node_alloc_max_size(),
907 numeric_limits<difference_type >::max());
910 _LIBCPP_INLINE_VISIBILITY
911 iterator begin() _NOEXCEPT {return base::begin();}
912 _LIBCPP_INLINE_VISIBILITY
913 const_iterator begin() const _NOEXCEPT {return base::begin();}
914 _LIBCPP_INLINE_VISIBILITY
915 iterator end() _NOEXCEPT {return base::end();}
916 _LIBCPP_INLINE_VISIBILITY
917 const_iterator end() const _NOEXCEPT {return base::end();}
918 _LIBCPP_INLINE_VISIBILITY
919 const_iterator cbegin() const _NOEXCEPT {return base::begin();}
920 _LIBCPP_INLINE_VISIBILITY
921 const_iterator cend() const _NOEXCEPT {return base::end();}
923 _LIBCPP_INLINE_VISIBILITY
924 reverse_iterator rbegin() _NOEXCEPT
925 {return reverse_iterator(end());}
926 _LIBCPP_INLINE_VISIBILITY
927 const_reverse_iterator rbegin() const _NOEXCEPT
928 {return const_reverse_iterator(end());}
929 _LIBCPP_INLINE_VISIBILITY
930 reverse_iterator rend() _NOEXCEPT
931 {return reverse_iterator(begin());}
932 _LIBCPP_INLINE_VISIBILITY
933 const_reverse_iterator rend() const _NOEXCEPT
934 {return const_reverse_iterator(begin());}
935 _LIBCPP_INLINE_VISIBILITY
936 const_reverse_iterator crbegin() const _NOEXCEPT
937 {return const_reverse_iterator(end());}
938 _LIBCPP_INLINE_VISIBILITY
939 const_reverse_iterator crend() const _NOEXCEPT
940 {return const_reverse_iterator(begin());}
942 _LIBCPP_INLINE_VISIBILITY
945 _LIBCPP_ASSERT(!empty(), "list::front called on empty list");
946 return base::__end_.__next_->__as_node()->__value_;
948 _LIBCPP_INLINE_VISIBILITY
949 const_reference front() const
951 _LIBCPP_ASSERT(!empty(), "list::front called on empty list");
952 return base::__end_.__next_->__as_node()->__value_;
954 _LIBCPP_INLINE_VISIBILITY
957 _LIBCPP_ASSERT(!empty(), "list::back called on empty list");
958 return base::__end_.__prev_->__as_node()->__value_;
960 _LIBCPP_INLINE_VISIBILITY
961 const_reference back() const
963 _LIBCPP_ASSERT(!empty(), "list::back called on empty list");
964 return base::__end_.__prev_->__as_node()->__value_;
967 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
968 void push_front(value_type&& __x);
969 void push_back(value_type&& __x);
970 #ifndef _LIBCPP_HAS_NO_VARIADICS
971 template <class... _Args>
972 reference emplace_front(_Args&&... __args);
973 template <class... _Args>
974 reference emplace_back(_Args&&... __args);
975 template <class... _Args>
976 iterator emplace(const_iterator __p, _Args&&... __args);
977 #endif // _LIBCPP_HAS_NO_VARIADICS
978 iterator insert(const_iterator __p, value_type&& __x);
979 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
981 void push_front(const value_type& __x);
982 void push_back(const value_type& __x);
984 iterator insert(const_iterator __p, const value_type& __x);
985 iterator insert(const_iterator __p, size_type __n, const value_type& __x);
986 template <class _InpIter>
987 iterator insert(const_iterator __p, _InpIter __f, _InpIter __l,
988 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
989 #ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
990 _LIBCPP_INLINE_VISIBILITY
991 iterator insert(const_iterator __p, initializer_list<value_type> __il)
992 {return insert(__p, __il.begin(), __il.end());}
993 #endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
995 _LIBCPP_INLINE_VISIBILITY
997 #if _LIBCPP_STD_VER >= 14
1000 _NOEXCEPT_DEBUG_(!__node_alloc_traits::propagate_on_container_swap::value ||
1001 __is_nothrow_swappable<__node_allocator>::value)
1004 _LIBCPP_INLINE_VISIBILITY
1005 void clear() _NOEXCEPT {base::clear();}
1010 iterator erase(const_iterator __p);
1011 iterator erase(const_iterator __f, const_iterator __l);
1013 void resize(size_type __n);
1014 void resize(size_type __n, const value_type& __x);
1016 void splice(const_iterator __p, list& __c);
1017 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1018 _LIBCPP_INLINE_VISIBILITY
1019 void splice(const_iterator __p, list&& __c) {splice(__p, __c);}
1021 void splice(const_iterator __p, list& __c, const_iterator __i);
1022 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1023 _LIBCPP_INLINE_VISIBILITY
1024 void splice(const_iterator __p, list&& __c, const_iterator __i)
1025 {splice(__p, __c, __i);}
1026 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1027 void splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l);
1028 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1029 _LIBCPP_INLINE_VISIBILITY
1030 void splice(const_iterator __p, list&& __c, const_iterator __f, const_iterator __l)
1031 {splice(__p, __c, __f, __l);}
1032 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1034 void remove(const value_type& __x);
1035 template <class _Pred> void remove_if(_Pred __pred);
1036 _LIBCPP_INLINE_VISIBILITY
1038 template <class _BinaryPred>
1039 void unique(_BinaryPred __binary_pred);
1040 _LIBCPP_INLINE_VISIBILITY
1041 void merge(list& __c);
1042 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1043 _LIBCPP_INLINE_VISIBILITY
1044 void merge(list&& __c) {merge(__c);}
1046 template <class _Comp>
1047 void merge(list& __c, _Comp __comp);
1048 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1049 template <class _Comp>
1050 _LIBCPP_INLINE_VISIBILITY
1051 void merge(list&& __c, _Comp __comp) {merge(__c, __comp);}
1052 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1053 _LIBCPP_INLINE_VISIBILITY
1055 template <class _Comp>
1056 _LIBCPP_INLINE_VISIBILITY
1057 void sort(_Comp __comp);
1059 void reverse() _NOEXCEPT;
1061 bool __invariants() const;
1063 #if _LIBCPP_DEBUG_LEVEL >= 2
1065 bool __dereferenceable(const const_iterator* __i) const;
1066 bool __decrementable(const const_iterator* __i) const;
1067 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1068 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1070 #endif // _LIBCPP_DEBUG_LEVEL >= 2
1073 _LIBCPP_INLINE_VISIBILITY
1074 static void __link_nodes (__link_pointer __p, __link_pointer __f, __link_pointer __l);
1075 _LIBCPP_INLINE_VISIBILITY
1076 void __link_nodes_at_front(__link_pointer __f, __link_pointer __l);
1077 _LIBCPP_INLINE_VISIBILITY
1078 void __link_nodes_at_back (__link_pointer __f, __link_pointer __l);
1079 iterator __iterator(size_type __n);
1080 template <class _Comp>
1081 static iterator __sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp);
1083 void __move_assign(list& __c, true_type)
1084 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value);
1085 void __move_assign(list& __c, false_type);
1088 // Link in nodes [__f, __l] just prior to __p
1089 template <class _Tp, class _Alloc>
1092 list<_Tp, _Alloc>::__link_nodes(__link_pointer __p, __link_pointer __f, __link_pointer __l)
1094 __p->__prev_->__next_ = __f;
1095 __f->__prev_ = __p->__prev_;
1100 // Link in nodes [__f, __l] at the front of the list
1101 template <class _Tp, class _Alloc>
1104 list<_Tp, _Alloc>::__link_nodes_at_front(__link_pointer __f, __link_pointer __l)
1106 __f->__prev_ = base::__end_as_link();
1107 __l->__next_ = base::__end_.__next_;
1108 __l->__next_->__prev_ = __l;
1109 base::__end_.__next_ = __f;
1112 // Link in nodes [__f, __l] at the front of the list
1113 template <class _Tp, class _Alloc>
1116 list<_Tp, _Alloc>::__link_nodes_at_back(__link_pointer __f, __link_pointer __l)
1118 __l->__next_ = base::__end_as_link();
1119 __f->__prev_ = base::__end_.__prev_;
1120 __f->__prev_->__next_ = __f;
1121 base::__end_.__prev_ = __l;
1125 template <class _Tp, class _Alloc>
1127 typename list<_Tp, _Alloc>::iterator
1128 list<_Tp, _Alloc>::__iterator(size_type __n)
1130 return __n <= base::__sz() / 2 ? _VSTD::next(begin(), __n)
1131 : _VSTD::prev(end(), base::__sz() - __n);
1134 template <class _Tp, class _Alloc>
1135 list<_Tp, _Alloc>::list(size_type __n)
1137 #if _LIBCPP_DEBUG_LEVEL >= 2
1138 __get_db()->__insert_c(this);
1140 for (; __n > 0; --__n)
1141 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1144 push_back(value_type());
1148 #if _LIBCPP_STD_VER > 11
1149 template <class _Tp, class _Alloc>
1150 list<_Tp, _Alloc>::list(size_type __n, const allocator_type& __a) : base(__a)
1152 #if _LIBCPP_DEBUG_LEVEL >= 2
1153 __get_db()->__insert_c(this);
1155 for (; __n > 0; --__n)
1156 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1159 push_back(value_type());
1164 template <class _Tp, class _Alloc>
1165 list<_Tp, _Alloc>::list(size_type __n, const value_type& __x)
1167 #if _LIBCPP_DEBUG_LEVEL >= 2
1168 __get_db()->__insert_c(this);
1170 for (; __n > 0; --__n)
1174 template <class _Tp, class _Alloc>
1175 list<_Tp, _Alloc>::list(size_type __n, const value_type& __x, const allocator_type& __a)
1178 #if _LIBCPP_DEBUG_LEVEL >= 2
1179 __get_db()->__insert_c(this);
1181 for (; __n > 0; --__n)
1185 template <class _Tp, class _Alloc>
1186 template <class _InpIter>
1187 list<_Tp, _Alloc>::list(_InpIter __f, _InpIter __l,
1188 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1190 #if _LIBCPP_DEBUG_LEVEL >= 2
1191 __get_db()->__insert_c(this);
1193 for (; __f != __l; ++__f)
1197 template <class _Tp, class _Alloc>
1198 template <class _InpIter>
1199 list<_Tp, _Alloc>::list(_InpIter __f, _InpIter __l, const allocator_type& __a,
1200 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1203 #if _LIBCPP_DEBUG_LEVEL >= 2
1204 __get_db()->__insert_c(this);
1206 for (; __f != __l; ++__f)
1210 template <class _Tp, class _Alloc>
1211 list<_Tp, _Alloc>::list(const list& __c)
1212 : base(allocator_type(
1213 __node_alloc_traits::select_on_container_copy_construction(
1214 __c.__node_alloc())))
1216 #if _LIBCPP_DEBUG_LEVEL >= 2
1217 __get_db()->__insert_c(this);
1219 for (const_iterator __i = __c.begin(), __e = __c.end(); __i != __e; ++__i)
1223 template <class _Tp, class _Alloc>
1224 list<_Tp, _Alloc>::list(const list& __c, const allocator_type& __a)
1227 #if _LIBCPP_DEBUG_LEVEL >= 2
1228 __get_db()->__insert_c(this);
1230 for (const_iterator __i = __c.begin(), __e = __c.end(); __i != __e; ++__i)
1234 #ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1236 template <class _Tp, class _Alloc>
1237 list<_Tp, _Alloc>::list(initializer_list<value_type> __il, const allocator_type& __a)
1240 #if _LIBCPP_DEBUG_LEVEL >= 2
1241 __get_db()->__insert_c(this);
1243 for (typename initializer_list<value_type>::const_iterator __i = __il.begin(),
1244 __e = __il.end(); __i != __e; ++__i)
1248 template <class _Tp, class _Alloc>
1249 list<_Tp, _Alloc>::list(initializer_list<value_type> __il)
1251 #if _LIBCPP_DEBUG_LEVEL >= 2
1252 __get_db()->__insert_c(this);
1254 for (typename initializer_list<value_type>::const_iterator __i = __il.begin(),
1255 __e = __il.end(); __i != __e; ++__i)
1259 #endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1261 template <class _Tp, class _Alloc>
1264 list<_Tp, _Alloc>::operator=(const list& __c)
1268 base::__copy_assign_alloc(__c);
1269 assign(__c.begin(), __c.end());
1274 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1276 template <class _Tp, class _Alloc>
1278 list<_Tp, _Alloc>::list(list&& __c)
1279 _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value)
1280 : base(allocator_type(_VSTD::move(__c.__node_alloc())))
1282 #if _LIBCPP_DEBUG_LEVEL >= 2
1283 __get_db()->__insert_c(this);
1288 template <class _Tp, class _Alloc>
1290 list<_Tp, _Alloc>::list(list&& __c, const allocator_type& __a)
1293 #if _LIBCPP_DEBUG_LEVEL >= 2
1294 __get_db()->__insert_c(this);
1296 if (__a == __c.get_allocator())
1300 typedef move_iterator<iterator> _Ip;
1301 assign(_Ip(__c.begin()), _Ip(__c.end()));
1305 template <class _Tp, class _Alloc>
1308 list<_Tp, _Alloc>::operator=(list&& __c)
1310 __node_alloc_traits::propagate_on_container_move_assignment::value &&
1311 is_nothrow_move_assignable<__node_allocator>::value)
1313 __move_assign(__c, integral_constant<bool,
1314 __node_alloc_traits::propagate_on_container_move_assignment::value>());
1318 template <class _Tp, class _Alloc>
1320 list<_Tp, _Alloc>::__move_assign(list& __c, false_type)
1322 if (base::__node_alloc() != __c.__node_alloc())
1324 typedef move_iterator<iterator> _Ip;
1325 assign(_Ip(__c.begin()), _Ip(__c.end()));
1328 __move_assign(__c, true_type());
1331 template <class _Tp, class _Alloc>
1333 list<_Tp, _Alloc>::__move_assign(list& __c, true_type)
1334 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
1337 base::__move_assign_alloc(__c);
1341 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1343 template <class _Tp, class _Alloc>
1344 template <class _InpIter>
1346 list<_Tp, _Alloc>::assign(_InpIter __f, _InpIter __l,
1347 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1349 iterator __i = begin();
1350 iterator __e = end();
1351 for (; __f != __l && __i != __e; ++__f, ++__i)
1354 insert(__e, __f, __l);
1357 #if _LIBCPP_DEBUG_LEVEL >= 2
1358 __get_db()->__invalidate_all(this);
1362 template <class _Tp, class _Alloc>
1364 list<_Tp, _Alloc>::assign(size_type __n, const value_type& __x)
1366 iterator __i = begin();
1367 iterator __e = end();
1368 for (; __n > 0 && __i != __e; --__n, ++__i)
1371 insert(__e, __n, __x);
1374 #if _LIBCPP_DEBUG_LEVEL >= 2
1375 __get_db()->__invalidate_all(this);
1379 template <class _Tp, class _Alloc>
1382 list<_Tp, _Alloc>::get_allocator() const _NOEXCEPT
1384 return allocator_type(base::__node_alloc());
1387 template <class _Tp, class _Alloc>
1388 typename list<_Tp, _Alloc>::iterator
1389 list<_Tp, _Alloc>::insert(const_iterator __p, const value_type& __x)
1391 #if _LIBCPP_DEBUG_LEVEL >= 2
1392 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1393 "list::insert(iterator, x) called with an iterator not"
1394 " referring to this list");
1396 __node_allocator& __na = base::__node_alloc();
1397 typedef __allocator_destructor<__node_allocator> _Dp;
1398 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
1399 __hold->__prev_ = 0;
1400 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
1401 __link_nodes(__p.__ptr_, __hold->__as_link(), __hold->__as_link());
1403 #if _LIBCPP_DEBUG_LEVEL >= 2
1404 return iterator(__hold.release()->__as_link(), this);
1406 return iterator(__hold.release()->__as_link());
1410 template <class _Tp, class _Alloc>
1411 typename list<_Tp, _Alloc>::iterator
1412 list<_Tp, _Alloc>::insert(const_iterator __p, size_type __n, const value_type& __x)
1414 #if _LIBCPP_DEBUG_LEVEL >= 2
1415 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1416 "list::insert(iterator, n, x) called with an iterator not"
1417 " referring to this list");
1418 iterator __r(__p.__ptr_, this);
1420 iterator __r(__p.__ptr_);
1425 __node_allocator& __na = base::__node_alloc();
1426 typedef __allocator_destructor<__node_allocator> _Dp;
1427 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
1428 __hold->__prev_ = 0;
1429 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
1431 #if _LIBCPP_DEBUG_LEVEL >= 2
1432 __r = iterator(__hold->__as_link(), this);
1434 __r = iterator(__hold->__as_link());
1438 #ifndef _LIBCPP_NO_EXCEPTIONS
1441 #endif // _LIBCPP_NO_EXCEPTIONS
1442 for (--__n; __n != 0; --__n, ++__e, ++__ds)
1444 __hold.reset(__node_alloc_traits::allocate(__na, 1));
1445 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
1446 __e.__ptr_->__next_ = __hold->__as_link();
1447 __hold->__prev_ = __e.__ptr_;
1450 #ifndef _LIBCPP_NO_EXCEPTIONS
1456 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
1457 __link_pointer __prev = __e.__ptr_->__prev_;
1458 __node_alloc_traits::deallocate(__na, __e.__ptr_->__as_node(), 1);
1461 #if _LIBCPP_DEBUG_LEVEL >= 2
1462 __e = iterator(__prev, this);
1464 __e = iterator(__prev);
1469 #endif // _LIBCPP_NO_EXCEPTIONS
1470 __link_nodes(__p.__ptr_, __r.__ptr_, __e.__ptr_);
1471 base::__sz() += __ds;
1476 template <class _Tp, class _Alloc>
1477 template <class _InpIter>
1478 typename list<_Tp, _Alloc>::iterator
1479 list<_Tp, _Alloc>::insert(const_iterator __p, _InpIter __f, _InpIter __l,
1480 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1482 #if _LIBCPP_DEBUG_LEVEL >= 2
1483 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1484 "list::insert(iterator, range) called with an iterator not"
1485 " referring to this list");
1486 iterator __r(__p.__ptr_, this);
1488 iterator __r(__p.__ptr_);
1493 __node_allocator& __na = base::__node_alloc();
1494 typedef __allocator_destructor<__node_allocator> _Dp;
1495 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
1496 __hold->__prev_ = 0;
1497 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), *__f);
1499 #if _LIBCPP_DEBUG_LEVEL >= 2
1500 __r = iterator(__hold.get()->__as_link(), this);
1502 __r = iterator(__hold.get()->__as_link());
1506 #ifndef _LIBCPP_NO_EXCEPTIONS
1509 #endif // _LIBCPP_NO_EXCEPTIONS
1510 for (++__f; __f != __l; ++__f, (void) ++__e, (void) ++__ds)
1512 __hold.reset(__node_alloc_traits::allocate(__na, 1));
1513 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), *__f);
1514 __e.__ptr_->__next_ = __hold.get()->__as_link();
1515 __hold->__prev_ = __e.__ptr_;
1518 #ifndef _LIBCPP_NO_EXCEPTIONS
1524 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
1525 __link_pointer __prev = __e.__ptr_->__prev_;
1526 __node_alloc_traits::deallocate(__na, __e.__ptr_->__as_node(), 1);
1529 #if _LIBCPP_DEBUG_LEVEL >= 2
1530 __e = iterator(__prev, this);
1532 __e = iterator(__prev);
1537 #endif // _LIBCPP_NO_EXCEPTIONS
1538 __link_nodes(__p.__ptr_, __r.__ptr_, __e.__ptr_);
1539 base::__sz() += __ds;
1544 template <class _Tp, class _Alloc>
1546 list<_Tp, _Alloc>::push_front(const value_type& __x)
1548 __node_allocator& __na = base::__node_alloc();
1549 typedef __allocator_destructor<__node_allocator> _Dp;
1550 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
1551 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
1552 __link_pointer __nl = __hold->__as_link();
1553 __link_nodes_at_front(__nl, __nl);
1558 template <class _Tp, class _Alloc>
1560 list<_Tp, _Alloc>::push_back(const value_type& __x)
1562 __node_allocator& __na = base::__node_alloc();
1563 typedef __allocator_destructor<__node_allocator> _Dp;
1564 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
1565 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
1566 __link_nodes_at_back(__hold.get()->__as_link(), __hold.get()->__as_link());
1571 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1573 template <class _Tp, class _Alloc>
1575 list<_Tp, _Alloc>::push_front(value_type&& __x)
1577 __node_allocator& __na = base::__node_alloc();
1578 typedef __allocator_destructor<__node_allocator> _Dp;
1579 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
1580 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
1581 __link_nodes_at_front(__hold.get()->__as_link(), __hold.get()->__as_link());
1586 template <class _Tp, class _Alloc>
1588 list<_Tp, _Alloc>::push_back(value_type&& __x)
1590 __node_allocator& __na = base::__node_alloc();
1591 typedef __allocator_destructor<__node_allocator> _Dp;
1592 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
1593 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
1594 __link_nodes_at_back(__hold.get()->__as_link(), __hold.get()->__as_link());
1599 #ifndef _LIBCPP_HAS_NO_VARIADICS
1601 template <class _Tp, class _Alloc>
1602 template <class... _Args>
1603 typename list<_Tp, _Alloc>::reference
1604 list<_Tp, _Alloc>::emplace_front(_Args&&... __args)
1606 __node_allocator& __na = base::__node_alloc();
1607 typedef __allocator_destructor<__node_allocator> _Dp;
1608 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
1609 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
1610 __link_nodes_at_front(__hold.get()->__as_link(), __hold.get()->__as_link());
1612 return __hold.release()->__value_;
1615 template <class _Tp, class _Alloc>
1616 template <class... _Args>
1617 typename list<_Tp, _Alloc>::reference
1618 list<_Tp, _Alloc>::emplace_back(_Args&&... __args)
1620 __node_allocator& __na = base::__node_alloc();
1621 typedef __allocator_destructor<__node_allocator> _Dp;
1622 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
1623 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
1624 __link_pointer __nl = __hold->__as_link();
1625 __link_nodes_at_back(__nl, __nl);
1627 return __hold.release()->__value_;
1630 template <class _Tp, class _Alloc>
1631 template <class... _Args>
1632 typename list<_Tp, _Alloc>::iterator
1633 list<_Tp, _Alloc>::emplace(const_iterator __p, _Args&&... __args)
1635 #if _LIBCPP_DEBUG_LEVEL >= 2
1636 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1637 "list::emplace(iterator, args...) called with an iterator not"
1638 " referring to this list");
1640 __node_allocator& __na = base::__node_alloc();
1641 typedef __allocator_destructor<__node_allocator> _Dp;
1642 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
1643 __hold->__prev_ = 0;
1644 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
1645 __link_pointer __nl = __hold.get()->__as_link();
1646 __link_nodes(__p.__ptr_, __nl, __nl);
1649 #if _LIBCPP_DEBUG_LEVEL >= 2
1650 return iterator(__nl, this);
1652 return iterator(__nl);
1656 #endif // _LIBCPP_HAS_NO_VARIADICS
1658 template <class _Tp, class _Alloc>
1659 typename list<_Tp, _Alloc>::iterator
1660 list<_Tp, _Alloc>::insert(const_iterator __p, value_type&& __x)
1662 #if _LIBCPP_DEBUG_LEVEL >= 2
1663 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1664 "list::insert(iterator, x) called with an iterator not"
1665 " referring to this list");
1667 __node_allocator& __na = base::__node_alloc();
1668 typedef __allocator_destructor<__node_allocator> _Dp;
1669 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
1670 __hold->__prev_ = 0;
1671 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
1672 __link_pointer __nl = __hold->__as_link();
1673 __link_nodes(__p.__ptr_, __nl, __nl);
1676 #if _LIBCPP_DEBUG_LEVEL >= 2
1677 return iterator(__nl, this);
1679 return iterator(__nl);
1683 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1685 template <class _Tp, class _Alloc>
1687 list<_Tp, _Alloc>::pop_front()
1689 _LIBCPP_ASSERT(!empty(), "list::pop_front() called with empty list");
1690 __node_allocator& __na = base::__node_alloc();
1691 __link_pointer __n = base::__end_.__next_;
1692 base::__unlink_nodes(__n, __n);
1694 #if _LIBCPP_DEBUG_LEVEL >= 2
1695 __c_node* __c = __get_db()->__find_c_and_lock(this);
1696 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1699 iterator* __i = static_cast<iterator*>((*__p)->__i_);
1700 if (__i->__ptr_ == __n)
1702 (*__p)->__c_ = nullptr;
1703 if (--__c->end_ != __p)
1704 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1707 __get_db()->unlock();
1709 __node_pointer __np = __n->__as_node();
1710 __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
1711 __node_alloc_traits::deallocate(__na, __np, 1);
1714 template <class _Tp, class _Alloc>
1716 list<_Tp, _Alloc>::pop_back()
1718 _LIBCPP_ASSERT(!empty(), "list::pop_back() called with empty list");
1719 __node_allocator& __na = base::__node_alloc();
1720 __link_pointer __n = base::__end_.__prev_;
1721 base::__unlink_nodes(__n, __n);
1723 #if _LIBCPP_DEBUG_LEVEL >= 2
1724 __c_node* __c = __get_db()->__find_c_and_lock(this);
1725 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1728 iterator* __i = static_cast<iterator*>((*__p)->__i_);
1729 if (__i->__ptr_ == __n)
1731 (*__p)->__c_ = nullptr;
1732 if (--__c->end_ != __p)
1733 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1736 __get_db()->unlock();
1738 __node_pointer __np = __n->__as_node();
1739 __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
1740 __node_alloc_traits::deallocate(__na, __np, 1);
1743 template <class _Tp, class _Alloc>
1744 typename list<_Tp, _Alloc>::iterator
1745 list<_Tp, _Alloc>::erase(const_iterator __p)
1747 #if _LIBCPP_DEBUG_LEVEL >= 2
1748 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1749 "list::erase(iterator) called with an iterator not"
1750 " referring to this list");
1752 _LIBCPP_ASSERT(__p != end(),
1753 "list::erase(iterator) called with a non-dereferenceable iterator");
1754 __node_allocator& __na = base::__node_alloc();
1755 __link_pointer __n = __p.__ptr_;
1756 __link_pointer __r = __n->__next_;
1757 base::__unlink_nodes(__n, __n);
1759 #if _LIBCPP_DEBUG_LEVEL >= 2
1760 __c_node* __c = __get_db()->__find_c_and_lock(this);
1761 for (__i_node** __ip = __c->end_; __ip != __c->beg_; )
1764 iterator* __i = static_cast<iterator*>((*__ip)->__i_);
1765 if (__i->__ptr_ == __n)
1767 (*__ip)->__c_ = nullptr;
1768 if (--__c->end_ != __ip)
1769 memmove(__ip, __ip+1, (__c->end_ - __ip)*sizeof(__i_node*));
1772 __get_db()->unlock();
1774 __node_pointer __np = __n->__as_node();
1775 __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
1776 __node_alloc_traits::deallocate(__na, __np, 1);
1777 #if _LIBCPP_DEBUG_LEVEL >= 2
1778 return iterator(__r, this);
1780 return iterator(__r);
1784 template <class _Tp, class _Alloc>
1785 typename list<_Tp, _Alloc>::iterator
1786 list<_Tp, _Alloc>::erase(const_iterator __f, const_iterator __l)
1788 #if _LIBCPP_DEBUG_LEVEL >= 2
1789 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__f) == this,
1790 "list::erase(iterator, iterator) called with an iterator not"
1791 " referring to this list");
1792 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__l) == this,
1793 "list::erase(iterator, iterator) called with an iterator not"
1794 " referring to this list");
1798 __node_allocator& __na = base::__node_alloc();
1799 base::__unlink_nodes(__f.__ptr_, __l.__ptr_->__prev_);
1802 __link_pointer __n = __f.__ptr_;
1805 #if _LIBCPP_DEBUG_LEVEL >= 2
1806 __c_node* __c = __get_db()->__find_c_and_lock(this);
1807 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1810 iterator* __i = static_cast<iterator*>((*__p)->__i_);
1811 if (__i->__ptr_ == __n)
1813 (*__p)->__c_ = nullptr;
1814 if (--__c->end_ != __p)
1815 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1818 __get_db()->unlock();
1820 __node_pointer __np = __n->__as_node();
1821 __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
1822 __node_alloc_traits::deallocate(__na, __np, 1);
1825 #if _LIBCPP_DEBUG_LEVEL >= 2
1826 return iterator(__l.__ptr_, this);
1828 return iterator(__l.__ptr_);
1832 template <class _Tp, class _Alloc>
1834 list<_Tp, _Alloc>::resize(size_type __n)
1836 if (__n < base::__sz())
1837 erase(__iterator(__n), end());
1838 else if (__n > base::__sz())
1840 __n -= base::__sz();
1842 __node_allocator& __na = base::__node_alloc();
1843 typedef __allocator_destructor<__node_allocator> _Dp;
1844 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
1845 __hold->__prev_ = 0;
1846 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_));
1848 #if _LIBCPP_DEBUG_LEVEL >= 2
1849 iterator __r = iterator(__hold.release()->__as_link(), this);
1851 iterator __r = iterator(__hold.release()->__as_link());
1854 #ifndef _LIBCPP_NO_EXCEPTIONS
1857 #endif // _LIBCPP_NO_EXCEPTIONS
1858 for (--__n; __n != 0; --__n, ++__e, ++__ds)
1860 __hold.reset(__node_alloc_traits::allocate(__na, 1));
1861 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_));
1862 __e.__ptr_->__next_ = __hold.get()->__as_link();
1863 __hold->__prev_ = __e.__ptr_;
1866 #ifndef _LIBCPP_NO_EXCEPTIONS
1872 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
1873 __link_pointer __prev = __e.__ptr_->__prev_;
1874 __node_alloc_traits::deallocate(__na, __e.__ptr_->__as_node(), 1);
1877 #if _LIBCPP_DEBUG_LEVEL >= 2
1878 __e = iterator(__prev, this);
1880 __e = iterator(__prev);
1885 #endif // _LIBCPP_NO_EXCEPTIONS
1886 __link_nodes_at_back(__r.__ptr_, __e.__ptr_);
1887 base::__sz() += __ds;
1891 template <class _Tp, class _Alloc>
1893 list<_Tp, _Alloc>::resize(size_type __n, const value_type& __x)
1895 if (__n < base::__sz())
1896 erase(__iterator(__n), end());
1897 else if (__n > base::__sz())
1899 __n -= base::__sz();
1901 __node_allocator& __na = base::__node_alloc();
1902 typedef __allocator_destructor<__node_allocator> _Dp;
1903 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
1904 __hold->__prev_ = 0;
1905 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
1907 __link_pointer __nl = __hold.release()->__as_link();
1908 #if _LIBCPP_DEBUG_LEVEL >= 2
1909 iterator __r = iterator(__nl, this);
1911 iterator __r = iterator(__nl);
1914 #ifndef _LIBCPP_NO_EXCEPTIONS
1917 #endif // _LIBCPP_NO_EXCEPTIONS
1918 for (--__n; __n != 0; --__n, ++__e, ++__ds)
1920 __hold.reset(__node_alloc_traits::allocate(__na, 1));
1921 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
1922 __e.__ptr_->__next_ = __hold.get()->__as_link();
1923 __hold->__prev_ = __e.__ptr_;
1926 #ifndef _LIBCPP_NO_EXCEPTIONS
1932 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
1933 __link_pointer __prev = __e.__ptr_->__prev_;
1934 __node_alloc_traits::deallocate(__na, __e.__ptr_->__as_node(), 1);
1937 #if _LIBCPP_DEBUG_LEVEL >= 2
1938 __e = iterator(__prev, this);
1940 __e = iterator(__prev);
1945 #endif // _LIBCPP_NO_EXCEPTIONS
1946 __link_nodes(base::__end_as_link(), __r.__ptr_, __e.__ptr_);
1947 base::__sz() += __ds;
1951 template <class _Tp, class _Alloc>
1953 list<_Tp, _Alloc>::splice(const_iterator __p, list& __c)
1955 _LIBCPP_ASSERT(this != &__c,
1956 "list::splice(iterator, list) called with this == &list");
1957 #if _LIBCPP_DEBUG_LEVEL >= 2
1958 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1959 "list::splice(iterator, list) called with an iterator not"
1960 " referring to this list");
1964 __link_pointer __f = __c.__end_.__next_;
1965 __link_pointer __l = __c.__end_.__prev_;
1966 base::__unlink_nodes(__f, __l);
1967 __link_nodes(__p.__ptr_, __f, __l);
1968 base::__sz() += __c.__sz();
1970 #if _LIBCPP_DEBUG_LEVEL >= 2
1971 __libcpp_db* __db = __get_db();
1972 __c_node* __cn1 = __db->__find_c_and_lock(this);
1973 __c_node* __cn2 = __db->__find_c(&__c);
1974 for (__i_node** __ip = __cn2->end_; __ip != __cn2->beg_;)
1977 iterator* __i = static_cast<iterator*>((*__ip)->__i_);
1978 if (__i->__ptr_ != __c.__end_as_link())
1980 __cn1->__add(*__ip);
1981 (*__ip)->__c_ = __cn1;
1982 if (--__cn2->end_ != __ip)
1983 memmove(__ip, __ip+1, (__cn2->end_ - __ip)*sizeof(__i_node*));
1991 template <class _Tp, class _Alloc>
1993 list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __i)
1995 #if _LIBCPP_DEBUG_LEVEL >= 2
1996 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1997 "list::splice(iterator, list, iterator) called with first iterator not"
1998 " referring to this list");
1999 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__i) == &__c,
2000 "list::splice(iterator, list, iterator) called with second iterator not"
2001 " referring to list argument");
2002 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(&__i),
2003 "list::splice(iterator, list, iterator) called with second iterator not"
2006 if (__p.__ptr_ != __i.__ptr_ && __p.__ptr_ != __i.__ptr_->__next_)
2008 __link_pointer __f = __i.__ptr_;
2009 base::__unlink_nodes(__f, __f);
2010 __link_nodes(__p.__ptr_, __f, __f);
2013 #if _LIBCPP_DEBUG_LEVEL >= 2
2014 __libcpp_db* __db = __get_db();
2015 __c_node* __cn1 = __db->__find_c_and_lock(this);
2016 __c_node* __cn2 = __db->__find_c(&__c);
2017 for (__i_node** __ip = __cn2->end_; __ip != __cn2->beg_;)
2020 iterator* __j = static_cast<iterator*>((*__ip)->__i_);
2021 if (__j->__ptr_ == __f)
2023 __cn1->__add(*__ip);
2024 (*__ip)->__c_ = __cn1;
2025 if (--__cn2->end_ != __ip)
2026 memmove(__ip, __ip+1, (__cn2->end_ - __ip)*sizeof(__i_node*));
2034 template <class _Tp, class _Alloc>
2036 list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l)
2038 #if _LIBCPP_DEBUG_LEVEL >= 2
2039 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2040 "list::splice(iterator, list, iterator, iterator) called with first iterator not"
2041 " referring to this list");
2042 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__f) == &__c,
2043 "list::splice(iterator, list, iterator, iterator) called with second iterator not"
2044 " referring to list argument");
2047 for (const_iterator __i = __f; __i != __l; ++__i)
2048 _LIBCPP_ASSERT(__i != __p,
2049 "list::splice(iterator, list, iterator, iterator)"
2050 " called with the first iterator within the range"
2051 " of the second and third iterators");
2058 size_type __s = _VSTD::distance(__f, __l);
2060 base::__sz() += __s;
2062 __link_pointer __first = __f.__ptr_;
2064 __link_pointer __last = __l.__ptr_;
2065 base::__unlink_nodes(__first, __last);
2066 __link_nodes(__p.__ptr_, __first, __last);
2067 #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 for (__link_pointer __k = __f.__ptr_;
2076 __k != __l.__ptr_; __k = __k->__next_)
2078 if (__j->__ptr_ == __k)
2080 __cn1->__add(*__ip);
2081 (*__ip)->__c_ = __cn1;
2082 if (--__cn2->end_ != __ip)
2083 memmove(__ip, __ip+1, (__cn2->end_ - __ip)*sizeof(__i_node*));
2092 template <class _Tp, class _Alloc>
2094 list<_Tp, _Alloc>::remove(const value_type& __x)
2096 list<_Tp, _Alloc> __deleted_nodes(get_allocator()); // collect the nodes we're removing
2097 for (const_iterator __i = begin(), __e = end(); __i != __e;)
2101 const_iterator __j = _VSTD::next(__i);
2102 for (; __j != __e && *__j == __x; ++__j)
2104 __deleted_nodes.splice(__deleted_nodes.end(), *this, __i, __j);
2114 template <class _Tp, class _Alloc>
2115 template <class _Pred>
2117 list<_Tp, _Alloc>::remove_if(_Pred __pred)
2119 for (iterator __i = begin(), __e = end(); __i != __e;)
2123 iterator __j = _VSTD::next(__i);
2124 for (; __j != __e && __pred(*__j); ++__j)
2126 __i = erase(__i, __j);
2135 template <class _Tp, class _Alloc>
2138 list<_Tp, _Alloc>::unique()
2140 unique(__equal_to<value_type>());
2143 template <class _Tp, class _Alloc>
2144 template <class _BinaryPred>
2146 list<_Tp, _Alloc>::unique(_BinaryPred __binary_pred)
2148 for (iterator __i = begin(), __e = end(); __i != __e;)
2150 iterator __j = _VSTD::next(__i);
2151 for (; __j != __e && __binary_pred(*__i, *__j); ++__j)
2154 __i = erase(__i, __j);
2158 template <class _Tp, class _Alloc>
2161 list<_Tp, _Alloc>::merge(list& __c)
2163 merge(__c, __less<value_type>());
2166 template <class _Tp, class _Alloc>
2167 template <class _Comp>
2169 list<_Tp, _Alloc>::merge(list& __c, _Comp __comp)
2173 iterator __f1 = begin();
2174 iterator __e1 = end();
2175 iterator __f2 = __c.begin();
2176 iterator __e2 = __c.end();
2177 while (__f1 != __e1 && __f2 != __e2)
2179 if (__comp(*__f2, *__f1))
2182 iterator __m2 = _VSTD::next(__f2);
2183 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2, ++__ds)
2185 base::__sz() += __ds;
2187 __link_pointer __f = __f2.__ptr_;
2188 __link_pointer __l = __m2.__ptr_->__prev_;
2190 base::__unlink_nodes(__f, __l);
2191 __m2 = _VSTD::next(__f1);
2192 __link_nodes(__f1.__ptr_, __f, __l);
2199 #if _LIBCPP_DEBUG_LEVEL >= 2
2200 __libcpp_db* __db = __get_db();
2201 __c_node* __cn1 = __db->__find_c_and_lock(this);
2202 __c_node* __cn2 = __db->__find_c(&__c);
2203 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
2206 iterator* __i = static_cast<iterator*>((*__p)->__i_);
2207 if (__i->__ptr_ != __c.__end_as_link())
2210 (*__p)->__c_ = __cn1;
2211 if (--__cn2->end_ != __p)
2212 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
2220 template <class _Tp, class _Alloc>
2223 list<_Tp, _Alloc>::sort()
2225 sort(__less<value_type>());
2228 template <class _Tp, class _Alloc>
2229 template <class _Comp>
2232 list<_Tp, _Alloc>::sort(_Comp __comp)
2234 __sort(begin(), end(), base::__sz(), __comp);
2237 template <class _Tp, class _Alloc>
2238 template <class _Comp>
2239 typename list<_Tp, _Alloc>::iterator
2240 list<_Tp, _Alloc>::__sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp)
2248 if (__comp(*--__e2, *__f1))
2250 __link_pointer __f = __e2.__ptr_;
2251 base::__unlink_nodes(__f, __f);
2252 __link_nodes(__f1.__ptr_, __f, __f);
2257 size_type __n2 = __n / 2;
2258 iterator __e1 = _VSTD::next(__f1, __n2);
2259 iterator __r = __f1 = __sort(__f1, __e1, __n2, __comp);
2260 iterator __f2 = __e1 = __sort(__e1, __e2, __n - __n2, __comp);
2261 if (__comp(*__f2, *__f1))
2263 iterator __m2 = _VSTD::next(__f2);
2264 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2)
2266 __link_pointer __f = __f2.__ptr_;
2267 __link_pointer __l = __m2.__ptr_->__prev_;
2270 base::__unlink_nodes(__f, __l);
2271 __m2 = _VSTD::next(__f1);
2272 __link_nodes(__f1.__ptr_, __f, __l);
2277 while (__f1 != __e1 && __f2 != __e2)
2279 if (__comp(*__f2, *__f1))
2281 iterator __m2 = _VSTD::next(__f2);
2282 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2)
2284 __link_pointer __f = __f2.__ptr_;
2285 __link_pointer __l = __m2.__ptr_->__prev_;
2289 base::__unlink_nodes(__f, __l);
2290 __m2 = _VSTD::next(__f1);
2291 __link_nodes(__f1.__ptr_, __f, __l);
2300 template <class _Tp, class _Alloc>
2302 list<_Tp, _Alloc>::reverse() _NOEXCEPT
2304 if (base::__sz() > 1)
2306 iterator __e = end();
2307 for (iterator __i = begin(); __i.__ptr_ != __e.__ptr_;)
2309 _VSTD::swap(__i.__ptr_->__prev_, __i.__ptr_->__next_);
2310 __i.__ptr_ = __i.__ptr_->__prev_;
2312 _VSTD::swap(__e.__ptr_->__prev_, __e.__ptr_->__next_);
2316 template <class _Tp, class _Alloc>
2318 list<_Tp, _Alloc>::__invariants() const
2320 return size() == _VSTD::distance(begin(), end());
2323 #if _LIBCPP_DEBUG_LEVEL >= 2
2325 template <class _Tp, class _Alloc>
2327 list<_Tp, _Alloc>::__dereferenceable(const const_iterator* __i) const
2329 return __i->__ptr_ != this->__end_as_link();
2332 template <class _Tp, class _Alloc>
2334 list<_Tp, _Alloc>::__decrementable(const const_iterator* __i) const
2336 return !empty() && __i->__ptr_ != base::__end_.__next_;
2339 template <class _Tp, class _Alloc>
2341 list<_Tp, _Alloc>::__addable(const const_iterator*, ptrdiff_t) const
2346 template <class _Tp, class _Alloc>
2348 list<_Tp, _Alloc>::__subscriptable(const const_iterator*, ptrdiff_t) const
2353 #endif // _LIBCPP_DEBUG_LEVEL >= 2
2355 template <class _Tp, class _Alloc>
2356 inline _LIBCPP_INLINE_VISIBILITY
2358 operator==(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2360 return __x.size() == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
2363 template <class _Tp, class _Alloc>
2364 inline _LIBCPP_INLINE_VISIBILITY
2366 operator< (const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2368 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
2371 template <class _Tp, class _Alloc>
2372 inline _LIBCPP_INLINE_VISIBILITY
2374 operator!=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2376 return !(__x == __y);
2379 template <class _Tp, class _Alloc>
2380 inline _LIBCPP_INLINE_VISIBILITY
2382 operator> (const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2387 template <class _Tp, class _Alloc>
2388 inline _LIBCPP_INLINE_VISIBILITY
2390 operator>=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2392 return !(__x < __y);
2395 template <class _Tp, class _Alloc>
2396 inline _LIBCPP_INLINE_VISIBILITY
2398 operator<=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2400 return !(__y < __x);
2403 template <class _Tp, class _Alloc>
2404 inline _LIBCPP_INLINE_VISIBILITY
2406 swap(list<_Tp, _Alloc>& __x, list<_Tp, _Alloc>& __y)
2407 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
2412 _LIBCPP_END_NAMESPACE_STD
2414 #endif // _LIBCPP_LIST