xref: /freebsd/contrib/llvm-project/libcxx/include/vector (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
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
135f757f3fSDimitry Andric// clang-format off
145f757f3fSDimitry Andric
150b57cec5SDimitry Andric/*
160b57cec5SDimitry Andric    vector synopsis
170b57cec5SDimitry Andric
180b57cec5SDimitry Andricnamespace std
190b57cec5SDimitry Andric{
200b57cec5SDimitry Andric
210b57cec5SDimitry Andrictemplate <class T, class Allocator = allocator<T> >
220b57cec5SDimitry Andricclass vector
230b57cec5SDimitry Andric{
240b57cec5SDimitry Andricpublic:
250b57cec5SDimitry Andric    typedef T                                        value_type;
260b57cec5SDimitry Andric    typedef Allocator                                allocator_type;
270b57cec5SDimitry Andric    typedef typename allocator_type::reference       reference;
280b57cec5SDimitry Andric    typedef typename allocator_type::const_reference const_reference;
290b57cec5SDimitry Andric    typedef implementation-defined                   iterator;
300b57cec5SDimitry Andric    typedef implementation-defined                   const_iterator;
310b57cec5SDimitry Andric    typedef typename allocator_type::size_type       size_type;
320b57cec5SDimitry Andric    typedef typename allocator_type::difference_type difference_type;
330b57cec5SDimitry Andric    typedef typename allocator_type::pointer         pointer;
340b57cec5SDimitry Andric    typedef typename allocator_type::const_pointer   const_pointer;
350b57cec5SDimitry Andric    typedef std::reverse_iterator<iterator>          reverse_iterator;
360b57cec5SDimitry Andric    typedef std::reverse_iterator<const_iterator>    const_reverse_iterator;
370b57cec5SDimitry Andric
380b57cec5SDimitry Andric    vector()
390b57cec5SDimitry Andric        noexcept(is_nothrow_default_constructible<allocator_type>::value);
400b57cec5SDimitry Andric    explicit vector(const allocator_type&);
410b57cec5SDimitry Andric    explicit vector(size_type n);
420b57cec5SDimitry Andric    explicit vector(size_type n, const allocator_type&); // C++14
430b57cec5SDimitry Andric    vector(size_type n, const value_type& value, const allocator_type& = allocator_type());
440b57cec5SDimitry Andric    template <class InputIterator>
450b57cec5SDimitry Andric        vector(InputIterator first, InputIterator last, const allocator_type& = allocator_type());
4606c3fb27SDimitry Andric    template<container-compatible-range<T> R>
4706c3fb27SDimitry Andric      constexpr vector(from_range_t, R&& rg, const Allocator& = Allocator()); // C++23
480b57cec5SDimitry Andric    vector(const vector& x);
490b57cec5SDimitry Andric    vector(vector&& x)
500b57cec5SDimitry Andric        noexcept(is_nothrow_move_constructible<allocator_type>::value);
510b57cec5SDimitry Andric    vector(initializer_list<value_type> il);
520b57cec5SDimitry Andric    vector(initializer_list<value_type> il, const allocator_type& a);
530b57cec5SDimitry Andric    ~vector();
540b57cec5SDimitry Andric    vector& operator=(const vector& x);
550b57cec5SDimitry Andric    vector& operator=(vector&& x)
560b57cec5SDimitry Andric        noexcept(
570b57cec5SDimitry Andric             allocator_type::propagate_on_container_move_assignment::value ||
580b57cec5SDimitry Andric             allocator_type::is_always_equal::value); // C++17
590b57cec5SDimitry Andric    vector& operator=(initializer_list<value_type> il);
600b57cec5SDimitry Andric    template <class InputIterator>
610b57cec5SDimitry Andric        void assign(InputIterator first, InputIterator last);
6206c3fb27SDimitry Andric    template<container-compatible-range<T> R>
6306c3fb27SDimitry Andric      constexpr void assign_range(R&& rg); // C++23
640b57cec5SDimitry Andric    void assign(size_type n, const value_type& u);
650b57cec5SDimitry Andric    void assign(initializer_list<value_type> il);
660b57cec5SDimitry Andric
670b57cec5SDimitry Andric    allocator_type get_allocator() const noexcept;
680b57cec5SDimitry Andric
690b57cec5SDimitry Andric    iterator               begin() noexcept;
700b57cec5SDimitry Andric    const_iterator         begin()   const noexcept;
710b57cec5SDimitry Andric    iterator               end() noexcept;
720b57cec5SDimitry Andric    const_iterator         end()     const noexcept;
730b57cec5SDimitry Andric
740b57cec5SDimitry Andric    reverse_iterator       rbegin() noexcept;
750b57cec5SDimitry Andric    const_reverse_iterator rbegin()  const noexcept;
760b57cec5SDimitry Andric    reverse_iterator       rend() noexcept;
770b57cec5SDimitry Andric    const_reverse_iterator rend()    const noexcept;
780b57cec5SDimitry Andric
790b57cec5SDimitry Andric    const_iterator         cbegin()  const noexcept;
800b57cec5SDimitry Andric    const_iterator         cend()    const noexcept;
810b57cec5SDimitry Andric    const_reverse_iterator crbegin() const noexcept;
820b57cec5SDimitry Andric    const_reverse_iterator crend()   const noexcept;
830b57cec5SDimitry Andric
840b57cec5SDimitry Andric    size_type size() const noexcept;
850b57cec5SDimitry Andric    size_type max_size() const noexcept;
860b57cec5SDimitry Andric    size_type capacity() const noexcept;
870b57cec5SDimitry Andric    bool empty() const noexcept;
880b57cec5SDimitry Andric    void reserve(size_type n);
890b57cec5SDimitry Andric    void shrink_to_fit() noexcept;
900b57cec5SDimitry Andric
910b57cec5SDimitry Andric    reference       operator[](size_type n);
920b57cec5SDimitry Andric    const_reference operator[](size_type n) const;
930b57cec5SDimitry Andric    reference       at(size_type n);
940b57cec5SDimitry Andric    const_reference at(size_type n) const;
950b57cec5SDimitry Andric
960b57cec5SDimitry Andric    reference       front();
970b57cec5SDimitry Andric    const_reference front() const;
980b57cec5SDimitry Andric    reference       back();
990b57cec5SDimitry Andric    const_reference back() const;
1000b57cec5SDimitry Andric
1010b57cec5SDimitry Andric    value_type*       data() noexcept;
1020b57cec5SDimitry Andric    const value_type* data() const noexcept;
1030b57cec5SDimitry Andric
1040b57cec5SDimitry Andric    void push_back(const value_type& x);
1050b57cec5SDimitry Andric    void push_back(value_type&& x);
1060b57cec5SDimitry Andric    template <class... Args>
1070b57cec5SDimitry Andric        reference emplace_back(Args&&... args); // reference in C++17
10806c3fb27SDimitry Andric    template<container-compatible-range<T> R>
10906c3fb27SDimitry Andric      constexpr void append_range(R&& rg); // C++23
1100b57cec5SDimitry Andric    void pop_back();
1110b57cec5SDimitry Andric
1120b57cec5SDimitry Andric    template <class... Args> iterator emplace(const_iterator position, Args&&... args);
1130b57cec5SDimitry Andric    iterator insert(const_iterator position, const value_type& x);
1140b57cec5SDimitry Andric    iterator insert(const_iterator position, value_type&& x);
1150b57cec5SDimitry Andric    iterator insert(const_iterator position, size_type n, const value_type& x);
1160b57cec5SDimitry Andric    template <class InputIterator>
1170b57cec5SDimitry Andric        iterator insert(const_iterator position, InputIterator first, InputIterator last);
11806c3fb27SDimitry Andric    template<container-compatible-range<T> R>
11906c3fb27SDimitry Andric      constexpr iterator insert_range(const_iterator position, R&& rg); // C++23
1200b57cec5SDimitry Andric    iterator insert(const_iterator position, initializer_list<value_type> il);
1210b57cec5SDimitry Andric
1220b57cec5SDimitry Andric    iterator erase(const_iterator position);
1230b57cec5SDimitry Andric    iterator erase(const_iterator first, const_iterator last);
1240b57cec5SDimitry Andric
1250b57cec5SDimitry Andric    void clear() noexcept;
1260b57cec5SDimitry Andric
1270b57cec5SDimitry Andric    void resize(size_type sz);
1280b57cec5SDimitry Andric    void resize(size_type sz, const value_type& c);
1290b57cec5SDimitry Andric
1300b57cec5SDimitry Andric    void swap(vector&)
1310b57cec5SDimitry Andric        noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
1320b57cec5SDimitry Andric                 allocator_traits<allocator_type>::is_always_equal::value);  // C++17
1330b57cec5SDimitry Andric
1340b57cec5SDimitry Andric    bool __invariants() const;
1350b57cec5SDimitry Andric};
1360b57cec5SDimitry Andric
1370b57cec5SDimitry Andrictemplate <class Allocator = allocator<T> >
1380b57cec5SDimitry Andricclass vector<bool, Allocator>
1390b57cec5SDimitry Andric{
1400b57cec5SDimitry Andricpublic:
1410b57cec5SDimitry Andric    typedef bool                                     value_type;
1420b57cec5SDimitry Andric    typedef Allocator                                allocator_type;
1430b57cec5SDimitry Andric    typedef implementation-defined                   iterator;
1440b57cec5SDimitry Andric    typedef implementation-defined                   const_iterator;
1450b57cec5SDimitry Andric    typedef typename allocator_type::size_type       size_type;
1460b57cec5SDimitry Andric    typedef typename allocator_type::difference_type difference_type;
1470b57cec5SDimitry Andric    typedef iterator                                 pointer;
1480b57cec5SDimitry Andric    typedef const_iterator                           const_pointer;
1490b57cec5SDimitry Andric    typedef std::reverse_iterator<iterator>          reverse_iterator;
1500b57cec5SDimitry Andric    typedef std::reverse_iterator<const_iterator>    const_reverse_iterator;
1510b57cec5SDimitry Andric
1520b57cec5SDimitry Andric    class reference
1530b57cec5SDimitry Andric    {
1540b57cec5SDimitry Andric    public:
1550b57cec5SDimitry Andric        reference(const reference&) noexcept;
1560b57cec5SDimitry Andric        operator bool() const noexcept;
157fe6060f1SDimitry Andric        reference& operator=(bool x) noexcept;
1580b57cec5SDimitry Andric        reference& operator=(const reference& x) noexcept;
1590b57cec5SDimitry Andric        iterator operator&() const noexcept;
1600b57cec5SDimitry Andric        void flip() noexcept;
1610b57cec5SDimitry Andric    };
1620b57cec5SDimitry Andric
1630b57cec5SDimitry Andric    class const_reference
1640b57cec5SDimitry Andric    {
1650b57cec5SDimitry Andric    public:
1660b57cec5SDimitry Andric        const_reference(const reference&) noexcept;
1670b57cec5SDimitry Andric        operator bool() const noexcept;
1680b57cec5SDimitry Andric        const_iterator operator&() const noexcept;
1690b57cec5SDimitry Andric    };
1700b57cec5SDimitry Andric
1710b57cec5SDimitry Andric    vector()
1720b57cec5SDimitry Andric        noexcept(is_nothrow_default_constructible<allocator_type>::value);
1730b57cec5SDimitry Andric    explicit vector(const allocator_type&);
1740b57cec5SDimitry Andric    explicit vector(size_type n, const allocator_type& a = allocator_type()); // C++14
1750b57cec5SDimitry Andric    vector(size_type n, const value_type& value, const allocator_type& = allocator_type());
1760b57cec5SDimitry Andric    template <class InputIterator>
1770b57cec5SDimitry Andric        vector(InputIterator first, InputIterator last, const allocator_type& = allocator_type());
17806c3fb27SDimitry Andric    template<container-compatible-range<bool> R>
17906c3fb27SDimitry Andric      constexpr vector(from_range_t, R&& rg, const Allocator& = Allocator());
1800b57cec5SDimitry Andric    vector(const vector& x);
1810b57cec5SDimitry Andric    vector(vector&& x)
1820b57cec5SDimitry Andric        noexcept(is_nothrow_move_constructible<allocator_type>::value);
1830b57cec5SDimitry Andric    vector(initializer_list<value_type> il);
1840b57cec5SDimitry Andric    vector(initializer_list<value_type> il, const allocator_type& a);
1850b57cec5SDimitry Andric    ~vector();
1860b57cec5SDimitry Andric    vector& operator=(const vector& x);
1870b57cec5SDimitry Andric    vector& operator=(vector&& x)
1880b57cec5SDimitry Andric        noexcept(
1890b57cec5SDimitry Andric             allocator_type::propagate_on_container_move_assignment::value ||
1900b57cec5SDimitry Andric             allocator_type::is_always_equal::value); // C++17
1910b57cec5SDimitry Andric    vector& operator=(initializer_list<value_type> il);
1920b57cec5SDimitry Andric    template <class InputIterator>
1930b57cec5SDimitry Andric        void assign(InputIterator first, InputIterator last);
19406c3fb27SDimitry Andric    template<container-compatible-range<T> R>
19506c3fb27SDimitry Andric      constexpr void assign_range(R&& rg); // C++23
1960b57cec5SDimitry Andric    void assign(size_type n, const value_type& u);
1970b57cec5SDimitry Andric    void assign(initializer_list<value_type> il);
1980b57cec5SDimitry Andric
1990b57cec5SDimitry Andric    allocator_type get_allocator() const noexcept;
2000b57cec5SDimitry Andric
2010b57cec5SDimitry Andric    iterator               begin() noexcept;
2020b57cec5SDimitry Andric    const_iterator         begin()   const noexcept;
2030b57cec5SDimitry Andric    iterator               end() noexcept;
2040b57cec5SDimitry Andric    const_iterator         end()     const noexcept;
2050b57cec5SDimitry Andric
2060b57cec5SDimitry Andric    reverse_iterator       rbegin() noexcept;
2070b57cec5SDimitry Andric    const_reverse_iterator rbegin()  const noexcept;
2080b57cec5SDimitry Andric    reverse_iterator       rend() noexcept;
2090b57cec5SDimitry Andric    const_reverse_iterator rend()    const noexcept;
2100b57cec5SDimitry Andric
2110b57cec5SDimitry Andric    const_iterator         cbegin()  const noexcept;
2120b57cec5SDimitry Andric    const_iterator         cend()    const noexcept;
2130b57cec5SDimitry Andric    const_reverse_iterator crbegin() const noexcept;
2140b57cec5SDimitry Andric    const_reverse_iterator crend()   const noexcept;
2150b57cec5SDimitry Andric
2160b57cec5SDimitry Andric    size_type size() const noexcept;
2170b57cec5SDimitry Andric    size_type max_size() const noexcept;
2180b57cec5SDimitry Andric    size_type capacity() const noexcept;
2190b57cec5SDimitry Andric    bool empty() const noexcept;
2200b57cec5SDimitry Andric    void reserve(size_type n);
2210b57cec5SDimitry Andric    void shrink_to_fit() noexcept;
2220b57cec5SDimitry Andric
2230b57cec5SDimitry Andric    reference       operator[](size_type n);
2240b57cec5SDimitry Andric    const_reference operator[](size_type n) const;
2250b57cec5SDimitry Andric    reference       at(size_type n);
2260b57cec5SDimitry Andric    const_reference at(size_type n) const;
2270b57cec5SDimitry Andric
2280b57cec5SDimitry Andric    reference       front();
2290b57cec5SDimitry Andric    const_reference front() const;
2300b57cec5SDimitry Andric    reference       back();
2310b57cec5SDimitry Andric    const_reference back() const;
2320b57cec5SDimitry Andric
2330b57cec5SDimitry Andric    void push_back(const value_type& x);
2340b57cec5SDimitry Andric    template <class... Args> reference emplace_back(Args&&... args);  // C++14; reference in C++17
23506c3fb27SDimitry Andric    template<container-compatible-range<T> R>
23606c3fb27SDimitry Andric      constexpr void append_range(R&& rg); // C++23
2370b57cec5SDimitry Andric    void pop_back();
2380b57cec5SDimitry Andric
2390b57cec5SDimitry Andric    template <class... Args> iterator emplace(const_iterator position, Args&&... args);  // C++14
2400b57cec5SDimitry Andric    iterator insert(const_iterator position, const value_type& x);
2410b57cec5SDimitry Andric    iterator insert(const_iterator position, size_type n, const value_type& x);
2420b57cec5SDimitry Andric    template <class InputIterator>
2430b57cec5SDimitry Andric        iterator insert(const_iterator position, InputIterator first, InputIterator last);
24406c3fb27SDimitry Andric    template<container-compatible-range<T> R>
24506c3fb27SDimitry Andric      constexpr iterator insert_range(const_iterator position, R&& rg); // C++23
2460b57cec5SDimitry Andric    iterator insert(const_iterator position, initializer_list<value_type> il);
2470b57cec5SDimitry Andric
2480b57cec5SDimitry Andric    iterator erase(const_iterator position);
2490b57cec5SDimitry Andric    iterator erase(const_iterator first, const_iterator last);
2500b57cec5SDimitry Andric
2510b57cec5SDimitry Andric    void clear() noexcept;
2520b57cec5SDimitry Andric
2530b57cec5SDimitry Andric    void resize(size_type sz);
2540b57cec5SDimitry Andric    void resize(size_type sz, value_type x);
2550b57cec5SDimitry Andric
2560b57cec5SDimitry Andric    void swap(vector&)
2570b57cec5SDimitry Andric        noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
2580b57cec5SDimitry Andric                 allocator_traits<allocator_type>::is_always_equal::value);  // C++17
2590b57cec5SDimitry Andric    void flip() noexcept;
2600b57cec5SDimitry Andric
2610b57cec5SDimitry Andric    bool __invariants() const;
2620b57cec5SDimitry Andric};
2630b57cec5SDimitry Andric
2640b57cec5SDimitry Andrictemplate <class InputIterator, class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
2650b57cec5SDimitry Andric   vector(InputIterator, InputIterator, Allocator = Allocator())
266349cc55cSDimitry Andric   -> vector<typename iterator_traits<InputIterator>::value_type, Allocator>; // C++17
2670b57cec5SDimitry Andric
26806c3fb27SDimitry Andrictemplate<ranges::input_range R, class Allocator = allocator<ranges::range_value_t<R>>>
26906c3fb27SDimitry Andric  vector(from_range_t, R&&, Allocator = Allocator())
27006c3fb27SDimitry Andric    -> vector<ranges::range_value_t<R>, Allocator>; // C++23
27106c3fb27SDimitry Andric
2720b57cec5SDimitry Andrictemplate <class Allocator> struct hash<std::vector<bool, Allocator>>;
2730b57cec5SDimitry Andric
27406c3fb27SDimitry Andrictemplate <class T, class Allocator> bool operator==(const vector<T,Allocator>& x, const vector<T,Allocator>& y);   // constexpr since C++20
27506c3fb27SDimitry Andrictemplate <class T, class Allocator> bool operator!=(const vector<T,Allocator>& x, const vector<T,Allocator>& y);   // removed in C++20
27606c3fb27SDimitry Andrictemplate <class T, class Allocator> bool operator< (const vector<T,Allocator>& x, const vector<T,Allocator>& y);   // removed in C++20
27706c3fb27SDimitry Andrictemplate <class T, class Allocator> bool operator> (const vector<T,Allocator>& x, const vector<T,Allocator>& y);   // removed in C++20
27806c3fb27SDimitry Andrictemplate <class T, class Allocator> bool operator>=(const vector<T,Allocator>& x, const vector<T,Allocator>& y);   // removed in C++20
27906c3fb27SDimitry Andrictemplate <class T, class Allocator> bool operator<=(const vector<T,Allocator>& x, const vector<T,Allocator>& y);   // removed in C++20
28006c3fb27SDimitry Andrictemplate <class T, class Allocator> constexpr
28106c3fb27SDimitry Andric  constexpr synth-three-way-result<T> operator<=>(const vector<T, Allocator>& x,
28206c3fb27SDimitry Andric                                                  const vector<T, Allocator>& y);                                  // since C++20
2830b57cec5SDimitry Andric
2840b57cec5SDimitry Andrictemplate <class T, class Allocator>
2850b57cec5SDimitry Andricvoid swap(vector<T,Allocator>& x, vector<T,Allocator>& y)
2860b57cec5SDimitry Andric    noexcept(noexcept(x.swap(y)));
2870b57cec5SDimitry Andric
2880b57cec5SDimitry Andrictemplate <class T, class Allocator, class U>
2895ffd83dbSDimitry Andrictypename vector<T, Allocator>::size_type
29006c3fb27SDimitry Andricerase(vector<T, Allocator>& c, const U& value);       // since C++20
2910b57cec5SDimitry Andrictemplate <class T, class Allocator, class Predicate>
2925ffd83dbSDimitry Andrictypename vector<T, Allocator>::size_type
29306c3fb27SDimitry Andricerase_if(vector<T, Allocator>& c, Predicate pred);    // since C++20
2940b57cec5SDimitry Andric
295bdd1243dSDimitry Andric
296bdd1243dSDimitry Andrictemplate<class T>
297bdd1243dSDimitry Andric inline constexpr bool is-vector-bool-reference = see below;        // exposition only, since C++23
298bdd1243dSDimitry Andric
299bdd1243dSDimitry Andrictemplate<class T, class charT> requires is-vector-bool-reference<T> // Since C++23
300bdd1243dSDimitry Andric struct formatter<T, charT>;
301bdd1243dSDimitry Andric
3020b57cec5SDimitry Andric}  // std
3030b57cec5SDimitry Andric
3040b57cec5SDimitry Andric*/
3050b57cec5SDimitry Andric
3065f757f3fSDimitry Andric// clang-format on
3075f757f3fSDimitry Andric
30881ad6265SDimitry Andric#include <__algorithm/copy.h>
30981ad6265SDimitry Andric#include <__algorithm/equal.h>
31081ad6265SDimitry Andric#include <__algorithm/fill_n.h>
31106c3fb27SDimitry Andric#include <__algorithm/iterator_operations.h>
31281ad6265SDimitry Andric#include <__algorithm/lexicographical_compare.h>
31306c3fb27SDimitry Andric#include <__algorithm/lexicographical_compare_three_way.h>
31481ad6265SDimitry Andric#include <__algorithm/remove.h>
31581ad6265SDimitry Andric#include <__algorithm/remove_if.h>
31681ad6265SDimitry Andric#include <__algorithm/rotate.h>
31781ad6265SDimitry Andric#include <__algorithm/unwrap_iter.h>
318*0fca6ea1SDimitry Andric#include <__assert>
3190b57cec5SDimitry Andric#include <__bit_reference>
320bdd1243dSDimitry Andric#include <__concepts/same_as.h>
32104eeddc0SDimitry Andric#include <__config>
322*0fca6ea1SDimitry Andric#include <__debug_utils/sanitizers.h>
32381ad6265SDimitry Andric#include <__format/enable_insertable.h>
324bdd1243dSDimitry Andric#include <__format/formatter.h>
32506c3fb27SDimitry Andric#include <__format/formatter_bool.h>
32681ad6265SDimitry Andric#include <__functional/hash.h>
32781ad6265SDimitry Andric#include <__functional/unary_function.h>
328*0fca6ea1SDimitry Andric#include <__fwd/vector.h>
32981ad6265SDimitry Andric#include <__iterator/advance.h>
330*0fca6ea1SDimitry Andric#include <__iterator/bounded_iter.h>
33106c3fb27SDimitry Andric#include <__iterator/distance.h>
332349cc55cSDimitry Andric#include <__iterator/iterator_traits.h>
33381ad6265SDimitry Andric#include <__iterator/reverse_iterator.h>
334fe6060f1SDimitry Andric#include <__iterator/wrap_iter.h>
33506c3fb27SDimitry Andric#include <__memory/addressof.h>
33681ad6265SDimitry Andric#include <__memory/allocate_at_least.h>
33706c3fb27SDimitry Andric#include <__memory/allocator_traits.h>
338972a253aSDimitry Andric#include <__memory/pointer_traits.h>
339972a253aSDimitry Andric#include <__memory/swap_allocator.h>
340bdd1243dSDimitry Andric#include <__memory/temp_value.h>
341bdd1243dSDimitry Andric#include <__memory/uninitialized_algorithms.h>
342bdd1243dSDimitry Andric#include <__memory_resource/polymorphic_allocator.h>
34306c3fb27SDimitry Andric#include <__ranges/access.h>
34406c3fb27SDimitry Andric#include <__ranges/concepts.h>
34506c3fb27SDimitry Andric#include <__ranges/container_compatible_range.h>
34606c3fb27SDimitry Andric#include <__ranges/from_range.h>
34706c3fb27SDimitry Andric#include <__ranges/size.h>
348fe6060f1SDimitry Andric#include <__split_buffer>
349bdd1243dSDimitry Andric#include <__type_traits/is_allocator.h>
35006c3fb27SDimitry Andric#include <__type_traits/is_constructible.h>
351*0fca6ea1SDimitry Andric#include <__type_traits/is_nothrow_assignable.h>
352bdd1243dSDimitry Andric#include <__type_traits/noexcept_move_assign_container.h>
35306c3fb27SDimitry Andric#include <__type_traits/type_identity.h>
354bdd1243dSDimitry Andric#include <__utility/exception_guard.h>
355fe6060f1SDimitry Andric#include <__utility/forward.h>
356*0fca6ea1SDimitry Andric#include <__utility/is_pointer_in_range.h>
35781ad6265SDimitry Andric#include <__utility/move.h>
35806c3fb27SDimitry Andric#include <__utility/pair.h>
35981ad6265SDimitry Andric#include <__utility/swap.h>
3600b57cec5SDimitry Andric#include <climits>
361fe6060f1SDimitry Andric#include <cstring>
362fe6060f1SDimitry Andric#include <limits>
3630b57cec5SDimitry Andric#include <stdexcept>
3640b57cec5SDimitry Andric#include <version>
3650b57cec5SDimitry Andric
36681ad6265SDimitry Andric// standard-mandated includes
36781ad6265SDimitry Andric
36881ad6265SDimitry Andric// [iterator.range]
36981ad6265SDimitry Andric#include <__iterator/access.h>
37081ad6265SDimitry Andric#include <__iterator/data.h>
37181ad6265SDimitry Andric#include <__iterator/empty.h>
37281ad6265SDimitry Andric#include <__iterator/reverse_access.h>
37381ad6265SDimitry Andric#include <__iterator/size.h>
37481ad6265SDimitry Andric
37581ad6265SDimitry Andric// [vector.syn]
37681ad6265SDimitry Andric#include <compare>
37781ad6265SDimitry Andric#include <initializer_list>
37881ad6265SDimitry Andric
3790b57cec5SDimitry Andric#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
3800b57cec5SDimitry Andric#  pragma GCC system_header
3810b57cec5SDimitry Andric#endif
3820b57cec5SDimitry Andric
3830b57cec5SDimitry Andric_LIBCPP_PUSH_MACROS
3840b57cec5SDimitry Andric#include <__undef_macros>
3850b57cec5SDimitry Andric
3860b57cec5SDimitry Andric_LIBCPP_BEGIN_NAMESPACE_STD
3870b57cec5SDimitry Andric
3880b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator /* = allocator<_Tp> */>
389cb14a3feSDimitry Andricclass _LIBCPP_TEMPLATE_VIS vector {
3900b57cec5SDimitry Andricprivate:
3910b57cec5SDimitry Andric  typedef allocator<_Tp> __default_allocator_type;
392cb14a3feSDimitry Andric
3930b57cec5SDimitry Andricpublic:
3940b57cec5SDimitry Andric  typedef vector __self;
3950b57cec5SDimitry Andric  typedef _Tp value_type;
3960b57cec5SDimitry Andric  typedef _Allocator allocator_type;
397349cc55cSDimitry Andric  typedef allocator_traits<allocator_type> __alloc_traits;
398349cc55cSDimitry Andric  typedef value_type& reference;
399349cc55cSDimitry Andric  typedef const value_type& const_reference;
4004824e7fdSDimitry Andric  typedef typename __alloc_traits::size_type size_type;
401349cc55cSDimitry Andric  typedef typename __alloc_traits::difference_type difference_type;
402349cc55cSDimitry Andric  typedef typename __alloc_traits::pointer pointer;
403349cc55cSDimitry Andric  typedef typename __alloc_traits::const_pointer const_pointer;
404*0fca6ea1SDimitry Andric#ifdef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_VECTOR
405*0fca6ea1SDimitry Andric  // Users might provide custom allocators, and prior to C++20 we have no existing way to detect whether the allocator's
406*0fca6ea1SDimitry Andric  // pointer type is contiguous (though it has to be by the Standard). Using the wrapper type ensures the iterator is
407*0fca6ea1SDimitry Andric  // considered contiguous.
408*0fca6ea1SDimitry Andric  typedef __bounded_iter<__wrap_iter<pointer>> iterator;
409*0fca6ea1SDimitry Andric  typedef __bounded_iter<__wrap_iter<const_pointer>> const_iterator;
410*0fca6ea1SDimitry Andric#else
4110b57cec5SDimitry Andric  typedef __wrap_iter<pointer> iterator;
4120b57cec5SDimitry Andric  typedef __wrap_iter<const_pointer> const_iterator;
413*0fca6ea1SDimitry Andric#endif
414bdd1243dSDimitry Andric  typedef std::reverse_iterator<iterator> reverse_iterator;
415bdd1243dSDimitry Andric  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
4160b57cec5SDimitry Andric
417*0fca6ea1SDimitry Andric  // A vector containers the following members which may be trivially relocatable:
418*0fca6ea1SDimitry Andric  // - pointer: may be trivially relocatable, so it's checked
419*0fca6ea1SDimitry Andric  // - allocator_type: may be trivially relocatable, so it's checked
420*0fca6ea1SDimitry Andric  // vector doesn't contain any self-references, so it's trivially relocatable if its members are.
421*0fca6ea1SDimitry Andric  using __trivially_relocatable = __conditional_t<
422*0fca6ea1SDimitry Andric      __libcpp_is_trivially_relocatable<pointer>::value && __libcpp_is_trivially_relocatable<allocator_type>::value,
423*0fca6ea1SDimitry Andric      vector,
424*0fca6ea1SDimitry Andric      void>;
4250b57cec5SDimitry Andric
426*0fca6ea1SDimitry Andric  static_assert(__check_valid_allocator<allocator_type>::value, "");
427*0fca6ea1SDimitry Andric  static_assert(is_same<typename allocator_type::value_type, value_type>::value,
428*0fca6ea1SDimitry Andric                "Allocator::value_type must be same type as value_type");
429bdd1243dSDimitry Andric
430cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI vector()
431cb14a3feSDimitry Andric      _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value) {}
432bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI explicit vector(const allocator_type& __a)
4330b57cec5SDimitry Andric#if _LIBCPP_STD_VER <= 14
4340b57cec5SDimitry Andric      _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
4350b57cec5SDimitry Andric#else
4360b57cec5SDimitry Andric      _NOEXCEPT
4370b57cec5SDimitry Andric#endif
438cb14a3feSDimitry Andric      : __end_cap_(nullptr, __a) {
4390b57cec5SDimitry Andric  }
4404824e7fdSDimitry Andric
441*0fca6ea1SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI explicit vector(size_type __n) {
442*0fca6ea1SDimitry Andric    auto __guard = std::__make_exception_guard(__destroy_vector(*this));
443*0fca6ea1SDimitry Andric    if (__n > 0) {
444*0fca6ea1SDimitry Andric      __vallocate(__n);
445*0fca6ea1SDimitry Andric      __construct_at_end(__n);
446*0fca6ea1SDimitry Andric    }
447*0fca6ea1SDimitry Andric    __guard.__complete();
448*0fca6ea1SDimitry Andric  }
449*0fca6ea1SDimitry Andric
450*0fca6ea1SDimitry Andric#if _LIBCPP_STD_VER >= 14
451*0fca6ea1SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI explicit vector(size_type __n, const allocator_type& __a)
452*0fca6ea1SDimitry Andric      : __end_cap_(nullptr, __a) {
453*0fca6ea1SDimitry Andric    auto __guard = std::__make_exception_guard(__destroy_vector(*this));
454*0fca6ea1SDimitry Andric    if (__n > 0) {
455*0fca6ea1SDimitry Andric      __vallocate(__n);
456*0fca6ea1SDimitry Andric      __construct_at_end(__n);
457*0fca6ea1SDimitry Andric    }
458*0fca6ea1SDimitry Andric    __guard.__complete();
459*0fca6ea1SDimitry Andric  }
460*0fca6ea1SDimitry Andric#endif
461*0fca6ea1SDimitry Andric
462*0fca6ea1SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI vector(size_type __n, const value_type& __x) {
463*0fca6ea1SDimitry Andric    auto __guard = std::__make_exception_guard(__destroy_vector(*this));
464*0fca6ea1SDimitry Andric    if (__n > 0) {
465*0fca6ea1SDimitry Andric      __vallocate(__n);
466*0fca6ea1SDimitry Andric      __construct_at_end(__n, __x);
467*0fca6ea1SDimitry Andric    }
468*0fca6ea1SDimitry Andric    __guard.__complete();
469*0fca6ea1SDimitry Andric  }
470*0fca6ea1SDimitry Andric
471*0fca6ea1SDimitry Andric  template <__enable_if_t<__is_allocator<_Allocator>::value, int> = 0>
472bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
4734824e7fdSDimitry Andric  vector(size_type __n, const value_type& __x, const allocator_type& __a)
474cb14a3feSDimitry Andric      : __end_cap_(nullptr, __a) {
475cb14a3feSDimitry Andric    if (__n > 0) {
4764824e7fdSDimitry Andric      __vallocate(__n);
4774824e7fdSDimitry Andric      __construct_at_end(__n, __x);
4784824e7fdSDimitry Andric    }
4794824e7fdSDimitry Andric  }
4804824e7fdSDimitry Andric
481bdd1243dSDimitry Andric  template <class _InputIterator,
48206c3fb27SDimitry Andric            __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value &&
483bdd1243dSDimitry Andric                              is_constructible<value_type, typename iterator_traits<_InputIterator>::reference>::value,
484bdd1243dSDimitry Andric                          int> = 0>
485bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI vector(_InputIterator __first, _InputIterator __last);
486bdd1243dSDimitry Andric  template <class _InputIterator,
48706c3fb27SDimitry Andric            __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value &&
488bdd1243dSDimitry Andric                              is_constructible<value_type, typename iterator_traits<_InputIterator>::reference>::value,
489bdd1243dSDimitry Andric                          int> = 0>
490bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
491bdd1243dSDimitry Andric  vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a);
492bdd1243dSDimitry Andric
493bdd1243dSDimitry Andric  template <
494bdd1243dSDimitry Andric      class _ForwardIterator,
49506c3fb27SDimitry Andric      __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value &&
496bdd1243dSDimitry Andric                        is_constructible<value_type, typename iterator_traits<_ForwardIterator>::reference>::value,
497bdd1243dSDimitry Andric                    int> = 0>
498bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI vector(_ForwardIterator __first, _ForwardIterator __last);
499bdd1243dSDimitry Andric
500cb14a3feSDimitry Andric  template <
501cb14a3feSDimitry Andric      class _ForwardIterator,
50206c3fb27SDimitry Andric      __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value &&
503bdd1243dSDimitry Andric                        is_constructible<value_type, typename iterator_traits<_ForwardIterator>::reference>::value,
504bdd1243dSDimitry Andric                    int> = 0>
505bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
506bdd1243dSDimitry Andric  vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a);
5070b57cec5SDimitry Andric
50806c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 23
50906c3fb27SDimitry Andric  template <_ContainerCompatibleRange<_Tp> _Range>
510cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr vector(
511cb14a3feSDimitry Andric      from_range_t, _Range&& __range, const allocator_type& __alloc = allocator_type())
512cb14a3feSDimitry Andric      : __end_cap_(nullptr, __alloc) {
51306c3fb27SDimitry Andric    if constexpr (ranges::forward_range<_Range> || ranges::sized_range<_Range>) {
51406c3fb27SDimitry Andric      auto __n = static_cast<size_type>(ranges::distance(__range));
51506c3fb27SDimitry Andric      __init_with_size(ranges::begin(__range), ranges::end(__range), __n);
51606c3fb27SDimitry Andric
51706c3fb27SDimitry Andric    } else {
51806c3fb27SDimitry Andric      __init_with_sentinel(ranges::begin(__range), ranges::end(__range));
51906c3fb27SDimitry Andric    }
52006c3fb27SDimitry Andric  }
52106c3fb27SDimitry Andric#endif
52206c3fb27SDimitry Andric
52350d7464cSDimitry Andricprivate:
52450d7464cSDimitry Andric  class __destroy_vector {
52550d7464cSDimitry Andric  public:
52606c3fb27SDimitry Andric    _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI __destroy_vector(vector& __vec) : __vec_(__vec) {}
527349cc55cSDimitry Andric
528bdd1243dSDimitry Andric    _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void operator()() {
52950d7464cSDimitry Andric      if (__vec_.__begin_ != nullptr) {
53050d7464cSDimitry Andric        __vec_.__clear();
53106c3fb27SDimitry Andric        __vec_.__annotate_delete();
53250d7464cSDimitry Andric        __alloc_traits::deallocate(__vec_.__alloc(), __vec_.__begin_, __vec_.capacity());
533349cc55cSDimitry Andric      }
5340b57cec5SDimitry Andric    }
5350b57cec5SDimitry Andric
53650d7464cSDimitry Andric  private:
53750d7464cSDimitry Andric    vector& __vec_;
53850d7464cSDimitry Andric  };
53950d7464cSDimitry Andric
54050d7464cSDimitry Andricpublic:
541bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI ~vector() { __destroy_vector (*this)(); }
54250d7464cSDimitry Andric
543bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI vector(const vector& __x);
544bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
545cb14a3feSDimitry Andric  vector(const vector& __x, const __type_identity_t<allocator_type>& __a);
546cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI vector& operator=(const vector& __x);
5470b57cec5SDimitry Andric
5480b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
549cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI vector(initializer_list<value_type> __il);
5500b57cec5SDimitry Andric
551bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
5520b57cec5SDimitry Andric  vector(initializer_list<value_type> __il, const allocator_type& __a);
5530b57cec5SDimitry Andric
554cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI vector& operator=(initializer_list<value_type> __il) {
555cb14a3feSDimitry Andric    assign(__il.begin(), __il.end());
556cb14a3feSDimitry Andric    return *this;
557cb14a3feSDimitry Andric  }
55881ad6265SDimitry Andric#endif // !_LIBCPP_CXX03_LANG
55981ad6265SDimitry Andric
560cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI vector(vector&& __x)
56106c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 17
56281ad6265SDimitry Andric      noexcept;
5630b57cec5SDimitry Andric#else
5640b57cec5SDimitry Andric      _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
5650b57cec5SDimitry Andric#endif
5660b57cec5SDimitry Andric
567bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
56881ad6265SDimitry Andric  vector(vector&& __x, const __type_identity_t<allocator_type>& __a);
569cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI vector& operator=(vector&& __x)
570*0fca6ea1SDimitry Andric      _NOEXCEPT_(__noexcept_move_assign_container<_Allocator, __alloc_traits>::value);
5710b57cec5SDimitry Andric
572bdd1243dSDimitry Andric  template <class _InputIterator,
57306c3fb27SDimitry Andric            __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value &&
574bdd1243dSDimitry Andric                              is_constructible<value_type, typename iterator_traits<_InputIterator>::reference>::value,
575bdd1243dSDimitry Andric                          int> = 0>
576bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void assign(_InputIterator __first, _InputIterator __last);
577bdd1243dSDimitry Andric  template <
578bdd1243dSDimitry Andric      class _ForwardIterator,
57906c3fb27SDimitry Andric      __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value &&
580bdd1243dSDimitry Andric                        is_constructible<value_type, typename iterator_traits<_ForwardIterator>::reference>::value,
581bdd1243dSDimitry Andric                    int> = 0>
582bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void assign(_ForwardIterator __first, _ForwardIterator __last);
5830b57cec5SDimitry Andric
58406c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 23
58506c3fb27SDimitry Andric  template <_ContainerCompatibleRange<_Tp> _Range>
586cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr void assign_range(_Range&& __range) {
58706c3fb27SDimitry Andric    if constexpr (ranges::forward_range<_Range> || ranges::sized_range<_Range>) {
58806c3fb27SDimitry Andric      auto __n = static_cast<size_type>(ranges::distance(__range));
58906c3fb27SDimitry Andric      __assign_with_size(ranges::begin(__range), ranges::end(__range), __n);
59006c3fb27SDimitry Andric
59106c3fb27SDimitry Andric    } else {
59206c3fb27SDimitry Andric      __assign_with_sentinel(ranges::begin(__range), ranges::end(__range));
59306c3fb27SDimitry Andric    }
59406c3fb27SDimitry Andric  }
59506c3fb27SDimitry Andric#endif
59606c3fb27SDimitry Andric
597bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void assign(size_type __n, const_reference __u);
5980b57cec5SDimitry Andric
5990b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
600cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void assign(initializer_list<value_type> __il) {
601cb14a3feSDimitry Andric    assign(__il.begin(), __il.end());
602cb14a3feSDimitry Andric  }
6030b57cec5SDimitry Andric#endif
6040b57cec5SDimitry Andric
605cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI allocator_type get_allocator() const _NOEXCEPT {
606cb14a3feSDimitry Andric    return this->__alloc();
607cb14a3feSDimitry Andric  }
6080b57cec5SDimitry Andric
609bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator begin() _NOEXCEPT;
610bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_iterator begin() const _NOEXCEPT;
611bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator end() _NOEXCEPT;
612bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_iterator end() const _NOEXCEPT;
6130b57cec5SDimitry Andric
614cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI reverse_iterator rbegin() _NOEXCEPT {
615cb14a3feSDimitry Andric    return reverse_iterator(end());
616cb14a3feSDimitry Andric  }
617cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator rbegin() const _NOEXCEPT {
618cb14a3feSDimitry Andric    return const_reverse_iterator(end());
619cb14a3feSDimitry Andric  }
620cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI reverse_iterator rend() _NOEXCEPT {
621cb14a3feSDimitry Andric    return reverse_iterator(begin());
622cb14a3feSDimitry Andric  }
623cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator rend() const _NOEXCEPT {
624cb14a3feSDimitry Andric    return const_reverse_iterator(begin());
625cb14a3feSDimitry Andric  }
6260b57cec5SDimitry Andric
627cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_iterator cbegin() const _NOEXCEPT { return begin(); }
628cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_iterator cend() const _NOEXCEPT { return end(); }
629cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator crbegin() const _NOEXCEPT {
630cb14a3feSDimitry Andric    return rbegin();
631cb14a3feSDimitry Andric  }
632cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator crend() const _NOEXCEPT { return rend(); }
6330b57cec5SDimitry Andric
634cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type size() const _NOEXCEPT {
635cb14a3feSDimitry Andric    return static_cast<size_type>(this->__end_ - this->__begin_);
636cb14a3feSDimitry Andric  }
637cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type capacity() const _NOEXCEPT {
638cb14a3feSDimitry Andric    return static_cast<size_type>(__end_cap() - this->__begin_);
639cb14a3feSDimitry Andric  }
640*0fca6ea1SDimitry Andric  _LIBCPP_NODISCARD _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool empty() const _NOEXCEPT {
641cb14a3feSDimitry Andric    return this->__begin_ == this->__end_;
642cb14a3feSDimitry Andric  }
643bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT;
644bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void reserve(size_type __n);
645bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void shrink_to_fit() _NOEXCEPT;
6460b57cec5SDimitry Andric
647bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI reference operator[](size_type __n) _NOEXCEPT;
648bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_reference operator[](size_type __n) const _NOEXCEPT;
649bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI reference at(size_type __n);
650bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_reference at(size_type __n) const;
6510b57cec5SDimitry Andric
652cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI reference front() _NOEXCEPT {
65306c3fb27SDimitry Andric    _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "front() called on an empty vector");
6540b57cec5SDimitry Andric    return *this->__begin_;
6550b57cec5SDimitry Andric  }
656cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_reference front() const _NOEXCEPT {
65706c3fb27SDimitry Andric    _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "front() called on an empty vector");
6580b57cec5SDimitry Andric    return *this->__begin_;
6590b57cec5SDimitry Andric  }
660cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI reference back() _NOEXCEPT {
66106c3fb27SDimitry Andric    _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "back() called on an empty vector");
6620b57cec5SDimitry Andric    return *(this->__end_ - 1);
6630b57cec5SDimitry Andric  }
664cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_reference back() const _NOEXCEPT {
66506c3fb27SDimitry Andric    _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "back() called on an empty vector");
6660b57cec5SDimitry Andric    return *(this->__end_ - 1);
6670b57cec5SDimitry Andric  }
6680b57cec5SDimitry Andric
669cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI value_type* data() _NOEXCEPT {
670cb14a3feSDimitry Andric    return std::__to_address(this->__begin_);
671cb14a3feSDimitry Andric  }
67261cfbce3SDimitry Andric
673cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const value_type* data() const _NOEXCEPT {
674cb14a3feSDimitry Andric    return std::__to_address(this->__begin_);
675cb14a3feSDimitry Andric  }
6760b57cec5SDimitry Andric
677bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void push_back(const_reference __x);
6780b57cec5SDimitry Andric
679bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void push_back(value_type&& __x);
6800b57cec5SDimitry Andric
6810b57cec5SDimitry Andric  template <class... _Args>
682bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
68306c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 17
684cb14a3feSDimitry Andric  reference
685cb14a3feSDimitry Andric  emplace_back(_Args&&... __args);
6860b57cec5SDimitry Andric#else
687cb14a3feSDimitry Andric  void
688cb14a3feSDimitry Andric  emplace_back(_Args&&... __args);
6890b57cec5SDimitry Andric#endif
6900b57cec5SDimitry Andric
69106c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 23
69206c3fb27SDimitry Andric  template <_ContainerCompatibleRange<_Tp> _Range>
693cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr void append_range(_Range&& __range) {
69406c3fb27SDimitry Andric    insert_range(end(), std::forward<_Range>(__range));
69506c3fb27SDimitry Andric  }
69606c3fb27SDimitry Andric#endif
69706c3fb27SDimitry Andric
698cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void pop_back();
6990b57cec5SDimitry Andric
700bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __position, const_reference __x);
7010b57cec5SDimitry Andric
702bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __position, value_type&& __x);
7030b57cec5SDimitry Andric  template <class... _Args>
704bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator emplace(const_iterator __position, _Args&&... __args);
7050b57cec5SDimitry Andric
706cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator
707cb14a3feSDimitry Andric  insert(const_iterator __position, size_type __n, const_reference __x);
708bdd1243dSDimitry Andric
709bdd1243dSDimitry Andric  template <class _InputIterator,
71006c3fb27SDimitry Andric            __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value &&
711bdd1243dSDimitry Andric                              is_constructible< value_type, typename iterator_traits<_InputIterator>::reference>::value,
712bdd1243dSDimitry Andric                          int> = 0>
713bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator
7140b57cec5SDimitry Andric  insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
715bdd1243dSDimitry Andric
71606c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 23
71706c3fb27SDimitry Andric  template <_ContainerCompatibleRange<_Tp> _Range>
718cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr iterator insert_range(const_iterator __position, _Range&& __range) {
71906c3fb27SDimitry Andric    if constexpr (ranges::forward_range<_Range> || ranges::sized_range<_Range>) {
72006c3fb27SDimitry Andric      auto __n = static_cast<size_type>(ranges::distance(__range));
72106c3fb27SDimitry Andric      return __insert_with_size(__position, ranges::begin(__range), ranges::end(__range), __n);
72206c3fb27SDimitry Andric
72306c3fb27SDimitry Andric    } else {
72406c3fb27SDimitry Andric      return __insert_with_sentinel(__position, ranges::begin(__range), ranges::end(__range));
72506c3fb27SDimitry Andric    }
72606c3fb27SDimitry Andric  }
72706c3fb27SDimitry Andric#endif
72806c3fb27SDimitry Andric
729bdd1243dSDimitry Andric  template <
730bdd1243dSDimitry Andric      class _ForwardIterator,
73106c3fb27SDimitry Andric      __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value &&
732bdd1243dSDimitry Andric                        is_constructible< value_type, typename iterator_traits<_ForwardIterator>::reference>::value,
733bdd1243dSDimitry Andric                    int> = 0>
734bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator
7350b57cec5SDimitry Andric  insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
7360b57cec5SDimitry Andric
7370b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
738cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator
739cb14a3feSDimitry Andric  insert(const_iterator __position, initializer_list<value_type> __il) {
740cb14a3feSDimitry Andric    return insert(__position, __il.begin(), __il.end());
741cb14a3feSDimitry Andric  }
7420b57cec5SDimitry Andric#endif
7430b57cec5SDimitry Andric
744bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __position);
745bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __first, const_iterator __last);
7460b57cec5SDimitry Andric
747cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void clear() _NOEXCEPT {
7480b57cec5SDimitry Andric    size_type __old_size = size();
749349cc55cSDimitry Andric    __clear();
7500b57cec5SDimitry Andric    __annotate_shrink(__old_size);
7510b57cec5SDimitry Andric  }
7520b57cec5SDimitry Andric
753bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void resize(size_type __sz);
754bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void resize(size_type __sz, const_reference __x);
7550b57cec5SDimitry Andric
756bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void swap(vector&)
7570b57cec5SDimitry Andric#if _LIBCPP_STD_VER >= 14
7580b57cec5SDimitry Andric      _NOEXCEPT;
7590b57cec5SDimitry Andric#else
760*0fca6ea1SDimitry Andric      _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || __is_nothrow_swappable_v<allocator_type>);
7610b57cec5SDimitry Andric#endif
7620b57cec5SDimitry Andric
763bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool __invariants() const;
7640b57cec5SDimitry Andric
7650b57cec5SDimitry Andricprivate:
766d56accc7SDimitry Andric  pointer __begin_ = nullptr;
767d56accc7SDimitry Andric  pointer __end_   = nullptr;
768d56accc7SDimitry Andric  __compressed_pair<pointer, allocator_type> __end_cap_ =
769d56accc7SDimitry Andric      __compressed_pair<pointer, allocator_type>(nullptr, __default_init_tag());
770d56accc7SDimitry Andric
77181ad6265SDimitry Andric  //  Allocate space for __n objects
77281ad6265SDimitry Andric  //  throws length_error if __n > max_size()
77381ad6265SDimitry Andric  //  throws (probably bad_alloc) if memory run out
77481ad6265SDimitry Andric  //  Precondition:  __begin_ == __end_ == __end_cap() == 0
77581ad6265SDimitry Andric  //  Precondition:  __n > 0
77681ad6265SDimitry Andric  //  Postcondition:  capacity() >= __n
77781ad6265SDimitry Andric  //  Postcondition:  size() == 0
778bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __vallocate(size_type __n) {
77981ad6265SDimitry Andric    if (__n > max_size())
78081ad6265SDimitry Andric      __throw_length_error();
78181ad6265SDimitry Andric    auto __allocation = std::__allocate_at_least(__alloc(), __n);
78281ad6265SDimitry Andric    __begin_          = __allocation.ptr;
78381ad6265SDimitry Andric    __end_            = __allocation.ptr;
78481ad6265SDimitry Andric    __end_cap()       = __begin_ + __allocation.count;
78581ad6265SDimitry Andric    __annotate_new(0);
78681ad6265SDimitry Andric  }
78781ad6265SDimitry Andric
788bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __vdeallocate() _NOEXCEPT;
789bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type __recommend(size_type __new_size) const;
790bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __construct_at_end(size_type __n);
791cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __construct_at_end(size_type __n, const_reference __x);
792bdd1243dSDimitry Andric
79306c3fb27SDimitry Andric  template <class _InputIterator, class _Sentinel>
794cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
795cb14a3feSDimitry Andric  __init_with_size(_InputIterator __first, _Sentinel __last, size_type __n) {
79606c3fb27SDimitry Andric    auto __guard = std::__make_exception_guard(__destroy_vector(*this));
79706c3fb27SDimitry Andric
79806c3fb27SDimitry Andric    if (__n > 0) {
79906c3fb27SDimitry Andric      __vallocate(__n);
80006c3fb27SDimitry Andric      __construct_at_end(__first, __last, __n);
80106c3fb27SDimitry Andric    }
80206c3fb27SDimitry Andric
80306c3fb27SDimitry Andric    __guard.__complete();
80406c3fb27SDimitry Andric  }
80506c3fb27SDimitry Andric
80606c3fb27SDimitry Andric  template <class _InputIterator, class _Sentinel>
807cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
808cb14a3feSDimitry Andric  __init_with_sentinel(_InputIterator __first, _Sentinel __last) {
80906c3fb27SDimitry Andric    auto __guard = std::__make_exception_guard(__destroy_vector(*this));
81006c3fb27SDimitry Andric
81106c3fb27SDimitry Andric    for (; __first != __last; ++__first)
81206c3fb27SDimitry Andric      emplace_back(*__first);
81306c3fb27SDimitry Andric
81406c3fb27SDimitry Andric    __guard.__complete();
81506c3fb27SDimitry Andric  }
81606c3fb27SDimitry Andric
81706c3fb27SDimitry Andric  template <class _Iterator, class _Sentinel>
818cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __assign_with_sentinel(_Iterator __first, _Sentinel __last);
81906c3fb27SDimitry Andric
82006c3fb27SDimitry Andric  template <class _ForwardIterator, class _Sentinel>
821cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
822cb14a3feSDimitry Andric  __assign_with_size(_ForwardIterator __first, _Sentinel __last, difference_type __n);
82306c3fb27SDimitry Andric
82406c3fb27SDimitry Andric  template <class _InputIterator, class _Sentinel>
825cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator
826cb14a3feSDimitry Andric  __insert_with_sentinel(const_iterator __position, _InputIterator __first, _Sentinel __last);
82706c3fb27SDimitry Andric
82806c3fb27SDimitry Andric  template <class _Iterator, class _Sentinel>
829cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator
830cb14a3feSDimitry Andric  __insert_with_size(const_iterator __position, _Iterator __first, _Sentinel __last, difference_type __n);
83106c3fb27SDimitry Andric
83206c3fb27SDimitry Andric  template <class _InputIterator, class _Sentinel>
833cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
834cb14a3feSDimitry Andric  __construct_at_end(_InputIterator __first, _Sentinel __last, size_type __n);
835bdd1243dSDimitry Andric
836bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __append(size_type __n);
837bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __append(size_type __n, const_reference __x);
838*0fca6ea1SDimitry Andric
839cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator __make_iter(pointer __p) _NOEXCEPT {
840*0fca6ea1SDimitry Andric#ifdef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_VECTOR
841*0fca6ea1SDimitry Andric    // Bound the iterator according to the capacity, rather than the size.
842*0fca6ea1SDimitry Andric    //
843*0fca6ea1SDimitry Andric    // Vector guarantees that iterators stay valid as long as no reallocation occurs even if new elements are inserted
844*0fca6ea1SDimitry Andric    // into the container; for these cases, we need to make sure that the newly-inserted elements can be accessed
845*0fca6ea1SDimitry Andric    // through the bounded iterator without failing checks. The downside is that the bounded iterator won't catch
846*0fca6ea1SDimitry Andric    // access that is logically out-of-bounds, i.e., goes beyond the size, but is still within the capacity. With the
847*0fca6ea1SDimitry Andric    // current implementation, there is no connection between a bounded iterator and its associated container, so we
848*0fca6ea1SDimitry Andric    // don't have a way to update existing valid iterators when the container is resized and thus have to go with
849*0fca6ea1SDimitry Andric    // a laxer approach.
850*0fca6ea1SDimitry Andric    return std::__make_bounded_iter(
851*0fca6ea1SDimitry Andric        std::__wrap_iter<pointer>(__p),
852*0fca6ea1SDimitry Andric        std::__wrap_iter<pointer>(this->__begin_),
853*0fca6ea1SDimitry Andric        std::__wrap_iter<pointer>(this->__end_cap()));
854*0fca6ea1SDimitry Andric#else
855cb14a3feSDimitry Andric    return iterator(__p);
856*0fca6ea1SDimitry Andric#endif // _LIBCPP_ABI_BOUNDED_ITERATORS_IN_VECTOR
857cb14a3feSDimitry Andric  }
858*0fca6ea1SDimitry Andric
859cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_iterator __make_iter(const_pointer __p) const _NOEXCEPT {
860*0fca6ea1SDimitry Andric#ifdef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_VECTOR
861*0fca6ea1SDimitry Andric    // Bound the iterator according to the capacity, rather than the size.
862*0fca6ea1SDimitry Andric    return std::__make_bounded_iter(
863*0fca6ea1SDimitry Andric        std::__wrap_iter<const_pointer>(__p),
864*0fca6ea1SDimitry Andric        std::__wrap_iter<const_pointer>(this->__begin_),
865*0fca6ea1SDimitry Andric        std::__wrap_iter<const_pointer>(this->__end_cap()));
866*0fca6ea1SDimitry Andric#else
867cb14a3feSDimitry Andric    return const_iterator(__p);
868*0fca6ea1SDimitry Andric#endif // _LIBCPP_ABI_BOUNDED_ITERATORS_IN_VECTOR
869cb14a3feSDimitry Andric  }
870*0fca6ea1SDimitry Andric
871cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
872cb14a3feSDimitry Andric  __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v);
873cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI pointer
874cb14a3feSDimitry Andric  __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p);
875cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
876cb14a3feSDimitry Andric  __move_range(pointer __from_s, pointer __from_e, pointer __to);
877bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __move_assign(vector& __c, true_type)
8780b57cec5SDimitry Andric      _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
879bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __move_assign(vector& __c, false_type)
8800b57cec5SDimitry Andric      _NOEXCEPT_(__alloc_traits::is_always_equal::value);
881cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __destruct_at_end(pointer __new_last) _NOEXCEPT {
8820b57cec5SDimitry Andric    size_type __old_size = size();
883349cc55cSDimitry Andric    __base_destruct_at_end(__new_last);
8840b57cec5SDimitry Andric    __annotate_shrink(__old_size);
8850b57cec5SDimitry Andric  }
8860b57cec5SDimitry Andric
8870b57cec5SDimitry Andric  template <class _Up>
888cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI inline pointer __push_back_slow_path(_Up&& __x);
8890b57cec5SDimitry Andric
8900b57cec5SDimitry Andric  template <class... _Args>
891cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI inline pointer __emplace_back_slow_path(_Args&&... __args);
8920b57cec5SDimitry Andric
8930b57cec5SDimitry Andric  // The following functions are no-ops outside of AddressSanitizer mode.
89406c3fb27SDimitry Andric  // We call annotations for every allocator, unless explicitly disabled.
89506c3fb27SDimitry Andric  //
89606c3fb27SDimitry Andric  // To disable annotations for a particular allocator, change value of
89706c3fb27SDimitry Andric  // __asan_annotate_container_with_allocator to false.
89806c3fb27SDimitry Andric  // For more details, see the "Using libc++" documentation page or
89906c3fb27SDimitry Andric  // the documentation for __sanitizer_annotate_contiguous_container.
9005f757f3fSDimitry Andric
901*0fca6ea1SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
902*0fca6ea1SDimitry Andric  __annotate_contiguous_container(const void* __old_mid, const void* __new_mid) const {
903*0fca6ea1SDimitry Andric    std::__annotate_contiguous_container<_Allocator>(data(), data() + capacity(), __old_mid, __new_mid);
9045f757f3fSDimitry Andric  }
9055f757f3fSDimitry Andric
906cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __annotate_new(size_type __current_size) const _NOEXCEPT {
907cb14a3feSDimitry Andric    (void)__current_size;
908cb14a3feSDimitry Andric#ifndef _LIBCPP_HAS_NO_ASAN
9097a6dacacSDimitry Andric    __annotate_contiguous_container(data() + capacity(), data() + __current_size);
910cb14a3feSDimitry Andric#endif
9110b57cec5SDimitry Andric  }
9120b57cec5SDimitry Andric
913cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __annotate_delete() const _NOEXCEPT {
914cb14a3feSDimitry Andric#ifndef _LIBCPP_HAS_NO_ASAN
9157a6dacacSDimitry Andric    __annotate_contiguous_container(data() + size(), data() + capacity());
916cb14a3feSDimitry Andric#endif
9170b57cec5SDimitry Andric  }
9180b57cec5SDimitry Andric
919cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __annotate_increase(size_type __n) const _NOEXCEPT {
920cb14a3feSDimitry Andric    (void)__n;
921cb14a3feSDimitry Andric#ifndef _LIBCPP_HAS_NO_ASAN
9227a6dacacSDimitry Andric    __annotate_contiguous_container(data() + size(), data() + size() + __n);
923cb14a3feSDimitry Andric#endif
9240b57cec5SDimitry Andric  }
9250b57cec5SDimitry Andric
926cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __annotate_shrink(size_type __old_size) const _NOEXCEPT {
927cb14a3feSDimitry Andric    (void)__old_size;
928cb14a3feSDimitry Andric#ifndef _LIBCPP_HAS_NO_ASAN
9297a6dacacSDimitry Andric    __annotate_contiguous_container(data() + __old_size, data() + size());
930cb14a3feSDimitry Andric#endif
9310b57cec5SDimitry Andric  }
9320b57cec5SDimitry Andric
933e40139ffSDimitry Andric  struct _ConstructTransaction {
934cb14a3feSDimitry Andric    _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI explicit _ConstructTransaction(vector& __v, size_type __n)
935e40139ffSDimitry Andric        : __v_(__v), __pos_(__v.__end_), __new_end_(__v.__end_ + __n) {
936e40139ffSDimitry Andric#ifndef _LIBCPP_HAS_NO_ASAN
937e40139ffSDimitry Andric      __v_.__annotate_increase(__n);
938e40139ffSDimitry Andric#endif
939e40139ffSDimitry Andric    }
940*0fca6ea1SDimitry Andric
941bdd1243dSDimitry Andric    _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI ~_ConstructTransaction() {
942e40139ffSDimitry Andric      __v_.__end_ = __pos_;
943e40139ffSDimitry Andric#ifndef _LIBCPP_HAS_NO_ASAN
944e40139ffSDimitry Andric      if (__pos_ != __new_end_) {
945e40139ffSDimitry Andric        __v_.__annotate_shrink(__new_end_ - __v_.__begin_);
946e40139ffSDimitry Andric      }
947e40139ffSDimitry Andric#endif
948e40139ffSDimitry Andric    }
949e40139ffSDimitry Andric
950e40139ffSDimitry Andric    vector& __v_;
951e40139ffSDimitry Andric    pointer __pos_;
952e40139ffSDimitry Andric    const_pointer const __new_end_;
953e40139ffSDimitry Andric
954e40139ffSDimitry Andric    _ConstructTransaction(_ConstructTransaction const&)            = delete;
955e40139ffSDimitry Andric    _ConstructTransaction& operator=(_ConstructTransaction const&) = delete;
956e40139ffSDimitry Andric  };
957e40139ffSDimitry Andric
958e40139ffSDimitry Andric  template <class... _Args>
959cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __construct_one_at_end(_Args&&... __args) {
960e40139ffSDimitry Andric    _ConstructTransaction __tx(*this, 1);
961cb14a3feSDimitry Andric    __alloc_traits::construct(this->__alloc(), std::__to_address(__tx.__pos_), std::forward<_Args>(__args)...);
962e40139ffSDimitry Andric    ++__tx.__pos_;
963e40139ffSDimitry Andric  }
964349cc55cSDimitry Andric
965cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI allocator_type& __alloc() _NOEXCEPT {
966cb14a3feSDimitry Andric    return this->__end_cap_.second();
967cb14a3feSDimitry Andric  }
968cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const allocator_type& __alloc() const _NOEXCEPT {
969cb14a3feSDimitry Andric    return this->__end_cap_.second();
970cb14a3feSDimitry Andric  }
971cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI pointer& __end_cap() _NOEXCEPT {
972cb14a3feSDimitry Andric    return this->__end_cap_.first();
973cb14a3feSDimitry Andric  }
974cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const pointer& __end_cap() const _NOEXCEPT {
975cb14a3feSDimitry Andric    return this->__end_cap_.first();
976cb14a3feSDimitry Andric  }
977349cc55cSDimitry Andric
978cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __clear() _NOEXCEPT {
979cb14a3feSDimitry Andric    __base_destruct_at_end(this->__begin_);
980cb14a3feSDimitry Andric  }
981349cc55cSDimitry Andric
982cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __base_destruct_at_end(pointer __new_last) _NOEXCEPT {
983349cc55cSDimitry Andric    pointer __soon_to_be_end = this->__end_;
984349cc55cSDimitry Andric    while (__new_last != __soon_to_be_end)
985bdd1243dSDimitry Andric      __alloc_traits::destroy(__alloc(), std::__to_address(--__soon_to_be_end));
986349cc55cSDimitry Andric    this->__end_ = __new_last;
987349cc55cSDimitry Andric  }
988349cc55cSDimitry Andric
989cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __copy_assign_alloc(const vector& __c) {
990cb14a3feSDimitry Andric    __copy_assign_alloc(__c, integral_constant<bool, __alloc_traits::propagate_on_container_copy_assignment::value>());
991349cc55cSDimitry Andric  }
992349cc55cSDimitry Andric
993cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(vector& __c)
994cb14a3feSDimitry Andric      _NOEXCEPT_(!__alloc_traits::propagate_on_container_move_assignment::value ||
995cb14a3feSDimitry Andric                 is_nothrow_move_assignable<allocator_type>::value) {
996cb14a3feSDimitry Andric    __move_assign_alloc(__c, integral_constant<bool, __alloc_traits::propagate_on_container_move_assignment::value>());
997349cc55cSDimitry Andric  }
998349cc55cSDimitry Andric
999cb14a3feSDimitry Andric  _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI void __throw_length_error() const { std::__throw_length_error("vector"); }
1000cb14a3feSDimitry Andric
1001cb14a3feSDimitry Andric  _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI void __throw_out_of_range() const { std::__throw_out_of_range("vector"); }
1002cb14a3feSDimitry Andric
1003cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __copy_assign_alloc(const vector& __c, true_type) {
1004cb14a3feSDimitry Andric    if (__alloc() != __c.__alloc()) {
1005349cc55cSDimitry Andric      __clear();
100606c3fb27SDimitry Andric      __annotate_delete();
1007349cc55cSDimitry Andric      __alloc_traits::deallocate(__alloc(), this->__begin_, capacity());
1008349cc55cSDimitry Andric      this->__begin_ = this->__end_ = __end_cap() = nullptr;
1009349cc55cSDimitry Andric    }
1010349cc55cSDimitry Andric    __alloc() = __c.__alloc();
1011349cc55cSDimitry Andric  }
1012349cc55cSDimitry Andric
1013cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __copy_assign_alloc(const vector&, false_type) {}
1014349cc55cSDimitry Andric
1015cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(vector& __c, true_type)
1016cb14a3feSDimitry Andric      _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value) {
1017bdd1243dSDimitry Andric    __alloc() = std::move(__c.__alloc());
1018349cc55cSDimitry Andric  }
1019349cc55cSDimitry Andric
1020cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(vector&, false_type) _NOEXCEPT {}
10210b57cec5SDimitry Andric};
10220b57cec5SDimitry Andric
1023349cc55cSDimitry Andric#if _LIBCPP_STD_VER >= 17
10240b57cec5SDimitry Andrictemplate <class _InputIterator,
1025fe6060f1SDimitry Andric          class _Alloc = allocator<__iter_value_type<_InputIterator>>,
102606c3fb27SDimitry Andric          class        = enable_if_t<__has_input_iterator_category<_InputIterator>::value>,
1027cb14a3feSDimitry Andric          class        = enable_if_t<__is_allocator<_Alloc>::value> >
1028cb14a3feSDimitry Andricvector(_InputIterator, _InputIterator) -> vector<__iter_value_type<_InputIterator>, _Alloc>;
10290b57cec5SDimitry Andric
10300b57cec5SDimitry Andrictemplate <class _InputIterator,
10310b57cec5SDimitry Andric          class _Alloc,
103206c3fb27SDimitry Andric          class = enable_if_t<__has_input_iterator_category<_InputIterator>::value>,
1033cb14a3feSDimitry Andric          class = enable_if_t<__is_allocator<_Alloc>::value> >
1034cb14a3feSDimitry Andricvector(_InputIterator, _InputIterator, _Alloc) -> vector<__iter_value_type<_InputIterator>, _Alloc>;
10350b57cec5SDimitry Andric#endif
10360b57cec5SDimitry Andric
103706c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 23
103806c3fb27SDimitry Andrictemplate <ranges::input_range _Range,
103906c3fb27SDimitry Andric          class _Alloc = allocator<ranges::range_value_t<_Range>>,
1040cb14a3feSDimitry Andric          class        = enable_if_t<__is_allocator<_Alloc>::value> >
1041cb14a3feSDimitry Andricvector(from_range_t, _Range&&, _Alloc = _Alloc()) -> vector<ranges::range_value_t<_Range>, _Alloc>;
104206c3fb27SDimitry Andric#endif
104306c3fb27SDimitry Andric
1044*0fca6ea1SDimitry Andric// __swap_out_circular_buffer relocates the objects in [__begin_, __end_) into the front of __v and swaps the buffers of
1045*0fca6ea1SDimitry Andric// *this and __v. It is assumed that __v provides space for exactly (__end_ - __begin_) objects in the front. This
1046*0fca6ea1SDimitry Andric// function has a strong exception guarantee.
10470b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
1048cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 void
1049cb14a3feSDimitry Andricvector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v) {
10500b57cec5SDimitry Andric  __annotate_delete();
1051*0fca6ea1SDimitry Andric  auto __new_begin = __v.__begin_ - (__end_ - __begin_);
1052*0fca6ea1SDimitry Andric  std::__uninitialized_allocator_relocate(
1053*0fca6ea1SDimitry Andric      __alloc(), std::__to_address(__begin_), std::__to_address(__end_), std::__to_address(__new_begin));
1054*0fca6ea1SDimitry Andric  __v.__begin_ = __new_begin;
1055*0fca6ea1SDimitry Andric  __end_       = __begin_; // All the objects have been destroyed by relocating them.
1056bdd1243dSDimitry Andric  std::swap(this->__begin_, __v.__begin_);
1057bdd1243dSDimitry Andric  std::swap(this->__end_, __v.__end_);
1058bdd1243dSDimitry Andric  std::swap(this->__end_cap(), __v.__end_cap());
10590b57cec5SDimitry Andric  __v.__first_ = __v.__begin_;
10600b57cec5SDimitry Andric  __annotate_new(size());
10610b57cec5SDimitry Andric}
10620b57cec5SDimitry Andric
1063*0fca6ea1SDimitry Andric// __swap_out_circular_buffer relocates the objects in [__begin_, __p) into the front of __v, the objects in
1064*0fca6ea1SDimitry Andric// [__p, __end_) into the back of __v and swaps the buffers of *this and __v. It is assumed that __v provides space for
1065*0fca6ea1SDimitry Andric// exactly (__p - __begin_) objects in the front and space for at least (__end_ - __p) objects in the back. This
1066*0fca6ea1SDimitry Andric// function has a strong exception guarantee if __begin_ == __p || __end_ == __p.
10670b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
1068cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<_Tp, _Allocator>::pointer
1069cb14a3feSDimitry Andricvector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p) {
10700b57cec5SDimitry Andric  __annotate_delete();
1071*0fca6ea1SDimitry Andric  pointer __ret = __v.__begin_;
1072*0fca6ea1SDimitry Andric
1073*0fca6ea1SDimitry Andric  // Relocate [__p, __end_) first to avoid having a hole in [__begin_, __end_)
1074*0fca6ea1SDimitry Andric  // in case something in [__begin_, __p) throws.
1075*0fca6ea1SDimitry Andric  std::__uninitialized_allocator_relocate(
1076*0fca6ea1SDimitry Andric      __alloc(), std::__to_address(__p), std::__to_address(__end_), std::__to_address(__v.__end_));
1077*0fca6ea1SDimitry Andric  __v.__end_ += (__end_ - __p);
1078*0fca6ea1SDimitry Andric  __end_           = __p; // The objects in [__p, __end_) have been destroyed by relocating them.
1079*0fca6ea1SDimitry Andric  auto __new_begin = __v.__begin_ - (__p - __begin_);
1080*0fca6ea1SDimitry Andric
1081*0fca6ea1SDimitry Andric  std::__uninitialized_allocator_relocate(
1082*0fca6ea1SDimitry Andric      __alloc(), std::__to_address(__begin_), std::__to_address(__p), std::__to_address(__new_begin));
1083*0fca6ea1SDimitry Andric  __v.__begin_ = __new_begin;
1084*0fca6ea1SDimitry Andric  __end_       = __begin_; // All the objects have been destroyed by relocating them.
1085*0fca6ea1SDimitry Andric
1086bdd1243dSDimitry Andric  std::swap(this->__begin_, __v.__begin_);
1087bdd1243dSDimitry Andric  std::swap(this->__end_, __v.__end_);
1088bdd1243dSDimitry Andric  std::swap(this->__end_cap(), __v.__end_cap());
10890b57cec5SDimitry Andric  __v.__first_ = __v.__begin_;
10900b57cec5SDimitry Andric  __annotate_new(size());
1091*0fca6ea1SDimitry Andric  return __ret;
10920b57cec5SDimitry Andric}
10930b57cec5SDimitry Andric
10940b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
1095cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<_Tp, _Allocator>::__vdeallocate() _NOEXCEPT {
1096cb14a3feSDimitry Andric  if (this->__begin_ != nullptr) {
10970b57cec5SDimitry Andric    clear();
109806c3fb27SDimitry Andric    __annotate_delete();
10990b57cec5SDimitry Andric    __alloc_traits::deallocate(this->__alloc(), this->__begin_, capacity());
11000b57cec5SDimitry Andric    this->__begin_ = this->__end_ = this->__end_cap() = nullptr;
11010b57cec5SDimitry Andric  }
11020b57cec5SDimitry Andric}
11030b57cec5SDimitry Andric
11040b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
1105cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<_Tp, _Allocator>::size_type
1106cb14a3feSDimitry Andricvector<_Tp, _Allocator>::max_size() const _NOEXCEPT {
1107cb14a3feSDimitry Andric  return std::min<size_type>(__alloc_traits::max_size(this->__alloc()), numeric_limits<difference_type>::max());
11080b57cec5SDimitry Andric}
11090b57cec5SDimitry Andric
11100b57cec5SDimitry Andric//  Precondition:  __new_size > capacity()
11110b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
1112cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI typename vector<_Tp, _Allocator>::size_type
1113cb14a3feSDimitry Andricvector<_Tp, _Allocator>::__recommend(size_type __new_size) const {
11140b57cec5SDimitry Andric  const size_type __ms = max_size();
11150b57cec5SDimitry Andric  if (__new_size > __ms)
11160b57cec5SDimitry Andric    this->__throw_length_error();
11170b57cec5SDimitry Andric  const size_type __cap = capacity();
11180b57cec5SDimitry Andric  if (__cap >= __ms / 2)
11190b57cec5SDimitry Andric    return __ms;
1120bdd1243dSDimitry Andric  return std::max<size_type>(2 * __cap, __new_size);
11210b57cec5SDimitry Andric}
11220b57cec5SDimitry Andric
11230b57cec5SDimitry Andric//  Default constructs __n objects starting at __end_
11240b57cec5SDimitry Andric//  throws if construction throws
11250b57cec5SDimitry Andric//  Precondition:  __n > 0
11260b57cec5SDimitry Andric//  Precondition:  size() + __n <= capacity()
11270b57cec5SDimitry Andric//  Postcondition:  size() == size() + __n
11280b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
1129cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<_Tp, _Allocator>::__construct_at_end(size_type __n) {
1130e40139ffSDimitry Andric  _ConstructTransaction __tx(*this, __n);
11315ffd83dbSDimitry Andric  const_pointer __new_end = __tx.__new_end_;
1132349cc55cSDimitry Andric  for (pointer __pos = __tx.__pos_; __pos != __new_end; __tx.__pos_ = ++__pos) {
1133bdd1243dSDimitry Andric    __alloc_traits::construct(this->__alloc(), std::__to_address(__pos));
1134e40139ffSDimitry Andric  }
11350b57cec5SDimitry Andric}
11360b57cec5SDimitry Andric
11370b57cec5SDimitry Andric//  Copy constructs __n objects starting at __end_ from __x
11380b57cec5SDimitry Andric//  throws if construction throws
11390b57cec5SDimitry Andric//  Precondition:  __n > 0
11400b57cec5SDimitry Andric//  Precondition:  size() + __n <= capacity()
11410b57cec5SDimitry Andric//  Postcondition:  size() == old size() + __n
11420b57cec5SDimitry Andric//  Postcondition:  [i] == __x for all i in [size() - __n, __n)
11430b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
1144cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 inline void
1145cb14a3feSDimitry Andricvector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x) {
1146e40139ffSDimitry Andric  _ConstructTransaction __tx(*this, __n);
11475ffd83dbSDimitry Andric  const_pointer __new_end = __tx.__new_end_;
1148349cc55cSDimitry Andric  for (pointer __pos = __tx.__pos_; __pos != __new_end; __tx.__pos_ = ++__pos) {
1149bdd1243dSDimitry Andric    __alloc_traits::construct(this->__alloc(), std::__to_address(__pos), __x);
1150e40139ffSDimitry Andric  }
11510b57cec5SDimitry Andric}
11520b57cec5SDimitry Andric
11530b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
115406c3fb27SDimitry Andrictemplate <class _InputIterator, class _Sentinel>
1155bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 void
115606c3fb27SDimitry Andricvector<_Tp, _Allocator>::__construct_at_end(_InputIterator __first, _Sentinel __last, size_type __n) {
1157e40139ffSDimitry Andric  _ConstructTransaction __tx(*this, __n);
1158972a253aSDimitry Andric  __tx.__pos_ = std::__uninitialized_allocator_copy(__alloc(), __first, __last, __tx.__pos_);
11590b57cec5SDimitry Andric}
11600b57cec5SDimitry Andric
11610b57cec5SDimitry Andric//  Default constructs __n objects starting at __end_
11620b57cec5SDimitry Andric//  throws if construction throws
11630b57cec5SDimitry Andric//  Postcondition:  size() == size() + __n
11640b57cec5SDimitry Andric//  Exception safety: strong.
11650b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
1166cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<_Tp, _Allocator>::__append(size_type __n) {
11670b57cec5SDimitry Andric  if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
11680b57cec5SDimitry Andric    this->__construct_at_end(__n);
1169cb14a3feSDimitry Andric  else {
11700b57cec5SDimitry Andric    allocator_type& __a = this->__alloc();
11710b57cec5SDimitry Andric    __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
11720b57cec5SDimitry Andric    __v.__construct_at_end(__n);
11730b57cec5SDimitry Andric    __swap_out_circular_buffer(__v);
11740b57cec5SDimitry Andric  }
11750b57cec5SDimitry Andric}
11760b57cec5SDimitry Andric
11770b57cec5SDimitry Andric//  Default constructs __n objects starting at __end_
11780b57cec5SDimitry Andric//  throws if construction throws
11790b57cec5SDimitry Andric//  Postcondition:  size() == size() + __n
11800b57cec5SDimitry Andric//  Exception safety: strong.
11810b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
1182cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<_Tp, _Allocator>::__append(size_type __n, const_reference __x) {
11830b57cec5SDimitry Andric  if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
11840b57cec5SDimitry Andric    this->__construct_at_end(__n, __x);
1185cb14a3feSDimitry Andric  else {
11860b57cec5SDimitry Andric    allocator_type& __a = this->__alloc();
11870b57cec5SDimitry Andric    __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
11880b57cec5SDimitry Andric    __v.__construct_at_end(__n, __x);
11890b57cec5SDimitry Andric    __swap_out_circular_buffer(__v);
11900b57cec5SDimitry Andric  }
11910b57cec5SDimitry Andric}
11920b57cec5SDimitry Andric
11930b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
1194cb14a3feSDimitry Andrictemplate <class _InputIterator,
1195cb14a3feSDimitry Andric          __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value &&
1196bdd1243dSDimitry Andric                            is_constructible<_Tp, typename iterator_traits<_InputIterator>::reference>::value,
1197bdd1243dSDimitry Andric                        int> >
1198cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last) {
119906c3fb27SDimitry Andric  __init_with_sentinel(__first, __last);
12000b57cec5SDimitry Andric}
12010b57cec5SDimitry Andric
12020b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
1203cb14a3feSDimitry Andrictemplate <class _InputIterator,
1204cb14a3feSDimitry Andric          __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value &&
1205bdd1243dSDimitry Andric                            is_constructible<_Tp, typename iterator_traits<_InputIterator>::reference>::value,
1206bdd1243dSDimitry Andric                        int> >
1207bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20
1208bdd1243dSDimitry Andricvector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a)
1209cb14a3feSDimitry Andric    : __end_cap_(nullptr, __a) {
121006c3fb27SDimitry Andric  __init_with_sentinel(__first, __last);
12110b57cec5SDimitry Andric}
12120b57cec5SDimitry Andric
12130b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
1214cb14a3feSDimitry Andrictemplate <class _ForwardIterator,
1215cb14a3feSDimitry Andric          __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value &&
1216bdd1243dSDimitry Andric                            is_constructible<_Tp, typename iterator_traits<_ForwardIterator>::reference>::value,
1217bdd1243dSDimitry Andric                        int> >
1218cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last) {
121950d7464cSDimitry Andric  size_type __n = static_cast<size_type>(std::distance(__first, __last));
122006c3fb27SDimitry Andric  __init_with_size(__first, __last, __n);
12210b57cec5SDimitry Andric}
12220b57cec5SDimitry Andric
12230b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
1224cb14a3feSDimitry Andrictemplate <class _ForwardIterator,
1225cb14a3feSDimitry Andric          __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value &&
1226bdd1243dSDimitry Andric                            is_constructible<_Tp, typename iterator_traits<_ForwardIterator>::reference>::value,
1227bdd1243dSDimitry Andric                        int> >
1228bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20
1229bdd1243dSDimitry Andricvector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a)
1230cb14a3feSDimitry Andric    : __end_cap_(nullptr, __a) {
123150d7464cSDimitry Andric  size_type __n = static_cast<size_type>(std::distance(__first, __last));
123206c3fb27SDimitry Andric  __init_with_size(__first, __last, __n);
12330b57cec5SDimitry Andric}
12340b57cec5SDimitry Andric
12350b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
1236cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 vector<_Tp, _Allocator>::vector(const vector& __x)
1237cb14a3feSDimitry Andric    : __end_cap_(nullptr, __alloc_traits::select_on_container_copy_construction(__x.__alloc())) {
123806c3fb27SDimitry Andric  __init_with_size(__x.__begin_, __x.__end_, __x.size());
12390b57cec5SDimitry Andric}
12400b57cec5SDimitry Andric
12410b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
1242bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20
124381ad6265SDimitry Andricvector<_Tp, _Allocator>::vector(const vector& __x, const __type_identity_t<allocator_type>& __a)
1244cb14a3feSDimitry Andric    : __end_cap_(nullptr, __a) {
124506c3fb27SDimitry Andric  __init_with_size(__x.__begin_, __x.__end_, __x.size());
12460b57cec5SDimitry Andric}
12470b57cec5SDimitry Andric
12480b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
1249cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI vector<_Tp, _Allocator>::vector(vector&& __x)
125006c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 17
125181ad6265SDimitry Andric    noexcept
12520b57cec5SDimitry Andric#else
12530b57cec5SDimitry Andric    _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
12540b57cec5SDimitry Andric#endif
1255cb14a3feSDimitry Andric    : __end_cap_(nullptr, std::move(__x.__alloc())) {
12560b57cec5SDimitry Andric  this->__begin_    = __x.__begin_;
12570b57cec5SDimitry Andric  this->__end_      = __x.__end_;
12580b57cec5SDimitry Andric  this->__end_cap() = __x.__end_cap();
12590b57cec5SDimitry Andric  __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
12600b57cec5SDimitry Andric}
12610b57cec5SDimitry Andric
12620b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
1263cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI
126481ad6265SDimitry Andricvector<_Tp, _Allocator>::vector(vector&& __x, const __type_identity_t<allocator_type>& __a)
1265cb14a3feSDimitry Andric    : __end_cap_(nullptr, __a) {
1266cb14a3feSDimitry Andric  if (__a == __x.__alloc()) {
12670b57cec5SDimitry Andric    this->__begin_    = __x.__begin_;
12680b57cec5SDimitry Andric    this->__end_      = __x.__end_;
12690b57cec5SDimitry Andric    this->__end_cap() = __x.__end_cap();
12700b57cec5SDimitry Andric    __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
1271cb14a3feSDimitry Andric  } else {
12720b57cec5SDimitry Andric    typedef move_iterator<iterator> _Ip;
1273bdd1243dSDimitry Andric    auto __guard = std::__make_exception_guard(__destroy_vector(*this));
12740b57cec5SDimitry Andric    assign(_Ip(__x.begin()), _Ip(__x.end()));
127550d7464cSDimitry Andric    __guard.__complete();
12760b57cec5SDimitry Andric  }
12770b57cec5SDimitry Andric}
12780b57cec5SDimitry Andric
127981ad6265SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
128081ad6265SDimitry Andric
12810b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
1282cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI
1283cb14a3feSDimitry Andricvector<_Tp, _Allocator>::vector(initializer_list<value_type> __il) {
1284bdd1243dSDimitry Andric  auto __guard = std::__make_exception_guard(__destroy_vector(*this));
1285cb14a3feSDimitry Andric  if (__il.size() > 0) {
12860b57cec5SDimitry Andric    __vallocate(__il.size());
12870b57cec5SDimitry Andric    __construct_at_end(__il.begin(), __il.end(), __il.size());
12880b57cec5SDimitry Andric  }
128950d7464cSDimitry Andric  __guard.__complete();
12900b57cec5SDimitry Andric}
12910b57cec5SDimitry Andric
12920b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
1293cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI
12940b57cec5SDimitry Andricvector<_Tp, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
1295cb14a3feSDimitry Andric    : __end_cap_(nullptr, __a) {
1296bdd1243dSDimitry Andric  auto __guard = std::__make_exception_guard(__destroy_vector(*this));
1297cb14a3feSDimitry Andric  if (__il.size() > 0) {
12980b57cec5SDimitry Andric    __vallocate(__il.size());
12990b57cec5SDimitry Andric    __construct_at_end(__il.begin(), __il.end(), __il.size());
13000b57cec5SDimitry Andric  }
130150d7464cSDimitry Andric  __guard.__complete();
13020b57cec5SDimitry Andric}
13030b57cec5SDimitry Andric
130481ad6265SDimitry Andric#endif // _LIBCPP_CXX03_LANG
130581ad6265SDimitry Andric
13060b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
1307cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI vector<_Tp, _Allocator>&
13080b57cec5SDimitry Andricvector<_Tp, _Allocator>::operator=(vector&& __x)
1309*0fca6ea1SDimitry Andric    _NOEXCEPT_(__noexcept_move_assign_container<_Allocator, __alloc_traits>::value) {
1310cb14a3feSDimitry Andric  __move_assign(__x, integral_constant<bool, __alloc_traits::propagate_on_container_move_assignment::value>());
13110b57cec5SDimitry Andric  return *this;
13120b57cec5SDimitry Andric}
13130b57cec5SDimitry Andric
13140b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
1315cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<_Tp, _Allocator>::__move_assign(vector& __c, false_type)
1316cb14a3feSDimitry Andric    _NOEXCEPT_(__alloc_traits::is_always_equal::value) {
1317cb14a3feSDimitry Andric  if (__alloc() != __c.__alloc()) {
13180b57cec5SDimitry Andric    typedef move_iterator<iterator> _Ip;
13190b57cec5SDimitry Andric    assign(_Ip(__c.begin()), _Ip(__c.end()));
1320cb14a3feSDimitry Andric  } else
13210b57cec5SDimitry Andric    __move_assign(__c, true_type());
13220b57cec5SDimitry Andric}
13230b57cec5SDimitry Andric
13240b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
1325cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<_Tp, _Allocator>::__move_assign(vector& __c, true_type)
1326cb14a3feSDimitry Andric    _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value) {
13270b57cec5SDimitry Andric  __vdeallocate();
1328349cc55cSDimitry Andric  __move_assign_alloc(__c); // this can throw
13290b57cec5SDimitry Andric  this->__begin_    = __c.__begin_;
13300b57cec5SDimitry Andric  this->__end_      = __c.__end_;
13310b57cec5SDimitry Andric  this->__end_cap() = __c.__end_cap();
13320b57cec5SDimitry Andric  __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
13330b57cec5SDimitry Andric}
13340b57cec5SDimitry Andric
13350b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
1336cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI vector<_Tp, _Allocator>&
1337cb14a3feSDimitry Andricvector<_Tp, _Allocator>::operator=(const vector& __x) {
1338cb14a3feSDimitry Andric  if (this != std::addressof(__x)) {
1339349cc55cSDimitry Andric    __copy_assign_alloc(__x);
13400b57cec5SDimitry Andric    assign(__x.__begin_, __x.__end_);
13410b57cec5SDimitry Andric  }
13420b57cec5SDimitry Andric  return *this;
13430b57cec5SDimitry Andric}
13440b57cec5SDimitry Andric
13450b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
1346cb14a3feSDimitry Andrictemplate <class _InputIterator,
1347cb14a3feSDimitry Andric          __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value &&
1348bdd1243dSDimitry Andric                            is_constructible<_Tp, typename iterator_traits<_InputIterator>::reference>::value,
1349bdd1243dSDimitry Andric                        int> >
1350cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<_Tp, _Allocator>::assign(_InputIterator __first, _InputIterator __last) {
135106c3fb27SDimitry Andric  __assign_with_sentinel(__first, __last);
135206c3fb27SDimitry Andric}
135306c3fb27SDimitry Andric
135406c3fb27SDimitry Andrictemplate <class _Tp, class _Allocator>
135506c3fb27SDimitry Andrictemplate <class _Iterator, class _Sentinel>
1356cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
1357cb14a3feSDimitry Andricvector<_Tp, _Allocator>::__assign_with_sentinel(_Iterator __first, _Sentinel __last) {
13580b57cec5SDimitry Andric  clear();
13590b57cec5SDimitry Andric  for (; __first != __last; ++__first)
136081ad6265SDimitry Andric    emplace_back(*__first);
13610b57cec5SDimitry Andric}
13620b57cec5SDimitry Andric
13630b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
1364cb14a3feSDimitry Andrictemplate <class _ForwardIterator,
1365cb14a3feSDimitry Andric          __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value &&
1366bdd1243dSDimitry Andric                            is_constructible<_Tp, typename iterator_traits<_ForwardIterator>::reference>::value,
1367bdd1243dSDimitry Andric                        int> >
1368cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<_Tp, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last) {
136906c3fb27SDimitry Andric  __assign_with_size(__first, __last, std::distance(__first, __last));
137006c3fb27SDimitry Andric}
137106c3fb27SDimitry Andric
137206c3fb27SDimitry Andrictemplate <class _Tp, class _Allocator>
137306c3fb27SDimitry Andrictemplate <class _ForwardIterator, class _Sentinel>
1374cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
1375cb14a3feSDimitry Andricvector<_Tp, _Allocator>::__assign_with_size(_ForwardIterator __first, _Sentinel __last, difference_type __n) {
137606c3fb27SDimitry Andric  size_type __new_size = static_cast<size_type>(__n);
1377cb14a3feSDimitry Andric  if (__new_size <= capacity()) {
1378cb14a3feSDimitry Andric    if (__new_size > size()) {
137906c3fb27SDimitry Andric      _ForwardIterator __mid = std::next(__first, size());
138006c3fb27SDimitry Andric      std::copy(__first, __mid, this->__begin_);
13810b57cec5SDimitry Andric      __construct_at_end(__mid, __last, __new_size - size());
1382cb14a3feSDimitry Andric    } else {
138306c3fb27SDimitry Andric      pointer __m = std::__copy<_ClassicAlgPolicy>(__first, __last, this->__begin_).second;
13840b57cec5SDimitry Andric      this->__destruct_at_end(__m);
13850b57cec5SDimitry Andric    }
1386cb14a3feSDimitry Andric  } else {
13870b57cec5SDimitry Andric    __vdeallocate();
13880b57cec5SDimitry Andric    __vallocate(__recommend(__new_size));
13890b57cec5SDimitry Andric    __construct_at_end(__first, __last, __new_size);
13900b57cec5SDimitry Andric  }
13910b57cec5SDimitry Andric}
13920b57cec5SDimitry Andric
13930b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
1394cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<_Tp, _Allocator>::assign(size_type __n, const_reference __u) {
1395cb14a3feSDimitry Andric  if (__n <= capacity()) {
13960b57cec5SDimitry Andric    size_type __s = size();
1397bdd1243dSDimitry Andric    std::fill_n(this->__begin_, std::min(__n, __s), __u);
13980b57cec5SDimitry Andric    if (__n > __s)
13990b57cec5SDimitry Andric      __construct_at_end(__n - __s, __u);
14000b57cec5SDimitry Andric    else
14010b57cec5SDimitry Andric      this->__destruct_at_end(this->__begin_ + __n);
1402cb14a3feSDimitry Andric  } else {
14030b57cec5SDimitry Andric    __vdeallocate();
14040b57cec5SDimitry Andric    __vallocate(__recommend(static_cast<size_type>(__n)));
14050b57cec5SDimitry Andric    __construct_at_end(__n, __u);
14060b57cec5SDimitry Andric  }
14070b57cec5SDimitry Andric}
14080b57cec5SDimitry Andric
14090b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
1410cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI typename vector<_Tp, _Allocator>::iterator
1411cb14a3feSDimitry Andricvector<_Tp, _Allocator>::begin() _NOEXCEPT {
1412bdd1243dSDimitry Andric  return __make_iter(this->__begin_);
14130b57cec5SDimitry Andric}
14140b57cec5SDimitry Andric
14150b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
1416cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI typename vector<_Tp, _Allocator>::const_iterator
1417cb14a3feSDimitry Andricvector<_Tp, _Allocator>::begin() const _NOEXCEPT {
1418bdd1243dSDimitry Andric  return __make_iter(this->__begin_);
14190b57cec5SDimitry Andric}
14200b57cec5SDimitry Andric
14210b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
1422cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI typename vector<_Tp, _Allocator>::iterator
1423cb14a3feSDimitry Andricvector<_Tp, _Allocator>::end() _NOEXCEPT {
1424bdd1243dSDimitry Andric  return __make_iter(this->__end_);
14250b57cec5SDimitry Andric}
14260b57cec5SDimitry Andric
14270b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
1428cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI typename vector<_Tp, _Allocator>::const_iterator
1429cb14a3feSDimitry Andricvector<_Tp, _Allocator>::end() const _NOEXCEPT {
1430bdd1243dSDimitry Andric  return __make_iter(this->__end_);
14310b57cec5SDimitry Andric}
14320b57cec5SDimitry Andric
14330b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
1434cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI typename vector<_Tp, _Allocator>::reference
1435cb14a3feSDimitry Andricvector<_Tp, _Allocator>::operator[](size_type __n) _NOEXCEPT {
143606c3fb27SDimitry Andric  _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__n < size(), "vector[] index out of bounds");
14370b57cec5SDimitry Andric  return this->__begin_[__n];
14380b57cec5SDimitry Andric}
14390b57cec5SDimitry Andric
14400b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
1441cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI typename vector<_Tp, _Allocator>::const_reference
1442cb14a3feSDimitry Andricvector<_Tp, _Allocator>::operator[](size_type __n) const _NOEXCEPT {
144306c3fb27SDimitry Andric  _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__n < size(), "vector[] index out of bounds");
14440b57cec5SDimitry Andric  return this->__begin_[__n];
14450b57cec5SDimitry Andric}
14460b57cec5SDimitry Andric
14470b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
1448cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<_Tp, _Allocator>::reference vector<_Tp, _Allocator>::at(size_type __n) {
14490b57cec5SDimitry Andric  if (__n >= size())
14500b57cec5SDimitry Andric    this->__throw_out_of_range();
14510b57cec5SDimitry Andric  return this->__begin_[__n];
14520b57cec5SDimitry Andric}
14530b57cec5SDimitry Andric
14540b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
1455cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<_Tp, _Allocator>::const_reference
1456cb14a3feSDimitry Andricvector<_Tp, _Allocator>::at(size_type __n) const {
14570b57cec5SDimitry Andric  if (__n >= size())
14580b57cec5SDimitry Andric    this->__throw_out_of_range();
14590b57cec5SDimitry Andric  return this->__begin_[__n];
14600b57cec5SDimitry Andric}
14610b57cec5SDimitry Andric
14620b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
1463cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<_Tp, _Allocator>::reserve(size_type __n) {
1464cb14a3feSDimitry Andric  if (__n > capacity()) {
1465349cc55cSDimitry Andric    if (__n > max_size())
1466349cc55cSDimitry Andric      this->__throw_length_error();
14670b57cec5SDimitry Andric    allocator_type& __a = this->__alloc();
14680b57cec5SDimitry Andric    __split_buffer<value_type, allocator_type&> __v(__n, size(), __a);
14690b57cec5SDimitry Andric    __swap_out_circular_buffer(__v);
14700b57cec5SDimitry Andric  }
14710b57cec5SDimitry Andric}
14720b57cec5SDimitry Andric
14730b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
1474cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT {
1475cb14a3feSDimitry Andric  if (capacity() > size()) {
147606c3fb27SDimitry Andric#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1477cb14a3feSDimitry Andric    try {
147806c3fb27SDimitry Andric#endif // _LIBCPP_HAS_NO_EXCEPTIONS
14790b57cec5SDimitry Andric      allocator_type& __a = this->__alloc();
14800b57cec5SDimitry Andric      __split_buffer<value_type, allocator_type&> __v(size(), size(), __a);
1481*0fca6ea1SDimitry Andric      // The Standard mandates shrink_to_fit() does not increase the capacity.
1482*0fca6ea1SDimitry Andric      // With equal capacity keep the existing buffer. This avoids extra work
1483*0fca6ea1SDimitry Andric      // due to swapping the elements.
1484*0fca6ea1SDimitry Andric      if (__v.capacity() < capacity())
14850b57cec5SDimitry Andric        __swap_out_circular_buffer(__v);
148606c3fb27SDimitry Andric#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1487cb14a3feSDimitry Andric    } catch (...) {
14880b57cec5SDimitry Andric    }
148906c3fb27SDimitry Andric#endif // _LIBCPP_HAS_NO_EXCEPTIONS
14900b57cec5SDimitry Andric  }
14910b57cec5SDimitry Andric}
14920b57cec5SDimitry Andric
14930b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
14940b57cec5SDimitry Andrictemplate <class _Up>
1495cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<_Tp, _Allocator>::pointer
1496cb14a3feSDimitry Andricvector<_Tp, _Allocator>::__push_back_slow_path(_Up&& __x) {
14970b57cec5SDimitry Andric  allocator_type& __a = this->__alloc();
14980b57cec5SDimitry Andric  __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1499bdd1243dSDimitry Andric  // __v.push_back(std::forward<_Up>(__x));
1500bdd1243dSDimitry Andric  __alloc_traits::construct(__a, std::__to_address(__v.__end_), std::forward<_Up>(__x));
15010b57cec5SDimitry Andric  __v.__end_++;
15020b57cec5SDimitry Andric  __swap_out_circular_buffer(__v);
15035f757f3fSDimitry Andric  return this->__end_;
15040b57cec5SDimitry Andric}
15050b57cec5SDimitry Andric
15060b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
1507cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI void
1508cb14a3feSDimitry Andricvector<_Tp, _Allocator>::push_back(const_reference __x) {
15095f757f3fSDimitry Andric  pointer __end = this->__end_;
15105f757f3fSDimitry Andric  if (__end < this->__end_cap()) {
1511e40139ffSDimitry Andric    __construct_one_at_end(__x);
15125f757f3fSDimitry Andric    ++__end;
15135f757f3fSDimitry Andric  } else {
15145f757f3fSDimitry Andric    __end = __push_back_slow_path(__x);
15150b57cec5SDimitry Andric  }
15165f757f3fSDimitry Andric  this->__end_ = __end;
15170b57cec5SDimitry Andric}
15180b57cec5SDimitry Andric
15190b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
1520cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI void vector<_Tp, _Allocator>::push_back(value_type&& __x) {
15215f757f3fSDimitry Andric  pointer __end = this->__end_;
15225f757f3fSDimitry Andric  if (__end < this->__end_cap()) {
1523bdd1243dSDimitry Andric    __construct_one_at_end(std::move(__x));
15245f757f3fSDimitry Andric    ++__end;
15255f757f3fSDimitry Andric  } else {
15265f757f3fSDimitry Andric    __end = __push_back_slow_path(std::move(__x));
15270b57cec5SDimitry Andric  }
15285f757f3fSDimitry Andric  this->__end_ = __end;
15290b57cec5SDimitry Andric}
15300b57cec5SDimitry Andric
15310b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
15320b57cec5SDimitry Andrictemplate <class... _Args>
1533cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<_Tp, _Allocator>::pointer
1534cb14a3feSDimitry Andricvector<_Tp, _Allocator>::__emplace_back_slow_path(_Args&&... __args) {
15350b57cec5SDimitry Andric  allocator_type& __a = this->__alloc();
15360b57cec5SDimitry Andric  __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1537bdd1243dSDimitry Andric  //    __v.emplace_back(std::forward<_Args>(__args)...);
1538bdd1243dSDimitry Andric  __alloc_traits::construct(__a, std::__to_address(__v.__end_), std::forward<_Args>(__args)...);
15390b57cec5SDimitry Andric  __v.__end_++;
15400b57cec5SDimitry Andric  __swap_out_circular_buffer(__v);
15415f757f3fSDimitry Andric  return this->__end_;
15420b57cec5SDimitry Andric}
15430b57cec5SDimitry Andric
15440b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
15450b57cec5SDimitry Andrictemplate <class... _Args>
1546cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 inline
154706c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 17
15480b57cec5SDimitry Andric    typename vector<_Tp, _Allocator>::reference
15490b57cec5SDimitry Andric#else
15500b57cec5SDimitry Andric    void
15510b57cec5SDimitry Andric#endif
1552cb14a3feSDimitry Andric    vector<_Tp, _Allocator>::emplace_back(_Args&&... __args) {
15535f757f3fSDimitry Andric  pointer __end = this->__end_;
15545f757f3fSDimitry Andric  if (__end < this->__end_cap()) {
1555bdd1243dSDimitry Andric    __construct_one_at_end(std::forward<_Args>(__args)...);
15565f757f3fSDimitry Andric    ++__end;
15575f757f3fSDimitry Andric  } else {
15585f757f3fSDimitry Andric    __end = __emplace_back_slow_path(std::forward<_Args>(__args)...);
15590b57cec5SDimitry Andric  }
15605f757f3fSDimitry Andric  this->__end_ = __end;
156106c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 17
15625f757f3fSDimitry Andric  return *(__end - 1);
15630b57cec5SDimitry Andric#endif
15640b57cec5SDimitry Andric}
15650b57cec5SDimitry Andric
15660b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
1567cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 inline void vector<_Tp, _Allocator>::pop_back() {
156806c3fb27SDimitry Andric  _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "vector::pop_back called on an empty vector");
15690b57cec5SDimitry Andric  this->__destruct_at_end(this->__end_ - 1);
15700b57cec5SDimitry Andric}
15710b57cec5SDimitry Andric
15720b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
1573cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI typename vector<_Tp, _Allocator>::iterator
1574cb14a3feSDimitry Andricvector<_Tp, _Allocator>::erase(const_iterator __position) {
1575cb14a3feSDimitry Andric  _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
1576cb14a3feSDimitry Andric      __position != end(), "vector::erase(iterator) called with a non-dereferenceable iterator");
15770b57cec5SDimitry Andric  difference_type __ps = __position - cbegin();
15780b57cec5SDimitry Andric  pointer __p          = this->__begin_ + __ps;
1579bdd1243dSDimitry Andric  this->__destruct_at_end(std::move(__p + 1, this->__end_, __p));
1580bdd1243dSDimitry Andric  return __make_iter(__p);
15810b57cec5SDimitry Andric}
15820b57cec5SDimitry Andric
15830b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
1584cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<_Tp, _Allocator>::iterator
1585cb14a3feSDimitry Andricvector<_Tp, _Allocator>::erase(const_iterator __first, const_iterator __last) {
158606c3fb27SDimitry Andric  _LIBCPP_ASSERT_VALID_INPUT_RANGE(__first <= __last, "vector::erase(first, last) called with invalid range");
15870b57cec5SDimitry Andric  pointer __p = this->__begin_ + (__first - begin());
15880b57cec5SDimitry Andric  if (__first != __last) {
1589bdd1243dSDimitry Andric    this->__destruct_at_end(std::move(__p + (__last - __first), this->__end_, __p));
15900b57cec5SDimitry Andric  }
1591bdd1243dSDimitry Andric  return __make_iter(__p);
15920b57cec5SDimitry Andric}
15930b57cec5SDimitry Andric
15940b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
1595cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 void
1596cb14a3feSDimitry Andricvector<_Tp, _Allocator>::__move_range(pointer __from_s, pointer __from_e, pointer __to) {
15970b57cec5SDimitry Andric  pointer __old_last  = this->__end_;
15980b57cec5SDimitry Andric  difference_type __n = __old_last - __to;
1599e40139ffSDimitry Andric  {
1600e40139ffSDimitry Andric    pointer __i = __from_s + __n;
1601e40139ffSDimitry Andric    _ConstructTransaction __tx(*this, __from_e - __i);
1602cb14a3feSDimitry Andric    for (pointer __pos = __tx.__pos_; __i < __from_e; ++__i, (void)++__pos, __tx.__pos_ = __pos) {
1603cb14a3feSDimitry Andric      __alloc_traits::construct(this->__alloc(), std::__to_address(__pos), std::move(*__i));
1604e40139ffSDimitry Andric    }
1605e40139ffSDimitry Andric  }
1606bdd1243dSDimitry Andric  std::move_backward(__from_s, __from_s + __n, __old_last);
16070b57cec5SDimitry Andric}
16080b57cec5SDimitry Andric
16090b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
1610cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<_Tp, _Allocator>::iterator
1611cb14a3feSDimitry Andricvector<_Tp, _Allocator>::insert(const_iterator __position, const_reference __x) {
16120b57cec5SDimitry Andric  pointer __p = this->__begin_ + (__position - begin());
1613*0fca6ea1SDimitry Andric  if (this->__end_ < this->__end_cap()) {
1614cb14a3feSDimitry Andric    if (__p == this->__end_) {
1615e40139ffSDimitry Andric      __construct_one_at_end(__x);
1616cb14a3feSDimitry Andric    } else {
16170b57cec5SDimitry Andric      __move_range(__p, this->__end_, __p + 1);
16180b57cec5SDimitry Andric      const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1619*0fca6ea1SDimitry Andric      if (std::__is_pointer_in_range(std::__to_address(__p), std::__to_address(__end_), std::addressof(__x)))
16200b57cec5SDimitry Andric        ++__xr;
16210b57cec5SDimitry Andric      *__p = *__xr;
16220b57cec5SDimitry Andric    }
1623cb14a3feSDimitry Andric  } else {
16240b57cec5SDimitry Andric    allocator_type& __a = this->__alloc();
16250b57cec5SDimitry Andric    __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
16260b57cec5SDimitry Andric    __v.push_back(__x);
16270b57cec5SDimitry Andric    __p = __swap_out_circular_buffer(__v, __p);
16280b57cec5SDimitry Andric  }
1629bdd1243dSDimitry Andric  return __make_iter(__p);
16300b57cec5SDimitry Andric}
16310b57cec5SDimitry Andric
16320b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
1633cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<_Tp, _Allocator>::iterator
1634cb14a3feSDimitry Andricvector<_Tp, _Allocator>::insert(const_iterator __position, value_type&& __x) {
16350b57cec5SDimitry Andric  pointer __p = this->__begin_ + (__position - begin());
1636cb14a3feSDimitry Andric  if (this->__end_ < this->__end_cap()) {
1637cb14a3feSDimitry Andric    if (__p == this->__end_) {
1638bdd1243dSDimitry Andric      __construct_one_at_end(std::move(__x));
1639cb14a3feSDimitry Andric    } else {
16400b57cec5SDimitry Andric      __move_range(__p, this->__end_, __p + 1);
1641bdd1243dSDimitry Andric      *__p = std::move(__x);
16420b57cec5SDimitry Andric    }
1643cb14a3feSDimitry Andric  } else {
16440b57cec5SDimitry Andric    allocator_type& __a = this->__alloc();
16450b57cec5SDimitry Andric    __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
1646bdd1243dSDimitry Andric    __v.push_back(std::move(__x));
16470b57cec5SDimitry Andric    __p = __swap_out_circular_buffer(__v, __p);
16480b57cec5SDimitry Andric  }
1649bdd1243dSDimitry Andric  return __make_iter(__p);
16500b57cec5SDimitry Andric}
16510b57cec5SDimitry Andric
16520b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
16530b57cec5SDimitry Andrictemplate <class... _Args>
1654cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<_Tp, _Allocator>::iterator
1655cb14a3feSDimitry Andricvector<_Tp, _Allocator>::emplace(const_iterator __position, _Args&&... __args) {
16560b57cec5SDimitry Andric  pointer __p = this->__begin_ + (__position - begin());
1657cb14a3feSDimitry Andric  if (this->__end_ < this->__end_cap()) {
1658cb14a3feSDimitry Andric    if (__p == this->__end_) {
1659bdd1243dSDimitry Andric      __construct_one_at_end(std::forward<_Args>(__args)...);
1660cb14a3feSDimitry Andric    } else {
1661bdd1243dSDimitry Andric      __temp_value<value_type, _Allocator> __tmp(this->__alloc(), std::forward<_Args>(__args)...);
16620b57cec5SDimitry Andric      __move_range(__p, this->__end_, __p + 1);
1663bdd1243dSDimitry Andric      *__p = std::move(__tmp.get());
16640b57cec5SDimitry Andric    }
1665cb14a3feSDimitry Andric  } else {
16660b57cec5SDimitry Andric    allocator_type& __a = this->__alloc();
16670b57cec5SDimitry Andric    __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
1668bdd1243dSDimitry Andric    __v.emplace_back(std::forward<_Args>(__args)...);
16690b57cec5SDimitry Andric    __p = __swap_out_circular_buffer(__v, __p);
16700b57cec5SDimitry Andric  }
1671bdd1243dSDimitry Andric  return __make_iter(__p);
16720b57cec5SDimitry Andric}
16730b57cec5SDimitry Andric
16740b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
1675cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<_Tp, _Allocator>::iterator
1676cb14a3feSDimitry Andricvector<_Tp, _Allocator>::insert(const_iterator __position, size_type __n, const_reference __x) {
16770b57cec5SDimitry Andric  pointer __p = this->__begin_ + (__position - begin());
1678cb14a3feSDimitry Andric  if (__n > 0) {
167961cfbce3SDimitry Andric    // We can't compare unrelated pointers inside constant expressions
1680cb14a3feSDimitry Andric    if (!__libcpp_is_constant_evaluated() && __n <= static_cast<size_type>(this->__end_cap() - this->__end_)) {
16810b57cec5SDimitry Andric      size_type __old_n  = __n;
16820b57cec5SDimitry Andric      pointer __old_last = this->__end_;
1683cb14a3feSDimitry Andric      if (__n > static_cast<size_type>(this->__end_ - __p)) {
16840b57cec5SDimitry Andric        size_type __cx = __n - (this->__end_ - __p);
16850b57cec5SDimitry Andric        __construct_at_end(__cx, __x);
16860b57cec5SDimitry Andric        __n -= __cx;
16870b57cec5SDimitry Andric      }
1688cb14a3feSDimitry Andric      if (__n > 0) {
16890b57cec5SDimitry Andric        __move_range(__p, __old_last, __p + __old_n);
16900b57cec5SDimitry Andric        const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
16910b57cec5SDimitry Andric        if (__p <= __xr && __xr < this->__end_)
16920b57cec5SDimitry Andric          __xr += __old_n;
1693bdd1243dSDimitry Andric        std::fill_n(__p, __n, *__xr);
16940b57cec5SDimitry Andric      }
1695cb14a3feSDimitry Andric    } else {
16960b57cec5SDimitry Andric      allocator_type& __a = this->__alloc();
16970b57cec5SDimitry Andric      __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
16980b57cec5SDimitry Andric      __v.__construct_at_end(__n, __x);
16990b57cec5SDimitry Andric      __p = __swap_out_circular_buffer(__v, __p);
17000b57cec5SDimitry Andric    }
17010b57cec5SDimitry Andric  }
1702bdd1243dSDimitry Andric  return __make_iter(__p);
17030b57cec5SDimitry Andric}
17040b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
1705cb14a3feSDimitry Andrictemplate <class _InputIterator,
1706cb14a3feSDimitry Andric          __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value &&
1707bdd1243dSDimitry Andric                            is_constructible<_Tp, typename iterator_traits<_InputIterator>::reference>::value,
1708bdd1243dSDimitry Andric                        int> >
1709bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<_Tp, _Allocator>::iterator
1710cb14a3feSDimitry Andricvector<_Tp, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last) {
171106c3fb27SDimitry Andric  return __insert_with_sentinel(__position, __first, __last);
171206c3fb27SDimitry Andric}
171306c3fb27SDimitry Andric
171406c3fb27SDimitry Andrictemplate <class _Tp, class _Allocator>
171506c3fb27SDimitry Andrictemplate <class _InputIterator, class _Sentinel>
1716cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI typename vector<_Tp, _Allocator>::iterator
171706c3fb27SDimitry Andricvector<_Tp, _Allocator>::__insert_with_sentinel(const_iterator __position, _InputIterator __first, _Sentinel __last) {
17180b57cec5SDimitry Andric  difference_type __off = __position - begin();
17190b57cec5SDimitry Andric  pointer __p           = this->__begin_ + __off;
17200b57cec5SDimitry Andric  allocator_type& __a   = this->__alloc();
17210b57cec5SDimitry Andric  pointer __old_last    = this->__end_;
1722cb14a3feSDimitry Andric  for (; this->__end_ != this->__end_cap() && __first != __last; ++__first) {
1723e40139ffSDimitry Andric    __construct_one_at_end(*__first);
17240b57cec5SDimitry Andric  }
17250b57cec5SDimitry Andric  __split_buffer<value_type, allocator_type&> __v(__a);
1726cb14a3feSDimitry Andric  if (__first != __last) {
172706c3fb27SDimitry Andric#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1728cb14a3feSDimitry Andric    try {
172906c3fb27SDimitry Andric#endif // _LIBCPP_HAS_NO_EXCEPTIONS
173006c3fb27SDimitry Andric      __v.__construct_at_end_with_sentinel(std::move(__first), std::move(__last));
17310b57cec5SDimitry Andric      difference_type __old_size = __old_last - this->__begin_;
17320b57cec5SDimitry Andric      difference_type __old_p    = __p - this->__begin_;
17330b57cec5SDimitry Andric      reserve(__recommend(size() + __v.size()));
17340b57cec5SDimitry Andric      __p        = this->__begin_ + __old_p;
17350b57cec5SDimitry Andric      __old_last = this->__begin_ + __old_size;
173606c3fb27SDimitry Andric#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1737cb14a3feSDimitry Andric    } catch (...) {
1738bdd1243dSDimitry Andric      erase(__make_iter(__old_last), end());
17390b57cec5SDimitry Andric      throw;
17400b57cec5SDimitry Andric    }
174106c3fb27SDimitry Andric#endif // _LIBCPP_HAS_NO_EXCEPTIONS
17420b57cec5SDimitry Andric  }
1743bdd1243dSDimitry Andric  __p = std::rotate(__p, __old_last, this->__end_);
1744cb14a3feSDimitry Andric  insert(__make_iter(__p), std::make_move_iterator(__v.begin()), std::make_move_iterator(__v.end()));
17450b57cec5SDimitry Andric  return begin() + __off;
17460b57cec5SDimitry Andric}
17470b57cec5SDimitry Andric
17480b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
1749cb14a3feSDimitry Andrictemplate <class _ForwardIterator,
1750cb14a3feSDimitry Andric          __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value &&
1751bdd1243dSDimitry Andric                            is_constructible<_Tp, typename iterator_traits<_ForwardIterator>::reference>::value,
1752bdd1243dSDimitry Andric                        int> >
1753bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<_Tp, _Allocator>::iterator
1754cb14a3feSDimitry Andricvector<_Tp, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last) {
175506c3fb27SDimitry Andric  return __insert_with_size(__position, __first, __last, std::distance(__first, __last));
175606c3fb27SDimitry Andric}
175706c3fb27SDimitry Andric
175806c3fb27SDimitry Andrictemplate <class _Tp, class _Allocator>
175906c3fb27SDimitry Andrictemplate <class _Iterator, class _Sentinel>
1760cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI typename vector<_Tp, _Allocator>::iterator
1761cb14a3feSDimitry Andricvector<_Tp, _Allocator>::__insert_with_size(
1762cb14a3feSDimitry Andric    const_iterator __position, _Iterator __first, _Sentinel __last, difference_type __n) {
176306c3fb27SDimitry Andric  auto __insertion_size = __n;
17640b57cec5SDimitry Andric  pointer __p           = this->__begin_ + (__position - begin());
1765cb14a3feSDimitry Andric  if (__n > 0) {
1766cb14a3feSDimitry Andric    if (__n <= this->__end_cap() - this->__end_) {
17670b57cec5SDimitry Andric      size_type __old_n    = __n;
17680b57cec5SDimitry Andric      pointer __old_last   = this->__end_;
176906c3fb27SDimitry Andric      _Iterator __m        = std::next(__first, __n);
17700b57cec5SDimitry Andric      difference_type __dx = this->__end_ - __p;
1771cb14a3feSDimitry Andric      if (__n > __dx) {
17720b57cec5SDimitry Andric        __m                    = __first;
17730b57cec5SDimitry Andric        difference_type __diff = this->__end_ - __p;
1774bdd1243dSDimitry Andric        std::advance(__m, __diff);
17750b57cec5SDimitry Andric        __construct_at_end(__m, __last, __n - __diff);
17760b57cec5SDimitry Andric        __n = __dx;
17770b57cec5SDimitry Andric      }
1778cb14a3feSDimitry Andric      if (__n > 0) {
17790b57cec5SDimitry Andric        __move_range(__p, __old_last, __p + __old_n);
1780bdd1243dSDimitry Andric        std::copy(__first, __m, __p);
17810b57cec5SDimitry Andric      }
1782cb14a3feSDimitry Andric    } else {
17830b57cec5SDimitry Andric      allocator_type& __a = this->__alloc();
17840b57cec5SDimitry Andric      __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
178506c3fb27SDimitry Andric      __v.__construct_at_end_with_size(__first, __insertion_size);
17860b57cec5SDimitry Andric      __p = __swap_out_circular_buffer(__v, __p);
17870b57cec5SDimitry Andric    }
17880b57cec5SDimitry Andric  }
1789bdd1243dSDimitry Andric  return __make_iter(__p);
17900b57cec5SDimitry Andric}
17910b57cec5SDimitry Andric
17920b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
1793cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<_Tp, _Allocator>::resize(size_type __sz) {
17940b57cec5SDimitry Andric  size_type __cs = size();
17950b57cec5SDimitry Andric  if (__cs < __sz)
17960b57cec5SDimitry Andric    this->__append(__sz - __cs);
17970b57cec5SDimitry Andric  else if (__cs > __sz)
17980b57cec5SDimitry Andric    this->__destruct_at_end(this->__begin_ + __sz);
17990b57cec5SDimitry Andric}
18000b57cec5SDimitry Andric
18010b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
1802cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<_Tp, _Allocator>::resize(size_type __sz, const_reference __x) {
18030b57cec5SDimitry Andric  size_type __cs = size();
18040b57cec5SDimitry Andric  if (__cs < __sz)
18050b57cec5SDimitry Andric    this->__append(__sz - __cs, __x);
18060b57cec5SDimitry Andric  else if (__cs > __sz)
18070b57cec5SDimitry Andric    this->__destruct_at_end(this->__begin_ + __sz);
18080b57cec5SDimitry Andric}
18090b57cec5SDimitry Andric
18100b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
1811cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<_Tp, _Allocator>::swap(vector& __x)
18120b57cec5SDimitry Andric#if _LIBCPP_STD_VER >= 14
18130b57cec5SDimitry Andric    _NOEXCEPT
18140b57cec5SDimitry Andric#else
1815*0fca6ea1SDimitry Andric    _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || __is_nothrow_swappable_v<allocator_type>)
18160b57cec5SDimitry Andric#endif
18170b57cec5SDimitry Andric{
1818cb14a3feSDimitry Andric  _LIBCPP_ASSERT_COMPATIBLE_ALLOCATOR(
1819cb14a3feSDimitry Andric      __alloc_traits::propagate_on_container_swap::value || this->__alloc() == __x.__alloc(),
18200b57cec5SDimitry Andric      "vector::swap: Either propagate_on_container_swap must be true"
18210b57cec5SDimitry Andric      " or the allocators must compare equal");
1822bdd1243dSDimitry Andric  std::swap(this->__begin_, __x.__begin_);
1823bdd1243dSDimitry Andric  std::swap(this->__end_, __x.__end_);
1824bdd1243dSDimitry Andric  std::swap(this->__end_cap(), __x.__end_cap());
1825cb14a3feSDimitry Andric  std::__swap_allocator(
1826cb14a3feSDimitry Andric      this->__alloc(), __x.__alloc(), integral_constant<bool, __alloc_traits::propagate_on_container_swap::value>());
18270b57cec5SDimitry Andric}
18280b57cec5SDimitry Andric
18290b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
1830cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 bool vector<_Tp, _Allocator>::__invariants() const {
1831cb14a3feSDimitry Andric  if (this->__begin_ == nullptr) {
18320b57cec5SDimitry Andric    if (this->__end_ != nullptr || this->__end_cap() != nullptr)
18330b57cec5SDimitry Andric      return false;
1834cb14a3feSDimitry Andric  } else {
18350b57cec5SDimitry Andric    if (this->__begin_ > this->__end_)
18360b57cec5SDimitry Andric      return false;
18370b57cec5SDimitry Andric    if (this->__begin_ == this->__end_cap())
18380b57cec5SDimitry Andric      return false;
18390b57cec5SDimitry Andric    if (this->__end_ > this->__end_cap())
18400b57cec5SDimitry Andric      return false;
18410b57cec5SDimitry Andric  }
18420b57cec5SDimitry Andric  return true;
18430b57cec5SDimitry Andric}
18440b57cec5SDimitry Andric
18450b57cec5SDimitry Andric// vector<bool>
18460b57cec5SDimitry Andric
1847cb14a3feSDimitry Andrictemplate <class _Allocator>
1848cb14a3feSDimitry Andricclass vector<bool, _Allocator>;
18490b57cec5SDimitry Andric
18500b57cec5SDimitry Andrictemplate <class _Allocator>
1851cb14a3feSDimitry Andricstruct hash<vector<bool, _Allocator> >;
1852cb14a3feSDimitry Andric
1853cb14a3feSDimitry Andrictemplate <class _Allocator>
1854cb14a3feSDimitry Andricstruct __has_storage_type<vector<bool, _Allocator> > {
18550b57cec5SDimitry Andric  static const bool value = true;
18560b57cec5SDimitry Andric};
18570b57cec5SDimitry Andric
18580b57cec5SDimitry Andrictemplate <class _Allocator>
1859cb14a3feSDimitry Andricclass _LIBCPP_TEMPLATE_VIS vector<bool, _Allocator> {
18600b57cec5SDimitry Andricpublic:
18610b57cec5SDimitry Andric  typedef vector __self;
18620b57cec5SDimitry Andric  typedef bool value_type;
18630b57cec5SDimitry Andric  typedef _Allocator allocator_type;
18640b57cec5SDimitry Andric  typedef allocator_traits<allocator_type> __alloc_traits;
18650b57cec5SDimitry Andric  typedef typename __alloc_traits::size_type size_type;
18660b57cec5SDimitry Andric  typedef typename __alloc_traits::difference_type difference_type;
18670b57cec5SDimitry Andric  typedef size_type __storage_type;
18680b57cec5SDimitry Andric  typedef __bit_iterator<vector, false> pointer;
18690b57cec5SDimitry Andric  typedef __bit_iterator<vector, true> const_pointer;
18700b57cec5SDimitry Andric  typedef pointer iterator;
18710b57cec5SDimitry Andric  typedef const_pointer const_iterator;
1872bdd1243dSDimitry Andric  typedef std::reverse_iterator<iterator> reverse_iterator;
1873bdd1243dSDimitry Andric  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
18740b57cec5SDimitry Andric
18750b57cec5SDimitry Andricprivate:
1876bdd1243dSDimitry Andric  typedef __rebind_alloc<__alloc_traits, __storage_type> __storage_allocator;
18770b57cec5SDimitry Andric  typedef allocator_traits<__storage_allocator> __storage_traits;
18780b57cec5SDimitry Andric  typedef typename __storage_traits::pointer __storage_pointer;
18790b57cec5SDimitry Andric  typedef typename __storage_traits::const_pointer __const_storage_pointer;
18800b57cec5SDimitry Andric
18810b57cec5SDimitry Andric  __storage_pointer __begin_;
18820b57cec5SDimitry Andric  size_type __size_;
18830b57cec5SDimitry Andric  __compressed_pair<size_type, __storage_allocator> __cap_alloc_;
1884cb14a3feSDimitry Andric
18850b57cec5SDimitry Andricpublic:
18860b57cec5SDimitry Andric  typedef __bit_reference<vector> reference;
188781ad6265SDimitry Andric#ifdef _LIBCPP_ABI_BITSET_VECTOR_BOOL_CONST_SUBSCRIPT_RETURN_BOOL
188881ad6265SDimitry Andric  using const_reference = bool;
188981ad6265SDimitry Andric#else
18900b57cec5SDimitry Andric  typedef __bit_const_reference<vector> const_reference;
189181ad6265SDimitry Andric#endif
1892cb14a3feSDimitry Andric
18930b57cec5SDimitry Andricprivate:
1894cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type& __cap() _NOEXCEPT { return __cap_alloc_.first(); }
1895cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const size_type& __cap() const _NOEXCEPT {
1896cb14a3feSDimitry Andric    return __cap_alloc_.first();
1897cb14a3feSDimitry Andric  }
1898cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __storage_allocator& __alloc() _NOEXCEPT {
1899cb14a3feSDimitry Andric    return __cap_alloc_.second();
1900cb14a3feSDimitry Andric  }
1901cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const __storage_allocator& __alloc() const _NOEXCEPT {
1902cb14a3feSDimitry Andric    return __cap_alloc_.second();
1903cb14a3feSDimitry Andric  }
19040b57cec5SDimitry Andric
19050b57cec5SDimitry Andric  static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
19060b57cec5SDimitry Andric
1907cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static size_type
1908cb14a3feSDimitry Andric  __internal_cap_to_external(size_type __n) _NOEXCEPT {
1909cb14a3feSDimitry Andric    return __n * __bits_per_word;
1910cb14a3feSDimitry Andric  }
1911cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static size_type
1912cb14a3feSDimitry Andric  __external_cap_to_internal(size_type __n) _NOEXCEPT {
1913cb14a3feSDimitry Andric    return (__n - 1) / __bits_per_word + 1;
1914cb14a3feSDimitry Andric  }
19150b57cec5SDimitry Andric
19160b57cec5SDimitry Andricpublic:
1917cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 vector()
1918cb14a3feSDimitry Andric      _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
19190b57cec5SDimitry Andric
1920bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit vector(const allocator_type& __a)
19210b57cec5SDimitry Andric#if _LIBCPP_STD_VER <= 14
19220b57cec5SDimitry Andric      _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
19230b57cec5SDimitry Andric#else
19240b57cec5SDimitry Andric      _NOEXCEPT;
19250b57cec5SDimitry Andric#endif
192650d7464cSDimitry Andric
192750d7464cSDimitry Andricprivate:
192850d7464cSDimitry Andric  class __destroy_vector {
192950d7464cSDimitry Andric  public:
193006c3fb27SDimitry Andric    _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI __destroy_vector(vector& __vec) : __vec_(__vec) {}
193150d7464cSDimitry Andric
1932bdd1243dSDimitry Andric    _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void operator()() {
193350d7464cSDimitry Andric      if (__vec_.__begin_ != nullptr)
193450d7464cSDimitry Andric        __storage_traits::deallocate(__vec_.__alloc(), __vec_.__begin_, __vec_.__cap());
193550d7464cSDimitry Andric    }
193650d7464cSDimitry Andric
193750d7464cSDimitry Andric  private:
193850d7464cSDimitry Andric    vector& __vec_;
193950d7464cSDimitry Andric  };
194050d7464cSDimitry Andric
194150d7464cSDimitry Andricpublic:
1942bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 ~vector() { __destroy_vector (*this)(); }
194350d7464cSDimitry Andric
1944bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit vector(size_type __n);
194506c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 14
1946bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit vector(size_type __n, const allocator_type& __a);
19470b57cec5SDimitry Andric#endif
1948bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 vector(size_type __n, const value_type& __v);
1949cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
1950cb14a3feSDimitry Andric  vector(size_type __n, const value_type& __v, const allocator_type& __a);
19515f757f3fSDimitry Andric  template <class _InputIterator, __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value, int> = 0>
19525f757f3fSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 vector(_InputIterator __first, _InputIterator __last);
19535f757f3fSDimitry Andric  template <class _InputIterator, __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value, int> = 0>
1954cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
1955cb14a3feSDimitry Andric  vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a);
19565f757f3fSDimitry Andric  template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> = 0>
19575f757f3fSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 vector(_ForwardIterator __first, _ForwardIterator __last);
19585f757f3fSDimitry Andric  template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> = 0>
1959cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
1960cb14a3feSDimitry Andric  vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a);
196106c3fb27SDimitry Andric
196206c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 23
196306c3fb27SDimitry Andric  template <_ContainerCompatibleRange<bool> _Range>
1964cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr vector(from_range_t, _Range&& __range, const allocator_type& __a = allocator_type())
1965cb14a3feSDimitry Andric      : __begin_(nullptr), __size_(0), __cap_alloc_(0, static_cast<__storage_allocator>(__a)) {
196606c3fb27SDimitry Andric    if constexpr (ranges::forward_range<_Range> || ranges::sized_range<_Range>) {
196706c3fb27SDimitry Andric      auto __n = static_cast<size_type>(ranges::distance(__range));
196806c3fb27SDimitry Andric      __init_with_size(ranges::begin(__range), ranges::end(__range), __n);
196906c3fb27SDimitry Andric
197006c3fb27SDimitry Andric    } else {
197106c3fb27SDimitry Andric      __init_with_sentinel(ranges::begin(__range), ranges::end(__range));
197206c3fb27SDimitry Andric    }
197306c3fb27SDimitry Andric  }
197406c3fb27SDimitry Andric#endif
19750b57cec5SDimitry Andric
1976bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 vector(const vector& __v);
1977bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 vector(const vector& __v, const allocator_type& __a);
1978bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 vector& operator=(const vector& __v);
19790b57cec5SDimitry Andric
19800b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
1981bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 vector(initializer_list<value_type> __il);
1982bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
1983cb14a3feSDimitry Andric  vector(initializer_list<value_type> __il, const allocator_type& __a);
1984cb14a3feSDimitry Andric
1985cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 vector& operator=(initializer_list<value_type> __il) {
1986cb14a3feSDimitry Andric    assign(__il.begin(), __il.end());
1987cb14a3feSDimitry Andric    return *this;
1988cb14a3feSDimitry Andric  }
19890b57cec5SDimitry Andric
19900b57cec5SDimitry Andric#endif // !_LIBCPP_CXX03_LANG
19910b57cec5SDimitry Andric
1992cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 vector(vector&& __v)
199306c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 17
199481ad6265SDimitry Andric      noexcept;
199581ad6265SDimitry Andric#else
199681ad6265SDimitry Andric      _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
199781ad6265SDimitry Andric#endif
1998bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
1999cb14a3feSDimitry Andric  vector(vector&& __v, const __type_identity_t<allocator_type>& __a);
2000cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 vector& operator=(vector&& __v)
2001*0fca6ea1SDimitry Andric      _NOEXCEPT_(__noexcept_move_assign_container<_Allocator, __alloc_traits>::value);
200281ad6265SDimitry Andric
20035f757f3fSDimitry Andric  template <class _InputIterator, __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value, int> = 0>
2004cb14a3feSDimitry Andric  void _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 assign(_InputIterator __first, _InputIterator __last);
20055f757f3fSDimitry Andric  template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> = 0>
2006cb14a3feSDimitry Andric  void _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 assign(_ForwardIterator __first, _ForwardIterator __last);
20070b57cec5SDimitry Andric
200806c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 23
200906c3fb27SDimitry Andric  template <_ContainerCompatibleRange<bool> _Range>
2010cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr void assign_range(_Range&& __range) {
201106c3fb27SDimitry Andric    if constexpr (ranges::forward_range<_Range> || ranges::sized_range<_Range>) {
201206c3fb27SDimitry Andric      auto __n = static_cast<size_type>(ranges::distance(__range));
201306c3fb27SDimitry Andric      __assign_with_size(ranges::begin(__range), ranges::end(__range), __n);
201406c3fb27SDimitry Andric
201506c3fb27SDimitry Andric    } else {
201606c3fb27SDimitry Andric      __assign_with_sentinel(ranges::begin(__range), ranges::end(__range));
201706c3fb27SDimitry Andric    }
201806c3fb27SDimitry Andric  }
201906c3fb27SDimitry Andric#endif
202006c3fb27SDimitry Andric
2021bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void assign(size_type __n, const value_type& __x);
20220b57cec5SDimitry Andric
20230b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
2024cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void assign(initializer_list<value_type> __il) {
2025cb14a3feSDimitry Andric    assign(__il.begin(), __il.end());
2026cb14a3feSDimitry Andric  }
20270b57cec5SDimitry Andric#endif
20280b57cec5SDimitry Andric
2029cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 allocator_type get_allocator() const _NOEXCEPT {
2030cb14a3feSDimitry Andric    return allocator_type(this->__alloc());
2031cb14a3feSDimitry Andric  }
20320b57cec5SDimitry Andric
2033bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type max_size() const _NOEXCEPT;
2034cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type capacity() const _NOEXCEPT {
2035cb14a3feSDimitry Andric    return __internal_cap_to_external(__cap());
2036cb14a3feSDimitry Andric  }
2037cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type size() const _NOEXCEPT { return __size_; }
2038*0fca6ea1SDimitry Andric  _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool empty() const _NOEXCEPT {
2039cb14a3feSDimitry Andric    return __size_ == 0;
2040cb14a3feSDimitry Andric  }
2041bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void reserve(size_type __n);
2042bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void shrink_to_fit() _NOEXCEPT;
20430b57cec5SDimitry Andric
2044cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator begin() _NOEXCEPT { return __make_iter(0); }
2045cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_iterator begin() const _NOEXCEPT { return __make_iter(0); }
2046cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator end() _NOEXCEPT { return __make_iter(__size_); }
2047cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_iterator end() const _NOEXCEPT {
2048cb14a3feSDimitry Andric    return __make_iter(__size_);
2049cb14a3feSDimitry Andric  }
20500b57cec5SDimitry Andric
2051cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reverse_iterator rbegin() _NOEXCEPT {
2052cb14a3feSDimitry Andric    return reverse_iterator(end());
2053cb14a3feSDimitry Andric  }
2054cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reverse_iterator rbegin() const _NOEXCEPT {
2055cb14a3feSDimitry Andric    return const_reverse_iterator(end());
2056cb14a3feSDimitry Andric  }
2057cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reverse_iterator rend() _NOEXCEPT {
2058cb14a3feSDimitry Andric    return reverse_iterator(begin());
2059cb14a3feSDimitry Andric  }
2060cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reverse_iterator rend() const _NOEXCEPT {
2061cb14a3feSDimitry Andric    return const_reverse_iterator(begin());
2062cb14a3feSDimitry Andric  }
20630b57cec5SDimitry Andric
2064cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_iterator cbegin() const _NOEXCEPT { return __make_iter(0); }
2065cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_iterator cend() const _NOEXCEPT {
2066cb14a3feSDimitry Andric    return __make_iter(__size_);
2067cb14a3feSDimitry Andric  }
2068cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reverse_iterator crbegin() const _NOEXCEPT {
2069cb14a3feSDimitry Andric    return rbegin();
2070cb14a3feSDimitry Andric  }
2071cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reverse_iterator crend() const _NOEXCEPT { return rend(); }
20720b57cec5SDimitry Andric
2073bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference operator[](size_type __n) { return __make_ref(__n); }
2074cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference operator[](size_type __n) const {
2075cb14a3feSDimitry Andric    return __make_ref(__n);
2076cb14a3feSDimitry Andric  }
2077bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI reference at(size_type __n);
2078bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI const_reference at(size_type __n) const;
20790b57cec5SDimitry Andric
2080bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference front() { return __make_ref(0); }
2081bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference front() const { return __make_ref(0); }
2082bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference back() { return __make_ref(__size_ - 1); }
2083bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference back() const { return __make_ref(__size_ - 1); }
20840b57cec5SDimitry Andric
2085bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void push_back(const value_type& __x);
208606c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 14
20870b57cec5SDimitry Andric  template <class... _Args>
208806c3fb27SDimitry Andric#  if _LIBCPP_STD_VER >= 17
2089bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference emplace_back(_Args&&... __args)
20900b57cec5SDimitry Andric#  else
2091bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI void emplace_back(_Args&&... __args)
20920b57cec5SDimitry Andric#  endif
20930b57cec5SDimitry Andric  {
2094bdd1243dSDimitry Andric    push_back(value_type(std::forward<_Args>(__args)...));
209506c3fb27SDimitry Andric#  if _LIBCPP_STD_VER >= 17
20960b57cec5SDimitry Andric    return this->back();
20970b57cec5SDimitry Andric#  endif
20980b57cec5SDimitry Andric  }
20990b57cec5SDimitry Andric#endif
21000b57cec5SDimitry Andric
210106c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 23
210206c3fb27SDimitry Andric  template <_ContainerCompatibleRange<bool> _Range>
2103cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr void append_range(_Range&& __range) {
210406c3fb27SDimitry Andric    insert_range(end(), std::forward<_Range>(__range));
210506c3fb27SDimitry Andric  }
210606c3fb27SDimitry Andric#endif
210706c3fb27SDimitry Andric
2108bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void pop_back() { --__size_; }
21090b57cec5SDimitry Andric
211006c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 14
21110b57cec5SDimitry Andric  template <class... _Args>
2112cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator emplace(const_iterator __position, _Args&&... __args) {
2113cb14a3feSDimitry Andric    return insert(__position, value_type(std::forward<_Args>(__args)...));
2114cb14a3feSDimitry Andric  }
21150b57cec5SDimitry Andric#endif
21160b57cec5SDimitry Andric
2117bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator insert(const_iterator __position, const value_type& __x);
2118cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator
2119cb14a3feSDimitry Andric  insert(const_iterator __position, size_type __n, const value_type& __x);
21205f757f3fSDimitry Andric  template <class _InputIterator, __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value, int> = 0>
2121cb14a3feSDimitry Andric  iterator _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
2122cb14a3feSDimitry Andric  insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
21235f757f3fSDimitry Andric  template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> = 0>
2124cb14a3feSDimitry Andric  iterator _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
2125cb14a3feSDimitry Andric  insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
21260b57cec5SDimitry Andric
212706c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 23
212806c3fb27SDimitry Andric  template <_ContainerCompatibleRange<bool> _Range>
2129cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr iterator insert_range(const_iterator __position, _Range&& __range) {
213006c3fb27SDimitry Andric    if constexpr (ranges::forward_range<_Range> || ranges::sized_range<_Range>) {
213106c3fb27SDimitry Andric      auto __n = static_cast<size_type>(ranges::distance(__range));
213206c3fb27SDimitry Andric      return __insert_with_size(__position, ranges::begin(__range), ranges::end(__range), __n);
213306c3fb27SDimitry Andric
213406c3fb27SDimitry Andric    } else {
213506c3fb27SDimitry Andric      return __insert_with_sentinel(__position, ranges::begin(__range), ranges::end(__range));
213606c3fb27SDimitry Andric    }
213706c3fb27SDimitry Andric  }
213806c3fb27SDimitry Andric#endif
213906c3fb27SDimitry Andric
21400b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
2141cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator
2142cb14a3feSDimitry Andric  insert(const_iterator __position, initializer_list<value_type> __il) {
2143cb14a3feSDimitry Andric    return insert(__position, __il.begin(), __il.end());
2144cb14a3feSDimitry Andric  }
21450b57cec5SDimitry Andric#endif
21460b57cec5SDimitry Andric
2147bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator erase(const_iterator __position);
2148bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator erase(const_iterator __first, const_iterator __last);
21490b57cec5SDimitry Andric
2150cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void clear() _NOEXCEPT { __size_ = 0; }
21510b57cec5SDimitry Andric
215206c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void swap(vector&)
21530b57cec5SDimitry Andric#if _LIBCPP_STD_VER >= 14
21540b57cec5SDimitry Andric      _NOEXCEPT;
21550b57cec5SDimitry Andric#else
2156*0fca6ea1SDimitry Andric      _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || __is_nothrow_swappable_v<allocator_type>);
21570b57cec5SDimitry Andric#endif
2158cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static void swap(reference __x, reference __y) _NOEXCEPT {
2159cb14a3feSDimitry Andric    std::swap(__x, __y);
2160cb14a3feSDimitry Andric  }
21610b57cec5SDimitry Andric
2162bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void resize(size_type __sz, value_type __x = false);
2163bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void flip() _NOEXCEPT;
21640b57cec5SDimitry Andric
2165bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __invariants() const;
21660b57cec5SDimitry Andric
21670b57cec5SDimitry Andricprivate:
2168cb14a3feSDimitry Andric  _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI void __throw_length_error() const { std::__throw_length_error("vector"); }
2169d56accc7SDimitry Andric
2170cb14a3feSDimitry Andric  _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI void __throw_out_of_range() const { std::__throw_out_of_range("vector"); }
2171d56accc7SDimitry Andric
217206c3fb27SDimitry Andric  template <class _InputIterator, class _Sentinel>
2173cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
2174cb14a3feSDimitry Andric  __init_with_size(_InputIterator __first, _Sentinel __last, size_type __n) {
217506c3fb27SDimitry Andric    auto __guard = std::__make_exception_guard(__destroy_vector(*this));
217606c3fb27SDimitry Andric
217706c3fb27SDimitry Andric    if (__n > 0) {
217806c3fb27SDimitry Andric      __vallocate(__n);
217906c3fb27SDimitry Andric      __construct_at_end(std::move(__first), std::move(__last), __n);
218006c3fb27SDimitry Andric    }
218106c3fb27SDimitry Andric
218206c3fb27SDimitry Andric    __guard.__complete();
218306c3fb27SDimitry Andric  }
218406c3fb27SDimitry Andric
218506c3fb27SDimitry Andric  template <class _InputIterator, class _Sentinel>
2186cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
2187cb14a3feSDimitry Andric  __init_with_sentinel(_InputIterator __first, _Sentinel __last) {
218806c3fb27SDimitry Andric#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
218906c3fb27SDimitry Andric    try {
219006c3fb27SDimitry Andric#endif // _LIBCPP_HAS_NO_EXCEPTIONS
219106c3fb27SDimitry Andric      for (; __first != __last; ++__first)
219206c3fb27SDimitry Andric        push_back(*__first);
219306c3fb27SDimitry Andric#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
219406c3fb27SDimitry Andric    } catch (...) {
219506c3fb27SDimitry Andric      if (__begin_ != nullptr)
219606c3fb27SDimitry Andric        __storage_traits::deallocate(__alloc(), __begin_, __cap());
219706c3fb27SDimitry Andric      throw;
219806c3fb27SDimitry Andric    }
219906c3fb27SDimitry Andric#endif // _LIBCPP_HAS_NO_EXCEPTIONS
220006c3fb27SDimitry Andric  }
220106c3fb27SDimitry Andric
220206c3fb27SDimitry Andric  template <class _Iterator, class _Sentinel>
2203cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __assign_with_sentinel(_Iterator __first, _Sentinel __last);
220406c3fb27SDimitry Andric
220506c3fb27SDimitry Andric  template <class _ForwardIterator, class _Sentinel>
2206cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
2207cb14a3feSDimitry Andric  __assign_with_size(_ForwardIterator __first, _Sentinel __last, difference_type __ns);
220806c3fb27SDimitry Andric
220906c3fb27SDimitry Andric  template <class _InputIterator, class _Sentinel>
2210cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator
2211cb14a3feSDimitry Andric  __insert_with_sentinel(const_iterator __position, _InputIterator __first, _Sentinel __last);
221206c3fb27SDimitry Andric
221306c3fb27SDimitry Andric  template <class _Iterator, class _Sentinel>
2214cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator
2215cb14a3feSDimitry Andric  __insert_with_size(const_iterator __position, _Iterator __first, _Sentinel __last, difference_type __n);
221606c3fb27SDimitry Andric
221781ad6265SDimitry Andric  //  Allocate space for __n objects
221881ad6265SDimitry Andric  //  throws length_error if __n > max_size()
221981ad6265SDimitry Andric  //  throws (probably bad_alloc) if memory run out
222081ad6265SDimitry Andric  //  Precondition:  __begin_ == __end_ == __cap() == 0
222181ad6265SDimitry Andric  //  Precondition:  __n > 0
222281ad6265SDimitry Andric  //  Postcondition:  capacity() >= __n
222381ad6265SDimitry Andric  //  Postcondition:  size() == 0
2224bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __vallocate(size_type __n) {
222581ad6265SDimitry Andric    if (__n > max_size())
222681ad6265SDimitry Andric      __throw_length_error();
222781ad6265SDimitry Andric    auto __allocation = std::__allocate_at_least(__alloc(), __external_cap_to_internal(__n));
222881ad6265SDimitry Andric    __begin_          = __allocation.ptr;
222981ad6265SDimitry Andric    __size_           = 0;
223081ad6265SDimitry Andric    __cap()           = __allocation.count;
223161cfbce3SDimitry Andric    if (__libcpp_is_constant_evaluated()) {
223261cfbce3SDimitry Andric      for (size_type __i = 0; __i != __cap(); ++__i)
223361cfbce3SDimitry Andric        std::__construct_at(std::__to_address(__begin_) + __i);
223461cfbce3SDimitry Andric    }
223581ad6265SDimitry Andric  }
223681ad6265SDimitry Andric
2237bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __vdeallocate() _NOEXCEPT;
2238cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static size_type __align_it(size_type __new_size) _NOEXCEPT {
2239cb14a3feSDimitry Andric    return (__new_size + (__bits_per_word - 1)) & ~((size_type)__bits_per_word - 1);
2240cb14a3feSDimitry Andric  }
2241bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type __recommend(size_type __new_size) const;
2242bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __construct_at_end(size_type __n, bool __x);
224306c3fb27SDimitry Andric  template <class _InputIterator, class _Sentinel>
2244cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
2245cb14a3feSDimitry Andric  __construct_at_end(_InputIterator __first, _Sentinel __last, size_type __n);
2246bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __append(size_type __n, const_reference __x);
2247cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference __make_ref(size_type __pos) _NOEXCEPT {
2248cb14a3feSDimitry Andric    return reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);
224981ad6265SDimitry Andric  }
2250cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference __make_ref(size_type __pos) const _NOEXCEPT {
2251cb14a3feSDimitry Andric    return __bit_const_reference<vector>(
2252cb14a3feSDimitry Andric        __begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);
2253cb14a3feSDimitry Andric  }
2254cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator __make_iter(size_type __pos) _NOEXCEPT {
2255cb14a3feSDimitry Andric    return iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));
2256cb14a3feSDimitry Andric  }
2257cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_iterator __make_iter(size_type __pos) const _NOEXCEPT {
2258cb14a3feSDimitry Andric    return const_iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));
2259cb14a3feSDimitry Andric  }
2260cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator __const_iterator_cast(const_iterator __p) _NOEXCEPT {
2261cb14a3feSDimitry Andric    return begin() + (__p - cbegin());
2262cb14a3feSDimitry Andric  }
22630b57cec5SDimitry Andric
2264cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __copy_assign_alloc(const vector& __v) {
2265cb14a3feSDimitry Andric    __copy_assign_alloc(
2266cb14a3feSDimitry Andric        __v, integral_constant<bool, __storage_traits::propagate_on_container_copy_assignment::value>());
2267cb14a3feSDimitry Andric  }
2268cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __copy_assign_alloc(const vector& __c, true_type) {
22690b57cec5SDimitry Andric    if (__alloc() != __c.__alloc())
22700b57cec5SDimitry Andric      __vdeallocate();
22710b57cec5SDimitry Andric    __alloc() = __c.__alloc();
22720b57cec5SDimitry Andric  }
22730b57cec5SDimitry Andric
2274cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __copy_assign_alloc(const vector&, false_type) {}
22750b57cec5SDimitry Andric
2276bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __move_assign(vector& __c, false_type);
2277bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __move_assign(vector& __c, true_type)
22780b57cec5SDimitry Andric      _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
2279cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __move_assign_alloc(vector& __c)
2280cb14a3feSDimitry Andric      _NOEXCEPT_(!__storage_traits::propagate_on_container_move_assignment::value ||
2281cb14a3feSDimitry Andric                 is_nothrow_move_assignable<allocator_type>::value) {
2282cb14a3feSDimitry Andric    __move_assign_alloc(
2283cb14a3feSDimitry Andric        __c, integral_constant<bool, __storage_traits::propagate_on_container_move_assignment::value>());
2284cb14a3feSDimitry Andric  }
2285cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __move_assign_alloc(vector& __c, true_type)
2286cb14a3feSDimitry Andric      _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value) {
2287bdd1243dSDimitry Andric    __alloc() = std::move(__c.__alloc());
22880b57cec5SDimitry Andric  }
22890b57cec5SDimitry Andric
2290cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __move_assign_alloc(vector&, false_type) _NOEXCEPT {}
22910b57cec5SDimitry Andric
2292bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_t __hash_code() const _NOEXCEPT;
22930b57cec5SDimitry Andric
22940b57cec5SDimitry Andric  friend class __bit_reference<vector>;
22950b57cec5SDimitry Andric  friend class __bit_const_reference<vector>;
22960b57cec5SDimitry Andric  friend class __bit_iterator<vector, false>;
22970b57cec5SDimitry Andric  friend class __bit_iterator<vector, true>;
22980b57cec5SDimitry Andric  friend struct __bit_array<vector>;
22990b57cec5SDimitry Andric  friend struct _LIBCPP_TEMPLATE_VIS hash<vector>;
23000b57cec5SDimitry Andric};
23010b57cec5SDimitry Andric
23020b57cec5SDimitry Andrictemplate <class _Allocator>
2303cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<bool, _Allocator>::__vdeallocate() _NOEXCEPT {
2304cb14a3feSDimitry Andric  if (this->__begin_ != nullptr) {
23050b57cec5SDimitry Andric    __storage_traits::deallocate(this->__alloc(), this->__begin_, __cap());
23060b57cec5SDimitry Andric    this->__begin_ = nullptr;
23070b57cec5SDimitry Andric    this->__size_ = this->__cap() = 0;
23080b57cec5SDimitry Andric  }
23090b57cec5SDimitry Andric}
23100b57cec5SDimitry Andric
23110b57cec5SDimitry Andrictemplate <class _Allocator>
2312cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<bool, _Allocator>::size_type
2313cb14a3feSDimitry Andricvector<bool, _Allocator>::max_size() const _NOEXCEPT {
23140b57cec5SDimitry Andric  size_type __amax = __storage_traits::max_size(__alloc());
23150b57cec5SDimitry Andric  size_type __nmax = numeric_limits<size_type>::max() / 2; // end() >= begin(), always
23160b57cec5SDimitry Andric  if (__nmax / __bits_per_word <= __amax)
23170b57cec5SDimitry Andric    return __nmax;
23180b57cec5SDimitry Andric  return __internal_cap_to_external(__amax);
23190b57cec5SDimitry Andric}
23200b57cec5SDimitry Andric
23210b57cec5SDimitry Andric//  Precondition:  __new_size > capacity()
23220b57cec5SDimitry Andrictemplate <class _Allocator>
2323cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<bool, _Allocator>::size_type
2324cb14a3feSDimitry Andricvector<bool, _Allocator>::__recommend(size_type __new_size) const {
23250b57cec5SDimitry Andric  const size_type __ms = max_size();
23260b57cec5SDimitry Andric  if (__new_size > __ms)
23270b57cec5SDimitry Andric    this->__throw_length_error();
23280b57cec5SDimitry Andric  const size_type __cap = capacity();
23290b57cec5SDimitry Andric  if (__cap >= __ms / 2)
23300b57cec5SDimitry Andric    return __ms;
2331bdd1243dSDimitry Andric  return std::max(2 * __cap, __align_it(__new_size));
23320b57cec5SDimitry Andric}
23330b57cec5SDimitry Andric
23340b57cec5SDimitry Andric//  Default constructs __n objects starting at __end_
23350b57cec5SDimitry Andric//  Precondition:  __n > 0
23360b57cec5SDimitry Andric//  Precondition:  size() + __n <= capacity()
23370b57cec5SDimitry Andric//  Postcondition:  size() == size() + __n
23380b57cec5SDimitry Andrictemplate <class _Allocator>
2339cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
2340cb14a3feSDimitry Andricvector<bool, _Allocator>::__construct_at_end(size_type __n, bool __x) {
23410b57cec5SDimitry Andric  size_type __old_size = this->__size_;
23420b57cec5SDimitry Andric  this->__size_ += __n;
2343cb14a3feSDimitry Andric  if (__old_size == 0 || ((__old_size - 1) / __bits_per_word) != ((this->__size_ - 1) / __bits_per_word)) {
23440b57cec5SDimitry Andric    if (this->__size_ <= __bits_per_word)
23450b57cec5SDimitry Andric      this->__begin_[0] = __storage_type(0);
23460b57cec5SDimitry Andric    else
23470b57cec5SDimitry Andric      this->__begin_[(this->__size_ - 1) / __bits_per_word] = __storage_type(0);
23480b57cec5SDimitry Andric  }
2349bdd1243dSDimitry Andric  std::fill_n(__make_iter(__old_size), __n, __x);
23500b57cec5SDimitry Andric}
23510b57cec5SDimitry Andric
23520b57cec5SDimitry Andrictemplate <class _Allocator>
235306c3fb27SDimitry Andrictemplate <class _InputIterator, class _Sentinel>
2354cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 void
2355cb14a3feSDimitry Andricvector<bool, _Allocator>::__construct_at_end(_InputIterator __first, _Sentinel __last, size_type __n) {
23560b57cec5SDimitry Andric  size_type __old_size = this->__size_;
235706c3fb27SDimitry Andric  this->__size_ += __n;
2358cb14a3feSDimitry Andric  if (__old_size == 0 || ((__old_size - 1) / __bits_per_word) != ((this->__size_ - 1) / __bits_per_word)) {
23590b57cec5SDimitry Andric    if (this->__size_ <= __bits_per_word)
23600b57cec5SDimitry Andric      this->__begin_[0] = __storage_type(0);
23610b57cec5SDimitry Andric    else
23620b57cec5SDimitry Andric      this->__begin_[(this->__size_ - 1) / __bits_per_word] = __storage_type(0);
23630b57cec5SDimitry Andric  }
236406c3fb27SDimitry Andric  std::__copy<_ClassicAlgPolicy>(__first, __last, __make_iter(__old_size));
23650b57cec5SDimitry Andric}
23660b57cec5SDimitry Andric
23670b57cec5SDimitry Andrictemplate <class _Allocator>
2368cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 vector<bool, _Allocator>::vector()
23690b57cec5SDimitry Andric    _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
2370cb14a3feSDimitry Andric    : __begin_(nullptr), __size_(0), __cap_alloc_(0, __default_init_tag()) {}
23710b57cec5SDimitry Andric
23720b57cec5SDimitry Andrictemplate <class _Allocator>
2373cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 vector<bool, _Allocator>::vector(const allocator_type& __a)
23740b57cec5SDimitry Andric#if _LIBCPP_STD_VER <= 14
23750b57cec5SDimitry Andric    _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
23760b57cec5SDimitry Andric#else
23770b57cec5SDimitry Andric        _NOEXCEPT
23780b57cec5SDimitry Andric#endif
2379cb14a3feSDimitry Andric    : __begin_(nullptr), __size_(0), __cap_alloc_(0, static_cast<__storage_allocator>(__a)) {
23800b57cec5SDimitry Andric}
23810b57cec5SDimitry Andric
23820b57cec5SDimitry Andrictemplate <class _Allocator>
2383cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 vector<bool, _Allocator>::vector(size_type __n)
2384cb14a3feSDimitry Andric    : __begin_(nullptr), __size_(0), __cap_alloc_(0, __default_init_tag()) {
2385cb14a3feSDimitry Andric  if (__n > 0) {
23860b57cec5SDimitry Andric    __vallocate(__n);
23870b57cec5SDimitry Andric    __construct_at_end(__n, false);
23880b57cec5SDimitry Andric  }
23890b57cec5SDimitry Andric}
23900b57cec5SDimitry Andric
239106c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 14
23920b57cec5SDimitry Andrictemplate <class _Allocator>
2393cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 vector<bool, _Allocator>::vector(size_type __n, const allocator_type& __a)
2394cb14a3feSDimitry Andric    : __begin_(nullptr), __size_(0), __cap_alloc_(0, static_cast<__storage_allocator>(__a)) {
2395cb14a3feSDimitry Andric  if (__n > 0) {
23960b57cec5SDimitry Andric    __vallocate(__n);
23970b57cec5SDimitry Andric    __construct_at_end(__n, false);
23980b57cec5SDimitry Andric  }
23990b57cec5SDimitry Andric}
24000b57cec5SDimitry Andric#endif
24010b57cec5SDimitry Andric
24020b57cec5SDimitry Andrictemplate <class _Allocator>
2403cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 vector<bool, _Allocator>::vector(size_type __n, const value_type& __x)
2404cb14a3feSDimitry Andric    : __begin_(nullptr), __size_(0), __cap_alloc_(0, __default_init_tag()) {
2405cb14a3feSDimitry Andric  if (__n > 0) {
24060b57cec5SDimitry Andric    __vallocate(__n);
24070b57cec5SDimitry Andric    __construct_at_end(__n, __x);
24080b57cec5SDimitry Andric  }
24090b57cec5SDimitry Andric}
24100b57cec5SDimitry Andric
24110b57cec5SDimitry Andrictemplate <class _Allocator>
2412bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20
24130b57cec5SDimitry Andricvector<bool, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
2414cb14a3feSDimitry Andric    : __begin_(nullptr), __size_(0), __cap_alloc_(0, static_cast<__storage_allocator>(__a)) {
2415cb14a3feSDimitry Andric  if (__n > 0) {
24160b57cec5SDimitry Andric    __vallocate(__n);
24170b57cec5SDimitry Andric    __construct_at_end(__n, __x);
24180b57cec5SDimitry Andric  }
24190b57cec5SDimitry Andric}
24200b57cec5SDimitry Andric
24210b57cec5SDimitry Andrictemplate <class _Allocator>
24225f757f3fSDimitry Andrictemplate <class _InputIterator, __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value, int> >
2423cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last)
2424cb14a3feSDimitry Andric    : __begin_(nullptr), __size_(0), __cap_alloc_(0, __default_init_tag()) {
242506c3fb27SDimitry Andric  __init_with_sentinel(__first, __last);
24260b57cec5SDimitry Andric}
24270b57cec5SDimitry Andric
24280b57cec5SDimitry Andrictemplate <class _Allocator>
24295f757f3fSDimitry Andrictemplate <class _InputIterator, __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value, int> >
2430bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20
24315f757f3fSDimitry Andricvector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a)
2432cb14a3feSDimitry Andric    : __begin_(nullptr), __size_(0), __cap_alloc_(0, static_cast<__storage_allocator>(__a)) {
243306c3fb27SDimitry Andric  __init_with_sentinel(__first, __last);
24340b57cec5SDimitry Andric}
24350b57cec5SDimitry Andric
24360b57cec5SDimitry Andrictemplate <class _Allocator>
24375f757f3fSDimitry Andrictemplate <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> >
2438cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last)
2439cb14a3feSDimitry Andric    : __begin_(nullptr), __size_(0), __cap_alloc_(0, __default_init_tag()) {
244006c3fb27SDimitry Andric  auto __n = static_cast<size_type>(std::distance(__first, __last));
244106c3fb27SDimitry Andric  __init_with_size(__first, __last, __n);
24420b57cec5SDimitry Andric}
24430b57cec5SDimitry Andric
24440b57cec5SDimitry Andrictemplate <class _Allocator>
24455f757f3fSDimitry Andrictemplate <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> >
2446bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20
24475f757f3fSDimitry Andricvector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a)
2448cb14a3feSDimitry Andric    : __begin_(nullptr), __size_(0), __cap_alloc_(0, static_cast<__storage_allocator>(__a)) {
244906c3fb27SDimitry Andric  auto __n = static_cast<size_type>(std::distance(__first, __last));
245006c3fb27SDimitry Andric  __init_with_size(__first, __last, __n);
24510b57cec5SDimitry Andric}
24520b57cec5SDimitry Andric
24530b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
24540b57cec5SDimitry Andric
24550b57cec5SDimitry Andrictemplate <class _Allocator>
2456cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 vector<bool, _Allocator>::vector(initializer_list<value_type> __il)
2457cb14a3feSDimitry Andric    : __begin_(nullptr), __size_(0), __cap_alloc_(0, __default_init_tag()) {
24580b57cec5SDimitry Andric  size_type __n = static_cast<size_type>(__il.size());
2459cb14a3feSDimitry Andric  if (__n > 0) {
24600b57cec5SDimitry Andric    __vallocate(__n);
246106c3fb27SDimitry Andric    __construct_at_end(__il.begin(), __il.end(), __n);
24620b57cec5SDimitry Andric  }
24630b57cec5SDimitry Andric}
24640b57cec5SDimitry Andric
24650b57cec5SDimitry Andrictemplate <class _Allocator>
2466bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20
24670b57cec5SDimitry Andricvector<bool, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
2468cb14a3feSDimitry Andric    : __begin_(nullptr), __size_(0), __cap_alloc_(0, static_cast<__storage_allocator>(__a)) {
24690b57cec5SDimitry Andric  size_type __n = static_cast<size_type>(__il.size());
2470cb14a3feSDimitry Andric  if (__n > 0) {
24710b57cec5SDimitry Andric    __vallocate(__n);
247206c3fb27SDimitry Andric    __construct_at_end(__il.begin(), __il.end(), __n);
24730b57cec5SDimitry Andric  }
24740b57cec5SDimitry Andric}
24750b57cec5SDimitry Andric
24760b57cec5SDimitry Andric#endif // _LIBCPP_CXX03_LANG
24770b57cec5SDimitry Andric
24780b57cec5SDimitry Andrictemplate <class _Allocator>
2479cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 vector<bool, _Allocator>::vector(const vector& __v)
24800b57cec5SDimitry Andric    : __begin_(nullptr),
24810b57cec5SDimitry Andric      __size_(0),
2482cb14a3feSDimitry Andric      __cap_alloc_(0, __storage_traits::select_on_container_copy_construction(__v.__alloc())) {
2483cb14a3feSDimitry Andric  if (__v.size() > 0) {
24840b57cec5SDimitry Andric    __vallocate(__v.size());
248506c3fb27SDimitry Andric    __construct_at_end(__v.begin(), __v.end(), __v.size());
24860b57cec5SDimitry Andric  }
24870b57cec5SDimitry Andric}
24880b57cec5SDimitry Andric
24890b57cec5SDimitry Andrictemplate <class _Allocator>
2490cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 vector<bool, _Allocator>::vector(const vector& __v, const allocator_type& __a)
2491cb14a3feSDimitry Andric    : __begin_(nullptr), __size_(0), __cap_alloc_(0, __a) {
2492cb14a3feSDimitry Andric  if (__v.size() > 0) {
24930b57cec5SDimitry Andric    __vallocate(__v.size());
249406c3fb27SDimitry Andric    __construct_at_end(__v.begin(), __v.end(), __v.size());
24950b57cec5SDimitry Andric  }
24960b57cec5SDimitry Andric}
24970b57cec5SDimitry Andric
24980b57cec5SDimitry Andrictemplate <class _Allocator>
2499cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 vector<bool, _Allocator>& vector<bool, _Allocator>::operator=(const vector& __v) {
2500cb14a3feSDimitry Andric  if (this != std::addressof(__v)) {
25010b57cec5SDimitry Andric    __copy_assign_alloc(__v);
2502cb14a3feSDimitry Andric    if (__v.__size_) {
2503cb14a3feSDimitry Andric      if (__v.__size_ > capacity()) {
25040b57cec5SDimitry Andric        __vdeallocate();
25050b57cec5SDimitry Andric        __vallocate(__v.__size_);
25060b57cec5SDimitry Andric      }
2507bdd1243dSDimitry Andric      std::copy(__v.__begin_, __v.__begin_ + __external_cap_to_internal(__v.__size_), __begin_);
25080b57cec5SDimitry Andric    }
25090b57cec5SDimitry Andric    __size_ = __v.__size_;
25100b57cec5SDimitry Andric  }
25110b57cec5SDimitry Andric  return *this;
25120b57cec5SDimitry Andric}
25130b57cec5SDimitry Andric
25140b57cec5SDimitry Andrictemplate <class _Allocator>
2515bdd1243dSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 vector<bool, _Allocator>::vector(vector&& __v)
251606c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 17
25170b57cec5SDimitry Andric    _NOEXCEPT
25180b57cec5SDimitry Andric#else
25190b57cec5SDimitry Andric    _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
25200b57cec5SDimitry Andric#endif
25210b57cec5SDimitry Andric    : __begin_(__v.__begin_),
25220b57cec5SDimitry Andric      __size_(__v.__size_),
2523bdd1243dSDimitry Andric      __cap_alloc_(std::move(__v.__cap_alloc_)) {
25240b57cec5SDimitry Andric  __v.__begin_ = nullptr;
25250b57cec5SDimitry Andric  __v.__size_  = 0;
25260b57cec5SDimitry Andric  __v.__cap()  = 0;
25270b57cec5SDimitry Andric}
25280b57cec5SDimitry Andric
25290b57cec5SDimitry Andrictemplate <class _Allocator>
2530bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20
253181ad6265SDimitry Andricvector<bool, _Allocator>::vector(vector&& __v, const __type_identity_t<allocator_type>& __a)
2532cb14a3feSDimitry Andric    : __begin_(nullptr), __size_(0), __cap_alloc_(0, __a) {
2533cb14a3feSDimitry Andric  if (__a == allocator_type(__v.__alloc())) {
25340b57cec5SDimitry Andric    this->__begin_ = __v.__begin_;
25350b57cec5SDimitry Andric    this->__size_  = __v.__size_;
25360b57cec5SDimitry Andric    this->__cap()  = __v.__cap();
25370b57cec5SDimitry Andric    __v.__begin_   = nullptr;
25380b57cec5SDimitry Andric    __v.__cap() = __v.__size_ = 0;
2539cb14a3feSDimitry Andric  } else if (__v.size() > 0) {
25400b57cec5SDimitry Andric    __vallocate(__v.size());
254106c3fb27SDimitry Andric    __construct_at_end(__v.begin(), __v.end(), __v.size());
25420b57cec5SDimitry Andric  }
25430b57cec5SDimitry Andric}
25440b57cec5SDimitry Andric
25450b57cec5SDimitry Andrictemplate <class _Allocator>
2546cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 vector<bool, _Allocator>&
25470b57cec5SDimitry Andricvector<bool, _Allocator>::operator=(vector&& __v)
2548*0fca6ea1SDimitry Andric    _NOEXCEPT_(__noexcept_move_assign_container<_Allocator, __alloc_traits>::value) {
2549cb14a3feSDimitry Andric  __move_assign(__v, integral_constant<bool, __storage_traits::propagate_on_container_move_assignment::value>());
25500b57cec5SDimitry Andric  return *this;
25510b57cec5SDimitry Andric}
25520b57cec5SDimitry Andric
25530b57cec5SDimitry Andrictemplate <class _Allocator>
2554cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<bool, _Allocator>::__move_assign(vector& __c, false_type) {
25550b57cec5SDimitry Andric  if (__alloc() != __c.__alloc())
25560b57cec5SDimitry Andric    assign(__c.begin(), __c.end());
25570b57cec5SDimitry Andric  else
25580b57cec5SDimitry Andric    __move_assign(__c, true_type());
25590b57cec5SDimitry Andric}
25600b57cec5SDimitry Andric
25610b57cec5SDimitry Andrictemplate <class _Allocator>
2562cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<bool, _Allocator>::__move_assign(vector& __c, true_type)
2563cb14a3feSDimitry Andric    _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value) {
25640b57cec5SDimitry Andric  __vdeallocate();
25650b57cec5SDimitry Andric  __move_assign_alloc(__c);
25660b57cec5SDimitry Andric  this->__begin_ = __c.__begin_;
25670b57cec5SDimitry Andric  this->__size_  = __c.__size_;
25680b57cec5SDimitry Andric  this->__cap()  = __c.__cap();
25690b57cec5SDimitry Andric  __c.__begin_   = nullptr;
25700b57cec5SDimitry Andric  __c.__cap() = __c.__size_ = 0;
25710b57cec5SDimitry Andric}
25720b57cec5SDimitry Andric
25730b57cec5SDimitry Andrictemplate <class _Allocator>
2574cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<bool, _Allocator>::assign(size_type __n, const value_type& __x) {
25750b57cec5SDimitry Andric  __size_ = 0;
2576cb14a3feSDimitry Andric  if (__n > 0) {
25770b57cec5SDimitry Andric    size_type __c = capacity();
25780b57cec5SDimitry Andric    if (__n <= __c)
25790b57cec5SDimitry Andric      __size_ = __n;
2580cb14a3feSDimitry Andric    else {
2581349cc55cSDimitry Andric      vector __v(get_allocator());
25820b57cec5SDimitry Andric      __v.reserve(__recommend(__n));
25830b57cec5SDimitry Andric      __v.__size_ = __n;
25840b57cec5SDimitry Andric      swap(__v);
25850b57cec5SDimitry Andric    }
2586bdd1243dSDimitry Andric    std::fill_n(begin(), __n, __x);
25870b57cec5SDimitry Andric  }
25880b57cec5SDimitry Andric}
25890b57cec5SDimitry Andric
25900b57cec5SDimitry Andrictemplate <class _Allocator>
25915f757f3fSDimitry Andrictemplate <class _InputIterator, __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value, int> >
2592cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<bool, _Allocator>::assign(_InputIterator __first, _InputIterator __last) {
259306c3fb27SDimitry Andric  __assign_with_sentinel(__first, __last);
259406c3fb27SDimitry Andric}
259506c3fb27SDimitry Andric
259606c3fb27SDimitry Andrictemplate <class _Allocator>
259706c3fb27SDimitry Andrictemplate <class _Iterator, class _Sentinel>
2598cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
2599cb14a3feSDimitry Andricvector<bool, _Allocator>::__assign_with_sentinel(_Iterator __first, _Sentinel __last) {
26000b57cec5SDimitry Andric  clear();
26010b57cec5SDimitry Andric  for (; __first != __last; ++__first)
26020b57cec5SDimitry Andric    push_back(*__first);
26030b57cec5SDimitry Andric}
26040b57cec5SDimitry Andric
26050b57cec5SDimitry Andrictemplate <class _Allocator>
26065f757f3fSDimitry Andrictemplate <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> >
2607cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<bool, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last) {
260806c3fb27SDimitry Andric  __assign_with_size(__first, __last, std::distance(__first, __last));
260906c3fb27SDimitry Andric}
261006c3fb27SDimitry Andric
261106c3fb27SDimitry Andrictemplate <class _Allocator>
261206c3fb27SDimitry Andrictemplate <class _ForwardIterator, class _Sentinel>
2613cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
2614cb14a3feSDimitry Andricvector<bool, _Allocator>::__assign_with_size(_ForwardIterator __first, _Sentinel __last, difference_type __ns) {
261506c3fb27SDimitry Andric  _LIBCPP_ASSERT_VALID_INPUT_RANGE(__ns >= 0, "invalid range specified");
261606c3fb27SDimitry Andric
26170b57cec5SDimitry Andric  clear();
261806c3fb27SDimitry Andric
26190b57cec5SDimitry Andric  const size_t __n = static_cast<size_type>(__ns);
2620cb14a3feSDimitry Andric  if (__n) {
2621cb14a3feSDimitry Andric    if (__n > capacity()) {
26220b57cec5SDimitry Andric      __vdeallocate();
26230b57cec5SDimitry Andric      __vallocate(__n);
26240b57cec5SDimitry Andric    }
262506c3fb27SDimitry Andric    __construct_at_end(__first, __last, __n);
26260b57cec5SDimitry Andric  }
26270b57cec5SDimitry Andric}
26280b57cec5SDimitry Andric
26290b57cec5SDimitry Andrictemplate <class _Allocator>
2630cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<bool, _Allocator>::reserve(size_type __n) {
2631cb14a3feSDimitry Andric  if (__n > capacity()) {
2632349cc55cSDimitry Andric    if (__n > max_size())
2633349cc55cSDimitry Andric      this->__throw_length_error();
2634349cc55cSDimitry Andric    vector __v(this->get_allocator());
26350b57cec5SDimitry Andric    __v.__vallocate(__n);
263606c3fb27SDimitry Andric    __v.__construct_at_end(this->begin(), this->end(), this->size());
26370b57cec5SDimitry Andric    swap(__v);
26380b57cec5SDimitry Andric  }
26390b57cec5SDimitry Andric}
26400b57cec5SDimitry Andric
26410b57cec5SDimitry Andrictemplate <class _Allocator>
2642cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<bool, _Allocator>::shrink_to_fit() _NOEXCEPT {
2643cb14a3feSDimitry Andric  if (__external_cap_to_internal(size()) > __cap()) {
264406c3fb27SDimitry Andric#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
2645cb14a3feSDimitry Andric    try {
264606c3fb27SDimitry Andric#endif // _LIBCPP_HAS_NO_EXCEPTIONS
26470b57cec5SDimitry Andric      vector(*this, allocator_type(__alloc())).swap(*this);
264806c3fb27SDimitry Andric#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
2649cb14a3feSDimitry Andric    } catch (...) {
26500b57cec5SDimitry Andric    }
265106c3fb27SDimitry Andric#endif // _LIBCPP_HAS_NO_EXCEPTIONS
26520b57cec5SDimitry Andric  }
26530b57cec5SDimitry Andric}
26540b57cec5SDimitry Andric
26550b57cec5SDimitry Andrictemplate <class _Allocator>
2656cb14a3feSDimitry Andrictypename vector<bool, _Allocator>::reference vector<bool, _Allocator>::at(size_type __n) {
26570b57cec5SDimitry Andric  if (__n >= size())
26580b57cec5SDimitry Andric    this->__throw_out_of_range();
26590b57cec5SDimitry Andric  return (*this)[__n];
26600b57cec5SDimitry Andric}
26610b57cec5SDimitry Andric
26620b57cec5SDimitry Andrictemplate <class _Allocator>
2663cb14a3feSDimitry Andrictypename vector<bool, _Allocator>::const_reference vector<bool, _Allocator>::at(size_type __n) const {
26640b57cec5SDimitry Andric  if (__n >= size())
26650b57cec5SDimitry Andric    this->__throw_out_of_range();
26660b57cec5SDimitry Andric  return (*this)[__n];
26670b57cec5SDimitry Andric}
26680b57cec5SDimitry Andric
26690b57cec5SDimitry Andrictemplate <class _Allocator>
2670cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<bool, _Allocator>::push_back(const value_type& __x) {
26710b57cec5SDimitry Andric  if (this->__size_ == this->capacity())
26720b57cec5SDimitry Andric    reserve(__recommend(this->__size_ + 1));
26730b57cec5SDimitry Andric  ++this->__size_;
26740b57cec5SDimitry Andric  back() = __x;
26750b57cec5SDimitry Andric}
26760b57cec5SDimitry Andric
26770b57cec5SDimitry Andrictemplate <class _Allocator>
2678bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<bool, _Allocator>::iterator
2679cb14a3feSDimitry Andricvector<bool, _Allocator>::insert(const_iterator __position, const value_type& __x) {
26800b57cec5SDimitry Andric  iterator __r;
2681cb14a3feSDimitry Andric  if (size() < capacity()) {
26820b57cec5SDimitry Andric    const_iterator __old_end = end();
26830b57cec5SDimitry Andric    ++__size_;
2684bdd1243dSDimitry Andric    std::copy_backward(__position, __old_end, end());
26850b57cec5SDimitry Andric    __r = __const_iterator_cast(__position);
2686cb14a3feSDimitry Andric  } else {
2687349cc55cSDimitry Andric    vector __v(get_allocator());
26880b57cec5SDimitry Andric    __v.reserve(__recommend(__size_ + 1));
26890b57cec5SDimitry Andric    __v.__size_ = __size_ + 1;
2690bdd1243dSDimitry Andric    __r         = std::copy(cbegin(), __position, __v.begin());
2691bdd1243dSDimitry Andric    std::copy_backward(__position, cend(), __v.end());
26920b57cec5SDimitry Andric    swap(__v);
26930b57cec5SDimitry Andric  }
26940b57cec5SDimitry Andric  *__r = __x;
26950b57cec5SDimitry Andric  return __r;
26960b57cec5SDimitry Andric}
26970b57cec5SDimitry Andric
26980b57cec5SDimitry Andrictemplate <class _Allocator>
2699bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<bool, _Allocator>::iterator
2700cb14a3feSDimitry Andricvector<bool, _Allocator>::insert(const_iterator __position, size_type __n, const value_type& __x) {
27010b57cec5SDimitry Andric  iterator __r;
27020b57cec5SDimitry Andric  size_type __c = capacity();
2703cb14a3feSDimitry Andric  if (__n <= __c && size() <= __c - __n) {
27040b57cec5SDimitry Andric    const_iterator __old_end = end();
27050b57cec5SDimitry Andric    __size_ += __n;
2706bdd1243dSDimitry Andric    std::copy_backward(__position, __old_end, end());
27070b57cec5SDimitry Andric    __r = __const_iterator_cast(__position);
2708cb14a3feSDimitry Andric  } else {
2709349cc55cSDimitry Andric    vector __v(get_allocator());
27100b57cec5SDimitry Andric    __v.reserve(__recommend(__size_ + __n));
27110b57cec5SDimitry Andric    __v.__size_ = __size_ + __n;
2712bdd1243dSDimitry Andric    __r         = std::copy(cbegin(), __position, __v.begin());
2713bdd1243dSDimitry Andric    std::copy_backward(__position, cend(), __v.end());
27140b57cec5SDimitry Andric    swap(__v);
27150b57cec5SDimitry Andric  }
2716bdd1243dSDimitry Andric  std::fill_n(__r, __n, __x);
27170b57cec5SDimitry Andric  return __r;
27180b57cec5SDimitry Andric}
27190b57cec5SDimitry Andric
27200b57cec5SDimitry Andrictemplate <class _Allocator>
27215f757f3fSDimitry Andrictemplate <class _InputIterator, __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value, int> >
2722cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<bool, _Allocator>::iterator
2723cb14a3feSDimitry Andricvector<bool, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last) {
272406c3fb27SDimitry Andric  return __insert_with_sentinel(__position, __first, __last);
272506c3fb27SDimitry Andric}
272606c3fb27SDimitry Andric
272706c3fb27SDimitry Andrictemplate <class _Allocator>
272806c3fb27SDimitry Andrictemplate <class _InputIterator, class _Sentinel>
2729cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI typename vector<bool, _Allocator>::iterator
273006c3fb27SDimitry Andricvector<bool, _Allocator>::__insert_with_sentinel(const_iterator __position, _InputIterator __first, _Sentinel __last) {
27310b57cec5SDimitry Andric  difference_type __off = __position - begin();
27320b57cec5SDimitry Andric  iterator __p          = __const_iterator_cast(__position);
27330b57cec5SDimitry Andric  iterator __old_end    = end();
2734cb14a3feSDimitry Andric  for (; size() != capacity() && __first != __last; ++__first) {
27350b57cec5SDimitry Andric    ++this->__size_;
27360b57cec5SDimitry Andric    back() = *__first;
27370b57cec5SDimitry Andric  }
2738349cc55cSDimitry Andric  vector __v(get_allocator());
2739cb14a3feSDimitry Andric  if (__first != __last) {
274006c3fb27SDimitry Andric#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
2741cb14a3feSDimitry Andric    try {
274206c3fb27SDimitry Andric#endif // _LIBCPP_HAS_NO_EXCEPTIONS
274306c3fb27SDimitry Andric      __v.__assign_with_sentinel(std::move(__first), std::move(__last));
27440b57cec5SDimitry Andric      difference_type __old_size = static_cast<difference_type>(__old_end - begin());
27450b57cec5SDimitry Andric      difference_type __old_p    = __p - begin();
27460b57cec5SDimitry Andric      reserve(__recommend(size() + __v.size()));
27470b57cec5SDimitry Andric      __p       = begin() + __old_p;
27480b57cec5SDimitry Andric      __old_end = begin() + __old_size;
274906c3fb27SDimitry Andric#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
2750cb14a3feSDimitry Andric    } catch (...) {
27510b57cec5SDimitry Andric      erase(__old_end, end());
27520b57cec5SDimitry Andric      throw;
27530b57cec5SDimitry Andric    }
275406c3fb27SDimitry Andric#endif // _LIBCPP_HAS_NO_EXCEPTIONS
27550b57cec5SDimitry Andric  }
2756bdd1243dSDimitry Andric  __p = std::rotate(__p, __old_end, end());
27570b57cec5SDimitry Andric  insert(__p, __v.begin(), __v.end());
27580b57cec5SDimitry Andric  return begin() + __off;
27590b57cec5SDimitry Andric}
27600b57cec5SDimitry Andric
27610b57cec5SDimitry Andrictemplate <class _Allocator>
27625f757f3fSDimitry Andrictemplate <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> >
2763cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<bool, _Allocator>::iterator
2764cb14a3feSDimitry Andricvector<bool, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last) {
276506c3fb27SDimitry Andric  return __insert_with_size(__position, __first, __last, std::distance(__first, __last));
276606c3fb27SDimitry Andric}
276706c3fb27SDimitry Andric
276806c3fb27SDimitry Andrictemplate <class _Allocator>
276906c3fb27SDimitry Andrictemplate <class _ForwardIterator, class _Sentinel>
2770cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI typename vector<bool, _Allocator>::iterator
2771cb14a3feSDimitry Andricvector<bool, _Allocator>::__insert_with_size(
2772cb14a3feSDimitry Andric    const_iterator __position, _ForwardIterator __first, _Sentinel __last, difference_type __n_signed) {
277306c3fb27SDimitry Andric  _LIBCPP_ASSERT_VALID_INPUT_RANGE(__n_signed >= 0, "invalid range specified");
27740b57cec5SDimitry Andric  const size_type __n = static_cast<size_type>(__n_signed);
27750b57cec5SDimitry Andric  iterator __r;
27760b57cec5SDimitry Andric  size_type __c = capacity();
2777cb14a3feSDimitry Andric  if (__n <= __c && size() <= __c - __n) {
27780b57cec5SDimitry Andric    const_iterator __old_end = end();
27790b57cec5SDimitry Andric    __size_ += __n;
2780bdd1243dSDimitry Andric    std::copy_backward(__position, __old_end, end());
27810b57cec5SDimitry Andric    __r = __const_iterator_cast(__position);
2782cb14a3feSDimitry Andric  } else {
2783349cc55cSDimitry Andric    vector __v(get_allocator());
27840b57cec5SDimitry Andric    __v.reserve(__recommend(__size_ + __n));
27850b57cec5SDimitry Andric    __v.__size_ = __size_ + __n;
2786bdd1243dSDimitry Andric    __r         = std::copy(cbegin(), __position, __v.begin());
2787bdd1243dSDimitry Andric    std::copy_backward(__position, cend(), __v.end());
27880b57cec5SDimitry Andric    swap(__v);
27890b57cec5SDimitry Andric  }
279006c3fb27SDimitry Andric  std::__copy<_ClassicAlgPolicy>(__first, __last, __r);
27910b57cec5SDimitry Andric  return __r;
27920b57cec5SDimitry Andric}
27930b57cec5SDimitry Andric
27940b57cec5SDimitry Andrictemplate <class _Allocator>
2795cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<bool, _Allocator>::iterator
2796cb14a3feSDimitry Andricvector<bool, _Allocator>::erase(const_iterator __position) {
27970b57cec5SDimitry Andric  iterator __r = __const_iterator_cast(__position);
2798bdd1243dSDimitry Andric  std::copy(__position + 1, this->cend(), __r);
27990b57cec5SDimitry Andric  --__size_;
28000b57cec5SDimitry Andric  return __r;
28010b57cec5SDimitry Andric}
28020b57cec5SDimitry Andric
28030b57cec5SDimitry Andrictemplate <class _Allocator>
2804cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<bool, _Allocator>::iterator
2805cb14a3feSDimitry Andricvector<bool, _Allocator>::erase(const_iterator __first, const_iterator __last) {
28060b57cec5SDimitry Andric  iterator __r        = __const_iterator_cast(__first);
28070b57cec5SDimitry Andric  difference_type __d = __last - __first;
2808bdd1243dSDimitry Andric  std::copy(__last, this->cend(), __r);
28090b57cec5SDimitry Andric  __size_ -= __d;
28100b57cec5SDimitry Andric  return __r;
28110b57cec5SDimitry Andric}
28120b57cec5SDimitry Andric
28130b57cec5SDimitry Andrictemplate <class _Allocator>
2814cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<bool, _Allocator>::swap(vector& __x)
28150b57cec5SDimitry Andric#if _LIBCPP_STD_VER >= 14
28160b57cec5SDimitry Andric    _NOEXCEPT
28170b57cec5SDimitry Andric#else
2818*0fca6ea1SDimitry Andric    _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || __is_nothrow_swappable_v<allocator_type>)
28190b57cec5SDimitry Andric#endif
28200b57cec5SDimitry Andric{
2821bdd1243dSDimitry Andric  std::swap(this->__begin_, __x.__begin_);
2822bdd1243dSDimitry Andric  std::swap(this->__size_, __x.__size_);
2823bdd1243dSDimitry Andric  std::swap(this->__cap(), __x.__cap());
2824cb14a3feSDimitry Andric  std::__swap_allocator(
2825cb14a3feSDimitry Andric      this->__alloc(), __x.__alloc(), integral_constant<bool, __alloc_traits::propagate_on_container_swap::value>());
28260b57cec5SDimitry Andric}
28270b57cec5SDimitry Andric
28280b57cec5SDimitry Andrictemplate <class _Allocator>
2829cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<bool, _Allocator>::resize(size_type __sz, value_type __x) {
28300b57cec5SDimitry Andric  size_type __cs = size();
2831cb14a3feSDimitry Andric  if (__cs < __sz) {
28320b57cec5SDimitry Andric    iterator __r;
28330b57cec5SDimitry Andric    size_type __c = capacity();
28340b57cec5SDimitry Andric    size_type __n = __sz - __cs;
2835cb14a3feSDimitry Andric    if (__n <= __c && __cs <= __c - __n) {
28360b57cec5SDimitry Andric      __r = end();
28370b57cec5SDimitry Andric      __size_ += __n;
2838cb14a3feSDimitry Andric    } else {
2839349cc55cSDimitry Andric      vector __v(get_allocator());
28400b57cec5SDimitry Andric      __v.reserve(__recommend(__size_ + __n));
28410b57cec5SDimitry Andric      __v.__size_ = __size_ + __n;
2842bdd1243dSDimitry Andric      __r         = std::copy(cbegin(), cend(), __v.begin());
28430b57cec5SDimitry Andric      swap(__v);
28440b57cec5SDimitry Andric    }
2845bdd1243dSDimitry Andric    std::fill_n(__r, __n, __x);
2846cb14a3feSDimitry Andric  } else
28470b57cec5SDimitry Andric    __size_ = __sz;
28480b57cec5SDimitry Andric}
28490b57cec5SDimitry Andric
28500b57cec5SDimitry Andrictemplate <class _Allocator>
2851cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<bool, _Allocator>::flip() _NOEXCEPT {
28520b57cec5SDimitry Andric  // do middle whole words
28530b57cec5SDimitry Andric  size_type __n         = __size_;
28540b57cec5SDimitry Andric  __storage_pointer __p = __begin_;
28550b57cec5SDimitry Andric  for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
28560b57cec5SDimitry Andric    *__p = ~*__p;
28570b57cec5SDimitry Andric  // do last partial word
2858cb14a3feSDimitry Andric  if (__n > 0) {
28590b57cec5SDimitry Andric    __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
28600b57cec5SDimitry Andric    __storage_type __b = *__p & __m;
28610b57cec5SDimitry Andric    *__p &= ~__m;
28620b57cec5SDimitry Andric    *__p |= ~__b & __m;
28630b57cec5SDimitry Andric  }
28640b57cec5SDimitry Andric}
28650b57cec5SDimitry Andric
28660b57cec5SDimitry Andrictemplate <class _Allocator>
2867cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 bool vector<bool, _Allocator>::__invariants() const {
2868cb14a3feSDimitry Andric  if (this->__begin_ == nullptr) {
28690b57cec5SDimitry Andric    if (this->__size_ != 0 || this->__cap() != 0)
28700b57cec5SDimitry Andric      return false;
2871cb14a3feSDimitry Andric  } else {
28720b57cec5SDimitry Andric    if (this->__cap() == 0)
28730b57cec5SDimitry Andric      return false;
28740b57cec5SDimitry Andric    if (this->__size_ > this->capacity())
28750b57cec5SDimitry Andric      return false;
28760b57cec5SDimitry Andric  }
28770b57cec5SDimitry Andric  return true;
28780b57cec5SDimitry Andric}
28790b57cec5SDimitry Andric
28800b57cec5SDimitry Andrictemplate <class _Allocator>
2881cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 size_t vector<bool, _Allocator>::__hash_code() const _NOEXCEPT {
28820b57cec5SDimitry Andric  size_t __h = 0;
28830b57cec5SDimitry Andric  // do middle whole words
28840b57cec5SDimitry Andric  size_type __n         = __size_;
28850b57cec5SDimitry Andric  __storage_pointer __p = __begin_;
28860b57cec5SDimitry Andric  for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
28870b57cec5SDimitry Andric    __h ^= *__p;
28880b57cec5SDimitry Andric  // do last partial word
2889cb14a3feSDimitry Andric  if (__n > 0) {
28900b57cec5SDimitry Andric    const __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
28910b57cec5SDimitry Andric    __h ^= *__p & __m;
28920b57cec5SDimitry Andric  }
28930b57cec5SDimitry Andric  return __h;
28940b57cec5SDimitry Andric}
28950b57cec5SDimitry Andric
28960b57cec5SDimitry Andrictemplate <class _Allocator>
28970b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS hash<vector<bool, _Allocator> >
2898cb14a3feSDimitry Andric    : public __unary_function<vector<bool, _Allocator>, size_t> {
2899cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_t
2900cb14a3feSDimitry Andric  operator()(const vector<bool, _Allocator>& __vec) const _NOEXCEPT {
2901cb14a3feSDimitry Andric    return __vec.__hash_code();
2902cb14a3feSDimitry Andric  }
29030b57cec5SDimitry Andric};
29040b57cec5SDimitry Andric
29050b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
2906cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI bool
2907cb14a3feSDimitry Andricoperator==(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y) {
29080b57cec5SDimitry Andric  const typename vector<_Tp, _Allocator>::size_type __sz = __x.size();
2909bdd1243dSDimitry Andric  return __sz == __y.size() && std::equal(__x.begin(), __x.end(), __y.begin());
29100b57cec5SDimitry Andric}
29110b57cec5SDimitry Andric
291206c3fb27SDimitry Andric#if _LIBCPP_STD_VER <= 17
291306c3fb27SDimitry Andric
29140b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
2915cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool operator!=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y) {
29160b57cec5SDimitry Andric  return !(__x == __y);
29170b57cec5SDimitry Andric}
29180b57cec5SDimitry Andric
29190b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
2920cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool operator<(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y) {
2921bdd1243dSDimitry Andric  return std::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
29220b57cec5SDimitry Andric}
29230b57cec5SDimitry Andric
29240b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
2925cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool operator>(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y) {
29260b57cec5SDimitry Andric  return __y < __x;
29270b57cec5SDimitry Andric}
29280b57cec5SDimitry Andric
29290b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
2930cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool operator>=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y) {
29310b57cec5SDimitry Andric  return !(__x < __y);
29320b57cec5SDimitry Andric}
29330b57cec5SDimitry Andric
29340b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
2935cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool operator<=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y) {
29360b57cec5SDimitry Andric  return !(__y < __x);
29370b57cec5SDimitry Andric}
29380b57cec5SDimitry Andric
293906c3fb27SDimitry Andric#else // _LIBCPP_STD_VER <= 17
294006c3fb27SDimitry Andric
294106c3fb27SDimitry Andrictemplate <class _Tp, class _Allocator>
294206c3fb27SDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr __synth_three_way_result<_Tp>
294306c3fb27SDimitry Andricoperator<=>(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y) {
294406c3fb27SDimitry Andric  return std::lexicographical_compare_three_way(
2945*0fca6ea1SDimitry Andric      __x.begin(), __x.end(), __y.begin(), __y.end(), std::__synth_three_way);
294606c3fb27SDimitry Andric}
294706c3fb27SDimitry Andric
294806c3fb27SDimitry Andric#endif // _LIBCPP_STD_VER <= 17
294906c3fb27SDimitry Andric
29500b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
2951cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI void
2952cb14a3feSDimitry Andricswap(vector<_Tp, _Allocator>& __x, vector<_Tp, _Allocator>& __y) _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y))) {
29530b57cec5SDimitry Andric  __x.swap(__y);
29540b57cec5SDimitry Andric}
29550b57cec5SDimitry Andric
295606c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 20
29570b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator, class _Up>
2958cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI typename vector<_Tp, _Allocator>::size_type
29595ffd83dbSDimitry Andricerase(vector<_Tp, _Allocator>& __c, const _Up& __v) {
29605ffd83dbSDimitry Andric  auto __old_size = __c.size();
2961bdd1243dSDimitry Andric  __c.erase(std::remove(__c.begin(), __c.end(), __v), __c.end());
29625ffd83dbSDimitry Andric  return __old_size - __c.size();
29635ffd83dbSDimitry Andric}
29640b57cec5SDimitry Andric
29650b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator, class _Predicate>
2966cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI typename vector<_Tp, _Allocator>::size_type
29675ffd83dbSDimitry Andricerase_if(vector<_Tp, _Allocator>& __c, _Predicate __pred) {
29685ffd83dbSDimitry Andric  auto __old_size = __c.size();
2969bdd1243dSDimitry Andric  __c.erase(std::remove_if(__c.begin(), __c.end(), __pred), __c.end());
29705ffd83dbSDimitry Andric  return __old_size - __c.size();
29715ffd83dbSDimitry Andric}
297281ad6265SDimitry Andric
297381ad6265SDimitry Andrictemplate <>
2974bdd1243dSDimitry Andricinline constexpr bool __format::__enable_insertable<vector<char>> = true;
297581ad6265SDimitry Andric#  ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
297681ad6265SDimitry Andrictemplate <>
2977bdd1243dSDimitry Andricinline constexpr bool __format::__enable_insertable<vector<wchar_t>> = true;
29780b57cec5SDimitry Andric#  endif
29790b57cec5SDimitry Andric
298006c3fb27SDimitry Andric#endif // _LIBCPP_STD_VER >= 20
298181ad6265SDimitry Andric
298206c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 23
298306c3fb27SDimitry Andrictemplate <class _Tp, class _CharT>
2984bdd1243dSDimitry Andric// Since is-vector-bool-reference is only used once it's inlined here.
2985bdd1243dSDimitry Andric  requires same_as<typename _Tp::__container, vector<bool, typename _Tp::__container::allocator_type>>
298606c3fb27SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS formatter<_Tp, _CharT> {
2987bdd1243dSDimitry Andricprivate:
298806c3fb27SDimitry Andric  formatter<bool, _CharT> __underlying_;
2989bdd1243dSDimitry Andric
2990bdd1243dSDimitry Andricpublic:
2991bdd1243dSDimitry Andric  template <class _ParseContext>
2992bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) {
2993bdd1243dSDimitry Andric    return __underlying_.parse(__ctx);
2994bdd1243dSDimitry Andric  }
2995bdd1243dSDimitry Andric
2996bdd1243dSDimitry Andric  template <class _FormatContext>
2997bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator format(const _Tp& __ref, _FormatContext& __ctx) const {
2998bdd1243dSDimitry Andric    return __underlying_.format(__ref, __ctx);
2999bdd1243dSDimitry Andric  }
3000bdd1243dSDimitry Andric};
300106c3fb27SDimitry Andric#endif // _LIBCPP_STD_VER >= 23
3002bdd1243dSDimitry Andric
30030b57cec5SDimitry Andric_LIBCPP_END_NAMESPACE_STD
30040b57cec5SDimitry Andric
300506c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 17
3006bdd1243dSDimitry Andric_LIBCPP_BEGIN_NAMESPACE_STD
3007bdd1243dSDimitry Andricnamespace pmr {
3008bdd1243dSDimitry Andrictemplate <class _ValueT>
300906c3fb27SDimitry Andricusing vector _LIBCPP_AVAILABILITY_PMR = std::vector<_ValueT, polymorphic_allocator<_ValueT>>;
3010bdd1243dSDimitry Andric} // namespace pmr
3011bdd1243dSDimitry Andric_LIBCPP_END_NAMESPACE_STD
3012bdd1243dSDimitry Andric#endif
3013bdd1243dSDimitry Andric
30140b57cec5SDimitry Andric_LIBCPP_POP_MACROS
30150b57cec5SDimitry Andric
3016bdd1243dSDimitry Andric#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
3017bdd1243dSDimitry Andric#  include <algorithm>
3018bdd1243dSDimitry Andric#  include <atomic>
3019bdd1243dSDimitry Andric#  include <concepts>
302006c3fb27SDimitry Andric#  include <cstdlib>
3021*0fca6ea1SDimitry Andric#  include <iosfwd>
3022*0fca6ea1SDimitry Andric#  if !defined(_LIBCPP_HAS_NO_LOCALIZATION)
3023*0fca6ea1SDimitry Andric#    include <locale>
3024*0fca6ea1SDimitry Andric#  endif
3025*0fca6ea1SDimitry Andric#  include <tuple>
302606c3fb27SDimitry Andric#  include <type_traits>
3027bdd1243dSDimitry Andric#  include <typeinfo>
3028bdd1243dSDimitry Andric#  include <utility>
3029bdd1243dSDimitry Andric#endif
3030bdd1243dSDimitry Andric
30310b57cec5SDimitry Andric#endif // _LIBCPP_VECTOR
3032