• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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    template<container-compatible-range<T> R>
50      list(from_range_t, R&& rg, const Allocator& = Allocator()); // C++23
51    list(const list& x);
52    list(const list&, const allocator_type& a);
53    list(list&& x)
54        noexcept(is_nothrow_move_constructible<allocator_type>::value);
55    list(list&&, const allocator_type& a);
56    list(initializer_list<value_type>);
57    list(initializer_list<value_type>, const allocator_type& a);
58
59    ~list();
60
61    list& operator=(const list& x);
62    list& operator=(list&& x)
63        noexcept(
64             allocator_type::propagate_on_container_move_assignment::value &&
65             is_nothrow_move_assignable<allocator_type>::value);
66    list& operator=(initializer_list<value_type>);
67    template <class Iter>
68        void assign(Iter first, Iter last);
69    template<container-compatible-range<T> R>
70      void assign_range(R&& rg); // C++23
71    void assign(size_type n, const value_type& t);
72    void assign(initializer_list<value_type>);
73
74    allocator_type get_allocator() const noexcept;
75
76    iterator begin() noexcept;
77    const_iterator begin() const noexcept;
78    iterator end() noexcept;
79    const_iterator end() const noexcept;
80    reverse_iterator rbegin() noexcept;
81    const_reverse_iterator rbegin() const noexcept;
82    reverse_iterator rend() noexcept;
83    const_reverse_iterator rend() const noexcept;
84    const_iterator cbegin() const noexcept;
85    const_iterator cend() const noexcept;
86    const_reverse_iterator crbegin() const noexcept;
87    const_reverse_iterator crend() const noexcept;
88
89    reference front();
90    const_reference front() const;
91    reference back();
92    const_reference back() const;
93
94    bool empty() const noexcept;
95    size_type size() const noexcept;
96    size_type max_size() const noexcept;
97
98    template <class... Args>
99        reference emplace_front(Args&&... args); // reference in C++17
100    void pop_front();
101    template <class... Args>
102        reference emplace_back(Args&&... args);  // reference in C++17
103    void pop_back();
104    void push_front(const value_type& x);
105    void push_front(value_type&& x);
106    template<container-compatible-range<T> R>
107      void prepend_range(R&& rg); // C++23
108    void push_back(const value_type& x);
109    void push_back(value_type&& x);
110    template<container-compatible-range<T> R>
111      void append_range(R&& rg); // C++23
112    template <class... Args>
113        iterator emplace(const_iterator position, Args&&... args);
114    iterator insert(const_iterator position, const value_type& x);
115    iterator insert(const_iterator position, value_type&& x);
116    iterator insert(const_iterator position, size_type n, const value_type& x);
117    template <class Iter>
118        iterator insert(const_iterator position, Iter first, Iter last);
119    template<container-compatible-range<T> R>
120      iterator insert_range(const_iterator position, R&& rg); // C++23
121    iterator insert(const_iterator position, initializer_list<value_type> il);
122
123    iterator erase(const_iterator position);
124    iterator erase(const_iterator position, const_iterator last);
125
126    void resize(size_type sz);
127    void resize(size_type sz, const value_type& c);
128
129    void swap(list&)
130        noexcept(allocator_traits<allocator_type>::is_always_equal::value);  // C++17
131    void clear() noexcept;
132
133    void splice(const_iterator position, list& x);
134    void splice(const_iterator position, list&& x);
135    void splice(const_iterator position, list& x, const_iterator i);
136    void splice(const_iterator position, list&& x, const_iterator i);
137    void splice(const_iterator position, list& x, const_iterator first,
138                                                  const_iterator last);
139    void splice(const_iterator position, list&& x, const_iterator first,
140                                                  const_iterator last);
141
142    size_type remove(const value_type& value);       // void before C++20
143    template <class Pred>
144      size_type remove_if(Pred pred);                // void before C++20
145    size_type unique();                              // void before C++20
146    template <class BinaryPredicate>
147      size_type unique(BinaryPredicate binary_pred); // void before C++20
148    void merge(list& x);
149    void merge(list&& x);
150    template <class Compare>
151        void merge(list& x, Compare comp);
152    template <class Compare>
153        void merge(list&& x, Compare comp);
154    void sort();
155    template <class Compare>
156        void sort(Compare comp);
157    void reverse() noexcept;
158};
159
160
161template <class InputIterator, class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
162    list(InputIterator, InputIterator, Allocator = Allocator())
163    -> list<typename iterator_traits<InputIterator>::value_type, Allocator>;  // C++17
164
165template<ranges::input_range R, class Allocator = allocator<ranges::range_value_t<R>>>
166  list(from_range_t, R&&, Allocator = Allocator())
167    -> list<ranges::range_value_t<R>, Allocator>; // C++23
168
169template <class T, class Alloc>
170    bool operator==(const list<T,Alloc>& x, const list<T,Alloc>& y);
171template <class T, class Alloc>
172    bool operator< (const list<T,Alloc>& x, const list<T,Alloc>& y);     // removed in C++20
173template <class T, class Alloc>
174    bool operator!=(const list<T,Alloc>& x, const list<T,Alloc>& y);     // removed in C++20
175template <class T, class Alloc>
176    bool operator> (const list<T,Alloc>& x, const list<T,Alloc>& y);     // removed in C++20
177template <class T, class Alloc>
178    bool operator>=(const list<T,Alloc>& x, const list<T,Alloc>& y);     // removed in C++20
179template <class T, class Alloc>
180    bool operator<=(const list<T,Alloc>& x, const list<T,Alloc>& y);     // removed in C++20
181template<class T, class Allocator>
182  synth-three-way-result<T> operator<=>(const list<T, Allocator>& x,
183                                        const list<T, Allocator>& y);    // since C++20
184
185template <class T, class Alloc>
186    void swap(list<T,Alloc>& x, list<T,Alloc>& y)
187         noexcept(noexcept(x.swap(y)));
188
189template <class T, class Allocator, class U>
190    typename list<T, Allocator>::size_type
191    erase(list<T, Allocator>& c, const U& value);       // since C++20
192template <class T, class Allocator, class Predicate>
193    typename list<T, Allocator>::size_type
194    erase_if(list<T, Allocator>& c, Predicate pred);    // since C++20
195
196}  // std
197
198*/
199
200#include <__algorithm/comp.h>
201#include <__algorithm/equal.h>
202#include <__algorithm/lexicographical_compare.h>
203#include <__algorithm/lexicographical_compare_three_way.h>
204#include <__algorithm/min.h>
205#include <__assert>
206#include <__config>
207#include <__format/enable_insertable.h>
208#include <__iterator/distance.h>
209#include <__iterator/iterator_traits.h>
210#include <__iterator/move_iterator.h>
211#include <__iterator/next.h>
212#include <__iterator/prev.h>
213#include <__iterator/reverse_iterator.h>
214#include <__memory/addressof.h>
215#include <__memory/allocation_guard.h>
216#include <__memory/allocator.h>
217#include <__memory/allocator_traits.h>
218#include <__memory/compressed_pair.h>
219#include <__memory/construct_at.h>
220#include <__memory/pointer_traits.h>
221#include <__memory/swap_allocator.h>
222#include <__memory_resource/polymorphic_allocator.h>
223#include <__ranges/access.h>
224#include <__ranges/concepts.h>
225#include <__ranges/container_compatible_range.h>
226#include <__ranges/from_range.h>
227#include <__type_traits/conditional.h>
228#include <__type_traits/container_traits.h>
229#include <__type_traits/enable_if.h>
230#include <__type_traits/is_allocator.h>
231#include <__type_traits/is_nothrow_assignable.h>
232#include <__type_traits/is_nothrow_constructible.h>
233#include <__type_traits/is_pointer.h>
234#include <__type_traits/is_same.h>
235#include <__type_traits/type_identity.h>
236#include <__utility/forward.h>
237#include <__utility/move.h>
238#include <__utility/swap.h>
239#include <cstring>
240#include <limits>
241#include <new> // __launder
242#include <version>
243
244// standard-mandated includes
245
246// [iterator.range]
247#include <__iterator/access.h>
248#include <__iterator/data.h>
249#include <__iterator/empty.h>
250#include <__iterator/reverse_access.h>
251#include <__iterator/size.h>
252
253// [list.syn]
254#include <compare>
255#include <initializer_list>
256
257#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
258#  pragma GCC system_header
259#endif
260
261_LIBCPP_PUSH_MACROS
262#include <__undef_macros>
263
264_LIBCPP_BEGIN_NAMESPACE_STD
265
266template <class _Tp, class _VoidPtr>
267struct __list_node;
268template <class _Tp, class _VoidPtr>
269struct __list_node_base;
270
271template <class _Tp, class _VoidPtr>
272struct __list_node_pointer_traits {
273  typedef __rebind_pointer_t<_VoidPtr, __list_node<_Tp, _VoidPtr> > __node_pointer;
274  typedef __rebind_pointer_t<_VoidPtr, __list_node_base<_Tp, _VoidPtr> > __base_pointer;
275
276// TODO(LLVM 22): Remove this check
277#ifndef _LIBCPP_ABI_LIST_REMOVE_NODE_POINTER_UB
278  static_assert(sizeof(__node_pointer) == sizeof(__node_pointer) && _LIBCPP_ALIGNOF(__base_pointer) ==
279                    _LIBCPP_ALIGNOF(__node_pointer),
280                "It looks like you are using std::list with a fancy pointer type that thas a different representation "
281                "depending on whether it points to a list base pointer or a list node pointer (both of which are "
282                "implementation details of the standard library). This means that your ABI is being broken between "
283                "LLVM 19 and LLVM 20. If you don't care about your ABI being broken, define the "
284                "_LIBCPP_ABI_LIST_REMOVE_NODE_POINTER_UB macro to silence this diagnostic.");
285#endif
286
287  static _LIBCPP_HIDE_FROM_ABI __base_pointer __unsafe_link_pointer_cast(__base_pointer __p) { return __p; }
288
289  static _LIBCPP_HIDE_FROM_ABI __base_pointer __unsafe_link_pointer_cast(__node_pointer __p) {
290    return static_cast<__base_pointer>(static_cast<_VoidPtr>(__p));
291  }
292};
293
294template <class _Tp, class _VoidPtr>
295struct __list_node_base {
296  typedef __list_node_pointer_traits<_Tp, _VoidPtr> _NodeTraits;
297  typedef typename _NodeTraits::__node_pointer __node_pointer;
298  typedef typename _NodeTraits::__base_pointer __base_pointer;
299
300  __base_pointer __prev_;
301  __base_pointer __next_;
302
303  _LIBCPP_HIDE_FROM_ABI __list_node_base() : __prev_(__self()), __next_(__self()) {}
304
305  _LIBCPP_HIDE_FROM_ABI explicit __list_node_base(__base_pointer __prev, __base_pointer __next)
306      : __prev_(__prev), __next_(__next) {}
307
308  _LIBCPP_HIDE_FROM_ABI __base_pointer __self() { return pointer_traits<__base_pointer>::pointer_to(*this); }
309
310  _LIBCPP_HIDE_FROM_ABI __node_pointer __as_node() { return static_cast<__node_pointer>(__self()); }
311};
312
313template <class _Tp, class _VoidPtr>
314struct __list_node : public __list_node_base<_Tp, _VoidPtr> {
315  // We allow starting the lifetime of nodes without initializing the value held by the node,
316  // since that is handled by the list itself in order to be allocator-aware.
317#ifndef _LIBCPP_CXX03_LANG
318
319private:
320  union {
321    _Tp __value_;
322  };
323
324public:
325  _LIBCPP_HIDE_FROM_ABI _Tp& __get_value() { return __value_; }
326#else
327
328private:
329  _ALIGNAS_TYPE(_Tp) char __buffer_[sizeof(_Tp)];
330
331public:
332  _LIBCPP_HIDE_FROM_ABI _Tp& __get_value() { return *std::__launder(reinterpret_cast<_Tp*>(&__buffer_)); }
333#endif
334
335  typedef __list_node_base<_Tp, _VoidPtr> __base;
336  typedef typename __base::__base_pointer __base_pointer;
337
338  _LIBCPP_HIDE_FROM_ABI explicit __list_node(__base_pointer __prev, __base_pointer __next) : __base(__prev, __next) {}
339  _LIBCPP_HIDE_FROM_ABI ~__list_node() {}
340
341  _LIBCPP_HIDE_FROM_ABI __base_pointer __as_link() { return __base::__self(); }
342};
343
344template <class _Tp, class _Alloc = allocator<_Tp> >
345class _LIBCPP_TEMPLATE_VIS list;
346template <class _Tp, class _Alloc>
347class __list_imp;
348template <class _Tp, class _VoidPtr>
349class _LIBCPP_TEMPLATE_VIS __list_const_iterator;
350
351template <class _Tp, class _VoidPtr>
352class _LIBCPP_TEMPLATE_VIS __list_iterator {
353  typedef __list_node_pointer_traits<_Tp, _VoidPtr> _NodeTraits;
354  typedef typename _NodeTraits::__base_pointer __base_pointer;
355
356  __base_pointer __ptr_;
357
358  _LIBCPP_HIDE_FROM_ABI explicit __list_iterator(__base_pointer __p) _NOEXCEPT : __ptr_(__p) {}
359
360  template <class, class>
361  friend class list;
362  template <class, class>
363  friend class __list_imp;
364  template <class, class>
365  friend class __list_const_iterator;
366
367public:
368  typedef bidirectional_iterator_tag iterator_category;
369  typedef _Tp value_type;
370  typedef value_type& reference;
371  typedef __rebind_pointer_t<_VoidPtr, value_type> pointer;
372  typedef typename pointer_traits<pointer>::difference_type difference_type;
373
374  _LIBCPP_HIDE_FROM_ABI __list_iterator() _NOEXCEPT : __ptr_(nullptr) {}
375
376  _LIBCPP_HIDE_FROM_ABI reference operator*() const { return __ptr_->__as_node()->__get_value(); }
377  _LIBCPP_HIDE_FROM_ABI pointer operator->() const {
378    return pointer_traits<pointer>::pointer_to(__ptr_->__as_node()->__get_value());
379  }
380
381  _LIBCPP_HIDE_FROM_ABI __list_iterator& operator++() {
382    __ptr_ = __ptr_->__next_;
383    return *this;
384  }
385  _LIBCPP_HIDE_FROM_ABI __list_iterator operator++(int) {
386    __list_iterator __t(*this);
387    ++(*this);
388    return __t;
389  }
390
391  _LIBCPP_HIDE_FROM_ABI __list_iterator& operator--() {
392    __ptr_ = __ptr_->__prev_;
393    return *this;
394  }
395  _LIBCPP_HIDE_FROM_ABI __list_iterator operator--(int) {
396    __list_iterator __t(*this);
397    --(*this);
398    return __t;
399  }
400
401  friend _LIBCPP_HIDE_FROM_ABI bool operator==(const __list_iterator& __x, const __list_iterator& __y) {
402    return __x.__ptr_ == __y.__ptr_;
403  }
404  friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const __list_iterator& __x, const __list_iterator& __y) {
405    return !(__x == __y);
406  }
407};
408
409template <class _Tp, class _VoidPtr>
410class _LIBCPP_TEMPLATE_VIS __list_const_iterator {
411  typedef __list_node_pointer_traits<_Tp, _VoidPtr> _NodeTraits;
412  typedef typename _NodeTraits::__base_pointer __base_pointer;
413
414  __base_pointer __ptr_;
415
416  _LIBCPP_HIDE_FROM_ABI explicit __list_const_iterator(__base_pointer __p) _NOEXCEPT : __ptr_(__p) {}
417
418  template <class, class>
419  friend class list;
420  template <class, class>
421  friend class __list_imp;
422
423public:
424  typedef bidirectional_iterator_tag iterator_category;
425  typedef _Tp value_type;
426  typedef const value_type& reference;
427  typedef __rebind_pointer_t<_VoidPtr, const value_type> pointer;
428  typedef typename pointer_traits<pointer>::difference_type difference_type;
429
430  _LIBCPP_HIDE_FROM_ABI __list_const_iterator() _NOEXCEPT : __ptr_(nullptr) {}
431  _LIBCPP_HIDE_FROM_ABI __list_const_iterator(const __list_iterator<_Tp, _VoidPtr>& __p) _NOEXCEPT
432      : __ptr_(__p.__ptr_) {}
433
434  _LIBCPP_HIDE_FROM_ABI reference operator*() const { return __ptr_->__as_node()->__get_value(); }
435  _LIBCPP_HIDE_FROM_ABI pointer operator->() const {
436    return pointer_traits<pointer>::pointer_to(__ptr_->__as_node()->__get_value());
437  }
438
439  _LIBCPP_HIDE_FROM_ABI __list_const_iterator& operator++() {
440    __ptr_ = __ptr_->__next_;
441    return *this;
442  }
443  _LIBCPP_HIDE_FROM_ABI __list_const_iterator operator++(int) {
444    __list_const_iterator __t(*this);
445    ++(*this);
446    return __t;
447  }
448
449  _LIBCPP_HIDE_FROM_ABI __list_const_iterator& operator--() {
450    __ptr_ = __ptr_->__prev_;
451    return *this;
452  }
453  _LIBCPP_HIDE_FROM_ABI __list_const_iterator operator--(int) {
454    __list_const_iterator __t(*this);
455    --(*this);
456    return __t;
457  }
458
459  friend _LIBCPP_HIDE_FROM_ABI bool operator==(const __list_const_iterator& __x, const __list_const_iterator& __y) {
460    return __x.__ptr_ == __y.__ptr_;
461  }
462  friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const __list_const_iterator& __x, const __list_const_iterator& __y) {
463    return !(__x == __y);
464  }
465};
466
467template <class _Tp, class _Alloc>
468class __list_imp {
469public:
470  __list_imp(const __list_imp&)            = delete;
471  __list_imp& operator=(const __list_imp&) = delete;
472
473  typedef _Alloc allocator_type;
474  typedef allocator_traits<allocator_type> __alloc_traits;
475  typedef typename __alloc_traits::size_type size_type;
476
477protected:
478  typedef _Tp value_type;
479  typedef typename __alloc_traits::void_pointer __void_pointer;
480  typedef __list_iterator<value_type, __void_pointer> iterator;
481  typedef __list_const_iterator<value_type, __void_pointer> const_iterator;
482  typedef __list_node_base<value_type, __void_pointer> __node_base;
483  typedef __list_node<value_type, __void_pointer> __node_type;
484  typedef __rebind_alloc<__alloc_traits, __node_type> __node_allocator;
485  typedef allocator_traits<__node_allocator> __node_alloc_traits;
486  typedef typename __node_alloc_traits::pointer __node_pointer;
487  typedef typename __node_alloc_traits::pointer __node_const_pointer;
488  typedef __list_node_pointer_traits<value_type, __void_pointer> __node_pointer_traits;
489  typedef typename __node_pointer_traits::__base_pointer __base_pointer;
490  typedef __base_pointer __link_const_pointer;
491  typedef typename __alloc_traits::pointer pointer;
492  typedef typename __alloc_traits::const_pointer const_pointer;
493  typedef typename __alloc_traits::difference_type difference_type;
494
495  typedef __rebind_alloc<__alloc_traits, __node_base> __node_base_allocator;
496  typedef typename allocator_traits<__node_base_allocator>::pointer __node_base_pointer;
497  static_assert(!is_same<allocator_type, __node_allocator>::value,
498                "internal allocator type must differ from user-specified type; otherwise overload resolution breaks");
499
500  __node_base __end_;
501  _LIBCPP_COMPRESSED_PAIR(size_type, __size_, __node_allocator, __node_alloc_);
502
503  _LIBCPP_HIDE_FROM_ABI __base_pointer __end_as_link() const _NOEXCEPT {
504    return __node_pointer_traits::__unsafe_link_pointer_cast(const_cast<__node_base&>(__end_).__self());
505  }
506
507  _LIBCPP_HIDE_FROM_ABI size_type __node_alloc_max_size() const _NOEXCEPT {
508    return __node_alloc_traits::max_size(__node_alloc_);
509  }
510  _LIBCPP_HIDE_FROM_ABI static void __unlink_nodes(__base_pointer __f, __base_pointer __l) _NOEXCEPT;
511
512  _LIBCPP_HIDE_FROM_ABI __list_imp() _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value);
513  _LIBCPP_HIDE_FROM_ABI __list_imp(const allocator_type& __a);
514  _LIBCPP_HIDE_FROM_ABI __list_imp(const __node_allocator& __a);
515#ifndef _LIBCPP_CXX03_LANG
516  _LIBCPP_HIDE_FROM_ABI __list_imp(__node_allocator&& __a) _NOEXCEPT;
517#endif
518  _LIBCPP_HIDE_FROM_ABI ~__list_imp();
519  _LIBCPP_HIDE_FROM_ABI void clear() _NOEXCEPT;
520  _LIBCPP_HIDE_FROM_ABI bool empty() const _NOEXCEPT { return __size_ == 0; }
521
522  _LIBCPP_HIDE_FROM_ABI iterator begin() _NOEXCEPT { return iterator(__end_.__next_); }
523  _LIBCPP_HIDE_FROM_ABI const_iterator begin() const _NOEXCEPT { return const_iterator(__end_.__next_); }
524  _LIBCPP_HIDE_FROM_ABI iterator end() _NOEXCEPT { return iterator(__end_as_link()); }
525  _LIBCPP_HIDE_FROM_ABI const_iterator end() const _NOEXCEPT { return const_iterator(__end_as_link()); }
526
527  _LIBCPP_HIDE_FROM_ABI void swap(__list_imp& __c)
528#if _LIBCPP_STD_VER >= 14
529      _NOEXCEPT;
530#else
531      _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || __is_nothrow_swappable_v<allocator_type>);
532#endif
533
534  _LIBCPP_HIDE_FROM_ABI void __copy_assign_alloc(const __list_imp& __c) {
535    __copy_assign_alloc(
536        __c, integral_constant<bool, __node_alloc_traits::propagate_on_container_copy_assignment::value>());
537  }
538
539  _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(__list_imp& __c)
540      _NOEXCEPT_(!__node_alloc_traits::propagate_on_container_move_assignment::value ||
541                 is_nothrow_move_assignable<__node_allocator>::value) {
542    __move_assign_alloc(
543        __c, integral_constant<bool, __node_alloc_traits::propagate_on_container_move_assignment::value>());
544  }
545
546  template <class... _Args>
547  _LIBCPP_HIDE_FROM_ABI __node_pointer __create_node(__base_pointer __prev, __base_pointer __next, _Args&&... __args) {
548    __allocation_guard<__node_allocator> __guard(__node_alloc_, 1);
549    // Begin the lifetime of the node itself. Note that this doesn't begin the lifetime of the value
550    // held inside the node, since we need to use the allocator's construct() method for that.
551    //
552    // We don't use the allocator's construct() method to construct the node itself since the
553    // Cpp17FooInsertable named requirements don't require the allocator's construct() method
554    // to work on anything other than the value_type.
555    std::__construct_at(std::addressof(*__guard.__get()), __prev, __next);
556
557    // Now construct the value_type using the allocator's construct() method.
558    __node_alloc_traits::construct(
559        __node_alloc_, std::addressof(__guard.__get()->__get_value()), std::forward<_Args>(__args)...);
560    return __guard.__release_ptr();
561  }
562
563  _LIBCPP_HIDE_FROM_ABI void __delete_node(__node_pointer __node) {
564    // For the same reason as above, we use the allocator's destroy() method for the value_type,
565    // but not for the node itself.
566    __node_alloc_traits::destroy(__node_alloc_, std::addressof(__node->__get_value()));
567    std::__destroy_at(std::addressof(*__node));
568    __node_alloc_traits::deallocate(__node_alloc_, __node, 1);
569  }
570
571private:
572  _LIBCPP_HIDE_FROM_ABI void __copy_assign_alloc(const __list_imp& __c, true_type) {
573    if (__node_alloc_ != __c.__node_alloc_)
574      clear();
575    __node_alloc_ = __c.__node_alloc_;
576  }
577
578  _LIBCPP_HIDE_FROM_ABI void __copy_assign_alloc(const __list_imp&, false_type) {}
579
580  _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(__list_imp& __c, true_type)
581      _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value) {
582    __node_alloc_ = std::move(__c.__node_alloc_);
583  }
584
585  _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(__list_imp&, false_type) _NOEXCEPT {}
586};
587
588// Unlink nodes [__f, __l]
589template <class _Tp, class _Alloc>
590inline void __list_imp<_Tp, _Alloc>::__unlink_nodes(__base_pointer __f, __base_pointer __l) _NOEXCEPT {
591  __f->__prev_->__next_ = __l->__next_;
592  __l->__next_->__prev_ = __f->__prev_;
593}
594
595template <class _Tp, class _Alloc>
596inline __list_imp<_Tp, _Alloc>::__list_imp() _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value)
597    : __size_(0) {}
598
599template <class _Tp, class _Alloc>
600inline __list_imp<_Tp, _Alloc>::__list_imp(const allocator_type& __a)
601    : __size_(0), __node_alloc_(__node_allocator(__a)) {}
602
603template <class _Tp, class _Alloc>
604inline __list_imp<_Tp, _Alloc>::__list_imp(const __node_allocator& __a) : __size_(0), __node_alloc_(__a) {}
605
606#ifndef _LIBCPP_CXX03_LANG
607template <class _Tp, class _Alloc>
608inline __list_imp<_Tp, _Alloc>::__list_imp(__node_allocator&& __a) _NOEXCEPT
609    : __size_(0),
610      __node_alloc_(std::move(__a)) {}
611#endif
612
613template <class _Tp, class _Alloc>
614__list_imp<_Tp, _Alloc>::~__list_imp() {
615  clear();
616}
617
618template <class _Tp, class _Alloc>
619void __list_imp<_Tp, _Alloc>::clear() _NOEXCEPT {
620  if (!empty()) {
621    __base_pointer __f = __end_.__next_;
622    __base_pointer __l = __end_as_link();
623    __unlink_nodes(__f, __l->__prev_);
624    __size_ = 0;
625    while (__f != __l) {
626      __node_pointer __np = __f->__as_node();
627      __f                 = __f->__next_;
628      __delete_node(__np);
629    }
630  }
631}
632
633template <class _Tp, class _Alloc>
634void __list_imp<_Tp, _Alloc>::swap(__list_imp& __c)
635#if _LIBCPP_STD_VER >= 14
636    _NOEXCEPT
637#else
638    _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || __is_nothrow_swappable_v<allocator_type>)
639#endif
640{
641  _LIBCPP_ASSERT_COMPATIBLE_ALLOCATOR(
642      __alloc_traits::propagate_on_container_swap::value || this->__node_alloc_ == __c.__node_alloc_,
643      "list::swap: Either propagate_on_container_swap must be true"
644      " or the allocators must compare equal");
645  using std::swap;
646  std::__swap_allocator(__node_alloc_, __c.__node_alloc_);
647  swap(__size_, __c.__size_);
648  swap(__end_, __c.__end_);
649  if (__size_ == 0)
650    __end_.__next_ = __end_.__prev_ = __end_as_link();
651  else
652    __end_.__prev_->__next_ = __end_.__next_->__prev_ = __end_as_link();
653  if (__c.__size_ == 0)
654    __c.__end_.__next_ = __c.__end_.__prev_ = __c.__end_as_link();
655  else
656    __c.__end_.__prev_->__next_ = __c.__end_.__next_->__prev_ = __c.__end_as_link();
657}
658
659template <class _Tp, class _Alloc /*= allocator<_Tp>*/>
660class _LIBCPP_TEMPLATE_VIS list : private __list_imp<_Tp, _Alloc> {
661  typedef __list_imp<_Tp, _Alloc> __base;
662  typedef typename __base::__node_type __node_type;
663  typedef typename __base::__node_allocator __node_allocator;
664  typedef typename __base::__node_pointer __node_pointer;
665  typedef typename __base::__node_alloc_traits __node_alloc_traits;
666  typedef typename __base::__node_base __node_base;
667  typedef typename __base::__node_base_pointer __node_base_pointer;
668  typedef typename __base::__base_pointer __base_pointer;
669
670public:
671  typedef _Tp value_type;
672  typedef _Alloc allocator_type;
673  static_assert(__check_valid_allocator<allocator_type>::value);
674  static_assert(is_same<value_type, typename allocator_type::value_type>::value,
675                "Allocator::value_type must be same type as value_type");
676  typedef value_type& reference;
677  typedef const value_type& const_reference;
678  typedef typename __base::pointer pointer;
679  typedef typename __base::const_pointer const_pointer;
680  typedef typename __base::size_type size_type;
681  typedef typename __base::difference_type difference_type;
682  typedef typename __base::iterator iterator;
683  typedef typename __base::const_iterator const_iterator;
684  typedef std::reverse_iterator<iterator> reverse_iterator;
685  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
686#if _LIBCPP_STD_VER >= 20
687  typedef size_type __remove_return_type;
688#else
689  typedef void __remove_return_type;
690#endif
691
692  _LIBCPP_HIDE_FROM_ABI list() _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value) {}
693  _LIBCPP_HIDE_FROM_ABI explicit list(const allocator_type& __a) : __base(__a) {}
694  _LIBCPP_HIDE_FROM_ABI explicit list(size_type __n);
695#if _LIBCPP_STD_VER >= 14
696  _LIBCPP_HIDE_FROM_ABI explicit list(size_type __n, const allocator_type& __a);
697#endif
698  _LIBCPP_HIDE_FROM_ABI list(size_type __n, const value_type& __x);
699  template <__enable_if_t<__is_allocator<_Alloc>::value, int> = 0>
700  _LIBCPP_HIDE_FROM_ABI list(size_type __n, const value_type& __x, const allocator_type& __a) : __base(__a) {
701    for (; __n > 0; --__n)
702      push_back(__x);
703  }
704
705  template <class _InpIter, __enable_if_t<__has_input_iterator_category<_InpIter>::value, int> = 0>
706  _LIBCPP_HIDE_FROM_ABI list(_InpIter __f, _InpIter __l);
707
708  template <class _InpIter, __enable_if_t<__has_input_iterator_category<_InpIter>::value, int> = 0>
709  _LIBCPP_HIDE_FROM_ABI list(_InpIter __f, _InpIter __l, const allocator_type& __a);
710
711#if _LIBCPP_STD_VER >= 23
712  template <_ContainerCompatibleRange<_Tp> _Range>
713  _LIBCPP_HIDE_FROM_ABI list(from_range_t, _Range&& __range, const allocator_type& __a = allocator_type())
714      : __base(__a) {
715    prepend_range(std::forward<_Range>(__range));
716  }
717#endif
718
719  _LIBCPP_HIDE_FROM_ABI list(const list& __c);
720  _LIBCPP_HIDE_FROM_ABI list(const list& __c, const __type_identity_t<allocator_type>& __a);
721  _LIBCPP_HIDE_FROM_ABI list& operator=(const list& __c);
722#ifndef _LIBCPP_CXX03_LANG
723  _LIBCPP_HIDE_FROM_ABI list(initializer_list<value_type> __il);
724  _LIBCPP_HIDE_FROM_ABI list(initializer_list<value_type> __il, const allocator_type& __a);
725
726  _LIBCPP_HIDE_FROM_ABI list(list&& __c) _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value);
727  _LIBCPP_HIDE_FROM_ABI list(list&& __c, const __type_identity_t<allocator_type>& __a);
728  _LIBCPP_HIDE_FROM_ABI list& operator=(list&& __c)
729      _NOEXCEPT_(__node_alloc_traits::propagate_on_container_move_assignment::value&&
730                     is_nothrow_move_assignable<__node_allocator>::value);
731
732  _LIBCPP_HIDE_FROM_ABI list& operator=(initializer_list<value_type> __il) {
733    assign(__il.begin(), __il.end());
734    return *this;
735  }
736
737  _LIBCPP_HIDE_FROM_ABI void assign(initializer_list<value_type> __il) { assign(__il.begin(), __il.end()); }
738#endif // _LIBCPP_CXX03_LANG
739
740  template <class _InpIter, __enable_if_t<__has_input_iterator_category<_InpIter>::value, int> = 0>
741  _LIBCPP_HIDE_FROM_ABI void assign(_InpIter __f, _InpIter __l);
742
743#if _LIBCPP_STD_VER >= 23
744  template <_ContainerCompatibleRange<_Tp> _Range>
745  _LIBCPP_HIDE_FROM_ABI void assign_range(_Range&& __range) {
746    __assign_with_sentinel(ranges::begin(__range), ranges::end(__range));
747  }
748#endif
749
750  _LIBCPP_HIDE_FROM_ABI void assign(size_type __n, const value_type& __x);
751
752  _LIBCPP_HIDE_FROM_ABI allocator_type get_allocator() const _NOEXCEPT;
753
754  _LIBCPP_HIDE_FROM_ABI size_type size() const _NOEXCEPT { return this->__size_; }
755  [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool empty() const _NOEXCEPT { return __base::empty(); }
756  _LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT {
757    return std::min<size_type>(this->__node_alloc_max_size(), numeric_limits<difference_type >::max());
758  }
759
760  _LIBCPP_HIDE_FROM_ABI iterator begin() _NOEXCEPT { return __base::begin(); }
761  _LIBCPP_HIDE_FROM_ABI const_iterator begin() const _NOEXCEPT { return __base::begin(); }
762  _LIBCPP_HIDE_FROM_ABI iterator end() _NOEXCEPT { return __base::end(); }
763  _LIBCPP_HIDE_FROM_ABI const_iterator end() const _NOEXCEPT { return __base::end(); }
764  _LIBCPP_HIDE_FROM_ABI const_iterator cbegin() const _NOEXCEPT { return __base::begin(); }
765  _LIBCPP_HIDE_FROM_ABI const_iterator cend() const _NOEXCEPT { return __base::end(); }
766
767  _LIBCPP_HIDE_FROM_ABI reverse_iterator rbegin() _NOEXCEPT { return reverse_iterator(end()); }
768  _LIBCPP_HIDE_FROM_ABI const_reverse_iterator rbegin() const _NOEXCEPT { return const_reverse_iterator(end()); }
769  _LIBCPP_HIDE_FROM_ABI reverse_iterator rend() _NOEXCEPT { return reverse_iterator(begin()); }
770  _LIBCPP_HIDE_FROM_ABI const_reverse_iterator rend() const _NOEXCEPT { return const_reverse_iterator(begin()); }
771  _LIBCPP_HIDE_FROM_ABI const_reverse_iterator crbegin() const _NOEXCEPT { return const_reverse_iterator(end()); }
772  _LIBCPP_HIDE_FROM_ABI const_reverse_iterator crend() const _NOEXCEPT { return const_reverse_iterator(begin()); }
773
774  _LIBCPP_HIDE_FROM_ABI reference front() {
775    _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "list::front called on empty list");
776    return __base::__end_.__next_->__as_node()->__get_value();
777  }
778  _LIBCPP_HIDE_FROM_ABI const_reference front() const {
779    _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "list::front called on empty list");
780    return __base::__end_.__next_->__as_node()->__get_value();
781  }
782  _LIBCPP_HIDE_FROM_ABI reference back() {
783    _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "list::back called on empty list");
784    return __base::__end_.__prev_->__as_node()->__get_value();
785  }
786  _LIBCPP_HIDE_FROM_ABI const_reference back() const {
787    _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "list::back called on empty list");
788    return __base::__end_.__prev_->__as_node()->__get_value();
789  }
790
791#ifndef _LIBCPP_CXX03_LANG
792  _LIBCPP_HIDE_FROM_ABI void push_front(value_type&& __x);
793  _LIBCPP_HIDE_FROM_ABI void push_back(value_type&& __x);
794
795#  if _LIBCPP_STD_VER >= 23
796  template <_ContainerCompatibleRange<_Tp> _Range>
797  _LIBCPP_HIDE_FROM_ABI void prepend_range(_Range&& __range) {
798    insert_range(begin(), std::forward<_Range>(__range));
799  }
800
801  template <_ContainerCompatibleRange<_Tp> _Range>
802  _LIBCPP_HIDE_FROM_ABI void append_range(_Range&& __range) {
803    insert_range(end(), std::forward<_Range>(__range));
804  }
805#  endif
806
807  template <class... _Args>
808#  if _LIBCPP_STD_VER >= 17
809  _LIBCPP_HIDE_FROM_ABI reference emplace_front(_Args&&... __args);
810#  else
811  _LIBCPP_HIDE_FROM_ABI void emplace_front(_Args&&... __args);
812#  endif
813  template <class... _Args>
814#  if _LIBCPP_STD_VER >= 17
815  _LIBCPP_HIDE_FROM_ABI reference emplace_back(_Args&&... __args);
816#  else
817  _LIBCPP_HIDE_FROM_ABI void emplace_back(_Args&&... __args);
818#  endif
819  template <class... _Args>
820  _LIBCPP_HIDE_FROM_ABI iterator emplace(const_iterator __p, _Args&&... __args);
821
822  _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, value_type&& __x);
823
824  _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, initializer_list<value_type> __il) {
825    return insert(__p, __il.begin(), __il.end());
826  }
827#endif // _LIBCPP_CXX03_LANG
828
829  _LIBCPP_HIDE_FROM_ABI void push_front(const value_type& __x);
830  _LIBCPP_HIDE_FROM_ABI void push_back(const value_type& __x);
831
832#ifndef _LIBCPP_CXX03_LANG
833  template <class _Arg>
834  _LIBCPP_HIDE_FROM_ABI void __emplace_back(_Arg&& __arg) {
835    emplace_back(std::forward<_Arg>(__arg));
836  }
837#else
838  _LIBCPP_HIDE_FROM_ABI void __emplace_back(value_type const& __arg) { push_back(__arg); }
839#endif
840
841  _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, const value_type& __x);
842  _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, size_type __n, const value_type& __x);
843
844  template <class _InpIter, __enable_if_t<__has_input_iterator_category<_InpIter>::value, int> = 0>
845  _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, _InpIter __f, _InpIter __l);
846
847#if _LIBCPP_STD_VER >= 23
848  template <_ContainerCompatibleRange<_Tp> _Range>
849  _LIBCPP_HIDE_FROM_ABI iterator insert_range(const_iterator __position, _Range&& __range) {
850    return __insert_with_sentinel(__position, ranges::begin(__range), ranges::end(__range));
851  }
852#endif
853
854  _LIBCPP_HIDE_FROM_ABI void swap(list& __c)
855#if _LIBCPP_STD_VER >= 14
856      _NOEXCEPT
857#else
858      _NOEXCEPT_(!__node_alloc_traits::propagate_on_container_swap::value || __is_nothrow_swappable_v<__node_allocator>)
859#endif
860  {
861    __base::swap(__c);
862  }
863  _LIBCPP_HIDE_FROM_ABI void clear() _NOEXCEPT { __base::clear(); }
864
865  _LIBCPP_HIDE_FROM_ABI void pop_front();
866  _LIBCPP_HIDE_FROM_ABI void pop_back();
867
868  _LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __p);
869  _LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __f, const_iterator __l);
870
871  _LIBCPP_HIDE_FROM_ABI void resize(size_type __n);
872  _LIBCPP_HIDE_FROM_ABI void resize(size_type __n, const value_type& __x);
873
874  _LIBCPP_HIDE_FROM_ABI void splice(const_iterator __p, list& __c);
875#ifndef _LIBCPP_CXX03_LANG
876  _LIBCPP_HIDE_FROM_ABI void splice(const_iterator __p, list&& __c) { splice(__p, __c); }
877  _LIBCPP_HIDE_FROM_ABI void splice(const_iterator __p, list&& __c, const_iterator __i) { splice(__p, __c, __i); }
878  _LIBCPP_HIDE_FROM_ABI void splice(const_iterator __p, list&& __c, const_iterator __f, const_iterator __l) {
879    splice(__p, __c, __f, __l);
880  }
881#endif
882  _LIBCPP_HIDE_FROM_ABI void splice(const_iterator __p, list& __c, const_iterator __i);
883  _LIBCPP_HIDE_FROM_ABI void splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l);
884
885  _LIBCPP_HIDE_FROM_ABI __remove_return_type remove(const value_type& __x);
886  template <class _Pred>
887  _LIBCPP_HIDE_FROM_ABI __remove_return_type remove_if(_Pred __pred);
888  _LIBCPP_HIDE_FROM_ABI __remove_return_type unique() { return unique(__equal_to()); }
889  template <class _BinaryPred>
890  _LIBCPP_HIDE_FROM_ABI __remove_return_type unique(_BinaryPred __binary_pred);
891  _LIBCPP_HIDE_FROM_ABI void merge(list& __c);
892#ifndef _LIBCPP_CXX03_LANG
893  _LIBCPP_HIDE_FROM_ABI void merge(list&& __c) { merge(__c); }
894
895  template <class _Comp>
896  _LIBCPP_HIDE_FROM_ABI void merge(list&& __c, _Comp __comp) {
897    merge(__c, __comp);
898  }
899#endif
900  template <class _Comp>
901  _LIBCPP_HIDE_FROM_ABI void merge(list& __c, _Comp __comp);
902
903  _LIBCPP_HIDE_FROM_ABI void sort();
904  template <class _Comp>
905  _LIBCPP_HIDE_FROM_ABI void sort(_Comp __comp);
906
907  _LIBCPP_HIDE_FROM_ABI void reverse() _NOEXCEPT;
908
909  _LIBCPP_HIDE_FROM_ABI bool __invariants() const;
910
911private:
912  template <class _Iterator, class _Sentinel>
913  _LIBCPP_HIDE_FROM_ABI void __assign_with_sentinel(_Iterator __f, _Sentinel __l);
914
915  template <class _Iterator, class _Sentinel>
916  _LIBCPP_HIDE_FROM_ABI iterator __insert_with_sentinel(const_iterator __p, _Iterator __f, _Sentinel __l);
917
918  _LIBCPP_HIDE_FROM_ABI static void __link_nodes(__base_pointer __p, __base_pointer __f, __base_pointer __l);
919  _LIBCPP_HIDE_FROM_ABI void __link_nodes_at_front(__base_pointer __f, __base_pointer __l);
920  _LIBCPP_HIDE_FROM_ABI void __link_nodes_at_back(__base_pointer __f, __base_pointer __l);
921  _LIBCPP_HIDE_FROM_ABI iterator __iterator(size_type __n);
922  // TODO: Make this _LIBCPP_HIDE_FROM_ABI
923  template <class _Comp>
924  _LIBCPP_HIDDEN static iterator __sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp);
925
926  _LIBCPP_HIDE_FROM_ABI void __move_assign(list& __c, true_type)
927      _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value);
928  _LIBCPP_HIDE_FROM_ABI void __move_assign(list& __c, false_type);
929};
930
931#if _LIBCPP_STD_VER >= 17
932template <class _InputIterator,
933          class _Alloc = allocator<__iter_value_type<_InputIterator>>,
934          class        = enable_if_t<__has_input_iterator_category<_InputIterator>::value>,
935          class        = enable_if_t<__is_allocator<_Alloc>::value> >
936list(_InputIterator, _InputIterator) -> list<__iter_value_type<_InputIterator>, _Alloc>;
937
938template <class _InputIterator,
939          class _Alloc,
940          class = enable_if_t<__has_input_iterator_category<_InputIterator>::value>,
941          class = enable_if_t<__is_allocator<_Alloc>::value> >
942list(_InputIterator, _InputIterator, _Alloc) -> list<__iter_value_type<_InputIterator>, _Alloc>;
943#endif
944
945#if _LIBCPP_STD_VER >= 23
946template <ranges::input_range _Range,
947          class _Alloc = allocator<ranges::range_value_t<_Range>>,
948          class        = enable_if_t<__is_allocator<_Alloc>::value> >
949list(from_range_t, _Range&&, _Alloc = _Alloc()) -> list<ranges::range_value_t<_Range>, _Alloc>;
950#endif
951
952// Link in nodes [__f, __l] just prior to __p
953template <class _Tp, class _Alloc>
954inline void list<_Tp, _Alloc>::__link_nodes(__base_pointer __p, __base_pointer __f, __base_pointer __l) {
955  __p->__prev_->__next_ = __f;
956  __f->__prev_          = __p->__prev_;
957  __p->__prev_          = __l;
958  __l->__next_          = __p;
959}
960
961// Link in nodes [__f, __l] at the front of the list
962template <class _Tp, class _Alloc>
963inline void list<_Tp, _Alloc>::__link_nodes_at_front(__base_pointer __f, __base_pointer __l) {
964  __f->__prev_           = __base::__end_as_link();
965  __l->__next_           = __base::__end_.__next_;
966  __l->__next_->__prev_  = __l;
967  __base::__end_.__next_ = __f;
968}
969
970// Link in nodes [__f, __l] at the back of the list
971template <class _Tp, class _Alloc>
972inline void list<_Tp, _Alloc>::__link_nodes_at_back(__base_pointer __f, __base_pointer __l) {
973  __l->__next_           = __base::__end_as_link();
974  __f->__prev_           = __base::__end_.__prev_;
975  __f->__prev_->__next_  = __f;
976  __base::__end_.__prev_ = __l;
977}
978
979template <class _Tp, class _Alloc>
980inline typename list<_Tp, _Alloc>::iterator list<_Tp, _Alloc>::__iterator(size_type __n) {
981  return __n <= this->__size_ / 2 ? std::next(begin(), __n) : std::prev(end(), this->__size_ - __n);
982}
983
984template <class _Tp, class _Alloc>
985list<_Tp, _Alloc>::list(size_type __n) {
986  for (; __n > 0; --__n)
987#ifndef _LIBCPP_CXX03_LANG
988    emplace_back();
989#else
990    push_back(value_type());
991#endif
992}
993
994#if _LIBCPP_STD_VER >= 14
995template <class _Tp, class _Alloc>
996list<_Tp, _Alloc>::list(size_type __n, const allocator_type& __a) : __base(__a) {
997  for (; __n > 0; --__n)
998    emplace_back();
999}
1000#endif
1001
1002template <class _Tp, class _Alloc>
1003list<_Tp, _Alloc>::list(size_type __n, const value_type& __x) {
1004  for (; __n > 0; --__n)
1005    push_back(__x);
1006}
1007
1008template <class _Tp, class _Alloc>
1009template <class _InpIter, __enable_if_t<__has_input_iterator_category<_InpIter>::value, int> >
1010list<_Tp, _Alloc>::list(_InpIter __f, _InpIter __l) {
1011  for (; __f != __l; ++__f)
1012    __emplace_back(*__f);
1013}
1014
1015template <class _Tp, class _Alloc>
1016template <class _InpIter, __enable_if_t<__has_input_iterator_category<_InpIter>::value, int> >
1017list<_Tp, _Alloc>::list(_InpIter __f, _InpIter __l, const allocator_type& __a) : __base(__a) {
1018  for (; __f != __l; ++__f)
1019    __emplace_back(*__f);
1020}
1021
1022template <class _Tp, class _Alloc>
1023list<_Tp, _Alloc>::list(const list& __c)
1024    : __base(__node_alloc_traits::select_on_container_copy_construction(__c.__node_alloc_)) {
1025  for (const_iterator __i = __c.begin(), __e = __c.end(); __i != __e; ++__i)
1026    push_back(*__i);
1027}
1028
1029template <class _Tp, class _Alloc>
1030list<_Tp, _Alloc>::list(const list& __c, const __type_identity_t<allocator_type>& __a) : __base(__a) {
1031  for (const_iterator __i = __c.begin(), __e = __c.end(); __i != __e; ++__i)
1032    push_back(*__i);
1033}
1034
1035#ifndef _LIBCPP_CXX03_LANG
1036
1037template <class _Tp, class _Alloc>
1038list<_Tp, _Alloc>::list(initializer_list<value_type> __il, const allocator_type& __a) : __base(__a) {
1039  for (typename initializer_list<value_type>::const_iterator __i = __il.begin(), __e = __il.end(); __i != __e; ++__i)
1040    push_back(*__i);
1041}
1042
1043template <class _Tp, class _Alloc>
1044list<_Tp, _Alloc>::list(initializer_list<value_type> __il) {
1045  for (typename initializer_list<value_type>::const_iterator __i = __il.begin(), __e = __il.end(); __i != __e; ++__i)
1046    push_back(*__i);
1047}
1048
1049template <class _Tp, class _Alloc>
1050inline list<_Tp, _Alloc>::list(list&& __c) noexcept(is_nothrow_move_constructible<__node_allocator>::value)
1051    : __base(std::move(__c.__node_alloc_)) {
1052  splice(end(), __c);
1053}
1054
1055template <class _Tp, class _Alloc>
1056inline list<_Tp, _Alloc>::list(list&& __c, const __type_identity_t<allocator_type>& __a) : __base(__a) {
1057  if (__a == __c.get_allocator())
1058    splice(end(), __c);
1059  else {
1060    typedef move_iterator<iterator> _Ip;
1061    assign(_Ip(__c.begin()), _Ip(__c.end()));
1062  }
1063}
1064
1065template <class _Tp, class _Alloc>
1066inline list<_Tp, _Alloc>& list<_Tp, _Alloc>::operator=(list&& __c) noexcept(
1067    __node_alloc_traits::propagate_on_container_move_assignment::value &&
1068    is_nothrow_move_assignable<__node_allocator>::value) {
1069  __move_assign(__c, integral_constant<bool, __node_alloc_traits::propagate_on_container_move_assignment::value>());
1070  return *this;
1071}
1072
1073template <class _Tp, class _Alloc>
1074void list<_Tp, _Alloc>::__move_assign(list& __c, false_type) {
1075  if (this->__node_alloc_ != __c.__node_alloc_) {
1076    typedef move_iterator<iterator> _Ip;
1077    assign(_Ip(__c.begin()), _Ip(__c.end()));
1078  } else
1079    __move_assign(__c, true_type());
1080}
1081
1082template <class _Tp, class _Alloc>
1083void list<_Tp, _Alloc>::__move_assign(list& __c,
1084                                      true_type) noexcept(is_nothrow_move_assignable<__node_allocator>::value) {
1085  clear();
1086  __base::__move_assign_alloc(__c);
1087  splice(end(), __c);
1088}
1089
1090#endif // _LIBCPP_CXX03_LANG
1091
1092template <class _Tp, class _Alloc>
1093inline list<_Tp, _Alloc>& list<_Tp, _Alloc>::operator=(const list& __c) {
1094  if (this != std::addressof(__c)) {
1095    __base::__copy_assign_alloc(__c);
1096    assign(__c.begin(), __c.end());
1097  }
1098  return *this;
1099}
1100
1101template <class _Tp, class _Alloc>
1102template <class _InpIter, __enable_if_t<__has_input_iterator_category<_InpIter>::value, int> >
1103void list<_Tp, _Alloc>::assign(_InpIter __f, _InpIter __l) {
1104  __assign_with_sentinel(__f, __l);
1105}
1106
1107template <class _Tp, class _Alloc>
1108template <class _Iterator, class _Sentinel>
1109_LIBCPP_HIDE_FROM_ABI void list<_Tp, _Alloc>::__assign_with_sentinel(_Iterator __f, _Sentinel __l) {
1110  iterator __i = begin();
1111  iterator __e = end();
1112  for (; __f != __l && __i != __e; ++__f, (void)++__i)
1113    *__i = *__f;
1114  if (__i == __e)
1115    __insert_with_sentinel(__e, std::move(__f), std::move(__l));
1116  else
1117    erase(__i, __e);
1118}
1119
1120template <class _Tp, class _Alloc>
1121void list<_Tp, _Alloc>::assign(size_type __n, const value_type& __x) {
1122  iterator __i = begin();
1123  iterator __e = end();
1124  for (; __n > 0 && __i != __e; --__n, (void)++__i)
1125    *__i = __x;
1126  if (__i == __e)
1127    insert(__e, __n, __x);
1128  else
1129    erase(__i, __e);
1130}
1131
1132template <class _Tp, class _Alloc>
1133inline _Alloc list<_Tp, _Alloc>::get_allocator() const _NOEXCEPT {
1134  return allocator_type(this->__node_alloc_);
1135}
1136
1137template <class _Tp, class _Alloc>
1138typename list<_Tp, _Alloc>::iterator list<_Tp, _Alloc>::insert(const_iterator __p, const value_type& __x) {
1139  __node_pointer __node = this->__create_node(/* prev = */ nullptr, /* next = */ nullptr, __x);
1140  __link_nodes(__p.__ptr_, __node->__as_link(), __node->__as_link());
1141  ++this->__size_;
1142  return iterator(__node->__as_link());
1143}
1144
1145template <class _Tp, class _Alloc>
1146typename list<_Tp, _Alloc>::iterator
1147list<_Tp, _Alloc>::insert(const_iterator __p, size_type __n, const value_type& __x) {
1148  iterator __r(__p.__ptr_);
1149  if (__n > 0) {
1150    size_type __ds        = 0;
1151    __node_pointer __node = this->__create_node(/* prev = */ nullptr, /* next = */ nullptr, __x);
1152    ++__ds;
1153    __r          = iterator(__node->__as_link());
1154    iterator __e = __r;
1155#if _LIBCPP_HAS_EXCEPTIONS
1156    try {
1157#endif // _LIBCPP_HAS_EXCEPTIONS
1158      for (--__n; __n != 0; --__n, (void)++__e, ++__ds) {
1159        __e.__ptr_->__next_ = this->__create_node(/* prev = */ __e.__ptr_, /* next = */ nullptr, __x)->__as_link();
1160      }
1161#if _LIBCPP_HAS_EXCEPTIONS
1162    } catch (...) {
1163      while (true) {
1164        __base_pointer __prev    = __e.__ptr_->__prev_;
1165        __node_pointer __current = __e.__ptr_->__as_node();
1166        this->__delete_node(__current);
1167        if (__prev == 0)
1168          break;
1169        __e = iterator(__prev);
1170      }
1171      throw;
1172    }
1173#endif // _LIBCPP_HAS_EXCEPTIONS
1174    __link_nodes(__p.__ptr_, __r.__ptr_, __e.__ptr_);
1175    this->__size_ += __ds;
1176  }
1177  return __r;
1178}
1179
1180template <class _Tp, class _Alloc>
1181template <class _InpIter, __enable_if_t<__has_input_iterator_category<_InpIter>::value, int> >
1182typename list<_Tp, _Alloc>::iterator list<_Tp, _Alloc>::insert(const_iterator __p, _InpIter __f, _InpIter __l) {
1183  return __insert_with_sentinel(__p, __f, __l);
1184}
1185
1186template <class _Tp, class _Alloc>
1187template <class _Iterator, class _Sentinel>
1188_LIBCPP_HIDE_FROM_ABI typename list<_Tp, _Alloc>::iterator
1189list<_Tp, _Alloc>::__insert_with_sentinel(const_iterator __p, _Iterator __f, _Sentinel __l) {
1190  iterator __r(__p.__ptr_);
1191  if (__f != __l) {
1192    size_type __ds        = 0;
1193    __node_pointer __node = this->__create_node(/* prev = */ nullptr, /* next = */ nullptr, *__f);
1194    ++__ds;
1195    __r          = iterator(__node->__as_link());
1196    iterator __e = __r;
1197#if _LIBCPP_HAS_EXCEPTIONS
1198    try {
1199#endif // _LIBCPP_HAS_EXCEPTIONS
1200      for (++__f; __f != __l; ++__f, (void)++__e, ++__ds) {
1201        __e.__ptr_->__next_ = this->__create_node(/* prev = */ __e.__ptr_, /* next = */ nullptr, *__f)->__as_link();
1202      }
1203#if _LIBCPP_HAS_EXCEPTIONS
1204    } catch (...) {
1205      while (true) {
1206        __base_pointer __prev    = __e.__ptr_->__prev_;
1207        __node_pointer __current = __e.__ptr_->__as_node();
1208        this->__delete_node(__current);
1209        if (__prev == 0)
1210          break;
1211        __e = iterator(__prev);
1212      }
1213      throw;
1214    }
1215#endif // _LIBCPP_HAS_EXCEPTIONS
1216    __link_nodes(__p.__ptr_, __r.__ptr_, __e.__ptr_);
1217    this->__size_ += __ds;
1218  }
1219  return __r;
1220}
1221
1222template <class _Tp, class _Alloc>
1223void list<_Tp, _Alloc>::push_front(const value_type& __x) {
1224  __node_pointer __node = this->__create_node(/* prev = */ nullptr, /* next = */ nullptr, __x);
1225  __base_pointer __nl   = __node->__as_link();
1226  __link_nodes_at_front(__nl, __nl);
1227  ++this->__size_;
1228}
1229
1230template <class _Tp, class _Alloc>
1231void list<_Tp, _Alloc>::push_back(const value_type& __x) {
1232  __node_pointer __node = this->__create_node(/* prev = */ nullptr, /* next = */ nullptr, __x);
1233  __base_pointer __nl   = __node->__as_link();
1234  __link_nodes_at_back(__nl, __nl);
1235  ++this->__size_;
1236}
1237
1238#ifndef _LIBCPP_CXX03_LANG
1239
1240template <class _Tp, class _Alloc>
1241void list<_Tp, _Alloc>::push_front(value_type&& __x) {
1242  __node_pointer __node = this->__create_node(/* prev = */ nullptr, /* next = */ nullptr, std::move(__x));
1243  __base_pointer __nl   = __node->__as_link();
1244  __link_nodes_at_front(__nl, __nl);
1245  ++this->__size_;
1246}
1247
1248template <class _Tp, class _Alloc>
1249void list<_Tp, _Alloc>::push_back(value_type&& __x) {
1250  __node_pointer __node = this->__create_node(/* prev = */ nullptr, /* next = */ nullptr, std::move(__x));
1251  __base_pointer __nl   = __node->__as_link();
1252  __link_nodes_at_back(__nl, __nl);
1253  ++this->__size_;
1254}
1255
1256template <class _Tp, class _Alloc>
1257template <class... _Args>
1258#  if _LIBCPP_STD_VER >= 17
1259typename list<_Tp, _Alloc>::reference
1260#  else
1261void
1262#  endif
1263list<_Tp, _Alloc>::emplace_front(_Args&&... __args) {
1264  __node_pointer __node =
1265      this->__create_node(/* prev = */ nullptr, /* next = */ nullptr, std::forward<_Args>(__args)...);
1266  __base_pointer __nl = __node->__as_link();
1267  __link_nodes_at_front(__nl, __nl);
1268  ++this->__size_;
1269#  if _LIBCPP_STD_VER >= 17
1270  return __node->__get_value();
1271#  endif
1272}
1273
1274template <class _Tp, class _Alloc>
1275template <class... _Args>
1276#  if _LIBCPP_STD_VER >= 17
1277typename list<_Tp, _Alloc>::reference
1278#  else
1279void
1280#  endif
1281list<_Tp, _Alloc>::emplace_back(_Args&&... __args) {
1282  __node_pointer __node =
1283      this->__create_node(/* prev = */ nullptr, /* next = */ nullptr, std::forward<_Args>(__args)...);
1284  __base_pointer __nl = __node->__as_link();
1285  __link_nodes_at_back(__nl, __nl);
1286  ++this->__size_;
1287#  if _LIBCPP_STD_VER >= 17
1288  return __node->__get_value();
1289#  endif
1290}
1291
1292template <class _Tp, class _Alloc>
1293template <class... _Args>
1294typename list<_Tp, _Alloc>::iterator list<_Tp, _Alloc>::emplace(const_iterator __p, _Args&&... __args) {
1295  __node_pointer __node =
1296      this->__create_node(/* prev = */ nullptr, /* next = */ nullptr, std::forward<_Args>(__args)...);
1297  __base_pointer __nl = __node->__as_link();
1298  __link_nodes(__p.__ptr_, __nl, __nl);
1299  ++this->__size_;
1300  return iterator(__nl);
1301}
1302
1303template <class _Tp, class _Alloc>
1304typename list<_Tp, _Alloc>::iterator list<_Tp, _Alloc>::insert(const_iterator __p, value_type&& __x) {
1305  __node_pointer __node = this->__create_node(/* prev = */ nullptr, /* next = */ nullptr, std::move(__x));
1306  __base_pointer __nl   = __node->__as_link();
1307  __link_nodes(__p.__ptr_, __nl, __nl);
1308  ++this->__size_;
1309  return iterator(__nl);
1310}
1311
1312#endif // _LIBCPP_CXX03_LANG
1313
1314template <class _Tp, class _Alloc>
1315void list<_Tp, _Alloc>::pop_front() {
1316  _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "list::pop_front() called with empty list");
1317  __base_pointer __n = __base::__end_.__next_;
1318  __base::__unlink_nodes(__n, __n);
1319  --this->__size_;
1320  this->__delete_node(__n->__as_node());
1321}
1322
1323template <class _Tp, class _Alloc>
1324void list<_Tp, _Alloc>::pop_back() {
1325  _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "list::pop_back() called on an empty list");
1326  __base_pointer __n = __base::__end_.__prev_;
1327  __base::__unlink_nodes(__n, __n);
1328  --this->__size_;
1329  this->__delete_node(__n->__as_node());
1330}
1331
1332template <class _Tp, class _Alloc>
1333typename list<_Tp, _Alloc>::iterator list<_Tp, _Alloc>::erase(const_iterator __p) {
1334  _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__p != end(), "list::erase(iterator) called with a non-dereferenceable iterator");
1335  __base_pointer __n = __p.__ptr_;
1336  __base_pointer __r = __n->__next_;
1337  __base::__unlink_nodes(__n, __n);
1338  --this->__size_;
1339  this->__delete_node(__n->__as_node());
1340  return iterator(__r);
1341}
1342
1343template <class _Tp, class _Alloc>
1344typename list<_Tp, _Alloc>::iterator list<_Tp, _Alloc>::erase(const_iterator __f, const_iterator __l) {
1345  if (__f != __l) {
1346    __base::__unlink_nodes(__f.__ptr_, __l.__ptr_->__prev_);
1347    while (__f != __l) {
1348      __base_pointer __n = __f.__ptr_;
1349      ++__f;
1350      --this->__size_;
1351      this->__delete_node(__n->__as_node());
1352    }
1353  }
1354  return iterator(__l.__ptr_);
1355}
1356
1357template <class _Tp, class _Alloc>
1358void list<_Tp, _Alloc>::resize(size_type __n) {
1359  if (__n < this->__size_)
1360    erase(__iterator(__n), end());
1361  else if (__n > this->__size_) {
1362    __n -= this->__size_;
1363    size_type __ds        = 0;
1364    __node_pointer __node = this->__create_node(/* prev = */ nullptr, /* next = */ nullptr);
1365    ++__ds;
1366    iterator __r = iterator(__node->__as_link());
1367    iterator __e = __r;
1368#if _LIBCPP_HAS_EXCEPTIONS
1369    try {
1370#endif // _LIBCPP_HAS_EXCEPTIONS
1371      for (--__n; __n != 0; --__n, (void)++__e, ++__ds) {
1372        __e.__ptr_->__next_ = this->__create_node(/* prev = */ __e.__ptr_, /* next = */ nullptr)->__as_link();
1373      }
1374#if _LIBCPP_HAS_EXCEPTIONS
1375    } catch (...) {
1376      while (true) {
1377        __base_pointer __prev    = __e.__ptr_->__prev_;
1378        __node_pointer __current = __e.__ptr_->__as_node();
1379        this->__delete_node(__current);
1380        if (__prev == 0)
1381          break;
1382        __e = iterator(__prev);
1383      }
1384      throw;
1385    }
1386#endif // _LIBCPP_HAS_EXCEPTIONS
1387    __link_nodes_at_back(__r.__ptr_, __e.__ptr_);
1388    this->__size_ += __ds;
1389  }
1390}
1391
1392template <class _Tp, class _Alloc>
1393void list<_Tp, _Alloc>::resize(size_type __n, const value_type& __x) {
1394  if (__n < this->__size_)
1395    erase(__iterator(__n), end());
1396  else if (__n > this->__size_) {
1397    __n -= this->__size_;
1398    size_type __ds        = 0;
1399    __node_pointer __node = this->__create_node(/* prev = */ nullptr, /* next = */ nullptr, __x);
1400    ++__ds;
1401    __base_pointer __nl = __node->__as_link();
1402    iterator __r        = iterator(__nl);
1403    iterator __e        = __r;
1404#if _LIBCPP_HAS_EXCEPTIONS
1405    try {
1406#endif // _LIBCPP_HAS_EXCEPTIONS
1407      for (--__n; __n != 0; --__n, (void)++__e, ++__ds) {
1408        __e.__ptr_->__next_ = this->__create_node(/* prev = */ __e.__ptr_, /* next = */ nullptr, __x)->__as_link();
1409      }
1410#if _LIBCPP_HAS_EXCEPTIONS
1411    } catch (...) {
1412      while (true) {
1413        __base_pointer __prev    = __e.__ptr_->__prev_;
1414        __node_pointer __current = __e.__ptr_->__as_node();
1415        this->__delete_node(__current);
1416        if (__prev == 0)
1417          break;
1418        __e = iterator(__prev);
1419      }
1420      throw;
1421    }
1422#endif // _LIBCPP_HAS_EXCEPTIONS
1423    __link_nodes(__base::__end_as_link(), __r.__ptr_, __e.__ptr_);
1424    this->__size_ += __ds;
1425  }
1426}
1427
1428template <class _Tp, class _Alloc>
1429void list<_Tp, _Alloc>::splice(const_iterator __p, list& __c) {
1430  _LIBCPP_ASSERT_VALID_INPUT_RANGE(
1431      this != std::addressof(__c), "list::splice(iterator, list) called with this == &list");
1432  if (!__c.empty()) {
1433    __base_pointer __f = __c.__end_.__next_;
1434    __base_pointer __l = __c.__end_.__prev_;
1435    __base::__unlink_nodes(__f, __l);
1436    __link_nodes(__p.__ptr_, __f, __l);
1437    this->__size_ += __c.__size_;
1438    __c.__size_ = 0;
1439  }
1440}
1441
1442template <class _Tp, class _Alloc>
1443void list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __i) {
1444  if (__p.__ptr_ != __i.__ptr_ && __p.__ptr_ != __i.__ptr_->__next_) {
1445    __base_pointer __f = __i.__ptr_;
1446    __base::__unlink_nodes(__f, __f);
1447    __link_nodes(__p.__ptr_, __f, __f);
1448    --__c.__size_;
1449    ++this->__size_;
1450  }
1451}
1452
1453template <class _Tp, class _Alloc>
1454void list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l) {
1455  if (__f != __l) {
1456    __base_pointer __first = __f.__ptr_;
1457    --__l;
1458    __base_pointer __last = __l.__ptr_;
1459    if (this != std::addressof(__c)) {
1460      size_type __s = std::distance(__f, __l) + 1;
1461      __c.__size_ -= __s;
1462      this->__size_ += __s;
1463    }
1464    __base::__unlink_nodes(__first, __last);
1465    __link_nodes(__p.__ptr_, __first, __last);
1466  }
1467}
1468
1469template <class _Tp, class _Alloc>
1470typename list<_Tp, _Alloc>::__remove_return_type list<_Tp, _Alloc>::remove(const value_type& __x) {
1471  list<_Tp, _Alloc> __deleted_nodes(get_allocator()); // collect the nodes we're removing
1472  for (const_iterator __i = begin(), __e = end(); __i != __e;) {
1473    if (*__i == __x) {
1474      const_iterator __j = std::next(__i);
1475      for (; __j != __e && *__j == __x; ++__j)
1476        ;
1477      __deleted_nodes.splice(__deleted_nodes.end(), *this, __i, __j);
1478      __i = __j;
1479      if (__i != __e)
1480        ++__i;
1481    } else
1482      ++__i;
1483  }
1484
1485  return (__remove_return_type)__deleted_nodes.size();
1486}
1487
1488template <class _Tp, class _Alloc>
1489template <class _Pred>
1490typename list<_Tp, _Alloc>::__remove_return_type list<_Tp, _Alloc>::remove_if(_Pred __pred) {
1491  list<_Tp, _Alloc> __deleted_nodes(get_allocator()); // collect the nodes we're removing
1492  for (iterator __i = begin(), __e = end(); __i != __e;) {
1493    if (__pred(*__i)) {
1494      iterator __j = std::next(__i);
1495      for (; __j != __e && __pred(*__j); ++__j)
1496        ;
1497      __deleted_nodes.splice(__deleted_nodes.end(), *this, __i, __j);
1498      __i = __j;
1499      if (__i != __e)
1500        ++__i;
1501    } else
1502      ++__i;
1503  }
1504
1505  return (__remove_return_type)__deleted_nodes.size();
1506}
1507
1508template <class _Tp, class _Alloc>
1509template <class _BinaryPred>
1510typename list<_Tp, _Alloc>::__remove_return_type list<_Tp, _Alloc>::unique(_BinaryPred __binary_pred) {
1511  list<_Tp, _Alloc> __deleted_nodes(get_allocator()); // collect the nodes we're removing
1512  for (iterator __i = begin(), __e = end(); __i != __e;) {
1513    iterator __j = std::next(__i);
1514    for (; __j != __e && __binary_pred(*__i, *__j); ++__j)
1515      ;
1516    if (++__i != __j) {
1517      __deleted_nodes.splice(__deleted_nodes.end(), *this, __i, __j);
1518      __i = __j;
1519    }
1520  }
1521
1522  return (__remove_return_type)__deleted_nodes.size();
1523}
1524
1525template <class _Tp, class _Alloc>
1526inline void list<_Tp, _Alloc>::merge(list& __c) {
1527  merge(__c, __less<>());
1528}
1529
1530template <class _Tp, class _Alloc>
1531template <class _Comp>
1532void list<_Tp, _Alloc>::merge(list& __c, _Comp __comp) {
1533  if (this != std::addressof(__c)) {
1534    iterator __f1 = begin();
1535    iterator __e1 = end();
1536    iterator __f2 = __c.begin();
1537    iterator __e2 = __c.end();
1538    while (__f1 != __e1 && __f2 != __e2) {
1539      if (__comp(*__f2, *__f1)) {
1540        size_type __ds = 1;
1541        iterator __m2  = std::next(__f2);
1542        for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2, (void)++__ds)
1543          ;
1544        this->__size_ += __ds;
1545        __c.__size_ -= __ds;
1546        __base_pointer __f = __f2.__ptr_;
1547        __base_pointer __l = __m2.__ptr_->__prev_;
1548        __f2               = __m2;
1549        __base::__unlink_nodes(__f, __l);
1550        __m2 = std::next(__f1);
1551        __link_nodes(__f1.__ptr_, __f, __l);
1552        __f1 = __m2;
1553      } else
1554        ++__f1;
1555    }
1556    splice(__e1, __c);
1557  }
1558}
1559
1560template <class _Tp, class _Alloc>
1561inline void list<_Tp, _Alloc>::sort() {
1562  sort(__less<>());
1563}
1564
1565template <class _Tp, class _Alloc>
1566template <class _Comp>
1567inline void list<_Tp, _Alloc>::sort(_Comp __comp) {
1568  __sort(begin(), end(), this->__size_, __comp);
1569}
1570
1571template <class _Tp, class _Alloc>
1572template <class _Comp>
1573typename list<_Tp, _Alloc>::iterator
1574list<_Tp, _Alloc>::__sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp) {
1575  switch (__n) {
1576  case 0:
1577  case 1:
1578    return __f1;
1579  case 2:
1580    if (__comp(*--__e2, *__f1)) {
1581      __base_pointer __f = __e2.__ptr_;
1582      __base::__unlink_nodes(__f, __f);
1583      __link_nodes(__f1.__ptr_, __f, __f);
1584      return __e2;
1585    }
1586    return __f1;
1587  }
1588  size_type __n2 = __n / 2;
1589  iterator __e1  = std::next(__f1, __n2);
1590  iterator __r = __f1 = __sort(__f1, __e1, __n2, __comp);
1591  iterator __f2 = __e1 = __sort(__e1, __e2, __n - __n2, __comp);
1592  if (__comp(*__f2, *__f1)) {
1593    iterator __m2 = std::next(__f2);
1594    for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2)
1595      ;
1596    __base_pointer __f = __f2.__ptr_;
1597    __base_pointer __l = __m2.__ptr_->__prev_;
1598    __r                = __f2;
1599    __e1 = __f2 = __m2;
1600    __base::__unlink_nodes(__f, __l);
1601    __m2 = std::next(__f1);
1602    __link_nodes(__f1.__ptr_, __f, __l);
1603    __f1 = __m2;
1604  } else
1605    ++__f1;
1606  while (__f1 != __e1 && __f2 != __e2) {
1607    if (__comp(*__f2, *__f1)) {
1608      iterator __m2 = std::next(__f2);
1609      for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2)
1610        ;
1611      __base_pointer __f = __f2.__ptr_;
1612      __base_pointer __l = __m2.__ptr_->__prev_;
1613      if (__e1 == __f2)
1614        __e1 = __m2;
1615      __f2 = __m2;
1616      __base::__unlink_nodes(__f, __l);
1617      __m2 = std::next(__f1);
1618      __link_nodes(__f1.__ptr_, __f, __l);
1619      __f1 = __m2;
1620    } else
1621      ++__f1;
1622  }
1623  return __r;
1624}
1625
1626template <class _Tp, class _Alloc>
1627void list<_Tp, _Alloc>::reverse() _NOEXCEPT {
1628  if (this->__size_ > 1) {
1629    iterator __e = end();
1630    for (iterator __i = begin(); __i.__ptr_ != __e.__ptr_;) {
1631      std::swap(__i.__ptr_->__prev_, __i.__ptr_->__next_);
1632      __i.__ptr_ = __i.__ptr_->__prev_;
1633    }
1634    std::swap(__e.__ptr_->__prev_, __e.__ptr_->__next_);
1635  }
1636}
1637
1638template <class _Tp, class _Alloc>
1639bool list<_Tp, _Alloc>::__invariants() const {
1640  return size() == std::distance(begin(), end());
1641}
1642
1643template <class _Tp, class _Alloc>
1644inline _LIBCPP_HIDE_FROM_ABI bool operator==(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y) {
1645  return __x.size() == __y.size() && std::equal(__x.begin(), __x.end(), __y.begin());
1646}
1647
1648#if _LIBCPP_STD_VER <= 17
1649
1650template <class _Tp, class _Alloc>
1651inline _LIBCPP_HIDE_FROM_ABI bool operator<(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y) {
1652  return std::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
1653}
1654
1655template <class _Tp, class _Alloc>
1656inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y) {
1657  return !(__x == __y);
1658}
1659
1660template <class _Tp, class _Alloc>
1661inline _LIBCPP_HIDE_FROM_ABI bool operator>(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y) {
1662  return __y < __x;
1663}
1664
1665template <class _Tp, class _Alloc>
1666inline _LIBCPP_HIDE_FROM_ABI bool operator>=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y) {
1667  return !(__x < __y);
1668}
1669
1670template <class _Tp, class _Alloc>
1671inline _LIBCPP_HIDE_FROM_ABI bool operator<=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y) {
1672  return !(__y < __x);
1673}
1674
1675#else // _LIBCPP_STD_VER <= 17
1676
1677template <class _Tp, class _Allocator>
1678_LIBCPP_HIDE_FROM_ABI __synth_three_way_result<_Tp>
1679operator<=>(const list<_Tp, _Allocator>& __x, const list<_Tp, _Allocator>& __y) {
1680  return std::lexicographical_compare_three_way(__x.begin(), __x.end(), __y.begin(), __y.end(), std::__synth_three_way);
1681}
1682
1683#endif // _LIBCPP_STD_VER <= 17
1684
1685template <class _Tp, class _Alloc>
1686inline _LIBCPP_HIDE_FROM_ABI void swap(list<_Tp, _Alloc>& __x, list<_Tp, _Alloc>& __y)
1687    _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y))) {
1688  __x.swap(__y);
1689}
1690
1691#if _LIBCPP_STD_VER >= 20
1692template <class _Tp, class _Allocator, class _Predicate>
1693inline _LIBCPP_HIDE_FROM_ABI typename list<_Tp, _Allocator>::size_type
1694erase_if(list<_Tp, _Allocator>& __c, _Predicate __pred) {
1695  return __c.remove_if(__pred);
1696}
1697
1698template <class _Tp, class _Allocator, class _Up>
1699inline _LIBCPP_HIDE_FROM_ABI typename list<_Tp, _Allocator>::size_type
1700erase(list<_Tp, _Allocator>& __c, const _Up& __v) {
1701  return std::erase_if(__c, [&](auto& __elem) { return __elem == __v; });
1702}
1703
1704template <>
1705inline constexpr bool __format::__enable_insertable<std::list<char>> = true;
1706#  if _LIBCPP_HAS_WIDE_CHARACTERS
1707template <>
1708inline constexpr bool __format::__enable_insertable<std::list<wchar_t>> = true;
1709#  endif
1710
1711#endif // _LIBCPP_STD_VER >= 20
1712
1713template <class _Tp, class _Allocator>
1714struct __container_traits<list<_Tp, _Allocator> > {
1715  // http://eel.is/c++draft/container.reqmts
1716  // Unless otherwise specified (see [associative.reqmts.except], [unord.req.except], [deque.modifiers],
1717  // [inplace.vector.modifiers], and [vector.modifiers]) all container types defined in this Clause meet the following
1718  // additional requirements:
1719  // - If an exception is thrown by an insert() or emplace() function while inserting a single element, that
1720  // function has no effects.
1721  static _LIBCPP_CONSTEXPR const bool __emplacement_has_strong_exception_safety_guarantee = true;
1722};
1723
1724_LIBCPP_END_NAMESPACE_STD
1725
1726#if _LIBCPP_STD_VER >= 17
1727_LIBCPP_BEGIN_NAMESPACE_STD
1728namespace pmr {
1729template <class _ValueT>
1730using list _LIBCPP_AVAILABILITY_PMR = std::list<_ValueT, polymorphic_allocator<_ValueT>>;
1731} // namespace pmr
1732_LIBCPP_END_NAMESPACE_STD
1733#endif
1734
1735_LIBCPP_POP_MACROS
1736
1737#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
1738#  include <algorithm>
1739#  include <atomic>
1740#  include <concepts>
1741#  include <cstdint>
1742#  include <cstdlib>
1743#  include <functional>
1744#  include <iosfwd>
1745#  include <iterator>
1746#  include <stdexcept>
1747#  include <type_traits>
1748#  include <typeinfo>
1749#endif
1750
1751#endif // _LIBCPP_LIST
1752