blob: f92caf941e8d8c198292bc65971ba08f494c1af8 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
Louis Dionne9bd93882021-11-17 16:25:01 -05002//===----------------------------------------------------------------------===//
Howard Hinnantc51e1022010-05-11 19:42:16 +00003//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_VECTOR
11#define _LIBCPP_VECTOR
12
13/*
14 vector synopsis
15
16namespace std
17{
18
Howard Hinnant3b6579a2010-08-22 00:02:43 +000019template <class T, class Allocator = allocator<T> >
Howard Hinnantc51e1022010-05-11 19:42:16 +000020class vector
Howard Hinnant3b6579a2010-08-22 00:02:43 +000021{
22public:
23 typedef T value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +000024 typedef Allocator allocator_type;
25 typedef typename allocator_type::reference reference;
26 typedef typename allocator_type::const_reference const_reference;
27 typedef implementation-defined iterator;
28 typedef implementation-defined const_iterator;
29 typedef typename allocator_type::size_type size_type;
30 typedef typename allocator_type::difference_type difference_type;
31 typedef typename allocator_type::pointer pointer;
32 typedef typename allocator_type::const_pointer const_pointer;
33 typedef std::reverse_iterator<iterator> reverse_iterator;
34 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
35
Howard Hinnant1c936782011-06-03 19:40:40 +000036 vector()
37 noexcept(is_nothrow_default_constructible<allocator_type>::value);
38 explicit vector(const allocator_type&);
Howard Hinnantc51e1022010-05-11 19:42:16 +000039 explicit vector(size_type n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +000040 explicit vector(size_type n, const allocator_type&); // C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +000041 vector(size_type n, const value_type& value, const allocator_type& = allocator_type());
42 template <class InputIterator>
43 vector(InputIterator first, InputIterator last, const allocator_type& = allocator_type());
44 vector(const vector& x);
Howard Hinnant1c936782011-06-03 19:40:40 +000045 vector(vector&& x)
46 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +000047 vector(initializer_list<value_type> il);
48 vector(initializer_list<value_type> il, const allocator_type& a);
49 ~vector();
50 vector& operator=(const vector& x);
Howard Hinnant1c936782011-06-03 19:40:40 +000051 vector& operator=(vector&& x)
52 noexcept(
Marshall Clow2fe8a8d2015-08-18 18:57:00 +000053 allocator_type::propagate_on_container_move_assignment::value ||
54 allocator_type::is_always_equal::value); // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +000055 vector& operator=(initializer_list<value_type> il);
56 template <class InputIterator>
57 void assign(InputIterator first, InputIterator last);
58 void assign(size_type n, const value_type& u);
59 void assign(initializer_list<value_type> il);
60
Howard Hinnant1c936782011-06-03 19:40:40 +000061 allocator_type get_allocator() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000062
Howard Hinnant1c936782011-06-03 19:40:40 +000063 iterator begin() noexcept;
64 const_iterator begin() const noexcept;
65 iterator end() noexcept;
66 const_iterator end() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000067
Howard Hinnant1c936782011-06-03 19:40:40 +000068 reverse_iterator rbegin() noexcept;
69 const_reverse_iterator rbegin() const noexcept;
70 reverse_iterator rend() noexcept;
71 const_reverse_iterator rend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000072
Howard Hinnant1c936782011-06-03 19:40:40 +000073 const_iterator cbegin() const noexcept;
74 const_iterator cend() const noexcept;
75 const_reverse_iterator crbegin() const noexcept;
76 const_reverse_iterator crend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000077
Howard Hinnant1c936782011-06-03 19:40:40 +000078 size_type size() const noexcept;
79 size_type max_size() const noexcept;
80 size_type capacity() const noexcept;
81 bool empty() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000082 void reserve(size_type n);
Howard Hinnant1c936782011-06-03 19:40:40 +000083 void shrink_to_fit() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000084
85 reference operator[](size_type n);
86 const_reference operator[](size_type n) const;
87 reference at(size_type n);
88 const_reference at(size_type n) const;
89
90 reference front();
91 const_reference front() const;
92 reference back();
93 const_reference back() const;
94
Howard Hinnant1c936782011-06-03 19:40:40 +000095 value_type* data() noexcept;
96 const value_type* data() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000097
98 void push_back(const value_type& x);
99 void push_back(value_type&& x);
100 template <class... Args>
Marshall Clowea52cc42017-01-24 23:09:12 +0000101 reference emplace_back(Args&&... args); // reference in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000102 void pop_back();
103
104 template <class... Args> iterator emplace(const_iterator position, Args&&... args);
105 iterator insert(const_iterator position, const value_type& x);
106 iterator insert(const_iterator position, value_type&& x);
107 iterator insert(const_iterator position, size_type n, const value_type& x);
108 template <class InputIterator>
109 iterator insert(const_iterator position, InputIterator first, InputIterator last);
110 iterator insert(const_iterator position, initializer_list<value_type> il);
111
112 iterator erase(const_iterator position);
113 iterator erase(const_iterator first, const_iterator last);
114
Howard Hinnant1c936782011-06-03 19:40:40 +0000115 void clear() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000116
117 void resize(size_type sz);
118 void resize(size_type sz, const value_type& c);
119
Howard Hinnant1c936782011-06-03 19:40:40 +0000120 void swap(vector&)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000121 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
122 allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000123
124 bool __invariants() const;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000125};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000126
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000127template <class Allocator = allocator<T> >
Howard Hinnantc51e1022010-05-11 19:42:16 +0000128class vector<bool, Allocator>
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000129{
130public:
131 typedef bool value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000132 typedef Allocator allocator_type;
133 typedef implementation-defined iterator;
134 typedef implementation-defined const_iterator;
135 typedef typename allocator_type::size_type size_type;
136 typedef typename allocator_type::difference_type difference_type;
137 typedef iterator pointer;
138 typedef const_iterator const_pointer;
139 typedef std::reverse_iterator<iterator> reverse_iterator;
140 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
141
142 class reference
143 {
144 public:
Howard Hinnant1c936782011-06-03 19:40:40 +0000145 reference(const reference&) noexcept;
146 operator bool() const noexcept;
Louis Dionned9f528e2021-06-29 13:52:26 -0400147 reference& operator=(bool x) noexcept;
Howard Hinnant1c936782011-06-03 19:40:40 +0000148 reference& operator=(const reference& x) noexcept;
149 iterator operator&() const noexcept;
150 void flip() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000151 };
152
153 class const_reference
154 {
155 public:
Howard Hinnant1c936782011-06-03 19:40:40 +0000156 const_reference(const reference&) noexcept;
157 operator bool() const noexcept;
158 const_iterator operator&() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000159 };
160
Howard Hinnant1c936782011-06-03 19:40:40 +0000161 vector()
162 noexcept(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnantc2734962011-09-02 20:42:31 +0000163 explicit vector(const allocator_type&);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +0000164 explicit vector(size_type n, const allocator_type& a = allocator_type()); // C++14
165 vector(size_type n, const value_type& value, const allocator_type& = allocator_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000166 template <class InputIterator>
167 vector(InputIterator first, InputIterator last, const allocator_type& = allocator_type());
168 vector(const vector& x);
Howard Hinnant1c936782011-06-03 19:40:40 +0000169 vector(vector&& x)
170 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000171 vector(initializer_list<value_type> il);
172 vector(initializer_list<value_type> il, const allocator_type& a);
173 ~vector();
174 vector& operator=(const vector& x);
Howard Hinnant1c936782011-06-03 19:40:40 +0000175 vector& operator=(vector&& x)
176 noexcept(
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000177 allocator_type::propagate_on_container_move_assignment::value ||
178 allocator_type::is_always_equal::value); // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000179 vector& operator=(initializer_list<value_type> il);
180 template <class InputIterator>
181 void assign(InputIterator first, InputIterator last);
182 void assign(size_type n, const value_type& u);
183 void assign(initializer_list<value_type> il);
184
Howard Hinnant1c936782011-06-03 19:40:40 +0000185 allocator_type get_allocator() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000186
Howard Hinnant1c936782011-06-03 19:40:40 +0000187 iterator begin() noexcept;
188 const_iterator begin() const noexcept;
189 iterator end() noexcept;
190 const_iterator end() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000191
Howard Hinnant1c936782011-06-03 19:40:40 +0000192 reverse_iterator rbegin() noexcept;
193 const_reverse_iterator rbegin() const noexcept;
194 reverse_iterator rend() noexcept;
195 const_reverse_iterator rend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000196
Howard Hinnant1c936782011-06-03 19:40:40 +0000197 const_iterator cbegin() const noexcept;
198 const_iterator cend() const noexcept;
199 const_reverse_iterator crbegin() const noexcept;
200 const_reverse_iterator crend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000201
Howard Hinnant1c936782011-06-03 19:40:40 +0000202 size_type size() const noexcept;
203 size_type max_size() const noexcept;
204 size_type capacity() const noexcept;
205 bool empty() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000206 void reserve(size_type n);
Howard Hinnant1c936782011-06-03 19:40:40 +0000207 void shrink_to_fit() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000208
209 reference operator[](size_type n);
210 const_reference operator[](size_type n) const;
211 reference at(size_type n);
212 const_reference at(size_type n) const;
213
214 reference front();
215 const_reference front() const;
216 reference back();
217 const_reference back() const;
218
219 void push_back(const value_type& x);
Marshall Clowea52cc42017-01-24 23:09:12 +0000220 template <class... Args> reference emplace_back(Args&&... args); // C++14; reference in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000221 void pop_back();
222
Marshall Clowc46bb8e2013-08-13 23:54:12 +0000223 template <class... Args> iterator emplace(const_iterator position, Args&&... args); // C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000224 iterator insert(const_iterator position, const value_type& x);
225 iterator insert(const_iterator position, size_type n, const value_type& x);
226 template <class InputIterator>
227 iterator insert(const_iterator position, InputIterator first, InputIterator last);
228 iterator insert(const_iterator position, initializer_list<value_type> il);
229
230 iterator erase(const_iterator position);
231 iterator erase(const_iterator first, const_iterator last);
232
Howard Hinnant1c936782011-06-03 19:40:40 +0000233 void clear() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000234
235 void resize(size_type sz);
236 void resize(size_type sz, value_type x);
237
Howard Hinnant1c936782011-06-03 19:40:40 +0000238 void swap(vector&)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000239 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
240 allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnant1c936782011-06-03 19:40:40 +0000241 void flip() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000242
243 bool __invariants() const;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000244};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000245
Marshall Clowf0ca1492018-05-21 21:30:12 +0000246template <class InputIterator, class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
247 vector(InputIterator, InputIterator, Allocator = Allocator())
Konstantin Varlamov53b543c2021-11-09 09:21:02 -0800248 -> vector<typename iterator_traits<InputIterator>::value_type, Allocator>; // C++17
Marshall Clowf0ca1492018-05-21 21:30:12 +0000249
Howard Hinnantc51e1022010-05-11 19:42:16 +0000250template <class Allocator> struct hash<std::vector<bool, Allocator>>;
251
252template <class T, class Allocator> bool operator==(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
253template <class T, class Allocator> bool operator< (const vector<T,Allocator>& x, const vector<T,Allocator>& y);
254template <class T, class Allocator> bool operator!=(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
255template <class T, class Allocator> bool operator> (const vector<T,Allocator>& x, const vector<T,Allocator>& y);
256template <class T, class Allocator> bool operator>=(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
257template <class T, class Allocator> bool operator<=(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
258
Howard Hinnant1c936782011-06-03 19:40:40 +0000259template <class T, class Allocator>
260void swap(vector<T,Allocator>& x, vector<T,Allocator>& y)
261 noexcept(noexcept(x.swap(y)));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000262
Marshall Clow29b53f22018-12-14 18:49:35 +0000263template <class T, class Allocator, class U>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200264typename vector<T, Allocator>::size_type
265erase(vector<T, Allocator>& c, const U& value); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000266template <class T, class Allocator, class Predicate>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200267typename vector<T, Allocator>::size_type
268erase_if(vector<T, Allocator>& c, Predicate pred); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000269
Howard Hinnantc51e1022010-05-11 19:42:16 +0000270} // std
271
272*/
273
Howard Hinnantc51e1022010-05-11 19:42:16 +0000274#include <__bit_reference>
Arthur O'Dwyer65077c02022-01-07 09:45:05 -0500275#include <__config>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400276#include <__debug>
277#include <__functional_base>
Konstantin Varlamov53b543c2021-11-09 09:21:02 -0800278#include <__iterator/iterator_traits.h>
Louis Dionne77249522021-06-11 09:55:11 -0400279#include <__iterator/wrap_iter.h>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400280#include <__split_buffer>
Christopher Di Bella41f26e82021-06-05 02:47:47 +0000281#include <__utility/forward.h>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400282#include <algorithm>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000283#include <climits>
Arthur O'Dwyer7deec122021-03-24 18:19:12 -0400284#include <compare>
Louis Dionned24191c2021-08-19 12:39:16 -0400285#include <cstdlib>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400286#include <cstring>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000287#include <initializer_list>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400288#include <iosfwd> // for forward declaration of vector
289#include <limits>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000290#include <memory>
291#include <stdexcept>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400292#include <type_traits>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000293#include <version>
Howard Hinnante6ff0b62013-08-02 00:26:35 +0000294
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000295#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Arthur O'Dwyer6eeaa002022-02-01 20:16:40 -0500296# pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000297#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000298
Eric Fiselierf4433a32017-05-31 22:07:49 +0000299_LIBCPP_PUSH_MACROS
300#include <__undef_macros>
301
302
Howard Hinnantc51e1022010-05-11 19:42:16 +0000303_LIBCPP_BEGIN_NAMESPACE_STD
304
Eric Fiselier876c6862016-02-20 00:19:45 +0000305template <class _Tp, class _Allocator /* = allocator<_Tp> */>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000306class _LIBCPP_TEMPLATE_VIS vector
Howard Hinnantc51e1022010-05-11 19:42:16 +0000307{
308private:
Konstantin Varlamovf2872972021-12-01 11:55:46 -0800309 typedef allocator<_Tp> __default_allocator_type;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000310public:
Konstantin Varlamovf2872972021-12-01 11:55:46 -0800311 typedef vector __self;
312 typedef _Tp value_type;
313 typedef _Allocator allocator_type;
314 typedef allocator_traits<allocator_type> __alloc_traits;
315 typedef value_type& reference;
316 typedef const value_type& const_reference;
317 typedef typename __alloc_traits::size_type size_type;
318 typedef typename __alloc_traits::difference_type difference_type;
319 typedef typename __alloc_traits::pointer pointer;
320 typedef typename __alloc_traits::const_pointer const_pointer;
321 typedef __wrap_iter<pointer> iterator;
322 typedef __wrap_iter<const_pointer> const_iterator;
323 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
324 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000325
Howard Hinnanta416ff02013-03-26 19:04:56 +0000326 static_assert((is_same<typename allocator_type::value_type, value_type>::value),
327 "Allocator::value_type must be same type as value_type");
328
Howard Hinnant1c936782011-06-03 19:40:40 +0000329 _LIBCPP_INLINE_VISIBILITY
Marshall Clowe546cbb2015-06-04 02:05:41 +0000330 vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Nikolas Klauserf2807732022-01-11 00:33:35 +0100331 {
332 _VSTD::__debug_db_insert_c(this);
333 }
Howard Hinnant27e0e772011-09-14 18:33:51 +0000334 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
Marshall Clow8f282f42015-06-04 00:10:20 +0000335#if _LIBCPP_STD_VER <= 14
336 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
337#else
338 _NOEXCEPT
339#endif
Nikolas Klauser129974f2022-02-03 23:09:37 +0100340 : __end_cap_(nullptr, __a)
Howard Hinnant27e0e772011-09-14 18:33:51 +0000341 {
Nikolas Klauserf2807732022-01-11 00:33:35 +0100342 _VSTD::__debug_db_insert_c(this);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000343 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000344 explicit vector(size_type __n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +0000345#if _LIBCPP_STD_VER > 11
346 explicit vector(size_type __n, const allocator_type& __a);
347#endif
Marshall Clowf0ca1492018-05-21 21:30:12 +0000348 vector(size_type __n, const value_type& __x);
Konstantin Varlamovf2872972021-12-01 11:55:46 -0800349
350 template <class = __enable_if_t<__is_allocator<_Allocator>::value> >
351 vector(size_type __n, const value_type& __x, const allocator_type& __a)
Nikolas Klauser129974f2022-02-03 23:09:37 +0100352 : __end_cap_(nullptr, __a)
Konstantin Varlamovf2872972021-12-01 11:55:46 -0800353 {
Nikolas Klauserf2807732022-01-11 00:33:35 +0100354 _VSTD::__debug_db_insert_c(this);
Konstantin Varlamovf2872972021-12-01 11:55:46 -0800355 if (__n > 0)
356 {
357 __vallocate(__n);
358 __construct_at_end(__n, __x);
359 }
360 }
361
Howard Hinnantc51e1022010-05-11 19:42:16 +0000362 template <class _InputIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +0000363 vector(_InputIterator __first,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500364 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
365 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000366 is_constructible<
367 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +0000368 typename iterator_traits<_InputIterator>::reference>::value,
369 _InputIterator>::type __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000370 template <class _InputIterator>
371 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500372 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
373 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000374 is_constructible<
375 value_type,
376 typename iterator_traits<_InputIterator>::reference>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000377 template <class _ForwardIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +0000378 vector(_ForwardIterator __first,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500379 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000380 is_constructible<
381 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +0000382 typename iterator_traits<_ForwardIterator>::reference>::value,
383 _ForwardIterator>::type __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000384 template <class _ForwardIterator>
385 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500386 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000387 is_constructible<
388 value_type,
389 typename iterator_traits<_ForwardIterator>::reference>::value>::type* = 0);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000390
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000391 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant27e0e772011-09-14 18:33:51 +0000392 ~vector()
393 {
Konstantin Varlamov5f23d662021-11-08 00:10:13 -0800394 __annotate_delete();
Louis Dionneba400782020-10-02 15:02:52 -0400395#if _LIBCPP_DEBUG_LEVEL == 2
Konstantin Varlamov5f23d662021-11-08 00:10:13 -0800396 __get_db()->__erase_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000397#endif
Konstantin Varlamov5f23d662021-11-08 00:10:13 -0800398
399 if (this->__begin_ != nullptr)
400 {
401 __clear();
402 __alloc_traits::deallocate(__alloc(), this->__begin_, capacity());
403 }
Marshall Clow68af4f32018-09-07 15:47:59 +0000404 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000405
406 vector(const vector& __x);
Arthur O'Dwyer9b9662f2021-03-01 17:08:24 -0500407 vector(const vector& __x, const __identity_t<allocator_type>& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000408 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000409 vector& operator=(const vector& __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000410
411#ifndef _LIBCPP_CXX03_LANG
412 _LIBCPP_INLINE_VISIBILITY
413 vector(initializer_list<value_type> __il);
414
415 _LIBCPP_INLINE_VISIBILITY
416 vector(initializer_list<value_type> __il, const allocator_type& __a);
417
Howard Hinnantcf823322010-12-17 14:46:43 +0000418 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000419 vector(vector&& __x)
Marshall Clowe5108202015-07-14 14:46:32 +0000420#if _LIBCPP_STD_VER > 14
421 _NOEXCEPT;
422#else
Howard Hinnant1c936782011-06-03 19:40:40 +0000423 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowe5108202015-07-14 14:46:32 +0000424#endif
Eric Fiseliered9e9362017-04-16 02:40:45 +0000425
Howard Hinnantcf823322010-12-17 14:46:43 +0000426 _LIBCPP_INLINE_VISIBILITY
Arthur O'Dwyer9b9662f2021-03-01 17:08:24 -0500427 vector(vector&& __x, const __identity_t<allocator_type>& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000428 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000429 vector& operator=(vector&& __x)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000430 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiseliered9e9362017-04-16 02:40:45 +0000431
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000432 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000433 vector& operator=(initializer_list<value_type> __il)
434 {assign(__il.begin(), __il.end()); return *this;}
Eric Fiseliered9e9362017-04-16 02:40:45 +0000435
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400436#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000437
438 template <class _InputIterator>
439 typename enable_if
440 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500441 __is_cpp17_input_iterator <_InputIterator>::value &&
442 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000443 is_constructible<
444 value_type,
445 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000446 void
447 >::type
448 assign(_InputIterator __first, _InputIterator __last);
449 template <class _ForwardIterator>
450 typename enable_if
451 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500452 __is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000453 is_constructible<
454 value_type,
455 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000456 void
457 >::type
458 assign(_ForwardIterator __first, _ForwardIterator __last);
459
460 void assign(size_type __n, const_reference __u);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000461
462#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000463 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000464 void assign(initializer_list<value_type> __il)
465 {assign(__il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +0000466#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000467
Howard Hinnant1c936782011-06-03 19:40:40 +0000468 _LIBCPP_INLINE_VISIBILITY
469 allocator_type get_allocator() const _NOEXCEPT
470 {return this->__alloc();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000471
Howard Hinnant1c936782011-06-03 19:40:40 +0000472 _LIBCPP_INLINE_VISIBILITY iterator begin() _NOEXCEPT;
473 _LIBCPP_INLINE_VISIBILITY const_iterator begin() const _NOEXCEPT;
474 _LIBCPP_INLINE_VISIBILITY iterator end() _NOEXCEPT;
475 _LIBCPP_INLINE_VISIBILITY const_iterator end() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000476
Howard Hinnant1c936782011-06-03 19:40:40 +0000477 _LIBCPP_INLINE_VISIBILITY
478 reverse_iterator rbegin() _NOEXCEPT
479 {return reverse_iterator(end());}
480 _LIBCPP_INLINE_VISIBILITY
481 const_reverse_iterator rbegin() const _NOEXCEPT
482 {return const_reverse_iterator(end());}
483 _LIBCPP_INLINE_VISIBILITY
484 reverse_iterator rend() _NOEXCEPT
485 {return reverse_iterator(begin());}
486 _LIBCPP_INLINE_VISIBILITY
487 const_reverse_iterator rend() const _NOEXCEPT
488 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000489
Howard Hinnant1c936782011-06-03 19:40:40 +0000490 _LIBCPP_INLINE_VISIBILITY
491 const_iterator cbegin() const _NOEXCEPT
492 {return begin();}
493 _LIBCPP_INLINE_VISIBILITY
494 const_iterator cend() const _NOEXCEPT
495 {return end();}
496 _LIBCPP_INLINE_VISIBILITY
497 const_reverse_iterator crbegin() const _NOEXCEPT
498 {return rbegin();}
499 _LIBCPP_INLINE_VISIBILITY
500 const_reverse_iterator crend() const _NOEXCEPT
501 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000502
Howard Hinnant1c936782011-06-03 19:40:40 +0000503 _LIBCPP_INLINE_VISIBILITY
504 size_type size() const _NOEXCEPT
505 {return static_cast<size_type>(this->__end_ - this->__begin_);}
506 _LIBCPP_INLINE_VISIBILITY
507 size_type capacity() const _NOEXCEPT
Konstantin Varlamov5f23d662021-11-08 00:10:13 -0800508 {return static_cast<size_type>(__end_cap() - this->__begin_);}
Marshall Clow425f5752017-11-15 05:51:26 +0000509 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000510 bool empty() const _NOEXCEPT
511 {return this->__begin_ == this->__end_;}
512 size_type max_size() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000513 void reserve(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +0000514 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000515
Marshall Clowd6470492019-03-15 00:29:35 +0000516 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) _NOEXCEPT;
517 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000518 reference at(size_type __n);
519 const_reference at(size_type __n) const;
520
Marshall Clowd6470492019-03-15 00:29:35 +0000521 _LIBCPP_INLINE_VISIBILITY reference front() _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000522 {
Kristina Bessonovaaeeaa7e2021-05-09 19:29:56 +0200523 _LIBCPP_ASSERT(!empty(), "front() called on an empty vector");
Howard Hinnant27e0e772011-09-14 18:33:51 +0000524 return *this->__begin_;
525 }
Marshall Clowd6470492019-03-15 00:29:35 +0000526 _LIBCPP_INLINE_VISIBILITY const_reference front() const _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000527 {
Kristina Bessonovaaeeaa7e2021-05-09 19:29:56 +0200528 _LIBCPP_ASSERT(!empty(), "front() called on an empty vector");
Howard Hinnant27e0e772011-09-14 18:33:51 +0000529 return *this->__begin_;
530 }
Marshall Clowd6470492019-03-15 00:29:35 +0000531 _LIBCPP_INLINE_VISIBILITY reference back() _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000532 {
Kristina Bessonovaaeeaa7e2021-05-09 19:29:56 +0200533 _LIBCPP_ASSERT(!empty(), "back() called on an empty vector");
Howard Hinnant27e0e772011-09-14 18:33:51 +0000534 return *(this->__end_ - 1);
535 }
Marshall Clowd6470492019-03-15 00:29:35 +0000536 _LIBCPP_INLINE_VISIBILITY const_reference back() const _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000537 {
Kristina Bessonovaaeeaa7e2021-05-09 19:29:56 +0200538 _LIBCPP_ASSERT(!empty(), "back() called on an empty vector");
Howard Hinnant27e0e772011-09-14 18:33:51 +0000539 return *(this->__end_ - 1);
540 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000541
Howard Hinnant1c936782011-06-03 19:40:40 +0000542 _LIBCPP_INLINE_VISIBILITY
543 value_type* data() _NOEXCEPT
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500544 {return _VSTD::__to_address(this->__begin_);}
Howard Hinnant1c936782011-06-03 19:40:40 +0000545 _LIBCPP_INLINE_VISIBILITY
546 const value_type* data() const _NOEXCEPT
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500547 {return _VSTD::__to_address(this->__begin_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000548
Eric Fiselier96919722017-10-17 13:03:17 +0000549#ifdef _LIBCPP_CXX03_LANG
550 _LIBCPP_INLINE_VISIBILITY
551 void __emplace_back(const value_type& __x) { push_back(__x); }
552#else
553 template <class _Arg>
554 _LIBCPP_INLINE_VISIBILITY
555 void __emplace_back(_Arg&& __arg) {
556 emplace_back(_VSTD::forward<_Arg>(__arg));
557 }
558#endif
559
Howard Hinnantcf823322010-12-17 14:46:43 +0000560 _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000561
562#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000563 _LIBCPP_INLINE_VISIBILITY void push_back(value_type&& __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000564
Howard Hinnantc51e1022010-05-11 19:42:16 +0000565 template <class... _Args>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000566 _LIBCPP_INLINE_VISIBILITY
Marshall Clowea52cc42017-01-24 23:09:12 +0000567#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +0000568 reference emplace_back(_Args&&... __args);
Marshall Clowea52cc42017-01-24 23:09:12 +0000569#else
570 void emplace_back(_Args&&... __args);
571#endif
Eric Fiseliered9e9362017-04-16 02:40:45 +0000572#endif // !_LIBCPP_CXX03_LANG
573
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000574 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000575 void pop_back();
576
577 iterator insert(const_iterator __position, const_reference __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000578
579#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000580 iterator insert(const_iterator __position, value_type&& __x);
581 template <class... _Args>
582 iterator emplace(const_iterator __position, _Args&&... __args);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400583#endif // !_LIBCPP_CXX03_LANG
Eric Fiseliered9e9362017-04-16 02:40:45 +0000584
Howard Hinnantc51e1022010-05-11 19:42:16 +0000585 iterator insert(const_iterator __position, size_type __n, const_reference __x);
586 template <class _InputIterator>
587 typename enable_if
588 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500589 __is_cpp17_input_iterator <_InputIterator>::value &&
590 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000591 is_constructible<
592 value_type,
593 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000594 iterator
595 >::type
596 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
597 template <class _ForwardIterator>
598 typename enable_if
599 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500600 __is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000601 is_constructible<
602 value_type,
603 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000604 iterator
605 >::type
606 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000607
608#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000609 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000610 iterator insert(const_iterator __position, initializer_list<value_type> __il)
611 {return insert(__position, __il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +0000612#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000613
Howard Hinnantcf823322010-12-17 14:46:43 +0000614 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000615 iterator erase(const_iterator __first, const_iterator __last);
616
Howard Hinnant1c936782011-06-03 19:40:40 +0000617 _LIBCPP_INLINE_VISIBILITY
618 void clear() _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000619 {
Marshall Clow2cd9d372014-05-08 14:14:06 +0000620 size_type __old_size = size();
Konstantin Varlamov5f23d662021-11-08 00:10:13 -0800621 __clear();
Marshall Clow2cd9d372014-05-08 14:14:06 +0000622 __annotate_shrink(__old_size);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000623 __invalidate_all_iterators();
624 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000625
626 void resize(size_type __sz);
627 void resize(size_type __sz, const_reference __x);
628
Howard Hinnant1c936782011-06-03 19:40:40 +0000629 void swap(vector&)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000630#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +0000631 _NOEXCEPT;
Marshall Clow8982dcd2015-07-13 20:04:56 +0000632#else
Eric Fiselier873b8d32019-03-18 21:50:12 +0000633 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +0000634 __is_nothrow_swappable<allocator_type>::value);
635#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000636
637 bool __invariants() const;
638
Louis Dionneba400782020-10-02 15:02:52 -0400639#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000640
641 bool __dereferenceable(const const_iterator* __i) const;
642 bool __decrementable(const const_iterator* __i) const;
643 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
644 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
645
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400646#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000647
Howard Hinnantc51e1022010-05-11 19:42:16 +0000648private:
Nikolas Klauser129974f2022-02-03 23:09:37 +0100649 pointer __begin_ = nullptr;
650 pointer __end_ = nullptr;
651 __compressed_pair<pointer, allocator_type> __end_cap_ =
652 __compressed_pair<pointer, allocator_type>(nullptr, __default_init_tag());
653
Howard Hinnantcf823322010-12-17 14:46:43 +0000654 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Eric Fiselier69c51982016-12-28 06:06:09 +0000655 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(pointer __new_last);
Marshall Clow3ff48e02018-05-22 16:20:28 +0000656 void __vallocate(size_type __n);
657 void __vdeallocate() _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000658 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
Howard Hinnanta5589382011-01-04 19:53:31 +0000659 void __construct_at_end(size_type __n);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000660 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000661 void __construct_at_end(size_type __n, const_reference __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000662 template <class _ForwardIterator>
663 typename enable_if
664 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500665 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000666 void
667 >::type
Eric Fiselieracfe6f02015-03-31 16:54:19 +0000668 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000669 void __append(size_type __n);
670 void __append(size_type __n, const_reference __x);
Howard Hinnantcf823322010-12-17 14:46:43 +0000671 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000672 iterator __make_iter(pointer __p) _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000673 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000674 const_iterator __make_iter(const_pointer __p) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000675 void __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v);
676 pointer __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p);
677 void __move_range(pointer __from_s, pointer __from_e, pointer __to);
Howard Hinnant1c936782011-06-03 19:40:40 +0000678 void __move_assign(vector& __c, true_type)
679 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000680 void __move_assign(vector& __c, false_type)
681 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000682 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76053d72013-06-27 19:35:32 +0000683 void __destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000684 {
Eric Fiselier69c51982016-12-28 06:06:09 +0000685 __invalidate_iterators_past(__new_last);
Marshall Clow2cd9d372014-05-08 14:14:06 +0000686 size_type __old_size = size();
Konstantin Varlamov5f23d662021-11-08 00:10:13 -0800687 __base_destruct_at_end(__new_last);
Marshall Clow2cd9d372014-05-08 14:14:06 +0000688 __annotate_shrink(__old_size);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000689 }
Eric Fiseliered9e9362017-04-16 02:40:45 +0000690
691#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier7110f442019-03-19 19:19:44 +0000692 template <class _Up>
693 _LIBCPP_INLINE_VISIBILITY
694 inline void __push_back_slow_path(_Up&& __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000695
Howard Hinnantb6c49562012-02-26 15:30:12 +0000696 template <class... _Args>
Eric Fiselier7110f442019-03-19 19:19:44 +0000697 _LIBCPP_INLINE_VISIBILITY
698 inline void __emplace_back_slow_path(_Args&&... __args);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000699#else
Eric Fiselier7110f442019-03-19 19:19:44 +0000700 template <class _Up>
701 _LIBCPP_INLINE_VISIBILITY
702 inline void __push_back_slow_path(_Up& __x);
Howard Hinnantb6c49562012-02-26 15:30:12 +0000703#endif
Eric Fiseliered9e9362017-04-16 02:40:45 +0000704
Marshall Clow2cd9d372014-05-08 14:14:06 +0000705 // The following functions are no-ops outside of AddressSanitizer mode.
706 // We call annotatations only for the default Allocator because other allocators
707 // may not meet the AddressSanitizer alignment constraints.
708 // See the documentation for __sanitizer_annotate_contiguous_container for more details.
Marshall Clow2cd9d372014-05-08 14:14:06 +0000709#ifndef _LIBCPP_HAS_NO_ASAN
Eric Fiselier6003c772016-12-23 23:37:52 +0000710 void __annotate_contiguous_container(const void *__beg, const void *__end,
711 const void *__old_mid,
712 const void *__new_mid) const
713 {
714
Marshall Clow2cd9d372014-05-08 14:14:06 +0000715 if (__beg && is_same<allocator_type, __default_allocator_type>::value)
716 __sanitizer_annotate_contiguous_container(__beg, __end, __old_mid, __new_mid);
Marshall Clow2cd9d372014-05-08 14:14:06 +0000717 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000718#else
719 _LIBCPP_INLINE_VISIBILITY
720 void __annotate_contiguous_container(const void*, const void*, const void*,
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000721 const void*) const _NOEXCEPT {}
Eric Fiselier6003c772016-12-23 23:37:52 +0000722#endif
723 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000724 void __annotate_new(size_type __current_size) const _NOEXCEPT {
Marshall Clow2cd9d372014-05-08 14:14:06 +0000725 __annotate_contiguous_container(data(), data() + capacity(),
726 data() + capacity(), data() + __current_size);
727 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000728
729 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000730 void __annotate_delete() const _NOEXCEPT {
Marshall Clow2cd9d372014-05-08 14:14:06 +0000731 __annotate_contiguous_container(data(), data() + capacity(),
732 data() + size(), data() + capacity());
733 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000734
735 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000736 void __annotate_increase(size_type __n) const _NOEXCEPT
Marshall Clow2cd9d372014-05-08 14:14:06 +0000737 {
738 __annotate_contiguous_container(data(), data() + capacity(),
739 data() + size(), data() + size() + __n);
740 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000741
742 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000743 void __annotate_shrink(size_type __old_size) const _NOEXCEPT
Marshall Clow2cd9d372014-05-08 14:14:06 +0000744 {
745 __annotate_contiguous_container(data(), data() + capacity(),
746 data() + __old_size, data() + size());
747 }
Marshall Clowc78ffa52014-09-03 21:37:43 +0000748
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000749 struct _ConstructTransaction {
750 explicit _ConstructTransaction(vector &__v, size_type __n)
Mark de Weverdcafc462021-10-21 18:29:14 +0200751 : __v_(__v), __pos_(__v.__end_), __new_end_(__v.__end_ + __n) {
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000752#ifndef _LIBCPP_HAS_NO_ASAN
753 __v_.__annotate_increase(__n);
754#endif
755 }
756 ~_ConstructTransaction() {
757 __v_.__end_ = __pos_;
758#ifndef _LIBCPP_HAS_NO_ASAN
759 if (__pos_ != __new_end_) {
760 __v_.__annotate_shrink(__new_end_ - __v_.__begin_);
761 }
762#endif
763 }
764
765 vector &__v_;
766 pointer __pos_;
767 const_pointer const __new_end_;
768
769 private:
770 _ConstructTransaction(_ConstructTransaction const&) = delete;
771 _ConstructTransaction& operator=(_ConstructTransaction const&) = delete;
772 };
773
774 template <class ..._Args>
775 _LIBCPP_INLINE_VISIBILITY
776 void __construct_one_at_end(_Args&& ...__args) {
777 _ConstructTransaction __tx(*this, 1);
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500778 __alloc_traits::construct(this->__alloc(), _VSTD::__to_address(__tx.__pos_),
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000779 _VSTD::forward<_Args>(__args)...);
780 ++__tx.__pos_;
781 }
Konstantin Varlamov5f23d662021-11-08 00:10:13 -0800782
783 _LIBCPP_INLINE_VISIBILITY
784 allocator_type& __alloc() _NOEXCEPT
785 {return this->__end_cap_.second();}
786 _LIBCPP_INLINE_VISIBILITY
787 const allocator_type& __alloc() const _NOEXCEPT
788 {return this->__end_cap_.second();}
789 _LIBCPP_INLINE_VISIBILITY
790 pointer& __end_cap() _NOEXCEPT
791 {return this->__end_cap_.first();}
792 _LIBCPP_INLINE_VISIBILITY
793 const pointer& __end_cap() const _NOEXCEPT
794 {return this->__end_cap_.first();}
795
796 _LIBCPP_INLINE_VISIBILITY
797 void __clear() _NOEXCEPT {__base_destruct_at_end(this->__begin_);}
798
799 _LIBCPP_INLINE_VISIBILITY
800 void __base_destruct_at_end(pointer __new_last) _NOEXCEPT {
801 pointer __soon_to_be_end = this->__end_;
802 while (__new_last != __soon_to_be_end)
803 __alloc_traits::destroy(__alloc(), _VSTD::__to_address(--__soon_to_be_end));
804 this->__end_ = __new_last;
805 }
806
807 _LIBCPP_INLINE_VISIBILITY
808 void __copy_assign_alloc(const vector& __c)
809 {__copy_assign_alloc(__c, integral_constant<bool,
810 __alloc_traits::propagate_on_container_copy_assignment::value>());}
811
812 _LIBCPP_INLINE_VISIBILITY
813 void __move_assign_alloc(vector& __c)
814 _NOEXCEPT_(
815 !__alloc_traits::propagate_on_container_move_assignment::value ||
816 is_nothrow_move_assignable<allocator_type>::value)
817 {__move_assign_alloc(__c, integral_constant<bool,
818 __alloc_traits::propagate_on_container_move_assignment::value>());}
819
820 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
821 void __throw_length_error() const {
Nikolas Klauser129974f2022-02-03 23:09:37 +0100822 _VSTD::__throw_length_error("vector");
Konstantin Varlamov5f23d662021-11-08 00:10:13 -0800823 }
824
825 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
826 void __throw_out_of_range() const {
Nikolas Klauser129974f2022-02-03 23:09:37 +0100827 _VSTD::__throw_out_of_range("vector");
Konstantin Varlamov5f23d662021-11-08 00:10:13 -0800828 }
829
830 _LIBCPP_INLINE_VISIBILITY
831 void __copy_assign_alloc(const vector& __c, true_type)
832 {
833 if (__alloc() != __c.__alloc())
834 {
835 __clear();
836 __alloc_traits::deallocate(__alloc(), this->__begin_, capacity());
837 this->__begin_ = this->__end_ = __end_cap() = nullptr;
838 }
839 __alloc() = __c.__alloc();
840 }
841
842 _LIBCPP_INLINE_VISIBILITY
843 void __copy_assign_alloc(const vector&, false_type)
844 {}
845
846 _LIBCPP_INLINE_VISIBILITY
847 void __move_assign_alloc(vector& __c, true_type)
848 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
849 {
850 __alloc() = _VSTD::move(__c.__alloc());
851 }
852
853 _LIBCPP_INLINE_VISIBILITY
854 void __move_assign_alloc(vector&, false_type)
855 _NOEXCEPT
856 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000857};
858
Louis Dionned59f8a52021-08-17 11:59:07 -0400859#if _LIBCPP_STD_VER >= 17
Marshall Clowf0ca1492018-05-21 21:30:12 +0000860template<class _InputIterator,
Arthur O'Dwyer56226762021-03-03 23:02:20 -0500861 class _Alloc = allocator<__iter_value_type<_InputIterator>>,
Konstantin Varlamov53b543c2021-11-09 09:21:02 -0800862 class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
Louis Dionne25547162021-08-17 12:26:09 -0400863 class = enable_if_t<__is_allocator<_Alloc>::value>
Marshall Clowf0ca1492018-05-21 21:30:12 +0000864 >
865vector(_InputIterator, _InputIterator)
Arthur O'Dwyer56226762021-03-03 23:02:20 -0500866 -> vector<__iter_value_type<_InputIterator>, _Alloc>;
Marshall Clowf0ca1492018-05-21 21:30:12 +0000867
868template<class _InputIterator,
869 class _Alloc,
Konstantin Varlamov53b543c2021-11-09 09:21:02 -0800870 class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
Louis Dionne25547162021-08-17 12:26:09 -0400871 class = enable_if_t<__is_allocator<_Alloc>::value>
Marshall Clowf0ca1492018-05-21 21:30:12 +0000872 >
873vector(_InputIterator, _InputIterator, _Alloc)
Arthur O'Dwyer56226762021-03-03 23:02:20 -0500874 -> vector<__iter_value_type<_InputIterator>, _Alloc>;
Marshall Clowf0ca1492018-05-21 21:30:12 +0000875#endif
876
Howard Hinnantc51e1022010-05-11 19:42:16 +0000877template <class _Tp, class _Allocator>
878void
879vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v)
880{
Eric Fiselier909fe962019-09-13 16:09:33 +0000881
Marshall Clow2cd9d372014-05-08 14:14:06 +0000882 __annotate_delete();
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -0500883 _VSTD::__construct_backward_with_exception_guarantees(this->__alloc(), this->__begin_, this->__end_, __v.__begin_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000884 _VSTD::swap(this->__begin_, __v.__begin_);
885 _VSTD::swap(this->__end_, __v.__end_);
886 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000887 __v.__first_ = __v.__begin_;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000888 __annotate_new(size());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000889 __invalidate_all_iterators();
890}
891
892template <class _Tp, class _Allocator>
893typename vector<_Tp, _Allocator>::pointer
894vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p)
895{
Marshall Clow2cd9d372014-05-08 14:14:06 +0000896 __annotate_delete();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000897 pointer __r = __v.__begin_;
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -0500898 _VSTD::__construct_backward_with_exception_guarantees(this->__alloc(), this->__begin_, __p, __v.__begin_);
899 _VSTD::__construct_forward_with_exception_guarantees(this->__alloc(), __p, this->__end_, __v.__end_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000900 _VSTD::swap(this->__begin_, __v.__begin_);
901 _VSTD::swap(this->__end_, __v.__end_);
902 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000903 __v.__first_ = __v.__begin_;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000904 __annotate_new(size());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000905 __invalidate_all_iterators();
906 return __r;
907}
908
909// Allocate space for __n objects
910// throws length_error if __n > max_size()
911// throws (probably bad_alloc) if memory run out
912// Precondition: __begin_ == __end_ == __end_cap() == 0
913// Precondition: __n > 0
914// Postcondition: capacity() == __n
915// Postcondition: size() == 0
916template <class _Tp, class _Allocator>
917void
Marshall Clow3ff48e02018-05-22 16:20:28 +0000918vector<_Tp, _Allocator>::__vallocate(size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000919{
920 if (__n > max_size())
921 this->__throw_length_error();
922 this->__begin_ = this->__end_ = __alloc_traits::allocate(this->__alloc(), __n);
923 this->__end_cap() = this->__begin_ + __n;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000924 __annotate_new(0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000925}
926
927template <class _Tp, class _Allocator>
928void
Marshall Clow3ff48e02018-05-22 16:20:28 +0000929vector<_Tp, _Allocator>::__vdeallocate() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000930{
Howard Hinnant76053d72013-06-27 19:35:32 +0000931 if (this->__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000932 {
933 clear();
934 __alloc_traits::deallocate(this->__alloc(), this->__begin_, capacity());
Howard Hinnant76053d72013-06-27 19:35:32 +0000935 this->__begin_ = this->__end_ = this->__end_cap() = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000936 }
937}
938
939template <class _Tp, class _Allocator>
940typename vector<_Tp, _Allocator>::size_type
Howard Hinnant1c936782011-06-03 19:40:40 +0000941vector<_Tp, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000942{
Eric Fiselierb5d9f442016-11-23 01:18:56 +0000943 return _VSTD::min<size_type>(__alloc_traits::max_size(this->__alloc()),
944 numeric_limits<difference_type>::max());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000945}
946
947// Precondition: __new_size > capacity()
948template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000949inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000950typename vector<_Tp, _Allocator>::size_type
951vector<_Tp, _Allocator>::__recommend(size_type __new_size) const
952{
953 const size_type __ms = max_size();
954 if (__new_size > __ms)
955 this->__throw_length_error();
956 const size_type __cap = capacity();
957 if (__cap >= __ms / 2)
958 return __ms;
Arthur O'Dwyerc0fb14b2021-07-26 18:23:00 -0400959 return _VSTD::max<size_type>(2 * __cap, __new_size);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000960}
961
962// Default constructs __n objects starting at __end_
963// throws if construction throws
964// Precondition: __n > 0
965// Precondition: size() + __n <= capacity()
966// Postcondition: size() == size() + __n
967template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000968void
969vector<_Tp, _Allocator>::__construct_at_end(size_type __n)
970{
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000971 _ConstructTransaction __tx(*this, __n);
Martijn Vels1e2e1b82020-06-18 13:14:02 -0400972 const_pointer __new_end = __tx.__new_end_;
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -0400973 for (pointer __pos = __tx.__pos_; __pos != __new_end; __tx.__pos_ = ++__pos) {
Martijn Vels1e2e1b82020-06-18 13:14:02 -0400974 __alloc_traits::construct(this->__alloc(), _VSTD::__to_address(__pos));
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000975 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000976}
977
Howard Hinnantc51e1022010-05-11 19:42:16 +0000978// Copy constructs __n objects starting at __end_ from __x
979// throws if construction throws
980// Precondition: __n > 0
981// Precondition: size() + __n <= capacity()
982// Postcondition: size() == old size() + __n
983// Postcondition: [i] == __x for all i in [size() - __n, __n)
984template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000985inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000986void
987vector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
988{
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000989 _ConstructTransaction __tx(*this, __n);
Martijn Vels1e2e1b82020-06-18 13:14:02 -0400990 const_pointer __new_end = __tx.__new_end_;
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -0400991 for (pointer __pos = __tx.__pos_; __pos != __new_end; __tx.__pos_ = ++__pos) {
Martijn Vels1e2e1b82020-06-18 13:14:02 -0400992 __alloc_traits::construct(this->__alloc(), _VSTD::__to_address(__pos), __x);
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000993 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000994}
995
996template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000997template <class _ForwardIterator>
998typename enable_if
999<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001000 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001001 void
1002>::type
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001003vector<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001004{
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001005 _ConstructTransaction __tx(*this, __n);
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05001006 _VSTD::__construct_range_forward(this->__alloc(), __first, __last, __tx.__pos_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001007}
1008
1009// Default constructs __n objects starting at __end_
1010// throws if construction throws
1011// Postcondition: size() == size() + __n
Howard Hinnant1c936782011-06-03 19:40:40 +00001012// Exception safety: strong.
Howard Hinnantc51e1022010-05-11 19:42:16 +00001013template <class _Tp, class _Allocator>
1014void
1015vector<_Tp, _Allocator>::__append(size_type __n)
1016{
1017 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1018 this->__construct_at_end(__n);
1019 else
1020 {
1021 allocator_type& __a = this->__alloc();
1022 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1023 __v.__construct_at_end(__n);
1024 __swap_out_circular_buffer(__v);
1025 }
1026}
1027
1028// Default constructs __n objects starting at __end_
1029// throws if construction throws
1030// Postcondition: size() == size() + __n
Howard Hinnant1c936782011-06-03 19:40:40 +00001031// Exception safety: strong.
Howard Hinnantc51e1022010-05-11 19:42:16 +00001032template <class _Tp, class _Allocator>
1033void
1034vector<_Tp, _Allocator>::__append(size_type __n, const_reference __x)
1035{
1036 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1037 this->__construct_at_end(__n, __x);
1038 else
1039 {
1040 allocator_type& __a = this->__alloc();
1041 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1042 __v.__construct_at_end(__n, __x);
1043 __swap_out_circular_buffer(__v);
1044 }
1045}
1046
1047template <class _Tp, class _Allocator>
1048vector<_Tp, _Allocator>::vector(size_type __n)
1049{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001050 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001051 if (__n > 0)
1052 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001053 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001054 __construct_at_end(__n);
1055 }
1056}
1057
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00001058#if _LIBCPP_STD_VER > 11
1059template <class _Tp, class _Allocator>
1060vector<_Tp, _Allocator>::vector(size_type __n, const allocator_type& __a)
Nikolas Klauser129974f2022-02-03 23:09:37 +01001061 : __end_cap_(nullptr, __a)
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00001062{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001063 _VSTD::__debug_db_insert_c(this);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00001064 if (__n > 0)
1065 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001066 __vallocate(__n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00001067 __construct_at_end(__n);
1068 }
1069}
1070#endif
1071
Howard Hinnantc51e1022010-05-11 19:42:16 +00001072template <class _Tp, class _Allocator>
Marshall Clowf0ca1492018-05-21 21:30:12 +00001073vector<_Tp, _Allocator>::vector(size_type __n, const value_type& __x)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001074{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001075 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001076 if (__n > 0)
1077 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001078 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001079 __construct_at_end(__n, __x);
1080 }
1081}
1082
1083template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001084template <class _InputIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +00001085vector<_Tp, _Allocator>::vector(_InputIterator __first,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001086 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
1087 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001088 is_constructible<
1089 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +00001090 typename iterator_traits<_InputIterator>::reference>::value,
1091 _InputIterator>::type __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001092{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001093 _VSTD::__debug_db_insert_c(this);
Howard Hinnant9cd22302011-09-16 18:41:29 +00001094 for (; __first != __last; ++__first)
Eric Fiselier96919722017-10-17 13:03:17 +00001095 __emplace_back(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001096}
1097
1098template <class _Tp, class _Allocator>
1099template <class _InputIterator>
1100vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001101 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
1102 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001103 is_constructible<
1104 value_type,
1105 typename iterator_traits<_InputIterator>::reference>::value>::type*)
Nikolas Klauser129974f2022-02-03 23:09:37 +01001106 : __end_cap_(nullptr, __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001107{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001108 _VSTD::__debug_db_insert_c(this);
Howard Hinnant9cd22302011-09-16 18:41:29 +00001109 for (; __first != __last; ++__first)
Eric Fiselier96919722017-10-17 13:03:17 +00001110 __emplace_back(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001111}
1112
1113template <class _Tp, class _Allocator>
1114template <class _ForwardIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +00001115vector<_Tp, _Allocator>::vector(_ForwardIterator __first,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001116 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001117 is_constructible<
1118 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +00001119 typename iterator_traits<_ForwardIterator>::reference>::value,
1120 _ForwardIterator>::type __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001121{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001122 _VSTD::__debug_db_insert_c(this);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001123 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001124 if (__n > 0)
1125 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001126 __vallocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001127 __construct_at_end(__first, __last, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001128 }
1129}
1130
1131template <class _Tp, class _Allocator>
1132template <class _ForwardIterator>
1133vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001134 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001135 is_constructible<
1136 value_type,
1137 typename iterator_traits<_ForwardIterator>::reference>::value>::type*)
Nikolas Klauser129974f2022-02-03 23:09:37 +01001138 : __end_cap_(nullptr, __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001139{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001140 _VSTD::__debug_db_insert_c(this);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001141 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001142 if (__n > 0)
1143 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001144 __vallocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001145 __construct_at_end(__first, __last, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001146 }
1147}
1148
1149template <class _Tp, class _Allocator>
1150vector<_Tp, _Allocator>::vector(const vector& __x)
Nikolas Klauser129974f2022-02-03 23:09:37 +01001151 : __end_cap_(nullptr, __alloc_traits::select_on_container_copy_construction(__x.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001152{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001153 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001154 size_type __n = __x.size();
1155 if (__n > 0)
1156 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001157 __vallocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001158 __construct_at_end(__x.__begin_, __x.__end_, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001159 }
1160}
1161
1162template <class _Tp, class _Allocator>
Arthur O'Dwyer9b9662f2021-03-01 17:08:24 -05001163vector<_Tp, _Allocator>::vector(const vector& __x, const __identity_t<allocator_type>& __a)
Nikolas Klauser129974f2022-02-03 23:09:37 +01001164 : __end_cap_(nullptr, __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001165{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001166 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001167 size_type __n = __x.size();
1168 if (__n > 0)
1169 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001170 __vallocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001171 __construct_at_end(__x.__begin_, __x.__end_, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001172 }
1173}
1174
Eric Fiseliered9e9362017-04-16 02:40:45 +00001175#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001176
1177template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001178inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001179vector<_Tp, _Allocator>::vector(vector&& __x)
Marshall Clowe5108202015-07-14 14:46:32 +00001180#if _LIBCPP_STD_VER > 14
1181 _NOEXCEPT
1182#else
Howard Hinnant1c936782011-06-03 19:40:40 +00001183 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowe5108202015-07-14 14:46:32 +00001184#endif
Nikolas Klauser129974f2022-02-03 23:09:37 +01001185 : __end_cap_(nullptr, _VSTD::move(__x.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001186{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001187 _VSTD::__debug_db_insert_c(this);
Louis Dionneba400782020-10-02 15:02:52 -04001188#if _LIBCPP_DEBUG_LEVEL == 2
Mark de Wever8f72c892021-10-10 15:40:50 +02001189 __get_db()->swap(this, _VSTD::addressof(__x));
Howard Hinnant27e0e772011-09-14 18:33:51 +00001190#endif
Howard Hinnant9cd22302011-09-16 18:41:29 +00001191 this->__begin_ = __x.__begin_;
1192 this->__end_ = __x.__end_;
1193 this->__end_cap() = __x.__end_cap();
Howard Hinnant76053d72013-06-27 19:35:32 +00001194 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001195}
1196
1197template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001198inline _LIBCPP_INLINE_VISIBILITY
Arthur O'Dwyer9b9662f2021-03-01 17:08:24 -05001199vector<_Tp, _Allocator>::vector(vector&& __x, const __identity_t<allocator_type>& __a)
Nikolas Klauser129974f2022-02-03 23:09:37 +01001200 : __end_cap_(nullptr, __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001201{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001202 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001203 if (__a == __x.__alloc())
1204 {
1205 this->__begin_ = __x.__begin_;
1206 this->__end_ = __x.__end_;
1207 this->__end_cap() = __x.__end_cap();
1208 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Louis Dionneba400782020-10-02 15:02:52 -04001209#if _LIBCPP_DEBUG_LEVEL == 2
Mark de Wever8f72c892021-10-10 15:40:50 +02001210 __get_db()->swap(this, _VSTD::addressof(__x));
Howard Hinnanta1e60cd2011-09-19 16:34:29 +00001211#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001212 }
1213 else
1214 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001215 typedef move_iterator<iterator> _Ip;
1216 assign(_Ip(__x.begin()), _Ip(__x.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001217 }
1218}
1219
1220template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001221inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001222vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il)
1223{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001224 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001225 if (__il.size() > 0)
1226 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001227 __vallocate(__il.size());
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001228 __construct_at_end(__il.begin(), __il.end(), __il.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001229 }
1230}
1231
1232template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001233inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001234vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
Nikolas Klauser129974f2022-02-03 23:09:37 +01001235 : __end_cap_(nullptr, __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001236{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001237 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001238 if (__il.size() > 0)
1239 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001240 __vallocate(__il.size());
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001241 __construct_at_end(__il.begin(), __il.end(), __il.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001242 }
1243}
1244
1245template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001246inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001247vector<_Tp, _Allocator>&
1248vector<_Tp, _Allocator>::operator=(vector&& __x)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001249 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001250{
1251 __move_assign(__x, integral_constant<bool,
1252 __alloc_traits::propagate_on_container_move_assignment::value>());
1253 return *this;
1254}
1255
1256template <class _Tp, class _Allocator>
1257void
1258vector<_Tp, _Allocator>::__move_assign(vector& __c, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001259 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001260{
Konstantin Varlamov5f23d662021-11-08 00:10:13 -08001261 if (__alloc() != __c.__alloc())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001262 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001263 typedef move_iterator<iterator> _Ip;
1264 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001265 }
1266 else
1267 __move_assign(__c, true_type());
1268}
1269
1270template <class _Tp, class _Allocator>
1271void
1272vector<_Tp, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00001273 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001274{
Marshall Clow3ff48e02018-05-22 16:20:28 +00001275 __vdeallocate();
Konstantin Varlamov5f23d662021-11-08 00:10:13 -08001276 __move_assign_alloc(__c); // this can throw
Howard Hinnantc51e1022010-05-11 19:42:16 +00001277 this->__begin_ = __c.__begin_;
1278 this->__end_ = __c.__end_;
1279 this->__end_cap() = __c.__end_cap();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001280 __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
Louis Dionneba400782020-10-02 15:02:52 -04001281#if _LIBCPP_DEBUG_LEVEL == 2
Mark de Wever8f72c892021-10-10 15:40:50 +02001282 __get_db()->swap(this, _VSTD::addressof(__c));
Howard Hinnanta1e60cd2011-09-19 16:34:29 +00001283#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001284}
1285
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001286#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001287
1288template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001289inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001290vector<_Tp, _Allocator>&
1291vector<_Tp, _Allocator>::operator=(const vector& __x)
1292{
Mark de Wever357a1fc2021-09-28 19:15:18 +02001293 if (this != _VSTD::addressof(__x))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001294 {
Konstantin Varlamov5f23d662021-11-08 00:10:13 -08001295 __copy_assign_alloc(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001296 assign(__x.__begin_, __x.__end_);
1297 }
1298 return *this;
1299}
1300
1301template <class _Tp, class _Allocator>
1302template <class _InputIterator>
1303typename enable_if
1304<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001305 __is_cpp17_input_iterator <_InputIterator>::value &&
1306 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001307 is_constructible<
1308 _Tp,
1309 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001310 void
1311>::type
1312vector<_Tp, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
1313{
1314 clear();
1315 for (; __first != __last; ++__first)
Eric Fiselier96919722017-10-17 13:03:17 +00001316 __emplace_back(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001317}
1318
1319template <class _Tp, class _Allocator>
1320template <class _ForwardIterator>
1321typename enable_if
1322<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001323 __is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001324 is_constructible<
1325 _Tp,
1326 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001327 void
1328>::type
1329vector<_Tp, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
1330{
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001331 size_type __new_size = static_cast<size_type>(_VSTD::distance(__first, __last));
1332 if (__new_size <= capacity())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001333 {
1334 _ForwardIterator __mid = __last;
1335 bool __growing = false;
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001336 if (__new_size > size())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001337 {
1338 __growing = true;
1339 __mid = __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001340 _VSTD::advance(__mid, size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001341 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001342 pointer __m = _VSTD::copy(__first, __mid, this->__begin_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001343 if (__growing)
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001344 __construct_at_end(__mid, __last, __new_size - size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001345 else
1346 this->__destruct_at_end(__m);
1347 }
1348 else
1349 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001350 __vdeallocate();
1351 __vallocate(__recommend(__new_size));
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001352 __construct_at_end(__first, __last, __new_size);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001353 }
Eric Fiselier69c51982016-12-28 06:06:09 +00001354 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001355}
1356
1357template <class _Tp, class _Allocator>
1358void
1359vector<_Tp, _Allocator>::assign(size_type __n, const_reference __u)
1360{
1361 if (__n <= capacity())
1362 {
1363 size_type __s = size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001364 _VSTD::fill_n(this->__begin_, _VSTD::min(__n, __s), __u);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001365 if (__n > __s)
1366 __construct_at_end(__n - __s, __u);
1367 else
Howard Hinnant155c2af2010-05-24 17:49:41 +00001368 this->__destruct_at_end(this->__begin_ + __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001369 }
1370 else
1371 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001372 __vdeallocate();
1373 __vallocate(__recommend(static_cast<size_type>(__n)));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001374 __construct_at_end(__n, __u);
1375 }
Eric Fiselier69c51982016-12-28 06:06:09 +00001376 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001377}
1378
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001379template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001380inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001381typename vector<_Tp, _Allocator>::iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001382vector<_Tp, _Allocator>::__make_iter(pointer __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001383{
Louis Dionneba400782020-10-02 15:02:52 -04001384#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001385 return iterator(this, __p);
1386#else
1387 return iterator(__p);
1388#endif
1389}
1390
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001391template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001392inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001393typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001394vector<_Tp, _Allocator>::__make_iter(const_pointer __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001395{
Louis Dionneba400782020-10-02 15:02:52 -04001396#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001397 return const_iterator(this, __p);
1398#else
1399 return const_iterator(__p);
1400#endif
1401}
1402
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001403template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001404inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001405typename vector<_Tp, _Allocator>::iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001406vector<_Tp, _Allocator>::begin() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001407{
1408 return __make_iter(this->__begin_);
1409}
1410
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001411template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001412inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001413typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001414vector<_Tp, _Allocator>::begin() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001415{
1416 return __make_iter(this->__begin_);
1417}
1418
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001419template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001420inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001421typename vector<_Tp, _Allocator>::iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001422vector<_Tp, _Allocator>::end() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001423{
1424 return __make_iter(this->__end_);
1425}
1426
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001427template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001428inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001429typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001430vector<_Tp, _Allocator>::end() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001431{
1432 return __make_iter(this->__end_);
1433}
1434
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001435template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001436inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001437typename vector<_Tp, _Allocator>::reference
Marshall Clowd6470492019-03-15 00:29:35 +00001438vector<_Tp, _Allocator>::operator[](size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001439{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001440 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001441 return this->__begin_[__n];
1442}
1443
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001444template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001445inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001446typename vector<_Tp, _Allocator>::const_reference
Marshall Clowd6470492019-03-15 00:29:35 +00001447vector<_Tp, _Allocator>::operator[](size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001448{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001449 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001450 return this->__begin_[__n];
1451}
1452
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001453template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001454typename vector<_Tp, _Allocator>::reference
1455vector<_Tp, _Allocator>::at(size_type __n)
1456{
1457 if (__n >= size())
1458 this->__throw_out_of_range();
1459 return this->__begin_[__n];
1460}
1461
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001462template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001463typename vector<_Tp, _Allocator>::const_reference
1464vector<_Tp, _Allocator>::at(size_type __n) const
1465{
1466 if (__n >= size())
1467 this->__throw_out_of_range();
1468 return this->__begin_[__n];
1469}
1470
1471template <class _Tp, class _Allocator>
1472void
1473vector<_Tp, _Allocator>::reserve(size_type __n)
1474{
1475 if (__n > capacity())
1476 {
Mikhail Maltsev0b0a51d2021-10-21 10:40:05 +01001477 if (__n > max_size())
1478 this->__throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001479 allocator_type& __a = this->__alloc();
Howard Hinnant1c936782011-06-03 19:40:40 +00001480 __split_buffer<value_type, allocator_type&> __v(__n, size(), __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001481 __swap_out_circular_buffer(__v);
1482 }
1483}
1484
1485template <class _Tp, class _Allocator>
1486void
Howard Hinnant1c936782011-06-03 19:40:40 +00001487vector<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001488{
1489 if (capacity() > size())
1490 {
1491#ifndef _LIBCPP_NO_EXCEPTIONS
1492 try
1493 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001494#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001495 allocator_type& __a = this->__alloc();
Howard Hinnant1c936782011-06-03 19:40:40 +00001496 __split_buffer<value_type, allocator_type&> __v(size(), size(), __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001497 __swap_out_circular_buffer(__v);
1498#ifndef _LIBCPP_NO_EXCEPTIONS
1499 }
1500 catch (...)
1501 {
1502 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001503#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001504 }
1505}
1506
1507template <class _Tp, class _Allocator>
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001508template <class _Up>
1509void
Eric Fiseliered9e9362017-04-16 02:40:45 +00001510#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001511vector<_Tp, _Allocator>::__push_back_slow_path(_Up&& __x)
1512#else
1513vector<_Tp, _Allocator>::__push_back_slow_path(_Up& __x)
1514#endif
1515{
1516 allocator_type& __a = this->__alloc();
1517 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1518 // __v.push_back(_VSTD::forward<_Up>(__x));
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001519 __alloc_traits::construct(__a, _VSTD::__to_address(__v.__end_), _VSTD::forward<_Up>(__x));
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001520 __v.__end_++;
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001521 __swap_out_circular_buffer(__v);
1522}
1523
1524template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001525inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001526void
1527vector<_Tp, _Allocator>::push_back(const_reference __x)
1528{
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001529 if (this->__end_ != this->__end_cap())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001530 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001531 __construct_one_at_end(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001532 }
1533 else
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001534 __push_back_slow_path(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001535}
1536
Eric Fiseliered9e9362017-04-16 02:40:45 +00001537#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001538
1539template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001540inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001541void
1542vector<_Tp, _Allocator>::push_back(value_type&& __x)
1543{
1544 if (this->__end_ < this->__end_cap())
1545 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001546 __construct_one_at_end(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001547 }
1548 else
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001549 __push_back_slow_path(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001550}
1551
1552template <class _Tp, class _Allocator>
1553template <class... _Args>
1554void
Howard Hinnantb6c49562012-02-26 15:30:12 +00001555vector<_Tp, _Allocator>::__emplace_back_slow_path(_Args&&... __args)
1556{
1557 allocator_type& __a = this->__alloc();
1558 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1559// __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001560 __alloc_traits::construct(__a, _VSTD::__to_address(__v.__end_), _VSTD::forward<_Args>(__args)...);
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001561 __v.__end_++;
Howard Hinnantb6c49562012-02-26 15:30:12 +00001562 __swap_out_circular_buffer(__v);
1563}
1564
1565template <class _Tp, class _Allocator>
1566template <class... _Args>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001567inline
Marshall Clowea52cc42017-01-24 23:09:12 +00001568#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001569typename vector<_Tp, _Allocator>::reference
Marshall Clowea52cc42017-01-24 23:09:12 +00001570#else
1571void
1572#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001573vector<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1574{
1575 if (this->__end_ < this->__end_cap())
1576 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001577 __construct_one_at_end(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001578 }
1579 else
Howard Hinnantb6c49562012-02-26 15:30:12 +00001580 __emplace_back_slow_path(_VSTD::forward<_Args>(__args)...);
Marshall Clowea52cc42017-01-24 23:09:12 +00001581#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001582 return this->back();
Marshall Clowea52cc42017-01-24 23:09:12 +00001583#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001584}
1585
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001586#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001587
1588template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001589inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001590void
1591vector<_Tp, _Allocator>::pop_back()
1592{
Kristina Bessonovaaeeaa7e2021-05-09 19:29:56 +02001593 _LIBCPP_ASSERT(!empty(), "vector::pop_back called on an empty vector");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001594 this->__destruct_at_end(this->__end_ - 1);
1595}
1596
1597template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001598inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001599typename vector<_Tp, _Allocator>::iterator
1600vector<_Tp, _Allocator>::erase(const_iterator __position)
1601{
Nikolas Klauserc8984bf2022-01-15 20:23:29 +01001602 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__position)) == this,
1603 "vector::erase(iterator) called with an iterator not referring to this vector");
Howard Hinnant58d52d52013-03-25 22:12:26 +00001604 _LIBCPP_ASSERT(__position != end(),
1605 "vector::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnant76053d72013-06-27 19:35:32 +00001606 difference_type __ps = __position - cbegin();
1607 pointer __p = this->__begin_ + __ps;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001608 this->__destruct_at_end(_VSTD::move(__p + 1, this->__end_, __p));
Eric Fiselier69c51982016-12-28 06:06:09 +00001609 this->__invalidate_iterators_past(__p-1);
1610 iterator __r = __make_iter(__p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001611 return __r;
1612}
1613
1614template <class _Tp, class _Allocator>
1615typename vector<_Tp, _Allocator>::iterator
1616vector<_Tp, _Allocator>::erase(const_iterator __first, const_iterator __last)
1617{
Nikolas Klauserc8984bf2022-01-15 20:23:29 +01001618 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__first)) == this,
1619 "vector::erase(iterator, iterator) called with an iterator not referring to this vector");
1620 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__last)) == this,
1621 "vector::erase(iterator, iterator) called with an iterator not referring to this vector");
1622
Howard Hinnant27e0e772011-09-14 18:33:51 +00001623 _LIBCPP_ASSERT(__first <= __last, "vector::erase(first, last) called with invalid range");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001624 pointer __p = this->__begin_ + (__first - begin());
Eric Fiselier69c51982016-12-28 06:06:09 +00001625 if (__first != __last) {
Howard Hinnantc580cc32013-04-18 15:02:57 +00001626 this->__destruct_at_end(_VSTD::move(__p + (__last - __first), this->__end_, __p));
Eric Fiselier69c51982016-12-28 06:06:09 +00001627 this->__invalidate_iterators_past(__p - 1);
1628 }
1629 iterator __r = __make_iter(__p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001630 return __r;
1631}
1632
1633template <class _Tp, class _Allocator>
1634void
1635vector<_Tp, _Allocator>::__move_range(pointer __from_s, pointer __from_e, pointer __to)
1636{
1637 pointer __old_last = this->__end_;
1638 difference_type __n = __old_last - __to;
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001639 {
1640 pointer __i = __from_s + __n;
1641 _ConstructTransaction __tx(*this, __from_e - __i);
Martijn Vels1e2e1b82020-06-18 13:14:02 -04001642 for (pointer __pos = __tx.__pos_; __i < __from_e;
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04001643 ++__i, (void) ++__pos, __tx.__pos_ = __pos) {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001644 __alloc_traits::construct(this->__alloc(),
Martijn Vels1e2e1b82020-06-18 13:14:02 -04001645 _VSTD::__to_address(__pos),
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001646 _VSTD::move(*__i));
1647 }
1648 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001649 _VSTD::move_backward(__from_s, __from_s + __n, __old_last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001650}
1651
1652template <class _Tp, class _Allocator>
1653typename vector<_Tp, _Allocator>::iterator
1654vector<_Tp, _Allocator>::insert(const_iterator __position, const_reference __x)
1655{
Nikolas Klauserc8984bf2022-01-15 20:23:29 +01001656 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__position)) == this,
1657 "vector::insert(iterator, x) called with an iterator not referring to this vector");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001658 pointer __p = this->__begin_ + (__position - begin());
1659 if (this->__end_ < this->__end_cap())
1660 {
1661 if (__p == this->__end_)
1662 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001663 __construct_one_at_end(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001664 }
1665 else
1666 {
1667 __move_range(__p, this->__end_, __p + 1);
1668 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1669 if (__p <= __xr && __xr < this->__end_)
1670 ++__xr;
1671 *__p = *__xr;
1672 }
1673 }
1674 else
1675 {
1676 allocator_type& __a = this->__alloc();
1677 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
1678 __v.push_back(__x);
1679 __p = __swap_out_circular_buffer(__v, __p);
1680 }
1681 return __make_iter(__p);
1682}
1683
Eric Fiseliered9e9362017-04-16 02:40:45 +00001684#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001685
1686template <class _Tp, class _Allocator>
1687typename vector<_Tp, _Allocator>::iterator
1688vector<_Tp, _Allocator>::insert(const_iterator __position, value_type&& __x)
1689{
Nikolas Klauserc8984bf2022-01-15 20:23:29 +01001690 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__position)) == this,
1691 "vector::insert(iterator, x) called with an iterator not referring to this vector");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001692 pointer __p = this->__begin_ + (__position - begin());
1693 if (this->__end_ < this->__end_cap())
1694 {
1695 if (__p == this->__end_)
1696 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001697 __construct_one_at_end(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001698 }
1699 else
1700 {
1701 __move_range(__p, this->__end_, __p + 1);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001702 *__p = _VSTD::move(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001703 }
1704 }
1705 else
1706 {
1707 allocator_type& __a = this->__alloc();
1708 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001709 __v.push_back(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001710 __p = __swap_out_circular_buffer(__v, __p);
1711 }
1712 return __make_iter(__p);
1713}
1714
1715template <class _Tp, class _Allocator>
1716template <class... _Args>
1717typename vector<_Tp, _Allocator>::iterator
1718vector<_Tp, _Allocator>::emplace(const_iterator __position, _Args&&... __args)
1719{
Nikolas Klauserc8984bf2022-01-15 20:23:29 +01001720 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__position)) == this,
1721 "vector::emplace(iterator, x) called with an iterator not referring to this vector");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001722 pointer __p = this->__begin_ + (__position - begin());
1723 if (this->__end_ < this->__end_cap())
1724 {
1725 if (__p == this->__end_)
1726 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001727 __construct_one_at_end(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001728 }
1729 else
1730 {
Marshall Clowa591b9a2016-07-11 21:38:08 +00001731 __temp_value<value_type, _Allocator> __tmp(this->__alloc(), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001732 __move_range(__p, this->__end_, __p + 1);
Marshall Clowa591b9a2016-07-11 21:38:08 +00001733 *__p = _VSTD::move(__tmp.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001734 }
1735 }
1736 else
1737 {
1738 allocator_type& __a = this->__alloc();
1739 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001740 __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001741 __p = __swap_out_circular_buffer(__v, __p);
1742 }
1743 return __make_iter(__p);
1744}
1745
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001746#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001747
1748template <class _Tp, class _Allocator>
1749typename vector<_Tp, _Allocator>::iterator
1750vector<_Tp, _Allocator>::insert(const_iterator __position, size_type __n, const_reference __x)
1751{
Nikolas Klauserc8984bf2022-01-15 20:23:29 +01001752 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__position)) == this,
1753 "vector::insert(iterator, n, x) called with an iterator not referring to this vector");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001754 pointer __p = this->__begin_ + (__position - begin());
1755 if (__n > 0)
1756 {
1757 if (__n <= static_cast<size_type>(this->__end_cap() - this->__end_))
1758 {
1759 size_type __old_n = __n;
1760 pointer __old_last = this->__end_;
1761 if (__n > static_cast<size_type>(this->__end_ - __p))
1762 {
1763 size_type __cx = __n - (this->__end_ - __p);
1764 __construct_at_end(__cx, __x);
1765 __n -= __cx;
1766 }
1767 if (__n > 0)
1768 {
1769 __move_range(__p, __old_last, __p + __old_n);
1770 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1771 if (__p <= __xr && __xr < this->__end_)
1772 __xr += __old_n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001773 _VSTD::fill_n(__p, __n, *__xr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001774 }
1775 }
1776 else
1777 {
1778 allocator_type& __a = this->__alloc();
1779 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1780 __v.__construct_at_end(__n, __x);
1781 __p = __swap_out_circular_buffer(__v, __p);
1782 }
1783 }
1784 return __make_iter(__p);
1785}
1786
1787template <class _Tp, class _Allocator>
1788template <class _InputIterator>
1789typename enable_if
1790<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001791 __is_cpp17_input_iterator <_InputIterator>::value &&
1792 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001793 is_constructible<
1794 _Tp,
1795 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001796 typename vector<_Tp, _Allocator>::iterator
1797>::type
1798vector<_Tp, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
1799{
Nikolas Klauserc8984bf2022-01-15 20:23:29 +01001800 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__position)) == this,
1801 "vector::insert(iterator, range) called with an iterator not referring to this vector");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001802 difference_type __off = __position - begin();
1803 pointer __p = this->__begin_ + __off;
1804 allocator_type& __a = this->__alloc();
1805 pointer __old_last = this->__end_;
1806 for (; this->__end_ != this->__end_cap() && __first != __last; ++__first)
1807 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001808 __construct_one_at_end(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001809 }
1810 __split_buffer<value_type, allocator_type&> __v(__a);
1811 if (__first != __last)
1812 {
1813#ifndef _LIBCPP_NO_EXCEPTIONS
1814 try
1815 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001816#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001817 __v.__construct_at_end(__first, __last);
1818 difference_type __old_size = __old_last - this->__begin_;
1819 difference_type __old_p = __p - this->__begin_;
1820 reserve(__recommend(size() + __v.size()));
1821 __p = this->__begin_ + __old_p;
1822 __old_last = this->__begin_ + __old_size;
1823#ifndef _LIBCPP_NO_EXCEPTIONS
1824 }
1825 catch (...)
1826 {
1827 erase(__make_iter(__old_last), end());
1828 throw;
1829 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001830#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001831 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001832 __p = _VSTD::rotate(__p, __old_last, this->__end_);
Louis Dionne202a4cb2020-02-11 10:59:12 +01001833 insert(__make_iter(__p), _VSTD::make_move_iterator(__v.begin()),
1834 _VSTD::make_move_iterator(__v.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001835 return begin() + __off;
1836}
1837
1838template <class _Tp, class _Allocator>
1839template <class _ForwardIterator>
1840typename enable_if
1841<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001842 __is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001843 is_constructible<
1844 _Tp,
1845 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001846 typename vector<_Tp, _Allocator>::iterator
1847>::type
1848vector<_Tp, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
1849{
Nikolas Klauserc8984bf2022-01-15 20:23:29 +01001850 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__position)) == this,
1851 "vector::insert(iterator, range) called with an iterator not referring to this vector");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001852 pointer __p = this->__begin_ + (__position - begin());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001853 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001854 if (__n > 0)
1855 {
1856 if (__n <= this->__end_cap() - this->__end_)
1857 {
1858 size_type __old_n = __n;
1859 pointer __old_last = this->__end_;
1860 _ForwardIterator __m = __last;
1861 difference_type __dx = this->__end_ - __p;
1862 if (__n > __dx)
1863 {
1864 __m = __first;
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001865 difference_type __diff = this->__end_ - __p;
1866 _VSTD::advance(__m, __diff);
1867 __construct_at_end(__m, __last, __n - __diff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001868 __n = __dx;
1869 }
1870 if (__n > 0)
1871 {
1872 __move_range(__p, __old_last, __p + __old_n);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001873 _VSTD::copy(__first, __m, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001874 }
1875 }
1876 else
1877 {
1878 allocator_type& __a = this->__alloc();
1879 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1880 __v.__construct_at_end(__first, __last);
1881 __p = __swap_out_circular_buffer(__v, __p);
1882 }
1883 }
1884 return __make_iter(__p);
1885}
1886
1887template <class _Tp, class _Allocator>
1888void
1889vector<_Tp, _Allocator>::resize(size_type __sz)
1890{
1891 size_type __cs = size();
1892 if (__cs < __sz)
1893 this->__append(__sz - __cs);
1894 else if (__cs > __sz)
1895 this->__destruct_at_end(this->__begin_ + __sz);
1896}
1897
1898template <class _Tp, class _Allocator>
1899void
1900vector<_Tp, _Allocator>::resize(size_type __sz, const_reference __x)
1901{
1902 size_type __cs = size();
1903 if (__cs < __sz)
1904 this->__append(__sz - __cs, __x);
1905 else if (__cs > __sz)
1906 this->__destruct_at_end(this->__begin_ + __sz);
1907}
1908
1909template <class _Tp, class _Allocator>
1910void
1911vector<_Tp, _Allocator>::swap(vector& __x)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001912#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00001913 _NOEXCEPT
Marshall Clow8982dcd2015-07-13 20:04:56 +00001914#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00001915 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00001916 __is_nothrow_swappable<allocator_type>::value)
1917#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001918{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001919 _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
1920 this->__alloc() == __x.__alloc(),
1921 "vector::swap: Either propagate_on_container_swap must be true"
1922 " or the allocators must compare equal");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001923 _VSTD::swap(this->__begin_, __x.__begin_);
1924 _VSTD::swap(this->__end_, __x.__end_);
1925 _VSTD::swap(this->__end_cap(), __x.__end_cap());
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05001926 _VSTD::__swap_allocator(this->__alloc(), __x.__alloc(),
Marshall Clow8982dcd2015-07-13 20:04:56 +00001927 integral_constant<bool,__alloc_traits::propagate_on_container_swap::value>());
Louis Dionneba400782020-10-02 15:02:52 -04001928#if _LIBCPP_DEBUG_LEVEL == 2
Mark de Wever8f72c892021-10-10 15:40:50 +02001929 __get_db()->swap(this, _VSTD::addressof(__x));
Louis Dionneba400782020-10-02 15:02:52 -04001930#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001931}
1932
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001933template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001934bool
1935vector<_Tp, _Allocator>::__invariants() const
1936{
Howard Hinnant76053d72013-06-27 19:35:32 +00001937 if (this->__begin_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001938 {
Howard Hinnant76053d72013-06-27 19:35:32 +00001939 if (this->__end_ != nullptr || this->__end_cap() != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001940 return false;
1941 }
1942 else
1943 {
1944 if (this->__begin_ > this->__end_)
1945 return false;
1946 if (this->__begin_ == this->__end_cap())
1947 return false;
1948 if (this->__end_ > this->__end_cap())
1949 return false;
1950 }
1951 return true;
1952}
1953
Louis Dionneba400782020-10-02 15:02:52 -04001954#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001955
Howard Hinnantc51e1022010-05-11 19:42:16 +00001956template <class _Tp, class _Allocator>
Howard Hinnant27e0e772011-09-14 18:33:51 +00001957bool
1958vector<_Tp, _Allocator>::__dereferenceable(const const_iterator* __i) const
1959{
1960 return this->__begin_ <= __i->base() && __i->base() < this->__end_;
1961}
1962
1963template <class _Tp, class _Allocator>
1964bool
1965vector<_Tp, _Allocator>::__decrementable(const const_iterator* __i) const
1966{
1967 return this->__begin_ < __i->base() && __i->base() <= this->__end_;
1968}
1969
1970template <class _Tp, class _Allocator>
1971bool
1972vector<_Tp, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
1973{
1974 const_pointer __p = __i->base() + __n;
1975 return this->__begin_ <= __p && __p <= this->__end_;
1976}
1977
1978template <class _Tp, class _Allocator>
1979bool
1980vector<_Tp, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
1981{
1982 const_pointer __p = __i->base() + __n;
1983 return this->__begin_ <= __p && __p < this->__end_;
1984}
1985
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001986#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001987
1988template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001989inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001990void
1991vector<_Tp, _Allocator>::__invalidate_all_iterators()
1992{
Louis Dionneba400782020-10-02 15:02:52 -04001993#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001994 __get_db()->__invalidate_all(this);
Louis Dionneba400782020-10-02 15:02:52 -04001995#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001996}
1997
Eric Fiselier69c51982016-12-28 06:06:09 +00001998
1999template <class _Tp, class _Allocator>
2000inline _LIBCPP_INLINE_VISIBILITY
2001void
2002vector<_Tp, _Allocator>::__invalidate_iterators_past(pointer __new_last) {
Louis Dionneba400782020-10-02 15:02:52 -04002003#if _LIBCPP_DEBUG_LEVEL == 2
Eric Fiselier69c51982016-12-28 06:06:09 +00002004 __c_node* __c = __get_db()->__find_c_and_lock(this);
2005 for (__i_node** __p = __c->end_; __p != __c->beg_; ) {
2006 --__p;
2007 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
2008 if (__i->base() > __new_last) {
2009 (*__p)->__c_ = nullptr;
2010 if (--__c->end_ != __p)
Arthur O'Dwyer22236632020-12-07 21:50:15 -05002011 _VSTD::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
Eric Fiselier69c51982016-12-28 06:06:09 +00002012 }
2013 }
2014 __get_db()->unlock();
2015#else
2016 ((void)__new_last);
2017#endif
2018}
2019
Howard Hinnantc51e1022010-05-11 19:42:16 +00002020// vector<bool>
2021
2022template <class _Allocator> class vector<bool, _Allocator>;
2023
2024template <class _Allocator> struct hash<vector<bool, _Allocator> >;
2025
2026template <class _Allocator>
Howard Hinnantb4ef6482011-07-02 20:33:23 +00002027struct __has_storage_type<vector<bool, _Allocator> >
2028{
2029 static const bool value = true;
2030};
2031
2032template <class _Allocator>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002033class _LIBCPP_TEMPLATE_VIS vector<bool, _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002034{
2035public:
2036 typedef vector __self;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002037 typedef bool value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002038 typedef _Allocator allocator_type;
2039 typedef allocator_traits<allocator_type> __alloc_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002040 typedef typename __alloc_traits::size_type size_type;
2041 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant896b7622012-05-07 16:50:38 +00002042 typedef size_type __storage_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002043 typedef __bit_iterator<vector, false> pointer;
2044 typedef __bit_iterator<vector, true> const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002045 typedef pointer iterator;
2046 typedef const_pointer const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002047 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
2048 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002049
2050private:
Marshall Clow940e01c2015-04-07 05:21:38 +00002051 typedef typename __rebind_alloc_helper<__alloc_traits, __storage_type>::type __storage_allocator;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002052 typedef allocator_traits<__storage_allocator> __storage_traits;
2053 typedef typename __storage_traits::pointer __storage_pointer;
2054 typedef typename __storage_traits::const_pointer __const_storage_pointer;
2055
2056 __storage_pointer __begin_;
2057 size_type __size_;
2058 __compressed_pair<size_type, __storage_allocator> __cap_alloc_;
Howard Hinnant71d1c192011-07-09 15:50:42 +00002059public:
Howard Hinnantb4ef6482011-07-02 20:33:23 +00002060 typedef __bit_reference<vector> reference;
2061 typedef __bit_const_reference<vector> const_reference;
Howard Hinnant71d1c192011-07-09 15:50:42 +00002062private:
Howard Hinnant1c936782011-06-03 19:40:40 +00002063 _LIBCPP_INLINE_VISIBILITY
2064 size_type& __cap() _NOEXCEPT
2065 {return __cap_alloc_.first();}
2066 _LIBCPP_INLINE_VISIBILITY
2067 const size_type& __cap() const _NOEXCEPT
2068 {return __cap_alloc_.first();}
2069 _LIBCPP_INLINE_VISIBILITY
2070 __storage_allocator& __alloc() _NOEXCEPT
2071 {return __cap_alloc_.second();}
2072 _LIBCPP_INLINE_VISIBILITY
2073 const __storage_allocator& __alloc() const _NOEXCEPT
2074 {return __cap_alloc_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002075
2076 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
2077
Howard Hinnant1c936782011-06-03 19:40:40 +00002078 _LIBCPP_INLINE_VISIBILITY
2079 static size_type __internal_cap_to_external(size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002080 {return __n * __bits_per_word;}
Howard Hinnant1c936782011-06-03 19:40:40 +00002081 _LIBCPP_INLINE_VISIBILITY
2082 static size_type __external_cap_to_internal(size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002083 {return (__n - 1) / __bits_per_word + 1;}
2084
2085public:
Howard Hinnant1c936782011-06-03 19:40:40 +00002086 _LIBCPP_INLINE_VISIBILITY
Marshall Clowe546cbb2015-06-04 02:05:41 +00002087 vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clow8f282f42015-06-04 00:10:20 +00002088
2089 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
2090#if _LIBCPP_STD_VER <= 14
2091 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
2092#else
2093 _NOEXCEPT;
2094#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002095 ~vector();
2096 explicit vector(size_type __n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00002097#if _LIBCPP_STD_VER > 11
2098 explicit vector(size_type __n, const allocator_type& __a);
2099#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002100 vector(size_type __n, const value_type& __v);
2101 vector(size_type __n, const value_type& __v, const allocator_type& __a);
2102 template <class _InputIterator>
2103 vector(_InputIterator __first, _InputIterator __last,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002104 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
2105 !__is_cpp17_forward_iterator<_InputIterator>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002106 template <class _InputIterator>
2107 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002108 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
2109 !__is_cpp17_forward_iterator<_InputIterator>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002110 template <class _ForwardIterator>
2111 vector(_ForwardIterator __first, _ForwardIterator __last,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002112 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002113 template <class _ForwardIterator>
2114 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002115 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002116
2117 vector(const vector& __v);
2118 vector(const vector& __v, const allocator_type& __a);
2119 vector& operator=(const vector& __v);
Eric Fiseliered9e9362017-04-16 02:40:45 +00002120
2121#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002122 vector(initializer_list<value_type> __il);
2123 vector(initializer_list<value_type> __il, const allocator_type& __a);
2124
Howard Hinnant1c936782011-06-03 19:40:40 +00002125 _LIBCPP_INLINE_VISIBILITY
2126 vector(vector&& __v)
Marshall Clowe5108202015-07-14 14:46:32 +00002127#if _LIBCPP_STD_VER > 14
2128 _NOEXCEPT;
2129#else
Howard Hinnant1c936782011-06-03 19:40:40 +00002130 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowe5108202015-07-14 14:46:32 +00002131#endif
Arthur O'Dwyer9b9662f2021-03-01 17:08:24 -05002132 vector(vector&& __v, const __identity_t<allocator_type>& __a);
Howard Hinnant1c936782011-06-03 19:40:40 +00002133 _LIBCPP_INLINE_VISIBILITY
2134 vector& operator=(vector&& __v)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002135 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiseliered9e9362017-04-16 02:40:45 +00002136
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002137 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002138 vector& operator=(initializer_list<value_type> __il)
2139 {assign(__il.begin(), __il.end()); return *this;}
Eric Fiseliered9e9362017-04-16 02:40:45 +00002140
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002141#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002142
2143 template <class _InputIterator>
2144 typename enable_if
2145 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002146 __is_cpp17_input_iterator<_InputIterator>::value &&
2147 !__is_cpp17_forward_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002148 void
2149 >::type
2150 assign(_InputIterator __first, _InputIterator __last);
2151 template <class _ForwardIterator>
2152 typename enable_if
2153 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002154 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002155 void
2156 >::type
2157 assign(_ForwardIterator __first, _ForwardIterator __last);
2158
2159 void assign(size_type __n, const value_type& __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +00002160
2161#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002162 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002163 void assign(initializer_list<value_type> __il)
2164 {assign(__il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +00002165#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002166
Howard Hinnant1c936782011-06-03 19:40:40 +00002167 _LIBCPP_INLINE_VISIBILITY allocator_type get_allocator() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002168 {return allocator_type(this->__alloc());}
2169
Howard Hinnant1c936782011-06-03 19:40:40 +00002170 size_type max_size() const _NOEXCEPT;
2171 _LIBCPP_INLINE_VISIBILITY
2172 size_type capacity() const _NOEXCEPT
2173 {return __internal_cap_to_external(__cap());}
2174 _LIBCPP_INLINE_VISIBILITY
2175 size_type size() const _NOEXCEPT
2176 {return __size_;}
Marshall Clow425f5752017-11-15 05:51:26 +00002177 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +00002178 bool empty() const _NOEXCEPT
2179 {return __size_ == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002180 void reserve(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +00002181 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002182
Howard Hinnant1c936782011-06-03 19:40:40 +00002183 _LIBCPP_INLINE_VISIBILITY
2184 iterator begin() _NOEXCEPT
2185 {return __make_iter(0);}
2186 _LIBCPP_INLINE_VISIBILITY
2187 const_iterator begin() const _NOEXCEPT
2188 {return __make_iter(0);}
2189 _LIBCPP_INLINE_VISIBILITY
2190 iterator end() _NOEXCEPT
2191 {return __make_iter(__size_);}
2192 _LIBCPP_INLINE_VISIBILITY
2193 const_iterator end() const _NOEXCEPT
2194 {return __make_iter(__size_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002195
Howard Hinnant1c936782011-06-03 19:40:40 +00002196 _LIBCPP_INLINE_VISIBILITY
2197 reverse_iterator rbegin() _NOEXCEPT
2198 {return reverse_iterator(end());}
2199 _LIBCPP_INLINE_VISIBILITY
2200 const_reverse_iterator rbegin() const _NOEXCEPT
2201 {return const_reverse_iterator(end());}
2202 _LIBCPP_INLINE_VISIBILITY
2203 reverse_iterator rend() _NOEXCEPT
2204 {return reverse_iterator(begin());}
2205 _LIBCPP_INLINE_VISIBILITY
2206 const_reverse_iterator rend() const _NOEXCEPT
2207 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002208
Howard Hinnant1c936782011-06-03 19:40:40 +00002209 _LIBCPP_INLINE_VISIBILITY
2210 const_iterator cbegin() const _NOEXCEPT
2211 {return __make_iter(0);}
2212 _LIBCPP_INLINE_VISIBILITY
2213 const_iterator cend() const _NOEXCEPT
2214 {return __make_iter(__size_);}
2215 _LIBCPP_INLINE_VISIBILITY
2216 const_reverse_iterator crbegin() const _NOEXCEPT
2217 {return rbegin();}
2218 _LIBCPP_INLINE_VISIBILITY
2219 const_reverse_iterator crend() const _NOEXCEPT
2220 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002221
2222 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) {return __make_ref(__n);}
2223 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const {return __make_ref(__n);}
2224 reference at(size_type __n);
2225 const_reference at(size_type __n) const;
2226
2227 _LIBCPP_INLINE_VISIBILITY reference front() {return __make_ref(0);}
2228 _LIBCPP_INLINE_VISIBILITY const_reference front() const {return __make_ref(0);}
2229 _LIBCPP_INLINE_VISIBILITY reference back() {return __make_ref(__size_ - 1);}
2230 _LIBCPP_INLINE_VISIBILITY const_reference back() const {return __make_ref(__size_ - 1);}
2231
2232 void push_back(const value_type& __x);
Marshall Clowc46bb8e2013-08-13 23:54:12 +00002233#if _LIBCPP_STD_VER > 11
2234 template <class... _Args>
Marshall Clowea52cc42017-01-24 23:09:12 +00002235#if _LIBCPP_STD_VER > 14
2236 _LIBCPP_INLINE_VISIBILITY reference emplace_back(_Args&&... __args)
2237#else
2238 _LIBCPP_INLINE_VISIBILITY void emplace_back(_Args&&... __args)
2239#endif
2240 {
Eric Fiselier34ba5b92016-07-21 03:20:17 +00002241 push_back ( value_type ( _VSTD::forward<_Args>(__args)... ));
Marshall Clowea52cc42017-01-24 23:09:12 +00002242#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00002243 return this->back();
Marshall Clowea52cc42017-01-24 23:09:12 +00002244#endif
Eric Fiselier34ba5b92016-07-21 03:20:17 +00002245 }
Marshall Clowc46bb8e2013-08-13 23:54:12 +00002246#endif
2247
Howard Hinnantc51e1022010-05-11 19:42:16 +00002248 _LIBCPP_INLINE_VISIBILITY void pop_back() {--__size_;}
2249
Marshall Clowc46bb8e2013-08-13 23:54:12 +00002250#if _LIBCPP_STD_VER > 11
2251 template <class... _Args>
2252 _LIBCPP_INLINE_VISIBILITY iterator emplace(const_iterator position, _Args&&... __args)
2253 { return insert ( position, value_type ( _VSTD::forward<_Args>(__args)... )); }
2254#endif
2255
Howard Hinnantc51e1022010-05-11 19:42:16 +00002256 iterator insert(const_iterator __position, const value_type& __x);
2257 iterator insert(const_iterator __position, size_type __n, const value_type& __x);
2258 iterator insert(const_iterator __position, size_type __n, const_reference __x);
2259 template <class _InputIterator>
2260 typename enable_if
2261 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002262 __is_cpp17_input_iterator <_InputIterator>::value &&
2263 !__is_cpp17_forward_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002264 iterator
2265 >::type
2266 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
2267 template <class _ForwardIterator>
2268 typename enable_if
2269 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002270 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002271 iterator
2272 >::type
2273 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiseliered9e9362017-04-16 02:40:45 +00002274
2275#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002276 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002277 iterator insert(const_iterator __position, initializer_list<value_type> __il)
2278 {return insert(__position, __il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +00002279#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002280
Howard Hinnantcf823322010-12-17 14:46:43 +00002281 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002282 iterator erase(const_iterator __first, const_iterator __last);
2283
Howard Hinnant1c936782011-06-03 19:40:40 +00002284 _LIBCPP_INLINE_VISIBILITY
2285 void clear() _NOEXCEPT {__size_ = 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002286
Howard Hinnant1c936782011-06-03 19:40:40 +00002287 void swap(vector&)
Marshall Clow8982dcd2015-07-13 20:04:56 +00002288#if _LIBCPP_STD_VER >= 14
2289 _NOEXCEPT;
2290#else
Eric Fiselier69c51982016-12-28 06:06:09 +00002291 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00002292 __is_nothrow_swappable<allocator_type>::value);
2293#endif
Marshall Clow89eb3822016-04-07 14:20:31 +00002294 static void swap(reference __x, reference __y) _NOEXCEPT { _VSTD::swap(__x, __y); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002295
2296 void resize(size_type __sz, value_type __x = false);
Howard Hinnant1c936782011-06-03 19:40:40 +00002297 void flip() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002298
2299 bool __invariants() const;
2300
2301private:
Nikolas Klauser129974f2022-02-03 23:09:37 +01002302 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
2303 void __throw_length_error() const {
2304 _VSTD::__throw_length_error("vector");
2305 }
2306
2307 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
2308 void __throw_out_of_range() const {
2309 _VSTD::__throw_out_of_range("vector");
2310 }
2311
Howard Hinnantcf823322010-12-17 14:46:43 +00002312 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Marshall Clow3ff48e02018-05-22 16:20:28 +00002313 void __vallocate(size_type __n);
2314 void __vdeallocate() _NOEXCEPT;
Howard Hinnant1c936782011-06-03 19:40:40 +00002315 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantea382952013-08-14 18:00:20 +00002316 static size_type __align_it(size_type __new_size) _NOEXCEPT
Eric Fiselierb41db9a2018-10-01 01:59:37 +00002317 {return __new_size + (__bits_per_word-1) & ~((size_type)__bits_per_word-1);}
Howard Hinnantcf823322010-12-17 14:46:43 +00002318 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
2319 _LIBCPP_INLINE_VISIBILITY void __construct_at_end(size_type __n, bool __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002320 template <class _ForwardIterator>
2321 typename enable_if
2322 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002323 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002324 void
2325 >::type
2326 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
2327 void __append(size_type __n, const_reference __x);
Howard Hinnant1c936782011-06-03 19:40:40 +00002328 _LIBCPP_INLINE_VISIBILITY
2329 reference __make_ref(size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002330 {return reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnant1c936782011-06-03 19:40:40 +00002331 _LIBCPP_INLINE_VISIBILITY
2332 const_reference __make_ref(size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002333 {return const_reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnant1c936782011-06-03 19:40:40 +00002334 _LIBCPP_INLINE_VISIBILITY
2335 iterator __make_iter(size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002336 {return iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnant1c936782011-06-03 19:40:40 +00002337 _LIBCPP_INLINE_VISIBILITY
2338 const_iterator __make_iter(size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002339 {return const_iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnant1c936782011-06-03 19:40:40 +00002340 _LIBCPP_INLINE_VISIBILITY
2341 iterator __const_iterator_cast(const_iterator __p) _NOEXCEPT
Howard Hinnant76053d72013-06-27 19:35:32 +00002342 {return begin() + (__p - cbegin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002343
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002344 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002345 void __copy_assign_alloc(const vector& __v)
2346 {__copy_assign_alloc(__v, integral_constant<bool,
2347 __storage_traits::propagate_on_container_copy_assignment::value>());}
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002348 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002349 void __copy_assign_alloc(const vector& __c, true_type)
2350 {
2351 if (__alloc() != __c.__alloc())
Marshall Clow3ff48e02018-05-22 16:20:28 +00002352 __vdeallocate();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002353 __alloc() = __c.__alloc();
2354 }
2355
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002356 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00002357 void __copy_assign_alloc(const vector&, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002358 {}
2359
2360 void __move_assign(vector& __c, false_type);
Howard Hinnant1c936782011-06-03 19:40:40 +00002361 void __move_assign(vector& __c, true_type)
2362 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002363 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002364 void __move_assign_alloc(vector& __c)
Howard Hinnant1c936782011-06-03 19:40:40 +00002365 _NOEXCEPT_(
2366 !__storage_traits::propagate_on_container_move_assignment::value ||
2367 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002368 {__move_assign_alloc(__c, integral_constant<bool,
2369 __storage_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002370 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +00002371 void __move_assign_alloc(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002372 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002373 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002374 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002375 }
2376
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002377 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00002378 void __move_assign_alloc(vector&, false_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002379 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002380 {}
2381
Howard Hinnant1c936782011-06-03 19:40:40 +00002382 size_t __hash_code() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002383
2384 friend class __bit_reference<vector>;
2385 friend class __bit_const_reference<vector>;
2386 friend class __bit_iterator<vector, false>;
2387 friend class __bit_iterator<vector, true>;
Howard Hinnant4210a0f2012-08-17 17:10:18 +00002388 friend struct __bit_array<vector>;
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002389 friend struct _LIBCPP_TEMPLATE_VIS hash<vector>;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002390};
2391
2392template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002393inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002394void
2395vector<bool, _Allocator>::__invalidate_all_iterators()
2396{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002397}
2398
2399// Allocate space for __n objects
2400// throws length_error if __n > max_size()
2401// throws (probably bad_alloc) if memory run out
2402// Precondition: __begin_ == __end_ == __cap() == 0
2403// Precondition: __n > 0
2404// Postcondition: capacity() == __n
2405// Postcondition: size() == 0
2406template <class _Allocator>
2407void
Marshall Clow3ff48e02018-05-22 16:20:28 +00002408vector<bool, _Allocator>::__vallocate(size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002409{
2410 if (__n > max_size())
2411 this->__throw_length_error();
2412 __n = __external_cap_to_internal(__n);
2413 this->__begin_ = __storage_traits::allocate(this->__alloc(), __n);
2414 this->__size_ = 0;
2415 this->__cap() = __n;
2416}
2417
2418template <class _Allocator>
2419void
Marshall Clow3ff48e02018-05-22 16:20:28 +00002420vector<bool, _Allocator>::__vdeallocate() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002421{
Howard Hinnant76053d72013-06-27 19:35:32 +00002422 if (this->__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002423 {
2424 __storage_traits::deallocate(this->__alloc(), this->__begin_, __cap());
2425 __invalidate_all_iterators();
Howard Hinnant76053d72013-06-27 19:35:32 +00002426 this->__begin_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002427 this->__size_ = this->__cap() = 0;
2428 }
2429}
2430
2431template <class _Allocator>
2432typename vector<bool, _Allocator>::size_type
Howard Hinnant1c936782011-06-03 19:40:40 +00002433vector<bool, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002434{
2435 size_type __amax = __storage_traits::max_size(__alloc());
2436 size_type __nmax = numeric_limits<size_type>::max() / 2; // end() >= begin(), always
2437 if (__nmax / __bits_per_word <= __amax)
2438 return __nmax;
2439 return __internal_cap_to_external(__amax);
2440}
2441
2442// Precondition: __new_size > capacity()
2443template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002444inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002445typename vector<bool, _Allocator>::size_type
2446vector<bool, _Allocator>::__recommend(size_type __new_size) const
2447{
2448 const size_type __ms = max_size();
2449 if (__new_size > __ms)
2450 this->__throw_length_error();
2451 const size_type __cap = capacity();
2452 if (__cap >= __ms / 2)
2453 return __ms;
Arthur O'Dwyerc0fb14b2021-07-26 18:23:00 -04002454 return _VSTD::max(2 * __cap, __align_it(__new_size));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002455}
2456
2457// Default constructs __n objects starting at __end_
2458// Precondition: __n > 0
2459// Precondition: size() + __n <= capacity()
2460// Postcondition: size() == size() + __n
2461template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002462inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002463void
2464vector<bool, _Allocator>::__construct_at_end(size_type __n, bool __x)
2465{
2466 size_type __old_size = this->__size_;
2467 this->__size_ += __n;
Marshall Clow1893ec72018-10-23 20:07:45 +00002468 if (__old_size == 0 || ((__old_size - 1) / __bits_per_word) != ((this->__size_ - 1) / __bits_per_word))
2469 {
2470 if (this->__size_ <= __bits_per_word)
2471 this->__begin_[0] = __storage_type(0);
2472 else
2473 this->__begin_[(this->__size_ - 1) / __bits_per_word] = __storage_type(0);
2474 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002475 _VSTD::fill_n(__make_iter(__old_size), __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002476}
2477
2478template <class _Allocator>
2479template <class _ForwardIterator>
2480typename enable_if
2481<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002482 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002483 void
2484>::type
2485vector<bool, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
2486{
2487 size_type __old_size = this->__size_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002488 this->__size_ += _VSTD::distance(__first, __last);
Marshall Clow1893ec72018-10-23 20:07:45 +00002489 if (__old_size == 0 || ((__old_size - 1) / __bits_per_word) != ((this->__size_ - 1) / __bits_per_word))
2490 {
2491 if (this->__size_ <= __bits_per_word)
2492 this->__begin_[0] = __storage_type(0);
2493 else
2494 this->__begin_[(this->__size_ - 1) / __bits_per_word] = __storage_type(0);
2495 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002496 _VSTD::copy(__first, __last, __make_iter(__old_size));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002497}
2498
2499template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002500inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002501vector<bool, _Allocator>::vector()
Marshall Clowe546cbb2015-06-04 02:05:41 +00002502 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant76053d72013-06-27 19:35:32 +00002503 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002504 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002505 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002506{
2507}
2508
2509template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002510inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002511vector<bool, _Allocator>::vector(const allocator_type& __a)
Marshall Clow8f282f42015-06-04 00:10:20 +00002512#if _LIBCPP_STD_VER <= 14
2513 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
2514#else
2515 _NOEXCEPT
2516#endif
Howard Hinnant76053d72013-06-27 19:35:32 +00002517 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002518 __size_(0),
2519 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2520{
2521}
2522
2523template <class _Allocator>
2524vector<bool, _Allocator>::vector(size_type __n)
Howard Hinnant76053d72013-06-27 19:35:32 +00002525 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002526 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002527 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002528{
2529 if (__n > 0)
2530 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002531 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002532 __construct_at_end(__n, false);
2533 }
2534}
2535
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00002536#if _LIBCPP_STD_VER > 11
2537template <class _Allocator>
2538vector<bool, _Allocator>::vector(size_type __n, const allocator_type& __a)
2539 : __begin_(nullptr),
2540 __size_(0),
2541 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2542{
2543 if (__n > 0)
2544 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002545 __vallocate(__n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00002546 __construct_at_end(__n, false);
2547 }
2548}
2549#endif
2550
Howard Hinnantc51e1022010-05-11 19:42:16 +00002551template <class _Allocator>
2552vector<bool, _Allocator>::vector(size_type __n, const value_type& __x)
Howard Hinnant76053d72013-06-27 19:35:32 +00002553 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002554 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002555 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002556{
2557 if (__n > 0)
2558 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002559 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002560 __construct_at_end(__n, __x);
2561 }
2562}
2563
2564template <class _Allocator>
2565vector<bool, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002566 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002567 __size_(0),
2568 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2569{
2570 if (__n > 0)
2571 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002572 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002573 __construct_at_end(__n, __x);
2574 }
2575}
2576
2577template <class _Allocator>
2578template <class _InputIterator>
2579vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002580 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
2581 !__is_cpp17_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002582 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002583 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002584 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002585{
2586#ifndef _LIBCPP_NO_EXCEPTIONS
2587 try
2588 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002589#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002590 for (; __first != __last; ++__first)
2591 push_back(*__first);
2592#ifndef _LIBCPP_NO_EXCEPTIONS
2593 }
2594 catch (...)
2595 {
Howard Hinnant76053d72013-06-27 19:35:32 +00002596 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002597 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2598 __invalidate_all_iterators();
2599 throw;
2600 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002601#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002602}
2603
2604template <class _Allocator>
2605template <class _InputIterator>
2606vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002607 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
2608 !__is_cpp17_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002609 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002610 __size_(0),
2611 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2612{
2613#ifndef _LIBCPP_NO_EXCEPTIONS
2614 try
2615 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002616#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002617 for (; __first != __last; ++__first)
2618 push_back(*__first);
2619#ifndef _LIBCPP_NO_EXCEPTIONS
2620 }
2621 catch (...)
2622 {
Howard Hinnant76053d72013-06-27 19:35:32 +00002623 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002624 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2625 __invalidate_all_iterators();
2626 throw;
2627 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002628#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002629}
2630
2631template <class _Allocator>
2632template <class _ForwardIterator>
2633vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002634 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002635 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002636 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002637 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002638{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002639 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002640 if (__n > 0)
2641 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002642 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002643 __construct_at_end(__first, __last);
2644 }
2645}
2646
2647template <class _Allocator>
2648template <class _ForwardIterator>
2649vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002650 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002651 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002652 __size_(0),
2653 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2654{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002655 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002656 if (__n > 0)
2657 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002658 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002659 __construct_at_end(__first, __last);
2660 }
2661}
2662
Eric Fiseliered9e9362017-04-16 02:40:45 +00002663#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002664
Howard Hinnantc51e1022010-05-11 19:42:16 +00002665template <class _Allocator>
2666vector<bool, _Allocator>::vector(initializer_list<value_type> __il)
Howard Hinnant76053d72013-06-27 19:35:32 +00002667 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002668 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002669 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002670{
2671 size_type __n = static_cast<size_type>(__il.size());
2672 if (__n > 0)
2673 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002674 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002675 __construct_at_end(__il.begin(), __il.end());
2676 }
2677}
2678
2679template <class _Allocator>
2680vector<bool, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002681 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002682 __size_(0),
2683 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2684{
2685 size_type __n = static_cast<size_type>(__il.size());
2686 if (__n > 0)
2687 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002688 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002689 __construct_at_end(__il.begin(), __il.end());
2690 }
2691}
2692
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002693#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002694
Howard Hinnantc51e1022010-05-11 19:42:16 +00002695template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002696vector<bool, _Allocator>::~vector()
2697{
Howard Hinnant76053d72013-06-27 19:35:32 +00002698 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002699 __storage_traits::deallocate(__alloc(), __begin_, __cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002700 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002701}
2702
2703template <class _Allocator>
2704vector<bool, _Allocator>::vector(const vector& __v)
Howard Hinnant76053d72013-06-27 19:35:32 +00002705 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002706 __size_(0),
2707 __cap_alloc_(0, __storage_traits::select_on_container_copy_construction(__v.__alloc()))
2708{
2709 if (__v.size() > 0)
2710 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002711 __vallocate(__v.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002712 __construct_at_end(__v.begin(), __v.end());
2713 }
2714}
2715
2716template <class _Allocator>
2717vector<bool, _Allocator>::vector(const vector& __v, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002718 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002719 __size_(0),
2720 __cap_alloc_(0, __a)
2721{
2722 if (__v.size() > 0)
2723 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002724 __vallocate(__v.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002725 __construct_at_end(__v.begin(), __v.end());
2726 }
2727}
2728
2729template <class _Allocator>
2730vector<bool, _Allocator>&
2731vector<bool, _Allocator>::operator=(const vector& __v)
2732{
Mark de Wever357a1fc2021-09-28 19:15:18 +02002733 if (this != _VSTD::addressof(__v))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002734 {
2735 __copy_assign_alloc(__v);
2736 if (__v.__size_)
2737 {
2738 if (__v.__size_ > capacity())
2739 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002740 __vdeallocate();
2741 __vallocate(__v.__size_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002742 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002743 _VSTD::copy(__v.__begin_, __v.__begin_ + __external_cap_to_internal(__v.__size_), __begin_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002744 }
2745 __size_ = __v.__size_;
2746 }
2747 return *this;
2748}
2749
Eric Fiseliered9e9362017-04-16 02:40:45 +00002750#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant74279a52010-09-04 23:28:19 +00002751
Howard Hinnantc51e1022010-05-11 19:42:16 +00002752template <class _Allocator>
Eric Fiselier4f1534c2018-06-05 22:32:52 +00002753inline _LIBCPP_INLINE_VISIBILITY vector<bool, _Allocator>::vector(vector&& __v)
Marshall Clowe5108202015-07-14 14:46:32 +00002754#if _LIBCPP_STD_VER > 14
Eric Fiselier4f1534c2018-06-05 22:32:52 +00002755 _NOEXCEPT
Marshall Clowe5108202015-07-14 14:46:32 +00002756#else
Eric Fiselier4f1534c2018-06-05 22:32:52 +00002757 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowe5108202015-07-14 14:46:32 +00002758#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002759 : __begin_(__v.__begin_),
2760 __size_(__v.__size_),
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05002761 __cap_alloc_(_VSTD::move(__v.__cap_alloc_)) {
Howard Hinnant76053d72013-06-27 19:35:32 +00002762 __v.__begin_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002763 __v.__size_ = 0;
2764 __v.__cap() = 0;
2765}
2766
2767template <class _Allocator>
Arthur O'Dwyer9b9662f2021-03-01 17:08:24 -05002768vector<bool, _Allocator>::vector(vector&& __v, const __identity_t<allocator_type>& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002769 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002770 __size_(0),
2771 __cap_alloc_(0, __a)
2772{
2773 if (__a == allocator_type(__v.__alloc()))
2774 {
2775 this->__begin_ = __v.__begin_;
2776 this->__size_ = __v.__size_;
2777 this->__cap() = __v.__cap();
2778 __v.__begin_ = nullptr;
2779 __v.__cap() = __v.__size_ = 0;
2780 }
2781 else if (__v.size() > 0)
2782 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002783 __vallocate(__v.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002784 __construct_at_end(__v.begin(), __v.end());
2785 }
2786}
2787
2788template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002789inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002790vector<bool, _Allocator>&
2791vector<bool, _Allocator>::operator=(vector&& __v)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002792 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002793{
2794 __move_assign(__v, integral_constant<bool,
2795 __storage_traits::propagate_on_container_move_assignment::value>());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002796 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002797}
2798
2799template <class _Allocator>
2800void
2801vector<bool, _Allocator>::__move_assign(vector& __c, false_type)
2802{
2803 if (__alloc() != __c.__alloc())
2804 assign(__c.begin(), __c.end());
2805 else
2806 __move_assign(__c, true_type());
2807}
2808
2809template <class _Allocator>
2810void
2811vector<bool, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002812 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002813{
Marshall Clow3ff48e02018-05-22 16:20:28 +00002814 __vdeallocate();
Marshall Clowb4871fa2014-07-21 15:15:15 +00002815 __move_assign_alloc(__c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002816 this->__begin_ = __c.__begin_;
2817 this->__size_ = __c.__size_;
2818 this->__cap() = __c.__cap();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002819 __c.__begin_ = nullptr;
2820 __c.__cap() = __c.__size_ = 0;
2821}
Howard Hinnant74279a52010-09-04 23:28:19 +00002822
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002823#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002824
2825template <class _Allocator>
2826void
2827vector<bool, _Allocator>::assign(size_type __n, const value_type& __x)
2828{
2829 __size_ = 0;
2830 if (__n > 0)
2831 {
2832 size_type __c = capacity();
2833 if (__n <= __c)
2834 __size_ = __n;
2835 else
2836 {
Mikhail Maltsevf56b0692021-10-21 10:38:56 +01002837 vector __v(get_allocator());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002838 __v.reserve(__recommend(__n));
2839 __v.__size_ = __n;
2840 swap(__v);
2841 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002842 _VSTD::fill_n(begin(), __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002843 }
Eric Fiselier69c51982016-12-28 06:06:09 +00002844 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002845}
2846
2847template <class _Allocator>
2848template <class _InputIterator>
2849typename enable_if
2850<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002851 __is_cpp17_input_iterator<_InputIterator>::value &&
2852 !__is_cpp17_forward_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002853 void
2854>::type
2855vector<bool, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2856{
2857 clear();
2858 for (; __first != __last; ++__first)
2859 push_back(*__first);
2860}
2861
2862template <class _Allocator>
2863template <class _ForwardIterator>
2864typename enable_if
2865<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002866 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002867 void
2868>::type
2869vector<bool, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2870{
2871 clear();
Eric Fiselier6003c772016-12-23 23:37:52 +00002872 difference_type __ns = _VSTD::distance(__first, __last);
2873 _LIBCPP_ASSERT(__ns >= 0, "invalid range specified");
2874 const size_t __n = static_cast<size_type>(__ns);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002875 if (__n)
2876 {
2877 if (__n > capacity())
2878 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002879 __vdeallocate();
2880 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002881 }
2882 __construct_at_end(__first, __last);
2883 }
2884}
2885
2886template <class _Allocator>
2887void
2888vector<bool, _Allocator>::reserve(size_type __n)
2889{
2890 if (__n > capacity())
2891 {
Mikhail Maltsev0b0a51d2021-10-21 10:40:05 +01002892 if (__n > max_size())
2893 this->__throw_length_error();
Mikhail Maltsevf56b0692021-10-21 10:38:56 +01002894 vector __v(this->get_allocator());
Marshall Clow3ff48e02018-05-22 16:20:28 +00002895 __v.__vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002896 __v.__construct_at_end(this->begin(), this->end());
2897 swap(__v);
2898 __invalidate_all_iterators();
2899 }
2900}
2901
2902template <class _Allocator>
2903void
Howard Hinnant1c936782011-06-03 19:40:40 +00002904vector<bool, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002905{
2906 if (__external_cap_to_internal(size()) > __cap())
2907 {
2908#ifndef _LIBCPP_NO_EXCEPTIONS
2909 try
2910 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002911#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002912 vector(*this, allocator_type(__alloc())).swap(*this);
2913#ifndef _LIBCPP_NO_EXCEPTIONS
2914 }
2915 catch (...)
2916 {
2917 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002918#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002919 }
2920}
2921
2922template <class _Allocator>
2923typename vector<bool, _Allocator>::reference
2924vector<bool, _Allocator>::at(size_type __n)
2925{
2926 if (__n >= size())
2927 this->__throw_out_of_range();
2928 return (*this)[__n];
2929}
2930
2931template <class _Allocator>
2932typename vector<bool, _Allocator>::const_reference
2933vector<bool, _Allocator>::at(size_type __n) const
2934{
2935 if (__n >= size())
2936 this->__throw_out_of_range();
2937 return (*this)[__n];
2938}
2939
2940template <class _Allocator>
2941void
2942vector<bool, _Allocator>::push_back(const value_type& __x)
2943{
2944 if (this->__size_ == this->capacity())
2945 reserve(__recommend(this->__size_ + 1));
2946 ++this->__size_;
2947 back() = __x;
2948}
2949
2950template <class _Allocator>
2951typename vector<bool, _Allocator>::iterator
2952vector<bool, _Allocator>::insert(const_iterator __position, const value_type& __x)
2953{
2954 iterator __r;
2955 if (size() < capacity())
2956 {
2957 const_iterator __old_end = end();
2958 ++__size_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002959 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002960 __r = __const_iterator_cast(__position);
2961 }
2962 else
2963 {
Mikhail Maltsevf56b0692021-10-21 10:38:56 +01002964 vector __v(get_allocator());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002965 __v.reserve(__recommend(__size_ + 1));
2966 __v.__size_ = __size_ + 1;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002967 __r = _VSTD::copy(cbegin(), __position, __v.begin());
2968 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002969 swap(__v);
2970 }
2971 *__r = __x;
2972 return __r;
2973}
2974
2975template <class _Allocator>
2976typename vector<bool, _Allocator>::iterator
2977vector<bool, _Allocator>::insert(const_iterator __position, size_type __n, const value_type& __x)
2978{
2979 iterator __r;
2980 size_type __c = capacity();
2981 if (__n <= __c && size() <= __c - __n)
2982 {
2983 const_iterator __old_end = end();
2984 __size_ += __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002985 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002986 __r = __const_iterator_cast(__position);
2987 }
2988 else
2989 {
Mikhail Maltsevf56b0692021-10-21 10:38:56 +01002990 vector __v(get_allocator());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002991 __v.reserve(__recommend(__size_ + __n));
2992 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002993 __r = _VSTD::copy(cbegin(), __position, __v.begin());
2994 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002995 swap(__v);
2996 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002997 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002998 return __r;
2999}
3000
3001template <class _Allocator>
3002template <class _InputIterator>
3003typename enable_if
3004<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05003005 __is_cpp17_input_iterator <_InputIterator>::value &&
3006 !__is_cpp17_forward_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003007 typename vector<bool, _Allocator>::iterator
3008>::type
3009vector<bool, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
3010{
3011 difference_type __off = __position - begin();
3012 iterator __p = __const_iterator_cast(__position);
3013 iterator __old_end = end();
3014 for (; size() != capacity() && __first != __last; ++__first)
3015 {
3016 ++this->__size_;
3017 back() = *__first;
3018 }
Mikhail Maltsevf56b0692021-10-21 10:38:56 +01003019 vector __v(get_allocator());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003020 if (__first != __last)
3021 {
3022#ifndef _LIBCPP_NO_EXCEPTIONS
3023 try
3024 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003025#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003026 __v.assign(__first, __last);
3027 difference_type __old_size = static_cast<difference_type>(__old_end - begin());
3028 difference_type __old_p = __p - begin();
3029 reserve(__recommend(size() + __v.size()));
3030 __p = begin() + __old_p;
3031 __old_end = begin() + __old_size;
3032#ifndef _LIBCPP_NO_EXCEPTIONS
3033 }
3034 catch (...)
3035 {
3036 erase(__old_end, end());
3037 throw;
3038 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003039#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003040 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003041 __p = _VSTD::rotate(__p, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003042 insert(__p, __v.begin(), __v.end());
3043 return begin() + __off;
3044}
3045
3046template <class _Allocator>
3047template <class _ForwardIterator>
3048typename enable_if
3049<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05003050 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003051 typename vector<bool, _Allocator>::iterator
3052>::type
3053vector<bool, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
3054{
Eric Fiselier654dd332016-12-11 05:31:00 +00003055 const difference_type __n_signed = _VSTD::distance(__first, __last);
3056 _LIBCPP_ASSERT(__n_signed >= 0, "invalid range specified");
3057 const size_type __n = static_cast<size_type>(__n_signed);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003058 iterator __r;
3059 size_type __c = capacity();
3060 if (__n <= __c && size() <= __c - __n)
3061 {
3062 const_iterator __old_end = end();
3063 __size_ += __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003064 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003065 __r = __const_iterator_cast(__position);
3066 }
3067 else
3068 {
Mikhail Maltsevf56b0692021-10-21 10:38:56 +01003069 vector __v(get_allocator());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003070 __v.reserve(__recommend(__size_ + __n));
3071 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003072 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3073 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003074 swap(__v);
3075 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003076 _VSTD::copy(__first, __last, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003077 return __r;
3078}
3079
3080template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003081inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003082typename vector<bool, _Allocator>::iterator
3083vector<bool, _Allocator>::erase(const_iterator __position)
3084{
3085 iterator __r = __const_iterator_cast(__position);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003086 _VSTD::copy(__position + 1, this->cend(), __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003087 --__size_;
3088 return __r;
3089}
3090
3091template <class _Allocator>
3092typename vector<bool, _Allocator>::iterator
3093vector<bool, _Allocator>::erase(const_iterator __first, const_iterator __last)
3094{
3095 iterator __r = __const_iterator_cast(__first);
3096 difference_type __d = __last - __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003097 _VSTD::copy(__last, this->cend(), __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003098 __size_ -= __d;
3099 return __r;
3100}
3101
3102template <class _Allocator>
3103void
3104vector<bool, _Allocator>::swap(vector& __x)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003105#if _LIBCPP_STD_VER >= 14
3106 _NOEXCEPT
3107#else
Eric Fiselier69c51982016-12-28 06:06:09 +00003108 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003109 __is_nothrow_swappable<allocator_type>::value)
3110#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003111{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003112 _VSTD::swap(this->__begin_, __x.__begin_);
3113 _VSTD::swap(this->__size_, __x.__size_);
3114 _VSTD::swap(this->__cap(), __x.__cap());
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05003115 _VSTD::__swap_allocator(this->__alloc(), __x.__alloc(),
Marshall Clow8982dcd2015-07-13 20:04:56 +00003116 integral_constant<bool, __alloc_traits::propagate_on_container_swap::value>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003117}
3118
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003119template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003120void
3121vector<bool, _Allocator>::resize(size_type __sz, value_type __x)
3122{
3123 size_type __cs = size();
3124 if (__cs < __sz)
3125 {
3126 iterator __r;
3127 size_type __c = capacity();
3128 size_type __n = __sz - __cs;
3129 if (__n <= __c && __cs <= __c - __n)
3130 {
3131 __r = end();
3132 __size_ += __n;
3133 }
3134 else
3135 {
Mikhail Maltsevf56b0692021-10-21 10:38:56 +01003136 vector __v(get_allocator());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003137 __v.reserve(__recommend(__size_ + __n));
3138 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003139 __r = _VSTD::copy(cbegin(), cend(), __v.begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003140 swap(__v);
3141 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003142 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003143 }
3144 else
3145 __size_ = __sz;
3146}
3147
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003148template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003149void
Howard Hinnant1c936782011-06-03 19:40:40 +00003150vector<bool, _Allocator>::flip() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003151{
3152 // do middle whole words
3153 size_type __n = __size_;
3154 __storage_pointer __p = __begin_;
3155 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3156 *__p = ~*__p;
3157 // do last partial word
3158 if (__n > 0)
3159 {
3160 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3161 __storage_type __b = *__p & __m;
3162 *__p &= ~__m;
3163 *__p |= ~__b & __m;
3164 }
3165}
3166
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003167template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003168bool
3169vector<bool, _Allocator>::__invariants() const
3170{
Howard Hinnant76053d72013-06-27 19:35:32 +00003171 if (this->__begin_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003172 {
3173 if (this->__size_ != 0 || this->__cap() != 0)
3174 return false;
3175 }
3176 else
3177 {
3178 if (this->__cap() == 0)
3179 return false;
3180 if (this->__size_ > this->capacity())
3181 return false;
3182 }
3183 return true;
3184}
3185
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003186template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003187size_t
Howard Hinnant1c936782011-06-03 19:40:40 +00003188vector<bool, _Allocator>::__hash_code() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003189{
3190 size_t __h = 0;
3191 // do middle whole words
3192 size_type __n = __size_;
3193 __storage_pointer __p = __begin_;
3194 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3195 __h ^= *__p;
3196 // do last partial word
3197 if (__n > 0)
3198 {
3199 const __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3200 __h ^= *__p & __m;
3201 }
3202 return __h;
3203}
3204
3205template <class _Allocator>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003206struct _LIBCPP_TEMPLATE_VIS hash<vector<bool, _Allocator> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00003207 : public unary_function<vector<bool, _Allocator>, size_t>
3208{
Howard Hinnant1c265cd2010-09-23 18:58:28 +00003209 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +00003210 size_t operator()(const vector<bool, _Allocator>& __vec) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003211 {return __vec.__hash_code();}
3212};
3213
3214template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003215inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003216bool
3217operator==(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3218{
3219 const typename vector<_Tp, _Allocator>::size_type __sz = __x.size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003220 return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003221}
3222
3223template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003224inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003225bool
3226operator!=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3227{
3228 return !(__x == __y);
3229}
3230
3231template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003232inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003233bool
3234operator< (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3235{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003236 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003237}
3238
3239template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003240inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003241bool
3242operator> (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3243{
3244 return __y < __x;
3245}
3246
3247template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003248inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003249bool
3250operator>=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3251{
3252 return !(__x < __y);
3253}
3254
3255template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003256inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003257bool
3258operator<=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3259{
3260 return !(__y < __x);
3261}
3262
3263template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003264inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003265void
3266swap(vector<_Tp, _Allocator>& __x, vector<_Tp, _Allocator>& __y)
Howard Hinnant1c936782011-06-03 19:40:40 +00003267 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00003268{
3269 __x.swap(__y);
3270}
3271
Marshall Clow29b53f22018-12-14 18:49:35 +00003272#if _LIBCPP_STD_VER > 17
3273template <class _Tp, class _Allocator, class _Up>
Marek Kurdeja98b1412020-05-02 13:58:03 +02003274inline _LIBCPP_INLINE_VISIBILITY typename vector<_Tp, _Allocator>::size_type
3275erase(vector<_Tp, _Allocator>& __c, const _Up& __v) {
3276 auto __old_size = __c.size();
3277 __c.erase(_VSTD::remove(__c.begin(), __c.end(), __v), __c.end());
3278 return __old_size - __c.size();
3279}
Marshall Clow29b53f22018-12-14 18:49:35 +00003280
3281template <class _Tp, class _Allocator, class _Predicate>
Marek Kurdeja98b1412020-05-02 13:58:03 +02003282inline _LIBCPP_INLINE_VISIBILITY typename vector<_Tp, _Allocator>::size_type
3283erase_if(vector<_Tp, _Allocator>& __c, _Predicate __pred) {
3284 auto __old_size = __c.size();
3285 __c.erase(_VSTD::remove_if(__c.begin(), __c.end(), __pred), __c.end());
3286 return __old_size - __c.size();
3287}
Marshall Clow29b53f22018-12-14 18:49:35 +00003288#endif
3289
Howard Hinnantc51e1022010-05-11 19:42:16 +00003290_LIBCPP_END_NAMESPACE_STD
3291
Eric Fiselierf4433a32017-05-31 22:07:49 +00003292_LIBCPP_POP_MACROS
3293
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003294#endif // _LIBCPP_VECTOR