• 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 <__availability>
207#include <__config>
208#include <__format/enable_insertable.h>
209#include <__iterator/distance.h>
210#include <__iterator/iterator_traits.h>
211#include <__iterator/move_iterator.h>
212#include <__iterator/next.h>
213#include <__iterator/prev.h>
214#include <__iterator/reverse_iterator.h>
215#include <__memory/addressof.h>
216#include <__memory/allocation_guard.h>
217#include <__memory/allocator.h>
218#include <__memory/allocator_traits.h>
219#include <__memory/compressed_pair.h>
220#include <__memory/construct_at.h>
221#include <__memory/pointer_traits.h>
222#include <__memory/swap_allocator.h>
223#include <__memory_resource/polymorphic_allocator.h>
224#include <__ranges/access.h>
225#include <__ranges/concepts.h>
226#include <__ranges/container_compatible_range.h>
227#include <__ranges/from_range.h>
228#include <__type_traits/conditional.h>
229#include <__type_traits/is_allocator.h>
230#include <__type_traits/is_nothrow_assignable.h>
231#include <__type_traits/is_nothrow_constructible.h>
232#include <__type_traits/is_pointer.h>
233#include <__type_traits/is_same.h>
234#include <__type_traits/type_identity.h>
235#include <__utility/forward.h>
236#include <__utility/move.h>
237#include <__utility/swap.h>
238#include <cstring>
239#include <limits>
240#include <new> // __launder
241#include <version>
242
243// standard-mandated includes
244
245// [iterator.range]
246#include <__iterator/access.h>
247#include <__iterator/data.h>
248#include <__iterator/empty.h>
249#include <__iterator/reverse_access.h>
250#include <__iterator/size.h>
251
252// [list.syn]
253#include <compare>
254#include <initializer_list>
255
256#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
257#  pragma GCC system_header
258#endif
259
260_LIBCPP_PUSH_MACROS
261#include <__undef_macros>
262
263_LIBCPP_BEGIN_NAMESPACE_STD
264
265template <class _Tp, class _VoidPtr>
266struct __list_node;
267template <class _Tp, class _VoidPtr>
268struct __list_node_base;
269
270template <class _Tp, class _VoidPtr>
271struct __list_node_pointer_traits {
272  typedef __rebind_pointer_t<_VoidPtr, __list_node<_Tp, _VoidPtr> > __node_pointer;
273  typedef __rebind_pointer_t<_VoidPtr, __list_node_base<_Tp, _VoidPtr> > __base_pointer;
274
275#if defined(_LIBCPP_ABI_LIST_REMOVE_NODE_POINTER_UB)
276  typedef __base_pointer __link_pointer;
277#else
278  typedef __conditional_t<is_pointer<_VoidPtr>::value, __base_pointer, __node_pointer> __link_pointer;
279#endif
280
281  typedef __conditional_t<is_same<__link_pointer, __node_pointer>::value, __base_pointer, __node_pointer>
282      __non_link_pointer;
283
284  static _LIBCPP_HIDE_FROM_ABI __link_pointer __unsafe_link_pointer_cast(__link_pointer __p) { return __p; }
285
286  static _LIBCPP_HIDE_FROM_ABI __link_pointer __unsafe_link_pointer_cast(__non_link_pointer __p) {
287    return static_cast<__link_pointer>(static_cast<_VoidPtr>(__p));
288  }
289};
290
291template <class _Tp, class _VoidPtr>
292struct __list_node_base {
293  typedef __list_node_pointer_traits<_Tp, _VoidPtr> _NodeTraits;
294  typedef typename _NodeTraits::__node_pointer __node_pointer;
295  typedef typename _NodeTraits::__base_pointer __base_pointer;
296  typedef typename _NodeTraits::__link_pointer __link_pointer;
297
298  __link_pointer __prev_;
299  __link_pointer __next_;
300
301  _LIBCPP_HIDE_FROM_ABI __list_node_base()
302      : __prev_(_NodeTraits::__unsafe_link_pointer_cast(__self())),
303        __next_(_NodeTraits::__unsafe_link_pointer_cast(__self())) {}
304
305  _LIBCPP_HIDE_FROM_ABI explicit __list_node_base(__link_pointer __prev, __link_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::__link_pointer __link_pointer;
337
338  _LIBCPP_HIDE_FROM_ABI explicit __list_node(__link_pointer __prev, __link_pointer __next) : __base(__prev, __next) {}
339  _LIBCPP_HIDE_FROM_ABI ~__list_node() {}
340
341  _LIBCPP_HIDE_FROM_ABI __link_pointer __as_link() { return static_cast<__link_pointer>(__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::__link_pointer __link_pointer;
355
356  __link_pointer __ptr_;
357
358  _LIBCPP_HIDE_FROM_ABI explicit __list_iterator(__link_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::__link_pointer __link_pointer;
413
414  __link_pointer __ptr_;
415
416  _LIBCPP_HIDE_FROM_ABI explicit __list_const_iterator(__link_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 {
469  __list_imp(const __list_imp&);
470  __list_imp& operator=(const __list_imp&);
471
472public:
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::__link_pointer __link_pointer;
490  typedef __link_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 "
499                "type; otherwise overload resolution breaks");
500
501  __node_base __end_;
502  __compressed_pair<size_type, __node_allocator> __size_alloc_;
503
504  _LIBCPP_HIDE_FROM_ABI __link_pointer __end_as_link() const _NOEXCEPT {
505    return __node_pointer_traits::__unsafe_link_pointer_cast(const_cast<__node_base&>(__end_).__self());
506  }
507
508  _LIBCPP_HIDE_FROM_ABI size_type& __sz() _NOEXCEPT { return __size_alloc_.first(); }
509  _LIBCPP_HIDE_FROM_ABI const size_type& __sz() const _NOEXCEPT { return __size_alloc_.first(); }
510  _LIBCPP_HIDE_FROM_ABI __node_allocator& __node_alloc() _NOEXCEPT { return __size_alloc_.second(); }
511  _LIBCPP_HIDE_FROM_ABI const __node_allocator& __node_alloc() const _NOEXCEPT { return __size_alloc_.second(); }
512
513  _LIBCPP_HIDE_FROM_ABI size_type __node_alloc_max_size() const _NOEXCEPT {
514    return __node_alloc_traits::max_size(__node_alloc());
515  }
516  _LIBCPP_HIDE_FROM_ABI static void __unlink_nodes(__link_pointer __f, __link_pointer __l) _NOEXCEPT;
517
518  _LIBCPP_HIDE_FROM_ABI __list_imp() _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value);
519  _LIBCPP_HIDE_FROM_ABI __list_imp(const allocator_type& __a);
520  _LIBCPP_HIDE_FROM_ABI __list_imp(const __node_allocator& __a);
521#ifndef _LIBCPP_CXX03_LANG
522  _LIBCPP_HIDE_FROM_ABI __list_imp(__node_allocator&& __a) _NOEXCEPT;
523#endif
524  _LIBCPP_HIDE_FROM_ABI ~__list_imp();
525  _LIBCPP_HIDE_FROM_ABI void clear() _NOEXCEPT;
526  _LIBCPP_HIDE_FROM_ABI bool empty() const _NOEXCEPT { return __sz() == 0; }
527
528  _LIBCPP_HIDE_FROM_ABI iterator begin() _NOEXCEPT { return iterator(__end_.__next_); }
529  _LIBCPP_HIDE_FROM_ABI const_iterator begin() const _NOEXCEPT { return const_iterator(__end_.__next_); }
530  _LIBCPP_HIDE_FROM_ABI iterator end() _NOEXCEPT { return iterator(__end_as_link()); }
531  _LIBCPP_HIDE_FROM_ABI const_iterator end() const _NOEXCEPT { return const_iterator(__end_as_link()); }
532
533  _LIBCPP_HIDE_FROM_ABI void swap(__list_imp& __c)
534#if _LIBCPP_STD_VER >= 14
535      _NOEXCEPT;
536#else
537      _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || __is_nothrow_swappable<allocator_type>::value);
538#endif
539
540  _LIBCPP_HIDE_FROM_ABI void __copy_assign_alloc(const __list_imp& __c) {
541    __copy_assign_alloc(
542        __c, integral_constant<bool, __node_alloc_traits::propagate_on_container_copy_assignment::value>());
543  }
544
545  _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(__list_imp& __c)
546      _NOEXCEPT_(!__node_alloc_traits::propagate_on_container_move_assignment::value ||
547                 is_nothrow_move_assignable<__node_allocator>::value) {
548    __move_assign_alloc(
549        __c, integral_constant<bool, __node_alloc_traits::propagate_on_container_move_assignment::value>());
550  }
551
552  template <class... _Args>
553  _LIBCPP_HIDE_FROM_ABI __node_pointer __create_node(__link_pointer __prev, __link_pointer __next, _Args&&... __args) {
554    __node_allocator& __alloc = __node_alloc();
555    __allocation_guard<__node_allocator> __guard(__alloc, 1);
556    // Begin the lifetime of the node itself. Note that this doesn't begin the lifetime of the value
557    // held inside the node, since we need to use the allocator's construct() method for that.
558    //
559    // We don't use the allocator's construct() method to construct the node itself since the
560    // Cpp17FooInsertable named requirements don't require the allocator's construct() method
561    // to work on anything other than the value_type.
562    std::__construct_at(std::addressof(*__guard.__get()), __prev, __next);
563
564    // Now construct the value_type using the allocator's construct() method.
565    __node_alloc_traits::construct(
566        __alloc, std::addressof(__guard.__get()->__get_value()), std::forward<_Args>(__args)...);
567    return __guard.__release_ptr();
568  }
569
570  template <class... _Args>
571  _LIBCPP_HIDE_FROM_ABI void __delete_node(__node_pointer __node) {
572    // For the same reason as above, we use the allocator's destroy() method for the value_type,
573    // but not for the node itself.
574    __node_allocator& __alloc = __node_alloc();
575    __node_alloc_traits::destroy(__alloc, std::addressof(__node->__get_value()));
576    std::__destroy_at(std::addressof(*__node));
577    __node_alloc_traits::deallocate(__alloc, __node, 1);
578  }
579
580private:
581  _LIBCPP_HIDE_FROM_ABI void __copy_assign_alloc(const __list_imp& __c, true_type) {
582    if (__node_alloc() != __c.__node_alloc())
583      clear();
584    __node_alloc() = __c.__node_alloc();
585  }
586
587  _LIBCPP_HIDE_FROM_ABI void __copy_assign_alloc(const __list_imp&, false_type) {}
588
589  _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(__list_imp& __c, true_type)
590      _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value) {
591    __node_alloc() = std::move(__c.__node_alloc());
592  }
593
594  _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(__list_imp&, false_type) _NOEXCEPT {}
595};
596
597// Unlink nodes [__f, __l]
598template <class _Tp, class _Alloc>
599inline void __list_imp<_Tp, _Alloc>::__unlink_nodes(__link_pointer __f, __link_pointer __l) _NOEXCEPT {
600  __f->__prev_->__next_ = __l->__next_;
601  __l->__next_->__prev_ = __f->__prev_;
602}
603
604template <class _Tp, class _Alloc>
605inline __list_imp<_Tp, _Alloc>::__list_imp() _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value)
606    : __size_alloc_(0, __default_init_tag()) {}
607
608template <class _Tp, class _Alloc>
609inline __list_imp<_Tp, _Alloc>::__list_imp(const allocator_type& __a) : __size_alloc_(0, __node_allocator(__a)) {}
610
611template <class _Tp, class _Alloc>
612inline __list_imp<_Tp, _Alloc>::__list_imp(const __node_allocator& __a) : __size_alloc_(0, __a) {}
613
614#ifndef _LIBCPP_CXX03_LANG
615template <class _Tp, class _Alloc>
616inline __list_imp<_Tp, _Alloc>::__list_imp(__node_allocator&& __a) _NOEXCEPT : __size_alloc_(0, std::move(__a)) {}
617#endif
618
619template <class _Tp, class _Alloc>
620__list_imp<_Tp, _Alloc>::~__list_imp() {
621  clear();
622}
623
624template <class _Tp, class _Alloc>
625void __list_imp<_Tp, _Alloc>::clear() _NOEXCEPT {
626  if (!empty()) {
627    __link_pointer __f = __end_.__next_;
628    __link_pointer __l = __end_as_link();
629    __unlink_nodes(__f, __l->__prev_);
630    __sz() = 0;
631    while (__f != __l) {
632      __node_pointer __np = __f->__as_node();
633      __f                 = __f->__next_;
634      __delete_node(__np);
635    }
636  }
637}
638
639template <class _Tp, class _Alloc>
640void __list_imp<_Tp, _Alloc>::swap(__list_imp& __c)
641#if _LIBCPP_STD_VER >= 14
642    _NOEXCEPT
643#else
644    _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || __is_nothrow_swappable<allocator_type>::value)
645#endif
646{
647  _LIBCPP_ASSERT_COMPATIBLE_ALLOCATOR(
648      __alloc_traits::propagate_on_container_swap::value || this->__node_alloc() == __c.__node_alloc(),
649      "list::swap: Either propagate_on_container_swap must be true"
650      " or the allocators must compare equal");
651  using std::swap;
652  std::__swap_allocator(__node_alloc(), __c.__node_alloc());
653  swap(__sz(), __c.__sz());
654  swap(__end_, __c.__end_);
655  if (__sz() == 0)
656    __end_.__next_ = __end_.__prev_ = __end_as_link();
657  else
658    __end_.__prev_->__next_ = __end_.__next_->__prev_ = __end_as_link();
659  if (__c.__sz() == 0)
660    __c.__end_.__next_ = __c.__end_.__prev_ = __c.__end_as_link();
661  else
662    __c.__end_.__prev_->__next_ = __c.__end_.__next_->__prev_ = __c.__end_as_link();
663}
664
665template <class _Tp, class _Alloc /*= allocator<_Tp>*/>
666class _LIBCPP_TEMPLATE_VIS list : private __list_imp<_Tp, _Alloc> {
667  typedef __list_imp<_Tp, _Alloc> base;
668  typedef typename base::__node_type __node_type;
669  typedef typename base::__node_allocator __node_allocator;
670  typedef typename base::__node_pointer __node_pointer;
671  typedef typename base::__node_alloc_traits __node_alloc_traits;
672  typedef typename base::__node_base __node_base;
673  typedef typename base::__node_base_pointer __node_base_pointer;
674  typedef typename base::__link_pointer __link_pointer;
675
676public:
677  typedef _Tp value_type;
678  typedef _Alloc allocator_type;
679  static_assert((is_same<value_type, typename allocator_type::value_type>::value),
680                "Allocator::value_type must be same type as value_type");
681  typedef value_type& reference;
682  typedef const value_type& const_reference;
683  typedef typename base::pointer pointer;
684  typedef typename base::const_pointer const_pointer;
685  typedef typename base::size_type size_type;
686  typedef typename base::difference_type difference_type;
687  typedef typename base::iterator iterator;
688  typedef typename base::const_iterator const_iterator;
689  typedef std::reverse_iterator<iterator> reverse_iterator;
690  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
691#if _LIBCPP_STD_VER >= 20
692  typedef size_type __remove_return_type;
693#else
694  typedef void __remove_return_type;
695#endif
696
697  static_assert(is_same<allocator_type, __rebind_alloc<allocator_traits<allocator_type>, value_type> >::value,
698                "[allocator.requirements] states that rebinding an allocator to the same type should result in the "
699                "original allocator");
700
701  _LIBCPP_HIDE_FROM_ABI list() _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value) {}
702  _LIBCPP_HIDE_FROM_ABI explicit list(const allocator_type& __a) : base(__a) {}
703  _LIBCPP_HIDE_FROM_ABI explicit list(size_type __n);
704#if _LIBCPP_STD_VER >= 14
705  _LIBCPP_HIDE_FROM_ABI explicit list(size_type __n, const allocator_type& __a);
706#endif
707  _LIBCPP_HIDE_FROM_ABI list(size_type __n, const value_type& __x);
708  template <__enable_if_t<__is_allocator<_Alloc>::value, int> = 0>
709  _LIBCPP_HIDE_FROM_ABI list(size_type __n, const value_type& __x, const allocator_type& __a) : base(__a) {
710    for (; __n > 0; --__n)
711      push_back(__x);
712  }
713
714  template <class _InpIter, __enable_if_t<__has_input_iterator_category<_InpIter>::value, int> = 0>
715  _LIBCPP_HIDE_FROM_ABI list(_InpIter __f, _InpIter __l);
716
717  template <class _InpIter, __enable_if_t<__has_input_iterator_category<_InpIter>::value, int> = 0>
718  _LIBCPP_HIDE_FROM_ABI list(_InpIter __f, _InpIter __l, const allocator_type& __a);
719
720#if _LIBCPP_STD_VER >= 23
721  template <_ContainerCompatibleRange<_Tp> _Range>
722  _LIBCPP_HIDE_FROM_ABI list(from_range_t, _Range&& __range, const allocator_type& __a = allocator_type()) : base(__a) {
723    prepend_range(std::forward<_Range>(__range));
724  }
725#endif
726
727  _LIBCPP_HIDE_FROM_ABI list(const list& __c);
728  _LIBCPP_HIDE_FROM_ABI list(const list& __c, const __type_identity_t<allocator_type>& __a);
729  _LIBCPP_HIDE_FROM_ABI list& operator=(const list& __c);
730#ifndef _LIBCPP_CXX03_LANG
731  _LIBCPP_HIDE_FROM_ABI list(initializer_list<value_type> __il);
732  _LIBCPP_HIDE_FROM_ABI list(initializer_list<value_type> __il, const allocator_type& __a);
733
734  _LIBCPP_HIDE_FROM_ABI list(list&& __c) _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value);
735  _LIBCPP_HIDE_FROM_ABI list(list&& __c, const __type_identity_t<allocator_type>& __a);
736  _LIBCPP_HIDE_FROM_ABI list& operator=(list&& __c)
737      _NOEXCEPT_(__node_alloc_traits::propagate_on_container_move_assignment::value&&
738                     is_nothrow_move_assignable<__node_allocator>::value);
739
740  _LIBCPP_HIDE_FROM_ABI list& operator=(initializer_list<value_type> __il) {
741    assign(__il.begin(), __il.end());
742    return *this;
743  }
744
745  _LIBCPP_HIDE_FROM_ABI void assign(initializer_list<value_type> __il) { assign(__il.begin(), __il.end()); }
746#endif // _LIBCPP_CXX03_LANG
747
748  template <class _InpIter, __enable_if_t<__has_input_iterator_category<_InpIter>::value, int> = 0>
749  _LIBCPP_HIDE_FROM_ABI void assign(_InpIter __f, _InpIter __l);
750
751#if _LIBCPP_STD_VER >= 23
752  template <_ContainerCompatibleRange<_Tp> _Range>
753  _LIBCPP_HIDE_FROM_ABI void assign_range(_Range&& __range) {
754    __assign_with_sentinel(ranges::begin(__range), ranges::end(__range));
755  }
756#endif
757
758  _LIBCPP_HIDE_FROM_ABI void assign(size_type __n, const value_type& __x);
759
760  _LIBCPP_HIDE_FROM_ABI allocator_type get_allocator() const _NOEXCEPT;
761
762  _LIBCPP_HIDE_FROM_ABI size_type size() const _NOEXCEPT { return base::__sz(); }
763  _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI bool empty() const _NOEXCEPT { return base::empty(); }
764  _LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT {
765    return std::min<size_type>(base::__node_alloc_max_size(), numeric_limits<difference_type >::max());
766  }
767
768  _LIBCPP_HIDE_FROM_ABI iterator begin() _NOEXCEPT { return base::begin(); }
769  _LIBCPP_HIDE_FROM_ABI const_iterator begin() const _NOEXCEPT { return base::begin(); }
770  _LIBCPP_HIDE_FROM_ABI iterator end() _NOEXCEPT { return base::end(); }
771  _LIBCPP_HIDE_FROM_ABI const_iterator end() const _NOEXCEPT { return base::end(); }
772  _LIBCPP_HIDE_FROM_ABI const_iterator cbegin() const _NOEXCEPT { return base::begin(); }
773  _LIBCPP_HIDE_FROM_ABI const_iterator cend() const _NOEXCEPT { return base::end(); }
774
775  _LIBCPP_HIDE_FROM_ABI reverse_iterator rbegin() _NOEXCEPT { return reverse_iterator(end()); }
776  _LIBCPP_HIDE_FROM_ABI const_reverse_iterator rbegin() const _NOEXCEPT { return const_reverse_iterator(end()); }
777  _LIBCPP_HIDE_FROM_ABI reverse_iterator rend() _NOEXCEPT { return reverse_iterator(begin()); }
778  _LIBCPP_HIDE_FROM_ABI const_reverse_iterator rend() const _NOEXCEPT { return const_reverse_iterator(begin()); }
779  _LIBCPP_HIDE_FROM_ABI const_reverse_iterator crbegin() const _NOEXCEPT { return const_reverse_iterator(end()); }
780  _LIBCPP_HIDE_FROM_ABI const_reverse_iterator crend() const _NOEXCEPT { return const_reverse_iterator(begin()); }
781
782  _LIBCPP_HIDE_FROM_ABI reference front() {
783    _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "list::front called on empty list");
784    return base::__end_.__next_->__as_node()->__get_value();
785  }
786  _LIBCPP_HIDE_FROM_ABI const_reference front() const {
787    _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "list::front called on empty list");
788    return base::__end_.__next_->__as_node()->__get_value();
789  }
790  _LIBCPP_HIDE_FROM_ABI reference back() {
791    _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "list::back called on empty list");
792    return base::__end_.__prev_->__as_node()->__get_value();
793  }
794  _LIBCPP_HIDE_FROM_ABI const_reference back() const {
795    _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "list::back called on empty list");
796    return base::__end_.__prev_->__as_node()->__get_value();
797  }
798
799#ifndef _LIBCPP_CXX03_LANG
800  _LIBCPP_HIDE_FROM_ABI void push_front(value_type&& __x);
801  _LIBCPP_HIDE_FROM_ABI void push_back(value_type&& __x);
802
803#  if _LIBCPP_STD_VER >= 23
804  template <_ContainerCompatibleRange<_Tp> _Range>
805  _LIBCPP_HIDE_FROM_ABI void prepend_range(_Range&& __range) {
806    insert_range(begin(), std::forward<_Range>(__range));
807  }
808
809  template <_ContainerCompatibleRange<_Tp> _Range>
810  _LIBCPP_HIDE_FROM_ABI void append_range(_Range&& __range) {
811    insert_range(end(), std::forward<_Range>(__range));
812  }
813#  endif
814
815  template <class... _Args>
816#  if _LIBCPP_STD_VER >= 17
817  _LIBCPP_HIDE_FROM_ABI reference emplace_front(_Args&&... __args);
818#  else
819  _LIBCPP_HIDE_FROM_ABI void emplace_front(_Args&&... __args);
820#  endif
821  template <class... _Args>
822#  if _LIBCPP_STD_VER >= 17
823  _LIBCPP_HIDE_FROM_ABI reference emplace_back(_Args&&... __args);
824#  else
825  _LIBCPP_HIDE_FROM_ABI void emplace_back(_Args&&... __args);
826#  endif
827  template <class... _Args>
828  _LIBCPP_HIDE_FROM_ABI iterator emplace(const_iterator __p, _Args&&... __args);
829
830  _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, value_type&& __x);
831
832  _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, initializer_list<value_type> __il) {
833    return insert(__p, __il.begin(), __il.end());
834  }
835#endif // _LIBCPP_CXX03_LANG
836
837  _LIBCPP_HIDE_FROM_ABI void push_front(const value_type& __x);
838  _LIBCPP_HIDE_FROM_ABI void push_back(const value_type& __x);
839
840#ifndef _LIBCPP_CXX03_LANG
841  template <class _Arg>
842  _LIBCPP_HIDE_FROM_ABI void __emplace_back(_Arg&& __arg) {
843    emplace_back(std::forward<_Arg>(__arg));
844  }
845#else
846  _LIBCPP_HIDE_FROM_ABI void __emplace_back(value_type const& __arg) { push_back(__arg); }
847#endif
848
849  _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, const value_type& __x);
850  _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, size_type __n, const value_type& __x);
851
852  template <class _InpIter, __enable_if_t<__has_input_iterator_category<_InpIter>::value, int> = 0>
853  _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, _InpIter __f, _InpIter __l);
854
855#if _LIBCPP_STD_VER >= 23
856  template <_ContainerCompatibleRange<_Tp> _Range>
857  _LIBCPP_HIDE_FROM_ABI iterator insert_range(const_iterator __position, _Range&& __range) {
858    return __insert_with_sentinel(__position, ranges::begin(__range), ranges::end(__range));
859  }
860#endif
861
862  _LIBCPP_HIDE_FROM_ABI void swap(list& __c)
863#if _LIBCPP_STD_VER >= 14
864      _NOEXCEPT
865#else
866      _NOEXCEPT_(!__node_alloc_traits::propagate_on_container_swap::value ||
867                 __is_nothrow_swappable<__node_allocator>::value)
868#endif
869  {
870    base::swap(__c);
871  }
872  _LIBCPP_HIDE_FROM_ABI void clear() _NOEXCEPT { base::clear(); }
873
874  _LIBCPP_HIDE_FROM_ABI void pop_front();
875  _LIBCPP_HIDE_FROM_ABI void pop_back();
876
877  _LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __p);
878  _LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __f, const_iterator __l);
879
880  _LIBCPP_HIDE_FROM_ABI void resize(size_type __n);
881  _LIBCPP_HIDE_FROM_ABI void resize(size_type __n, const value_type& __x);
882
883  _LIBCPP_HIDE_FROM_ABI void splice(const_iterator __p, list& __c);
884#ifndef _LIBCPP_CXX03_LANG
885  _LIBCPP_HIDE_FROM_ABI void splice(const_iterator __p, list&& __c) { splice(__p, __c); }
886  _LIBCPP_HIDE_FROM_ABI void splice(const_iterator __p, list&& __c, const_iterator __i) { splice(__p, __c, __i); }
887  _LIBCPP_HIDE_FROM_ABI void splice(const_iterator __p, list&& __c, const_iterator __f, const_iterator __l) {
888    splice(__p, __c, __f, __l);
889  }
890#endif
891  _LIBCPP_HIDE_FROM_ABI void splice(const_iterator __p, list& __c, const_iterator __i);
892  _LIBCPP_HIDE_FROM_ABI void splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l);
893
894  _LIBCPP_HIDE_FROM_ABI __remove_return_type remove(const value_type& __x);
895  template <class _Pred>
896  _LIBCPP_HIDE_FROM_ABI __remove_return_type remove_if(_Pred __pred);
897  _LIBCPP_HIDE_FROM_ABI __remove_return_type unique() { return unique(__equal_to()); }
898  template <class _BinaryPred>
899  _LIBCPP_HIDE_FROM_ABI __remove_return_type unique(_BinaryPred __binary_pred);
900  _LIBCPP_HIDE_FROM_ABI void merge(list& __c);
901#ifndef _LIBCPP_CXX03_LANG
902  _LIBCPP_HIDE_FROM_ABI void merge(list&& __c) { merge(__c); }
903
904  template <class _Comp>
905  _LIBCPP_HIDE_FROM_ABI void merge(list&& __c, _Comp __comp) {
906    merge(__c, __comp);
907  }
908#endif
909  template <class _Comp>
910  _LIBCPP_HIDE_FROM_ABI void merge(list& __c, _Comp __comp);
911
912  _LIBCPP_HIDE_FROM_ABI void sort();
913  template <class _Comp>
914  _LIBCPP_HIDE_FROM_ABI void sort(_Comp __comp);
915
916  _LIBCPP_HIDE_FROM_ABI void reverse() _NOEXCEPT;
917
918  _LIBCPP_HIDE_FROM_ABI bool __invariants() const;
919
920private:
921  template <class _Iterator, class _Sentinel>
922  _LIBCPP_HIDE_FROM_ABI void __assign_with_sentinel(_Iterator __f, _Sentinel __l);
923
924  template <class _Iterator, class _Sentinel>
925  _LIBCPP_HIDE_FROM_ABI iterator __insert_with_sentinel(const_iterator __p, _Iterator __f, _Sentinel __l);
926
927  _LIBCPP_HIDE_FROM_ABI static void __link_nodes(__link_pointer __p, __link_pointer __f, __link_pointer __l);
928  _LIBCPP_HIDE_FROM_ABI void __link_nodes_at_front(__link_pointer __f, __link_pointer __l);
929  _LIBCPP_HIDE_FROM_ABI void __link_nodes_at_back(__link_pointer __f, __link_pointer __l);
930  _LIBCPP_HIDE_FROM_ABI iterator __iterator(size_type __n);
931  // TODO: Make this _LIBCPP_HIDE_FROM_ABI
932  template <class _Comp>
933  _LIBCPP_HIDDEN static iterator __sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp);
934
935  _LIBCPP_HIDE_FROM_ABI void __move_assign(list& __c, true_type)
936      _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value);
937  _LIBCPP_HIDE_FROM_ABI void __move_assign(list& __c, false_type);
938};
939
940#if _LIBCPP_STD_VER >= 17
941template <class _InputIterator,
942          class _Alloc = allocator<__iter_value_type<_InputIterator>>,
943          class        = enable_if_t<__has_input_iterator_category<_InputIterator>::value>,
944          class        = enable_if_t<__is_allocator<_Alloc>::value> >
945list(_InputIterator, _InputIterator) -> list<__iter_value_type<_InputIterator>, _Alloc>;
946
947template <class _InputIterator,
948          class _Alloc,
949          class = enable_if_t<__has_input_iterator_category<_InputIterator>::value>,
950          class = enable_if_t<__is_allocator<_Alloc>::value> >
951list(_InputIterator, _InputIterator, _Alloc) -> list<__iter_value_type<_InputIterator>, _Alloc>;
952#endif
953
954#if _LIBCPP_STD_VER >= 23
955template <ranges::input_range _Range,
956          class _Alloc = allocator<ranges::range_value_t<_Range>>,
957          class        = enable_if_t<__is_allocator<_Alloc>::value> >
958list(from_range_t, _Range&&, _Alloc = _Alloc()) -> list<ranges::range_value_t<_Range>, _Alloc>;
959#endif
960
961// Link in nodes [__f, __l] just prior to __p
962template <class _Tp, class _Alloc>
963inline void list<_Tp, _Alloc>::__link_nodes(__link_pointer __p, __link_pointer __f, __link_pointer __l) {
964  __p->__prev_->__next_ = __f;
965  __f->__prev_          = __p->__prev_;
966  __p->__prev_          = __l;
967  __l->__next_          = __p;
968}
969
970// Link in nodes [__f, __l] at the front of the list
971template <class _Tp, class _Alloc>
972inline void list<_Tp, _Alloc>::__link_nodes_at_front(__link_pointer __f, __link_pointer __l) {
973  __f->__prev_          = base::__end_as_link();
974  __l->__next_          = base::__end_.__next_;
975  __l->__next_->__prev_ = __l;
976  base::__end_.__next_  = __f;
977}
978
979// Link in nodes [__f, __l] at the back of the list
980template <class _Tp, class _Alloc>
981inline void list<_Tp, _Alloc>::__link_nodes_at_back(__link_pointer __f, __link_pointer __l) {
982  __l->__next_          = base::__end_as_link();
983  __f->__prev_          = base::__end_.__prev_;
984  __f->__prev_->__next_ = __f;
985  base::__end_.__prev_  = __l;
986}
987
988template <class _Tp, class _Alloc>
989inline typename list<_Tp, _Alloc>::iterator list<_Tp, _Alloc>::__iterator(size_type __n) {
990  return __n <= base::__sz() / 2 ? std::next(begin(), __n) : std::prev(end(), base::__sz() - __n);
991}
992
993template <class _Tp, class _Alloc>
994list<_Tp, _Alloc>::list(size_type __n) {
995  for (; __n > 0; --__n)
996#ifndef _LIBCPP_CXX03_LANG
997    emplace_back();
998#else
999    push_back(value_type());
1000#endif
1001}
1002
1003#if _LIBCPP_STD_VER >= 14
1004template <class _Tp, class _Alloc>
1005list<_Tp, _Alloc>::list(size_type __n, const allocator_type& __a) : base(__a) {
1006  for (; __n > 0; --__n)
1007    emplace_back();
1008}
1009#endif
1010
1011template <class _Tp, class _Alloc>
1012list<_Tp, _Alloc>::list(size_type __n, const value_type& __x) {
1013  for (; __n > 0; --__n)
1014    push_back(__x);
1015}
1016
1017template <class _Tp, class _Alloc>
1018template <class _InpIter, __enable_if_t<__has_input_iterator_category<_InpIter>::value, int> >
1019list<_Tp, _Alloc>::list(_InpIter __f, _InpIter __l) {
1020  for (; __f != __l; ++__f)
1021    __emplace_back(*__f);
1022}
1023
1024template <class _Tp, class _Alloc>
1025template <class _InpIter, __enable_if_t<__has_input_iterator_category<_InpIter>::value, int> >
1026list<_Tp, _Alloc>::list(_InpIter __f, _InpIter __l, const allocator_type& __a) : base(__a) {
1027  for (; __f != __l; ++__f)
1028    __emplace_back(*__f);
1029}
1030
1031template <class _Tp, class _Alloc>
1032list<_Tp, _Alloc>::list(const list& __c)
1033    : base(__node_alloc_traits::select_on_container_copy_construction(__c.__node_alloc())) {
1034  for (const_iterator __i = __c.begin(), __e = __c.end(); __i != __e; ++__i)
1035    push_back(*__i);
1036}
1037
1038template <class _Tp, class _Alloc>
1039list<_Tp, _Alloc>::list(const list& __c, const __type_identity_t<allocator_type>& __a) : base(__a) {
1040  for (const_iterator __i = __c.begin(), __e = __c.end(); __i != __e; ++__i)
1041    push_back(*__i);
1042}
1043
1044#ifndef _LIBCPP_CXX03_LANG
1045
1046template <class _Tp, class _Alloc>
1047list<_Tp, _Alloc>::list(initializer_list<value_type> __il, const allocator_type& __a) : base(__a) {
1048  for (typename initializer_list<value_type>::const_iterator __i = __il.begin(), __e = __il.end(); __i != __e; ++__i)
1049    push_back(*__i);
1050}
1051
1052template <class _Tp, class _Alloc>
1053list<_Tp, _Alloc>::list(initializer_list<value_type> __il) {
1054  for (typename initializer_list<value_type>::const_iterator __i = __il.begin(), __e = __il.end(); __i != __e; ++__i)
1055    push_back(*__i);
1056}
1057
1058template <class _Tp, class _Alloc>
1059inline list<_Tp, _Alloc>::list(list&& __c) _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value)
1060    : base(std::move(__c.__node_alloc())) {
1061  splice(end(), __c);
1062}
1063
1064template <class _Tp, class _Alloc>
1065inline list<_Tp, _Alloc>::list(list&& __c, const __type_identity_t<allocator_type>& __a) : base(__a) {
1066  if (__a == __c.get_allocator())
1067    splice(end(), __c);
1068  else {
1069    typedef move_iterator<iterator> _Ip;
1070    assign(_Ip(__c.begin()), _Ip(__c.end()));
1071  }
1072}
1073
1074template <class _Tp, class _Alloc>
1075inline list<_Tp, _Alloc>& list<_Tp, _Alloc>::operator=(list&& __c)
1076    _NOEXCEPT_(__node_alloc_traits::propagate_on_container_move_assignment::value&&
1077                   is_nothrow_move_assignable<__node_allocator>::value) {
1078  __move_assign(__c, integral_constant<bool, __node_alloc_traits::propagate_on_container_move_assignment::value>());
1079  return *this;
1080}
1081
1082template <class _Tp, class _Alloc>
1083void list<_Tp, _Alloc>::__move_assign(list& __c, false_type) {
1084  if (base::__node_alloc() != __c.__node_alloc()) {
1085    typedef move_iterator<iterator> _Ip;
1086    assign(_Ip(__c.begin()), _Ip(__c.end()));
1087  } else
1088    __move_assign(__c, true_type());
1089}
1090
1091template <class _Tp, class _Alloc>
1092void list<_Tp, _Alloc>::__move_assign(list& __c, true_type)
1093    _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value) {
1094  clear();
1095  base::__move_assign_alloc(__c);
1096  splice(end(), __c);
1097}
1098
1099#endif // _LIBCPP_CXX03_LANG
1100
1101template <class _Tp, class _Alloc>
1102inline list<_Tp, _Alloc>& list<_Tp, _Alloc>::operator=(const list& __c) {
1103  if (this != std::addressof(__c)) {
1104    base::__copy_assign_alloc(__c);
1105    assign(__c.begin(), __c.end());
1106  }
1107  return *this;
1108}
1109
1110template <class _Tp, class _Alloc>
1111template <class _InpIter, __enable_if_t<__has_input_iterator_category<_InpIter>::value, int> >
1112void list<_Tp, _Alloc>::assign(_InpIter __f, _InpIter __l) {
1113  __assign_with_sentinel(__f, __l);
1114}
1115
1116template <class _Tp, class _Alloc>
1117template <class _Iterator, class _Sentinel>
1118_LIBCPP_HIDE_FROM_ABI void list<_Tp, _Alloc>::__assign_with_sentinel(_Iterator __f, _Sentinel __l) {
1119  iterator __i = begin();
1120  iterator __e = end();
1121  for (; __f != __l && __i != __e; ++__f, (void)++__i)
1122    *__i = *__f;
1123  if (__i == __e)
1124    __insert_with_sentinel(__e, std::move(__f), std::move(__l));
1125  else
1126    erase(__i, __e);
1127}
1128
1129template <class _Tp, class _Alloc>
1130void list<_Tp, _Alloc>::assign(size_type __n, const value_type& __x) {
1131  iterator __i = begin();
1132  iterator __e = end();
1133  for (; __n > 0 && __i != __e; --__n, (void)++__i)
1134    *__i = __x;
1135  if (__i == __e)
1136    insert(__e, __n, __x);
1137  else
1138    erase(__i, __e);
1139}
1140
1141template <class _Tp, class _Alloc>
1142inline _Alloc list<_Tp, _Alloc>::get_allocator() const _NOEXCEPT {
1143  return allocator_type(base::__node_alloc());
1144}
1145
1146template <class _Tp, class _Alloc>
1147typename list<_Tp, _Alloc>::iterator list<_Tp, _Alloc>::insert(const_iterator __p, const value_type& __x) {
1148  __node_pointer __node = this->__create_node(/* prev = */ nullptr, /* next = */ nullptr, __x);
1149  __link_nodes(__p.__ptr_, __node->__as_link(), __node->__as_link());
1150  ++base::__sz();
1151  return iterator(__node->__as_link());
1152}
1153
1154template <class _Tp, class _Alloc>
1155typename list<_Tp, _Alloc>::iterator
1156list<_Tp, _Alloc>::insert(const_iterator __p, size_type __n, const value_type& __x) {
1157  iterator __r(__p.__ptr_);
1158  if (__n > 0) {
1159    size_type __ds        = 0;
1160    __node_pointer __node = this->__create_node(/* prev = */ nullptr, /* next = */ nullptr, __x);
1161    ++__ds;
1162    __r          = iterator(__node->__as_link());
1163    iterator __e = __r;
1164#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1165    try {
1166#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1167      for (--__n; __n != 0; --__n, (void)++__e, ++__ds) {
1168        __e.__ptr_->__next_ = this->__create_node(/* prev = */ __e.__ptr_, /* next = */ nullptr, __x)->__as_link();
1169      }
1170#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1171    } catch (...) {
1172      while (true) {
1173        __link_pointer __prev    = __e.__ptr_->__prev_;
1174        __node_pointer __current = __e.__ptr_->__as_node();
1175        this->__delete_node(__current);
1176        if (__prev == 0)
1177          break;
1178        __e = iterator(__prev);
1179      }
1180      throw;
1181    }
1182#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1183    __link_nodes(__p.__ptr_, __r.__ptr_, __e.__ptr_);
1184    base::__sz() += __ds;
1185  }
1186  return __r;
1187}
1188
1189template <class _Tp, class _Alloc>
1190template <class _InpIter, __enable_if_t<__has_input_iterator_category<_InpIter>::value, int> >
1191typename list<_Tp, _Alloc>::iterator list<_Tp, _Alloc>::insert(const_iterator __p, _InpIter __f, _InpIter __l) {
1192  return __insert_with_sentinel(__p, __f, __l);
1193}
1194
1195template <class _Tp, class _Alloc>
1196template <class _Iterator, class _Sentinel>
1197_LIBCPP_HIDE_FROM_ABI typename list<_Tp, _Alloc>::iterator
1198list<_Tp, _Alloc>::__insert_with_sentinel(const_iterator __p, _Iterator __f, _Sentinel __l) {
1199  iterator __r(__p.__ptr_);
1200  if (__f != __l) {
1201    size_type __ds        = 0;
1202    __node_pointer __node = this->__create_node(/* prev = */ nullptr, /* next = */ nullptr, *__f);
1203    ++__ds;
1204    __r          = iterator(__node->__as_link());
1205    iterator __e = __r;
1206#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1207    try {
1208#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1209      for (++__f; __f != __l; ++__f, (void)++__e, ++__ds) {
1210        __e.__ptr_->__next_ = this->__create_node(/* prev = */ __e.__ptr_, /* next = */ nullptr, *__f)->__as_link();
1211      }
1212#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1213    } catch (...) {
1214      while (true) {
1215        __link_pointer __prev    = __e.__ptr_->__prev_;
1216        __node_pointer __current = __e.__ptr_->__as_node();
1217        this->__delete_node(__current);
1218        if (__prev == 0)
1219          break;
1220        __e = iterator(__prev);
1221      }
1222      throw;
1223    }
1224#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1225    __link_nodes(__p.__ptr_, __r.__ptr_, __e.__ptr_);
1226    base::__sz() += __ds;
1227  }
1228  return __r;
1229}
1230
1231template <class _Tp, class _Alloc>
1232void list<_Tp, _Alloc>::push_front(const value_type& __x) {
1233  __node_pointer __node = this->__create_node(/* prev = */ nullptr, /* next = */ nullptr, __x);
1234  __link_pointer __nl   = __node->__as_link();
1235  __link_nodes_at_front(__nl, __nl);
1236  ++base::__sz();
1237}
1238
1239template <class _Tp, class _Alloc>
1240void list<_Tp, _Alloc>::push_back(const value_type& __x) {
1241  __node_pointer __node = this->__create_node(/* prev = */ nullptr, /* next = */ nullptr, __x);
1242  __link_pointer __nl   = __node->__as_link();
1243  __link_nodes_at_back(__nl, __nl);
1244  ++base::__sz();
1245}
1246
1247#ifndef _LIBCPP_CXX03_LANG
1248
1249template <class _Tp, class _Alloc>
1250void list<_Tp, _Alloc>::push_front(value_type&& __x) {
1251  __node_pointer __node = this->__create_node(/* prev = */ nullptr, /* next = */ nullptr, std::move(__x));
1252  __link_pointer __nl   = __node->__as_link();
1253  __link_nodes_at_front(__nl, __nl);
1254  ++base::__sz();
1255}
1256
1257template <class _Tp, class _Alloc>
1258void list<_Tp, _Alloc>::push_back(value_type&& __x) {
1259  __node_pointer __node = this->__create_node(/* prev = */ nullptr, /* next = */ nullptr, std::move(__x));
1260  __link_pointer __nl   = __node->__as_link();
1261  __link_nodes_at_back(__nl, __nl);
1262  ++base::__sz();
1263}
1264
1265template <class _Tp, class _Alloc>
1266template <class... _Args>
1267#  if _LIBCPP_STD_VER >= 17
1268typename list<_Tp, _Alloc>::reference
1269#  else
1270void
1271#  endif
1272list<_Tp, _Alloc>::emplace_front(_Args&&... __args) {
1273  __node_pointer __node =
1274      this->__create_node(/* prev = */ nullptr, /* next = */ nullptr, std::forward<_Args>(__args)...);
1275  __link_pointer __nl = __node->__as_link();
1276  __link_nodes_at_front(__nl, __nl);
1277  ++base::__sz();
1278#  if _LIBCPP_STD_VER >= 17
1279  return __node->__get_value();
1280#  endif
1281}
1282
1283template <class _Tp, class _Alloc>
1284template <class... _Args>
1285#  if _LIBCPP_STD_VER >= 17
1286typename list<_Tp, _Alloc>::reference
1287#  else
1288void
1289#  endif
1290list<_Tp, _Alloc>::emplace_back(_Args&&... __args) {
1291  __node_pointer __node =
1292      this->__create_node(/* prev = */ nullptr, /* next = */ nullptr, std::forward<_Args>(__args)...);
1293  __link_pointer __nl = __node->__as_link();
1294  __link_nodes_at_back(__nl, __nl);
1295  ++base::__sz();
1296#  if _LIBCPP_STD_VER >= 17
1297  return __node->__get_value();
1298#  endif
1299}
1300
1301template <class _Tp, class _Alloc>
1302template <class... _Args>
1303typename list<_Tp, _Alloc>::iterator list<_Tp, _Alloc>::emplace(const_iterator __p, _Args&&... __args) {
1304  __node_pointer __node =
1305      this->__create_node(/* prev = */ nullptr, /* next = */ nullptr, std::forward<_Args>(__args)...);
1306  __link_pointer __nl = __node->__as_link();
1307  __link_nodes(__p.__ptr_, __nl, __nl);
1308  ++base::__sz();
1309  return iterator(__nl);
1310}
1311
1312template <class _Tp, class _Alloc>
1313typename list<_Tp, _Alloc>::iterator list<_Tp, _Alloc>::insert(const_iterator __p, value_type&& __x) {
1314  __node_pointer __node = this->__create_node(/* prev = */ nullptr, /* next = */ nullptr, std::move(__x));
1315  __link_pointer __nl   = __node->__as_link();
1316  __link_nodes(__p.__ptr_, __nl, __nl);
1317  ++base::__sz();
1318  return iterator(__nl);
1319}
1320
1321#endif // _LIBCPP_CXX03_LANG
1322
1323template <class _Tp, class _Alloc>
1324void list<_Tp, _Alloc>::pop_front() {
1325  _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "list::pop_front() called with empty list");
1326  __link_pointer __n = base::__end_.__next_;
1327  base::__unlink_nodes(__n, __n);
1328  --base::__sz();
1329  this->__delete_node(__n->__as_node());
1330}
1331
1332template <class _Tp, class _Alloc>
1333void list<_Tp, _Alloc>::pop_back() {
1334  _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "list::pop_back() called on an empty list");
1335  __link_pointer __n = base::__end_.__prev_;
1336  base::__unlink_nodes(__n, __n);
1337  --base::__sz();
1338  this->__delete_node(__n->__as_node());
1339}
1340
1341template <class _Tp, class _Alloc>
1342typename list<_Tp, _Alloc>::iterator list<_Tp, _Alloc>::erase(const_iterator __p) {
1343  _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__p != end(), "list::erase(iterator) called with a non-dereferenceable iterator");
1344  __link_pointer __n = __p.__ptr_;
1345  __link_pointer __r = __n->__next_;
1346  base::__unlink_nodes(__n, __n);
1347  --base::__sz();
1348  this->__delete_node(__n->__as_node());
1349  return iterator(__r);
1350}
1351
1352template <class _Tp, class _Alloc>
1353typename list<_Tp, _Alloc>::iterator list<_Tp, _Alloc>::erase(const_iterator __f, const_iterator __l) {
1354  if (__f != __l) {
1355    base::__unlink_nodes(__f.__ptr_, __l.__ptr_->__prev_);
1356    while (__f != __l) {
1357      __link_pointer __n = __f.__ptr_;
1358      ++__f;
1359      --base::__sz();
1360      this->__delete_node(__n->__as_node());
1361    }
1362  }
1363  return iterator(__l.__ptr_);
1364}
1365
1366template <class _Tp, class _Alloc>
1367void list<_Tp, _Alloc>::resize(size_type __n) {
1368  if (__n < base::__sz())
1369    erase(__iterator(__n), end());
1370  else if (__n > base::__sz()) {
1371    __n -= base::__sz();
1372    size_type __ds        = 0;
1373    __node_pointer __node = this->__create_node(/* prev = */ nullptr, /* next = */ nullptr);
1374    ++__ds;
1375    iterator __r = iterator(__node->__as_link());
1376    iterator __e = __r;
1377#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1378    try {
1379#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1380      for (--__n; __n != 0; --__n, (void)++__e, ++__ds) {
1381        __e.__ptr_->__next_ = this->__create_node(/* prev = */ __e.__ptr_, /* next = */ nullptr)->__as_link();
1382      }
1383#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1384    } catch (...) {
1385      while (true) {
1386        __link_pointer __prev    = __e.__ptr_->__prev_;
1387        __node_pointer __current = __e.__ptr_->__as_node();
1388        this->__delete_node(__current);
1389        if (__prev == 0)
1390          break;
1391        __e = iterator(__prev);
1392      }
1393      throw;
1394    }
1395#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1396    __link_nodes_at_back(__r.__ptr_, __e.__ptr_);
1397    base::__sz() += __ds;
1398  }
1399}
1400
1401template <class _Tp, class _Alloc>
1402void list<_Tp, _Alloc>::resize(size_type __n, const value_type& __x) {
1403  if (__n < base::__sz())
1404    erase(__iterator(__n), end());
1405  else if (__n > base::__sz()) {
1406    __n -= base::__sz();
1407    size_type __ds        = 0;
1408    __node_pointer __node = this->__create_node(/* prev = */ nullptr, /* next = */ nullptr, __x);
1409    ++__ds;
1410    __link_pointer __nl = __node->__as_link();
1411    iterator __r        = iterator(__nl);
1412    iterator __e        = __r;
1413#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1414    try {
1415#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1416      for (--__n; __n != 0; --__n, (void)++__e, ++__ds) {
1417        __e.__ptr_->__next_ = this->__create_node(/* prev = */ __e.__ptr_, /* next = */ nullptr, __x)->__as_link();
1418      }
1419#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1420    } catch (...) {
1421      while (true) {
1422        __link_pointer __prev    = __e.__ptr_->__prev_;
1423        __node_pointer __current = __e.__ptr_->__as_node();
1424        this->__delete_node(__current);
1425        if (__prev == 0)
1426          break;
1427        __e = iterator(__prev);
1428      }
1429      throw;
1430    }
1431#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1432    __link_nodes(base::__end_as_link(), __r.__ptr_, __e.__ptr_);
1433    base::__sz() += __ds;
1434  }
1435}
1436
1437template <class _Tp, class _Alloc>
1438void list<_Tp, _Alloc>::splice(const_iterator __p, list& __c) {
1439  _LIBCPP_ASSERT_VALID_INPUT_RANGE(
1440      this != std::addressof(__c), "list::splice(iterator, list) called with this == &list");
1441  if (!__c.empty()) {
1442    __link_pointer __f = __c.__end_.__next_;
1443    __link_pointer __l = __c.__end_.__prev_;
1444    base::__unlink_nodes(__f, __l);
1445    __link_nodes(__p.__ptr_, __f, __l);
1446    base::__sz() += __c.__sz();
1447    __c.__sz() = 0;
1448  }
1449}
1450
1451template <class _Tp, class _Alloc>
1452void list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __i) {
1453  if (__p.__ptr_ != __i.__ptr_ && __p.__ptr_ != __i.__ptr_->__next_) {
1454    __link_pointer __f = __i.__ptr_;
1455    base::__unlink_nodes(__f, __f);
1456    __link_nodes(__p.__ptr_, __f, __f);
1457    --__c.__sz();
1458    ++base::__sz();
1459  }
1460}
1461
1462template <class _Tp, class _Alloc>
1463void list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l) {
1464  if (__f != __l) {
1465    __link_pointer __first = __f.__ptr_;
1466    --__l;
1467    __link_pointer __last = __l.__ptr_;
1468    if (this != std::addressof(__c)) {
1469      size_type __s = std::distance(__f, __l) + 1;
1470      __c.__sz() -= __s;
1471      base::__sz() += __s;
1472    }
1473    base::__unlink_nodes(__first, __last);
1474    __link_nodes(__p.__ptr_, __first, __last);
1475  }
1476}
1477
1478template <class _Tp, class _Alloc>
1479typename list<_Tp, _Alloc>::__remove_return_type list<_Tp, _Alloc>::remove(const value_type& __x) {
1480  list<_Tp, _Alloc> __deleted_nodes(get_allocator()); // collect the nodes we're removing
1481  for (const_iterator __i = begin(), __e = end(); __i != __e;) {
1482    if (*__i == __x) {
1483      const_iterator __j = std::next(__i);
1484      for (; __j != __e && *__j == __x; ++__j)
1485        ;
1486      __deleted_nodes.splice(__deleted_nodes.end(), *this, __i, __j);
1487      __i = __j;
1488      if (__i != __e)
1489        ++__i;
1490    } else
1491      ++__i;
1492  }
1493
1494  return (__remove_return_type)__deleted_nodes.size();
1495}
1496
1497template <class _Tp, class _Alloc>
1498template <class _Pred>
1499typename list<_Tp, _Alloc>::__remove_return_type list<_Tp, _Alloc>::remove_if(_Pred __pred) {
1500  list<_Tp, _Alloc> __deleted_nodes(get_allocator()); // collect the nodes we're removing
1501  for (iterator __i = begin(), __e = end(); __i != __e;) {
1502    if (__pred(*__i)) {
1503      iterator __j = std::next(__i);
1504      for (; __j != __e && __pred(*__j); ++__j)
1505        ;
1506      __deleted_nodes.splice(__deleted_nodes.end(), *this, __i, __j);
1507      __i = __j;
1508      if (__i != __e)
1509        ++__i;
1510    } else
1511      ++__i;
1512  }
1513
1514  return (__remove_return_type)__deleted_nodes.size();
1515}
1516
1517template <class _Tp, class _Alloc>
1518template <class _BinaryPred>
1519typename list<_Tp, _Alloc>::__remove_return_type list<_Tp, _Alloc>::unique(_BinaryPred __binary_pred) {
1520  list<_Tp, _Alloc> __deleted_nodes(get_allocator()); // collect the nodes we're removing
1521  for (iterator __i = begin(), __e = end(); __i != __e;) {
1522    iterator __j = std::next(__i);
1523    for (; __j != __e && __binary_pred(*__i, *__j); ++__j)
1524      ;
1525    if (++__i != __j) {
1526      __deleted_nodes.splice(__deleted_nodes.end(), *this, __i, __j);
1527      __i = __j;
1528    }
1529  }
1530
1531  return (__remove_return_type)__deleted_nodes.size();
1532}
1533
1534template <class _Tp, class _Alloc>
1535inline void list<_Tp, _Alloc>::merge(list& __c) {
1536  merge(__c, __less<>());
1537}
1538
1539template <class _Tp, class _Alloc>
1540template <class _Comp>
1541void list<_Tp, _Alloc>::merge(list& __c, _Comp __comp) {
1542  if (this != std::addressof(__c)) {
1543    iterator __f1 = begin();
1544    iterator __e1 = end();
1545    iterator __f2 = __c.begin();
1546    iterator __e2 = __c.end();
1547    while (__f1 != __e1 && __f2 != __e2) {
1548      if (__comp(*__f2, *__f1)) {
1549        size_type __ds = 1;
1550        iterator __m2  = std::next(__f2);
1551        for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2, (void)++__ds)
1552          ;
1553        base::__sz() += __ds;
1554        __c.__sz() -= __ds;
1555        __link_pointer __f = __f2.__ptr_;
1556        __link_pointer __l = __m2.__ptr_->__prev_;
1557        __f2               = __m2;
1558        base::__unlink_nodes(__f, __l);
1559        __m2 = std::next(__f1);
1560        __link_nodes(__f1.__ptr_, __f, __l);
1561        __f1 = __m2;
1562      } else
1563        ++__f1;
1564    }
1565    splice(__e1, __c);
1566  }
1567}
1568
1569template <class _Tp, class _Alloc>
1570inline void list<_Tp, _Alloc>::sort() {
1571  sort(__less<>());
1572}
1573
1574template <class _Tp, class _Alloc>
1575template <class _Comp>
1576inline void list<_Tp, _Alloc>::sort(_Comp __comp) {
1577  __sort(begin(), end(), base::__sz(), __comp);
1578}
1579
1580template <class _Tp, class _Alloc>
1581template <class _Comp>
1582typename list<_Tp, _Alloc>::iterator
1583list<_Tp, _Alloc>::__sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp) {
1584  switch (__n) {
1585  case 0:
1586  case 1:
1587    return __f1;
1588  case 2:
1589    if (__comp(*--__e2, *__f1)) {
1590      __link_pointer __f = __e2.__ptr_;
1591      base::__unlink_nodes(__f, __f);
1592      __link_nodes(__f1.__ptr_, __f, __f);
1593      return __e2;
1594    }
1595    return __f1;
1596  }
1597  size_type __n2 = __n / 2;
1598  iterator __e1  = std::next(__f1, __n2);
1599  iterator __r = __f1 = __sort(__f1, __e1, __n2, __comp);
1600  iterator __f2 = __e1 = __sort(__e1, __e2, __n - __n2, __comp);
1601  if (__comp(*__f2, *__f1)) {
1602    iterator __m2 = std::next(__f2);
1603    for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2)
1604      ;
1605    __link_pointer __f = __f2.__ptr_;
1606    __link_pointer __l = __m2.__ptr_->__prev_;
1607    __r                = __f2;
1608    __e1 = __f2 = __m2;
1609    base::__unlink_nodes(__f, __l);
1610    __m2 = std::next(__f1);
1611    __link_nodes(__f1.__ptr_, __f, __l);
1612    __f1 = __m2;
1613  } else
1614    ++__f1;
1615  while (__f1 != __e1 && __f2 != __e2) {
1616    if (__comp(*__f2, *__f1)) {
1617      iterator __m2 = std::next(__f2);
1618      for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2)
1619        ;
1620      __link_pointer __f = __f2.__ptr_;
1621      __link_pointer __l = __m2.__ptr_->__prev_;
1622      if (__e1 == __f2)
1623        __e1 = __m2;
1624      __f2 = __m2;
1625      base::__unlink_nodes(__f, __l);
1626      __m2 = std::next(__f1);
1627      __link_nodes(__f1.__ptr_, __f, __l);
1628      __f1 = __m2;
1629    } else
1630      ++__f1;
1631  }
1632  return __r;
1633}
1634
1635template <class _Tp, class _Alloc>
1636void list<_Tp, _Alloc>::reverse() _NOEXCEPT {
1637  if (base::__sz() > 1) {
1638    iterator __e = end();
1639    for (iterator __i = begin(); __i.__ptr_ != __e.__ptr_;) {
1640      std::swap(__i.__ptr_->__prev_, __i.__ptr_->__next_);
1641      __i.__ptr_ = __i.__ptr_->__prev_;
1642    }
1643    std::swap(__e.__ptr_->__prev_, __e.__ptr_->__next_);
1644  }
1645}
1646
1647template <class _Tp, class _Alloc>
1648bool list<_Tp, _Alloc>::__invariants() const {
1649  return size() == std::distance(begin(), end());
1650}
1651
1652template <class _Tp, class _Alloc>
1653inline _LIBCPP_HIDE_FROM_ABI bool operator==(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y) {
1654  return __x.size() == __y.size() && std::equal(__x.begin(), __x.end(), __y.begin());
1655}
1656
1657#if _LIBCPP_STD_VER <= 17
1658
1659template <class _Tp, class _Alloc>
1660inline _LIBCPP_HIDE_FROM_ABI bool operator<(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y) {
1661  return std::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
1662}
1663
1664template <class _Tp, class _Alloc>
1665inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y) {
1666  return !(__x == __y);
1667}
1668
1669template <class _Tp, class _Alloc>
1670inline _LIBCPP_HIDE_FROM_ABI bool operator>(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y) {
1671  return __y < __x;
1672}
1673
1674template <class _Tp, class _Alloc>
1675inline _LIBCPP_HIDE_FROM_ABI bool operator>=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y) {
1676  return !(__x < __y);
1677}
1678
1679template <class _Tp, class _Alloc>
1680inline _LIBCPP_HIDE_FROM_ABI bool operator<=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y) {
1681  return !(__y < __x);
1682}
1683
1684#else // _LIBCPP_STD_VER <= 17
1685
1686template <class _Tp, class _Allocator>
1687_LIBCPP_HIDE_FROM_ABI __synth_three_way_result<_Tp>
1688operator<=>(const list<_Tp, _Allocator>& __x, const list<_Tp, _Allocator>& __y) {
1689  return std::lexicographical_compare_three_way(
1690      __x.begin(), __x.end(), __y.begin(), __y.end(), std::__synth_three_way<_Tp, _Tp>);
1691}
1692
1693#endif // _LIBCPP_STD_VER <= 17
1694
1695template <class _Tp, class _Alloc>
1696inline _LIBCPP_HIDE_FROM_ABI void swap(list<_Tp, _Alloc>& __x, list<_Tp, _Alloc>& __y)
1697    _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y))) {
1698  __x.swap(__y);
1699}
1700
1701#if _LIBCPP_STD_VER >= 20
1702template <class _Tp, class _Allocator, class _Predicate>
1703inline _LIBCPP_HIDE_FROM_ABI typename list<_Tp, _Allocator>::size_type
1704erase_if(list<_Tp, _Allocator>& __c, _Predicate __pred) {
1705  return __c.remove_if(__pred);
1706}
1707
1708template <class _Tp, class _Allocator, class _Up>
1709inline _LIBCPP_HIDE_FROM_ABI typename list<_Tp, _Allocator>::size_type
1710erase(list<_Tp, _Allocator>& __c, const _Up& __v) {
1711  return std::erase_if(__c, [&](auto& __elem) { return __elem == __v; });
1712}
1713
1714template <>
1715inline constexpr bool __format::__enable_insertable<std::list<char>> = true;
1716#  ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1717template <>
1718inline constexpr bool __format::__enable_insertable<std::list<wchar_t>> = true;
1719#  endif
1720
1721#endif // _LIBCPP_STD_VER >= 20
1722
1723_LIBCPP_END_NAMESPACE_STD
1724
1725#if _LIBCPP_STD_VER >= 17
1726_LIBCPP_BEGIN_NAMESPACE_STD
1727namespace pmr {
1728template <class _ValueT>
1729using list _LIBCPP_AVAILABILITY_PMR = std::list<_ValueT, polymorphic_allocator<_ValueT>>;
1730} // namespace pmr
1731_LIBCPP_END_NAMESPACE_STD
1732#endif
1733
1734_LIBCPP_POP_MACROS
1735
1736#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
1737#  include <algorithm>
1738#  include <atomic>
1739#  include <concepts>
1740#  include <cstdint>
1741#  include <cstdlib>
1742#  include <functional>
1743#  include <iosfwd>
1744#  include <iterator>
1745#  include <stdexcept>
1746#  include <type_traits>
1747#  include <typeinfo>
1748#endif
1749
1750#endif // _LIBCPP_LIST
1751