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