xref: /freebsd/contrib/llvm-project/libcxx/include/vector (revision 4824e7fd18a1223177218d4aec1b3c6c5c4a444e)
10b57cec5SDimitry Andric// -*- C++ -*-
2349cc55cSDimitry Andric//===----------------------------------------------------------------------===//
30b57cec5SDimitry Andric//
40b57cec5SDimitry Andric// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
50b57cec5SDimitry Andric// See https://llvm.org/LICENSE.txt for license information.
60b57cec5SDimitry Andric// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
70b57cec5SDimitry Andric//
80b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
90b57cec5SDimitry Andric
100b57cec5SDimitry Andric#ifndef _LIBCPP_VECTOR
110b57cec5SDimitry Andric#define _LIBCPP_VECTOR
120b57cec5SDimitry Andric
130b57cec5SDimitry Andric/*
140b57cec5SDimitry Andric    vector synopsis
150b57cec5SDimitry Andric
160b57cec5SDimitry Andricnamespace std
170b57cec5SDimitry Andric{
180b57cec5SDimitry Andric
190b57cec5SDimitry Andrictemplate <class T, class Allocator = allocator<T> >
200b57cec5SDimitry Andricclass vector
210b57cec5SDimitry Andric{
220b57cec5SDimitry Andricpublic:
230b57cec5SDimitry Andric    typedef T                                        value_type;
240b57cec5SDimitry Andric    typedef Allocator                                allocator_type;
250b57cec5SDimitry Andric    typedef typename allocator_type::reference       reference;
260b57cec5SDimitry Andric    typedef typename allocator_type::const_reference const_reference;
270b57cec5SDimitry Andric    typedef implementation-defined                   iterator;
280b57cec5SDimitry Andric    typedef implementation-defined                   const_iterator;
290b57cec5SDimitry Andric    typedef typename allocator_type::size_type       size_type;
300b57cec5SDimitry Andric    typedef typename allocator_type::difference_type difference_type;
310b57cec5SDimitry Andric    typedef typename allocator_type::pointer         pointer;
320b57cec5SDimitry Andric    typedef typename allocator_type::const_pointer   const_pointer;
330b57cec5SDimitry Andric    typedef std::reverse_iterator<iterator>          reverse_iterator;
340b57cec5SDimitry Andric    typedef std::reverse_iterator<const_iterator>    const_reverse_iterator;
350b57cec5SDimitry Andric
360b57cec5SDimitry Andric    vector()
370b57cec5SDimitry Andric        noexcept(is_nothrow_default_constructible<allocator_type>::value);
380b57cec5SDimitry Andric    explicit vector(const allocator_type&);
390b57cec5SDimitry Andric    explicit vector(size_type n);
400b57cec5SDimitry Andric    explicit vector(size_type n, const allocator_type&); // C++14
410b57cec5SDimitry Andric    vector(size_type n, const value_type& value, const allocator_type& = allocator_type());
420b57cec5SDimitry Andric    template <class InputIterator>
430b57cec5SDimitry Andric        vector(InputIterator first, InputIterator last, const allocator_type& = allocator_type());
440b57cec5SDimitry Andric    vector(const vector& x);
450b57cec5SDimitry Andric    vector(vector&& x)
460b57cec5SDimitry Andric        noexcept(is_nothrow_move_constructible<allocator_type>::value);
470b57cec5SDimitry Andric    vector(initializer_list<value_type> il);
480b57cec5SDimitry Andric    vector(initializer_list<value_type> il, const allocator_type& a);
490b57cec5SDimitry Andric    ~vector();
500b57cec5SDimitry Andric    vector& operator=(const vector& x);
510b57cec5SDimitry Andric    vector& operator=(vector&& x)
520b57cec5SDimitry Andric        noexcept(
530b57cec5SDimitry Andric             allocator_type::propagate_on_container_move_assignment::value ||
540b57cec5SDimitry Andric             allocator_type::is_always_equal::value); // C++17
550b57cec5SDimitry Andric    vector& operator=(initializer_list<value_type> il);
560b57cec5SDimitry Andric    template <class InputIterator>
570b57cec5SDimitry Andric        void assign(InputIterator first, InputIterator last);
580b57cec5SDimitry Andric    void assign(size_type n, const value_type& u);
590b57cec5SDimitry Andric    void assign(initializer_list<value_type> il);
600b57cec5SDimitry Andric
610b57cec5SDimitry Andric    allocator_type get_allocator() const noexcept;
620b57cec5SDimitry Andric
630b57cec5SDimitry Andric    iterator               begin() noexcept;
640b57cec5SDimitry Andric    const_iterator         begin()   const noexcept;
650b57cec5SDimitry Andric    iterator               end() noexcept;
660b57cec5SDimitry Andric    const_iterator         end()     const noexcept;
670b57cec5SDimitry Andric
680b57cec5SDimitry Andric    reverse_iterator       rbegin() noexcept;
690b57cec5SDimitry Andric    const_reverse_iterator rbegin()  const noexcept;
700b57cec5SDimitry Andric    reverse_iterator       rend() noexcept;
710b57cec5SDimitry Andric    const_reverse_iterator rend()    const noexcept;
720b57cec5SDimitry Andric
730b57cec5SDimitry Andric    const_iterator         cbegin()  const noexcept;
740b57cec5SDimitry Andric    const_iterator         cend()    const noexcept;
750b57cec5SDimitry Andric    const_reverse_iterator crbegin() const noexcept;
760b57cec5SDimitry Andric    const_reverse_iterator crend()   const noexcept;
770b57cec5SDimitry Andric
780b57cec5SDimitry Andric    size_type size() const noexcept;
790b57cec5SDimitry Andric    size_type max_size() const noexcept;
800b57cec5SDimitry Andric    size_type capacity() const noexcept;
810b57cec5SDimitry Andric    bool empty() const noexcept;
820b57cec5SDimitry Andric    void reserve(size_type n);
830b57cec5SDimitry Andric    void shrink_to_fit() noexcept;
840b57cec5SDimitry Andric
850b57cec5SDimitry Andric    reference       operator[](size_type n);
860b57cec5SDimitry Andric    const_reference operator[](size_type n) const;
870b57cec5SDimitry Andric    reference       at(size_type n);
880b57cec5SDimitry Andric    const_reference at(size_type n) const;
890b57cec5SDimitry Andric
900b57cec5SDimitry Andric    reference       front();
910b57cec5SDimitry Andric    const_reference front() const;
920b57cec5SDimitry Andric    reference       back();
930b57cec5SDimitry Andric    const_reference back() const;
940b57cec5SDimitry Andric
950b57cec5SDimitry Andric    value_type*       data() noexcept;
960b57cec5SDimitry Andric    const value_type* data() const noexcept;
970b57cec5SDimitry Andric
980b57cec5SDimitry Andric    void push_back(const value_type& x);
990b57cec5SDimitry Andric    void push_back(value_type&& x);
1000b57cec5SDimitry Andric    template <class... Args>
1010b57cec5SDimitry Andric        reference emplace_back(Args&&... args); // reference in C++17
1020b57cec5SDimitry Andric    void pop_back();
1030b57cec5SDimitry Andric
1040b57cec5SDimitry Andric    template <class... Args> iterator emplace(const_iterator position, Args&&... args);
1050b57cec5SDimitry Andric    iterator insert(const_iterator position, const value_type& x);
1060b57cec5SDimitry Andric    iterator insert(const_iterator position, value_type&& x);
1070b57cec5SDimitry Andric    iterator insert(const_iterator position, size_type n, const value_type& x);
1080b57cec5SDimitry Andric    template <class InputIterator>
1090b57cec5SDimitry Andric        iterator insert(const_iterator position, InputIterator first, InputIterator last);
1100b57cec5SDimitry Andric    iterator insert(const_iterator position, initializer_list<value_type> il);
1110b57cec5SDimitry Andric
1120b57cec5SDimitry Andric    iterator erase(const_iterator position);
1130b57cec5SDimitry Andric    iterator erase(const_iterator first, const_iterator last);
1140b57cec5SDimitry Andric
1150b57cec5SDimitry Andric    void clear() noexcept;
1160b57cec5SDimitry Andric
1170b57cec5SDimitry Andric    void resize(size_type sz);
1180b57cec5SDimitry Andric    void resize(size_type sz, const value_type& c);
1190b57cec5SDimitry Andric
1200b57cec5SDimitry Andric    void swap(vector&)
1210b57cec5SDimitry Andric        noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
1220b57cec5SDimitry Andric                 allocator_traits<allocator_type>::is_always_equal::value);  // C++17
1230b57cec5SDimitry Andric
1240b57cec5SDimitry Andric    bool __invariants() const;
1250b57cec5SDimitry Andric};
1260b57cec5SDimitry Andric
1270b57cec5SDimitry Andrictemplate <class Allocator = allocator<T> >
1280b57cec5SDimitry Andricclass vector<bool, Allocator>
1290b57cec5SDimitry Andric{
1300b57cec5SDimitry Andricpublic:
1310b57cec5SDimitry Andric    typedef bool                                     value_type;
1320b57cec5SDimitry Andric    typedef Allocator                                allocator_type;
1330b57cec5SDimitry Andric    typedef implementation-defined                   iterator;
1340b57cec5SDimitry Andric    typedef implementation-defined                   const_iterator;
1350b57cec5SDimitry Andric    typedef typename allocator_type::size_type       size_type;
1360b57cec5SDimitry Andric    typedef typename allocator_type::difference_type difference_type;
1370b57cec5SDimitry Andric    typedef iterator                                 pointer;
1380b57cec5SDimitry Andric    typedef const_iterator                           const_pointer;
1390b57cec5SDimitry Andric    typedef std::reverse_iterator<iterator>          reverse_iterator;
1400b57cec5SDimitry Andric    typedef std::reverse_iterator<const_iterator>    const_reverse_iterator;
1410b57cec5SDimitry Andric
1420b57cec5SDimitry Andric    class reference
1430b57cec5SDimitry Andric    {
1440b57cec5SDimitry Andric    public:
1450b57cec5SDimitry Andric        reference(const reference&) noexcept;
1460b57cec5SDimitry Andric        operator bool() const noexcept;
147fe6060f1SDimitry Andric        reference& operator=(bool x) noexcept;
1480b57cec5SDimitry Andric        reference& operator=(const reference& x) noexcept;
1490b57cec5SDimitry Andric        iterator operator&() const noexcept;
1500b57cec5SDimitry Andric        void flip() noexcept;
1510b57cec5SDimitry Andric    };
1520b57cec5SDimitry Andric
1530b57cec5SDimitry Andric    class const_reference
1540b57cec5SDimitry Andric    {
1550b57cec5SDimitry Andric    public:
1560b57cec5SDimitry Andric        const_reference(const reference&) noexcept;
1570b57cec5SDimitry Andric        operator bool() const noexcept;
1580b57cec5SDimitry Andric        const_iterator operator&() const noexcept;
1590b57cec5SDimitry Andric    };
1600b57cec5SDimitry Andric
1610b57cec5SDimitry Andric    vector()
1620b57cec5SDimitry Andric        noexcept(is_nothrow_default_constructible<allocator_type>::value);
1630b57cec5SDimitry Andric    explicit vector(const allocator_type&);
1640b57cec5SDimitry Andric    explicit vector(size_type n, const allocator_type& a = allocator_type()); // C++14
1650b57cec5SDimitry Andric    vector(size_type n, const value_type& value, const allocator_type& = allocator_type());
1660b57cec5SDimitry Andric    template <class InputIterator>
1670b57cec5SDimitry Andric        vector(InputIterator first, InputIterator last, const allocator_type& = allocator_type());
1680b57cec5SDimitry Andric    vector(const vector& x);
1690b57cec5SDimitry Andric    vector(vector&& x)
1700b57cec5SDimitry Andric        noexcept(is_nothrow_move_constructible<allocator_type>::value);
1710b57cec5SDimitry Andric    vector(initializer_list<value_type> il);
1720b57cec5SDimitry Andric    vector(initializer_list<value_type> il, const allocator_type& a);
1730b57cec5SDimitry Andric    ~vector();
1740b57cec5SDimitry Andric    vector& operator=(const vector& x);
1750b57cec5SDimitry Andric    vector& operator=(vector&& x)
1760b57cec5SDimitry Andric        noexcept(
1770b57cec5SDimitry Andric             allocator_type::propagate_on_container_move_assignment::value ||
1780b57cec5SDimitry Andric             allocator_type::is_always_equal::value); // C++17
1790b57cec5SDimitry Andric    vector& operator=(initializer_list<value_type> il);
1800b57cec5SDimitry Andric    template <class InputIterator>
1810b57cec5SDimitry Andric        void assign(InputIterator first, InputIterator last);
1820b57cec5SDimitry Andric    void assign(size_type n, const value_type& u);
1830b57cec5SDimitry Andric    void assign(initializer_list<value_type> il);
1840b57cec5SDimitry Andric
1850b57cec5SDimitry Andric    allocator_type get_allocator() const noexcept;
1860b57cec5SDimitry Andric
1870b57cec5SDimitry Andric    iterator               begin() noexcept;
1880b57cec5SDimitry Andric    const_iterator         begin()   const noexcept;
1890b57cec5SDimitry Andric    iterator               end() noexcept;
1900b57cec5SDimitry Andric    const_iterator         end()     const noexcept;
1910b57cec5SDimitry Andric
1920b57cec5SDimitry Andric    reverse_iterator       rbegin() noexcept;
1930b57cec5SDimitry Andric    const_reverse_iterator rbegin()  const noexcept;
1940b57cec5SDimitry Andric    reverse_iterator       rend() noexcept;
1950b57cec5SDimitry Andric    const_reverse_iterator rend()    const noexcept;
1960b57cec5SDimitry Andric
1970b57cec5SDimitry Andric    const_iterator         cbegin()  const noexcept;
1980b57cec5SDimitry Andric    const_iterator         cend()    const noexcept;
1990b57cec5SDimitry Andric    const_reverse_iterator crbegin() const noexcept;
2000b57cec5SDimitry Andric    const_reverse_iterator crend()   const noexcept;
2010b57cec5SDimitry Andric
2020b57cec5SDimitry Andric    size_type size() const noexcept;
2030b57cec5SDimitry Andric    size_type max_size() const noexcept;
2040b57cec5SDimitry Andric    size_type capacity() const noexcept;
2050b57cec5SDimitry Andric    bool empty() const noexcept;
2060b57cec5SDimitry Andric    void reserve(size_type n);
2070b57cec5SDimitry Andric    void shrink_to_fit() noexcept;
2080b57cec5SDimitry Andric
2090b57cec5SDimitry Andric    reference       operator[](size_type n);
2100b57cec5SDimitry Andric    const_reference operator[](size_type n) const;
2110b57cec5SDimitry Andric    reference       at(size_type n);
2120b57cec5SDimitry Andric    const_reference at(size_type n) const;
2130b57cec5SDimitry Andric
2140b57cec5SDimitry Andric    reference       front();
2150b57cec5SDimitry Andric    const_reference front() const;
2160b57cec5SDimitry Andric    reference       back();
2170b57cec5SDimitry Andric    const_reference back() const;
2180b57cec5SDimitry Andric
2190b57cec5SDimitry Andric    void push_back(const value_type& x);
2200b57cec5SDimitry Andric    template <class... Args> reference emplace_back(Args&&... args);  // C++14; reference in C++17
2210b57cec5SDimitry Andric    void pop_back();
2220b57cec5SDimitry Andric
2230b57cec5SDimitry Andric    template <class... Args> iterator emplace(const_iterator position, Args&&... args);  // C++14
2240b57cec5SDimitry Andric    iterator insert(const_iterator position, const value_type& x);
2250b57cec5SDimitry Andric    iterator insert(const_iterator position, size_type n, const value_type& x);
2260b57cec5SDimitry Andric    template <class InputIterator>
2270b57cec5SDimitry Andric        iterator insert(const_iterator position, InputIterator first, InputIterator last);
2280b57cec5SDimitry Andric    iterator insert(const_iterator position, initializer_list<value_type> il);
2290b57cec5SDimitry Andric
2300b57cec5SDimitry Andric    iterator erase(const_iterator position);
2310b57cec5SDimitry Andric    iterator erase(const_iterator first, const_iterator last);
2320b57cec5SDimitry Andric
2330b57cec5SDimitry Andric    void clear() noexcept;
2340b57cec5SDimitry Andric
2350b57cec5SDimitry Andric    void resize(size_type sz);
2360b57cec5SDimitry Andric    void resize(size_type sz, value_type x);
2370b57cec5SDimitry Andric
2380b57cec5SDimitry Andric    void swap(vector&)
2390b57cec5SDimitry Andric        noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
2400b57cec5SDimitry Andric                 allocator_traits<allocator_type>::is_always_equal::value);  // C++17
2410b57cec5SDimitry Andric    void flip() noexcept;
2420b57cec5SDimitry Andric
2430b57cec5SDimitry Andric    bool __invariants() const;
2440b57cec5SDimitry Andric};
2450b57cec5SDimitry Andric
2460b57cec5SDimitry Andrictemplate <class InputIterator, class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
2470b57cec5SDimitry Andric   vector(InputIterator, InputIterator, Allocator = Allocator())
248349cc55cSDimitry Andric   -> vector<typename iterator_traits<InputIterator>::value_type, Allocator>; // C++17
2490b57cec5SDimitry Andric
2500b57cec5SDimitry Andrictemplate <class Allocator> struct hash<std::vector<bool, Allocator>>;
2510b57cec5SDimitry Andric
2520b57cec5SDimitry Andrictemplate <class T, class Allocator> bool operator==(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
2530b57cec5SDimitry Andrictemplate <class T, class Allocator> bool operator< (const vector<T,Allocator>& x, const vector<T,Allocator>& y);
2540b57cec5SDimitry Andrictemplate <class T, class Allocator> bool operator!=(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
2550b57cec5SDimitry Andrictemplate <class T, class Allocator> bool operator> (const vector<T,Allocator>& x, const vector<T,Allocator>& y);
2560b57cec5SDimitry Andrictemplate <class T, class Allocator> bool operator>=(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
2570b57cec5SDimitry Andrictemplate <class T, class Allocator> bool operator<=(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
2580b57cec5SDimitry Andric
2590b57cec5SDimitry Andrictemplate <class T, class Allocator>
2600b57cec5SDimitry Andricvoid swap(vector<T,Allocator>& x, vector<T,Allocator>& y)
2610b57cec5SDimitry Andric    noexcept(noexcept(x.swap(y)));
2620b57cec5SDimitry Andric
2630b57cec5SDimitry Andrictemplate <class T, class Allocator, class U>
2645ffd83dbSDimitry Andrictypename vector<T, Allocator>::size_type
2655ffd83dbSDimitry Andricerase(vector<T, Allocator>& c, const U& value);       // C++20
2660b57cec5SDimitry Andrictemplate <class T, class Allocator, class Predicate>
2675ffd83dbSDimitry Andrictypename vector<T, Allocator>::size_type
2685ffd83dbSDimitry Andricerase_if(vector<T, Allocator>& c, Predicate pred);    // C++20
2690b57cec5SDimitry Andric
2700b57cec5SDimitry Andric}  // std
2710b57cec5SDimitry Andric
2720b57cec5SDimitry Andric*/
2730b57cec5SDimitry Andric
2740b57cec5SDimitry Andric#include <__config>
2750b57cec5SDimitry Andric#include <__bit_reference>
276fe6060f1SDimitry Andric#include <__debug>
277fe6060f1SDimitry Andric#include <__functional_base>
278349cc55cSDimitry Andric#include <__iterator/iterator_traits.h>
279fe6060f1SDimitry Andric#include <__iterator/wrap_iter.h>
280fe6060f1SDimitry Andric#include <__split_buffer>
281fe6060f1SDimitry Andric#include <__utility/forward.h>
282fe6060f1SDimitry Andric#include <algorithm>
2830b57cec5SDimitry Andric#include <climits>
284fe6060f1SDimitry Andric#include <compare>
28569ade1e0SDimitry Andric#include <cstdlib>
286fe6060f1SDimitry Andric#include <cstring>
2870b57cec5SDimitry Andric#include <initializer_list>
288fe6060f1SDimitry Andric#include <iosfwd> // for forward declaration of vector
289fe6060f1SDimitry Andric#include <limits>
2900b57cec5SDimitry Andric#include <memory>
2910b57cec5SDimitry Andric#include <stdexcept>
292fe6060f1SDimitry Andric#include <type_traits>
2930b57cec5SDimitry Andric#include <version>
2940b57cec5SDimitry Andric
2950b57cec5SDimitry Andric#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
2960b57cec5SDimitry Andric#pragma GCC system_header
2970b57cec5SDimitry Andric#endif
2980b57cec5SDimitry Andric
2990b57cec5SDimitry Andric_LIBCPP_PUSH_MACROS
3000b57cec5SDimitry Andric#include <__undef_macros>
3010b57cec5SDimitry Andric
3020b57cec5SDimitry Andric
3030b57cec5SDimitry Andric_LIBCPP_BEGIN_NAMESPACE_STD
3040b57cec5SDimitry Andric
3050b57cec5SDimitry Andrictemplate <bool>
306349cc55cSDimitry Andricstruct __vector_base_common;
307349cc55cSDimitry Andric
308349cc55cSDimitry Andrictemplate <>
309349cc55cSDimitry Andricstruct __vector_base_common<true> {
310349cc55cSDimitry Andric    // Both are defined in vector.cpp
311349cc55cSDimitry Andric    _LIBCPP_NORETURN _LIBCPP_EXPORTED_FROM_ABI void __throw_length_error() const;
312349cc55cSDimitry Andric    _LIBCPP_NORETURN _LIBCPP_EXPORTED_FROM_ABI void __throw_out_of_range() const;
3130b57cec5SDimitry Andric};
3140b57cec5SDimitry Andric
3150b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
3160b57cec5SDimitry Andricclass __vector_base
317349cc55cSDimitry Andric    : protected __vector_base_common<true> // This base class is historical, but it needs to remain for ABI compatibility
3180b57cec5SDimitry Andric{
3190b57cec5SDimitry Andric    typedef _Allocator                                           allocator_type;
320349cc55cSDimitry Andric    typedef typename allocator_traits<allocator_type>::pointer   pointer;
3210b57cec5SDimitry Andric
322349cc55cSDimitry Andricprotected:
3230b57cec5SDimitry Andric    pointer                                                      __begin_;
3240b57cec5SDimitry Andric    pointer                                                      __end_;
3250b57cec5SDimitry Andric    __compressed_pair<pointer, allocator_type>                   __end_cap_;
3260b57cec5SDimitry Andric
3270b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
3280b57cec5SDimitry Andric    __vector_base()
3290b57cec5SDimitry Andric        _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
3300b57cec5SDimitry Andric      : __begin_(nullptr),
3310b57cec5SDimitry Andric        __end_(nullptr),
332349cc55cSDimitry Andric        __end_cap_(nullptr, __default_init_tag()) {}
3330b57cec5SDimitry Andric
334349cc55cSDimitry Andric    _LIBCPP_INLINE_VISIBILITY __vector_base(const allocator_type& __a)
3350b57cec5SDimitry Andric      : __begin_(nullptr),
3360b57cec5SDimitry Andric        __end_(nullptr),
337349cc55cSDimitry Andric        __end_cap_(nullptr, __a) {}
3380b57cec5SDimitry Andric
3390b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
340349cc55cSDimitry Andric    _LIBCPP_INLINE_VISIBILITY __vector_base(allocator_type&& __a) _NOEXCEPT
3410b57cec5SDimitry Andric      : __begin_(nullptr),
3420b57cec5SDimitry Andric        __end_(nullptr),
343e8d8bef9SDimitry Andric        __end_cap_(nullptr, _VSTD::move(__a)) {}
3440b57cec5SDimitry Andric#endif
345349cc55cSDimitry Andric};
3460b57cec5SDimitry Andric
3470b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator /* = allocator<_Tp> */>
3480b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS vector
349349cc55cSDimitry Andric    // This base class is historical, but it needs to remain for ABI compatibility.
3500b57cec5SDimitry Andric    : private __vector_base<_Tp, _Allocator>
3510b57cec5SDimitry Andric{
3520b57cec5SDimitry Andricprivate:
3530b57cec5SDimitry Andric    typedef __vector_base<_Tp, _Allocator>                  __base;
3540b57cec5SDimitry Andric    typedef allocator<_Tp>                                  __default_allocator_type;
3550b57cec5SDimitry Andricpublic:
3560b57cec5SDimitry Andric    typedef vector                                          __self;
3570b57cec5SDimitry Andric    typedef _Tp                                             value_type;
3580b57cec5SDimitry Andric    typedef _Allocator                                      allocator_type;
359349cc55cSDimitry Andric    typedef allocator_traits<allocator_type>                __alloc_traits;
360349cc55cSDimitry Andric    typedef value_type&                                     reference;
361349cc55cSDimitry Andric    typedef const value_type&                               const_reference;
362*4824e7fdSDimitry Andric    typedef typename __alloc_traits::size_type              size_type;
363349cc55cSDimitry Andric    typedef typename __alloc_traits::difference_type        difference_type;
364349cc55cSDimitry Andric    typedef typename __alloc_traits::pointer                pointer;
365349cc55cSDimitry Andric    typedef typename __alloc_traits::const_pointer          const_pointer;
3660b57cec5SDimitry Andric    typedef __wrap_iter<pointer>                            iterator;
3670b57cec5SDimitry Andric    typedef __wrap_iter<const_pointer>                      const_iterator;
3680b57cec5SDimitry Andric    typedef _VSTD::reverse_iterator<iterator>               reverse_iterator;
3690b57cec5SDimitry Andric    typedef _VSTD::reverse_iterator<const_iterator>         const_reverse_iterator;
3700b57cec5SDimitry Andric
3710b57cec5SDimitry Andric    static_assert((is_same<typename allocator_type::value_type, value_type>::value),
3720b57cec5SDimitry Andric                  "Allocator::value_type must be same type as value_type");
3730b57cec5SDimitry Andric
3740b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
3750b57cec5SDimitry Andric    vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
3760b57cec5SDimitry Andric        {
377e8d8bef9SDimitry Andric#if _LIBCPP_DEBUG_LEVEL == 2
3780b57cec5SDimitry Andric            __get_db()->__insert_c(this);
3790b57cec5SDimitry Andric#endif
3800b57cec5SDimitry Andric        }
3810b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
3820b57cec5SDimitry Andric#if _LIBCPP_STD_VER <= 14
3830b57cec5SDimitry Andric        _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
3840b57cec5SDimitry Andric#else
3850b57cec5SDimitry Andric        _NOEXCEPT
3860b57cec5SDimitry Andric#endif
3870b57cec5SDimitry Andric        : __base(__a)
3880b57cec5SDimitry Andric    {
389e8d8bef9SDimitry Andric#if _LIBCPP_DEBUG_LEVEL == 2
3900b57cec5SDimitry Andric        __get_db()->__insert_c(this);
3910b57cec5SDimitry Andric#endif
3920b57cec5SDimitry Andric    }
3930b57cec5SDimitry Andric    explicit vector(size_type __n);
3940b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 11
3950b57cec5SDimitry Andric    explicit vector(size_type __n, const allocator_type& __a);
3960b57cec5SDimitry Andric#endif
3970b57cec5SDimitry Andric    vector(size_type __n, const value_type& __x);
398*4824e7fdSDimitry Andric
399*4824e7fdSDimitry Andric    template <class = __enable_if_t<__is_allocator<_Allocator>::value> >
400*4824e7fdSDimitry Andric    vector(size_type __n, const value_type& __x, const allocator_type& __a)
401*4824e7fdSDimitry Andric        : __base(__a)
402*4824e7fdSDimitry Andric    {
403*4824e7fdSDimitry Andric#if _LIBCPP_DEBUG_LEVEL == 2
404*4824e7fdSDimitry Andric      __get_db()->__insert_c(this);
405*4824e7fdSDimitry Andric#endif
406*4824e7fdSDimitry Andric      if (__n > 0)
407*4824e7fdSDimitry Andric      {
408*4824e7fdSDimitry Andric          __vallocate(__n);
409*4824e7fdSDimitry Andric          __construct_at_end(__n, __x);
410*4824e7fdSDimitry Andric      }
411*4824e7fdSDimitry Andric    }
412*4824e7fdSDimitry Andric
4130b57cec5SDimitry Andric    template <class _InputIterator>
4140b57cec5SDimitry Andric        vector(_InputIterator __first,
415480093f4SDimitry Andric               typename enable_if<__is_cpp17_input_iterator  <_InputIterator>::value &&
416480093f4SDimitry Andric                                 !__is_cpp17_forward_iterator<_InputIterator>::value &&
4170b57cec5SDimitry Andric                                 is_constructible<
4180b57cec5SDimitry Andric                                    value_type,
4190b57cec5SDimitry Andric                                    typename iterator_traits<_InputIterator>::reference>::value,
4200b57cec5SDimitry Andric                                 _InputIterator>::type __last);
4210b57cec5SDimitry Andric    template <class _InputIterator>
4220b57cec5SDimitry Andric        vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
423480093f4SDimitry Andric               typename enable_if<__is_cpp17_input_iterator  <_InputIterator>::value &&
424480093f4SDimitry Andric                                 !__is_cpp17_forward_iterator<_InputIterator>::value &&
4250b57cec5SDimitry Andric                                 is_constructible<
4260b57cec5SDimitry Andric                                    value_type,
4270b57cec5SDimitry Andric                                    typename iterator_traits<_InputIterator>::reference>::value>::type* = 0);
4280b57cec5SDimitry Andric    template <class _ForwardIterator>
4290b57cec5SDimitry Andric        vector(_ForwardIterator __first,
430480093f4SDimitry Andric               typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value &&
4310b57cec5SDimitry Andric                                 is_constructible<
4320b57cec5SDimitry Andric                                    value_type,
4330b57cec5SDimitry Andric                                    typename iterator_traits<_ForwardIterator>::reference>::value,
4340b57cec5SDimitry Andric                                 _ForwardIterator>::type __last);
4350b57cec5SDimitry Andric    template <class _ForwardIterator>
4360b57cec5SDimitry Andric        vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
437480093f4SDimitry Andric               typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value &&
4380b57cec5SDimitry Andric                                 is_constructible<
4390b57cec5SDimitry Andric                                    value_type,
4400b57cec5SDimitry Andric                                    typename iterator_traits<_ForwardIterator>::reference>::value>::type* = 0);
4410b57cec5SDimitry Andric
4420b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
4430b57cec5SDimitry Andric    ~vector()
4440b57cec5SDimitry Andric    {
4450b57cec5SDimitry Andric      __annotate_delete();
446e8d8bef9SDimitry Andric#if _LIBCPP_DEBUG_LEVEL == 2
4470b57cec5SDimitry Andric      __get_db()->__erase_c(this);
4480b57cec5SDimitry Andric#endif
449349cc55cSDimitry Andric
450349cc55cSDimitry Andric      if (this->__begin_ != nullptr)
451349cc55cSDimitry Andric      {
452349cc55cSDimitry Andric        __clear();
453349cc55cSDimitry Andric        __alloc_traits::deallocate(__alloc(), this->__begin_, capacity());
454349cc55cSDimitry Andric      }
4550b57cec5SDimitry Andric    }
4560b57cec5SDimitry Andric
4570b57cec5SDimitry Andric    vector(const vector& __x);
458fe6060f1SDimitry Andric    vector(const vector& __x, const __identity_t<allocator_type>& __a);
4590b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
4600b57cec5SDimitry Andric    vector& operator=(const vector& __x);
4610b57cec5SDimitry Andric
4620b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
4630b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
4640b57cec5SDimitry Andric    vector(initializer_list<value_type> __il);
4650b57cec5SDimitry Andric
4660b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
4670b57cec5SDimitry Andric    vector(initializer_list<value_type> __il, const allocator_type& __a);
4680b57cec5SDimitry Andric
4690b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
4700b57cec5SDimitry Andric    vector(vector&& __x)
4710b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 14
4720b57cec5SDimitry Andric        _NOEXCEPT;
4730b57cec5SDimitry Andric#else
4740b57cec5SDimitry Andric        _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
4750b57cec5SDimitry Andric#endif
4760b57cec5SDimitry Andric
4770b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
478fe6060f1SDimitry Andric    vector(vector&& __x, const __identity_t<allocator_type>& __a);
4790b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
4800b57cec5SDimitry Andric    vector& operator=(vector&& __x)
4810b57cec5SDimitry Andric        _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
4820b57cec5SDimitry Andric
4830b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
4840b57cec5SDimitry Andric    vector& operator=(initializer_list<value_type> __il)
4850b57cec5SDimitry Andric        {assign(__il.begin(), __il.end()); return *this;}
4860b57cec5SDimitry Andric
4870b57cec5SDimitry Andric#endif // !_LIBCPP_CXX03_LANG
4880b57cec5SDimitry Andric
4890b57cec5SDimitry Andric    template <class _InputIterator>
4900b57cec5SDimitry Andric        typename enable_if
4910b57cec5SDimitry Andric        <
492480093f4SDimitry Andric             __is_cpp17_input_iterator  <_InputIterator>::value &&
493480093f4SDimitry Andric            !__is_cpp17_forward_iterator<_InputIterator>::value &&
4940b57cec5SDimitry Andric            is_constructible<
4950b57cec5SDimitry Andric                 value_type,
4960b57cec5SDimitry Andric                 typename iterator_traits<_InputIterator>::reference>::value,
4970b57cec5SDimitry Andric            void
4980b57cec5SDimitry Andric        >::type
4990b57cec5SDimitry Andric        assign(_InputIterator __first, _InputIterator __last);
5000b57cec5SDimitry Andric    template <class _ForwardIterator>
5010b57cec5SDimitry Andric        typename enable_if
5020b57cec5SDimitry Andric        <
503480093f4SDimitry Andric            __is_cpp17_forward_iterator<_ForwardIterator>::value &&
5040b57cec5SDimitry Andric            is_constructible<
5050b57cec5SDimitry Andric                 value_type,
5060b57cec5SDimitry Andric                 typename iterator_traits<_ForwardIterator>::reference>::value,
5070b57cec5SDimitry Andric            void
5080b57cec5SDimitry Andric        >::type
5090b57cec5SDimitry Andric        assign(_ForwardIterator __first, _ForwardIterator __last);
5100b57cec5SDimitry Andric
5110b57cec5SDimitry Andric    void assign(size_type __n, const_reference __u);
5120b57cec5SDimitry Andric
5130b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
5140b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
5150b57cec5SDimitry Andric    void assign(initializer_list<value_type> __il)
5160b57cec5SDimitry Andric        {assign(__il.begin(), __il.end());}
5170b57cec5SDimitry Andric#endif
5180b57cec5SDimitry Andric
5190b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
5200b57cec5SDimitry Andric    allocator_type get_allocator() const _NOEXCEPT
5210b57cec5SDimitry Andric        {return this->__alloc();}
5220b57cec5SDimitry Andric
5230b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY iterator               begin() _NOEXCEPT;
5240b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY const_iterator         begin()   const _NOEXCEPT;
5250b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY iterator               end() _NOEXCEPT;
5260b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY const_iterator         end()     const _NOEXCEPT;
5270b57cec5SDimitry Andric
5280b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
5290b57cec5SDimitry Andric    reverse_iterator       rbegin() _NOEXCEPT
5300b57cec5SDimitry Andric        {return       reverse_iterator(end());}
5310b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
5320b57cec5SDimitry Andric    const_reverse_iterator rbegin()  const _NOEXCEPT
5330b57cec5SDimitry Andric        {return const_reverse_iterator(end());}
5340b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
5350b57cec5SDimitry Andric    reverse_iterator       rend() _NOEXCEPT
5360b57cec5SDimitry Andric        {return       reverse_iterator(begin());}
5370b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
5380b57cec5SDimitry Andric    const_reverse_iterator rend()    const _NOEXCEPT
5390b57cec5SDimitry Andric        {return const_reverse_iterator(begin());}
5400b57cec5SDimitry Andric
5410b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
5420b57cec5SDimitry Andric    const_iterator         cbegin()  const _NOEXCEPT
5430b57cec5SDimitry Andric        {return begin();}
5440b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
5450b57cec5SDimitry Andric    const_iterator         cend()    const _NOEXCEPT
5460b57cec5SDimitry Andric        {return end();}
5470b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
5480b57cec5SDimitry Andric    const_reverse_iterator crbegin() const _NOEXCEPT
5490b57cec5SDimitry Andric        {return rbegin();}
5500b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
5510b57cec5SDimitry Andric    const_reverse_iterator crend()   const _NOEXCEPT
5520b57cec5SDimitry Andric        {return rend();}
5530b57cec5SDimitry Andric
5540b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
5550b57cec5SDimitry Andric    size_type size() const _NOEXCEPT
5560b57cec5SDimitry Andric        {return static_cast<size_type>(this->__end_ - this->__begin_);}
5570b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
5580b57cec5SDimitry Andric    size_type capacity() const _NOEXCEPT
559349cc55cSDimitry Andric        {return static_cast<size_type>(__end_cap() - this->__begin_);}
5600b57cec5SDimitry Andric    _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
5610b57cec5SDimitry Andric    bool empty() const _NOEXCEPT
5620b57cec5SDimitry Andric        {return this->__begin_ == this->__end_;}
5630b57cec5SDimitry Andric    size_type max_size() const _NOEXCEPT;
5640b57cec5SDimitry Andric    void reserve(size_type __n);
5650b57cec5SDimitry Andric    void shrink_to_fit() _NOEXCEPT;
5660b57cec5SDimitry Andric
5670b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY reference       operator[](size_type __n) _NOEXCEPT;
5680b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const _NOEXCEPT;
5690b57cec5SDimitry Andric    reference       at(size_type __n);
5700b57cec5SDimitry Andric    const_reference at(size_type __n) const;
5710b57cec5SDimitry Andric
5720b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY reference       front() _NOEXCEPT
5730b57cec5SDimitry Andric    {
574fe6060f1SDimitry Andric        _LIBCPP_ASSERT(!empty(), "front() called on an empty vector");
5750b57cec5SDimitry Andric        return *this->__begin_;
5760b57cec5SDimitry Andric    }
5770b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY const_reference front() const _NOEXCEPT
5780b57cec5SDimitry Andric    {
579fe6060f1SDimitry Andric        _LIBCPP_ASSERT(!empty(), "front() called on an empty vector");
5800b57cec5SDimitry Andric        return *this->__begin_;
5810b57cec5SDimitry Andric    }
5820b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY reference       back() _NOEXCEPT
5830b57cec5SDimitry Andric    {
584fe6060f1SDimitry Andric        _LIBCPP_ASSERT(!empty(), "back() called on an empty vector");
5850b57cec5SDimitry Andric        return *(this->__end_ - 1);
5860b57cec5SDimitry Andric    }
5870b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY const_reference back()  const _NOEXCEPT
5880b57cec5SDimitry Andric    {
589fe6060f1SDimitry Andric        _LIBCPP_ASSERT(!empty(), "back() called on an empty vector");
5900b57cec5SDimitry Andric        return *(this->__end_ - 1);
5910b57cec5SDimitry Andric    }
5920b57cec5SDimitry Andric
5930b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
5940b57cec5SDimitry Andric    value_type*       data() _NOEXCEPT
595480093f4SDimitry Andric        {return _VSTD::__to_address(this->__begin_);}
5960b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
5970b57cec5SDimitry Andric    const value_type* data() const _NOEXCEPT
598480093f4SDimitry Andric        {return _VSTD::__to_address(this->__begin_);}
5990b57cec5SDimitry Andric
6000b57cec5SDimitry Andric#ifdef _LIBCPP_CXX03_LANG
6010b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
6020b57cec5SDimitry Andric    void __emplace_back(const value_type& __x) { push_back(__x); }
6030b57cec5SDimitry Andric#else
6040b57cec5SDimitry Andric    template <class _Arg>
6050b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
6060b57cec5SDimitry Andric    void __emplace_back(_Arg&& __arg) {
6070b57cec5SDimitry Andric      emplace_back(_VSTD::forward<_Arg>(__arg));
6080b57cec5SDimitry Andric    }
6090b57cec5SDimitry Andric#endif
6100b57cec5SDimitry Andric
6110b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
6120b57cec5SDimitry Andric
6130b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
6140b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY void push_back(value_type&& __x);
6150b57cec5SDimitry Andric
6160b57cec5SDimitry Andric    template <class... _Args>
6170b57cec5SDimitry Andric        _LIBCPP_INLINE_VISIBILITY
6180b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 14
6190b57cec5SDimitry Andric        reference emplace_back(_Args&&... __args);
6200b57cec5SDimitry Andric#else
6210b57cec5SDimitry Andric        void      emplace_back(_Args&&... __args);
6220b57cec5SDimitry Andric#endif
6230b57cec5SDimitry Andric#endif // !_LIBCPP_CXX03_LANG
6240b57cec5SDimitry Andric
6250b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
6260b57cec5SDimitry Andric    void pop_back();
6270b57cec5SDimitry Andric
6280b57cec5SDimitry Andric    iterator insert(const_iterator __position, const_reference __x);
6290b57cec5SDimitry Andric
6300b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
6310b57cec5SDimitry Andric    iterator insert(const_iterator __position, value_type&& __x);
6320b57cec5SDimitry Andric    template <class... _Args>
6330b57cec5SDimitry Andric        iterator emplace(const_iterator __position, _Args&&... __args);
6340b57cec5SDimitry Andric#endif // !_LIBCPP_CXX03_LANG
6350b57cec5SDimitry Andric
6360b57cec5SDimitry Andric    iterator insert(const_iterator __position, size_type __n, const_reference __x);
6370b57cec5SDimitry Andric    template <class _InputIterator>
6380b57cec5SDimitry Andric        typename enable_if
6390b57cec5SDimitry Andric        <
640480093f4SDimitry Andric             __is_cpp17_input_iterator  <_InputIterator>::value &&
641480093f4SDimitry Andric            !__is_cpp17_forward_iterator<_InputIterator>::value &&
6420b57cec5SDimitry Andric            is_constructible<
6430b57cec5SDimitry Andric                 value_type,
6440b57cec5SDimitry Andric                 typename iterator_traits<_InputIterator>::reference>::value,
6450b57cec5SDimitry Andric            iterator
6460b57cec5SDimitry Andric        >::type
6470b57cec5SDimitry Andric        insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
6480b57cec5SDimitry Andric    template <class _ForwardIterator>
6490b57cec5SDimitry Andric        typename enable_if
6500b57cec5SDimitry Andric        <
651480093f4SDimitry Andric            __is_cpp17_forward_iterator<_ForwardIterator>::value &&
6520b57cec5SDimitry Andric            is_constructible<
6530b57cec5SDimitry Andric                 value_type,
6540b57cec5SDimitry Andric                 typename iterator_traits<_ForwardIterator>::reference>::value,
6550b57cec5SDimitry Andric            iterator
6560b57cec5SDimitry Andric        >::type
6570b57cec5SDimitry Andric        insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
6580b57cec5SDimitry Andric
6590b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
6600b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
6610b57cec5SDimitry Andric    iterator insert(const_iterator __position, initializer_list<value_type> __il)
6620b57cec5SDimitry Andric        {return insert(__position, __il.begin(), __il.end());}
6630b57cec5SDimitry Andric#endif
6640b57cec5SDimitry Andric
6650b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
6660b57cec5SDimitry Andric    iterator erase(const_iterator __first, const_iterator __last);
6670b57cec5SDimitry Andric
6680b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
6690b57cec5SDimitry Andric    void clear() _NOEXCEPT
6700b57cec5SDimitry Andric    {
6710b57cec5SDimitry Andric        size_type __old_size = size();
672349cc55cSDimitry Andric        __clear();
6730b57cec5SDimitry Andric        __annotate_shrink(__old_size);
6740b57cec5SDimitry Andric        __invalidate_all_iterators();
6750b57cec5SDimitry Andric    }
6760b57cec5SDimitry Andric
6770b57cec5SDimitry Andric    void resize(size_type __sz);
6780b57cec5SDimitry Andric    void resize(size_type __sz, const_reference __x);
6790b57cec5SDimitry Andric
6800b57cec5SDimitry Andric    void swap(vector&)
6810b57cec5SDimitry Andric#if _LIBCPP_STD_VER >= 14
6820b57cec5SDimitry Andric        _NOEXCEPT;
6830b57cec5SDimitry Andric#else
6840b57cec5SDimitry Andric        _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
6850b57cec5SDimitry Andric                    __is_nothrow_swappable<allocator_type>::value);
6860b57cec5SDimitry Andric#endif
6870b57cec5SDimitry Andric
6880b57cec5SDimitry Andric    bool __invariants() const;
6890b57cec5SDimitry Andric
690e8d8bef9SDimitry Andric#if _LIBCPP_DEBUG_LEVEL == 2
6910b57cec5SDimitry Andric
6920b57cec5SDimitry Andric    bool __dereferenceable(const const_iterator* __i) const;
6930b57cec5SDimitry Andric    bool __decrementable(const const_iterator* __i) const;
6940b57cec5SDimitry Andric    bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
6950b57cec5SDimitry Andric    bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
6960b57cec5SDimitry Andric
697e8d8bef9SDimitry Andric#endif // _LIBCPP_DEBUG_LEVEL == 2
6980b57cec5SDimitry Andric
6990b57cec5SDimitry Andricprivate:
7000b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
7010b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(pointer __new_last);
7020b57cec5SDimitry Andric    void __vallocate(size_type __n);
7030b57cec5SDimitry Andric    void __vdeallocate() _NOEXCEPT;
7040b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
7050b57cec5SDimitry Andric    void __construct_at_end(size_type __n);
7060b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
7070b57cec5SDimitry Andric    void __construct_at_end(size_type __n, const_reference __x);
7080b57cec5SDimitry Andric    template <class _ForwardIterator>
7090b57cec5SDimitry Andric        typename enable_if
7100b57cec5SDimitry Andric        <
711480093f4SDimitry Andric            __is_cpp17_forward_iterator<_ForwardIterator>::value,
7120b57cec5SDimitry Andric            void
7130b57cec5SDimitry Andric        >::type
7140b57cec5SDimitry Andric        __construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n);
7150b57cec5SDimitry Andric    void __append(size_type __n);
7160b57cec5SDimitry Andric    void __append(size_type __n, const_reference __x);
7170b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
7180b57cec5SDimitry Andric    iterator       __make_iter(pointer __p) _NOEXCEPT;
7190b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
7200b57cec5SDimitry Andric    const_iterator __make_iter(const_pointer __p) const _NOEXCEPT;
7210b57cec5SDimitry Andric    void __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v);
7220b57cec5SDimitry Andric    pointer __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p);
7230b57cec5SDimitry Andric    void __move_range(pointer __from_s, pointer __from_e, pointer __to);
7240b57cec5SDimitry Andric    void __move_assign(vector& __c, true_type)
7250b57cec5SDimitry Andric        _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
7260b57cec5SDimitry Andric    void __move_assign(vector& __c, false_type)
7270b57cec5SDimitry Andric        _NOEXCEPT_(__alloc_traits::is_always_equal::value);
7280b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
7290b57cec5SDimitry Andric    void __destruct_at_end(pointer __new_last) _NOEXCEPT
7300b57cec5SDimitry Andric    {
7310b57cec5SDimitry Andric        __invalidate_iterators_past(__new_last);
7320b57cec5SDimitry Andric        size_type __old_size = size();
733349cc55cSDimitry Andric        __base_destruct_at_end(__new_last);
7340b57cec5SDimitry Andric        __annotate_shrink(__old_size);
7350b57cec5SDimitry Andric    }
7360b57cec5SDimitry Andric
7370b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
7380b57cec5SDimitry Andric    template <class _Up>
7390b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
7400b57cec5SDimitry Andric    inline void __push_back_slow_path(_Up&& __x);
7410b57cec5SDimitry Andric
7420b57cec5SDimitry Andric    template <class... _Args>
7430b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
7440b57cec5SDimitry Andric    inline void __emplace_back_slow_path(_Args&&... __args);
7450b57cec5SDimitry Andric#else
7460b57cec5SDimitry Andric    template <class _Up>
7470b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
7480b57cec5SDimitry Andric    inline void __push_back_slow_path(_Up& __x);
7490b57cec5SDimitry Andric#endif
7500b57cec5SDimitry Andric
7510b57cec5SDimitry Andric    // The following functions are no-ops outside of AddressSanitizer mode.
7520b57cec5SDimitry Andric    // We call annotatations only for the default Allocator because other allocators
7530b57cec5SDimitry Andric    // may not meet the AddressSanitizer alignment constraints.
7540b57cec5SDimitry Andric    // See the documentation for __sanitizer_annotate_contiguous_container for more details.
7550b57cec5SDimitry Andric#ifndef _LIBCPP_HAS_NO_ASAN
7560b57cec5SDimitry Andric    void __annotate_contiguous_container(const void *__beg, const void *__end,
7570b57cec5SDimitry Andric                                         const void *__old_mid,
7580b57cec5SDimitry Andric                                         const void *__new_mid) const
7590b57cec5SDimitry Andric    {
7600b57cec5SDimitry Andric
7610b57cec5SDimitry Andric      if (__beg && is_same<allocator_type, __default_allocator_type>::value)
7620b57cec5SDimitry Andric        __sanitizer_annotate_contiguous_container(__beg, __end, __old_mid, __new_mid);
7630b57cec5SDimitry Andric    }
7640b57cec5SDimitry Andric#else
7650b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
7660b57cec5SDimitry Andric    void __annotate_contiguous_container(const void*, const void*, const void*,
767e40139ffSDimitry Andric                                         const void*) const _NOEXCEPT {}
7680b57cec5SDimitry Andric#endif
7690b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
770e40139ffSDimitry Andric    void __annotate_new(size_type __current_size) const _NOEXCEPT {
7710b57cec5SDimitry Andric      __annotate_contiguous_container(data(), data() + capacity(),
7720b57cec5SDimitry Andric                                      data() + capacity(), data() + __current_size);
7730b57cec5SDimitry Andric    }
7740b57cec5SDimitry Andric
7750b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
776e40139ffSDimitry Andric    void __annotate_delete() const _NOEXCEPT {
7770b57cec5SDimitry Andric      __annotate_contiguous_container(data(), data() + capacity(),
7780b57cec5SDimitry Andric                                      data() + size(), data() + capacity());
7790b57cec5SDimitry Andric    }
7800b57cec5SDimitry Andric
7810b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
782e40139ffSDimitry Andric    void __annotate_increase(size_type __n) const _NOEXCEPT
7830b57cec5SDimitry Andric    {
7840b57cec5SDimitry Andric      __annotate_contiguous_container(data(), data() + capacity(),
7850b57cec5SDimitry Andric                                      data() + size(), data() + size() + __n);
7860b57cec5SDimitry Andric    }
7870b57cec5SDimitry Andric
7880b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
789e40139ffSDimitry Andric    void __annotate_shrink(size_type __old_size) const _NOEXCEPT
7900b57cec5SDimitry Andric    {
7910b57cec5SDimitry Andric      __annotate_contiguous_container(data(), data() + capacity(),
7920b57cec5SDimitry Andric                                      data() + __old_size, data() + size());
7930b57cec5SDimitry Andric    }
7940b57cec5SDimitry Andric
795e40139ffSDimitry Andric  struct _ConstructTransaction {
796e40139ffSDimitry Andric    explicit _ConstructTransaction(vector &__v, size_type __n)
797e40139ffSDimitry Andric      : __v_(__v), __pos_(__v.__end_), __new_end_(__v.__end_ + __n) {
798e40139ffSDimitry Andric#ifndef _LIBCPP_HAS_NO_ASAN
799e40139ffSDimitry Andric      __v_.__annotate_increase(__n);
800e40139ffSDimitry Andric#endif
801e40139ffSDimitry Andric    }
802e40139ffSDimitry Andric    ~_ConstructTransaction() {
803e40139ffSDimitry Andric      __v_.__end_ = __pos_;
804e40139ffSDimitry Andric#ifndef _LIBCPP_HAS_NO_ASAN
805e40139ffSDimitry Andric      if (__pos_ != __new_end_) {
806e40139ffSDimitry Andric        __v_.__annotate_shrink(__new_end_ - __v_.__begin_);
807e40139ffSDimitry Andric      }
808e40139ffSDimitry Andric#endif
809e40139ffSDimitry Andric    }
810e40139ffSDimitry Andric
811e40139ffSDimitry Andric    vector &__v_;
812e40139ffSDimitry Andric    pointer __pos_;
813e40139ffSDimitry Andric    const_pointer const __new_end_;
814e40139ffSDimitry Andric
815e40139ffSDimitry Andric  private:
816e40139ffSDimitry Andric    _ConstructTransaction(_ConstructTransaction const&) = delete;
817e40139ffSDimitry Andric    _ConstructTransaction& operator=(_ConstructTransaction const&) = delete;
818e40139ffSDimitry Andric  };
819e40139ffSDimitry Andric
820e40139ffSDimitry Andric  template <class ..._Args>
821e40139ffSDimitry Andric  _LIBCPP_INLINE_VISIBILITY
822e40139ffSDimitry Andric  void __construct_one_at_end(_Args&& ...__args) {
823e40139ffSDimitry Andric    _ConstructTransaction __tx(*this, 1);
824480093f4SDimitry Andric    __alloc_traits::construct(this->__alloc(), _VSTD::__to_address(__tx.__pos_),
825e40139ffSDimitry Andric        _VSTD::forward<_Args>(__args)...);
826e40139ffSDimitry Andric    ++__tx.__pos_;
827e40139ffSDimitry Andric  }
828349cc55cSDimitry Andric
829349cc55cSDimitry Andric  _LIBCPP_INLINE_VISIBILITY
830349cc55cSDimitry Andric  allocator_type& __alloc() _NOEXCEPT
831349cc55cSDimitry Andric      {return this->__end_cap_.second();}
832349cc55cSDimitry Andric  _LIBCPP_INLINE_VISIBILITY
833349cc55cSDimitry Andric  const allocator_type& __alloc() const _NOEXCEPT
834349cc55cSDimitry Andric      {return this->__end_cap_.second();}
835349cc55cSDimitry Andric  _LIBCPP_INLINE_VISIBILITY
836349cc55cSDimitry Andric  pointer& __end_cap() _NOEXCEPT
837349cc55cSDimitry Andric      {return this->__end_cap_.first();}
838349cc55cSDimitry Andric  _LIBCPP_INLINE_VISIBILITY
839349cc55cSDimitry Andric  const pointer& __end_cap() const _NOEXCEPT
840349cc55cSDimitry Andric      {return this->__end_cap_.first();}
841349cc55cSDimitry Andric
842349cc55cSDimitry Andric  _LIBCPP_INLINE_VISIBILITY
843349cc55cSDimitry Andric  void __clear() _NOEXCEPT {__base_destruct_at_end(this->__begin_);}
844349cc55cSDimitry Andric
845349cc55cSDimitry Andric  _LIBCPP_INLINE_VISIBILITY
846349cc55cSDimitry Andric  void __base_destruct_at_end(pointer __new_last) _NOEXCEPT {
847349cc55cSDimitry Andric    pointer __soon_to_be_end = this->__end_;
848349cc55cSDimitry Andric    while (__new_last != __soon_to_be_end)
849349cc55cSDimitry Andric        __alloc_traits::destroy(__alloc(), _VSTD::__to_address(--__soon_to_be_end));
850349cc55cSDimitry Andric    this->__end_ = __new_last;
851349cc55cSDimitry Andric  }
852349cc55cSDimitry Andric
853349cc55cSDimitry Andric  _LIBCPP_INLINE_VISIBILITY
854349cc55cSDimitry Andric  void __copy_assign_alloc(const vector& __c)
855349cc55cSDimitry Andric      {__copy_assign_alloc(__c, integral_constant<bool,
856349cc55cSDimitry Andric                    __alloc_traits::propagate_on_container_copy_assignment::value>());}
857349cc55cSDimitry Andric
858349cc55cSDimitry Andric  _LIBCPP_INLINE_VISIBILITY
859349cc55cSDimitry Andric  void __move_assign_alloc(vector& __c)
860349cc55cSDimitry Andric      _NOEXCEPT_(
861349cc55cSDimitry Andric          !__alloc_traits::propagate_on_container_move_assignment::value ||
862349cc55cSDimitry Andric          is_nothrow_move_assignable<allocator_type>::value)
863349cc55cSDimitry Andric      {__move_assign_alloc(__c, integral_constant<bool,
864349cc55cSDimitry Andric                    __alloc_traits::propagate_on_container_move_assignment::value>());}
865349cc55cSDimitry Andric
866349cc55cSDimitry Andric  _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
867349cc55cSDimitry Andric  void __throw_length_error() const {
868349cc55cSDimitry Andric#ifndef _LIBCPP_NO_EXCEPTIONS
869349cc55cSDimitry Andric    __vector_base_common<true>::__throw_length_error();
870349cc55cSDimitry Andric#else
871349cc55cSDimitry Andric    _VSTD::abort();
872349cc55cSDimitry Andric#endif
873349cc55cSDimitry Andric  }
874349cc55cSDimitry Andric
875349cc55cSDimitry Andric  _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
876349cc55cSDimitry Andric  void __throw_out_of_range() const {
877349cc55cSDimitry Andric#ifndef _LIBCPP_NO_EXCEPTIONS
878349cc55cSDimitry Andric    __vector_base_common<true>::__throw_out_of_range();
879349cc55cSDimitry Andric#else
880349cc55cSDimitry Andric    _VSTD::abort();
881349cc55cSDimitry Andric#endif
882349cc55cSDimitry Andric  }
883349cc55cSDimitry Andric
884349cc55cSDimitry Andric  _LIBCPP_INLINE_VISIBILITY
885349cc55cSDimitry Andric  void __copy_assign_alloc(const vector& __c, true_type)
886349cc55cSDimitry Andric  {
887349cc55cSDimitry Andric    if (__alloc() != __c.__alloc())
888349cc55cSDimitry Andric    {
889349cc55cSDimitry Andric      __clear();
890349cc55cSDimitry Andric      __alloc_traits::deallocate(__alloc(), this->__begin_, capacity());
891349cc55cSDimitry Andric      this->__begin_ = this->__end_ = __end_cap() = nullptr;
892349cc55cSDimitry Andric    }
893349cc55cSDimitry Andric    __alloc() = __c.__alloc();
894349cc55cSDimitry Andric  }
895349cc55cSDimitry Andric
896349cc55cSDimitry Andric  _LIBCPP_INLINE_VISIBILITY
897349cc55cSDimitry Andric  void __copy_assign_alloc(const vector&, false_type)
898349cc55cSDimitry Andric  {}
899349cc55cSDimitry Andric
900349cc55cSDimitry Andric  _LIBCPP_INLINE_VISIBILITY
901349cc55cSDimitry Andric  void __move_assign_alloc(vector& __c, true_type)
902349cc55cSDimitry Andric      _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
903349cc55cSDimitry Andric  {
904349cc55cSDimitry Andric    __alloc() = _VSTD::move(__c.__alloc());
905349cc55cSDimitry Andric  }
906349cc55cSDimitry Andric
907349cc55cSDimitry Andric  _LIBCPP_INLINE_VISIBILITY
908349cc55cSDimitry Andric  void __move_assign_alloc(vector&, false_type)
909349cc55cSDimitry Andric      _NOEXCEPT
910349cc55cSDimitry Andric  {}
9110b57cec5SDimitry Andric};
9120b57cec5SDimitry Andric
913349cc55cSDimitry Andric#if _LIBCPP_STD_VER >= 17
9140b57cec5SDimitry Andrictemplate<class _InputIterator,
915fe6060f1SDimitry Andric         class _Alloc = allocator<__iter_value_type<_InputIterator>>,
916349cc55cSDimitry Andric         class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
917349cc55cSDimitry Andric         class = enable_if_t<__is_allocator<_Alloc>::value>
9180b57cec5SDimitry Andric         >
9190b57cec5SDimitry Andricvector(_InputIterator, _InputIterator)
920fe6060f1SDimitry Andric  -> vector<__iter_value_type<_InputIterator>, _Alloc>;
9210b57cec5SDimitry Andric
9220b57cec5SDimitry Andrictemplate<class _InputIterator,
9230b57cec5SDimitry Andric         class _Alloc,
924349cc55cSDimitry Andric         class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
925349cc55cSDimitry Andric         class = enable_if_t<__is_allocator<_Alloc>::value>
9260b57cec5SDimitry Andric         >
9270b57cec5SDimitry Andricvector(_InputIterator, _InputIterator, _Alloc)
928fe6060f1SDimitry Andric  -> vector<__iter_value_type<_InputIterator>, _Alloc>;
9290b57cec5SDimitry Andric#endif
9300b57cec5SDimitry Andric
9310b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
9320b57cec5SDimitry Andricvoid
9330b57cec5SDimitry Andricvector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v)
9340b57cec5SDimitry Andric{
935e40139ffSDimitry Andric
9360b57cec5SDimitry Andric    __annotate_delete();
937e8d8bef9SDimitry Andric    _VSTD::__construct_backward_with_exception_guarantees(this->__alloc(), this->__begin_, this->__end_, __v.__begin_);
9380b57cec5SDimitry Andric    _VSTD::swap(this->__begin_, __v.__begin_);
9390b57cec5SDimitry Andric    _VSTD::swap(this->__end_, __v.__end_);
9400b57cec5SDimitry Andric    _VSTD::swap(this->__end_cap(), __v.__end_cap());
9410b57cec5SDimitry Andric    __v.__first_ = __v.__begin_;
9420b57cec5SDimitry Andric    __annotate_new(size());
9430b57cec5SDimitry Andric    __invalidate_all_iterators();
9440b57cec5SDimitry Andric}
9450b57cec5SDimitry Andric
9460b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
9470b57cec5SDimitry Andrictypename vector<_Tp, _Allocator>::pointer
9480b57cec5SDimitry Andricvector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p)
9490b57cec5SDimitry Andric{
9500b57cec5SDimitry Andric    __annotate_delete();
9510b57cec5SDimitry Andric    pointer __r = __v.__begin_;
952e8d8bef9SDimitry Andric    _VSTD::__construct_backward_with_exception_guarantees(this->__alloc(), this->__begin_, __p, __v.__begin_);
953e8d8bef9SDimitry Andric    _VSTD::__construct_forward_with_exception_guarantees(this->__alloc(), __p, this->__end_, __v.__end_);
9540b57cec5SDimitry Andric    _VSTD::swap(this->__begin_, __v.__begin_);
9550b57cec5SDimitry Andric    _VSTD::swap(this->__end_, __v.__end_);
9560b57cec5SDimitry Andric    _VSTD::swap(this->__end_cap(), __v.__end_cap());
9570b57cec5SDimitry Andric    __v.__first_ = __v.__begin_;
9580b57cec5SDimitry Andric    __annotate_new(size());
9590b57cec5SDimitry Andric    __invalidate_all_iterators();
9600b57cec5SDimitry Andric    return __r;
9610b57cec5SDimitry Andric}
9620b57cec5SDimitry Andric
9630b57cec5SDimitry Andric//  Allocate space for __n objects
9640b57cec5SDimitry Andric//  throws length_error if __n > max_size()
9650b57cec5SDimitry Andric//  throws (probably bad_alloc) if memory run out
9660b57cec5SDimitry Andric//  Precondition:  __begin_ == __end_ == __end_cap() == 0
9670b57cec5SDimitry Andric//  Precondition:  __n > 0
9680b57cec5SDimitry Andric//  Postcondition:  capacity() == __n
9690b57cec5SDimitry Andric//  Postcondition:  size() == 0
9700b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
9710b57cec5SDimitry Andricvoid
9720b57cec5SDimitry Andricvector<_Tp, _Allocator>::__vallocate(size_type __n)
9730b57cec5SDimitry Andric{
9740b57cec5SDimitry Andric    if (__n > max_size())
9750b57cec5SDimitry Andric        this->__throw_length_error();
9760b57cec5SDimitry Andric    this->__begin_ = this->__end_ = __alloc_traits::allocate(this->__alloc(), __n);
9770b57cec5SDimitry Andric    this->__end_cap() = this->__begin_ + __n;
9780b57cec5SDimitry Andric    __annotate_new(0);
9790b57cec5SDimitry Andric}
9800b57cec5SDimitry Andric
9810b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
9820b57cec5SDimitry Andricvoid
9830b57cec5SDimitry Andricvector<_Tp, _Allocator>::__vdeallocate() _NOEXCEPT
9840b57cec5SDimitry Andric{
9850b57cec5SDimitry Andric    if (this->__begin_ != nullptr)
9860b57cec5SDimitry Andric    {
9870b57cec5SDimitry Andric        clear();
9880b57cec5SDimitry Andric        __alloc_traits::deallocate(this->__alloc(), this->__begin_, capacity());
9890b57cec5SDimitry Andric        this->__begin_ = this->__end_ = this->__end_cap() = nullptr;
9900b57cec5SDimitry Andric    }
9910b57cec5SDimitry Andric}
9920b57cec5SDimitry Andric
9930b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
9940b57cec5SDimitry Andrictypename vector<_Tp, _Allocator>::size_type
9950b57cec5SDimitry Andricvector<_Tp, _Allocator>::max_size() const _NOEXCEPT
9960b57cec5SDimitry Andric{
9970b57cec5SDimitry Andric    return _VSTD::min<size_type>(__alloc_traits::max_size(this->__alloc()),
9980b57cec5SDimitry Andric                                 numeric_limits<difference_type>::max());
9990b57cec5SDimitry Andric}
10000b57cec5SDimitry Andric
10010b57cec5SDimitry Andric//  Precondition:  __new_size > capacity()
10020b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
10030b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
10040b57cec5SDimitry Andrictypename vector<_Tp, _Allocator>::size_type
10050b57cec5SDimitry Andricvector<_Tp, _Allocator>::__recommend(size_type __new_size) const
10060b57cec5SDimitry Andric{
10070b57cec5SDimitry Andric    const size_type __ms = max_size();
10080b57cec5SDimitry Andric    if (__new_size > __ms)
10090b57cec5SDimitry Andric        this->__throw_length_error();
10100b57cec5SDimitry Andric    const size_type __cap = capacity();
10110b57cec5SDimitry Andric    if (__cap >= __ms / 2)
10120b57cec5SDimitry Andric        return __ms;
10130b57cec5SDimitry Andric    return _VSTD::max<size_type>(2 * __cap, __new_size);
10140b57cec5SDimitry Andric}
10150b57cec5SDimitry Andric
10160b57cec5SDimitry Andric//  Default constructs __n objects starting at __end_
10170b57cec5SDimitry Andric//  throws if construction throws
10180b57cec5SDimitry Andric//  Precondition:  __n > 0
10190b57cec5SDimitry Andric//  Precondition:  size() + __n <= capacity()
10200b57cec5SDimitry Andric//  Postcondition:  size() == size() + __n
10210b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
10220b57cec5SDimitry Andricvoid
10230b57cec5SDimitry Andricvector<_Tp, _Allocator>::__construct_at_end(size_type __n)
10240b57cec5SDimitry Andric{
1025e40139ffSDimitry Andric    _ConstructTransaction __tx(*this, __n);
10265ffd83dbSDimitry Andric    const_pointer __new_end = __tx.__new_end_;
1027349cc55cSDimitry Andric    for (pointer __pos = __tx.__pos_; __pos != __new_end; __tx.__pos_ = ++__pos) {
10285ffd83dbSDimitry Andric        __alloc_traits::construct(this->__alloc(), _VSTD::__to_address(__pos));
1029e40139ffSDimitry Andric    }
10300b57cec5SDimitry Andric}
10310b57cec5SDimitry Andric
10320b57cec5SDimitry Andric//  Copy constructs __n objects starting at __end_ from __x
10330b57cec5SDimitry Andric//  throws if construction throws
10340b57cec5SDimitry Andric//  Precondition:  __n > 0
10350b57cec5SDimitry Andric//  Precondition:  size() + __n <= capacity()
10360b57cec5SDimitry Andric//  Postcondition:  size() == old size() + __n
10370b57cec5SDimitry Andric//  Postcondition:  [i] == __x for all i in [size() - __n, __n)
10380b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
10390b57cec5SDimitry Andricinline
10400b57cec5SDimitry Andricvoid
10410b57cec5SDimitry Andricvector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
10420b57cec5SDimitry Andric{
1043e40139ffSDimitry Andric    _ConstructTransaction __tx(*this, __n);
10445ffd83dbSDimitry Andric    const_pointer __new_end = __tx.__new_end_;
1045349cc55cSDimitry Andric    for (pointer __pos = __tx.__pos_; __pos != __new_end; __tx.__pos_ = ++__pos) {
10465ffd83dbSDimitry Andric        __alloc_traits::construct(this->__alloc(), _VSTD::__to_address(__pos), __x);
1047e40139ffSDimitry Andric    }
10480b57cec5SDimitry Andric}
10490b57cec5SDimitry Andric
10500b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
10510b57cec5SDimitry Andrictemplate <class _ForwardIterator>
10520b57cec5SDimitry Andrictypename enable_if
10530b57cec5SDimitry Andric<
1054480093f4SDimitry Andric    __is_cpp17_forward_iterator<_ForwardIterator>::value,
10550b57cec5SDimitry Andric    void
10560b57cec5SDimitry Andric>::type
10570b57cec5SDimitry Andricvector<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n)
10580b57cec5SDimitry Andric{
1059e40139ffSDimitry Andric    _ConstructTransaction __tx(*this, __n);
1060e8d8bef9SDimitry Andric    _VSTD::__construct_range_forward(this->__alloc(), __first, __last, __tx.__pos_);
10610b57cec5SDimitry Andric}
10620b57cec5SDimitry Andric
10630b57cec5SDimitry Andric//  Default constructs __n objects starting at __end_
10640b57cec5SDimitry Andric//  throws if construction throws
10650b57cec5SDimitry Andric//  Postcondition:  size() == size() + __n
10660b57cec5SDimitry Andric//  Exception safety: strong.
10670b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
10680b57cec5SDimitry Andricvoid
10690b57cec5SDimitry Andricvector<_Tp, _Allocator>::__append(size_type __n)
10700b57cec5SDimitry Andric{
10710b57cec5SDimitry Andric    if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
10720b57cec5SDimitry Andric        this->__construct_at_end(__n);
10730b57cec5SDimitry Andric    else
10740b57cec5SDimitry Andric    {
10750b57cec5SDimitry Andric        allocator_type& __a = this->__alloc();
10760b57cec5SDimitry Andric        __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
10770b57cec5SDimitry Andric        __v.__construct_at_end(__n);
10780b57cec5SDimitry Andric        __swap_out_circular_buffer(__v);
10790b57cec5SDimitry Andric    }
10800b57cec5SDimitry Andric}
10810b57cec5SDimitry Andric
10820b57cec5SDimitry Andric//  Default constructs __n objects starting at __end_
10830b57cec5SDimitry Andric//  throws if construction throws
10840b57cec5SDimitry Andric//  Postcondition:  size() == size() + __n
10850b57cec5SDimitry Andric//  Exception safety: strong.
10860b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
10870b57cec5SDimitry Andricvoid
10880b57cec5SDimitry Andricvector<_Tp, _Allocator>::__append(size_type __n, const_reference __x)
10890b57cec5SDimitry Andric{
10900b57cec5SDimitry Andric    if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
10910b57cec5SDimitry Andric        this->__construct_at_end(__n, __x);
10920b57cec5SDimitry Andric    else
10930b57cec5SDimitry Andric    {
10940b57cec5SDimitry Andric        allocator_type& __a = this->__alloc();
10950b57cec5SDimitry Andric        __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
10960b57cec5SDimitry Andric        __v.__construct_at_end(__n, __x);
10970b57cec5SDimitry Andric        __swap_out_circular_buffer(__v);
10980b57cec5SDimitry Andric    }
10990b57cec5SDimitry Andric}
11000b57cec5SDimitry Andric
11010b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
11020b57cec5SDimitry Andricvector<_Tp, _Allocator>::vector(size_type __n)
11030b57cec5SDimitry Andric{
1104e8d8bef9SDimitry Andric#if _LIBCPP_DEBUG_LEVEL == 2
11050b57cec5SDimitry Andric    __get_db()->__insert_c(this);
11060b57cec5SDimitry Andric#endif
11070b57cec5SDimitry Andric    if (__n > 0)
11080b57cec5SDimitry Andric    {
11090b57cec5SDimitry Andric        __vallocate(__n);
11100b57cec5SDimitry Andric        __construct_at_end(__n);
11110b57cec5SDimitry Andric    }
11120b57cec5SDimitry Andric}
11130b57cec5SDimitry Andric
11140b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 11
11150b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
11160b57cec5SDimitry Andricvector<_Tp, _Allocator>::vector(size_type __n, const allocator_type& __a)
11170b57cec5SDimitry Andric    : __base(__a)
11180b57cec5SDimitry Andric{
1119e8d8bef9SDimitry Andric#if _LIBCPP_DEBUG_LEVEL == 2
11200b57cec5SDimitry Andric    __get_db()->__insert_c(this);
11210b57cec5SDimitry Andric#endif
11220b57cec5SDimitry Andric    if (__n > 0)
11230b57cec5SDimitry Andric    {
11240b57cec5SDimitry Andric        __vallocate(__n);
11250b57cec5SDimitry Andric        __construct_at_end(__n);
11260b57cec5SDimitry Andric    }
11270b57cec5SDimitry Andric}
11280b57cec5SDimitry Andric#endif
11290b57cec5SDimitry Andric
11300b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
11310b57cec5SDimitry Andricvector<_Tp, _Allocator>::vector(size_type __n, const value_type& __x)
11320b57cec5SDimitry Andric{
1133e8d8bef9SDimitry Andric#if _LIBCPP_DEBUG_LEVEL == 2
11340b57cec5SDimitry Andric    __get_db()->__insert_c(this);
11350b57cec5SDimitry Andric#endif
11360b57cec5SDimitry Andric    if (__n > 0)
11370b57cec5SDimitry Andric    {
11380b57cec5SDimitry Andric        __vallocate(__n);
11390b57cec5SDimitry Andric        __construct_at_end(__n, __x);
11400b57cec5SDimitry Andric    }
11410b57cec5SDimitry Andric}
11420b57cec5SDimitry Andric
11430b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
11440b57cec5SDimitry Andrictemplate <class _InputIterator>
11450b57cec5SDimitry Andricvector<_Tp, _Allocator>::vector(_InputIterator __first,
1146480093f4SDimitry Andric       typename enable_if<__is_cpp17_input_iterator  <_InputIterator>::value &&
1147480093f4SDimitry Andric                         !__is_cpp17_forward_iterator<_InputIterator>::value &&
11480b57cec5SDimitry Andric                         is_constructible<
11490b57cec5SDimitry Andric                            value_type,
11500b57cec5SDimitry Andric                            typename iterator_traits<_InputIterator>::reference>::value,
11510b57cec5SDimitry Andric                          _InputIterator>::type __last)
11520b57cec5SDimitry Andric{
1153e8d8bef9SDimitry Andric#if _LIBCPP_DEBUG_LEVEL == 2
11540b57cec5SDimitry Andric    __get_db()->__insert_c(this);
11550b57cec5SDimitry Andric#endif
11560b57cec5SDimitry Andric    for (; __first != __last; ++__first)
11570b57cec5SDimitry Andric        __emplace_back(*__first);
11580b57cec5SDimitry Andric}
11590b57cec5SDimitry Andric
11600b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
11610b57cec5SDimitry Andrictemplate <class _InputIterator>
11620b57cec5SDimitry Andricvector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
1163480093f4SDimitry Andric       typename enable_if<__is_cpp17_input_iterator  <_InputIterator>::value &&
1164480093f4SDimitry Andric                         !__is_cpp17_forward_iterator<_InputIterator>::value &&
11650b57cec5SDimitry Andric                         is_constructible<
11660b57cec5SDimitry Andric                            value_type,
11670b57cec5SDimitry Andric                            typename iterator_traits<_InputIterator>::reference>::value>::type*)
11680b57cec5SDimitry Andric    : __base(__a)
11690b57cec5SDimitry Andric{
1170e8d8bef9SDimitry Andric#if _LIBCPP_DEBUG_LEVEL == 2
11710b57cec5SDimitry Andric    __get_db()->__insert_c(this);
11720b57cec5SDimitry Andric#endif
11730b57cec5SDimitry Andric    for (; __first != __last; ++__first)
11740b57cec5SDimitry Andric        __emplace_back(*__first);
11750b57cec5SDimitry Andric}
11760b57cec5SDimitry Andric
11770b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
11780b57cec5SDimitry Andrictemplate <class _ForwardIterator>
11790b57cec5SDimitry Andricvector<_Tp, _Allocator>::vector(_ForwardIterator __first,
1180480093f4SDimitry Andric                                typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value &&
11810b57cec5SDimitry Andric                                is_constructible<
11820b57cec5SDimitry Andric                                   value_type,
11830b57cec5SDimitry Andric                                   typename iterator_traits<_ForwardIterator>::reference>::value,
11840b57cec5SDimitry Andric                                                   _ForwardIterator>::type __last)
11850b57cec5SDimitry Andric{
1186e8d8bef9SDimitry Andric#if _LIBCPP_DEBUG_LEVEL == 2
11870b57cec5SDimitry Andric    __get_db()->__insert_c(this);
11880b57cec5SDimitry Andric#endif
11890b57cec5SDimitry Andric    size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
11900b57cec5SDimitry Andric    if (__n > 0)
11910b57cec5SDimitry Andric    {
11920b57cec5SDimitry Andric        __vallocate(__n);
11930b57cec5SDimitry Andric        __construct_at_end(__first, __last, __n);
11940b57cec5SDimitry Andric    }
11950b57cec5SDimitry Andric}
11960b57cec5SDimitry Andric
11970b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
11980b57cec5SDimitry Andrictemplate <class _ForwardIterator>
11990b57cec5SDimitry Andricvector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
1200480093f4SDimitry Andric                                typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value &&
12010b57cec5SDimitry Andric                                is_constructible<
12020b57cec5SDimitry Andric                                   value_type,
12030b57cec5SDimitry Andric                                   typename iterator_traits<_ForwardIterator>::reference>::value>::type*)
12040b57cec5SDimitry Andric    : __base(__a)
12050b57cec5SDimitry Andric{
1206e8d8bef9SDimitry Andric#if _LIBCPP_DEBUG_LEVEL == 2
12070b57cec5SDimitry Andric    __get_db()->__insert_c(this);
12080b57cec5SDimitry Andric#endif
12090b57cec5SDimitry Andric    size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
12100b57cec5SDimitry Andric    if (__n > 0)
12110b57cec5SDimitry Andric    {
12120b57cec5SDimitry Andric        __vallocate(__n);
12130b57cec5SDimitry Andric        __construct_at_end(__first, __last, __n);
12140b57cec5SDimitry Andric    }
12150b57cec5SDimitry Andric}
12160b57cec5SDimitry Andric
12170b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
12180b57cec5SDimitry Andricvector<_Tp, _Allocator>::vector(const vector& __x)
12190b57cec5SDimitry Andric    : __base(__alloc_traits::select_on_container_copy_construction(__x.__alloc()))
12200b57cec5SDimitry Andric{
1221e8d8bef9SDimitry Andric#if _LIBCPP_DEBUG_LEVEL == 2
12220b57cec5SDimitry Andric    __get_db()->__insert_c(this);
12230b57cec5SDimitry Andric#endif
12240b57cec5SDimitry Andric    size_type __n = __x.size();
12250b57cec5SDimitry Andric    if (__n > 0)
12260b57cec5SDimitry Andric    {
12270b57cec5SDimitry Andric        __vallocate(__n);
12280b57cec5SDimitry Andric        __construct_at_end(__x.__begin_, __x.__end_, __n);
12290b57cec5SDimitry Andric    }
12300b57cec5SDimitry Andric}
12310b57cec5SDimitry Andric
12320b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
1233fe6060f1SDimitry Andricvector<_Tp, _Allocator>::vector(const vector& __x, const __identity_t<allocator_type>& __a)
12340b57cec5SDimitry Andric    : __base(__a)
12350b57cec5SDimitry Andric{
1236e8d8bef9SDimitry Andric#if _LIBCPP_DEBUG_LEVEL == 2
12370b57cec5SDimitry Andric    __get_db()->__insert_c(this);
12380b57cec5SDimitry Andric#endif
12390b57cec5SDimitry Andric    size_type __n = __x.size();
12400b57cec5SDimitry Andric    if (__n > 0)
12410b57cec5SDimitry Andric    {
12420b57cec5SDimitry Andric        __vallocate(__n);
12430b57cec5SDimitry Andric        __construct_at_end(__x.__begin_, __x.__end_, __n);
12440b57cec5SDimitry Andric    }
12450b57cec5SDimitry Andric}
12460b57cec5SDimitry Andric
12470b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
12480b57cec5SDimitry Andric
12490b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
12500b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
12510b57cec5SDimitry Andricvector<_Tp, _Allocator>::vector(vector&& __x)
12520b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 14
12530b57cec5SDimitry Andric        _NOEXCEPT
12540b57cec5SDimitry Andric#else
12550b57cec5SDimitry Andric        _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
12560b57cec5SDimitry Andric#endif
12570b57cec5SDimitry Andric    : __base(_VSTD::move(__x.__alloc()))
12580b57cec5SDimitry Andric{
1259e8d8bef9SDimitry Andric#if _LIBCPP_DEBUG_LEVEL == 2
12600b57cec5SDimitry Andric    __get_db()->__insert_c(this);
1261349cc55cSDimitry Andric    __get_db()->swap(this, _VSTD::addressof(__x));
12620b57cec5SDimitry Andric#endif
12630b57cec5SDimitry Andric    this->__begin_ = __x.__begin_;
12640b57cec5SDimitry Andric    this->__end_ = __x.__end_;
12650b57cec5SDimitry Andric    this->__end_cap() = __x.__end_cap();
12660b57cec5SDimitry Andric    __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
12670b57cec5SDimitry Andric}
12680b57cec5SDimitry Andric
12690b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
12700b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
1271fe6060f1SDimitry Andricvector<_Tp, _Allocator>::vector(vector&& __x, const __identity_t<allocator_type>& __a)
12720b57cec5SDimitry Andric    : __base(__a)
12730b57cec5SDimitry Andric{
1274e8d8bef9SDimitry Andric#if _LIBCPP_DEBUG_LEVEL == 2
12750b57cec5SDimitry Andric    __get_db()->__insert_c(this);
12760b57cec5SDimitry Andric#endif
12770b57cec5SDimitry Andric    if (__a == __x.__alloc())
12780b57cec5SDimitry Andric    {
12790b57cec5SDimitry Andric        this->__begin_ = __x.__begin_;
12800b57cec5SDimitry Andric        this->__end_ = __x.__end_;
12810b57cec5SDimitry Andric        this->__end_cap() = __x.__end_cap();
12820b57cec5SDimitry Andric        __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
1283e8d8bef9SDimitry Andric#if _LIBCPP_DEBUG_LEVEL == 2
1284349cc55cSDimitry Andric        __get_db()->swap(this, _VSTD::addressof(__x));
12850b57cec5SDimitry Andric#endif
12860b57cec5SDimitry Andric    }
12870b57cec5SDimitry Andric    else
12880b57cec5SDimitry Andric    {
12890b57cec5SDimitry Andric        typedef move_iterator<iterator> _Ip;
12900b57cec5SDimitry Andric        assign(_Ip(__x.begin()), _Ip(__x.end()));
12910b57cec5SDimitry Andric    }
12920b57cec5SDimitry Andric}
12930b57cec5SDimitry Andric
12940b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
12950b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
12960b57cec5SDimitry Andricvector<_Tp, _Allocator>::vector(initializer_list<value_type> __il)
12970b57cec5SDimitry Andric{
1298e8d8bef9SDimitry Andric#if _LIBCPP_DEBUG_LEVEL == 2
12990b57cec5SDimitry Andric    __get_db()->__insert_c(this);
13000b57cec5SDimitry Andric#endif
13010b57cec5SDimitry Andric    if (__il.size() > 0)
13020b57cec5SDimitry Andric    {
13030b57cec5SDimitry Andric        __vallocate(__il.size());
13040b57cec5SDimitry Andric        __construct_at_end(__il.begin(), __il.end(), __il.size());
13050b57cec5SDimitry Andric    }
13060b57cec5SDimitry Andric}
13070b57cec5SDimitry Andric
13080b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
13090b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
13100b57cec5SDimitry Andricvector<_Tp, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
13110b57cec5SDimitry Andric    : __base(__a)
13120b57cec5SDimitry Andric{
1313e8d8bef9SDimitry Andric#if _LIBCPP_DEBUG_LEVEL == 2
13140b57cec5SDimitry Andric    __get_db()->__insert_c(this);
13150b57cec5SDimitry Andric#endif
13160b57cec5SDimitry Andric    if (__il.size() > 0)
13170b57cec5SDimitry Andric    {
13180b57cec5SDimitry Andric        __vallocate(__il.size());
13190b57cec5SDimitry Andric        __construct_at_end(__il.begin(), __il.end(), __il.size());
13200b57cec5SDimitry Andric    }
13210b57cec5SDimitry Andric}
13220b57cec5SDimitry Andric
13230b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
13240b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
13250b57cec5SDimitry Andricvector<_Tp, _Allocator>&
13260b57cec5SDimitry Andricvector<_Tp, _Allocator>::operator=(vector&& __x)
13270b57cec5SDimitry Andric    _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
13280b57cec5SDimitry Andric{
13290b57cec5SDimitry Andric    __move_assign(__x, integral_constant<bool,
13300b57cec5SDimitry Andric          __alloc_traits::propagate_on_container_move_assignment::value>());
13310b57cec5SDimitry Andric    return *this;
13320b57cec5SDimitry Andric}
13330b57cec5SDimitry Andric
13340b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
13350b57cec5SDimitry Andricvoid
13360b57cec5SDimitry Andricvector<_Tp, _Allocator>::__move_assign(vector& __c, false_type)
13370b57cec5SDimitry Andric    _NOEXCEPT_(__alloc_traits::is_always_equal::value)
13380b57cec5SDimitry Andric{
1339349cc55cSDimitry Andric    if (__alloc() != __c.__alloc())
13400b57cec5SDimitry Andric    {
13410b57cec5SDimitry Andric        typedef move_iterator<iterator> _Ip;
13420b57cec5SDimitry Andric        assign(_Ip(__c.begin()), _Ip(__c.end()));
13430b57cec5SDimitry Andric    }
13440b57cec5SDimitry Andric    else
13450b57cec5SDimitry Andric        __move_assign(__c, true_type());
13460b57cec5SDimitry Andric}
13470b57cec5SDimitry Andric
13480b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
13490b57cec5SDimitry Andricvoid
13500b57cec5SDimitry Andricvector<_Tp, _Allocator>::__move_assign(vector& __c, true_type)
13510b57cec5SDimitry Andric    _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
13520b57cec5SDimitry Andric{
13530b57cec5SDimitry Andric    __vdeallocate();
1354349cc55cSDimitry Andric    __move_assign_alloc(__c); // this can throw
13550b57cec5SDimitry Andric    this->__begin_ = __c.__begin_;
13560b57cec5SDimitry Andric    this->__end_ = __c.__end_;
13570b57cec5SDimitry Andric    this->__end_cap() = __c.__end_cap();
13580b57cec5SDimitry Andric    __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
1359e8d8bef9SDimitry Andric#if _LIBCPP_DEBUG_LEVEL == 2
1360349cc55cSDimitry Andric    __get_db()->swap(this, _VSTD::addressof(__c));
13610b57cec5SDimitry Andric#endif
13620b57cec5SDimitry Andric}
13630b57cec5SDimitry Andric
13640b57cec5SDimitry Andric#endif // !_LIBCPP_CXX03_LANG
13650b57cec5SDimitry Andric
13660b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
13670b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
13680b57cec5SDimitry Andricvector<_Tp, _Allocator>&
13690b57cec5SDimitry Andricvector<_Tp, _Allocator>::operator=(const vector& __x)
13700b57cec5SDimitry Andric{
1371349cc55cSDimitry Andric    if (this != _VSTD::addressof(__x))
13720b57cec5SDimitry Andric    {
1373349cc55cSDimitry Andric        __copy_assign_alloc(__x);
13740b57cec5SDimitry Andric        assign(__x.__begin_, __x.__end_);
13750b57cec5SDimitry Andric    }
13760b57cec5SDimitry Andric    return *this;
13770b57cec5SDimitry Andric}
13780b57cec5SDimitry Andric
13790b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
13800b57cec5SDimitry Andrictemplate <class _InputIterator>
13810b57cec5SDimitry Andrictypename enable_if
13820b57cec5SDimitry Andric<
1383480093f4SDimitry Andric     __is_cpp17_input_iterator  <_InputIterator>::value &&
1384480093f4SDimitry Andric    !__is_cpp17_forward_iterator<_InputIterator>::value &&
13850b57cec5SDimitry Andric    is_constructible<
13860b57cec5SDimitry Andric       _Tp,
13870b57cec5SDimitry Andric       typename iterator_traits<_InputIterator>::reference>::value,
13880b57cec5SDimitry Andric    void
13890b57cec5SDimitry Andric>::type
13900b57cec5SDimitry Andricvector<_Tp, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
13910b57cec5SDimitry Andric{
13920b57cec5SDimitry Andric    clear();
13930b57cec5SDimitry Andric    for (; __first != __last; ++__first)
13940b57cec5SDimitry Andric        __emplace_back(*__first);
13950b57cec5SDimitry Andric}
13960b57cec5SDimitry Andric
13970b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
13980b57cec5SDimitry Andrictemplate <class _ForwardIterator>
13990b57cec5SDimitry Andrictypename enable_if
14000b57cec5SDimitry Andric<
1401480093f4SDimitry Andric    __is_cpp17_forward_iterator<_ForwardIterator>::value &&
14020b57cec5SDimitry Andric    is_constructible<
14030b57cec5SDimitry Andric       _Tp,
14040b57cec5SDimitry Andric       typename iterator_traits<_ForwardIterator>::reference>::value,
14050b57cec5SDimitry Andric    void
14060b57cec5SDimitry Andric>::type
14070b57cec5SDimitry Andricvector<_Tp, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
14080b57cec5SDimitry Andric{
14090b57cec5SDimitry Andric    size_type __new_size = static_cast<size_type>(_VSTD::distance(__first, __last));
14100b57cec5SDimitry Andric    if (__new_size <= capacity())
14110b57cec5SDimitry Andric    {
14120b57cec5SDimitry Andric        _ForwardIterator __mid = __last;
14130b57cec5SDimitry Andric        bool __growing = false;
14140b57cec5SDimitry Andric        if (__new_size > size())
14150b57cec5SDimitry Andric        {
14160b57cec5SDimitry Andric            __growing = true;
14170b57cec5SDimitry Andric            __mid =  __first;
14180b57cec5SDimitry Andric            _VSTD::advance(__mid, size());
14190b57cec5SDimitry Andric        }
14200b57cec5SDimitry Andric        pointer __m = _VSTD::copy(__first, __mid, this->__begin_);
14210b57cec5SDimitry Andric        if (__growing)
14220b57cec5SDimitry Andric            __construct_at_end(__mid, __last, __new_size - size());
14230b57cec5SDimitry Andric        else
14240b57cec5SDimitry Andric            this->__destruct_at_end(__m);
14250b57cec5SDimitry Andric    }
14260b57cec5SDimitry Andric    else
14270b57cec5SDimitry Andric    {
14280b57cec5SDimitry Andric        __vdeallocate();
14290b57cec5SDimitry Andric        __vallocate(__recommend(__new_size));
14300b57cec5SDimitry Andric        __construct_at_end(__first, __last, __new_size);
14310b57cec5SDimitry Andric    }
14320b57cec5SDimitry Andric    __invalidate_all_iterators();
14330b57cec5SDimitry Andric}
14340b57cec5SDimitry Andric
14350b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
14360b57cec5SDimitry Andricvoid
14370b57cec5SDimitry Andricvector<_Tp, _Allocator>::assign(size_type __n, const_reference __u)
14380b57cec5SDimitry Andric{
14390b57cec5SDimitry Andric    if (__n <= capacity())
14400b57cec5SDimitry Andric    {
14410b57cec5SDimitry Andric        size_type __s = size();
14420b57cec5SDimitry Andric        _VSTD::fill_n(this->__begin_, _VSTD::min(__n, __s), __u);
14430b57cec5SDimitry Andric        if (__n > __s)
14440b57cec5SDimitry Andric            __construct_at_end(__n - __s, __u);
14450b57cec5SDimitry Andric        else
14460b57cec5SDimitry Andric            this->__destruct_at_end(this->__begin_ + __n);
14470b57cec5SDimitry Andric    }
14480b57cec5SDimitry Andric    else
14490b57cec5SDimitry Andric    {
14500b57cec5SDimitry Andric        __vdeallocate();
14510b57cec5SDimitry Andric        __vallocate(__recommend(static_cast<size_type>(__n)));
14520b57cec5SDimitry Andric        __construct_at_end(__n, __u);
14530b57cec5SDimitry Andric    }
14540b57cec5SDimitry Andric    __invalidate_all_iterators();
14550b57cec5SDimitry Andric}
14560b57cec5SDimitry Andric
14570b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
14580b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
14590b57cec5SDimitry Andrictypename vector<_Tp, _Allocator>::iterator
14600b57cec5SDimitry Andricvector<_Tp, _Allocator>::__make_iter(pointer __p) _NOEXCEPT
14610b57cec5SDimitry Andric{
1462e8d8bef9SDimitry Andric#if _LIBCPP_DEBUG_LEVEL == 2
14630b57cec5SDimitry Andric    return iterator(this, __p);
14640b57cec5SDimitry Andric#else
14650b57cec5SDimitry Andric    return iterator(__p);
14660b57cec5SDimitry Andric#endif
14670b57cec5SDimitry Andric}
14680b57cec5SDimitry Andric
14690b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
14700b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
14710b57cec5SDimitry Andrictypename vector<_Tp, _Allocator>::const_iterator
14720b57cec5SDimitry Andricvector<_Tp, _Allocator>::__make_iter(const_pointer __p) const _NOEXCEPT
14730b57cec5SDimitry Andric{
1474e8d8bef9SDimitry Andric#if _LIBCPP_DEBUG_LEVEL == 2
14750b57cec5SDimitry Andric    return const_iterator(this, __p);
14760b57cec5SDimitry Andric#else
14770b57cec5SDimitry Andric    return const_iterator(__p);
14780b57cec5SDimitry Andric#endif
14790b57cec5SDimitry Andric}
14800b57cec5SDimitry Andric
14810b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
14820b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
14830b57cec5SDimitry Andrictypename vector<_Tp, _Allocator>::iterator
14840b57cec5SDimitry Andricvector<_Tp, _Allocator>::begin() _NOEXCEPT
14850b57cec5SDimitry Andric{
14860b57cec5SDimitry Andric    return __make_iter(this->__begin_);
14870b57cec5SDimitry Andric}
14880b57cec5SDimitry Andric
14890b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
14900b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
14910b57cec5SDimitry Andrictypename vector<_Tp, _Allocator>::const_iterator
14920b57cec5SDimitry Andricvector<_Tp, _Allocator>::begin() const _NOEXCEPT
14930b57cec5SDimitry Andric{
14940b57cec5SDimitry Andric    return __make_iter(this->__begin_);
14950b57cec5SDimitry Andric}
14960b57cec5SDimitry Andric
14970b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
14980b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
14990b57cec5SDimitry Andrictypename vector<_Tp, _Allocator>::iterator
15000b57cec5SDimitry Andricvector<_Tp, _Allocator>::end() _NOEXCEPT
15010b57cec5SDimitry Andric{
15020b57cec5SDimitry Andric    return __make_iter(this->__end_);
15030b57cec5SDimitry Andric}
15040b57cec5SDimitry Andric
15050b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
15060b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
15070b57cec5SDimitry Andrictypename vector<_Tp, _Allocator>::const_iterator
15080b57cec5SDimitry Andricvector<_Tp, _Allocator>::end() const _NOEXCEPT
15090b57cec5SDimitry Andric{
15100b57cec5SDimitry Andric    return __make_iter(this->__end_);
15110b57cec5SDimitry Andric}
15120b57cec5SDimitry Andric
15130b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
15140b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
15150b57cec5SDimitry Andrictypename vector<_Tp, _Allocator>::reference
15160b57cec5SDimitry Andricvector<_Tp, _Allocator>::operator[](size_type __n) _NOEXCEPT
15170b57cec5SDimitry Andric{
15180b57cec5SDimitry Andric    _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
15190b57cec5SDimitry Andric    return this->__begin_[__n];
15200b57cec5SDimitry Andric}
15210b57cec5SDimitry Andric
15220b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
15230b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
15240b57cec5SDimitry Andrictypename vector<_Tp, _Allocator>::const_reference
15250b57cec5SDimitry Andricvector<_Tp, _Allocator>::operator[](size_type __n) const _NOEXCEPT
15260b57cec5SDimitry Andric{
15270b57cec5SDimitry Andric    _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
15280b57cec5SDimitry Andric    return this->__begin_[__n];
15290b57cec5SDimitry Andric}
15300b57cec5SDimitry Andric
15310b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
15320b57cec5SDimitry Andrictypename vector<_Tp, _Allocator>::reference
15330b57cec5SDimitry Andricvector<_Tp, _Allocator>::at(size_type __n)
15340b57cec5SDimitry Andric{
15350b57cec5SDimitry Andric    if (__n >= size())
15360b57cec5SDimitry Andric        this->__throw_out_of_range();
15370b57cec5SDimitry Andric    return this->__begin_[__n];
15380b57cec5SDimitry Andric}
15390b57cec5SDimitry Andric
15400b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
15410b57cec5SDimitry Andrictypename vector<_Tp, _Allocator>::const_reference
15420b57cec5SDimitry Andricvector<_Tp, _Allocator>::at(size_type __n) const
15430b57cec5SDimitry Andric{
15440b57cec5SDimitry Andric    if (__n >= size())
15450b57cec5SDimitry Andric        this->__throw_out_of_range();
15460b57cec5SDimitry Andric    return this->__begin_[__n];
15470b57cec5SDimitry Andric}
15480b57cec5SDimitry Andric
15490b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
15500b57cec5SDimitry Andricvoid
15510b57cec5SDimitry Andricvector<_Tp, _Allocator>::reserve(size_type __n)
15520b57cec5SDimitry Andric{
15530b57cec5SDimitry Andric    if (__n > capacity())
15540b57cec5SDimitry Andric    {
1555349cc55cSDimitry Andric        if (__n > max_size())
1556349cc55cSDimitry Andric            this->__throw_length_error();
15570b57cec5SDimitry Andric        allocator_type& __a = this->__alloc();
15580b57cec5SDimitry Andric        __split_buffer<value_type, allocator_type&> __v(__n, size(), __a);
15590b57cec5SDimitry Andric        __swap_out_circular_buffer(__v);
15600b57cec5SDimitry Andric    }
15610b57cec5SDimitry Andric}
15620b57cec5SDimitry Andric
15630b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
15640b57cec5SDimitry Andricvoid
15650b57cec5SDimitry Andricvector<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
15660b57cec5SDimitry Andric{
15670b57cec5SDimitry Andric    if (capacity() > size())
15680b57cec5SDimitry Andric    {
15690b57cec5SDimitry Andric#ifndef _LIBCPP_NO_EXCEPTIONS
15700b57cec5SDimitry Andric        try
15710b57cec5SDimitry Andric        {
15720b57cec5SDimitry Andric#endif // _LIBCPP_NO_EXCEPTIONS
15730b57cec5SDimitry Andric            allocator_type& __a = this->__alloc();
15740b57cec5SDimitry Andric            __split_buffer<value_type, allocator_type&> __v(size(), size(), __a);
15750b57cec5SDimitry Andric            __swap_out_circular_buffer(__v);
15760b57cec5SDimitry Andric#ifndef _LIBCPP_NO_EXCEPTIONS
15770b57cec5SDimitry Andric        }
15780b57cec5SDimitry Andric        catch (...)
15790b57cec5SDimitry Andric        {
15800b57cec5SDimitry Andric        }
15810b57cec5SDimitry Andric#endif // _LIBCPP_NO_EXCEPTIONS
15820b57cec5SDimitry Andric    }
15830b57cec5SDimitry Andric}
15840b57cec5SDimitry Andric
15850b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
15860b57cec5SDimitry Andrictemplate <class _Up>
15870b57cec5SDimitry Andricvoid
15880b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
15890b57cec5SDimitry Andricvector<_Tp, _Allocator>::__push_back_slow_path(_Up&& __x)
15900b57cec5SDimitry Andric#else
15910b57cec5SDimitry Andricvector<_Tp, _Allocator>::__push_back_slow_path(_Up& __x)
15920b57cec5SDimitry Andric#endif
15930b57cec5SDimitry Andric{
15940b57cec5SDimitry Andric    allocator_type& __a = this->__alloc();
15950b57cec5SDimitry Andric    __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
15960b57cec5SDimitry Andric    // __v.push_back(_VSTD::forward<_Up>(__x));
1597480093f4SDimitry Andric    __alloc_traits::construct(__a, _VSTD::__to_address(__v.__end_), _VSTD::forward<_Up>(__x));
15980b57cec5SDimitry Andric    __v.__end_++;
15990b57cec5SDimitry Andric    __swap_out_circular_buffer(__v);
16000b57cec5SDimitry Andric}
16010b57cec5SDimitry Andric
16020b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
16030b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
16040b57cec5SDimitry Andricvoid
16050b57cec5SDimitry Andricvector<_Tp, _Allocator>::push_back(const_reference __x)
16060b57cec5SDimitry Andric{
16070b57cec5SDimitry Andric    if (this->__end_ != this->__end_cap())
16080b57cec5SDimitry Andric    {
1609e40139ffSDimitry Andric        __construct_one_at_end(__x);
16100b57cec5SDimitry Andric    }
16110b57cec5SDimitry Andric    else
16120b57cec5SDimitry Andric        __push_back_slow_path(__x);
16130b57cec5SDimitry Andric}
16140b57cec5SDimitry Andric
16150b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
16160b57cec5SDimitry Andric
16170b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
16180b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
16190b57cec5SDimitry Andricvoid
16200b57cec5SDimitry Andricvector<_Tp, _Allocator>::push_back(value_type&& __x)
16210b57cec5SDimitry Andric{
16220b57cec5SDimitry Andric    if (this->__end_ < this->__end_cap())
16230b57cec5SDimitry Andric    {
1624e40139ffSDimitry Andric        __construct_one_at_end(_VSTD::move(__x));
16250b57cec5SDimitry Andric    }
16260b57cec5SDimitry Andric    else
16270b57cec5SDimitry Andric        __push_back_slow_path(_VSTD::move(__x));
16280b57cec5SDimitry Andric}
16290b57cec5SDimitry Andric
16300b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
16310b57cec5SDimitry Andrictemplate <class... _Args>
16320b57cec5SDimitry Andricvoid
16330b57cec5SDimitry Andricvector<_Tp, _Allocator>::__emplace_back_slow_path(_Args&&... __args)
16340b57cec5SDimitry Andric{
16350b57cec5SDimitry Andric    allocator_type& __a = this->__alloc();
16360b57cec5SDimitry Andric    __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
16370b57cec5SDimitry Andric//    __v.emplace_back(_VSTD::forward<_Args>(__args)...);
1638480093f4SDimitry Andric    __alloc_traits::construct(__a, _VSTD::__to_address(__v.__end_), _VSTD::forward<_Args>(__args)...);
16390b57cec5SDimitry Andric    __v.__end_++;
16400b57cec5SDimitry Andric    __swap_out_circular_buffer(__v);
16410b57cec5SDimitry Andric}
16420b57cec5SDimitry Andric
16430b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
16440b57cec5SDimitry Andrictemplate <class... _Args>
16450b57cec5SDimitry Andricinline
16460b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 14
16470b57cec5SDimitry Andrictypename vector<_Tp, _Allocator>::reference
16480b57cec5SDimitry Andric#else
16490b57cec5SDimitry Andricvoid
16500b57cec5SDimitry Andric#endif
16510b57cec5SDimitry Andricvector<_Tp, _Allocator>::emplace_back(_Args&&... __args)
16520b57cec5SDimitry Andric{
16530b57cec5SDimitry Andric    if (this->__end_ < this->__end_cap())
16540b57cec5SDimitry Andric    {
1655e40139ffSDimitry Andric        __construct_one_at_end(_VSTD::forward<_Args>(__args)...);
16560b57cec5SDimitry Andric    }
16570b57cec5SDimitry Andric    else
16580b57cec5SDimitry Andric        __emplace_back_slow_path(_VSTD::forward<_Args>(__args)...);
16590b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 14
16600b57cec5SDimitry Andric    return this->back();
16610b57cec5SDimitry Andric#endif
16620b57cec5SDimitry Andric}
16630b57cec5SDimitry Andric
16640b57cec5SDimitry Andric#endif // !_LIBCPP_CXX03_LANG
16650b57cec5SDimitry Andric
16660b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
16670b57cec5SDimitry Andricinline
16680b57cec5SDimitry Andricvoid
16690b57cec5SDimitry Andricvector<_Tp, _Allocator>::pop_back()
16700b57cec5SDimitry Andric{
1671fe6060f1SDimitry Andric    _LIBCPP_ASSERT(!empty(), "vector::pop_back called on an empty vector");
16720b57cec5SDimitry Andric    this->__destruct_at_end(this->__end_ - 1);
16730b57cec5SDimitry Andric}
16740b57cec5SDimitry Andric
16750b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
16760b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
16770b57cec5SDimitry Andrictypename vector<_Tp, _Allocator>::iterator
16780b57cec5SDimitry Andricvector<_Tp, _Allocator>::erase(const_iterator __position)
16790b57cec5SDimitry Andric{
1680e8d8bef9SDimitry Andric#if _LIBCPP_DEBUG_LEVEL == 2
1681349cc55cSDimitry Andric    _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__position)) == this,
16820b57cec5SDimitry Andric        "vector::erase(iterator) called with an iterator not"
16830b57cec5SDimitry Andric        " referring to this vector");
16840b57cec5SDimitry Andric#endif
16850b57cec5SDimitry Andric    _LIBCPP_ASSERT(__position != end(),
16860b57cec5SDimitry Andric        "vector::erase(iterator) called with a non-dereferenceable iterator");
16870b57cec5SDimitry Andric    difference_type __ps = __position - cbegin();
16880b57cec5SDimitry Andric    pointer __p = this->__begin_ + __ps;
16890b57cec5SDimitry Andric    this->__destruct_at_end(_VSTD::move(__p + 1, this->__end_, __p));
16900b57cec5SDimitry Andric    this->__invalidate_iterators_past(__p-1);
16910b57cec5SDimitry Andric    iterator __r = __make_iter(__p);
16920b57cec5SDimitry Andric    return __r;
16930b57cec5SDimitry Andric}
16940b57cec5SDimitry Andric
16950b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
16960b57cec5SDimitry Andrictypename vector<_Tp, _Allocator>::iterator
16970b57cec5SDimitry Andricvector<_Tp, _Allocator>::erase(const_iterator __first, const_iterator __last)
16980b57cec5SDimitry Andric{
1699e8d8bef9SDimitry Andric#if _LIBCPP_DEBUG_LEVEL == 2
1700349cc55cSDimitry Andric    _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__first)) == this,
17010b57cec5SDimitry Andric        "vector::erase(iterator, iterator) called with an iterator not"
17020b57cec5SDimitry Andric        " referring to this vector");
1703349cc55cSDimitry Andric    _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__last)) == this,
17040b57cec5SDimitry Andric        "vector::erase(iterator, iterator) called with an iterator not"
17050b57cec5SDimitry Andric        " referring to this vector");
17060b57cec5SDimitry Andric#endif
17070b57cec5SDimitry Andric    _LIBCPP_ASSERT(__first <= __last, "vector::erase(first, last) called with invalid range");
17080b57cec5SDimitry Andric    pointer __p = this->__begin_ + (__first - begin());
17090b57cec5SDimitry Andric    if (__first != __last) {
17100b57cec5SDimitry Andric        this->__destruct_at_end(_VSTD::move(__p + (__last - __first), this->__end_, __p));
17110b57cec5SDimitry Andric        this->__invalidate_iterators_past(__p - 1);
17120b57cec5SDimitry Andric    }
17130b57cec5SDimitry Andric    iterator __r = __make_iter(__p);
17140b57cec5SDimitry Andric    return __r;
17150b57cec5SDimitry Andric}
17160b57cec5SDimitry Andric
17170b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
17180b57cec5SDimitry Andricvoid
17190b57cec5SDimitry Andricvector<_Tp, _Allocator>::__move_range(pointer __from_s, pointer __from_e, pointer __to)
17200b57cec5SDimitry Andric{
17210b57cec5SDimitry Andric    pointer __old_last = this->__end_;
17220b57cec5SDimitry Andric    difference_type __n = __old_last - __to;
1723e40139ffSDimitry Andric    {
1724e40139ffSDimitry Andric      pointer __i = __from_s + __n;
1725e40139ffSDimitry Andric      _ConstructTransaction __tx(*this, __from_e - __i);
17265ffd83dbSDimitry Andric      for (pointer __pos = __tx.__pos_; __i < __from_e;
1727349cc55cSDimitry Andric           ++__i, (void) ++__pos, __tx.__pos_ = __pos) {
17280b57cec5SDimitry Andric          __alloc_traits::construct(this->__alloc(),
17295ffd83dbSDimitry Andric                                    _VSTD::__to_address(__pos),
17300b57cec5SDimitry Andric                                    _VSTD::move(*__i));
1731e40139ffSDimitry Andric      }
1732e40139ffSDimitry Andric    }
17330b57cec5SDimitry Andric    _VSTD::move_backward(__from_s, __from_s + __n, __old_last);
17340b57cec5SDimitry Andric}
17350b57cec5SDimitry Andric
17360b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
17370b57cec5SDimitry Andrictypename vector<_Tp, _Allocator>::iterator
17380b57cec5SDimitry Andricvector<_Tp, _Allocator>::insert(const_iterator __position, const_reference __x)
17390b57cec5SDimitry Andric{
1740e8d8bef9SDimitry Andric#if _LIBCPP_DEBUG_LEVEL == 2
1741349cc55cSDimitry Andric    _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__position)) == this,
17420b57cec5SDimitry Andric        "vector::insert(iterator, x) called with an iterator not"
17430b57cec5SDimitry Andric        " referring to this vector");
17440b57cec5SDimitry Andric#endif
17450b57cec5SDimitry Andric    pointer __p = this->__begin_ + (__position - begin());
17460b57cec5SDimitry Andric    if (this->__end_ < this->__end_cap())
17470b57cec5SDimitry Andric    {
17480b57cec5SDimitry Andric        if (__p == this->__end_)
17490b57cec5SDimitry Andric        {
1750e40139ffSDimitry Andric            __construct_one_at_end(__x);
17510b57cec5SDimitry Andric        }
17520b57cec5SDimitry Andric        else
17530b57cec5SDimitry Andric        {
17540b57cec5SDimitry Andric            __move_range(__p, this->__end_, __p + 1);
17550b57cec5SDimitry Andric            const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
17560b57cec5SDimitry Andric            if (__p <= __xr && __xr < this->__end_)
17570b57cec5SDimitry Andric                ++__xr;
17580b57cec5SDimitry Andric            *__p = *__xr;
17590b57cec5SDimitry Andric        }
17600b57cec5SDimitry Andric    }
17610b57cec5SDimitry Andric    else
17620b57cec5SDimitry Andric    {
17630b57cec5SDimitry Andric        allocator_type& __a = this->__alloc();
17640b57cec5SDimitry Andric        __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
17650b57cec5SDimitry Andric        __v.push_back(__x);
17660b57cec5SDimitry Andric        __p = __swap_out_circular_buffer(__v, __p);
17670b57cec5SDimitry Andric    }
17680b57cec5SDimitry Andric    return __make_iter(__p);
17690b57cec5SDimitry Andric}
17700b57cec5SDimitry Andric
17710b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
17720b57cec5SDimitry Andric
17730b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
17740b57cec5SDimitry Andrictypename vector<_Tp, _Allocator>::iterator
17750b57cec5SDimitry Andricvector<_Tp, _Allocator>::insert(const_iterator __position, value_type&& __x)
17760b57cec5SDimitry Andric{
1777e8d8bef9SDimitry Andric#if _LIBCPP_DEBUG_LEVEL == 2
1778349cc55cSDimitry Andric    _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__position)) == this,
17790b57cec5SDimitry Andric        "vector::insert(iterator, x) called with an iterator not"
17800b57cec5SDimitry Andric        " referring to this vector");
17810b57cec5SDimitry Andric#endif
17820b57cec5SDimitry Andric    pointer __p = this->__begin_ + (__position - begin());
17830b57cec5SDimitry Andric    if (this->__end_ < this->__end_cap())
17840b57cec5SDimitry Andric    {
17850b57cec5SDimitry Andric        if (__p == this->__end_)
17860b57cec5SDimitry Andric        {
1787e40139ffSDimitry Andric            __construct_one_at_end(_VSTD::move(__x));
17880b57cec5SDimitry Andric        }
17890b57cec5SDimitry Andric        else
17900b57cec5SDimitry Andric        {
17910b57cec5SDimitry Andric            __move_range(__p, this->__end_, __p + 1);
17920b57cec5SDimitry Andric            *__p = _VSTD::move(__x);
17930b57cec5SDimitry Andric        }
17940b57cec5SDimitry Andric    }
17950b57cec5SDimitry Andric    else
17960b57cec5SDimitry Andric    {
17970b57cec5SDimitry Andric        allocator_type& __a = this->__alloc();
17980b57cec5SDimitry Andric        __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
17990b57cec5SDimitry Andric        __v.push_back(_VSTD::move(__x));
18000b57cec5SDimitry Andric        __p = __swap_out_circular_buffer(__v, __p);
18010b57cec5SDimitry Andric    }
18020b57cec5SDimitry Andric    return __make_iter(__p);
18030b57cec5SDimitry Andric}
18040b57cec5SDimitry Andric
18050b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
18060b57cec5SDimitry Andrictemplate <class... _Args>
18070b57cec5SDimitry Andrictypename vector<_Tp, _Allocator>::iterator
18080b57cec5SDimitry Andricvector<_Tp, _Allocator>::emplace(const_iterator __position, _Args&&... __args)
18090b57cec5SDimitry Andric{
1810e8d8bef9SDimitry Andric#if _LIBCPP_DEBUG_LEVEL == 2
1811349cc55cSDimitry Andric    _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__position)) == this,
18120b57cec5SDimitry Andric        "vector::emplace(iterator, x) called with an iterator not"
18130b57cec5SDimitry Andric        " referring to this vector");
18140b57cec5SDimitry Andric#endif
18150b57cec5SDimitry Andric    pointer __p = this->__begin_ + (__position - begin());
18160b57cec5SDimitry Andric    if (this->__end_ < this->__end_cap())
18170b57cec5SDimitry Andric    {
18180b57cec5SDimitry Andric        if (__p == this->__end_)
18190b57cec5SDimitry Andric        {
1820e40139ffSDimitry Andric            __construct_one_at_end(_VSTD::forward<_Args>(__args)...);
18210b57cec5SDimitry Andric        }
18220b57cec5SDimitry Andric        else
18230b57cec5SDimitry Andric        {
18240b57cec5SDimitry Andric            __temp_value<value_type, _Allocator> __tmp(this->__alloc(), _VSTD::forward<_Args>(__args)...);
18250b57cec5SDimitry Andric            __move_range(__p, this->__end_, __p + 1);
18260b57cec5SDimitry Andric            *__p = _VSTD::move(__tmp.get());
18270b57cec5SDimitry Andric        }
18280b57cec5SDimitry Andric    }
18290b57cec5SDimitry Andric    else
18300b57cec5SDimitry Andric    {
18310b57cec5SDimitry Andric        allocator_type& __a = this->__alloc();
18320b57cec5SDimitry Andric        __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
18330b57cec5SDimitry Andric        __v.emplace_back(_VSTD::forward<_Args>(__args)...);
18340b57cec5SDimitry Andric        __p = __swap_out_circular_buffer(__v, __p);
18350b57cec5SDimitry Andric    }
18360b57cec5SDimitry Andric    return __make_iter(__p);
18370b57cec5SDimitry Andric}
18380b57cec5SDimitry Andric
18390b57cec5SDimitry Andric#endif // !_LIBCPP_CXX03_LANG
18400b57cec5SDimitry Andric
18410b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
18420b57cec5SDimitry Andrictypename vector<_Tp, _Allocator>::iterator
18430b57cec5SDimitry Andricvector<_Tp, _Allocator>::insert(const_iterator __position, size_type __n, const_reference __x)
18440b57cec5SDimitry Andric{
1845e8d8bef9SDimitry Andric#if _LIBCPP_DEBUG_LEVEL == 2
1846349cc55cSDimitry Andric    _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__position)) == this,
18470b57cec5SDimitry Andric        "vector::insert(iterator, n, x) called with an iterator not"
18480b57cec5SDimitry Andric        " referring to this vector");
18490b57cec5SDimitry Andric#endif
18500b57cec5SDimitry Andric    pointer __p = this->__begin_ + (__position - begin());
18510b57cec5SDimitry Andric    if (__n > 0)
18520b57cec5SDimitry Andric    {
18530b57cec5SDimitry Andric        if (__n <= static_cast<size_type>(this->__end_cap() - this->__end_))
18540b57cec5SDimitry Andric        {
18550b57cec5SDimitry Andric            size_type __old_n = __n;
18560b57cec5SDimitry Andric            pointer __old_last = this->__end_;
18570b57cec5SDimitry Andric            if (__n > static_cast<size_type>(this->__end_ - __p))
18580b57cec5SDimitry Andric            {
18590b57cec5SDimitry Andric                size_type __cx = __n - (this->__end_ - __p);
18600b57cec5SDimitry Andric                __construct_at_end(__cx, __x);
18610b57cec5SDimitry Andric                __n -= __cx;
18620b57cec5SDimitry Andric            }
18630b57cec5SDimitry Andric            if (__n > 0)
18640b57cec5SDimitry Andric            {
18650b57cec5SDimitry Andric                __move_range(__p, __old_last, __p + __old_n);
18660b57cec5SDimitry Andric                const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
18670b57cec5SDimitry Andric                if (__p <= __xr && __xr < this->__end_)
18680b57cec5SDimitry Andric                    __xr += __old_n;
18690b57cec5SDimitry Andric                _VSTD::fill_n(__p, __n, *__xr);
18700b57cec5SDimitry Andric            }
18710b57cec5SDimitry Andric        }
18720b57cec5SDimitry Andric        else
18730b57cec5SDimitry Andric        {
18740b57cec5SDimitry Andric            allocator_type& __a = this->__alloc();
18750b57cec5SDimitry Andric            __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
18760b57cec5SDimitry Andric            __v.__construct_at_end(__n, __x);
18770b57cec5SDimitry Andric            __p = __swap_out_circular_buffer(__v, __p);
18780b57cec5SDimitry Andric        }
18790b57cec5SDimitry Andric    }
18800b57cec5SDimitry Andric    return __make_iter(__p);
18810b57cec5SDimitry Andric}
18820b57cec5SDimitry Andric
18830b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
18840b57cec5SDimitry Andrictemplate <class _InputIterator>
18850b57cec5SDimitry Andrictypename enable_if
18860b57cec5SDimitry Andric<
1887480093f4SDimitry Andric     __is_cpp17_input_iterator  <_InputIterator>::value &&
1888480093f4SDimitry Andric    !__is_cpp17_forward_iterator<_InputIterator>::value &&
18890b57cec5SDimitry Andric    is_constructible<
18900b57cec5SDimitry Andric       _Tp,
18910b57cec5SDimitry Andric       typename iterator_traits<_InputIterator>::reference>::value,
18920b57cec5SDimitry Andric    typename vector<_Tp, _Allocator>::iterator
18930b57cec5SDimitry Andric>::type
18940b57cec5SDimitry Andricvector<_Tp, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
18950b57cec5SDimitry Andric{
1896e8d8bef9SDimitry Andric#if _LIBCPP_DEBUG_LEVEL == 2
1897349cc55cSDimitry Andric    _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__position)) == this,
18980b57cec5SDimitry Andric        "vector::insert(iterator, range) called with an iterator not"
18990b57cec5SDimitry Andric        " referring to this vector");
19000b57cec5SDimitry Andric#endif
19010b57cec5SDimitry Andric    difference_type __off = __position - begin();
19020b57cec5SDimitry Andric    pointer __p = this->__begin_ + __off;
19030b57cec5SDimitry Andric    allocator_type& __a = this->__alloc();
19040b57cec5SDimitry Andric    pointer __old_last = this->__end_;
19050b57cec5SDimitry Andric    for (; this->__end_ != this->__end_cap() && __first != __last; ++__first)
19060b57cec5SDimitry Andric    {
1907e40139ffSDimitry Andric        __construct_one_at_end(*__first);
19080b57cec5SDimitry Andric    }
19090b57cec5SDimitry Andric    __split_buffer<value_type, allocator_type&> __v(__a);
19100b57cec5SDimitry Andric    if (__first != __last)
19110b57cec5SDimitry Andric    {
19120b57cec5SDimitry Andric#ifndef _LIBCPP_NO_EXCEPTIONS
19130b57cec5SDimitry Andric        try
19140b57cec5SDimitry Andric        {
19150b57cec5SDimitry Andric#endif // _LIBCPP_NO_EXCEPTIONS
19160b57cec5SDimitry Andric            __v.__construct_at_end(__first, __last);
19170b57cec5SDimitry Andric            difference_type __old_size = __old_last - this->__begin_;
19180b57cec5SDimitry Andric            difference_type __old_p = __p - this->__begin_;
19190b57cec5SDimitry Andric            reserve(__recommend(size() + __v.size()));
19200b57cec5SDimitry Andric            __p = this->__begin_ + __old_p;
19210b57cec5SDimitry Andric            __old_last = this->__begin_ + __old_size;
19220b57cec5SDimitry Andric#ifndef _LIBCPP_NO_EXCEPTIONS
19230b57cec5SDimitry Andric        }
19240b57cec5SDimitry Andric        catch (...)
19250b57cec5SDimitry Andric        {
19260b57cec5SDimitry Andric            erase(__make_iter(__old_last), end());
19270b57cec5SDimitry Andric            throw;
19280b57cec5SDimitry Andric        }
19290b57cec5SDimitry Andric#endif // _LIBCPP_NO_EXCEPTIONS
19300b57cec5SDimitry Andric    }
19310b57cec5SDimitry Andric    __p = _VSTD::rotate(__p, __old_last, this->__end_);
19325ffd83dbSDimitry Andric    insert(__make_iter(__p), _VSTD::make_move_iterator(__v.begin()),
19335ffd83dbSDimitry Andric                             _VSTD::make_move_iterator(__v.end()));
19340b57cec5SDimitry Andric    return begin() + __off;
19350b57cec5SDimitry Andric}
19360b57cec5SDimitry Andric
19370b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
19380b57cec5SDimitry Andrictemplate <class _ForwardIterator>
19390b57cec5SDimitry Andrictypename enable_if
19400b57cec5SDimitry Andric<
1941480093f4SDimitry Andric    __is_cpp17_forward_iterator<_ForwardIterator>::value &&
19420b57cec5SDimitry Andric    is_constructible<
19430b57cec5SDimitry Andric       _Tp,
19440b57cec5SDimitry Andric       typename iterator_traits<_ForwardIterator>::reference>::value,
19450b57cec5SDimitry Andric    typename vector<_Tp, _Allocator>::iterator
19460b57cec5SDimitry Andric>::type
19470b57cec5SDimitry Andricvector<_Tp, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
19480b57cec5SDimitry Andric{
1949e8d8bef9SDimitry Andric#if _LIBCPP_DEBUG_LEVEL == 2
1950349cc55cSDimitry Andric    _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__position)) == this,
19510b57cec5SDimitry Andric        "vector::insert(iterator, range) called with an iterator not"
19520b57cec5SDimitry Andric        " referring to this vector");
19530b57cec5SDimitry Andric#endif
19540b57cec5SDimitry Andric    pointer __p = this->__begin_ + (__position - begin());
19550b57cec5SDimitry Andric    difference_type __n = _VSTD::distance(__first, __last);
19560b57cec5SDimitry Andric    if (__n > 0)
19570b57cec5SDimitry Andric    {
19580b57cec5SDimitry Andric        if (__n <= this->__end_cap() - this->__end_)
19590b57cec5SDimitry Andric        {
19600b57cec5SDimitry Andric            size_type __old_n = __n;
19610b57cec5SDimitry Andric            pointer __old_last = this->__end_;
19620b57cec5SDimitry Andric            _ForwardIterator __m = __last;
19630b57cec5SDimitry Andric            difference_type __dx = this->__end_ - __p;
19640b57cec5SDimitry Andric            if (__n > __dx)
19650b57cec5SDimitry Andric            {
19660b57cec5SDimitry Andric                __m = __first;
19670b57cec5SDimitry Andric                difference_type __diff = this->__end_ - __p;
19680b57cec5SDimitry Andric                _VSTD::advance(__m, __diff);
19690b57cec5SDimitry Andric                __construct_at_end(__m, __last, __n - __diff);
19700b57cec5SDimitry Andric                __n = __dx;
19710b57cec5SDimitry Andric            }
19720b57cec5SDimitry Andric            if (__n > 0)
19730b57cec5SDimitry Andric            {
19740b57cec5SDimitry Andric                __move_range(__p, __old_last, __p + __old_n);
19750b57cec5SDimitry Andric                _VSTD::copy(__first, __m, __p);
19760b57cec5SDimitry Andric            }
19770b57cec5SDimitry Andric        }
19780b57cec5SDimitry Andric        else
19790b57cec5SDimitry Andric        {
19800b57cec5SDimitry Andric            allocator_type& __a = this->__alloc();
19810b57cec5SDimitry Andric            __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
19820b57cec5SDimitry Andric            __v.__construct_at_end(__first, __last);
19830b57cec5SDimitry Andric            __p = __swap_out_circular_buffer(__v, __p);
19840b57cec5SDimitry Andric        }
19850b57cec5SDimitry Andric    }
19860b57cec5SDimitry Andric    return __make_iter(__p);
19870b57cec5SDimitry Andric}
19880b57cec5SDimitry Andric
19890b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
19900b57cec5SDimitry Andricvoid
19910b57cec5SDimitry Andricvector<_Tp, _Allocator>::resize(size_type __sz)
19920b57cec5SDimitry Andric{
19930b57cec5SDimitry Andric    size_type __cs = size();
19940b57cec5SDimitry Andric    if (__cs < __sz)
19950b57cec5SDimitry Andric        this->__append(__sz - __cs);
19960b57cec5SDimitry Andric    else if (__cs > __sz)
19970b57cec5SDimitry Andric        this->__destruct_at_end(this->__begin_ + __sz);
19980b57cec5SDimitry Andric}
19990b57cec5SDimitry Andric
20000b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
20010b57cec5SDimitry Andricvoid
20020b57cec5SDimitry Andricvector<_Tp, _Allocator>::resize(size_type __sz, const_reference __x)
20030b57cec5SDimitry Andric{
20040b57cec5SDimitry Andric    size_type __cs = size();
20050b57cec5SDimitry Andric    if (__cs < __sz)
20060b57cec5SDimitry Andric        this->__append(__sz - __cs, __x);
20070b57cec5SDimitry Andric    else if (__cs > __sz)
20080b57cec5SDimitry Andric        this->__destruct_at_end(this->__begin_ + __sz);
20090b57cec5SDimitry Andric}
20100b57cec5SDimitry Andric
20110b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
20120b57cec5SDimitry Andricvoid
20130b57cec5SDimitry Andricvector<_Tp, _Allocator>::swap(vector& __x)
20140b57cec5SDimitry Andric#if _LIBCPP_STD_VER >= 14
20150b57cec5SDimitry Andric    _NOEXCEPT
20160b57cec5SDimitry Andric#else
20170b57cec5SDimitry Andric    _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
20180b57cec5SDimitry Andric                __is_nothrow_swappable<allocator_type>::value)
20190b57cec5SDimitry Andric#endif
20200b57cec5SDimitry Andric{
20210b57cec5SDimitry Andric    _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
20220b57cec5SDimitry Andric                   this->__alloc() == __x.__alloc(),
20230b57cec5SDimitry Andric                   "vector::swap: Either propagate_on_container_swap must be true"
20240b57cec5SDimitry Andric                   " or the allocators must compare equal");
20250b57cec5SDimitry Andric    _VSTD::swap(this->__begin_, __x.__begin_);
20260b57cec5SDimitry Andric    _VSTD::swap(this->__end_, __x.__end_);
20270b57cec5SDimitry Andric    _VSTD::swap(this->__end_cap(), __x.__end_cap());
2028e8d8bef9SDimitry Andric    _VSTD::__swap_allocator(this->__alloc(), __x.__alloc(),
20290b57cec5SDimitry Andric        integral_constant<bool,__alloc_traits::propagate_on_container_swap::value>());
2030e8d8bef9SDimitry Andric#if _LIBCPP_DEBUG_LEVEL == 2
2031349cc55cSDimitry Andric    __get_db()->swap(this, _VSTD::addressof(__x));
2032e8d8bef9SDimitry Andric#endif
20330b57cec5SDimitry Andric}
20340b57cec5SDimitry Andric
20350b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
20360b57cec5SDimitry Andricbool
20370b57cec5SDimitry Andricvector<_Tp, _Allocator>::__invariants() const
20380b57cec5SDimitry Andric{
20390b57cec5SDimitry Andric    if (this->__begin_ == nullptr)
20400b57cec5SDimitry Andric    {
20410b57cec5SDimitry Andric        if (this->__end_ != nullptr || this->__end_cap() != nullptr)
20420b57cec5SDimitry Andric            return false;
20430b57cec5SDimitry Andric    }
20440b57cec5SDimitry Andric    else
20450b57cec5SDimitry Andric    {
20460b57cec5SDimitry Andric        if (this->__begin_ > this->__end_)
20470b57cec5SDimitry Andric            return false;
20480b57cec5SDimitry Andric        if (this->__begin_ == this->__end_cap())
20490b57cec5SDimitry Andric            return false;
20500b57cec5SDimitry Andric        if (this->__end_ > this->__end_cap())
20510b57cec5SDimitry Andric            return false;
20520b57cec5SDimitry Andric    }
20530b57cec5SDimitry Andric    return true;
20540b57cec5SDimitry Andric}
20550b57cec5SDimitry Andric
2056e8d8bef9SDimitry Andric#if _LIBCPP_DEBUG_LEVEL == 2
20570b57cec5SDimitry Andric
20580b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
20590b57cec5SDimitry Andricbool
20600b57cec5SDimitry Andricvector<_Tp, _Allocator>::__dereferenceable(const const_iterator* __i) const
20610b57cec5SDimitry Andric{
20620b57cec5SDimitry Andric    return this->__begin_ <= __i->base() && __i->base() < this->__end_;
20630b57cec5SDimitry Andric}
20640b57cec5SDimitry Andric
20650b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
20660b57cec5SDimitry Andricbool
20670b57cec5SDimitry Andricvector<_Tp, _Allocator>::__decrementable(const const_iterator* __i) const
20680b57cec5SDimitry Andric{
20690b57cec5SDimitry Andric    return this->__begin_ < __i->base() && __i->base() <= this->__end_;
20700b57cec5SDimitry Andric}
20710b57cec5SDimitry Andric
20720b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
20730b57cec5SDimitry Andricbool
20740b57cec5SDimitry Andricvector<_Tp, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
20750b57cec5SDimitry Andric{
20760b57cec5SDimitry Andric    const_pointer __p = __i->base() + __n;
20770b57cec5SDimitry Andric    return this->__begin_ <= __p && __p <= this->__end_;
20780b57cec5SDimitry Andric}
20790b57cec5SDimitry Andric
20800b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
20810b57cec5SDimitry Andricbool
20820b57cec5SDimitry Andricvector<_Tp, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
20830b57cec5SDimitry Andric{
20840b57cec5SDimitry Andric    const_pointer __p = __i->base() + __n;
20850b57cec5SDimitry Andric    return this->__begin_ <= __p && __p < this->__end_;
20860b57cec5SDimitry Andric}
20870b57cec5SDimitry Andric
2088e8d8bef9SDimitry Andric#endif // _LIBCPP_DEBUG_LEVEL == 2
20890b57cec5SDimitry Andric
20900b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
20910b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
20920b57cec5SDimitry Andricvoid
20930b57cec5SDimitry Andricvector<_Tp, _Allocator>::__invalidate_all_iterators()
20940b57cec5SDimitry Andric{
2095e8d8bef9SDimitry Andric#if _LIBCPP_DEBUG_LEVEL == 2
20960b57cec5SDimitry Andric    __get_db()->__invalidate_all(this);
2097e8d8bef9SDimitry Andric#endif
20980b57cec5SDimitry Andric}
20990b57cec5SDimitry Andric
21000b57cec5SDimitry Andric
21010b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
21020b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
21030b57cec5SDimitry Andricvoid
21040b57cec5SDimitry Andricvector<_Tp, _Allocator>::__invalidate_iterators_past(pointer __new_last) {
2105e8d8bef9SDimitry Andric#if _LIBCPP_DEBUG_LEVEL == 2
21060b57cec5SDimitry Andric  __c_node* __c = __get_db()->__find_c_and_lock(this);
21070b57cec5SDimitry Andric  for (__i_node** __p = __c->end_; __p != __c->beg_; ) {
21080b57cec5SDimitry Andric    --__p;
21090b57cec5SDimitry Andric    const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
21100b57cec5SDimitry Andric    if (__i->base() > __new_last) {
21110b57cec5SDimitry Andric      (*__p)->__c_ = nullptr;
21120b57cec5SDimitry Andric      if (--__c->end_ != __p)
2113e8d8bef9SDimitry Andric        _VSTD::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
21140b57cec5SDimitry Andric    }
21150b57cec5SDimitry Andric  }
21160b57cec5SDimitry Andric  __get_db()->unlock();
21170b57cec5SDimitry Andric#else
21180b57cec5SDimitry Andric  ((void)__new_last);
21190b57cec5SDimitry Andric#endif
21200b57cec5SDimitry Andric}
21210b57cec5SDimitry Andric
21220b57cec5SDimitry Andric// vector<bool>
21230b57cec5SDimitry Andric
21240b57cec5SDimitry Andrictemplate <class _Allocator> class vector<bool, _Allocator>;
21250b57cec5SDimitry Andric
21260b57cec5SDimitry Andrictemplate <class _Allocator> struct hash<vector<bool, _Allocator> >;
21270b57cec5SDimitry Andric
21280b57cec5SDimitry Andrictemplate <class _Allocator>
21290b57cec5SDimitry Andricstruct __has_storage_type<vector<bool, _Allocator> >
21300b57cec5SDimitry Andric{
21310b57cec5SDimitry Andric    static const bool value = true;
21320b57cec5SDimitry Andric};
21330b57cec5SDimitry Andric
21340b57cec5SDimitry Andrictemplate <class _Allocator>
21350b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS vector<bool, _Allocator>
21360b57cec5SDimitry Andric    : private __vector_base_common<true>
21370b57cec5SDimitry Andric{
21380b57cec5SDimitry Andricpublic:
21390b57cec5SDimitry Andric    typedef vector                                   __self;
21400b57cec5SDimitry Andric    typedef bool                                     value_type;
21410b57cec5SDimitry Andric    typedef _Allocator                               allocator_type;
21420b57cec5SDimitry Andric    typedef allocator_traits<allocator_type>         __alloc_traits;
21430b57cec5SDimitry Andric    typedef typename __alloc_traits::size_type       size_type;
21440b57cec5SDimitry Andric    typedef typename __alloc_traits::difference_type difference_type;
21450b57cec5SDimitry Andric    typedef size_type __storage_type;
21460b57cec5SDimitry Andric    typedef __bit_iterator<vector, false>            pointer;
21470b57cec5SDimitry Andric    typedef __bit_iterator<vector, true>             const_pointer;
21480b57cec5SDimitry Andric    typedef pointer                                  iterator;
21490b57cec5SDimitry Andric    typedef const_pointer                            const_iterator;
21500b57cec5SDimitry Andric    typedef _VSTD::reverse_iterator<iterator>         reverse_iterator;
21510b57cec5SDimitry Andric    typedef _VSTD::reverse_iterator<const_iterator>   const_reverse_iterator;
21520b57cec5SDimitry Andric
21530b57cec5SDimitry Andricprivate:
21540b57cec5SDimitry Andric    typedef typename __rebind_alloc_helper<__alloc_traits, __storage_type>::type __storage_allocator;
21550b57cec5SDimitry Andric    typedef allocator_traits<__storage_allocator>    __storage_traits;
21560b57cec5SDimitry Andric    typedef typename __storage_traits::pointer       __storage_pointer;
21570b57cec5SDimitry Andric    typedef typename __storage_traits::const_pointer __const_storage_pointer;
21580b57cec5SDimitry Andric
21590b57cec5SDimitry Andric    __storage_pointer                                      __begin_;
21600b57cec5SDimitry Andric    size_type                                              __size_;
21610b57cec5SDimitry Andric    __compressed_pair<size_type, __storage_allocator> __cap_alloc_;
21620b57cec5SDimitry Andricpublic:
21630b57cec5SDimitry Andric    typedef __bit_reference<vector>                  reference;
21640b57cec5SDimitry Andric    typedef __bit_const_reference<vector>            const_reference;
21650b57cec5SDimitry Andricprivate:
21660b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
21670b57cec5SDimitry Andric    size_type& __cap() _NOEXCEPT
21680b57cec5SDimitry Andric        {return __cap_alloc_.first();}
21690b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
21700b57cec5SDimitry Andric    const size_type& __cap() const _NOEXCEPT
21710b57cec5SDimitry Andric        {return __cap_alloc_.first();}
21720b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
21730b57cec5SDimitry Andric    __storage_allocator& __alloc() _NOEXCEPT
21740b57cec5SDimitry Andric        {return __cap_alloc_.second();}
21750b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
21760b57cec5SDimitry Andric    const __storage_allocator& __alloc() const _NOEXCEPT
21770b57cec5SDimitry Andric        {return __cap_alloc_.second();}
21780b57cec5SDimitry Andric
21790b57cec5SDimitry Andric    static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
21800b57cec5SDimitry Andric
21810b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
21820b57cec5SDimitry Andric    static size_type __internal_cap_to_external(size_type __n) _NOEXCEPT
21830b57cec5SDimitry Andric        {return __n * __bits_per_word;}
21840b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
21850b57cec5SDimitry Andric    static size_type __external_cap_to_internal(size_type __n) _NOEXCEPT
21860b57cec5SDimitry Andric        {return (__n - 1) / __bits_per_word + 1;}
21870b57cec5SDimitry Andric
21880b57cec5SDimitry Andricpublic:
21890b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
21900b57cec5SDimitry Andric    vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
21910b57cec5SDimitry Andric
21920b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
21930b57cec5SDimitry Andric#if _LIBCPP_STD_VER <= 14
21940b57cec5SDimitry Andric        _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
21950b57cec5SDimitry Andric#else
21960b57cec5SDimitry Andric        _NOEXCEPT;
21970b57cec5SDimitry Andric#endif
21980b57cec5SDimitry Andric    ~vector();
21990b57cec5SDimitry Andric    explicit vector(size_type __n);
22000b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 11
22010b57cec5SDimitry Andric    explicit vector(size_type __n, const allocator_type& __a);
22020b57cec5SDimitry Andric#endif
22030b57cec5SDimitry Andric    vector(size_type __n, const value_type& __v);
22040b57cec5SDimitry Andric    vector(size_type __n, const value_type& __v, const allocator_type& __a);
22050b57cec5SDimitry Andric    template <class _InputIterator>
22060b57cec5SDimitry Andric        vector(_InputIterator __first, _InputIterator __last,
2207480093f4SDimitry Andric               typename enable_if<__is_cpp17_input_iterator  <_InputIterator>::value &&
2208480093f4SDimitry Andric                                 !__is_cpp17_forward_iterator<_InputIterator>::value>::type* = 0);
22090b57cec5SDimitry Andric    template <class _InputIterator>
22100b57cec5SDimitry Andric        vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2211480093f4SDimitry Andric               typename enable_if<__is_cpp17_input_iterator  <_InputIterator>::value &&
2212480093f4SDimitry Andric                                 !__is_cpp17_forward_iterator<_InputIterator>::value>::type* = 0);
22130b57cec5SDimitry Andric    template <class _ForwardIterator>
22140b57cec5SDimitry Andric        vector(_ForwardIterator __first, _ForwardIterator __last,
2215480093f4SDimitry Andric               typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value>::type* = 0);
22160b57cec5SDimitry Andric    template <class _ForwardIterator>
22170b57cec5SDimitry Andric        vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2218480093f4SDimitry Andric               typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value>::type* = 0);
22190b57cec5SDimitry Andric
22200b57cec5SDimitry Andric    vector(const vector& __v);
22210b57cec5SDimitry Andric    vector(const vector& __v, const allocator_type& __a);
22220b57cec5SDimitry Andric    vector& operator=(const vector& __v);
22230b57cec5SDimitry Andric
22240b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
22250b57cec5SDimitry Andric    vector(initializer_list<value_type> __il);
22260b57cec5SDimitry Andric    vector(initializer_list<value_type> __il, const allocator_type& __a);
22270b57cec5SDimitry Andric
22280b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
22290b57cec5SDimitry Andric    vector(vector&& __v)
22300b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 14
22310b57cec5SDimitry Andric        _NOEXCEPT;
22320b57cec5SDimitry Andric#else
22330b57cec5SDimitry Andric        _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
22340b57cec5SDimitry Andric#endif
2235fe6060f1SDimitry Andric    vector(vector&& __v, const __identity_t<allocator_type>& __a);
22360b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
22370b57cec5SDimitry Andric    vector& operator=(vector&& __v)
22380b57cec5SDimitry Andric        _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
22390b57cec5SDimitry Andric
22400b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
22410b57cec5SDimitry Andric    vector& operator=(initializer_list<value_type> __il)
22420b57cec5SDimitry Andric        {assign(__il.begin(), __il.end()); return *this;}
22430b57cec5SDimitry Andric
22440b57cec5SDimitry Andric#endif // !_LIBCPP_CXX03_LANG
22450b57cec5SDimitry Andric
22460b57cec5SDimitry Andric    template <class _InputIterator>
22470b57cec5SDimitry Andric        typename enable_if
22480b57cec5SDimitry Andric        <
2249480093f4SDimitry Andric            __is_cpp17_input_iterator<_InputIterator>::value &&
2250480093f4SDimitry Andric           !__is_cpp17_forward_iterator<_InputIterator>::value,
22510b57cec5SDimitry Andric           void
22520b57cec5SDimitry Andric        >::type
22530b57cec5SDimitry Andric        assign(_InputIterator __first, _InputIterator __last);
22540b57cec5SDimitry Andric    template <class _ForwardIterator>
22550b57cec5SDimitry Andric        typename enable_if
22560b57cec5SDimitry Andric        <
2257480093f4SDimitry Andric            __is_cpp17_forward_iterator<_ForwardIterator>::value,
22580b57cec5SDimitry Andric           void
22590b57cec5SDimitry Andric        >::type
22600b57cec5SDimitry Andric        assign(_ForwardIterator __first, _ForwardIterator __last);
22610b57cec5SDimitry Andric
22620b57cec5SDimitry Andric    void assign(size_type __n, const value_type& __x);
22630b57cec5SDimitry Andric
22640b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
22650b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
22660b57cec5SDimitry Andric    void assign(initializer_list<value_type> __il)
22670b57cec5SDimitry Andric        {assign(__il.begin(), __il.end());}
22680b57cec5SDimitry Andric#endif
22690b57cec5SDimitry Andric
22700b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY allocator_type get_allocator() const _NOEXCEPT
22710b57cec5SDimitry Andric        {return allocator_type(this->__alloc());}
22720b57cec5SDimitry Andric
22730b57cec5SDimitry Andric    size_type max_size() const _NOEXCEPT;
22740b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
22750b57cec5SDimitry Andric    size_type capacity() const _NOEXCEPT
22760b57cec5SDimitry Andric        {return __internal_cap_to_external(__cap());}
22770b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
22780b57cec5SDimitry Andric    size_type size() const _NOEXCEPT
22790b57cec5SDimitry Andric        {return __size_;}
22800b57cec5SDimitry Andric    _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
22810b57cec5SDimitry Andric    bool empty() const _NOEXCEPT
22820b57cec5SDimitry Andric        {return __size_ == 0;}
22830b57cec5SDimitry Andric    void reserve(size_type __n);
22840b57cec5SDimitry Andric    void shrink_to_fit() _NOEXCEPT;
22850b57cec5SDimitry Andric
22860b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
22870b57cec5SDimitry Andric    iterator begin() _NOEXCEPT
22880b57cec5SDimitry Andric        {return __make_iter(0);}
22890b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
22900b57cec5SDimitry Andric    const_iterator begin() const _NOEXCEPT
22910b57cec5SDimitry Andric        {return __make_iter(0);}
22920b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
22930b57cec5SDimitry Andric    iterator end() _NOEXCEPT
22940b57cec5SDimitry Andric        {return __make_iter(__size_);}
22950b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
22960b57cec5SDimitry Andric    const_iterator end()   const _NOEXCEPT
22970b57cec5SDimitry Andric        {return __make_iter(__size_);}
22980b57cec5SDimitry Andric
22990b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
23000b57cec5SDimitry Andric    reverse_iterator rbegin() _NOEXCEPT
23010b57cec5SDimitry Andric        {return       reverse_iterator(end());}
23020b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
23030b57cec5SDimitry Andric    const_reverse_iterator rbegin() const _NOEXCEPT
23040b57cec5SDimitry Andric        {return const_reverse_iterator(end());}
23050b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
23060b57cec5SDimitry Andric    reverse_iterator rend() _NOEXCEPT
23070b57cec5SDimitry Andric        {return       reverse_iterator(begin());}
23080b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
23090b57cec5SDimitry Andric    const_reverse_iterator rend()   const _NOEXCEPT
23100b57cec5SDimitry Andric        {return const_reverse_iterator(begin());}
23110b57cec5SDimitry Andric
23120b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
23130b57cec5SDimitry Andric    const_iterator         cbegin()  const _NOEXCEPT
23140b57cec5SDimitry Andric        {return __make_iter(0);}
23150b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
23160b57cec5SDimitry Andric    const_iterator         cend()    const _NOEXCEPT
23170b57cec5SDimitry Andric        {return __make_iter(__size_);}
23180b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
23190b57cec5SDimitry Andric    const_reverse_iterator crbegin() const _NOEXCEPT
23200b57cec5SDimitry Andric        {return rbegin();}
23210b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
23220b57cec5SDimitry Andric    const_reverse_iterator crend()   const _NOEXCEPT
23230b57cec5SDimitry Andric        {return rend();}
23240b57cec5SDimitry Andric
23250b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY reference       operator[](size_type __n)       {return __make_ref(__n);}
23260b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const {return __make_ref(__n);}
23270b57cec5SDimitry Andric    reference       at(size_type __n);
23280b57cec5SDimitry Andric    const_reference at(size_type __n) const;
23290b57cec5SDimitry Andric
23300b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY reference       front()       {return __make_ref(0);}
23310b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY const_reference front() const {return __make_ref(0);}
23320b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY reference       back()        {return __make_ref(__size_ - 1);}
23330b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY const_reference back()  const {return __make_ref(__size_ - 1);}
23340b57cec5SDimitry Andric
23350b57cec5SDimitry Andric    void push_back(const value_type& __x);
23360b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 11
23370b57cec5SDimitry Andric    template <class... _Args>
23380b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 14
23390b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY reference emplace_back(_Args&&... __args)
23400b57cec5SDimitry Andric#else
23410b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY void      emplace_back(_Args&&... __args)
23420b57cec5SDimitry Andric#endif
23430b57cec5SDimitry Andric    {
23440b57cec5SDimitry Andric        push_back ( value_type ( _VSTD::forward<_Args>(__args)... ));
23450b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 14
23460b57cec5SDimitry Andric        return this->back();
23470b57cec5SDimitry Andric#endif
23480b57cec5SDimitry Andric    }
23490b57cec5SDimitry Andric#endif
23500b57cec5SDimitry Andric
23510b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY void pop_back() {--__size_;}
23520b57cec5SDimitry Andric
23530b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 11
23540b57cec5SDimitry Andric    template <class... _Args>
23550b57cec5SDimitry Andric   _LIBCPP_INLINE_VISIBILITY iterator emplace(const_iterator position, _Args&&... __args)
23560b57cec5SDimitry Andric        { return insert ( position, value_type ( _VSTD::forward<_Args>(__args)... )); }
23570b57cec5SDimitry Andric#endif
23580b57cec5SDimitry Andric
23590b57cec5SDimitry Andric    iterator insert(const_iterator __position, const value_type& __x);
23600b57cec5SDimitry Andric    iterator insert(const_iterator __position, size_type __n, const value_type& __x);
23610b57cec5SDimitry Andric    iterator insert(const_iterator __position, size_type __n, const_reference __x);
23620b57cec5SDimitry Andric    template <class _InputIterator>
23630b57cec5SDimitry Andric        typename enable_if
23640b57cec5SDimitry Andric        <
2365480093f4SDimitry Andric             __is_cpp17_input_iterator  <_InputIterator>::value &&
2366480093f4SDimitry Andric            !__is_cpp17_forward_iterator<_InputIterator>::value,
23670b57cec5SDimitry Andric            iterator
23680b57cec5SDimitry Andric        >::type
23690b57cec5SDimitry Andric        insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
23700b57cec5SDimitry Andric    template <class _ForwardIterator>
23710b57cec5SDimitry Andric        typename enable_if
23720b57cec5SDimitry Andric        <
2373480093f4SDimitry Andric            __is_cpp17_forward_iterator<_ForwardIterator>::value,
23740b57cec5SDimitry Andric            iterator
23750b57cec5SDimitry Andric        >::type
23760b57cec5SDimitry Andric        insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
23770b57cec5SDimitry Andric
23780b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
23790b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
23800b57cec5SDimitry Andric    iterator insert(const_iterator __position, initializer_list<value_type> __il)
23810b57cec5SDimitry Andric        {return insert(__position, __il.begin(), __il.end());}
23820b57cec5SDimitry Andric#endif
23830b57cec5SDimitry Andric
23840b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
23850b57cec5SDimitry Andric    iterator erase(const_iterator __first, const_iterator __last);
23860b57cec5SDimitry Andric
23870b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
23880b57cec5SDimitry Andric    void clear() _NOEXCEPT {__size_ = 0;}
23890b57cec5SDimitry Andric
23900b57cec5SDimitry Andric    void swap(vector&)
23910b57cec5SDimitry Andric#if _LIBCPP_STD_VER >= 14
23920b57cec5SDimitry Andric        _NOEXCEPT;
23930b57cec5SDimitry Andric#else
23940b57cec5SDimitry Andric        _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
23950b57cec5SDimitry Andric                    __is_nothrow_swappable<allocator_type>::value);
23960b57cec5SDimitry Andric#endif
23970b57cec5SDimitry Andric    static void swap(reference __x, reference __y) _NOEXCEPT { _VSTD::swap(__x, __y); }
23980b57cec5SDimitry Andric
23990b57cec5SDimitry Andric    void resize(size_type __sz, value_type __x = false);
24000b57cec5SDimitry Andric    void flip() _NOEXCEPT;
24010b57cec5SDimitry Andric
24020b57cec5SDimitry Andric    bool __invariants() const;
24030b57cec5SDimitry Andric
24040b57cec5SDimitry Andricprivate:
24050b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
24060b57cec5SDimitry Andric    void __vallocate(size_type __n);
24070b57cec5SDimitry Andric    void __vdeallocate() _NOEXCEPT;
24080b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
24090b57cec5SDimitry Andric    static size_type __align_it(size_type __new_size) _NOEXCEPT
24100b57cec5SDimitry Andric        {return __new_size + (__bits_per_word-1) & ~((size_type)__bits_per_word-1);}
24110b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY  size_type __recommend(size_type __new_size) const;
24120b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY void __construct_at_end(size_type __n, bool __x);
24130b57cec5SDimitry Andric    template <class _ForwardIterator>
24140b57cec5SDimitry Andric        typename enable_if
24150b57cec5SDimitry Andric        <
2416480093f4SDimitry Andric            __is_cpp17_forward_iterator<_ForwardIterator>::value,
24170b57cec5SDimitry Andric            void
24180b57cec5SDimitry Andric        >::type
24190b57cec5SDimitry Andric        __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
24200b57cec5SDimitry Andric    void __append(size_type __n, const_reference __x);
24210b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
24220b57cec5SDimitry Andric    reference __make_ref(size_type __pos) _NOEXCEPT
24230b57cec5SDimitry Andric        {return reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
24240b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
24250b57cec5SDimitry Andric    const_reference __make_ref(size_type __pos) const _NOEXCEPT
24260b57cec5SDimitry Andric        {return const_reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
24270b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
24280b57cec5SDimitry Andric    iterator __make_iter(size_type __pos) _NOEXCEPT
24290b57cec5SDimitry Andric        {return iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
24300b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
24310b57cec5SDimitry Andric    const_iterator __make_iter(size_type __pos) const _NOEXCEPT
24320b57cec5SDimitry Andric        {return const_iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
24330b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
24340b57cec5SDimitry Andric    iterator __const_iterator_cast(const_iterator __p) _NOEXCEPT
24350b57cec5SDimitry Andric        {return begin() + (__p - cbegin());}
24360b57cec5SDimitry Andric
24370b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
24380b57cec5SDimitry Andric    void __copy_assign_alloc(const vector& __v)
24390b57cec5SDimitry Andric        {__copy_assign_alloc(__v, integral_constant<bool,
24400b57cec5SDimitry Andric                      __storage_traits::propagate_on_container_copy_assignment::value>());}
24410b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
24420b57cec5SDimitry Andric    void __copy_assign_alloc(const vector& __c, true_type)
24430b57cec5SDimitry Andric        {
24440b57cec5SDimitry Andric            if (__alloc() != __c.__alloc())
24450b57cec5SDimitry Andric                __vdeallocate();
24460b57cec5SDimitry Andric            __alloc() = __c.__alloc();
24470b57cec5SDimitry Andric        }
24480b57cec5SDimitry Andric
24490b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
24500b57cec5SDimitry Andric    void __copy_assign_alloc(const vector&, false_type)
24510b57cec5SDimitry Andric        {}
24520b57cec5SDimitry Andric
24530b57cec5SDimitry Andric    void __move_assign(vector& __c, false_type);
24540b57cec5SDimitry Andric    void __move_assign(vector& __c, true_type)
24550b57cec5SDimitry Andric        _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
24560b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
24570b57cec5SDimitry Andric    void __move_assign_alloc(vector& __c)
24580b57cec5SDimitry Andric        _NOEXCEPT_(
24590b57cec5SDimitry Andric            !__storage_traits::propagate_on_container_move_assignment::value ||
24600b57cec5SDimitry Andric            is_nothrow_move_assignable<allocator_type>::value)
24610b57cec5SDimitry Andric        {__move_assign_alloc(__c, integral_constant<bool,
24620b57cec5SDimitry Andric                      __storage_traits::propagate_on_container_move_assignment::value>());}
24630b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
24640b57cec5SDimitry Andric    void __move_assign_alloc(vector& __c, true_type)
24650b57cec5SDimitry Andric        _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
24660b57cec5SDimitry Andric        {
24670b57cec5SDimitry Andric            __alloc() = _VSTD::move(__c.__alloc());
24680b57cec5SDimitry Andric        }
24690b57cec5SDimitry Andric
24700b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
24710b57cec5SDimitry Andric    void __move_assign_alloc(vector&, false_type)
24720b57cec5SDimitry Andric        _NOEXCEPT
24730b57cec5SDimitry Andric        {}
24740b57cec5SDimitry Andric
24750b57cec5SDimitry Andric    size_t __hash_code() const _NOEXCEPT;
24760b57cec5SDimitry Andric
24770b57cec5SDimitry Andric    friend class __bit_reference<vector>;
24780b57cec5SDimitry Andric    friend class __bit_const_reference<vector>;
24790b57cec5SDimitry Andric    friend class __bit_iterator<vector, false>;
24800b57cec5SDimitry Andric    friend class __bit_iterator<vector, true>;
24810b57cec5SDimitry Andric    friend struct __bit_array<vector>;
24820b57cec5SDimitry Andric    friend struct _LIBCPP_TEMPLATE_VIS hash<vector>;
24830b57cec5SDimitry Andric};
24840b57cec5SDimitry Andric
24850b57cec5SDimitry Andrictemplate <class _Allocator>
24860b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
24870b57cec5SDimitry Andricvoid
24880b57cec5SDimitry Andricvector<bool, _Allocator>::__invalidate_all_iterators()
24890b57cec5SDimitry Andric{
24900b57cec5SDimitry Andric}
24910b57cec5SDimitry Andric
24920b57cec5SDimitry Andric//  Allocate space for __n objects
24930b57cec5SDimitry Andric//  throws length_error if __n > max_size()
24940b57cec5SDimitry Andric//  throws (probably bad_alloc) if memory run out
24950b57cec5SDimitry Andric//  Precondition:  __begin_ == __end_ == __cap() == 0
24960b57cec5SDimitry Andric//  Precondition:  __n > 0
24970b57cec5SDimitry Andric//  Postcondition:  capacity() == __n
24980b57cec5SDimitry Andric//  Postcondition:  size() == 0
24990b57cec5SDimitry Andrictemplate <class _Allocator>
25000b57cec5SDimitry Andricvoid
25010b57cec5SDimitry Andricvector<bool, _Allocator>::__vallocate(size_type __n)
25020b57cec5SDimitry Andric{
25030b57cec5SDimitry Andric    if (__n > max_size())
25040b57cec5SDimitry Andric        this->__throw_length_error();
25050b57cec5SDimitry Andric    __n = __external_cap_to_internal(__n);
25060b57cec5SDimitry Andric    this->__begin_ = __storage_traits::allocate(this->__alloc(), __n);
25070b57cec5SDimitry Andric    this->__size_ = 0;
25080b57cec5SDimitry Andric    this->__cap() = __n;
25090b57cec5SDimitry Andric}
25100b57cec5SDimitry Andric
25110b57cec5SDimitry Andrictemplate <class _Allocator>
25120b57cec5SDimitry Andricvoid
25130b57cec5SDimitry Andricvector<bool, _Allocator>::__vdeallocate() _NOEXCEPT
25140b57cec5SDimitry Andric{
25150b57cec5SDimitry Andric    if (this->__begin_ != nullptr)
25160b57cec5SDimitry Andric    {
25170b57cec5SDimitry Andric        __storage_traits::deallocate(this->__alloc(), this->__begin_, __cap());
25180b57cec5SDimitry Andric        __invalidate_all_iterators();
25190b57cec5SDimitry Andric        this->__begin_ = nullptr;
25200b57cec5SDimitry Andric        this->__size_ = this->__cap() = 0;
25210b57cec5SDimitry Andric    }
25220b57cec5SDimitry Andric}
25230b57cec5SDimitry Andric
25240b57cec5SDimitry Andrictemplate <class _Allocator>
25250b57cec5SDimitry Andrictypename vector<bool, _Allocator>::size_type
25260b57cec5SDimitry Andricvector<bool, _Allocator>::max_size() const _NOEXCEPT
25270b57cec5SDimitry Andric{
25280b57cec5SDimitry Andric    size_type __amax = __storage_traits::max_size(__alloc());
25290b57cec5SDimitry Andric    size_type __nmax = numeric_limits<size_type>::max() / 2;  // end() >= begin(), always
25300b57cec5SDimitry Andric    if (__nmax / __bits_per_word <= __amax)
25310b57cec5SDimitry Andric        return __nmax;
25320b57cec5SDimitry Andric    return __internal_cap_to_external(__amax);
25330b57cec5SDimitry Andric}
25340b57cec5SDimitry Andric
25350b57cec5SDimitry Andric//  Precondition:  __new_size > capacity()
25360b57cec5SDimitry Andrictemplate <class _Allocator>
25370b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
25380b57cec5SDimitry Andrictypename vector<bool, _Allocator>::size_type
25390b57cec5SDimitry Andricvector<bool, _Allocator>::__recommend(size_type __new_size) const
25400b57cec5SDimitry Andric{
25410b57cec5SDimitry Andric    const size_type __ms = max_size();
25420b57cec5SDimitry Andric    if (__new_size > __ms)
25430b57cec5SDimitry Andric        this->__throw_length_error();
25440b57cec5SDimitry Andric    const size_type __cap = capacity();
25450b57cec5SDimitry Andric    if (__cap >= __ms / 2)
25460b57cec5SDimitry Andric        return __ms;
25470b57cec5SDimitry Andric    return _VSTD::max(2 * __cap, __align_it(__new_size));
25480b57cec5SDimitry Andric}
25490b57cec5SDimitry Andric
25500b57cec5SDimitry Andric//  Default constructs __n objects starting at __end_
25510b57cec5SDimitry Andric//  Precondition:  __n > 0
25520b57cec5SDimitry Andric//  Precondition:  size() + __n <= capacity()
25530b57cec5SDimitry Andric//  Postcondition:  size() == size() + __n
25540b57cec5SDimitry Andrictemplate <class _Allocator>
25550b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
25560b57cec5SDimitry Andricvoid
25570b57cec5SDimitry Andricvector<bool, _Allocator>::__construct_at_end(size_type __n, bool __x)
25580b57cec5SDimitry Andric{
25590b57cec5SDimitry Andric    size_type __old_size = this->__size_;
25600b57cec5SDimitry Andric    this->__size_ += __n;
25610b57cec5SDimitry Andric    if (__old_size == 0 || ((__old_size - 1) / __bits_per_word) != ((this->__size_ - 1) / __bits_per_word))
25620b57cec5SDimitry Andric    {
25630b57cec5SDimitry Andric        if (this->__size_ <= __bits_per_word)
25640b57cec5SDimitry Andric            this->__begin_[0] = __storage_type(0);
25650b57cec5SDimitry Andric        else
25660b57cec5SDimitry Andric            this->__begin_[(this->__size_ - 1) / __bits_per_word] = __storage_type(0);
25670b57cec5SDimitry Andric    }
25680b57cec5SDimitry Andric    _VSTD::fill_n(__make_iter(__old_size), __n, __x);
25690b57cec5SDimitry Andric}
25700b57cec5SDimitry Andric
25710b57cec5SDimitry Andrictemplate <class _Allocator>
25720b57cec5SDimitry Andrictemplate <class _ForwardIterator>
25730b57cec5SDimitry Andrictypename enable_if
25740b57cec5SDimitry Andric<
2575480093f4SDimitry Andric    __is_cpp17_forward_iterator<_ForwardIterator>::value,
25760b57cec5SDimitry Andric    void
25770b57cec5SDimitry Andric>::type
25780b57cec5SDimitry Andricvector<bool, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
25790b57cec5SDimitry Andric{
25800b57cec5SDimitry Andric    size_type __old_size = this->__size_;
25810b57cec5SDimitry Andric    this->__size_ += _VSTD::distance(__first, __last);
25820b57cec5SDimitry Andric    if (__old_size == 0 || ((__old_size - 1) / __bits_per_word) != ((this->__size_ - 1) / __bits_per_word))
25830b57cec5SDimitry Andric    {
25840b57cec5SDimitry Andric        if (this->__size_ <= __bits_per_word)
25850b57cec5SDimitry Andric            this->__begin_[0] = __storage_type(0);
25860b57cec5SDimitry Andric        else
25870b57cec5SDimitry Andric            this->__begin_[(this->__size_ - 1) / __bits_per_word] = __storage_type(0);
25880b57cec5SDimitry Andric    }
25890b57cec5SDimitry Andric    _VSTD::copy(__first, __last, __make_iter(__old_size));
25900b57cec5SDimitry Andric}
25910b57cec5SDimitry Andric
25920b57cec5SDimitry Andrictemplate <class _Allocator>
25930b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
25940b57cec5SDimitry Andricvector<bool, _Allocator>::vector()
25950b57cec5SDimitry Andric    _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
25960b57cec5SDimitry Andric    : __begin_(nullptr),
25970b57cec5SDimitry Andric      __size_(0),
2598480093f4SDimitry Andric      __cap_alloc_(0, __default_init_tag())
25990b57cec5SDimitry Andric{
26000b57cec5SDimitry Andric}
26010b57cec5SDimitry Andric
26020b57cec5SDimitry Andrictemplate <class _Allocator>
26030b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
26040b57cec5SDimitry Andricvector<bool, _Allocator>::vector(const allocator_type& __a)
26050b57cec5SDimitry Andric#if _LIBCPP_STD_VER <= 14
26060b57cec5SDimitry Andric        _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
26070b57cec5SDimitry Andric#else
26080b57cec5SDimitry Andric        _NOEXCEPT
26090b57cec5SDimitry Andric#endif
26100b57cec5SDimitry Andric    : __begin_(nullptr),
26110b57cec5SDimitry Andric      __size_(0),
26120b57cec5SDimitry Andric      __cap_alloc_(0, static_cast<__storage_allocator>(__a))
26130b57cec5SDimitry Andric{
26140b57cec5SDimitry Andric}
26150b57cec5SDimitry Andric
26160b57cec5SDimitry Andrictemplate <class _Allocator>
26170b57cec5SDimitry Andricvector<bool, _Allocator>::vector(size_type __n)
26180b57cec5SDimitry Andric    : __begin_(nullptr),
26190b57cec5SDimitry Andric      __size_(0),
2620480093f4SDimitry Andric      __cap_alloc_(0, __default_init_tag())
26210b57cec5SDimitry Andric{
26220b57cec5SDimitry Andric    if (__n > 0)
26230b57cec5SDimitry Andric    {
26240b57cec5SDimitry Andric        __vallocate(__n);
26250b57cec5SDimitry Andric        __construct_at_end(__n, false);
26260b57cec5SDimitry Andric    }
26270b57cec5SDimitry Andric}
26280b57cec5SDimitry Andric
26290b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 11
26300b57cec5SDimitry Andrictemplate <class _Allocator>
26310b57cec5SDimitry Andricvector<bool, _Allocator>::vector(size_type __n, const allocator_type& __a)
26320b57cec5SDimitry Andric    : __begin_(nullptr),
26330b57cec5SDimitry Andric      __size_(0),
26340b57cec5SDimitry Andric      __cap_alloc_(0, static_cast<__storage_allocator>(__a))
26350b57cec5SDimitry Andric{
26360b57cec5SDimitry Andric    if (__n > 0)
26370b57cec5SDimitry Andric    {
26380b57cec5SDimitry Andric        __vallocate(__n);
26390b57cec5SDimitry Andric        __construct_at_end(__n, false);
26400b57cec5SDimitry Andric    }
26410b57cec5SDimitry Andric}
26420b57cec5SDimitry Andric#endif
26430b57cec5SDimitry Andric
26440b57cec5SDimitry Andrictemplate <class _Allocator>
26450b57cec5SDimitry Andricvector<bool, _Allocator>::vector(size_type __n, const value_type& __x)
26460b57cec5SDimitry Andric    : __begin_(nullptr),
26470b57cec5SDimitry Andric      __size_(0),
2648480093f4SDimitry Andric      __cap_alloc_(0, __default_init_tag())
26490b57cec5SDimitry Andric{
26500b57cec5SDimitry Andric    if (__n > 0)
26510b57cec5SDimitry Andric    {
26520b57cec5SDimitry Andric        __vallocate(__n);
26530b57cec5SDimitry Andric        __construct_at_end(__n, __x);
26540b57cec5SDimitry Andric    }
26550b57cec5SDimitry Andric}
26560b57cec5SDimitry Andric
26570b57cec5SDimitry Andrictemplate <class _Allocator>
26580b57cec5SDimitry Andricvector<bool, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
26590b57cec5SDimitry Andric    : __begin_(nullptr),
26600b57cec5SDimitry Andric      __size_(0),
26610b57cec5SDimitry Andric      __cap_alloc_(0, static_cast<__storage_allocator>(__a))
26620b57cec5SDimitry Andric{
26630b57cec5SDimitry Andric    if (__n > 0)
26640b57cec5SDimitry Andric    {
26650b57cec5SDimitry Andric        __vallocate(__n);
26660b57cec5SDimitry Andric        __construct_at_end(__n, __x);
26670b57cec5SDimitry Andric    }
26680b57cec5SDimitry Andric}
26690b57cec5SDimitry Andric
26700b57cec5SDimitry Andrictemplate <class _Allocator>
26710b57cec5SDimitry Andrictemplate <class _InputIterator>
26720b57cec5SDimitry Andricvector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last,
2673480093f4SDimitry Andric       typename enable_if<__is_cpp17_input_iterator  <_InputIterator>::value &&
2674480093f4SDimitry Andric                         !__is_cpp17_forward_iterator<_InputIterator>::value>::type*)
26750b57cec5SDimitry Andric    : __begin_(nullptr),
26760b57cec5SDimitry Andric      __size_(0),
2677480093f4SDimitry Andric      __cap_alloc_(0, __default_init_tag())
26780b57cec5SDimitry Andric{
26790b57cec5SDimitry Andric#ifndef _LIBCPP_NO_EXCEPTIONS
26800b57cec5SDimitry Andric    try
26810b57cec5SDimitry Andric    {
26820b57cec5SDimitry Andric#endif // _LIBCPP_NO_EXCEPTIONS
26830b57cec5SDimitry Andric        for (; __first != __last; ++__first)
26840b57cec5SDimitry Andric            push_back(*__first);
26850b57cec5SDimitry Andric#ifndef _LIBCPP_NO_EXCEPTIONS
26860b57cec5SDimitry Andric    }
26870b57cec5SDimitry Andric    catch (...)
26880b57cec5SDimitry Andric    {
26890b57cec5SDimitry Andric        if (__begin_ != nullptr)
26900b57cec5SDimitry Andric            __storage_traits::deallocate(__alloc(), __begin_, __cap());
26910b57cec5SDimitry Andric        __invalidate_all_iterators();
26920b57cec5SDimitry Andric        throw;
26930b57cec5SDimitry Andric    }
26940b57cec5SDimitry Andric#endif // _LIBCPP_NO_EXCEPTIONS
26950b57cec5SDimitry Andric}
26960b57cec5SDimitry Andric
26970b57cec5SDimitry Andrictemplate <class _Allocator>
26980b57cec5SDimitry Andrictemplate <class _InputIterator>
26990b57cec5SDimitry Andricvector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2700480093f4SDimitry Andric       typename enable_if<__is_cpp17_input_iterator  <_InputIterator>::value &&
2701480093f4SDimitry Andric                         !__is_cpp17_forward_iterator<_InputIterator>::value>::type*)
27020b57cec5SDimitry Andric    : __begin_(nullptr),
27030b57cec5SDimitry Andric      __size_(0),
27040b57cec5SDimitry Andric      __cap_alloc_(0, static_cast<__storage_allocator>(__a))
27050b57cec5SDimitry Andric{
27060b57cec5SDimitry Andric#ifndef _LIBCPP_NO_EXCEPTIONS
27070b57cec5SDimitry Andric    try
27080b57cec5SDimitry Andric    {
27090b57cec5SDimitry Andric#endif // _LIBCPP_NO_EXCEPTIONS
27100b57cec5SDimitry Andric        for (; __first != __last; ++__first)
27110b57cec5SDimitry Andric            push_back(*__first);
27120b57cec5SDimitry Andric#ifndef _LIBCPP_NO_EXCEPTIONS
27130b57cec5SDimitry Andric    }
27140b57cec5SDimitry Andric    catch (...)
27150b57cec5SDimitry Andric    {
27160b57cec5SDimitry Andric        if (__begin_ != nullptr)
27170b57cec5SDimitry Andric            __storage_traits::deallocate(__alloc(), __begin_, __cap());
27180b57cec5SDimitry Andric        __invalidate_all_iterators();
27190b57cec5SDimitry Andric        throw;
27200b57cec5SDimitry Andric    }
27210b57cec5SDimitry Andric#endif // _LIBCPP_NO_EXCEPTIONS
27220b57cec5SDimitry Andric}
27230b57cec5SDimitry Andric
27240b57cec5SDimitry Andrictemplate <class _Allocator>
27250b57cec5SDimitry Andrictemplate <class _ForwardIterator>
27260b57cec5SDimitry Andricvector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last,
2727480093f4SDimitry Andric                                typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value>::type*)
27280b57cec5SDimitry Andric    : __begin_(nullptr),
27290b57cec5SDimitry Andric      __size_(0),
2730480093f4SDimitry Andric      __cap_alloc_(0, __default_init_tag())
27310b57cec5SDimitry Andric{
27320b57cec5SDimitry Andric    size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
27330b57cec5SDimitry Andric    if (__n > 0)
27340b57cec5SDimitry Andric    {
27350b57cec5SDimitry Andric        __vallocate(__n);
27360b57cec5SDimitry Andric        __construct_at_end(__first, __last);
27370b57cec5SDimitry Andric    }
27380b57cec5SDimitry Andric}
27390b57cec5SDimitry Andric
27400b57cec5SDimitry Andrictemplate <class _Allocator>
27410b57cec5SDimitry Andrictemplate <class _ForwardIterator>
27420b57cec5SDimitry Andricvector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2743480093f4SDimitry Andric                                typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value>::type*)
27440b57cec5SDimitry Andric    : __begin_(nullptr),
27450b57cec5SDimitry Andric      __size_(0),
27460b57cec5SDimitry Andric      __cap_alloc_(0, static_cast<__storage_allocator>(__a))
27470b57cec5SDimitry Andric{
27480b57cec5SDimitry Andric    size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
27490b57cec5SDimitry Andric    if (__n > 0)
27500b57cec5SDimitry Andric    {
27510b57cec5SDimitry Andric        __vallocate(__n);
27520b57cec5SDimitry Andric        __construct_at_end(__first, __last);
27530b57cec5SDimitry Andric    }
27540b57cec5SDimitry Andric}
27550b57cec5SDimitry Andric
27560b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
27570b57cec5SDimitry Andric
27580b57cec5SDimitry Andrictemplate <class _Allocator>
27590b57cec5SDimitry Andricvector<bool, _Allocator>::vector(initializer_list<value_type> __il)
27600b57cec5SDimitry Andric    : __begin_(nullptr),
27610b57cec5SDimitry Andric      __size_(0),
2762480093f4SDimitry Andric      __cap_alloc_(0, __default_init_tag())
27630b57cec5SDimitry Andric{
27640b57cec5SDimitry Andric    size_type __n = static_cast<size_type>(__il.size());
27650b57cec5SDimitry Andric    if (__n > 0)
27660b57cec5SDimitry Andric    {
27670b57cec5SDimitry Andric        __vallocate(__n);
27680b57cec5SDimitry Andric        __construct_at_end(__il.begin(), __il.end());
27690b57cec5SDimitry Andric    }
27700b57cec5SDimitry Andric}
27710b57cec5SDimitry Andric
27720b57cec5SDimitry Andrictemplate <class _Allocator>
27730b57cec5SDimitry Andricvector<bool, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
27740b57cec5SDimitry Andric    : __begin_(nullptr),
27750b57cec5SDimitry Andric      __size_(0),
27760b57cec5SDimitry Andric      __cap_alloc_(0, static_cast<__storage_allocator>(__a))
27770b57cec5SDimitry Andric{
27780b57cec5SDimitry Andric    size_type __n = static_cast<size_type>(__il.size());
27790b57cec5SDimitry Andric    if (__n > 0)
27800b57cec5SDimitry Andric    {
27810b57cec5SDimitry Andric        __vallocate(__n);
27820b57cec5SDimitry Andric        __construct_at_end(__il.begin(), __il.end());
27830b57cec5SDimitry Andric    }
27840b57cec5SDimitry Andric}
27850b57cec5SDimitry Andric
27860b57cec5SDimitry Andric#endif // _LIBCPP_CXX03_LANG
27870b57cec5SDimitry Andric
27880b57cec5SDimitry Andrictemplate <class _Allocator>
27890b57cec5SDimitry Andricvector<bool, _Allocator>::~vector()
27900b57cec5SDimitry Andric{
27910b57cec5SDimitry Andric    if (__begin_ != nullptr)
27920b57cec5SDimitry Andric        __storage_traits::deallocate(__alloc(), __begin_, __cap());
27930b57cec5SDimitry Andric    __invalidate_all_iterators();
27940b57cec5SDimitry Andric}
27950b57cec5SDimitry Andric
27960b57cec5SDimitry Andrictemplate <class _Allocator>
27970b57cec5SDimitry Andricvector<bool, _Allocator>::vector(const vector& __v)
27980b57cec5SDimitry Andric    : __begin_(nullptr),
27990b57cec5SDimitry Andric      __size_(0),
28000b57cec5SDimitry Andric      __cap_alloc_(0, __storage_traits::select_on_container_copy_construction(__v.__alloc()))
28010b57cec5SDimitry Andric{
28020b57cec5SDimitry Andric    if (__v.size() > 0)
28030b57cec5SDimitry Andric    {
28040b57cec5SDimitry Andric        __vallocate(__v.size());
28050b57cec5SDimitry Andric        __construct_at_end(__v.begin(), __v.end());
28060b57cec5SDimitry Andric    }
28070b57cec5SDimitry Andric}
28080b57cec5SDimitry Andric
28090b57cec5SDimitry Andrictemplate <class _Allocator>
28100b57cec5SDimitry Andricvector<bool, _Allocator>::vector(const vector& __v, const allocator_type& __a)
28110b57cec5SDimitry Andric    : __begin_(nullptr),
28120b57cec5SDimitry Andric      __size_(0),
28130b57cec5SDimitry Andric      __cap_alloc_(0, __a)
28140b57cec5SDimitry Andric{
28150b57cec5SDimitry Andric    if (__v.size() > 0)
28160b57cec5SDimitry Andric    {
28170b57cec5SDimitry Andric        __vallocate(__v.size());
28180b57cec5SDimitry Andric        __construct_at_end(__v.begin(), __v.end());
28190b57cec5SDimitry Andric    }
28200b57cec5SDimitry Andric}
28210b57cec5SDimitry Andric
28220b57cec5SDimitry Andrictemplate <class _Allocator>
28230b57cec5SDimitry Andricvector<bool, _Allocator>&
28240b57cec5SDimitry Andricvector<bool, _Allocator>::operator=(const vector& __v)
28250b57cec5SDimitry Andric{
2826349cc55cSDimitry Andric    if (this != _VSTD::addressof(__v))
28270b57cec5SDimitry Andric    {
28280b57cec5SDimitry Andric        __copy_assign_alloc(__v);
28290b57cec5SDimitry Andric        if (__v.__size_)
28300b57cec5SDimitry Andric        {
28310b57cec5SDimitry Andric            if (__v.__size_ > capacity())
28320b57cec5SDimitry Andric            {
28330b57cec5SDimitry Andric                __vdeallocate();
28340b57cec5SDimitry Andric                __vallocate(__v.__size_);
28350b57cec5SDimitry Andric            }
28360b57cec5SDimitry Andric            _VSTD::copy(__v.__begin_, __v.__begin_ + __external_cap_to_internal(__v.__size_), __begin_);
28370b57cec5SDimitry Andric        }
28380b57cec5SDimitry Andric        __size_ = __v.__size_;
28390b57cec5SDimitry Andric    }
28400b57cec5SDimitry Andric    return *this;
28410b57cec5SDimitry Andric}
28420b57cec5SDimitry Andric
28430b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
28440b57cec5SDimitry Andric
28450b57cec5SDimitry Andrictemplate <class _Allocator>
28460b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY vector<bool, _Allocator>::vector(vector&& __v)
28470b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 14
28480b57cec5SDimitry Andric    _NOEXCEPT
28490b57cec5SDimitry Andric#else
28500b57cec5SDimitry Andric    _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
28510b57cec5SDimitry Andric#endif
28520b57cec5SDimitry Andric    : __begin_(__v.__begin_),
28530b57cec5SDimitry Andric      __size_(__v.__size_),
2854e8d8bef9SDimitry Andric      __cap_alloc_(_VSTD::move(__v.__cap_alloc_)) {
28550b57cec5SDimitry Andric    __v.__begin_ = nullptr;
28560b57cec5SDimitry Andric    __v.__size_ = 0;
28570b57cec5SDimitry Andric    __v.__cap() = 0;
28580b57cec5SDimitry Andric}
28590b57cec5SDimitry Andric
28600b57cec5SDimitry Andrictemplate <class _Allocator>
2861fe6060f1SDimitry Andricvector<bool, _Allocator>::vector(vector&& __v, const __identity_t<allocator_type>& __a)
28620b57cec5SDimitry Andric    : __begin_(nullptr),
28630b57cec5SDimitry Andric      __size_(0),
28640b57cec5SDimitry Andric      __cap_alloc_(0, __a)
28650b57cec5SDimitry Andric{
28660b57cec5SDimitry Andric    if (__a == allocator_type(__v.__alloc()))
28670b57cec5SDimitry Andric    {
28680b57cec5SDimitry Andric        this->__begin_ = __v.__begin_;
28690b57cec5SDimitry Andric        this->__size_ = __v.__size_;
28700b57cec5SDimitry Andric        this->__cap() = __v.__cap();
28710b57cec5SDimitry Andric        __v.__begin_ = nullptr;
28720b57cec5SDimitry Andric        __v.__cap() = __v.__size_ = 0;
28730b57cec5SDimitry Andric    }
28740b57cec5SDimitry Andric    else if (__v.size() > 0)
28750b57cec5SDimitry Andric    {
28760b57cec5SDimitry Andric        __vallocate(__v.size());
28770b57cec5SDimitry Andric        __construct_at_end(__v.begin(), __v.end());
28780b57cec5SDimitry Andric    }
28790b57cec5SDimitry Andric}
28800b57cec5SDimitry Andric
28810b57cec5SDimitry Andrictemplate <class _Allocator>
28820b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
28830b57cec5SDimitry Andricvector<bool, _Allocator>&
28840b57cec5SDimitry Andricvector<bool, _Allocator>::operator=(vector&& __v)
28850b57cec5SDimitry Andric    _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
28860b57cec5SDimitry Andric{
28870b57cec5SDimitry Andric    __move_assign(__v, integral_constant<bool,
28880b57cec5SDimitry Andric          __storage_traits::propagate_on_container_move_assignment::value>());
28890b57cec5SDimitry Andric    return *this;
28900b57cec5SDimitry Andric}
28910b57cec5SDimitry Andric
28920b57cec5SDimitry Andrictemplate <class _Allocator>
28930b57cec5SDimitry Andricvoid
28940b57cec5SDimitry Andricvector<bool, _Allocator>::__move_assign(vector& __c, false_type)
28950b57cec5SDimitry Andric{
28960b57cec5SDimitry Andric    if (__alloc() != __c.__alloc())
28970b57cec5SDimitry Andric        assign(__c.begin(), __c.end());
28980b57cec5SDimitry Andric    else
28990b57cec5SDimitry Andric        __move_assign(__c, true_type());
29000b57cec5SDimitry Andric}
29010b57cec5SDimitry Andric
29020b57cec5SDimitry Andrictemplate <class _Allocator>
29030b57cec5SDimitry Andricvoid
29040b57cec5SDimitry Andricvector<bool, _Allocator>::__move_assign(vector& __c, true_type)
29050b57cec5SDimitry Andric    _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
29060b57cec5SDimitry Andric{
29070b57cec5SDimitry Andric    __vdeallocate();
29080b57cec5SDimitry Andric    __move_assign_alloc(__c);
29090b57cec5SDimitry Andric    this->__begin_ = __c.__begin_;
29100b57cec5SDimitry Andric    this->__size_ = __c.__size_;
29110b57cec5SDimitry Andric    this->__cap() = __c.__cap();
29120b57cec5SDimitry Andric    __c.__begin_ = nullptr;
29130b57cec5SDimitry Andric    __c.__cap() = __c.__size_ = 0;
29140b57cec5SDimitry Andric}
29150b57cec5SDimitry Andric
29160b57cec5SDimitry Andric#endif // !_LIBCPP_CXX03_LANG
29170b57cec5SDimitry Andric
29180b57cec5SDimitry Andrictemplate <class _Allocator>
29190b57cec5SDimitry Andricvoid
29200b57cec5SDimitry Andricvector<bool, _Allocator>::assign(size_type __n, const value_type& __x)
29210b57cec5SDimitry Andric{
29220b57cec5SDimitry Andric    __size_ = 0;
29230b57cec5SDimitry Andric    if (__n > 0)
29240b57cec5SDimitry Andric    {
29250b57cec5SDimitry Andric        size_type __c = capacity();
29260b57cec5SDimitry Andric        if (__n <= __c)
29270b57cec5SDimitry Andric            __size_ = __n;
29280b57cec5SDimitry Andric        else
29290b57cec5SDimitry Andric        {
2930349cc55cSDimitry Andric            vector __v(get_allocator());
29310b57cec5SDimitry Andric            __v.reserve(__recommend(__n));
29320b57cec5SDimitry Andric            __v.__size_ = __n;
29330b57cec5SDimitry Andric            swap(__v);
29340b57cec5SDimitry Andric        }
29350b57cec5SDimitry Andric        _VSTD::fill_n(begin(), __n, __x);
29360b57cec5SDimitry Andric    }
29370b57cec5SDimitry Andric  __invalidate_all_iterators();
29380b57cec5SDimitry Andric}
29390b57cec5SDimitry Andric
29400b57cec5SDimitry Andrictemplate <class _Allocator>
29410b57cec5SDimitry Andrictemplate <class _InputIterator>
29420b57cec5SDimitry Andrictypename enable_if
29430b57cec5SDimitry Andric<
2944480093f4SDimitry Andric    __is_cpp17_input_iterator<_InputIterator>::value &&
2945480093f4SDimitry Andric   !__is_cpp17_forward_iterator<_InputIterator>::value,
29460b57cec5SDimitry Andric   void
29470b57cec5SDimitry Andric>::type
29480b57cec5SDimitry Andricvector<bool, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
29490b57cec5SDimitry Andric{
29500b57cec5SDimitry Andric    clear();
29510b57cec5SDimitry Andric    for (; __first != __last; ++__first)
29520b57cec5SDimitry Andric        push_back(*__first);
29530b57cec5SDimitry Andric}
29540b57cec5SDimitry Andric
29550b57cec5SDimitry Andrictemplate <class _Allocator>
29560b57cec5SDimitry Andrictemplate <class _ForwardIterator>
29570b57cec5SDimitry Andrictypename enable_if
29580b57cec5SDimitry Andric<
2959480093f4SDimitry Andric    __is_cpp17_forward_iterator<_ForwardIterator>::value,
29600b57cec5SDimitry Andric   void
29610b57cec5SDimitry Andric>::type
29620b57cec5SDimitry Andricvector<bool, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
29630b57cec5SDimitry Andric{
29640b57cec5SDimitry Andric    clear();
29650b57cec5SDimitry Andric    difference_type __ns = _VSTD::distance(__first, __last);
29660b57cec5SDimitry Andric    _LIBCPP_ASSERT(__ns >= 0, "invalid range specified");
29670b57cec5SDimitry Andric    const size_t __n = static_cast<size_type>(__ns);
29680b57cec5SDimitry Andric    if (__n)
29690b57cec5SDimitry Andric    {
29700b57cec5SDimitry Andric        if (__n > capacity())
29710b57cec5SDimitry Andric        {
29720b57cec5SDimitry Andric            __vdeallocate();
29730b57cec5SDimitry Andric            __vallocate(__n);
29740b57cec5SDimitry Andric        }
29750b57cec5SDimitry Andric        __construct_at_end(__first, __last);
29760b57cec5SDimitry Andric    }
29770b57cec5SDimitry Andric}
29780b57cec5SDimitry Andric
29790b57cec5SDimitry Andrictemplate <class _Allocator>
29800b57cec5SDimitry Andricvoid
29810b57cec5SDimitry Andricvector<bool, _Allocator>::reserve(size_type __n)
29820b57cec5SDimitry Andric{
29830b57cec5SDimitry Andric    if (__n > capacity())
29840b57cec5SDimitry Andric    {
2985349cc55cSDimitry Andric        if (__n > max_size())
2986349cc55cSDimitry Andric            this->__throw_length_error();
2987349cc55cSDimitry Andric        vector __v(this->get_allocator());
29880b57cec5SDimitry Andric        __v.__vallocate(__n);
29890b57cec5SDimitry Andric        __v.__construct_at_end(this->begin(), this->end());
29900b57cec5SDimitry Andric        swap(__v);
29910b57cec5SDimitry Andric        __invalidate_all_iterators();
29920b57cec5SDimitry Andric    }
29930b57cec5SDimitry Andric}
29940b57cec5SDimitry Andric
29950b57cec5SDimitry Andrictemplate <class _Allocator>
29960b57cec5SDimitry Andricvoid
29970b57cec5SDimitry Andricvector<bool, _Allocator>::shrink_to_fit() _NOEXCEPT
29980b57cec5SDimitry Andric{
29990b57cec5SDimitry Andric    if (__external_cap_to_internal(size()) > __cap())
30000b57cec5SDimitry Andric    {
30010b57cec5SDimitry Andric#ifndef _LIBCPP_NO_EXCEPTIONS
30020b57cec5SDimitry Andric        try
30030b57cec5SDimitry Andric        {
30040b57cec5SDimitry Andric#endif // _LIBCPP_NO_EXCEPTIONS
30050b57cec5SDimitry Andric            vector(*this, allocator_type(__alloc())).swap(*this);
30060b57cec5SDimitry Andric#ifndef _LIBCPP_NO_EXCEPTIONS
30070b57cec5SDimitry Andric        }
30080b57cec5SDimitry Andric        catch (...)
30090b57cec5SDimitry Andric        {
30100b57cec5SDimitry Andric        }
30110b57cec5SDimitry Andric#endif // _LIBCPP_NO_EXCEPTIONS
30120b57cec5SDimitry Andric    }
30130b57cec5SDimitry Andric}
30140b57cec5SDimitry Andric
30150b57cec5SDimitry Andrictemplate <class _Allocator>
30160b57cec5SDimitry Andrictypename vector<bool, _Allocator>::reference
30170b57cec5SDimitry Andricvector<bool, _Allocator>::at(size_type __n)
30180b57cec5SDimitry Andric{
30190b57cec5SDimitry Andric    if (__n >= size())
30200b57cec5SDimitry Andric        this->__throw_out_of_range();
30210b57cec5SDimitry Andric    return (*this)[__n];
30220b57cec5SDimitry Andric}
30230b57cec5SDimitry Andric
30240b57cec5SDimitry Andrictemplate <class _Allocator>
30250b57cec5SDimitry Andrictypename vector<bool, _Allocator>::const_reference
30260b57cec5SDimitry Andricvector<bool, _Allocator>::at(size_type __n) const
30270b57cec5SDimitry Andric{
30280b57cec5SDimitry Andric    if (__n >= size())
30290b57cec5SDimitry Andric        this->__throw_out_of_range();
30300b57cec5SDimitry Andric    return (*this)[__n];
30310b57cec5SDimitry Andric}
30320b57cec5SDimitry Andric
30330b57cec5SDimitry Andrictemplate <class _Allocator>
30340b57cec5SDimitry Andricvoid
30350b57cec5SDimitry Andricvector<bool, _Allocator>::push_back(const value_type& __x)
30360b57cec5SDimitry Andric{
30370b57cec5SDimitry Andric    if (this->__size_ == this->capacity())
30380b57cec5SDimitry Andric        reserve(__recommend(this->__size_ + 1));
30390b57cec5SDimitry Andric    ++this->__size_;
30400b57cec5SDimitry Andric    back() = __x;
30410b57cec5SDimitry Andric}
30420b57cec5SDimitry Andric
30430b57cec5SDimitry Andrictemplate <class _Allocator>
30440b57cec5SDimitry Andrictypename vector<bool, _Allocator>::iterator
30450b57cec5SDimitry Andricvector<bool, _Allocator>::insert(const_iterator __position, const value_type& __x)
30460b57cec5SDimitry Andric{
30470b57cec5SDimitry Andric    iterator __r;
30480b57cec5SDimitry Andric    if (size() < capacity())
30490b57cec5SDimitry Andric    {
30500b57cec5SDimitry Andric        const_iterator __old_end = end();
30510b57cec5SDimitry Andric        ++__size_;
30520b57cec5SDimitry Andric        _VSTD::copy_backward(__position, __old_end, end());
30530b57cec5SDimitry Andric        __r = __const_iterator_cast(__position);
30540b57cec5SDimitry Andric    }
30550b57cec5SDimitry Andric    else
30560b57cec5SDimitry Andric    {
3057349cc55cSDimitry Andric        vector __v(get_allocator());
30580b57cec5SDimitry Andric        __v.reserve(__recommend(__size_ + 1));
30590b57cec5SDimitry Andric        __v.__size_ = __size_ + 1;
30600b57cec5SDimitry Andric        __r = _VSTD::copy(cbegin(), __position, __v.begin());
30610b57cec5SDimitry Andric        _VSTD::copy_backward(__position, cend(), __v.end());
30620b57cec5SDimitry Andric        swap(__v);
30630b57cec5SDimitry Andric    }
30640b57cec5SDimitry Andric    *__r = __x;
30650b57cec5SDimitry Andric    return __r;
30660b57cec5SDimitry Andric}
30670b57cec5SDimitry Andric
30680b57cec5SDimitry Andrictemplate <class _Allocator>
30690b57cec5SDimitry Andrictypename vector<bool, _Allocator>::iterator
30700b57cec5SDimitry Andricvector<bool, _Allocator>::insert(const_iterator __position, size_type __n, const value_type& __x)
30710b57cec5SDimitry Andric{
30720b57cec5SDimitry Andric    iterator __r;
30730b57cec5SDimitry Andric    size_type __c = capacity();
30740b57cec5SDimitry Andric    if (__n <= __c && size() <= __c - __n)
30750b57cec5SDimitry Andric    {
30760b57cec5SDimitry Andric        const_iterator __old_end = end();
30770b57cec5SDimitry Andric        __size_ += __n;
30780b57cec5SDimitry Andric        _VSTD::copy_backward(__position, __old_end, end());
30790b57cec5SDimitry Andric        __r = __const_iterator_cast(__position);
30800b57cec5SDimitry Andric    }
30810b57cec5SDimitry Andric    else
30820b57cec5SDimitry Andric    {
3083349cc55cSDimitry Andric        vector __v(get_allocator());
30840b57cec5SDimitry Andric        __v.reserve(__recommend(__size_ + __n));
30850b57cec5SDimitry Andric        __v.__size_ = __size_ + __n;
30860b57cec5SDimitry Andric        __r = _VSTD::copy(cbegin(), __position, __v.begin());
30870b57cec5SDimitry Andric        _VSTD::copy_backward(__position, cend(), __v.end());
30880b57cec5SDimitry Andric        swap(__v);
30890b57cec5SDimitry Andric    }
30900b57cec5SDimitry Andric    _VSTD::fill_n(__r, __n, __x);
30910b57cec5SDimitry Andric    return __r;
30920b57cec5SDimitry Andric}
30930b57cec5SDimitry Andric
30940b57cec5SDimitry Andrictemplate <class _Allocator>
30950b57cec5SDimitry Andrictemplate <class _InputIterator>
30960b57cec5SDimitry Andrictypename enable_if
30970b57cec5SDimitry Andric<
3098480093f4SDimitry Andric     __is_cpp17_input_iterator  <_InputIterator>::value &&
3099480093f4SDimitry Andric    !__is_cpp17_forward_iterator<_InputIterator>::value,
31000b57cec5SDimitry Andric    typename vector<bool, _Allocator>::iterator
31010b57cec5SDimitry Andric>::type
31020b57cec5SDimitry Andricvector<bool, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
31030b57cec5SDimitry Andric{
31040b57cec5SDimitry Andric    difference_type __off = __position - begin();
31050b57cec5SDimitry Andric    iterator __p = __const_iterator_cast(__position);
31060b57cec5SDimitry Andric    iterator __old_end = end();
31070b57cec5SDimitry Andric    for (; size() != capacity() && __first != __last; ++__first)
31080b57cec5SDimitry Andric    {
31090b57cec5SDimitry Andric        ++this->__size_;
31100b57cec5SDimitry Andric        back() = *__first;
31110b57cec5SDimitry Andric    }
3112349cc55cSDimitry Andric    vector __v(get_allocator());
31130b57cec5SDimitry Andric    if (__first != __last)
31140b57cec5SDimitry Andric    {
31150b57cec5SDimitry Andric#ifndef _LIBCPP_NO_EXCEPTIONS
31160b57cec5SDimitry Andric        try
31170b57cec5SDimitry Andric        {
31180b57cec5SDimitry Andric#endif // _LIBCPP_NO_EXCEPTIONS
31190b57cec5SDimitry Andric            __v.assign(__first, __last);
31200b57cec5SDimitry Andric            difference_type __old_size = static_cast<difference_type>(__old_end - begin());
31210b57cec5SDimitry Andric            difference_type __old_p = __p - begin();
31220b57cec5SDimitry Andric            reserve(__recommend(size() + __v.size()));
31230b57cec5SDimitry Andric            __p = begin() + __old_p;
31240b57cec5SDimitry Andric            __old_end = begin() + __old_size;
31250b57cec5SDimitry Andric#ifndef _LIBCPP_NO_EXCEPTIONS
31260b57cec5SDimitry Andric        }
31270b57cec5SDimitry Andric        catch (...)
31280b57cec5SDimitry Andric        {
31290b57cec5SDimitry Andric            erase(__old_end, end());
31300b57cec5SDimitry Andric            throw;
31310b57cec5SDimitry Andric        }
31320b57cec5SDimitry Andric#endif // _LIBCPP_NO_EXCEPTIONS
31330b57cec5SDimitry Andric    }
31340b57cec5SDimitry Andric    __p = _VSTD::rotate(__p, __old_end, end());
31350b57cec5SDimitry Andric    insert(__p, __v.begin(), __v.end());
31360b57cec5SDimitry Andric    return begin() + __off;
31370b57cec5SDimitry Andric}
31380b57cec5SDimitry Andric
31390b57cec5SDimitry Andrictemplate <class _Allocator>
31400b57cec5SDimitry Andrictemplate <class _ForwardIterator>
31410b57cec5SDimitry Andrictypename enable_if
31420b57cec5SDimitry Andric<
3143480093f4SDimitry Andric    __is_cpp17_forward_iterator<_ForwardIterator>::value,
31440b57cec5SDimitry Andric    typename vector<bool, _Allocator>::iterator
31450b57cec5SDimitry Andric>::type
31460b57cec5SDimitry Andricvector<bool, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
31470b57cec5SDimitry Andric{
31480b57cec5SDimitry Andric    const difference_type __n_signed = _VSTD::distance(__first, __last);
31490b57cec5SDimitry Andric    _LIBCPP_ASSERT(__n_signed >= 0, "invalid range specified");
31500b57cec5SDimitry Andric    const size_type __n = static_cast<size_type>(__n_signed);
31510b57cec5SDimitry Andric    iterator __r;
31520b57cec5SDimitry Andric    size_type __c = capacity();
31530b57cec5SDimitry Andric    if (__n <= __c && size() <= __c - __n)
31540b57cec5SDimitry Andric    {
31550b57cec5SDimitry Andric        const_iterator __old_end = end();
31560b57cec5SDimitry Andric        __size_ += __n;
31570b57cec5SDimitry Andric        _VSTD::copy_backward(__position, __old_end, end());
31580b57cec5SDimitry Andric        __r = __const_iterator_cast(__position);
31590b57cec5SDimitry Andric    }
31600b57cec5SDimitry Andric    else
31610b57cec5SDimitry Andric    {
3162349cc55cSDimitry Andric        vector __v(get_allocator());
31630b57cec5SDimitry Andric        __v.reserve(__recommend(__size_ + __n));
31640b57cec5SDimitry Andric        __v.__size_ = __size_ + __n;
31650b57cec5SDimitry Andric        __r = _VSTD::copy(cbegin(), __position, __v.begin());
31660b57cec5SDimitry Andric        _VSTD::copy_backward(__position, cend(), __v.end());
31670b57cec5SDimitry Andric        swap(__v);
31680b57cec5SDimitry Andric    }
31690b57cec5SDimitry Andric    _VSTD::copy(__first, __last, __r);
31700b57cec5SDimitry Andric    return __r;
31710b57cec5SDimitry Andric}
31720b57cec5SDimitry Andric
31730b57cec5SDimitry Andrictemplate <class _Allocator>
31740b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
31750b57cec5SDimitry Andrictypename vector<bool, _Allocator>::iterator
31760b57cec5SDimitry Andricvector<bool, _Allocator>::erase(const_iterator __position)
31770b57cec5SDimitry Andric{
31780b57cec5SDimitry Andric    iterator __r = __const_iterator_cast(__position);
31790b57cec5SDimitry Andric    _VSTD::copy(__position + 1, this->cend(), __r);
31800b57cec5SDimitry Andric    --__size_;
31810b57cec5SDimitry Andric    return __r;
31820b57cec5SDimitry Andric}
31830b57cec5SDimitry Andric
31840b57cec5SDimitry Andrictemplate <class _Allocator>
31850b57cec5SDimitry Andrictypename vector<bool, _Allocator>::iterator
31860b57cec5SDimitry Andricvector<bool, _Allocator>::erase(const_iterator __first, const_iterator __last)
31870b57cec5SDimitry Andric{
31880b57cec5SDimitry Andric    iterator __r = __const_iterator_cast(__first);
31890b57cec5SDimitry Andric    difference_type __d = __last - __first;
31900b57cec5SDimitry Andric    _VSTD::copy(__last, this->cend(), __r);
31910b57cec5SDimitry Andric    __size_ -= __d;
31920b57cec5SDimitry Andric    return __r;
31930b57cec5SDimitry Andric}
31940b57cec5SDimitry Andric
31950b57cec5SDimitry Andrictemplate <class _Allocator>
31960b57cec5SDimitry Andricvoid
31970b57cec5SDimitry Andricvector<bool, _Allocator>::swap(vector& __x)
31980b57cec5SDimitry Andric#if _LIBCPP_STD_VER >= 14
31990b57cec5SDimitry Andric    _NOEXCEPT
32000b57cec5SDimitry Andric#else
32010b57cec5SDimitry Andric    _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
32020b57cec5SDimitry Andric                __is_nothrow_swappable<allocator_type>::value)
32030b57cec5SDimitry Andric#endif
32040b57cec5SDimitry Andric{
32050b57cec5SDimitry Andric    _VSTD::swap(this->__begin_, __x.__begin_);
32060b57cec5SDimitry Andric    _VSTD::swap(this->__size_, __x.__size_);
32070b57cec5SDimitry Andric    _VSTD::swap(this->__cap(), __x.__cap());
3208e8d8bef9SDimitry Andric    _VSTD::__swap_allocator(this->__alloc(), __x.__alloc(),
32090b57cec5SDimitry Andric        integral_constant<bool, __alloc_traits::propagate_on_container_swap::value>());
32100b57cec5SDimitry Andric}
32110b57cec5SDimitry Andric
32120b57cec5SDimitry Andrictemplate <class _Allocator>
32130b57cec5SDimitry Andricvoid
32140b57cec5SDimitry Andricvector<bool, _Allocator>::resize(size_type __sz, value_type __x)
32150b57cec5SDimitry Andric{
32160b57cec5SDimitry Andric    size_type __cs = size();
32170b57cec5SDimitry Andric    if (__cs < __sz)
32180b57cec5SDimitry Andric    {
32190b57cec5SDimitry Andric        iterator __r;
32200b57cec5SDimitry Andric        size_type __c = capacity();
32210b57cec5SDimitry Andric        size_type __n = __sz - __cs;
32220b57cec5SDimitry Andric        if (__n <= __c && __cs <= __c - __n)
32230b57cec5SDimitry Andric        {
32240b57cec5SDimitry Andric            __r = end();
32250b57cec5SDimitry Andric            __size_ += __n;
32260b57cec5SDimitry Andric        }
32270b57cec5SDimitry Andric        else
32280b57cec5SDimitry Andric        {
3229349cc55cSDimitry Andric            vector __v(get_allocator());
32300b57cec5SDimitry Andric            __v.reserve(__recommend(__size_ + __n));
32310b57cec5SDimitry Andric            __v.__size_ = __size_ + __n;
32320b57cec5SDimitry Andric            __r = _VSTD::copy(cbegin(), cend(), __v.begin());
32330b57cec5SDimitry Andric            swap(__v);
32340b57cec5SDimitry Andric        }
32350b57cec5SDimitry Andric        _VSTD::fill_n(__r, __n, __x);
32360b57cec5SDimitry Andric    }
32370b57cec5SDimitry Andric    else
32380b57cec5SDimitry Andric        __size_ = __sz;
32390b57cec5SDimitry Andric}
32400b57cec5SDimitry Andric
32410b57cec5SDimitry Andrictemplate <class _Allocator>
32420b57cec5SDimitry Andricvoid
32430b57cec5SDimitry Andricvector<bool, _Allocator>::flip() _NOEXCEPT
32440b57cec5SDimitry Andric{
32450b57cec5SDimitry Andric    // do middle whole words
32460b57cec5SDimitry Andric    size_type __n = __size_;
32470b57cec5SDimitry Andric    __storage_pointer __p = __begin_;
32480b57cec5SDimitry Andric    for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
32490b57cec5SDimitry Andric        *__p = ~*__p;
32500b57cec5SDimitry Andric    // do last partial word
32510b57cec5SDimitry Andric    if (__n > 0)
32520b57cec5SDimitry Andric    {
32530b57cec5SDimitry Andric        __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
32540b57cec5SDimitry Andric        __storage_type __b = *__p & __m;
32550b57cec5SDimitry Andric        *__p &= ~__m;
32560b57cec5SDimitry Andric        *__p |= ~__b & __m;
32570b57cec5SDimitry Andric    }
32580b57cec5SDimitry Andric}
32590b57cec5SDimitry Andric
32600b57cec5SDimitry Andrictemplate <class _Allocator>
32610b57cec5SDimitry Andricbool
32620b57cec5SDimitry Andricvector<bool, _Allocator>::__invariants() const
32630b57cec5SDimitry Andric{
32640b57cec5SDimitry Andric    if (this->__begin_ == nullptr)
32650b57cec5SDimitry Andric    {
32660b57cec5SDimitry Andric        if (this->__size_ != 0 || this->__cap() != 0)
32670b57cec5SDimitry Andric            return false;
32680b57cec5SDimitry Andric    }
32690b57cec5SDimitry Andric    else
32700b57cec5SDimitry Andric    {
32710b57cec5SDimitry Andric        if (this->__cap() == 0)
32720b57cec5SDimitry Andric            return false;
32730b57cec5SDimitry Andric        if (this->__size_ > this->capacity())
32740b57cec5SDimitry Andric            return false;
32750b57cec5SDimitry Andric    }
32760b57cec5SDimitry Andric    return true;
32770b57cec5SDimitry Andric}
32780b57cec5SDimitry Andric
32790b57cec5SDimitry Andrictemplate <class _Allocator>
32800b57cec5SDimitry Andricsize_t
32810b57cec5SDimitry Andricvector<bool, _Allocator>::__hash_code() const _NOEXCEPT
32820b57cec5SDimitry Andric{
32830b57cec5SDimitry Andric    size_t __h = 0;
32840b57cec5SDimitry Andric    // do middle whole words
32850b57cec5SDimitry Andric    size_type __n = __size_;
32860b57cec5SDimitry Andric    __storage_pointer __p = __begin_;
32870b57cec5SDimitry Andric    for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
32880b57cec5SDimitry Andric        __h ^= *__p;
32890b57cec5SDimitry Andric    // do last partial word
32900b57cec5SDimitry Andric    if (__n > 0)
32910b57cec5SDimitry Andric    {
32920b57cec5SDimitry Andric        const __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
32930b57cec5SDimitry Andric        __h ^= *__p & __m;
32940b57cec5SDimitry Andric    }
32950b57cec5SDimitry Andric    return __h;
32960b57cec5SDimitry Andric}
32970b57cec5SDimitry Andric
32980b57cec5SDimitry Andrictemplate <class _Allocator>
32990b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS hash<vector<bool, _Allocator> >
33000b57cec5SDimitry Andric    : public unary_function<vector<bool, _Allocator>, size_t>
33010b57cec5SDimitry Andric{
33020b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
33030b57cec5SDimitry Andric    size_t operator()(const vector<bool, _Allocator>& __vec) const _NOEXCEPT
33040b57cec5SDimitry Andric        {return __vec.__hash_code();}
33050b57cec5SDimitry Andric};
33060b57cec5SDimitry Andric
33070b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
33080b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
33090b57cec5SDimitry Andricbool
33100b57cec5SDimitry Andricoperator==(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
33110b57cec5SDimitry Andric{
33120b57cec5SDimitry Andric    const typename vector<_Tp, _Allocator>::size_type __sz = __x.size();
33130b57cec5SDimitry Andric    return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
33140b57cec5SDimitry Andric}
33150b57cec5SDimitry Andric
33160b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
33170b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
33180b57cec5SDimitry Andricbool
33190b57cec5SDimitry Andricoperator!=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
33200b57cec5SDimitry Andric{
33210b57cec5SDimitry Andric    return !(__x == __y);
33220b57cec5SDimitry Andric}
33230b57cec5SDimitry Andric
33240b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
33250b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
33260b57cec5SDimitry Andricbool
33270b57cec5SDimitry Andricoperator< (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
33280b57cec5SDimitry Andric{
33290b57cec5SDimitry Andric    return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
33300b57cec5SDimitry Andric}
33310b57cec5SDimitry Andric
33320b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
33330b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
33340b57cec5SDimitry Andricbool
33350b57cec5SDimitry Andricoperator> (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
33360b57cec5SDimitry Andric{
33370b57cec5SDimitry Andric    return __y < __x;
33380b57cec5SDimitry Andric}
33390b57cec5SDimitry Andric
33400b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
33410b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
33420b57cec5SDimitry Andricbool
33430b57cec5SDimitry Andricoperator>=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
33440b57cec5SDimitry Andric{
33450b57cec5SDimitry Andric    return !(__x < __y);
33460b57cec5SDimitry Andric}
33470b57cec5SDimitry Andric
33480b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
33490b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
33500b57cec5SDimitry Andricbool
33510b57cec5SDimitry Andricoperator<=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
33520b57cec5SDimitry Andric{
33530b57cec5SDimitry Andric    return !(__y < __x);
33540b57cec5SDimitry Andric}
33550b57cec5SDimitry Andric
33560b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
33570b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
33580b57cec5SDimitry Andricvoid
33590b57cec5SDimitry Andricswap(vector<_Tp, _Allocator>& __x, vector<_Tp, _Allocator>& __y)
33600b57cec5SDimitry Andric    _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
33610b57cec5SDimitry Andric{
33620b57cec5SDimitry Andric    __x.swap(__y);
33630b57cec5SDimitry Andric}
33640b57cec5SDimitry Andric
33650b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 17
33660b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator, class _Up>
33675ffd83dbSDimitry Andricinline _LIBCPP_INLINE_VISIBILITY typename vector<_Tp, _Allocator>::size_type
33685ffd83dbSDimitry Andricerase(vector<_Tp, _Allocator>& __c, const _Up& __v) {
33695ffd83dbSDimitry Andric  auto __old_size = __c.size();
33705ffd83dbSDimitry Andric  __c.erase(_VSTD::remove(__c.begin(), __c.end(), __v), __c.end());
33715ffd83dbSDimitry Andric  return __old_size - __c.size();
33725ffd83dbSDimitry Andric}
33730b57cec5SDimitry Andric
33740b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator, class _Predicate>
33755ffd83dbSDimitry Andricinline _LIBCPP_INLINE_VISIBILITY typename vector<_Tp, _Allocator>::size_type
33765ffd83dbSDimitry Andricerase_if(vector<_Tp, _Allocator>& __c, _Predicate __pred) {
33775ffd83dbSDimitry Andric  auto __old_size = __c.size();
33785ffd83dbSDimitry Andric  __c.erase(_VSTD::remove_if(__c.begin(), __c.end(), __pred), __c.end());
33795ffd83dbSDimitry Andric  return __old_size - __c.size();
33805ffd83dbSDimitry Andric}
33810b57cec5SDimitry Andric#endif
33820b57cec5SDimitry Andric
33830b57cec5SDimitry Andric_LIBCPP_END_NAMESPACE_STD
33840b57cec5SDimitry Andric
33850b57cec5SDimitry Andric_LIBCPP_POP_MACROS
33860b57cec5SDimitry Andric
33870b57cec5SDimitry Andric#endif // _LIBCPP_VECTOR
3388