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