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