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