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