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