xref: /freebsd/contrib/llvm-project/libcxx/include/list (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric// -*- C++ -*-
2349cc55cSDimitry Andric//===----------------------------------------------------------------------===//
30b57cec5SDimitry Andric//
40b57cec5SDimitry Andric// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
50b57cec5SDimitry Andric// See https://llvm.org/LICENSE.txt for license information.
60b57cec5SDimitry Andric// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
70b57cec5SDimitry Andric//
80b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
90b57cec5SDimitry Andric
100b57cec5SDimitry Andric#ifndef _LIBCPP_LIST
110b57cec5SDimitry Andric#define _LIBCPP_LIST
120b57cec5SDimitry Andric
130b57cec5SDimitry Andric/*
140b57cec5SDimitry Andric    list synopsis
150b57cec5SDimitry Andric
160b57cec5SDimitry Andricnamespace std
170b57cec5SDimitry Andric{
180b57cec5SDimitry Andric
190b57cec5SDimitry Andrictemplate <class T, class Alloc = allocator<T> >
200b57cec5SDimitry Andricclass list
210b57cec5SDimitry Andric{
220b57cec5SDimitry Andricpublic:
230b57cec5SDimitry Andric
240b57cec5SDimitry Andric    // types:
250b57cec5SDimitry Andric    typedef T value_type;
260b57cec5SDimitry Andric    typedef Alloc allocator_type;
270b57cec5SDimitry Andric    typedef typename allocator_type::reference reference;
280b57cec5SDimitry Andric    typedef typename allocator_type::const_reference const_reference;
290b57cec5SDimitry Andric    typedef typename allocator_type::pointer pointer;
300b57cec5SDimitry Andric    typedef typename allocator_type::const_pointer const_pointer;
310b57cec5SDimitry Andric    typedef implementation-defined iterator;
320b57cec5SDimitry Andric    typedef implementation-defined const_iterator;
330b57cec5SDimitry Andric    typedef implementation-defined size_type;
340b57cec5SDimitry Andric    typedef implementation-defined difference_type;
350b57cec5SDimitry Andric    typedef reverse_iterator<iterator> reverse_iterator;
360b57cec5SDimitry Andric    typedef reverse_iterator<const_iterator> const_reverse_iterator;
370b57cec5SDimitry Andric
380b57cec5SDimitry Andric    list()
390b57cec5SDimitry Andric        noexcept(is_nothrow_default_constructible<allocator_type>::value);
400b57cec5SDimitry Andric    explicit list(const allocator_type& a);
410b57cec5SDimitry Andric    explicit list(size_type n);
420b57cec5SDimitry Andric    explicit list(size_type n, const allocator_type& a); // C++14
430b57cec5SDimitry Andric    list(size_type n, const value_type& value);
440b57cec5SDimitry Andric    list(size_type n, const value_type& value, const allocator_type& a);
450b57cec5SDimitry Andric    template <class Iter>
460b57cec5SDimitry Andric        list(Iter first, Iter last);
470b57cec5SDimitry Andric    template <class Iter>
480b57cec5SDimitry Andric        list(Iter first, Iter last, const allocator_type& a);
4906c3fb27SDimitry Andric    template<container-compatible-range<T> R>
5006c3fb27SDimitry Andric      list(from_range_t, R&& rg, const Allocator& = Allocator()); // C++23
510b57cec5SDimitry Andric    list(const list& x);
520b57cec5SDimitry Andric    list(const list&, const allocator_type& a);
530b57cec5SDimitry Andric    list(list&& x)
540b57cec5SDimitry Andric        noexcept(is_nothrow_move_constructible<allocator_type>::value);
550b57cec5SDimitry Andric    list(list&&, const allocator_type& a);
560b57cec5SDimitry Andric    list(initializer_list<value_type>);
570b57cec5SDimitry Andric    list(initializer_list<value_type>, const allocator_type& a);
580b57cec5SDimitry Andric
590b57cec5SDimitry Andric    ~list();
600b57cec5SDimitry Andric
610b57cec5SDimitry Andric    list& operator=(const list& x);
620b57cec5SDimitry Andric    list& operator=(list&& x)
630b57cec5SDimitry Andric        noexcept(
640b57cec5SDimitry Andric             allocator_type::propagate_on_container_move_assignment::value &&
650b57cec5SDimitry Andric             is_nothrow_move_assignable<allocator_type>::value);
660b57cec5SDimitry Andric    list& operator=(initializer_list<value_type>);
670b57cec5SDimitry Andric    template <class Iter>
680b57cec5SDimitry Andric        void assign(Iter first, Iter last);
6906c3fb27SDimitry Andric    template<container-compatible-range<T> R>
7006c3fb27SDimitry Andric      void assign_range(R&& rg); // C++23
710b57cec5SDimitry Andric    void assign(size_type n, const value_type& t);
720b57cec5SDimitry Andric    void assign(initializer_list<value_type>);
730b57cec5SDimitry Andric
740b57cec5SDimitry Andric    allocator_type get_allocator() const noexcept;
750b57cec5SDimitry Andric
760b57cec5SDimitry Andric    iterator begin() noexcept;
770b57cec5SDimitry Andric    const_iterator begin() const noexcept;
780b57cec5SDimitry Andric    iterator end() noexcept;
790b57cec5SDimitry Andric    const_iterator end() const noexcept;
800b57cec5SDimitry Andric    reverse_iterator rbegin() noexcept;
810b57cec5SDimitry Andric    const_reverse_iterator rbegin() const noexcept;
820b57cec5SDimitry Andric    reverse_iterator rend() noexcept;
830b57cec5SDimitry Andric    const_reverse_iterator rend() const noexcept;
840b57cec5SDimitry Andric    const_iterator cbegin() const noexcept;
850b57cec5SDimitry Andric    const_iterator cend() const noexcept;
860b57cec5SDimitry Andric    const_reverse_iterator crbegin() const noexcept;
870b57cec5SDimitry Andric    const_reverse_iterator crend() const noexcept;
880b57cec5SDimitry Andric
890b57cec5SDimitry Andric    reference front();
900b57cec5SDimitry Andric    const_reference front() const;
910b57cec5SDimitry Andric    reference back();
920b57cec5SDimitry Andric    const_reference back() const;
930b57cec5SDimitry Andric
940b57cec5SDimitry Andric    bool empty() const noexcept;
950b57cec5SDimitry Andric    size_type size() const noexcept;
960b57cec5SDimitry Andric    size_type max_size() const noexcept;
970b57cec5SDimitry Andric
980b57cec5SDimitry Andric    template <class... Args>
990b57cec5SDimitry Andric        reference emplace_front(Args&&... args); // reference in C++17
1000b57cec5SDimitry Andric    void pop_front();
1010b57cec5SDimitry Andric    template <class... Args>
1020b57cec5SDimitry Andric        reference emplace_back(Args&&... args);  // reference in C++17
1030b57cec5SDimitry Andric    void pop_back();
1040b57cec5SDimitry Andric    void push_front(const value_type& x);
1050b57cec5SDimitry Andric    void push_front(value_type&& x);
10606c3fb27SDimitry Andric    template<container-compatible-range<T> R>
10706c3fb27SDimitry Andric      void prepend_range(R&& rg); // C++23
1080b57cec5SDimitry Andric    void push_back(const value_type& x);
1090b57cec5SDimitry Andric    void push_back(value_type&& x);
11006c3fb27SDimitry Andric    template<container-compatible-range<T> R>
11106c3fb27SDimitry Andric      void append_range(R&& rg); // C++23
1120b57cec5SDimitry Andric    template <class... Args>
1130b57cec5SDimitry Andric        iterator emplace(const_iterator position, Args&&... args);
1140b57cec5SDimitry Andric    iterator insert(const_iterator position, const value_type& x);
1150b57cec5SDimitry Andric    iterator insert(const_iterator position, value_type&& x);
1160b57cec5SDimitry Andric    iterator insert(const_iterator position, size_type n, const value_type& x);
1170b57cec5SDimitry Andric    template <class Iter>
1180b57cec5SDimitry Andric        iterator insert(const_iterator position, Iter first, Iter last);
11906c3fb27SDimitry Andric    template<container-compatible-range<T> R>
12006c3fb27SDimitry Andric      iterator insert_range(const_iterator position, R&& rg); // C++23
1210b57cec5SDimitry Andric    iterator insert(const_iterator position, initializer_list<value_type> il);
1220b57cec5SDimitry Andric
1230b57cec5SDimitry Andric    iterator erase(const_iterator position);
1240b57cec5SDimitry Andric    iterator erase(const_iterator position, const_iterator last);
1250b57cec5SDimitry Andric
1260b57cec5SDimitry Andric    void resize(size_type sz);
1270b57cec5SDimitry Andric    void resize(size_type sz, const value_type& c);
1280b57cec5SDimitry Andric
1290b57cec5SDimitry Andric    void swap(list&)
1300b57cec5SDimitry Andric        noexcept(allocator_traits<allocator_type>::is_always_equal::value);  // C++17
1310b57cec5SDimitry Andric    void clear() noexcept;
1320b57cec5SDimitry Andric
1330b57cec5SDimitry Andric    void splice(const_iterator position, list& x);
1340b57cec5SDimitry Andric    void splice(const_iterator position, list&& x);
1350b57cec5SDimitry Andric    void splice(const_iterator position, list& x, const_iterator i);
1360b57cec5SDimitry Andric    void splice(const_iterator position, list&& x, const_iterator i);
1370b57cec5SDimitry Andric    void splice(const_iterator position, list& x, const_iterator first,
1380b57cec5SDimitry Andric                                                  const_iterator last);
1390b57cec5SDimitry Andric    void splice(const_iterator position, list&& x, const_iterator first,
1400b57cec5SDimitry Andric                                                  const_iterator last);
1410b57cec5SDimitry Andric
1420b57cec5SDimitry Andric    size_type remove(const value_type& value);       // void before C++20
1430b57cec5SDimitry Andric    template <class Pred>
1440b57cec5SDimitry Andric      size_type remove_if(Pred pred);                // void before C++20
1450b57cec5SDimitry Andric    size_type unique();                              // void before C++20
1460b57cec5SDimitry Andric    template <class BinaryPredicate>
1470b57cec5SDimitry Andric      size_type unique(BinaryPredicate binary_pred); // void before C++20
1480b57cec5SDimitry Andric    void merge(list& x);
1490b57cec5SDimitry Andric    void merge(list&& x);
1500b57cec5SDimitry Andric    template <class Compare>
1510b57cec5SDimitry Andric        void merge(list& x, Compare comp);
1520b57cec5SDimitry Andric    template <class Compare>
1530b57cec5SDimitry Andric        void merge(list&& x, Compare comp);
1540b57cec5SDimitry Andric    void sort();
1550b57cec5SDimitry Andric    template <class Compare>
1560b57cec5SDimitry Andric        void sort(Compare comp);
1570b57cec5SDimitry Andric    void reverse() noexcept;
1580b57cec5SDimitry Andric};
1590b57cec5SDimitry Andric
1600b57cec5SDimitry Andric
1610b57cec5SDimitry Andrictemplate <class InputIterator, class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
1620b57cec5SDimitry Andric    list(InputIterator, InputIterator, Allocator = Allocator())
1630b57cec5SDimitry Andric    -> list<typename iterator_traits<InputIterator>::value_type, Allocator>;  // C++17
1640b57cec5SDimitry Andric
16506c3fb27SDimitry Andrictemplate<ranges::input_range R, class Allocator = allocator<ranges::range_value_t<R>>>
16606c3fb27SDimitry Andric  list(from_range_t, R&&, Allocator = Allocator())
16706c3fb27SDimitry Andric    -> list<ranges::range_value_t<R>, Allocator>; // C++23
16806c3fb27SDimitry Andric
1690b57cec5SDimitry Andrictemplate <class T, class Alloc>
1700b57cec5SDimitry Andric    bool operator==(const list<T,Alloc>& x, const list<T,Alloc>& y);
1710b57cec5SDimitry Andrictemplate <class T, class Alloc>
17206c3fb27SDimitry Andric    bool operator< (const list<T,Alloc>& x, const list<T,Alloc>& y);     // removed in C++20
1730b57cec5SDimitry Andrictemplate <class T, class Alloc>
17406c3fb27SDimitry Andric    bool operator!=(const list<T,Alloc>& x, const list<T,Alloc>& y);     // removed in C++20
1750b57cec5SDimitry Andrictemplate <class T, class Alloc>
17606c3fb27SDimitry Andric    bool operator> (const list<T,Alloc>& x, const list<T,Alloc>& y);     // removed in C++20
1770b57cec5SDimitry Andrictemplate <class T, class Alloc>
17806c3fb27SDimitry Andric    bool operator>=(const list<T,Alloc>& x, const list<T,Alloc>& y);     // removed in C++20
1790b57cec5SDimitry Andrictemplate <class T, class Alloc>
18006c3fb27SDimitry Andric    bool operator<=(const list<T,Alloc>& x, const list<T,Alloc>& y);     // removed in C++20
18106c3fb27SDimitry Andrictemplate<class T, class Allocator>
18206c3fb27SDimitry Andric  synth-three-way-result<T> operator<=>(const list<T, Allocator>& x,
18306c3fb27SDimitry Andric                                        const list<T, Allocator>& y);    // since C++20
1840b57cec5SDimitry Andric
1850b57cec5SDimitry Andrictemplate <class T, class Alloc>
1860b57cec5SDimitry Andric    void swap(list<T,Alloc>& x, list<T,Alloc>& y)
1870b57cec5SDimitry Andric         noexcept(noexcept(x.swap(y)));
1880b57cec5SDimitry Andric
1890b57cec5SDimitry Andrictemplate <class T, class Allocator, class U>
1905ffd83dbSDimitry Andric    typename list<T, Allocator>::size_type
19106c3fb27SDimitry Andric    erase(list<T, Allocator>& c, const U& value);       // since C++20
1920b57cec5SDimitry Andrictemplate <class T, class Allocator, class Predicate>
1935ffd83dbSDimitry Andric    typename list<T, Allocator>::size_type
19406c3fb27SDimitry Andric    erase_if(list<T, Allocator>& c, Predicate pred);    // since C++20
1950b57cec5SDimitry Andric
1960b57cec5SDimitry Andric}  // std
1970b57cec5SDimitry Andric
1980b57cec5SDimitry Andric*/
1990b57cec5SDimitry Andric
20081ad6265SDimitry Andric#include <__algorithm/comp.h>
20181ad6265SDimitry Andric#include <__algorithm/equal.h>
20281ad6265SDimitry Andric#include <__algorithm/lexicographical_compare.h>
20306c3fb27SDimitry Andric#include <__algorithm/lexicographical_compare_three_way.h>
20481ad6265SDimitry Andric#include <__algorithm/min.h>
205*0fca6ea1SDimitry Andric#include <__assert>
2060b57cec5SDimitry Andric#include <__config>
20781ad6265SDimitry Andric#include <__format/enable_insertable.h>
20881ad6265SDimitry Andric#include <__iterator/distance.h>
20981ad6265SDimitry Andric#include <__iterator/iterator_traits.h>
21081ad6265SDimitry Andric#include <__iterator/move_iterator.h>
21181ad6265SDimitry Andric#include <__iterator/next.h>
21281ad6265SDimitry Andric#include <__iterator/prev.h>
21381ad6265SDimitry Andric#include <__iterator/reverse_iterator.h>
214bdd1243dSDimitry Andric#include <__memory/addressof.h>
2155f757f3fSDimitry Andric#include <__memory/allocation_guard.h>
216bdd1243dSDimitry Andric#include <__memory/allocator.h>
217bdd1243dSDimitry Andric#include <__memory/allocator_traits.h>
218bdd1243dSDimitry Andric#include <__memory/compressed_pair.h>
2195f757f3fSDimitry Andric#include <__memory/construct_at.h>
220bdd1243dSDimitry Andric#include <__memory/pointer_traits.h>
221972a253aSDimitry Andric#include <__memory/swap_allocator.h>
222bdd1243dSDimitry Andric#include <__memory_resource/polymorphic_allocator.h>
22306c3fb27SDimitry Andric#include <__ranges/access.h>
22406c3fb27SDimitry Andric#include <__ranges/concepts.h>
22506c3fb27SDimitry Andric#include <__ranges/container_compatible_range.h>
22606c3fb27SDimitry Andric#include <__ranges/from_range.h>
22706c3fb27SDimitry Andric#include <__type_traits/conditional.h>
228bdd1243dSDimitry Andric#include <__type_traits/is_allocator.h>
229*0fca6ea1SDimitry Andric#include <__type_traits/is_nothrow_assignable.h>
230*0fca6ea1SDimitry Andric#include <__type_traits/is_nothrow_constructible.h>
23106c3fb27SDimitry Andric#include <__type_traits/is_pointer.h>
23206c3fb27SDimitry Andric#include <__type_traits/is_same.h>
23306c3fb27SDimitry Andric#include <__type_traits/type_identity.h>
234fe6060f1SDimitry Andric#include <__utility/forward.h>
23581ad6265SDimitry Andric#include <__utility/move.h>
23681ad6265SDimitry Andric#include <__utility/swap.h>
237bdd1243dSDimitry Andric#include <cstring>
238fe6060f1SDimitry Andric#include <limits>
2395f757f3fSDimitry Andric#include <new> // __launder
2400b57cec5SDimitry Andric#include <version>
2410b57cec5SDimitry Andric
24281ad6265SDimitry Andric// standard-mandated includes
24381ad6265SDimitry Andric
24481ad6265SDimitry Andric// [iterator.range]
24581ad6265SDimitry Andric#include <__iterator/access.h>
24681ad6265SDimitry Andric#include <__iterator/data.h>
24781ad6265SDimitry Andric#include <__iterator/empty.h>
24881ad6265SDimitry Andric#include <__iterator/reverse_access.h>
24981ad6265SDimitry Andric#include <__iterator/size.h>
25081ad6265SDimitry Andric
25181ad6265SDimitry Andric// [list.syn]
25281ad6265SDimitry Andric#include <compare>
25381ad6265SDimitry Andric#include <initializer_list>
25481ad6265SDimitry Andric
2550b57cec5SDimitry Andric#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
2560b57cec5SDimitry Andric#  pragma GCC system_header
2570b57cec5SDimitry Andric#endif
2580b57cec5SDimitry Andric
2590b57cec5SDimitry Andric_LIBCPP_PUSH_MACROS
2600b57cec5SDimitry Andric#include <__undef_macros>
2610b57cec5SDimitry Andric
2620b57cec5SDimitry Andric_LIBCPP_BEGIN_NAMESPACE_STD
2630b57cec5SDimitry Andric
264cb14a3feSDimitry Andrictemplate <class _Tp, class _VoidPtr>
265cb14a3feSDimitry Andricstruct __list_node;
266cb14a3feSDimitry Andrictemplate <class _Tp, class _VoidPtr>
267cb14a3feSDimitry Andricstruct __list_node_base;
2680b57cec5SDimitry Andric
2690b57cec5SDimitry Andrictemplate <class _Tp, class _VoidPtr>
2700b57cec5SDimitry Andricstruct __list_node_pointer_traits {
271cb14a3feSDimitry Andric  typedef __rebind_pointer_t<_VoidPtr, __list_node<_Tp, _VoidPtr> > __node_pointer;
272cb14a3feSDimitry Andric  typedef __rebind_pointer_t<_VoidPtr, __list_node_base<_Tp, _VoidPtr> > __base_pointer;
2730b57cec5SDimitry Andric
2740b57cec5SDimitry Andric#if defined(_LIBCPP_ABI_LIST_REMOVE_NODE_POINTER_UB)
2750b57cec5SDimitry Andric  typedef __base_pointer __link_pointer;
2760b57cec5SDimitry Andric#else
277bdd1243dSDimitry Andric  typedef __conditional_t<is_pointer<_VoidPtr>::value, __base_pointer, __node_pointer> __link_pointer;
2780b57cec5SDimitry Andric#endif
2790b57cec5SDimitry Andric
280bdd1243dSDimitry Andric  typedef __conditional_t<is_same<__link_pointer, __node_pointer>::value, __base_pointer, __node_pointer>
281bdd1243dSDimitry Andric      __non_link_pointer;
2820b57cec5SDimitry Andric
283cb14a3feSDimitry Andric  static _LIBCPP_HIDE_FROM_ABI __link_pointer __unsafe_link_pointer_cast(__link_pointer __p) { return __p; }
2840b57cec5SDimitry Andric
285cb14a3feSDimitry Andric  static _LIBCPP_HIDE_FROM_ABI __link_pointer __unsafe_link_pointer_cast(__non_link_pointer __p) {
2860b57cec5SDimitry Andric    return static_cast<__link_pointer>(static_cast<_VoidPtr>(__p));
2870b57cec5SDimitry Andric  }
2880b57cec5SDimitry Andric};
2890b57cec5SDimitry Andric
2900b57cec5SDimitry Andrictemplate <class _Tp, class _VoidPtr>
291cb14a3feSDimitry Andricstruct __list_node_base {
2920b57cec5SDimitry Andric  typedef __list_node_pointer_traits<_Tp, _VoidPtr> _NodeTraits;
2930b57cec5SDimitry Andric  typedef typename _NodeTraits::__node_pointer __node_pointer;
2940b57cec5SDimitry Andric  typedef typename _NodeTraits::__base_pointer __base_pointer;
2950b57cec5SDimitry Andric  typedef typename _NodeTraits::__link_pointer __link_pointer;
2960b57cec5SDimitry Andric
2970b57cec5SDimitry Andric  __link_pointer __prev_;
2980b57cec5SDimitry Andric  __link_pointer __next_;
2990b57cec5SDimitry Andric
300cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI __list_node_base()
301cb14a3feSDimitry Andric      : __prev_(_NodeTraits::__unsafe_link_pointer_cast(__self())),
3020b57cec5SDimitry Andric        __next_(_NodeTraits::__unsafe_link_pointer_cast(__self())) {}
3030b57cec5SDimitry Andric
3045f757f3fSDimitry Andric  _LIBCPP_HIDE_FROM_ABI explicit __list_node_base(__link_pointer __prev, __link_pointer __next)
3055f757f3fSDimitry Andric      : __prev_(__prev), __next_(__next) {}
3065f757f3fSDimitry Andric
307cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI __base_pointer __self() { return pointer_traits<__base_pointer>::pointer_to(*this); }
3080b57cec5SDimitry Andric
309cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI __node_pointer __as_node() { return static_cast<__node_pointer>(__self()); }
3100b57cec5SDimitry Andric};
3110b57cec5SDimitry Andric
3120b57cec5SDimitry Andrictemplate <class _Tp, class _VoidPtr>
313cb14a3feSDimitry Andricstruct __list_node : public __list_node_base<_Tp, _VoidPtr> {
3145f757f3fSDimitry Andric  // We allow starting the lifetime of nodes without initializing the value held by the node,
3155f757f3fSDimitry Andric  // since that is handled by the list itself in order to be allocator-aware.
3165f757f3fSDimitry Andric#ifndef _LIBCPP_CXX03_LANG
317cb14a3feSDimitry Andric
3185f757f3fSDimitry Andricprivate:
3195f757f3fSDimitry Andric  union {
3200b57cec5SDimitry Andric    _Tp __value_;
3215f757f3fSDimitry Andric  };
3225f757f3fSDimitry Andric
3235f757f3fSDimitry Andricpublic:
3245f757f3fSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _Tp& __get_value() { return __value_; }
3255f757f3fSDimitry Andric#else
326cb14a3feSDimitry Andric
3275f757f3fSDimitry Andricprivate:
3285f757f3fSDimitry Andric  _ALIGNAS_TYPE(_Tp) char __buffer_[sizeof(_Tp)];
3295f757f3fSDimitry Andric
3305f757f3fSDimitry Andricpublic:
331cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _Tp& __get_value() { return *std::__launder(reinterpret_cast<_Tp*>(&__buffer_)); }
3325f757f3fSDimitry Andric#endif
3330b57cec5SDimitry Andric
3340b57cec5SDimitry Andric  typedef __list_node_base<_Tp, _VoidPtr> __base;
3350b57cec5SDimitry Andric  typedef typename __base::__link_pointer __link_pointer;
3360b57cec5SDimitry Andric
3375f757f3fSDimitry Andric  _LIBCPP_HIDE_FROM_ABI explicit __list_node(__link_pointer __prev, __link_pointer __next) : __base(__prev, __next) {}
3385f757f3fSDimitry Andric  _LIBCPP_HIDE_FROM_ABI ~__list_node() {}
3395f757f3fSDimitry Andric
340cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI __link_pointer __as_link() { return static_cast<__link_pointer>(__base::__self()); }
3410b57cec5SDimitry Andric};
3420b57cec5SDimitry Andric
343cb14a3feSDimitry Andrictemplate <class _Tp, class _Alloc = allocator<_Tp> >
344cb14a3feSDimitry Andricclass _LIBCPP_TEMPLATE_VIS list;
345cb14a3feSDimitry Andrictemplate <class _Tp, class _Alloc>
346cb14a3feSDimitry Andricclass __list_imp;
347cb14a3feSDimitry Andrictemplate <class _Tp, class _VoidPtr>
348cb14a3feSDimitry Andricclass _LIBCPP_TEMPLATE_VIS __list_const_iterator;
3490b57cec5SDimitry Andric
3500b57cec5SDimitry Andrictemplate <class _Tp, class _VoidPtr>
351cb14a3feSDimitry Andricclass _LIBCPP_TEMPLATE_VIS __list_iterator {
3520b57cec5SDimitry Andric  typedef __list_node_pointer_traits<_Tp, _VoidPtr> _NodeTraits;
3530b57cec5SDimitry Andric  typedef typename _NodeTraits::__link_pointer __link_pointer;
3540b57cec5SDimitry Andric
3550b57cec5SDimitry Andric  __link_pointer __ptr_;
3560b57cec5SDimitry Andric
357cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI explicit __list_iterator(__link_pointer __p) _NOEXCEPT : __ptr_(__p) {}
3580b57cec5SDimitry Andric
359cb14a3feSDimitry Andric  template <class, class>
360cb14a3feSDimitry Andric  friend class list;
361cb14a3feSDimitry Andric  template <class, class>
362cb14a3feSDimitry Andric  friend class __list_imp;
363cb14a3feSDimitry Andric  template <class, class>
364cb14a3feSDimitry Andric  friend class __list_const_iterator;
365cb14a3feSDimitry Andric
3660b57cec5SDimitry Andricpublic:
3670b57cec5SDimitry Andric  typedef bidirectional_iterator_tag iterator_category;
3680b57cec5SDimitry Andric  typedef _Tp value_type;
3690b57cec5SDimitry Andric  typedef value_type& reference;
370bdd1243dSDimitry Andric  typedef __rebind_pointer_t<_VoidPtr, value_type> pointer;
3710b57cec5SDimitry Andric  typedef typename pointer_traits<pointer>::difference_type difference_type;
3720b57cec5SDimitry Andric
373cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI __list_iterator() _NOEXCEPT : __ptr_(nullptr) {}
3740b57cec5SDimitry Andric
375cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI reference operator*() const { return __ptr_->__as_node()->__get_value(); }
376cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI pointer operator->() const {
3775f757f3fSDimitry Andric    return pointer_traits<pointer>::pointer_to(__ptr_->__as_node()->__get_value());
3780b57cec5SDimitry Andric  }
3790b57cec5SDimitry Andric
380cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI __list_iterator& operator++() {
3810b57cec5SDimitry Andric    __ptr_ = __ptr_->__next_;
3820b57cec5SDimitry Andric    return *this;
3830b57cec5SDimitry Andric  }
384cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI __list_iterator operator++(int) {
385cb14a3feSDimitry Andric    __list_iterator __t(*this);
386cb14a3feSDimitry Andric    ++(*this);
387cb14a3feSDimitry Andric    return __t;
388cb14a3feSDimitry Andric  }
3890b57cec5SDimitry Andric
390cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI __list_iterator& operator--() {
3910b57cec5SDimitry Andric    __ptr_ = __ptr_->__prev_;
3920b57cec5SDimitry Andric    return *this;
3930b57cec5SDimitry Andric  }
394cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI __list_iterator operator--(int) {
395cb14a3feSDimitry Andric    __list_iterator __t(*this);
396cb14a3feSDimitry Andric    --(*this);
397cb14a3feSDimitry Andric    return __t;
398cb14a3feSDimitry Andric  }
3990b57cec5SDimitry Andric
400cb14a3feSDimitry Andric  friend _LIBCPP_HIDE_FROM_ABI bool operator==(const __list_iterator& __x, const __list_iterator& __y) {
4010b57cec5SDimitry Andric    return __x.__ptr_ == __y.__ptr_;
4020b57cec5SDimitry Andric  }
403cb14a3feSDimitry Andric  friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const __list_iterator& __x, const __list_iterator& __y) {
404cb14a3feSDimitry Andric    return !(__x == __y);
405cb14a3feSDimitry Andric  }
4060b57cec5SDimitry Andric};
4070b57cec5SDimitry Andric
4080b57cec5SDimitry Andrictemplate <class _Tp, class _VoidPtr>
409cb14a3feSDimitry Andricclass _LIBCPP_TEMPLATE_VIS __list_const_iterator {
4100b57cec5SDimitry Andric  typedef __list_node_pointer_traits<_Tp, _VoidPtr> _NodeTraits;
4110b57cec5SDimitry Andric  typedef typename _NodeTraits::__link_pointer __link_pointer;
4120b57cec5SDimitry Andric
4130b57cec5SDimitry Andric  __link_pointer __ptr_;
4140b57cec5SDimitry Andric
415cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI explicit __list_const_iterator(__link_pointer __p) _NOEXCEPT : __ptr_(__p) {}
4160b57cec5SDimitry Andric
417cb14a3feSDimitry Andric  template <class, class>
418cb14a3feSDimitry Andric  friend class list;
419cb14a3feSDimitry Andric  template <class, class>
420cb14a3feSDimitry Andric  friend class __list_imp;
421cb14a3feSDimitry Andric
4220b57cec5SDimitry Andricpublic:
4230b57cec5SDimitry Andric  typedef bidirectional_iterator_tag iterator_category;
4240b57cec5SDimitry Andric  typedef _Tp value_type;
4250b57cec5SDimitry Andric  typedef const value_type& reference;
426bdd1243dSDimitry Andric  typedef __rebind_pointer_t<_VoidPtr, const value_type> pointer;
4270b57cec5SDimitry Andric  typedef typename pointer_traits<pointer>::difference_type difference_type;
4280b57cec5SDimitry Andric
429cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI __list_const_iterator() _NOEXCEPT : __ptr_(nullptr) {}
430cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI __list_const_iterator(const __list_iterator<_Tp, _VoidPtr>& __p) _NOEXCEPT
431cb14a3feSDimitry Andric      : __ptr_(__p.__ptr_) {}
4320b57cec5SDimitry Andric
433cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI reference operator*() const { return __ptr_->__as_node()->__get_value(); }
434cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI pointer operator->() const {
4355f757f3fSDimitry Andric    return pointer_traits<pointer>::pointer_to(__ptr_->__as_node()->__get_value());
4360b57cec5SDimitry Andric  }
4370b57cec5SDimitry Andric
438cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI __list_const_iterator& operator++() {
4390b57cec5SDimitry Andric    __ptr_ = __ptr_->__next_;
4400b57cec5SDimitry Andric    return *this;
4410b57cec5SDimitry Andric  }
442cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI __list_const_iterator operator++(int) {
443cb14a3feSDimitry Andric    __list_const_iterator __t(*this);
444cb14a3feSDimitry Andric    ++(*this);
445cb14a3feSDimitry Andric    return __t;
446cb14a3feSDimitry Andric  }
4470b57cec5SDimitry Andric
448cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI __list_const_iterator& operator--() {
4490b57cec5SDimitry Andric    __ptr_ = __ptr_->__prev_;
4500b57cec5SDimitry Andric    return *this;
4510b57cec5SDimitry Andric  }
452cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI __list_const_iterator operator--(int) {
453cb14a3feSDimitry Andric    __list_const_iterator __t(*this);
454cb14a3feSDimitry Andric    --(*this);
455cb14a3feSDimitry Andric    return __t;
456cb14a3feSDimitry Andric  }
4570b57cec5SDimitry Andric
458cb14a3feSDimitry Andric  friend _LIBCPP_HIDE_FROM_ABI bool operator==(const __list_const_iterator& __x, const __list_const_iterator& __y) {
4590b57cec5SDimitry Andric    return __x.__ptr_ == __y.__ptr_;
4600b57cec5SDimitry Andric  }
461cb14a3feSDimitry Andric  friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const __list_const_iterator& __x, const __list_const_iterator& __y) {
462cb14a3feSDimitry Andric    return !(__x == __y);
463cb14a3feSDimitry Andric  }
4640b57cec5SDimitry Andric};
4650b57cec5SDimitry Andric
4660b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
467cb14a3feSDimitry Andricclass __list_imp {
4680b57cec5SDimitry Andricpublic:
469*0fca6ea1SDimitry Andric  __list_imp(const __list_imp&) = delete;
470*0fca6ea1SDimitry Andric  __list_imp& operator=(const __list_imp&) = delete;
471*0fca6ea1SDimitry Andric
4720b57cec5SDimitry Andric  typedef _Alloc allocator_type;
4730b57cec5SDimitry Andric  typedef allocator_traits<allocator_type> __alloc_traits;
4740b57cec5SDimitry Andric  typedef typename __alloc_traits::size_type size_type;
475cb14a3feSDimitry Andric
4760b57cec5SDimitry Andricprotected:
4770b57cec5SDimitry Andric  typedef _Tp value_type;
4780b57cec5SDimitry Andric  typedef typename __alloc_traits::void_pointer __void_pointer;
4790b57cec5SDimitry Andric  typedef __list_iterator<value_type, __void_pointer> iterator;
4800b57cec5SDimitry Andric  typedef __list_const_iterator<value_type, __void_pointer> const_iterator;
4810b57cec5SDimitry Andric  typedef __list_node_base<value_type, __void_pointer> __node_base;
4825f757f3fSDimitry Andric  typedef __list_node<value_type, __void_pointer> __node_type;
4835f757f3fSDimitry Andric  typedef __rebind_alloc<__alloc_traits, __node_type> __node_allocator;
4840b57cec5SDimitry Andric  typedef allocator_traits<__node_allocator> __node_alloc_traits;
4850b57cec5SDimitry Andric  typedef typename __node_alloc_traits::pointer __node_pointer;
4860b57cec5SDimitry Andric  typedef typename __node_alloc_traits::pointer __node_const_pointer;
4870b57cec5SDimitry Andric  typedef __list_node_pointer_traits<value_type, __void_pointer> __node_pointer_traits;
4880b57cec5SDimitry Andric  typedef typename __node_pointer_traits::__link_pointer __link_pointer;
4890b57cec5SDimitry Andric  typedef __link_pointer __link_const_pointer;
4900b57cec5SDimitry Andric  typedef typename __alloc_traits::pointer pointer;
4910b57cec5SDimitry Andric  typedef typename __alloc_traits::const_pointer const_pointer;
4920b57cec5SDimitry Andric  typedef typename __alloc_traits::difference_type difference_type;
4930b57cec5SDimitry Andric
494bdd1243dSDimitry Andric  typedef __rebind_alloc<__alloc_traits, __node_base> __node_base_allocator;
4950b57cec5SDimitry Andric  typedef typename allocator_traits<__node_base_allocator>::pointer __node_base_pointer;
496*0fca6ea1SDimitry Andric  static_assert(!is_same<allocator_type, __node_allocator>::value,
497*0fca6ea1SDimitry Andric                "internal allocator type must differ from user-specified type; otherwise overload resolution breaks");
4980b57cec5SDimitry Andric
4990b57cec5SDimitry Andric  __node_base __end_;
5000b57cec5SDimitry Andric  __compressed_pair<size_type, __node_allocator> __size_alloc_;
5010b57cec5SDimitry Andric
502cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI __link_pointer __end_as_link() const _NOEXCEPT {
503cb14a3feSDimitry Andric    return __node_pointer_traits::__unsafe_link_pointer_cast(const_cast<__node_base&>(__end_).__self());
5040b57cec5SDimitry Andric  }
5050b57cec5SDimitry Andric
506cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI size_type& __sz() _NOEXCEPT { return __size_alloc_.first(); }
507cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI const size_type& __sz() const _NOEXCEPT { return __size_alloc_.first(); }
508cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI __node_allocator& __node_alloc() _NOEXCEPT { return __size_alloc_.second(); }
509cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI const __node_allocator& __node_alloc() const _NOEXCEPT { return __size_alloc_.second(); }
5100b57cec5SDimitry Andric
511cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI size_type __node_alloc_max_size() const _NOEXCEPT {
5120b57cec5SDimitry Andric    return __node_alloc_traits::max_size(__node_alloc());
5130b57cec5SDimitry Andric  }
514cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI static void __unlink_nodes(__link_pointer __f, __link_pointer __l) _NOEXCEPT;
5150b57cec5SDimitry Andric
516cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI __list_imp() _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value);
517cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI __list_imp(const allocator_type& __a);
518cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI __list_imp(const __node_allocator& __a);
5190b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
52006c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI __list_imp(__node_allocator&& __a) _NOEXCEPT;
5210b57cec5SDimitry Andric#endif
52206c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI ~__list_imp();
52306c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI void clear() _NOEXCEPT;
524cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI bool empty() const _NOEXCEPT { return __sz() == 0; }
5250b57cec5SDimitry Andric
526cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI iterator begin() _NOEXCEPT { return iterator(__end_.__next_); }
527cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI const_iterator begin() const _NOEXCEPT { return const_iterator(__end_.__next_); }
528cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI iterator end() _NOEXCEPT { return iterator(__end_as_link()); }
529cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI const_iterator end() const _NOEXCEPT { return const_iterator(__end_as_link()); }
5300b57cec5SDimitry Andric
53106c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI void swap(__list_imp& __c)
5320b57cec5SDimitry Andric#if _LIBCPP_STD_VER >= 14
5330b57cec5SDimitry Andric      _NOEXCEPT;
5340b57cec5SDimitry Andric#else
535*0fca6ea1SDimitry Andric      _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || __is_nothrow_swappable_v<allocator_type>);
5360b57cec5SDimitry Andric#endif
5370b57cec5SDimitry Andric
538cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI void __copy_assign_alloc(const __list_imp& __c) {
539cb14a3feSDimitry Andric    __copy_assign_alloc(
540cb14a3feSDimitry Andric        __c, integral_constant<bool, __node_alloc_traits::propagate_on_container_copy_assignment::value>());
541cb14a3feSDimitry Andric  }
5420b57cec5SDimitry Andric
543cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(__list_imp& __c)
544cb14a3feSDimitry Andric      _NOEXCEPT_(!__node_alloc_traits::propagate_on_container_move_assignment::value ||
545cb14a3feSDimitry Andric                 is_nothrow_move_assignable<__node_allocator>::value) {
546cb14a3feSDimitry Andric    __move_assign_alloc(
547cb14a3feSDimitry Andric        __c, integral_constant<bool, __node_alloc_traits::propagate_on_container_move_assignment::value>());
548cb14a3feSDimitry Andric  }
5490b57cec5SDimitry Andric
5505f757f3fSDimitry Andric  template <class... _Args>
5515f757f3fSDimitry Andric  _LIBCPP_HIDE_FROM_ABI __node_pointer __create_node(__link_pointer __prev, __link_pointer __next, _Args&&... __args) {
5525f757f3fSDimitry Andric    __node_allocator& __alloc = __node_alloc();
5535f757f3fSDimitry Andric    __allocation_guard<__node_allocator> __guard(__alloc, 1);
5545f757f3fSDimitry Andric    // Begin the lifetime of the node itself. Note that this doesn't begin the lifetime of the value
5555f757f3fSDimitry Andric    // held inside the node, since we need to use the allocator's construct() method for that.
5565f757f3fSDimitry Andric    //
5575f757f3fSDimitry Andric    // We don't use the allocator's construct() method to construct the node itself since the
5585f757f3fSDimitry Andric    // Cpp17FooInsertable named requirements don't require the allocator's construct() method
5595f757f3fSDimitry Andric    // to work on anything other than the value_type.
5605f757f3fSDimitry Andric    std::__construct_at(std::addressof(*__guard.__get()), __prev, __next);
5615f757f3fSDimitry Andric
5625f757f3fSDimitry Andric    // Now construct the value_type using the allocator's construct() method.
563cb14a3feSDimitry Andric    __node_alloc_traits::construct(
564cb14a3feSDimitry Andric        __alloc, std::addressof(__guard.__get()->__get_value()), std::forward<_Args>(__args)...);
5655f757f3fSDimitry Andric    return __guard.__release_ptr();
5665f757f3fSDimitry Andric  }
5675f757f3fSDimitry Andric
5685f757f3fSDimitry Andric  _LIBCPP_HIDE_FROM_ABI void __delete_node(__node_pointer __node) {
5695f757f3fSDimitry Andric    // For the same reason as above, we use the allocator's destroy() method for the value_type,
5705f757f3fSDimitry Andric    // but not for the node itself.
5715f757f3fSDimitry Andric    __node_allocator& __alloc = __node_alloc();
5725f757f3fSDimitry Andric    __node_alloc_traits::destroy(__alloc, std::addressof(__node->__get_value()));
5735f757f3fSDimitry Andric    std::__destroy_at(std::addressof(*__node));
5745f757f3fSDimitry Andric    __node_alloc_traits::deallocate(__alloc, __node, 1);
5755f757f3fSDimitry Andric  }
5765f757f3fSDimitry Andric
5770b57cec5SDimitry Andricprivate:
578cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI void __copy_assign_alloc(const __list_imp& __c, true_type) {
5790b57cec5SDimitry Andric    if (__node_alloc() != __c.__node_alloc())
5800b57cec5SDimitry Andric      clear();
5810b57cec5SDimitry Andric    __node_alloc() = __c.__node_alloc();
5820b57cec5SDimitry Andric  }
5830b57cec5SDimitry Andric
584cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI void __copy_assign_alloc(const __list_imp&, false_type) {}
5850b57cec5SDimitry Andric
586cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(__list_imp& __c, true_type)
587cb14a3feSDimitry Andric      _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value) {
5885f757f3fSDimitry Andric    __node_alloc() = std::move(__c.__node_alloc());
5890b57cec5SDimitry Andric  }
5900b57cec5SDimitry Andric
591cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(__list_imp&, false_type) _NOEXCEPT {}
5920b57cec5SDimitry Andric};
5930b57cec5SDimitry Andric
5940b57cec5SDimitry Andric// Unlink nodes [__f, __l]
5950b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
596cb14a3feSDimitry Andricinline void __list_imp<_Tp, _Alloc>::__unlink_nodes(__link_pointer __f, __link_pointer __l) _NOEXCEPT {
5970b57cec5SDimitry Andric  __f->__prev_->__next_ = __l->__next_;
5980b57cec5SDimitry Andric  __l->__next_->__prev_ = __f->__prev_;
5990b57cec5SDimitry Andric}
6000b57cec5SDimitry Andric
6010b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
602cb14a3feSDimitry Andricinline __list_imp<_Tp, _Alloc>::__list_imp() _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value)
603cb14a3feSDimitry Andric    : __size_alloc_(0, __default_init_tag()) {}
6040b57cec5SDimitry Andric
6050b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
606cb14a3feSDimitry Andricinline __list_imp<_Tp, _Alloc>::__list_imp(const allocator_type& __a) : __size_alloc_(0, __node_allocator(__a)) {}
6070b57cec5SDimitry Andric
6080b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
609cb14a3feSDimitry Andricinline __list_imp<_Tp, _Alloc>::__list_imp(const __node_allocator& __a) : __size_alloc_(0, __a) {}
6100b57cec5SDimitry Andric
6110b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
6120b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
613cb14a3feSDimitry Andricinline __list_imp<_Tp, _Alloc>::__list_imp(__node_allocator&& __a) _NOEXCEPT : __size_alloc_(0, std::move(__a)) {}
6140b57cec5SDimitry Andric#endif
6150b57cec5SDimitry Andric
6160b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
6170b57cec5SDimitry Andric__list_imp<_Tp, _Alloc>::~__list_imp() {
6180b57cec5SDimitry Andric  clear();
6190b57cec5SDimitry Andric}
6200b57cec5SDimitry Andric
6210b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
622cb14a3feSDimitry Andricvoid __list_imp<_Tp, _Alloc>::clear() _NOEXCEPT {
623cb14a3feSDimitry Andric  if (!empty()) {
6240b57cec5SDimitry Andric    __link_pointer __f = __end_.__next_;
6250b57cec5SDimitry Andric    __link_pointer __l = __end_as_link();
6260b57cec5SDimitry Andric    __unlink_nodes(__f, __l->__prev_);
6270b57cec5SDimitry Andric    __sz() = 0;
628cb14a3feSDimitry Andric    while (__f != __l) {
6290b57cec5SDimitry Andric      __node_pointer __np = __f->__as_node();
6300b57cec5SDimitry Andric      __f                 = __f->__next_;
6315f757f3fSDimitry Andric      __delete_node(__np);
6320b57cec5SDimitry Andric    }
6330b57cec5SDimitry Andric  }
6340b57cec5SDimitry Andric}
6350b57cec5SDimitry Andric
6360b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
637cb14a3feSDimitry Andricvoid __list_imp<_Tp, _Alloc>::swap(__list_imp& __c)
6380b57cec5SDimitry Andric#if _LIBCPP_STD_VER >= 14
6390b57cec5SDimitry Andric    _NOEXCEPT
6400b57cec5SDimitry Andric#else
641*0fca6ea1SDimitry Andric    _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || __is_nothrow_swappable_v<allocator_type>)
6420b57cec5SDimitry Andric#endif
6430b57cec5SDimitry Andric{
644cb14a3feSDimitry Andric  _LIBCPP_ASSERT_COMPATIBLE_ALLOCATOR(
645cb14a3feSDimitry Andric      __alloc_traits::propagate_on_container_swap::value || this->__node_alloc() == __c.__node_alloc(),
6460b57cec5SDimitry Andric      "list::swap: Either propagate_on_container_swap must be true"
6470b57cec5SDimitry Andric      " or the allocators must compare equal");
6485f757f3fSDimitry Andric  using std::swap;
6495f757f3fSDimitry Andric  std::__swap_allocator(__node_alloc(), __c.__node_alloc());
6500b57cec5SDimitry Andric  swap(__sz(), __c.__sz());
6510b57cec5SDimitry Andric  swap(__end_, __c.__end_);
6520b57cec5SDimitry Andric  if (__sz() == 0)
6530b57cec5SDimitry Andric    __end_.__next_ = __end_.__prev_ = __end_as_link();
6540b57cec5SDimitry Andric  else
6550b57cec5SDimitry Andric    __end_.__prev_->__next_ = __end_.__next_->__prev_ = __end_as_link();
6560b57cec5SDimitry Andric  if (__c.__sz() == 0)
6570b57cec5SDimitry Andric    __c.__end_.__next_ = __c.__end_.__prev_ = __c.__end_as_link();
6580b57cec5SDimitry Andric  else
6590b57cec5SDimitry Andric    __c.__end_.__prev_->__next_ = __c.__end_.__next_->__prev_ = __c.__end_as_link();
6600b57cec5SDimitry Andric}
6610b57cec5SDimitry Andric
6620b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc /*= allocator<_Tp>*/>
663cb14a3feSDimitry Andricclass _LIBCPP_TEMPLATE_VIS list : private __list_imp<_Tp, _Alloc> {
6640b57cec5SDimitry Andric  typedef __list_imp<_Tp, _Alloc> base;
6655f757f3fSDimitry Andric  typedef typename base::__node_type __node_type;
6660b57cec5SDimitry Andric  typedef typename base::__node_allocator __node_allocator;
6670b57cec5SDimitry Andric  typedef typename base::__node_pointer __node_pointer;
6680b57cec5SDimitry Andric  typedef typename base::__node_alloc_traits __node_alloc_traits;
6690b57cec5SDimitry Andric  typedef typename base::__node_base __node_base;
6700b57cec5SDimitry Andric  typedef typename base::__node_base_pointer __node_base_pointer;
6710b57cec5SDimitry Andric  typedef typename base::__link_pointer __link_pointer;
6720b57cec5SDimitry Andric
6730b57cec5SDimitry Andricpublic:
6740b57cec5SDimitry Andric  typedef _Tp value_type;
6750b57cec5SDimitry Andric  typedef _Alloc allocator_type;
676*0fca6ea1SDimitry Andric  static_assert(__check_valid_allocator<allocator_type>::value);
677*0fca6ea1SDimitry Andric  static_assert(is_same<value_type, typename allocator_type::value_type>::value,
67806c3fb27SDimitry Andric                "Allocator::value_type must be same type as value_type");
6790b57cec5SDimitry Andric  typedef value_type& reference;
6800b57cec5SDimitry Andric  typedef const value_type& const_reference;
6810b57cec5SDimitry Andric  typedef typename base::pointer pointer;
6820b57cec5SDimitry Andric  typedef typename base::const_pointer const_pointer;
6834824e7fdSDimitry Andric  typedef typename base::size_type size_type;
6840b57cec5SDimitry Andric  typedef typename base::difference_type difference_type;
6850b57cec5SDimitry Andric  typedef typename base::iterator iterator;
6860b57cec5SDimitry Andric  typedef typename base::const_iterator const_iterator;
6875f757f3fSDimitry Andric  typedef std::reverse_iterator<iterator> reverse_iterator;
6885f757f3fSDimitry Andric  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
68906c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 20
6900b57cec5SDimitry Andric  typedef size_type __remove_return_type;
6910b57cec5SDimitry Andric#else
6920b57cec5SDimitry Andric  typedef void __remove_return_type;
6930b57cec5SDimitry Andric#endif
6940b57cec5SDimitry Andric
695cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI list() _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value) {}
696cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI explicit list(const allocator_type& __a) : base(__a) {}
69706c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI explicit list(size_type __n);
69806c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 14
69906c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI explicit list(size_type __n, const allocator_type& __a);
7000b57cec5SDimitry Andric#endif
70106c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI list(size_type __n, const value_type& __x);
702*0fca6ea1SDimitry Andric  template <__enable_if_t<__is_allocator<_Alloc>::value, int> = 0>
703cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI list(size_type __n, const value_type& __x, const allocator_type& __a) : base(__a) {
7044824e7fdSDimitry Andric    for (; __n > 0; --__n)
7054824e7fdSDimitry Andric      push_back(__x);
7064824e7fdSDimitry Andric  }
7074824e7fdSDimitry Andric
708*0fca6ea1SDimitry Andric  template <class _InpIter, __enable_if_t<__has_input_iterator_category<_InpIter>::value, int> = 0>
709*0fca6ea1SDimitry Andric  _LIBCPP_HIDE_FROM_ABI list(_InpIter __f, _InpIter __l);
710*0fca6ea1SDimitry Andric
711*0fca6ea1SDimitry Andric  template <class _InpIter, __enable_if_t<__has_input_iterator_category<_InpIter>::value, int> = 0>
712*0fca6ea1SDimitry Andric  _LIBCPP_HIDE_FROM_ABI list(_InpIter __f, _InpIter __l, const allocator_type& __a);
7130b57cec5SDimitry Andric
71406c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 23
71506c3fb27SDimitry Andric  template <_ContainerCompatibleRange<_Tp> _Range>
716cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI list(from_range_t, _Range&& __range, const allocator_type& __a = allocator_type()) : base(__a) {
71706c3fb27SDimitry Andric    prepend_range(std::forward<_Range>(__range));
71806c3fb27SDimitry Andric  }
71906c3fb27SDimitry Andric#endif
72006c3fb27SDimitry Andric
72106c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI list(const list& __c);
72206c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI list(const list& __c, const __type_identity_t<allocator_type>& __a);
723cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI list& operator=(const list& __c);
7240b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
72506c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI list(initializer_list<value_type> __il);
72606c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI list(initializer_list<value_type> __il, const allocator_type& __a);
7270b57cec5SDimitry Andric
728cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI list(list&& __c) _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value);
729cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI list(list&& __c, const __type_identity_t<allocator_type>& __a);
730cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI list& operator=(list&& __c)
731cb14a3feSDimitry Andric      _NOEXCEPT_(__node_alloc_traits::propagate_on_container_move_assignment::value&&
7320b57cec5SDimitry Andric                     is_nothrow_move_assignable<__node_allocator>::value);
7330b57cec5SDimitry Andric
734cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI list& operator=(initializer_list<value_type> __il) {
735cb14a3feSDimitry Andric    assign(__il.begin(), __il.end());
736cb14a3feSDimitry Andric    return *this;
737cb14a3feSDimitry Andric  }
7380b57cec5SDimitry Andric
739cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI void assign(initializer_list<value_type> __il) { assign(__il.begin(), __il.end()); }
7400b57cec5SDimitry Andric#endif // _LIBCPP_CXX03_LANG
7410b57cec5SDimitry Andric
742*0fca6ea1SDimitry Andric  template <class _InpIter, __enable_if_t<__has_input_iterator_category<_InpIter>::value, int> = 0>
743*0fca6ea1SDimitry Andric  _LIBCPP_HIDE_FROM_ABI void assign(_InpIter __f, _InpIter __l);
74406c3fb27SDimitry Andric
74506c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 23
74606c3fb27SDimitry Andric  template <_ContainerCompatibleRange<_Tp> _Range>
747cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI void assign_range(_Range&& __range) {
74806c3fb27SDimitry Andric    __assign_with_sentinel(ranges::begin(__range), ranges::end(__range));
74906c3fb27SDimitry Andric  }
75006c3fb27SDimitry Andric#endif
75106c3fb27SDimitry Andric
75206c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI void assign(size_type __n, const value_type& __x);
7530b57cec5SDimitry Andric
754cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI allocator_type get_allocator() const _NOEXCEPT;
7550b57cec5SDimitry Andric
756cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI size_type size() const _NOEXCEPT { return base::__sz(); }
757*0fca6ea1SDimitry Andric  _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI bool empty() const _NOEXCEPT { return base::empty(); }
758cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT {
759cb14a3feSDimitry Andric    return std::min<size_type>(base::__node_alloc_max_size(), numeric_limits<difference_type >::max());
7600b57cec5SDimitry Andric  }
7610b57cec5SDimitry Andric
762cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI iterator begin() _NOEXCEPT { return base::begin(); }
763cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI const_iterator begin() const _NOEXCEPT { return base::begin(); }
764cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI iterator end() _NOEXCEPT { return base::end(); }
765cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI const_iterator end() const _NOEXCEPT { return base::end(); }
766cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI const_iterator cbegin() const _NOEXCEPT { return base::begin(); }
767cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI const_iterator cend() const _NOEXCEPT { return base::end(); }
7680b57cec5SDimitry Andric
769cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI reverse_iterator rbegin() _NOEXCEPT { return reverse_iterator(end()); }
770cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI const_reverse_iterator rbegin() const _NOEXCEPT { return const_reverse_iterator(end()); }
771cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI reverse_iterator rend() _NOEXCEPT { return reverse_iterator(begin()); }
772cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI const_reverse_iterator rend() const _NOEXCEPT { return const_reverse_iterator(begin()); }
773cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI const_reverse_iterator crbegin() const _NOEXCEPT { return const_reverse_iterator(end()); }
774cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI const_reverse_iterator crend() const _NOEXCEPT { return const_reverse_iterator(begin()); }
7750b57cec5SDimitry Andric
776cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI reference front() {
77706c3fb27SDimitry Andric    _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "list::front called on empty list");
7785f757f3fSDimitry Andric    return base::__end_.__next_->__as_node()->__get_value();
7790b57cec5SDimitry Andric  }
780cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI const_reference front() const {
78106c3fb27SDimitry Andric    _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "list::front called on empty list");
7825f757f3fSDimitry Andric    return base::__end_.__next_->__as_node()->__get_value();
7830b57cec5SDimitry Andric  }
784cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI reference back() {
78506c3fb27SDimitry Andric    _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "list::back called on empty list");
7865f757f3fSDimitry Andric    return base::__end_.__prev_->__as_node()->__get_value();
7870b57cec5SDimitry Andric  }
788cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI const_reference back() const {
78906c3fb27SDimitry Andric    _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "list::back called on empty list");
7905f757f3fSDimitry Andric    return base::__end_.__prev_->__as_node()->__get_value();
7910b57cec5SDimitry Andric  }
7920b57cec5SDimitry Andric
7930b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
79406c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI void push_front(value_type&& __x);
79506c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI void push_back(value_type&& __x);
79606c3fb27SDimitry Andric
79706c3fb27SDimitry Andric#  if _LIBCPP_STD_VER >= 23
79806c3fb27SDimitry Andric  template <_ContainerCompatibleRange<_Tp> _Range>
799cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI void prepend_range(_Range&& __range) {
80006c3fb27SDimitry Andric    insert_range(begin(), std::forward<_Range>(__range));
80106c3fb27SDimitry Andric  }
80206c3fb27SDimitry Andric
80306c3fb27SDimitry Andric  template <_ContainerCompatibleRange<_Tp> _Range>
804cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI void append_range(_Range&& __range) {
80506c3fb27SDimitry Andric    insert_range(end(), std::forward<_Range>(__range));
80606c3fb27SDimitry Andric  }
80706c3fb27SDimitry Andric#  endif
8080b57cec5SDimitry Andric
8090b57cec5SDimitry Andric  template <class... _Args>
81006c3fb27SDimitry Andric#  if _LIBCPP_STD_VER >= 17
81106c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI reference emplace_front(_Args&&... __args);
8120b57cec5SDimitry Andric#  else
81306c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI void emplace_front(_Args&&... __args);
8140b57cec5SDimitry Andric#  endif
8150b57cec5SDimitry Andric  template <class... _Args>
81606c3fb27SDimitry Andric#  if _LIBCPP_STD_VER >= 17
81706c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI reference emplace_back(_Args&&... __args);
8180b57cec5SDimitry Andric#  else
81906c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI void emplace_back(_Args&&... __args);
8200b57cec5SDimitry Andric#  endif
8210b57cec5SDimitry Andric  template <class... _Args>
82206c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI iterator emplace(const_iterator __p, _Args&&... __args);
8230b57cec5SDimitry Andric
82406c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, value_type&& __x);
8250b57cec5SDimitry Andric
826cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, initializer_list<value_type> __il) {
827cb14a3feSDimitry Andric    return insert(__p, __il.begin(), __il.end());
828cb14a3feSDimitry Andric  }
8290b57cec5SDimitry Andric#endif // _LIBCPP_CXX03_LANG
8300b57cec5SDimitry Andric
83106c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI void push_front(const value_type& __x);
83206c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI void push_back(const value_type& __x);
8330b57cec5SDimitry Andric
8340b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
8350b57cec5SDimitry Andric  template <class _Arg>
836cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI void __emplace_back(_Arg&& __arg) {
837cb14a3feSDimitry Andric    emplace_back(std::forward<_Arg>(__arg));
838cb14a3feSDimitry Andric  }
8390b57cec5SDimitry Andric#else
840cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI void __emplace_back(value_type const& __arg) { push_back(__arg); }
8410b57cec5SDimitry Andric#endif
8420b57cec5SDimitry Andric
84306c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, const value_type& __x);
84406c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, size_type __n, const value_type& __x);
845*0fca6ea1SDimitry Andric
846*0fca6ea1SDimitry Andric  template <class _InpIter, __enable_if_t<__has_input_iterator_category<_InpIter>::value, int> = 0>
847*0fca6ea1SDimitry Andric  _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, _InpIter __f, _InpIter __l);
84806c3fb27SDimitry Andric
84906c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 23
85006c3fb27SDimitry Andric  template <_ContainerCompatibleRange<_Tp> _Range>
851cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI iterator insert_range(const_iterator __position, _Range&& __range) {
85206c3fb27SDimitry Andric    return __insert_with_sentinel(__position, ranges::begin(__range), ranges::end(__range));
85306c3fb27SDimitry Andric  }
85406c3fb27SDimitry Andric#endif
8550b57cec5SDimitry Andric
856cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI void swap(list& __c)
8570b57cec5SDimitry Andric#if _LIBCPP_STD_VER >= 14
8580b57cec5SDimitry Andric      _NOEXCEPT
8590b57cec5SDimitry Andric#else
860*0fca6ea1SDimitry Andric      _NOEXCEPT_(!__node_alloc_traits::propagate_on_container_swap::value || __is_nothrow_swappable_v<__node_allocator>)
8610b57cec5SDimitry Andric#endif
862cb14a3feSDimitry Andric  {
863cb14a3feSDimitry Andric    base::swap(__c);
864cb14a3feSDimitry Andric  }
865cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI void clear() _NOEXCEPT { base::clear(); }
8660b57cec5SDimitry Andric
86706c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI void pop_front();
86806c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI void pop_back();
8690b57cec5SDimitry Andric
87006c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __p);
87106c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __f, const_iterator __l);
8720b57cec5SDimitry Andric
87306c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI void resize(size_type __n);
87406c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI void resize(size_type __n, const value_type& __x);
8750b57cec5SDimitry Andric
87606c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI void splice(const_iterator __p, list& __c);
8770b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
878cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI void splice(const_iterator __p, list&& __c) { splice(__p, __c); }
879cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI void splice(const_iterator __p, list&& __c, const_iterator __i) { splice(__p, __c, __i); }
880cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI void splice(const_iterator __p, list&& __c, const_iterator __f, const_iterator __l) {
881cb14a3feSDimitry Andric    splice(__p, __c, __f, __l);
882cb14a3feSDimitry Andric  }
8830b57cec5SDimitry Andric#endif
88406c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI void splice(const_iterator __p, list& __c, const_iterator __i);
88506c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI void splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l);
8860b57cec5SDimitry Andric
88706c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI __remove_return_type remove(const value_type& __x);
88806c3fb27SDimitry Andric  template <class _Pred>
88906c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI __remove_return_type remove_if(_Pred __pred);
890cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI __remove_return_type unique() { return unique(__equal_to()); }
8910b57cec5SDimitry Andric  template <class _BinaryPred>
89206c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI __remove_return_type unique(_BinaryPred __binary_pred);
893cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI void merge(list& __c);
8940b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
895cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI void merge(list&& __c) { merge(__c); }
8960b57cec5SDimitry Andric
8970b57cec5SDimitry Andric  template <class _Comp>
898cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI void merge(list&& __c, _Comp __comp) {
899cb14a3feSDimitry Andric    merge(__c, __comp);
900cb14a3feSDimitry Andric  }
9010b57cec5SDimitry Andric#endif
9020b57cec5SDimitry Andric  template <class _Comp>
90306c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI void merge(list& __c, _Comp __comp);
9040b57cec5SDimitry Andric
905cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI void sort();
9060b57cec5SDimitry Andric  template <class _Comp>
907cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI void sort(_Comp __comp);
9080b57cec5SDimitry Andric
90906c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI void reverse() _NOEXCEPT;
9100b57cec5SDimitry Andric
91106c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI bool __invariants() const;
9120b57cec5SDimitry Andric
9130b57cec5SDimitry Andricprivate:
91406c3fb27SDimitry Andric  template <class _Iterator, class _Sentinel>
915cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI void __assign_with_sentinel(_Iterator __f, _Sentinel __l);
91606c3fb27SDimitry Andric
91706c3fb27SDimitry Andric  template <class _Iterator, class _Sentinel>
918cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI iterator __insert_with_sentinel(const_iterator __p, _Iterator __f, _Sentinel __l);
91906c3fb27SDimitry Andric
920cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI static void __link_nodes(__link_pointer __p, __link_pointer __f, __link_pointer __l);
921cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI void __link_nodes_at_front(__link_pointer __f, __link_pointer __l);
922cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI void __link_nodes_at_back(__link_pointer __f, __link_pointer __l);
92306c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI iterator __iterator(size_type __n);
92406c3fb27SDimitry Andric  // TODO: Make this _LIBCPP_HIDE_FROM_ABI
9250b57cec5SDimitry Andric  template <class _Comp>
92606c3fb27SDimitry Andric  _LIBCPP_HIDDEN static iterator __sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp);
9270b57cec5SDimitry Andric
92806c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI void __move_assign(list& __c, true_type)
9290b57cec5SDimitry Andric      _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value);
93006c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI void __move_assign(list& __c, false_type);
9310b57cec5SDimitry Andric};
9320b57cec5SDimitry Andric
933349cc55cSDimitry Andric#if _LIBCPP_STD_VER >= 17
9340b57cec5SDimitry Andrictemplate <class _InputIterator,
935fe6060f1SDimitry Andric          class _Alloc = allocator<__iter_value_type<_InputIterator>>,
93606c3fb27SDimitry Andric          class        = enable_if_t<__has_input_iterator_category<_InputIterator>::value>,
937cb14a3feSDimitry Andric          class        = enable_if_t<__is_allocator<_Alloc>::value> >
938cb14a3feSDimitry Andriclist(_InputIterator, _InputIterator) -> list<__iter_value_type<_InputIterator>, _Alloc>;
9390b57cec5SDimitry Andric
9400b57cec5SDimitry Andrictemplate <class _InputIterator,
9410b57cec5SDimitry Andric          class _Alloc,
94206c3fb27SDimitry Andric          class = enable_if_t<__has_input_iterator_category<_InputIterator>::value>,
943cb14a3feSDimitry Andric          class = enable_if_t<__is_allocator<_Alloc>::value> >
944cb14a3feSDimitry Andriclist(_InputIterator, _InputIterator, _Alloc) -> list<__iter_value_type<_InputIterator>, _Alloc>;
9450b57cec5SDimitry Andric#endif
9460b57cec5SDimitry Andric
94706c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 23
94806c3fb27SDimitry Andrictemplate <ranges::input_range _Range,
94906c3fb27SDimitry Andric          class _Alloc = allocator<ranges::range_value_t<_Range>>,
950cb14a3feSDimitry Andric          class        = enable_if_t<__is_allocator<_Alloc>::value> >
951cb14a3feSDimitry Andriclist(from_range_t, _Range&&, _Alloc = _Alloc()) -> list<ranges::range_value_t<_Range>, _Alloc>;
95206c3fb27SDimitry Andric#endif
95306c3fb27SDimitry Andric
9540b57cec5SDimitry Andric// Link in nodes [__f, __l] just prior to __p
9550b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
956cb14a3feSDimitry Andricinline void list<_Tp, _Alloc>::__link_nodes(__link_pointer __p, __link_pointer __f, __link_pointer __l) {
9570b57cec5SDimitry Andric  __p->__prev_->__next_ = __f;
9580b57cec5SDimitry Andric  __f->__prev_          = __p->__prev_;
9590b57cec5SDimitry Andric  __p->__prev_          = __l;
9600b57cec5SDimitry Andric  __l->__next_          = __p;
9610b57cec5SDimitry Andric}
9620b57cec5SDimitry Andric
9630b57cec5SDimitry Andric// Link in nodes [__f, __l] at the front of the list
9640b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
965cb14a3feSDimitry Andricinline void list<_Tp, _Alloc>::__link_nodes_at_front(__link_pointer __f, __link_pointer __l) {
9660b57cec5SDimitry Andric  __f->__prev_          = base::__end_as_link();
9670b57cec5SDimitry Andric  __l->__next_          = base::__end_.__next_;
9680b57cec5SDimitry Andric  __l->__next_->__prev_ = __l;
9690b57cec5SDimitry Andric  base::__end_.__next_  = __f;
9700b57cec5SDimitry Andric}
9710b57cec5SDimitry Andric
9720b57cec5SDimitry Andric// Link in nodes [__f, __l] at the back of the list
9730b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
974cb14a3feSDimitry Andricinline void list<_Tp, _Alloc>::__link_nodes_at_back(__link_pointer __f, __link_pointer __l) {
9750b57cec5SDimitry Andric  __l->__next_          = base::__end_as_link();
9760b57cec5SDimitry Andric  __f->__prev_          = base::__end_.__prev_;
9770b57cec5SDimitry Andric  __f->__prev_->__next_ = __f;
9780b57cec5SDimitry Andric  base::__end_.__prev_  = __l;
9790b57cec5SDimitry Andric}
9800b57cec5SDimitry Andric
9810b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
982cb14a3feSDimitry Andricinline typename list<_Tp, _Alloc>::iterator list<_Tp, _Alloc>::__iterator(size_type __n) {
983cb14a3feSDimitry Andric  return __n <= base::__sz() / 2 ? std::next(begin(), __n) : std::prev(end(), base::__sz() - __n);
9840b57cec5SDimitry Andric}
9850b57cec5SDimitry Andric
9860b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
987cb14a3feSDimitry Andriclist<_Tp, _Alloc>::list(size_type __n) {
9880b57cec5SDimitry Andric  for (; __n > 0; --__n)
9890b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
9900b57cec5SDimitry Andric    emplace_back();
9910b57cec5SDimitry Andric#else
9920b57cec5SDimitry Andric    push_back(value_type());
9930b57cec5SDimitry Andric#endif
9940b57cec5SDimitry Andric}
9950b57cec5SDimitry Andric
99606c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 14
9970b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
998cb14a3feSDimitry Andriclist<_Tp, _Alloc>::list(size_type __n, const allocator_type& __a) : base(__a) {
9990b57cec5SDimitry Andric  for (; __n > 0; --__n)
10000b57cec5SDimitry Andric    emplace_back();
10010b57cec5SDimitry Andric}
10020b57cec5SDimitry Andric#endif
10030b57cec5SDimitry Andric
10040b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
1005cb14a3feSDimitry Andriclist<_Tp, _Alloc>::list(size_type __n, const value_type& __x) {
10060b57cec5SDimitry Andric  for (; __n > 0; --__n)
10070b57cec5SDimitry Andric    push_back(__x);
10080b57cec5SDimitry Andric}
10090b57cec5SDimitry Andric
10100b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
1011*0fca6ea1SDimitry Andrictemplate <class _InpIter, __enable_if_t<__has_input_iterator_category<_InpIter>::value, int> >
1012*0fca6ea1SDimitry Andriclist<_Tp, _Alloc>::list(_InpIter __f, _InpIter __l) {
10130b57cec5SDimitry Andric  for (; __f != __l; ++__f)
10140b57cec5SDimitry Andric    __emplace_back(*__f);
10150b57cec5SDimitry Andric}
10160b57cec5SDimitry Andric
10170b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
1018*0fca6ea1SDimitry Andrictemplate <class _InpIter, __enable_if_t<__has_input_iterator_category<_InpIter>::value, int> >
1019*0fca6ea1SDimitry Andriclist<_Tp, _Alloc>::list(_InpIter __f, _InpIter __l, const allocator_type& __a) : base(__a) {
10200b57cec5SDimitry Andric  for (; __f != __l; ++__f)
10210b57cec5SDimitry Andric    __emplace_back(*__f);
10220b57cec5SDimitry Andric}
10230b57cec5SDimitry Andric
10240b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
10250b57cec5SDimitry Andriclist<_Tp, _Alloc>::list(const list& __c)
1026cb14a3feSDimitry Andric    : base(__node_alloc_traits::select_on_container_copy_construction(__c.__node_alloc())) {
10270b57cec5SDimitry Andric  for (const_iterator __i = __c.begin(), __e = __c.end(); __i != __e; ++__i)
10280b57cec5SDimitry Andric    push_back(*__i);
10290b57cec5SDimitry Andric}
10300b57cec5SDimitry Andric
10310b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
1032cb14a3feSDimitry Andriclist<_Tp, _Alloc>::list(const list& __c, const __type_identity_t<allocator_type>& __a) : base(__a) {
10330b57cec5SDimitry Andric  for (const_iterator __i = __c.begin(), __e = __c.end(); __i != __e; ++__i)
10340b57cec5SDimitry Andric    push_back(*__i);
10350b57cec5SDimitry Andric}
10360b57cec5SDimitry Andric
10370b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
10380b57cec5SDimitry Andric
10390b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
1040cb14a3feSDimitry Andriclist<_Tp, _Alloc>::list(initializer_list<value_type> __il, const allocator_type& __a) : base(__a) {
1041cb14a3feSDimitry Andric  for (typename initializer_list<value_type>::const_iterator __i = __il.begin(), __e = __il.end(); __i != __e; ++__i)
10420b57cec5SDimitry Andric    push_back(*__i);
10430b57cec5SDimitry Andric}
10440b57cec5SDimitry Andric
10450b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
1046cb14a3feSDimitry Andriclist<_Tp, _Alloc>::list(initializer_list<value_type> __il) {
1047cb14a3feSDimitry Andric  for (typename initializer_list<value_type>::const_iterator __i = __il.begin(), __e = __il.end(); __i != __e; ++__i)
10480b57cec5SDimitry Andric    push_back(*__i);
10490b57cec5SDimitry Andric}
10500b57cec5SDimitry Andric
10510b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
1052*0fca6ea1SDimitry Andricinline list<_Tp, _Alloc>::list(list&& __c) noexcept(is_nothrow_move_constructible<__node_allocator>::value)
10535f757f3fSDimitry Andric    : base(std::move(__c.__node_alloc())) {
10540b57cec5SDimitry Andric  splice(end(), __c);
10550b57cec5SDimitry Andric}
10560b57cec5SDimitry Andric
10570b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
1058cb14a3feSDimitry Andricinline list<_Tp, _Alloc>::list(list&& __c, const __type_identity_t<allocator_type>& __a) : base(__a) {
10590b57cec5SDimitry Andric  if (__a == __c.get_allocator())
10600b57cec5SDimitry Andric    splice(end(), __c);
1061cb14a3feSDimitry Andric  else {
10620b57cec5SDimitry Andric    typedef move_iterator<iterator> _Ip;
10630b57cec5SDimitry Andric    assign(_Ip(__c.begin()), _Ip(__c.end()));
10640b57cec5SDimitry Andric  }
10650b57cec5SDimitry Andric}
10660b57cec5SDimitry Andric
10670b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
1068*0fca6ea1SDimitry Andricinline list<_Tp, _Alloc>& list<_Tp, _Alloc>::operator=(list&& __c) noexcept(
1069*0fca6ea1SDimitry Andric    __node_alloc_traits::propagate_on_container_move_assignment::value &&
1070cb14a3feSDimitry Andric    is_nothrow_move_assignable<__node_allocator>::value) {
1071cb14a3feSDimitry Andric  __move_assign(__c, integral_constant<bool, __node_alloc_traits::propagate_on_container_move_assignment::value>());
10720b57cec5SDimitry Andric  return *this;
10730b57cec5SDimitry Andric}
10740b57cec5SDimitry Andric
10750b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
1076cb14a3feSDimitry Andricvoid list<_Tp, _Alloc>::__move_assign(list& __c, false_type) {
1077cb14a3feSDimitry Andric  if (base::__node_alloc() != __c.__node_alloc()) {
10780b57cec5SDimitry Andric    typedef move_iterator<iterator> _Ip;
10790b57cec5SDimitry Andric    assign(_Ip(__c.begin()), _Ip(__c.end()));
1080cb14a3feSDimitry Andric  } else
10810b57cec5SDimitry Andric    __move_assign(__c, true_type());
10820b57cec5SDimitry Andric}
10830b57cec5SDimitry Andric
10840b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
1085*0fca6ea1SDimitry Andricvoid list<_Tp, _Alloc>::__move_assign(list& __c,
1086*0fca6ea1SDimitry Andric                                      true_type) noexcept(is_nothrow_move_assignable<__node_allocator>::value) {
10870b57cec5SDimitry Andric  clear();
10880b57cec5SDimitry Andric  base::__move_assign_alloc(__c);
10890b57cec5SDimitry Andric  splice(end(), __c);
10900b57cec5SDimitry Andric}
10910b57cec5SDimitry Andric
10920b57cec5SDimitry Andric#endif // _LIBCPP_CXX03_LANG
10930b57cec5SDimitry Andric
10940b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
1095cb14a3feSDimitry Andricinline list<_Tp, _Alloc>& list<_Tp, _Alloc>::operator=(const list& __c) {
1096cb14a3feSDimitry Andric  if (this != std::addressof(__c)) {
10970b57cec5SDimitry Andric    base::__copy_assign_alloc(__c);
10980b57cec5SDimitry Andric    assign(__c.begin(), __c.end());
10990b57cec5SDimitry Andric  }
11000b57cec5SDimitry Andric  return *this;
11010b57cec5SDimitry Andric}
11020b57cec5SDimitry Andric
11030b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
1104*0fca6ea1SDimitry Andrictemplate <class _InpIter, __enable_if_t<__has_input_iterator_category<_InpIter>::value, int> >
1105*0fca6ea1SDimitry Andricvoid list<_Tp, _Alloc>::assign(_InpIter __f, _InpIter __l) {
110606c3fb27SDimitry Andric  __assign_with_sentinel(__f, __l);
110706c3fb27SDimitry Andric}
110806c3fb27SDimitry Andric
110906c3fb27SDimitry Andrictemplate <class _Tp, class _Alloc>
111006c3fb27SDimitry Andrictemplate <class _Iterator, class _Sentinel>
1111cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI void list<_Tp, _Alloc>::__assign_with_sentinel(_Iterator __f, _Sentinel __l) {
11120b57cec5SDimitry Andric  iterator __i = begin();
11130b57cec5SDimitry Andric  iterator __e = end();
1114349cc55cSDimitry Andric  for (; __f != __l && __i != __e; ++__f, (void)++__i)
11150b57cec5SDimitry Andric    *__i = *__f;
11160b57cec5SDimitry Andric  if (__i == __e)
111706c3fb27SDimitry Andric    __insert_with_sentinel(__e, std::move(__f), std::move(__l));
11180b57cec5SDimitry Andric  else
11190b57cec5SDimitry Andric    erase(__i, __e);
11200b57cec5SDimitry Andric}
11210b57cec5SDimitry Andric
11220b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
1123cb14a3feSDimitry Andricvoid list<_Tp, _Alloc>::assign(size_type __n, const value_type& __x) {
11240b57cec5SDimitry Andric  iterator __i = begin();
11250b57cec5SDimitry Andric  iterator __e = end();
1126349cc55cSDimitry Andric  for (; __n > 0 && __i != __e; --__n, (void)++__i)
11270b57cec5SDimitry Andric    *__i = __x;
11280b57cec5SDimitry Andric  if (__i == __e)
11290b57cec5SDimitry Andric    insert(__e, __n, __x);
11300b57cec5SDimitry Andric  else
11310b57cec5SDimitry Andric    erase(__i, __e);
11320b57cec5SDimitry Andric}
11330b57cec5SDimitry Andric
11340b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
1135cb14a3feSDimitry Andricinline _Alloc list<_Tp, _Alloc>::get_allocator() const _NOEXCEPT {
11360b57cec5SDimitry Andric  return allocator_type(base::__node_alloc());
11370b57cec5SDimitry Andric}
11380b57cec5SDimitry Andric
11390b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
1140cb14a3feSDimitry Andrictypename list<_Tp, _Alloc>::iterator list<_Tp, _Alloc>::insert(const_iterator __p, const value_type& __x) {
11415f757f3fSDimitry Andric  __node_pointer __node = this->__create_node(/* prev = */ nullptr, /* next = */ nullptr, __x);
11425f757f3fSDimitry Andric  __link_nodes(__p.__ptr_, __node->__as_link(), __node->__as_link());
11430b57cec5SDimitry Andric  ++base::__sz();
11445f757f3fSDimitry Andric  return iterator(__node->__as_link());
11450b57cec5SDimitry Andric}
11460b57cec5SDimitry Andric
11470b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
11480b57cec5SDimitry Andrictypename list<_Tp, _Alloc>::iterator
1149cb14a3feSDimitry Andriclist<_Tp, _Alloc>::insert(const_iterator __p, size_type __n, const value_type& __x) {
115006c3fb27SDimitry Andric  iterator __r(__p.__ptr_);
1151cb14a3feSDimitry Andric  if (__n > 0) {
11520b57cec5SDimitry Andric    size_type __ds        = 0;
11535f757f3fSDimitry Andric    __node_pointer __node = this->__create_node(/* prev = */ nullptr, /* next = */ nullptr, __x);
11540b57cec5SDimitry Andric    ++__ds;
11555f757f3fSDimitry Andric    __r          = iterator(__node->__as_link());
11560b57cec5SDimitry Andric    iterator __e = __r;
115706c3fb27SDimitry Andric#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1158cb14a3feSDimitry Andric    try {
115906c3fb27SDimitry Andric#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1160cb14a3feSDimitry Andric      for (--__n; __n != 0; --__n, (void)++__e, ++__ds) {
11615f757f3fSDimitry Andric        __e.__ptr_->__next_ = this->__create_node(/* prev = */ __e.__ptr_, /* next = */ nullptr, __x)->__as_link();
11620b57cec5SDimitry Andric      }
116306c3fb27SDimitry Andric#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1164cb14a3feSDimitry Andric    } catch (...) {
1165cb14a3feSDimitry Andric      while (true) {
11660b57cec5SDimitry Andric        __link_pointer __prev    = __e.__ptr_->__prev_;
11675f757f3fSDimitry Andric        __node_pointer __current = __e.__ptr_->__as_node();
11685f757f3fSDimitry Andric        this->__delete_node(__current);
11690b57cec5SDimitry Andric        if (__prev == 0)
11700b57cec5SDimitry Andric          break;
117106c3fb27SDimitry Andric        __e = iterator(__prev);
11720b57cec5SDimitry Andric      }
11730b57cec5SDimitry Andric      throw;
11740b57cec5SDimitry Andric    }
117506c3fb27SDimitry Andric#endif // _LIBCPP_HAS_NO_EXCEPTIONS
11760b57cec5SDimitry Andric    __link_nodes(__p.__ptr_, __r.__ptr_, __e.__ptr_);
11770b57cec5SDimitry Andric    base::__sz() += __ds;
11780b57cec5SDimitry Andric  }
11790b57cec5SDimitry Andric  return __r;
11800b57cec5SDimitry Andric}
11810b57cec5SDimitry Andric
11820b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
1183*0fca6ea1SDimitry Andrictemplate <class _InpIter, __enable_if_t<__has_input_iterator_category<_InpIter>::value, int> >
1184*0fca6ea1SDimitry Andrictypename list<_Tp, _Alloc>::iterator list<_Tp, _Alloc>::insert(const_iterator __p, _InpIter __f, _InpIter __l) {
118506c3fb27SDimitry Andric  return __insert_with_sentinel(__p, __f, __l);
118606c3fb27SDimitry Andric}
118706c3fb27SDimitry Andric
118806c3fb27SDimitry Andrictemplate <class _Tp, class _Alloc>
118906c3fb27SDimitry Andrictemplate <class _Iterator, class _Sentinel>
1190cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI typename list<_Tp, _Alloc>::iterator
119106c3fb27SDimitry Andriclist<_Tp, _Alloc>::__insert_with_sentinel(const_iterator __p, _Iterator __f, _Sentinel __l) {
119206c3fb27SDimitry Andric  iterator __r(__p.__ptr_);
1193cb14a3feSDimitry Andric  if (__f != __l) {
11940b57cec5SDimitry Andric    size_type __ds        = 0;
11955f757f3fSDimitry Andric    __node_pointer __node = this->__create_node(/* prev = */ nullptr, /* next = */ nullptr, *__f);
11960b57cec5SDimitry Andric    ++__ds;
11975f757f3fSDimitry Andric    __r          = iterator(__node->__as_link());
11980b57cec5SDimitry Andric    iterator __e = __r;
119906c3fb27SDimitry Andric#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1200cb14a3feSDimitry Andric    try {
120106c3fb27SDimitry Andric#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1202cb14a3feSDimitry Andric      for (++__f; __f != __l; ++__f, (void)++__e, ++__ds) {
12035f757f3fSDimitry Andric        __e.__ptr_->__next_ = this->__create_node(/* prev = */ __e.__ptr_, /* next = */ nullptr, *__f)->__as_link();
12040b57cec5SDimitry Andric      }
120506c3fb27SDimitry Andric#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1206cb14a3feSDimitry Andric    } catch (...) {
1207cb14a3feSDimitry Andric      while (true) {
12080b57cec5SDimitry Andric        __link_pointer __prev    = __e.__ptr_->__prev_;
12095f757f3fSDimitry Andric        __node_pointer __current = __e.__ptr_->__as_node();
12105f757f3fSDimitry Andric        this->__delete_node(__current);
12110b57cec5SDimitry Andric        if (__prev == 0)
12120b57cec5SDimitry Andric          break;
121306c3fb27SDimitry Andric        __e = iterator(__prev);
12140b57cec5SDimitry Andric      }
12150b57cec5SDimitry Andric      throw;
12160b57cec5SDimitry Andric    }
121706c3fb27SDimitry Andric#endif // _LIBCPP_HAS_NO_EXCEPTIONS
12180b57cec5SDimitry Andric    __link_nodes(__p.__ptr_, __r.__ptr_, __e.__ptr_);
12190b57cec5SDimitry Andric    base::__sz() += __ds;
12200b57cec5SDimitry Andric  }
12210b57cec5SDimitry Andric  return __r;
12220b57cec5SDimitry Andric}
12230b57cec5SDimitry Andric
12240b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
1225cb14a3feSDimitry Andricvoid list<_Tp, _Alloc>::push_front(const value_type& __x) {
12265f757f3fSDimitry Andric  __node_pointer __node = this->__create_node(/* prev = */ nullptr, /* next = */ nullptr, __x);
12275f757f3fSDimitry Andric  __link_pointer __nl   = __node->__as_link();
12280b57cec5SDimitry Andric  __link_nodes_at_front(__nl, __nl);
12290b57cec5SDimitry Andric  ++base::__sz();
12300b57cec5SDimitry Andric}
12310b57cec5SDimitry Andric
12320b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
1233cb14a3feSDimitry Andricvoid list<_Tp, _Alloc>::push_back(const value_type& __x) {
12345f757f3fSDimitry Andric  __node_pointer __node = this->__create_node(/* prev = */ nullptr, /* next = */ nullptr, __x);
12355f757f3fSDimitry Andric  __link_pointer __nl   = __node->__as_link();
12365f757f3fSDimitry Andric  __link_nodes_at_back(__nl, __nl);
12370b57cec5SDimitry Andric  ++base::__sz();
12380b57cec5SDimitry Andric}
12390b57cec5SDimitry Andric
12400b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
12410b57cec5SDimitry Andric
12420b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
1243cb14a3feSDimitry Andricvoid list<_Tp, _Alloc>::push_front(value_type&& __x) {
12445f757f3fSDimitry Andric  __node_pointer __node = this->__create_node(/* prev = */ nullptr, /* next = */ nullptr, std::move(__x));
12455f757f3fSDimitry Andric  __link_pointer __nl   = __node->__as_link();
12465f757f3fSDimitry Andric  __link_nodes_at_front(__nl, __nl);
12470b57cec5SDimitry Andric  ++base::__sz();
12480b57cec5SDimitry Andric}
12490b57cec5SDimitry Andric
12500b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
1251cb14a3feSDimitry Andricvoid list<_Tp, _Alloc>::push_back(value_type&& __x) {
12525f757f3fSDimitry Andric  __node_pointer __node = this->__create_node(/* prev = */ nullptr, /* next = */ nullptr, std::move(__x));
12535f757f3fSDimitry Andric  __link_pointer __nl   = __node->__as_link();
12545f757f3fSDimitry Andric  __link_nodes_at_back(__nl, __nl);
12550b57cec5SDimitry Andric  ++base::__sz();
12560b57cec5SDimitry Andric}
12570b57cec5SDimitry Andric
12580b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
12590b57cec5SDimitry Andrictemplate <class... _Args>
126006c3fb27SDimitry Andric#  if _LIBCPP_STD_VER >= 17
12610b57cec5SDimitry Andrictypename list<_Tp, _Alloc>::reference
12620b57cec5SDimitry Andric#  else
12630b57cec5SDimitry Andricvoid
12640b57cec5SDimitry Andric#  endif
1265cb14a3feSDimitry Andriclist<_Tp, _Alloc>::emplace_front(_Args&&... __args) {
1266cb14a3feSDimitry Andric  __node_pointer __node =
1267cb14a3feSDimitry Andric      this->__create_node(/* prev = */ nullptr, /* next = */ nullptr, std::forward<_Args>(__args)...);
12685f757f3fSDimitry Andric  __link_pointer __nl = __node->__as_link();
12695f757f3fSDimitry Andric  __link_nodes_at_front(__nl, __nl);
12700b57cec5SDimitry Andric  ++base::__sz();
127106c3fb27SDimitry Andric#  if _LIBCPP_STD_VER >= 17
12725f757f3fSDimitry Andric  return __node->__get_value();
12730b57cec5SDimitry Andric#  endif
12740b57cec5SDimitry Andric}
12750b57cec5SDimitry Andric
12760b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
12770b57cec5SDimitry Andrictemplate <class... _Args>
127806c3fb27SDimitry Andric#  if _LIBCPP_STD_VER >= 17
12790b57cec5SDimitry Andrictypename list<_Tp, _Alloc>::reference
12800b57cec5SDimitry Andric#  else
12810b57cec5SDimitry Andricvoid
12820b57cec5SDimitry Andric#  endif
1283cb14a3feSDimitry Andriclist<_Tp, _Alloc>::emplace_back(_Args&&... __args) {
1284cb14a3feSDimitry Andric  __node_pointer __node =
1285cb14a3feSDimitry Andric      this->__create_node(/* prev = */ nullptr, /* next = */ nullptr, std::forward<_Args>(__args)...);
12865f757f3fSDimitry Andric  __link_pointer __nl = __node->__as_link();
12870b57cec5SDimitry Andric  __link_nodes_at_back(__nl, __nl);
12880b57cec5SDimitry Andric  ++base::__sz();
128906c3fb27SDimitry Andric#  if _LIBCPP_STD_VER >= 17
12905f757f3fSDimitry Andric  return __node->__get_value();
12910b57cec5SDimitry Andric#  endif
12920b57cec5SDimitry Andric}
12930b57cec5SDimitry Andric
12940b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
12950b57cec5SDimitry Andrictemplate <class... _Args>
1296cb14a3feSDimitry Andrictypename list<_Tp, _Alloc>::iterator list<_Tp, _Alloc>::emplace(const_iterator __p, _Args&&... __args) {
1297cb14a3feSDimitry Andric  __node_pointer __node =
1298cb14a3feSDimitry Andric      this->__create_node(/* prev = */ nullptr, /* next = */ nullptr, std::forward<_Args>(__args)...);
12995f757f3fSDimitry Andric  __link_pointer __nl = __node->__as_link();
13000b57cec5SDimitry Andric  __link_nodes(__p.__ptr_, __nl, __nl);
13010b57cec5SDimitry Andric  ++base::__sz();
130206c3fb27SDimitry Andric  return iterator(__nl);
13030b57cec5SDimitry Andric}
13040b57cec5SDimitry Andric
13050b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
1306cb14a3feSDimitry Andrictypename list<_Tp, _Alloc>::iterator list<_Tp, _Alloc>::insert(const_iterator __p, value_type&& __x) {
13075f757f3fSDimitry Andric  __node_pointer __node = this->__create_node(/* prev = */ nullptr, /* next = */ nullptr, std::move(__x));
13085f757f3fSDimitry Andric  __link_pointer __nl   = __node->__as_link();
13090b57cec5SDimitry Andric  __link_nodes(__p.__ptr_, __nl, __nl);
13100b57cec5SDimitry Andric  ++base::__sz();
131106c3fb27SDimitry Andric  return iterator(__nl);
13120b57cec5SDimitry Andric}
13130b57cec5SDimitry Andric
13140b57cec5SDimitry Andric#endif // _LIBCPP_CXX03_LANG
13150b57cec5SDimitry Andric
13160b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
1317cb14a3feSDimitry Andricvoid list<_Tp, _Alloc>::pop_front() {
131806c3fb27SDimitry Andric  _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "list::pop_front() called with empty list");
13190b57cec5SDimitry Andric  __link_pointer __n = base::__end_.__next_;
13200b57cec5SDimitry Andric  base::__unlink_nodes(__n, __n);
13210b57cec5SDimitry Andric  --base::__sz();
13225f757f3fSDimitry Andric  this->__delete_node(__n->__as_node());
13230b57cec5SDimitry Andric}
13240b57cec5SDimitry Andric
13250b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
1326cb14a3feSDimitry Andricvoid list<_Tp, _Alloc>::pop_back() {
132706c3fb27SDimitry Andric  _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "list::pop_back() called on an empty list");
13280b57cec5SDimitry Andric  __link_pointer __n = base::__end_.__prev_;
13290b57cec5SDimitry Andric  base::__unlink_nodes(__n, __n);
13300b57cec5SDimitry Andric  --base::__sz();
13315f757f3fSDimitry Andric  this->__delete_node(__n->__as_node());
13320b57cec5SDimitry Andric}
13330b57cec5SDimitry Andric
13340b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
1335cb14a3feSDimitry Andrictypename list<_Tp, _Alloc>::iterator list<_Tp, _Alloc>::erase(const_iterator __p) {
1336cb14a3feSDimitry Andric  _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__p != end(), "list::erase(iterator) called with a non-dereferenceable iterator");
13370b57cec5SDimitry Andric  __link_pointer __n = __p.__ptr_;
13380b57cec5SDimitry Andric  __link_pointer __r = __n->__next_;
13390b57cec5SDimitry Andric  base::__unlink_nodes(__n, __n);
13400b57cec5SDimitry Andric  --base::__sz();
13415f757f3fSDimitry Andric  this->__delete_node(__n->__as_node());
134206c3fb27SDimitry Andric  return iterator(__r);
13430b57cec5SDimitry Andric}
13440b57cec5SDimitry Andric
13450b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
1346cb14a3feSDimitry Andrictypename list<_Tp, _Alloc>::iterator list<_Tp, _Alloc>::erase(const_iterator __f, const_iterator __l) {
1347cb14a3feSDimitry Andric  if (__f != __l) {
13480b57cec5SDimitry Andric    base::__unlink_nodes(__f.__ptr_, __l.__ptr_->__prev_);
1349cb14a3feSDimitry Andric    while (__f != __l) {
13500b57cec5SDimitry Andric      __link_pointer __n = __f.__ptr_;
13510b57cec5SDimitry Andric      ++__f;
13520b57cec5SDimitry Andric      --base::__sz();
13535f757f3fSDimitry Andric      this->__delete_node(__n->__as_node());
13540b57cec5SDimitry Andric    }
13550b57cec5SDimitry Andric  }
135606c3fb27SDimitry Andric  return iterator(__l.__ptr_);
13570b57cec5SDimitry Andric}
13580b57cec5SDimitry Andric
13590b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
1360cb14a3feSDimitry Andricvoid list<_Tp, _Alloc>::resize(size_type __n) {
13610b57cec5SDimitry Andric  if (__n < base::__sz())
13620b57cec5SDimitry Andric    erase(__iterator(__n), end());
1363cb14a3feSDimitry Andric  else if (__n > base::__sz()) {
13640b57cec5SDimitry Andric    __n -= base::__sz();
13650b57cec5SDimitry Andric    size_type __ds        = 0;
13665f757f3fSDimitry Andric    __node_pointer __node = this->__create_node(/* prev = */ nullptr, /* next = */ nullptr);
13670b57cec5SDimitry Andric    ++__ds;
13685f757f3fSDimitry Andric    iterator __r = iterator(__node->__as_link());
13690b57cec5SDimitry Andric    iterator __e = __r;
137006c3fb27SDimitry Andric#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1371cb14a3feSDimitry Andric    try {
137206c3fb27SDimitry Andric#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1373cb14a3feSDimitry Andric      for (--__n; __n != 0; --__n, (void)++__e, ++__ds) {
13745f757f3fSDimitry Andric        __e.__ptr_->__next_ = this->__create_node(/* prev = */ __e.__ptr_, /* next = */ nullptr)->__as_link();
13750b57cec5SDimitry Andric      }
137606c3fb27SDimitry Andric#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1377cb14a3feSDimitry Andric    } catch (...) {
1378cb14a3feSDimitry Andric      while (true) {
13790b57cec5SDimitry Andric        __link_pointer __prev    = __e.__ptr_->__prev_;
13805f757f3fSDimitry Andric        __node_pointer __current = __e.__ptr_->__as_node();
13815f757f3fSDimitry Andric        this->__delete_node(__current);
13820b57cec5SDimitry Andric        if (__prev == 0)
13830b57cec5SDimitry Andric          break;
138406c3fb27SDimitry Andric        __e = iterator(__prev);
13850b57cec5SDimitry Andric      }
13860b57cec5SDimitry Andric      throw;
13870b57cec5SDimitry Andric    }
138806c3fb27SDimitry Andric#endif // _LIBCPP_HAS_NO_EXCEPTIONS
13890b57cec5SDimitry Andric    __link_nodes_at_back(__r.__ptr_, __e.__ptr_);
13900b57cec5SDimitry Andric    base::__sz() += __ds;
13910b57cec5SDimitry Andric  }
13920b57cec5SDimitry Andric}
13930b57cec5SDimitry Andric
13940b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
1395cb14a3feSDimitry Andricvoid list<_Tp, _Alloc>::resize(size_type __n, const value_type& __x) {
13960b57cec5SDimitry Andric  if (__n < base::__sz())
13970b57cec5SDimitry Andric    erase(__iterator(__n), end());
1398cb14a3feSDimitry Andric  else if (__n > base::__sz()) {
13990b57cec5SDimitry Andric    __n -= base::__sz();
14000b57cec5SDimitry Andric    size_type __ds        = 0;
14015f757f3fSDimitry Andric    __node_pointer __node = this->__create_node(/* prev = */ nullptr, /* next = */ nullptr, __x);
14020b57cec5SDimitry Andric    ++__ds;
14035f757f3fSDimitry Andric    __link_pointer __nl = __node->__as_link();
140406c3fb27SDimitry Andric    iterator __r        = iterator(__nl);
14050b57cec5SDimitry Andric    iterator __e        = __r;
140606c3fb27SDimitry Andric#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1407cb14a3feSDimitry Andric    try {
140806c3fb27SDimitry Andric#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1409cb14a3feSDimitry Andric      for (--__n; __n != 0; --__n, (void)++__e, ++__ds) {
14105f757f3fSDimitry Andric        __e.__ptr_->__next_ = this->__create_node(/* prev = */ __e.__ptr_, /* next = */ nullptr, __x)->__as_link();
14110b57cec5SDimitry Andric      }
141206c3fb27SDimitry Andric#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1413cb14a3feSDimitry Andric    } catch (...) {
1414cb14a3feSDimitry Andric      while (true) {
14150b57cec5SDimitry Andric        __link_pointer __prev    = __e.__ptr_->__prev_;
14165f757f3fSDimitry Andric        __node_pointer __current = __e.__ptr_->__as_node();
14175f757f3fSDimitry Andric        this->__delete_node(__current);
14180b57cec5SDimitry Andric        if (__prev == 0)
14190b57cec5SDimitry Andric          break;
142006c3fb27SDimitry Andric        __e = iterator(__prev);
14210b57cec5SDimitry Andric      }
14220b57cec5SDimitry Andric      throw;
14230b57cec5SDimitry Andric    }
142406c3fb27SDimitry Andric#endif // _LIBCPP_HAS_NO_EXCEPTIONS
14250b57cec5SDimitry Andric    __link_nodes(base::__end_as_link(), __r.__ptr_, __e.__ptr_);
14260b57cec5SDimitry Andric    base::__sz() += __ds;
14270b57cec5SDimitry Andric  }
14280b57cec5SDimitry Andric}
14290b57cec5SDimitry Andric
14300b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
1431cb14a3feSDimitry Andricvoid list<_Tp, _Alloc>::splice(const_iterator __p, list& __c) {
1432cb14a3feSDimitry Andric  _LIBCPP_ASSERT_VALID_INPUT_RANGE(
1433cb14a3feSDimitry Andric      this != std::addressof(__c), "list::splice(iterator, list) called with this == &list");
1434cb14a3feSDimitry Andric  if (!__c.empty()) {
14350b57cec5SDimitry Andric    __link_pointer __f = __c.__end_.__next_;
14360b57cec5SDimitry Andric    __link_pointer __l = __c.__end_.__prev_;
14370b57cec5SDimitry Andric    base::__unlink_nodes(__f, __l);
14380b57cec5SDimitry Andric    __link_nodes(__p.__ptr_, __f, __l);
14390b57cec5SDimitry Andric    base::__sz() += __c.__sz();
14400b57cec5SDimitry Andric    __c.__sz() = 0;
14410b57cec5SDimitry Andric  }
14420b57cec5SDimitry Andric}
14430b57cec5SDimitry Andric
14440b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
1445cb14a3feSDimitry Andricvoid list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __i) {
1446cb14a3feSDimitry Andric  if (__p.__ptr_ != __i.__ptr_ && __p.__ptr_ != __i.__ptr_->__next_) {
14470b57cec5SDimitry Andric    __link_pointer __f = __i.__ptr_;
14480b57cec5SDimitry Andric    base::__unlink_nodes(__f, __f);
14490b57cec5SDimitry Andric    __link_nodes(__p.__ptr_, __f, __f);
14500b57cec5SDimitry Andric    --__c.__sz();
14510b57cec5SDimitry Andric    ++base::__sz();
14520b57cec5SDimitry Andric  }
14530b57cec5SDimitry Andric}
14540b57cec5SDimitry Andric
14550b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
1456cb14a3feSDimitry Andricvoid list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l) {
1457cb14a3feSDimitry Andric  if (__f != __l) {
14580b57cec5SDimitry Andric    __link_pointer __first = __f.__ptr_;
14590b57cec5SDimitry Andric    --__l;
14600b57cec5SDimitry Andric    __link_pointer __last = __l.__ptr_;
1461cb14a3feSDimitry Andric    if (this != std::addressof(__c)) {
14625f757f3fSDimitry Andric      size_type __s = std::distance(__f, __l) + 1;
14630b57cec5SDimitry Andric      __c.__sz() -= __s;
14640b57cec5SDimitry Andric      base::__sz() += __s;
14650b57cec5SDimitry Andric    }
14660b57cec5SDimitry Andric    base::__unlink_nodes(__first, __last);
14670b57cec5SDimitry Andric    __link_nodes(__p.__ptr_, __first, __last);
14680b57cec5SDimitry Andric  }
14690b57cec5SDimitry Andric}
14700b57cec5SDimitry Andric
14710b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
1472cb14a3feSDimitry Andrictypename list<_Tp, _Alloc>::__remove_return_type list<_Tp, _Alloc>::remove(const value_type& __x) {
14730b57cec5SDimitry Andric  list<_Tp, _Alloc> __deleted_nodes(get_allocator()); // collect the nodes we're removing
1474cb14a3feSDimitry Andric  for (const_iterator __i = begin(), __e = end(); __i != __e;) {
1475cb14a3feSDimitry Andric    if (*__i == __x) {
14765f757f3fSDimitry Andric      const_iterator __j = std::next(__i);
14770b57cec5SDimitry Andric      for (; __j != __e && *__j == __x; ++__j)
14780b57cec5SDimitry Andric        ;
14790b57cec5SDimitry Andric      __deleted_nodes.splice(__deleted_nodes.end(), *this, __i, __j);
14800b57cec5SDimitry Andric      __i = __j;
14810b57cec5SDimitry Andric      if (__i != __e)
14820b57cec5SDimitry Andric        ++__i;
1483cb14a3feSDimitry Andric    } else
14840b57cec5SDimitry Andric      ++__i;
14850b57cec5SDimitry Andric  }
14860b57cec5SDimitry Andric
14870b57cec5SDimitry Andric  return (__remove_return_type)__deleted_nodes.size();
14880b57cec5SDimitry Andric}
14890b57cec5SDimitry Andric
14900b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
14910b57cec5SDimitry Andrictemplate <class _Pred>
1492cb14a3feSDimitry Andrictypename list<_Tp, _Alloc>::__remove_return_type list<_Tp, _Alloc>::remove_if(_Pred __pred) {
14930b57cec5SDimitry Andric  list<_Tp, _Alloc> __deleted_nodes(get_allocator()); // collect the nodes we're removing
1494cb14a3feSDimitry Andric  for (iterator __i = begin(), __e = end(); __i != __e;) {
1495cb14a3feSDimitry Andric    if (__pred(*__i)) {
14965f757f3fSDimitry Andric      iterator __j = std::next(__i);
14970b57cec5SDimitry Andric      for (; __j != __e && __pred(*__j); ++__j)
14980b57cec5SDimitry Andric        ;
14990b57cec5SDimitry Andric      __deleted_nodes.splice(__deleted_nodes.end(), *this, __i, __j);
15000b57cec5SDimitry Andric      __i = __j;
15010b57cec5SDimitry Andric      if (__i != __e)
15020b57cec5SDimitry Andric        ++__i;
1503cb14a3feSDimitry Andric    } else
15040b57cec5SDimitry Andric      ++__i;
15050b57cec5SDimitry Andric  }
15060b57cec5SDimitry Andric
15070b57cec5SDimitry Andric  return (__remove_return_type)__deleted_nodes.size();
15080b57cec5SDimitry Andric}
15090b57cec5SDimitry Andric
15100b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
15110b57cec5SDimitry Andrictemplate <class _BinaryPred>
1512cb14a3feSDimitry Andrictypename list<_Tp, _Alloc>::__remove_return_type list<_Tp, _Alloc>::unique(_BinaryPred __binary_pred) {
15130b57cec5SDimitry Andric  list<_Tp, _Alloc> __deleted_nodes(get_allocator()); // collect the nodes we're removing
1514cb14a3feSDimitry Andric  for (iterator __i = begin(), __e = end(); __i != __e;) {
15155f757f3fSDimitry Andric    iterator __j = std::next(__i);
15160b57cec5SDimitry Andric    for (; __j != __e && __binary_pred(*__i, *__j); ++__j)
15170b57cec5SDimitry Andric      ;
15180b57cec5SDimitry Andric    if (++__i != __j) {
15190b57cec5SDimitry Andric      __deleted_nodes.splice(__deleted_nodes.end(), *this, __i, __j);
15200b57cec5SDimitry Andric      __i = __j;
15210b57cec5SDimitry Andric    }
15220b57cec5SDimitry Andric  }
15230b57cec5SDimitry Andric
15240b57cec5SDimitry Andric  return (__remove_return_type)__deleted_nodes.size();
15250b57cec5SDimitry Andric}
15260b57cec5SDimitry Andric
15270b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
1528cb14a3feSDimitry Andricinline void list<_Tp, _Alloc>::merge(list& __c) {
152906c3fb27SDimitry Andric  merge(__c, __less<>());
15300b57cec5SDimitry Andric}
15310b57cec5SDimitry Andric
15320b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
15330b57cec5SDimitry Andrictemplate <class _Comp>
1534cb14a3feSDimitry Andricvoid list<_Tp, _Alloc>::merge(list& __c, _Comp __comp) {
1535cb14a3feSDimitry Andric  if (this != std::addressof(__c)) {
15360b57cec5SDimitry Andric    iterator __f1 = begin();
15370b57cec5SDimitry Andric    iterator __e1 = end();
15380b57cec5SDimitry Andric    iterator __f2 = __c.begin();
15390b57cec5SDimitry Andric    iterator __e2 = __c.end();
1540cb14a3feSDimitry Andric    while (__f1 != __e1 && __f2 != __e2) {
1541cb14a3feSDimitry Andric      if (__comp(*__f2, *__f1)) {
15420b57cec5SDimitry Andric        size_type __ds = 1;
15435f757f3fSDimitry Andric        iterator __m2  = std::next(__f2);
1544349cc55cSDimitry Andric        for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2, (void)++__ds)
15450b57cec5SDimitry Andric          ;
15460b57cec5SDimitry Andric        base::__sz() += __ds;
15470b57cec5SDimitry Andric        __c.__sz() -= __ds;
15480b57cec5SDimitry Andric        __link_pointer __f = __f2.__ptr_;
15490b57cec5SDimitry Andric        __link_pointer __l = __m2.__ptr_->__prev_;
15500b57cec5SDimitry Andric        __f2               = __m2;
15510b57cec5SDimitry Andric        base::__unlink_nodes(__f, __l);
15525f757f3fSDimitry Andric        __m2 = std::next(__f1);
15530b57cec5SDimitry Andric        __link_nodes(__f1.__ptr_, __f, __l);
15540b57cec5SDimitry Andric        __f1 = __m2;
1555cb14a3feSDimitry Andric      } else
15560b57cec5SDimitry Andric        ++__f1;
15570b57cec5SDimitry Andric    }
15580b57cec5SDimitry Andric    splice(__e1, __c);
15590b57cec5SDimitry Andric  }
15600b57cec5SDimitry Andric}
15610b57cec5SDimitry Andric
15620b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
1563cb14a3feSDimitry Andricinline void list<_Tp, _Alloc>::sort() {
156406c3fb27SDimitry Andric  sort(__less<>());
15650b57cec5SDimitry Andric}
15660b57cec5SDimitry Andric
15670b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
15680b57cec5SDimitry Andrictemplate <class _Comp>
1569cb14a3feSDimitry Andricinline void list<_Tp, _Alloc>::sort(_Comp __comp) {
15700b57cec5SDimitry Andric  __sort(begin(), end(), base::__sz(), __comp);
15710b57cec5SDimitry Andric}
15720b57cec5SDimitry Andric
15730b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
15740b57cec5SDimitry Andrictemplate <class _Comp>
15750b57cec5SDimitry Andrictypename list<_Tp, _Alloc>::iterator
1576cb14a3feSDimitry Andriclist<_Tp, _Alloc>::__sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp) {
1577cb14a3feSDimitry Andric  switch (__n) {
15780b57cec5SDimitry Andric  case 0:
15790b57cec5SDimitry Andric  case 1:
15800b57cec5SDimitry Andric    return __f1;
15810b57cec5SDimitry Andric  case 2:
1582cb14a3feSDimitry Andric    if (__comp(*--__e2, *__f1)) {
15830b57cec5SDimitry Andric      __link_pointer __f = __e2.__ptr_;
15840b57cec5SDimitry Andric      base::__unlink_nodes(__f, __f);
15850b57cec5SDimitry Andric      __link_nodes(__f1.__ptr_, __f, __f);
15860b57cec5SDimitry Andric      return __e2;
15870b57cec5SDimitry Andric    }
15880b57cec5SDimitry Andric    return __f1;
15890b57cec5SDimitry Andric  }
15900b57cec5SDimitry Andric  size_type __n2 = __n / 2;
15915f757f3fSDimitry Andric  iterator __e1  = std::next(__f1, __n2);
15920b57cec5SDimitry Andric  iterator __r = __f1 = __sort(__f1, __e1, __n2, __comp);
15930b57cec5SDimitry Andric  iterator __f2 = __e1 = __sort(__e1, __e2, __n - __n2, __comp);
1594cb14a3feSDimitry Andric  if (__comp(*__f2, *__f1)) {
15955f757f3fSDimitry Andric    iterator __m2 = std::next(__f2);
15960b57cec5SDimitry Andric    for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2)
15970b57cec5SDimitry Andric      ;
15980b57cec5SDimitry Andric    __link_pointer __f = __f2.__ptr_;
15990b57cec5SDimitry Andric    __link_pointer __l = __m2.__ptr_->__prev_;
16000b57cec5SDimitry Andric    __r                = __f2;
16010b57cec5SDimitry Andric    __e1 = __f2 = __m2;
16020b57cec5SDimitry Andric    base::__unlink_nodes(__f, __l);
16035f757f3fSDimitry Andric    __m2 = std::next(__f1);
16040b57cec5SDimitry Andric    __link_nodes(__f1.__ptr_, __f, __l);
16050b57cec5SDimitry Andric    __f1 = __m2;
1606cb14a3feSDimitry Andric  } else
16070b57cec5SDimitry Andric    ++__f1;
1608cb14a3feSDimitry Andric  while (__f1 != __e1 && __f2 != __e2) {
1609cb14a3feSDimitry Andric    if (__comp(*__f2, *__f1)) {
16105f757f3fSDimitry Andric      iterator __m2 = std::next(__f2);
16110b57cec5SDimitry Andric      for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2)
16120b57cec5SDimitry Andric        ;
16130b57cec5SDimitry Andric      __link_pointer __f = __f2.__ptr_;
16140b57cec5SDimitry Andric      __link_pointer __l = __m2.__ptr_->__prev_;
16150b57cec5SDimitry Andric      if (__e1 == __f2)
16160b57cec5SDimitry Andric        __e1 = __m2;
16170b57cec5SDimitry Andric      __f2 = __m2;
16180b57cec5SDimitry Andric      base::__unlink_nodes(__f, __l);
16195f757f3fSDimitry Andric      __m2 = std::next(__f1);
16200b57cec5SDimitry Andric      __link_nodes(__f1.__ptr_, __f, __l);
16210b57cec5SDimitry Andric      __f1 = __m2;
1622cb14a3feSDimitry Andric    } else
16230b57cec5SDimitry Andric      ++__f1;
16240b57cec5SDimitry Andric  }
16250b57cec5SDimitry Andric  return __r;
16260b57cec5SDimitry Andric}
16270b57cec5SDimitry Andric
16280b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
1629cb14a3feSDimitry Andricvoid list<_Tp, _Alloc>::reverse() _NOEXCEPT {
1630cb14a3feSDimitry Andric  if (base::__sz() > 1) {
16310b57cec5SDimitry Andric    iterator __e = end();
1632cb14a3feSDimitry Andric    for (iterator __i = begin(); __i.__ptr_ != __e.__ptr_;) {
16335f757f3fSDimitry Andric      std::swap(__i.__ptr_->__prev_, __i.__ptr_->__next_);
16340b57cec5SDimitry Andric      __i.__ptr_ = __i.__ptr_->__prev_;
16350b57cec5SDimitry Andric    }
16365f757f3fSDimitry Andric    std::swap(__e.__ptr_->__prev_, __e.__ptr_->__next_);
16370b57cec5SDimitry Andric  }
16380b57cec5SDimitry Andric}
16390b57cec5SDimitry Andric
16400b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
1641cb14a3feSDimitry Andricbool list<_Tp, _Alloc>::__invariants() const {
16425f757f3fSDimitry Andric  return size() == std::distance(begin(), end());
16430b57cec5SDimitry Andric}
16440b57cec5SDimitry Andric
16450b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
1646cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool operator==(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y) {
16475f757f3fSDimitry Andric  return __x.size() == __y.size() && std::equal(__x.begin(), __x.end(), __y.begin());
16480b57cec5SDimitry Andric}
16490b57cec5SDimitry Andric
165006c3fb27SDimitry Andric#if _LIBCPP_STD_VER <= 17
165106c3fb27SDimitry Andric
16520b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
1653cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool operator<(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y) {
16545f757f3fSDimitry Andric  return std::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
16550b57cec5SDimitry Andric}
16560b57cec5SDimitry Andric
16570b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
1658cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool operator!=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y) {
16590b57cec5SDimitry Andric  return !(__x == __y);
16600b57cec5SDimitry Andric}
16610b57cec5SDimitry Andric
16620b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
1663cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool operator>(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y) {
16640b57cec5SDimitry Andric  return __y < __x;
16650b57cec5SDimitry Andric}
16660b57cec5SDimitry Andric
16670b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
1668cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool operator>=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y) {
16690b57cec5SDimitry Andric  return !(__x < __y);
16700b57cec5SDimitry Andric}
16710b57cec5SDimitry Andric
16720b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
1673cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool operator<=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y) {
16740b57cec5SDimitry Andric  return !(__y < __x);
16750b57cec5SDimitry Andric}
16760b57cec5SDimitry Andric
167706c3fb27SDimitry Andric#else // _LIBCPP_STD_VER <= 17
167806c3fb27SDimitry Andric
167906c3fb27SDimitry Andrictemplate <class _Tp, class _Allocator>
168006c3fb27SDimitry Andric_LIBCPP_HIDE_FROM_ABI __synth_three_way_result<_Tp>
168106c3fb27SDimitry Andricoperator<=>(const list<_Tp, _Allocator>& __x, const list<_Tp, _Allocator>& __y) {
168206c3fb27SDimitry Andric  return std::lexicographical_compare_three_way(
1683*0fca6ea1SDimitry Andric      __x.begin(), __x.end(), __y.begin(), __y.end(), std::__synth_three_way);
168406c3fb27SDimitry Andric}
168506c3fb27SDimitry Andric
168606c3fb27SDimitry Andric#endif // _LIBCPP_STD_VER <= 17
168706c3fb27SDimitry Andric
16880b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
1689cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI void swap(list<_Tp, _Alloc>& __x, list<_Tp, _Alloc>& __y)
1690cb14a3feSDimitry Andric    _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y))) {
16910b57cec5SDimitry Andric  __x.swap(__y);
16920b57cec5SDimitry Andric}
16930b57cec5SDimitry Andric
169406c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 20
16950b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator, class _Predicate>
16965f757f3fSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI typename list<_Tp, _Allocator>::size_type
16975ffd83dbSDimitry Andricerase_if(list<_Tp, _Allocator>& __c, _Predicate __pred) {
16985ffd83dbSDimitry Andric  return __c.remove_if(__pred);
16995ffd83dbSDimitry Andric}
17000b57cec5SDimitry Andric
17010b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator, class _Up>
17025f757f3fSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI typename list<_Tp, _Allocator>::size_type
17035ffd83dbSDimitry Andricerase(list<_Tp, _Allocator>& __c, const _Up& __v) {
17045f757f3fSDimitry Andric  return std::erase_if(__c, [&](auto& __elem) { return __elem == __v; });
17055ffd83dbSDimitry Andric}
170681ad6265SDimitry Andric
170781ad6265SDimitry Andrictemplate <>
170881ad6265SDimitry Andricinline constexpr bool __format::__enable_insertable<std::list<char>> = true;
170981ad6265SDimitry Andric#  ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
171081ad6265SDimitry Andrictemplate <>
171181ad6265SDimitry Andricinline constexpr bool __format::__enable_insertable<std::list<wchar_t>> = true;
17120b57cec5SDimitry Andric#  endif
17130b57cec5SDimitry Andric
171406c3fb27SDimitry Andric#endif // _LIBCPP_STD_VER >= 20
171581ad6265SDimitry Andric
17160b57cec5SDimitry Andric_LIBCPP_END_NAMESPACE_STD
17170b57cec5SDimitry Andric
171806c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 17
1719bdd1243dSDimitry Andric_LIBCPP_BEGIN_NAMESPACE_STD
1720bdd1243dSDimitry Andricnamespace pmr {
1721bdd1243dSDimitry Andrictemplate <class _ValueT>
172206c3fb27SDimitry Andricusing list _LIBCPP_AVAILABILITY_PMR = std::list<_ValueT, polymorphic_allocator<_ValueT>>;
1723bdd1243dSDimitry Andric} // namespace pmr
1724bdd1243dSDimitry Andric_LIBCPP_END_NAMESPACE_STD
1725bdd1243dSDimitry Andric#endif
1726bdd1243dSDimitry Andric
17270b57cec5SDimitry Andric_LIBCPP_POP_MACROS
17280b57cec5SDimitry Andric
1729bdd1243dSDimitry Andric#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
1730bdd1243dSDimitry Andric#  include <algorithm>
1731bdd1243dSDimitry Andric#  include <atomic>
1732bdd1243dSDimitry Andric#  include <concepts>
17335f757f3fSDimitry Andric#  include <cstdint>
173406c3fb27SDimitry Andric#  include <cstdlib>
1735bdd1243dSDimitry Andric#  include <functional>
1736bdd1243dSDimitry Andric#  include <iosfwd>
1737bdd1243dSDimitry Andric#  include <iterator>
17385f757f3fSDimitry Andric#  include <stdexcept>
173906c3fb27SDimitry Andric#  include <type_traits>
1740bdd1243dSDimitry Andric#  include <typeinfo>
1741bdd1243dSDimitry Andric#endif
1742bdd1243dSDimitry Andric
17430b57cec5SDimitry Andric#endif // _LIBCPP_LIST
1744