list revision 300770
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    static void __unlink_nodes(__link_pointer __f, __link_pointer __l) _NOEXCEPT;
574
575    __list_imp()
576        _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value);
577    __list_imp(const allocator_type& __a);
578    ~__list_imp();
579    void clear() _NOEXCEPT;
580    _LIBCPP_INLINE_VISIBILITY
581    bool empty() const _NOEXCEPT {return __sz() == 0;}
582
583    _LIBCPP_INLINE_VISIBILITY
584    iterator begin() _NOEXCEPT
585    {
586#if _LIBCPP_DEBUG_LEVEL >= 2
587        return iterator(__end_.__next_, this);
588#else
589        return iterator(__end_.__next_);
590#endif
591    }
592    _LIBCPP_INLINE_VISIBILITY
593    const_iterator begin() const  _NOEXCEPT
594    {
595#if _LIBCPP_DEBUG_LEVEL >= 2
596        return const_iterator(__end_.__next_, this);
597#else
598        return const_iterator(__end_.__next_);
599#endif
600    }
601    _LIBCPP_INLINE_VISIBILITY
602    iterator end() _NOEXCEPT
603    {
604#if _LIBCPP_DEBUG_LEVEL >= 2
605        return iterator(__end_as_link(), this);
606#else
607        return iterator(__end_as_link());
608#endif
609    }
610    _LIBCPP_INLINE_VISIBILITY
611    const_iterator end() const _NOEXCEPT
612    {
613#if _LIBCPP_DEBUG_LEVEL >= 2
614        return const_iterator(__end_as_link(), this);
615#else
616        return const_iterator(__end_as_link());
617#endif
618    }
619
620    void swap(__list_imp& __c)
621#if _LIBCPP_STD_VER >= 14
622        _NOEXCEPT;
623#else
624        _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || 
625                    __is_nothrow_swappable<allocator_type>::value);
626#endif
627
628    _LIBCPP_INLINE_VISIBILITY
629    void __copy_assign_alloc(const __list_imp& __c)
630        {__copy_assign_alloc(__c, integral_constant<bool,
631                      __node_alloc_traits::propagate_on_container_copy_assignment::value>());}
632
633    _LIBCPP_INLINE_VISIBILITY
634    void __move_assign_alloc(__list_imp& __c)
635        _NOEXCEPT_(
636            !__node_alloc_traits::propagate_on_container_move_assignment::value ||
637            is_nothrow_move_assignable<__node_allocator>::value)
638        {__move_assign_alloc(__c, integral_constant<bool,
639                      __node_alloc_traits::propagate_on_container_move_assignment::value>());}
640
641private:
642    _LIBCPP_INLINE_VISIBILITY
643    void __copy_assign_alloc(const __list_imp& __c, true_type)
644        {
645            if (__node_alloc() != __c.__node_alloc())
646                clear();
647            __node_alloc() = __c.__node_alloc();
648        }
649
650    _LIBCPP_INLINE_VISIBILITY
651    void __copy_assign_alloc(const __list_imp& __c, false_type)
652        {}
653
654    _LIBCPP_INLINE_VISIBILITY
655    void __move_assign_alloc(__list_imp& __c, true_type)
656        _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
657        {
658            __node_alloc() = _VSTD::move(__c.__node_alloc());
659        }
660
661    _LIBCPP_INLINE_VISIBILITY
662    void __move_assign_alloc(__list_imp& __c, false_type)
663        _NOEXCEPT
664        {}
665};
666
667// Unlink nodes [__f, __l]
668template <class _Tp, class _Alloc>
669inline _LIBCPP_INLINE_VISIBILITY
670void
671__list_imp<_Tp, _Alloc>::__unlink_nodes(__link_pointer __f, __link_pointer __l)
672    _NOEXCEPT
673{
674    __f->__prev_->__next_ = __l->__next_;
675    __l->__next_->__prev_ = __f->__prev_;
676}
677
678template <class _Tp, class _Alloc>
679inline _LIBCPP_INLINE_VISIBILITY
680__list_imp<_Tp, _Alloc>::__list_imp()
681        _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value)
682    : __size_alloc_(0)
683{
684}
685
686template <class _Tp, class _Alloc>
687inline _LIBCPP_INLINE_VISIBILITY
688__list_imp<_Tp, _Alloc>::__list_imp(const allocator_type& __a)
689    : __size_alloc_(0, __node_allocator(__a))
690{
691}
692
693template <class _Tp, class _Alloc>
694__list_imp<_Tp, _Alloc>::~__list_imp()
695{
696    clear();
697#if _LIBCPP_DEBUG_LEVEL >= 2
698    __get_db()->__erase_c(this);
699#endif
700}
701
702template <class _Tp, class _Alloc>
703void
704__list_imp<_Tp, _Alloc>::clear() _NOEXCEPT
705{
706    if (!empty())
707    {
708        __node_allocator& __na = __node_alloc();
709        __link_pointer __f = __end_.__next_;
710        __link_pointer __l = __end_as_link();
711        __unlink_nodes(__f, __l->__prev_);
712        __sz() = 0;
713        while (__f != __l)
714        {
715            __node_pointer __np = __f->__as_node();
716            __f = __f->__next_;
717            __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
718            __node_alloc_traits::deallocate(__na, __np, 1);
719        }
720#if _LIBCPP_DEBUG_LEVEL >= 2
721        __c_node* __c = __get_db()->__find_c_and_lock(this);
722        for (__i_node** __p = __c->end_; __p != __c->beg_; )
723        {
724            --__p;
725            const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
726            if (__i->__ptr_ != __l)
727            {
728                (*__p)->__c_ = nullptr;
729                if (--__c->end_ != __p)
730                    memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
731            }
732        }
733        __get_db()->unlock();
734#endif
735    }
736}
737
738template <class _Tp, class _Alloc>
739void
740__list_imp<_Tp, _Alloc>::swap(__list_imp& __c)
741#if _LIBCPP_STD_VER >= 14
742        _NOEXCEPT
743#else
744        _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || 
745                    __is_nothrow_swappable<allocator_type>::value)
746#endif
747{
748    _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
749                   this->__node_alloc() == __c.__node_alloc(),
750                   "list::swap: Either propagate_on_container_swap must be true"
751                   " or the allocators must compare equal");
752    using _VSTD::swap;
753    __swap_allocator(__node_alloc(), __c.__node_alloc());
754    swap(__sz(), __c.__sz());
755    swap(__end_, __c.__end_);
756    if (__sz() == 0)
757        __end_.__next_ = __end_.__prev_ = __end_as_link();
758    else
759        __end_.__prev_->__next_ = __end_.__next_->__prev_ = __end_as_link();
760    if (__c.__sz() == 0)
761        __c.__end_.__next_ = __c.__end_.__prev_ = __c.__end_as_link();
762    else
763        __c.__end_.__prev_->__next_ = __c.__end_.__next_->__prev_ = __c.__end_as_link();
764
765#if _LIBCPP_DEBUG_LEVEL >= 2
766    __libcpp_db* __db = __get_db();
767    __c_node* __cn1 = __db->__find_c_and_lock(this);
768    __c_node* __cn2 = __db->__find_c(&__c);
769    std::swap(__cn1->beg_, __cn2->beg_);
770    std::swap(__cn1->end_, __cn2->end_);
771    std::swap(__cn1->cap_, __cn2->cap_);
772    for (__i_node** __p = __cn1->end_; __p != __cn1->beg_;)
773    {
774        --__p;
775        const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
776        if (__i->__ptr_ == __c.__end_as_link())
777        {
778            __cn2->__add(*__p);
779            if (--__cn1->end_ != __p)
780                memmove(__p, __p+1, (__cn1->end_ - __p)*sizeof(__i_node*));
781        }
782        else
783            (*__p)->__c_ = __cn1;
784    }
785    for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
786    {
787        --__p;
788        const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
789        if (__i->__ptr_ == __end_as_link())
790        {
791            __cn1->__add(*__p);
792            if (--__cn2->end_ != __p)
793                memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
794        }
795        else
796            (*__p)->__c_ = __cn2;
797    }
798    __db->unlock();
799#endif
800}
801
802template <class _Tp, class _Alloc /*= allocator<_Tp>*/>
803class _LIBCPP_TYPE_VIS_ONLY list
804    : private __list_imp<_Tp, _Alloc>
805{
806    typedef __list_imp<_Tp, _Alloc> base;
807    typedef typename base::__node              __node;
808    typedef typename base::__node_allocator    __node_allocator;
809    typedef typename base::__node_pointer      __node_pointer;
810    typedef typename base::__node_alloc_traits __node_alloc_traits;
811    typedef typename base::__node_base         __node_base;
812    typedef typename base::__node_base_pointer __node_base_pointer;
813    typedef typename base::__link_pointer __link_pointer;
814
815public:
816    typedef _Tp                                      value_type;
817    typedef _Alloc                                   allocator_type;
818    static_assert((is_same<value_type, typename allocator_type::value_type>::value),
819                  "Invalid allocator::value_type");
820    typedef value_type&                              reference;
821    typedef const value_type&                        const_reference;
822    typedef typename base::pointer                   pointer;
823    typedef typename base::const_pointer             const_pointer;
824    typedef typename base::size_type                 size_type;
825    typedef typename base::difference_type           difference_type;
826    typedef typename base::iterator                  iterator;
827    typedef typename base::const_iterator            const_iterator;
828    typedef _VSTD::reverse_iterator<iterator>         reverse_iterator;
829    typedef _VSTD::reverse_iterator<const_iterator>   const_reverse_iterator;
830
831    _LIBCPP_INLINE_VISIBILITY
832    list()
833        _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value)
834    {
835#if _LIBCPP_DEBUG_LEVEL >= 2
836        __get_db()->__insert_c(this);
837#endif
838    }
839    _LIBCPP_INLINE_VISIBILITY
840    explicit list(const allocator_type& __a) : base(__a)
841    {
842#if _LIBCPP_DEBUG_LEVEL >= 2
843        __get_db()->__insert_c(this);
844#endif
845    }
846    explicit list(size_type __n);
847#if _LIBCPP_STD_VER > 11
848    explicit list(size_type __n, const allocator_type& __a);
849#endif
850    list(size_type __n, const value_type& __x);
851    list(size_type __n, const value_type& __x, const allocator_type& __a);
852    template <class _InpIter>
853        list(_InpIter __f, _InpIter __l,
854             typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
855    template <class _InpIter>
856        list(_InpIter __f, _InpIter __l, const allocator_type& __a,
857             typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
858
859    list(const list& __c);
860    list(const list& __c, const allocator_type& __a);
861    list& operator=(const list& __c);
862#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
863    list(initializer_list<value_type> __il);
864    list(initializer_list<value_type> __il, const allocator_type& __a);
865#endif  // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
866#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
867    list(list&& __c)
868        _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value);
869    list(list&& __c, const allocator_type& __a);
870    list& operator=(list&& __c)
871        _NOEXCEPT_(
872            __node_alloc_traits::propagate_on_container_move_assignment::value &&
873            is_nothrow_move_assignable<__node_allocator>::value);
874#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
875#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
876    _LIBCPP_INLINE_VISIBILITY
877    list& operator=(initializer_list<value_type> __il)
878        {assign(__il.begin(), __il.end()); return *this;}
879#endif  // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
880
881    template <class _InpIter>
882        void assign(_InpIter __f, _InpIter __l,
883             typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
884    void assign(size_type __n, const value_type& __x);
885#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
886    _LIBCPP_INLINE_VISIBILITY
887    void assign(initializer_list<value_type> __il)
888        {assign(__il.begin(), __il.end());}
889#endif  // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
890
891    allocator_type get_allocator() const _NOEXCEPT;
892
893    _LIBCPP_INLINE_VISIBILITY
894    size_type size() const _NOEXCEPT     {return base::__sz();}
895    _LIBCPP_INLINE_VISIBILITY
896    bool empty() const _NOEXCEPT         {return base::empty();}
897    _LIBCPP_INLINE_VISIBILITY
898    size_type max_size() const _NOEXCEPT
899        {return numeric_limits<difference_type>::max();}
900
901    _LIBCPP_INLINE_VISIBILITY
902          iterator begin() _NOEXCEPT        {return base::begin();}
903    _LIBCPP_INLINE_VISIBILITY
904    const_iterator begin()  const _NOEXCEPT {return base::begin();}
905    _LIBCPP_INLINE_VISIBILITY
906          iterator end() _NOEXCEPT          {return base::end();}
907    _LIBCPP_INLINE_VISIBILITY
908    const_iterator end()    const _NOEXCEPT {return base::end();}
909    _LIBCPP_INLINE_VISIBILITY
910    const_iterator cbegin() const _NOEXCEPT {return base::begin();}
911    _LIBCPP_INLINE_VISIBILITY
912    const_iterator cend()   const _NOEXCEPT {return base::end();}
913
914    _LIBCPP_INLINE_VISIBILITY
915          reverse_iterator rbegin() _NOEXCEPT
916            {return       reverse_iterator(end());}
917    _LIBCPP_INLINE_VISIBILITY
918    const_reverse_iterator rbegin()  const _NOEXCEPT
919        {return const_reverse_iterator(end());}
920    _LIBCPP_INLINE_VISIBILITY
921          reverse_iterator rend() _NOEXCEPT
922            {return       reverse_iterator(begin());}
923    _LIBCPP_INLINE_VISIBILITY
924    const_reverse_iterator rend()    const _NOEXCEPT
925        {return const_reverse_iterator(begin());}
926    _LIBCPP_INLINE_VISIBILITY
927    const_reverse_iterator crbegin() const _NOEXCEPT
928        {return const_reverse_iterator(end());}
929    _LIBCPP_INLINE_VISIBILITY
930    const_reverse_iterator crend()   const _NOEXCEPT
931        {return const_reverse_iterator(begin());}
932
933    _LIBCPP_INLINE_VISIBILITY
934    reference front()
935    {
936        _LIBCPP_ASSERT(!empty(), "list::front called on empty list");
937        return base::__end_.__next_->__as_node()->__value_;
938    }
939    _LIBCPP_INLINE_VISIBILITY
940    const_reference front() const
941    {
942        _LIBCPP_ASSERT(!empty(), "list::front called on empty list");
943        return base::__end_.__next_->__as_node()->__value_;
944    }
945    _LIBCPP_INLINE_VISIBILITY
946    reference back()
947    {
948        _LIBCPP_ASSERT(!empty(), "list::back called on empty list");
949        return base::__end_.__prev_->__as_node()->__value_;
950    }
951    _LIBCPP_INLINE_VISIBILITY
952    const_reference back() const
953    {
954        _LIBCPP_ASSERT(!empty(), "list::back called on empty list");
955        return base::__end_.__prev_->__as_node()->__value_;
956    }
957
958#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
959    void push_front(value_type&& __x);
960    void push_back(value_type&& __x);
961#ifndef _LIBCPP_HAS_NO_VARIADICS
962    template <class... _Args>
963       void emplace_front(_Args&&... __args);
964    template <class... _Args>
965        void emplace_back(_Args&&... __args);
966    template <class... _Args>
967        iterator emplace(const_iterator __p, _Args&&... __args);
968#endif  // _LIBCPP_HAS_NO_VARIADICS
969    iterator insert(const_iterator __p, value_type&& __x);
970#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
971
972    void push_front(const value_type& __x);
973    void push_back(const value_type& __x);
974
975    iterator insert(const_iterator __p, const value_type& __x);
976    iterator insert(const_iterator __p, size_type __n, const value_type& __x);
977    template <class _InpIter>
978        iterator insert(const_iterator __p, _InpIter __f, _InpIter __l,
979             typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
980#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
981    _LIBCPP_INLINE_VISIBILITY
982    iterator insert(const_iterator __p, initializer_list<value_type> __il)
983        {return insert(__p, __il.begin(), __il.end());}
984#endif   // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
985
986    _LIBCPP_INLINE_VISIBILITY
987    void swap(list& __c)
988#if _LIBCPP_STD_VER >= 14
989        _NOEXCEPT
990#else
991        _NOEXCEPT_(!__node_alloc_traits::propagate_on_container_swap::value ||
992                   __is_nothrow_swappable<__node_allocator>::value)
993#endif
994        {base::swap(__c);}
995    _LIBCPP_INLINE_VISIBILITY
996    void clear() _NOEXCEPT {base::clear();}
997
998    void pop_front();
999    void pop_back();
1000
1001    iterator erase(const_iterator __p);
1002    iterator erase(const_iterator __f, const_iterator __l);
1003
1004    void resize(size_type __n);
1005    void resize(size_type __n, const value_type& __x);
1006
1007    void splice(const_iterator __p, list& __c);
1008#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1009    _LIBCPP_INLINE_VISIBILITY
1010    void splice(const_iterator __p, list&& __c) {splice(__p, __c);}
1011#endif
1012    void splice(const_iterator __p, list& __c, const_iterator __i);
1013#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1014    _LIBCPP_INLINE_VISIBILITY
1015    void splice(const_iterator __p, list&& __c, const_iterator __i)
1016        {splice(__p, __c, __i);}
1017#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1018    void splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l);
1019#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1020    _LIBCPP_INLINE_VISIBILITY
1021    void splice(const_iterator __p, list&& __c, const_iterator __f, const_iterator __l)
1022        {splice(__p, __c, __f, __l);}
1023#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1024
1025    void remove(const value_type& __x);
1026    template <class _Pred> void remove_if(_Pred __pred);
1027    void unique();
1028    template <class _BinaryPred>
1029        void unique(_BinaryPred __binary_pred);
1030    void merge(list& __c);
1031#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1032    _LIBCPP_INLINE_VISIBILITY
1033    void merge(list&& __c) {merge(__c);}
1034#endif
1035    template <class _Comp>
1036        void merge(list& __c, _Comp __comp);
1037#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1038    template <class _Comp>
1039    _LIBCPP_INLINE_VISIBILITY
1040        void merge(list&& __c, _Comp __comp) {merge(__c, __comp);}
1041#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1042    void sort();
1043    template <class _Comp>
1044        void sort(_Comp __comp);
1045
1046    void reverse() _NOEXCEPT;
1047
1048    bool __invariants() const;
1049
1050#if _LIBCPP_DEBUG_LEVEL >= 2
1051
1052    bool __dereferenceable(const const_iterator* __i) const;
1053    bool __decrementable(const const_iterator* __i) const;
1054    bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1055    bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1056
1057#endif  // _LIBCPP_DEBUG_LEVEL >= 2
1058
1059private:
1060    static void __link_nodes  (__link_pointer __p, __link_pointer __f, __link_pointer __l);
1061    void __link_nodes_at_front(__link_pointer __f, __link_pointer __l);
1062    void __link_nodes_at_back (__link_pointer __f, __link_pointer __l);
1063    iterator __iterator(size_type __n);
1064    template <class _Comp>
1065        static iterator __sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp);
1066
1067    void __move_assign(list& __c, true_type)
1068        _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value);
1069    void __move_assign(list& __c, false_type);
1070};
1071
1072// Link in nodes [__f, __l] just prior to __p
1073template <class _Tp, class _Alloc>
1074inline _LIBCPP_INLINE_VISIBILITY
1075void
1076list<_Tp, _Alloc>::__link_nodes(__link_pointer __p, __link_pointer __f, __link_pointer __l)
1077{
1078    __p->__prev_->__next_ = __f;
1079    __f->__prev_ = __p->__prev_;
1080    __p->__prev_ = __l;
1081    __l->__next_ = __p;
1082}
1083
1084// Link in nodes [__f, __l] at the front of the list
1085template <class _Tp, class _Alloc>
1086inline _LIBCPP_INLINE_VISIBILITY
1087void
1088list<_Tp, _Alloc>::__link_nodes_at_front(__link_pointer __f, __link_pointer __l)
1089{
1090    __f->__prev_ = base::__end_as_link();
1091    __l->__next_ = base::__end_.__next_;
1092    __l->__next_->__prev_ = __l;
1093    base::__end_.__next_ = __f;
1094}
1095
1096// Link in nodes [__f, __l] at the front of the list
1097template <class _Tp, class _Alloc>
1098inline _LIBCPP_INLINE_VISIBILITY
1099void
1100list<_Tp, _Alloc>::__link_nodes_at_back(__link_pointer __f, __link_pointer __l)
1101{
1102    __l->__next_ = base::__end_as_link();
1103    __f->__prev_ = base::__end_.__prev_;
1104    __f->__prev_->__next_ = __f;
1105    base::__end_.__prev_ = __l;
1106}
1107
1108
1109template <class _Tp, class _Alloc>
1110inline _LIBCPP_INLINE_VISIBILITY
1111typename list<_Tp, _Alloc>::iterator
1112list<_Tp, _Alloc>::__iterator(size_type __n)
1113{
1114    return __n <= base::__sz() / 2 ? _VSTD::next(begin(), __n)
1115                                   : _VSTD::prev(end(), base::__sz() - __n);
1116}
1117
1118template <class _Tp, class _Alloc>
1119list<_Tp, _Alloc>::list(size_type __n)
1120{
1121#if _LIBCPP_DEBUG_LEVEL >= 2
1122    __get_db()->__insert_c(this);
1123#endif
1124    for (; __n > 0; --__n)
1125#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1126        emplace_back();
1127#else
1128        push_back(value_type());
1129#endif
1130}
1131
1132#if _LIBCPP_STD_VER > 11
1133template <class _Tp, class _Alloc>
1134list<_Tp, _Alloc>::list(size_type __n, const allocator_type& __a) : base(__a)
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#endif
1147
1148template <class _Tp, class _Alloc>
1149list<_Tp, _Alloc>::list(size_type __n, const value_type& __x)
1150{
1151#if _LIBCPP_DEBUG_LEVEL >= 2
1152    __get_db()->__insert_c(this);
1153#endif
1154    for (; __n > 0; --__n)
1155        push_back(__x);
1156}
1157
1158template <class _Tp, class _Alloc>
1159list<_Tp, _Alloc>::list(size_type __n, const value_type& __x, const allocator_type& __a)
1160    : base(__a)
1161{
1162#if _LIBCPP_DEBUG_LEVEL >= 2
1163    __get_db()->__insert_c(this);
1164#endif
1165    for (; __n > 0; --__n)
1166        push_back(__x);
1167}
1168
1169template <class _Tp, class _Alloc>
1170template <class _InpIter>
1171list<_Tp, _Alloc>::list(_InpIter __f, _InpIter __l,
1172                        typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1173{
1174#if _LIBCPP_DEBUG_LEVEL >= 2
1175    __get_db()->__insert_c(this);
1176#endif
1177    for (; __f != __l; ++__f)
1178        push_back(*__f);
1179}
1180
1181template <class _Tp, class _Alloc>
1182template <class _InpIter>
1183list<_Tp, _Alloc>::list(_InpIter __f, _InpIter __l, const allocator_type& __a,
1184                        typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1185    : base(__a)
1186{
1187#if _LIBCPP_DEBUG_LEVEL >= 2
1188    __get_db()->__insert_c(this);
1189#endif
1190    for (; __f != __l; ++__f)
1191        push_back(*__f);
1192}
1193
1194template <class _Tp, class _Alloc>
1195list<_Tp, _Alloc>::list(const list& __c)
1196    : base(allocator_type(
1197           __node_alloc_traits::select_on_container_copy_construction(
1198                __c.__node_alloc())))
1199{
1200#if _LIBCPP_DEBUG_LEVEL >= 2
1201    __get_db()->__insert_c(this);
1202#endif
1203    for (const_iterator __i = __c.begin(), __e = __c.end(); __i != __e; ++__i)
1204        push_back(*__i);
1205}
1206
1207template <class _Tp, class _Alloc>
1208list<_Tp, _Alloc>::list(const list& __c, const allocator_type& __a)
1209    : base(__a)
1210{
1211#if _LIBCPP_DEBUG_LEVEL >= 2
1212    __get_db()->__insert_c(this);
1213#endif
1214    for (const_iterator __i = __c.begin(), __e = __c.end(); __i != __e; ++__i)
1215        push_back(*__i);
1216}
1217
1218#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1219
1220template <class _Tp, class _Alloc>
1221list<_Tp, _Alloc>::list(initializer_list<value_type> __il, const allocator_type& __a)
1222    : base(__a)
1223{
1224#if _LIBCPP_DEBUG_LEVEL >= 2
1225    __get_db()->__insert_c(this);
1226#endif
1227    for (typename initializer_list<value_type>::const_iterator __i = __il.begin(),
1228            __e = __il.end(); __i != __e; ++__i)
1229        push_back(*__i);
1230}
1231
1232template <class _Tp, class _Alloc>
1233list<_Tp, _Alloc>::list(initializer_list<value_type> __il)
1234{
1235#if _LIBCPP_DEBUG_LEVEL >= 2
1236    __get_db()->__insert_c(this);
1237#endif
1238    for (typename initializer_list<value_type>::const_iterator __i = __il.begin(),
1239            __e = __il.end(); __i != __e; ++__i)
1240        push_back(*__i);
1241}
1242
1243#endif  // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1244
1245template <class _Tp, class _Alloc>
1246inline _LIBCPP_INLINE_VISIBILITY
1247list<_Tp, _Alloc>&
1248list<_Tp, _Alloc>::operator=(const list& __c)
1249{
1250    if (this != &__c)
1251    {
1252        base::__copy_assign_alloc(__c);
1253        assign(__c.begin(), __c.end());
1254    }
1255    return *this;
1256}
1257
1258#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1259
1260template <class _Tp, class _Alloc>
1261inline _LIBCPP_INLINE_VISIBILITY
1262list<_Tp, _Alloc>::list(list&& __c)
1263    _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value)
1264    : base(allocator_type(_VSTD::move(__c.__node_alloc())))
1265{
1266#if _LIBCPP_DEBUG_LEVEL >= 2
1267    __get_db()->__insert_c(this);
1268#endif
1269    splice(end(), __c);
1270}
1271
1272template <class _Tp, class _Alloc>
1273inline _LIBCPP_INLINE_VISIBILITY
1274list<_Tp, _Alloc>::list(list&& __c, const allocator_type& __a)
1275    : base(__a)
1276{
1277#if _LIBCPP_DEBUG_LEVEL >= 2
1278    __get_db()->__insert_c(this);
1279#endif
1280    if (__a == __c.get_allocator())
1281        splice(end(), __c);
1282    else
1283    {
1284        typedef move_iterator<iterator> _Ip;
1285        assign(_Ip(__c.begin()), _Ip(__c.end()));
1286    }
1287}
1288
1289template <class _Tp, class _Alloc>
1290inline _LIBCPP_INLINE_VISIBILITY
1291list<_Tp, _Alloc>&
1292list<_Tp, _Alloc>::operator=(list&& __c)
1293        _NOEXCEPT_(
1294            __node_alloc_traits::propagate_on_container_move_assignment::value &&
1295            is_nothrow_move_assignable<__node_allocator>::value)
1296{
1297    __move_assign(__c, integral_constant<bool,
1298          __node_alloc_traits::propagate_on_container_move_assignment::value>());
1299    return *this;
1300}
1301
1302template <class _Tp, class _Alloc>
1303void
1304list<_Tp, _Alloc>::__move_assign(list& __c, false_type)
1305{
1306    if (base::__node_alloc() != __c.__node_alloc())
1307    {
1308        typedef move_iterator<iterator> _Ip;
1309        assign(_Ip(__c.begin()), _Ip(__c.end()));
1310    }
1311    else
1312        __move_assign(__c, true_type());
1313}
1314
1315template <class _Tp, class _Alloc>
1316void
1317list<_Tp, _Alloc>::__move_assign(list& __c, true_type)
1318        _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
1319{
1320    clear();
1321    base::__move_assign_alloc(__c);
1322    splice(end(), __c);
1323}
1324
1325#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1326
1327template <class _Tp, class _Alloc>
1328template <class _InpIter>
1329void
1330list<_Tp, _Alloc>::assign(_InpIter __f, _InpIter __l,
1331                          typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1332{
1333    iterator __i = begin();
1334    iterator __e = end();
1335    for (; __f != __l && __i != __e; ++__f, ++__i)
1336        *__i = *__f;
1337    if (__i == __e)
1338        insert(__e, __f, __l);
1339    else
1340        erase(__i, __e);
1341}
1342
1343template <class _Tp, class _Alloc>
1344void
1345list<_Tp, _Alloc>::assign(size_type __n, const value_type& __x)
1346{
1347    iterator __i = begin();
1348    iterator __e = end();
1349    for (; __n > 0 && __i != __e; --__n, ++__i)
1350        *__i = __x;
1351    if (__i == __e)
1352        insert(__e, __n, __x);
1353    else
1354        erase(__i, __e);
1355}
1356
1357template <class _Tp, class _Alloc>
1358inline _LIBCPP_INLINE_VISIBILITY
1359_Alloc
1360list<_Tp, _Alloc>::get_allocator() const _NOEXCEPT
1361{
1362    return allocator_type(base::__node_alloc());
1363}
1364
1365template <class _Tp, class _Alloc>
1366typename list<_Tp, _Alloc>::iterator
1367list<_Tp, _Alloc>::insert(const_iterator __p, const value_type& __x)
1368{
1369#if _LIBCPP_DEBUG_LEVEL >= 2
1370    _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1371        "list::insert(iterator, x) called with an iterator not"
1372        " referring to this list");
1373#endif
1374    __node_allocator& __na = base::__node_alloc();
1375    typedef __allocator_destructor<__node_allocator> _Dp;
1376    unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
1377    __hold->__prev_ = 0;
1378    __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
1379    __link_nodes(__p.__ptr_, __hold->__as_link(), __hold->__as_link());
1380    ++base::__sz();
1381#if _LIBCPP_DEBUG_LEVEL >= 2
1382    return iterator(__hold.release()->__as_link(), this);
1383#else
1384    return iterator(__hold.release()->__as_link());
1385#endif
1386}
1387
1388template <class _Tp, class _Alloc>
1389typename list<_Tp, _Alloc>::iterator
1390list<_Tp, _Alloc>::insert(const_iterator __p, size_type __n, const value_type& __x)
1391{
1392#if _LIBCPP_DEBUG_LEVEL >= 2
1393    _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1394        "list::insert(iterator, n, x) called with an iterator not"
1395        " referring to this list");
1396    iterator __r(__p.__ptr_, this);
1397#else
1398    iterator __r(__p.__ptr_);
1399#endif
1400    if (__n > 0)
1401    {
1402        size_type __ds = 0;
1403        __node_allocator& __na = base::__node_alloc();
1404        typedef __allocator_destructor<__node_allocator> _Dp;
1405        unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
1406        __hold->__prev_ = 0;
1407        __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
1408        ++__ds;
1409#if _LIBCPP_DEBUG_LEVEL >= 2
1410        __r = iterator(__hold->__as_link(), this);
1411#else
1412        __r = iterator(__hold->__as_link());
1413#endif
1414        __hold.release();
1415        iterator __e = __r;
1416#ifndef _LIBCPP_NO_EXCEPTIONS
1417        try
1418        {
1419#endif  // _LIBCPP_NO_EXCEPTIONS
1420            for (--__n; __n != 0; --__n, ++__e, ++__ds)
1421            {
1422                __hold.reset(__node_alloc_traits::allocate(__na, 1));
1423                __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
1424                __e.__ptr_->__next_ = __hold->__as_link();
1425                __hold->__prev_ = __e.__ptr_;
1426                __hold.release();
1427            }
1428#ifndef _LIBCPP_NO_EXCEPTIONS
1429        }
1430        catch (...)
1431        {
1432            while (true)
1433            {
1434                __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
1435                __link_pointer __prev = __e.__ptr_->__prev_;
1436                __node_alloc_traits::deallocate(__na, __e.__ptr_->__as_node(), 1);
1437                if (__prev == 0)
1438                    break;
1439#if _LIBCPP_DEBUG_LEVEL >= 2
1440                __e = iterator(__prev, this);
1441#else
1442                __e = iterator(__prev);
1443#endif
1444            }
1445            throw;
1446        }
1447#endif  // _LIBCPP_NO_EXCEPTIONS
1448        __link_nodes(__p.__ptr_, __r.__ptr_, __e.__ptr_);
1449        base::__sz() += __ds;
1450    }
1451    return __r;
1452}
1453
1454template <class _Tp, class _Alloc>
1455template <class _InpIter>
1456typename list<_Tp, _Alloc>::iterator
1457list<_Tp, _Alloc>::insert(const_iterator __p, _InpIter __f, _InpIter __l,
1458             typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1459{
1460#if _LIBCPP_DEBUG_LEVEL >= 2
1461    _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1462        "list::insert(iterator, range) called with an iterator not"
1463        " referring to this list");
1464    iterator __r(__p.__ptr_, this);
1465#else
1466    iterator __r(__p.__ptr_);
1467#endif
1468    if (__f != __l)
1469    {
1470        size_type __ds = 0;
1471        __node_allocator& __na = base::__node_alloc();
1472        typedef __allocator_destructor<__node_allocator> _Dp;
1473        unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
1474        __hold->__prev_ = 0;
1475        __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), *__f);
1476        ++__ds;
1477#if _LIBCPP_DEBUG_LEVEL >= 2
1478        __r = iterator(__hold.get()->__as_link(), this);
1479#else
1480        __r = iterator(__hold.get()->__as_link());
1481#endif
1482        __hold.release();
1483        iterator __e = __r;
1484#ifndef _LIBCPP_NO_EXCEPTIONS
1485        try
1486        {
1487#endif  // _LIBCPP_NO_EXCEPTIONS
1488            for (++__f; __f != __l; ++__f, (void) ++__e, (void) ++__ds)
1489            {
1490                __hold.reset(__node_alloc_traits::allocate(__na, 1));
1491                __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), *__f);
1492                __e.__ptr_->__next_ = __hold.get()->__as_link();
1493                __hold->__prev_ = __e.__ptr_;
1494                __hold.release();
1495            }
1496#ifndef _LIBCPP_NO_EXCEPTIONS
1497        }
1498        catch (...)
1499        {
1500            while (true)
1501            {
1502                __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
1503                __link_pointer __prev = __e.__ptr_->__prev_;
1504                __node_alloc_traits::deallocate(__na, __e.__ptr_->__as_node(), 1);
1505                if (__prev == 0)
1506                    break;
1507#if _LIBCPP_DEBUG_LEVEL >= 2
1508                __e = iterator(__prev, this);
1509#else
1510                __e = iterator(__prev);
1511#endif
1512            }
1513            throw;
1514        }
1515#endif  // _LIBCPP_NO_EXCEPTIONS
1516        __link_nodes(__p.__ptr_, __r.__ptr_, __e.__ptr_);
1517        base::__sz() += __ds;
1518    }
1519    return __r;
1520}
1521
1522template <class _Tp, class _Alloc>
1523void
1524list<_Tp, _Alloc>::push_front(const value_type& __x)
1525{
1526    __node_allocator& __na = base::__node_alloc();
1527    typedef __allocator_destructor<__node_allocator> _Dp;
1528    unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
1529    __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
1530    __link_pointer __nl = __hold->__as_link();
1531    __link_nodes_at_front(__nl, __nl);
1532    ++base::__sz();
1533    __hold.release();
1534}
1535
1536template <class _Tp, class _Alloc>
1537void
1538list<_Tp, _Alloc>::push_back(const value_type& __x)
1539{
1540    __node_allocator& __na = base::__node_alloc();
1541    typedef __allocator_destructor<__node_allocator> _Dp;
1542    unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
1543    __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
1544    __link_nodes_at_back(__hold.get()->__as_link(), __hold.get()->__as_link());
1545    ++base::__sz();
1546    __hold.release();
1547}
1548
1549#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1550
1551template <class _Tp, class _Alloc>
1552void
1553list<_Tp, _Alloc>::push_front(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_), _VSTD::move(__x));
1559    __link_nodes_at_front(__hold.get()->__as_link(), __hold.get()->__as_link());
1560    ++base::__sz();
1561    __hold.release();
1562}
1563
1564template <class _Tp, class _Alloc>
1565void
1566list<_Tp, _Alloc>::push_back(value_type&& __x)
1567{
1568    __node_allocator& __na = base::__node_alloc();
1569    typedef __allocator_destructor<__node_allocator> _Dp;
1570    unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
1571    __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
1572    __link_nodes_at_back(__hold.get()->__as_link(), __hold.get()->__as_link());
1573    ++base::__sz();
1574    __hold.release();
1575}
1576
1577#ifndef _LIBCPP_HAS_NO_VARIADICS
1578
1579template <class _Tp, class _Alloc>
1580template <class... _Args>
1581void
1582list<_Tp, _Alloc>::emplace_front(_Args&&... __args)
1583{
1584    __node_allocator& __na = base::__node_alloc();
1585    typedef __allocator_destructor<__node_allocator> _Dp;
1586    unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
1587    __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
1588    __link_nodes_at_front(__hold.get()->__as_link(), __hold.get()->__as_link());
1589    ++base::__sz();
1590    __hold.release();
1591}
1592
1593template <class _Tp, class _Alloc>
1594template <class... _Args>
1595void
1596list<_Tp, _Alloc>::emplace_back(_Args&&... __args)
1597{
1598    __node_allocator& __na = base::__node_alloc();
1599    typedef __allocator_destructor<__node_allocator> _Dp;
1600    unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
1601    __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
1602    __link_pointer __nl = __hold->__as_link();
1603    __link_nodes_at_back(__nl, __nl);
1604    ++base::__sz();
1605    __hold.release();
1606}
1607
1608template <class _Tp, class _Alloc>
1609template <class... _Args>
1610typename list<_Tp, _Alloc>::iterator
1611list<_Tp, _Alloc>::emplace(const_iterator __p, _Args&&... __args)
1612{
1613#if _LIBCPP_DEBUG_LEVEL >= 2
1614    _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1615        "list::emplace(iterator, args...) called with an iterator not"
1616        " referring to this list");
1617#endif
1618    __node_allocator& __na = base::__node_alloc();
1619    typedef __allocator_destructor<__node_allocator> _Dp;
1620    unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
1621    __hold->__prev_ = 0;
1622    __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
1623    __link_pointer __nl = __hold.get()->__as_link();
1624    __link_nodes(__p.__ptr_, __nl, __nl);
1625    ++base::__sz();
1626    __hold.release();
1627#if _LIBCPP_DEBUG_LEVEL >= 2
1628    return iterator(__nl, this);
1629#else
1630    return iterator(__nl);
1631#endif
1632}
1633
1634#endif  // _LIBCPP_HAS_NO_VARIADICS
1635
1636template <class _Tp, class _Alloc>
1637typename list<_Tp, _Alloc>::iterator
1638list<_Tp, _Alloc>::insert(const_iterator __p, value_type&& __x)
1639{
1640#if _LIBCPP_DEBUG_LEVEL >= 2
1641    _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1642        "list::insert(iterator, x) called with an iterator not"
1643        " referring to this list");
1644#endif
1645    __node_allocator& __na = base::__node_alloc();
1646    typedef __allocator_destructor<__node_allocator> _Dp;
1647    unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
1648    __hold->__prev_ = 0;
1649    __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
1650    __link_pointer __nl = __hold->__as_link();
1651    __link_nodes(__p.__ptr_, __nl, __nl);
1652    ++base::__sz();
1653    __hold.release();
1654#if _LIBCPP_DEBUG_LEVEL >= 2
1655    return iterator(__nl, this);
1656#else
1657    return iterator(__nl);
1658#endif
1659}
1660
1661#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1662
1663template <class _Tp, class _Alloc>
1664void
1665list<_Tp, _Alloc>::pop_front()
1666{
1667    _LIBCPP_ASSERT(!empty(), "list::pop_front() called with empty list");
1668    __node_allocator& __na = base::__node_alloc();
1669    __link_pointer __n = base::__end_.__next_;
1670    base::__unlink_nodes(__n, __n);
1671    --base::__sz();
1672#if _LIBCPP_DEBUG_LEVEL >= 2
1673    __c_node* __c = __get_db()->__find_c_and_lock(this);
1674    for (__i_node** __p = __c->end_; __p != __c->beg_; )
1675    {
1676        --__p;
1677        iterator* __i = static_cast<iterator*>((*__p)->__i_);
1678        if (__i->__ptr_ == __n)
1679        {
1680            (*__p)->__c_ = nullptr;
1681            if (--__c->end_ != __p)
1682                memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1683        }
1684    }
1685    __get_db()->unlock();
1686#endif
1687    __node_pointer __np = __n->__as_node();
1688    __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
1689    __node_alloc_traits::deallocate(__na, __np, 1);
1690}
1691
1692template <class _Tp, class _Alloc>
1693void
1694list<_Tp, _Alloc>::pop_back()
1695{
1696    _LIBCPP_ASSERT(!empty(), "list::pop_back() called with empty list");
1697    __node_allocator& __na = base::__node_alloc();
1698    __link_pointer __n = base::__end_.__prev_;
1699    base::__unlink_nodes(__n, __n);
1700    --base::__sz();
1701#if _LIBCPP_DEBUG_LEVEL >= 2
1702    __c_node* __c = __get_db()->__find_c_and_lock(this);
1703    for (__i_node** __p = __c->end_; __p != __c->beg_; )
1704    {
1705        --__p;
1706        iterator* __i = static_cast<iterator*>((*__p)->__i_);
1707        if (__i->__ptr_ == __n)
1708        {
1709            (*__p)->__c_ = nullptr;
1710            if (--__c->end_ != __p)
1711                memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1712        }
1713    }
1714    __get_db()->unlock();
1715#endif
1716    __node_pointer __np = __n->__as_node();
1717    __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
1718    __node_alloc_traits::deallocate(__na, __np, 1);
1719}
1720
1721template <class _Tp, class _Alloc>
1722typename list<_Tp, _Alloc>::iterator
1723list<_Tp, _Alloc>::erase(const_iterator __p)
1724{
1725#if _LIBCPP_DEBUG_LEVEL >= 2
1726    _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1727        "list::erase(iterator) called with an iterator not"
1728        " referring to this list");
1729#endif
1730    _LIBCPP_ASSERT(__p != end(),
1731        "list::erase(iterator) called with a non-dereferenceable iterator");
1732    __node_allocator& __na = base::__node_alloc();
1733    __link_pointer __n = __p.__ptr_;
1734    __link_pointer __r = __n->__next_;
1735    base::__unlink_nodes(__n, __n);
1736    --base::__sz();
1737#if _LIBCPP_DEBUG_LEVEL >= 2
1738    __c_node* __c = __get_db()->__find_c_and_lock(this);
1739    for (__i_node** __p = __c->end_; __p != __c->beg_; )
1740    {
1741        --__p;
1742        iterator* __i = static_cast<iterator*>((*__p)->__i_);
1743        if (__i->__ptr_ == __n)
1744        {
1745            (*__p)->__c_ = nullptr;
1746            if (--__c->end_ != __p)
1747                memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1748        }
1749    }
1750    __get_db()->unlock();
1751#endif
1752    __node_pointer __np = __n->__as_node();
1753    __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
1754    __node_alloc_traits::deallocate(__na, __np, 1);
1755#if _LIBCPP_DEBUG_LEVEL >= 2
1756    return iterator(__r, this);
1757#else
1758    return iterator(__r);
1759#endif
1760}
1761
1762template <class _Tp, class _Alloc>
1763typename list<_Tp, _Alloc>::iterator
1764list<_Tp, _Alloc>::erase(const_iterator __f, const_iterator __l)
1765{
1766#if _LIBCPP_DEBUG_LEVEL >= 2
1767    _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__f) == this,
1768        "list::erase(iterator, iterator) called with an iterator not"
1769        " referring to this list");
1770#endif
1771    if (__f != __l)
1772    {
1773        __node_allocator& __na = base::__node_alloc();
1774        base::__unlink_nodes(__f.__ptr_, __l.__ptr_->__prev_);
1775        while (__f != __l)
1776        {
1777            __link_pointer __n = __f.__ptr_;
1778            ++__f;
1779            --base::__sz();
1780#if _LIBCPP_DEBUG_LEVEL >= 2
1781            __c_node* __c = __get_db()->__find_c_and_lock(this);
1782            for (__i_node** __p = __c->end_; __p != __c->beg_; )
1783            {
1784                --__p;
1785                iterator* __i = static_cast<iterator*>((*__p)->__i_);
1786                if (__i->__ptr_ == __n)
1787                {
1788                    (*__p)->__c_ = nullptr;
1789                    if (--__c->end_ != __p)
1790                        memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1791                }
1792            }
1793            __get_db()->unlock();
1794#endif
1795            __node_pointer __np = __n->__as_node();
1796            __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_));
1797            __node_alloc_traits::deallocate(__na, __np, 1);
1798        }
1799    }
1800#if _LIBCPP_DEBUG_LEVEL >= 2
1801    return iterator(__l.__ptr_, this);
1802#else
1803    return iterator(__l.__ptr_);
1804#endif
1805}
1806
1807template <class _Tp, class _Alloc>
1808void
1809list<_Tp, _Alloc>::resize(size_type __n)
1810{
1811    if (__n < base::__sz())
1812        erase(__iterator(__n), end());
1813    else if (__n > base::__sz())
1814    {
1815        __n -= base::__sz();
1816        size_type __ds = 0;
1817        __node_allocator& __na = base::__node_alloc();
1818        typedef __allocator_destructor<__node_allocator> _Dp;
1819        unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
1820        __hold->__prev_ = 0;
1821        __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_));
1822        ++__ds;
1823#if _LIBCPP_DEBUG_LEVEL >= 2
1824        iterator __r = iterator(__hold.release()->__as_link(), this);
1825#else
1826        iterator __r = iterator(__hold.release()->__as_link());
1827#endif
1828        iterator __e = __r;
1829#ifndef _LIBCPP_NO_EXCEPTIONS
1830        try
1831        {
1832#endif  // _LIBCPP_NO_EXCEPTIONS
1833            for (--__n; __n != 0; --__n, ++__e, ++__ds)
1834            {
1835                __hold.reset(__node_alloc_traits::allocate(__na, 1));
1836                __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_));
1837                __e.__ptr_->__next_ = __hold.get()->__as_link();
1838                __hold->__prev_ = __e.__ptr_;
1839                __hold.release();
1840            }
1841#ifndef _LIBCPP_NO_EXCEPTIONS
1842        }
1843        catch (...)
1844        {
1845            while (true)
1846            {
1847                __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
1848                __link_pointer __prev = __e.__ptr_->__prev_;
1849                __node_alloc_traits::deallocate(__na, __e.__ptr_->__as_node(), 1);
1850                if (__prev == 0)
1851                    break;
1852#if _LIBCPP_DEBUG_LEVEL >= 2
1853                __e = iterator(__prev, this);
1854#else
1855                __e = iterator(__prev);
1856#endif
1857            }
1858            throw;
1859        }
1860#endif  // _LIBCPP_NO_EXCEPTIONS
1861        __link_nodes_at_back(__r.__ptr_, __e.__ptr_);
1862        base::__sz() += __ds;
1863    }
1864}
1865
1866template <class _Tp, class _Alloc>
1867void
1868list<_Tp, _Alloc>::resize(size_type __n, const value_type& __x)
1869{
1870    if (__n < base::__sz())
1871        erase(__iterator(__n), end());
1872    else if (__n > base::__sz())
1873    {
1874        __n -= base::__sz();
1875        size_type __ds = 0;
1876        __node_allocator& __na = base::__node_alloc();
1877        typedef __allocator_destructor<__node_allocator> _Dp;
1878        unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
1879        __hold->__prev_ = 0;
1880        __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
1881        ++__ds;
1882        __link_pointer __nl = __hold.release()->__as_link();
1883#if _LIBCPP_DEBUG_LEVEL >= 2
1884        iterator __r = iterator(__nl, this);
1885#else
1886        iterator __r = iterator(__nl);
1887#endif
1888        iterator __e = __r;
1889#ifndef _LIBCPP_NO_EXCEPTIONS
1890        try
1891        {
1892#endif  // _LIBCPP_NO_EXCEPTIONS
1893            for (--__n; __n != 0; --__n, ++__e, ++__ds)
1894            {
1895                __hold.reset(__node_alloc_traits::allocate(__na, 1));
1896                __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
1897                __e.__ptr_->__next_ = __hold.get()->__as_link();
1898                __hold->__prev_ = __e.__ptr_;
1899                __hold.release();
1900            }
1901#ifndef _LIBCPP_NO_EXCEPTIONS
1902        }
1903        catch (...)
1904        {
1905            while (true)
1906            {
1907                __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
1908                __link_pointer __prev = __e.__ptr_->__prev_;
1909                __node_alloc_traits::deallocate(__na, __e.__ptr_->__as_node(), 1);
1910                if (__prev == 0)
1911                    break;
1912#if _LIBCPP_DEBUG_LEVEL >= 2
1913                __e = iterator(__prev, this);
1914#else
1915                __e = iterator(__prev);
1916#endif
1917            }
1918            throw;
1919        }
1920#endif  // _LIBCPP_NO_EXCEPTIONS
1921        __link_nodes(base::__end_as_link(), __r.__ptr_, __e.__ptr_);
1922        base::__sz() += __ds;
1923    }
1924}
1925
1926template <class _Tp, class _Alloc>
1927void
1928list<_Tp, _Alloc>::splice(const_iterator __p, list& __c)
1929{
1930    _LIBCPP_ASSERT(this != &__c,
1931                   "list::splice(iterator, list) called with this == &list");
1932#if _LIBCPP_DEBUG_LEVEL >= 2
1933    _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1934        "list::splice(iterator, list) called with an iterator not"
1935        " referring to this list");
1936#endif
1937    if (!__c.empty())
1938    {
1939        __link_pointer __f = __c.__end_.__next_;
1940        __link_pointer __l = __c.__end_.__prev_;
1941        base::__unlink_nodes(__f, __l);
1942        __link_nodes(__p.__ptr_, __f, __l);
1943        base::__sz() += __c.__sz();
1944        __c.__sz() = 0;
1945#if _LIBCPP_DEBUG_LEVEL >= 2
1946        __libcpp_db* __db = __get_db();
1947        __c_node* __cn1 = __db->__find_c_and_lock(this);
1948        __c_node* __cn2 = __db->__find_c(&__c);
1949        for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
1950        {
1951            --__p;
1952            iterator* __i = static_cast<iterator*>((*__p)->__i_);
1953            if (__i->__ptr_ != __c.__end_as_link())
1954            {
1955                __cn1->__add(*__p);
1956                (*__p)->__c_ = __cn1;
1957                if (--__cn2->end_ != __p)
1958                    memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
1959            }
1960        }
1961        __db->unlock();
1962#endif
1963    }
1964}
1965
1966template <class _Tp, class _Alloc>
1967void
1968list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __i)
1969{
1970#if _LIBCPP_DEBUG_LEVEL >= 2
1971    _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1972        "list::splice(iterator, list, iterator) called with first iterator not"
1973        " referring to this list");
1974    _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__i) == &__c,
1975        "list::splice(iterator, list, iterator) called with second iterator not"
1976        " referring to list argument");
1977    _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(&__i),
1978        "list::splice(iterator, list, iterator) called with second iterator not"
1979        " derefereceable");
1980#endif
1981    if (__p.__ptr_ != __i.__ptr_ && __p.__ptr_ != __i.__ptr_->__next_)
1982    {
1983        __link_pointer __f = __i.__ptr_;
1984        base::__unlink_nodes(__f, __f);
1985        __link_nodes(__p.__ptr_, __f, __f);
1986        --__c.__sz();
1987        ++base::__sz();
1988#if _LIBCPP_DEBUG_LEVEL >= 2
1989        __libcpp_db* __db = __get_db();
1990        __c_node* __cn1 = __db->__find_c_and_lock(this);
1991        __c_node* __cn2 = __db->__find_c(&__c);
1992        for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
1993        {
1994            --__p;
1995            iterator* __j = static_cast<iterator*>((*__p)->__i_);
1996            if (__j->__ptr_ == __f)
1997            {
1998                __cn1->__add(*__p);
1999                (*__p)->__c_ = __cn1;
2000                if (--__cn2->end_ != __p)
2001                    memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
2002            }
2003        }
2004        __db->unlock();
2005#endif
2006    }
2007}
2008
2009template <class _Tp, class _Alloc>
2010void
2011list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l)
2012{
2013#if _LIBCPP_DEBUG_LEVEL >= 2
2014    _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2015        "list::splice(iterator, list, iterator, iterator) called with first iterator not"
2016        " referring to this list");
2017    _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__f) == &__c,
2018        "list::splice(iterator, list, iterator, iterator) called with second iterator not"
2019        " referring to list argument");
2020    if (this == &__c)
2021    {
2022        for (const_iterator __i = __f; __i != __l; ++__i)
2023            _LIBCPP_ASSERT(__i != __p,
2024                           "list::splice(iterator, list, iterator, iterator)"
2025                           " called with the first iterator within the range"
2026                           " of the second and third iterators");
2027    }
2028#endif
2029    if (__f != __l)
2030    {
2031        if (this != &__c)
2032        {
2033            size_type __s = _VSTD::distance(__f, __l);
2034            __c.__sz() -= __s;
2035            base::__sz() += __s;
2036        }
2037        __link_pointer __first = __f.__ptr_;
2038        --__l;
2039        __link_pointer __last = __l.__ptr_;
2040        base::__unlink_nodes(__first, __last);
2041        __link_nodes(__p.__ptr_, __first, __last);
2042#if _LIBCPP_DEBUG_LEVEL >= 2
2043        __libcpp_db* __db = __get_db();
2044        __c_node* __cn1 = __db->__find_c_and_lock(this);
2045        __c_node* __cn2 = __db->__find_c(&__c);
2046        for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
2047        {
2048            --__p;
2049            iterator* __j = static_cast<iterator*>((*__p)->__i_);
2050            for (__link_pointer __k = __f.__ptr_;
2051                                          __k != __l.__ptr_; __k = __k->__next_)
2052            {
2053                if (__j->__ptr_ == __k)
2054                {
2055                    __cn1->__add(*__p);
2056                    (*__p)->__c_ = __cn1;
2057                    if (--__cn2->end_ != __p)
2058                        memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
2059                }
2060            }
2061        }
2062        __db->unlock();
2063#endif
2064    }
2065}
2066
2067template <class _Tp, class _Alloc>
2068void
2069list<_Tp, _Alloc>::remove(const value_type& __x)
2070{
2071    list<_Tp, _Alloc> __deleted_nodes; // collect the nodes we're removing
2072    for (const_iterator __i = begin(), __e = end(); __i != __e;)
2073    {
2074        if (*__i == __x)
2075        {
2076            const_iterator __j = _VSTD::next(__i);
2077            for (; __j != __e && *__j == __x; ++__j)
2078                ;
2079            __deleted_nodes.splice(__deleted_nodes.end(), *this, __i, __j);
2080            __i = __j;
2081            if (__i != __e)
2082                ++__i;
2083        }
2084        else
2085            ++__i;
2086    }
2087}
2088
2089template <class _Tp, class _Alloc>
2090template <class _Pred>
2091void
2092list<_Tp, _Alloc>::remove_if(_Pred __pred)
2093{
2094    for (iterator __i = begin(), __e = end(); __i != __e;)
2095    {
2096        if (__pred(*__i))
2097        {
2098            iterator __j = _VSTD::next(__i);
2099            for (; __j != __e && __pred(*__j); ++__j)
2100                ;
2101            __i = erase(__i, __j);
2102            if (__i != __e)
2103                ++__i;
2104        }
2105        else
2106            ++__i;
2107    }
2108}
2109
2110template <class _Tp, class _Alloc>
2111inline _LIBCPP_INLINE_VISIBILITY
2112void
2113list<_Tp, _Alloc>::unique()
2114{
2115    unique(__equal_to<value_type>());
2116}
2117
2118template <class _Tp, class _Alloc>
2119template <class _BinaryPred>
2120void
2121list<_Tp, _Alloc>::unique(_BinaryPred __binary_pred)
2122{
2123    for (iterator __i = begin(), __e = end(); __i != __e;)
2124    {
2125        iterator __j = _VSTD::next(__i);
2126        for (; __j != __e && __binary_pred(*__i, *__j); ++__j)
2127            ;
2128        if (++__i != __j)
2129            __i = erase(__i, __j);
2130    }
2131}
2132
2133template <class _Tp, class _Alloc>
2134inline _LIBCPP_INLINE_VISIBILITY
2135void
2136list<_Tp, _Alloc>::merge(list& __c)
2137{
2138    merge(__c, __less<value_type>());
2139}
2140
2141template <class _Tp, class _Alloc>
2142template <class _Comp>
2143void
2144list<_Tp, _Alloc>::merge(list& __c, _Comp __comp)
2145{
2146    if (this != &__c)
2147    {
2148        iterator __f1 = begin();
2149        iterator __e1 = end();
2150        iterator __f2 = __c.begin();
2151        iterator __e2 = __c.end();
2152        while (__f1 != __e1 && __f2 != __e2)
2153        {
2154            if (__comp(*__f2, *__f1))
2155            {
2156                size_type __ds = 1;
2157                iterator __m2 = _VSTD::next(__f2);
2158                for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2, ++__ds)
2159                    ;
2160                base::__sz() += __ds;
2161                __c.__sz() -= __ds;
2162                __link_pointer __f = __f2.__ptr_;
2163                __link_pointer __l = __m2.__ptr_->__prev_;
2164                __f2 = __m2;
2165                base::__unlink_nodes(__f, __l);
2166                __m2 = _VSTD::next(__f1);
2167                __link_nodes(__f1.__ptr_, __f, __l);
2168                __f1 = __m2;
2169            }
2170            else
2171                ++__f1;
2172        }
2173        splice(__e1, __c);
2174#if _LIBCPP_DEBUG_LEVEL >= 2
2175        __libcpp_db* __db = __get_db();
2176        __c_node* __cn1 = __db->__find_c_and_lock(this);
2177        __c_node* __cn2 = __db->__find_c(&__c);
2178        for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
2179        {
2180            --__p;
2181            iterator* __i = static_cast<iterator*>((*__p)->__i_);
2182            if (__i->__ptr_ != __c.__end_as_link())
2183            {
2184                __cn1->__add(*__p);
2185                (*__p)->__c_ = __cn1;
2186                if (--__cn2->end_ != __p)
2187                    memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*));
2188            }
2189        }
2190        __db->unlock();
2191#endif
2192    }
2193}
2194
2195template <class _Tp, class _Alloc>
2196inline _LIBCPP_INLINE_VISIBILITY
2197void
2198list<_Tp, _Alloc>::sort()
2199{
2200    sort(__less<value_type>());
2201}
2202
2203template <class _Tp, class _Alloc>
2204template <class _Comp>
2205inline _LIBCPP_INLINE_VISIBILITY
2206void
2207list<_Tp, _Alloc>::sort(_Comp __comp)
2208{
2209    __sort(begin(), end(), base::__sz(), __comp);
2210}
2211
2212template <class _Tp, class _Alloc>
2213template <class _Comp>
2214typename list<_Tp, _Alloc>::iterator
2215list<_Tp, _Alloc>::__sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp)
2216{
2217    switch (__n)
2218    {
2219    case 0:
2220    case 1:
2221        return __f1;
2222    case 2:
2223        if (__comp(*--__e2, *__f1))
2224        {
2225            __link_pointer __f = __e2.__ptr_;
2226            base::__unlink_nodes(__f, __f);
2227            __link_nodes(__f1.__ptr_, __f, __f);
2228            return __e2;
2229        }
2230        return __f1;
2231    }
2232    size_type __n2 = __n / 2;
2233    iterator __e1 = _VSTD::next(__f1, __n2);
2234    iterator  __r = __f1 = __sort(__f1, __e1, __n2, __comp);
2235    iterator __f2 = __e1 = __sort(__e1, __e2, __n - __n2, __comp);
2236    if (__comp(*__f2, *__f1))
2237    {
2238        iterator __m2 = _VSTD::next(__f2);
2239        for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2)
2240            ;
2241        __link_pointer __f = __f2.__ptr_;
2242        __link_pointer __l = __m2.__ptr_->__prev_;
2243        __r = __f2;
2244        __e1 = __f2 = __m2;
2245        base::__unlink_nodes(__f, __l);
2246        __m2 = _VSTD::next(__f1);
2247        __link_nodes(__f1.__ptr_, __f, __l);
2248        __f1 = __m2;
2249    }
2250    else
2251        ++__f1;
2252    while (__f1 != __e1 && __f2 != __e2)
2253    {
2254        if (__comp(*__f2, *__f1))
2255        {
2256            iterator __m2 = _VSTD::next(__f2);
2257            for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2)
2258                ;
2259            __link_pointer __f = __f2.__ptr_;
2260            __link_pointer __l = __m2.__ptr_->__prev_;
2261            if (__e1 == __f2)
2262                __e1 = __m2;
2263            __f2 = __m2;
2264            base::__unlink_nodes(__f, __l);
2265            __m2 = _VSTD::next(__f1);
2266            __link_nodes(__f1.__ptr_, __f, __l);
2267            __f1 = __m2;
2268        }
2269        else
2270            ++__f1;
2271    }
2272    return __r;
2273}
2274
2275template <class _Tp, class _Alloc>
2276void
2277list<_Tp, _Alloc>::reverse() _NOEXCEPT
2278{
2279    if (base::__sz() > 1)
2280    {
2281        iterator __e = end();
2282        for (iterator __i = begin(); __i.__ptr_ != __e.__ptr_;)
2283        {
2284            _VSTD::swap(__i.__ptr_->__prev_, __i.__ptr_->__next_);
2285            __i.__ptr_ = __i.__ptr_->__prev_;
2286        }
2287        _VSTD::swap(__e.__ptr_->__prev_, __e.__ptr_->__next_);
2288    }
2289}
2290
2291template <class _Tp, class _Alloc>
2292bool
2293list<_Tp, _Alloc>::__invariants() const
2294{
2295    return size() == _VSTD::distance(begin(), end());
2296}
2297
2298#if _LIBCPP_DEBUG_LEVEL >= 2
2299
2300template <class _Tp, class _Alloc>
2301bool
2302list<_Tp, _Alloc>::__dereferenceable(const const_iterator* __i) const
2303{
2304    return __i->__ptr_ != this->__end_as_link();
2305}
2306
2307template <class _Tp, class _Alloc>
2308bool
2309list<_Tp, _Alloc>::__decrementable(const const_iterator* __i) const
2310{
2311    return !empty() &&  __i->__ptr_ != base::__end_.__next_;
2312}
2313
2314template <class _Tp, class _Alloc>
2315bool
2316list<_Tp, _Alloc>::__addable(const const_iterator* __i, ptrdiff_t __n) const
2317{
2318    return false;
2319}
2320
2321template <class _Tp, class _Alloc>
2322bool
2323list<_Tp, _Alloc>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
2324{
2325    return false;
2326}
2327
2328#endif  // _LIBCPP_DEBUG_LEVEL >= 2
2329
2330template <class _Tp, class _Alloc>
2331inline _LIBCPP_INLINE_VISIBILITY
2332bool
2333operator==(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2334{
2335    return __x.size() == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
2336}
2337
2338template <class _Tp, class _Alloc>
2339inline _LIBCPP_INLINE_VISIBILITY
2340bool
2341operator< (const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2342{
2343    return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
2344}
2345
2346template <class _Tp, class _Alloc>
2347inline _LIBCPP_INLINE_VISIBILITY
2348bool
2349operator!=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2350{
2351    return !(__x == __y);
2352}
2353
2354template <class _Tp, class _Alloc>
2355inline _LIBCPP_INLINE_VISIBILITY
2356bool
2357operator> (const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2358{
2359    return __y < __x;
2360}
2361
2362template <class _Tp, class _Alloc>
2363inline _LIBCPP_INLINE_VISIBILITY
2364bool
2365operator>=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2366{
2367    return !(__x < __y);
2368}
2369
2370template <class _Tp, class _Alloc>
2371inline _LIBCPP_INLINE_VISIBILITY
2372bool
2373operator<=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2374{
2375    return !(__y < __x);
2376}
2377
2378template <class _Tp, class _Alloc>
2379inline _LIBCPP_INLINE_VISIBILITY
2380void
2381swap(list<_Tp, _Alloc>& __x, list<_Tp, _Alloc>& __y)
2382    _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
2383{
2384    __x.swap(__y);
2385}
2386
2387_LIBCPP_END_NAMESPACE_STD
2388
2389#endif  // _LIBCPP_LIST
2390