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