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