blob: fac1f95d7c523ef171980c9d67ba221adf881a3b [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 Dionne53dca682019-08-28 18:10:39 +0000305class _LIBCPP_TEMPLATE_VIS __vector_base_common
Howard Hinnantc51e1022010-05-11 19:42:16 +0000306{
307protected:
Louis Dionne16fe2952018-07-11 23:14:33 +0000308 _LIBCPP_INLINE_VISIBILITY __vector_base_common() {}
Marshall Clow8fea1612016-08-25 15:09:01 +0000309 _LIBCPP_NORETURN void __throw_length_error() const;
310 _LIBCPP_NORETURN void __throw_out_of_range() const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000311};
312
313template <bool __b>
314void
315__vector_base_common<__b>::__throw_length_error() const
316{
Marshall Clow8fea1612016-08-25 15:09:01 +0000317 _VSTD::__throw_length_error("vector");
Howard Hinnantc51e1022010-05-11 19:42:16 +0000318}
319
320template <bool __b>
321void
322__vector_base_common<__b>::__throw_out_of_range() const
323{
Marshall Clow8fea1612016-08-25 15:09:01 +0000324 _VSTD::__throw_out_of_range("vector");
Howard Hinnantc51e1022010-05-11 19:42:16 +0000325}
326
Eric Fiselier1b57fa82016-09-15 22:27:07 +0000327_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __vector_base_common<true>)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000328
329template <class _Tp, class _Allocator>
330class __vector_base
331 : protected __vector_base_common<true>
332{
Marshall Clowf0ca1492018-05-21 21:30:12 +0000333public:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000334 typedef _Allocator allocator_type;
335 typedef allocator_traits<allocator_type> __alloc_traits;
Marshall Clowf0ca1492018-05-21 21:30:12 +0000336 typedef typename __alloc_traits::size_type size_type;
337protected:
338 typedef _Tp value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000339 typedef value_type& reference;
340 typedef const value_type& const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000341 typedef typename __alloc_traits::difference_type difference_type;
342 typedef typename __alloc_traits::pointer pointer;
343 typedef typename __alloc_traits::const_pointer const_pointer;
344 typedef pointer iterator;
345 typedef const_pointer const_iterator;
346
347 pointer __begin_;
348 pointer __end_;
349 __compressed_pair<pointer, allocator_type> __end_cap_;
350
Howard Hinnant1c936782011-06-03 19:40:40 +0000351 _LIBCPP_INLINE_VISIBILITY
352 allocator_type& __alloc() _NOEXCEPT
353 {return __end_cap_.second();}
354 _LIBCPP_INLINE_VISIBILITY
355 const allocator_type& __alloc() const _NOEXCEPT
356 {return __end_cap_.second();}
357 _LIBCPP_INLINE_VISIBILITY
358 pointer& __end_cap() _NOEXCEPT
359 {return __end_cap_.first();}
360 _LIBCPP_INLINE_VISIBILITY
361 const pointer& __end_cap() const _NOEXCEPT
362 {return __end_cap_.first();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000363
Howard Hinnant1c936782011-06-03 19:40:40 +0000364 _LIBCPP_INLINE_VISIBILITY
365 __vector_base()
366 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnantcf823322010-12-17 14:46:43 +0000367 _LIBCPP_INLINE_VISIBILITY __vector_base(const allocator_type& __a);
Eric Fiselier4f1534c2018-06-05 22:32:52 +0000368#ifndef _LIBCPP_CXX03_LANG
369 _LIBCPP_INLINE_VISIBILITY __vector_base(allocator_type&& __a) _NOEXCEPT;
370#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000371 ~__vector_base();
372
Howard Hinnant1c936782011-06-03 19:40:40 +0000373 _LIBCPP_INLINE_VISIBILITY
374 void clear() _NOEXCEPT {__destruct_at_end(__begin_);}
375 _LIBCPP_INLINE_VISIBILITY
376 size_type capacity() const _NOEXCEPT
377 {return static_cast<size_type>(__end_cap() - __begin_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000378
Howard Hinnant1c936782011-06-03 19:40:40 +0000379 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76053d72013-06-27 19:35:32 +0000380 void __destruct_at_end(pointer __new_last) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000381
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000382 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000383 void __copy_assign_alloc(const __vector_base& __c)
384 {__copy_assign_alloc(__c, integral_constant<bool,
385 __alloc_traits::propagate_on_container_copy_assignment::value>());}
386
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000387 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000388 void __move_assign_alloc(__vector_base& __c)
Howard Hinnant1c936782011-06-03 19:40:40 +0000389 _NOEXCEPT_(
390 !__alloc_traits::propagate_on_container_move_assignment::value ||
391 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000392 {__move_assign_alloc(__c, integral_constant<bool,
393 __alloc_traits::propagate_on_container_move_assignment::value>());}
Louis Dionned24191c2021-08-19 12:39:16 -0400394
395 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
396 void __throw_length_error() const {
397#ifndef _LIBCPP_NO_EXCEPTIONS
398 __vector_base_common<true>::__throw_length_error();
399#else
400 _VSTD::abort();
401#endif
402 }
403
404 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
405 void __throw_out_of_range() const {
406#ifndef _LIBCPP_NO_EXCEPTIONS
407 __vector_base_common<true>::__throw_out_of_range();
408#else
409 _VSTD::abort();
410#endif
411 }
412
Howard Hinnantc51e1022010-05-11 19:42:16 +0000413private:
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000414 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000415 void __copy_assign_alloc(const __vector_base& __c, true_type)
416 {
417 if (__alloc() != __c.__alloc())
418 {
419 clear();
420 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
421 __begin_ = __end_ = __end_cap() = nullptr;
422 }
423 __alloc() = __c.__alloc();
424 }
425
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000426 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +0000427 void __copy_assign_alloc(const __vector_base&, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000428 {}
429
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000430 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +0000431 void __move_assign_alloc(__vector_base& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +0000432 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000433 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000434 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000435 }
436
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000437 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +0000438 void __move_assign_alloc(__vector_base&, false_type)
Howard Hinnant1c936782011-06-03 19:40:40 +0000439 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000440 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000441};
442
443template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000444inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000445void
Howard Hinnant76053d72013-06-27 19:35:32 +0000446__vector_base<_Tp, _Allocator>::__destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000447{
Bruce Mitchener405fdcd2017-03-23 14:39:23 +0000448 pointer __soon_to_be_end = __end_;
449 while (__new_last != __soon_to_be_end)
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500450 __alloc_traits::destroy(__alloc(), _VSTD::__to_address(--__soon_to_be_end));
Bruce Mitchener405fdcd2017-03-23 14:39:23 +0000451 __end_ = __new_last;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000452}
453
454template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000455inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000456__vector_base<_Tp, _Allocator>::__vector_base()
Howard Hinnant1c936782011-06-03 19:40:40 +0000457 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant76053d72013-06-27 19:35:32 +0000458 : __begin_(nullptr),
459 __end_(nullptr),
Eric Fiselier33ebfb62019-12-16 18:23:39 -0500460 __end_cap_(nullptr, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +0000461{
462}
463
464template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000465inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000466__vector_base<_Tp, _Allocator>::__vector_base(const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +0000467 : __begin_(nullptr),
468 __end_(nullptr),
469 __end_cap_(nullptr, __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000470{
471}
472
Eric Fiselier4f1534c2018-06-05 22:32:52 +0000473#ifndef _LIBCPP_CXX03_LANG
474template <class _Tp, class _Allocator>
475inline _LIBCPP_INLINE_VISIBILITY
476__vector_base<_Tp, _Allocator>::__vector_base(allocator_type&& __a) _NOEXCEPT
477 : __begin_(nullptr),
478 __end_(nullptr),
Arthur O'Dwyer07b22492020-11-27 11:02:06 -0500479 __end_cap_(nullptr, _VSTD::move(__a)) {}
Eric Fiselier4f1534c2018-06-05 22:32:52 +0000480#endif
481
Howard Hinnantc51e1022010-05-11 19:42:16 +0000482template <class _Tp, class _Allocator>
483__vector_base<_Tp, _Allocator>::~__vector_base()
484{
Howard Hinnant76053d72013-06-27 19:35:32 +0000485 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000486 {
487 clear();
488 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
489 }
490}
491
Eric Fiselier876c6862016-02-20 00:19:45 +0000492template <class _Tp, class _Allocator /* = allocator<_Tp> */>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000493class _LIBCPP_TEMPLATE_VIS vector
Howard Hinnantc51e1022010-05-11 19:42:16 +0000494 : private __vector_base<_Tp, _Allocator>
495{
496private:
497 typedef __vector_base<_Tp, _Allocator> __base;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000498 typedef allocator<_Tp> __default_allocator_type;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000499public:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000500 typedef vector __self;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000501 typedef _Tp value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000502 typedef _Allocator allocator_type;
503 typedef typename __base::__alloc_traits __alloc_traits;
504 typedef typename __base::reference reference;
505 typedef typename __base::const_reference const_reference;
506 typedef typename __base::size_type size_type;
507 typedef typename __base::difference_type difference_type;
508 typedef typename __base::pointer pointer;
509 typedef typename __base::const_pointer const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000510 typedef __wrap_iter<pointer> iterator;
511 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000512 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
513 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000514
Howard Hinnanta416ff02013-03-26 19:04:56 +0000515 static_assert((is_same<typename allocator_type::value_type, value_type>::value),
516 "Allocator::value_type must be same type as value_type");
517
Howard Hinnant1c936782011-06-03 19:40:40 +0000518 _LIBCPP_INLINE_VISIBILITY
Marshall Clowe546cbb2015-06-04 02:05:41 +0000519 vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant27e0e772011-09-14 18:33:51 +0000520 {
Louis Dionneba400782020-10-02 15:02:52 -0400521#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000522 __get_db()->__insert_c(this);
523#endif
524 }
525 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
Marshall Clow8f282f42015-06-04 00:10:20 +0000526#if _LIBCPP_STD_VER <= 14
527 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
528#else
529 _NOEXCEPT
530#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +0000531 : __base(__a)
532 {
Louis Dionneba400782020-10-02 15:02:52 -0400533#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000534 __get_db()->__insert_c(this);
535#endif
536 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000537 explicit vector(size_type __n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +0000538#if _LIBCPP_STD_VER > 11
539 explicit vector(size_type __n, const allocator_type& __a);
540#endif
Marshall Clowf0ca1492018-05-21 21:30:12 +0000541 vector(size_type __n, const value_type& __x);
542 vector(size_type __n, const value_type& __x, const allocator_type& __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000543 template <class _InputIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +0000544 vector(_InputIterator __first,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500545 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
546 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000547 is_constructible<
548 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +0000549 typename iterator_traits<_InputIterator>::reference>::value,
550 _InputIterator>::type __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000551 template <class _InputIterator>
552 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500553 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
554 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000555 is_constructible<
556 value_type,
557 typename iterator_traits<_InputIterator>::reference>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000558 template <class _ForwardIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +0000559 vector(_ForwardIterator __first,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500560 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000561 is_constructible<
562 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +0000563 typename iterator_traits<_ForwardIterator>::reference>::value,
564 _ForwardIterator>::type __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000565 template <class _ForwardIterator>
566 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500567 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000568 is_constructible<
569 value_type,
570 typename iterator_traits<_ForwardIterator>::reference>::value>::type* = 0);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000571
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000572 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant27e0e772011-09-14 18:33:51 +0000573 ~vector()
574 {
Marshall Clow68af4f32018-09-07 15:47:59 +0000575 __annotate_delete();
Louis Dionneba400782020-10-02 15:02:52 -0400576#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000577 __get_db()->__erase_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000578#endif
Marshall Clow68af4f32018-09-07 15:47:59 +0000579 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000580
581 vector(const vector& __x);
Arthur O'Dwyer9b9662f2021-03-01 17:08:24 -0500582 vector(const vector& __x, const __identity_t<allocator_type>& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000583 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000584 vector& operator=(const vector& __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000585
586#ifndef _LIBCPP_CXX03_LANG
587 _LIBCPP_INLINE_VISIBILITY
588 vector(initializer_list<value_type> __il);
589
590 _LIBCPP_INLINE_VISIBILITY
591 vector(initializer_list<value_type> __il, const allocator_type& __a);
592
Howard Hinnantcf823322010-12-17 14:46:43 +0000593 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000594 vector(vector&& __x)
Marshall Clowe5108202015-07-14 14:46:32 +0000595#if _LIBCPP_STD_VER > 14
596 _NOEXCEPT;
597#else
Howard Hinnant1c936782011-06-03 19:40:40 +0000598 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowe5108202015-07-14 14:46:32 +0000599#endif
Eric Fiseliered9e9362017-04-16 02:40:45 +0000600
Howard Hinnantcf823322010-12-17 14:46:43 +0000601 _LIBCPP_INLINE_VISIBILITY
Arthur O'Dwyer9b9662f2021-03-01 17:08:24 -0500602 vector(vector&& __x, const __identity_t<allocator_type>& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000603 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000604 vector& operator=(vector&& __x)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000605 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiseliered9e9362017-04-16 02:40:45 +0000606
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000607 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000608 vector& operator=(initializer_list<value_type> __il)
609 {assign(__il.begin(), __il.end()); return *this;}
Eric Fiseliered9e9362017-04-16 02:40:45 +0000610
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400611#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000612
613 template <class _InputIterator>
614 typename enable_if
615 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500616 __is_cpp17_input_iterator <_InputIterator>::value &&
617 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000618 is_constructible<
619 value_type,
620 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000621 void
622 >::type
623 assign(_InputIterator __first, _InputIterator __last);
624 template <class _ForwardIterator>
625 typename enable_if
626 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500627 __is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000628 is_constructible<
629 value_type,
630 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000631 void
632 >::type
633 assign(_ForwardIterator __first, _ForwardIterator __last);
634
635 void assign(size_type __n, const_reference __u);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000636
637#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000638 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000639 void assign(initializer_list<value_type> __il)
640 {assign(__il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +0000641#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000642
Howard Hinnant1c936782011-06-03 19:40:40 +0000643 _LIBCPP_INLINE_VISIBILITY
644 allocator_type get_allocator() const _NOEXCEPT
645 {return this->__alloc();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000646
Howard Hinnant1c936782011-06-03 19:40:40 +0000647 _LIBCPP_INLINE_VISIBILITY iterator begin() _NOEXCEPT;
648 _LIBCPP_INLINE_VISIBILITY const_iterator begin() const _NOEXCEPT;
649 _LIBCPP_INLINE_VISIBILITY iterator end() _NOEXCEPT;
650 _LIBCPP_INLINE_VISIBILITY const_iterator end() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000651
Howard Hinnant1c936782011-06-03 19:40:40 +0000652 _LIBCPP_INLINE_VISIBILITY
653 reverse_iterator rbegin() _NOEXCEPT
654 {return reverse_iterator(end());}
655 _LIBCPP_INLINE_VISIBILITY
656 const_reverse_iterator rbegin() const _NOEXCEPT
657 {return const_reverse_iterator(end());}
658 _LIBCPP_INLINE_VISIBILITY
659 reverse_iterator rend() _NOEXCEPT
660 {return reverse_iterator(begin());}
661 _LIBCPP_INLINE_VISIBILITY
662 const_reverse_iterator rend() const _NOEXCEPT
663 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000664
Howard Hinnant1c936782011-06-03 19:40:40 +0000665 _LIBCPP_INLINE_VISIBILITY
666 const_iterator cbegin() const _NOEXCEPT
667 {return begin();}
668 _LIBCPP_INLINE_VISIBILITY
669 const_iterator cend() const _NOEXCEPT
670 {return end();}
671 _LIBCPP_INLINE_VISIBILITY
672 const_reverse_iterator crbegin() const _NOEXCEPT
673 {return rbegin();}
674 _LIBCPP_INLINE_VISIBILITY
675 const_reverse_iterator crend() const _NOEXCEPT
676 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000677
Howard Hinnant1c936782011-06-03 19:40:40 +0000678 _LIBCPP_INLINE_VISIBILITY
679 size_type size() const _NOEXCEPT
680 {return static_cast<size_type>(this->__end_ - this->__begin_);}
681 _LIBCPP_INLINE_VISIBILITY
682 size_type capacity() const _NOEXCEPT
683 {return __base::capacity();}
Marshall Clow425f5752017-11-15 05:51:26 +0000684 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000685 bool empty() const _NOEXCEPT
686 {return this->__begin_ == this->__end_;}
687 size_type max_size() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000688 void reserve(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +0000689 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000690
Marshall Clowd6470492019-03-15 00:29:35 +0000691 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) _NOEXCEPT;
692 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000693 reference at(size_type __n);
694 const_reference at(size_type __n) const;
695
Marshall Clowd6470492019-03-15 00:29:35 +0000696 _LIBCPP_INLINE_VISIBILITY reference front() _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000697 {
Kristina Bessonovaaeeaa7e2021-05-09 19:29:56 +0200698 _LIBCPP_ASSERT(!empty(), "front() called on an empty vector");
Howard Hinnant27e0e772011-09-14 18:33:51 +0000699 return *this->__begin_;
700 }
Marshall Clowd6470492019-03-15 00:29:35 +0000701 _LIBCPP_INLINE_VISIBILITY const_reference front() const _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000702 {
Kristina Bessonovaaeeaa7e2021-05-09 19:29:56 +0200703 _LIBCPP_ASSERT(!empty(), "front() called on an empty vector");
Howard Hinnant27e0e772011-09-14 18:33:51 +0000704 return *this->__begin_;
705 }
Marshall Clowd6470492019-03-15 00:29:35 +0000706 _LIBCPP_INLINE_VISIBILITY reference back() _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000707 {
Kristina Bessonovaaeeaa7e2021-05-09 19:29:56 +0200708 _LIBCPP_ASSERT(!empty(), "back() called on an empty vector");
Howard Hinnant27e0e772011-09-14 18:33:51 +0000709 return *(this->__end_ - 1);
710 }
Marshall Clowd6470492019-03-15 00:29:35 +0000711 _LIBCPP_INLINE_VISIBILITY const_reference back() const _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000712 {
Kristina Bessonovaaeeaa7e2021-05-09 19:29:56 +0200713 _LIBCPP_ASSERT(!empty(), "back() called on an empty vector");
Howard Hinnant27e0e772011-09-14 18:33:51 +0000714 return *(this->__end_ - 1);
715 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000716
Howard Hinnant1c936782011-06-03 19:40:40 +0000717 _LIBCPP_INLINE_VISIBILITY
718 value_type* data() _NOEXCEPT
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500719 {return _VSTD::__to_address(this->__begin_);}
Howard Hinnant1c936782011-06-03 19:40:40 +0000720 _LIBCPP_INLINE_VISIBILITY
721 const value_type* data() const _NOEXCEPT
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500722 {return _VSTD::__to_address(this->__begin_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000723
Eric Fiselier96919722017-10-17 13:03:17 +0000724#ifdef _LIBCPP_CXX03_LANG
725 _LIBCPP_INLINE_VISIBILITY
726 void __emplace_back(const value_type& __x) { push_back(__x); }
727#else
728 template <class _Arg>
729 _LIBCPP_INLINE_VISIBILITY
730 void __emplace_back(_Arg&& __arg) {
731 emplace_back(_VSTD::forward<_Arg>(__arg));
732 }
733#endif
734
Howard Hinnantcf823322010-12-17 14:46:43 +0000735 _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000736
737#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000738 _LIBCPP_INLINE_VISIBILITY void push_back(value_type&& __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000739
Howard Hinnantc51e1022010-05-11 19:42:16 +0000740 template <class... _Args>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000741 _LIBCPP_INLINE_VISIBILITY
Marshall Clowea52cc42017-01-24 23:09:12 +0000742#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +0000743 reference emplace_back(_Args&&... __args);
Marshall Clowea52cc42017-01-24 23:09:12 +0000744#else
745 void emplace_back(_Args&&... __args);
746#endif
Eric Fiseliered9e9362017-04-16 02:40:45 +0000747#endif // !_LIBCPP_CXX03_LANG
748
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000749 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000750 void pop_back();
751
752 iterator insert(const_iterator __position, const_reference __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000753
754#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000755 iterator insert(const_iterator __position, value_type&& __x);
756 template <class... _Args>
757 iterator emplace(const_iterator __position, _Args&&... __args);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400758#endif // !_LIBCPP_CXX03_LANG
Eric Fiseliered9e9362017-04-16 02:40:45 +0000759
Howard Hinnantc51e1022010-05-11 19:42:16 +0000760 iterator insert(const_iterator __position, size_type __n, const_reference __x);
761 template <class _InputIterator>
762 typename enable_if
763 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500764 __is_cpp17_input_iterator <_InputIterator>::value &&
765 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000766 is_constructible<
767 value_type,
768 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000769 iterator
770 >::type
771 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
772 template <class _ForwardIterator>
773 typename enable_if
774 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500775 __is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000776 is_constructible<
777 value_type,
778 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000779 iterator
780 >::type
781 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000782
783#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000784 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000785 iterator insert(const_iterator __position, initializer_list<value_type> __il)
786 {return insert(__position, __il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +0000787#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000788
Howard Hinnantcf823322010-12-17 14:46:43 +0000789 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000790 iterator erase(const_iterator __first, const_iterator __last);
791
Howard Hinnant1c936782011-06-03 19:40:40 +0000792 _LIBCPP_INLINE_VISIBILITY
793 void clear() _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000794 {
Marshall Clow2cd9d372014-05-08 14:14:06 +0000795 size_type __old_size = size();
Howard Hinnant27e0e772011-09-14 18:33:51 +0000796 __base::clear();
Marshall Clow2cd9d372014-05-08 14:14:06 +0000797 __annotate_shrink(__old_size);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000798 __invalidate_all_iterators();
799 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000800
801 void resize(size_type __sz);
802 void resize(size_type __sz, const_reference __x);
803
Howard Hinnant1c936782011-06-03 19:40:40 +0000804 void swap(vector&)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000805#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +0000806 _NOEXCEPT;
Marshall Clow8982dcd2015-07-13 20:04:56 +0000807#else
Eric Fiselier873b8d32019-03-18 21:50:12 +0000808 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +0000809 __is_nothrow_swappable<allocator_type>::value);
810#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000811
812 bool __invariants() const;
813
Louis Dionneba400782020-10-02 15:02:52 -0400814#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000815
816 bool __dereferenceable(const const_iterator* __i) const;
817 bool __decrementable(const const_iterator* __i) const;
818 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
819 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
820
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400821#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000822
Howard Hinnantc51e1022010-05-11 19:42:16 +0000823private:
Howard Hinnantcf823322010-12-17 14:46:43 +0000824 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Eric Fiselier69c51982016-12-28 06:06:09 +0000825 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(pointer __new_last);
Marshall Clow3ff48e02018-05-22 16:20:28 +0000826 void __vallocate(size_type __n);
827 void __vdeallocate() _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000828 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
Howard Hinnanta5589382011-01-04 19:53:31 +0000829 void __construct_at_end(size_type __n);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000830 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000831 void __construct_at_end(size_type __n, const_reference __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000832 template <class _ForwardIterator>
833 typename enable_if
834 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500835 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000836 void
837 >::type
Eric Fiselieracfe6f02015-03-31 16:54:19 +0000838 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000839 void __append(size_type __n);
840 void __append(size_type __n, const_reference __x);
Howard Hinnantcf823322010-12-17 14:46:43 +0000841 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000842 iterator __make_iter(pointer __p) _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000843 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000844 const_iterator __make_iter(const_pointer __p) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000845 void __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v);
846 pointer __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p);
847 void __move_range(pointer __from_s, pointer __from_e, pointer __to);
Howard Hinnant1c936782011-06-03 19:40:40 +0000848 void __move_assign(vector& __c, true_type)
849 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000850 void __move_assign(vector& __c, false_type)
851 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000852 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76053d72013-06-27 19:35:32 +0000853 void __destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000854 {
Eric Fiselier69c51982016-12-28 06:06:09 +0000855 __invalidate_iterators_past(__new_last);
Marshall Clow2cd9d372014-05-08 14:14:06 +0000856 size_type __old_size = size();
Howard Hinnant27e0e772011-09-14 18:33:51 +0000857 __base::__destruct_at_end(__new_last);
Marshall Clow2cd9d372014-05-08 14:14:06 +0000858 __annotate_shrink(__old_size);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000859 }
Eric Fiseliered9e9362017-04-16 02:40:45 +0000860
861#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier7110f442019-03-19 19:19:44 +0000862 template <class _Up>
863 _LIBCPP_INLINE_VISIBILITY
864 inline void __push_back_slow_path(_Up&& __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000865
Howard Hinnantb6c49562012-02-26 15:30:12 +0000866 template <class... _Args>
Eric Fiselier7110f442019-03-19 19:19:44 +0000867 _LIBCPP_INLINE_VISIBILITY
868 inline void __emplace_back_slow_path(_Args&&... __args);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000869#else
Eric Fiselier7110f442019-03-19 19:19:44 +0000870 template <class _Up>
871 _LIBCPP_INLINE_VISIBILITY
872 inline void __push_back_slow_path(_Up& __x);
Howard Hinnantb6c49562012-02-26 15:30:12 +0000873#endif
Eric Fiseliered9e9362017-04-16 02:40:45 +0000874
Marshall Clow2cd9d372014-05-08 14:14:06 +0000875 // The following functions are no-ops outside of AddressSanitizer mode.
876 // We call annotatations only for the default Allocator because other allocators
877 // may not meet the AddressSanitizer alignment constraints.
878 // See the documentation for __sanitizer_annotate_contiguous_container for more details.
Marshall Clow2cd9d372014-05-08 14:14:06 +0000879#ifndef _LIBCPP_HAS_NO_ASAN
Eric Fiselier6003c772016-12-23 23:37:52 +0000880 void __annotate_contiguous_container(const void *__beg, const void *__end,
881 const void *__old_mid,
882 const void *__new_mid) const
883 {
884
Marshall Clow2cd9d372014-05-08 14:14:06 +0000885 if (__beg && is_same<allocator_type, __default_allocator_type>::value)
886 __sanitizer_annotate_contiguous_container(__beg, __end, __old_mid, __new_mid);
Marshall Clow2cd9d372014-05-08 14:14:06 +0000887 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000888#else
889 _LIBCPP_INLINE_VISIBILITY
890 void __annotate_contiguous_container(const void*, const void*, const void*,
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000891 const void*) const _NOEXCEPT {}
Eric Fiselier6003c772016-12-23 23:37:52 +0000892#endif
893 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000894 void __annotate_new(size_type __current_size) const _NOEXCEPT {
Marshall Clow2cd9d372014-05-08 14:14:06 +0000895 __annotate_contiguous_container(data(), data() + capacity(),
896 data() + capacity(), data() + __current_size);
897 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000898
899 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000900 void __annotate_delete() const _NOEXCEPT {
Marshall Clow2cd9d372014-05-08 14:14:06 +0000901 __annotate_contiguous_container(data(), data() + capacity(),
902 data() + size(), data() + capacity());
903 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000904
905 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000906 void __annotate_increase(size_type __n) const _NOEXCEPT
Marshall Clow2cd9d372014-05-08 14:14:06 +0000907 {
908 __annotate_contiguous_container(data(), data() + capacity(),
909 data() + size(), data() + size() + __n);
910 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000911
912 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000913 void __annotate_shrink(size_type __old_size) const _NOEXCEPT
Marshall Clow2cd9d372014-05-08 14:14:06 +0000914 {
915 __annotate_contiguous_container(data(), data() + capacity(),
916 data() + __old_size, data() + size());
917 }
Marshall Clowc78ffa52014-09-03 21:37:43 +0000918
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000919 struct _ConstructTransaction {
920 explicit _ConstructTransaction(vector &__v, size_type __n)
921 : __v_(__v), __pos_(__v.__end_), __new_end_(__v.__end_ + __n) {
922#ifndef _LIBCPP_HAS_NO_ASAN
923 __v_.__annotate_increase(__n);
924#endif
925 }
926 ~_ConstructTransaction() {
927 __v_.__end_ = __pos_;
928#ifndef _LIBCPP_HAS_NO_ASAN
929 if (__pos_ != __new_end_) {
930 __v_.__annotate_shrink(__new_end_ - __v_.__begin_);
931 }
932#endif
933 }
934
935 vector &__v_;
936 pointer __pos_;
937 const_pointer const __new_end_;
938
939 private:
940 _ConstructTransaction(_ConstructTransaction const&) = delete;
941 _ConstructTransaction& operator=(_ConstructTransaction const&) = delete;
942 };
943
944 template <class ..._Args>
945 _LIBCPP_INLINE_VISIBILITY
946 void __construct_one_at_end(_Args&& ...__args) {
947 _ConstructTransaction __tx(*this, 1);
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500948 __alloc_traits::construct(this->__alloc(), _VSTD::__to_address(__tx.__pos_),
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000949 _VSTD::forward<_Args>(__args)...);
950 ++__tx.__pos_;
951 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000952};
953
Louis Dionned59f8a52021-08-17 11:59:07 -0400954#if _LIBCPP_STD_VER >= 17
Marshall Clowf0ca1492018-05-21 21:30:12 +0000955template<class _InputIterator,
Arthur O'Dwyer56226762021-03-03 23:02:20 -0500956 class _Alloc = allocator<__iter_value_type<_InputIterator>>,
957 class = _EnableIf<__is_allocator<_Alloc>::value>
Marshall Clowf0ca1492018-05-21 21:30:12 +0000958 >
959vector(_InputIterator, _InputIterator)
Arthur O'Dwyer56226762021-03-03 23:02:20 -0500960 -> vector<__iter_value_type<_InputIterator>, _Alloc>;
Marshall Clowf0ca1492018-05-21 21:30:12 +0000961
962template<class _InputIterator,
963 class _Alloc,
Arthur O'Dwyer56226762021-03-03 23:02:20 -0500964 class = _EnableIf<__is_allocator<_Alloc>::value>
Marshall Clowf0ca1492018-05-21 21:30:12 +0000965 >
966vector(_InputIterator, _InputIterator, _Alloc)
Arthur O'Dwyer56226762021-03-03 23:02:20 -0500967 -> vector<__iter_value_type<_InputIterator>, _Alloc>;
Marshall Clowf0ca1492018-05-21 21:30:12 +0000968#endif
969
Howard Hinnantc51e1022010-05-11 19:42:16 +0000970template <class _Tp, class _Allocator>
971void
972vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v)
973{
Eric Fiselier909fe962019-09-13 16:09:33 +0000974
Marshall Clow2cd9d372014-05-08 14:14:06 +0000975 __annotate_delete();
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -0500976 _VSTD::__construct_backward_with_exception_guarantees(this->__alloc(), this->__begin_, this->__end_, __v.__begin_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000977 _VSTD::swap(this->__begin_, __v.__begin_);
978 _VSTD::swap(this->__end_, __v.__end_);
979 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000980 __v.__first_ = __v.__begin_;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000981 __annotate_new(size());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000982 __invalidate_all_iterators();
983}
984
985template <class _Tp, class _Allocator>
986typename vector<_Tp, _Allocator>::pointer
987vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p)
988{
Marshall Clow2cd9d372014-05-08 14:14:06 +0000989 __annotate_delete();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000990 pointer __r = __v.__begin_;
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -0500991 _VSTD::__construct_backward_with_exception_guarantees(this->__alloc(), this->__begin_, __p, __v.__begin_);
992 _VSTD::__construct_forward_with_exception_guarantees(this->__alloc(), __p, this->__end_, __v.__end_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000993 _VSTD::swap(this->__begin_, __v.__begin_);
994 _VSTD::swap(this->__end_, __v.__end_);
995 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000996 __v.__first_ = __v.__begin_;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000997 __annotate_new(size());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000998 __invalidate_all_iterators();
999 return __r;
1000}
1001
1002// Allocate space for __n objects
1003// throws length_error if __n > max_size()
1004// throws (probably bad_alloc) if memory run out
1005// Precondition: __begin_ == __end_ == __end_cap() == 0
1006// Precondition: __n > 0
1007// Postcondition: capacity() == __n
1008// Postcondition: size() == 0
1009template <class _Tp, class _Allocator>
1010void
Marshall Clow3ff48e02018-05-22 16:20:28 +00001011vector<_Tp, _Allocator>::__vallocate(size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001012{
1013 if (__n > max_size())
1014 this->__throw_length_error();
1015 this->__begin_ = this->__end_ = __alloc_traits::allocate(this->__alloc(), __n);
1016 this->__end_cap() = this->__begin_ + __n;
Marshall Clow2cd9d372014-05-08 14:14:06 +00001017 __annotate_new(0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001018}
1019
1020template <class _Tp, class _Allocator>
1021void
Marshall Clow3ff48e02018-05-22 16:20:28 +00001022vector<_Tp, _Allocator>::__vdeallocate() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001023{
Howard Hinnant76053d72013-06-27 19:35:32 +00001024 if (this->__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001025 {
1026 clear();
1027 __alloc_traits::deallocate(this->__alloc(), this->__begin_, capacity());
Howard Hinnant76053d72013-06-27 19:35:32 +00001028 this->__begin_ = this->__end_ = this->__end_cap() = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001029 }
1030}
1031
1032template <class _Tp, class _Allocator>
1033typename vector<_Tp, _Allocator>::size_type
Howard Hinnant1c936782011-06-03 19:40:40 +00001034vector<_Tp, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001035{
Eric Fiselierb5d9f442016-11-23 01:18:56 +00001036 return _VSTD::min<size_type>(__alloc_traits::max_size(this->__alloc()),
1037 numeric_limits<difference_type>::max());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001038}
1039
1040// Precondition: __new_size > capacity()
1041template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001042inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001043typename vector<_Tp, _Allocator>::size_type
1044vector<_Tp, _Allocator>::__recommend(size_type __new_size) const
1045{
1046 const size_type __ms = max_size();
1047 if (__new_size > __ms)
1048 this->__throw_length_error();
1049 const size_type __cap = capacity();
1050 if (__cap >= __ms / 2)
1051 return __ms;
Arthur O'Dwyerc0fb14b2021-07-26 18:23:00 -04001052 return _VSTD::max<size_type>(2 * __cap, __new_size);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001053}
1054
1055// Default constructs __n objects starting at __end_
1056// throws if construction throws
1057// Precondition: __n > 0
1058// Precondition: size() + __n <= capacity()
1059// Postcondition: size() == size() + __n
1060template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001061void
1062vector<_Tp, _Allocator>::__construct_at_end(size_type __n)
1063{
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001064 _ConstructTransaction __tx(*this, __n);
Martijn Vels1e2e1b82020-06-18 13:14:02 -04001065 const_pointer __new_end = __tx.__new_end_;
1066 for (pointer __pos = __tx.__pos_; __pos != __new_end; ++__pos, __tx.__pos_ = __pos) {
1067 __alloc_traits::construct(this->__alloc(), _VSTD::__to_address(__pos));
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001068 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001069}
1070
Howard Hinnantc51e1022010-05-11 19:42:16 +00001071// Copy constructs __n objects starting at __end_ from __x
1072// throws if construction throws
1073// Precondition: __n > 0
1074// Precondition: size() + __n <= capacity()
1075// Postcondition: size() == old size() + __n
1076// Postcondition: [i] == __x for all i in [size() - __n, __n)
1077template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001078inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001079void
1080vector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
1081{
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001082 _ConstructTransaction __tx(*this, __n);
Martijn Vels1e2e1b82020-06-18 13:14:02 -04001083 const_pointer __new_end = __tx.__new_end_;
1084 for (pointer __pos = __tx.__pos_; __pos != __new_end; ++__pos, __tx.__pos_ = __pos) {
1085 __alloc_traits::construct(this->__alloc(), _VSTD::__to_address(__pos), __x);
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001086 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001087}
1088
1089template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001090template <class _ForwardIterator>
1091typename enable_if
1092<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001093 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001094 void
1095>::type
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001096vector<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001097{
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001098 _ConstructTransaction __tx(*this, __n);
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05001099 _VSTD::__construct_range_forward(this->__alloc(), __first, __last, __tx.__pos_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001100}
1101
1102// Default constructs __n objects starting at __end_
1103// throws if construction throws
1104// Postcondition: size() == size() + __n
Howard Hinnant1c936782011-06-03 19:40:40 +00001105// Exception safety: strong.
Howard Hinnantc51e1022010-05-11 19:42:16 +00001106template <class _Tp, class _Allocator>
1107void
1108vector<_Tp, _Allocator>::__append(size_type __n)
1109{
1110 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1111 this->__construct_at_end(__n);
1112 else
1113 {
1114 allocator_type& __a = this->__alloc();
1115 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1116 __v.__construct_at_end(__n);
1117 __swap_out_circular_buffer(__v);
1118 }
1119}
1120
1121// Default constructs __n objects starting at __end_
1122// throws if construction throws
1123// Postcondition: size() == size() + __n
Howard Hinnant1c936782011-06-03 19:40:40 +00001124// Exception safety: strong.
Howard Hinnantc51e1022010-05-11 19:42:16 +00001125template <class _Tp, class _Allocator>
1126void
1127vector<_Tp, _Allocator>::__append(size_type __n, const_reference __x)
1128{
1129 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1130 this->__construct_at_end(__n, __x);
1131 else
1132 {
1133 allocator_type& __a = this->__alloc();
1134 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1135 __v.__construct_at_end(__n, __x);
1136 __swap_out_circular_buffer(__v);
1137 }
1138}
1139
1140template <class _Tp, class _Allocator>
1141vector<_Tp, _Allocator>::vector(size_type __n)
1142{
Louis Dionneba400782020-10-02 15:02:52 -04001143#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001144 __get_db()->__insert_c(this);
1145#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001146 if (__n > 0)
1147 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001148 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001149 __construct_at_end(__n);
1150 }
1151}
1152
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00001153#if _LIBCPP_STD_VER > 11
1154template <class _Tp, class _Allocator>
1155vector<_Tp, _Allocator>::vector(size_type __n, const allocator_type& __a)
1156 : __base(__a)
1157{
Louis Dionneba400782020-10-02 15:02:52 -04001158#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00001159 __get_db()->__insert_c(this);
1160#endif
1161 if (__n > 0)
1162 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001163 __vallocate(__n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00001164 __construct_at_end(__n);
1165 }
1166}
1167#endif
1168
Howard Hinnantc51e1022010-05-11 19:42:16 +00001169template <class _Tp, class _Allocator>
Marshall Clowf0ca1492018-05-21 21:30:12 +00001170vector<_Tp, _Allocator>::vector(size_type __n, const value_type& __x)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001171{
Louis Dionneba400782020-10-02 15:02:52 -04001172#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001173 __get_db()->__insert_c(this);
1174#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001175 if (__n > 0)
1176 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001177 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001178 __construct_at_end(__n, __x);
1179 }
1180}
1181
1182template <class _Tp, class _Allocator>
Marshall Clowf0ca1492018-05-21 21:30:12 +00001183vector<_Tp, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001184 : __base(__a)
1185{
Louis Dionneba400782020-10-02 15:02:52 -04001186#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001187 __get_db()->__insert_c(this);
1188#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001189 if (__n > 0)
1190 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001191 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001192 __construct_at_end(__n, __x);
1193 }
1194}
1195
1196template <class _Tp, class _Allocator>
1197template <class _InputIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +00001198vector<_Tp, _Allocator>::vector(_InputIterator __first,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001199 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
1200 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001201 is_constructible<
1202 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +00001203 typename iterator_traits<_InputIterator>::reference>::value,
1204 _InputIterator>::type __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001205{
Louis Dionneba400782020-10-02 15:02:52 -04001206#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001207 __get_db()->__insert_c(this);
1208#endif
Howard Hinnant9cd22302011-09-16 18:41:29 +00001209 for (; __first != __last; ++__first)
Eric Fiselier96919722017-10-17 13:03:17 +00001210 __emplace_back(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001211}
1212
1213template <class _Tp, class _Allocator>
1214template <class _InputIterator>
1215vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001216 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
1217 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001218 is_constructible<
1219 value_type,
1220 typename iterator_traits<_InputIterator>::reference>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001221 : __base(__a)
1222{
Louis Dionneba400782020-10-02 15:02:52 -04001223#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001224 __get_db()->__insert_c(this);
1225#endif
Howard Hinnant9cd22302011-09-16 18:41:29 +00001226 for (; __first != __last; ++__first)
Eric Fiselier96919722017-10-17 13:03:17 +00001227 __emplace_back(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001228}
1229
1230template <class _Tp, class _Allocator>
1231template <class _ForwardIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +00001232vector<_Tp, _Allocator>::vector(_ForwardIterator __first,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001233 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001234 is_constructible<
1235 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +00001236 typename iterator_traits<_ForwardIterator>::reference>::value,
1237 _ForwardIterator>::type __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001238{
Louis Dionneba400782020-10-02 15:02:52 -04001239#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001240 __get_db()->__insert_c(this);
1241#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001242 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001243 if (__n > 0)
1244 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001245 __vallocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001246 __construct_at_end(__first, __last, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001247 }
1248}
1249
1250template <class _Tp, class _Allocator>
1251template <class _ForwardIterator>
1252vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001253 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001254 is_constructible<
1255 value_type,
1256 typename iterator_traits<_ForwardIterator>::reference>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001257 : __base(__a)
1258{
Louis Dionneba400782020-10-02 15:02:52 -04001259#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001260 __get_db()->__insert_c(this);
1261#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001262 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001263 if (__n > 0)
1264 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001265 __vallocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001266 __construct_at_end(__first, __last, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001267 }
1268}
1269
1270template <class _Tp, class _Allocator>
1271vector<_Tp, _Allocator>::vector(const vector& __x)
1272 : __base(__alloc_traits::select_on_container_copy_construction(__x.__alloc()))
1273{
Louis Dionneba400782020-10-02 15:02:52 -04001274#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001275 __get_db()->__insert_c(this);
1276#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001277 size_type __n = __x.size();
1278 if (__n > 0)
1279 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001280 __vallocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001281 __construct_at_end(__x.__begin_, __x.__end_, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001282 }
1283}
1284
1285template <class _Tp, class _Allocator>
Arthur O'Dwyer9b9662f2021-03-01 17:08:24 -05001286vector<_Tp, _Allocator>::vector(const vector& __x, const __identity_t<allocator_type>& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001287 : __base(__a)
1288{
Louis Dionneba400782020-10-02 15:02:52 -04001289#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001290 __get_db()->__insert_c(this);
1291#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001292 size_type __n = __x.size();
1293 if (__n > 0)
1294 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001295 __vallocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001296 __construct_at_end(__x.__begin_, __x.__end_, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001297 }
1298}
1299
Eric Fiseliered9e9362017-04-16 02:40:45 +00001300#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001301
1302template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001303inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001304vector<_Tp, _Allocator>::vector(vector&& __x)
Marshall Clowe5108202015-07-14 14:46:32 +00001305#if _LIBCPP_STD_VER > 14
1306 _NOEXCEPT
1307#else
Howard Hinnant1c936782011-06-03 19:40:40 +00001308 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowe5108202015-07-14 14:46:32 +00001309#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001310 : __base(_VSTD::move(__x.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001311{
Louis Dionneba400782020-10-02 15:02:52 -04001312#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001313 __get_db()->__insert_c(this);
Howard Hinnanta1e60cd2011-09-19 16:34:29 +00001314 __get_db()->swap(this, &__x);
Howard Hinnant27e0e772011-09-14 18:33:51 +00001315#endif
Howard Hinnant9cd22302011-09-16 18:41:29 +00001316 this->__begin_ = __x.__begin_;
1317 this->__end_ = __x.__end_;
1318 this->__end_cap() = __x.__end_cap();
Howard Hinnant76053d72013-06-27 19:35:32 +00001319 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001320}
1321
1322template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001323inline _LIBCPP_INLINE_VISIBILITY
Arthur O'Dwyer9b9662f2021-03-01 17:08:24 -05001324vector<_Tp, _Allocator>::vector(vector&& __x, const __identity_t<allocator_type>& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001325 : __base(__a)
1326{
Louis Dionneba400782020-10-02 15:02:52 -04001327#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001328 __get_db()->__insert_c(this);
1329#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001330 if (__a == __x.__alloc())
1331 {
1332 this->__begin_ = __x.__begin_;
1333 this->__end_ = __x.__end_;
1334 this->__end_cap() = __x.__end_cap();
1335 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Louis Dionneba400782020-10-02 15:02:52 -04001336#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnanta1e60cd2011-09-19 16:34:29 +00001337 __get_db()->swap(this, &__x);
1338#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001339 }
1340 else
1341 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001342 typedef move_iterator<iterator> _Ip;
1343 assign(_Ip(__x.begin()), _Ip(__x.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001344 }
1345}
1346
1347template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001348inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001349vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il)
1350{
Louis Dionneba400782020-10-02 15:02:52 -04001351#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001352 __get_db()->__insert_c(this);
1353#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001354 if (__il.size() > 0)
1355 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001356 __vallocate(__il.size());
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001357 __construct_at_end(__il.begin(), __il.end(), __il.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001358 }
1359}
1360
1361template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001362inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001363vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
1364 : __base(__a)
1365{
Louis Dionneba400782020-10-02 15:02:52 -04001366#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001367 __get_db()->__insert_c(this);
1368#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001369 if (__il.size() > 0)
1370 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001371 __vallocate(__il.size());
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001372 __construct_at_end(__il.begin(), __il.end(), __il.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001373 }
1374}
1375
1376template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001377inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001378vector<_Tp, _Allocator>&
1379vector<_Tp, _Allocator>::operator=(vector&& __x)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001380 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001381{
1382 __move_assign(__x, integral_constant<bool,
1383 __alloc_traits::propagate_on_container_move_assignment::value>());
1384 return *this;
1385}
1386
1387template <class _Tp, class _Allocator>
1388void
1389vector<_Tp, _Allocator>::__move_assign(vector& __c, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001390 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001391{
1392 if (__base::__alloc() != __c.__alloc())
1393 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001394 typedef move_iterator<iterator> _Ip;
1395 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001396 }
1397 else
1398 __move_assign(__c, true_type());
1399}
1400
1401template <class _Tp, class _Allocator>
1402void
1403vector<_Tp, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00001404 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001405{
Marshall Clow3ff48e02018-05-22 16:20:28 +00001406 __vdeallocate();
Marshall Clow136d45c2014-07-21 15:11:13 +00001407 __base::__move_assign_alloc(__c); // this can throw
Howard Hinnantc51e1022010-05-11 19:42:16 +00001408 this->__begin_ = __c.__begin_;
1409 this->__end_ = __c.__end_;
1410 this->__end_cap() = __c.__end_cap();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001411 __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
Louis Dionneba400782020-10-02 15:02:52 -04001412#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnanta1e60cd2011-09-19 16:34:29 +00001413 __get_db()->swap(this, &__c);
1414#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001415}
1416
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001417#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001418
1419template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001420inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001421vector<_Tp, _Allocator>&
1422vector<_Tp, _Allocator>::operator=(const vector& __x)
1423{
1424 if (this != &__x)
1425 {
1426 __base::__copy_assign_alloc(__x);
1427 assign(__x.__begin_, __x.__end_);
1428 }
1429 return *this;
1430}
1431
1432template <class _Tp, class _Allocator>
1433template <class _InputIterator>
1434typename enable_if
1435<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001436 __is_cpp17_input_iterator <_InputIterator>::value &&
1437 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001438 is_constructible<
1439 _Tp,
1440 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001441 void
1442>::type
1443vector<_Tp, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
1444{
1445 clear();
1446 for (; __first != __last; ++__first)
Eric Fiselier96919722017-10-17 13:03:17 +00001447 __emplace_back(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001448}
1449
1450template <class _Tp, class _Allocator>
1451template <class _ForwardIterator>
1452typename enable_if
1453<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001454 __is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001455 is_constructible<
1456 _Tp,
1457 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001458 void
1459>::type
1460vector<_Tp, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
1461{
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001462 size_type __new_size = static_cast<size_type>(_VSTD::distance(__first, __last));
1463 if (__new_size <= capacity())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001464 {
1465 _ForwardIterator __mid = __last;
1466 bool __growing = false;
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001467 if (__new_size > size())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001468 {
1469 __growing = true;
1470 __mid = __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001471 _VSTD::advance(__mid, size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001472 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001473 pointer __m = _VSTD::copy(__first, __mid, this->__begin_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001474 if (__growing)
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001475 __construct_at_end(__mid, __last, __new_size - size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001476 else
1477 this->__destruct_at_end(__m);
1478 }
1479 else
1480 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001481 __vdeallocate();
1482 __vallocate(__recommend(__new_size));
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001483 __construct_at_end(__first, __last, __new_size);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001484 }
Eric Fiselier69c51982016-12-28 06:06:09 +00001485 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001486}
1487
1488template <class _Tp, class _Allocator>
1489void
1490vector<_Tp, _Allocator>::assign(size_type __n, const_reference __u)
1491{
1492 if (__n <= capacity())
1493 {
1494 size_type __s = size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001495 _VSTD::fill_n(this->__begin_, _VSTD::min(__n, __s), __u);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001496 if (__n > __s)
1497 __construct_at_end(__n - __s, __u);
1498 else
Howard Hinnant155c2af2010-05-24 17:49:41 +00001499 this->__destruct_at_end(this->__begin_ + __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001500 }
1501 else
1502 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001503 __vdeallocate();
1504 __vallocate(__recommend(static_cast<size_type>(__n)));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001505 __construct_at_end(__n, __u);
1506 }
Eric Fiselier69c51982016-12-28 06:06:09 +00001507 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001508}
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>::iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001513vector<_Tp, _Allocator>::__make_iter(pointer __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001514{
Louis Dionneba400782020-10-02 15:02:52 -04001515#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001516 return iterator(this, __p);
1517#else
1518 return iterator(__p);
1519#endif
1520}
1521
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001522template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001523inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001524typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001525vector<_Tp, _Allocator>::__make_iter(const_pointer __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001526{
Louis Dionneba400782020-10-02 15:02:52 -04001527#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001528 return const_iterator(this, __p);
1529#else
1530 return const_iterator(__p);
1531#endif
1532}
1533
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001534template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001535inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001536typename vector<_Tp, _Allocator>::iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001537vector<_Tp, _Allocator>::begin() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001538{
1539 return __make_iter(this->__begin_);
1540}
1541
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001542template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001543inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001544typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001545vector<_Tp, _Allocator>::begin() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001546{
1547 return __make_iter(this->__begin_);
1548}
1549
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001550template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001551inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001552typename vector<_Tp, _Allocator>::iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001553vector<_Tp, _Allocator>::end() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001554{
1555 return __make_iter(this->__end_);
1556}
1557
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001558template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001559inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001560typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001561vector<_Tp, _Allocator>::end() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001562{
1563 return __make_iter(this->__end_);
1564}
1565
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001566template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001567inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001568typename vector<_Tp, _Allocator>::reference
Marshall Clowd6470492019-03-15 00:29:35 +00001569vector<_Tp, _Allocator>::operator[](size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001570{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001571 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001572 return this->__begin_[__n];
1573}
1574
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001575template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001576inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001577typename vector<_Tp, _Allocator>::const_reference
Marshall Clowd6470492019-03-15 00:29:35 +00001578vector<_Tp, _Allocator>::operator[](size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001579{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001580 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001581 return this->__begin_[__n];
1582}
1583
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001584template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001585typename vector<_Tp, _Allocator>::reference
1586vector<_Tp, _Allocator>::at(size_type __n)
1587{
1588 if (__n >= size())
1589 this->__throw_out_of_range();
1590 return this->__begin_[__n];
1591}
1592
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001593template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001594typename vector<_Tp, _Allocator>::const_reference
1595vector<_Tp, _Allocator>::at(size_type __n) const
1596{
1597 if (__n >= size())
1598 this->__throw_out_of_range();
1599 return this->__begin_[__n];
1600}
1601
1602template <class _Tp, class _Allocator>
1603void
1604vector<_Tp, _Allocator>::reserve(size_type __n)
1605{
1606 if (__n > capacity())
1607 {
1608 allocator_type& __a = this->__alloc();
Howard Hinnant1c936782011-06-03 19:40:40 +00001609 __split_buffer<value_type, allocator_type&> __v(__n, size(), __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001610 __swap_out_circular_buffer(__v);
1611 }
1612}
1613
1614template <class _Tp, class _Allocator>
1615void
Howard Hinnant1c936782011-06-03 19:40:40 +00001616vector<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001617{
1618 if (capacity() > size())
1619 {
1620#ifndef _LIBCPP_NO_EXCEPTIONS
1621 try
1622 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001623#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001624 allocator_type& __a = this->__alloc();
Howard Hinnant1c936782011-06-03 19:40:40 +00001625 __split_buffer<value_type, allocator_type&> __v(size(), size(), __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001626 __swap_out_circular_buffer(__v);
1627#ifndef _LIBCPP_NO_EXCEPTIONS
1628 }
1629 catch (...)
1630 {
1631 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001632#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001633 }
1634}
1635
1636template <class _Tp, class _Allocator>
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001637template <class _Up>
1638void
Eric Fiseliered9e9362017-04-16 02:40:45 +00001639#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001640vector<_Tp, _Allocator>::__push_back_slow_path(_Up&& __x)
1641#else
1642vector<_Tp, _Allocator>::__push_back_slow_path(_Up& __x)
1643#endif
1644{
1645 allocator_type& __a = this->__alloc();
1646 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1647 // __v.push_back(_VSTD::forward<_Up>(__x));
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001648 __alloc_traits::construct(__a, _VSTD::__to_address(__v.__end_), _VSTD::forward<_Up>(__x));
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001649 __v.__end_++;
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001650 __swap_out_circular_buffer(__v);
1651}
1652
1653template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001654inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001655void
1656vector<_Tp, _Allocator>::push_back(const_reference __x)
1657{
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001658 if (this->__end_ != this->__end_cap())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001659 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001660 __construct_one_at_end(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001661 }
1662 else
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001663 __push_back_slow_path(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001664}
1665
Eric Fiseliered9e9362017-04-16 02:40:45 +00001666#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001667
1668template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001669inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001670void
1671vector<_Tp, _Allocator>::push_back(value_type&& __x)
1672{
1673 if (this->__end_ < this->__end_cap())
1674 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001675 __construct_one_at_end(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001676 }
1677 else
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001678 __push_back_slow_path(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001679}
1680
1681template <class _Tp, class _Allocator>
1682template <class... _Args>
1683void
Howard Hinnantb6c49562012-02-26 15:30:12 +00001684vector<_Tp, _Allocator>::__emplace_back_slow_path(_Args&&... __args)
1685{
1686 allocator_type& __a = this->__alloc();
1687 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1688// __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001689 __alloc_traits::construct(__a, _VSTD::__to_address(__v.__end_), _VSTD::forward<_Args>(__args)...);
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001690 __v.__end_++;
Howard Hinnantb6c49562012-02-26 15:30:12 +00001691 __swap_out_circular_buffer(__v);
1692}
1693
1694template <class _Tp, class _Allocator>
1695template <class... _Args>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001696inline
Marshall Clowea52cc42017-01-24 23:09:12 +00001697#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001698typename vector<_Tp, _Allocator>::reference
Marshall Clowea52cc42017-01-24 23:09:12 +00001699#else
1700void
1701#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001702vector<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1703{
1704 if (this->__end_ < this->__end_cap())
1705 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001706 __construct_one_at_end(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001707 }
1708 else
Howard Hinnantb6c49562012-02-26 15:30:12 +00001709 __emplace_back_slow_path(_VSTD::forward<_Args>(__args)...);
Marshall Clowea52cc42017-01-24 23:09:12 +00001710#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001711 return this->back();
Marshall Clowea52cc42017-01-24 23:09:12 +00001712#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001713}
1714
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001715#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001716
1717template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001718inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001719void
1720vector<_Tp, _Allocator>::pop_back()
1721{
Kristina Bessonovaaeeaa7e2021-05-09 19:29:56 +02001722 _LIBCPP_ASSERT(!empty(), "vector::pop_back called on an empty vector");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001723 this->__destruct_at_end(this->__end_ - 1);
1724}
1725
1726template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001727inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001728typename vector<_Tp, _Allocator>::iterator
1729vector<_Tp, _Allocator>::erase(const_iterator __position)
1730{
Louis Dionneba400782020-10-02 15:02:52 -04001731#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001732 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1733 "vector::erase(iterator) called with an iterator not"
1734 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001735#endif
Howard Hinnant58d52d52013-03-25 22:12:26 +00001736 _LIBCPP_ASSERT(__position != end(),
1737 "vector::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnant76053d72013-06-27 19:35:32 +00001738 difference_type __ps = __position - cbegin();
1739 pointer __p = this->__begin_ + __ps;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001740 this->__destruct_at_end(_VSTD::move(__p + 1, this->__end_, __p));
Eric Fiselier69c51982016-12-28 06:06:09 +00001741 this->__invalidate_iterators_past(__p-1);
1742 iterator __r = __make_iter(__p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001743 return __r;
1744}
1745
1746template <class _Tp, class _Allocator>
1747typename vector<_Tp, _Allocator>::iterator
1748vector<_Tp, _Allocator>::erase(const_iterator __first, const_iterator __last)
1749{
Louis Dionneba400782020-10-02 15:02:52 -04001750#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001751 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
1752 "vector::erase(iterator, iterator) called with an iterator not"
1753 " referring to this vector");
Eric Fiselier69c51982016-12-28 06:06:09 +00001754 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__last) == this,
1755 "vector::erase(iterator, iterator) called with an iterator not"
1756 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001757#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001758 _LIBCPP_ASSERT(__first <= __last, "vector::erase(first, last) called with invalid range");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001759 pointer __p = this->__begin_ + (__first - begin());
Eric Fiselier69c51982016-12-28 06:06:09 +00001760 if (__first != __last) {
Howard Hinnantc580cc32013-04-18 15:02:57 +00001761 this->__destruct_at_end(_VSTD::move(__p + (__last - __first), this->__end_, __p));
Eric Fiselier69c51982016-12-28 06:06:09 +00001762 this->__invalidate_iterators_past(__p - 1);
1763 }
1764 iterator __r = __make_iter(__p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001765 return __r;
1766}
1767
1768template <class _Tp, class _Allocator>
1769void
1770vector<_Tp, _Allocator>::__move_range(pointer __from_s, pointer __from_e, pointer __to)
1771{
1772 pointer __old_last = this->__end_;
1773 difference_type __n = __old_last - __to;
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001774 {
1775 pointer __i = __from_s + __n;
1776 _ConstructTransaction __tx(*this, __from_e - __i);
Martijn Vels1e2e1b82020-06-18 13:14:02 -04001777 for (pointer __pos = __tx.__pos_; __i < __from_e;
1778 ++__i, ++__pos, __tx.__pos_ = __pos) {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001779 __alloc_traits::construct(this->__alloc(),
Martijn Vels1e2e1b82020-06-18 13:14:02 -04001780 _VSTD::__to_address(__pos),
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001781 _VSTD::move(*__i));
1782 }
1783 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001784 _VSTD::move_backward(__from_s, __from_s + __n, __old_last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001785}
1786
1787template <class _Tp, class _Allocator>
1788typename vector<_Tp, _Allocator>::iterator
1789vector<_Tp, _Allocator>::insert(const_iterator __position, const_reference __x)
1790{
Louis Dionneba400782020-10-02 15:02:52 -04001791#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001792 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1793 "vector::insert(iterator, x) called with an iterator not"
1794 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001795#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001796 pointer __p = this->__begin_ + (__position - begin());
1797 if (this->__end_ < this->__end_cap())
1798 {
1799 if (__p == this->__end_)
1800 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001801 __construct_one_at_end(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001802 }
1803 else
1804 {
1805 __move_range(__p, this->__end_, __p + 1);
1806 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1807 if (__p <= __xr && __xr < this->__end_)
1808 ++__xr;
1809 *__p = *__xr;
1810 }
1811 }
1812 else
1813 {
1814 allocator_type& __a = this->__alloc();
1815 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
1816 __v.push_back(__x);
1817 __p = __swap_out_circular_buffer(__v, __p);
1818 }
1819 return __make_iter(__p);
1820}
1821
Eric Fiseliered9e9362017-04-16 02:40:45 +00001822#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001823
1824template <class _Tp, class _Allocator>
1825typename vector<_Tp, _Allocator>::iterator
1826vector<_Tp, _Allocator>::insert(const_iterator __position, value_type&& __x)
1827{
Louis Dionneba400782020-10-02 15:02:52 -04001828#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001829 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1830 "vector::insert(iterator, x) called with an iterator not"
1831 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001832#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001833 pointer __p = this->__begin_ + (__position - begin());
1834 if (this->__end_ < this->__end_cap())
1835 {
1836 if (__p == this->__end_)
1837 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001838 __construct_one_at_end(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001839 }
1840 else
1841 {
1842 __move_range(__p, this->__end_, __p + 1);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001843 *__p = _VSTD::move(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001844 }
1845 }
1846 else
1847 {
1848 allocator_type& __a = this->__alloc();
1849 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001850 __v.push_back(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001851 __p = __swap_out_circular_buffer(__v, __p);
1852 }
1853 return __make_iter(__p);
1854}
1855
1856template <class _Tp, class _Allocator>
1857template <class... _Args>
1858typename vector<_Tp, _Allocator>::iterator
1859vector<_Tp, _Allocator>::emplace(const_iterator __position, _Args&&... __args)
1860{
Louis Dionneba400782020-10-02 15:02:52 -04001861#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001862 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1863 "vector::emplace(iterator, x) called with an iterator not"
1864 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001865#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001866 pointer __p = this->__begin_ + (__position - begin());
1867 if (this->__end_ < this->__end_cap())
1868 {
1869 if (__p == this->__end_)
1870 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001871 __construct_one_at_end(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001872 }
1873 else
1874 {
Marshall Clowa591b9a2016-07-11 21:38:08 +00001875 __temp_value<value_type, _Allocator> __tmp(this->__alloc(), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001876 __move_range(__p, this->__end_, __p + 1);
Marshall Clowa591b9a2016-07-11 21:38:08 +00001877 *__p = _VSTD::move(__tmp.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001878 }
1879 }
1880 else
1881 {
1882 allocator_type& __a = this->__alloc();
1883 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001884 __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001885 __p = __swap_out_circular_buffer(__v, __p);
1886 }
1887 return __make_iter(__p);
1888}
1889
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001890#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001891
1892template <class _Tp, class _Allocator>
1893typename vector<_Tp, _Allocator>::iterator
1894vector<_Tp, _Allocator>::insert(const_iterator __position, size_type __n, const_reference __x)
1895{
Louis Dionneba400782020-10-02 15:02:52 -04001896#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001897 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1898 "vector::insert(iterator, n, x) called with an iterator not"
1899 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001900#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001901 pointer __p = this->__begin_ + (__position - begin());
1902 if (__n > 0)
1903 {
1904 if (__n <= static_cast<size_type>(this->__end_cap() - this->__end_))
1905 {
1906 size_type __old_n = __n;
1907 pointer __old_last = this->__end_;
1908 if (__n > static_cast<size_type>(this->__end_ - __p))
1909 {
1910 size_type __cx = __n - (this->__end_ - __p);
1911 __construct_at_end(__cx, __x);
1912 __n -= __cx;
1913 }
1914 if (__n > 0)
1915 {
1916 __move_range(__p, __old_last, __p + __old_n);
1917 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1918 if (__p <= __xr && __xr < this->__end_)
1919 __xr += __old_n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001920 _VSTD::fill_n(__p, __n, *__xr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001921 }
1922 }
1923 else
1924 {
1925 allocator_type& __a = this->__alloc();
1926 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1927 __v.__construct_at_end(__n, __x);
1928 __p = __swap_out_circular_buffer(__v, __p);
1929 }
1930 }
1931 return __make_iter(__p);
1932}
1933
1934template <class _Tp, class _Allocator>
1935template <class _InputIterator>
1936typename enable_if
1937<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001938 __is_cpp17_input_iterator <_InputIterator>::value &&
1939 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001940 is_constructible<
1941 _Tp,
1942 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001943 typename vector<_Tp, _Allocator>::iterator
1944>::type
1945vector<_Tp, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
1946{
Louis Dionneba400782020-10-02 15:02:52 -04001947#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001948 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1949 "vector::insert(iterator, range) called with an iterator not"
1950 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001951#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001952 difference_type __off = __position - begin();
1953 pointer __p = this->__begin_ + __off;
1954 allocator_type& __a = this->__alloc();
1955 pointer __old_last = this->__end_;
1956 for (; this->__end_ != this->__end_cap() && __first != __last; ++__first)
1957 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001958 __construct_one_at_end(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001959 }
1960 __split_buffer<value_type, allocator_type&> __v(__a);
1961 if (__first != __last)
1962 {
1963#ifndef _LIBCPP_NO_EXCEPTIONS
1964 try
1965 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001966#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001967 __v.__construct_at_end(__first, __last);
1968 difference_type __old_size = __old_last - this->__begin_;
1969 difference_type __old_p = __p - this->__begin_;
1970 reserve(__recommend(size() + __v.size()));
1971 __p = this->__begin_ + __old_p;
1972 __old_last = this->__begin_ + __old_size;
1973#ifndef _LIBCPP_NO_EXCEPTIONS
1974 }
1975 catch (...)
1976 {
1977 erase(__make_iter(__old_last), end());
1978 throw;
1979 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001980#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001981 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001982 __p = _VSTD::rotate(__p, __old_last, this->__end_);
Louis Dionne202a4cb2020-02-11 10:59:12 +01001983 insert(__make_iter(__p), _VSTD::make_move_iterator(__v.begin()),
1984 _VSTD::make_move_iterator(__v.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001985 return begin() + __off;
1986}
1987
1988template <class _Tp, class _Allocator>
1989template <class _ForwardIterator>
1990typename enable_if
1991<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001992 __is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001993 is_constructible<
1994 _Tp,
1995 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001996 typename vector<_Tp, _Allocator>::iterator
1997>::type
1998vector<_Tp, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
1999{
Louis Dionneba400782020-10-02 15:02:52 -04002000#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002001 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
2002 "vector::insert(iterator, range) called with an iterator not"
2003 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002004#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002005 pointer __p = this->__begin_ + (__position - begin());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002006 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002007 if (__n > 0)
2008 {
2009 if (__n <= this->__end_cap() - this->__end_)
2010 {
2011 size_type __old_n = __n;
2012 pointer __old_last = this->__end_;
2013 _ForwardIterator __m = __last;
2014 difference_type __dx = this->__end_ - __p;
2015 if (__n > __dx)
2016 {
2017 __m = __first;
Eric Fiselieracfe6f02015-03-31 16:54:19 +00002018 difference_type __diff = this->__end_ - __p;
2019 _VSTD::advance(__m, __diff);
2020 __construct_at_end(__m, __last, __n - __diff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002021 __n = __dx;
2022 }
2023 if (__n > 0)
2024 {
2025 __move_range(__p, __old_last, __p + __old_n);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002026 _VSTD::copy(__first, __m, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002027 }
2028 }
2029 else
2030 {
2031 allocator_type& __a = this->__alloc();
2032 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
2033 __v.__construct_at_end(__first, __last);
2034 __p = __swap_out_circular_buffer(__v, __p);
2035 }
2036 }
2037 return __make_iter(__p);
2038}
2039
2040template <class _Tp, class _Allocator>
2041void
2042vector<_Tp, _Allocator>::resize(size_type __sz)
2043{
2044 size_type __cs = size();
2045 if (__cs < __sz)
2046 this->__append(__sz - __cs);
2047 else if (__cs > __sz)
2048 this->__destruct_at_end(this->__begin_ + __sz);
2049}
2050
2051template <class _Tp, class _Allocator>
2052void
2053vector<_Tp, _Allocator>::resize(size_type __sz, const_reference __x)
2054{
2055 size_type __cs = size();
2056 if (__cs < __sz)
2057 this->__append(__sz - __cs, __x);
2058 else if (__cs > __sz)
2059 this->__destruct_at_end(this->__begin_ + __sz);
2060}
2061
2062template <class _Tp, class _Allocator>
2063void
2064vector<_Tp, _Allocator>::swap(vector& __x)
Marshall Clow8982dcd2015-07-13 20:04:56 +00002065#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00002066 _NOEXCEPT
Marshall Clow8982dcd2015-07-13 20:04:56 +00002067#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00002068 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00002069 __is_nothrow_swappable<allocator_type>::value)
2070#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002071{
Howard Hinnant27e0e772011-09-14 18:33:51 +00002072 _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
2073 this->__alloc() == __x.__alloc(),
2074 "vector::swap: Either propagate_on_container_swap must be true"
2075 " or the allocators must compare equal");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002076 _VSTD::swap(this->__begin_, __x.__begin_);
2077 _VSTD::swap(this->__end_, __x.__end_);
2078 _VSTD::swap(this->__end_cap(), __x.__end_cap());
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05002079 _VSTD::__swap_allocator(this->__alloc(), __x.__alloc(),
Marshall Clow8982dcd2015-07-13 20:04:56 +00002080 integral_constant<bool,__alloc_traits::propagate_on_container_swap::value>());
Louis Dionneba400782020-10-02 15:02:52 -04002081#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002082 __get_db()->swap(this, &__x);
Louis Dionneba400782020-10-02 15:02:52 -04002083#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002084}
2085
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002086template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002087bool
2088vector<_Tp, _Allocator>::__invariants() const
2089{
Howard Hinnant76053d72013-06-27 19:35:32 +00002090 if (this->__begin_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002091 {
Howard Hinnant76053d72013-06-27 19:35:32 +00002092 if (this->__end_ != nullptr || this->__end_cap() != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002093 return false;
2094 }
2095 else
2096 {
2097 if (this->__begin_ > this->__end_)
2098 return false;
2099 if (this->__begin_ == this->__end_cap())
2100 return false;
2101 if (this->__end_ > this->__end_cap())
2102 return false;
2103 }
2104 return true;
2105}
2106
Louis Dionneba400782020-10-02 15:02:52 -04002107#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002108
Howard Hinnantc51e1022010-05-11 19:42:16 +00002109template <class _Tp, class _Allocator>
Howard Hinnant27e0e772011-09-14 18:33:51 +00002110bool
2111vector<_Tp, _Allocator>::__dereferenceable(const const_iterator* __i) const
2112{
2113 return this->__begin_ <= __i->base() && __i->base() < this->__end_;
2114}
2115
2116template <class _Tp, class _Allocator>
2117bool
2118vector<_Tp, _Allocator>::__decrementable(const const_iterator* __i) const
2119{
2120 return this->__begin_ < __i->base() && __i->base() <= this->__end_;
2121}
2122
2123template <class _Tp, class _Allocator>
2124bool
2125vector<_Tp, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
2126{
2127 const_pointer __p = __i->base() + __n;
2128 return this->__begin_ <= __p && __p <= this->__end_;
2129}
2130
2131template <class _Tp, class _Allocator>
2132bool
2133vector<_Tp, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
2134{
2135 const_pointer __p = __i->base() + __n;
2136 return this->__begin_ <= __p && __p < this->__end_;
2137}
2138
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002139#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002140
2141template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002142inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002143void
2144vector<_Tp, _Allocator>::__invalidate_all_iterators()
2145{
Louis Dionneba400782020-10-02 15:02:52 -04002146#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002147 __get_db()->__invalidate_all(this);
Louis Dionneba400782020-10-02 15:02:52 -04002148#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002149}
2150
Eric Fiselier69c51982016-12-28 06:06:09 +00002151
2152template <class _Tp, class _Allocator>
2153inline _LIBCPP_INLINE_VISIBILITY
2154void
2155vector<_Tp, _Allocator>::__invalidate_iterators_past(pointer __new_last) {
Louis Dionneba400782020-10-02 15:02:52 -04002156#if _LIBCPP_DEBUG_LEVEL == 2
Eric Fiselier69c51982016-12-28 06:06:09 +00002157 __c_node* __c = __get_db()->__find_c_and_lock(this);
2158 for (__i_node** __p = __c->end_; __p != __c->beg_; ) {
2159 --__p;
2160 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
2161 if (__i->base() > __new_last) {
2162 (*__p)->__c_ = nullptr;
2163 if (--__c->end_ != __p)
Arthur O'Dwyer22236632020-12-07 21:50:15 -05002164 _VSTD::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
Eric Fiselier69c51982016-12-28 06:06:09 +00002165 }
2166 }
2167 __get_db()->unlock();
2168#else
2169 ((void)__new_last);
2170#endif
2171}
2172
Howard Hinnantc51e1022010-05-11 19:42:16 +00002173// vector<bool>
2174
2175template <class _Allocator> class vector<bool, _Allocator>;
2176
2177template <class _Allocator> struct hash<vector<bool, _Allocator> >;
2178
2179template <class _Allocator>
Howard Hinnantb4ef6482011-07-02 20:33:23 +00002180struct __has_storage_type<vector<bool, _Allocator> >
2181{
2182 static const bool value = true;
2183};
2184
2185template <class _Allocator>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002186class _LIBCPP_TEMPLATE_VIS vector<bool, _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002187 : private __vector_base_common<true>
2188{
2189public:
2190 typedef vector __self;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002191 typedef bool value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002192 typedef _Allocator allocator_type;
2193 typedef allocator_traits<allocator_type> __alloc_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002194 typedef typename __alloc_traits::size_type size_type;
2195 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant896b7622012-05-07 16:50:38 +00002196 typedef size_type __storage_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002197 typedef __bit_iterator<vector, false> pointer;
2198 typedef __bit_iterator<vector, true> const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002199 typedef pointer iterator;
2200 typedef const_pointer const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002201 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
2202 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002203
2204private:
Marshall Clow940e01c2015-04-07 05:21:38 +00002205 typedef typename __rebind_alloc_helper<__alloc_traits, __storage_type>::type __storage_allocator;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002206 typedef allocator_traits<__storage_allocator> __storage_traits;
2207 typedef typename __storage_traits::pointer __storage_pointer;
2208 typedef typename __storage_traits::const_pointer __const_storage_pointer;
2209
2210 __storage_pointer __begin_;
2211 size_type __size_;
2212 __compressed_pair<size_type, __storage_allocator> __cap_alloc_;
Howard Hinnant71d1c192011-07-09 15:50:42 +00002213public:
Howard Hinnantb4ef6482011-07-02 20:33:23 +00002214 typedef __bit_reference<vector> reference;
2215 typedef __bit_const_reference<vector> const_reference;
Howard Hinnant71d1c192011-07-09 15:50:42 +00002216private:
Howard Hinnant1c936782011-06-03 19:40:40 +00002217 _LIBCPP_INLINE_VISIBILITY
2218 size_type& __cap() _NOEXCEPT
2219 {return __cap_alloc_.first();}
2220 _LIBCPP_INLINE_VISIBILITY
2221 const size_type& __cap() const _NOEXCEPT
2222 {return __cap_alloc_.first();}
2223 _LIBCPP_INLINE_VISIBILITY
2224 __storage_allocator& __alloc() _NOEXCEPT
2225 {return __cap_alloc_.second();}
2226 _LIBCPP_INLINE_VISIBILITY
2227 const __storage_allocator& __alloc() const _NOEXCEPT
2228 {return __cap_alloc_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002229
2230 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
2231
Howard Hinnant1c936782011-06-03 19:40:40 +00002232 _LIBCPP_INLINE_VISIBILITY
2233 static size_type __internal_cap_to_external(size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002234 {return __n * __bits_per_word;}
Howard Hinnant1c936782011-06-03 19:40:40 +00002235 _LIBCPP_INLINE_VISIBILITY
2236 static size_type __external_cap_to_internal(size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002237 {return (__n - 1) / __bits_per_word + 1;}
2238
2239public:
Howard Hinnant1c936782011-06-03 19:40:40 +00002240 _LIBCPP_INLINE_VISIBILITY
Marshall Clowe546cbb2015-06-04 02:05:41 +00002241 vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clow8f282f42015-06-04 00:10:20 +00002242
2243 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
2244#if _LIBCPP_STD_VER <= 14
2245 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
2246#else
2247 _NOEXCEPT;
2248#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002249 ~vector();
2250 explicit vector(size_type __n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00002251#if _LIBCPP_STD_VER > 11
2252 explicit vector(size_type __n, const allocator_type& __a);
2253#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002254 vector(size_type __n, const value_type& __v);
2255 vector(size_type __n, const value_type& __v, const allocator_type& __a);
2256 template <class _InputIterator>
2257 vector(_InputIterator __first, _InputIterator __last,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002258 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
2259 !__is_cpp17_forward_iterator<_InputIterator>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002260 template <class _InputIterator>
2261 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002262 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
2263 !__is_cpp17_forward_iterator<_InputIterator>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002264 template <class _ForwardIterator>
2265 vector(_ForwardIterator __first, _ForwardIterator __last,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002266 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002267 template <class _ForwardIterator>
2268 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002269 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002270
2271 vector(const vector& __v);
2272 vector(const vector& __v, const allocator_type& __a);
2273 vector& operator=(const vector& __v);
Eric Fiseliered9e9362017-04-16 02:40:45 +00002274
2275#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002276 vector(initializer_list<value_type> __il);
2277 vector(initializer_list<value_type> __il, const allocator_type& __a);
2278
Howard Hinnant1c936782011-06-03 19:40:40 +00002279 _LIBCPP_INLINE_VISIBILITY
2280 vector(vector&& __v)
Marshall Clowe5108202015-07-14 14:46:32 +00002281#if _LIBCPP_STD_VER > 14
2282 _NOEXCEPT;
2283#else
Howard Hinnant1c936782011-06-03 19:40:40 +00002284 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowe5108202015-07-14 14:46:32 +00002285#endif
Arthur O'Dwyer9b9662f2021-03-01 17:08:24 -05002286 vector(vector&& __v, const __identity_t<allocator_type>& __a);
Howard Hinnant1c936782011-06-03 19:40:40 +00002287 _LIBCPP_INLINE_VISIBILITY
2288 vector& operator=(vector&& __v)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002289 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiseliered9e9362017-04-16 02:40:45 +00002290
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002291 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002292 vector& operator=(initializer_list<value_type> __il)
2293 {assign(__il.begin(), __il.end()); return *this;}
Eric Fiseliered9e9362017-04-16 02:40:45 +00002294
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002295#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002296
2297 template <class _InputIterator>
2298 typename enable_if
2299 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002300 __is_cpp17_input_iterator<_InputIterator>::value &&
2301 !__is_cpp17_forward_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002302 void
2303 >::type
2304 assign(_InputIterator __first, _InputIterator __last);
2305 template <class _ForwardIterator>
2306 typename enable_if
2307 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002308 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002309 void
2310 >::type
2311 assign(_ForwardIterator __first, _ForwardIterator __last);
2312
2313 void assign(size_type __n, const value_type& __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +00002314
2315#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002316 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002317 void assign(initializer_list<value_type> __il)
2318 {assign(__il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +00002319#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002320
Howard Hinnant1c936782011-06-03 19:40:40 +00002321 _LIBCPP_INLINE_VISIBILITY allocator_type get_allocator() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002322 {return allocator_type(this->__alloc());}
2323
Howard Hinnant1c936782011-06-03 19:40:40 +00002324 size_type max_size() const _NOEXCEPT;
2325 _LIBCPP_INLINE_VISIBILITY
2326 size_type capacity() const _NOEXCEPT
2327 {return __internal_cap_to_external(__cap());}
2328 _LIBCPP_INLINE_VISIBILITY
2329 size_type size() const _NOEXCEPT
2330 {return __size_;}
Marshall Clow425f5752017-11-15 05:51:26 +00002331 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +00002332 bool empty() const _NOEXCEPT
2333 {return __size_ == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002334 void reserve(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +00002335 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002336
Howard Hinnant1c936782011-06-03 19:40:40 +00002337 _LIBCPP_INLINE_VISIBILITY
2338 iterator begin() _NOEXCEPT
2339 {return __make_iter(0);}
2340 _LIBCPP_INLINE_VISIBILITY
2341 const_iterator begin() const _NOEXCEPT
2342 {return __make_iter(0);}
2343 _LIBCPP_INLINE_VISIBILITY
2344 iterator end() _NOEXCEPT
2345 {return __make_iter(__size_);}
2346 _LIBCPP_INLINE_VISIBILITY
2347 const_iterator end() const _NOEXCEPT
2348 {return __make_iter(__size_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002349
Howard Hinnant1c936782011-06-03 19:40:40 +00002350 _LIBCPP_INLINE_VISIBILITY
2351 reverse_iterator rbegin() _NOEXCEPT
2352 {return reverse_iterator(end());}
2353 _LIBCPP_INLINE_VISIBILITY
2354 const_reverse_iterator rbegin() const _NOEXCEPT
2355 {return const_reverse_iterator(end());}
2356 _LIBCPP_INLINE_VISIBILITY
2357 reverse_iterator rend() _NOEXCEPT
2358 {return reverse_iterator(begin());}
2359 _LIBCPP_INLINE_VISIBILITY
2360 const_reverse_iterator rend() const _NOEXCEPT
2361 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002362
Howard Hinnant1c936782011-06-03 19:40:40 +00002363 _LIBCPP_INLINE_VISIBILITY
2364 const_iterator cbegin() const _NOEXCEPT
2365 {return __make_iter(0);}
2366 _LIBCPP_INLINE_VISIBILITY
2367 const_iterator cend() const _NOEXCEPT
2368 {return __make_iter(__size_);}
2369 _LIBCPP_INLINE_VISIBILITY
2370 const_reverse_iterator crbegin() const _NOEXCEPT
2371 {return rbegin();}
2372 _LIBCPP_INLINE_VISIBILITY
2373 const_reverse_iterator crend() const _NOEXCEPT
2374 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002375
2376 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) {return __make_ref(__n);}
2377 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const {return __make_ref(__n);}
2378 reference at(size_type __n);
2379 const_reference at(size_type __n) const;
2380
2381 _LIBCPP_INLINE_VISIBILITY reference front() {return __make_ref(0);}
2382 _LIBCPP_INLINE_VISIBILITY const_reference front() const {return __make_ref(0);}
2383 _LIBCPP_INLINE_VISIBILITY reference back() {return __make_ref(__size_ - 1);}
2384 _LIBCPP_INLINE_VISIBILITY const_reference back() const {return __make_ref(__size_ - 1);}
2385
2386 void push_back(const value_type& __x);
Marshall Clowc46bb8e2013-08-13 23:54:12 +00002387#if _LIBCPP_STD_VER > 11
2388 template <class... _Args>
Marshall Clowea52cc42017-01-24 23:09:12 +00002389#if _LIBCPP_STD_VER > 14
2390 _LIBCPP_INLINE_VISIBILITY reference emplace_back(_Args&&... __args)
2391#else
2392 _LIBCPP_INLINE_VISIBILITY void emplace_back(_Args&&... __args)
2393#endif
2394 {
Eric Fiselier34ba5b92016-07-21 03:20:17 +00002395 push_back ( value_type ( _VSTD::forward<_Args>(__args)... ));
Marshall Clowea52cc42017-01-24 23:09:12 +00002396#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00002397 return this->back();
Marshall Clowea52cc42017-01-24 23:09:12 +00002398#endif
Eric Fiselier34ba5b92016-07-21 03:20:17 +00002399 }
Marshall Clowc46bb8e2013-08-13 23:54:12 +00002400#endif
2401
Howard Hinnantc51e1022010-05-11 19:42:16 +00002402 _LIBCPP_INLINE_VISIBILITY void pop_back() {--__size_;}
2403
Marshall Clowc46bb8e2013-08-13 23:54:12 +00002404#if _LIBCPP_STD_VER > 11
2405 template <class... _Args>
2406 _LIBCPP_INLINE_VISIBILITY iterator emplace(const_iterator position, _Args&&... __args)
2407 { return insert ( position, value_type ( _VSTD::forward<_Args>(__args)... )); }
2408#endif
2409
Howard Hinnantc51e1022010-05-11 19:42:16 +00002410 iterator insert(const_iterator __position, const value_type& __x);
2411 iterator insert(const_iterator __position, size_type __n, const value_type& __x);
2412 iterator insert(const_iterator __position, size_type __n, const_reference __x);
2413 template <class _InputIterator>
2414 typename enable_if
2415 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002416 __is_cpp17_input_iterator <_InputIterator>::value &&
2417 !__is_cpp17_forward_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002418 iterator
2419 >::type
2420 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
2421 template <class _ForwardIterator>
2422 typename enable_if
2423 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002424 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002425 iterator
2426 >::type
2427 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiseliered9e9362017-04-16 02:40:45 +00002428
2429#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002430 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002431 iterator insert(const_iterator __position, initializer_list<value_type> __il)
2432 {return insert(__position, __il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +00002433#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002434
Howard Hinnantcf823322010-12-17 14:46:43 +00002435 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002436 iterator erase(const_iterator __first, const_iterator __last);
2437
Howard Hinnant1c936782011-06-03 19:40:40 +00002438 _LIBCPP_INLINE_VISIBILITY
2439 void clear() _NOEXCEPT {__size_ = 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002440
Howard Hinnant1c936782011-06-03 19:40:40 +00002441 void swap(vector&)
Marshall Clow8982dcd2015-07-13 20:04:56 +00002442#if _LIBCPP_STD_VER >= 14
2443 _NOEXCEPT;
2444#else
Eric Fiselier69c51982016-12-28 06:06:09 +00002445 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00002446 __is_nothrow_swappable<allocator_type>::value);
2447#endif
Marshall Clow89eb3822016-04-07 14:20:31 +00002448 static void swap(reference __x, reference __y) _NOEXCEPT { _VSTD::swap(__x, __y); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002449
2450 void resize(size_type __sz, value_type __x = false);
Howard Hinnant1c936782011-06-03 19:40:40 +00002451 void flip() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002452
2453 bool __invariants() const;
2454
2455private:
Howard Hinnantcf823322010-12-17 14:46:43 +00002456 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Marshall Clow3ff48e02018-05-22 16:20:28 +00002457 void __vallocate(size_type __n);
2458 void __vdeallocate() _NOEXCEPT;
Howard Hinnant1c936782011-06-03 19:40:40 +00002459 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantea382952013-08-14 18:00:20 +00002460 static size_type __align_it(size_type __new_size) _NOEXCEPT
Eric Fiselierb41db9a2018-10-01 01:59:37 +00002461 {return __new_size + (__bits_per_word-1) & ~((size_type)__bits_per_word-1);}
Howard Hinnantcf823322010-12-17 14:46:43 +00002462 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
2463 _LIBCPP_INLINE_VISIBILITY void __construct_at_end(size_type __n, bool __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002464 template <class _ForwardIterator>
2465 typename enable_if
2466 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002467 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002468 void
2469 >::type
2470 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
2471 void __append(size_type __n, const_reference __x);
Howard Hinnant1c936782011-06-03 19:40:40 +00002472 _LIBCPP_INLINE_VISIBILITY
2473 reference __make_ref(size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002474 {return reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnant1c936782011-06-03 19:40:40 +00002475 _LIBCPP_INLINE_VISIBILITY
2476 const_reference __make_ref(size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002477 {return const_reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnant1c936782011-06-03 19:40:40 +00002478 _LIBCPP_INLINE_VISIBILITY
2479 iterator __make_iter(size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002480 {return iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnant1c936782011-06-03 19:40:40 +00002481 _LIBCPP_INLINE_VISIBILITY
2482 const_iterator __make_iter(size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002483 {return const_iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnant1c936782011-06-03 19:40:40 +00002484 _LIBCPP_INLINE_VISIBILITY
2485 iterator __const_iterator_cast(const_iterator __p) _NOEXCEPT
Howard Hinnant76053d72013-06-27 19:35:32 +00002486 {return begin() + (__p - cbegin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002487
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002488 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002489 void __copy_assign_alloc(const vector& __v)
2490 {__copy_assign_alloc(__v, integral_constant<bool,
2491 __storage_traits::propagate_on_container_copy_assignment::value>());}
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002492 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002493 void __copy_assign_alloc(const vector& __c, true_type)
2494 {
2495 if (__alloc() != __c.__alloc())
Marshall Clow3ff48e02018-05-22 16:20:28 +00002496 __vdeallocate();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002497 __alloc() = __c.__alloc();
2498 }
2499
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002500 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00002501 void __copy_assign_alloc(const vector&, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002502 {}
2503
2504 void __move_assign(vector& __c, false_type);
Howard Hinnant1c936782011-06-03 19:40:40 +00002505 void __move_assign(vector& __c, true_type)
2506 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002507 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002508 void __move_assign_alloc(vector& __c)
Howard Hinnant1c936782011-06-03 19:40:40 +00002509 _NOEXCEPT_(
2510 !__storage_traits::propagate_on_container_move_assignment::value ||
2511 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002512 {__move_assign_alloc(__c, integral_constant<bool,
2513 __storage_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002514 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +00002515 void __move_assign_alloc(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002516 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002517 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002518 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002519 }
2520
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002521 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00002522 void __move_assign_alloc(vector&, false_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002523 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002524 {}
2525
Howard Hinnant1c936782011-06-03 19:40:40 +00002526 size_t __hash_code() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002527
2528 friend class __bit_reference<vector>;
2529 friend class __bit_const_reference<vector>;
2530 friend class __bit_iterator<vector, false>;
2531 friend class __bit_iterator<vector, true>;
Howard Hinnant4210a0f2012-08-17 17:10:18 +00002532 friend struct __bit_array<vector>;
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002533 friend struct _LIBCPP_TEMPLATE_VIS hash<vector>;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002534};
2535
2536template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002537inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002538void
2539vector<bool, _Allocator>::__invalidate_all_iterators()
2540{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002541}
2542
2543// Allocate space for __n objects
2544// throws length_error if __n > max_size()
2545// throws (probably bad_alloc) if memory run out
2546// Precondition: __begin_ == __end_ == __cap() == 0
2547// Precondition: __n > 0
2548// Postcondition: capacity() == __n
2549// Postcondition: size() == 0
2550template <class _Allocator>
2551void
Marshall Clow3ff48e02018-05-22 16:20:28 +00002552vector<bool, _Allocator>::__vallocate(size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002553{
2554 if (__n > max_size())
2555 this->__throw_length_error();
2556 __n = __external_cap_to_internal(__n);
2557 this->__begin_ = __storage_traits::allocate(this->__alloc(), __n);
2558 this->__size_ = 0;
2559 this->__cap() = __n;
2560}
2561
2562template <class _Allocator>
2563void
Marshall Clow3ff48e02018-05-22 16:20:28 +00002564vector<bool, _Allocator>::__vdeallocate() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002565{
Howard Hinnant76053d72013-06-27 19:35:32 +00002566 if (this->__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002567 {
2568 __storage_traits::deallocate(this->__alloc(), this->__begin_, __cap());
2569 __invalidate_all_iterators();
Howard Hinnant76053d72013-06-27 19:35:32 +00002570 this->__begin_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002571 this->__size_ = this->__cap() = 0;
2572 }
2573}
2574
2575template <class _Allocator>
2576typename vector<bool, _Allocator>::size_type
Howard Hinnant1c936782011-06-03 19:40:40 +00002577vector<bool, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002578{
2579 size_type __amax = __storage_traits::max_size(__alloc());
2580 size_type __nmax = numeric_limits<size_type>::max() / 2; // end() >= begin(), always
2581 if (__nmax / __bits_per_word <= __amax)
2582 return __nmax;
2583 return __internal_cap_to_external(__amax);
2584}
2585
2586// Precondition: __new_size > capacity()
2587template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002588inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002589typename vector<bool, _Allocator>::size_type
2590vector<bool, _Allocator>::__recommend(size_type __new_size) const
2591{
2592 const size_type __ms = max_size();
2593 if (__new_size > __ms)
2594 this->__throw_length_error();
2595 const size_type __cap = capacity();
2596 if (__cap >= __ms / 2)
2597 return __ms;
Arthur O'Dwyerc0fb14b2021-07-26 18:23:00 -04002598 return _VSTD::max(2 * __cap, __align_it(__new_size));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002599}
2600
2601// Default constructs __n objects starting at __end_
2602// Precondition: __n > 0
2603// Precondition: size() + __n <= capacity()
2604// Postcondition: size() == size() + __n
2605template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002606inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002607void
2608vector<bool, _Allocator>::__construct_at_end(size_type __n, bool __x)
2609{
2610 size_type __old_size = this->__size_;
2611 this->__size_ += __n;
Marshall Clow1893ec72018-10-23 20:07:45 +00002612 if (__old_size == 0 || ((__old_size - 1) / __bits_per_word) != ((this->__size_ - 1) / __bits_per_word))
2613 {
2614 if (this->__size_ <= __bits_per_word)
2615 this->__begin_[0] = __storage_type(0);
2616 else
2617 this->__begin_[(this->__size_ - 1) / __bits_per_word] = __storage_type(0);
2618 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002619 _VSTD::fill_n(__make_iter(__old_size), __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002620}
2621
2622template <class _Allocator>
2623template <class _ForwardIterator>
2624typename enable_if
2625<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002626 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002627 void
2628>::type
2629vector<bool, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
2630{
2631 size_type __old_size = this->__size_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002632 this->__size_ += _VSTD::distance(__first, __last);
Marshall Clow1893ec72018-10-23 20:07:45 +00002633 if (__old_size == 0 || ((__old_size - 1) / __bits_per_word) != ((this->__size_ - 1) / __bits_per_word))
2634 {
2635 if (this->__size_ <= __bits_per_word)
2636 this->__begin_[0] = __storage_type(0);
2637 else
2638 this->__begin_[(this->__size_ - 1) / __bits_per_word] = __storage_type(0);
2639 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002640 _VSTD::copy(__first, __last, __make_iter(__old_size));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002641}
2642
2643template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002644inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002645vector<bool, _Allocator>::vector()
Marshall Clowe546cbb2015-06-04 02:05:41 +00002646 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant76053d72013-06-27 19:35:32 +00002647 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002648 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002649 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002650{
2651}
2652
2653template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002654inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002655vector<bool, _Allocator>::vector(const allocator_type& __a)
Marshall Clow8f282f42015-06-04 00:10:20 +00002656#if _LIBCPP_STD_VER <= 14
2657 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
2658#else
2659 _NOEXCEPT
2660#endif
Howard Hinnant76053d72013-06-27 19:35:32 +00002661 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002662 __size_(0),
2663 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2664{
2665}
2666
2667template <class _Allocator>
2668vector<bool, _Allocator>::vector(size_type __n)
Howard Hinnant76053d72013-06-27 19:35:32 +00002669 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002670 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002671 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002672{
2673 if (__n > 0)
2674 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002675 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002676 __construct_at_end(__n, false);
2677 }
2678}
2679
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00002680#if _LIBCPP_STD_VER > 11
2681template <class _Allocator>
2682vector<bool, _Allocator>::vector(size_type __n, const allocator_type& __a)
2683 : __begin_(nullptr),
2684 __size_(0),
2685 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2686{
2687 if (__n > 0)
2688 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002689 __vallocate(__n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00002690 __construct_at_end(__n, false);
2691 }
2692}
2693#endif
2694
Howard Hinnantc51e1022010-05-11 19:42:16 +00002695template <class _Allocator>
2696vector<bool, _Allocator>::vector(size_type __n, const value_type& __x)
Howard Hinnant76053d72013-06-27 19:35:32 +00002697 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002698 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002699 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002700{
2701 if (__n > 0)
2702 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002703 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002704 __construct_at_end(__n, __x);
2705 }
2706}
2707
2708template <class _Allocator>
2709vector<bool, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002710 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002711 __size_(0),
2712 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2713{
2714 if (__n > 0)
2715 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002716 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002717 __construct_at_end(__n, __x);
2718 }
2719}
2720
2721template <class _Allocator>
2722template <class _InputIterator>
2723vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002724 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
2725 !__is_cpp17_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002726 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002727 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002728 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002729{
2730#ifndef _LIBCPP_NO_EXCEPTIONS
2731 try
2732 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002733#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002734 for (; __first != __last; ++__first)
2735 push_back(*__first);
2736#ifndef _LIBCPP_NO_EXCEPTIONS
2737 }
2738 catch (...)
2739 {
Howard Hinnant76053d72013-06-27 19:35:32 +00002740 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002741 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2742 __invalidate_all_iterators();
2743 throw;
2744 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002745#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002746}
2747
2748template <class _Allocator>
2749template <class _InputIterator>
2750vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002751 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
2752 !__is_cpp17_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002753 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002754 __size_(0),
2755 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2756{
2757#ifndef _LIBCPP_NO_EXCEPTIONS
2758 try
2759 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002760#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002761 for (; __first != __last; ++__first)
2762 push_back(*__first);
2763#ifndef _LIBCPP_NO_EXCEPTIONS
2764 }
2765 catch (...)
2766 {
Howard Hinnant76053d72013-06-27 19:35:32 +00002767 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002768 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2769 __invalidate_all_iterators();
2770 throw;
2771 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002772#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002773}
2774
2775template <class _Allocator>
2776template <class _ForwardIterator>
2777vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002778 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002779 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002780 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002781 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002782{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002783 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002784 if (__n > 0)
2785 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002786 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002787 __construct_at_end(__first, __last);
2788 }
2789}
2790
2791template <class _Allocator>
2792template <class _ForwardIterator>
2793vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002794 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002795 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002796 __size_(0),
2797 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2798{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002799 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002800 if (__n > 0)
2801 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002802 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002803 __construct_at_end(__first, __last);
2804 }
2805}
2806
Eric Fiseliered9e9362017-04-16 02:40:45 +00002807#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002808
Howard Hinnantc51e1022010-05-11 19:42:16 +00002809template <class _Allocator>
2810vector<bool, _Allocator>::vector(initializer_list<value_type> __il)
Howard Hinnant76053d72013-06-27 19:35:32 +00002811 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002812 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002813 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002814{
2815 size_type __n = static_cast<size_type>(__il.size());
2816 if (__n > 0)
2817 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002818 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002819 __construct_at_end(__il.begin(), __il.end());
2820 }
2821}
2822
2823template <class _Allocator>
2824vector<bool, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002825 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002826 __size_(0),
2827 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2828{
2829 size_type __n = static_cast<size_type>(__il.size());
2830 if (__n > 0)
2831 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002832 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002833 __construct_at_end(__il.begin(), __il.end());
2834 }
2835}
2836
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002837#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002838
Howard Hinnantc51e1022010-05-11 19:42:16 +00002839template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002840vector<bool, _Allocator>::~vector()
2841{
Howard Hinnant76053d72013-06-27 19:35:32 +00002842 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002843 __storage_traits::deallocate(__alloc(), __begin_, __cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002844 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002845}
2846
2847template <class _Allocator>
2848vector<bool, _Allocator>::vector(const vector& __v)
Howard Hinnant76053d72013-06-27 19:35:32 +00002849 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002850 __size_(0),
2851 __cap_alloc_(0, __storage_traits::select_on_container_copy_construction(__v.__alloc()))
2852{
2853 if (__v.size() > 0)
2854 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002855 __vallocate(__v.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002856 __construct_at_end(__v.begin(), __v.end());
2857 }
2858}
2859
2860template <class _Allocator>
2861vector<bool, _Allocator>::vector(const vector& __v, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002862 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002863 __size_(0),
2864 __cap_alloc_(0, __a)
2865{
2866 if (__v.size() > 0)
2867 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002868 __vallocate(__v.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002869 __construct_at_end(__v.begin(), __v.end());
2870 }
2871}
2872
2873template <class _Allocator>
2874vector<bool, _Allocator>&
2875vector<bool, _Allocator>::operator=(const vector& __v)
2876{
2877 if (this != &__v)
2878 {
2879 __copy_assign_alloc(__v);
2880 if (__v.__size_)
2881 {
2882 if (__v.__size_ > capacity())
2883 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002884 __vdeallocate();
2885 __vallocate(__v.__size_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002886 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002887 _VSTD::copy(__v.__begin_, __v.__begin_ + __external_cap_to_internal(__v.__size_), __begin_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002888 }
2889 __size_ = __v.__size_;
2890 }
2891 return *this;
2892}
2893
Eric Fiseliered9e9362017-04-16 02:40:45 +00002894#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant74279a52010-09-04 23:28:19 +00002895
Howard Hinnantc51e1022010-05-11 19:42:16 +00002896template <class _Allocator>
Eric Fiselier4f1534c2018-06-05 22:32:52 +00002897inline _LIBCPP_INLINE_VISIBILITY vector<bool, _Allocator>::vector(vector&& __v)
Marshall Clowe5108202015-07-14 14:46:32 +00002898#if _LIBCPP_STD_VER > 14
Eric Fiselier4f1534c2018-06-05 22:32:52 +00002899 _NOEXCEPT
Marshall Clowe5108202015-07-14 14:46:32 +00002900#else
Eric Fiselier4f1534c2018-06-05 22:32:52 +00002901 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowe5108202015-07-14 14:46:32 +00002902#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002903 : __begin_(__v.__begin_),
2904 __size_(__v.__size_),
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05002905 __cap_alloc_(_VSTD::move(__v.__cap_alloc_)) {
Howard Hinnant76053d72013-06-27 19:35:32 +00002906 __v.__begin_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002907 __v.__size_ = 0;
2908 __v.__cap() = 0;
2909}
2910
2911template <class _Allocator>
Arthur O'Dwyer9b9662f2021-03-01 17:08:24 -05002912vector<bool, _Allocator>::vector(vector&& __v, const __identity_t<allocator_type>& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002913 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002914 __size_(0),
2915 __cap_alloc_(0, __a)
2916{
2917 if (__a == allocator_type(__v.__alloc()))
2918 {
2919 this->__begin_ = __v.__begin_;
2920 this->__size_ = __v.__size_;
2921 this->__cap() = __v.__cap();
2922 __v.__begin_ = nullptr;
2923 __v.__cap() = __v.__size_ = 0;
2924 }
2925 else if (__v.size() > 0)
2926 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002927 __vallocate(__v.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002928 __construct_at_end(__v.begin(), __v.end());
2929 }
2930}
2931
2932template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002933inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002934vector<bool, _Allocator>&
2935vector<bool, _Allocator>::operator=(vector&& __v)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002936 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002937{
2938 __move_assign(__v, integral_constant<bool,
2939 __storage_traits::propagate_on_container_move_assignment::value>());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002940 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002941}
2942
2943template <class _Allocator>
2944void
2945vector<bool, _Allocator>::__move_assign(vector& __c, false_type)
2946{
2947 if (__alloc() != __c.__alloc())
2948 assign(__c.begin(), __c.end());
2949 else
2950 __move_assign(__c, true_type());
2951}
2952
2953template <class _Allocator>
2954void
2955vector<bool, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002956 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002957{
Marshall Clow3ff48e02018-05-22 16:20:28 +00002958 __vdeallocate();
Marshall Clowb4871fa2014-07-21 15:15:15 +00002959 __move_assign_alloc(__c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002960 this->__begin_ = __c.__begin_;
2961 this->__size_ = __c.__size_;
2962 this->__cap() = __c.__cap();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002963 __c.__begin_ = nullptr;
2964 __c.__cap() = __c.__size_ = 0;
2965}
Howard Hinnant74279a52010-09-04 23:28:19 +00002966
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002967#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002968
2969template <class _Allocator>
2970void
2971vector<bool, _Allocator>::assign(size_type __n, const value_type& __x)
2972{
2973 __size_ = 0;
2974 if (__n > 0)
2975 {
2976 size_type __c = capacity();
2977 if (__n <= __c)
2978 __size_ = __n;
2979 else
2980 {
2981 vector __v(__alloc());
2982 __v.reserve(__recommend(__n));
2983 __v.__size_ = __n;
2984 swap(__v);
2985 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002986 _VSTD::fill_n(begin(), __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002987 }
Eric Fiselier69c51982016-12-28 06:06:09 +00002988 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002989}
2990
2991template <class _Allocator>
2992template <class _InputIterator>
2993typename enable_if
2994<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002995 __is_cpp17_input_iterator<_InputIterator>::value &&
2996 !__is_cpp17_forward_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002997 void
2998>::type
2999vector<bool, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
3000{
3001 clear();
3002 for (; __first != __last; ++__first)
3003 push_back(*__first);
3004}
3005
3006template <class _Allocator>
3007template <class _ForwardIterator>
3008typename enable_if
3009<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05003010 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003011 void
3012>::type
3013vector<bool, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
3014{
3015 clear();
Eric Fiselier6003c772016-12-23 23:37:52 +00003016 difference_type __ns = _VSTD::distance(__first, __last);
3017 _LIBCPP_ASSERT(__ns >= 0, "invalid range specified");
3018 const size_t __n = static_cast<size_type>(__ns);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003019 if (__n)
3020 {
3021 if (__n > capacity())
3022 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00003023 __vdeallocate();
3024 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003025 }
3026 __construct_at_end(__first, __last);
3027 }
3028}
3029
3030template <class _Allocator>
3031void
3032vector<bool, _Allocator>::reserve(size_type __n)
3033{
3034 if (__n > capacity())
3035 {
3036 vector __v(this->__alloc());
Marshall Clow3ff48e02018-05-22 16:20:28 +00003037 __v.__vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003038 __v.__construct_at_end(this->begin(), this->end());
3039 swap(__v);
3040 __invalidate_all_iterators();
3041 }
3042}
3043
3044template <class _Allocator>
3045void
Howard Hinnant1c936782011-06-03 19:40:40 +00003046vector<bool, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003047{
3048 if (__external_cap_to_internal(size()) > __cap())
3049 {
3050#ifndef _LIBCPP_NO_EXCEPTIONS
3051 try
3052 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003053#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003054 vector(*this, allocator_type(__alloc())).swap(*this);
3055#ifndef _LIBCPP_NO_EXCEPTIONS
3056 }
3057 catch (...)
3058 {
3059 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003060#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003061 }
3062}
3063
3064template <class _Allocator>
3065typename vector<bool, _Allocator>::reference
3066vector<bool, _Allocator>::at(size_type __n)
3067{
3068 if (__n >= size())
3069 this->__throw_out_of_range();
3070 return (*this)[__n];
3071}
3072
3073template <class _Allocator>
3074typename vector<bool, _Allocator>::const_reference
3075vector<bool, _Allocator>::at(size_type __n) const
3076{
3077 if (__n >= size())
3078 this->__throw_out_of_range();
3079 return (*this)[__n];
3080}
3081
3082template <class _Allocator>
3083void
3084vector<bool, _Allocator>::push_back(const value_type& __x)
3085{
3086 if (this->__size_ == this->capacity())
3087 reserve(__recommend(this->__size_ + 1));
3088 ++this->__size_;
3089 back() = __x;
3090}
3091
3092template <class _Allocator>
3093typename vector<bool, _Allocator>::iterator
3094vector<bool, _Allocator>::insert(const_iterator __position, const value_type& __x)
3095{
3096 iterator __r;
3097 if (size() < capacity())
3098 {
3099 const_iterator __old_end = end();
3100 ++__size_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003101 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003102 __r = __const_iterator_cast(__position);
3103 }
3104 else
3105 {
3106 vector __v(__alloc());
3107 __v.reserve(__recommend(__size_ + 1));
3108 __v.__size_ = __size_ + 1;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003109 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3110 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003111 swap(__v);
3112 }
3113 *__r = __x;
3114 return __r;
3115}
3116
3117template <class _Allocator>
3118typename vector<bool, _Allocator>::iterator
3119vector<bool, _Allocator>::insert(const_iterator __position, size_type __n, const value_type& __x)
3120{
3121 iterator __r;
3122 size_type __c = capacity();
3123 if (__n <= __c && size() <= __c - __n)
3124 {
3125 const_iterator __old_end = end();
3126 __size_ += __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003127 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003128 __r = __const_iterator_cast(__position);
3129 }
3130 else
3131 {
3132 vector __v(__alloc());
3133 __v.reserve(__recommend(__size_ + __n));
3134 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003135 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3136 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003137 swap(__v);
3138 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003139 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003140 return __r;
3141}
3142
3143template <class _Allocator>
3144template <class _InputIterator>
3145typename enable_if
3146<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05003147 __is_cpp17_input_iterator <_InputIterator>::value &&
3148 !__is_cpp17_forward_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003149 typename vector<bool, _Allocator>::iterator
3150>::type
3151vector<bool, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
3152{
3153 difference_type __off = __position - begin();
3154 iterator __p = __const_iterator_cast(__position);
3155 iterator __old_end = end();
3156 for (; size() != capacity() && __first != __last; ++__first)
3157 {
3158 ++this->__size_;
3159 back() = *__first;
3160 }
3161 vector __v(__alloc());
3162 if (__first != __last)
3163 {
3164#ifndef _LIBCPP_NO_EXCEPTIONS
3165 try
3166 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003167#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003168 __v.assign(__first, __last);
3169 difference_type __old_size = static_cast<difference_type>(__old_end - begin());
3170 difference_type __old_p = __p - begin();
3171 reserve(__recommend(size() + __v.size()));
3172 __p = begin() + __old_p;
3173 __old_end = begin() + __old_size;
3174#ifndef _LIBCPP_NO_EXCEPTIONS
3175 }
3176 catch (...)
3177 {
3178 erase(__old_end, end());
3179 throw;
3180 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003181#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003182 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003183 __p = _VSTD::rotate(__p, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003184 insert(__p, __v.begin(), __v.end());
3185 return begin() + __off;
3186}
3187
3188template <class _Allocator>
3189template <class _ForwardIterator>
3190typename enable_if
3191<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05003192 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003193 typename vector<bool, _Allocator>::iterator
3194>::type
3195vector<bool, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
3196{
Eric Fiselier654dd332016-12-11 05:31:00 +00003197 const difference_type __n_signed = _VSTD::distance(__first, __last);
3198 _LIBCPP_ASSERT(__n_signed >= 0, "invalid range specified");
3199 const size_type __n = static_cast<size_type>(__n_signed);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003200 iterator __r;
3201 size_type __c = capacity();
3202 if (__n <= __c && size() <= __c - __n)
3203 {
3204 const_iterator __old_end = end();
3205 __size_ += __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003206 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003207 __r = __const_iterator_cast(__position);
3208 }
3209 else
3210 {
3211 vector __v(__alloc());
3212 __v.reserve(__recommend(__size_ + __n));
3213 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003214 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3215 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003216 swap(__v);
3217 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003218 _VSTD::copy(__first, __last, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003219 return __r;
3220}
3221
3222template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003223inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003224typename vector<bool, _Allocator>::iterator
3225vector<bool, _Allocator>::erase(const_iterator __position)
3226{
3227 iterator __r = __const_iterator_cast(__position);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003228 _VSTD::copy(__position + 1, this->cend(), __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003229 --__size_;
3230 return __r;
3231}
3232
3233template <class _Allocator>
3234typename vector<bool, _Allocator>::iterator
3235vector<bool, _Allocator>::erase(const_iterator __first, const_iterator __last)
3236{
3237 iterator __r = __const_iterator_cast(__first);
3238 difference_type __d = __last - __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003239 _VSTD::copy(__last, this->cend(), __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003240 __size_ -= __d;
3241 return __r;
3242}
3243
3244template <class _Allocator>
3245void
3246vector<bool, _Allocator>::swap(vector& __x)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003247#if _LIBCPP_STD_VER >= 14
3248 _NOEXCEPT
3249#else
Eric Fiselier69c51982016-12-28 06:06:09 +00003250 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003251 __is_nothrow_swappable<allocator_type>::value)
3252#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003253{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003254 _VSTD::swap(this->__begin_, __x.__begin_);
3255 _VSTD::swap(this->__size_, __x.__size_);
3256 _VSTD::swap(this->__cap(), __x.__cap());
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05003257 _VSTD::__swap_allocator(this->__alloc(), __x.__alloc(),
Marshall Clow8982dcd2015-07-13 20:04:56 +00003258 integral_constant<bool, __alloc_traits::propagate_on_container_swap::value>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003259}
3260
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003261template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003262void
3263vector<bool, _Allocator>::resize(size_type __sz, value_type __x)
3264{
3265 size_type __cs = size();
3266 if (__cs < __sz)
3267 {
3268 iterator __r;
3269 size_type __c = capacity();
3270 size_type __n = __sz - __cs;
3271 if (__n <= __c && __cs <= __c - __n)
3272 {
3273 __r = end();
3274 __size_ += __n;
3275 }
3276 else
3277 {
3278 vector __v(__alloc());
3279 __v.reserve(__recommend(__size_ + __n));
3280 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003281 __r = _VSTD::copy(cbegin(), cend(), __v.begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003282 swap(__v);
3283 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003284 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003285 }
3286 else
3287 __size_ = __sz;
3288}
3289
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003290template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003291void
Howard Hinnant1c936782011-06-03 19:40:40 +00003292vector<bool, _Allocator>::flip() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003293{
3294 // do middle whole words
3295 size_type __n = __size_;
3296 __storage_pointer __p = __begin_;
3297 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3298 *__p = ~*__p;
3299 // do last partial word
3300 if (__n > 0)
3301 {
3302 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3303 __storage_type __b = *__p & __m;
3304 *__p &= ~__m;
3305 *__p |= ~__b & __m;
3306 }
3307}
3308
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003309template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003310bool
3311vector<bool, _Allocator>::__invariants() const
3312{
Howard Hinnant76053d72013-06-27 19:35:32 +00003313 if (this->__begin_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003314 {
3315 if (this->__size_ != 0 || this->__cap() != 0)
3316 return false;
3317 }
3318 else
3319 {
3320 if (this->__cap() == 0)
3321 return false;
3322 if (this->__size_ > this->capacity())
3323 return false;
3324 }
3325 return true;
3326}
3327
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003328template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003329size_t
Howard Hinnant1c936782011-06-03 19:40:40 +00003330vector<bool, _Allocator>::__hash_code() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003331{
3332 size_t __h = 0;
3333 // do middle whole words
3334 size_type __n = __size_;
3335 __storage_pointer __p = __begin_;
3336 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3337 __h ^= *__p;
3338 // do last partial word
3339 if (__n > 0)
3340 {
3341 const __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3342 __h ^= *__p & __m;
3343 }
3344 return __h;
3345}
3346
3347template <class _Allocator>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003348struct _LIBCPP_TEMPLATE_VIS hash<vector<bool, _Allocator> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00003349 : public unary_function<vector<bool, _Allocator>, size_t>
3350{
Howard Hinnant1c265cd2010-09-23 18:58:28 +00003351 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +00003352 size_t operator()(const vector<bool, _Allocator>& __vec) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003353 {return __vec.__hash_code();}
3354};
3355
3356template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003357inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003358bool
3359operator==(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3360{
3361 const typename vector<_Tp, _Allocator>::size_type __sz = __x.size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003362 return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003363}
3364
3365template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003366inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003367bool
3368operator!=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3369{
3370 return !(__x == __y);
3371}
3372
3373template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003374inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003375bool
3376operator< (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3377{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003378 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003379}
3380
3381template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003382inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003383bool
3384operator> (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3385{
3386 return __y < __x;
3387}
3388
3389template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003390inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003391bool
3392operator>=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3393{
3394 return !(__x < __y);
3395}
3396
3397template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003398inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003399bool
3400operator<=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3401{
3402 return !(__y < __x);
3403}
3404
3405template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003406inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003407void
3408swap(vector<_Tp, _Allocator>& __x, vector<_Tp, _Allocator>& __y)
Howard Hinnant1c936782011-06-03 19:40:40 +00003409 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00003410{
3411 __x.swap(__y);
3412}
3413
Marshall Clow29b53f22018-12-14 18:49:35 +00003414#if _LIBCPP_STD_VER > 17
3415template <class _Tp, class _Allocator, class _Up>
Marek Kurdeja98b1412020-05-02 13:58:03 +02003416inline _LIBCPP_INLINE_VISIBILITY typename vector<_Tp, _Allocator>::size_type
3417erase(vector<_Tp, _Allocator>& __c, const _Up& __v) {
3418 auto __old_size = __c.size();
3419 __c.erase(_VSTD::remove(__c.begin(), __c.end(), __v), __c.end());
3420 return __old_size - __c.size();
3421}
Marshall Clow29b53f22018-12-14 18:49:35 +00003422
3423template <class _Tp, class _Allocator, class _Predicate>
Marek Kurdeja98b1412020-05-02 13:58:03 +02003424inline _LIBCPP_INLINE_VISIBILITY typename vector<_Tp, _Allocator>::size_type
3425erase_if(vector<_Tp, _Allocator>& __c, _Predicate __pred) {
3426 auto __old_size = __c.size();
3427 __c.erase(_VSTD::remove_if(__c.begin(), __c.end(), __pred), __c.end());
3428 return __old_size - __c.size();
3429}
Marshall Clow29b53f22018-12-14 18:49:35 +00003430#endif
3431
Howard Hinnantc51e1022010-05-11 19:42:16 +00003432_LIBCPP_END_NAMESPACE_STD
3433
Eric Fiselierf4433a32017-05-31 22:07:49 +00003434_LIBCPP_POP_MACROS
3435
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003436#endif // _LIBCPP_VECTOR