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