xref: /llvm-project-15.0.7/libcxx/include/list (revision f4fb72e6)
13e519524SHoward Hinnant// -*- C++ -*-
2eb8650a7SLouis Dionne//===----------------------------------------------------------------------===//
33e519524SHoward Hinnant//
457b08b09SChandler Carruth// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
557b08b09SChandler Carruth// See https://llvm.org/LICENSE.txt for license information.
657b08b09SChandler Carruth// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
73e519524SHoward Hinnant//
83e519524SHoward Hinnant//===----------------------------------------------------------------------===//
93e519524SHoward Hinnant
103e519524SHoward Hinnant#ifndef _LIBCPP_LIST
113e519524SHoward Hinnant#define _LIBCPP_LIST
123e519524SHoward Hinnant
133e519524SHoward Hinnant/*
143e519524SHoward Hinnant    list synopsis
153e519524SHoward Hinnant
163e519524SHoward Hinnantnamespace std
173e519524SHoward Hinnant{
183e519524SHoward Hinnant
193e519524SHoward Hinnanttemplate <class T, class Alloc = allocator<T> >
203e519524SHoward Hinnantclass list
213e519524SHoward Hinnant{
223e519524SHoward Hinnantpublic:
233e519524SHoward Hinnant
243e519524SHoward Hinnant    // types:
253e519524SHoward Hinnant    typedef T value_type;
263e519524SHoward Hinnant    typedef Alloc allocator_type;
273e519524SHoward Hinnant    typedef typename allocator_type::reference reference;
283e519524SHoward Hinnant    typedef typename allocator_type::const_reference const_reference;
293e519524SHoward Hinnant    typedef typename allocator_type::pointer pointer;
303e519524SHoward Hinnant    typedef typename allocator_type::const_pointer const_pointer;
313e519524SHoward Hinnant    typedef implementation-defined iterator;
323e519524SHoward Hinnant    typedef implementation-defined const_iterator;
333e519524SHoward Hinnant    typedef implementation-defined size_type;
343e519524SHoward Hinnant    typedef implementation-defined difference_type;
353e519524SHoward Hinnant    typedef reverse_iterator<iterator> reverse_iterator;
363e519524SHoward Hinnant    typedef reverse_iterator<const_iterator> const_reverse_iterator;
373e519524SHoward Hinnant
3845900104SHoward Hinnant    list()
3945900104SHoward Hinnant        noexcept(is_nothrow_default_constructible<allocator_type>::value);
403e519524SHoward Hinnant    explicit list(const allocator_type& a);
413e519524SHoward Hinnant    explicit list(size_type n);
42f1b6d1b5SMarshall Clow    explicit list(size_type n, const allocator_type& a); // C++14
433e519524SHoward Hinnant    list(size_type n, const value_type& value);
443e519524SHoward Hinnant    list(size_type n, const value_type& value, const allocator_type& a);
453e519524SHoward Hinnant    template <class Iter>
463e519524SHoward Hinnant        list(Iter first, Iter last);
473e519524SHoward Hinnant    template <class Iter>
483e519524SHoward Hinnant        list(Iter first, Iter last, const allocator_type& a);
493e519524SHoward Hinnant    list(const list& x);
503e519524SHoward Hinnant    list(const list&, const allocator_type& a);
5145900104SHoward Hinnant    list(list&& x)
5245900104SHoward Hinnant        noexcept(is_nothrow_move_constructible<allocator_type>::value);
533e519524SHoward Hinnant    list(list&&, const allocator_type& a);
543e519524SHoward Hinnant    list(initializer_list<value_type>);
553e519524SHoward Hinnant    list(initializer_list<value_type>, const allocator_type& a);
563e519524SHoward Hinnant
573e519524SHoward Hinnant    ~list();
583e519524SHoward Hinnant
593e519524SHoward Hinnant    list& operator=(const list& x);
6045900104SHoward Hinnant    list& operator=(list&& x)
6145900104SHoward Hinnant        noexcept(
6245900104SHoward Hinnant             allocator_type::propagate_on_container_move_assignment::value &&
6345900104SHoward Hinnant             is_nothrow_move_assignable<allocator_type>::value);
643e519524SHoward Hinnant    list& operator=(initializer_list<value_type>);
653e519524SHoward Hinnant    template <class Iter>
663e519524SHoward Hinnant        void assign(Iter first, Iter last);
673e519524SHoward Hinnant    void assign(size_type n, const value_type& t);
683e519524SHoward Hinnant    void assign(initializer_list<value_type>);
693e519524SHoward Hinnant
7045900104SHoward Hinnant    allocator_type get_allocator() const noexcept;
713e519524SHoward Hinnant
7245900104SHoward Hinnant    iterator begin() noexcept;
7345900104SHoward Hinnant    const_iterator begin() const noexcept;
7445900104SHoward Hinnant    iterator end() noexcept;
7545900104SHoward Hinnant    const_iterator end() const noexcept;
7645900104SHoward Hinnant    reverse_iterator rbegin() noexcept;
7745900104SHoward Hinnant    const_reverse_iterator rbegin() const noexcept;
7845900104SHoward Hinnant    reverse_iterator rend() noexcept;
7945900104SHoward Hinnant    const_reverse_iterator rend() const noexcept;
8045900104SHoward Hinnant    const_iterator cbegin() const noexcept;
8145900104SHoward Hinnant    const_iterator cend() const noexcept;
8245900104SHoward Hinnant    const_reverse_iterator crbegin() const noexcept;
8345900104SHoward Hinnant    const_reverse_iterator crend() const noexcept;
843e519524SHoward Hinnant
853e519524SHoward Hinnant    reference front();
863e519524SHoward Hinnant    const_reference front() const;
873e519524SHoward Hinnant    reference back();
883e519524SHoward Hinnant    const_reference back() const;
893e519524SHoward Hinnant
9045900104SHoward Hinnant    bool empty() const noexcept;
9145900104SHoward Hinnant    size_type size() const noexcept;
9245900104SHoward Hinnant    size_type max_size() const noexcept;
933e519524SHoward Hinnant
943e519524SHoward Hinnant    template <class... Args>
9563b560beSMarshall Clow        reference emplace_front(Args&&... args); // reference in C++17
963e519524SHoward Hinnant    void pop_front();
973e519524SHoward Hinnant    template <class... Args>
9863b560beSMarshall Clow        reference emplace_back(Args&&... args);  // reference in C++17
993e519524SHoward Hinnant    void pop_back();
1003e519524SHoward Hinnant    void push_front(const value_type& x);
1013e519524SHoward Hinnant    void push_front(value_type&& x);
1023e519524SHoward Hinnant    void push_back(const value_type& x);
1033e519524SHoward Hinnant    void push_back(value_type&& x);
1043e519524SHoward Hinnant    template <class... Args>
1053e519524SHoward Hinnant        iterator emplace(const_iterator position, Args&&... args);
1063e519524SHoward Hinnant    iterator insert(const_iterator position, const value_type& x);
1073e519524SHoward Hinnant    iterator insert(const_iterator position, value_type&& x);
1083e519524SHoward Hinnant    iterator insert(const_iterator position, size_type n, const value_type& x);
1093e519524SHoward Hinnant    template <class Iter>
1103e519524SHoward Hinnant        iterator insert(const_iterator position, Iter first, Iter last);
1113e519524SHoward Hinnant    iterator insert(const_iterator position, initializer_list<value_type> il);
1123e519524SHoward Hinnant
1133e519524SHoward Hinnant    iterator erase(const_iterator position);
1143e519524SHoward Hinnant    iterator erase(const_iterator position, const_iterator last);
1153e519524SHoward Hinnant
1163e519524SHoward Hinnant    void resize(size_type sz);
1173e519524SHoward Hinnant    void resize(size_type sz, const value_type& c);
1183e519524SHoward Hinnant
11945900104SHoward Hinnant    void swap(list&)
120e3fbe143SMarshall Clow        noexcept(allocator_traits<allocator_type>::is_always_equal::value);  // C++17
12145900104SHoward Hinnant    void clear() noexcept;
1223e519524SHoward Hinnant
1233e519524SHoward Hinnant    void splice(const_iterator position, list& x);
1243e519524SHoward Hinnant    void splice(const_iterator position, list&& x);
1253e519524SHoward Hinnant    void splice(const_iterator position, list& x, const_iterator i);
1263e519524SHoward Hinnant    void splice(const_iterator position, list&& x, const_iterator i);
1273e519524SHoward Hinnant    void splice(const_iterator position, list& x, const_iterator first,
1283e519524SHoward Hinnant                                                  const_iterator last);
1293e519524SHoward Hinnant    void splice(const_iterator position, list&& x, const_iterator first,
1303e519524SHoward Hinnant                                                  const_iterator last);
1313e519524SHoward Hinnant
1321ab3fe8aSMarshall Clow    size_type remove(const value_type& value);       // void before C++20
133f814dcbaSMarshall Clow    template <class Pred>
134f814dcbaSMarshall Clow      size_type remove_if(Pred pred);                // void before C++20
1351ab3fe8aSMarshall Clow    size_type unique();                              // void before C++20
1363e519524SHoward Hinnant    template <class BinaryPredicate>
1371ab3fe8aSMarshall Clow      size_type unique(BinaryPredicate binary_pred); // void before C++20
1383e519524SHoward Hinnant    void merge(list& x);
1393e519524SHoward Hinnant    void merge(list&& x);
1403e519524SHoward Hinnant    template <class Compare>
1413e519524SHoward Hinnant        void merge(list& x, Compare comp);
1423e519524SHoward Hinnant    template <class Compare>
1433e519524SHoward Hinnant        void merge(list&& x, Compare comp);
1443e519524SHoward Hinnant    void sort();
1453e519524SHoward Hinnant    template <class Compare>
1463e519524SHoward Hinnant        void sort(Compare comp);
14745900104SHoward Hinnant    void reverse() noexcept;
1483e519524SHoward Hinnant};
1493e519524SHoward Hinnant
1504a227e58SMarshall Clow
1514a227e58SMarshall Clowtemplate <class InputIterator, class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
1524a227e58SMarshall Clow    list(InputIterator, InputIterator, Allocator = Allocator())
1534a227e58SMarshall Clow    -> list<typename iterator_traits<InputIterator>::value_type, Allocator>;  // C++17
1544a227e58SMarshall Clow
1553e519524SHoward Hinnanttemplate <class T, class Alloc>
1563e519524SHoward Hinnant    bool operator==(const list<T,Alloc>& x, const list<T,Alloc>& y);
1573e519524SHoward Hinnanttemplate <class T, class Alloc>
1583e519524SHoward Hinnant    bool operator< (const list<T,Alloc>& x, const list<T,Alloc>& y);
1593e519524SHoward Hinnanttemplate <class T, class Alloc>
1603e519524SHoward Hinnant    bool operator!=(const list<T,Alloc>& x, const list<T,Alloc>& y);
1613e519524SHoward Hinnanttemplate <class T, class Alloc>
1623e519524SHoward Hinnant    bool operator> (const list<T,Alloc>& x, const list<T,Alloc>& y);
1633e519524SHoward Hinnanttemplate <class T, class Alloc>
1643e519524SHoward Hinnant    bool operator>=(const list<T,Alloc>& x, const list<T,Alloc>& y);
1653e519524SHoward Hinnanttemplate <class T, class Alloc>
1663e519524SHoward Hinnant    bool operator<=(const list<T,Alloc>& x, const list<T,Alloc>& y);
1673e519524SHoward Hinnant
1683e519524SHoward Hinnanttemplate <class T, class Alloc>
16945900104SHoward Hinnant    void swap(list<T,Alloc>& x, list<T,Alloc>& y)
17045900104SHoward Hinnant         noexcept(noexcept(x.swap(y)));
1713e519524SHoward Hinnant
172f60c63c0SMarshall Clowtemplate <class T, class Allocator, class U>
1733e895085SMarek Kurdej    typename list<T, Allocator>::size_type
1743e895085SMarek Kurdej    erase(list<T, Allocator>& c, const U& value);       // C++20
175f60c63c0SMarshall Clowtemplate <class T, class Allocator, class Predicate>
1763e895085SMarek Kurdej    typename list<T, Allocator>::size_type
1773e895085SMarek Kurdej    erase_if(list<T, Allocator>& c, Predicate pred);    // C++20
178f60c63c0SMarshall Clow
1793e519524SHoward Hinnant}  // std
1803e519524SHoward Hinnant
1813e519524SHoward Hinnant*/
1823e519524SHoward Hinnant
1832e2f3158SNikolas Klauser#include <__algorithm/comp.h>
1842e2f3158SNikolas Klauser#include <__algorithm/equal.h>
1852e2f3158SNikolas Klauser#include <__algorithm/lexicographical_compare.h>
1862e2f3158SNikolas Klauser#include <__algorithm/min.h>
187385cc25aSLouis Dionne#include <__assert> // all public C++ headers provide the assertion handler
1883e519524SHoward Hinnant#include <__config>
189bfbd73f8SArthur O'Dwyer#include <__debug>
19088930229SMark de Wever#include <__format/enable_insertable.h>
1913cd4531bSNikolas Klauser#include <__iterator/distance.h>
1923cd4531bSNikolas Klauser#include <__iterator/iterator_traits.h>
1933cd4531bSNikolas Klauser#include <__iterator/move_iterator.h>
1943cd4531bSNikolas Klauser#include <__iterator/next.h>
1953cd4531bSNikolas Klauser#include <__iterator/prev.h>
1963cd4531bSNikolas Klauser#include <__iterator/reverse_iterator.h>
197*f4fb72e6SNikolas Klauser#include <__memory/swap_allocator.h>
1986adbc83eSChristopher Di Bella#include <__utility/forward.h>
19952915d78SNikolas Klauser#include <__utility/move.h>
20052915d78SNikolas Klauser#include <__utility/swap.h>
201bfbd73f8SArthur O'Dwyer#include <limits>
202bfbd73f8SArthur O'Dwyer#include <memory>
203b88ea354SEric Fiselier#include <type_traits>
204f56972e2SMarshall Clow#include <version>
2053e519524SHoward Hinnant
206de4a57cbSLouis Dionne#ifndef _LIBCPP_REMOVE_TRANSITIVE_INCLUDES
207de4a57cbSLouis Dionne#  include <algorithm>
208de4a57cbSLouis Dionne#  include <functional>
209de4a57cbSLouis Dionne#  include <iterator>
210de4a57cbSLouis Dionne#endif
211de4a57cbSLouis Dionne
212db1978b6SNikolas Klauser// standard-mandated includes
213db1978b6SNikolas Klauser
214db1978b6SNikolas Klauser// [iterator.range]
215db1978b6SNikolas Klauser#include <__iterator/access.h>
216db1978b6SNikolas Klauser#include <__iterator/data.h>
217db1978b6SNikolas Klauser#include <__iterator/empty.h>
218db1978b6SNikolas Klauser#include <__iterator/reverse_access.h>
219db1978b6SNikolas Klauser#include <__iterator/size.h>
220db1978b6SNikolas Klauser
221db1978b6SNikolas Klauser// [list.syn]
222db1978b6SNikolas Klauser#include <compare>
223db1978b6SNikolas Klauser#include <initializer_list>
224db1978b6SNikolas Klauser
225073458b1SHoward Hinnant#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
2263e519524SHoward Hinnant#  pragma GCC system_header
227073458b1SHoward Hinnant#endif
2283e519524SHoward Hinnant
229a016efb1SEric Fiselier_LIBCPP_PUSH_MACROS
230a016efb1SEric Fiselier#include <__undef_macros>
231a016efb1SEric Fiselier
232a016efb1SEric Fiselier
2333e519524SHoward Hinnant_LIBCPP_BEGIN_NAMESPACE_STD
2343e519524SHoward Hinnant
235ce53420eSHoward Hinnanttemplate <class _Tp, class _VoidPtr> struct __list_node;
236b88ea354SEric Fiseliertemplate <class _Tp, class _VoidPtr> struct __list_node_base;
237b88ea354SEric Fiselier
238b88ea354SEric Fiseliertemplate <class _Tp, class _VoidPtr>
239b88ea354SEric Fiselierstruct __list_node_pointer_traits {
240b88ea354SEric Fiselier  typedef typename __rebind_pointer<_VoidPtr, __list_node<_Tp, _VoidPtr> >::type
241b88ea354SEric Fiselier        __node_pointer;
242b88ea354SEric Fiselier  typedef typename __rebind_pointer<_VoidPtr, __list_node_base<_Tp, _VoidPtr> >::type
243b88ea354SEric Fiselier        __base_pointer;
244b88ea354SEric Fiselier
245b88ea354SEric Fiselier#if defined(_LIBCPP_ABI_LIST_REMOVE_NODE_POINTER_UB)
246b88ea354SEric Fiselier  typedef __base_pointer __link_pointer;
247b88ea354SEric Fiselier#else
248b88ea354SEric Fiselier  typedef typename conditional<
249b88ea354SEric Fiselier          is_pointer<_VoidPtr>::value,
250b88ea354SEric Fiselier          __base_pointer,
251b88ea354SEric Fiselier          __node_pointer
252b88ea354SEric Fiselier  >::type __link_pointer;
253b88ea354SEric Fiselier#endif
254b88ea354SEric Fiselier
2555243e190SEric Fiselier  typedef typename conditional<
2565243e190SEric Fiselier          is_same<__link_pointer, __node_pointer>::value,
2575243e190SEric Fiselier          __base_pointer,
2585243e190SEric Fiselier          __node_pointer
2595243e190SEric Fiselier  >::type __non_link_pointer;
2605243e190SEric Fiselier
2615243e190SEric Fiselier  static _LIBCPP_INLINE_VISIBILITY
2625243e190SEric Fiselier  __link_pointer __unsafe_link_pointer_cast(__link_pointer __p) {
2635243e190SEric Fiselier      return __p;
2645243e190SEric Fiselier  }
2655243e190SEric Fiselier
2665243e190SEric Fiselier  static _LIBCPP_INLINE_VISIBILITY
2675243e190SEric Fiselier  __link_pointer __unsafe_link_pointer_cast(__non_link_pointer __p) {
2685243e190SEric Fiselier      return static_cast<__link_pointer>(static_cast<_VoidPtr>(__p));
2695243e190SEric Fiselier  }
2705243e190SEric Fiselier
271b88ea354SEric Fiselier};
2723e519524SHoward Hinnant
2733e519524SHoward Hinnanttemplate <class _Tp, class _VoidPtr>
2743e519524SHoward Hinnantstruct __list_node_base
2753e519524SHoward Hinnant{
276b88ea354SEric Fiselier    typedef __list_node_pointer_traits<_Tp, _VoidPtr> _NodeTraits;
277b88ea354SEric Fiselier    typedef typename _NodeTraits::__node_pointer __node_pointer;
278b88ea354SEric Fiselier    typedef typename _NodeTraits::__base_pointer __base_pointer;
279b88ea354SEric Fiselier    typedef typename _NodeTraits::__link_pointer __link_pointer;
280866d4efaSHoward Hinnant
281b88ea354SEric Fiselier    __link_pointer __prev_;
282b88ea354SEric Fiselier    __link_pointer __next_;
2833e519524SHoward Hinnant
284848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
2855243e190SEric Fiselier    __list_node_base() : __prev_(_NodeTraits::__unsafe_link_pointer_cast(__self())),
2865243e190SEric Fiselier                         __next_(_NodeTraits::__unsafe_link_pointer_cast(__self())) {}
28728d65da6SMarshall Clow
28828d65da6SMarshall Clow    _LIBCPP_INLINE_VISIBILITY
2895243e190SEric Fiselier    __base_pointer __self() {
290b88ea354SEric Fiselier        return pointer_traits<__base_pointer>::pointer_to(*this);
291b88ea354SEric Fiselier    }
292b88ea354SEric Fiselier
293b88ea354SEric Fiselier    _LIBCPP_INLINE_VISIBILITY
294b88ea354SEric Fiselier    __node_pointer __as_node() {
2955243e190SEric Fiselier        return static_cast<__node_pointer>(__self());
29628d65da6SMarshall Clow    }
2973e519524SHoward Hinnant};
2983e519524SHoward Hinnant
2993e519524SHoward Hinnanttemplate <class _Tp, class _VoidPtr>
3007c2f5827SAmy Huangstruct _LIBCPP_STANDALONE_DEBUG __list_node
3013e519524SHoward Hinnant    : public __list_node_base<_Tp, _VoidPtr>
3023e519524SHoward Hinnant{
3033e519524SHoward Hinnant    _Tp __value_;
3045243e190SEric Fiselier
3055243e190SEric Fiselier    typedef __list_node_base<_Tp, _VoidPtr> __base;
3065243e190SEric Fiselier    typedef typename __base::__link_pointer __link_pointer;
3075243e190SEric Fiselier
3085243e190SEric Fiselier    _LIBCPP_INLINE_VISIBILITY
3095243e190SEric Fiselier    __link_pointer __as_link() {
3105243e190SEric Fiselier        return static_cast<__link_pointer>(__base::__self());
3115243e190SEric Fiselier    }
3123e519524SHoward Hinnant};
3133e519524SHoward Hinnant
314e2f2d1edSEric Fiseliertemplate <class _Tp, class _Alloc = allocator<_Tp> > class _LIBCPP_TEMPLATE_VIS list;
315ce53420eSHoward Hinnanttemplate <class _Tp, class _Alloc> class __list_imp;
316e2f2d1edSEric Fiseliertemplate <class _Tp, class _VoidPtr> class _LIBCPP_TEMPLATE_VIS __list_const_iterator;
3173e519524SHoward Hinnant
3183e519524SHoward Hinnanttemplate <class _Tp, class _VoidPtr>
319e2f2d1edSEric Fiselierclass _LIBCPP_TEMPLATE_VIS __list_iterator
3203e519524SHoward Hinnant{
321b88ea354SEric Fiselier    typedef __list_node_pointer_traits<_Tp, _VoidPtr> _NodeTraits;
322b88ea354SEric Fiselier    typedef typename _NodeTraits::__link_pointer __link_pointer;
3233e519524SHoward Hinnant
324b88ea354SEric Fiselier    __link_pointer __ptr_;
3253e519524SHoward Hinnant
326920b56caSHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
327b88ea354SEric Fiselier    explicit __list_iterator(__link_pointer __p, const void* __c) _NOEXCEPT
328920b56caSHoward Hinnant        : __ptr_(__p)
329920b56caSHoward Hinnant    {
3304eab04f8SLouis Dionne        (void)__c;
331f3966eafSLouis Dionne#ifdef _LIBCPP_ENABLE_DEBUG_MODE
332920b56caSHoward Hinnant        __get_db()->__insert_ic(this, __c);
333920b56caSHoward Hinnant#endif
3344eab04f8SLouis Dionne    }
3353e519524SHoward Hinnant
3363e519524SHoward Hinnant    template<class, class> friend class list;
3373e519524SHoward Hinnant    template<class, class> friend class __list_imp;
3383e519524SHoward Hinnant    template<class, class> friend class __list_const_iterator;
3393e519524SHoward Hinnantpublic:
3403e519524SHoward Hinnant    typedef bidirectional_iterator_tag       iterator_category;
3413e519524SHoward Hinnant    typedef _Tp                              value_type;
3423e519524SHoward Hinnant    typedef value_type&                      reference;
3431c813407SEric Fiselier    typedef typename __rebind_pointer<_VoidPtr, value_type>::type pointer;
3443e519524SHoward Hinnant    typedef typename pointer_traits<pointer>::difference_type difference_type;
3453e519524SHoward Hinnant
346848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
3470c37cfd8SMarshall Clow    __list_iterator() _NOEXCEPT : __ptr_(nullptr)
348920b56caSHoward Hinnant    {
349caf5548cSNikolas Klauser        _VSTD::__debug_db_insert_i(this);
350920b56caSHoward Hinnant    }
351920b56caSHoward Hinnant
352f3966eafSLouis Dionne#ifdef _LIBCPP_ENABLE_DEBUG_MODE
353920b56caSHoward Hinnant
35427745457SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
355920b56caSHoward Hinnant    __list_iterator(const __list_iterator& __p)
356920b56caSHoward Hinnant        : __ptr_(__p.__ptr_)
357920b56caSHoward Hinnant    {
358968e2739SMark de Wever        __get_db()->__iterator_copy(this, _VSTD::addressof(__p));
359920b56caSHoward Hinnant    }
360920b56caSHoward Hinnant
361920b56caSHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
362920b56caSHoward Hinnant    ~__list_iterator()
363920b56caSHoward Hinnant    {
364920b56caSHoward Hinnant        __get_db()->__erase_i(this);
365920b56caSHoward Hinnant    }
366920b56caSHoward Hinnant
367920b56caSHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
368920b56caSHoward Hinnant    __list_iterator& operator=(const __list_iterator& __p)
369920b56caSHoward Hinnant    {
370968e2739SMark de Wever        if (this != _VSTD::addressof(__p))
371920b56caSHoward Hinnant        {
372968e2739SMark de Wever            __get_db()->__iterator_copy(this, _VSTD::addressof(__p));
373920b56caSHoward Hinnant            __ptr_ = __p.__ptr_;
374920b56caSHoward Hinnant        }
375920b56caSHoward Hinnant        return *this;
376920b56caSHoward Hinnant    }
377920b56caSHoward Hinnant
378f3966eafSLouis Dionne#endif // _LIBCPP_ENABLE_DEBUG_MODE
379920b56caSHoward Hinnant
380920b56caSHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
381920b56caSHoward Hinnant    reference operator*() const
382920b56caSHoward Hinnant    {
3832154dbaaSNikolas Klauser        _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
384920b56caSHoward Hinnant                             "Attempted to dereference a non-dereferenceable list::iterator");
385b88ea354SEric Fiselier        return __ptr_->__as_node()->__value_;
386920b56caSHoward Hinnant    }
387848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
388866d4efaSHoward Hinnant    pointer operator->() const
389866d4efaSHoward Hinnant    {
3902154dbaaSNikolas Klauser        _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
391866d4efaSHoward Hinnant                             "Attempted to dereference a non-dereferenceable list::iterator");
392b88ea354SEric Fiselier        return pointer_traits<pointer>::pointer_to(__ptr_->__as_node()->__value_);
393866d4efaSHoward Hinnant    }
3943e519524SHoward Hinnant
395848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
396920b56caSHoward Hinnant    __list_iterator& operator++()
397920b56caSHoward Hinnant    {
3982154dbaaSNikolas Klauser        _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
39996100f15SKristina Bessonova                             "Attempted to increment a non-incrementable list::iterator");
400920b56caSHoward Hinnant        __ptr_ = __ptr_->__next_;
401920b56caSHoward Hinnant        return *this;
402920b56caSHoward Hinnant    }
403848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
4043e519524SHoward Hinnant    __list_iterator operator++(int) {__list_iterator __t(*this); ++(*this); return __t;}
4053e519524SHoward Hinnant
406848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
407920b56caSHoward Hinnant    __list_iterator& operator--()
408920b56caSHoward Hinnant    {
4092154dbaaSNikolas Klauser        _LIBCPP_DEBUG_ASSERT(__get_const_db()->__decrementable(this),
41096100f15SKristina Bessonova                             "Attempted to decrement a non-decrementable list::iterator");
411920b56caSHoward Hinnant        __ptr_ = __ptr_->__prev_;
412920b56caSHoward Hinnant        return *this;
413920b56caSHoward Hinnant    }
414848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
4153e519524SHoward Hinnant    __list_iterator operator--(int) {__list_iterator __t(*this); --(*this); return __t;}
4163e519524SHoward Hinnant
417848a5374SHoward Hinnant    friend _LIBCPP_INLINE_VISIBILITY
418848a5374SHoward Hinnant    bool operator==(const __list_iterator& __x, const __list_iterator& __y)
419920b56caSHoward Hinnant    {
420920b56caSHoward Hinnant        return __x.__ptr_ == __y.__ptr_;
421920b56caSHoward Hinnant    }
422848a5374SHoward Hinnant    friend _LIBCPP_INLINE_VISIBILITY
423848a5374SHoward Hinnant     bool operator!=(const __list_iterator& __x, const __list_iterator& __y)
4243e519524SHoward Hinnant        {return !(__x == __y);}
4253e519524SHoward Hinnant};
4263e519524SHoward Hinnant
4273e519524SHoward Hinnanttemplate <class _Tp, class _VoidPtr>
428e2f2d1edSEric Fiselierclass _LIBCPP_TEMPLATE_VIS __list_const_iterator
4293e519524SHoward Hinnant{
430b88ea354SEric Fiselier    typedef __list_node_pointer_traits<_Tp, _VoidPtr> _NodeTraits;
431b88ea354SEric Fiselier    typedef typename _NodeTraits::__link_pointer __link_pointer;
4323e519524SHoward Hinnant
433b88ea354SEric Fiselier    __link_pointer __ptr_;
4343e519524SHoward Hinnant
435920b56caSHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
436b88ea354SEric Fiselier    explicit __list_const_iterator(__link_pointer __p, const void* __c) _NOEXCEPT
437920b56caSHoward Hinnant        : __ptr_(__p)
438920b56caSHoward Hinnant    {
4394eab04f8SLouis Dionne        (void)__c;
440f3966eafSLouis Dionne#ifdef _LIBCPP_ENABLE_DEBUG_MODE
441920b56caSHoward Hinnant        __get_db()->__insert_ic(this, __c);
442920b56caSHoward Hinnant#endif
4434eab04f8SLouis Dionne    }
4443e519524SHoward Hinnant
4453e519524SHoward Hinnant    template<class, class> friend class list;
4463e519524SHoward Hinnant    template<class, class> friend class __list_imp;
4473e519524SHoward Hinnantpublic:
4483e519524SHoward Hinnant    typedef bidirectional_iterator_tag       iterator_category;
4493e519524SHoward Hinnant    typedef _Tp                              value_type;
4503e519524SHoward Hinnant    typedef const value_type&                reference;
4511c813407SEric Fiselier    typedef typename __rebind_pointer<_VoidPtr, const value_type>::type pointer;
4523e519524SHoward Hinnant    typedef typename pointer_traits<pointer>::difference_type difference_type;
4533e519524SHoward Hinnant
454848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
4550c37cfd8SMarshall Clow    __list_const_iterator() _NOEXCEPT : __ptr_(nullptr)
456920b56caSHoward Hinnant    {
457caf5548cSNikolas Klauser        _VSTD::__debug_db_insert_i(this);
458920b56caSHoward Hinnant    }
45927745457SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
460f7509231SHoward Hinnant    __list_const_iterator(const __list_iterator<_Tp, _VoidPtr>& __p) _NOEXCEPT
461920b56caSHoward Hinnant        : __ptr_(__p.__ptr_)
462920b56caSHoward Hinnant    {
463f3966eafSLouis Dionne#ifdef _LIBCPP_ENABLE_DEBUG_MODE
464968e2739SMark de Wever        __get_db()->__iterator_copy(this, _VSTD::addressof(__p));
465920b56caSHoward Hinnant#endif
466920b56caSHoward Hinnant    }
467920b56caSHoward Hinnant
468f3966eafSLouis Dionne#ifdef _LIBCPP_ENABLE_DEBUG_MODE
4693e519524SHoward Hinnant
470848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
471920b56caSHoward Hinnant    __list_const_iterator(const __list_const_iterator& __p)
472920b56caSHoward Hinnant        : __ptr_(__p.__ptr_)
473920b56caSHoward Hinnant    {
474968e2739SMark de Wever        __get_db()->__iterator_copy(this, _VSTD::addressof(__p));
475920b56caSHoward Hinnant    }
476920b56caSHoward Hinnant
477920b56caSHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
478920b56caSHoward Hinnant    ~__list_const_iterator()
479920b56caSHoward Hinnant    {
480920b56caSHoward Hinnant        __get_db()->__erase_i(this);
481920b56caSHoward Hinnant    }
482920b56caSHoward Hinnant
483920b56caSHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
484920b56caSHoward Hinnant    __list_const_iterator& operator=(const __list_const_iterator& __p)
485920b56caSHoward Hinnant    {
4864732dd30SMark de Wever        if (this != _VSTD::addressof(__p))
487920b56caSHoward Hinnant        {
4884732dd30SMark de Wever            __get_db()->__iterator_copy(this, _VSTD::addressof(__p));
489920b56caSHoward Hinnant            __ptr_ = __p.__ptr_;
490920b56caSHoward Hinnant        }
491920b56caSHoward Hinnant        return *this;
492920b56caSHoward Hinnant    }
493920b56caSHoward Hinnant
494f3966eafSLouis Dionne#endif // _LIBCPP_ENABLE_DEBUG_MODE
495920b56caSHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
496920b56caSHoward Hinnant    reference operator*() const
497920b56caSHoward Hinnant    {
4982154dbaaSNikolas Klauser        _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
499920b56caSHoward Hinnant                             "Attempted to dereference a non-dereferenceable list::const_iterator");
500b88ea354SEric Fiselier        return __ptr_->__as_node()->__value_;
501920b56caSHoward Hinnant    }
502848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
503866d4efaSHoward Hinnant    pointer operator->() const
504866d4efaSHoward Hinnant    {
5052154dbaaSNikolas Klauser        _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
506ed9010a2SMarshall Clow                             "Attempted to dereference a non-dereferenceable list::const_iterator");
507b88ea354SEric Fiselier        return pointer_traits<pointer>::pointer_to(__ptr_->__as_node()->__value_);
508866d4efaSHoward Hinnant    }
5093e519524SHoward Hinnant
510848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
511920b56caSHoward Hinnant    __list_const_iterator& operator++()
512920b56caSHoward Hinnant    {
5132154dbaaSNikolas Klauser        _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
51496100f15SKristina Bessonova                             "Attempted to increment a non-incrementable list::const_iterator");
515920b56caSHoward Hinnant        __ptr_ = __ptr_->__next_;
516920b56caSHoward Hinnant        return *this;
517920b56caSHoward Hinnant    }
518848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
5193e519524SHoward Hinnant    __list_const_iterator operator++(int) {__list_const_iterator __t(*this); ++(*this); return __t;}
5203e519524SHoward Hinnant
521848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
522920b56caSHoward Hinnant    __list_const_iterator& operator--()
523920b56caSHoward Hinnant    {
5242154dbaaSNikolas Klauser        _LIBCPP_DEBUG_ASSERT(__get_const_db()->__decrementable(this),
52596100f15SKristina Bessonova                             "Attempted to decrement a non-decrementable list::const_iterator");
526920b56caSHoward Hinnant        __ptr_ = __ptr_->__prev_;
527920b56caSHoward Hinnant        return *this;
528920b56caSHoward Hinnant    }
529848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
5303e519524SHoward Hinnant    __list_const_iterator operator--(int) {__list_const_iterator __t(*this); --(*this); return __t;}
5313e519524SHoward Hinnant
532848a5374SHoward Hinnant    friend _LIBCPP_INLINE_VISIBILITY
533848a5374SHoward Hinnant    bool operator==(const __list_const_iterator& __x, const __list_const_iterator& __y)
534920b56caSHoward Hinnant    {
535920b56caSHoward Hinnant        return __x.__ptr_ == __y.__ptr_;
536920b56caSHoward Hinnant    }
537848a5374SHoward Hinnant    friend _LIBCPP_INLINE_VISIBILITY
538848a5374SHoward Hinnant    bool operator!=(const __list_const_iterator& __x, const __list_const_iterator& __y)
5393e519524SHoward Hinnant        {return !(__x == __y);}
5403e519524SHoward Hinnant};
5413e519524SHoward Hinnant
5423e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
5433e519524SHoward Hinnantclass __list_imp
5443e519524SHoward Hinnant{
5453e519524SHoward Hinnant    __list_imp(const __list_imp&);
5463e519524SHoward Hinnant    __list_imp& operator=(const __list_imp&);
5474a227e58SMarshall Clowpublic:
5483e519524SHoward Hinnant    typedef _Alloc                                                  allocator_type;
5493e519524SHoward Hinnant    typedef allocator_traits<allocator_type>                        __alloc_traits;
5503e519524SHoward Hinnant    typedef typename __alloc_traits::size_type                      size_type;
5514a227e58SMarshall Clowprotected:
5524a227e58SMarshall Clow    typedef _Tp                                                     value_type;
5533e519524SHoward Hinnant    typedef typename __alloc_traits::void_pointer                   __void_pointer;
5543e519524SHoward Hinnant    typedef __list_iterator<value_type, __void_pointer>             iterator;
5553e519524SHoward Hinnant    typedef __list_const_iterator<value_type, __void_pointer>       const_iterator;
5563e519524SHoward Hinnant    typedef __list_node_base<value_type, __void_pointer>            __node_base;
5573e519524SHoward Hinnant    typedef __list_node<value_type, __void_pointer>                 __node;
5581f508014SMarshall Clow    typedef typename __rebind_alloc_helper<__alloc_traits, __node>::type __node_allocator;
5593e519524SHoward Hinnant    typedef allocator_traits<__node_allocator>                       __node_alloc_traits;
5603e519524SHoward Hinnant    typedef typename __node_alloc_traits::pointer                    __node_pointer;
561866d4efaSHoward Hinnant    typedef typename __node_alloc_traits::pointer                    __node_const_pointer;
5625243e190SEric Fiselier    typedef __list_node_pointer_traits<value_type, __void_pointer> __node_pointer_traits;
5635243e190SEric Fiselier    typedef typename __node_pointer_traits::__link_pointer __link_pointer;
564b88ea354SEric Fiselier    typedef __link_pointer __link_const_pointer;
5653e519524SHoward Hinnant    typedef typename __alloc_traits::pointer                         pointer;
5663e519524SHoward Hinnant    typedef typename __alloc_traits::const_pointer                   const_pointer;
5673e519524SHoward Hinnant    typedef typename __alloc_traits::difference_type                 difference_type;
5683e519524SHoward Hinnant
5691f508014SMarshall Clow    typedef typename __rebind_alloc_helper<__alloc_traits, __node_base>::type __node_base_allocator;
570866d4efaSHoward Hinnant    typedef typename allocator_traits<__node_base_allocator>::pointer __node_base_pointer;
5718cef7fd7SEric Fiselier    static_assert((!is_same<allocator_type, __node_allocator>::value),
5728cef7fd7SEric Fiselier                  "internal allocator type must differ from user-specified "
5738cef7fd7SEric Fiselier                  "type; otherwise overload resolution breaks");
574866d4efaSHoward Hinnant
5753e519524SHoward Hinnant    __node_base __end_;
5763e519524SHoward Hinnant    __compressed_pair<size_type, __node_allocator> __size_alloc_;
5773e519524SHoward Hinnant
578848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
5795243e190SEric Fiselier    __link_pointer __end_as_link() const _NOEXCEPT {
5805243e190SEric Fiselier        return __node_pointer_traits::__unsafe_link_pointer_cast(
5815243e190SEric Fiselier                const_cast<__node_base&>(__end_).__self());
5825243e190SEric Fiselier    }
5835243e190SEric Fiselier
5845243e190SEric Fiselier    _LIBCPP_INLINE_VISIBILITY
58545900104SHoward Hinnant          size_type& __sz() _NOEXCEPT {return __size_alloc_.first();}
586848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
58745900104SHoward Hinnant    const size_type& __sz() const _NOEXCEPT
58845900104SHoward Hinnant        {return __size_alloc_.first();}
589848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
59045900104SHoward Hinnant          __node_allocator& __node_alloc() _NOEXCEPT
59145900104SHoward Hinnant          {return __size_alloc_.second();}
592848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
59345900104SHoward Hinnant    const __node_allocator& __node_alloc() const _NOEXCEPT
59445900104SHoward Hinnant        {return __size_alloc_.second();}
5953e519524SHoward Hinnant
596cd31b434SEvgeniy Stepanov    _LIBCPP_INLINE_VISIBILITY
59755b31b4eSEric Fiselier    size_type __node_alloc_max_size() const _NOEXCEPT {
59855b31b4eSEric Fiselier        return __node_alloc_traits::max_size(__node_alloc());
59955b31b4eSEric Fiselier    }
60055b31b4eSEric Fiselier    _LIBCPP_INLINE_VISIBILITY
601b88ea354SEric Fiselier    static void __unlink_nodes(__link_pointer __f, __link_pointer __l) _NOEXCEPT;
6023e519524SHoward Hinnant
603cd31b434SEvgeniy Stepanov    _LIBCPP_INLINE_VISIBILITY
60445900104SHoward Hinnant    __list_imp()
60545900104SHoward Hinnant        _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value);
606cd31b434SEvgeniy Stepanov    _LIBCPP_INLINE_VISIBILITY
6073e519524SHoward Hinnant    __list_imp(const allocator_type& __a);
6088cef7fd7SEric Fiselier    _LIBCPP_INLINE_VISIBILITY
6098cef7fd7SEric Fiselier    __list_imp(const __node_allocator& __a);
6108cef7fd7SEric Fiselier#ifndef _LIBCPP_CXX03_LANG
6118cef7fd7SEric Fiselier    __list_imp(__node_allocator&& __a) _NOEXCEPT;
6128cef7fd7SEric Fiselier#endif
6133e519524SHoward Hinnant    ~__list_imp();
61445900104SHoward Hinnant    void clear() _NOEXCEPT;
615848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
61645900104SHoward Hinnant    bool empty() const _NOEXCEPT {return __sz() == 0;}
6173e519524SHoward Hinnant
618848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
61945900104SHoward Hinnant    iterator begin() _NOEXCEPT
620920b56caSHoward Hinnant    {
621920b56caSHoward Hinnant        return iterator(__end_.__next_, this);
622920b56caSHoward Hinnant    }
623848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
62445900104SHoward Hinnant    const_iterator begin() const  _NOEXCEPT
625920b56caSHoward Hinnant    {
626920b56caSHoward Hinnant        return const_iterator(__end_.__next_, this);
627920b56caSHoward Hinnant    }
628848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
62945900104SHoward Hinnant    iterator end() _NOEXCEPT
630920b56caSHoward Hinnant    {
6315243e190SEric Fiselier        return iterator(__end_as_link(), this);
632920b56caSHoward Hinnant    }
633848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
63445900104SHoward Hinnant    const_iterator end() const _NOEXCEPT
635920b56caSHoward Hinnant    {
6365243e190SEric Fiselier        return const_iterator(__end_as_link(), this);
637920b56caSHoward Hinnant    }
6383e519524SHoward Hinnant
63945900104SHoward Hinnant    void swap(__list_imp& __c)
640e3fbe143SMarshall Clow#if _LIBCPP_STD_VER >= 14
64161b302f9SEric Fiselier        _NOEXCEPT;
642e3fbe143SMarshall Clow#else
64361b302f9SEric Fiselier        _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
644e3fbe143SMarshall Clow                    __is_nothrow_swappable<allocator_type>::value);
645e3fbe143SMarshall Clow#endif
6463e519524SHoward Hinnant
647848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
6483e519524SHoward Hinnant    void __copy_assign_alloc(const __list_imp& __c)
6493e519524SHoward Hinnant        {__copy_assign_alloc(__c, integral_constant<bool,
6503e519524SHoward Hinnant                      __node_alloc_traits::propagate_on_container_copy_assignment::value>());}
6513e519524SHoward Hinnant
652848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
6533e519524SHoward Hinnant    void __move_assign_alloc(__list_imp& __c)
65445900104SHoward Hinnant        _NOEXCEPT_(
65545900104SHoward Hinnant            !__node_alloc_traits::propagate_on_container_move_assignment::value ||
65645900104SHoward Hinnant            is_nothrow_move_assignable<__node_allocator>::value)
6573e519524SHoward Hinnant        {__move_assign_alloc(__c, integral_constant<bool,
6583e519524SHoward Hinnant                      __node_alloc_traits::propagate_on_container_move_assignment::value>());}
6593e519524SHoward Hinnant
6603e519524SHoward Hinnantprivate:
661848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
6623e519524SHoward Hinnant    void __copy_assign_alloc(const __list_imp& __c, true_type)
6633e519524SHoward Hinnant        {
6643e519524SHoward Hinnant            if (__node_alloc() != __c.__node_alloc())
6653e519524SHoward Hinnant                clear();
6663e519524SHoward Hinnant            __node_alloc() = __c.__node_alloc();
6673e519524SHoward Hinnant        }
6683e519524SHoward Hinnant
669848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
670fd838227SEric Fiselier    void __copy_assign_alloc(const __list_imp&, false_type)
6713e519524SHoward Hinnant        {}
6723e519524SHoward Hinnant
673848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
6748668139fSHoward Hinnant    void __move_assign_alloc(__list_imp& __c, true_type)
67545900104SHoward Hinnant        _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
6763e519524SHoward Hinnant        {
677ce48a113SHoward Hinnant            __node_alloc() = _VSTD::move(__c.__node_alloc());
6783e519524SHoward Hinnant        }
6793e519524SHoward Hinnant
680848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
681fd838227SEric Fiselier    void __move_assign_alloc(__list_imp&, false_type)
68245900104SHoward Hinnant        _NOEXCEPT
6833e519524SHoward Hinnant        {}
6843e519524SHoward Hinnant};
6853e519524SHoward Hinnant
6863e519524SHoward Hinnant// Unlink nodes [__f, __l]
6873e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
688cd31b434SEvgeniy Stepanovinline
6893e519524SHoward Hinnantvoid
690b88ea354SEric Fiselier__list_imp<_Tp, _Alloc>::__unlink_nodes(__link_pointer __f, __link_pointer __l)
69145900104SHoward Hinnant    _NOEXCEPT
6923e519524SHoward Hinnant{
693866d4efaSHoward Hinnant    __f->__prev_->__next_ = __l->__next_;
694866d4efaSHoward Hinnant    __l->__next_->__prev_ = __f->__prev_;
6953e519524SHoward Hinnant}
6963e519524SHoward Hinnant
6973e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
698cd31b434SEvgeniy Stepanovinline
6993e519524SHoward Hinnant__list_imp<_Tp, _Alloc>::__list_imp()
70045900104SHoward Hinnant        _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value)
701549545b6SEric Fiselier    : __size_alloc_(0, __default_init_tag())
7023e519524SHoward Hinnant{
7033e519524SHoward Hinnant}
7043e519524SHoward Hinnant
7053e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
706cd31b434SEvgeniy Stepanovinline
7073e519524SHoward Hinnant__list_imp<_Tp, _Alloc>::__list_imp(const allocator_type& __a)
7083e519524SHoward Hinnant    : __size_alloc_(0, __node_allocator(__a))
7093e519524SHoward Hinnant{
7103e519524SHoward Hinnant}
7113e519524SHoward Hinnant
7123e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
7138cef7fd7SEric Fiselierinline __list_imp<_Tp, _Alloc>::__list_imp(const __node_allocator& __a)
7148cef7fd7SEric Fiselier    : __size_alloc_(0, __a) {}
7158cef7fd7SEric Fiselier
7168cef7fd7SEric Fiselier#ifndef _LIBCPP_CXX03_LANG
7178cef7fd7SEric Fiseliertemplate <class _Tp, class _Alloc>
7188cef7fd7SEric Fiselierinline __list_imp<_Tp, _Alloc>::__list_imp(__node_allocator&& __a) _NOEXCEPT
719d586f92cSArthur O'Dwyer    : __size_alloc_(0, _VSTD::move(__a)) {}
7208cef7fd7SEric Fiselier#endif
7218cef7fd7SEric Fiselier
7228cef7fd7SEric Fiseliertemplate <class _Tp, class _Alloc>
7238cef7fd7SEric Fiselier__list_imp<_Tp, _Alloc>::~__list_imp() {
7243e519524SHoward Hinnant  clear();
72508f68dfeSNikolas Klauser  std::__debug_db_erase_c(this);
7263e519524SHoward Hinnant}
7273e519524SHoward Hinnant
7283e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
7293e519524SHoward Hinnantvoid
73045900104SHoward Hinnant__list_imp<_Tp, _Alloc>::clear() _NOEXCEPT
7313e519524SHoward Hinnant{
7323e519524SHoward Hinnant    if (!empty())
7333e519524SHoward Hinnant    {
7343e519524SHoward Hinnant        __node_allocator& __na = __node_alloc();
735b88ea354SEric Fiselier        __link_pointer __f = __end_.__next_;
7365243e190SEric Fiselier        __link_pointer __l = __end_as_link();
737866d4efaSHoward Hinnant        __unlink_nodes(__f, __l->__prev_);
7383e519524SHoward Hinnant        __sz() = 0;
7393e519524SHoward Hinnant        while (__f != __l)
7403e519524SHoward Hinnant        {
741b88ea354SEric Fiselier            __node_pointer __np = __f->__as_node();
742920b56caSHoward Hinnant            __f = __f->__next_;
743b88ea354SEric Fiselier            __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
744b88ea354SEric Fiselier            __node_alloc_traits::deallocate(__na, __np, 1);
7453e519524SHoward Hinnant        }
746c095440cSNikolas Klauser        std::__debug_db_invalidate_all(this);
7473e519524SHoward Hinnant    }
7483e519524SHoward Hinnant}
7493e519524SHoward Hinnant
7503e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
7513e519524SHoward Hinnantvoid
7523e519524SHoward Hinnant__list_imp<_Tp, _Alloc>::swap(__list_imp& __c)
753e3fbe143SMarshall Clow#if _LIBCPP_STD_VER >= 14
75461b302f9SEric Fiselier        _NOEXCEPT
755e3fbe143SMarshall Clow#else
75661b302f9SEric Fiselier        _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
757e3fbe143SMarshall Clow                    __is_nothrow_swappable<allocator_type>::value)
758e3fbe143SMarshall Clow#endif
7593e519524SHoward Hinnant{
760920b56caSHoward Hinnant    _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
761920b56caSHoward Hinnant                   this->__node_alloc() == __c.__node_alloc(),
762920b56caSHoward Hinnant                   "list::swap: Either propagate_on_container_swap must be true"
763920b56caSHoward Hinnant                   " or the allocators must compare equal");
764ce48a113SHoward Hinnant    using _VSTD::swap;
7656e965df6SArthur O'Dwyer    _VSTD::__swap_allocator(__node_alloc(), __c.__node_alloc());
7663e519524SHoward Hinnant    swap(__sz(), __c.__sz());
7673e519524SHoward Hinnant    swap(__end_, __c.__end_);
7683e519524SHoward Hinnant    if (__sz() == 0)
7695243e190SEric Fiselier        __end_.__next_ = __end_.__prev_ = __end_as_link();
7703e519524SHoward Hinnant    else
7715243e190SEric Fiselier        __end_.__prev_->__next_ = __end_.__next_->__prev_ = __end_as_link();
7723e519524SHoward Hinnant    if (__c.__sz() == 0)
7735243e190SEric Fiselier        __c.__end_.__next_ = __c.__end_.__prev_ = __c.__end_as_link();
7743e519524SHoward Hinnant    else
7755243e190SEric Fiselier        __c.__end_.__prev_->__next_ = __c.__end_.__next_->__prev_ = __c.__end_as_link();
77628d65da6SMarshall Clow
777f3966eafSLouis Dionne#ifdef _LIBCPP_ENABLE_DEBUG_MODE
778920b56caSHoward Hinnant    __libcpp_db* __db = __get_db();
779920b56caSHoward Hinnant    __c_node* __cn1 = __db->__find_c_and_lock(this);
7804732dd30SMark de Wever    __c_node* __cn2 = __db->__find_c(_VSTD::addressof(__c));
781d586f92cSArthur O'Dwyer    _VSTD::swap(__cn1->beg_, __cn2->beg_);
782d586f92cSArthur O'Dwyer    _VSTD::swap(__cn1->end_, __cn2->end_);
783d586f92cSArthur O'Dwyer    _VSTD::swap(__cn1->cap_, __cn2->cap_);
784920b56caSHoward Hinnant    for (__i_node** __p = __cn1->end_; __p != __cn1->beg_;)
785920b56caSHoward Hinnant    {
786920b56caSHoward Hinnant        --__p;
787920b56caSHoward Hinnant        const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
7885243e190SEric Fiselier        if (__i->__ptr_ == __c.__end_as_link())
789920b56caSHoward Hinnant        {
790920b56caSHoward Hinnant            __cn2->__add(*__p);
791920b56caSHoward Hinnant            if (--__cn1->end_ != __p)
7923696227cSArthur O'Dwyer                _VSTD::memmove(__p, __p+1, (__cn1->end_ - __p)*sizeof(__i_node*));
793920b56caSHoward Hinnant        }
794920b56caSHoward Hinnant        else
795920b56caSHoward Hinnant            (*__p)->__c_ = __cn1;
796920b56caSHoward Hinnant    }
797920b56caSHoward Hinnant    for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
798920b56caSHoward Hinnant    {
799920b56caSHoward Hinnant        --__p;
800920b56caSHoward Hinnant        const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
8015243e190SEric Fiselier        if (__i->__ptr_ == __end_as_link())
802920b56caSHoward Hinnant        {
803920b56caSHoward Hinnant            __cn1->__add(*__p);
804920b56caSHoward Hinnant            if (--__cn2->end_ != __p)
8053696227cSArthur O'Dwyer                _VSTD::memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
806920b56caSHoward Hinnant        }
807920b56caSHoward Hinnant        else
808920b56caSHoward Hinnant            (*__p)->__c_ = __cn2;
809920b56caSHoward Hinnant    }
810920b56caSHoward Hinnant    __db->unlock();
811920b56caSHoward Hinnant#endif
8123e519524SHoward Hinnant}
8133e519524SHoward Hinnant
814b5d34aa4SMarshall Clowtemplate <class _Tp, class _Alloc /*= allocator<_Tp>*/>
815e2f2d1edSEric Fiselierclass _LIBCPP_TEMPLATE_VIS list
8163e519524SHoward Hinnant    : private __list_imp<_Tp, _Alloc>
8173e519524SHoward Hinnant{
8183e519524SHoward Hinnant    typedef __list_imp<_Tp, _Alloc> base;
8193e519524SHoward Hinnant    typedef typename base::__node              __node;
8203e519524SHoward Hinnant    typedef typename base::__node_allocator    __node_allocator;
8213e519524SHoward Hinnant    typedef typename base::__node_pointer      __node_pointer;
8223e519524SHoward Hinnant    typedef typename base::__node_alloc_traits __node_alloc_traits;
823866d4efaSHoward Hinnant    typedef typename base::__node_base         __node_base;
824866d4efaSHoward Hinnant    typedef typename base::__node_base_pointer __node_base_pointer;
825b88ea354SEric Fiselier    typedef typename base::__link_pointer __link_pointer;
8263e519524SHoward Hinnant
8273e519524SHoward Hinnantpublic:
8283e519524SHoward Hinnant    typedef _Tp                                            value_type;
8293e519524SHoward Hinnant    typedef _Alloc                                         allocator_type;
8303e519524SHoward Hinnant    static_assert((is_same<value_type, typename allocator_type::value_type>::value),
8313e519524SHoward Hinnant                  "Invalid allocator::value_type");
8323e519524SHoward Hinnant    typedef value_type&                                    reference;
8333e519524SHoward Hinnant    typedef const value_type&                              const_reference;
8343e519524SHoward Hinnant    typedef typename base::pointer                         pointer;
8353e519524SHoward Hinnant    typedef typename base::const_pointer                   const_pointer;
8367da4ee6fSKonstantin Varlamov    typedef typename base::size_type                       size_type;
8373e519524SHoward Hinnant    typedef typename base::difference_type                 difference_type;
8383e519524SHoward Hinnant    typedef typename base::iterator                        iterator;
8393e519524SHoward Hinnant    typedef typename base::const_iterator                  const_iterator;
840ce48a113SHoward Hinnant    typedef _VSTD::reverse_iterator<iterator>              reverse_iterator;
841ce48a113SHoward Hinnant    typedef _VSTD::reverse_iterator<const_iterator>        const_reverse_iterator;
8421ab3fe8aSMarshall Clow#if _LIBCPP_STD_VER > 17
8431ab3fe8aSMarshall Clow    typedef size_type                                      __remove_return_type;
8441ab3fe8aSMarshall Clow#else
8451ab3fe8aSMarshall Clow    typedef void                                           __remove_return_type;
8461ab3fe8aSMarshall Clow#endif
8473e519524SHoward Hinnant
848848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
84945900104SHoward Hinnant    list()
85045900104SHoward Hinnant        _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value)
851920b56caSHoward Hinnant    {
852e3cf7050SNikolas Klauser        _VSTD::__debug_db_insert_c(this);
853920b56caSHoward Hinnant    }
854848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
855fb829766SMarshall Clow    explicit list(const allocator_type& __a) : base(__a)
856920b56caSHoward Hinnant    {
857e3cf7050SNikolas Klauser        _VSTD::__debug_db_insert_c(this);
858920b56caSHoward Hinnant    }
859fb829766SMarshall Clow    explicit list(size_type __n);
860fb829766SMarshall Clow#if _LIBCPP_STD_VER > 11
861fb829766SMarshall Clow    explicit list(size_type __n, const allocator_type& __a);
862fb829766SMarshall Clow#endif
8633e519524SHoward Hinnant    list(size_type __n, const value_type& __x);
8647da4ee6fSKonstantin Varlamov    template <class = __enable_if_t<__is_allocator<_Alloc>::value> >
8657da4ee6fSKonstantin Varlamov    list(size_type __n, const value_type& __x, const allocator_type& __a) : base(__a)
8667da4ee6fSKonstantin Varlamov    {
867e3cf7050SNikolas Klauser        _VSTD::__debug_db_insert_c(this);
8687da4ee6fSKonstantin Varlamov        for (; __n > 0; --__n)
8697da4ee6fSKonstantin Varlamov            push_back(__x);
8707da4ee6fSKonstantin Varlamov    }
8717da4ee6fSKonstantin Varlamov
8723e519524SHoward Hinnant    template <class _InpIter>
8733e519524SHoward Hinnant        list(_InpIter __f, _InpIter __l,
8744887d047SNikolas Klauser             __enable_if_t<__is_cpp17_input_iterator<_InpIter>::value>* = 0);
8753e519524SHoward Hinnant    template <class _InpIter>
8763e519524SHoward Hinnant        list(_InpIter __f, _InpIter __l, const allocator_type& __a,
8774887d047SNikolas Klauser             __enable_if_t<__is_cpp17_input_iterator<_InpIter>::value>* = 0);
8783e519524SHoward Hinnant
8793e519524SHoward Hinnant    list(const list& __c);
8803c6bd176SNikolas Klauser    list(const list& __c, const __type_identity_t<allocator_type>& __a);
881cd31b434SEvgeniy Stepanov    _LIBCPP_INLINE_VISIBILITY
8823e519524SHoward Hinnant    list& operator=(const list& __c);
883cf9ed00dSEric Fiselier#ifndef _LIBCPP_CXX03_LANG
8843e519524SHoward Hinnant    list(initializer_list<value_type> __il);
8853e519524SHoward Hinnant    list(initializer_list<value_type> __il, const allocator_type& __a);
886cf9ed00dSEric Fiselier
887cd31b434SEvgeniy Stepanov    _LIBCPP_INLINE_VISIBILITY
88845900104SHoward Hinnant    list(list&& __c)
88945900104SHoward Hinnant        _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value);
890cd31b434SEvgeniy Stepanov    _LIBCPP_INLINE_VISIBILITY
8913c6bd176SNikolas Klauser    list(list&& __c, const __type_identity_t<allocator_type>& __a);
892cd31b434SEvgeniy Stepanov    _LIBCPP_INLINE_VISIBILITY
89345900104SHoward Hinnant    list& operator=(list&& __c)
89445900104SHoward Hinnant        _NOEXCEPT_(
89545900104SHoward Hinnant            __node_alloc_traits::propagate_on_container_move_assignment::value &&
89645900104SHoward Hinnant            is_nothrow_move_assignable<__node_allocator>::value);
897cf9ed00dSEric Fiselier
898848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
8993e519524SHoward Hinnant    list& operator=(initializer_list<value_type> __il)
9003e519524SHoward Hinnant        {assign(__il.begin(), __il.end()); return *this;}
901cf9ed00dSEric Fiselier
902cf9ed00dSEric Fiselier    _LIBCPP_INLINE_VISIBILITY
903cf9ed00dSEric Fiselier    void assign(initializer_list<value_type> __il)
904cf9ed00dSEric Fiselier        {assign(__il.begin(), __il.end());}
905cf9ed00dSEric Fiselier#endif // _LIBCPP_CXX03_LANG
9063e519524SHoward Hinnant
9073e519524SHoward Hinnant    template <class _InpIter>
9083e519524SHoward Hinnant        void assign(_InpIter __f, _InpIter __l,
9094887d047SNikolas Klauser                    __enable_if_t<__is_cpp17_input_iterator<_InpIter>::value>* = 0);
9103e519524SHoward Hinnant    void assign(size_type __n, const value_type& __x);
9113e519524SHoward Hinnant
912cd31b434SEvgeniy Stepanov    _LIBCPP_INLINE_VISIBILITY
91345900104SHoward Hinnant    allocator_type get_allocator() const _NOEXCEPT;
9143e519524SHoward Hinnant
915848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
91645900104SHoward Hinnant    size_type size() const _NOEXCEPT     {return base::__sz();}
91772c8fad4SMarshall Clow    _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
91845900104SHoward Hinnant    bool empty() const _NOEXCEPT         {return base::empty();}
919848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
92045900104SHoward Hinnant    size_type max_size() const _NOEXCEPT
92155b31b4eSEric Fiselier        {
922d586f92cSArthur O'Dwyer            return _VSTD::min<size_type>(
92355b31b4eSEric Fiselier                base::__node_alloc_max_size(),
92455b31b4eSEric Fiselier                numeric_limits<difference_type >::max());
92555b31b4eSEric Fiselier        }
9263e519524SHoward Hinnant
927848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
92845900104SHoward Hinnant          iterator begin() _NOEXCEPT        {return base::begin();}
929848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
93045900104SHoward Hinnant    const_iterator begin()  const _NOEXCEPT {return base::begin();}
931848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
93245900104SHoward Hinnant          iterator end() _NOEXCEPT          {return base::end();}
933848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
93445900104SHoward Hinnant    const_iterator end()    const _NOEXCEPT {return base::end();}
935848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
93645900104SHoward Hinnant    const_iterator cbegin() const _NOEXCEPT {return base::begin();}
937848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
93845900104SHoward Hinnant    const_iterator cend()   const _NOEXCEPT {return base::end();}
9393e519524SHoward Hinnant
940848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
94145900104SHoward Hinnant          reverse_iterator rbegin() _NOEXCEPT
94245900104SHoward Hinnant            {return       reverse_iterator(end());}
943848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
94445900104SHoward Hinnant    const_reverse_iterator rbegin()  const _NOEXCEPT
94545900104SHoward Hinnant        {return const_reverse_iterator(end());}
946848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
94745900104SHoward Hinnant          reverse_iterator rend() _NOEXCEPT
94845900104SHoward Hinnant            {return       reverse_iterator(begin());}
949848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
95045900104SHoward Hinnant    const_reverse_iterator rend()    const _NOEXCEPT
95145900104SHoward Hinnant        {return const_reverse_iterator(begin());}
952848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
95345900104SHoward Hinnant    const_reverse_iterator crbegin() const _NOEXCEPT
95445900104SHoward Hinnant        {return const_reverse_iterator(end());}
955848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
95645900104SHoward Hinnant    const_reverse_iterator crend()   const _NOEXCEPT
95745900104SHoward Hinnant        {return const_reverse_iterator(begin());}
9583e519524SHoward Hinnant
959848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
960920b56caSHoward Hinnant    reference front()
961920b56caSHoward Hinnant    {
962920b56caSHoward Hinnant        _LIBCPP_ASSERT(!empty(), "list::front called on empty list");
963b88ea354SEric Fiselier        return base::__end_.__next_->__as_node()->__value_;
964920b56caSHoward Hinnant    }
965848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
966920b56caSHoward Hinnant    const_reference front() const
967920b56caSHoward Hinnant    {
968920b56caSHoward Hinnant        _LIBCPP_ASSERT(!empty(), "list::front called on empty list");
969b88ea354SEric Fiselier        return base::__end_.__next_->__as_node()->__value_;
970920b56caSHoward Hinnant    }
971848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
972920b56caSHoward Hinnant    reference back()
973920b56caSHoward Hinnant    {
974920b56caSHoward Hinnant        _LIBCPP_ASSERT(!empty(), "list::back called on empty list");
975b88ea354SEric Fiselier        return base::__end_.__prev_->__as_node()->__value_;
976920b56caSHoward Hinnant    }
977848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
978920b56caSHoward Hinnant    const_reference back() const
979920b56caSHoward Hinnant    {
980920b56caSHoward Hinnant        _LIBCPP_ASSERT(!empty(), "list::back called on empty list");
981b88ea354SEric Fiselier        return base::__end_.__prev_->__as_node()->__value_;
982920b56caSHoward Hinnant    }
9833e519524SHoward Hinnant
984cf9ed00dSEric Fiselier#ifndef _LIBCPP_CXX03_LANG
9853e519524SHoward Hinnant    void push_front(value_type&& __x);
9863e519524SHoward Hinnant    void push_back(value_type&& __x);
987cf9ed00dSEric Fiselier
9883e519524SHoward Hinnant    template <class... _Args>
98963b560beSMarshall Clow#if _LIBCPP_STD_VER > 14
9900e411641SEric Fiselier       reference emplace_front(_Args&&... __args);
99163b560beSMarshall Clow#else
99263b560beSMarshall Clow       void      emplace_front(_Args&&... __args);
99363b560beSMarshall Clow#endif
9943e519524SHoward Hinnant    template <class... _Args>
99563b560beSMarshall Clow#if _LIBCPP_STD_VER > 14
9960e411641SEric Fiselier        reference emplace_back(_Args&&... __args);
99763b560beSMarshall Clow#else
99863b560beSMarshall Clow       void       emplace_back(_Args&&... __args);
99963b560beSMarshall Clow#endif
10003e519524SHoward Hinnant    template <class... _Args>
10013e519524SHoward Hinnant        iterator emplace(const_iterator __p, _Args&&... __args);
1002cf9ed00dSEric Fiselier
10033e519524SHoward Hinnant    iterator insert(const_iterator __p, value_type&& __x);
1004cf9ed00dSEric Fiselier
1005cf9ed00dSEric Fiselier    _LIBCPP_INLINE_VISIBILITY
1006cf9ed00dSEric Fiselier    iterator insert(const_iterator __p, initializer_list<value_type> __il)
1007cf9ed00dSEric Fiselier        {return insert(__p, __il.begin(), __il.end());}
1008cf9ed00dSEric Fiselier#endif // _LIBCPP_CXX03_LANG
10093e519524SHoward Hinnant
10103e519524SHoward Hinnant    void push_front(const value_type& __x);
10113e519524SHoward Hinnant    void push_back(const value_type& __x);
10123e519524SHoward Hinnant
10131c0cedccSEric Fiselier#ifndef _LIBCPP_CXX03_LANG
10141c0cedccSEric Fiselier    template <class _Arg>
10151c0cedccSEric Fiselier    _LIBCPP_INLINE_VISIBILITY
10161c0cedccSEric Fiselier    void __emplace_back(_Arg&& __arg) { emplace_back(_VSTD::forward<_Arg>(__arg)); }
10171c0cedccSEric Fiselier#else
10181c0cedccSEric Fiselier    _LIBCPP_INLINE_VISIBILITY
10191c0cedccSEric Fiselier    void __emplace_back(value_type const& __arg) { push_back(__arg); }
10201c0cedccSEric Fiselier#endif
10211c0cedccSEric Fiselier
10223e519524SHoward Hinnant    iterator insert(const_iterator __p, const value_type& __x);
10233e519524SHoward Hinnant    iterator insert(const_iterator __p, size_type __n, const value_type& __x);
10243e519524SHoward Hinnant    template <class _InpIter>
10253e519524SHoward Hinnant        iterator insert(const_iterator __p, _InpIter __f, _InpIter __l,
10264887d047SNikolas Klauser                        __enable_if_t<__is_cpp17_input_iterator<_InpIter>::value>* = 0);
10273e519524SHoward Hinnant
1028848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
102945900104SHoward Hinnant    void swap(list& __c)
1030e3fbe143SMarshall Clow#if _LIBCPP_STD_VER >= 14
103161b302f9SEric Fiselier        _NOEXCEPT
1032e3fbe143SMarshall Clow#else
103361b302f9SEric Fiselier        _NOEXCEPT_(!__node_alloc_traits::propagate_on_container_swap::value ||
103445900104SHoward Hinnant                   __is_nothrow_swappable<__node_allocator>::value)
1035e3fbe143SMarshall Clow#endif
103645900104SHoward Hinnant        {base::swap(__c);}
1037848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
103845900104SHoward Hinnant    void clear() _NOEXCEPT {base::clear();}
10393e519524SHoward Hinnant
10403e519524SHoward Hinnant    void pop_front();
10413e519524SHoward Hinnant    void pop_back();
10423e519524SHoward Hinnant
10433e519524SHoward Hinnant    iterator erase(const_iterator __p);
10443e519524SHoward Hinnant    iterator erase(const_iterator __f, const_iterator __l);
10453e519524SHoward Hinnant
10463e519524SHoward Hinnant    void resize(size_type __n);
10473e519524SHoward Hinnant    void resize(size_type __n, const value_type& __x);
10483e519524SHoward Hinnant
10493e519524SHoward Hinnant    void splice(const_iterator __p, list& __c);
1050cf9ed00dSEric Fiselier#ifndef _LIBCPP_CXX03_LANG
1051848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
10523e519524SHoward Hinnant    void splice(const_iterator __p, list&& __c) {splice(__p, __c);}
1053848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
10543e519524SHoward Hinnant    void splice(const_iterator __p, list&& __c, const_iterator __i)
10553e519524SHoward Hinnant        {splice(__p, __c, __i);}
1056848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
10573e519524SHoward Hinnant    void splice(const_iterator __p, list&& __c, const_iterator __f, const_iterator __l)
10583e519524SHoward Hinnant        {splice(__p, __c, __f, __l);}
1059cf9ed00dSEric Fiselier#endif
1060cf9ed00dSEric Fiselier    void splice(const_iterator __p, list& __c, const_iterator __i);
1061cf9ed00dSEric Fiselier    void splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l);
10623e519524SHoward Hinnant
10631ab3fe8aSMarshall Clow    __remove_return_type remove(const value_type& __x);
10641ab3fe8aSMarshall Clow    template <class _Pred> __remove_return_type remove_if(_Pred __pred);
1065cd31b434SEvgeniy Stepanov    _LIBCPP_INLINE_VISIBILITY
10661ab3fe8aSMarshall Clow    __remove_return_type unique() { return unique(__equal_to<value_type>()); }
10673e519524SHoward Hinnant    template <class _BinaryPred>
10681ab3fe8aSMarshall Clow        __remove_return_type unique(_BinaryPred __binary_pred);
1069cd31b434SEvgeniy Stepanov    _LIBCPP_INLINE_VISIBILITY
10703e519524SHoward Hinnant    void merge(list& __c);
1071cf9ed00dSEric Fiselier#ifndef _LIBCPP_CXX03_LANG
1072848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
10733e519524SHoward Hinnant    void merge(list&& __c) {merge(__c);}
1074cf9ed00dSEric Fiselier
10753e519524SHoward Hinnant    template <class _Comp>
1076848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
10773e519524SHoward Hinnant        void merge(list&& __c, _Comp __comp) {merge(__c, __comp);}
1078cf9ed00dSEric Fiselier#endif
1079cf9ed00dSEric Fiselier    template <class _Comp>
1080cf9ed00dSEric Fiselier        void merge(list& __c, _Comp __comp);
1081cf9ed00dSEric Fiselier
1082cd31b434SEvgeniy Stepanov    _LIBCPP_INLINE_VISIBILITY
10833e519524SHoward Hinnant    void sort();
10843e519524SHoward Hinnant    template <class _Comp>
1085cd31b434SEvgeniy Stepanov        _LIBCPP_INLINE_VISIBILITY
10863e519524SHoward Hinnant        void sort(_Comp __comp);
10873e519524SHoward Hinnant
108845900104SHoward Hinnant    void reverse() _NOEXCEPT;
10893e519524SHoward Hinnant
1090920b56caSHoward Hinnant    bool __invariants() const;
1091920b56caSHoward Hinnant
10920a412f4dSEric Fiselier    typedef __allocator_destructor<__node_allocator> __node_destructor;
10930a412f4dSEric Fiselier    typedef unique_ptr<__node, __node_destructor> __hold_pointer;
10940a412f4dSEric Fiselier
10950a412f4dSEric Fiselier    _LIBCPP_INLINE_VISIBILITY
10960a412f4dSEric Fiselier    __hold_pointer __allocate_node(__node_allocator& __na) {
10970a412f4dSEric Fiselier      __node_pointer __p = __node_alloc_traits::allocate(__na, 1);
10980a412f4dSEric Fiselier      __p->__prev_ = nullptr;
10990a412f4dSEric Fiselier      return __hold_pointer(__p, __node_destructor(__na, 1));
11000a412f4dSEric Fiselier    }
11010a412f4dSEric Fiselier
1102f3966eafSLouis Dionne#ifdef _LIBCPP_ENABLE_DEBUG_MODE
1103920b56caSHoward Hinnant
1104920b56caSHoward Hinnant    bool __dereferenceable(const const_iterator* __i) const;
1105920b56caSHoward Hinnant    bool __decrementable(const const_iterator* __i) const;
1106920b56caSHoward Hinnant    bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1107920b56caSHoward Hinnant    bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1108920b56caSHoward Hinnant
1109f3966eafSLouis Dionne#endif // _LIBCPP_ENABLE_DEBUG_MODE
1110920b56caSHoward Hinnant
11113e519524SHoward Hinnantprivate:
1112cd31b434SEvgeniy Stepanov    _LIBCPP_INLINE_VISIBILITY
1113b88ea354SEric Fiselier    static void __link_nodes  (__link_pointer __p, __link_pointer __f, __link_pointer __l);
1114cd31b434SEvgeniy Stepanov    _LIBCPP_INLINE_VISIBILITY
1115b88ea354SEric Fiselier    void __link_nodes_at_front(__link_pointer __f, __link_pointer __l);
1116cd31b434SEvgeniy Stepanov    _LIBCPP_INLINE_VISIBILITY
1117b88ea354SEric Fiselier    void __link_nodes_at_back (__link_pointer __f, __link_pointer __l);
11183e519524SHoward Hinnant    iterator __iterator(size_type __n);
11193e519524SHoward Hinnant    template <class _Comp>
11203e519524SHoward Hinnant        static iterator __sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp);
11213e519524SHoward Hinnant
112245900104SHoward Hinnant    void __move_assign(list& __c, true_type)
112345900104SHoward Hinnant        _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value);
11243e519524SHoward Hinnant    void __move_assign(list& __c, false_type);
11253e519524SHoward Hinnant};
11263e519524SHoward Hinnant
112701666904SLouis Dionne#if _LIBCPP_STD_VER >= 17
11284a227e58SMarshall Clowtemplate<class _InputIterator,
1129199d2ebeSArthur O'Dwyer         class _Alloc = allocator<__iter_value_type<_InputIterator>>,
113068072a71SKonstantin Varlamov         class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
11314e0ea2cfSLouis Dionne         class = enable_if_t<__is_allocator<_Alloc>::value>
11324a227e58SMarshall Clow         >
11334a227e58SMarshall Clowlist(_InputIterator, _InputIterator)
1134199d2ebeSArthur O'Dwyer  -> list<__iter_value_type<_InputIterator>, _Alloc>;
11354a227e58SMarshall Clow
11364a227e58SMarshall Clowtemplate<class _InputIterator,
11374a227e58SMarshall Clow         class _Alloc,
113868072a71SKonstantin Varlamov         class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
11394e0ea2cfSLouis Dionne         class = enable_if_t<__is_allocator<_Alloc>::value>
11404a227e58SMarshall Clow         >
11414a227e58SMarshall Clowlist(_InputIterator, _InputIterator, _Alloc)
1142199d2ebeSArthur O'Dwyer  -> list<__iter_value_type<_InputIterator>, _Alloc>;
11434a227e58SMarshall Clow#endif
11444a227e58SMarshall Clow
11453e519524SHoward Hinnant// Link in nodes [__f, __l] just prior to __p
11463e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
1147cd31b434SEvgeniy Stepanovinline
11483e519524SHoward Hinnantvoid
1149b88ea354SEric Fiselierlist<_Tp, _Alloc>::__link_nodes(__link_pointer __p, __link_pointer __f, __link_pointer __l)
11503e519524SHoward Hinnant{
1151866d4efaSHoward Hinnant    __p->__prev_->__next_ = __f;
1152866d4efaSHoward Hinnant    __f->__prev_ = __p->__prev_;
1153866d4efaSHoward Hinnant    __p->__prev_ = __l;
1154866d4efaSHoward Hinnant    __l->__next_ = __p;
11553e519524SHoward Hinnant}
11563e519524SHoward Hinnant
115728d65da6SMarshall Clow// Link in nodes [__f, __l] at the front of the list
115828d65da6SMarshall Clowtemplate <class _Tp, class _Alloc>
1159cd31b434SEvgeniy Stepanovinline
116028d65da6SMarshall Clowvoid
1161b88ea354SEric Fiselierlist<_Tp, _Alloc>::__link_nodes_at_front(__link_pointer __f, __link_pointer __l)
116228d65da6SMarshall Clow{
11635243e190SEric Fiselier    __f->__prev_ = base::__end_as_link();
116428d65da6SMarshall Clow    __l->__next_ = base::__end_.__next_;
116528d65da6SMarshall Clow    __l->__next_->__prev_ = __l;
116628d65da6SMarshall Clow    base::__end_.__next_ = __f;
116728d65da6SMarshall Clow}
116828d65da6SMarshall Clow
1169d1ad7b31SLouis Dionne// Link in nodes [__f, __l] at the back of the list
117028d65da6SMarshall Clowtemplate <class _Tp, class _Alloc>
1171cd31b434SEvgeniy Stepanovinline
117228d65da6SMarshall Clowvoid
1173b88ea354SEric Fiselierlist<_Tp, _Alloc>::__link_nodes_at_back(__link_pointer __f, __link_pointer __l)
117428d65da6SMarshall Clow{
11755243e190SEric Fiselier    __l->__next_ = base::__end_as_link();
117628d65da6SMarshall Clow    __f->__prev_ = base::__end_.__prev_;
117728d65da6SMarshall Clow    __f->__prev_->__next_ = __f;
117828d65da6SMarshall Clow    base::__end_.__prev_ = __l;
117928d65da6SMarshall Clow}
118028d65da6SMarshall Clow
118128d65da6SMarshall Clow
11823e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
1183cd31b434SEvgeniy Stepanovinline
11843e519524SHoward Hinnanttypename list<_Tp, _Alloc>::iterator
11853e519524SHoward Hinnantlist<_Tp, _Alloc>::__iterator(size_type __n)
11863e519524SHoward Hinnant{
1187ce48a113SHoward Hinnant    return __n <= base::__sz() / 2 ? _VSTD::next(begin(), __n)
1188ce48a113SHoward Hinnant                                   : _VSTD::prev(end(), base::__sz() - __n);
11893e519524SHoward Hinnant}
11903e519524SHoward Hinnant
11913e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
11923e519524SHoward Hinnantlist<_Tp, _Alloc>::list(size_type __n)
11933e519524SHoward Hinnant{
1194e3cf7050SNikolas Klauser    _VSTD::__debug_db_insert_c(this);
11953e519524SHoward Hinnant    for (; __n > 0; --__n)
1196cf9ed00dSEric Fiselier#ifndef _LIBCPP_CXX03_LANG
11973e519524SHoward Hinnant        emplace_back();
11983e519524SHoward Hinnant#else
11993e519524SHoward Hinnant        push_back(value_type());
12003e519524SHoward Hinnant#endif
12013e519524SHoward Hinnant}
12023e519524SHoward Hinnant
1203fb829766SMarshall Clow#if _LIBCPP_STD_VER > 11
1204fb829766SMarshall Clowtemplate <class _Tp, class _Alloc>
1205fb829766SMarshall Clowlist<_Tp, _Alloc>::list(size_type __n, const allocator_type& __a) : base(__a)
1206fb829766SMarshall Clow{
1207e3cf7050SNikolas Klauser    _VSTD::__debug_db_insert_c(this);
1208fb829766SMarshall Clow    for (; __n > 0; --__n)
1209fb829766SMarshall Clow        emplace_back();
1210fb829766SMarshall Clow}
1211fb829766SMarshall Clow#endif
1212fb829766SMarshall Clow
12133e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
12143e519524SHoward Hinnantlist<_Tp, _Alloc>::list(size_type __n, const value_type& __x)
12153e519524SHoward Hinnant{
1216e3cf7050SNikolas Klauser    _VSTD::__debug_db_insert_c(this);
12173e519524SHoward Hinnant    for (; __n > 0; --__n)
12183e519524SHoward Hinnant        push_back(__x);
12193e519524SHoward Hinnant}
12203e519524SHoward Hinnant
12213e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
12223e519524SHoward Hinnanttemplate <class _InpIter>
12233e519524SHoward Hinnantlist<_Tp, _Alloc>::list(_InpIter __f, _InpIter __l,
12244887d047SNikolas Klauser                        __enable_if_t<__is_cpp17_input_iterator<_InpIter>::value>*)
12253e519524SHoward Hinnant{
1226e3cf7050SNikolas Klauser    _VSTD::__debug_db_insert_c(this);
12273e519524SHoward Hinnant    for (; __f != __l; ++__f)
12281c0cedccSEric Fiselier        __emplace_back(*__f);
12293e519524SHoward Hinnant}
12303e519524SHoward Hinnant
12313e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
12323e519524SHoward Hinnanttemplate <class _InpIter>
12333e519524SHoward Hinnantlist<_Tp, _Alloc>::list(_InpIter __f, _InpIter __l, const allocator_type& __a,
12344887d047SNikolas Klauser                        __enable_if_t<__is_cpp17_input_iterator<_InpIter>::value>*)
12353e519524SHoward Hinnant    : base(__a)
12363e519524SHoward Hinnant{
1237e3cf7050SNikolas Klauser    _VSTD::__debug_db_insert_c(this);
12383e519524SHoward Hinnant    for (; __f != __l; ++__f)
12391c0cedccSEric Fiselier        __emplace_back(*__f);
12403e519524SHoward Hinnant}
12413e519524SHoward Hinnant
12423e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
12433e519524SHoward Hinnantlist<_Tp, _Alloc>::list(const list& __c)
12448cef7fd7SEric Fiselier    : base(__node_alloc_traits::select_on_container_copy_construction(
12458cef7fd7SEric Fiselier          __c.__node_alloc())) {
1246e3cf7050SNikolas Klauser    _VSTD::__debug_db_insert_c(this);
12473e519524SHoward Hinnant    for (const_iterator __i = __c.begin(), __e = __c.end(); __i != __e; ++__i)
12483e519524SHoward Hinnant        push_back(*__i);
12493e519524SHoward Hinnant}
12503e519524SHoward Hinnant
12513e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
12523c6bd176SNikolas Klauserlist<_Tp, _Alloc>::list(const list& __c, const __type_identity_t<allocator_type>& __a)
12533e519524SHoward Hinnant    : base(__a)
12543e519524SHoward Hinnant{
1255e3cf7050SNikolas Klauser    _VSTD::__debug_db_insert_c(this);
12563e519524SHoward Hinnant    for (const_iterator __i = __c.begin(), __e = __c.end(); __i != __e; ++__i)
12573e519524SHoward Hinnant        push_back(*__i);
12583e519524SHoward Hinnant}
12593e519524SHoward Hinnant
1260cf9ed00dSEric Fiselier#ifndef _LIBCPP_CXX03_LANG
126154976f26SHoward Hinnant
12623e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
12633e519524SHoward Hinnantlist<_Tp, _Alloc>::list(initializer_list<value_type> __il, const allocator_type& __a)
12643e519524SHoward Hinnant    : base(__a)
12653e519524SHoward Hinnant{
1266e3cf7050SNikolas Klauser    _VSTD::__debug_db_insert_c(this);
12673e519524SHoward Hinnant    for (typename initializer_list<value_type>::const_iterator __i = __il.begin(),
12683e519524SHoward Hinnant            __e = __il.end(); __i != __e; ++__i)
12693e519524SHoward Hinnant        push_back(*__i);
12703e519524SHoward Hinnant}
12713e519524SHoward Hinnant
12723e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
12733e519524SHoward Hinnantlist<_Tp, _Alloc>::list(initializer_list<value_type> __il)
12743e519524SHoward Hinnant{
1275e3cf7050SNikolas Klauser    _VSTD::__debug_db_insert_c(this);
12763e519524SHoward Hinnant    for (typename initializer_list<value_type>::const_iterator __i = __il.begin(),
12773e519524SHoward Hinnant            __e = __il.end(); __i != __e; ++__i)
12783e519524SHoward Hinnant        push_back(*__i);
12793e519524SHoward Hinnant}
12803e519524SHoward Hinnant
12813e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
12828cef7fd7SEric Fiselierinline list<_Tp, _Alloc>::list(list&& __c)
128345900104SHoward Hinnant        _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value)
12848cef7fd7SEric Fiselier        : base(_VSTD::move(__c.__node_alloc())) {
1285e3cf7050SNikolas Klauser    _VSTD::__debug_db_insert_c(this);
12863e519524SHoward Hinnant    splice(end(), __c);
12873e519524SHoward Hinnant}
12883e519524SHoward Hinnant
12893e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
1290cd31b434SEvgeniy Stepanovinline
12913c6bd176SNikolas Klauserlist<_Tp, _Alloc>::list(list&& __c, const __type_identity_t<allocator_type>& __a)
12923e519524SHoward Hinnant    : base(__a)
12933e519524SHoward Hinnant{
1294e3cf7050SNikolas Klauser    _VSTD::__debug_db_insert_c(this);
12953e519524SHoward Hinnant    if (__a == __c.get_allocator())
12963e519524SHoward Hinnant        splice(end(), __c);
12973e519524SHoward Hinnant    else
12983e519524SHoward Hinnant    {
1299c003db1fSHoward Hinnant        typedef move_iterator<iterator> _Ip;
1300c003db1fSHoward Hinnant        assign(_Ip(__c.begin()), _Ip(__c.end()));
13013e519524SHoward Hinnant    }
13023e519524SHoward Hinnant}
13033e519524SHoward Hinnant
13043e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
1305cd31b434SEvgeniy Stepanovinline
13063e519524SHoward Hinnantlist<_Tp, _Alloc>&
13073e519524SHoward Hinnantlist<_Tp, _Alloc>::operator=(list&& __c)
130845900104SHoward Hinnant        _NOEXCEPT_(
130945900104SHoward Hinnant            __node_alloc_traits::propagate_on_container_move_assignment::value &&
131045900104SHoward Hinnant            is_nothrow_move_assignable<__node_allocator>::value)
13113e519524SHoward Hinnant{
13123e519524SHoward Hinnant    __move_assign(__c, integral_constant<bool,
13133e519524SHoward Hinnant          __node_alloc_traits::propagate_on_container_move_assignment::value>());
13143e519524SHoward Hinnant    return *this;
13153e519524SHoward Hinnant}
13163e519524SHoward Hinnant
13173e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
13183e519524SHoward Hinnantvoid
13193e519524SHoward Hinnantlist<_Tp, _Alloc>::__move_assign(list& __c, false_type)
13203e519524SHoward Hinnant{
13213e519524SHoward Hinnant    if (base::__node_alloc() != __c.__node_alloc())
13223e519524SHoward Hinnant    {
1323c003db1fSHoward Hinnant        typedef move_iterator<iterator> _Ip;
1324c003db1fSHoward Hinnant        assign(_Ip(__c.begin()), _Ip(__c.end()));
13253e519524SHoward Hinnant    }
13263e519524SHoward Hinnant    else
13273e519524SHoward Hinnant        __move_assign(__c, true_type());
13283e519524SHoward Hinnant}
13293e519524SHoward Hinnant
13303e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
13313e519524SHoward Hinnantvoid
13323e519524SHoward Hinnantlist<_Tp, _Alloc>::__move_assign(list& __c, true_type)
133345900104SHoward Hinnant        _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
13343e519524SHoward Hinnant{
13353e519524SHoward Hinnant    clear();
13363e519524SHoward Hinnant    base::__move_assign_alloc(__c);
13373e519524SHoward Hinnant    splice(end(), __c);
13383e519524SHoward Hinnant}
13393e519524SHoward Hinnant
1340cf9ed00dSEric Fiselier#endif // _LIBCPP_CXX03_LANG
1341cf9ed00dSEric Fiselier
1342cf9ed00dSEric Fiseliertemplate <class _Tp, class _Alloc>
1343cf9ed00dSEric Fiselierinline
1344cf9ed00dSEric Fiselierlist<_Tp, _Alloc>&
1345cf9ed00dSEric Fiselierlist<_Tp, _Alloc>::operator=(const list& __c)
1346cf9ed00dSEric Fiselier{
1347b8608b87SMark de Wever    if (this != _VSTD::addressof(__c))
1348cf9ed00dSEric Fiselier    {
1349cf9ed00dSEric Fiselier        base::__copy_assign_alloc(__c);
1350cf9ed00dSEric Fiselier        assign(__c.begin(), __c.end());
1351cf9ed00dSEric Fiselier    }
1352cf9ed00dSEric Fiselier    return *this;
1353cf9ed00dSEric Fiselier}
13543e519524SHoward Hinnant
13553e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
13563e519524SHoward Hinnanttemplate <class _InpIter>
13573e519524SHoward Hinnantvoid
13583e519524SHoward Hinnantlist<_Tp, _Alloc>::assign(_InpIter __f, _InpIter __l,
13594887d047SNikolas Klauser                          __enable_if_t<__is_cpp17_input_iterator<_InpIter>::value>*)
13603e519524SHoward Hinnant{
13613e519524SHoward Hinnant    iterator __i = begin();
13623e519524SHoward Hinnant    iterator __e = end();
136316bf4339SArthur O'Dwyer    for (; __f != __l && __i != __e; ++__f, (void) ++__i)
13643e519524SHoward Hinnant        *__i = *__f;
13653e519524SHoward Hinnant    if (__i == __e)
13663e519524SHoward Hinnant        insert(__e, __f, __l);
13673e519524SHoward Hinnant    else
13683e519524SHoward Hinnant        erase(__i, __e);
136908f68dfeSNikolas Klauser    std::__debug_db_invalidate_all(this);
13703e519524SHoward Hinnant}
13713e519524SHoward Hinnant
13723e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
13733e519524SHoward Hinnantvoid
13743e519524SHoward Hinnantlist<_Tp, _Alloc>::assign(size_type __n, const value_type& __x)
13753e519524SHoward Hinnant{
13763e519524SHoward Hinnant    iterator __i = begin();
13773e519524SHoward Hinnant    iterator __e = end();
137816bf4339SArthur O'Dwyer    for (; __n > 0 && __i != __e; --__n, (void) ++__i)
13793e519524SHoward Hinnant        *__i = __x;
13803e519524SHoward Hinnant    if (__i == __e)
13813e519524SHoward Hinnant        insert(__e, __n, __x);
13823e519524SHoward Hinnant    else
13833e519524SHoward Hinnant        erase(__i, __e);
138408f68dfeSNikolas Klauser    std::__debug_db_invalidate_all(this);
13853e519524SHoward Hinnant}
13863e519524SHoward Hinnant
13873e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
1388cd31b434SEvgeniy Stepanovinline
13893e519524SHoward Hinnant_Alloc
139045900104SHoward Hinnantlist<_Tp, _Alloc>::get_allocator() const _NOEXCEPT
13913e519524SHoward Hinnant{
13923e519524SHoward Hinnant    return allocator_type(base::__node_alloc());
13933e519524SHoward Hinnant}
13943e519524SHoward Hinnant
13953e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
13963e519524SHoward Hinnanttypename list<_Tp, _Alloc>::iterator
13973e519524SHoward Hinnantlist<_Tp, _Alloc>::insert(const_iterator __p, const value_type& __x)
13983e519524SHoward Hinnant{
13992154dbaaSNikolas Klauser    _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__p)) == this,
14002154dbaaSNikolas Klauser                         "list::insert(iterator, x) called with an iterator not referring to this list");
14013e519524SHoward Hinnant    __node_allocator& __na = base::__node_alloc();
14020a412f4dSEric Fiselier    __hold_pointer __hold = __allocate_node(__na);
1403ce48a113SHoward Hinnant    __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
1404b88ea354SEric Fiselier    __link_nodes(__p.__ptr_, __hold->__as_link(), __hold->__as_link());
14053e519524SHoward Hinnant    ++base::__sz();
1406b88ea354SEric Fiselier    return iterator(__hold.release()->__as_link(), this);
14073e519524SHoward Hinnant}
14083e519524SHoward Hinnant
14093e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
14103e519524SHoward Hinnanttypename list<_Tp, _Alloc>::iterator
14113e519524SHoward Hinnantlist<_Tp, _Alloc>::insert(const_iterator __p, size_type __n, const value_type& __x)
14123e519524SHoward Hinnant{
14132154dbaaSNikolas Klauser    _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__p)) == this,
14142154dbaaSNikolas Klauser                         "list::insert(iterator, n, x) called with an iterator not referring to this list");
1415866d4efaSHoward Hinnant    iterator __r(__p.__ptr_, this);
14163e519524SHoward Hinnant    if (__n > 0)
14173e519524SHoward Hinnant    {
14183e519524SHoward Hinnant        size_type __ds = 0;
14193e519524SHoward Hinnant        __node_allocator& __na = base::__node_alloc();
14200a412f4dSEric Fiselier        __hold_pointer __hold = __allocate_node(__na);
1421ce48a113SHoward Hinnant        __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
14223e519524SHoward Hinnant        ++__ds;
1423b88ea354SEric Fiselier        __r = iterator(__hold->__as_link(), this);
14243e519524SHoward Hinnant        __hold.release();
14253e519524SHoward Hinnant        iterator __e = __r;
14263e519524SHoward Hinnant#ifndef _LIBCPP_NO_EXCEPTIONS
14273e519524SHoward Hinnant        try
14283e519524SHoward Hinnant        {
1429b3371f6fSHoward Hinnant#endif // _LIBCPP_NO_EXCEPTIONS
143016bf4339SArthur O'Dwyer            for (--__n; __n != 0; --__n, (void) ++__e, ++__ds)
14313e519524SHoward Hinnant            {
14323e519524SHoward Hinnant                __hold.reset(__node_alloc_traits::allocate(__na, 1));
1433ce48a113SHoward Hinnant                __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
1434b88ea354SEric Fiselier                __e.__ptr_->__next_ = __hold->__as_link();
14353e519524SHoward Hinnant                __hold->__prev_ = __e.__ptr_;
14363e519524SHoward Hinnant                __hold.release();
14373e519524SHoward Hinnant            }
14383e519524SHoward Hinnant#ifndef _LIBCPP_NO_EXCEPTIONS
14393e519524SHoward Hinnant        }
14403e519524SHoward Hinnant        catch (...)
14413e519524SHoward Hinnant        {
14423e519524SHoward Hinnant            while (true)
14433e519524SHoward Hinnant            {
1444ce48a113SHoward Hinnant                __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
1445b88ea354SEric Fiselier                __link_pointer __prev = __e.__ptr_->__prev_;
1446b88ea354SEric Fiselier                __node_alloc_traits::deallocate(__na, __e.__ptr_->__as_node(), 1);
14473e519524SHoward Hinnant                if (__prev == 0)
14483e519524SHoward Hinnant                    break;
1449920b56caSHoward Hinnant                __e = iterator(__prev, this);
14503e519524SHoward Hinnant            }
14513e519524SHoward Hinnant            throw;
14523e519524SHoward Hinnant        }
1453b3371f6fSHoward Hinnant#endif // _LIBCPP_NO_EXCEPTIONS
1454866d4efaSHoward Hinnant        __link_nodes(__p.__ptr_, __r.__ptr_, __e.__ptr_);
14553e519524SHoward Hinnant        base::__sz() += __ds;
14563e519524SHoward Hinnant    }
14573e519524SHoward Hinnant    return __r;
14583e519524SHoward Hinnant}
14593e519524SHoward Hinnant
14603e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
14613e519524SHoward Hinnanttemplate <class _InpIter>
14623e519524SHoward Hinnanttypename list<_Tp, _Alloc>::iterator
14633e519524SHoward Hinnantlist<_Tp, _Alloc>::insert(const_iterator __p, _InpIter __f, _InpIter __l,
14644887d047SNikolas Klauser                          __enable_if_t<__is_cpp17_input_iterator<_InpIter>::value>*)
14653e519524SHoward Hinnant{
14662154dbaaSNikolas Klauser    _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__p)) == this,
14672154dbaaSNikolas Klauser                         "list::insert(iterator, range) called with an iterator not referring to this list");
1468866d4efaSHoward Hinnant    iterator __r(__p.__ptr_, this);
14693e519524SHoward Hinnant    if (__f != __l)
14703e519524SHoward Hinnant    {
14713e519524SHoward Hinnant        size_type __ds = 0;
14723e519524SHoward Hinnant        __node_allocator& __na = base::__node_alloc();
14730a412f4dSEric Fiselier        __hold_pointer __hold = __allocate_node(__na);
1474ce48a113SHoward Hinnant        __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), *__f);
14753e519524SHoward Hinnant        ++__ds;
1476b88ea354SEric Fiselier        __r = iterator(__hold.get()->__as_link(), this);
14773e519524SHoward Hinnant        __hold.release();
14783e519524SHoward Hinnant        iterator __e = __r;
14793e519524SHoward Hinnant#ifndef _LIBCPP_NO_EXCEPTIONS
14803e519524SHoward Hinnant        try
14813e519524SHoward Hinnant        {
1482b3371f6fSHoward Hinnant#endif // _LIBCPP_NO_EXCEPTIONS
148316bf4339SArthur O'Dwyer            for (++__f; __f != __l; ++__f, (void) ++__e, ++__ds)
14843e519524SHoward Hinnant            {
14853e519524SHoward Hinnant                __hold.reset(__node_alloc_traits::allocate(__na, 1));
1486ce48a113SHoward Hinnant                __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), *__f);
1487b88ea354SEric Fiselier                __e.__ptr_->__next_ = __hold.get()->__as_link();
14883e519524SHoward Hinnant                __hold->__prev_ = __e.__ptr_;
14893e519524SHoward Hinnant                __hold.release();
14903e519524SHoward Hinnant            }
14913e519524SHoward Hinnant#ifndef _LIBCPP_NO_EXCEPTIONS
14923e519524SHoward Hinnant        }
14933e519524SHoward Hinnant        catch (...)
14943e519524SHoward Hinnant        {
14953e519524SHoward Hinnant            while (true)
14963e519524SHoward Hinnant            {
1497ce48a113SHoward Hinnant                __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
1498b88ea354SEric Fiselier                __link_pointer __prev = __e.__ptr_->__prev_;
1499b88ea354SEric Fiselier                __node_alloc_traits::deallocate(__na, __e.__ptr_->__as_node(), 1);
15003e519524SHoward Hinnant                if (__prev == 0)
15013e519524SHoward Hinnant                    break;
1502920b56caSHoward Hinnant                __e = iterator(__prev, this);
15033e519524SHoward Hinnant            }
15043e519524SHoward Hinnant            throw;
15053e519524SHoward Hinnant        }
1506b3371f6fSHoward Hinnant#endif // _LIBCPP_NO_EXCEPTIONS
1507866d4efaSHoward Hinnant        __link_nodes(__p.__ptr_, __r.__ptr_, __e.__ptr_);
15083e519524SHoward Hinnant        base::__sz() += __ds;
15093e519524SHoward Hinnant    }
15103e519524SHoward Hinnant    return __r;
15113e519524SHoward Hinnant}
15123e519524SHoward Hinnant
15133e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
15143e519524SHoward Hinnantvoid
15153e519524SHoward Hinnantlist<_Tp, _Alloc>::push_front(const value_type& __x)
15163e519524SHoward Hinnant{
15173e519524SHoward Hinnant    __node_allocator& __na = base::__node_alloc();
15180a412f4dSEric Fiselier    __hold_pointer __hold = __allocate_node(__na);
1519ce48a113SHoward Hinnant    __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
1520b88ea354SEric Fiselier    __link_pointer __nl = __hold->__as_link();
1521b88ea354SEric Fiselier    __link_nodes_at_front(__nl, __nl);
15223e519524SHoward Hinnant    ++base::__sz();
15233e519524SHoward Hinnant    __hold.release();
15243e519524SHoward Hinnant}
15253e519524SHoward Hinnant
15263e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
15273e519524SHoward Hinnantvoid
15283e519524SHoward Hinnantlist<_Tp, _Alloc>::push_back(const value_type& __x)
15293e519524SHoward Hinnant{
15303e519524SHoward Hinnant    __node_allocator& __na = base::__node_alloc();
15310a412f4dSEric Fiselier    __hold_pointer __hold = __allocate_node(__na);
1532ce48a113SHoward Hinnant    __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
1533b88ea354SEric Fiselier    __link_nodes_at_back(__hold.get()->__as_link(), __hold.get()->__as_link());
15343e519524SHoward Hinnant    ++base::__sz();
15353e519524SHoward Hinnant    __hold.release();
15363e519524SHoward Hinnant}
15373e519524SHoward Hinnant
1538cf9ed00dSEric Fiselier#ifndef _LIBCPP_CXX03_LANG
15393e519524SHoward Hinnant
15403e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
15413e519524SHoward Hinnantvoid
15423e519524SHoward Hinnantlist<_Tp, _Alloc>::push_front(value_type&& __x)
15433e519524SHoward Hinnant{
15443e519524SHoward Hinnant    __node_allocator& __na = base::__node_alloc();
15450a412f4dSEric Fiselier    __hold_pointer __hold = __allocate_node(__na);
1546ce48a113SHoward Hinnant    __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
1547b88ea354SEric Fiselier    __link_nodes_at_front(__hold.get()->__as_link(), __hold.get()->__as_link());
15483e519524SHoward Hinnant    ++base::__sz();
15493e519524SHoward Hinnant    __hold.release();
15503e519524SHoward Hinnant}
15513e519524SHoward Hinnant
15523e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
15533e519524SHoward Hinnantvoid
15543e519524SHoward Hinnantlist<_Tp, _Alloc>::push_back(value_type&& __x)
15553e519524SHoward Hinnant{
15563e519524SHoward Hinnant    __node_allocator& __na = base::__node_alloc();
15570a412f4dSEric Fiselier    __hold_pointer __hold = __allocate_node(__na);
1558ce48a113SHoward Hinnant    __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
1559b88ea354SEric Fiselier    __link_nodes_at_back(__hold.get()->__as_link(), __hold.get()->__as_link());
15603e519524SHoward Hinnant    ++base::__sz();
15613e519524SHoward Hinnant    __hold.release();
15623e519524SHoward Hinnant}
15633e519524SHoward Hinnant
15643e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
15653e519524SHoward Hinnanttemplate <class... _Args>
156663b560beSMarshall Clow#if _LIBCPP_STD_VER > 14
15670e411641SEric Fiseliertypename list<_Tp, _Alloc>::reference
156863b560beSMarshall Clow#else
156963b560beSMarshall Clowvoid
157063b560beSMarshall Clow#endif
15713e519524SHoward Hinnantlist<_Tp, _Alloc>::emplace_front(_Args&&... __args)
15723e519524SHoward Hinnant{
15733e519524SHoward Hinnant    __node_allocator& __na = base::__node_alloc();
15740a412f4dSEric Fiselier    __hold_pointer __hold = __allocate_node(__na);
1575ce48a113SHoward Hinnant    __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
1576b88ea354SEric Fiselier    __link_nodes_at_front(__hold.get()->__as_link(), __hold.get()->__as_link());
15773e519524SHoward Hinnant    ++base::__sz();
157863b560beSMarshall Clow#if _LIBCPP_STD_VER > 14
15790e411641SEric Fiselier    return __hold.release()->__value_;
158063b560beSMarshall Clow#else
158163b560beSMarshall Clow    __hold.release();
158263b560beSMarshall Clow#endif
15833e519524SHoward Hinnant}
15843e519524SHoward Hinnant
15853e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
15863e519524SHoward Hinnanttemplate <class... _Args>
158763b560beSMarshall Clow#if _LIBCPP_STD_VER > 14
15880e411641SEric Fiseliertypename list<_Tp, _Alloc>::reference
158963b560beSMarshall Clow#else
159063b560beSMarshall Clowvoid
159163b560beSMarshall Clow#endif
15923e519524SHoward Hinnantlist<_Tp, _Alloc>::emplace_back(_Args&&... __args)
15933e519524SHoward Hinnant{
15943e519524SHoward Hinnant    __node_allocator& __na = base::__node_alloc();
15950a412f4dSEric Fiselier    __hold_pointer __hold = __allocate_node(__na);
1596ce48a113SHoward Hinnant    __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
1597b88ea354SEric Fiselier    __link_pointer __nl = __hold->__as_link();
1598b88ea354SEric Fiselier    __link_nodes_at_back(__nl, __nl);
15993e519524SHoward Hinnant    ++base::__sz();
160063b560beSMarshall Clow#if _LIBCPP_STD_VER > 14
16010e411641SEric Fiselier    return __hold.release()->__value_;
160263b560beSMarshall Clow#else
160363b560beSMarshall Clow    __hold.release();
160463b560beSMarshall Clow#endif
16053e519524SHoward Hinnant}
16063e519524SHoward Hinnant
16073e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
16083e519524SHoward Hinnanttemplate <class... _Args>
16093e519524SHoward Hinnanttypename list<_Tp, _Alloc>::iterator
16103e519524SHoward Hinnantlist<_Tp, _Alloc>::emplace(const_iterator __p, _Args&&... __args)
16113e519524SHoward Hinnant{
16122154dbaaSNikolas Klauser    _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__p)) == this,
16132154dbaaSNikolas Klauser                         "list::emplace(iterator, args...) called with an iterator not referring to this list");
16143e519524SHoward Hinnant    __node_allocator& __na = base::__node_alloc();
16150a412f4dSEric Fiselier    __hold_pointer __hold = __allocate_node(__na);
1616ce48a113SHoward Hinnant    __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
1617b88ea354SEric Fiselier    __link_pointer __nl = __hold.get()->__as_link();
1618b88ea354SEric Fiselier    __link_nodes(__p.__ptr_, __nl, __nl);
16193e519524SHoward Hinnant    ++base::__sz();
1620b88ea354SEric Fiselier    __hold.release();
1621b88ea354SEric Fiselier    return iterator(__nl, this);
16223e519524SHoward Hinnant}
16233e519524SHoward Hinnant
16243e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
16253e519524SHoward Hinnanttypename list<_Tp, _Alloc>::iterator
16263e519524SHoward Hinnantlist<_Tp, _Alloc>::insert(const_iterator __p, value_type&& __x)
16273e519524SHoward Hinnant{
16282154dbaaSNikolas Klauser    _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__p)) == this,
16292154dbaaSNikolas Klauser                         "list::insert(iterator, x) called with an iterator not referring to this list");
16303e519524SHoward Hinnant    __node_allocator& __na = base::__node_alloc();
16310a412f4dSEric Fiselier    __hold_pointer __hold = __allocate_node(__na);
1632ce48a113SHoward Hinnant    __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
1633b88ea354SEric Fiselier    __link_pointer __nl = __hold->__as_link();
1634b88ea354SEric Fiselier    __link_nodes(__p.__ptr_, __nl, __nl);
16353e519524SHoward Hinnant    ++base::__sz();
1636b88ea354SEric Fiselier    __hold.release();
1637b88ea354SEric Fiselier    return iterator(__nl, this);
16383e519524SHoward Hinnant}
16393e519524SHoward Hinnant
1640cf9ed00dSEric Fiselier#endif // _LIBCPP_CXX03_LANG
16413e519524SHoward Hinnant
16423e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
16433e519524SHoward Hinnantvoid
16443e519524SHoward Hinnantlist<_Tp, _Alloc>::pop_front()
16453e519524SHoward Hinnant{
1646920b56caSHoward Hinnant    _LIBCPP_ASSERT(!empty(), "list::pop_front() called with empty list");
16473e519524SHoward Hinnant    __node_allocator& __na = base::__node_alloc();
1648b88ea354SEric Fiselier    __link_pointer __n = base::__end_.__next_;
16493e519524SHoward Hinnant    base::__unlink_nodes(__n, __n);
16503e519524SHoward Hinnant    --base::__sz();
1651f3966eafSLouis Dionne#ifdef _LIBCPP_ENABLE_DEBUG_MODE
1652920b56caSHoward Hinnant    __c_node* __c = __get_db()->__find_c_and_lock(this);
1653920b56caSHoward Hinnant    for (__i_node** __p = __c->end_; __p != __c->beg_; )
1654920b56caSHoward Hinnant    {
1655920b56caSHoward Hinnant        --__p;
1656920b56caSHoward Hinnant        iterator* __i = static_cast<iterator*>((*__p)->__i_);
1657866d4efaSHoward Hinnant        if (__i->__ptr_ == __n)
1658920b56caSHoward Hinnant        {
1659920b56caSHoward Hinnant            (*__p)->__c_ = nullptr;
1660920b56caSHoward Hinnant            if (--__c->end_ != __p)
16613696227cSArthur O'Dwyer                _VSTD::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1662920b56caSHoward Hinnant        }
1663920b56caSHoward Hinnant    }
1664920b56caSHoward Hinnant    __get_db()->unlock();
1665920b56caSHoward Hinnant#endif
1666b88ea354SEric Fiselier    __node_pointer __np = __n->__as_node();
1667b88ea354SEric Fiselier    __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
1668b88ea354SEric Fiselier    __node_alloc_traits::deallocate(__na, __np, 1);
16693e519524SHoward Hinnant}
16703e519524SHoward Hinnant
16713e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
16723e519524SHoward Hinnantvoid
16733e519524SHoward Hinnantlist<_Tp, _Alloc>::pop_back()
16743e519524SHoward Hinnant{
167596100f15SKristina Bessonova    _LIBCPP_ASSERT(!empty(), "list::pop_back() called on an empty list");
16763e519524SHoward Hinnant    __node_allocator& __na = base::__node_alloc();
1677b88ea354SEric Fiselier    __link_pointer __n = base::__end_.__prev_;
16783e519524SHoward Hinnant    base::__unlink_nodes(__n, __n);
16793e519524SHoward Hinnant    --base::__sz();
1680f3966eafSLouis Dionne#ifdef _LIBCPP_ENABLE_DEBUG_MODE
1681920b56caSHoward Hinnant    __c_node* __c = __get_db()->__find_c_and_lock(this);
1682920b56caSHoward Hinnant    for (__i_node** __p = __c->end_; __p != __c->beg_; )
1683920b56caSHoward Hinnant    {
1684920b56caSHoward Hinnant        --__p;
1685920b56caSHoward Hinnant        iterator* __i = static_cast<iterator*>((*__p)->__i_);
1686866d4efaSHoward Hinnant        if (__i->__ptr_ == __n)
1687920b56caSHoward Hinnant        {
1688920b56caSHoward Hinnant            (*__p)->__c_ = nullptr;
1689920b56caSHoward Hinnant            if (--__c->end_ != __p)
16903696227cSArthur O'Dwyer                _VSTD::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1691920b56caSHoward Hinnant        }
1692920b56caSHoward Hinnant    }
1693920b56caSHoward Hinnant    __get_db()->unlock();
1694920b56caSHoward Hinnant#endif
1695b88ea354SEric Fiselier    __node_pointer __np = __n->__as_node();
1696b88ea354SEric Fiselier    __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
1697b88ea354SEric Fiselier    __node_alloc_traits::deallocate(__na, __np, 1);
16983e519524SHoward Hinnant}
16993e519524SHoward Hinnant
17003e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
17013e519524SHoward Hinnanttypename list<_Tp, _Alloc>::iterator
17023e519524SHoward Hinnantlist<_Tp, _Alloc>::erase(const_iterator __p)
17033e519524SHoward Hinnant{
17042154dbaaSNikolas Klauser    _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__p)) == this,
17052154dbaaSNikolas Klauser                         "list::erase(iterator) called with an iterator not referring to this list");
1706b0e4c9d0SHoward Hinnant    _LIBCPP_ASSERT(__p != end(),
1707b0e4c9d0SHoward Hinnant        "list::erase(iterator) called with a non-dereferenceable iterator");
17083e519524SHoward Hinnant    __node_allocator& __na = base::__node_alloc();
1709b88ea354SEric Fiselier    __link_pointer __n = __p.__ptr_;
1710b88ea354SEric Fiselier    __link_pointer __r = __n->__next_;
17113e519524SHoward Hinnant    base::__unlink_nodes(__n, __n);
17123e519524SHoward Hinnant    --base::__sz();
1713f3966eafSLouis Dionne#ifdef _LIBCPP_ENABLE_DEBUG_MODE
1714920b56caSHoward Hinnant    __c_node* __c = __get_db()->__find_c_and_lock(this);
1715878e7e2fSEric Fiselier    for (__i_node** __ip = __c->end_; __ip != __c->beg_; )
1716920b56caSHoward Hinnant    {
1717878e7e2fSEric Fiselier        --__ip;
1718878e7e2fSEric Fiselier        iterator* __i = static_cast<iterator*>((*__ip)->__i_);
1719866d4efaSHoward Hinnant        if (__i->__ptr_ == __n)
1720920b56caSHoward Hinnant        {
1721878e7e2fSEric Fiselier            (*__ip)->__c_ = nullptr;
1722878e7e2fSEric Fiselier            if (--__c->end_ != __ip)
17233696227cSArthur O'Dwyer                _VSTD::memmove(__ip, __ip+1, (__c->end_ - __ip)*sizeof(__i_node*));
1724920b56caSHoward Hinnant        }
1725920b56caSHoward Hinnant    }
1726920b56caSHoward Hinnant    __get_db()->unlock();
1727920b56caSHoward Hinnant#endif
1728b88ea354SEric Fiselier    __node_pointer __np = __n->__as_node();
1729b88ea354SEric Fiselier    __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
1730b88ea354SEric Fiselier    __node_alloc_traits::deallocate(__na, __np, 1);
1731920b56caSHoward Hinnant    return iterator(__r, this);
17323e519524SHoward Hinnant}
17333e519524SHoward Hinnant
17343e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
17353e519524SHoward Hinnanttypename list<_Tp, _Alloc>::iterator
17363e519524SHoward Hinnantlist<_Tp, _Alloc>::erase(const_iterator __f, const_iterator __l)
17373e519524SHoward Hinnant{
17382154dbaaSNikolas Klauser    _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__f)) == this,
17392154dbaaSNikolas Klauser                         "list::erase(iterator, iterator) called with an iterator not referring to this list");
17402154dbaaSNikolas Klauser    _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__l)) == this,
17412154dbaaSNikolas Klauser                         "list::erase(iterator, iterator) called with an iterator not referring to this list");
17423e519524SHoward Hinnant    if (__f != __l)
17433e519524SHoward Hinnant    {
17443e519524SHoward Hinnant        __node_allocator& __na = base::__node_alloc();
1745866d4efaSHoward Hinnant        base::__unlink_nodes(__f.__ptr_, __l.__ptr_->__prev_);
17463e519524SHoward Hinnant        while (__f != __l)
17473e519524SHoward Hinnant        {
1748b88ea354SEric Fiselier            __link_pointer __n = __f.__ptr_;
17493e519524SHoward Hinnant            ++__f;
17503e519524SHoward Hinnant            --base::__sz();
1751f3966eafSLouis Dionne#ifdef _LIBCPP_ENABLE_DEBUG_MODE
1752920b56caSHoward Hinnant            __c_node* __c = __get_db()->__find_c_and_lock(this);
1753920b56caSHoward Hinnant            for (__i_node** __p = __c->end_; __p != __c->beg_; )
1754920b56caSHoward Hinnant            {
1755920b56caSHoward Hinnant                --__p;
1756920b56caSHoward Hinnant                iterator* __i = static_cast<iterator*>((*__p)->__i_);
1757866d4efaSHoward Hinnant                if (__i->__ptr_ == __n)
1758920b56caSHoward Hinnant                {
1759920b56caSHoward Hinnant                    (*__p)->__c_ = nullptr;
1760920b56caSHoward Hinnant                    if (--__c->end_ != __p)
17613696227cSArthur O'Dwyer                        _VSTD::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1762920b56caSHoward Hinnant                }
1763920b56caSHoward Hinnant            }
1764920b56caSHoward Hinnant            __get_db()->unlock();
1765920b56caSHoward Hinnant#endif
1766b88ea354SEric Fiselier            __node_pointer __np = __n->__as_node();
1767b88ea354SEric Fiselier            __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
1768b88ea354SEric Fiselier            __node_alloc_traits::deallocate(__na, __np, 1);
17693e519524SHoward Hinnant        }
17703e519524SHoward Hinnant    }
1771866d4efaSHoward Hinnant    return iterator(__l.__ptr_, this);
17723e519524SHoward Hinnant}
17733e519524SHoward Hinnant
17743e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
17753e519524SHoward Hinnantvoid
17763e519524SHoward Hinnantlist<_Tp, _Alloc>::resize(size_type __n)
17773e519524SHoward Hinnant{
17783e519524SHoward Hinnant    if (__n < base::__sz())
17793e519524SHoward Hinnant        erase(__iterator(__n), end());
17803e519524SHoward Hinnant    else if (__n > base::__sz())
17813e519524SHoward Hinnant    {
17823e519524SHoward Hinnant        __n -= base::__sz();
17833e519524SHoward Hinnant        size_type __ds = 0;
17843e519524SHoward Hinnant        __node_allocator& __na = base::__node_alloc();
17850a412f4dSEric Fiselier        __hold_pointer __hold = __allocate_node(__na);
1786ce48a113SHoward Hinnant        __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_));
17873e519524SHoward Hinnant        ++__ds;
1788b88ea354SEric Fiselier        iterator __r = iterator(__hold.release()->__as_link(), this);
17893e519524SHoward Hinnant        iterator __e = __r;
17903e519524SHoward Hinnant#ifndef _LIBCPP_NO_EXCEPTIONS
17913e519524SHoward Hinnant        try
17923e519524SHoward Hinnant        {
1793b3371f6fSHoward Hinnant#endif // _LIBCPP_NO_EXCEPTIONS
179416bf4339SArthur O'Dwyer            for (--__n; __n != 0; --__n, (void) ++__e, ++__ds)
17953e519524SHoward Hinnant            {
17963e519524SHoward Hinnant                __hold.reset(__node_alloc_traits::allocate(__na, 1));
1797ce48a113SHoward Hinnant                __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_));
1798b88ea354SEric Fiselier                __e.__ptr_->__next_ = __hold.get()->__as_link();
17993e519524SHoward Hinnant                __hold->__prev_ = __e.__ptr_;
18003e519524SHoward Hinnant                __hold.release();
18013e519524SHoward Hinnant            }
18023e519524SHoward Hinnant#ifndef _LIBCPP_NO_EXCEPTIONS
18033e519524SHoward Hinnant        }
18043e519524SHoward Hinnant        catch (...)
18053e519524SHoward Hinnant        {
18063e519524SHoward Hinnant            while (true)
18073e519524SHoward Hinnant            {
1808ce48a113SHoward Hinnant                __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
1809b88ea354SEric Fiselier                __link_pointer __prev = __e.__ptr_->__prev_;
1810b88ea354SEric Fiselier                __node_alloc_traits::deallocate(__na, __e.__ptr_->__as_node(), 1);
18113e519524SHoward Hinnant                if (__prev == 0)
18123e519524SHoward Hinnant                    break;
1813920b56caSHoward Hinnant                __e = iterator(__prev, this);
18143e519524SHoward Hinnant            }
18153e519524SHoward Hinnant            throw;
18163e519524SHoward Hinnant        }
1817b3371f6fSHoward Hinnant#endif // _LIBCPP_NO_EXCEPTIONS
181828d65da6SMarshall Clow        __link_nodes_at_back(__r.__ptr_, __e.__ptr_);
18193e519524SHoward Hinnant        base::__sz() += __ds;
18203e519524SHoward Hinnant    }
18213e519524SHoward Hinnant}
18223e519524SHoward Hinnant
18233e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
18243e519524SHoward Hinnantvoid
18253e519524SHoward Hinnantlist<_Tp, _Alloc>::resize(size_type __n, const value_type& __x)
18263e519524SHoward Hinnant{
18273e519524SHoward Hinnant    if (__n < base::__sz())
18283e519524SHoward Hinnant        erase(__iterator(__n), end());
18293e519524SHoward Hinnant    else if (__n > base::__sz())
18303e519524SHoward Hinnant    {
18313e519524SHoward Hinnant        __n -= base::__sz();
18323e519524SHoward Hinnant        size_type __ds = 0;
18333e519524SHoward Hinnant        __node_allocator& __na = base::__node_alloc();
18340a412f4dSEric Fiselier        __hold_pointer __hold = __allocate_node(__na);
1835ce48a113SHoward Hinnant        __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
18363e519524SHoward Hinnant        ++__ds;
1837b88ea354SEric Fiselier        __link_pointer __nl = __hold.release()->__as_link();
1838b88ea354SEric Fiselier        iterator __r = iterator(__nl, this);
18393e519524SHoward Hinnant        iterator __e = __r;
18403e519524SHoward Hinnant#ifndef _LIBCPP_NO_EXCEPTIONS
18413e519524SHoward Hinnant        try
18423e519524SHoward Hinnant        {
1843b3371f6fSHoward Hinnant#endif // _LIBCPP_NO_EXCEPTIONS
184416bf4339SArthur O'Dwyer            for (--__n; __n != 0; --__n, (void) ++__e, ++__ds)
18453e519524SHoward Hinnant            {
18463e519524SHoward Hinnant                __hold.reset(__node_alloc_traits::allocate(__na, 1));
1847ce48a113SHoward Hinnant                __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
1848b88ea354SEric Fiselier                __e.__ptr_->__next_ = __hold.get()->__as_link();
18493e519524SHoward Hinnant                __hold->__prev_ = __e.__ptr_;
18503e519524SHoward Hinnant                __hold.release();
18513e519524SHoward Hinnant            }
18523e519524SHoward Hinnant#ifndef _LIBCPP_NO_EXCEPTIONS
18533e519524SHoward Hinnant        }
18543e519524SHoward Hinnant        catch (...)
18553e519524SHoward Hinnant        {
18563e519524SHoward Hinnant            while (true)
18573e519524SHoward Hinnant            {
1858ce48a113SHoward Hinnant                __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
1859b88ea354SEric Fiselier                __link_pointer __prev = __e.__ptr_->__prev_;
1860b88ea354SEric Fiselier                __node_alloc_traits::deallocate(__na, __e.__ptr_->__as_node(), 1);
18613e519524SHoward Hinnant                if (__prev == 0)
18623e519524SHoward Hinnant                    break;
1863920b56caSHoward Hinnant                __e = iterator(__prev, this);
18643e519524SHoward Hinnant            }
18653e519524SHoward Hinnant            throw;
18663e519524SHoward Hinnant        }
1867b3371f6fSHoward Hinnant#endif // _LIBCPP_NO_EXCEPTIONS
18685243e190SEric Fiselier        __link_nodes(base::__end_as_link(), __r.__ptr_, __e.__ptr_);
18693e519524SHoward Hinnant        base::__sz() += __ds;
18703e519524SHoward Hinnant    }
18713e519524SHoward Hinnant}
18723e519524SHoward Hinnant
18733e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
18743e519524SHoward Hinnantvoid
18753e519524SHoward Hinnantlist<_Tp, _Alloc>::splice(const_iterator __p, list& __c)
18763e519524SHoward Hinnant{
18774732dd30SMark de Wever    _LIBCPP_ASSERT(this != _VSTD::addressof(__c),
1878920b56caSHoward Hinnant                   "list::splice(iterator, list) called with this == &list");
18792154dbaaSNikolas Klauser    _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__p)) == this,
18802154dbaaSNikolas Klauser                         "list::splice(iterator, list) called with an iterator not referring to this list");
18813e519524SHoward Hinnant    if (!__c.empty())
18823e519524SHoward Hinnant    {
1883b88ea354SEric Fiselier        __link_pointer __f = __c.__end_.__next_;
1884b88ea354SEric Fiselier        __link_pointer __l = __c.__end_.__prev_;
18853e519524SHoward Hinnant        base::__unlink_nodes(__f, __l);
1886866d4efaSHoward Hinnant        __link_nodes(__p.__ptr_, __f, __l);
18873e519524SHoward Hinnant        base::__sz() += __c.__sz();
18883e519524SHoward Hinnant        __c.__sz() = 0;
1889f3966eafSLouis Dionne#ifdef _LIBCPP_ENABLE_DEBUG_MODE
18904732dd30SMark de Wever        if (_VSTD::addressof(__c) != this) {
1891920b56caSHoward Hinnant            __libcpp_db* __db = __get_db();
1892920b56caSHoward Hinnant            __c_node* __cn1 = __db->__find_c_and_lock(this);
18934732dd30SMark de Wever            __c_node* __cn2 = __db->__find_c(_VSTD::addressof(__c));
1894878e7e2fSEric Fiselier            for (__i_node** __ip = __cn2->end_; __ip != __cn2->beg_;)
1895920b56caSHoward Hinnant            {
1896878e7e2fSEric Fiselier                --__ip;
1897878e7e2fSEric Fiselier                iterator* __i = static_cast<iterator*>((*__ip)->__i_);
18985243e190SEric Fiselier                if (__i->__ptr_ != __c.__end_as_link())
1899920b56caSHoward Hinnant                {
1900878e7e2fSEric Fiselier                    __cn1->__add(*__ip);
1901878e7e2fSEric Fiselier                    (*__ip)->__c_ = __cn1;
1902878e7e2fSEric Fiselier                    if (--__cn2->end_ != __ip)
19033696227cSArthur O'Dwyer                        _VSTD::memmove(__ip, __ip+1, (__cn2->end_ - __ip)*sizeof(__i_node*));
1904920b56caSHoward Hinnant                }
1905920b56caSHoward Hinnant            }
1906920b56caSHoward Hinnant            __db->unlock();
1907516d07deSThomas Anderson        }
1908920b56caSHoward Hinnant#endif
19093e519524SHoward Hinnant    }
19103e519524SHoward Hinnant}
19113e519524SHoward Hinnant
19123e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
19133e519524SHoward Hinnantvoid
19143e519524SHoward Hinnantlist<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __i)
19153e519524SHoward Hinnant{
19162154dbaaSNikolas Klauser    _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__p)) == this,
19172154dbaaSNikolas Klauser        "list::splice(iterator, list, iterator) called with the first iterator not referring to this list");
19182154dbaaSNikolas Klauser    _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__i)) == _VSTD::addressof(__c),
19192154dbaaSNikolas Klauser        "list::splice(iterator, list, iterator) called with the second iterator not referring to the list argument");
19202154dbaaSNikolas Klauser    _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(_VSTD::addressof(__i)),
19212154dbaaSNikolas Klauser        "list::splice(iterator, list, iterator) called with the second iterator not dereferenceable");
19222154dbaaSNikolas Klauser
1923920b56caSHoward Hinnant    if (__p.__ptr_ != __i.__ptr_ && __p.__ptr_ != __i.__ptr_->__next_)
19243e519524SHoward Hinnant    {
1925b88ea354SEric Fiselier        __link_pointer __f = __i.__ptr_;
19263e519524SHoward Hinnant        base::__unlink_nodes(__f, __f);
1927866d4efaSHoward Hinnant        __link_nodes(__p.__ptr_, __f, __f);
19283e519524SHoward Hinnant        --__c.__sz();
19293e519524SHoward Hinnant        ++base::__sz();
1930f3966eafSLouis Dionne#ifdef _LIBCPP_ENABLE_DEBUG_MODE
19314732dd30SMark de Wever        if (_VSTD::addressof(__c) != this) {
1932920b56caSHoward Hinnant            __libcpp_db* __db = __get_db();
1933920b56caSHoward Hinnant            __c_node* __cn1 = __db->__find_c_and_lock(this);
19344732dd30SMark de Wever            __c_node* __cn2 = __db->__find_c(_VSTD::addressof(__c));
1935878e7e2fSEric Fiselier            for (__i_node** __ip = __cn2->end_; __ip != __cn2->beg_;)
1936920b56caSHoward Hinnant            {
1937878e7e2fSEric Fiselier                --__ip;
1938878e7e2fSEric Fiselier                iterator* __j = static_cast<iterator*>((*__ip)->__i_);
1939866d4efaSHoward Hinnant                if (__j->__ptr_ == __f)
1940920b56caSHoward Hinnant                {
1941878e7e2fSEric Fiselier                    __cn1->__add(*__ip);
1942878e7e2fSEric Fiselier                    (*__ip)->__c_ = __cn1;
1943878e7e2fSEric Fiselier                    if (--__cn2->end_ != __ip)
19443696227cSArthur O'Dwyer                        _VSTD::memmove(__ip, __ip+1, (__cn2->end_ - __ip)*sizeof(__i_node*));
1945920b56caSHoward Hinnant                }
1946920b56caSHoward Hinnant            }
1947920b56caSHoward Hinnant            __db->unlock();
1948516d07deSThomas Anderson        }
1949920b56caSHoward Hinnant#endif
19503e519524SHoward Hinnant    }
19513e519524SHoward Hinnant}
19523e519524SHoward Hinnant
1953c87c8917SLouis Dionnetemplate <class _Iterator>
1954c87c8917SLouis Dionne_LIBCPP_HIDE_FROM_ABI
1955c87c8917SLouis Dionnebool __iterator_in_range(_Iterator __first, _Iterator __last, _Iterator __it) {
1956c87c8917SLouis Dionne    for (_Iterator __p = __first; __p != __last; ++__p) {
1957c87c8917SLouis Dionne        if (__p == __it) {
1958c87c8917SLouis Dionne            return true;
1959c87c8917SLouis Dionne        }
1960c87c8917SLouis Dionne    }
1961c87c8917SLouis Dionne    return false;
1962c87c8917SLouis Dionne}
1963c87c8917SLouis Dionne
19643e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
19653e519524SHoward Hinnantvoid
19663e519524SHoward Hinnantlist<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l)
19673e519524SHoward Hinnant{
19682154dbaaSNikolas Klauser    _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__p)) == this,
19692154dbaaSNikolas Klauser        "list::splice(iterator, list, iterator, iterator) called with first iterator not referring to this list");
19702154dbaaSNikolas Klauser    _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__f)) == _VSTD::addressof(__c),
19712154dbaaSNikolas Klauser        "list::splice(iterator, list, iterator, iterator) called with second iterator not referring to the list argument");
19722154dbaaSNikolas Klauser    _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__l)) == _VSTD::addressof(__c),
19732154dbaaSNikolas Klauser        "list::splice(iterator, list, iterator, iterator) called with third iterator not referring to the list argument");
1974c87c8917SLouis Dionne    _LIBCPP_DEBUG_ASSERT(this != std::addressof(__c) || !std::__iterator_in_range(__f, __l, __p),
1975920b56caSHoward Hinnant        "list::splice(iterator, list, iterator, iterator)"
19762154dbaaSNikolas Klauser        " called with the first iterator within the range of the second and third iterators");
1977c87c8917SLouis Dionne
19783e519524SHoward Hinnant    if (__f != __l)
19793e519524SHoward Hinnant    {
1980b88ea354SEric Fiselier        __link_pointer __first = __f.__ptr_;
19813e519524SHoward Hinnant        --__l;
1982b88ea354SEric Fiselier        __link_pointer __last = __l.__ptr_;
19834732dd30SMark de Wever        if (this != _VSTD::addressof(__c))
1984d053b597SEric Fiselier        {
1985d053b597SEric Fiselier            size_type __s = _VSTD::distance(__f, __l) + 1;
1986d053b597SEric Fiselier            __c.__sz() -= __s;
1987d053b597SEric Fiselier            base::__sz() += __s;
1988d053b597SEric Fiselier        }
19893e519524SHoward Hinnant        base::__unlink_nodes(__first, __last);
1990866d4efaSHoward Hinnant        __link_nodes(__p.__ptr_, __first, __last);
1991f3966eafSLouis Dionne#ifdef _LIBCPP_ENABLE_DEBUG_MODE
19924732dd30SMark de Wever        if (_VSTD::addressof(__c) != this) {
1993920b56caSHoward Hinnant            __libcpp_db* __db = __get_db();
1994920b56caSHoward Hinnant            __c_node* __cn1 = __db->__find_c_and_lock(this);
19954732dd30SMark de Wever            __c_node* __cn2 = __db->__find_c(_VSTD::addressof(__c));
1996878e7e2fSEric Fiselier            for (__i_node** __ip = __cn2->end_; __ip != __cn2->beg_;)
1997920b56caSHoward Hinnant            {
1998878e7e2fSEric Fiselier                --__ip;
1999878e7e2fSEric Fiselier                iterator* __j = static_cast<iterator*>((*__ip)->__i_);
2000b88ea354SEric Fiselier                for (__link_pointer __k = __f.__ptr_;
2001920b56caSHoward Hinnant                                              __k != __l.__ptr_; __k = __k->__next_)
2002920b56caSHoward Hinnant                {
2003920b56caSHoward Hinnant                    if (__j->__ptr_ == __k)
2004920b56caSHoward Hinnant                    {
2005878e7e2fSEric Fiselier                        __cn1->__add(*__ip);
2006878e7e2fSEric Fiselier                        (*__ip)->__c_ = __cn1;
2007878e7e2fSEric Fiselier                        if (--__cn2->end_ != __ip)
20083696227cSArthur O'Dwyer                            _VSTD::memmove(__ip, __ip+1, (__cn2->end_ - __ip)*sizeof(__i_node*));
2009920b56caSHoward Hinnant                    }
2010920b56caSHoward Hinnant                }
2011920b56caSHoward Hinnant            }
2012920b56caSHoward Hinnant            __db->unlock();
2013516d07deSThomas Anderson        }
2014920b56caSHoward Hinnant#endif
20153e519524SHoward Hinnant    }
20163e519524SHoward Hinnant}
20173e519524SHoward Hinnant
20183e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
20191ab3fe8aSMarshall Clowtypename list<_Tp, _Alloc>::__remove_return_type
20203e519524SHoward Hinnantlist<_Tp, _Alloc>::remove(const value_type& __x)
20213e519524SHoward Hinnant{
20225cac7755SEric Fiselier    list<_Tp, _Alloc> __deleted_nodes(get_allocator()); // collect the nodes we're removing
2023ced70066SMarshall Clow    for (const_iterator __i = begin(), __e = end(); __i != __e;)
20243e519524SHoward Hinnant    {
20253e519524SHoward Hinnant        if (*__i == __x)
20263e519524SHoward Hinnant        {
2027ced70066SMarshall Clow            const_iterator __j = _VSTD::next(__i);
20283e519524SHoward Hinnant            for (; __j != __e && *__j == __x; ++__j)
20293e519524SHoward Hinnant                ;
2030ced70066SMarshall Clow            __deleted_nodes.splice(__deleted_nodes.end(), *this, __i, __j);
2031ced70066SMarshall Clow            __i = __j;
203290ba0533SMarshall Clow            if (__i != __e)
203328d65da6SMarshall Clow                ++__i;
20343e519524SHoward Hinnant        }
20353e519524SHoward Hinnant        else
20363e519524SHoward Hinnant            ++__i;
20373e519524SHoward Hinnant    }
203824edf8efSMarshall Clow
20391ab3fe8aSMarshall Clow    return (__remove_return_type) __deleted_nodes.size();
20403e519524SHoward Hinnant}
20413e519524SHoward Hinnant
20423e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
20433e519524SHoward Hinnanttemplate <class _Pred>
20441ab3fe8aSMarshall Clowtypename list<_Tp, _Alloc>::__remove_return_type
20453e519524SHoward Hinnantlist<_Tp, _Alloc>::remove_if(_Pred __pred)
20463e519524SHoward Hinnant{
2047896b0c7bSMarshall Clow    list<_Tp, _Alloc> __deleted_nodes(get_allocator()); // collect the nodes we're removing
20483e519524SHoward Hinnant    for (iterator __i = begin(), __e = end(); __i != __e;)
20493e519524SHoward Hinnant    {
20503e519524SHoward Hinnant        if (__pred(*__i))
20513e519524SHoward Hinnant        {
2052ce48a113SHoward Hinnant            iterator __j = _VSTD::next(__i);
20533e519524SHoward Hinnant            for (; __j != __e && __pred(*__j); ++__j)
20543e519524SHoward Hinnant                ;
2055896b0c7bSMarshall Clow            __deleted_nodes.splice(__deleted_nodes.end(), *this, __i, __j);
2056896b0c7bSMarshall Clow            __i = __j;
205790ba0533SMarshall Clow            if (__i != __e)
205828d65da6SMarshall Clow                ++__i;
20593e519524SHoward Hinnant        }
20603e519524SHoward Hinnant        else
20613e519524SHoward Hinnant            ++__i;
20623e519524SHoward Hinnant    }
20633e519524SHoward Hinnant
20641ab3fe8aSMarshall Clow    return (__remove_return_type) __deleted_nodes.size();
20653e519524SHoward Hinnant}
20663e519524SHoward Hinnant
20673e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
20683e519524SHoward Hinnanttemplate <class _BinaryPred>
20691ab3fe8aSMarshall Clowtypename list<_Tp, _Alloc>::__remove_return_type
20703e519524SHoward Hinnantlist<_Tp, _Alloc>::unique(_BinaryPred __binary_pred)
20713e519524SHoward Hinnant{
2072896b0c7bSMarshall Clow    list<_Tp, _Alloc> __deleted_nodes(get_allocator()); // collect the nodes we're removing
20733e519524SHoward Hinnant    for (iterator __i = begin(), __e = end(); __i != __e;)
20743e519524SHoward Hinnant    {
2075ce48a113SHoward Hinnant        iterator __j = _VSTD::next(__i);
20763e519524SHoward Hinnant        for (; __j != __e && __binary_pred(*__i, *__j); ++__j)
20773e519524SHoward Hinnant            ;
2078896b0c7bSMarshall Clow        if (++__i != __j) {
2079896b0c7bSMarshall Clow            __deleted_nodes.splice(__deleted_nodes.end(), *this, __i, __j);
2080896b0c7bSMarshall Clow            __i = __j;
2081896b0c7bSMarshall Clow            }
20823e519524SHoward Hinnant    }
208324edf8efSMarshall Clow
20841ab3fe8aSMarshall Clow    return (__remove_return_type) __deleted_nodes.size();
20853e519524SHoward Hinnant}
20863e519524SHoward Hinnant
20873e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
2088cd31b434SEvgeniy Stepanovinline
20893e519524SHoward Hinnantvoid
20903e519524SHoward Hinnantlist<_Tp, _Alloc>::merge(list& __c)
20913e519524SHoward Hinnant{
20923e519524SHoward Hinnant    merge(__c, __less<value_type>());
20933e519524SHoward Hinnant}
20943e519524SHoward Hinnant
20953e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
20963e519524SHoward Hinnanttemplate <class _Comp>
20973e519524SHoward Hinnantvoid
20983e519524SHoward Hinnantlist<_Tp, _Alloc>::merge(list& __c, _Comp __comp)
20993e519524SHoward Hinnant{
2100ebff3123SMarshall Clow    if (this != _VSTD::addressof(__c))
21013e519524SHoward Hinnant    {
21023e519524SHoward Hinnant        iterator __f1 = begin();
21033e519524SHoward Hinnant        iterator __e1 = end();
21043e519524SHoward Hinnant        iterator __f2 = __c.begin();
21053e519524SHoward Hinnant        iterator __e2 = __c.end();
21063e519524SHoward Hinnant        while (__f1 != __e1 && __f2 != __e2)
21073e519524SHoward Hinnant        {
21083e519524SHoward Hinnant            if (__comp(*__f2, *__f1))
21093e519524SHoward Hinnant            {
21103e519524SHoward Hinnant                size_type __ds = 1;
2111ce48a113SHoward Hinnant                iterator __m2 = _VSTD::next(__f2);
211216bf4339SArthur O'Dwyer                for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2, (void) ++__ds)
21133e519524SHoward Hinnant                    ;
21143e519524SHoward Hinnant                base::__sz() += __ds;
21153e519524SHoward Hinnant                __c.__sz() -= __ds;
2116b88ea354SEric Fiselier                __link_pointer __f = __f2.__ptr_;
2117b88ea354SEric Fiselier                __link_pointer __l = __m2.__ptr_->__prev_;
21183e519524SHoward Hinnant                __f2 = __m2;
21193e519524SHoward Hinnant                base::__unlink_nodes(__f, __l);
2120ce48a113SHoward Hinnant                __m2 = _VSTD::next(__f1);
2121866d4efaSHoward Hinnant                __link_nodes(__f1.__ptr_, __f, __l);
21223e519524SHoward Hinnant                __f1 = __m2;
21233e519524SHoward Hinnant            }
21243e519524SHoward Hinnant            else
21253e519524SHoward Hinnant                ++__f1;
21263e519524SHoward Hinnant        }
21273e519524SHoward Hinnant        splice(__e1, __c);
2128f3966eafSLouis Dionne#ifdef _LIBCPP_ENABLE_DEBUG_MODE
2129920b56caSHoward Hinnant        __libcpp_db* __db = __get_db();
2130920b56caSHoward Hinnant        __c_node* __cn1 = __db->__find_c_and_lock(this);
21314732dd30SMark de Wever        __c_node* __cn2 = __db->__find_c(_VSTD::addressof(__c));
2132920b56caSHoward Hinnant        for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
2133920b56caSHoward Hinnant        {
2134920b56caSHoward Hinnant            --__p;
2135920b56caSHoward Hinnant            iterator* __i = static_cast<iterator*>((*__p)->__i_);
21365243e190SEric Fiselier            if (__i->__ptr_ != __c.__end_as_link())
2137920b56caSHoward Hinnant            {
2138920b56caSHoward Hinnant                __cn1->__add(*__p);
2139920b56caSHoward Hinnant                (*__p)->__c_ = __cn1;
2140920b56caSHoward Hinnant                if (--__cn2->end_ != __p)
21413696227cSArthur O'Dwyer                    _VSTD::memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
2142920b56caSHoward Hinnant            }
2143920b56caSHoward Hinnant        }
2144920b56caSHoward Hinnant        __db->unlock();
2145920b56caSHoward Hinnant#endif
21463e519524SHoward Hinnant    }
21473e519524SHoward Hinnant}
21483e519524SHoward Hinnant
21493e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
2150cd31b434SEvgeniy Stepanovinline
21513e519524SHoward Hinnantvoid
21523e519524SHoward Hinnantlist<_Tp, _Alloc>::sort()
21533e519524SHoward Hinnant{
21543e519524SHoward Hinnant    sort(__less<value_type>());
21553e519524SHoward Hinnant}
21563e519524SHoward Hinnant
21573e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
21583e519524SHoward Hinnanttemplate <class _Comp>
2159cd31b434SEvgeniy Stepanovinline
21603e519524SHoward Hinnantvoid
21613e519524SHoward Hinnantlist<_Tp, _Alloc>::sort(_Comp __comp)
21623e519524SHoward Hinnant{
21633e519524SHoward Hinnant    __sort(begin(), end(), base::__sz(), __comp);
21643e519524SHoward Hinnant}
21653e519524SHoward Hinnant
21663e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
21673e519524SHoward Hinnanttemplate <class _Comp>
21683e519524SHoward Hinnanttypename list<_Tp, _Alloc>::iterator
21693e519524SHoward Hinnantlist<_Tp, _Alloc>::__sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp)
21703e519524SHoward Hinnant{
21713e519524SHoward Hinnant    switch (__n)
21723e519524SHoward Hinnant    {
21733e519524SHoward Hinnant    case 0:
21743e519524SHoward Hinnant    case 1:
21753e519524SHoward Hinnant        return __f1;
21763e519524SHoward Hinnant    case 2:
21773e519524SHoward Hinnant        if (__comp(*--__e2, *__f1))
21783e519524SHoward Hinnant        {
2179b88ea354SEric Fiselier            __link_pointer __f = __e2.__ptr_;
21803e519524SHoward Hinnant            base::__unlink_nodes(__f, __f);
2181866d4efaSHoward Hinnant            __link_nodes(__f1.__ptr_, __f, __f);
21823e519524SHoward Hinnant            return __e2;
21833e519524SHoward Hinnant        }
21843e519524SHoward Hinnant        return __f1;
21853e519524SHoward Hinnant    }
21863e519524SHoward Hinnant    size_type __n2 = __n / 2;
2187ce48a113SHoward Hinnant    iterator __e1 = _VSTD::next(__f1, __n2);
21883e519524SHoward Hinnant    iterator  __r = __f1 = __sort(__f1, __e1, __n2, __comp);
21893e519524SHoward Hinnant    iterator __f2 = __e1 = __sort(__e1, __e2, __n - __n2, __comp);
21903e519524SHoward Hinnant    if (__comp(*__f2, *__f1))
21913e519524SHoward Hinnant    {
2192ce48a113SHoward Hinnant        iterator __m2 = _VSTD::next(__f2);
21933e519524SHoward Hinnant        for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2)
21943e519524SHoward Hinnant            ;
2195b88ea354SEric Fiselier        __link_pointer __f = __f2.__ptr_;
2196b88ea354SEric Fiselier        __link_pointer __l = __m2.__ptr_->__prev_;
21973e519524SHoward Hinnant        __r = __f2;
21983e519524SHoward Hinnant        __e1 = __f2 = __m2;
21993e519524SHoward Hinnant        base::__unlink_nodes(__f, __l);
2200ce48a113SHoward Hinnant        __m2 = _VSTD::next(__f1);
2201866d4efaSHoward Hinnant        __link_nodes(__f1.__ptr_, __f, __l);
22023e519524SHoward Hinnant        __f1 = __m2;
22033e519524SHoward Hinnant    }
22043e519524SHoward Hinnant    else
22053e519524SHoward Hinnant        ++__f1;
22063e519524SHoward Hinnant    while (__f1 != __e1 && __f2 != __e2)
22073e519524SHoward Hinnant    {
22083e519524SHoward Hinnant        if (__comp(*__f2, *__f1))
22093e519524SHoward Hinnant        {
2210ce48a113SHoward Hinnant            iterator __m2 = _VSTD::next(__f2);
22113e519524SHoward Hinnant            for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2)
22123e519524SHoward Hinnant                ;
2213b88ea354SEric Fiselier            __link_pointer __f = __f2.__ptr_;
2214b88ea354SEric Fiselier            __link_pointer __l = __m2.__ptr_->__prev_;
22153e519524SHoward Hinnant            if (__e1 == __f2)
22163e519524SHoward Hinnant                __e1 = __m2;
22173e519524SHoward Hinnant            __f2 = __m2;
22183e519524SHoward Hinnant            base::__unlink_nodes(__f, __l);
2219ce48a113SHoward Hinnant            __m2 = _VSTD::next(__f1);
2220866d4efaSHoward Hinnant            __link_nodes(__f1.__ptr_, __f, __l);
22213e519524SHoward Hinnant            __f1 = __m2;
22223e519524SHoward Hinnant        }
22233e519524SHoward Hinnant        else
22243e519524SHoward Hinnant            ++__f1;
22253e519524SHoward Hinnant    }
22263e519524SHoward Hinnant    return __r;
22273e519524SHoward Hinnant}
22283e519524SHoward Hinnant
22293e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
22303e519524SHoward Hinnantvoid
223145900104SHoward Hinnantlist<_Tp, _Alloc>::reverse() _NOEXCEPT
22323e519524SHoward Hinnant{
22333e519524SHoward Hinnant    if (base::__sz() > 1)
22343e519524SHoward Hinnant    {
22353e519524SHoward Hinnant        iterator __e = end();
2236920b56caSHoward Hinnant        for (iterator __i = begin(); __i.__ptr_ != __e.__ptr_;)
2237920b56caSHoward Hinnant        {
2238ce48a113SHoward Hinnant            _VSTD::swap(__i.__ptr_->__prev_, __i.__ptr_->__next_);
2239920b56caSHoward Hinnant            __i.__ptr_ = __i.__ptr_->__prev_;
2240920b56caSHoward Hinnant        }
2241ce48a113SHoward Hinnant        _VSTD::swap(__e.__ptr_->__prev_, __e.__ptr_->__next_);
22423e519524SHoward Hinnant    }
22433e519524SHoward Hinnant}
22443e519524SHoward Hinnant
22453e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
2246920b56caSHoward Hinnantbool
2247920b56caSHoward Hinnantlist<_Tp, _Alloc>::__invariants() const
2248920b56caSHoward Hinnant{
2249920b56caSHoward Hinnant    return size() == _VSTD::distance(begin(), end());
2250920b56caSHoward Hinnant}
2251920b56caSHoward Hinnant
2252f3966eafSLouis Dionne#ifdef _LIBCPP_ENABLE_DEBUG_MODE
2253920b56caSHoward Hinnant
2254920b56caSHoward Hinnanttemplate <class _Tp, class _Alloc>
2255920b56caSHoward Hinnantbool
2256920b56caSHoward Hinnantlist<_Tp, _Alloc>::__dereferenceable(const const_iterator* __i) const
2257920b56caSHoward Hinnant{
22585243e190SEric Fiselier    return __i->__ptr_ != this->__end_as_link();
2259920b56caSHoward Hinnant}
2260920b56caSHoward Hinnant
2261920b56caSHoward Hinnanttemplate <class _Tp, class _Alloc>
2262920b56caSHoward Hinnantbool
2263920b56caSHoward Hinnantlist<_Tp, _Alloc>::__decrementable(const const_iterator* __i) const
2264920b56caSHoward Hinnant{
2265920b56caSHoward Hinnant    return !empty() &&  __i->__ptr_ != base::__end_.__next_;
2266920b56caSHoward Hinnant}
2267920b56caSHoward Hinnant
2268920b56caSHoward Hinnanttemplate <class _Tp, class _Alloc>
2269920b56caSHoward Hinnantbool
2270fd838227SEric Fiselierlist<_Tp, _Alloc>::__addable(const const_iterator*, ptrdiff_t) const
2271920b56caSHoward Hinnant{
2272920b56caSHoward Hinnant    return false;
2273920b56caSHoward Hinnant}
2274920b56caSHoward Hinnant
2275920b56caSHoward Hinnanttemplate <class _Tp, class _Alloc>
2276920b56caSHoward Hinnantbool
2277fd838227SEric Fiselierlist<_Tp, _Alloc>::__subscriptable(const const_iterator*, ptrdiff_t) const
2278920b56caSHoward Hinnant{
2279920b56caSHoward Hinnant    return false;
2280920b56caSHoward Hinnant}
2281920b56caSHoward Hinnant
2282f3966eafSLouis Dionne#endif // _LIBCPP_ENABLE_DEBUG_MODE
2283920b56caSHoward Hinnant
2284920b56caSHoward Hinnanttemplate <class _Tp, class _Alloc>
2285848a5374SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
22863e519524SHoward Hinnantbool
22873e519524SHoward Hinnantoperator==(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
22883e519524SHoward Hinnant{
2289ce48a113SHoward Hinnant    return __x.size() == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
22903e519524SHoward Hinnant}
22913e519524SHoward Hinnant
22923e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
2293848a5374SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
22943e519524SHoward Hinnantbool
22953e519524SHoward Hinnantoperator< (const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
22963e519524SHoward Hinnant{
2297ce48a113SHoward Hinnant    return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
22983e519524SHoward Hinnant}
22993e519524SHoward Hinnant
23003e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
2301848a5374SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
23023e519524SHoward Hinnantbool
23033e519524SHoward Hinnantoperator!=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
23043e519524SHoward Hinnant{
23053e519524SHoward Hinnant    return !(__x == __y);
23063e519524SHoward Hinnant}
23073e519524SHoward Hinnant
23083e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
2309848a5374SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
23103e519524SHoward Hinnantbool
23113e519524SHoward Hinnantoperator> (const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
23123e519524SHoward Hinnant{
23133e519524SHoward Hinnant    return __y < __x;
23143e519524SHoward Hinnant}
23153e519524SHoward Hinnant
23163e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
2317848a5374SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
23183e519524SHoward Hinnantbool
23193e519524SHoward Hinnantoperator>=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
23203e519524SHoward Hinnant{
23213e519524SHoward Hinnant    return !(__x < __y);
23223e519524SHoward Hinnant}
23233e519524SHoward Hinnant
23243e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
2325848a5374SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
23263e519524SHoward Hinnantbool
23273e519524SHoward Hinnantoperator<=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
23283e519524SHoward Hinnant{
23293e519524SHoward Hinnant    return !(__y < __x);
23303e519524SHoward Hinnant}
23313e519524SHoward Hinnant
23323e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
2333848a5374SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
23343e519524SHoward Hinnantvoid
23353e519524SHoward Hinnantswap(list<_Tp, _Alloc>& __x, list<_Tp, _Alloc>& __y)
233645900104SHoward Hinnant    _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
23373e519524SHoward Hinnant{
23383e519524SHoward Hinnant    __x.swap(__y);
23393e519524SHoward Hinnant}
23403e519524SHoward Hinnant
2341f60c63c0SMarshall Clow#if _LIBCPP_STD_VER > 17
2342f60c63c0SMarshall Clowtemplate <class _Tp, class _Allocator, class _Predicate>
23433e895085SMarek Kurdejinline _LIBCPP_INLINE_VISIBILITY typename list<_Tp, _Allocator>::size_type
23443e895085SMarek Kurdejerase_if(list<_Tp, _Allocator>& __c, _Predicate __pred) {
23453e895085SMarek Kurdej  return __c.remove_if(__pred);
23463e895085SMarek Kurdej}
2347f60c63c0SMarshall Clow
2348f60c63c0SMarshall Clowtemplate <class _Tp, class _Allocator, class _Up>
23493e895085SMarek Kurdejinline _LIBCPP_INLINE_VISIBILITY typename list<_Tp, _Allocator>::size_type
23503e895085SMarek Kurdejerase(list<_Tp, _Allocator>& __c, const _Up& __v) {
23513e895085SMarek Kurdej  return _VSTD::erase_if(__c, [&](auto& __elem) { return __elem == __v; });
23523e895085SMarek Kurdej}
235388930229SMark de Wever
235488930229SMark de Wevertemplate <>
235588930229SMark de Weverinline constexpr bool __format::__enable_insertable<std::list<char>> = true;
235688930229SMark de Wever#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
235788930229SMark de Wevertemplate <>
235888930229SMark de Weverinline constexpr bool __format::__enable_insertable<std::list<wchar_t>> = true;
2359f60c63c0SMarshall Clow#endif
2360f60c63c0SMarshall Clow
236188930229SMark de Wever#endif // _LIBCPP_STD_VER > 17
236288930229SMark de Wever
23633e519524SHoward Hinnant_LIBCPP_END_NAMESPACE_STD
23643e519524SHoward Hinnant
2365a016efb1SEric Fiselier_LIBCPP_POP_MACROS
2366a016efb1SEric Fiselier
23673e519524SHoward Hinnant#endif // _LIBCPP_LIST
2368