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