xref: /llvm-project-15.0.7/libcxx/include/list (revision d2baefae)
1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
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
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_LIST
11#define _LIBCPP_LIST
12
13/*
14    list synopsis
15
16namespace std
17{
18
19template <class T, class Alloc = allocator<T> >
20class list
21{
22public:
23
24    // types:
25    typedef T value_type;
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;
37
38    list()
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);
45    template <class Iter>
46        list(Iter first, Iter last);
47    template <class Iter>
48        list(Iter first, Iter last, const allocator_type& a);
49    list(const list& x);
50    list(const list&, const allocator_type& a);
51    list(list&& x)
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);
56
57    ~list();
58
59    list& operator=(const list& x);
60    list& operator=(list&& x)
61        noexcept(
62             allocator_type::propagate_on_container_move_assignment::value &&
63             is_nothrow_move_assignable<allocator_type>::value);
64    list& operator=(initializer_list<value_type>);
65    template <class Iter>
66        void assign(Iter first, Iter last);
67    void assign(size_type n, const value_type& t);
68    void assign(initializer_list<value_type>);
69
70    allocator_type get_allocator() const noexcept;
71
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;
84
85    reference front();
86    const_reference front() const;
87    reference back();
88    const_reference back() const;
89
90    bool empty() const noexcept;
91    size_type size() const noexcept;
92    size_type max_size() const noexcept;
93
94    template <class... Args>
95        reference emplace_front(Args&&... args); // reference in C++17
96    void pop_front();
97    template <class... Args>
98        reference emplace_back(Args&&... args);  // reference in C++17
99    void pop_back();
100    void push_front(const value_type& x);
101    void push_front(value_type&& x);
102    void push_back(const value_type& x);
103    void push_back(value_type&& x);
104    template <class... Args>
105        iterator emplace(const_iterator position, Args&&... args);
106    iterator insert(const_iterator position, const value_type& x);
107    iterator insert(const_iterator position, value_type&& x);
108    iterator insert(const_iterator position, size_type n, const value_type& x);
109    template <class Iter>
110        iterator insert(const_iterator position, Iter first, Iter last);
111    iterator insert(const_iterator position, initializer_list<value_type> il);
112
113    iterator erase(const_iterator position);
114    iterator erase(const_iterator position, const_iterator last);
115
116    void resize(size_type sz);
117    void resize(size_type sz, const value_type& c);
118
119    void swap(list&)
120        noexcept(allocator_traits<allocator_type>::is_always_equal::value);  // C++17
121    void clear() noexcept;
122
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);
131
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
138    void merge(list& x);
139    void merge(list&& x);
140    template <class Compare>
141        void merge(list& x, Compare comp);
142    template <class Compare>
143        void merge(list&& x, Compare comp);
144    void sort();
145    template <class Compare>
146        void sort(Compare comp);
147    void reverse() noexcept;
148};
149
150
151template <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
154
155template <class T, class Alloc>
156    bool operator==(const list<T,Alloc>& x, const list<T,Alloc>& y);
157template <class T, class Alloc>
158    bool operator< (const list<T,Alloc>& x, const list<T,Alloc>& y);
159template <class T, class Alloc>
160    bool operator!=(const list<T,Alloc>& x, const list<T,Alloc>& y);
161template <class T, class Alloc>
162    bool operator> (const list<T,Alloc>& x, const list<T,Alloc>& y);
163template <class T, class Alloc>
164    bool operator>=(const list<T,Alloc>& x, const list<T,Alloc>& y);
165template <class T, class Alloc>
166    bool operator<=(const list<T,Alloc>& x, const list<T,Alloc>& y);
167
168template <class T, class Alloc>
169    void swap(list<T,Alloc>& x, list<T,Alloc>& y)
170         noexcept(noexcept(x.swap(y)));
171
172template <class T, class Allocator, class U>
173    typename list<T, Allocator>::size_type
174    erase(list<T, Allocator>& c, const U& value);       // C++20
175template <class T, class Allocator, class Predicate>
176    typename list<T, Allocator>::size_type
177    erase_if(list<T, Allocator>& c, Predicate pred);    // C++20
178
179}  // std
180
181*/
182
183#include <__algorithm/comp.h>
184#include <__algorithm/equal.h>
185#include <__algorithm/lexicographical_compare.h>
186#include <__algorithm/min.h>
187#include <__assert>
188#include <__config>
189#include <__debug>
190#include <__utility/forward.h>
191#include <__utility/move.h>
192#include <__utility/swap.h>
193#include <initializer_list>
194#include <iterator>
195#include <limits>
196#include <memory>
197#include <type_traits>
198#include <version>
199
200#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
201#  pragma GCC system_header
202#endif
203
204_LIBCPP_PUSH_MACROS
205#include <__undef_macros>
206
207
208_LIBCPP_BEGIN_NAMESPACE_STD
209
210template <class _Tp, class _VoidPtr> struct __list_node;
211template <class _Tp, class _VoidPtr> struct __list_node_base;
212
213template <class _Tp, class _VoidPtr>
214struct __list_node_pointer_traits {
215  typedef typename __rebind_pointer<_VoidPtr, __list_node<_Tp, _VoidPtr> >::type
216        __node_pointer;
217  typedef typename __rebind_pointer<_VoidPtr, __list_node_base<_Tp, _VoidPtr> >::type
218        __base_pointer;
219
220#if defined(_LIBCPP_ABI_LIST_REMOVE_NODE_POINTER_UB)
221  typedef __base_pointer __link_pointer;
222#else
223  typedef typename conditional<
224          is_pointer<_VoidPtr>::value,
225          __base_pointer,
226          __node_pointer
227  >::type __link_pointer;
228#endif
229
230  typedef typename conditional<
231          is_same<__link_pointer, __node_pointer>::value,
232          __base_pointer,
233          __node_pointer
234  >::type __non_link_pointer;
235
236  static _LIBCPP_INLINE_VISIBILITY
237  __link_pointer __unsafe_link_pointer_cast(__link_pointer __p) {
238      return __p;
239  }
240
241  static _LIBCPP_INLINE_VISIBILITY
242  __link_pointer __unsafe_link_pointer_cast(__non_link_pointer __p) {
243      return static_cast<__link_pointer>(static_cast<_VoidPtr>(__p));
244  }
245
246};
247
248template <class _Tp, class _VoidPtr>
249struct __list_node_base
250{
251    typedef __list_node_pointer_traits<_Tp, _VoidPtr> _NodeTraits;
252    typedef typename _NodeTraits::__node_pointer __node_pointer;
253    typedef typename _NodeTraits::__base_pointer __base_pointer;
254    typedef typename _NodeTraits::__link_pointer __link_pointer;
255
256    __link_pointer __prev_;
257    __link_pointer __next_;
258
259    _LIBCPP_INLINE_VISIBILITY
260    __list_node_base() : __prev_(_NodeTraits::__unsafe_link_pointer_cast(__self())),
261                         __next_(_NodeTraits::__unsafe_link_pointer_cast(__self())) {}
262
263    _LIBCPP_INLINE_VISIBILITY
264    __base_pointer __self() {
265        return pointer_traits<__base_pointer>::pointer_to(*this);
266    }
267
268    _LIBCPP_INLINE_VISIBILITY
269    __node_pointer __as_node() {
270        return static_cast<__node_pointer>(__self());
271    }
272};
273
274template <class _Tp, class _VoidPtr>
275struct _LIBCPP_STANDALONE_DEBUG __list_node
276    : public __list_node_base<_Tp, _VoidPtr>
277{
278    _Tp __value_;
279
280    typedef __list_node_base<_Tp, _VoidPtr> __base;
281    typedef typename __base::__link_pointer __link_pointer;
282
283    _LIBCPP_INLINE_VISIBILITY
284    __link_pointer __as_link() {
285        return static_cast<__link_pointer>(__base::__self());
286    }
287};
288
289template <class _Tp, class _Alloc = allocator<_Tp> > class _LIBCPP_TEMPLATE_VIS list;
290template <class _Tp, class _Alloc> class __list_imp;
291template <class _Tp, class _VoidPtr> class _LIBCPP_TEMPLATE_VIS __list_const_iterator;
292
293template <class _Tp, class _VoidPtr>
294class _LIBCPP_TEMPLATE_VIS __list_iterator
295{
296    typedef __list_node_pointer_traits<_Tp, _VoidPtr> _NodeTraits;
297    typedef typename _NodeTraits::__link_pointer __link_pointer;
298
299    __link_pointer __ptr_;
300
301#if _LIBCPP_DEBUG_LEVEL == 2
302    _LIBCPP_INLINE_VISIBILITY
303    explicit __list_iterator(__link_pointer __p, const void* __c) _NOEXCEPT
304        : __ptr_(__p)
305    {
306        __get_db()->__insert_ic(this, __c);
307    }
308#else
309    _LIBCPP_INLINE_VISIBILITY
310    explicit __list_iterator(__link_pointer __p) _NOEXCEPT : __ptr_(__p) {}
311#endif
312
313
314
315    template<class, class> friend class list;
316    template<class, class> friend class __list_imp;
317    template<class, class> friend class __list_const_iterator;
318public:
319    typedef bidirectional_iterator_tag       iterator_category;
320    typedef _Tp                              value_type;
321    typedef value_type&                      reference;
322    typedef typename __rebind_pointer<_VoidPtr, value_type>::type pointer;
323    typedef typename pointer_traits<pointer>::difference_type difference_type;
324
325    _LIBCPP_INLINE_VISIBILITY
326    __list_iterator() _NOEXCEPT : __ptr_(nullptr)
327    {
328        _VSTD::__debug_db_insert_i(this);
329    }
330
331#if _LIBCPP_DEBUG_LEVEL == 2
332
333    _LIBCPP_INLINE_VISIBILITY
334    __list_iterator(const __list_iterator& __p)
335        : __ptr_(__p.__ptr_)
336    {
337        __get_db()->__iterator_copy(this, _VSTD::addressof(__p));
338    }
339
340    _LIBCPP_INLINE_VISIBILITY
341    ~__list_iterator()
342    {
343        __get_db()->__erase_i(this);
344    }
345
346    _LIBCPP_INLINE_VISIBILITY
347    __list_iterator& operator=(const __list_iterator& __p)
348    {
349        if (this != _VSTD::addressof(__p))
350        {
351            __get_db()->__iterator_copy(this, _VSTD::addressof(__p));
352            __ptr_ = __p.__ptr_;
353        }
354        return *this;
355    }
356
357#endif // _LIBCPP_DEBUG_LEVEL == 2
358
359    _LIBCPP_INLINE_VISIBILITY
360    reference operator*() const
361    {
362        _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
363                             "Attempted to dereference a non-dereferenceable list::iterator");
364        return __ptr_->__as_node()->__value_;
365    }
366    _LIBCPP_INLINE_VISIBILITY
367    pointer operator->() const
368    {
369        _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
370                             "Attempted to dereference a non-dereferenceable list::iterator");
371        return pointer_traits<pointer>::pointer_to(__ptr_->__as_node()->__value_);
372    }
373
374    _LIBCPP_INLINE_VISIBILITY
375    __list_iterator& operator++()
376    {
377        _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
378                             "Attempted to increment a non-incrementable list::iterator");
379        __ptr_ = __ptr_->__next_;
380        return *this;
381    }
382    _LIBCPP_INLINE_VISIBILITY
383    __list_iterator operator++(int) {__list_iterator __t(*this); ++(*this); return __t;}
384
385    _LIBCPP_INLINE_VISIBILITY
386    __list_iterator& operator--()
387    {
388        _LIBCPP_DEBUG_ASSERT(__get_const_db()->__decrementable(this),
389                             "Attempted to decrement a non-decrementable list::iterator");
390        __ptr_ = __ptr_->__prev_;
391        return *this;
392    }
393    _LIBCPP_INLINE_VISIBILITY
394    __list_iterator operator--(int) {__list_iterator __t(*this); --(*this); return __t;}
395
396    friend _LIBCPP_INLINE_VISIBILITY
397    bool operator==(const __list_iterator& __x, const __list_iterator& __y)
398    {
399        return __x.__ptr_ == __y.__ptr_;
400    }
401    friend _LIBCPP_INLINE_VISIBILITY
402     bool operator!=(const __list_iterator& __x, const __list_iterator& __y)
403        {return !(__x == __y);}
404};
405
406template <class _Tp, class _VoidPtr>
407class _LIBCPP_TEMPLATE_VIS __list_const_iterator
408{
409    typedef __list_node_pointer_traits<_Tp, _VoidPtr> _NodeTraits;
410    typedef typename _NodeTraits::__link_pointer __link_pointer;
411
412    __link_pointer __ptr_;
413
414#if _LIBCPP_DEBUG_LEVEL == 2
415    _LIBCPP_INLINE_VISIBILITY
416    explicit __list_const_iterator(__link_pointer __p, const void* __c) _NOEXCEPT
417        : __ptr_(__p)
418    {
419        __get_db()->__insert_ic(this, __c);
420    }
421#else
422    _LIBCPP_INLINE_VISIBILITY
423    explicit __list_const_iterator(__link_pointer __p) _NOEXCEPT : __ptr_(__p) {}
424#endif
425
426    template<class, class> friend class list;
427    template<class, class> friend class __list_imp;
428public:
429    typedef bidirectional_iterator_tag       iterator_category;
430    typedef _Tp                              value_type;
431    typedef const value_type&                reference;
432    typedef typename __rebind_pointer<_VoidPtr, const value_type>::type pointer;
433    typedef typename pointer_traits<pointer>::difference_type difference_type;
434
435    _LIBCPP_INLINE_VISIBILITY
436    __list_const_iterator() _NOEXCEPT : __ptr_(nullptr)
437    {
438        _VSTD::__debug_db_insert_i(this);
439    }
440    _LIBCPP_INLINE_VISIBILITY
441    __list_const_iterator(const __list_iterator<_Tp, _VoidPtr>& __p) _NOEXCEPT
442        : __ptr_(__p.__ptr_)
443    {
444#if _LIBCPP_DEBUG_LEVEL == 2
445        __get_db()->__iterator_copy(this, _VSTD::addressof(__p));
446#endif
447    }
448
449#if _LIBCPP_DEBUG_LEVEL == 2
450
451    _LIBCPP_INLINE_VISIBILITY
452    __list_const_iterator(const __list_const_iterator& __p)
453        : __ptr_(__p.__ptr_)
454    {
455        __get_db()->__iterator_copy(this, _VSTD::addressof(__p));
456    }
457
458    _LIBCPP_INLINE_VISIBILITY
459    ~__list_const_iterator()
460    {
461        __get_db()->__erase_i(this);
462    }
463
464    _LIBCPP_INLINE_VISIBILITY
465    __list_const_iterator& operator=(const __list_const_iterator& __p)
466    {
467        if (this != _VSTD::addressof(__p))
468        {
469            __get_db()->__iterator_copy(this, _VSTD::addressof(__p));
470            __ptr_ = __p.__ptr_;
471        }
472        return *this;
473    }
474
475#endif // _LIBCPP_DEBUG_LEVEL == 2
476    _LIBCPP_INLINE_VISIBILITY
477    reference operator*() const
478    {
479        _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
480                             "Attempted to dereference a non-dereferenceable list::const_iterator");
481        return __ptr_->__as_node()->__value_;
482    }
483    _LIBCPP_INLINE_VISIBILITY
484    pointer operator->() const
485    {
486        _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
487                             "Attempted to dereference a non-dereferenceable list::const_iterator");
488        return pointer_traits<pointer>::pointer_to(__ptr_->__as_node()->__value_);
489    }
490
491    _LIBCPP_INLINE_VISIBILITY
492    __list_const_iterator& operator++()
493    {
494        _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
495                             "Attempted to increment a non-incrementable list::const_iterator");
496        __ptr_ = __ptr_->__next_;
497        return *this;
498    }
499    _LIBCPP_INLINE_VISIBILITY
500    __list_const_iterator operator++(int) {__list_const_iterator __t(*this); ++(*this); return __t;}
501
502    _LIBCPP_INLINE_VISIBILITY
503    __list_const_iterator& operator--()
504    {
505        _LIBCPP_DEBUG_ASSERT(__get_const_db()->__decrementable(this),
506                             "Attempted to decrement a non-decrementable list::const_iterator");
507        __ptr_ = __ptr_->__prev_;
508        return *this;
509    }
510    _LIBCPP_INLINE_VISIBILITY
511    __list_const_iterator operator--(int) {__list_const_iterator __t(*this); --(*this); return __t;}
512
513    friend _LIBCPP_INLINE_VISIBILITY
514    bool operator==(const __list_const_iterator& __x, const __list_const_iterator& __y)
515    {
516        return __x.__ptr_ == __y.__ptr_;
517    }
518    friend _LIBCPP_INLINE_VISIBILITY
519    bool operator!=(const __list_const_iterator& __x, const __list_const_iterator& __y)
520        {return !(__x == __y);}
521};
522
523template <class _Tp, class _Alloc>
524class __list_imp
525{
526    __list_imp(const __list_imp&);
527    __list_imp& operator=(const __list_imp&);
528public:
529    typedef _Alloc                                                  allocator_type;
530    typedef allocator_traits<allocator_type>                        __alloc_traits;
531    typedef typename __alloc_traits::size_type                      size_type;
532protected:
533    typedef _Tp                                                     value_type;
534    typedef typename __alloc_traits::void_pointer                   __void_pointer;
535    typedef __list_iterator<value_type, __void_pointer>             iterator;
536    typedef __list_const_iterator<value_type, __void_pointer>       const_iterator;
537    typedef __list_node_base<value_type, __void_pointer>            __node_base;
538    typedef __list_node<value_type, __void_pointer>                 __node;
539    typedef typename __rebind_alloc_helper<__alloc_traits, __node>::type __node_allocator;
540    typedef allocator_traits<__node_allocator>                       __node_alloc_traits;
541    typedef typename __node_alloc_traits::pointer                    __node_pointer;
542    typedef typename __node_alloc_traits::pointer                    __node_const_pointer;
543    typedef __list_node_pointer_traits<value_type, __void_pointer> __node_pointer_traits;
544    typedef typename __node_pointer_traits::__link_pointer __link_pointer;
545    typedef __link_pointer __link_const_pointer;
546    typedef typename __alloc_traits::pointer                         pointer;
547    typedef typename __alloc_traits::const_pointer                   const_pointer;
548    typedef typename __alloc_traits::difference_type                 difference_type;
549
550    typedef typename __rebind_alloc_helper<__alloc_traits, __node_base>::type __node_base_allocator;
551    typedef typename allocator_traits<__node_base_allocator>::pointer __node_base_pointer;
552    static_assert((!is_same<allocator_type, __node_allocator>::value),
553                  "internal allocator type must differ from user-specified "
554                  "type; otherwise overload resolution breaks");
555
556    __node_base __end_;
557    __compressed_pair<size_type, __node_allocator> __size_alloc_;
558
559    _LIBCPP_INLINE_VISIBILITY
560    __link_pointer __end_as_link() const _NOEXCEPT {
561        return __node_pointer_traits::__unsafe_link_pointer_cast(
562                const_cast<__node_base&>(__end_).__self());
563    }
564
565    _LIBCPP_INLINE_VISIBILITY
566          size_type& __sz() _NOEXCEPT {return __size_alloc_.first();}
567    _LIBCPP_INLINE_VISIBILITY
568    const size_type& __sz() const _NOEXCEPT
569        {return __size_alloc_.first();}
570    _LIBCPP_INLINE_VISIBILITY
571          __node_allocator& __node_alloc() _NOEXCEPT
572          {return __size_alloc_.second();}
573    _LIBCPP_INLINE_VISIBILITY
574    const __node_allocator& __node_alloc() const _NOEXCEPT
575        {return __size_alloc_.second();}
576
577    _LIBCPP_INLINE_VISIBILITY
578    size_type __node_alloc_max_size() const _NOEXCEPT {
579        return __node_alloc_traits::max_size(__node_alloc());
580    }
581    _LIBCPP_INLINE_VISIBILITY
582    static void __unlink_nodes(__link_pointer __f, __link_pointer __l) _NOEXCEPT;
583
584    _LIBCPP_INLINE_VISIBILITY
585    __list_imp()
586        _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value);
587    _LIBCPP_INLINE_VISIBILITY
588    __list_imp(const allocator_type& __a);
589    _LIBCPP_INLINE_VISIBILITY
590    __list_imp(const __node_allocator& __a);
591#ifndef _LIBCPP_CXX03_LANG
592    __list_imp(__node_allocator&& __a) _NOEXCEPT;
593#endif
594    ~__list_imp();
595    void clear() _NOEXCEPT;
596    _LIBCPP_INLINE_VISIBILITY
597    bool empty() const _NOEXCEPT {return __sz() == 0;}
598
599    _LIBCPP_INLINE_VISIBILITY
600    iterator begin() _NOEXCEPT
601    {
602#if _LIBCPP_DEBUG_LEVEL == 2
603        return iterator(__end_.__next_, this);
604#else
605        return iterator(__end_.__next_);
606#endif
607    }
608    _LIBCPP_INLINE_VISIBILITY
609    const_iterator begin() const  _NOEXCEPT
610    {
611#if _LIBCPP_DEBUG_LEVEL == 2
612        return const_iterator(__end_.__next_, this);
613#else
614        return const_iterator(__end_.__next_);
615#endif
616    }
617    _LIBCPP_INLINE_VISIBILITY
618    iterator end() _NOEXCEPT
619    {
620#if _LIBCPP_DEBUG_LEVEL == 2
621        return iterator(__end_as_link(), this);
622#else
623        return iterator(__end_as_link());
624#endif
625    }
626    _LIBCPP_INLINE_VISIBILITY
627    const_iterator end() const _NOEXCEPT
628    {
629#if _LIBCPP_DEBUG_LEVEL == 2
630        return const_iterator(__end_as_link(), this);
631#else
632        return const_iterator(__end_as_link());
633#endif
634    }
635
636    void swap(__list_imp& __c)
637#if _LIBCPP_STD_VER >= 14
638        _NOEXCEPT;
639#else
640        _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
641                    __is_nothrow_swappable<allocator_type>::value);
642#endif
643
644    _LIBCPP_INLINE_VISIBILITY
645    void __copy_assign_alloc(const __list_imp& __c)
646        {__copy_assign_alloc(__c, integral_constant<bool,
647                      __node_alloc_traits::propagate_on_container_copy_assignment::value>());}
648
649    _LIBCPP_INLINE_VISIBILITY
650    void __move_assign_alloc(__list_imp& __c)
651        _NOEXCEPT_(
652            !__node_alloc_traits::propagate_on_container_move_assignment::value ||
653            is_nothrow_move_assignable<__node_allocator>::value)
654        {__move_assign_alloc(__c, integral_constant<bool,
655                      __node_alloc_traits::propagate_on_container_move_assignment::value>());}
656
657private:
658    _LIBCPP_INLINE_VISIBILITY
659    void __copy_assign_alloc(const __list_imp& __c, true_type)
660        {
661            if (__node_alloc() != __c.__node_alloc())
662                clear();
663            __node_alloc() = __c.__node_alloc();
664        }
665
666    _LIBCPP_INLINE_VISIBILITY
667    void __copy_assign_alloc(const __list_imp&, false_type)
668        {}
669
670    _LIBCPP_INLINE_VISIBILITY
671    void __move_assign_alloc(__list_imp& __c, true_type)
672        _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
673        {
674            __node_alloc() = _VSTD::move(__c.__node_alloc());
675        }
676
677    _LIBCPP_INLINE_VISIBILITY
678    void __move_assign_alloc(__list_imp&, false_type)
679        _NOEXCEPT
680        {}
681
682    _LIBCPP_INLINE_VISIBILITY
683    void __invalidate_all_iterators() {
684#if _LIBCPP_DEBUG_LEVEL == 2
685      __get_db()->__invalidate_all(this);
686#endif
687    }
688};
689
690// Unlink nodes [__f, __l]
691template <class _Tp, class _Alloc>
692inline
693void
694__list_imp<_Tp, _Alloc>::__unlink_nodes(__link_pointer __f, __link_pointer __l)
695    _NOEXCEPT
696{
697    __f->__prev_->__next_ = __l->__next_;
698    __l->__next_->__prev_ = __f->__prev_;
699}
700
701template <class _Tp, class _Alloc>
702inline
703__list_imp<_Tp, _Alloc>::__list_imp()
704        _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value)
705    : __size_alloc_(0, __default_init_tag())
706{
707}
708
709template <class _Tp, class _Alloc>
710inline
711__list_imp<_Tp, _Alloc>::__list_imp(const allocator_type& __a)
712    : __size_alloc_(0, __node_allocator(__a))
713{
714}
715
716template <class _Tp, class _Alloc>
717inline __list_imp<_Tp, _Alloc>::__list_imp(const __node_allocator& __a)
718    : __size_alloc_(0, __a) {}
719
720#ifndef _LIBCPP_CXX03_LANG
721template <class _Tp, class _Alloc>
722inline __list_imp<_Tp, _Alloc>::__list_imp(__node_allocator&& __a) _NOEXCEPT
723    : __size_alloc_(0, _VSTD::move(__a)) {}
724#endif
725
726template <class _Tp, class _Alloc>
727__list_imp<_Tp, _Alloc>::~__list_imp() {
728  clear();
729#if _LIBCPP_DEBUG_LEVEL == 2
730    __get_db()->__erase_c(this);
731#endif
732}
733
734template <class _Tp, class _Alloc>
735void
736__list_imp<_Tp, _Alloc>::clear() _NOEXCEPT
737{
738    if (!empty())
739    {
740        __node_allocator& __na = __node_alloc();
741        __link_pointer __f = __end_.__next_;
742        __link_pointer __l = __end_as_link();
743        __unlink_nodes(__f, __l->__prev_);
744        __sz() = 0;
745        while (__f != __l)
746        {
747            __node_pointer __np = __f->__as_node();
748            __f = __f->__next_;
749            __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
750            __node_alloc_traits::deallocate(__na, __np, 1);
751        }
752        __invalidate_all_iterators();
753    }
754}
755
756template <class _Tp, class _Alloc>
757void
758__list_imp<_Tp, _Alloc>::swap(__list_imp& __c)
759#if _LIBCPP_STD_VER >= 14
760        _NOEXCEPT
761#else
762        _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
763                    __is_nothrow_swappable<allocator_type>::value)
764#endif
765{
766    _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
767                   this->__node_alloc() == __c.__node_alloc(),
768                   "list::swap: Either propagate_on_container_swap must be true"
769                   " or the allocators must compare equal");
770    using _VSTD::swap;
771    _VSTD::__swap_allocator(__node_alloc(), __c.__node_alloc());
772    swap(__sz(), __c.__sz());
773    swap(__end_, __c.__end_);
774    if (__sz() == 0)
775        __end_.__next_ = __end_.__prev_ = __end_as_link();
776    else
777        __end_.__prev_->__next_ = __end_.__next_->__prev_ = __end_as_link();
778    if (__c.__sz() == 0)
779        __c.__end_.__next_ = __c.__end_.__prev_ = __c.__end_as_link();
780    else
781        __c.__end_.__prev_->__next_ = __c.__end_.__next_->__prev_ = __c.__end_as_link();
782
783#if _LIBCPP_DEBUG_LEVEL == 2
784    __libcpp_db* __db = __get_db();
785    __c_node* __cn1 = __db->__find_c_and_lock(this);
786    __c_node* __cn2 = __db->__find_c(_VSTD::addressof(__c));
787    _VSTD::swap(__cn1->beg_, __cn2->beg_);
788    _VSTD::swap(__cn1->end_, __cn2->end_);
789    _VSTD::swap(__cn1->cap_, __cn2->cap_);
790    for (__i_node** __p = __cn1->end_; __p != __cn1->beg_;)
791    {
792        --__p;
793        const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
794        if (__i->__ptr_ == __c.__end_as_link())
795        {
796            __cn2->__add(*__p);
797            if (--__cn1->end_ != __p)
798                _VSTD::memmove(__p, __p+1, (__cn1->end_ - __p)*sizeof(__i_node*));
799        }
800        else
801            (*__p)->__c_ = __cn1;
802    }
803    for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
804    {
805        --__p;
806        const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
807        if (__i->__ptr_ == __end_as_link())
808        {
809            __cn1->__add(*__p);
810            if (--__cn2->end_ != __p)
811                _VSTD::memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
812        }
813        else
814            (*__p)->__c_ = __cn2;
815    }
816    __db->unlock();
817#endif
818}
819
820template <class _Tp, class _Alloc /*= allocator<_Tp>*/>
821class _LIBCPP_TEMPLATE_VIS list
822    : private __list_imp<_Tp, _Alloc>
823{
824    typedef __list_imp<_Tp, _Alloc> base;
825    typedef typename base::__node              __node;
826    typedef typename base::__node_allocator    __node_allocator;
827    typedef typename base::__node_pointer      __node_pointer;
828    typedef typename base::__node_alloc_traits __node_alloc_traits;
829    typedef typename base::__node_base         __node_base;
830    typedef typename base::__node_base_pointer __node_base_pointer;
831    typedef typename base::__link_pointer __link_pointer;
832
833public:
834    typedef _Tp                                            value_type;
835    typedef _Alloc                                         allocator_type;
836    static_assert((is_same<value_type, typename allocator_type::value_type>::value),
837                  "Invalid allocator::value_type");
838    typedef value_type&                                    reference;
839    typedef const value_type&                              const_reference;
840    typedef typename base::pointer                         pointer;
841    typedef typename base::const_pointer                   const_pointer;
842    typedef typename base::size_type                       size_type;
843    typedef typename base::difference_type                 difference_type;
844    typedef typename base::iterator                        iterator;
845    typedef typename base::const_iterator                  const_iterator;
846    typedef _VSTD::reverse_iterator<iterator>              reverse_iterator;
847    typedef _VSTD::reverse_iterator<const_iterator>        const_reverse_iterator;
848#if _LIBCPP_STD_VER > 17
849    typedef size_type                                      __remove_return_type;
850#else
851    typedef void                                           __remove_return_type;
852#endif
853
854    _LIBCPP_INLINE_VISIBILITY
855    list()
856        _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value)
857    {
858        _VSTD::__debug_db_insert_c(this);
859    }
860    _LIBCPP_INLINE_VISIBILITY
861    explicit list(const allocator_type& __a) : base(__a)
862    {
863        _VSTD::__debug_db_insert_c(this);
864    }
865    explicit list(size_type __n);
866#if _LIBCPP_STD_VER > 11
867    explicit list(size_type __n, const allocator_type& __a);
868#endif
869    list(size_type __n, const value_type& __x);
870    template <class = __enable_if_t<__is_allocator<_Alloc>::value> >
871    list(size_type __n, const value_type& __x, const allocator_type& __a) : base(__a)
872    {
873        _VSTD::__debug_db_insert_c(this);
874        for (; __n > 0; --__n)
875            push_back(__x);
876    }
877
878    template <class _InpIter>
879        list(_InpIter __f, _InpIter __l,
880             typename enable_if<__is_cpp17_input_iterator<_InpIter>::value>::type* = 0);
881    template <class _InpIter>
882        list(_InpIter __f, _InpIter __l, const allocator_type& __a,
883             typename enable_if<__is_cpp17_input_iterator<_InpIter>::value>::type* = 0);
884
885    list(const list& __c);
886    list(const list& __c, const __identity_t<allocator_type>& __a);
887    _LIBCPP_INLINE_VISIBILITY
888    list& operator=(const list& __c);
889#ifndef _LIBCPP_CXX03_LANG
890    list(initializer_list<value_type> __il);
891    list(initializer_list<value_type> __il, const allocator_type& __a);
892
893    _LIBCPP_INLINE_VISIBILITY
894    list(list&& __c)
895        _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value);
896    _LIBCPP_INLINE_VISIBILITY
897    list(list&& __c, const __identity_t<allocator_type>& __a);
898    _LIBCPP_INLINE_VISIBILITY
899    list& operator=(list&& __c)
900        _NOEXCEPT_(
901            __node_alloc_traits::propagate_on_container_move_assignment::value &&
902            is_nothrow_move_assignable<__node_allocator>::value);
903
904    _LIBCPP_INLINE_VISIBILITY
905    list& operator=(initializer_list<value_type> __il)
906        {assign(__il.begin(), __il.end()); return *this;}
907
908    _LIBCPP_INLINE_VISIBILITY
909    void assign(initializer_list<value_type> __il)
910        {assign(__il.begin(), __il.end());}
911#endif // _LIBCPP_CXX03_LANG
912
913    template <class _InpIter>
914        void assign(_InpIter __f, _InpIter __l,
915             typename enable_if<__is_cpp17_input_iterator<_InpIter>::value>::type* = 0);
916    void assign(size_type __n, const value_type& __x);
917
918    _LIBCPP_INLINE_VISIBILITY
919    allocator_type get_allocator() const _NOEXCEPT;
920
921    _LIBCPP_INLINE_VISIBILITY
922    size_type size() const _NOEXCEPT     {return base::__sz();}
923    _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
924    bool empty() const _NOEXCEPT         {return base::empty();}
925    _LIBCPP_INLINE_VISIBILITY
926    size_type max_size() const _NOEXCEPT
927        {
928            return _VSTD::min<size_type>(
929                base::__node_alloc_max_size(),
930                numeric_limits<difference_type >::max());
931        }
932
933    _LIBCPP_INLINE_VISIBILITY
934          iterator begin() _NOEXCEPT        {return base::begin();}
935    _LIBCPP_INLINE_VISIBILITY
936    const_iterator begin()  const _NOEXCEPT {return base::begin();}
937    _LIBCPP_INLINE_VISIBILITY
938          iterator end() _NOEXCEPT          {return base::end();}
939    _LIBCPP_INLINE_VISIBILITY
940    const_iterator end()    const _NOEXCEPT {return base::end();}
941    _LIBCPP_INLINE_VISIBILITY
942    const_iterator cbegin() const _NOEXCEPT {return base::begin();}
943    _LIBCPP_INLINE_VISIBILITY
944    const_iterator cend()   const _NOEXCEPT {return base::end();}
945
946    _LIBCPP_INLINE_VISIBILITY
947          reverse_iterator rbegin() _NOEXCEPT
948            {return       reverse_iterator(end());}
949    _LIBCPP_INLINE_VISIBILITY
950    const_reverse_iterator rbegin()  const _NOEXCEPT
951        {return const_reverse_iterator(end());}
952    _LIBCPP_INLINE_VISIBILITY
953          reverse_iterator rend() _NOEXCEPT
954            {return       reverse_iterator(begin());}
955    _LIBCPP_INLINE_VISIBILITY
956    const_reverse_iterator rend()    const _NOEXCEPT
957        {return const_reverse_iterator(begin());}
958    _LIBCPP_INLINE_VISIBILITY
959    const_reverse_iterator crbegin() const _NOEXCEPT
960        {return const_reverse_iterator(end());}
961    _LIBCPP_INLINE_VISIBILITY
962    const_reverse_iterator crend()   const _NOEXCEPT
963        {return const_reverse_iterator(begin());}
964
965    _LIBCPP_INLINE_VISIBILITY
966    reference front()
967    {
968        _LIBCPP_ASSERT(!empty(), "list::front called on empty list");
969        return base::__end_.__next_->__as_node()->__value_;
970    }
971    _LIBCPP_INLINE_VISIBILITY
972    const_reference front() const
973    {
974        _LIBCPP_ASSERT(!empty(), "list::front called on empty list");
975        return base::__end_.__next_->__as_node()->__value_;
976    }
977    _LIBCPP_INLINE_VISIBILITY
978    reference back()
979    {
980        _LIBCPP_ASSERT(!empty(), "list::back called on empty list");
981        return base::__end_.__prev_->__as_node()->__value_;
982    }
983    _LIBCPP_INLINE_VISIBILITY
984    const_reference back() const
985    {
986        _LIBCPP_ASSERT(!empty(), "list::back called on empty list");
987        return base::__end_.__prev_->__as_node()->__value_;
988    }
989
990#ifndef _LIBCPP_CXX03_LANG
991    void push_front(value_type&& __x);
992    void push_back(value_type&& __x);
993
994    template <class... _Args>
995#if _LIBCPP_STD_VER > 14
996       reference emplace_front(_Args&&... __args);
997#else
998       void      emplace_front(_Args&&... __args);
999#endif
1000    template <class... _Args>
1001#if _LIBCPP_STD_VER > 14
1002        reference emplace_back(_Args&&... __args);
1003#else
1004       void       emplace_back(_Args&&... __args);
1005#endif
1006    template <class... _Args>
1007        iterator emplace(const_iterator __p, _Args&&... __args);
1008
1009    iterator insert(const_iterator __p, value_type&& __x);
1010
1011    _LIBCPP_INLINE_VISIBILITY
1012    iterator insert(const_iterator __p, initializer_list<value_type> __il)
1013        {return insert(__p, __il.begin(), __il.end());}
1014#endif // _LIBCPP_CXX03_LANG
1015
1016    void push_front(const value_type& __x);
1017    void push_back(const value_type& __x);
1018
1019#ifndef _LIBCPP_CXX03_LANG
1020    template <class _Arg>
1021    _LIBCPP_INLINE_VISIBILITY
1022    void __emplace_back(_Arg&& __arg) { emplace_back(_VSTD::forward<_Arg>(__arg)); }
1023#else
1024    _LIBCPP_INLINE_VISIBILITY
1025    void __emplace_back(value_type const& __arg) { push_back(__arg); }
1026#endif
1027
1028    iterator insert(const_iterator __p, const value_type& __x);
1029    iterator insert(const_iterator __p, size_type __n, const value_type& __x);
1030    template <class _InpIter>
1031        iterator insert(const_iterator __p, _InpIter __f, _InpIter __l,
1032             typename enable_if<__is_cpp17_input_iterator<_InpIter>::value>::type* = 0);
1033
1034    _LIBCPP_INLINE_VISIBILITY
1035    void swap(list& __c)
1036#if _LIBCPP_STD_VER >= 14
1037        _NOEXCEPT
1038#else
1039        _NOEXCEPT_(!__node_alloc_traits::propagate_on_container_swap::value ||
1040                   __is_nothrow_swappable<__node_allocator>::value)
1041#endif
1042        {base::swap(__c);}
1043    _LIBCPP_INLINE_VISIBILITY
1044    void clear() _NOEXCEPT {base::clear();}
1045
1046    void pop_front();
1047    void pop_back();
1048
1049    iterator erase(const_iterator __p);
1050    iterator erase(const_iterator __f, const_iterator __l);
1051
1052    void resize(size_type __n);
1053    void resize(size_type __n, const value_type& __x);
1054
1055    void splice(const_iterator __p, list& __c);
1056#ifndef _LIBCPP_CXX03_LANG
1057    _LIBCPP_INLINE_VISIBILITY
1058    void splice(const_iterator __p, list&& __c) {splice(__p, __c);}
1059    _LIBCPP_INLINE_VISIBILITY
1060    void splice(const_iterator __p, list&& __c, const_iterator __i)
1061        {splice(__p, __c, __i);}
1062    _LIBCPP_INLINE_VISIBILITY
1063    void splice(const_iterator __p, list&& __c, const_iterator __f, const_iterator __l)
1064        {splice(__p, __c, __f, __l);}
1065#endif
1066    void splice(const_iterator __p, list& __c, const_iterator __i);
1067    void splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l);
1068
1069    __remove_return_type remove(const value_type& __x);
1070    template <class _Pred> __remove_return_type remove_if(_Pred __pred);
1071    _LIBCPP_INLINE_VISIBILITY
1072    __remove_return_type unique() { return unique(__equal_to<value_type>()); }
1073    template <class _BinaryPred>
1074        __remove_return_type unique(_BinaryPred __binary_pred);
1075    _LIBCPP_INLINE_VISIBILITY
1076    void merge(list& __c);
1077#ifndef _LIBCPP_CXX03_LANG
1078    _LIBCPP_INLINE_VISIBILITY
1079    void merge(list&& __c) {merge(__c);}
1080
1081    template <class _Comp>
1082    _LIBCPP_INLINE_VISIBILITY
1083        void merge(list&& __c, _Comp __comp) {merge(__c, __comp);}
1084#endif
1085    template <class _Comp>
1086        void merge(list& __c, _Comp __comp);
1087
1088    _LIBCPP_INLINE_VISIBILITY
1089    void sort();
1090    template <class _Comp>
1091        _LIBCPP_INLINE_VISIBILITY
1092        void sort(_Comp __comp);
1093
1094    void reverse() _NOEXCEPT;
1095
1096    bool __invariants() const;
1097
1098    typedef __allocator_destructor<__node_allocator> __node_destructor;
1099    typedef unique_ptr<__node, __node_destructor> __hold_pointer;
1100
1101    _LIBCPP_INLINE_VISIBILITY
1102    __hold_pointer __allocate_node(__node_allocator& __na) {
1103      __node_pointer __p = __node_alloc_traits::allocate(__na, 1);
1104      __p->__prev_ = nullptr;
1105      return __hold_pointer(__p, __node_destructor(__na, 1));
1106    }
1107
1108#if _LIBCPP_DEBUG_LEVEL == 2
1109
1110    bool __dereferenceable(const const_iterator* __i) const;
1111    bool __decrementable(const const_iterator* __i) const;
1112    bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1113    bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1114
1115#endif // _LIBCPP_DEBUG_LEVEL == 2
1116
1117private:
1118    _LIBCPP_INLINE_VISIBILITY
1119    static void __link_nodes  (__link_pointer __p, __link_pointer __f, __link_pointer __l);
1120    _LIBCPP_INLINE_VISIBILITY
1121    void __link_nodes_at_front(__link_pointer __f, __link_pointer __l);
1122    _LIBCPP_INLINE_VISIBILITY
1123    void __link_nodes_at_back (__link_pointer __f, __link_pointer __l);
1124    iterator __iterator(size_type __n);
1125    template <class _Comp>
1126        static iterator __sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp);
1127
1128    void __move_assign(list& __c, true_type)
1129        _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value);
1130    void __move_assign(list& __c, false_type);
1131};
1132
1133#if _LIBCPP_STD_VER >= 17
1134template<class _InputIterator,
1135         class _Alloc = allocator<__iter_value_type<_InputIterator>>,
1136         class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
1137         class = enable_if_t<__is_allocator<_Alloc>::value>
1138         >
1139list(_InputIterator, _InputIterator)
1140  -> list<__iter_value_type<_InputIterator>, _Alloc>;
1141
1142template<class _InputIterator,
1143         class _Alloc,
1144         class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
1145         class = enable_if_t<__is_allocator<_Alloc>::value>
1146         >
1147list(_InputIterator, _InputIterator, _Alloc)
1148  -> list<__iter_value_type<_InputIterator>, _Alloc>;
1149#endif
1150
1151// Link in nodes [__f, __l] just prior to __p
1152template <class _Tp, class _Alloc>
1153inline
1154void
1155list<_Tp, _Alloc>::__link_nodes(__link_pointer __p, __link_pointer __f, __link_pointer __l)
1156{
1157    __p->__prev_->__next_ = __f;
1158    __f->__prev_ = __p->__prev_;
1159    __p->__prev_ = __l;
1160    __l->__next_ = __p;
1161}
1162
1163// Link in nodes [__f, __l] at the front of the list
1164template <class _Tp, class _Alloc>
1165inline
1166void
1167list<_Tp, _Alloc>::__link_nodes_at_front(__link_pointer __f, __link_pointer __l)
1168{
1169    __f->__prev_ = base::__end_as_link();
1170    __l->__next_ = base::__end_.__next_;
1171    __l->__next_->__prev_ = __l;
1172    base::__end_.__next_ = __f;
1173}
1174
1175// Link in nodes [__f, __l] at the back of the list
1176template <class _Tp, class _Alloc>
1177inline
1178void
1179list<_Tp, _Alloc>::__link_nodes_at_back(__link_pointer __f, __link_pointer __l)
1180{
1181    __l->__next_ = base::__end_as_link();
1182    __f->__prev_ = base::__end_.__prev_;
1183    __f->__prev_->__next_ = __f;
1184    base::__end_.__prev_ = __l;
1185}
1186
1187
1188template <class _Tp, class _Alloc>
1189inline
1190typename list<_Tp, _Alloc>::iterator
1191list<_Tp, _Alloc>::__iterator(size_type __n)
1192{
1193    return __n <= base::__sz() / 2 ? _VSTD::next(begin(), __n)
1194                                   : _VSTD::prev(end(), base::__sz() - __n);
1195}
1196
1197template <class _Tp, class _Alloc>
1198list<_Tp, _Alloc>::list(size_type __n)
1199{
1200    _VSTD::__debug_db_insert_c(this);
1201    for (; __n > 0; --__n)
1202#ifndef _LIBCPP_CXX03_LANG
1203        emplace_back();
1204#else
1205        push_back(value_type());
1206#endif
1207}
1208
1209#if _LIBCPP_STD_VER > 11
1210template <class _Tp, class _Alloc>
1211list<_Tp, _Alloc>::list(size_type __n, const allocator_type& __a) : base(__a)
1212{
1213    _VSTD::__debug_db_insert_c(this);
1214    for (; __n > 0; --__n)
1215        emplace_back();
1216}
1217#endif
1218
1219template <class _Tp, class _Alloc>
1220list<_Tp, _Alloc>::list(size_type __n, const value_type& __x)
1221{
1222    _VSTD::__debug_db_insert_c(this);
1223    for (; __n > 0; --__n)
1224        push_back(__x);
1225}
1226
1227template <class _Tp, class _Alloc>
1228template <class _InpIter>
1229list<_Tp, _Alloc>::list(_InpIter __f, _InpIter __l,
1230                        typename enable_if<__is_cpp17_input_iterator<_InpIter>::value>::type*)
1231{
1232    _VSTD::__debug_db_insert_c(this);
1233    for (; __f != __l; ++__f)
1234        __emplace_back(*__f);
1235}
1236
1237template <class _Tp, class _Alloc>
1238template <class _InpIter>
1239list<_Tp, _Alloc>::list(_InpIter __f, _InpIter __l, const allocator_type& __a,
1240                        typename enable_if<__is_cpp17_input_iterator<_InpIter>::value>::type*)
1241    : base(__a)
1242{
1243    _VSTD::__debug_db_insert_c(this);
1244    for (; __f != __l; ++__f)
1245        __emplace_back(*__f);
1246}
1247
1248template <class _Tp, class _Alloc>
1249list<_Tp, _Alloc>::list(const list& __c)
1250    : base(__node_alloc_traits::select_on_container_copy_construction(
1251          __c.__node_alloc())) {
1252    _VSTD::__debug_db_insert_c(this);
1253    for (const_iterator __i = __c.begin(), __e = __c.end(); __i != __e; ++__i)
1254        push_back(*__i);
1255}
1256
1257template <class _Tp, class _Alloc>
1258list<_Tp, _Alloc>::list(const list& __c, const __identity_t<allocator_type>& __a)
1259    : base(__a)
1260{
1261    _VSTD::__debug_db_insert_c(this);
1262    for (const_iterator __i = __c.begin(), __e = __c.end(); __i != __e; ++__i)
1263        push_back(*__i);
1264}
1265
1266#ifndef _LIBCPP_CXX03_LANG
1267
1268template <class _Tp, class _Alloc>
1269list<_Tp, _Alloc>::list(initializer_list<value_type> __il, const allocator_type& __a)
1270    : base(__a)
1271{
1272    _VSTD::__debug_db_insert_c(this);
1273    for (typename initializer_list<value_type>::const_iterator __i = __il.begin(),
1274            __e = __il.end(); __i != __e; ++__i)
1275        push_back(*__i);
1276}
1277
1278template <class _Tp, class _Alloc>
1279list<_Tp, _Alloc>::list(initializer_list<value_type> __il)
1280{
1281    _VSTD::__debug_db_insert_c(this);
1282    for (typename initializer_list<value_type>::const_iterator __i = __il.begin(),
1283            __e = __il.end(); __i != __e; ++__i)
1284        push_back(*__i);
1285}
1286
1287template <class _Tp, class _Alloc>
1288inline list<_Tp, _Alloc>::list(list&& __c)
1289        _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value)
1290        : base(_VSTD::move(__c.__node_alloc())) {
1291    _VSTD::__debug_db_insert_c(this);
1292    splice(end(), __c);
1293}
1294
1295template <class _Tp, class _Alloc>
1296inline
1297list<_Tp, _Alloc>::list(list&& __c, const __identity_t<allocator_type>& __a)
1298    : base(__a)
1299{
1300    _VSTD::__debug_db_insert_c(this);
1301    if (__a == __c.get_allocator())
1302        splice(end(), __c);
1303    else
1304    {
1305        typedef move_iterator<iterator> _Ip;
1306        assign(_Ip(__c.begin()), _Ip(__c.end()));
1307    }
1308}
1309
1310template <class _Tp, class _Alloc>
1311inline
1312list<_Tp, _Alloc>&
1313list<_Tp, _Alloc>::operator=(list&& __c)
1314        _NOEXCEPT_(
1315            __node_alloc_traits::propagate_on_container_move_assignment::value &&
1316            is_nothrow_move_assignable<__node_allocator>::value)
1317{
1318    __move_assign(__c, integral_constant<bool,
1319          __node_alloc_traits::propagate_on_container_move_assignment::value>());
1320    return *this;
1321}
1322
1323template <class _Tp, class _Alloc>
1324void
1325list<_Tp, _Alloc>::__move_assign(list& __c, false_type)
1326{
1327    if (base::__node_alloc() != __c.__node_alloc())
1328    {
1329        typedef move_iterator<iterator> _Ip;
1330        assign(_Ip(__c.begin()), _Ip(__c.end()));
1331    }
1332    else
1333        __move_assign(__c, true_type());
1334}
1335
1336template <class _Tp, class _Alloc>
1337void
1338list<_Tp, _Alloc>::__move_assign(list& __c, true_type)
1339        _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
1340{
1341    clear();
1342    base::__move_assign_alloc(__c);
1343    splice(end(), __c);
1344}
1345
1346#endif // _LIBCPP_CXX03_LANG
1347
1348template <class _Tp, class _Alloc>
1349inline
1350list<_Tp, _Alloc>&
1351list<_Tp, _Alloc>::operator=(const list& __c)
1352{
1353    if (this != _VSTD::addressof(__c))
1354    {
1355        base::__copy_assign_alloc(__c);
1356        assign(__c.begin(), __c.end());
1357    }
1358    return *this;
1359}
1360
1361template <class _Tp, class _Alloc>
1362template <class _InpIter>
1363void
1364list<_Tp, _Alloc>::assign(_InpIter __f, _InpIter __l,
1365                          typename enable_if<__is_cpp17_input_iterator<_InpIter>::value>::type*)
1366{
1367    iterator __i = begin();
1368    iterator __e = end();
1369    for (; __f != __l && __i != __e; ++__f, (void) ++__i)
1370        *__i = *__f;
1371    if (__i == __e)
1372        insert(__e, __f, __l);
1373    else
1374        erase(__i, __e);
1375#if _LIBCPP_DEBUG_LEVEL == 2
1376      __get_db()->__invalidate_all(this);
1377#endif
1378}
1379
1380template <class _Tp, class _Alloc>
1381void
1382list<_Tp, _Alloc>::assign(size_type __n, const value_type& __x)
1383{
1384    iterator __i = begin();
1385    iterator __e = end();
1386    for (; __n > 0 && __i != __e; --__n, (void) ++__i)
1387        *__i = __x;
1388    if (__i == __e)
1389        insert(__e, __n, __x);
1390    else
1391        erase(__i, __e);
1392#if _LIBCPP_DEBUG_LEVEL == 2
1393      __get_db()->__invalidate_all(this);
1394#endif
1395}
1396
1397template <class _Tp, class _Alloc>
1398inline
1399_Alloc
1400list<_Tp, _Alloc>::get_allocator() const _NOEXCEPT
1401{
1402    return allocator_type(base::__node_alloc());
1403}
1404
1405template <class _Tp, class _Alloc>
1406typename list<_Tp, _Alloc>::iterator
1407list<_Tp, _Alloc>::insert(const_iterator __p, const value_type& __x)
1408{
1409    _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__p)) == this,
1410                         "list::insert(iterator, x) called with an iterator not referring to this list");
1411    __node_allocator& __na = base::__node_alloc();
1412    __hold_pointer __hold = __allocate_node(__na);
1413    __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
1414    __link_nodes(__p.__ptr_, __hold->__as_link(), __hold->__as_link());
1415    ++base::__sz();
1416#if _LIBCPP_DEBUG_LEVEL == 2
1417    return iterator(__hold.release()->__as_link(), this);
1418#else
1419    return iterator(__hold.release()->__as_link());
1420#endif
1421}
1422
1423template <class _Tp, class _Alloc>
1424typename list<_Tp, _Alloc>::iterator
1425list<_Tp, _Alloc>::insert(const_iterator __p, size_type __n, const value_type& __x)
1426{
1427    _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__p)) == this,
1428                         "list::insert(iterator, n, x) called with an iterator not referring to this list");
1429#if _LIBCPP_DEBUG_LEVEL == 2
1430    iterator __r(__p.__ptr_, this);
1431#else
1432    iterator __r(__p.__ptr_);
1433#endif
1434    if (__n > 0)
1435    {
1436        size_type __ds = 0;
1437        __node_allocator& __na = base::__node_alloc();
1438        __hold_pointer __hold = __allocate_node(__na);
1439        __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
1440        ++__ds;
1441#if _LIBCPP_DEBUG_LEVEL == 2
1442        __r = iterator(__hold->__as_link(), this);
1443#else
1444        __r = iterator(__hold->__as_link());
1445#endif
1446        __hold.release();
1447        iterator __e = __r;
1448#ifndef _LIBCPP_NO_EXCEPTIONS
1449        try
1450        {
1451#endif // _LIBCPP_NO_EXCEPTIONS
1452            for (--__n; __n != 0; --__n, (void) ++__e, ++__ds)
1453            {
1454                __hold.reset(__node_alloc_traits::allocate(__na, 1));
1455                __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
1456                __e.__ptr_->__next_ = __hold->__as_link();
1457                __hold->__prev_ = __e.__ptr_;
1458                __hold.release();
1459            }
1460#ifndef _LIBCPP_NO_EXCEPTIONS
1461        }
1462        catch (...)
1463        {
1464            while (true)
1465            {
1466                __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
1467                __link_pointer __prev = __e.__ptr_->__prev_;
1468                __node_alloc_traits::deallocate(__na, __e.__ptr_->__as_node(), 1);
1469                if (__prev == 0)
1470                    break;
1471#if _LIBCPP_DEBUG_LEVEL == 2
1472                __e = iterator(__prev, this);
1473#else
1474                __e = iterator(__prev);
1475#endif
1476            }
1477            throw;
1478        }
1479#endif // _LIBCPP_NO_EXCEPTIONS
1480        __link_nodes(__p.__ptr_, __r.__ptr_, __e.__ptr_);
1481        base::__sz() += __ds;
1482    }
1483    return __r;
1484}
1485
1486template <class _Tp, class _Alloc>
1487template <class _InpIter>
1488typename list<_Tp, _Alloc>::iterator
1489list<_Tp, _Alloc>::insert(const_iterator __p, _InpIter __f, _InpIter __l,
1490             typename enable_if<__is_cpp17_input_iterator<_InpIter>::value>::type*)
1491{
1492    _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__p)) == this,
1493                         "list::insert(iterator, range) called with an iterator not referring to this list");
1494#if _LIBCPP_DEBUG_LEVEL == 2
1495    iterator __r(__p.__ptr_, this);
1496#else
1497    iterator __r(__p.__ptr_);
1498#endif
1499    if (__f != __l)
1500    {
1501        size_type __ds = 0;
1502        __node_allocator& __na = base::__node_alloc();
1503        __hold_pointer __hold = __allocate_node(__na);
1504        __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), *__f);
1505        ++__ds;
1506#if _LIBCPP_DEBUG_LEVEL == 2
1507        __r = iterator(__hold.get()->__as_link(), this);
1508#else
1509        __r = iterator(__hold.get()->__as_link());
1510#endif
1511        __hold.release();
1512        iterator __e = __r;
1513#ifndef _LIBCPP_NO_EXCEPTIONS
1514        try
1515        {
1516#endif // _LIBCPP_NO_EXCEPTIONS
1517            for (++__f; __f != __l; ++__f, (void) ++__e, ++__ds)
1518            {
1519                __hold.reset(__node_alloc_traits::allocate(__na, 1));
1520                __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), *__f);
1521                __e.__ptr_->__next_ = __hold.get()->__as_link();
1522                __hold->__prev_ = __e.__ptr_;
1523                __hold.release();
1524            }
1525#ifndef _LIBCPP_NO_EXCEPTIONS
1526        }
1527        catch (...)
1528        {
1529            while (true)
1530            {
1531                __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
1532                __link_pointer __prev = __e.__ptr_->__prev_;
1533                __node_alloc_traits::deallocate(__na, __e.__ptr_->__as_node(), 1);
1534                if (__prev == 0)
1535                    break;
1536#if _LIBCPP_DEBUG_LEVEL == 2
1537                __e = iterator(__prev, this);
1538#else
1539                __e = iterator(__prev);
1540#endif
1541            }
1542            throw;
1543        }
1544#endif // _LIBCPP_NO_EXCEPTIONS
1545        __link_nodes(__p.__ptr_, __r.__ptr_, __e.__ptr_);
1546        base::__sz() += __ds;
1547    }
1548    return __r;
1549}
1550
1551template <class _Tp, class _Alloc>
1552void
1553list<_Tp, _Alloc>::push_front(const value_type& __x)
1554{
1555    __node_allocator& __na = base::__node_alloc();
1556    __hold_pointer __hold = __allocate_node(__na);
1557    __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
1558    __link_pointer __nl = __hold->__as_link();
1559    __link_nodes_at_front(__nl, __nl);
1560    ++base::__sz();
1561    __hold.release();
1562}
1563
1564template <class _Tp, class _Alloc>
1565void
1566list<_Tp, _Alloc>::push_back(const value_type& __x)
1567{
1568    __node_allocator& __na = base::__node_alloc();
1569    __hold_pointer __hold = __allocate_node(__na);
1570    __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
1571    __link_nodes_at_back(__hold.get()->__as_link(), __hold.get()->__as_link());
1572    ++base::__sz();
1573    __hold.release();
1574}
1575
1576#ifndef _LIBCPP_CXX03_LANG
1577
1578template <class _Tp, class _Alloc>
1579void
1580list<_Tp, _Alloc>::push_front(value_type&& __x)
1581{
1582    __node_allocator& __na = base::__node_alloc();
1583    __hold_pointer __hold = __allocate_node(__na);
1584    __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
1585    __link_nodes_at_front(__hold.get()->__as_link(), __hold.get()->__as_link());
1586    ++base::__sz();
1587    __hold.release();
1588}
1589
1590template <class _Tp, class _Alloc>
1591void
1592list<_Tp, _Alloc>::push_back(value_type&& __x)
1593{
1594    __node_allocator& __na = base::__node_alloc();
1595    __hold_pointer __hold = __allocate_node(__na);
1596    __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
1597    __link_nodes_at_back(__hold.get()->__as_link(), __hold.get()->__as_link());
1598    ++base::__sz();
1599    __hold.release();
1600}
1601
1602template <class _Tp, class _Alloc>
1603template <class... _Args>
1604#if _LIBCPP_STD_VER > 14
1605typename list<_Tp, _Alloc>::reference
1606#else
1607void
1608#endif
1609list<_Tp, _Alloc>::emplace_front(_Args&&... __args)
1610{
1611    __node_allocator& __na = base::__node_alloc();
1612    __hold_pointer __hold = __allocate_node(__na);
1613    __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
1614    __link_nodes_at_front(__hold.get()->__as_link(), __hold.get()->__as_link());
1615    ++base::__sz();
1616#if _LIBCPP_STD_VER > 14
1617    return __hold.release()->__value_;
1618#else
1619    __hold.release();
1620#endif
1621}
1622
1623template <class _Tp, class _Alloc>
1624template <class... _Args>
1625#if _LIBCPP_STD_VER > 14
1626typename list<_Tp, _Alloc>::reference
1627#else
1628void
1629#endif
1630list<_Tp, _Alloc>::emplace_back(_Args&&... __args)
1631{
1632    __node_allocator& __na = base::__node_alloc();
1633    __hold_pointer __hold = __allocate_node(__na);
1634    __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
1635    __link_pointer __nl = __hold->__as_link();
1636    __link_nodes_at_back(__nl, __nl);
1637    ++base::__sz();
1638#if _LIBCPP_STD_VER > 14
1639    return __hold.release()->__value_;
1640#else
1641    __hold.release();
1642#endif
1643}
1644
1645template <class _Tp, class _Alloc>
1646template <class... _Args>
1647typename list<_Tp, _Alloc>::iterator
1648list<_Tp, _Alloc>::emplace(const_iterator __p, _Args&&... __args)
1649{
1650    _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__p)) == this,
1651                         "list::emplace(iterator, args...) called with an iterator not referring to this list");
1652    __node_allocator& __na = base::__node_alloc();
1653    __hold_pointer __hold = __allocate_node(__na);
1654    __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
1655    __link_pointer __nl = __hold.get()->__as_link();
1656    __link_nodes(__p.__ptr_, __nl, __nl);
1657    ++base::__sz();
1658    __hold.release();
1659#if _LIBCPP_DEBUG_LEVEL == 2
1660    return iterator(__nl, this);
1661#else
1662    return iterator(__nl);
1663#endif
1664}
1665
1666template <class _Tp, class _Alloc>
1667typename list<_Tp, _Alloc>::iterator
1668list<_Tp, _Alloc>::insert(const_iterator __p, value_type&& __x)
1669{
1670    _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__p)) == this,
1671                         "list::insert(iterator, x) called with an iterator not referring to this list");
1672    __node_allocator& __na = base::__node_alloc();
1673    __hold_pointer __hold = __allocate_node(__na);
1674    __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
1675    __link_pointer __nl = __hold->__as_link();
1676    __link_nodes(__p.__ptr_, __nl, __nl);
1677    ++base::__sz();
1678    __hold.release();
1679#if _LIBCPP_DEBUG_LEVEL == 2
1680    return iterator(__nl, this);
1681#else
1682    return iterator(__nl);
1683#endif
1684}
1685
1686#endif // _LIBCPP_CXX03_LANG
1687
1688template <class _Tp, class _Alloc>
1689void
1690list<_Tp, _Alloc>::pop_front()
1691{
1692    _LIBCPP_ASSERT(!empty(), "list::pop_front() called with empty list");
1693    __node_allocator& __na = base::__node_alloc();
1694    __link_pointer __n = base::__end_.__next_;
1695    base::__unlink_nodes(__n, __n);
1696    --base::__sz();
1697#if _LIBCPP_DEBUG_LEVEL == 2
1698    __c_node* __c = __get_db()->__find_c_and_lock(this);
1699    for (__i_node** __p = __c->end_; __p != __c->beg_; )
1700    {
1701        --__p;
1702        iterator* __i = static_cast<iterator*>((*__p)->__i_);
1703        if (__i->__ptr_ == __n)
1704        {
1705            (*__p)->__c_ = nullptr;
1706            if (--__c->end_ != __p)
1707                _VSTD::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1708        }
1709    }
1710    __get_db()->unlock();
1711#endif
1712    __node_pointer __np = __n->__as_node();
1713    __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
1714    __node_alloc_traits::deallocate(__na, __np, 1);
1715}
1716
1717template <class _Tp, class _Alloc>
1718void
1719list<_Tp, _Alloc>::pop_back()
1720{
1721    _LIBCPP_ASSERT(!empty(), "list::pop_back() called on an empty list");
1722    __node_allocator& __na = base::__node_alloc();
1723    __link_pointer __n = base::__end_.__prev_;
1724    base::__unlink_nodes(__n, __n);
1725    --base::__sz();
1726#if _LIBCPP_DEBUG_LEVEL == 2
1727    __c_node* __c = __get_db()->__find_c_and_lock(this);
1728    for (__i_node** __p = __c->end_; __p != __c->beg_; )
1729    {
1730        --__p;
1731        iterator* __i = static_cast<iterator*>((*__p)->__i_);
1732        if (__i->__ptr_ == __n)
1733        {
1734            (*__p)->__c_ = nullptr;
1735            if (--__c->end_ != __p)
1736                _VSTD::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1737        }
1738    }
1739    __get_db()->unlock();
1740#endif
1741    __node_pointer __np = __n->__as_node();
1742    __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
1743    __node_alloc_traits::deallocate(__na, __np, 1);
1744}
1745
1746template <class _Tp, class _Alloc>
1747typename list<_Tp, _Alloc>::iterator
1748list<_Tp, _Alloc>::erase(const_iterator __p)
1749{
1750    _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__p)) == this,
1751                         "list::erase(iterator) called with an iterator not 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);
1758    --base::__sz();
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_; )
1762    {
1763        --__ip;
1764        iterator* __i = static_cast<iterator*>((*__ip)->__i_);
1765        if (__i->__ptr_ == __n)
1766        {
1767            (*__ip)->__c_ = nullptr;
1768            if (--__c->end_ != __ip)
1769                _VSTD::memmove(__ip, __ip+1, (__c->end_ - __ip)*sizeof(__i_node*));
1770        }
1771    }
1772    __get_db()->unlock();
1773#endif
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);
1779#else
1780    return iterator(__r);
1781#endif
1782}
1783
1784template <class _Tp, class _Alloc>
1785typename list<_Tp, _Alloc>::iterator
1786list<_Tp, _Alloc>::erase(const_iterator __f, const_iterator __l)
1787{
1788    _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__f)) == this,
1789                         "list::erase(iterator, iterator) called with an iterator not referring to this list");
1790    _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__l)) == this,
1791                         "list::erase(iterator, iterator) called with an iterator not referring to this list");
1792    if (__f != __l)
1793    {
1794        __node_allocator& __na = base::__node_alloc();
1795        base::__unlink_nodes(__f.__ptr_, __l.__ptr_->__prev_);
1796        while (__f != __l)
1797        {
1798            __link_pointer __n = __f.__ptr_;
1799            ++__f;
1800            --base::__sz();
1801#if _LIBCPP_DEBUG_LEVEL == 2
1802            __c_node* __c = __get_db()->__find_c_and_lock(this);
1803            for (__i_node** __p = __c->end_; __p != __c->beg_; )
1804            {
1805                --__p;
1806                iterator* __i = static_cast<iterator*>((*__p)->__i_);
1807                if (__i->__ptr_ == __n)
1808                {
1809                    (*__p)->__c_ = nullptr;
1810                    if (--__c->end_ != __p)
1811                        _VSTD::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1812                }
1813            }
1814            __get_db()->unlock();
1815#endif
1816            __node_pointer __np = __n->__as_node();
1817            __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
1818            __node_alloc_traits::deallocate(__na, __np, 1);
1819        }
1820    }
1821#if _LIBCPP_DEBUG_LEVEL == 2
1822    return iterator(__l.__ptr_, this);
1823#else
1824    return iterator(__l.__ptr_);
1825#endif
1826}
1827
1828template <class _Tp, class _Alloc>
1829void
1830list<_Tp, _Alloc>::resize(size_type __n)
1831{
1832    if (__n < base::__sz())
1833        erase(__iterator(__n), end());
1834    else if (__n > base::__sz())
1835    {
1836        __n -= base::__sz();
1837        size_type __ds = 0;
1838        __node_allocator& __na = base::__node_alloc();
1839        __hold_pointer __hold = __allocate_node(__na);
1840        __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_));
1841        ++__ds;
1842#if _LIBCPP_DEBUG_LEVEL == 2
1843        iterator __r = iterator(__hold.release()->__as_link(), this);
1844#else
1845        iterator __r = iterator(__hold.release()->__as_link());
1846#endif
1847        iterator __e = __r;
1848#ifndef _LIBCPP_NO_EXCEPTIONS
1849        try
1850        {
1851#endif // _LIBCPP_NO_EXCEPTIONS
1852            for (--__n; __n != 0; --__n, (void) ++__e, ++__ds)
1853            {
1854                __hold.reset(__node_alloc_traits::allocate(__na, 1));
1855                __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_));
1856                __e.__ptr_->__next_ = __hold.get()->__as_link();
1857                __hold->__prev_ = __e.__ptr_;
1858                __hold.release();
1859            }
1860#ifndef _LIBCPP_NO_EXCEPTIONS
1861        }
1862        catch (...)
1863        {
1864            while (true)
1865            {
1866                __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
1867                __link_pointer __prev = __e.__ptr_->__prev_;
1868                __node_alloc_traits::deallocate(__na, __e.__ptr_->__as_node(), 1);
1869                if (__prev == 0)
1870                    break;
1871#if _LIBCPP_DEBUG_LEVEL == 2
1872                __e = iterator(__prev, this);
1873#else
1874                __e = iterator(__prev);
1875#endif
1876            }
1877            throw;
1878        }
1879#endif // _LIBCPP_NO_EXCEPTIONS
1880        __link_nodes_at_back(__r.__ptr_, __e.__ptr_);
1881        base::__sz() += __ds;
1882    }
1883}
1884
1885template <class _Tp, class _Alloc>
1886void
1887list<_Tp, _Alloc>::resize(size_type __n, const value_type& __x)
1888{
1889    if (__n < base::__sz())
1890        erase(__iterator(__n), end());
1891    else if (__n > base::__sz())
1892    {
1893        __n -= base::__sz();
1894        size_type __ds = 0;
1895        __node_allocator& __na = base::__node_alloc();
1896        __hold_pointer __hold = __allocate_node(__na);
1897        __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
1898        ++__ds;
1899        __link_pointer __nl = __hold.release()->__as_link();
1900#if _LIBCPP_DEBUG_LEVEL == 2
1901        iterator __r = iterator(__nl, this);
1902#else
1903        iterator __r = iterator(__nl);
1904#endif
1905        iterator __e = __r;
1906#ifndef _LIBCPP_NO_EXCEPTIONS
1907        try
1908        {
1909#endif // _LIBCPP_NO_EXCEPTIONS
1910            for (--__n; __n != 0; --__n, (void) ++__e, ++__ds)
1911            {
1912                __hold.reset(__node_alloc_traits::allocate(__na, 1));
1913                __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
1914                __e.__ptr_->__next_ = __hold.get()->__as_link();
1915                __hold->__prev_ = __e.__ptr_;
1916                __hold.release();
1917            }
1918#ifndef _LIBCPP_NO_EXCEPTIONS
1919        }
1920        catch (...)
1921        {
1922            while (true)
1923            {
1924                __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
1925                __link_pointer __prev = __e.__ptr_->__prev_;
1926                __node_alloc_traits::deallocate(__na, __e.__ptr_->__as_node(), 1);
1927                if (__prev == 0)
1928                    break;
1929#if _LIBCPP_DEBUG_LEVEL == 2
1930                __e = iterator(__prev, this);
1931#else
1932                __e = iterator(__prev);
1933#endif
1934            }
1935            throw;
1936        }
1937#endif // _LIBCPP_NO_EXCEPTIONS
1938        __link_nodes(base::__end_as_link(), __r.__ptr_, __e.__ptr_);
1939        base::__sz() += __ds;
1940    }
1941}
1942
1943template <class _Tp, class _Alloc>
1944void
1945list<_Tp, _Alloc>::splice(const_iterator __p, list& __c)
1946{
1947    _LIBCPP_ASSERT(this != _VSTD::addressof(__c),
1948                   "list::splice(iterator, list) called with this == &list");
1949    _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__p)) == this,
1950                         "list::splice(iterator, list) called with an iterator not referring to this list");
1951    if (!__c.empty())
1952    {
1953        __link_pointer __f = __c.__end_.__next_;
1954        __link_pointer __l = __c.__end_.__prev_;
1955        base::__unlink_nodes(__f, __l);
1956        __link_nodes(__p.__ptr_, __f, __l);
1957        base::__sz() += __c.__sz();
1958        __c.__sz() = 0;
1959#if _LIBCPP_DEBUG_LEVEL == 2
1960        if (_VSTD::addressof(__c) != this) {
1961            __libcpp_db* __db = __get_db();
1962            __c_node* __cn1 = __db->__find_c_and_lock(this);
1963            __c_node* __cn2 = __db->__find_c(_VSTD::addressof(__c));
1964            for (__i_node** __ip = __cn2->end_; __ip != __cn2->beg_;)
1965            {
1966                --__ip;
1967                iterator* __i = static_cast<iterator*>((*__ip)->__i_);
1968                if (__i->__ptr_ != __c.__end_as_link())
1969                {
1970                    __cn1->__add(*__ip);
1971                    (*__ip)->__c_ = __cn1;
1972                    if (--__cn2->end_ != __ip)
1973                        _VSTD::memmove(__ip, __ip+1, (__cn2->end_ - __ip)*sizeof(__i_node*));
1974                }
1975            }
1976            __db->unlock();
1977        }
1978#endif
1979    }
1980}
1981
1982template <class _Tp, class _Alloc>
1983void
1984list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __i)
1985{
1986    _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__p)) == this,
1987        "list::splice(iterator, list, iterator) called with the first iterator not referring to this list");
1988    _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__i)) == _VSTD::addressof(__c),
1989        "list::splice(iterator, list, iterator) called with the second iterator not referring to the list argument");
1990    _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(_VSTD::addressof(__i)),
1991        "list::splice(iterator, list, iterator) called with the second iterator not dereferenceable");
1992
1993    if (__p.__ptr_ != __i.__ptr_ && __p.__ptr_ != __i.__ptr_->__next_)
1994    {
1995        __link_pointer __f = __i.__ptr_;
1996        base::__unlink_nodes(__f, __f);
1997        __link_nodes(__p.__ptr_, __f, __f);
1998        --__c.__sz();
1999        ++base::__sz();
2000#if _LIBCPP_DEBUG_LEVEL == 2
2001        if (_VSTD::addressof(__c) != this) {
2002            __libcpp_db* __db = __get_db();
2003            __c_node* __cn1 = __db->__find_c_and_lock(this);
2004            __c_node* __cn2 = __db->__find_c(_VSTD::addressof(__c));
2005            for (__i_node** __ip = __cn2->end_; __ip != __cn2->beg_;)
2006            {
2007                --__ip;
2008                iterator* __j = static_cast<iterator*>((*__ip)->__i_);
2009                if (__j->__ptr_ == __f)
2010                {
2011                    __cn1->__add(*__ip);
2012                    (*__ip)->__c_ = __cn1;
2013                    if (--__cn2->end_ != __ip)
2014                        _VSTD::memmove(__ip, __ip+1, (__cn2->end_ - __ip)*sizeof(__i_node*));
2015                }
2016            }
2017            __db->unlock();
2018        }
2019#endif
2020    }
2021}
2022
2023template <class _Tp, class _Alloc>
2024void
2025list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l)
2026{
2027    _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__p)) == this,
2028        "list::splice(iterator, list, iterator, iterator) called with first iterator not referring to this list");
2029    _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__f)) == _VSTD::addressof(__c),
2030        "list::splice(iterator, list, iterator, iterator) called with second iterator not referring to the list argument");
2031    _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__l)) == _VSTD::addressof(__c),
2032        "list::splice(iterator, list, iterator, iterator) called with third iterator not referring to the list argument");
2033
2034#if _LIBCPP_DEBUG_LEVEL == 2
2035    if (this == _VSTD::addressof(__c))
2036    {
2037        for (const_iterator __i = __f; __i != __l; ++__i)
2038            _LIBCPP_DEBUG_ASSERT(__i != __p,
2039                "list::splice(iterator, list, iterator, iterator)"
2040                " called with the first iterator within the range of the second and third iterators");
2041    }
2042#endif
2043    if (__f != __l)
2044    {
2045        __link_pointer __first = __f.__ptr_;
2046        --__l;
2047        __link_pointer __last = __l.__ptr_;
2048        if (this != _VSTD::addressof(__c))
2049        {
2050            size_type __s = _VSTD::distance(__f, __l) + 1;
2051            __c.__sz() -= __s;
2052            base::__sz() += __s;
2053        }
2054        base::__unlink_nodes(__first, __last);
2055        __link_nodes(__p.__ptr_, __first, __last);
2056#if _LIBCPP_DEBUG_LEVEL == 2
2057        if (_VSTD::addressof(__c) != this) {
2058            __libcpp_db* __db = __get_db();
2059            __c_node* __cn1 = __db->__find_c_and_lock(this);
2060            __c_node* __cn2 = __db->__find_c(_VSTD::addressof(__c));
2061            for (__i_node** __ip = __cn2->end_; __ip != __cn2->beg_;)
2062            {
2063                --__ip;
2064                iterator* __j = static_cast<iterator*>((*__ip)->__i_);
2065                for (__link_pointer __k = __f.__ptr_;
2066                                              __k != __l.__ptr_; __k = __k->__next_)
2067                {
2068                    if (__j->__ptr_ == __k)
2069                    {
2070                        __cn1->__add(*__ip);
2071                        (*__ip)->__c_ = __cn1;
2072                        if (--__cn2->end_ != __ip)
2073                            _VSTD::memmove(__ip, __ip+1, (__cn2->end_ - __ip)*sizeof(__i_node*));
2074                    }
2075                }
2076            }
2077            __db->unlock();
2078        }
2079#endif
2080    }
2081}
2082
2083template <class _Tp, class _Alloc>
2084typename list<_Tp, _Alloc>::__remove_return_type
2085list<_Tp, _Alloc>::remove(const value_type& __x)
2086{
2087    list<_Tp, _Alloc> __deleted_nodes(get_allocator()); // collect the nodes we're removing
2088    for (const_iterator __i = begin(), __e = end(); __i != __e;)
2089    {
2090        if (*__i == __x)
2091        {
2092            const_iterator __j = _VSTD::next(__i);
2093            for (; __j != __e && *__j == __x; ++__j)
2094                ;
2095            __deleted_nodes.splice(__deleted_nodes.end(), *this, __i, __j);
2096            __i = __j;
2097            if (__i != __e)
2098                ++__i;
2099        }
2100        else
2101            ++__i;
2102    }
2103
2104    return (__remove_return_type) __deleted_nodes.size();
2105}
2106
2107template <class _Tp, class _Alloc>
2108template <class _Pred>
2109typename list<_Tp, _Alloc>::__remove_return_type
2110list<_Tp, _Alloc>::remove_if(_Pred __pred)
2111{
2112    list<_Tp, _Alloc> __deleted_nodes(get_allocator()); // collect the nodes we're removing
2113    for (iterator __i = begin(), __e = end(); __i != __e;)
2114    {
2115        if (__pred(*__i))
2116        {
2117            iterator __j = _VSTD::next(__i);
2118            for (; __j != __e && __pred(*__j); ++__j)
2119                ;
2120            __deleted_nodes.splice(__deleted_nodes.end(), *this, __i, __j);
2121            __i = __j;
2122            if (__i != __e)
2123                ++__i;
2124        }
2125        else
2126            ++__i;
2127    }
2128
2129    return (__remove_return_type) __deleted_nodes.size();
2130}
2131
2132template <class _Tp, class _Alloc>
2133template <class _BinaryPred>
2134typename list<_Tp, _Alloc>::__remove_return_type
2135list<_Tp, _Alloc>::unique(_BinaryPred __binary_pred)
2136{
2137    list<_Tp, _Alloc> __deleted_nodes(get_allocator()); // collect the nodes we're removing
2138    for (iterator __i = begin(), __e = end(); __i != __e;)
2139    {
2140        iterator __j = _VSTD::next(__i);
2141        for (; __j != __e && __binary_pred(*__i, *__j); ++__j)
2142            ;
2143        if (++__i != __j) {
2144            __deleted_nodes.splice(__deleted_nodes.end(), *this, __i, __j);
2145            __i = __j;
2146            }
2147    }
2148
2149    return (__remove_return_type) __deleted_nodes.size();
2150}
2151
2152template <class _Tp, class _Alloc>
2153inline
2154void
2155list<_Tp, _Alloc>::merge(list& __c)
2156{
2157    merge(__c, __less<value_type>());
2158}
2159
2160template <class _Tp, class _Alloc>
2161template <class _Comp>
2162void
2163list<_Tp, _Alloc>::merge(list& __c, _Comp __comp)
2164{
2165    if (this != _VSTD::addressof(__c))
2166    {
2167        iterator __f1 = begin();
2168        iterator __e1 = end();
2169        iterator __f2 = __c.begin();
2170        iterator __e2 = __c.end();
2171        while (__f1 != __e1 && __f2 != __e2)
2172        {
2173            if (__comp(*__f2, *__f1))
2174            {
2175                size_type __ds = 1;
2176                iterator __m2 = _VSTD::next(__f2);
2177                for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2, (void) ++__ds)
2178                    ;
2179                base::__sz() += __ds;
2180                __c.__sz() -= __ds;
2181                __link_pointer __f = __f2.__ptr_;
2182                __link_pointer __l = __m2.__ptr_->__prev_;
2183                __f2 = __m2;
2184                base::__unlink_nodes(__f, __l);
2185                __m2 = _VSTD::next(__f1);
2186                __link_nodes(__f1.__ptr_, __f, __l);
2187                __f1 = __m2;
2188            }
2189            else
2190                ++__f1;
2191        }
2192        splice(__e1, __c);
2193#if _LIBCPP_DEBUG_LEVEL == 2
2194        __libcpp_db* __db = __get_db();
2195        __c_node* __cn1 = __db->__find_c_and_lock(this);
2196        __c_node* __cn2 = __db->__find_c(_VSTD::addressof(__c));
2197        for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
2198        {
2199            --__p;
2200            iterator* __i = static_cast<iterator*>((*__p)->__i_);
2201            if (__i->__ptr_ != __c.__end_as_link())
2202            {
2203                __cn1->__add(*__p);
2204                (*__p)->__c_ = __cn1;
2205                if (--__cn2->end_ != __p)
2206                    _VSTD::memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
2207            }
2208        }
2209        __db->unlock();
2210#endif
2211    }
2212}
2213
2214template <class _Tp, class _Alloc>
2215inline
2216void
2217list<_Tp, _Alloc>::sort()
2218{
2219    sort(__less<value_type>());
2220}
2221
2222template <class _Tp, class _Alloc>
2223template <class _Comp>
2224inline
2225void
2226list<_Tp, _Alloc>::sort(_Comp __comp)
2227{
2228    __sort(begin(), end(), base::__sz(), __comp);
2229}
2230
2231template <class _Tp, class _Alloc>
2232template <class _Comp>
2233typename list<_Tp, _Alloc>::iterator
2234list<_Tp, _Alloc>::__sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp)
2235{
2236    switch (__n)
2237    {
2238    case 0:
2239    case 1:
2240        return __f1;
2241    case 2:
2242        if (__comp(*--__e2, *__f1))
2243        {
2244            __link_pointer __f = __e2.__ptr_;
2245            base::__unlink_nodes(__f, __f);
2246            __link_nodes(__f1.__ptr_, __f, __f);
2247            return __e2;
2248        }
2249        return __f1;
2250    }
2251    size_type __n2 = __n / 2;
2252    iterator __e1 = _VSTD::next(__f1, __n2);
2253    iterator  __r = __f1 = __sort(__f1, __e1, __n2, __comp);
2254    iterator __f2 = __e1 = __sort(__e1, __e2, __n - __n2, __comp);
2255    if (__comp(*__f2, *__f1))
2256    {
2257        iterator __m2 = _VSTD::next(__f2);
2258        for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2)
2259            ;
2260        __link_pointer __f = __f2.__ptr_;
2261        __link_pointer __l = __m2.__ptr_->__prev_;
2262        __r = __f2;
2263        __e1 = __f2 = __m2;
2264        base::__unlink_nodes(__f, __l);
2265        __m2 = _VSTD::next(__f1);
2266        __link_nodes(__f1.__ptr_, __f, __l);
2267        __f1 = __m2;
2268    }
2269    else
2270        ++__f1;
2271    while (__f1 != __e1 && __f2 != __e2)
2272    {
2273        if (__comp(*__f2, *__f1))
2274        {
2275            iterator __m2 = _VSTD::next(__f2);
2276            for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2)
2277                ;
2278            __link_pointer __f = __f2.__ptr_;
2279            __link_pointer __l = __m2.__ptr_->__prev_;
2280            if (__e1 == __f2)
2281                __e1 = __m2;
2282            __f2 = __m2;
2283            base::__unlink_nodes(__f, __l);
2284            __m2 = _VSTD::next(__f1);
2285            __link_nodes(__f1.__ptr_, __f, __l);
2286            __f1 = __m2;
2287        }
2288        else
2289            ++__f1;
2290    }
2291    return __r;
2292}
2293
2294template <class _Tp, class _Alloc>
2295void
2296list<_Tp, _Alloc>::reverse() _NOEXCEPT
2297{
2298    if (base::__sz() > 1)
2299    {
2300        iterator __e = end();
2301        for (iterator __i = begin(); __i.__ptr_ != __e.__ptr_;)
2302        {
2303            _VSTD::swap(__i.__ptr_->__prev_, __i.__ptr_->__next_);
2304            __i.__ptr_ = __i.__ptr_->__prev_;
2305        }
2306        _VSTD::swap(__e.__ptr_->__prev_, __e.__ptr_->__next_);
2307    }
2308}
2309
2310template <class _Tp, class _Alloc>
2311bool
2312list<_Tp, _Alloc>::__invariants() const
2313{
2314    return size() == _VSTD::distance(begin(), end());
2315}
2316
2317#if _LIBCPP_DEBUG_LEVEL == 2
2318
2319template <class _Tp, class _Alloc>
2320bool
2321list<_Tp, _Alloc>::__dereferenceable(const const_iterator* __i) const
2322{
2323    return __i->__ptr_ != this->__end_as_link();
2324}
2325
2326template <class _Tp, class _Alloc>
2327bool
2328list<_Tp, _Alloc>::__decrementable(const const_iterator* __i) const
2329{
2330    return !empty() &&  __i->__ptr_ != base::__end_.__next_;
2331}
2332
2333template <class _Tp, class _Alloc>
2334bool
2335list<_Tp, _Alloc>::__addable(const const_iterator*, ptrdiff_t) const
2336{
2337    return false;
2338}
2339
2340template <class _Tp, class _Alloc>
2341bool
2342list<_Tp, _Alloc>::__subscriptable(const const_iterator*, ptrdiff_t) const
2343{
2344    return false;
2345}
2346
2347#endif // _LIBCPP_DEBUG_LEVEL == 2
2348
2349template <class _Tp, class _Alloc>
2350inline _LIBCPP_INLINE_VISIBILITY
2351bool
2352operator==(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2353{
2354    return __x.size() == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
2355}
2356
2357template <class _Tp, class _Alloc>
2358inline _LIBCPP_INLINE_VISIBILITY
2359bool
2360operator< (const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2361{
2362    return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
2363}
2364
2365template <class _Tp, class _Alloc>
2366inline _LIBCPP_INLINE_VISIBILITY
2367bool
2368operator!=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2369{
2370    return !(__x == __y);
2371}
2372
2373template <class _Tp, class _Alloc>
2374inline _LIBCPP_INLINE_VISIBILITY
2375bool
2376operator> (const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2377{
2378    return __y < __x;
2379}
2380
2381template <class _Tp, class _Alloc>
2382inline _LIBCPP_INLINE_VISIBILITY
2383bool
2384operator>=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2385{
2386    return !(__x < __y);
2387}
2388
2389template <class _Tp, class _Alloc>
2390inline _LIBCPP_INLINE_VISIBILITY
2391bool
2392operator<=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2393{
2394    return !(__y < __x);
2395}
2396
2397template <class _Tp, class _Alloc>
2398inline _LIBCPP_INLINE_VISIBILITY
2399void
2400swap(list<_Tp, _Alloc>& __x, list<_Tp, _Alloc>& __y)
2401    _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
2402{
2403    __x.swap(__y);
2404}
2405
2406#if _LIBCPP_STD_VER > 17
2407template <class _Tp, class _Allocator, class _Predicate>
2408inline _LIBCPP_INLINE_VISIBILITY typename list<_Tp, _Allocator>::size_type
2409erase_if(list<_Tp, _Allocator>& __c, _Predicate __pred) {
2410  return __c.remove_if(__pred);
2411}
2412
2413template <class _Tp, class _Allocator, class _Up>
2414inline _LIBCPP_INLINE_VISIBILITY typename list<_Tp, _Allocator>::size_type
2415erase(list<_Tp, _Allocator>& __c, const _Up& __v) {
2416  return _VSTD::erase_if(__c, [&](auto& __elem) { return __elem == __v; });
2417}
2418#endif
2419
2420_LIBCPP_END_NAMESPACE_STD
2421
2422_LIBCPP_POP_MACROS
2423
2424#endif // _LIBCPP_LIST
2425