blob: f19aaa1cbd7b6faf931015d34bd437932cf73c23 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===------------------------------ vector --------------------------------===//
3//
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())
248 -> vector<typename iterator_traits<InputIterator>::value_type, Allocator>;
249
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
274#include <__config>
275#include <__bit_reference>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400276#include <__debug>
277#include <__functional_base>
Louis Dionne77249522021-06-11 09:55:11 -0400278#include <__iterator/wrap_iter.h>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400279#include <__split_buffer>
Christopher Di Bella41f26e82021-06-05 02:47:47 +0000280#include <__utility/forward.h>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400281#include <algorithm>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000282#include <climits>
Arthur O'Dwyer7deec122021-03-24 18:19:12 -0400283#include <compare>
Louis Dionned24191c2021-08-19 12:39:16 -0400284#include <cstdlib>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400285#include <cstring>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000286#include <initializer_list>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400287#include <iosfwd> // for forward declaration of vector
288#include <limits>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000289#include <memory>
290#include <stdexcept>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400291#include <type_traits>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000292#include <version>
Howard Hinnante6ff0b62013-08-02 00:26:35 +0000293
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000294#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000295#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000296#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000297
Eric Fiselierf4433a32017-05-31 22:07:49 +0000298_LIBCPP_PUSH_MACROS
299#include <__undef_macros>
300
301
Howard Hinnantc51e1022010-05-11 19:42:16 +0000302_LIBCPP_BEGIN_NAMESPACE_STD
303
304template <bool>
Louis Dionneb6aabd92021-08-19 12:21:06 -0400305struct __vector_base_common;
306
307template <>
308struct __vector_base_common<true> {
309 // Both are defined in vector.cpp
310 _LIBCPP_NORETURN _LIBCPP_EXPORTED_FROM_ABI void __throw_length_error() const;
311 _LIBCPP_NORETURN _LIBCPP_EXPORTED_FROM_ABI void __throw_out_of_range() const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000312};
313
Howard Hinnantc51e1022010-05-11 19:42:16 +0000314template <class _Tp, class _Allocator>
315class __vector_base
Louis Dionneb6aabd92021-08-19 12:21:06 -0400316 : protected __vector_base_common<true> // This base class is historical, but it needs to remain for ABI compatibility
Howard Hinnantc51e1022010-05-11 19:42:16 +0000317{
Konstantin Varlamov5f23d662021-11-08 00:10:13 -0800318 typedef _Allocator allocator_type;
319 typedef typename allocator_traits<allocator_type>::pointer pointer;
320
Marshall Clowf0ca1492018-05-21 21:30:12 +0000321protected:
Konstantin Varlamov5f23d662021-11-08 00:10:13 -0800322 pointer __begin_;
323 pointer __end_;
324 __compressed_pair<pointer, allocator_type> __end_cap_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000325
Howard Hinnant1c936782011-06-03 19:40:40 +0000326 _LIBCPP_INLINE_VISIBILITY
327 __vector_base()
Howard Hinnant1c936782011-06-03 19:40:40 +0000328 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Konstantin Varlamov5f23d662021-11-08 00:10:13 -0800329 : __begin_(nullptr),
330 __end_(nullptr),
331 __end_cap_(nullptr, __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000332
Konstantin Varlamov5f23d662021-11-08 00:10:13 -0800333 _LIBCPP_INLINE_VISIBILITY __vector_base(const allocator_type& __a)
334 : __begin_(nullptr),
335 __end_(nullptr),
336 __end_cap_(nullptr, __a) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000337
Eric Fiselier4f1534c2018-06-05 22:32:52 +0000338#ifndef _LIBCPP_CXX03_LANG
Konstantin Varlamov5f23d662021-11-08 00:10:13 -0800339 _LIBCPP_INLINE_VISIBILITY __vector_base(allocator_type&& __a) _NOEXCEPT
340 : __begin_(nullptr),
341 __end_(nullptr),
342 __end_cap_(nullptr, _VSTD::move(__a)) {}
Eric Fiselier4f1534c2018-06-05 22:32:52 +0000343#endif
Konstantin Varlamov5f23d662021-11-08 00:10:13 -0800344};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000345
Eric Fiselier876c6862016-02-20 00:19:45 +0000346template <class _Tp, class _Allocator /* = allocator<_Tp> */>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000347class _LIBCPP_TEMPLATE_VIS vector
Konstantin Varlamov5f23d662021-11-08 00:10:13 -0800348 // This base class is historical, but it needs to remain for ABI compatibility.
Howard Hinnantc51e1022010-05-11 19:42:16 +0000349 : private __vector_base<_Tp, _Allocator>
350{
351private:
352 typedef __vector_base<_Tp, _Allocator> __base;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000353 typedef allocator<_Tp> __default_allocator_type;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000354public:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000355 typedef vector __self;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000356 typedef _Tp value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000357 typedef _Allocator allocator_type;
Konstantin Varlamov5f23d662021-11-08 00:10:13 -0800358 typedef allocator_traits<allocator_type> __alloc_traits;
359 typedef value_type& reference;
360 typedef const value_type& const_reference;
361 typedef typename __alloc_traits::size_type size_type;
362 typedef typename __alloc_traits::difference_type difference_type;
363 typedef typename __alloc_traits::pointer pointer;
364 typedef typename __alloc_traits::const_pointer const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000365 typedef __wrap_iter<pointer> iterator;
366 typedef __wrap_iter<const_pointer> const_iterator;
Konstantin Varlamov5f23d662021-11-08 00:10:13 -0800367 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
368 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000369
Howard Hinnanta416ff02013-03-26 19:04:56 +0000370 static_assert((is_same<typename allocator_type::value_type, value_type>::value),
371 "Allocator::value_type must be same type as value_type");
372
Howard Hinnant1c936782011-06-03 19:40:40 +0000373 _LIBCPP_INLINE_VISIBILITY
Marshall Clowe546cbb2015-06-04 02:05:41 +0000374 vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant27e0e772011-09-14 18:33:51 +0000375 {
Louis Dionneba400782020-10-02 15:02:52 -0400376#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000377 __get_db()->__insert_c(this);
378#endif
379 }
380 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
Marshall Clow8f282f42015-06-04 00:10:20 +0000381#if _LIBCPP_STD_VER <= 14
382 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
383#else
384 _NOEXCEPT
385#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +0000386 : __base(__a)
387 {
Louis Dionneba400782020-10-02 15:02:52 -0400388#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000389 __get_db()->__insert_c(this);
390#endif
391 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000392 explicit vector(size_type __n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +0000393#if _LIBCPP_STD_VER > 11
394 explicit vector(size_type __n, const allocator_type& __a);
395#endif
Marshall Clowf0ca1492018-05-21 21:30:12 +0000396 vector(size_type __n, const value_type& __x);
397 vector(size_type __n, const value_type& __x, const allocator_type& __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000398 template <class _InputIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +0000399 vector(_InputIterator __first,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500400 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
401 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000402 is_constructible<
403 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +0000404 typename iterator_traits<_InputIterator>::reference>::value,
405 _InputIterator>::type __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000406 template <class _InputIterator>
407 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500408 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
409 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000410 is_constructible<
411 value_type,
412 typename iterator_traits<_InputIterator>::reference>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000413 template <class _ForwardIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +0000414 vector(_ForwardIterator __first,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500415 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000416 is_constructible<
417 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +0000418 typename iterator_traits<_ForwardIterator>::reference>::value,
419 _ForwardIterator>::type __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000420 template <class _ForwardIterator>
421 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500422 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000423 is_constructible<
424 value_type,
425 typename iterator_traits<_ForwardIterator>::reference>::value>::type* = 0);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000426
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000427 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant27e0e772011-09-14 18:33:51 +0000428 ~vector()
429 {
Konstantin Varlamov5f23d662021-11-08 00:10:13 -0800430 __annotate_delete();
Louis Dionneba400782020-10-02 15:02:52 -0400431#if _LIBCPP_DEBUG_LEVEL == 2
Konstantin Varlamov5f23d662021-11-08 00:10:13 -0800432 __get_db()->__erase_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000433#endif
Konstantin Varlamov5f23d662021-11-08 00:10:13 -0800434
435 if (this->__begin_ != nullptr)
436 {
437 __clear();
438 __alloc_traits::deallocate(__alloc(), this->__begin_, capacity());
439 }
Marshall Clow68af4f32018-09-07 15:47:59 +0000440 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000441
442 vector(const vector& __x);
Arthur O'Dwyer9b9662f2021-03-01 17:08:24 -0500443 vector(const vector& __x, const __identity_t<allocator_type>& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000444 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000445 vector& operator=(const vector& __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000446
447#ifndef _LIBCPP_CXX03_LANG
448 _LIBCPP_INLINE_VISIBILITY
449 vector(initializer_list<value_type> __il);
450
451 _LIBCPP_INLINE_VISIBILITY
452 vector(initializer_list<value_type> __il, const allocator_type& __a);
453
Howard Hinnantcf823322010-12-17 14:46:43 +0000454 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000455 vector(vector&& __x)
Marshall Clowe5108202015-07-14 14:46:32 +0000456#if _LIBCPP_STD_VER > 14
457 _NOEXCEPT;
458#else
Howard Hinnant1c936782011-06-03 19:40:40 +0000459 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowe5108202015-07-14 14:46:32 +0000460#endif
Eric Fiseliered9e9362017-04-16 02:40:45 +0000461
Howard Hinnantcf823322010-12-17 14:46:43 +0000462 _LIBCPP_INLINE_VISIBILITY
Arthur O'Dwyer9b9662f2021-03-01 17:08:24 -0500463 vector(vector&& __x, const __identity_t<allocator_type>& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000464 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000465 vector& operator=(vector&& __x)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000466 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiseliered9e9362017-04-16 02:40:45 +0000467
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000468 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000469 vector& operator=(initializer_list<value_type> __il)
470 {assign(__il.begin(), __il.end()); return *this;}
Eric Fiseliered9e9362017-04-16 02:40:45 +0000471
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400472#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000473
474 template <class _InputIterator>
475 typename enable_if
476 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500477 __is_cpp17_input_iterator <_InputIterator>::value &&
478 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000479 is_constructible<
480 value_type,
481 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000482 void
483 >::type
484 assign(_InputIterator __first, _InputIterator __last);
485 template <class _ForwardIterator>
486 typename enable_if
487 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500488 __is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000489 is_constructible<
490 value_type,
491 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000492 void
493 >::type
494 assign(_ForwardIterator __first, _ForwardIterator __last);
495
496 void assign(size_type __n, const_reference __u);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000497
498#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000499 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000500 void assign(initializer_list<value_type> __il)
501 {assign(__il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +0000502#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000503
Howard Hinnant1c936782011-06-03 19:40:40 +0000504 _LIBCPP_INLINE_VISIBILITY
505 allocator_type get_allocator() const _NOEXCEPT
506 {return this->__alloc();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000507
Howard Hinnant1c936782011-06-03 19:40:40 +0000508 _LIBCPP_INLINE_VISIBILITY iterator begin() _NOEXCEPT;
509 _LIBCPP_INLINE_VISIBILITY const_iterator begin() const _NOEXCEPT;
510 _LIBCPP_INLINE_VISIBILITY iterator end() _NOEXCEPT;
511 _LIBCPP_INLINE_VISIBILITY const_iterator end() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000512
Howard Hinnant1c936782011-06-03 19:40:40 +0000513 _LIBCPP_INLINE_VISIBILITY
514 reverse_iterator rbegin() _NOEXCEPT
515 {return reverse_iterator(end());}
516 _LIBCPP_INLINE_VISIBILITY
517 const_reverse_iterator rbegin() const _NOEXCEPT
518 {return const_reverse_iterator(end());}
519 _LIBCPP_INLINE_VISIBILITY
520 reverse_iterator rend() _NOEXCEPT
521 {return reverse_iterator(begin());}
522 _LIBCPP_INLINE_VISIBILITY
523 const_reverse_iterator rend() const _NOEXCEPT
524 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000525
Howard Hinnant1c936782011-06-03 19:40:40 +0000526 _LIBCPP_INLINE_VISIBILITY
527 const_iterator cbegin() const _NOEXCEPT
528 {return begin();}
529 _LIBCPP_INLINE_VISIBILITY
530 const_iterator cend() const _NOEXCEPT
531 {return end();}
532 _LIBCPP_INLINE_VISIBILITY
533 const_reverse_iterator crbegin() const _NOEXCEPT
534 {return rbegin();}
535 _LIBCPP_INLINE_VISIBILITY
536 const_reverse_iterator crend() const _NOEXCEPT
537 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000538
Howard Hinnant1c936782011-06-03 19:40:40 +0000539 _LIBCPP_INLINE_VISIBILITY
540 size_type size() const _NOEXCEPT
541 {return static_cast<size_type>(this->__end_ - this->__begin_);}
542 _LIBCPP_INLINE_VISIBILITY
543 size_type capacity() const _NOEXCEPT
Konstantin Varlamov5f23d662021-11-08 00:10:13 -0800544 {return static_cast<size_type>(__end_cap() - this->__begin_);}
Marshall Clow425f5752017-11-15 05:51:26 +0000545 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000546 bool empty() const _NOEXCEPT
547 {return this->__begin_ == this->__end_;}
548 size_type max_size() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000549 void reserve(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +0000550 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000551
Marshall Clowd6470492019-03-15 00:29:35 +0000552 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) _NOEXCEPT;
553 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000554 reference at(size_type __n);
555 const_reference at(size_type __n) const;
556
Marshall Clowd6470492019-03-15 00:29:35 +0000557 _LIBCPP_INLINE_VISIBILITY reference front() _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000558 {
Kristina Bessonovaaeeaa7e2021-05-09 19:29:56 +0200559 _LIBCPP_ASSERT(!empty(), "front() called on an empty vector");
Howard Hinnant27e0e772011-09-14 18:33:51 +0000560 return *this->__begin_;
561 }
Marshall Clowd6470492019-03-15 00:29:35 +0000562 _LIBCPP_INLINE_VISIBILITY const_reference front() const _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000563 {
Kristina Bessonovaaeeaa7e2021-05-09 19:29:56 +0200564 _LIBCPP_ASSERT(!empty(), "front() called on an empty vector");
Howard Hinnant27e0e772011-09-14 18:33:51 +0000565 return *this->__begin_;
566 }
Marshall Clowd6470492019-03-15 00:29:35 +0000567 _LIBCPP_INLINE_VISIBILITY reference back() _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000568 {
Kristina Bessonovaaeeaa7e2021-05-09 19:29:56 +0200569 _LIBCPP_ASSERT(!empty(), "back() called on an empty vector");
Howard Hinnant27e0e772011-09-14 18:33:51 +0000570 return *(this->__end_ - 1);
571 }
Marshall Clowd6470492019-03-15 00:29:35 +0000572 _LIBCPP_INLINE_VISIBILITY const_reference back() const _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000573 {
Kristina Bessonovaaeeaa7e2021-05-09 19:29:56 +0200574 _LIBCPP_ASSERT(!empty(), "back() called on an empty vector");
Howard Hinnant27e0e772011-09-14 18:33:51 +0000575 return *(this->__end_ - 1);
576 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000577
Howard Hinnant1c936782011-06-03 19:40:40 +0000578 _LIBCPP_INLINE_VISIBILITY
579 value_type* data() _NOEXCEPT
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500580 {return _VSTD::__to_address(this->__begin_);}
Howard Hinnant1c936782011-06-03 19:40:40 +0000581 _LIBCPP_INLINE_VISIBILITY
582 const value_type* data() const _NOEXCEPT
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500583 {return _VSTD::__to_address(this->__begin_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000584
Eric Fiselier96919722017-10-17 13:03:17 +0000585#ifdef _LIBCPP_CXX03_LANG
586 _LIBCPP_INLINE_VISIBILITY
587 void __emplace_back(const value_type& __x) { push_back(__x); }
588#else
589 template <class _Arg>
590 _LIBCPP_INLINE_VISIBILITY
591 void __emplace_back(_Arg&& __arg) {
592 emplace_back(_VSTD::forward<_Arg>(__arg));
593 }
594#endif
595
Howard Hinnantcf823322010-12-17 14:46:43 +0000596 _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000597
598#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000599 _LIBCPP_INLINE_VISIBILITY void push_back(value_type&& __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000600
Howard Hinnantc51e1022010-05-11 19:42:16 +0000601 template <class... _Args>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000602 _LIBCPP_INLINE_VISIBILITY
Marshall Clowea52cc42017-01-24 23:09:12 +0000603#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +0000604 reference emplace_back(_Args&&... __args);
Marshall Clowea52cc42017-01-24 23:09:12 +0000605#else
606 void emplace_back(_Args&&... __args);
607#endif
Eric Fiseliered9e9362017-04-16 02:40:45 +0000608#endif // !_LIBCPP_CXX03_LANG
609
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000610 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000611 void pop_back();
612
613 iterator insert(const_iterator __position, const_reference __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000614
615#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000616 iterator insert(const_iterator __position, value_type&& __x);
617 template <class... _Args>
618 iterator emplace(const_iterator __position, _Args&&... __args);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400619#endif // !_LIBCPP_CXX03_LANG
Eric Fiseliered9e9362017-04-16 02:40:45 +0000620
Howard Hinnantc51e1022010-05-11 19:42:16 +0000621 iterator insert(const_iterator __position, size_type __n, const_reference __x);
622 template <class _InputIterator>
623 typename enable_if
624 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500625 __is_cpp17_input_iterator <_InputIterator>::value &&
626 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000627 is_constructible<
628 value_type,
629 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000630 iterator
631 >::type
632 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
633 template <class _ForwardIterator>
634 typename enable_if
635 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500636 __is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000637 is_constructible<
638 value_type,
639 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000640 iterator
641 >::type
642 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000643
644#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000645 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000646 iterator insert(const_iterator __position, initializer_list<value_type> __il)
647 {return insert(__position, __il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +0000648#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000649
Howard Hinnantcf823322010-12-17 14:46:43 +0000650 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000651 iterator erase(const_iterator __first, const_iterator __last);
652
Howard Hinnant1c936782011-06-03 19:40:40 +0000653 _LIBCPP_INLINE_VISIBILITY
654 void clear() _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000655 {
Marshall Clow2cd9d372014-05-08 14:14:06 +0000656 size_type __old_size = size();
Konstantin Varlamov5f23d662021-11-08 00:10:13 -0800657 __clear();
Marshall Clow2cd9d372014-05-08 14:14:06 +0000658 __annotate_shrink(__old_size);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000659 __invalidate_all_iterators();
660 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000661
662 void resize(size_type __sz);
663 void resize(size_type __sz, const_reference __x);
664
Howard Hinnant1c936782011-06-03 19:40:40 +0000665 void swap(vector&)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000666#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +0000667 _NOEXCEPT;
Marshall Clow8982dcd2015-07-13 20:04:56 +0000668#else
Eric Fiselier873b8d32019-03-18 21:50:12 +0000669 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +0000670 __is_nothrow_swappable<allocator_type>::value);
671#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000672
673 bool __invariants() const;
674
Louis Dionneba400782020-10-02 15:02:52 -0400675#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000676
677 bool __dereferenceable(const const_iterator* __i) const;
678 bool __decrementable(const const_iterator* __i) const;
679 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
680 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
681
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400682#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000683
Howard Hinnantc51e1022010-05-11 19:42:16 +0000684private:
Howard Hinnantcf823322010-12-17 14:46:43 +0000685 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Eric Fiselier69c51982016-12-28 06:06:09 +0000686 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(pointer __new_last);
Marshall Clow3ff48e02018-05-22 16:20:28 +0000687 void __vallocate(size_type __n);
688 void __vdeallocate() _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000689 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
Howard Hinnanta5589382011-01-04 19:53:31 +0000690 void __construct_at_end(size_type __n);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000691 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000692 void __construct_at_end(size_type __n, const_reference __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000693 template <class _ForwardIterator>
694 typename enable_if
695 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500696 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000697 void
698 >::type
Eric Fiselieracfe6f02015-03-31 16:54:19 +0000699 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000700 void __append(size_type __n);
701 void __append(size_type __n, const_reference __x);
Howard Hinnantcf823322010-12-17 14:46:43 +0000702 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000703 iterator __make_iter(pointer __p) _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000704 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000705 const_iterator __make_iter(const_pointer __p) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000706 void __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v);
707 pointer __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p);
708 void __move_range(pointer __from_s, pointer __from_e, pointer __to);
Howard Hinnant1c936782011-06-03 19:40:40 +0000709 void __move_assign(vector& __c, true_type)
710 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000711 void __move_assign(vector& __c, false_type)
712 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000713 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76053d72013-06-27 19:35:32 +0000714 void __destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000715 {
Eric Fiselier69c51982016-12-28 06:06:09 +0000716 __invalidate_iterators_past(__new_last);
Marshall Clow2cd9d372014-05-08 14:14:06 +0000717 size_type __old_size = size();
Konstantin Varlamov5f23d662021-11-08 00:10:13 -0800718 __base_destruct_at_end(__new_last);
Marshall Clow2cd9d372014-05-08 14:14:06 +0000719 __annotate_shrink(__old_size);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000720 }
Eric Fiseliered9e9362017-04-16 02:40:45 +0000721
722#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier7110f442019-03-19 19:19:44 +0000723 template <class _Up>
724 _LIBCPP_INLINE_VISIBILITY
725 inline void __push_back_slow_path(_Up&& __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000726
Howard Hinnantb6c49562012-02-26 15:30:12 +0000727 template <class... _Args>
Eric Fiselier7110f442019-03-19 19:19:44 +0000728 _LIBCPP_INLINE_VISIBILITY
729 inline void __emplace_back_slow_path(_Args&&... __args);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000730#else
Eric Fiselier7110f442019-03-19 19:19:44 +0000731 template <class _Up>
732 _LIBCPP_INLINE_VISIBILITY
733 inline void __push_back_slow_path(_Up& __x);
Howard Hinnantb6c49562012-02-26 15:30:12 +0000734#endif
Eric Fiseliered9e9362017-04-16 02:40:45 +0000735
Marshall Clow2cd9d372014-05-08 14:14:06 +0000736 // The following functions are no-ops outside of AddressSanitizer mode.
737 // We call annotatations only for the default Allocator because other allocators
738 // may not meet the AddressSanitizer alignment constraints.
739 // See the documentation for __sanitizer_annotate_contiguous_container for more details.
Marshall Clow2cd9d372014-05-08 14:14:06 +0000740#ifndef _LIBCPP_HAS_NO_ASAN
Eric Fiselier6003c772016-12-23 23:37:52 +0000741 void __annotate_contiguous_container(const void *__beg, const void *__end,
742 const void *__old_mid,
743 const void *__new_mid) const
744 {
745
Marshall Clow2cd9d372014-05-08 14:14:06 +0000746 if (__beg && is_same<allocator_type, __default_allocator_type>::value)
747 __sanitizer_annotate_contiguous_container(__beg, __end, __old_mid, __new_mid);
Marshall Clow2cd9d372014-05-08 14:14:06 +0000748 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000749#else
750 _LIBCPP_INLINE_VISIBILITY
751 void __annotate_contiguous_container(const void*, const void*, const void*,
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000752 const void*) const _NOEXCEPT {}
Eric Fiselier6003c772016-12-23 23:37:52 +0000753#endif
754 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000755 void __annotate_new(size_type __current_size) const _NOEXCEPT {
Marshall Clow2cd9d372014-05-08 14:14:06 +0000756 __annotate_contiguous_container(data(), data() + capacity(),
757 data() + capacity(), data() + __current_size);
758 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000759
760 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000761 void __annotate_delete() const _NOEXCEPT {
Marshall Clow2cd9d372014-05-08 14:14:06 +0000762 __annotate_contiguous_container(data(), data() + capacity(),
763 data() + size(), data() + capacity());
764 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000765
766 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000767 void __annotate_increase(size_type __n) const _NOEXCEPT
Marshall Clow2cd9d372014-05-08 14:14:06 +0000768 {
769 __annotate_contiguous_container(data(), data() + capacity(),
770 data() + size(), data() + size() + __n);
771 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000772
773 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000774 void __annotate_shrink(size_type __old_size) const _NOEXCEPT
Marshall Clow2cd9d372014-05-08 14:14:06 +0000775 {
776 __annotate_contiguous_container(data(), data() + capacity(),
777 data() + __old_size, data() + size());
778 }
Marshall Clowc78ffa52014-09-03 21:37:43 +0000779
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000780 struct _ConstructTransaction {
781 explicit _ConstructTransaction(vector &__v, size_type __n)
Mark de Weverdcafc462021-10-21 18:29:14 +0200782 : __v_(__v), __pos_(__v.__end_), __new_end_(__v.__end_ + __n) {
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000783#ifndef _LIBCPP_HAS_NO_ASAN
784 __v_.__annotate_increase(__n);
785#endif
786 }
787 ~_ConstructTransaction() {
788 __v_.__end_ = __pos_;
789#ifndef _LIBCPP_HAS_NO_ASAN
790 if (__pos_ != __new_end_) {
791 __v_.__annotate_shrink(__new_end_ - __v_.__begin_);
792 }
793#endif
794 }
795
796 vector &__v_;
797 pointer __pos_;
798 const_pointer const __new_end_;
799
800 private:
801 _ConstructTransaction(_ConstructTransaction const&) = delete;
802 _ConstructTransaction& operator=(_ConstructTransaction const&) = delete;
803 };
804
805 template <class ..._Args>
806 _LIBCPP_INLINE_VISIBILITY
807 void __construct_one_at_end(_Args&& ...__args) {
808 _ConstructTransaction __tx(*this, 1);
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500809 __alloc_traits::construct(this->__alloc(), _VSTD::__to_address(__tx.__pos_),
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000810 _VSTD::forward<_Args>(__args)...);
811 ++__tx.__pos_;
812 }
Konstantin Varlamov5f23d662021-11-08 00:10:13 -0800813
814 _LIBCPP_INLINE_VISIBILITY
815 allocator_type& __alloc() _NOEXCEPT
816 {return this->__end_cap_.second();}
817 _LIBCPP_INLINE_VISIBILITY
818 const allocator_type& __alloc() const _NOEXCEPT
819 {return this->__end_cap_.second();}
820 _LIBCPP_INLINE_VISIBILITY
821 pointer& __end_cap() _NOEXCEPT
822 {return this->__end_cap_.first();}
823 _LIBCPP_INLINE_VISIBILITY
824 const pointer& __end_cap() const _NOEXCEPT
825 {return this->__end_cap_.first();}
826
827 _LIBCPP_INLINE_VISIBILITY
828 void __clear() _NOEXCEPT {__base_destruct_at_end(this->__begin_);}
829
830 _LIBCPP_INLINE_VISIBILITY
831 void __base_destruct_at_end(pointer __new_last) _NOEXCEPT {
832 pointer __soon_to_be_end = this->__end_;
833 while (__new_last != __soon_to_be_end)
834 __alloc_traits::destroy(__alloc(), _VSTD::__to_address(--__soon_to_be_end));
835 this->__end_ = __new_last;
836 }
837
838 _LIBCPP_INLINE_VISIBILITY
839 void __copy_assign_alloc(const vector& __c)
840 {__copy_assign_alloc(__c, integral_constant<bool,
841 __alloc_traits::propagate_on_container_copy_assignment::value>());}
842
843 _LIBCPP_INLINE_VISIBILITY
844 void __move_assign_alloc(vector& __c)
845 _NOEXCEPT_(
846 !__alloc_traits::propagate_on_container_move_assignment::value ||
847 is_nothrow_move_assignable<allocator_type>::value)
848 {__move_assign_alloc(__c, integral_constant<bool,
849 __alloc_traits::propagate_on_container_move_assignment::value>());}
850
851 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
852 void __throw_length_error() const {
853#ifndef _LIBCPP_NO_EXCEPTIONS
854 __vector_base_common<true>::__throw_length_error();
855#else
856 _VSTD::abort();
857#endif
858 }
859
860 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
861 void __throw_out_of_range() const {
862#ifndef _LIBCPP_NO_EXCEPTIONS
863 __vector_base_common<true>::__throw_out_of_range();
864#else
865 _VSTD::abort();
866#endif
867 }
868
869 _LIBCPP_INLINE_VISIBILITY
870 void __copy_assign_alloc(const vector& __c, true_type)
871 {
872 if (__alloc() != __c.__alloc())
873 {
874 __clear();
875 __alloc_traits::deallocate(__alloc(), this->__begin_, capacity());
876 this->__begin_ = this->__end_ = __end_cap() = nullptr;
877 }
878 __alloc() = __c.__alloc();
879 }
880
881 _LIBCPP_INLINE_VISIBILITY
882 void __copy_assign_alloc(const vector&, false_type)
883 {}
884
885 _LIBCPP_INLINE_VISIBILITY
886 void __move_assign_alloc(vector& __c, true_type)
887 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
888 {
889 __alloc() = _VSTD::move(__c.__alloc());
890 }
891
892 _LIBCPP_INLINE_VISIBILITY
893 void __move_assign_alloc(vector&, false_type)
894 _NOEXCEPT
895 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000896};
897
Louis Dionned59f8a52021-08-17 11:59:07 -0400898#if _LIBCPP_STD_VER >= 17
Marshall Clowf0ca1492018-05-21 21:30:12 +0000899template<class _InputIterator,
Arthur O'Dwyer56226762021-03-03 23:02:20 -0500900 class _Alloc = allocator<__iter_value_type<_InputIterator>>,
Louis Dionne25547162021-08-17 12:26:09 -0400901 class = enable_if_t<__is_allocator<_Alloc>::value>
Marshall Clowf0ca1492018-05-21 21:30:12 +0000902 >
903vector(_InputIterator, _InputIterator)
Arthur O'Dwyer56226762021-03-03 23:02:20 -0500904 -> vector<__iter_value_type<_InputIterator>, _Alloc>;
Marshall Clowf0ca1492018-05-21 21:30:12 +0000905
906template<class _InputIterator,
907 class _Alloc,
Louis Dionne25547162021-08-17 12:26:09 -0400908 class = enable_if_t<__is_allocator<_Alloc>::value>
Marshall Clowf0ca1492018-05-21 21:30:12 +0000909 >
910vector(_InputIterator, _InputIterator, _Alloc)
Arthur O'Dwyer56226762021-03-03 23:02:20 -0500911 -> vector<__iter_value_type<_InputIterator>, _Alloc>;
Marshall Clowf0ca1492018-05-21 21:30:12 +0000912#endif
913
Howard Hinnantc51e1022010-05-11 19:42:16 +0000914template <class _Tp, class _Allocator>
915void
916vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v)
917{
Eric Fiselier909fe962019-09-13 16:09:33 +0000918
Marshall Clow2cd9d372014-05-08 14:14:06 +0000919 __annotate_delete();
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -0500920 _VSTD::__construct_backward_with_exception_guarantees(this->__alloc(), this->__begin_, this->__end_, __v.__begin_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000921 _VSTD::swap(this->__begin_, __v.__begin_);
922 _VSTD::swap(this->__end_, __v.__end_);
923 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000924 __v.__first_ = __v.__begin_;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000925 __annotate_new(size());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000926 __invalidate_all_iterators();
927}
928
929template <class _Tp, class _Allocator>
930typename vector<_Tp, _Allocator>::pointer
931vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p)
932{
Marshall Clow2cd9d372014-05-08 14:14:06 +0000933 __annotate_delete();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000934 pointer __r = __v.__begin_;
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -0500935 _VSTD::__construct_backward_with_exception_guarantees(this->__alloc(), this->__begin_, __p, __v.__begin_);
936 _VSTD::__construct_forward_with_exception_guarantees(this->__alloc(), __p, this->__end_, __v.__end_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000937 _VSTD::swap(this->__begin_, __v.__begin_);
938 _VSTD::swap(this->__end_, __v.__end_);
939 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000940 __v.__first_ = __v.__begin_;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000941 __annotate_new(size());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000942 __invalidate_all_iterators();
943 return __r;
944}
945
946// Allocate space for __n objects
947// throws length_error if __n > max_size()
948// throws (probably bad_alloc) if memory run out
949// Precondition: __begin_ == __end_ == __end_cap() == 0
950// Precondition: __n > 0
951// Postcondition: capacity() == __n
952// Postcondition: size() == 0
953template <class _Tp, class _Allocator>
954void
Marshall Clow3ff48e02018-05-22 16:20:28 +0000955vector<_Tp, _Allocator>::__vallocate(size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000956{
957 if (__n > max_size())
958 this->__throw_length_error();
959 this->__begin_ = this->__end_ = __alloc_traits::allocate(this->__alloc(), __n);
960 this->__end_cap() = this->__begin_ + __n;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000961 __annotate_new(0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000962}
963
964template <class _Tp, class _Allocator>
965void
Marshall Clow3ff48e02018-05-22 16:20:28 +0000966vector<_Tp, _Allocator>::__vdeallocate() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000967{
Howard Hinnant76053d72013-06-27 19:35:32 +0000968 if (this->__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000969 {
970 clear();
971 __alloc_traits::deallocate(this->__alloc(), this->__begin_, capacity());
Howard Hinnant76053d72013-06-27 19:35:32 +0000972 this->__begin_ = this->__end_ = this->__end_cap() = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000973 }
974}
975
976template <class _Tp, class _Allocator>
977typename vector<_Tp, _Allocator>::size_type
Howard Hinnant1c936782011-06-03 19:40:40 +0000978vector<_Tp, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000979{
Eric Fiselierb5d9f442016-11-23 01:18:56 +0000980 return _VSTD::min<size_type>(__alloc_traits::max_size(this->__alloc()),
981 numeric_limits<difference_type>::max());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000982}
983
984// Precondition: __new_size > capacity()
985template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000986inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000987typename vector<_Tp, _Allocator>::size_type
988vector<_Tp, _Allocator>::__recommend(size_type __new_size) const
989{
990 const size_type __ms = max_size();
991 if (__new_size > __ms)
992 this->__throw_length_error();
993 const size_type __cap = capacity();
994 if (__cap >= __ms / 2)
995 return __ms;
Arthur O'Dwyerc0fb14b2021-07-26 18:23:00 -0400996 return _VSTD::max<size_type>(2 * __cap, __new_size);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000997}
998
999// Default constructs __n objects starting at __end_
1000// throws if construction throws
1001// Precondition: __n > 0
1002// Precondition: size() + __n <= capacity()
1003// Postcondition: size() == size() + __n
1004template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001005void
1006vector<_Tp, _Allocator>::__construct_at_end(size_type __n)
1007{
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001008 _ConstructTransaction __tx(*this, __n);
Martijn Vels1e2e1b82020-06-18 13:14:02 -04001009 const_pointer __new_end = __tx.__new_end_;
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04001010 for (pointer __pos = __tx.__pos_; __pos != __new_end; __tx.__pos_ = ++__pos) {
Martijn Vels1e2e1b82020-06-18 13:14:02 -04001011 __alloc_traits::construct(this->__alloc(), _VSTD::__to_address(__pos));
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001012 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001013}
1014
Howard Hinnantc51e1022010-05-11 19:42:16 +00001015// Copy constructs __n objects starting at __end_ from __x
1016// throws if construction throws
1017// Precondition: __n > 0
1018// Precondition: size() + __n <= capacity()
1019// Postcondition: size() == old size() + __n
1020// Postcondition: [i] == __x for all i in [size() - __n, __n)
1021template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001022inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001023void
1024vector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
1025{
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001026 _ConstructTransaction __tx(*this, __n);
Martijn Vels1e2e1b82020-06-18 13:14:02 -04001027 const_pointer __new_end = __tx.__new_end_;
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04001028 for (pointer __pos = __tx.__pos_; __pos != __new_end; __tx.__pos_ = ++__pos) {
Martijn Vels1e2e1b82020-06-18 13:14:02 -04001029 __alloc_traits::construct(this->__alloc(), _VSTD::__to_address(__pos), __x);
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001030 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001031}
1032
1033template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001034template <class _ForwardIterator>
1035typename enable_if
1036<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001037 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001038 void
1039>::type
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001040vector<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001041{
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001042 _ConstructTransaction __tx(*this, __n);
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05001043 _VSTD::__construct_range_forward(this->__alloc(), __first, __last, __tx.__pos_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001044}
1045
1046// Default constructs __n objects starting at __end_
1047// throws if construction throws
1048// Postcondition: size() == size() + __n
Howard Hinnant1c936782011-06-03 19:40:40 +00001049// Exception safety: strong.
Howard Hinnantc51e1022010-05-11 19:42:16 +00001050template <class _Tp, class _Allocator>
1051void
1052vector<_Tp, _Allocator>::__append(size_type __n)
1053{
1054 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1055 this->__construct_at_end(__n);
1056 else
1057 {
1058 allocator_type& __a = this->__alloc();
1059 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1060 __v.__construct_at_end(__n);
1061 __swap_out_circular_buffer(__v);
1062 }
1063}
1064
1065// Default constructs __n objects starting at __end_
1066// throws if construction throws
1067// Postcondition: size() == size() + __n
Howard Hinnant1c936782011-06-03 19:40:40 +00001068// Exception safety: strong.
Howard Hinnantc51e1022010-05-11 19:42:16 +00001069template <class _Tp, class _Allocator>
1070void
1071vector<_Tp, _Allocator>::__append(size_type __n, const_reference __x)
1072{
1073 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1074 this->__construct_at_end(__n, __x);
1075 else
1076 {
1077 allocator_type& __a = this->__alloc();
1078 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1079 __v.__construct_at_end(__n, __x);
1080 __swap_out_circular_buffer(__v);
1081 }
1082}
1083
1084template <class _Tp, class _Allocator>
1085vector<_Tp, _Allocator>::vector(size_type __n)
1086{
Louis Dionneba400782020-10-02 15:02:52 -04001087#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001088 __get_db()->__insert_c(this);
1089#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001090 if (__n > 0)
1091 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001092 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001093 __construct_at_end(__n);
1094 }
1095}
1096
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00001097#if _LIBCPP_STD_VER > 11
1098template <class _Tp, class _Allocator>
1099vector<_Tp, _Allocator>::vector(size_type __n, const allocator_type& __a)
1100 : __base(__a)
1101{
Louis Dionneba400782020-10-02 15:02:52 -04001102#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00001103 __get_db()->__insert_c(this);
1104#endif
1105 if (__n > 0)
1106 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001107 __vallocate(__n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00001108 __construct_at_end(__n);
1109 }
1110}
1111#endif
1112
Howard Hinnantc51e1022010-05-11 19:42:16 +00001113template <class _Tp, class _Allocator>
Marshall Clowf0ca1492018-05-21 21:30:12 +00001114vector<_Tp, _Allocator>::vector(size_type __n, const value_type& __x)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001115{
Louis Dionneba400782020-10-02 15:02:52 -04001116#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001117 __get_db()->__insert_c(this);
1118#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001119 if (__n > 0)
1120 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001121 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001122 __construct_at_end(__n, __x);
1123 }
1124}
1125
1126template <class _Tp, class _Allocator>
Marshall Clowf0ca1492018-05-21 21:30:12 +00001127vector<_Tp, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001128 : __base(__a)
1129{
Louis Dionneba400782020-10-02 15:02:52 -04001130#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001131 __get_db()->__insert_c(this);
1132#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001133 if (__n > 0)
1134 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001135 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001136 __construct_at_end(__n, __x);
1137 }
1138}
1139
1140template <class _Tp, class _Allocator>
1141template <class _InputIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +00001142vector<_Tp, _Allocator>::vector(_InputIterator __first,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001143 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
1144 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001145 is_constructible<
1146 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +00001147 typename iterator_traits<_InputIterator>::reference>::value,
1148 _InputIterator>::type __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001149{
Louis Dionneba400782020-10-02 15:02:52 -04001150#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001151 __get_db()->__insert_c(this);
1152#endif
Howard Hinnant9cd22302011-09-16 18:41:29 +00001153 for (; __first != __last; ++__first)
Eric Fiselier96919722017-10-17 13:03:17 +00001154 __emplace_back(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001155}
1156
1157template <class _Tp, class _Allocator>
1158template <class _InputIterator>
1159vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001160 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
1161 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001162 is_constructible<
1163 value_type,
1164 typename iterator_traits<_InputIterator>::reference>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001165 : __base(__a)
1166{
Louis Dionneba400782020-10-02 15:02:52 -04001167#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001168 __get_db()->__insert_c(this);
1169#endif
Howard Hinnant9cd22302011-09-16 18:41:29 +00001170 for (; __first != __last; ++__first)
Eric Fiselier96919722017-10-17 13:03:17 +00001171 __emplace_back(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001172}
1173
1174template <class _Tp, class _Allocator>
1175template <class _ForwardIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +00001176vector<_Tp, _Allocator>::vector(_ForwardIterator __first,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001177 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001178 is_constructible<
1179 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +00001180 typename iterator_traits<_ForwardIterator>::reference>::value,
1181 _ForwardIterator>::type __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001182{
Louis Dionneba400782020-10-02 15:02:52 -04001183#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001184 __get_db()->__insert_c(this);
1185#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001186 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001187 if (__n > 0)
1188 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001189 __vallocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001190 __construct_at_end(__first, __last, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001191 }
1192}
1193
1194template <class _Tp, class _Allocator>
1195template <class _ForwardIterator>
1196vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001197 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001198 is_constructible<
1199 value_type,
1200 typename iterator_traits<_ForwardIterator>::reference>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001201 : __base(__a)
1202{
Louis Dionneba400782020-10-02 15:02:52 -04001203#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001204 __get_db()->__insert_c(this);
1205#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001206 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001207 if (__n > 0)
1208 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001209 __vallocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001210 __construct_at_end(__first, __last, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001211 }
1212}
1213
1214template <class _Tp, class _Allocator>
1215vector<_Tp, _Allocator>::vector(const vector& __x)
1216 : __base(__alloc_traits::select_on_container_copy_construction(__x.__alloc()))
1217{
Louis Dionneba400782020-10-02 15:02:52 -04001218#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001219 __get_db()->__insert_c(this);
1220#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001221 size_type __n = __x.size();
1222 if (__n > 0)
1223 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001224 __vallocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001225 __construct_at_end(__x.__begin_, __x.__end_, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001226 }
1227}
1228
1229template <class _Tp, class _Allocator>
Arthur O'Dwyer9b9662f2021-03-01 17:08:24 -05001230vector<_Tp, _Allocator>::vector(const vector& __x, const __identity_t<allocator_type>& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001231 : __base(__a)
1232{
Louis Dionneba400782020-10-02 15:02:52 -04001233#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001234 __get_db()->__insert_c(this);
1235#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001236 size_type __n = __x.size();
1237 if (__n > 0)
1238 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001239 __vallocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001240 __construct_at_end(__x.__begin_, __x.__end_, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001241 }
1242}
1243
Eric Fiseliered9e9362017-04-16 02:40:45 +00001244#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001245
1246template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001247inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001248vector<_Tp, _Allocator>::vector(vector&& __x)
Marshall Clowe5108202015-07-14 14:46:32 +00001249#if _LIBCPP_STD_VER > 14
1250 _NOEXCEPT
1251#else
Howard Hinnant1c936782011-06-03 19:40:40 +00001252 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowe5108202015-07-14 14:46:32 +00001253#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001254 : __base(_VSTD::move(__x.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001255{
Louis Dionneba400782020-10-02 15:02:52 -04001256#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001257 __get_db()->__insert_c(this);
Mark de Wever8f72c892021-10-10 15:40:50 +02001258 __get_db()->swap(this, _VSTD::addressof(__x));
Howard Hinnant27e0e772011-09-14 18:33:51 +00001259#endif
Howard Hinnant9cd22302011-09-16 18:41:29 +00001260 this->__begin_ = __x.__begin_;
1261 this->__end_ = __x.__end_;
1262 this->__end_cap() = __x.__end_cap();
Howard Hinnant76053d72013-06-27 19:35:32 +00001263 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001264}
1265
1266template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001267inline _LIBCPP_INLINE_VISIBILITY
Arthur O'Dwyer9b9662f2021-03-01 17:08:24 -05001268vector<_Tp, _Allocator>::vector(vector&& __x, const __identity_t<allocator_type>& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001269 : __base(__a)
1270{
Louis Dionneba400782020-10-02 15:02:52 -04001271#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001272 __get_db()->__insert_c(this);
1273#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001274 if (__a == __x.__alloc())
1275 {
1276 this->__begin_ = __x.__begin_;
1277 this->__end_ = __x.__end_;
1278 this->__end_cap() = __x.__end_cap();
1279 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Louis Dionneba400782020-10-02 15:02:52 -04001280#if _LIBCPP_DEBUG_LEVEL == 2
Mark de Wever8f72c892021-10-10 15:40:50 +02001281 __get_db()->swap(this, _VSTD::addressof(__x));
Howard Hinnanta1e60cd2011-09-19 16:34:29 +00001282#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001283 }
1284 else
1285 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001286 typedef move_iterator<iterator> _Ip;
1287 assign(_Ip(__x.begin()), _Ip(__x.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001288 }
1289}
1290
1291template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001292inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001293vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il)
1294{
Louis Dionneba400782020-10-02 15:02:52 -04001295#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001296 __get_db()->__insert_c(this);
1297#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001298 if (__il.size() > 0)
1299 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001300 __vallocate(__il.size());
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001301 __construct_at_end(__il.begin(), __il.end(), __il.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001302 }
1303}
1304
1305template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001306inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001307vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
1308 : __base(__a)
1309{
Louis Dionneba400782020-10-02 15:02:52 -04001310#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001311 __get_db()->__insert_c(this);
1312#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001313 if (__il.size() > 0)
1314 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001315 __vallocate(__il.size());
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001316 __construct_at_end(__il.begin(), __il.end(), __il.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001317 }
1318}
1319
1320template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001321inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001322vector<_Tp, _Allocator>&
1323vector<_Tp, _Allocator>::operator=(vector&& __x)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001324 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001325{
1326 __move_assign(__x, integral_constant<bool,
1327 __alloc_traits::propagate_on_container_move_assignment::value>());
1328 return *this;
1329}
1330
1331template <class _Tp, class _Allocator>
1332void
1333vector<_Tp, _Allocator>::__move_assign(vector& __c, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001334 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001335{
Konstantin Varlamov5f23d662021-11-08 00:10:13 -08001336 if (__alloc() != __c.__alloc())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001337 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001338 typedef move_iterator<iterator> _Ip;
1339 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001340 }
1341 else
1342 __move_assign(__c, true_type());
1343}
1344
1345template <class _Tp, class _Allocator>
1346void
1347vector<_Tp, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00001348 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001349{
Marshall Clow3ff48e02018-05-22 16:20:28 +00001350 __vdeallocate();
Konstantin Varlamov5f23d662021-11-08 00:10:13 -08001351 __move_assign_alloc(__c); // this can throw
Howard Hinnantc51e1022010-05-11 19:42:16 +00001352 this->__begin_ = __c.__begin_;
1353 this->__end_ = __c.__end_;
1354 this->__end_cap() = __c.__end_cap();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001355 __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
Louis Dionneba400782020-10-02 15:02:52 -04001356#if _LIBCPP_DEBUG_LEVEL == 2
Mark de Wever8f72c892021-10-10 15:40:50 +02001357 __get_db()->swap(this, _VSTD::addressof(__c));
Howard Hinnanta1e60cd2011-09-19 16:34:29 +00001358#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001359}
1360
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001361#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001362
1363template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001364inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001365vector<_Tp, _Allocator>&
1366vector<_Tp, _Allocator>::operator=(const vector& __x)
1367{
Mark de Wever357a1fc2021-09-28 19:15:18 +02001368 if (this != _VSTD::addressof(__x))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001369 {
Konstantin Varlamov5f23d662021-11-08 00:10:13 -08001370 __copy_assign_alloc(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001371 assign(__x.__begin_, __x.__end_);
1372 }
1373 return *this;
1374}
1375
1376template <class _Tp, class _Allocator>
1377template <class _InputIterator>
1378typename enable_if
1379<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001380 __is_cpp17_input_iterator <_InputIterator>::value &&
1381 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001382 is_constructible<
1383 _Tp,
1384 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001385 void
1386>::type
1387vector<_Tp, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
1388{
1389 clear();
1390 for (; __first != __last; ++__first)
Eric Fiselier96919722017-10-17 13:03:17 +00001391 __emplace_back(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001392}
1393
1394template <class _Tp, class _Allocator>
1395template <class _ForwardIterator>
1396typename enable_if
1397<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001398 __is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001399 is_constructible<
1400 _Tp,
1401 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001402 void
1403>::type
1404vector<_Tp, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
1405{
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001406 size_type __new_size = static_cast<size_type>(_VSTD::distance(__first, __last));
1407 if (__new_size <= capacity())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001408 {
1409 _ForwardIterator __mid = __last;
1410 bool __growing = false;
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001411 if (__new_size > size())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001412 {
1413 __growing = true;
1414 __mid = __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001415 _VSTD::advance(__mid, size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001416 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001417 pointer __m = _VSTD::copy(__first, __mid, this->__begin_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001418 if (__growing)
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001419 __construct_at_end(__mid, __last, __new_size - size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001420 else
1421 this->__destruct_at_end(__m);
1422 }
1423 else
1424 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001425 __vdeallocate();
1426 __vallocate(__recommend(__new_size));
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001427 __construct_at_end(__first, __last, __new_size);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001428 }
Eric Fiselier69c51982016-12-28 06:06:09 +00001429 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001430}
1431
1432template <class _Tp, class _Allocator>
1433void
1434vector<_Tp, _Allocator>::assign(size_type __n, const_reference __u)
1435{
1436 if (__n <= capacity())
1437 {
1438 size_type __s = size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001439 _VSTD::fill_n(this->__begin_, _VSTD::min(__n, __s), __u);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001440 if (__n > __s)
1441 __construct_at_end(__n - __s, __u);
1442 else
Howard Hinnant155c2af2010-05-24 17:49:41 +00001443 this->__destruct_at_end(this->__begin_ + __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001444 }
1445 else
1446 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001447 __vdeallocate();
1448 __vallocate(__recommend(static_cast<size_type>(__n)));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001449 __construct_at_end(__n, __u);
1450 }
Eric Fiselier69c51982016-12-28 06:06:09 +00001451 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001452}
1453
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001454template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001455inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001456typename vector<_Tp, _Allocator>::iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001457vector<_Tp, _Allocator>::__make_iter(pointer __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001458{
Louis Dionneba400782020-10-02 15:02:52 -04001459#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001460 return iterator(this, __p);
1461#else
1462 return iterator(__p);
1463#endif
1464}
1465
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001466template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001467inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001468typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001469vector<_Tp, _Allocator>::__make_iter(const_pointer __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001470{
Louis Dionneba400782020-10-02 15:02:52 -04001471#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001472 return const_iterator(this, __p);
1473#else
1474 return const_iterator(__p);
1475#endif
1476}
1477
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001478template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001479inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001480typename vector<_Tp, _Allocator>::iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001481vector<_Tp, _Allocator>::begin() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001482{
1483 return __make_iter(this->__begin_);
1484}
1485
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001486template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001487inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001488typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001489vector<_Tp, _Allocator>::begin() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001490{
1491 return __make_iter(this->__begin_);
1492}
1493
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001494template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001495inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001496typename vector<_Tp, _Allocator>::iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001497vector<_Tp, _Allocator>::end() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001498{
1499 return __make_iter(this->__end_);
1500}
1501
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001502template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001503inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001504typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001505vector<_Tp, _Allocator>::end() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001506{
1507 return __make_iter(this->__end_);
1508}
1509
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001510template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001511inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001512typename vector<_Tp, _Allocator>::reference
Marshall Clowd6470492019-03-15 00:29:35 +00001513vector<_Tp, _Allocator>::operator[](size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001514{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001515 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001516 return this->__begin_[__n];
1517}
1518
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001519template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001520inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001521typename vector<_Tp, _Allocator>::const_reference
Marshall Clowd6470492019-03-15 00:29:35 +00001522vector<_Tp, _Allocator>::operator[](size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001523{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001524 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001525 return this->__begin_[__n];
1526}
1527
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001528template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001529typename vector<_Tp, _Allocator>::reference
1530vector<_Tp, _Allocator>::at(size_type __n)
1531{
1532 if (__n >= size())
1533 this->__throw_out_of_range();
1534 return this->__begin_[__n];
1535}
1536
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001537template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001538typename vector<_Tp, _Allocator>::const_reference
1539vector<_Tp, _Allocator>::at(size_type __n) const
1540{
1541 if (__n >= size())
1542 this->__throw_out_of_range();
1543 return this->__begin_[__n];
1544}
1545
1546template <class _Tp, class _Allocator>
1547void
1548vector<_Tp, _Allocator>::reserve(size_type __n)
1549{
1550 if (__n > capacity())
1551 {
Mikhail Maltsev0b0a51d2021-10-21 10:40:05 +01001552 if (__n > max_size())
1553 this->__throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001554 allocator_type& __a = this->__alloc();
Howard Hinnant1c936782011-06-03 19:40:40 +00001555 __split_buffer<value_type, allocator_type&> __v(__n, size(), __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001556 __swap_out_circular_buffer(__v);
1557 }
1558}
1559
1560template <class _Tp, class _Allocator>
1561void
Howard Hinnant1c936782011-06-03 19:40:40 +00001562vector<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001563{
1564 if (capacity() > size())
1565 {
1566#ifndef _LIBCPP_NO_EXCEPTIONS
1567 try
1568 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001569#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001570 allocator_type& __a = this->__alloc();
Howard Hinnant1c936782011-06-03 19:40:40 +00001571 __split_buffer<value_type, allocator_type&> __v(size(), size(), __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001572 __swap_out_circular_buffer(__v);
1573#ifndef _LIBCPP_NO_EXCEPTIONS
1574 }
1575 catch (...)
1576 {
1577 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001578#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001579 }
1580}
1581
1582template <class _Tp, class _Allocator>
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001583template <class _Up>
1584void
Eric Fiseliered9e9362017-04-16 02:40:45 +00001585#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001586vector<_Tp, _Allocator>::__push_back_slow_path(_Up&& __x)
1587#else
1588vector<_Tp, _Allocator>::__push_back_slow_path(_Up& __x)
1589#endif
1590{
1591 allocator_type& __a = this->__alloc();
1592 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1593 // __v.push_back(_VSTD::forward<_Up>(__x));
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001594 __alloc_traits::construct(__a, _VSTD::__to_address(__v.__end_), _VSTD::forward<_Up>(__x));
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001595 __v.__end_++;
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001596 __swap_out_circular_buffer(__v);
1597}
1598
1599template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001600inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001601void
1602vector<_Tp, _Allocator>::push_back(const_reference __x)
1603{
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001604 if (this->__end_ != this->__end_cap())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001605 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001606 __construct_one_at_end(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001607 }
1608 else
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001609 __push_back_slow_path(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001610}
1611
Eric Fiseliered9e9362017-04-16 02:40:45 +00001612#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001613
1614template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001615inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001616void
1617vector<_Tp, _Allocator>::push_back(value_type&& __x)
1618{
1619 if (this->__end_ < this->__end_cap())
1620 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001621 __construct_one_at_end(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001622 }
1623 else
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001624 __push_back_slow_path(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001625}
1626
1627template <class _Tp, class _Allocator>
1628template <class... _Args>
1629void
Howard Hinnantb6c49562012-02-26 15:30:12 +00001630vector<_Tp, _Allocator>::__emplace_back_slow_path(_Args&&... __args)
1631{
1632 allocator_type& __a = this->__alloc();
1633 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1634// __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001635 __alloc_traits::construct(__a, _VSTD::__to_address(__v.__end_), _VSTD::forward<_Args>(__args)...);
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001636 __v.__end_++;
Howard Hinnantb6c49562012-02-26 15:30:12 +00001637 __swap_out_circular_buffer(__v);
1638}
1639
1640template <class _Tp, class _Allocator>
1641template <class... _Args>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001642inline
Marshall Clowea52cc42017-01-24 23:09:12 +00001643#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001644typename vector<_Tp, _Allocator>::reference
Marshall Clowea52cc42017-01-24 23:09:12 +00001645#else
1646void
1647#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001648vector<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1649{
1650 if (this->__end_ < this->__end_cap())
1651 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001652 __construct_one_at_end(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001653 }
1654 else
Howard Hinnantb6c49562012-02-26 15:30:12 +00001655 __emplace_back_slow_path(_VSTD::forward<_Args>(__args)...);
Marshall Clowea52cc42017-01-24 23:09:12 +00001656#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001657 return this->back();
Marshall Clowea52cc42017-01-24 23:09:12 +00001658#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001659}
1660
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001661#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001662
1663template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001664inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001665void
1666vector<_Tp, _Allocator>::pop_back()
1667{
Kristina Bessonovaaeeaa7e2021-05-09 19:29:56 +02001668 _LIBCPP_ASSERT(!empty(), "vector::pop_back called on an empty vector");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001669 this->__destruct_at_end(this->__end_ - 1);
1670}
1671
1672template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001673inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001674typename vector<_Tp, _Allocator>::iterator
1675vector<_Tp, _Allocator>::erase(const_iterator __position)
1676{
Louis Dionneba400782020-10-02 15:02:52 -04001677#if _LIBCPP_DEBUG_LEVEL == 2
Mark de Wever8f72c892021-10-10 15:40:50 +02001678 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__position)) == this,
Howard Hinnant27e0e772011-09-14 18:33:51 +00001679 "vector::erase(iterator) called with an iterator not"
1680 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001681#endif
Howard Hinnant58d52d52013-03-25 22:12:26 +00001682 _LIBCPP_ASSERT(__position != end(),
1683 "vector::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnant76053d72013-06-27 19:35:32 +00001684 difference_type __ps = __position - cbegin();
1685 pointer __p = this->__begin_ + __ps;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001686 this->__destruct_at_end(_VSTD::move(__p + 1, this->__end_, __p));
Eric Fiselier69c51982016-12-28 06:06:09 +00001687 this->__invalidate_iterators_past(__p-1);
1688 iterator __r = __make_iter(__p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001689 return __r;
1690}
1691
1692template <class _Tp, class _Allocator>
1693typename vector<_Tp, _Allocator>::iterator
1694vector<_Tp, _Allocator>::erase(const_iterator __first, const_iterator __last)
1695{
Louis Dionneba400782020-10-02 15:02:52 -04001696#if _LIBCPP_DEBUG_LEVEL == 2
Mark de Wever8f72c892021-10-10 15:40:50 +02001697 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__first)) == this,
Mark de Weverdcafc462021-10-21 18:29:14 +02001698 "vector::erase(iterator, iterator) called with an iterator not"
Howard Hinnant27e0e772011-09-14 18:33:51 +00001699 " referring to this vector");
Mark de Wever8f72c892021-10-10 15:40:50 +02001700 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__last)) == this,
Mark de Weverdcafc462021-10-21 18:29:14 +02001701 "vector::erase(iterator, iterator) called with an iterator not"
Eric Fiselier69c51982016-12-28 06:06:09 +00001702 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001703#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001704 _LIBCPP_ASSERT(__first <= __last, "vector::erase(first, last) called with invalid range");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001705 pointer __p = this->__begin_ + (__first - begin());
Eric Fiselier69c51982016-12-28 06:06:09 +00001706 if (__first != __last) {
Howard Hinnantc580cc32013-04-18 15:02:57 +00001707 this->__destruct_at_end(_VSTD::move(__p + (__last - __first), this->__end_, __p));
Eric Fiselier69c51982016-12-28 06:06:09 +00001708 this->__invalidate_iterators_past(__p - 1);
1709 }
1710 iterator __r = __make_iter(__p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001711 return __r;
1712}
1713
1714template <class _Tp, class _Allocator>
1715void
1716vector<_Tp, _Allocator>::__move_range(pointer __from_s, pointer __from_e, pointer __to)
1717{
1718 pointer __old_last = this->__end_;
1719 difference_type __n = __old_last - __to;
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001720 {
1721 pointer __i = __from_s + __n;
1722 _ConstructTransaction __tx(*this, __from_e - __i);
Martijn Vels1e2e1b82020-06-18 13:14:02 -04001723 for (pointer __pos = __tx.__pos_; __i < __from_e;
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04001724 ++__i, (void) ++__pos, __tx.__pos_ = __pos) {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001725 __alloc_traits::construct(this->__alloc(),
Martijn Vels1e2e1b82020-06-18 13:14:02 -04001726 _VSTD::__to_address(__pos),
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001727 _VSTD::move(*__i));
1728 }
1729 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001730 _VSTD::move_backward(__from_s, __from_s + __n, __old_last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001731}
1732
1733template <class _Tp, class _Allocator>
1734typename vector<_Tp, _Allocator>::iterator
1735vector<_Tp, _Allocator>::insert(const_iterator __position, const_reference __x)
1736{
Louis Dionneba400782020-10-02 15:02:52 -04001737#if _LIBCPP_DEBUG_LEVEL == 2
Mark de Wever8f72c892021-10-10 15:40:50 +02001738 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__position)) == this,
Howard Hinnant27e0e772011-09-14 18:33:51 +00001739 "vector::insert(iterator, x) called with an iterator not"
1740 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001741#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001742 pointer __p = this->__begin_ + (__position - begin());
1743 if (this->__end_ < this->__end_cap())
1744 {
1745 if (__p == this->__end_)
1746 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001747 __construct_one_at_end(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001748 }
1749 else
1750 {
1751 __move_range(__p, this->__end_, __p + 1);
1752 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1753 if (__p <= __xr && __xr < this->__end_)
1754 ++__xr;
1755 *__p = *__xr;
1756 }
1757 }
1758 else
1759 {
1760 allocator_type& __a = this->__alloc();
1761 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
1762 __v.push_back(__x);
1763 __p = __swap_out_circular_buffer(__v, __p);
1764 }
1765 return __make_iter(__p);
1766}
1767
Eric Fiseliered9e9362017-04-16 02:40:45 +00001768#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001769
1770template <class _Tp, class _Allocator>
1771typename vector<_Tp, _Allocator>::iterator
1772vector<_Tp, _Allocator>::insert(const_iterator __position, value_type&& __x)
1773{
Louis Dionneba400782020-10-02 15:02:52 -04001774#if _LIBCPP_DEBUG_LEVEL == 2
Mark de Wever8f72c892021-10-10 15:40:50 +02001775 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__position)) == this,
Howard Hinnant27e0e772011-09-14 18:33:51 +00001776 "vector::insert(iterator, x) called with an iterator not"
1777 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001778#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001779 pointer __p = this->__begin_ + (__position - begin());
1780 if (this->__end_ < this->__end_cap())
1781 {
1782 if (__p == this->__end_)
1783 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001784 __construct_one_at_end(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001785 }
1786 else
1787 {
1788 __move_range(__p, this->__end_, __p + 1);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001789 *__p = _VSTD::move(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001790 }
1791 }
1792 else
1793 {
1794 allocator_type& __a = this->__alloc();
1795 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001796 __v.push_back(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001797 __p = __swap_out_circular_buffer(__v, __p);
1798 }
1799 return __make_iter(__p);
1800}
1801
1802template <class _Tp, class _Allocator>
1803template <class... _Args>
1804typename vector<_Tp, _Allocator>::iterator
1805vector<_Tp, _Allocator>::emplace(const_iterator __position, _Args&&... __args)
1806{
Louis Dionneba400782020-10-02 15:02:52 -04001807#if _LIBCPP_DEBUG_LEVEL == 2
Mark de Wever8f72c892021-10-10 15:40:50 +02001808 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__position)) == this,
Howard Hinnant27e0e772011-09-14 18:33:51 +00001809 "vector::emplace(iterator, x) called with an iterator not"
1810 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001811#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001812 pointer __p = this->__begin_ + (__position - begin());
1813 if (this->__end_ < this->__end_cap())
1814 {
1815 if (__p == this->__end_)
1816 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001817 __construct_one_at_end(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001818 }
1819 else
1820 {
Marshall Clowa591b9a2016-07-11 21:38:08 +00001821 __temp_value<value_type, _Allocator> __tmp(this->__alloc(), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001822 __move_range(__p, this->__end_, __p + 1);
Marshall Clowa591b9a2016-07-11 21:38:08 +00001823 *__p = _VSTD::move(__tmp.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001824 }
1825 }
1826 else
1827 {
1828 allocator_type& __a = this->__alloc();
1829 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001830 __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001831 __p = __swap_out_circular_buffer(__v, __p);
1832 }
1833 return __make_iter(__p);
1834}
1835
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001836#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001837
1838template <class _Tp, class _Allocator>
1839typename vector<_Tp, _Allocator>::iterator
1840vector<_Tp, _Allocator>::insert(const_iterator __position, size_type __n, const_reference __x)
1841{
Louis Dionneba400782020-10-02 15:02:52 -04001842#if _LIBCPP_DEBUG_LEVEL == 2
Mark de Wever8f72c892021-10-10 15:40:50 +02001843 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__position)) == this,
Howard Hinnant27e0e772011-09-14 18:33:51 +00001844 "vector::insert(iterator, n, x) called with an iterator not"
1845 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001846#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001847 pointer __p = this->__begin_ + (__position - begin());
1848 if (__n > 0)
1849 {
1850 if (__n <= static_cast<size_type>(this->__end_cap() - this->__end_))
1851 {
1852 size_type __old_n = __n;
1853 pointer __old_last = this->__end_;
1854 if (__n > static_cast<size_type>(this->__end_ - __p))
1855 {
1856 size_type __cx = __n - (this->__end_ - __p);
1857 __construct_at_end(__cx, __x);
1858 __n -= __cx;
1859 }
1860 if (__n > 0)
1861 {
1862 __move_range(__p, __old_last, __p + __old_n);
1863 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1864 if (__p <= __xr && __xr < this->__end_)
1865 __xr += __old_n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001866 _VSTD::fill_n(__p, __n, *__xr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001867 }
1868 }
1869 else
1870 {
1871 allocator_type& __a = this->__alloc();
1872 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1873 __v.__construct_at_end(__n, __x);
1874 __p = __swap_out_circular_buffer(__v, __p);
1875 }
1876 }
1877 return __make_iter(__p);
1878}
1879
1880template <class _Tp, class _Allocator>
1881template <class _InputIterator>
1882typename enable_if
1883<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001884 __is_cpp17_input_iterator <_InputIterator>::value &&
1885 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001886 is_constructible<
1887 _Tp,
1888 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001889 typename vector<_Tp, _Allocator>::iterator
1890>::type
1891vector<_Tp, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
1892{
Louis Dionneba400782020-10-02 15:02:52 -04001893#if _LIBCPP_DEBUG_LEVEL == 2
Mark de Wever8f72c892021-10-10 15:40:50 +02001894 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__position)) == this,
Howard Hinnant27e0e772011-09-14 18:33:51 +00001895 "vector::insert(iterator, range) called with an iterator not"
1896 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001897#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001898 difference_type __off = __position - begin();
1899 pointer __p = this->__begin_ + __off;
1900 allocator_type& __a = this->__alloc();
1901 pointer __old_last = this->__end_;
1902 for (; this->__end_ != this->__end_cap() && __first != __last; ++__first)
1903 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001904 __construct_one_at_end(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001905 }
1906 __split_buffer<value_type, allocator_type&> __v(__a);
1907 if (__first != __last)
1908 {
1909#ifndef _LIBCPP_NO_EXCEPTIONS
1910 try
1911 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001912#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001913 __v.__construct_at_end(__first, __last);
1914 difference_type __old_size = __old_last - this->__begin_;
1915 difference_type __old_p = __p - this->__begin_;
1916 reserve(__recommend(size() + __v.size()));
1917 __p = this->__begin_ + __old_p;
1918 __old_last = this->__begin_ + __old_size;
1919#ifndef _LIBCPP_NO_EXCEPTIONS
1920 }
1921 catch (...)
1922 {
1923 erase(__make_iter(__old_last), end());
1924 throw;
1925 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001926#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001927 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001928 __p = _VSTD::rotate(__p, __old_last, this->__end_);
Louis Dionne202a4cb2020-02-11 10:59:12 +01001929 insert(__make_iter(__p), _VSTD::make_move_iterator(__v.begin()),
1930 _VSTD::make_move_iterator(__v.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001931 return begin() + __off;
1932}
1933
1934template <class _Tp, class _Allocator>
1935template <class _ForwardIterator>
1936typename enable_if
1937<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001938 __is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001939 is_constructible<
1940 _Tp,
1941 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001942 typename vector<_Tp, _Allocator>::iterator
1943>::type
1944vector<_Tp, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
1945{
Louis Dionneba400782020-10-02 15:02:52 -04001946#if _LIBCPP_DEBUG_LEVEL == 2
Mark de Wever8f72c892021-10-10 15:40:50 +02001947 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__position)) == this,
Howard Hinnant27e0e772011-09-14 18:33:51 +00001948 "vector::insert(iterator, range) called with an iterator not"
1949 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001950#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001951 pointer __p = this->__begin_ + (__position - begin());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001952 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001953 if (__n > 0)
1954 {
1955 if (__n <= this->__end_cap() - this->__end_)
1956 {
1957 size_type __old_n = __n;
1958 pointer __old_last = this->__end_;
1959 _ForwardIterator __m = __last;
1960 difference_type __dx = this->__end_ - __p;
1961 if (__n > __dx)
1962 {
1963 __m = __first;
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001964 difference_type __diff = this->__end_ - __p;
1965 _VSTD::advance(__m, __diff);
1966 __construct_at_end(__m, __last, __n - __diff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001967 __n = __dx;
1968 }
1969 if (__n > 0)
1970 {
1971 __move_range(__p, __old_last, __p + __old_n);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001972 _VSTD::copy(__first, __m, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001973 }
1974 }
1975 else
1976 {
1977 allocator_type& __a = this->__alloc();
1978 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1979 __v.__construct_at_end(__first, __last);
1980 __p = __swap_out_circular_buffer(__v, __p);
1981 }
1982 }
1983 return __make_iter(__p);
1984}
1985
1986template <class _Tp, class _Allocator>
1987void
1988vector<_Tp, _Allocator>::resize(size_type __sz)
1989{
1990 size_type __cs = size();
1991 if (__cs < __sz)
1992 this->__append(__sz - __cs);
1993 else if (__cs > __sz)
1994 this->__destruct_at_end(this->__begin_ + __sz);
1995}
1996
1997template <class _Tp, class _Allocator>
1998void
1999vector<_Tp, _Allocator>::resize(size_type __sz, const_reference __x)
2000{
2001 size_type __cs = size();
2002 if (__cs < __sz)
2003 this->__append(__sz - __cs, __x);
2004 else if (__cs > __sz)
2005 this->__destruct_at_end(this->__begin_ + __sz);
2006}
2007
2008template <class _Tp, class _Allocator>
2009void
2010vector<_Tp, _Allocator>::swap(vector& __x)
Marshall Clow8982dcd2015-07-13 20:04:56 +00002011#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00002012 _NOEXCEPT
Marshall Clow8982dcd2015-07-13 20:04:56 +00002013#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00002014 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00002015 __is_nothrow_swappable<allocator_type>::value)
2016#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002017{
Howard Hinnant27e0e772011-09-14 18:33:51 +00002018 _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
2019 this->__alloc() == __x.__alloc(),
2020 "vector::swap: Either propagate_on_container_swap must be true"
2021 " or the allocators must compare equal");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002022 _VSTD::swap(this->__begin_, __x.__begin_);
2023 _VSTD::swap(this->__end_, __x.__end_);
2024 _VSTD::swap(this->__end_cap(), __x.__end_cap());
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05002025 _VSTD::__swap_allocator(this->__alloc(), __x.__alloc(),
Marshall Clow8982dcd2015-07-13 20:04:56 +00002026 integral_constant<bool,__alloc_traits::propagate_on_container_swap::value>());
Louis Dionneba400782020-10-02 15:02:52 -04002027#if _LIBCPP_DEBUG_LEVEL == 2
Mark de Wever8f72c892021-10-10 15:40:50 +02002028 __get_db()->swap(this, _VSTD::addressof(__x));
Louis Dionneba400782020-10-02 15:02:52 -04002029#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002030}
2031
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002032template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002033bool
2034vector<_Tp, _Allocator>::__invariants() const
2035{
Howard Hinnant76053d72013-06-27 19:35:32 +00002036 if (this->__begin_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002037 {
Howard Hinnant76053d72013-06-27 19:35:32 +00002038 if (this->__end_ != nullptr || this->__end_cap() != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002039 return false;
2040 }
2041 else
2042 {
2043 if (this->__begin_ > this->__end_)
2044 return false;
2045 if (this->__begin_ == this->__end_cap())
2046 return false;
2047 if (this->__end_ > this->__end_cap())
2048 return false;
2049 }
2050 return true;
2051}
2052
Louis Dionneba400782020-10-02 15:02:52 -04002053#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002054
Howard Hinnantc51e1022010-05-11 19:42:16 +00002055template <class _Tp, class _Allocator>
Howard Hinnant27e0e772011-09-14 18:33:51 +00002056bool
2057vector<_Tp, _Allocator>::__dereferenceable(const const_iterator* __i) const
2058{
2059 return this->__begin_ <= __i->base() && __i->base() < this->__end_;
2060}
2061
2062template <class _Tp, class _Allocator>
2063bool
2064vector<_Tp, _Allocator>::__decrementable(const const_iterator* __i) const
2065{
2066 return this->__begin_ < __i->base() && __i->base() <= this->__end_;
2067}
2068
2069template <class _Tp, class _Allocator>
2070bool
2071vector<_Tp, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
2072{
2073 const_pointer __p = __i->base() + __n;
2074 return this->__begin_ <= __p && __p <= this->__end_;
2075}
2076
2077template <class _Tp, class _Allocator>
2078bool
2079vector<_Tp, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
2080{
2081 const_pointer __p = __i->base() + __n;
2082 return this->__begin_ <= __p && __p < this->__end_;
2083}
2084
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002085#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002086
2087template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002088inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002089void
2090vector<_Tp, _Allocator>::__invalidate_all_iterators()
2091{
Louis Dionneba400782020-10-02 15:02:52 -04002092#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002093 __get_db()->__invalidate_all(this);
Louis Dionneba400782020-10-02 15:02:52 -04002094#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002095}
2096
Eric Fiselier69c51982016-12-28 06:06:09 +00002097
2098template <class _Tp, class _Allocator>
2099inline _LIBCPP_INLINE_VISIBILITY
2100void
2101vector<_Tp, _Allocator>::__invalidate_iterators_past(pointer __new_last) {
Louis Dionneba400782020-10-02 15:02:52 -04002102#if _LIBCPP_DEBUG_LEVEL == 2
Eric Fiselier69c51982016-12-28 06:06:09 +00002103 __c_node* __c = __get_db()->__find_c_and_lock(this);
2104 for (__i_node** __p = __c->end_; __p != __c->beg_; ) {
2105 --__p;
2106 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
2107 if (__i->base() > __new_last) {
2108 (*__p)->__c_ = nullptr;
2109 if (--__c->end_ != __p)
Arthur O'Dwyer22236632020-12-07 21:50:15 -05002110 _VSTD::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
Eric Fiselier69c51982016-12-28 06:06:09 +00002111 }
2112 }
2113 __get_db()->unlock();
2114#else
2115 ((void)__new_last);
2116#endif
2117}
2118
Howard Hinnantc51e1022010-05-11 19:42:16 +00002119// vector<bool>
2120
2121template <class _Allocator> class vector<bool, _Allocator>;
2122
2123template <class _Allocator> struct hash<vector<bool, _Allocator> >;
2124
2125template <class _Allocator>
Howard Hinnantb4ef6482011-07-02 20:33:23 +00002126struct __has_storage_type<vector<bool, _Allocator> >
2127{
2128 static const bool value = true;
2129};
2130
2131template <class _Allocator>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002132class _LIBCPP_TEMPLATE_VIS vector<bool, _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002133 : private __vector_base_common<true>
2134{
2135public:
2136 typedef vector __self;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002137 typedef bool value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002138 typedef _Allocator allocator_type;
2139 typedef allocator_traits<allocator_type> __alloc_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002140 typedef typename __alloc_traits::size_type size_type;
2141 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant896b7622012-05-07 16:50:38 +00002142 typedef size_type __storage_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002143 typedef __bit_iterator<vector, false> pointer;
2144 typedef __bit_iterator<vector, true> const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002145 typedef pointer iterator;
2146 typedef const_pointer const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002147 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
2148 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002149
2150private:
Marshall Clow940e01c2015-04-07 05:21:38 +00002151 typedef typename __rebind_alloc_helper<__alloc_traits, __storage_type>::type __storage_allocator;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002152 typedef allocator_traits<__storage_allocator> __storage_traits;
2153 typedef typename __storage_traits::pointer __storage_pointer;
2154 typedef typename __storage_traits::const_pointer __const_storage_pointer;
2155
2156 __storage_pointer __begin_;
2157 size_type __size_;
2158 __compressed_pair<size_type, __storage_allocator> __cap_alloc_;
Howard Hinnant71d1c192011-07-09 15:50:42 +00002159public:
Howard Hinnantb4ef6482011-07-02 20:33:23 +00002160 typedef __bit_reference<vector> reference;
2161 typedef __bit_const_reference<vector> const_reference;
Howard Hinnant71d1c192011-07-09 15:50:42 +00002162private:
Howard Hinnant1c936782011-06-03 19:40:40 +00002163 _LIBCPP_INLINE_VISIBILITY
2164 size_type& __cap() _NOEXCEPT
2165 {return __cap_alloc_.first();}
2166 _LIBCPP_INLINE_VISIBILITY
2167 const size_type& __cap() const _NOEXCEPT
2168 {return __cap_alloc_.first();}
2169 _LIBCPP_INLINE_VISIBILITY
2170 __storage_allocator& __alloc() _NOEXCEPT
2171 {return __cap_alloc_.second();}
2172 _LIBCPP_INLINE_VISIBILITY
2173 const __storage_allocator& __alloc() const _NOEXCEPT
2174 {return __cap_alloc_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002175
2176 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
2177
Howard Hinnant1c936782011-06-03 19:40:40 +00002178 _LIBCPP_INLINE_VISIBILITY
2179 static size_type __internal_cap_to_external(size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002180 {return __n * __bits_per_word;}
Howard Hinnant1c936782011-06-03 19:40:40 +00002181 _LIBCPP_INLINE_VISIBILITY
2182 static size_type __external_cap_to_internal(size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002183 {return (__n - 1) / __bits_per_word + 1;}
2184
2185public:
Howard Hinnant1c936782011-06-03 19:40:40 +00002186 _LIBCPP_INLINE_VISIBILITY
Marshall Clowe546cbb2015-06-04 02:05:41 +00002187 vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clow8f282f42015-06-04 00:10:20 +00002188
2189 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
2190#if _LIBCPP_STD_VER <= 14
2191 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
2192#else
2193 _NOEXCEPT;
2194#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002195 ~vector();
2196 explicit vector(size_type __n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00002197#if _LIBCPP_STD_VER > 11
2198 explicit vector(size_type __n, const allocator_type& __a);
2199#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002200 vector(size_type __n, const value_type& __v);
2201 vector(size_type __n, const value_type& __v, const allocator_type& __a);
2202 template <class _InputIterator>
2203 vector(_InputIterator __first, _InputIterator __last,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002204 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
2205 !__is_cpp17_forward_iterator<_InputIterator>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002206 template <class _InputIterator>
2207 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002208 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
2209 !__is_cpp17_forward_iterator<_InputIterator>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002210 template <class _ForwardIterator>
2211 vector(_ForwardIterator __first, _ForwardIterator __last,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002212 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002213 template <class _ForwardIterator>
2214 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002215 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002216
2217 vector(const vector& __v);
2218 vector(const vector& __v, const allocator_type& __a);
2219 vector& operator=(const vector& __v);
Eric Fiseliered9e9362017-04-16 02:40:45 +00002220
2221#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002222 vector(initializer_list<value_type> __il);
2223 vector(initializer_list<value_type> __il, const allocator_type& __a);
2224
Howard Hinnant1c936782011-06-03 19:40:40 +00002225 _LIBCPP_INLINE_VISIBILITY
2226 vector(vector&& __v)
Marshall Clowe5108202015-07-14 14:46:32 +00002227#if _LIBCPP_STD_VER > 14
2228 _NOEXCEPT;
2229#else
Howard Hinnant1c936782011-06-03 19:40:40 +00002230 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowe5108202015-07-14 14:46:32 +00002231#endif
Arthur O'Dwyer9b9662f2021-03-01 17:08:24 -05002232 vector(vector&& __v, const __identity_t<allocator_type>& __a);
Howard Hinnant1c936782011-06-03 19:40:40 +00002233 _LIBCPP_INLINE_VISIBILITY
2234 vector& operator=(vector&& __v)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002235 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiseliered9e9362017-04-16 02:40:45 +00002236
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002237 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002238 vector& operator=(initializer_list<value_type> __il)
2239 {assign(__il.begin(), __il.end()); return *this;}
Eric Fiseliered9e9362017-04-16 02:40:45 +00002240
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002241#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002242
2243 template <class _InputIterator>
2244 typename enable_if
2245 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002246 __is_cpp17_input_iterator<_InputIterator>::value &&
2247 !__is_cpp17_forward_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002248 void
2249 >::type
2250 assign(_InputIterator __first, _InputIterator __last);
2251 template <class _ForwardIterator>
2252 typename enable_if
2253 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002254 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002255 void
2256 >::type
2257 assign(_ForwardIterator __first, _ForwardIterator __last);
2258
2259 void assign(size_type __n, const value_type& __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +00002260
2261#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002262 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002263 void assign(initializer_list<value_type> __il)
2264 {assign(__il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +00002265#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002266
Howard Hinnant1c936782011-06-03 19:40:40 +00002267 _LIBCPP_INLINE_VISIBILITY allocator_type get_allocator() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002268 {return allocator_type(this->__alloc());}
2269
Howard Hinnant1c936782011-06-03 19:40:40 +00002270 size_type max_size() const _NOEXCEPT;
2271 _LIBCPP_INLINE_VISIBILITY
2272 size_type capacity() const _NOEXCEPT
2273 {return __internal_cap_to_external(__cap());}
2274 _LIBCPP_INLINE_VISIBILITY
2275 size_type size() const _NOEXCEPT
2276 {return __size_;}
Marshall Clow425f5752017-11-15 05:51:26 +00002277 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +00002278 bool empty() const _NOEXCEPT
2279 {return __size_ == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002280 void reserve(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +00002281 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002282
Howard Hinnant1c936782011-06-03 19:40:40 +00002283 _LIBCPP_INLINE_VISIBILITY
2284 iterator begin() _NOEXCEPT
2285 {return __make_iter(0);}
2286 _LIBCPP_INLINE_VISIBILITY
2287 const_iterator begin() const _NOEXCEPT
2288 {return __make_iter(0);}
2289 _LIBCPP_INLINE_VISIBILITY
2290 iterator end() _NOEXCEPT
2291 {return __make_iter(__size_);}
2292 _LIBCPP_INLINE_VISIBILITY
2293 const_iterator end() const _NOEXCEPT
2294 {return __make_iter(__size_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002295
Howard Hinnant1c936782011-06-03 19:40:40 +00002296 _LIBCPP_INLINE_VISIBILITY
2297 reverse_iterator rbegin() _NOEXCEPT
2298 {return reverse_iterator(end());}
2299 _LIBCPP_INLINE_VISIBILITY
2300 const_reverse_iterator rbegin() const _NOEXCEPT
2301 {return const_reverse_iterator(end());}
2302 _LIBCPP_INLINE_VISIBILITY
2303 reverse_iterator rend() _NOEXCEPT
2304 {return reverse_iterator(begin());}
2305 _LIBCPP_INLINE_VISIBILITY
2306 const_reverse_iterator rend() const _NOEXCEPT
2307 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002308
Howard Hinnant1c936782011-06-03 19:40:40 +00002309 _LIBCPP_INLINE_VISIBILITY
2310 const_iterator cbegin() const _NOEXCEPT
2311 {return __make_iter(0);}
2312 _LIBCPP_INLINE_VISIBILITY
2313 const_iterator cend() const _NOEXCEPT
2314 {return __make_iter(__size_);}
2315 _LIBCPP_INLINE_VISIBILITY
2316 const_reverse_iterator crbegin() const _NOEXCEPT
2317 {return rbegin();}
2318 _LIBCPP_INLINE_VISIBILITY
2319 const_reverse_iterator crend() const _NOEXCEPT
2320 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002321
2322 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) {return __make_ref(__n);}
2323 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const {return __make_ref(__n);}
2324 reference at(size_type __n);
2325 const_reference at(size_type __n) const;
2326
2327 _LIBCPP_INLINE_VISIBILITY reference front() {return __make_ref(0);}
2328 _LIBCPP_INLINE_VISIBILITY const_reference front() const {return __make_ref(0);}
2329 _LIBCPP_INLINE_VISIBILITY reference back() {return __make_ref(__size_ - 1);}
2330 _LIBCPP_INLINE_VISIBILITY const_reference back() const {return __make_ref(__size_ - 1);}
2331
2332 void push_back(const value_type& __x);
Marshall Clowc46bb8e2013-08-13 23:54:12 +00002333#if _LIBCPP_STD_VER > 11
2334 template <class... _Args>
Marshall Clowea52cc42017-01-24 23:09:12 +00002335#if _LIBCPP_STD_VER > 14
2336 _LIBCPP_INLINE_VISIBILITY reference emplace_back(_Args&&... __args)
2337#else
2338 _LIBCPP_INLINE_VISIBILITY void emplace_back(_Args&&... __args)
2339#endif
2340 {
Eric Fiselier34ba5b92016-07-21 03:20:17 +00002341 push_back ( value_type ( _VSTD::forward<_Args>(__args)... ));
Marshall Clowea52cc42017-01-24 23:09:12 +00002342#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00002343 return this->back();
Marshall Clowea52cc42017-01-24 23:09:12 +00002344#endif
Eric Fiselier34ba5b92016-07-21 03:20:17 +00002345 }
Marshall Clowc46bb8e2013-08-13 23:54:12 +00002346#endif
2347
Howard Hinnantc51e1022010-05-11 19:42:16 +00002348 _LIBCPP_INLINE_VISIBILITY void pop_back() {--__size_;}
2349
Marshall Clowc46bb8e2013-08-13 23:54:12 +00002350#if _LIBCPP_STD_VER > 11
2351 template <class... _Args>
2352 _LIBCPP_INLINE_VISIBILITY iterator emplace(const_iterator position, _Args&&... __args)
2353 { return insert ( position, value_type ( _VSTD::forward<_Args>(__args)... )); }
2354#endif
2355
Howard Hinnantc51e1022010-05-11 19:42:16 +00002356 iterator insert(const_iterator __position, const value_type& __x);
2357 iterator insert(const_iterator __position, size_type __n, const value_type& __x);
2358 iterator insert(const_iterator __position, size_type __n, const_reference __x);
2359 template <class _InputIterator>
2360 typename enable_if
2361 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002362 __is_cpp17_input_iterator <_InputIterator>::value &&
2363 !__is_cpp17_forward_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002364 iterator
2365 >::type
2366 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
2367 template <class _ForwardIterator>
2368 typename enable_if
2369 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002370 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002371 iterator
2372 >::type
2373 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiseliered9e9362017-04-16 02:40:45 +00002374
2375#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002376 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002377 iterator insert(const_iterator __position, initializer_list<value_type> __il)
2378 {return insert(__position, __il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +00002379#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002380
Howard Hinnantcf823322010-12-17 14:46:43 +00002381 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002382 iterator erase(const_iterator __first, const_iterator __last);
2383
Howard Hinnant1c936782011-06-03 19:40:40 +00002384 _LIBCPP_INLINE_VISIBILITY
2385 void clear() _NOEXCEPT {__size_ = 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002386
Howard Hinnant1c936782011-06-03 19:40:40 +00002387 void swap(vector&)
Marshall Clow8982dcd2015-07-13 20:04:56 +00002388#if _LIBCPP_STD_VER >= 14
2389 _NOEXCEPT;
2390#else
Eric Fiselier69c51982016-12-28 06:06:09 +00002391 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00002392 __is_nothrow_swappable<allocator_type>::value);
2393#endif
Marshall Clow89eb3822016-04-07 14:20:31 +00002394 static void swap(reference __x, reference __y) _NOEXCEPT { _VSTD::swap(__x, __y); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002395
2396 void resize(size_type __sz, value_type __x = false);
Howard Hinnant1c936782011-06-03 19:40:40 +00002397 void flip() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002398
2399 bool __invariants() const;
2400
2401private:
Howard Hinnantcf823322010-12-17 14:46:43 +00002402 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Marshall Clow3ff48e02018-05-22 16:20:28 +00002403 void __vallocate(size_type __n);
2404 void __vdeallocate() _NOEXCEPT;
Howard Hinnant1c936782011-06-03 19:40:40 +00002405 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantea382952013-08-14 18:00:20 +00002406 static size_type __align_it(size_type __new_size) _NOEXCEPT
Eric Fiselierb41db9a2018-10-01 01:59:37 +00002407 {return __new_size + (__bits_per_word-1) & ~((size_type)__bits_per_word-1);}
Howard Hinnantcf823322010-12-17 14:46:43 +00002408 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
2409 _LIBCPP_INLINE_VISIBILITY void __construct_at_end(size_type __n, bool __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002410 template <class _ForwardIterator>
2411 typename enable_if
2412 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002413 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002414 void
2415 >::type
2416 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
2417 void __append(size_type __n, const_reference __x);
Howard Hinnant1c936782011-06-03 19:40:40 +00002418 _LIBCPP_INLINE_VISIBILITY
2419 reference __make_ref(size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002420 {return reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnant1c936782011-06-03 19:40:40 +00002421 _LIBCPP_INLINE_VISIBILITY
2422 const_reference __make_ref(size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002423 {return const_reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnant1c936782011-06-03 19:40:40 +00002424 _LIBCPP_INLINE_VISIBILITY
2425 iterator __make_iter(size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002426 {return iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnant1c936782011-06-03 19:40:40 +00002427 _LIBCPP_INLINE_VISIBILITY
2428 const_iterator __make_iter(size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002429 {return const_iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnant1c936782011-06-03 19:40:40 +00002430 _LIBCPP_INLINE_VISIBILITY
2431 iterator __const_iterator_cast(const_iterator __p) _NOEXCEPT
Howard Hinnant76053d72013-06-27 19:35:32 +00002432 {return begin() + (__p - cbegin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002433
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002434 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002435 void __copy_assign_alloc(const vector& __v)
2436 {__copy_assign_alloc(__v, integral_constant<bool,
2437 __storage_traits::propagate_on_container_copy_assignment::value>());}
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002438 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002439 void __copy_assign_alloc(const vector& __c, true_type)
2440 {
2441 if (__alloc() != __c.__alloc())
Marshall Clow3ff48e02018-05-22 16:20:28 +00002442 __vdeallocate();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002443 __alloc() = __c.__alloc();
2444 }
2445
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002446 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00002447 void __copy_assign_alloc(const vector&, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002448 {}
2449
2450 void __move_assign(vector& __c, false_type);
Howard Hinnant1c936782011-06-03 19:40:40 +00002451 void __move_assign(vector& __c, true_type)
2452 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002453 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002454 void __move_assign_alloc(vector& __c)
Howard Hinnant1c936782011-06-03 19:40:40 +00002455 _NOEXCEPT_(
2456 !__storage_traits::propagate_on_container_move_assignment::value ||
2457 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002458 {__move_assign_alloc(__c, integral_constant<bool,
2459 __storage_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002460 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +00002461 void __move_assign_alloc(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002462 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002463 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002464 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002465 }
2466
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002467 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00002468 void __move_assign_alloc(vector&, false_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002469 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002470 {}
2471
Howard Hinnant1c936782011-06-03 19:40:40 +00002472 size_t __hash_code() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002473
2474 friend class __bit_reference<vector>;
2475 friend class __bit_const_reference<vector>;
2476 friend class __bit_iterator<vector, false>;
2477 friend class __bit_iterator<vector, true>;
Howard Hinnant4210a0f2012-08-17 17:10:18 +00002478 friend struct __bit_array<vector>;
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002479 friend struct _LIBCPP_TEMPLATE_VIS hash<vector>;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002480};
2481
2482template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002483inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002484void
2485vector<bool, _Allocator>::__invalidate_all_iterators()
2486{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002487}
2488
2489// Allocate space for __n objects
2490// throws length_error if __n > max_size()
2491// throws (probably bad_alloc) if memory run out
2492// Precondition: __begin_ == __end_ == __cap() == 0
2493// Precondition: __n > 0
2494// Postcondition: capacity() == __n
2495// Postcondition: size() == 0
2496template <class _Allocator>
2497void
Marshall Clow3ff48e02018-05-22 16:20:28 +00002498vector<bool, _Allocator>::__vallocate(size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002499{
2500 if (__n > max_size())
2501 this->__throw_length_error();
2502 __n = __external_cap_to_internal(__n);
2503 this->__begin_ = __storage_traits::allocate(this->__alloc(), __n);
2504 this->__size_ = 0;
2505 this->__cap() = __n;
2506}
2507
2508template <class _Allocator>
2509void
Marshall Clow3ff48e02018-05-22 16:20:28 +00002510vector<bool, _Allocator>::__vdeallocate() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002511{
Howard Hinnant76053d72013-06-27 19:35:32 +00002512 if (this->__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002513 {
2514 __storage_traits::deallocate(this->__alloc(), this->__begin_, __cap());
2515 __invalidate_all_iterators();
Howard Hinnant76053d72013-06-27 19:35:32 +00002516 this->__begin_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002517 this->__size_ = this->__cap() = 0;
2518 }
2519}
2520
2521template <class _Allocator>
2522typename vector<bool, _Allocator>::size_type
Howard Hinnant1c936782011-06-03 19:40:40 +00002523vector<bool, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002524{
2525 size_type __amax = __storage_traits::max_size(__alloc());
2526 size_type __nmax = numeric_limits<size_type>::max() / 2; // end() >= begin(), always
2527 if (__nmax / __bits_per_word <= __amax)
2528 return __nmax;
2529 return __internal_cap_to_external(__amax);
2530}
2531
2532// Precondition: __new_size > capacity()
2533template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002534inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002535typename vector<bool, _Allocator>::size_type
2536vector<bool, _Allocator>::__recommend(size_type __new_size) const
2537{
2538 const size_type __ms = max_size();
2539 if (__new_size > __ms)
2540 this->__throw_length_error();
2541 const size_type __cap = capacity();
2542 if (__cap >= __ms / 2)
2543 return __ms;
Arthur O'Dwyerc0fb14b2021-07-26 18:23:00 -04002544 return _VSTD::max(2 * __cap, __align_it(__new_size));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002545}
2546
2547// Default constructs __n objects starting at __end_
2548// Precondition: __n > 0
2549// Precondition: size() + __n <= capacity()
2550// Postcondition: size() == size() + __n
2551template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002552inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002553void
2554vector<bool, _Allocator>::__construct_at_end(size_type __n, bool __x)
2555{
2556 size_type __old_size = this->__size_;
2557 this->__size_ += __n;
Marshall Clow1893ec72018-10-23 20:07:45 +00002558 if (__old_size == 0 || ((__old_size - 1) / __bits_per_word) != ((this->__size_ - 1) / __bits_per_word))
2559 {
2560 if (this->__size_ <= __bits_per_word)
2561 this->__begin_[0] = __storage_type(0);
2562 else
2563 this->__begin_[(this->__size_ - 1) / __bits_per_word] = __storage_type(0);
2564 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002565 _VSTD::fill_n(__make_iter(__old_size), __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002566}
2567
2568template <class _Allocator>
2569template <class _ForwardIterator>
2570typename enable_if
2571<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002572 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002573 void
2574>::type
2575vector<bool, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
2576{
2577 size_type __old_size = this->__size_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002578 this->__size_ += _VSTD::distance(__first, __last);
Marshall Clow1893ec72018-10-23 20:07:45 +00002579 if (__old_size == 0 || ((__old_size - 1) / __bits_per_word) != ((this->__size_ - 1) / __bits_per_word))
2580 {
2581 if (this->__size_ <= __bits_per_word)
2582 this->__begin_[0] = __storage_type(0);
2583 else
2584 this->__begin_[(this->__size_ - 1) / __bits_per_word] = __storage_type(0);
2585 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002586 _VSTD::copy(__first, __last, __make_iter(__old_size));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002587}
2588
2589template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002590inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002591vector<bool, _Allocator>::vector()
Marshall Clowe546cbb2015-06-04 02:05:41 +00002592 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant76053d72013-06-27 19:35:32 +00002593 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002594 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002595 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002596{
2597}
2598
2599template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002600inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002601vector<bool, _Allocator>::vector(const allocator_type& __a)
Marshall Clow8f282f42015-06-04 00:10:20 +00002602#if _LIBCPP_STD_VER <= 14
2603 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
2604#else
2605 _NOEXCEPT
2606#endif
Howard Hinnant76053d72013-06-27 19:35:32 +00002607 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002608 __size_(0),
2609 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2610{
2611}
2612
2613template <class _Allocator>
2614vector<bool, _Allocator>::vector(size_type __n)
Howard Hinnant76053d72013-06-27 19:35:32 +00002615 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002616 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002617 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002618{
2619 if (__n > 0)
2620 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002621 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002622 __construct_at_end(__n, false);
2623 }
2624}
2625
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00002626#if _LIBCPP_STD_VER > 11
2627template <class _Allocator>
2628vector<bool, _Allocator>::vector(size_type __n, const allocator_type& __a)
2629 : __begin_(nullptr),
2630 __size_(0),
2631 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2632{
2633 if (__n > 0)
2634 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002635 __vallocate(__n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00002636 __construct_at_end(__n, false);
2637 }
2638}
2639#endif
2640
Howard Hinnantc51e1022010-05-11 19:42:16 +00002641template <class _Allocator>
2642vector<bool, _Allocator>::vector(size_type __n, const value_type& __x)
Howard Hinnant76053d72013-06-27 19:35:32 +00002643 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002644 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002645 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002646{
2647 if (__n > 0)
2648 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002649 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002650 __construct_at_end(__n, __x);
2651 }
2652}
2653
2654template <class _Allocator>
2655vector<bool, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002656 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002657 __size_(0),
2658 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2659{
2660 if (__n > 0)
2661 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002662 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002663 __construct_at_end(__n, __x);
2664 }
2665}
2666
2667template <class _Allocator>
2668template <class _InputIterator>
2669vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002670 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
2671 !__is_cpp17_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002672 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002673 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002674 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002675{
2676#ifndef _LIBCPP_NO_EXCEPTIONS
2677 try
2678 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002679#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002680 for (; __first != __last; ++__first)
2681 push_back(*__first);
2682#ifndef _LIBCPP_NO_EXCEPTIONS
2683 }
2684 catch (...)
2685 {
Howard Hinnant76053d72013-06-27 19:35:32 +00002686 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002687 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2688 __invalidate_all_iterators();
2689 throw;
2690 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002691#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002692}
2693
2694template <class _Allocator>
2695template <class _InputIterator>
2696vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002697 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
2698 !__is_cpp17_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002699 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002700 __size_(0),
2701 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2702{
2703#ifndef _LIBCPP_NO_EXCEPTIONS
2704 try
2705 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002706#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002707 for (; __first != __last; ++__first)
2708 push_back(*__first);
2709#ifndef _LIBCPP_NO_EXCEPTIONS
2710 }
2711 catch (...)
2712 {
Howard Hinnant76053d72013-06-27 19:35:32 +00002713 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002714 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2715 __invalidate_all_iterators();
2716 throw;
2717 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002718#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002719}
2720
2721template <class _Allocator>
2722template <class _ForwardIterator>
2723vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002724 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002725 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002726 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002727 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002728{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002729 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002730 if (__n > 0)
2731 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002732 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002733 __construct_at_end(__first, __last);
2734 }
2735}
2736
2737template <class _Allocator>
2738template <class _ForwardIterator>
2739vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002740 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002741 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002742 __size_(0),
2743 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2744{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002745 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002746 if (__n > 0)
2747 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002748 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002749 __construct_at_end(__first, __last);
2750 }
2751}
2752
Eric Fiseliered9e9362017-04-16 02:40:45 +00002753#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002754
Howard Hinnantc51e1022010-05-11 19:42:16 +00002755template <class _Allocator>
2756vector<bool, _Allocator>::vector(initializer_list<value_type> __il)
Howard Hinnant76053d72013-06-27 19:35:32 +00002757 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002758 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002759 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002760{
2761 size_type __n = static_cast<size_type>(__il.size());
2762 if (__n > 0)
2763 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002764 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002765 __construct_at_end(__il.begin(), __il.end());
2766 }
2767}
2768
2769template <class _Allocator>
2770vector<bool, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002771 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002772 __size_(0),
2773 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2774{
2775 size_type __n = static_cast<size_type>(__il.size());
2776 if (__n > 0)
2777 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002778 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002779 __construct_at_end(__il.begin(), __il.end());
2780 }
2781}
2782
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002783#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002784
Howard Hinnantc51e1022010-05-11 19:42:16 +00002785template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002786vector<bool, _Allocator>::~vector()
2787{
Howard Hinnant76053d72013-06-27 19:35:32 +00002788 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002789 __storage_traits::deallocate(__alloc(), __begin_, __cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002790 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002791}
2792
2793template <class _Allocator>
2794vector<bool, _Allocator>::vector(const vector& __v)
Howard Hinnant76053d72013-06-27 19:35:32 +00002795 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002796 __size_(0),
2797 __cap_alloc_(0, __storage_traits::select_on_container_copy_construction(__v.__alloc()))
2798{
2799 if (__v.size() > 0)
2800 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002801 __vallocate(__v.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002802 __construct_at_end(__v.begin(), __v.end());
2803 }
2804}
2805
2806template <class _Allocator>
2807vector<bool, _Allocator>::vector(const vector& __v, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002808 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002809 __size_(0),
2810 __cap_alloc_(0, __a)
2811{
2812 if (__v.size() > 0)
2813 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002814 __vallocate(__v.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002815 __construct_at_end(__v.begin(), __v.end());
2816 }
2817}
2818
2819template <class _Allocator>
2820vector<bool, _Allocator>&
2821vector<bool, _Allocator>::operator=(const vector& __v)
2822{
Mark de Wever357a1fc2021-09-28 19:15:18 +02002823 if (this != _VSTD::addressof(__v))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002824 {
2825 __copy_assign_alloc(__v);
2826 if (__v.__size_)
2827 {
2828 if (__v.__size_ > capacity())
2829 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002830 __vdeallocate();
2831 __vallocate(__v.__size_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002832 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002833 _VSTD::copy(__v.__begin_, __v.__begin_ + __external_cap_to_internal(__v.__size_), __begin_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002834 }
2835 __size_ = __v.__size_;
2836 }
2837 return *this;
2838}
2839
Eric Fiseliered9e9362017-04-16 02:40:45 +00002840#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant74279a52010-09-04 23:28:19 +00002841
Howard Hinnantc51e1022010-05-11 19:42:16 +00002842template <class _Allocator>
Eric Fiselier4f1534c2018-06-05 22:32:52 +00002843inline _LIBCPP_INLINE_VISIBILITY vector<bool, _Allocator>::vector(vector&& __v)
Marshall Clowe5108202015-07-14 14:46:32 +00002844#if _LIBCPP_STD_VER > 14
Eric Fiselier4f1534c2018-06-05 22:32:52 +00002845 _NOEXCEPT
Marshall Clowe5108202015-07-14 14:46:32 +00002846#else
Eric Fiselier4f1534c2018-06-05 22:32:52 +00002847 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowe5108202015-07-14 14:46:32 +00002848#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002849 : __begin_(__v.__begin_),
2850 __size_(__v.__size_),
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05002851 __cap_alloc_(_VSTD::move(__v.__cap_alloc_)) {
Howard Hinnant76053d72013-06-27 19:35:32 +00002852 __v.__begin_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002853 __v.__size_ = 0;
2854 __v.__cap() = 0;
2855}
2856
2857template <class _Allocator>
Arthur O'Dwyer9b9662f2021-03-01 17:08:24 -05002858vector<bool, _Allocator>::vector(vector&& __v, const __identity_t<allocator_type>& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002859 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002860 __size_(0),
2861 __cap_alloc_(0, __a)
2862{
2863 if (__a == allocator_type(__v.__alloc()))
2864 {
2865 this->__begin_ = __v.__begin_;
2866 this->__size_ = __v.__size_;
2867 this->__cap() = __v.__cap();
2868 __v.__begin_ = nullptr;
2869 __v.__cap() = __v.__size_ = 0;
2870 }
2871 else if (__v.size() > 0)
2872 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002873 __vallocate(__v.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002874 __construct_at_end(__v.begin(), __v.end());
2875 }
2876}
2877
2878template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002879inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002880vector<bool, _Allocator>&
2881vector<bool, _Allocator>::operator=(vector&& __v)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002882 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002883{
2884 __move_assign(__v, integral_constant<bool,
2885 __storage_traits::propagate_on_container_move_assignment::value>());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002886 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002887}
2888
2889template <class _Allocator>
2890void
2891vector<bool, _Allocator>::__move_assign(vector& __c, false_type)
2892{
2893 if (__alloc() != __c.__alloc())
2894 assign(__c.begin(), __c.end());
2895 else
2896 __move_assign(__c, true_type());
2897}
2898
2899template <class _Allocator>
2900void
2901vector<bool, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002902 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002903{
Marshall Clow3ff48e02018-05-22 16:20:28 +00002904 __vdeallocate();
Marshall Clowb4871fa2014-07-21 15:15:15 +00002905 __move_assign_alloc(__c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002906 this->__begin_ = __c.__begin_;
2907 this->__size_ = __c.__size_;
2908 this->__cap() = __c.__cap();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002909 __c.__begin_ = nullptr;
2910 __c.__cap() = __c.__size_ = 0;
2911}
Howard Hinnant74279a52010-09-04 23:28:19 +00002912
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002913#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002914
2915template <class _Allocator>
2916void
2917vector<bool, _Allocator>::assign(size_type __n, const value_type& __x)
2918{
2919 __size_ = 0;
2920 if (__n > 0)
2921 {
2922 size_type __c = capacity();
2923 if (__n <= __c)
2924 __size_ = __n;
2925 else
2926 {
Mikhail Maltsevf56b0692021-10-21 10:38:56 +01002927 vector __v(get_allocator());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002928 __v.reserve(__recommend(__n));
2929 __v.__size_ = __n;
2930 swap(__v);
2931 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002932 _VSTD::fill_n(begin(), __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002933 }
Eric Fiselier69c51982016-12-28 06:06:09 +00002934 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002935}
2936
2937template <class _Allocator>
2938template <class _InputIterator>
2939typename enable_if
2940<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002941 __is_cpp17_input_iterator<_InputIterator>::value &&
2942 !__is_cpp17_forward_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002943 void
2944>::type
2945vector<bool, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2946{
2947 clear();
2948 for (; __first != __last; ++__first)
2949 push_back(*__first);
2950}
2951
2952template <class _Allocator>
2953template <class _ForwardIterator>
2954typename enable_if
2955<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002956 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002957 void
2958>::type
2959vector<bool, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2960{
2961 clear();
Eric Fiselier6003c772016-12-23 23:37:52 +00002962 difference_type __ns = _VSTD::distance(__first, __last);
2963 _LIBCPP_ASSERT(__ns >= 0, "invalid range specified");
2964 const size_t __n = static_cast<size_type>(__ns);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002965 if (__n)
2966 {
2967 if (__n > capacity())
2968 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002969 __vdeallocate();
2970 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002971 }
2972 __construct_at_end(__first, __last);
2973 }
2974}
2975
2976template <class _Allocator>
2977void
2978vector<bool, _Allocator>::reserve(size_type __n)
2979{
2980 if (__n > capacity())
2981 {
Mikhail Maltsev0b0a51d2021-10-21 10:40:05 +01002982 if (__n > max_size())
2983 this->__throw_length_error();
Mikhail Maltsevf56b0692021-10-21 10:38:56 +01002984 vector __v(this->get_allocator());
Marshall Clow3ff48e02018-05-22 16:20:28 +00002985 __v.__vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002986 __v.__construct_at_end(this->begin(), this->end());
2987 swap(__v);
2988 __invalidate_all_iterators();
2989 }
2990}
2991
2992template <class _Allocator>
2993void
Howard Hinnant1c936782011-06-03 19:40:40 +00002994vector<bool, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002995{
2996 if (__external_cap_to_internal(size()) > __cap())
2997 {
2998#ifndef _LIBCPP_NO_EXCEPTIONS
2999 try
3000 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003001#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003002 vector(*this, allocator_type(__alloc())).swap(*this);
3003#ifndef _LIBCPP_NO_EXCEPTIONS
3004 }
3005 catch (...)
3006 {
3007 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003008#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003009 }
3010}
3011
3012template <class _Allocator>
3013typename vector<bool, _Allocator>::reference
3014vector<bool, _Allocator>::at(size_type __n)
3015{
3016 if (__n >= size())
3017 this->__throw_out_of_range();
3018 return (*this)[__n];
3019}
3020
3021template <class _Allocator>
3022typename vector<bool, _Allocator>::const_reference
3023vector<bool, _Allocator>::at(size_type __n) const
3024{
3025 if (__n >= size())
3026 this->__throw_out_of_range();
3027 return (*this)[__n];
3028}
3029
3030template <class _Allocator>
3031void
3032vector<bool, _Allocator>::push_back(const value_type& __x)
3033{
3034 if (this->__size_ == this->capacity())
3035 reserve(__recommend(this->__size_ + 1));
3036 ++this->__size_;
3037 back() = __x;
3038}
3039
3040template <class _Allocator>
3041typename vector<bool, _Allocator>::iterator
3042vector<bool, _Allocator>::insert(const_iterator __position, const value_type& __x)
3043{
3044 iterator __r;
3045 if (size() < capacity())
3046 {
3047 const_iterator __old_end = end();
3048 ++__size_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003049 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003050 __r = __const_iterator_cast(__position);
3051 }
3052 else
3053 {
Mikhail Maltsevf56b0692021-10-21 10:38:56 +01003054 vector __v(get_allocator());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003055 __v.reserve(__recommend(__size_ + 1));
3056 __v.__size_ = __size_ + 1;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003057 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3058 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003059 swap(__v);
3060 }
3061 *__r = __x;
3062 return __r;
3063}
3064
3065template <class _Allocator>
3066typename vector<bool, _Allocator>::iterator
3067vector<bool, _Allocator>::insert(const_iterator __position, size_type __n, const value_type& __x)
3068{
3069 iterator __r;
3070 size_type __c = capacity();
3071 if (__n <= __c && size() <= __c - __n)
3072 {
3073 const_iterator __old_end = end();
3074 __size_ += __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003075 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003076 __r = __const_iterator_cast(__position);
3077 }
3078 else
3079 {
Mikhail Maltsevf56b0692021-10-21 10:38:56 +01003080 vector __v(get_allocator());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003081 __v.reserve(__recommend(__size_ + __n));
3082 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003083 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3084 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003085 swap(__v);
3086 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003087 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003088 return __r;
3089}
3090
3091template <class _Allocator>
3092template <class _InputIterator>
3093typename enable_if
3094<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05003095 __is_cpp17_input_iterator <_InputIterator>::value &&
3096 !__is_cpp17_forward_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003097 typename vector<bool, _Allocator>::iterator
3098>::type
3099vector<bool, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
3100{
3101 difference_type __off = __position - begin();
3102 iterator __p = __const_iterator_cast(__position);
3103 iterator __old_end = end();
3104 for (; size() != capacity() && __first != __last; ++__first)
3105 {
3106 ++this->__size_;
3107 back() = *__first;
3108 }
Mikhail Maltsevf56b0692021-10-21 10:38:56 +01003109 vector __v(get_allocator());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003110 if (__first != __last)
3111 {
3112#ifndef _LIBCPP_NO_EXCEPTIONS
3113 try
3114 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003115#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003116 __v.assign(__first, __last);
3117 difference_type __old_size = static_cast<difference_type>(__old_end - begin());
3118 difference_type __old_p = __p - begin();
3119 reserve(__recommend(size() + __v.size()));
3120 __p = begin() + __old_p;
3121 __old_end = begin() + __old_size;
3122#ifndef _LIBCPP_NO_EXCEPTIONS
3123 }
3124 catch (...)
3125 {
3126 erase(__old_end, end());
3127 throw;
3128 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003129#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003130 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003131 __p = _VSTD::rotate(__p, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003132 insert(__p, __v.begin(), __v.end());
3133 return begin() + __off;
3134}
3135
3136template <class _Allocator>
3137template <class _ForwardIterator>
3138typename enable_if
3139<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05003140 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003141 typename vector<bool, _Allocator>::iterator
3142>::type
3143vector<bool, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
3144{
Eric Fiselier654dd332016-12-11 05:31:00 +00003145 const difference_type __n_signed = _VSTD::distance(__first, __last);
3146 _LIBCPP_ASSERT(__n_signed >= 0, "invalid range specified");
3147 const size_type __n = static_cast<size_type>(__n_signed);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003148 iterator __r;
3149 size_type __c = capacity();
3150 if (__n <= __c && size() <= __c - __n)
3151 {
3152 const_iterator __old_end = end();
3153 __size_ += __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003154 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003155 __r = __const_iterator_cast(__position);
3156 }
3157 else
3158 {
Mikhail Maltsevf56b0692021-10-21 10:38:56 +01003159 vector __v(get_allocator());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003160 __v.reserve(__recommend(__size_ + __n));
3161 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003162 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3163 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003164 swap(__v);
3165 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003166 _VSTD::copy(__first, __last, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003167 return __r;
3168}
3169
3170template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003171inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003172typename vector<bool, _Allocator>::iterator
3173vector<bool, _Allocator>::erase(const_iterator __position)
3174{
3175 iterator __r = __const_iterator_cast(__position);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003176 _VSTD::copy(__position + 1, this->cend(), __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003177 --__size_;
3178 return __r;
3179}
3180
3181template <class _Allocator>
3182typename vector<bool, _Allocator>::iterator
3183vector<bool, _Allocator>::erase(const_iterator __first, const_iterator __last)
3184{
3185 iterator __r = __const_iterator_cast(__first);
3186 difference_type __d = __last - __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003187 _VSTD::copy(__last, this->cend(), __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003188 __size_ -= __d;
3189 return __r;
3190}
3191
3192template <class _Allocator>
3193void
3194vector<bool, _Allocator>::swap(vector& __x)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003195#if _LIBCPP_STD_VER >= 14
3196 _NOEXCEPT
3197#else
Eric Fiselier69c51982016-12-28 06:06:09 +00003198 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003199 __is_nothrow_swappable<allocator_type>::value)
3200#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003201{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003202 _VSTD::swap(this->__begin_, __x.__begin_);
3203 _VSTD::swap(this->__size_, __x.__size_);
3204 _VSTD::swap(this->__cap(), __x.__cap());
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05003205 _VSTD::__swap_allocator(this->__alloc(), __x.__alloc(),
Marshall Clow8982dcd2015-07-13 20:04:56 +00003206 integral_constant<bool, __alloc_traits::propagate_on_container_swap::value>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003207}
3208
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003209template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003210void
3211vector<bool, _Allocator>::resize(size_type __sz, value_type __x)
3212{
3213 size_type __cs = size();
3214 if (__cs < __sz)
3215 {
3216 iterator __r;
3217 size_type __c = capacity();
3218 size_type __n = __sz - __cs;
3219 if (__n <= __c && __cs <= __c - __n)
3220 {
3221 __r = end();
3222 __size_ += __n;
3223 }
3224 else
3225 {
Mikhail Maltsevf56b0692021-10-21 10:38:56 +01003226 vector __v(get_allocator());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003227 __v.reserve(__recommend(__size_ + __n));
3228 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003229 __r = _VSTD::copy(cbegin(), cend(), __v.begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003230 swap(__v);
3231 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003232 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003233 }
3234 else
3235 __size_ = __sz;
3236}
3237
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003238template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003239void
Howard Hinnant1c936782011-06-03 19:40:40 +00003240vector<bool, _Allocator>::flip() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003241{
3242 // do middle whole words
3243 size_type __n = __size_;
3244 __storage_pointer __p = __begin_;
3245 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3246 *__p = ~*__p;
3247 // do last partial word
3248 if (__n > 0)
3249 {
3250 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3251 __storage_type __b = *__p & __m;
3252 *__p &= ~__m;
3253 *__p |= ~__b & __m;
3254 }
3255}
3256
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003257template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003258bool
3259vector<bool, _Allocator>::__invariants() const
3260{
Howard Hinnant76053d72013-06-27 19:35:32 +00003261 if (this->__begin_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003262 {
3263 if (this->__size_ != 0 || this->__cap() != 0)
3264 return false;
3265 }
3266 else
3267 {
3268 if (this->__cap() == 0)
3269 return false;
3270 if (this->__size_ > this->capacity())
3271 return false;
3272 }
3273 return true;
3274}
3275
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003276template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003277size_t
Howard Hinnant1c936782011-06-03 19:40:40 +00003278vector<bool, _Allocator>::__hash_code() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003279{
3280 size_t __h = 0;
3281 // do middle whole words
3282 size_type __n = __size_;
3283 __storage_pointer __p = __begin_;
3284 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3285 __h ^= *__p;
3286 // do last partial word
3287 if (__n > 0)
3288 {
3289 const __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3290 __h ^= *__p & __m;
3291 }
3292 return __h;
3293}
3294
3295template <class _Allocator>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003296struct _LIBCPP_TEMPLATE_VIS hash<vector<bool, _Allocator> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00003297 : public unary_function<vector<bool, _Allocator>, size_t>
3298{
Howard Hinnant1c265cd2010-09-23 18:58:28 +00003299 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +00003300 size_t operator()(const vector<bool, _Allocator>& __vec) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003301 {return __vec.__hash_code();}
3302};
3303
3304template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003305inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003306bool
3307operator==(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3308{
3309 const typename vector<_Tp, _Allocator>::size_type __sz = __x.size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003310 return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003311}
3312
3313template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003314inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003315bool
3316operator!=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3317{
3318 return !(__x == __y);
3319}
3320
3321template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003322inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003323bool
3324operator< (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3325{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003326 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003327}
3328
3329template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003330inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003331bool
3332operator> (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3333{
3334 return __y < __x;
3335}
3336
3337template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003338inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003339bool
3340operator>=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3341{
3342 return !(__x < __y);
3343}
3344
3345template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003346inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003347bool
3348operator<=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3349{
3350 return !(__y < __x);
3351}
3352
3353template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003354inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003355void
3356swap(vector<_Tp, _Allocator>& __x, vector<_Tp, _Allocator>& __y)
Howard Hinnant1c936782011-06-03 19:40:40 +00003357 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00003358{
3359 __x.swap(__y);
3360}
3361
Marshall Clow29b53f22018-12-14 18:49:35 +00003362#if _LIBCPP_STD_VER > 17
3363template <class _Tp, class _Allocator, class _Up>
Marek Kurdeja98b1412020-05-02 13:58:03 +02003364inline _LIBCPP_INLINE_VISIBILITY typename vector<_Tp, _Allocator>::size_type
3365erase(vector<_Tp, _Allocator>& __c, const _Up& __v) {
3366 auto __old_size = __c.size();
3367 __c.erase(_VSTD::remove(__c.begin(), __c.end(), __v), __c.end());
3368 return __old_size - __c.size();
3369}
Marshall Clow29b53f22018-12-14 18:49:35 +00003370
3371template <class _Tp, class _Allocator, class _Predicate>
Marek Kurdeja98b1412020-05-02 13:58:03 +02003372inline _LIBCPP_INLINE_VISIBILITY typename vector<_Tp, _Allocator>::size_type
3373erase_if(vector<_Tp, _Allocator>& __c, _Predicate __pred) {
3374 auto __old_size = __c.size();
3375 __c.erase(_VSTD::remove_if(__c.begin(), __c.end(), __pred), __c.end());
3376 return __old_size - __c.size();
3377}
Marshall Clow29b53f22018-12-14 18:49:35 +00003378#endif
3379
Howard Hinnantc51e1022010-05-11 19:42:16 +00003380_LIBCPP_END_NAMESPACE_STD
3381
Eric Fiselierf4433a32017-05-31 22:07:49 +00003382_LIBCPP_POP_MACROS
3383
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003384#endif // _LIBCPP_VECTOR