1// -*- C++ -*-
2//===------------------------------ vector --------------------------------===//
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_VECTOR
12#define _LIBCPP_VECTOR
13
14/*
15    vector synopsis
16
17namespace std
18{
19
20template <class T, class Allocator = allocator<T> >
21class vector
22{
23public:
24    typedef T                                        value_type;
25    typedef Allocator                                allocator_type;
26    typedef typename allocator_type::reference       reference;
27    typedef typename allocator_type::const_reference const_reference;
28    typedef implementation-defined                   iterator;
29    typedef implementation-defined                   const_iterator;
30    typedef typename allocator_type::size_type       size_type;
31    typedef typename allocator_type::difference_type difference_type;
32    typedef typename allocator_type::pointer         pointer;
33    typedef typename allocator_type::const_pointer   const_pointer;
34    typedef std::reverse_iterator<iterator>          reverse_iterator;
35    typedef std::reverse_iterator<const_iterator>    const_reverse_iterator;
36
37    vector()
38        noexcept(is_nothrow_default_constructible<allocator_type>::value);
39    explicit vector(const allocator_type&);
40    explicit vector(size_type n);
41    explicit vector(size_type n, const allocator_type&); // C++14
42    vector(size_type n, const value_type& value, const allocator_type& = allocator_type());
43    template <class InputIterator>
44        vector(InputIterator first, InputIterator last, const allocator_type& = allocator_type());
45    vector(const vector& x);
46    vector(vector&& x)
47        noexcept(is_nothrow_move_constructible<allocator_type>::value);
48    vector(initializer_list<value_type> il);
49    vector(initializer_list<value_type> il, const allocator_type& a);
50    ~vector();
51    vector& operator=(const vector& x);
52    vector& operator=(vector&& x)
53        noexcept(
54             allocator_type::propagate_on_container_move_assignment::value &&
55             is_nothrow_move_assignable<allocator_type>::value);
56    vector& operator=(initializer_list<value_type> il);
57    template <class InputIterator>
58        void assign(InputIterator first, InputIterator last);
59    void assign(size_type n, const value_type& u);
60    void assign(initializer_list<value_type> il);
61
62    allocator_type get_allocator() const noexcept;
63
64    iterator               begin() noexcept;
65    const_iterator         begin()   const noexcept;
66    iterator               end() noexcept;
67    const_iterator         end()     const noexcept;
68
69    reverse_iterator       rbegin() noexcept;
70    const_reverse_iterator rbegin()  const noexcept;
71    reverse_iterator       rend() noexcept;
72    const_reverse_iterator rend()    const noexcept;
73
74    const_iterator         cbegin()  const noexcept;
75    const_iterator         cend()    const noexcept;
76    const_reverse_iterator crbegin() const noexcept;
77    const_reverse_iterator crend()   const noexcept;
78
79    size_type size() const noexcept;
80    size_type max_size() const noexcept;
81    size_type capacity() const noexcept;
82    bool empty() const noexcept;
83    void reserve(size_type n);
84    void shrink_to_fit() noexcept;
85
86    reference       operator[](size_type n);
87    const_reference operator[](size_type n) const;
88    reference       at(size_type n);
89    const_reference at(size_type n) const;
90
91    reference       front();
92    const_reference front() const;
93    reference       back();
94    const_reference back() const;
95
96    value_type*       data() noexcept;
97    const value_type* data() const noexcept;
98
99    void push_back(const value_type& x);
100    void push_back(value_type&& x);
101    template <class... Args>
102        void emplace_back(Args&&... args);
103    void pop_back();
104
105    template <class... Args> iterator emplace(const_iterator position, Args&&... args);
106    iterator insert(const_iterator position, const value_type& x);
107    iterator insert(const_iterator position, value_type&& x);
108    iterator insert(const_iterator position, size_type n, const value_type& x);
109    template <class InputIterator>
110        iterator insert(const_iterator position, InputIterator first, InputIterator last);
111    iterator insert(const_iterator position, initializer_list<value_type> il);
112
113    iterator erase(const_iterator position);
114    iterator erase(const_iterator first, const_iterator last);
115
116    void clear() noexcept;
117
118    void resize(size_type sz);
119    void resize(size_type sz, const value_type& c);
120
121    void swap(vector&)
122        noexcept(!allocator_type::propagate_on_container_swap::value ||
123                 __is_nothrow_swappable<allocator_type>::value);
124
125    bool __invariants() const;
126};
127
128template <class Allocator = allocator<T> >
129class vector<bool, Allocator>
130{
131public:
132    typedef bool                                     value_type;
133    typedef Allocator                                allocator_type;
134    typedef implementation-defined                   iterator;
135    typedef implementation-defined                   const_iterator;
136    typedef typename allocator_type::size_type       size_type;
137    typedef typename allocator_type::difference_type difference_type;
138    typedef iterator                                 pointer;
139    typedef const_iterator                           const_pointer;
140    typedef std::reverse_iterator<iterator>          reverse_iterator;
141    typedef std::reverse_iterator<const_iterator>    const_reverse_iterator;
142
143    class reference
144    {
145    public:
146        reference(const reference&) noexcept;
147        operator bool() const noexcept;
148        reference& operator=(const bool x) noexcept;
149        reference& operator=(const reference& x) noexcept;
150        iterator operator&() const noexcept;
151        void flip() noexcept;
152    };
153
154    class const_reference
155    {
156    public:
157        const_reference(const reference&) noexcept;
158        operator bool() const noexcept;
159        const_iterator operator&() const noexcept;
160    };
161
162    vector()
163        noexcept(is_nothrow_default_constructible<allocator_type>::value);
164    explicit vector(const allocator_type&);
165    explicit vector(size_type n, const allocator_type& a = allocator_type()); // C++14
166    vector(size_type n, const value_type& value, const allocator_type& = allocator_type());
167    template <class InputIterator>
168        vector(InputIterator first, InputIterator last, const allocator_type& = allocator_type());
169    vector(const vector& x);
170    vector(vector&& x)
171        noexcept(is_nothrow_move_constructible<allocator_type>::value);
172    vector(initializer_list<value_type> il);
173    vector(initializer_list<value_type> il, const allocator_type& a);
174    ~vector();
175    vector& operator=(const vector& x);
176    vector& operator=(vector&& x)
177        noexcept(
178             allocator_type::propagate_on_container_move_assignment::value &&
179             is_nothrow_move_assignable<allocator_type>::value);
180    vector& operator=(initializer_list<value_type> il);
181    template <class InputIterator>
182        void assign(InputIterator first, InputIterator last);
183    void assign(size_type n, const value_type& u);
184    void assign(initializer_list<value_type> il);
185
186    allocator_type get_allocator() const noexcept;
187
188    iterator               begin() noexcept;
189    const_iterator         begin()   const noexcept;
190    iterator               end() noexcept;
191    const_iterator         end()     const noexcept;
192
193    reverse_iterator       rbegin() noexcept;
194    const_reverse_iterator rbegin()  const noexcept;
195    reverse_iterator       rend() noexcept;
196    const_reverse_iterator rend()    const noexcept;
197
198    const_iterator         cbegin()  const noexcept;
199    const_iterator         cend()    const noexcept;
200    const_reverse_iterator crbegin() const noexcept;
201    const_reverse_iterator crend()   const noexcept;
202
203    size_type size() const noexcept;
204    size_type max_size() const noexcept;
205    size_type capacity() const noexcept;
206    bool empty() const noexcept;
207    void reserve(size_type n);
208    void shrink_to_fit() noexcept;
209
210    reference       operator[](size_type n);
211    const_reference operator[](size_type n) const;
212    reference       at(size_type n);
213    const_reference at(size_type n) const;
214
215    reference       front();
216    const_reference front() const;
217    reference       back();
218    const_reference back() const;
219
220    void push_back(const value_type& x);
221    template <class... Args> void emplace_back(Args&&... args);  // C++14
222    void pop_back();
223
224    template <class... Args> iterator emplace(const_iterator position, Args&&... args);  // C++14
225    iterator insert(const_iterator position, const value_type& x);
226    iterator insert(const_iterator position, size_type n, const value_type& x);
227    template <class InputIterator>
228        iterator insert(const_iterator position, InputIterator first, InputIterator last);
229    iterator insert(const_iterator position, initializer_list<value_type> il);
230
231    iterator erase(const_iterator position);
232    iterator erase(const_iterator first, const_iterator last);
233
234    void clear() noexcept;
235
236    void resize(size_type sz);
237    void resize(size_type sz, value_type x);
238
239    void swap(vector&)
240        noexcept(!allocator_type::propagate_on_container_swap::value ||
241                 __is_nothrow_swappable<allocator_type>::value);
242    void flip() noexcept;
243
244    bool __invariants() const;
245};
246
247template <class Allocator> struct hash<std::vector<bool, Allocator>>;
248
249template <class T, class Allocator> bool operator==(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
250template <class T, class Allocator> bool operator< (const vector<T,Allocator>& x, const vector<T,Allocator>& y);
251template <class T, class Allocator> bool operator!=(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
252template <class T, class Allocator> bool operator> (const vector<T,Allocator>& x, const vector<T,Allocator>& y);
253template <class T, class Allocator> bool operator>=(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
254template <class T, class Allocator> bool operator<=(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
255
256template <class T, class Allocator>
257void swap(vector<T,Allocator>& x, vector<T,Allocator>& y)
258    noexcept(noexcept(x.swap(y)));
259
260}  // std
261
262*/
263
264#include <__config>
265#include <__bit_reference>
266#include <type_traits>
267#include <climits>
268#include <limits>
269#include <initializer_list>
270#include <memory>
271#include <stdexcept>
272#include <algorithm>
273#include <cstring>
274#include <__split_buffer>
275#include <__functional_base>
276
277#include <__undef_min_max>
278
279#ifdef _LIBCPP_DEBUG
280#   include <__debug>
281#else
282#   define _LIBCPP_ASSERT(x, m) ((void)0)
283#endif
284
285#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
286#pragma GCC system_header
287#endif
288
289_LIBCPP_BEGIN_NAMESPACE_STD
290
291template <bool>
292class __vector_base_common
293{
294protected:
295    _LIBCPP_ALWAYS_INLINE __vector_base_common() {}
296    void __throw_length_error() const;
297    void __throw_out_of_range() const;
298};
299
300template <bool __b>
301void
302__vector_base_common<__b>::__throw_length_error() const
303{
304#ifndef _LIBCPP_NO_EXCEPTIONS
305    throw length_error("vector");
306#else
307    assert(!"vector length_error");
308#endif
309}
310
311template <bool __b>
312void
313__vector_base_common<__b>::__throw_out_of_range() const
314{
315#ifndef _LIBCPP_NO_EXCEPTIONS
316    throw out_of_range("vector");
317#else
318    assert(!"vector out_of_range");
319#endif
320}
321
322#ifdef _LIBCPP_MSVC
323#pragma warning( push )
324#pragma warning( disable: 4231 )
325#endif // _LIBCPP_MSVC
326_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_TYPE_VIS __vector_base_common<true>)
327#ifdef _LIBCPP_MSVC
328#pragma warning( pop )
329#endif // _LIBCPP_MSVC
330
331template <class _Tp, class _Allocator>
332class __vector_base
333    : protected __vector_base_common<true>
334{
335protected:
336    typedef _Tp                                      value_type;
337    typedef _Allocator                               allocator_type;
338    typedef allocator_traits<allocator_type>         __alloc_traits;
339    typedef value_type&                              reference;
340    typedef const value_type&                        const_reference;
341    typedef typename __alloc_traits::size_type       size_type;
342    typedef typename __alloc_traits::difference_type difference_type;
343    typedef typename __alloc_traits::pointer         pointer;
344    typedef typename __alloc_traits::const_pointer   const_pointer;
345    typedef pointer                                  iterator;
346    typedef const_pointer                            const_iterator;
347
348    pointer                                         __begin_;
349    pointer                                         __end_;
350    __compressed_pair<pointer, allocator_type> __end_cap_;
351
352    _LIBCPP_INLINE_VISIBILITY
353    allocator_type& __alloc() _NOEXCEPT
354        {return __end_cap_.second();}
355    _LIBCPP_INLINE_VISIBILITY
356    const allocator_type& __alloc() const _NOEXCEPT
357        {return __end_cap_.second();}
358    _LIBCPP_INLINE_VISIBILITY
359    pointer& __end_cap() _NOEXCEPT
360        {return __end_cap_.first();}
361    _LIBCPP_INLINE_VISIBILITY
362    const pointer& __end_cap() const _NOEXCEPT
363        {return __end_cap_.first();}
364
365    _LIBCPP_INLINE_VISIBILITY
366    __vector_base()
367        _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
368    _LIBCPP_INLINE_VISIBILITY __vector_base(const allocator_type& __a);
369    ~__vector_base();
370
371    _LIBCPP_INLINE_VISIBILITY
372    void clear() _NOEXCEPT {__destruct_at_end(__begin_);}
373    _LIBCPP_INLINE_VISIBILITY
374    size_type capacity() const _NOEXCEPT
375        {return static_cast<size_type>(__end_cap() - __begin_);}
376
377    _LIBCPP_INLINE_VISIBILITY
378    void __destruct_at_end(pointer __new_last) _NOEXCEPT;
379
380    _LIBCPP_INLINE_VISIBILITY
381    void __copy_assign_alloc(const __vector_base& __c)
382        {__copy_assign_alloc(__c, integral_constant<bool,
383                      __alloc_traits::propagate_on_container_copy_assignment::value>());}
384
385    _LIBCPP_INLINE_VISIBILITY
386    void __move_assign_alloc(__vector_base& __c)
387        _NOEXCEPT_(
388            !__alloc_traits::propagate_on_container_move_assignment::value ||
389            is_nothrow_move_assignable<allocator_type>::value)
390        {__move_assign_alloc(__c, integral_constant<bool,
391                      __alloc_traits::propagate_on_container_move_assignment::value>());}
392
393    _LIBCPP_INLINE_VISIBILITY
394    static void __swap_alloc(allocator_type& __x, allocator_type& __y)
395        _NOEXCEPT_(
396            !__alloc_traits::propagate_on_container_swap::value ||
397            __is_nothrow_swappable<allocator_type>::value)
398        {__swap_alloc(__x, __y, integral_constant<bool,
399                      __alloc_traits::propagate_on_container_swap::value>());}
400private:
401    _LIBCPP_INLINE_VISIBILITY
402    void __copy_assign_alloc(const __vector_base& __c, true_type)
403        {
404            if (__alloc() != __c.__alloc())
405            {
406                clear();
407                __alloc_traits::deallocate(__alloc(), __begin_, capacity());
408                __begin_ = __end_ = __end_cap() = nullptr;
409            }
410            __alloc() = __c.__alloc();
411        }
412
413    _LIBCPP_INLINE_VISIBILITY
414    void __copy_assign_alloc(const __vector_base&, false_type)
415        {}
416
417    _LIBCPP_INLINE_VISIBILITY
418    void __move_assign_alloc(__vector_base& __c, true_type)
419        _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
420        {
421            __alloc() = _VSTD::move(__c.__alloc());
422        }
423
424    _LIBCPP_INLINE_VISIBILITY
425    void __move_assign_alloc(__vector_base&, false_type)
426        _NOEXCEPT
427        {}
428
429    _LIBCPP_INLINE_VISIBILITY
430    static void __swap_alloc(allocator_type& __x, allocator_type& __y, true_type)
431        _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
432        {
433            using _VSTD::swap;
434            swap(__x, __y);
435        }
436    _LIBCPP_INLINE_VISIBILITY
437    static void __swap_alloc(allocator_type&, allocator_type&, false_type)
438        _NOEXCEPT
439        {}
440};
441
442template <class _Tp, class _Allocator>
443inline _LIBCPP_INLINE_VISIBILITY
444void
445__vector_base<_Tp, _Allocator>::__destruct_at_end(pointer __new_last) _NOEXCEPT
446{
447    while (__new_last != __end_)
448        __alloc_traits::destroy(__alloc(), _VSTD::__to_raw_pointer(--__end_));
449}
450
451template <class _Tp, class _Allocator>
452inline _LIBCPP_INLINE_VISIBILITY
453__vector_base<_Tp, _Allocator>::__vector_base()
454        _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
455    : __begin_(nullptr),
456      __end_(nullptr),
457      __end_cap_(nullptr)
458{
459}
460
461template <class _Tp, class _Allocator>
462inline _LIBCPP_INLINE_VISIBILITY
463__vector_base<_Tp, _Allocator>::__vector_base(const allocator_type& __a)
464    : __begin_(nullptr),
465      __end_(nullptr),
466      __end_cap_(nullptr, __a)
467{
468}
469
470template <class _Tp, class _Allocator>
471__vector_base<_Tp, _Allocator>::~__vector_base()
472{
473    if (__begin_ != nullptr)
474    {
475        clear();
476        __alloc_traits::deallocate(__alloc(), __begin_, capacity());
477    }
478}
479
480template <class _Tp, class _Allocator = allocator<_Tp> >
481class _LIBCPP_TYPE_VIS_ONLY vector
482    : private __vector_base<_Tp, _Allocator>
483{
484private:
485    typedef __vector_base<_Tp, _Allocator>           __base;
486    typedef allocator<_Tp>                           __default_allocator_type;
487public:
488    typedef vector                                   __self;
489    typedef _Tp                                      value_type;
490    typedef _Allocator                               allocator_type;
491    typedef typename __base::__alloc_traits          __alloc_traits;
492    typedef typename __base::reference               reference;
493    typedef typename __base::const_reference         const_reference;
494    typedef typename __base::size_type               size_type;
495    typedef typename __base::difference_type         difference_type;
496    typedef typename __base::pointer                 pointer;
497    typedef typename __base::const_pointer           const_pointer;
498    typedef __wrap_iter<pointer>                     iterator;
499    typedef __wrap_iter<const_pointer>               const_iterator;
500    typedef _VSTD::reverse_iterator<iterator>         reverse_iterator;
501    typedef _VSTD::reverse_iterator<const_iterator>   const_reverse_iterator;
502
503    static_assert((is_same<typename allocator_type::value_type, value_type>::value),
504                  "Allocator::value_type must be same type as value_type");
505
506    _LIBCPP_INLINE_VISIBILITY
507    vector()
508        _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
509        {
510#if _LIBCPP_DEBUG_LEVEL >= 2
511            __get_db()->__insert_c(this);
512#endif
513        }
514    _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
515        : __base(__a)
516    {
517#if _LIBCPP_DEBUG_LEVEL >= 2
518        __get_db()->__insert_c(this);
519#endif
520    }
521    explicit vector(size_type __n);
522#if _LIBCPP_STD_VER > 11
523    explicit vector(size_type __n, const allocator_type& __a);
524#endif
525    vector(size_type __n, const_reference __x);
526    vector(size_type __n, const_reference __x, const allocator_type& __a);
527    template <class _InputIterator>
528        vector(_InputIterator __first,
529               typename enable_if<__is_input_iterator  <_InputIterator>::value &&
530                                 !__is_forward_iterator<_InputIterator>::value &&
531                                 is_constructible<
532                                    value_type,
533                                    typename iterator_traits<_InputIterator>::reference>::value,
534                                 _InputIterator>::type __last);
535    template <class _InputIterator>
536        vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
537               typename enable_if<__is_input_iterator  <_InputIterator>::value &&
538                                 !__is_forward_iterator<_InputIterator>::value &&
539                                 is_constructible<
540                                    value_type,
541                                    typename iterator_traits<_InputIterator>::reference>::value>::type* = 0);
542    template <class _ForwardIterator>
543        vector(_ForwardIterator __first,
544               typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
545                                 is_constructible<
546                                    value_type,
547                                    typename iterator_traits<_ForwardIterator>::reference>::value,
548                                 _ForwardIterator>::type __last);
549    template <class _ForwardIterator>
550        vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
551               typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
552                                 is_constructible<
553                                    value_type,
554                                    typename iterator_traits<_ForwardIterator>::reference>::value>::type* = 0);
555#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
556    _LIBCPP_INLINE_VISIBILITY
557    vector(initializer_list<value_type> __il);
558    _LIBCPP_INLINE_VISIBILITY
559    vector(initializer_list<value_type> __il, const allocator_type& __a);
560#endif  // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
561#if _LIBCPP_DEBUG_LEVEL >= 2
562    _LIBCPP_INLINE_VISIBILITY
563    ~vector()
564    {
565        __get_db()->__erase_c(this);
566    }
567#endif
568
569    vector(const vector& __x);
570    vector(const vector& __x, const allocator_type& __a);
571    _LIBCPP_INLINE_VISIBILITY
572    vector& operator=(const vector& __x);
573#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
574    _LIBCPP_INLINE_VISIBILITY
575    vector(vector&& __x)
576        _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
577    _LIBCPP_INLINE_VISIBILITY
578    vector(vector&& __x, const allocator_type& __a);
579    _LIBCPP_INLINE_VISIBILITY
580    vector& operator=(vector&& __x)
581        _NOEXCEPT_(
582             __alloc_traits::propagate_on_container_move_assignment::value &&
583             is_nothrow_move_assignable<allocator_type>::value);
584#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
585#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
586    _LIBCPP_INLINE_VISIBILITY
587    vector& operator=(initializer_list<value_type> __il)
588        {assign(__il.begin(), __il.end()); return *this;}
589#endif  // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
590
591    template <class _InputIterator>
592        typename enable_if
593        <
594             __is_input_iterator  <_InputIterator>::value &&
595            !__is_forward_iterator<_InputIterator>::value &&
596            is_constructible<
597                 value_type,
598                 typename iterator_traits<_InputIterator>::reference>::value,
599            void
600        >::type
601        assign(_InputIterator __first, _InputIterator __last);
602    template <class _ForwardIterator>
603        typename enable_if
604        <
605            __is_forward_iterator<_ForwardIterator>::value &&
606            is_constructible<
607                 value_type,
608                 typename iterator_traits<_ForwardIterator>::reference>::value,
609            void
610        >::type
611        assign(_ForwardIterator __first, _ForwardIterator __last);
612
613    void assign(size_type __n, const_reference __u);
614#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
615    _LIBCPP_INLINE_VISIBILITY
616    void assign(initializer_list<value_type> __il)
617        {assign(__il.begin(), __il.end());}
618#endif  // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
619
620    _LIBCPP_INLINE_VISIBILITY
621    allocator_type get_allocator() const _NOEXCEPT
622        {return this->__alloc();}
623
624    _LIBCPP_INLINE_VISIBILITY iterator               begin() _NOEXCEPT;
625    _LIBCPP_INLINE_VISIBILITY const_iterator         begin()   const _NOEXCEPT;
626    _LIBCPP_INLINE_VISIBILITY iterator               end() _NOEXCEPT;
627    _LIBCPP_INLINE_VISIBILITY const_iterator         end()     const _NOEXCEPT;
628
629    _LIBCPP_INLINE_VISIBILITY
630    reverse_iterator       rbegin() _NOEXCEPT
631        {return       reverse_iterator(end());}
632    _LIBCPP_INLINE_VISIBILITY
633    const_reverse_iterator rbegin()  const _NOEXCEPT
634        {return const_reverse_iterator(end());}
635    _LIBCPP_INLINE_VISIBILITY
636    reverse_iterator       rend() _NOEXCEPT
637        {return       reverse_iterator(begin());}
638    _LIBCPP_INLINE_VISIBILITY
639    const_reverse_iterator rend()    const _NOEXCEPT
640        {return const_reverse_iterator(begin());}
641
642    _LIBCPP_INLINE_VISIBILITY
643    const_iterator         cbegin()  const _NOEXCEPT
644        {return begin();}
645    _LIBCPP_INLINE_VISIBILITY
646    const_iterator         cend()    const _NOEXCEPT
647        {return end();}
648    _LIBCPP_INLINE_VISIBILITY
649    const_reverse_iterator crbegin() const _NOEXCEPT
650        {return rbegin();}
651    _LIBCPP_INLINE_VISIBILITY
652    const_reverse_iterator crend()   const _NOEXCEPT
653        {return rend();}
654
655    _LIBCPP_INLINE_VISIBILITY
656    size_type size() const _NOEXCEPT
657        {return static_cast<size_type>(this->__end_ - this->__begin_);}
658    _LIBCPP_INLINE_VISIBILITY
659    size_type capacity() const _NOEXCEPT
660        {return __base::capacity();}
661    _LIBCPP_INLINE_VISIBILITY
662    bool empty() const _NOEXCEPT
663        {return this->__begin_ == this->__end_;}
664    size_type max_size() const _NOEXCEPT;
665    void reserve(size_type __n);
666    void shrink_to_fit() _NOEXCEPT;
667
668    _LIBCPP_INLINE_VISIBILITY reference       operator[](size_type __n);
669    _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const;
670    reference       at(size_type __n);
671    const_reference at(size_type __n) const;
672
673    _LIBCPP_INLINE_VISIBILITY reference       front()
674    {
675        _LIBCPP_ASSERT(!empty(), "front() called for empty vector");
676        return *this->__begin_;
677    }
678    _LIBCPP_INLINE_VISIBILITY const_reference front() const
679    {
680        _LIBCPP_ASSERT(!empty(), "front() called for empty vector");
681        return *this->__begin_;
682    }
683    _LIBCPP_INLINE_VISIBILITY reference       back()
684    {
685        _LIBCPP_ASSERT(!empty(), "back() called for empty vector");
686        return *(this->__end_ - 1);
687    }
688    _LIBCPP_INLINE_VISIBILITY const_reference back()  const
689    {
690        _LIBCPP_ASSERT(!empty(), "back() called for empty vector");
691        return *(this->__end_ - 1);
692    }
693
694    _LIBCPP_INLINE_VISIBILITY
695    value_type*       data() _NOEXCEPT
696        {return _VSTD::__to_raw_pointer(this->__begin_);}
697    _LIBCPP_INLINE_VISIBILITY
698    const value_type* data() const _NOEXCEPT
699        {return _VSTD::__to_raw_pointer(this->__begin_);}
700
701    _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
702#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
703    _LIBCPP_INLINE_VISIBILITY void push_back(value_type&& __x);
704#ifndef _LIBCPP_HAS_NO_VARIADICS
705    template <class... _Args>
706        void emplace_back(_Args&&... __args);
707#endif  // _LIBCPP_HAS_NO_VARIADICS
708#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
709    void pop_back();
710
711    iterator insert(const_iterator __position, const_reference __x);
712#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
713    iterator insert(const_iterator __position, value_type&& __x);
714#ifndef _LIBCPP_HAS_NO_VARIADICS
715    template <class... _Args>
716        iterator emplace(const_iterator __position, _Args&&... __args);
717#endif  // _LIBCPP_HAS_NO_VARIADICS
718#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
719    iterator insert(const_iterator __position, size_type __n, const_reference __x);
720    template <class _InputIterator>
721        typename enable_if
722        <
723             __is_input_iterator  <_InputIterator>::value &&
724            !__is_forward_iterator<_InputIterator>::value &&
725            is_constructible<
726                 value_type,
727                 typename iterator_traits<_InputIterator>::reference>::value,
728            iterator
729        >::type
730        insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
731    template <class _ForwardIterator>
732        typename enable_if
733        <
734            __is_forward_iterator<_ForwardIterator>::value &&
735            is_constructible<
736                 value_type,
737                 typename iterator_traits<_ForwardIterator>::reference>::value,
738            iterator
739        >::type
740        insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
741#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
742    _LIBCPP_INLINE_VISIBILITY
743    iterator insert(const_iterator __position, initializer_list<value_type> __il)
744        {return insert(__position, __il.begin(), __il.end());}
745#endif  // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
746
747    _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
748    iterator erase(const_iterator __first, const_iterator __last);
749
750    _LIBCPP_INLINE_VISIBILITY
751    void clear() _NOEXCEPT
752    {
753        size_type __old_size = size();
754        __base::clear();
755        __annotate_shrink(__old_size);
756        __invalidate_all_iterators();
757    }
758
759    void resize(size_type __sz);
760    void resize(size_type __sz, const_reference __x);
761
762    void swap(vector&)
763        _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
764                   __is_nothrow_swappable<allocator_type>::value);
765
766    bool __invariants() const;
767
768#if _LIBCPP_DEBUG_LEVEL >= 2
769
770    bool __dereferenceable(const const_iterator* __i) const;
771    bool __decrementable(const const_iterator* __i) const;
772    bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
773    bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
774
775#endif  // _LIBCPP_DEBUG_LEVEL >= 2
776
777private:
778    _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
779    void allocate(size_type __n);
780    void deallocate() _NOEXCEPT;
781    _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
782    void __construct_at_end(size_type __n);
783    void __construct_at_end(size_type __n, const_reference __x);
784    template <class _ForwardIterator>
785        typename enable_if
786        <
787            __is_forward_iterator<_ForwardIterator>::value,
788            void
789        >::type
790        __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
791    void __move_construct_at_end(pointer __first, pointer __last);
792    void __append(size_type __n);
793    void __append(size_type __n, const_reference __x);
794    _LIBCPP_INLINE_VISIBILITY
795    iterator       __make_iter(pointer __p) _NOEXCEPT;
796    _LIBCPP_INLINE_VISIBILITY
797    const_iterator __make_iter(const_pointer __p) const _NOEXCEPT;
798    void __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v);
799    pointer __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p);
800    void __move_range(pointer __from_s, pointer __from_e, pointer __to);
801    void __move_assign(vector& __c, true_type)
802        _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
803    void __move_assign(vector& __c, false_type);
804    _LIBCPP_INLINE_VISIBILITY
805    void __destruct_at_end(pointer __new_last) _NOEXCEPT
806    {
807#if _LIBCPP_DEBUG_LEVEL >= 2
808        __c_node* __c = __get_db()->__find_c_and_lock(this);
809        for (__i_node** __p = __c->end_; __p != __c->beg_; )
810        {
811            --__p;
812            const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
813            if (__i->base() > __new_last)
814            {
815                (*__p)->__c_ = nullptr;
816                if (--__c->end_ != __p)
817                    memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
818            }
819        }
820        __get_db()->unlock();
821#endif
822        size_type __old_size = size();
823        __base::__destruct_at_end(__new_last);
824        __annotate_shrink(__old_size);
825    }
826    template <class _Up>
827        void
828#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
829        __push_back_slow_path(_Up&& __x);
830#else
831        __push_back_slow_path(_Up& __x);
832#endif
833#if !defined(_LIBCPP_HAS_NO_VARIADICS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
834    template <class... _Args>
835        void
836        __emplace_back_slow_path(_Args&&... __args);
837#endif
838    // The following functions are no-ops outside of AddressSanitizer mode.
839    // We call annotatations only for the default Allocator because other allocators
840    // may not meet the AddressSanitizer alignment constraints.
841    // See the documentation for __sanitizer_annotate_contiguous_container for more details.
842    void __annotate_contiguous_container
843    (const void *__beg, const void *__end, const void *__old_mid, const void *__new_mid)
844    {
845#ifndef _LIBCPP_HAS_NO_ASAN
846      if (__beg && is_same<allocator_type, __default_allocator_type>::value)
847        __sanitizer_annotate_contiguous_container(__beg, __end, __old_mid, __new_mid);
848#endif
849    }
850
851    void __annotate_new(size_type __current_size)
852    {
853      __annotate_contiguous_container(data(), data() + capacity(),
854                                      data() + capacity(), data() + __current_size);
855    }
856    void __annotate_delete()
857    {
858      __annotate_contiguous_container(data(), data() + capacity(),
859                                      data() + size(), data() + capacity());
860    }
861    void __annotate_increase(size_type __n)
862    {
863      __annotate_contiguous_container(data(), data() + capacity(),
864                                      data() + size(), data() + size() + __n);
865    }
866    void __annotate_shrink(size_type __old_size)
867    {
868      __annotate_contiguous_container(data(), data() + capacity(),
869                                      data() + __old_size, data() + size());
870    }
871};
872
873template <class _Tp, class _Allocator>
874void
875vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v)
876{
877    __annotate_delete();
878    __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, this->__end_, __v.__begin_);
879    _VSTD::swap(this->__begin_, __v.__begin_);
880    _VSTD::swap(this->__end_, __v.__end_);
881    _VSTD::swap(this->__end_cap(), __v.__end_cap());
882    __v.__first_ = __v.__begin_;
883    __annotate_new(size());
884    __invalidate_all_iterators();
885}
886
887template <class _Tp, class _Allocator>
888typename vector<_Tp, _Allocator>::pointer
889vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p)
890{
891    __annotate_delete();
892    pointer __r = __v.__begin_;
893    __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, __p, __v.__begin_);
894    __alloc_traits::__construct_forward(this->__alloc(), __p, this->__end_, __v.__end_);
895    _VSTD::swap(this->__begin_, __v.__begin_);
896    _VSTD::swap(this->__end_, __v.__end_);
897    _VSTD::swap(this->__end_cap(), __v.__end_cap());
898    __v.__first_ = __v.__begin_;
899    __annotate_new(size());
900    __invalidate_all_iterators();
901    return __r;
902}
903
904//  Allocate space for __n objects
905//  throws length_error if __n > max_size()
906//  throws (probably bad_alloc) if memory run out
907//  Precondition:  __begin_ == __end_ == __end_cap() == 0
908//  Precondition:  __n > 0
909//  Postcondition:  capacity() == __n
910//  Postcondition:  size() == 0
911template <class _Tp, class _Allocator>
912void
913vector<_Tp, _Allocator>::allocate(size_type __n)
914{
915    if (__n > max_size())
916        this->__throw_length_error();
917    this->__begin_ = this->__end_ = __alloc_traits::allocate(this->__alloc(), __n);
918    this->__end_cap() = this->__begin_ + __n;
919    __annotate_new(0);
920}
921
922template <class _Tp, class _Allocator>
923void
924vector<_Tp, _Allocator>::deallocate() _NOEXCEPT
925{
926    if (this->__begin_ != nullptr)
927    {
928        clear();
929        __alloc_traits::deallocate(this->__alloc(), this->__begin_, capacity());
930        this->__begin_ = this->__end_ = this->__end_cap() = nullptr;
931    }
932}
933
934template <class _Tp, class _Allocator>
935typename vector<_Tp, _Allocator>::size_type
936vector<_Tp, _Allocator>::max_size() const _NOEXCEPT
937{
938    return _VSTD::min<size_type>(__alloc_traits::max_size(this->__alloc()), numeric_limits<size_type>::max() / 2);  // end() >= begin(), always
939}
940
941//  Precondition:  __new_size > capacity()
942template <class _Tp, class _Allocator>
943inline _LIBCPP_INLINE_VISIBILITY
944typename vector<_Tp, _Allocator>::size_type
945vector<_Tp, _Allocator>::__recommend(size_type __new_size) const
946{
947    const size_type __ms = max_size();
948    if (__new_size > __ms)
949        this->__throw_length_error();
950    const size_type __cap = capacity();
951    if (__cap >= __ms / 2)
952        return __ms;
953    return _VSTD::max<size_type>(2*__cap, __new_size);
954}
955
956//  Default constructs __n objects starting at __end_
957//  throws if construction throws
958//  Precondition:  __n > 0
959//  Precondition:  size() + __n <= capacity()
960//  Postcondition:  size() == size() + __n
961template <class _Tp, class _Allocator>
962void
963vector<_Tp, _Allocator>::__construct_at_end(size_type __n)
964{
965    allocator_type& __a = this->__alloc();
966    __annotate_increase(__n);
967    do
968    {
969        __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_));
970        ++this->__end_;
971        --__n;
972    } while (__n > 0);
973}
974
975//  Copy constructs __n objects starting at __end_ from __x
976//  throws if construction throws
977//  Precondition:  __n > 0
978//  Precondition:  size() + __n <= capacity()
979//  Postcondition:  size() == old size() + __n
980//  Postcondition:  [i] == __x for all i in [size() - __n, __n)
981template <class _Tp, class _Allocator>
982inline _LIBCPP_INLINE_VISIBILITY
983void
984vector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
985{
986    allocator_type& __a = this->__alloc();
987    __annotate_increase(__n);
988    do
989    {
990        __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), __x);
991        ++this->__end_;
992        --__n;
993    } while (__n > 0);
994}
995
996template <class _Tp, class _Allocator>
997template <class _ForwardIterator>
998typename enable_if
999<
1000    __is_forward_iterator<_ForwardIterator>::value,
1001    void
1002>::type
1003vector<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
1004{
1005    allocator_type& __a = this->__alloc();
1006    for (; __first != __last; ++__first)
1007    {
1008        __annotate_increase(1);
1009        __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), *__first);
1010        ++this->__end_;
1011    }
1012}
1013
1014template <class _Tp, class _Allocator>
1015void
1016vector<_Tp, _Allocator>::__move_construct_at_end(pointer __first, pointer __last)
1017{
1018    allocator_type& __a = this->__alloc();
1019    for (; __first != __last; ++__first)
1020    {
1021        __annotate_increase(1);
1022        __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_),
1023                                  _VSTD::move(*__first));
1024        ++this->__end_;
1025    }
1026}
1027
1028//  Default constructs __n objects starting at __end_
1029//  throws if construction throws
1030//  Postcondition:  size() == size() + __n
1031//  Exception safety: strong.
1032template <class _Tp, class _Allocator>
1033void
1034vector<_Tp, _Allocator>::__append(size_type __n)
1035{
1036    if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1037        this->__construct_at_end(__n);
1038    else
1039    {
1040        allocator_type& __a = this->__alloc();
1041        __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1042        __v.__construct_at_end(__n);
1043        __swap_out_circular_buffer(__v);
1044    }
1045}
1046
1047//  Default constructs __n objects starting at __end_
1048//  throws if construction throws
1049//  Postcondition:  size() == size() + __n
1050//  Exception safety: strong.
1051template <class _Tp, class _Allocator>
1052void
1053vector<_Tp, _Allocator>::__append(size_type __n, const_reference __x)
1054{
1055    if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1056        this->__construct_at_end(__n, __x);
1057    else
1058    {
1059        allocator_type& __a = this->__alloc();
1060        __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1061        __v.__construct_at_end(__n, __x);
1062        __swap_out_circular_buffer(__v);
1063    }
1064}
1065
1066template <class _Tp, class _Allocator>
1067vector<_Tp, _Allocator>::vector(size_type __n)
1068{
1069#if _LIBCPP_DEBUG_LEVEL >= 2
1070    __get_db()->__insert_c(this);
1071#endif
1072    if (__n > 0)
1073    {
1074        allocate(__n);
1075        __construct_at_end(__n);
1076    }
1077}
1078
1079#if _LIBCPP_STD_VER > 11
1080template <class _Tp, class _Allocator>
1081vector<_Tp, _Allocator>::vector(size_type __n, const allocator_type& __a)
1082    : __base(__a)
1083{
1084#if _LIBCPP_DEBUG_LEVEL >= 2
1085    __get_db()->__insert_c(this);
1086#endif
1087    if (__n > 0)
1088    {
1089        allocate(__n);
1090        __construct_at_end(__n);
1091    }
1092}
1093#endif
1094
1095template <class _Tp, class _Allocator>
1096vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x)
1097{
1098#if _LIBCPP_DEBUG_LEVEL >= 2
1099    __get_db()->__insert_c(this);
1100#endif
1101    if (__n > 0)
1102    {
1103        allocate(__n);
1104        __construct_at_end(__n, __x);
1105    }
1106}
1107
1108template <class _Tp, class _Allocator>
1109vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x, const allocator_type& __a)
1110    : __base(__a)
1111{
1112#if _LIBCPP_DEBUG_LEVEL >= 2
1113    __get_db()->__insert_c(this);
1114#endif
1115    if (__n > 0)
1116    {
1117        allocate(__n);
1118        __construct_at_end(__n, __x);
1119    }
1120}
1121
1122template <class _Tp, class _Allocator>
1123template <class _InputIterator>
1124vector<_Tp, _Allocator>::vector(_InputIterator __first,
1125       typename enable_if<__is_input_iterator  <_InputIterator>::value &&
1126                         !__is_forward_iterator<_InputIterator>::value &&
1127                         is_constructible<
1128                            value_type,
1129                            typename iterator_traits<_InputIterator>::reference>::value,
1130                          _InputIterator>::type __last)
1131{
1132#if _LIBCPP_DEBUG_LEVEL >= 2
1133    __get_db()->__insert_c(this);
1134#endif
1135    for (; __first != __last; ++__first)
1136        push_back(*__first);
1137}
1138
1139template <class _Tp, class _Allocator>
1140template <class _InputIterator>
1141vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
1142       typename enable_if<__is_input_iterator  <_InputIterator>::value &&
1143                         !__is_forward_iterator<_InputIterator>::value &&
1144                         is_constructible<
1145                            value_type,
1146                            typename iterator_traits<_InputIterator>::reference>::value>::type*)
1147    : __base(__a)
1148{
1149#if _LIBCPP_DEBUG_LEVEL >= 2
1150    __get_db()->__insert_c(this);
1151#endif
1152    for (; __first != __last; ++__first)
1153        push_back(*__first);
1154}
1155
1156template <class _Tp, class _Allocator>
1157template <class _ForwardIterator>
1158vector<_Tp, _Allocator>::vector(_ForwardIterator __first,
1159                                typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
1160                                is_constructible<
1161                                   value_type,
1162                                   typename iterator_traits<_ForwardIterator>::reference>::value,
1163                                                   _ForwardIterator>::type __last)
1164{
1165#if _LIBCPP_DEBUG_LEVEL >= 2
1166    __get_db()->__insert_c(this);
1167#endif
1168    size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
1169    if (__n > 0)
1170    {
1171        allocate(__n);
1172        __construct_at_end(__first, __last);
1173    }
1174}
1175
1176template <class _Tp, class _Allocator>
1177template <class _ForwardIterator>
1178vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
1179                                typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
1180                                is_constructible<
1181                                   value_type,
1182                                   typename iterator_traits<_ForwardIterator>::reference>::value>::type*)
1183    : __base(__a)
1184{
1185#if _LIBCPP_DEBUG_LEVEL >= 2
1186    __get_db()->__insert_c(this);
1187#endif
1188    size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
1189    if (__n > 0)
1190    {
1191        allocate(__n);
1192        __construct_at_end(__first, __last);
1193    }
1194}
1195
1196template <class _Tp, class _Allocator>
1197vector<_Tp, _Allocator>::vector(const vector& __x)
1198    : __base(__alloc_traits::select_on_container_copy_construction(__x.__alloc()))
1199{
1200#if _LIBCPP_DEBUG_LEVEL >= 2
1201    __get_db()->__insert_c(this);
1202#endif
1203    size_type __n = __x.size();
1204    if (__n > 0)
1205    {
1206        allocate(__n);
1207        __construct_at_end(__x.__begin_, __x.__end_);
1208    }
1209}
1210
1211template <class _Tp, class _Allocator>
1212vector<_Tp, _Allocator>::vector(const vector& __x, const allocator_type& __a)
1213    : __base(__a)
1214{
1215#if _LIBCPP_DEBUG_LEVEL >= 2
1216    __get_db()->__insert_c(this);
1217#endif
1218    size_type __n = __x.size();
1219    if (__n > 0)
1220    {
1221        allocate(__n);
1222        __construct_at_end(__x.__begin_, __x.__end_);
1223    }
1224}
1225
1226#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1227
1228template <class _Tp, class _Allocator>
1229inline _LIBCPP_INLINE_VISIBILITY
1230vector<_Tp, _Allocator>::vector(vector&& __x)
1231        _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
1232    : __base(_VSTD::move(__x.__alloc()))
1233{
1234#if _LIBCPP_DEBUG_LEVEL >= 2
1235    __get_db()->__insert_c(this);
1236    __get_db()->swap(this, &__x);
1237#endif
1238    this->__begin_ = __x.__begin_;
1239    this->__end_ = __x.__end_;
1240    this->__end_cap() = __x.__end_cap();
1241    __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
1242}
1243
1244template <class _Tp, class _Allocator>
1245inline _LIBCPP_INLINE_VISIBILITY
1246vector<_Tp, _Allocator>::vector(vector&& __x, const allocator_type& __a)
1247    : __base(__a)
1248{
1249#if _LIBCPP_DEBUG_LEVEL >= 2
1250    __get_db()->__insert_c(this);
1251#endif
1252    if (__a == __x.__alloc())
1253    {
1254        this->__begin_ = __x.__begin_;
1255        this->__end_ = __x.__end_;
1256        this->__end_cap() = __x.__end_cap();
1257        __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
1258#if _LIBCPP_DEBUG_LEVEL >= 2
1259        __get_db()->swap(this, &__x);
1260#endif
1261    }
1262    else
1263    {
1264        typedef move_iterator<iterator> _Ip;
1265        assign(_Ip(__x.begin()), _Ip(__x.end()));
1266    }
1267}
1268
1269#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1270
1271template <class _Tp, class _Allocator>
1272inline _LIBCPP_INLINE_VISIBILITY
1273vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il)
1274{
1275#if _LIBCPP_DEBUG_LEVEL >= 2
1276    __get_db()->__insert_c(this);
1277#endif
1278    if (__il.size() > 0)
1279    {
1280        allocate(__il.size());
1281        __construct_at_end(__il.begin(), __il.end());
1282    }
1283}
1284
1285template <class _Tp, class _Allocator>
1286inline _LIBCPP_INLINE_VISIBILITY
1287vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
1288    : __base(__a)
1289{
1290#if _LIBCPP_DEBUG_LEVEL >= 2
1291    __get_db()->__insert_c(this);
1292#endif
1293    if (__il.size() > 0)
1294    {
1295        allocate(__il.size());
1296        __construct_at_end(__il.begin(), __il.end());
1297    }
1298}
1299
1300#endif  // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1301
1302template <class _Tp, class _Allocator>
1303inline _LIBCPP_INLINE_VISIBILITY
1304vector<_Tp, _Allocator>&
1305vector<_Tp, _Allocator>::operator=(vector&& __x)
1306        _NOEXCEPT_(
1307             __alloc_traits::propagate_on_container_move_assignment::value &&
1308             is_nothrow_move_assignable<allocator_type>::value)
1309{
1310    __move_assign(__x, integral_constant<bool,
1311          __alloc_traits::propagate_on_container_move_assignment::value>());
1312    return *this;
1313}
1314
1315template <class _Tp, class _Allocator>
1316void
1317vector<_Tp, _Allocator>::__move_assign(vector& __c, false_type)
1318{
1319    if (__base::__alloc() != __c.__alloc())
1320    {
1321        typedef move_iterator<iterator> _Ip;
1322        assign(_Ip(__c.begin()), _Ip(__c.end()));
1323    }
1324    else
1325        __move_assign(__c, true_type());
1326}
1327
1328template <class _Tp, class _Allocator>
1329void
1330vector<_Tp, _Allocator>::__move_assign(vector& __c, true_type)
1331    _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1332{
1333    deallocate();
1334    this->__begin_ = __c.__begin_;
1335    this->__end_ = __c.__end_;
1336    this->__end_cap() = __c.__end_cap();
1337    __base::__move_assign_alloc(__c);
1338    __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
1339#if _LIBCPP_DEBUG_LEVEL >= 2
1340    __get_db()->swap(this, &__c);
1341#endif
1342}
1343
1344#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1345
1346template <class _Tp, class _Allocator>
1347inline _LIBCPP_INLINE_VISIBILITY
1348vector<_Tp, _Allocator>&
1349vector<_Tp, _Allocator>::operator=(const vector& __x)
1350{
1351    if (this != &__x)
1352    {
1353        __base::__copy_assign_alloc(__x);
1354        assign(__x.__begin_, __x.__end_);
1355    }
1356    return *this;
1357}
1358
1359template <class _Tp, class _Allocator>
1360template <class _InputIterator>
1361typename enable_if
1362<
1363     __is_input_iterator  <_InputIterator>::value &&
1364    !__is_forward_iterator<_InputIterator>::value &&
1365    is_constructible<
1366       _Tp,
1367       typename iterator_traits<_InputIterator>::reference>::value,
1368    void
1369>::type
1370vector<_Tp, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
1371{
1372    clear();
1373    for (; __first != __last; ++__first)
1374        push_back(*__first);
1375}
1376
1377template <class _Tp, class _Allocator>
1378template <class _ForwardIterator>
1379typename enable_if
1380<
1381    __is_forward_iterator<_ForwardIterator>::value &&
1382    is_constructible<
1383       _Tp,
1384       typename iterator_traits<_ForwardIterator>::reference>::value,
1385    void
1386>::type
1387vector<_Tp, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
1388{
1389    typename iterator_traits<_ForwardIterator>::difference_type __new_size = _VSTD::distance(__first, __last);
1390    if (static_cast<size_type>(__new_size) <= capacity())
1391    {
1392        _ForwardIterator __mid = __last;
1393        bool __growing = false;
1394        if (static_cast<size_type>(__new_size) > size())
1395        {
1396            __growing = true;
1397            __mid =  __first;
1398            _VSTD::advance(__mid, size());
1399        }
1400        pointer __m = _VSTD::copy(__first, __mid, this->__begin_);
1401        if (__growing)
1402            __construct_at_end(__mid, __last);
1403        else
1404            this->__destruct_at_end(__m);
1405    }
1406    else
1407    {
1408        deallocate();
1409        allocate(__recommend(static_cast<size_type>(__new_size)));
1410        __construct_at_end(__first, __last);
1411    }
1412}
1413
1414template <class _Tp, class _Allocator>
1415void
1416vector<_Tp, _Allocator>::assign(size_type __n, const_reference __u)
1417{
1418    if (__n <= capacity())
1419    {
1420        size_type __s = size();
1421        _VSTD::fill_n(this->__begin_, _VSTD::min(__n, __s), __u);
1422        if (__n > __s)
1423            __construct_at_end(__n - __s, __u);
1424        else
1425            this->__destruct_at_end(this->__begin_ + __n);
1426    }
1427    else
1428    {
1429        deallocate();
1430        allocate(__recommend(static_cast<size_type>(__n)));
1431        __construct_at_end(__n, __u);
1432    }
1433}
1434
1435template <class _Tp, class _Allocator>
1436inline _LIBCPP_INLINE_VISIBILITY
1437typename vector<_Tp, _Allocator>::iterator
1438vector<_Tp, _Allocator>::__make_iter(pointer __p) _NOEXCEPT
1439{
1440#if _LIBCPP_DEBUG_LEVEL >= 2
1441    return iterator(this, __p);
1442#else
1443    return iterator(__p);
1444#endif
1445}
1446
1447template <class _Tp, class _Allocator>
1448inline _LIBCPP_INLINE_VISIBILITY
1449typename vector<_Tp, _Allocator>::const_iterator
1450vector<_Tp, _Allocator>::__make_iter(const_pointer __p) const _NOEXCEPT
1451{
1452#if _LIBCPP_DEBUG_LEVEL >= 2
1453    return const_iterator(this, __p);
1454#else
1455    return const_iterator(__p);
1456#endif
1457}
1458
1459template <class _Tp, class _Allocator>
1460inline _LIBCPP_INLINE_VISIBILITY
1461typename vector<_Tp, _Allocator>::iterator
1462vector<_Tp, _Allocator>::begin() _NOEXCEPT
1463{
1464    return __make_iter(this->__begin_);
1465}
1466
1467template <class _Tp, class _Allocator>
1468inline _LIBCPP_INLINE_VISIBILITY
1469typename vector<_Tp, _Allocator>::const_iterator
1470vector<_Tp, _Allocator>::begin() const _NOEXCEPT
1471{
1472    return __make_iter(this->__begin_);
1473}
1474
1475template <class _Tp, class _Allocator>
1476inline _LIBCPP_INLINE_VISIBILITY
1477typename vector<_Tp, _Allocator>::iterator
1478vector<_Tp, _Allocator>::end() _NOEXCEPT
1479{
1480    return __make_iter(this->__end_);
1481}
1482
1483template <class _Tp, class _Allocator>
1484inline _LIBCPP_INLINE_VISIBILITY
1485typename vector<_Tp, _Allocator>::const_iterator
1486vector<_Tp, _Allocator>::end() const _NOEXCEPT
1487{
1488    return __make_iter(this->__end_);
1489}
1490
1491template <class _Tp, class _Allocator>
1492inline _LIBCPP_INLINE_VISIBILITY
1493typename vector<_Tp, _Allocator>::reference
1494vector<_Tp, _Allocator>::operator[](size_type __n)
1495{
1496    _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
1497    return this->__begin_[__n];
1498}
1499
1500template <class _Tp, class _Allocator>
1501inline _LIBCPP_INLINE_VISIBILITY
1502typename vector<_Tp, _Allocator>::const_reference
1503vector<_Tp, _Allocator>::operator[](size_type __n) const
1504{
1505    _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
1506    return this->__begin_[__n];
1507}
1508
1509template <class _Tp, class _Allocator>
1510typename vector<_Tp, _Allocator>::reference
1511vector<_Tp, _Allocator>::at(size_type __n)
1512{
1513    if (__n >= size())
1514        this->__throw_out_of_range();
1515    return this->__begin_[__n];
1516}
1517
1518template <class _Tp, class _Allocator>
1519typename vector<_Tp, _Allocator>::const_reference
1520vector<_Tp, _Allocator>::at(size_type __n) const
1521{
1522    if (__n >= size())
1523        this->__throw_out_of_range();
1524    return this->__begin_[__n];
1525}
1526
1527template <class _Tp, class _Allocator>
1528void
1529vector<_Tp, _Allocator>::reserve(size_type __n)
1530{
1531    if (__n > capacity())
1532    {
1533        allocator_type& __a = this->__alloc();
1534        __split_buffer<value_type, allocator_type&> __v(__n, size(), __a);
1535        __swap_out_circular_buffer(__v);
1536    }
1537}
1538
1539template <class _Tp, class _Allocator>
1540void
1541vector<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
1542{
1543    if (capacity() > size())
1544    {
1545#ifndef _LIBCPP_NO_EXCEPTIONS
1546        try
1547        {
1548#endif  // _LIBCPP_NO_EXCEPTIONS
1549            allocator_type& __a = this->__alloc();
1550            __split_buffer<value_type, allocator_type&> __v(size(), size(), __a);
1551            __swap_out_circular_buffer(__v);
1552#ifndef _LIBCPP_NO_EXCEPTIONS
1553        }
1554        catch (...)
1555        {
1556        }
1557#endif  // _LIBCPP_NO_EXCEPTIONS
1558    }
1559}
1560
1561template <class _Tp, class _Allocator>
1562template <class _Up>
1563void
1564#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1565vector<_Tp, _Allocator>::__push_back_slow_path(_Up&& __x)
1566#else
1567vector<_Tp, _Allocator>::__push_back_slow_path(_Up& __x)
1568#endif
1569{
1570    allocator_type& __a = this->__alloc();
1571    __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1572    // __v.push_back(_VSTD::forward<_Up>(__x));
1573    __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), _VSTD::forward<_Up>(__x));
1574    __v.__end_++;
1575    __swap_out_circular_buffer(__v);
1576}
1577
1578template <class _Tp, class _Allocator>
1579inline _LIBCPP_INLINE_VISIBILITY
1580void
1581vector<_Tp, _Allocator>::push_back(const_reference __x)
1582{
1583    if (this->__end_ != this->__end_cap())
1584    {
1585        __annotate_increase(1);
1586        __alloc_traits::construct(this->__alloc(),
1587                                  _VSTD::__to_raw_pointer(this->__end_), __x);
1588        ++this->__end_;
1589    }
1590    else
1591        __push_back_slow_path(__x);
1592}
1593
1594#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1595
1596template <class _Tp, class _Allocator>
1597inline _LIBCPP_INLINE_VISIBILITY
1598void
1599vector<_Tp, _Allocator>::push_back(value_type&& __x)
1600{
1601    if (this->__end_ < this->__end_cap())
1602    {
1603        __annotate_increase(1);
1604        __alloc_traits::construct(this->__alloc(),
1605                                  _VSTD::__to_raw_pointer(this->__end_),
1606                                  _VSTD::move(__x));
1607        ++this->__end_;
1608    }
1609    else
1610        __push_back_slow_path(_VSTD::move(__x));
1611}
1612
1613#ifndef _LIBCPP_HAS_NO_VARIADICS
1614
1615template <class _Tp, class _Allocator>
1616template <class... _Args>
1617void
1618vector<_Tp, _Allocator>::__emplace_back_slow_path(_Args&&... __args)
1619{
1620    allocator_type& __a = this->__alloc();
1621    __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1622//    __v.emplace_back(_VSTD::forward<_Args>(__args)...);
1623    __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), _VSTD::forward<_Args>(__args)...);
1624    __v.__end_++;
1625    __swap_out_circular_buffer(__v);
1626}
1627
1628template <class _Tp, class _Allocator>
1629template <class... _Args>
1630inline _LIBCPP_INLINE_VISIBILITY
1631void
1632vector<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1633{
1634    if (this->__end_ < this->__end_cap())
1635    {
1636        __annotate_increase(1);
1637        __alloc_traits::construct(this->__alloc(),
1638                                  _VSTD::__to_raw_pointer(this->__end_),
1639                                  _VSTD::forward<_Args>(__args)...);
1640        ++this->__end_;
1641    }
1642    else
1643        __emplace_back_slow_path(_VSTD::forward<_Args>(__args)...);
1644}
1645
1646#endif  // _LIBCPP_HAS_NO_VARIADICS
1647#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1648
1649template <class _Tp, class _Allocator>
1650inline _LIBCPP_INLINE_VISIBILITY
1651void
1652vector<_Tp, _Allocator>::pop_back()
1653{
1654    _LIBCPP_ASSERT(!empty(), "vector::pop_back called for empty vector");
1655    this->__destruct_at_end(this->__end_ - 1);
1656}
1657
1658template <class _Tp, class _Allocator>
1659inline _LIBCPP_INLINE_VISIBILITY
1660typename vector<_Tp, _Allocator>::iterator
1661vector<_Tp, _Allocator>::erase(const_iterator __position)
1662{
1663#if _LIBCPP_DEBUG_LEVEL >= 2
1664    _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1665        "vector::erase(iterator) called with an iterator not"
1666        " referring to this vector");
1667#endif
1668    _LIBCPP_ASSERT(__position != end(),
1669        "vector::erase(iterator) called with a non-dereferenceable iterator");
1670    difference_type __ps = __position - cbegin();
1671    pointer __p = this->__begin_ + __ps;
1672    iterator __r = __make_iter(__p);
1673    this->__destruct_at_end(_VSTD::move(__p + 1, this->__end_, __p));
1674    return __r;
1675}
1676
1677template <class _Tp, class _Allocator>
1678typename vector<_Tp, _Allocator>::iterator
1679vector<_Tp, _Allocator>::erase(const_iterator __first, const_iterator __last)
1680{
1681#if _LIBCPP_DEBUG_LEVEL >= 2
1682    _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
1683        "vector::erase(iterator,  iterator) called with an iterator not"
1684        " referring to this vector");
1685#endif
1686    _LIBCPP_ASSERT(__first <= __last, "vector::erase(first, last) called with invalid range");
1687    pointer __p = this->__begin_ + (__first - begin());
1688    iterator __r = __make_iter(__p);
1689    if (__first != __last)
1690        this->__destruct_at_end(_VSTD::move(__p + (__last - __first), this->__end_, __p));
1691    return __r;
1692}
1693
1694template <class _Tp, class _Allocator>
1695void
1696vector<_Tp, _Allocator>::__move_range(pointer __from_s, pointer __from_e, pointer __to)
1697{
1698    pointer __old_last = this->__end_;
1699    difference_type __n = __old_last - __to;
1700    for (pointer __i = __from_s + __n; __i < __from_e; ++__i, ++this->__end_)
1701        __alloc_traits::construct(this->__alloc(),
1702                                  _VSTD::__to_raw_pointer(this->__end_),
1703                                  _VSTD::move(*__i));
1704    _VSTD::move_backward(__from_s, __from_s + __n, __old_last);
1705}
1706
1707template <class _Tp, class _Allocator>
1708typename vector<_Tp, _Allocator>::iterator
1709vector<_Tp, _Allocator>::insert(const_iterator __position, const_reference __x)
1710{
1711#if _LIBCPP_DEBUG_LEVEL >= 2
1712    _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1713        "vector::insert(iterator, x) called with an iterator not"
1714        " referring to this vector");
1715#endif
1716    pointer __p = this->__begin_ + (__position - begin());
1717    if (this->__end_ < this->__end_cap())
1718    {
1719        __annotate_increase(1);
1720        if (__p == this->__end_)
1721        {
1722            __alloc_traits::construct(this->__alloc(),
1723                                      _VSTD::__to_raw_pointer(this->__end_), __x);
1724            ++this->__end_;
1725        }
1726        else
1727        {
1728            __move_range(__p, this->__end_, __p + 1);
1729            const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1730            if (__p <= __xr && __xr < this->__end_)
1731                ++__xr;
1732            *__p = *__xr;
1733        }
1734    }
1735    else
1736    {
1737        allocator_type& __a = this->__alloc();
1738        __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
1739        __v.push_back(__x);
1740        __p = __swap_out_circular_buffer(__v, __p);
1741    }
1742    return __make_iter(__p);
1743}
1744
1745#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1746
1747template <class _Tp, class _Allocator>
1748typename vector<_Tp, _Allocator>::iterator
1749vector<_Tp, _Allocator>::insert(const_iterator __position, value_type&& __x)
1750{
1751#if _LIBCPP_DEBUG_LEVEL >= 2
1752    _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1753        "vector::insert(iterator, x) called with an iterator not"
1754        " referring to this vector");
1755#endif
1756    pointer __p = this->__begin_ + (__position - begin());
1757    if (this->__end_ < this->__end_cap())
1758    {
1759        __annotate_increase(1);
1760        if (__p == this->__end_)
1761        {
1762            __alloc_traits::construct(this->__alloc(),
1763                                      _VSTD::__to_raw_pointer(this->__end_),
1764                                      _VSTD::move(__x));
1765            ++this->__end_;
1766        }
1767        else
1768        {
1769            __move_range(__p, this->__end_, __p + 1);
1770            *__p = _VSTD::move(__x);
1771        }
1772    }
1773    else
1774    {
1775        allocator_type& __a = this->__alloc();
1776        __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
1777        __v.push_back(_VSTD::move(__x));
1778        __p = __swap_out_circular_buffer(__v, __p);
1779    }
1780    return __make_iter(__p);
1781}
1782
1783#ifndef _LIBCPP_HAS_NO_VARIADICS
1784
1785template <class _Tp, class _Allocator>
1786template <class... _Args>
1787typename vector<_Tp, _Allocator>::iterator
1788vector<_Tp, _Allocator>::emplace(const_iterator __position, _Args&&... __args)
1789{
1790#if _LIBCPP_DEBUG_LEVEL >= 2
1791    _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1792        "vector::emplace(iterator, x) called with an iterator not"
1793        " referring to this vector");
1794#endif
1795    pointer __p = this->__begin_ + (__position - begin());
1796    if (this->__end_ < this->__end_cap())
1797    {
1798        __annotate_increase(1);
1799        if (__p == this->__end_)
1800        {
1801            __alloc_traits::construct(this->__alloc(),
1802                                      _VSTD::__to_raw_pointer(this->__end_),
1803                                      _VSTD::forward<_Args>(__args)...);
1804            ++this->__end_;
1805        }
1806        else
1807        {
1808            value_type __tmp(_VSTD::forward<_Args>(__args)...);
1809            __move_range(__p, this->__end_, __p + 1);
1810            *__p = _VSTD::move(__tmp);
1811        }
1812    }
1813    else
1814    {
1815        allocator_type& __a = this->__alloc();
1816        __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
1817        __v.emplace_back(_VSTD::forward<_Args>(__args)...);
1818        __p = __swap_out_circular_buffer(__v, __p);
1819    }
1820    return __make_iter(__p);
1821}
1822
1823#endif  // _LIBCPP_HAS_NO_VARIADICS
1824#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1825
1826template <class _Tp, class _Allocator>
1827typename vector<_Tp, _Allocator>::iterator
1828vector<_Tp, _Allocator>::insert(const_iterator __position, size_type __n, const_reference __x)
1829{
1830#if _LIBCPP_DEBUG_LEVEL >= 2
1831    _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1832        "vector::insert(iterator, n, x) called with an iterator not"
1833        " referring to this vector");
1834#endif
1835    pointer __p = this->__begin_ + (__position - begin());
1836    if (__n > 0)
1837    {
1838        if (__n <= static_cast<size_type>(this->__end_cap() - this->__end_))
1839        {
1840            size_type __old_n = __n;
1841            pointer __old_last = this->__end_;
1842            if (__n > static_cast<size_type>(this->__end_ - __p))
1843            {
1844                size_type __cx = __n - (this->__end_ - __p);
1845                __construct_at_end(__cx, __x);
1846                __n -= __cx;
1847            }
1848            if (__n > 0)
1849            {
1850                __annotate_increase(__n);
1851                __move_range(__p, __old_last, __p + __old_n);
1852                const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1853                if (__p <= __xr && __xr < this->__end_)
1854                    __xr += __old_n;
1855                _VSTD::fill_n(__p, __n, *__xr);
1856            }
1857        }
1858        else
1859        {
1860            allocator_type& __a = this->__alloc();
1861            __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1862            __v.__construct_at_end(__n, __x);
1863            __p = __swap_out_circular_buffer(__v, __p);
1864        }
1865    }
1866    return __make_iter(__p);
1867}
1868
1869template <class _Tp, class _Allocator>
1870template <class _InputIterator>
1871typename enable_if
1872<
1873     __is_input_iterator  <_InputIterator>::value &&
1874    !__is_forward_iterator<_InputIterator>::value &&
1875    is_constructible<
1876       _Tp,
1877       typename iterator_traits<_InputIterator>::reference>::value,
1878    typename vector<_Tp, _Allocator>::iterator
1879>::type
1880vector<_Tp, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
1881{
1882#if _LIBCPP_DEBUG_LEVEL >= 2
1883    _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1884        "vector::insert(iterator, range) called with an iterator not"
1885        " referring to this vector");
1886#endif
1887    difference_type __off = __position - begin();
1888    pointer __p = this->__begin_ + __off;
1889    allocator_type& __a = this->__alloc();
1890    pointer __old_last = this->__end_;
1891    for (; this->__end_ != this->__end_cap() && __first != __last; ++__first)
1892    {
1893        __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_),
1894                                  *__first);
1895        ++this->__end_;
1896    }
1897    __split_buffer<value_type, allocator_type&> __v(__a);
1898    if (__first != __last)
1899    {
1900#ifndef _LIBCPP_NO_EXCEPTIONS
1901        try
1902        {
1903#endif  // _LIBCPP_NO_EXCEPTIONS
1904            __v.__construct_at_end(__first, __last);
1905            difference_type __old_size = __old_last - this->__begin_;
1906            difference_type __old_p = __p - this->__begin_;
1907            reserve(__recommend(size() + __v.size()));
1908            __p = this->__begin_ + __old_p;
1909            __old_last = this->__begin_ + __old_size;
1910#ifndef _LIBCPP_NO_EXCEPTIONS
1911        }
1912        catch (...)
1913        {
1914            erase(__make_iter(__old_last), end());
1915            throw;
1916        }
1917#endif  // _LIBCPP_NO_EXCEPTIONS
1918    }
1919    __p = _VSTD::rotate(__p, __old_last, this->__end_);
1920    insert(__make_iter(__p), make_move_iterator(__v.begin()),
1921                                    make_move_iterator(__v.end()));
1922    return begin() + __off;
1923}
1924
1925template <class _Tp, class _Allocator>
1926template <class _ForwardIterator>
1927typename enable_if
1928<
1929    __is_forward_iterator<_ForwardIterator>::value &&
1930    is_constructible<
1931       _Tp,
1932       typename iterator_traits<_ForwardIterator>::reference>::value,
1933    typename vector<_Tp, _Allocator>::iterator
1934>::type
1935vector<_Tp, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
1936{
1937#if _LIBCPP_DEBUG_LEVEL >= 2
1938    _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1939        "vector::insert(iterator, range) called with an iterator not"
1940        " referring to this vector");
1941#endif
1942    pointer __p = this->__begin_ + (__position - begin());
1943    difference_type __n = _VSTD::distance(__first, __last);
1944    if (__n > 0)
1945    {
1946        if (__n <= this->__end_cap() - this->__end_)
1947        {
1948            size_type __old_n = __n;
1949            pointer __old_last = this->__end_;
1950            _ForwardIterator __m = __last;
1951            difference_type __dx = this->__end_ - __p;
1952            if (__n > __dx)
1953            {
1954                __m = __first;
1955                _VSTD::advance(__m, this->__end_ - __p);
1956                __construct_at_end(__m, __last);
1957                __n = __dx;
1958            }
1959            if (__n > 0)
1960            {
1961                __annotate_increase(__n);
1962                __move_range(__p, __old_last, __p + __old_n);
1963                _VSTD::copy(__first, __m, __p);
1964            }
1965        }
1966        else
1967        {
1968            allocator_type& __a = this->__alloc();
1969            __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1970            __v.__construct_at_end(__first, __last);
1971            __p = __swap_out_circular_buffer(__v, __p);
1972        }
1973    }
1974    return __make_iter(__p);
1975}
1976
1977template <class _Tp, class _Allocator>
1978void
1979vector<_Tp, _Allocator>::resize(size_type __sz)
1980{
1981    size_type __cs = size();
1982    if (__cs < __sz)
1983        this->__append(__sz - __cs);
1984    else if (__cs > __sz)
1985        this->__destruct_at_end(this->__begin_ + __sz);
1986}
1987
1988template <class _Tp, class _Allocator>
1989void
1990vector<_Tp, _Allocator>::resize(size_type __sz, const_reference __x)
1991{
1992    size_type __cs = size();
1993    if (__cs < __sz)
1994        this->__append(__sz - __cs, __x);
1995    else if (__cs > __sz)
1996        this->__destruct_at_end(this->__begin_ + __sz);
1997}
1998
1999template <class _Tp, class _Allocator>
2000void
2001vector<_Tp, _Allocator>::swap(vector& __x)
2002        _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
2003                   __is_nothrow_swappable<allocator_type>::value)
2004{
2005    _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
2006                   this->__alloc() == __x.__alloc(),
2007                   "vector::swap: Either propagate_on_container_swap must be true"
2008                   " or the allocators must compare equal");
2009    _VSTD::swap(this->__begin_, __x.__begin_);
2010    _VSTD::swap(this->__end_, __x.__end_);
2011    _VSTD::swap(this->__end_cap(), __x.__end_cap());
2012    __base::__swap_alloc(this->__alloc(), __x.__alloc());
2013#if _LIBCPP_DEBUG_LEVEL >= 2
2014    __get_db()->swap(this, &__x);
2015#endif  // _LIBCPP_DEBUG_LEVEL >= 2
2016}
2017
2018template <class _Tp, class _Allocator>
2019bool
2020vector<_Tp, _Allocator>::__invariants() const
2021{
2022    if (this->__begin_ == nullptr)
2023    {
2024        if (this->__end_ != nullptr || this->__end_cap() != nullptr)
2025            return false;
2026    }
2027    else
2028    {
2029        if (this->__begin_ > this->__end_)
2030            return false;
2031        if (this->__begin_ == this->__end_cap())
2032            return false;
2033        if (this->__end_ > this->__end_cap())
2034            return false;
2035    }
2036    return true;
2037}
2038
2039#if _LIBCPP_DEBUG_LEVEL >= 2
2040
2041template <class _Tp, class _Allocator>
2042bool
2043vector<_Tp, _Allocator>::__dereferenceable(const const_iterator* __i) const
2044{
2045    return this->__begin_ <= __i->base() && __i->base() < this->__end_;
2046}
2047
2048template <class _Tp, class _Allocator>
2049bool
2050vector<_Tp, _Allocator>::__decrementable(const const_iterator* __i) const
2051{
2052    return this->__begin_ < __i->base() && __i->base() <= this->__end_;
2053}
2054
2055template <class _Tp, class _Allocator>
2056bool
2057vector<_Tp, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
2058{
2059    const_pointer __p = __i->base() + __n;
2060    return this->__begin_ <= __p && __p <= this->__end_;
2061}
2062
2063template <class _Tp, class _Allocator>
2064bool
2065vector<_Tp, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
2066{
2067    const_pointer __p = __i->base() + __n;
2068    return this->__begin_ <= __p && __p < this->__end_;
2069}
2070
2071#endif  // _LIBCPP_DEBUG_LEVEL >= 2
2072
2073template <class _Tp, class _Allocator>
2074inline _LIBCPP_INLINE_VISIBILITY
2075void
2076vector<_Tp, _Allocator>::__invalidate_all_iterators()
2077{
2078#if _LIBCPP_DEBUG_LEVEL >= 2
2079    __get_db()->__invalidate_all(this);
2080#endif  // _LIBCPP_DEBUG_LEVEL >= 2
2081}
2082
2083// vector<bool>
2084
2085template <class _Allocator> class vector<bool, _Allocator>;
2086
2087template <class _Allocator> struct hash<vector<bool, _Allocator> >;
2088
2089template <class _Allocator>
2090struct __has_storage_type<vector<bool, _Allocator> >
2091{
2092    static const bool value = true;
2093};
2094
2095template <class _Allocator>
2096class _LIBCPP_TYPE_VIS_ONLY vector<bool, _Allocator>
2097    : private __vector_base_common<true>
2098{
2099public:
2100    typedef vector                                   __self;
2101    typedef bool                                     value_type;
2102    typedef _Allocator                               allocator_type;
2103    typedef allocator_traits<allocator_type>         __alloc_traits;
2104    typedef typename __alloc_traits::size_type       size_type;
2105    typedef typename __alloc_traits::difference_type difference_type;
2106    typedef size_type __storage_type;
2107    typedef __bit_iterator<vector, false>            pointer;
2108    typedef __bit_iterator<vector, true>             const_pointer;
2109    typedef pointer                                  iterator;
2110    typedef const_pointer                            const_iterator;
2111    typedef _VSTD::reverse_iterator<iterator>         reverse_iterator;
2112    typedef _VSTD::reverse_iterator<const_iterator>   const_reverse_iterator;
2113
2114private:
2115    typedef typename __alloc_traits::template
2116#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
2117                rebind_alloc<__storage_type>
2118#else
2119                rebind_alloc<__storage_type>::other
2120#endif
2121                                                     __storage_allocator;
2122    typedef allocator_traits<__storage_allocator>    __storage_traits;
2123    typedef typename __storage_traits::pointer       __storage_pointer;
2124    typedef typename __storage_traits::const_pointer __const_storage_pointer;
2125
2126    __storage_pointer                                      __begin_;
2127    size_type                                              __size_;
2128    __compressed_pair<size_type, __storage_allocator> __cap_alloc_;
2129public:
2130    typedef __bit_reference<vector>                  reference;
2131    typedef __bit_const_reference<vector>            const_reference;
2132private:
2133    _LIBCPP_INLINE_VISIBILITY
2134    size_type& __cap() _NOEXCEPT
2135        {return __cap_alloc_.first();}
2136    _LIBCPP_INLINE_VISIBILITY
2137    const size_type& __cap() const _NOEXCEPT
2138        {return __cap_alloc_.first();}
2139    _LIBCPP_INLINE_VISIBILITY
2140    __storage_allocator& __alloc() _NOEXCEPT
2141        {return __cap_alloc_.second();}
2142    _LIBCPP_INLINE_VISIBILITY
2143    const __storage_allocator& __alloc() const _NOEXCEPT
2144        {return __cap_alloc_.second();}
2145
2146    static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
2147
2148    _LIBCPP_INLINE_VISIBILITY
2149    static size_type __internal_cap_to_external(size_type __n) _NOEXCEPT
2150        {return __n * __bits_per_word;}
2151    _LIBCPP_INLINE_VISIBILITY
2152    static size_type __external_cap_to_internal(size_type __n) _NOEXCEPT
2153        {return (__n - 1) / __bits_per_word + 1;}
2154
2155public:
2156    _LIBCPP_INLINE_VISIBILITY
2157    vector()
2158        _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
2159    _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a);
2160    ~vector();
2161    explicit vector(size_type __n);
2162#if _LIBCPP_STD_VER > 11
2163    explicit vector(size_type __n, const allocator_type& __a);
2164#endif
2165    vector(size_type __n, const value_type& __v);
2166    vector(size_type __n, const value_type& __v, const allocator_type& __a);
2167    template <class _InputIterator>
2168        vector(_InputIterator __first, _InputIterator __last,
2169               typename enable_if<__is_input_iterator  <_InputIterator>::value &&
2170                                 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2171    template <class _InputIterator>
2172        vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2173               typename enable_if<__is_input_iterator  <_InputIterator>::value &&
2174                                 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2175    template <class _ForwardIterator>
2176        vector(_ForwardIterator __first, _ForwardIterator __last,
2177               typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2178    template <class _ForwardIterator>
2179        vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2180               typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2181
2182    vector(const vector& __v);
2183    vector(const vector& __v, const allocator_type& __a);
2184    vector& operator=(const vector& __v);
2185#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2186    vector(initializer_list<value_type> __il);
2187    vector(initializer_list<value_type> __il, const allocator_type& __a);
2188#endif  // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2189
2190#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2191    _LIBCPP_INLINE_VISIBILITY
2192    vector(vector&& __v)
2193        _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
2194    vector(vector&& __v, const allocator_type& __a);
2195    _LIBCPP_INLINE_VISIBILITY
2196    vector& operator=(vector&& __v)
2197        _NOEXCEPT_(
2198             __alloc_traits::propagate_on_container_move_assignment::value &&
2199             is_nothrow_move_assignable<allocator_type>::value);
2200#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2201#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2202    _LIBCPP_INLINE_VISIBILITY
2203    vector& operator=(initializer_list<value_type> __il)
2204        {assign(__il.begin(), __il.end()); return *this;}
2205#endif  // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2206
2207    template <class _InputIterator>
2208        typename enable_if
2209        <
2210            __is_input_iterator<_InputIterator>::value &&
2211           !__is_forward_iterator<_InputIterator>::value,
2212           void
2213        >::type
2214        assign(_InputIterator __first, _InputIterator __last);
2215    template <class _ForwardIterator>
2216        typename enable_if
2217        <
2218            __is_forward_iterator<_ForwardIterator>::value,
2219           void
2220        >::type
2221        assign(_ForwardIterator __first, _ForwardIterator __last);
2222
2223    void assign(size_type __n, const value_type& __x);
2224#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2225    _LIBCPP_INLINE_VISIBILITY
2226    void assign(initializer_list<value_type> __il)
2227        {assign(__il.begin(), __il.end());}
2228#endif  // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2229
2230    _LIBCPP_INLINE_VISIBILITY allocator_type get_allocator() const _NOEXCEPT
2231        {return allocator_type(this->__alloc());}
2232
2233    size_type max_size() const _NOEXCEPT;
2234    _LIBCPP_INLINE_VISIBILITY
2235    size_type capacity() const _NOEXCEPT
2236        {return __internal_cap_to_external(__cap());}
2237    _LIBCPP_INLINE_VISIBILITY
2238    size_type size() const _NOEXCEPT
2239        {return __size_;}
2240    _LIBCPP_INLINE_VISIBILITY
2241    bool empty() const _NOEXCEPT
2242        {return __size_ == 0;}
2243    void reserve(size_type __n);
2244    void shrink_to_fit() _NOEXCEPT;
2245
2246    _LIBCPP_INLINE_VISIBILITY
2247    iterator begin() _NOEXCEPT
2248        {return __make_iter(0);}
2249    _LIBCPP_INLINE_VISIBILITY
2250    const_iterator begin() const _NOEXCEPT
2251        {return __make_iter(0);}
2252    _LIBCPP_INLINE_VISIBILITY
2253    iterator end() _NOEXCEPT
2254        {return __make_iter(__size_);}
2255    _LIBCPP_INLINE_VISIBILITY
2256    const_iterator end()   const _NOEXCEPT
2257        {return __make_iter(__size_);}
2258
2259    _LIBCPP_INLINE_VISIBILITY
2260    reverse_iterator rbegin() _NOEXCEPT
2261        {return       reverse_iterator(end());}
2262    _LIBCPP_INLINE_VISIBILITY
2263    const_reverse_iterator rbegin() const _NOEXCEPT
2264        {return const_reverse_iterator(end());}
2265    _LIBCPP_INLINE_VISIBILITY
2266    reverse_iterator rend() _NOEXCEPT
2267        {return       reverse_iterator(begin());}
2268    _LIBCPP_INLINE_VISIBILITY
2269    const_reverse_iterator rend()   const _NOEXCEPT
2270        {return const_reverse_iterator(begin());}
2271
2272    _LIBCPP_INLINE_VISIBILITY
2273    const_iterator         cbegin()  const _NOEXCEPT
2274        {return __make_iter(0);}
2275    _LIBCPP_INLINE_VISIBILITY
2276    const_iterator         cend()    const _NOEXCEPT
2277        {return __make_iter(__size_);}
2278    _LIBCPP_INLINE_VISIBILITY
2279    const_reverse_iterator crbegin() const _NOEXCEPT
2280        {return rbegin();}
2281    _LIBCPP_INLINE_VISIBILITY
2282    const_reverse_iterator crend()   const _NOEXCEPT
2283        {return rend();}
2284
2285    _LIBCPP_INLINE_VISIBILITY reference       operator[](size_type __n)       {return __make_ref(__n);}
2286    _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const {return __make_ref(__n);}
2287    reference       at(size_type __n);
2288    const_reference at(size_type __n) const;
2289
2290    _LIBCPP_INLINE_VISIBILITY reference       front()       {return __make_ref(0);}
2291    _LIBCPP_INLINE_VISIBILITY const_reference front() const {return __make_ref(0);}
2292    _LIBCPP_INLINE_VISIBILITY reference       back()        {return __make_ref(__size_ - 1);}
2293    _LIBCPP_INLINE_VISIBILITY const_reference back()  const {return __make_ref(__size_ - 1);}
2294
2295    void push_back(const value_type& __x);
2296#if _LIBCPP_STD_VER > 11
2297    template <class... _Args>
2298    _LIBCPP_INLINE_VISIBILITY void emplace_back(_Args&&... __args)
2299        { push_back ( value_type ( _VSTD::forward<_Args>(__args)... )); }
2300#endif
2301
2302    _LIBCPP_INLINE_VISIBILITY void pop_back() {--__size_;}
2303
2304#if _LIBCPP_STD_VER > 11
2305    template <class... _Args>
2306   _LIBCPP_INLINE_VISIBILITY iterator emplace(const_iterator position, _Args&&... __args)
2307        { return insert ( position, value_type ( _VSTD::forward<_Args>(__args)... )); }
2308#endif
2309
2310    iterator insert(const_iterator __position, const value_type& __x);
2311    iterator insert(const_iterator __position, size_type __n, const value_type& __x);
2312    iterator insert(const_iterator __position, size_type __n, const_reference __x);
2313    template <class _InputIterator>
2314        typename enable_if
2315        <
2316             __is_input_iterator  <_InputIterator>::value &&
2317            !__is_forward_iterator<_InputIterator>::value,
2318            iterator
2319        >::type
2320        insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
2321    template <class _ForwardIterator>
2322        typename enable_if
2323        <
2324            __is_forward_iterator<_ForwardIterator>::value,
2325            iterator
2326        >::type
2327        insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
2328#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2329    _LIBCPP_INLINE_VISIBILITY
2330    iterator insert(const_iterator __position, initializer_list<value_type> __il)
2331        {return insert(__position, __il.begin(), __il.end());}
2332#endif  // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2333
2334    _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
2335    iterator erase(const_iterator __first, const_iterator __last);
2336
2337    _LIBCPP_INLINE_VISIBILITY
2338    void clear() _NOEXCEPT {__size_ = 0;}
2339
2340    void swap(vector&)
2341        _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
2342                   __is_nothrow_swappable<allocator_type>::value);
2343
2344    void resize(size_type __sz, value_type __x = false);
2345    void flip() _NOEXCEPT;
2346
2347    bool __invariants() const;
2348
2349private:
2350    _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
2351    void allocate(size_type __n);
2352    void deallocate() _NOEXCEPT;
2353    _LIBCPP_INLINE_VISIBILITY
2354    static size_type __align_it(size_type __new_size) _NOEXCEPT
2355        {return __new_size + (__bits_per_word-1) & ~(__bits_per_word-1);};
2356    _LIBCPP_INLINE_VISIBILITY  size_type __recommend(size_type __new_size) const;
2357    _LIBCPP_INLINE_VISIBILITY void __construct_at_end(size_type __n, bool __x);
2358    template <class _ForwardIterator>
2359        typename enable_if
2360        <
2361            __is_forward_iterator<_ForwardIterator>::value,
2362            void
2363        >::type
2364        __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
2365    void __append(size_type __n, const_reference __x);
2366    _LIBCPP_INLINE_VISIBILITY
2367    reference __make_ref(size_type __pos) _NOEXCEPT
2368        {return reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
2369    _LIBCPP_INLINE_VISIBILITY
2370    const_reference __make_ref(size_type __pos) const _NOEXCEPT
2371        {return const_reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
2372    _LIBCPP_INLINE_VISIBILITY
2373    iterator __make_iter(size_type __pos) _NOEXCEPT
2374        {return iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
2375    _LIBCPP_INLINE_VISIBILITY
2376    const_iterator __make_iter(size_type __pos) const _NOEXCEPT
2377        {return const_iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
2378    _LIBCPP_INLINE_VISIBILITY
2379    iterator __const_iterator_cast(const_iterator __p) _NOEXCEPT
2380        {return begin() + (__p - cbegin());}
2381
2382    _LIBCPP_INLINE_VISIBILITY
2383    void __copy_assign_alloc(const vector& __v)
2384        {__copy_assign_alloc(__v, integral_constant<bool,
2385                      __storage_traits::propagate_on_container_copy_assignment::value>());}
2386    _LIBCPP_INLINE_VISIBILITY
2387    void __copy_assign_alloc(const vector& __c, true_type)
2388        {
2389            if (__alloc() != __c.__alloc())
2390                deallocate();
2391            __alloc() = __c.__alloc();
2392        }
2393
2394    _LIBCPP_INLINE_VISIBILITY
2395    void __copy_assign_alloc(const vector&, false_type)
2396        {}
2397
2398    void __move_assign(vector& __c, false_type);
2399    void __move_assign(vector& __c, true_type)
2400        _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
2401    _LIBCPP_INLINE_VISIBILITY
2402    void __move_assign_alloc(vector& __c)
2403        _NOEXCEPT_(
2404            !__storage_traits::propagate_on_container_move_assignment::value ||
2405            is_nothrow_move_assignable<allocator_type>::value)
2406        {__move_assign_alloc(__c, integral_constant<bool,
2407                      __storage_traits::propagate_on_container_move_assignment::value>());}
2408    _LIBCPP_INLINE_VISIBILITY
2409    void __move_assign_alloc(vector& __c, true_type)
2410        _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
2411        {
2412            __alloc() = _VSTD::move(__c.__alloc());
2413        }
2414
2415    _LIBCPP_INLINE_VISIBILITY
2416    void __move_assign_alloc(vector&, false_type)
2417        _NOEXCEPT
2418        {}
2419
2420    _LIBCPP_INLINE_VISIBILITY
2421    static void __swap_alloc(__storage_allocator& __x, __storage_allocator& __y)
2422        _NOEXCEPT_(
2423            !__storage_traits::propagate_on_container_swap::value ||
2424            __is_nothrow_swappable<allocator_type>::value)
2425        {__swap_alloc(__x, __y, integral_constant<bool,
2426                      __storage_traits::propagate_on_container_swap::value>());}
2427
2428    _LIBCPP_INLINE_VISIBILITY
2429    static void __swap_alloc(__storage_allocator& __x, __storage_allocator& __y, true_type)
2430        _NOEXCEPT_(__is_nothrow_swappable<allocator_type>::value)
2431        {
2432            using _VSTD::swap;
2433            swap(__x, __y);
2434        }
2435    _LIBCPP_INLINE_VISIBILITY
2436    static void __swap_alloc(__storage_allocator&, __storage_allocator&, false_type)
2437        _NOEXCEPT
2438        {}
2439
2440    size_t __hash_code() const _NOEXCEPT;
2441
2442    friend class __bit_reference<vector>;
2443    friend class __bit_const_reference<vector>;
2444    friend class __bit_iterator<vector, false>;
2445    friend class __bit_iterator<vector, true>;
2446    friend struct __bit_array<vector>;
2447    friend struct _LIBCPP_TYPE_VIS_ONLY hash<vector>;
2448};
2449
2450template <class _Allocator>
2451inline _LIBCPP_INLINE_VISIBILITY
2452void
2453vector<bool, _Allocator>::__invalidate_all_iterators()
2454{
2455}
2456
2457//  Allocate space for __n objects
2458//  throws length_error if __n > max_size()
2459//  throws (probably bad_alloc) if memory run out
2460//  Precondition:  __begin_ == __end_ == __cap() == 0
2461//  Precondition:  __n > 0
2462//  Postcondition:  capacity() == __n
2463//  Postcondition:  size() == 0
2464template <class _Allocator>
2465void
2466vector<bool, _Allocator>::allocate(size_type __n)
2467{
2468    if (__n > max_size())
2469        this->__throw_length_error();
2470    __n = __external_cap_to_internal(__n);
2471    this->__begin_ = __storage_traits::allocate(this->__alloc(), __n);
2472    this->__size_ = 0;
2473    this->__cap() = __n;
2474}
2475
2476template <class _Allocator>
2477void
2478vector<bool, _Allocator>::deallocate() _NOEXCEPT
2479{
2480    if (this->__begin_ != nullptr)
2481    {
2482        __storage_traits::deallocate(this->__alloc(), this->__begin_, __cap());
2483        __invalidate_all_iterators();
2484        this->__begin_ = nullptr;
2485        this->__size_ = this->__cap() = 0;
2486    }
2487}
2488
2489template <class _Allocator>
2490typename vector<bool, _Allocator>::size_type
2491vector<bool, _Allocator>::max_size() const _NOEXCEPT
2492{
2493    size_type __amax = __storage_traits::max_size(__alloc());
2494    size_type __nmax = numeric_limits<size_type>::max() / 2;  // end() >= begin(), always
2495    if (__nmax / __bits_per_word <= __amax)
2496        return __nmax;
2497    return __internal_cap_to_external(__amax);
2498}
2499
2500//  Precondition:  __new_size > capacity()
2501template <class _Allocator>
2502inline _LIBCPP_INLINE_VISIBILITY
2503typename vector<bool, _Allocator>::size_type
2504vector<bool, _Allocator>::__recommend(size_type __new_size) const
2505{
2506    const size_type __ms = max_size();
2507    if (__new_size > __ms)
2508        this->__throw_length_error();
2509    const size_type __cap = capacity();
2510    if (__cap >= __ms / 2)
2511        return __ms;
2512    return _VSTD::max(2*__cap, __align_it(__new_size));
2513}
2514
2515//  Default constructs __n objects starting at __end_
2516//  Precondition:  __n > 0
2517//  Precondition:  size() + __n <= capacity()
2518//  Postcondition:  size() == size() + __n
2519template <class _Allocator>
2520inline _LIBCPP_INLINE_VISIBILITY
2521void
2522vector<bool, _Allocator>::__construct_at_end(size_type __n, bool __x)
2523{
2524    size_type __old_size = this->__size_;
2525    this->__size_ += __n;
2526    _VSTD::fill_n(__make_iter(__old_size), __n, __x);
2527}
2528
2529template <class _Allocator>
2530template <class _ForwardIterator>
2531typename enable_if
2532<
2533    __is_forward_iterator<_ForwardIterator>::value,
2534    void
2535>::type
2536vector<bool, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
2537{
2538    size_type __old_size = this->__size_;
2539    this->__size_ += _VSTD::distance(__first, __last);
2540    _VSTD::copy(__first, __last, __make_iter(__old_size));
2541}
2542
2543template <class _Allocator>
2544inline _LIBCPP_INLINE_VISIBILITY
2545vector<bool, _Allocator>::vector()
2546        _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
2547    : __begin_(nullptr),
2548      __size_(0),
2549      __cap_alloc_(0)
2550{
2551}
2552
2553template <class _Allocator>
2554inline _LIBCPP_INLINE_VISIBILITY
2555vector<bool, _Allocator>::vector(const allocator_type& __a)
2556    : __begin_(nullptr),
2557      __size_(0),
2558      __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2559{
2560}
2561
2562template <class _Allocator>
2563vector<bool, _Allocator>::vector(size_type __n)
2564    : __begin_(nullptr),
2565      __size_(0),
2566      __cap_alloc_(0)
2567{
2568    if (__n > 0)
2569    {
2570        allocate(__n);
2571        __construct_at_end(__n, false);
2572    }
2573}
2574
2575#if _LIBCPP_STD_VER > 11
2576template <class _Allocator>
2577vector<bool, _Allocator>::vector(size_type __n, const allocator_type& __a)
2578    : __begin_(nullptr),
2579      __size_(0),
2580      __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2581{
2582    if (__n > 0)
2583    {
2584        allocate(__n);
2585        __construct_at_end(__n, false);
2586    }
2587}
2588#endif
2589
2590template <class _Allocator>
2591vector<bool, _Allocator>::vector(size_type __n, const value_type& __x)
2592    : __begin_(nullptr),
2593      __size_(0),
2594      __cap_alloc_(0)
2595{
2596    if (__n > 0)
2597    {
2598        allocate(__n);
2599        __construct_at_end(__n, __x);
2600    }
2601}
2602
2603template <class _Allocator>
2604vector<bool, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
2605    : __begin_(nullptr),
2606      __size_(0),
2607      __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2608{
2609    if (__n > 0)
2610    {
2611        allocate(__n);
2612        __construct_at_end(__n, __x);
2613    }
2614}
2615
2616template <class _Allocator>
2617template <class _InputIterator>
2618vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last,
2619       typename enable_if<__is_input_iterator  <_InputIterator>::value &&
2620                         !__is_forward_iterator<_InputIterator>::value>::type*)
2621    : __begin_(nullptr),
2622      __size_(0),
2623      __cap_alloc_(0)
2624{
2625#ifndef _LIBCPP_NO_EXCEPTIONS
2626    try
2627    {
2628#endif  // _LIBCPP_NO_EXCEPTIONS
2629        for (; __first != __last; ++__first)
2630            push_back(*__first);
2631#ifndef _LIBCPP_NO_EXCEPTIONS
2632    }
2633    catch (...)
2634    {
2635        if (__begin_ != nullptr)
2636            __storage_traits::deallocate(__alloc(), __begin_, __cap());
2637        __invalidate_all_iterators();
2638        throw;
2639    }
2640#endif  // _LIBCPP_NO_EXCEPTIONS
2641}
2642
2643template <class _Allocator>
2644template <class _InputIterator>
2645vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2646       typename enable_if<__is_input_iterator  <_InputIterator>::value &&
2647                         !__is_forward_iterator<_InputIterator>::value>::type*)
2648    : __begin_(nullptr),
2649      __size_(0),
2650      __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2651{
2652#ifndef _LIBCPP_NO_EXCEPTIONS
2653    try
2654    {
2655#endif  // _LIBCPP_NO_EXCEPTIONS
2656        for (; __first != __last; ++__first)
2657            push_back(*__first);
2658#ifndef _LIBCPP_NO_EXCEPTIONS
2659    }
2660    catch (...)
2661    {
2662        if (__begin_ != nullptr)
2663            __storage_traits::deallocate(__alloc(), __begin_, __cap());
2664        __invalidate_all_iterators();
2665        throw;
2666    }
2667#endif  // _LIBCPP_NO_EXCEPTIONS
2668}
2669
2670template <class _Allocator>
2671template <class _ForwardIterator>
2672vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last,
2673                                typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
2674    : __begin_(nullptr),
2675      __size_(0),
2676      __cap_alloc_(0)
2677{
2678    size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
2679    if (__n > 0)
2680    {
2681        allocate(__n);
2682        __construct_at_end(__first, __last);
2683    }
2684}
2685
2686template <class _Allocator>
2687template <class _ForwardIterator>
2688vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2689                                typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
2690    : __begin_(nullptr),
2691      __size_(0),
2692      __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2693{
2694    size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
2695    if (__n > 0)
2696    {
2697        allocate(__n);
2698        __construct_at_end(__first, __last);
2699    }
2700}
2701
2702#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2703
2704template <class _Allocator>
2705vector<bool, _Allocator>::vector(initializer_list<value_type> __il)
2706    : __begin_(nullptr),
2707      __size_(0),
2708      __cap_alloc_(0)
2709{
2710    size_type __n = static_cast<size_type>(__il.size());
2711    if (__n > 0)
2712    {
2713        allocate(__n);
2714        __construct_at_end(__il.begin(), __il.end());
2715    }
2716}
2717
2718template <class _Allocator>
2719vector<bool, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
2720    : __begin_(nullptr),
2721      __size_(0),
2722      __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2723{
2724    size_type __n = static_cast<size_type>(__il.size());
2725    if (__n > 0)
2726    {
2727        allocate(__n);
2728        __construct_at_end(__il.begin(), __il.end());
2729    }
2730}
2731
2732#endif  // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2733
2734template <class _Allocator>
2735vector<bool, _Allocator>::~vector()
2736{
2737    if (__begin_ != nullptr)
2738        __storage_traits::deallocate(__alloc(), __begin_, __cap());
2739    __invalidate_all_iterators();
2740}
2741
2742template <class _Allocator>
2743vector<bool, _Allocator>::vector(const vector& __v)
2744    : __begin_(nullptr),
2745      __size_(0),
2746      __cap_alloc_(0, __storage_traits::select_on_container_copy_construction(__v.__alloc()))
2747{
2748    if (__v.size() > 0)
2749    {
2750        allocate(__v.size());
2751        __construct_at_end(__v.begin(), __v.end());
2752    }
2753}
2754
2755template <class _Allocator>
2756vector<bool, _Allocator>::vector(const vector& __v, const allocator_type& __a)
2757    : __begin_(nullptr),
2758      __size_(0),
2759      __cap_alloc_(0, __a)
2760{
2761    if (__v.size() > 0)
2762    {
2763        allocate(__v.size());
2764        __construct_at_end(__v.begin(), __v.end());
2765    }
2766}
2767
2768template <class _Allocator>
2769vector<bool, _Allocator>&
2770vector<bool, _Allocator>::operator=(const vector& __v)
2771{
2772    if (this != &__v)
2773    {
2774        __copy_assign_alloc(__v);
2775        if (__v.__size_)
2776        {
2777            if (__v.__size_ > capacity())
2778            {
2779                deallocate();
2780                allocate(__v.__size_);
2781            }
2782            _VSTD::copy(__v.__begin_, __v.__begin_ + __external_cap_to_internal(__v.__size_), __begin_);
2783        }
2784        __size_ = __v.__size_;
2785    }
2786    return *this;
2787}
2788
2789#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2790
2791template <class _Allocator>
2792inline _LIBCPP_INLINE_VISIBILITY
2793vector<bool, _Allocator>::vector(vector&& __v)
2794        _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
2795    : __begin_(__v.__begin_),
2796      __size_(__v.__size_),
2797      __cap_alloc_(__v.__cap_alloc_)
2798{
2799    __v.__begin_ = nullptr;
2800    __v.__size_ = 0;
2801    __v.__cap() = 0;
2802}
2803
2804template <class _Allocator>
2805vector<bool, _Allocator>::vector(vector&& __v, const allocator_type& __a)
2806    : __begin_(nullptr),
2807      __size_(0),
2808      __cap_alloc_(0, __a)
2809{
2810    if (__a == allocator_type(__v.__alloc()))
2811    {
2812        this->__begin_ = __v.__begin_;
2813        this->__size_ = __v.__size_;
2814        this->__cap() = __v.__cap();
2815        __v.__begin_ = nullptr;
2816        __v.__cap() = __v.__size_ = 0;
2817    }
2818    else if (__v.size() > 0)
2819    {
2820        allocate(__v.size());
2821        __construct_at_end(__v.begin(), __v.end());
2822    }
2823}
2824
2825template <class _Allocator>
2826inline _LIBCPP_INLINE_VISIBILITY
2827vector<bool, _Allocator>&
2828vector<bool, _Allocator>::operator=(vector&& __v)
2829        _NOEXCEPT_(
2830             __alloc_traits::propagate_on_container_move_assignment::value &&
2831             is_nothrow_move_assignable<allocator_type>::value)
2832{
2833    __move_assign(__v, integral_constant<bool,
2834          __storage_traits::propagate_on_container_move_assignment::value>());
2835    return *this;
2836}
2837
2838template <class _Allocator>
2839void
2840vector<bool, _Allocator>::__move_assign(vector& __c, false_type)
2841{
2842    if (__alloc() != __c.__alloc())
2843        assign(__c.begin(), __c.end());
2844    else
2845        __move_assign(__c, true_type());
2846}
2847
2848template <class _Allocator>
2849void
2850vector<bool, _Allocator>::__move_assign(vector& __c, true_type)
2851    _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
2852{
2853    deallocate();
2854    this->__begin_ = __c.__begin_;
2855    this->__size_ = __c.__size_;
2856    this->__cap() = __c.__cap();
2857    __move_assign_alloc(__c);
2858    __c.__begin_ = nullptr;
2859    __c.__cap() = __c.__size_ = 0;
2860}
2861
2862#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2863
2864template <class _Allocator>
2865void
2866vector<bool, _Allocator>::assign(size_type __n, const value_type& __x)
2867{
2868    __size_ = 0;
2869    if (__n > 0)
2870    {
2871        size_type __c = capacity();
2872        if (__n <= __c)
2873            __size_ = __n;
2874        else
2875        {
2876            vector __v(__alloc());
2877            __v.reserve(__recommend(__n));
2878            __v.__size_ = __n;
2879            swap(__v);
2880        }
2881        _VSTD::fill_n(begin(), __n, __x);
2882    }
2883}
2884
2885template <class _Allocator>
2886template <class _InputIterator>
2887typename enable_if
2888<
2889    __is_input_iterator<_InputIterator>::value &&
2890   !__is_forward_iterator<_InputIterator>::value,
2891   void
2892>::type
2893vector<bool, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2894{
2895    clear();
2896    for (; __first != __last; ++__first)
2897        push_back(*__first);
2898}
2899
2900template <class _Allocator>
2901template <class _ForwardIterator>
2902typename enable_if
2903<
2904    __is_forward_iterator<_ForwardIterator>::value,
2905   void
2906>::type
2907vector<bool, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2908{
2909    clear();
2910    difference_type __n = _VSTD::distance(__first, __last);
2911    if (__n)
2912    {
2913        if (__n > capacity())
2914        {
2915            deallocate();
2916            allocate(__n);
2917        }
2918        __construct_at_end(__first, __last);
2919    }
2920}
2921
2922template <class _Allocator>
2923void
2924vector<bool, _Allocator>::reserve(size_type __n)
2925{
2926    if (__n > capacity())
2927    {
2928        vector __v(this->__alloc());
2929        __v.allocate(__n);
2930        __v.__construct_at_end(this->begin(), this->end());
2931        swap(__v);
2932        __invalidate_all_iterators();
2933    }
2934}
2935
2936template <class _Allocator>
2937void
2938vector<bool, _Allocator>::shrink_to_fit() _NOEXCEPT
2939{
2940    if (__external_cap_to_internal(size()) > __cap())
2941    {
2942#ifndef _LIBCPP_NO_EXCEPTIONS
2943        try
2944        {
2945#endif  // _LIBCPP_NO_EXCEPTIONS
2946            vector(*this, allocator_type(__alloc())).swap(*this);
2947#ifndef _LIBCPP_NO_EXCEPTIONS
2948        }
2949        catch (...)
2950        {
2951        }
2952#endif  // _LIBCPP_NO_EXCEPTIONS
2953    }
2954}
2955
2956template <class _Allocator>
2957typename vector<bool, _Allocator>::reference
2958vector<bool, _Allocator>::at(size_type __n)
2959{
2960    if (__n >= size())
2961        this->__throw_out_of_range();
2962    return (*this)[__n];
2963}
2964
2965template <class _Allocator>
2966typename vector<bool, _Allocator>::const_reference
2967vector<bool, _Allocator>::at(size_type __n) const
2968{
2969    if (__n >= size())
2970        this->__throw_out_of_range();
2971    return (*this)[__n];
2972}
2973
2974template <class _Allocator>
2975void
2976vector<bool, _Allocator>::push_back(const value_type& __x)
2977{
2978    if (this->__size_ == this->capacity())
2979        reserve(__recommend(this->__size_ + 1));
2980    ++this->__size_;
2981    back() = __x;
2982}
2983
2984template <class _Allocator>
2985typename vector<bool, _Allocator>::iterator
2986vector<bool, _Allocator>::insert(const_iterator __position, const value_type& __x)
2987{
2988    iterator __r;
2989    if (size() < capacity())
2990    {
2991        const_iterator __old_end = end();
2992        ++__size_;
2993        _VSTD::copy_backward(__position, __old_end, end());
2994        __r = __const_iterator_cast(__position);
2995    }
2996    else
2997    {
2998        vector __v(__alloc());
2999        __v.reserve(__recommend(__size_ + 1));
3000        __v.__size_ = __size_ + 1;
3001        __r = _VSTD::copy(cbegin(), __position, __v.begin());
3002        _VSTD::copy_backward(__position, cend(), __v.end());
3003        swap(__v);
3004    }
3005    *__r = __x;
3006    return __r;
3007}
3008
3009template <class _Allocator>
3010typename vector<bool, _Allocator>::iterator
3011vector<bool, _Allocator>::insert(const_iterator __position, size_type __n, const value_type& __x)
3012{
3013    iterator __r;
3014    size_type __c = capacity();
3015    if (__n <= __c && size() <= __c - __n)
3016    {
3017        const_iterator __old_end = end();
3018        __size_ += __n;
3019        _VSTD::copy_backward(__position, __old_end, end());
3020        __r = __const_iterator_cast(__position);
3021    }
3022    else
3023    {
3024        vector __v(__alloc());
3025        __v.reserve(__recommend(__size_ + __n));
3026        __v.__size_ = __size_ + __n;
3027        __r = _VSTD::copy(cbegin(), __position, __v.begin());
3028        _VSTD::copy_backward(__position, cend(), __v.end());
3029        swap(__v);
3030    }
3031    _VSTD::fill_n(__r, __n, __x);
3032    return __r;
3033}
3034
3035template <class _Allocator>
3036template <class _InputIterator>
3037typename enable_if
3038<
3039     __is_input_iterator  <_InputIterator>::value &&
3040    !__is_forward_iterator<_InputIterator>::value,
3041    typename vector<bool, _Allocator>::iterator
3042>::type
3043vector<bool, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
3044{
3045    difference_type __off = __position - begin();
3046    iterator __p = __const_iterator_cast(__position);
3047    iterator __old_end = end();
3048    for (; size() != capacity() && __first != __last; ++__first)
3049    {
3050        ++this->__size_;
3051        back() = *__first;
3052    }
3053    vector __v(__alloc());
3054    if (__first != __last)
3055    {
3056#ifndef _LIBCPP_NO_EXCEPTIONS
3057        try
3058        {
3059#endif  // _LIBCPP_NO_EXCEPTIONS
3060            __v.assign(__first, __last);
3061            difference_type __old_size = static_cast<difference_type>(__old_end - begin());
3062            difference_type __old_p = __p - begin();
3063            reserve(__recommend(size() + __v.size()));
3064            __p = begin() + __old_p;
3065            __old_end = begin() + __old_size;
3066#ifndef _LIBCPP_NO_EXCEPTIONS
3067        }
3068        catch (...)
3069        {
3070            erase(__old_end, end());
3071            throw;
3072        }
3073#endif  // _LIBCPP_NO_EXCEPTIONS
3074    }
3075    __p = _VSTD::rotate(__p, __old_end, end());
3076    insert(__p, __v.begin(), __v.end());
3077    return begin() + __off;
3078}
3079
3080template <class _Allocator>
3081template <class _ForwardIterator>
3082typename enable_if
3083<
3084    __is_forward_iterator<_ForwardIterator>::value,
3085    typename vector<bool, _Allocator>::iterator
3086>::type
3087vector<bool, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
3088{
3089    difference_type __n = _VSTD::distance(__first, __last);
3090    iterator __r;
3091    size_type __c = capacity();
3092    if (__n <= __c && size() <= __c - __n)
3093    {
3094        const_iterator __old_end = end();
3095        __size_ += __n;
3096        _VSTD::copy_backward(__position, __old_end, end());
3097        __r = __const_iterator_cast(__position);
3098    }
3099    else
3100    {
3101        vector __v(__alloc());
3102        __v.reserve(__recommend(__size_ + __n));
3103        __v.__size_ = __size_ + __n;
3104        __r = _VSTD::copy(cbegin(), __position, __v.begin());
3105        _VSTD::copy_backward(__position, cend(), __v.end());
3106        swap(__v);
3107    }
3108    _VSTD::copy(__first, __last, __r);
3109    return __r;
3110}
3111
3112template <class _Allocator>
3113inline _LIBCPP_INLINE_VISIBILITY
3114typename vector<bool, _Allocator>::iterator
3115vector<bool, _Allocator>::erase(const_iterator __position)
3116{
3117    iterator __r = __const_iterator_cast(__position);
3118    _VSTD::copy(__position + 1, this->cend(), __r);
3119    --__size_;
3120    return __r;
3121}
3122
3123template <class _Allocator>
3124typename vector<bool, _Allocator>::iterator
3125vector<bool, _Allocator>::erase(const_iterator __first, const_iterator __last)
3126{
3127    iterator __r = __const_iterator_cast(__first);
3128    difference_type __d = __last - __first;
3129    _VSTD::copy(__last, this->cend(), __r);
3130    __size_ -= __d;
3131    return __r;
3132}
3133
3134template <class _Allocator>
3135void
3136vector<bool, _Allocator>::swap(vector& __x)
3137        _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
3138                   __is_nothrow_swappable<allocator_type>::value)
3139{
3140    _VSTD::swap(this->__begin_, __x.__begin_);
3141    _VSTD::swap(this->__size_, __x.__size_);
3142    _VSTD::swap(this->__cap(), __x.__cap());
3143    __swap_alloc(this->__alloc(), __x.__alloc());
3144}
3145
3146template <class _Allocator>
3147void
3148vector<bool, _Allocator>::resize(size_type __sz, value_type __x)
3149{
3150    size_type __cs = size();
3151    if (__cs < __sz)
3152    {
3153        iterator __r;
3154        size_type __c = capacity();
3155        size_type __n = __sz - __cs;
3156        if (__n <= __c && __cs <= __c - __n)
3157        {
3158            __r = end();
3159            __size_ += __n;
3160        }
3161        else
3162        {
3163            vector __v(__alloc());
3164            __v.reserve(__recommend(__size_ + __n));
3165            __v.__size_ = __size_ + __n;
3166            __r = _VSTD::copy(cbegin(), cend(), __v.begin());
3167            swap(__v);
3168        }
3169        _VSTD::fill_n(__r, __n, __x);
3170    }
3171    else
3172        __size_ = __sz;
3173}
3174
3175template <class _Allocator>
3176void
3177vector<bool, _Allocator>::flip() _NOEXCEPT
3178{
3179    // do middle whole words
3180    size_type __n = __size_;
3181    __storage_pointer __p = __begin_;
3182    for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3183        *__p = ~*__p;
3184    // do last partial word
3185    if (__n > 0)
3186    {
3187        __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3188        __storage_type __b = *__p & __m;
3189        *__p &= ~__m;
3190        *__p |= ~__b & __m;
3191    }
3192}
3193
3194template <class _Allocator>
3195bool
3196vector<bool, _Allocator>::__invariants() const
3197{
3198    if (this->__begin_ == nullptr)
3199    {
3200        if (this->__size_ != 0 || this->__cap() != 0)
3201            return false;
3202    }
3203    else
3204    {
3205        if (this->__cap() == 0)
3206            return false;
3207        if (this->__size_ > this->capacity())
3208            return false;
3209    }
3210    return true;
3211}
3212
3213template <class _Allocator>
3214size_t
3215vector<bool, _Allocator>::__hash_code() const _NOEXCEPT
3216{
3217    size_t __h = 0;
3218    // do middle whole words
3219    size_type __n = __size_;
3220    __storage_pointer __p = __begin_;
3221    for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3222        __h ^= *__p;
3223    // do last partial word
3224    if (__n > 0)
3225    {
3226        const __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3227        __h ^= *__p & __m;
3228    }
3229    return __h;
3230}
3231
3232template <class _Allocator>
3233struct _LIBCPP_TYPE_VIS_ONLY hash<vector<bool, _Allocator> >
3234    : public unary_function<vector<bool, _Allocator>, size_t>
3235{
3236    _LIBCPP_INLINE_VISIBILITY
3237    size_t operator()(const vector<bool, _Allocator>& __vec) const _NOEXCEPT
3238        {return __vec.__hash_code();}
3239};
3240
3241template <class _Tp, class _Allocator>
3242inline _LIBCPP_INLINE_VISIBILITY
3243bool
3244operator==(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3245{
3246    const typename vector<_Tp, _Allocator>::size_type __sz = __x.size();
3247    return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
3248}
3249
3250template <class _Tp, class _Allocator>
3251inline _LIBCPP_INLINE_VISIBILITY
3252bool
3253operator!=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3254{
3255    return !(__x == __y);
3256}
3257
3258template <class _Tp, class _Allocator>
3259inline _LIBCPP_INLINE_VISIBILITY
3260bool
3261operator< (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3262{
3263    return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
3264}
3265
3266template <class _Tp, class _Allocator>
3267inline _LIBCPP_INLINE_VISIBILITY
3268bool
3269operator> (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3270{
3271    return __y < __x;
3272}
3273
3274template <class _Tp, class _Allocator>
3275inline _LIBCPP_INLINE_VISIBILITY
3276bool
3277operator>=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3278{
3279    return !(__x < __y);
3280}
3281
3282template <class _Tp, class _Allocator>
3283inline _LIBCPP_INLINE_VISIBILITY
3284bool
3285operator<=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3286{
3287    return !(__y < __x);
3288}
3289
3290template <class _Tp, class _Allocator>
3291inline _LIBCPP_INLINE_VISIBILITY
3292void
3293swap(vector<_Tp, _Allocator>& __x, vector<_Tp, _Allocator>& __y)
3294    _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
3295{
3296    __x.swap(__y);
3297}
3298
3299_LIBCPP_END_NAMESPACE_STD
3300
3301#endif  // _LIBCPP_VECTOR
3302