blob: f75a1694910fec8124c7b443fea146922cea4a5e [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;
147 reference& operator=(const bool x) noexcept;
148 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>
278#include <__split_buffer>
279#include <algorithm>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000280#include <climits>
Arthur O'Dwyer7deec122021-03-24 18:19:12 -0400281#include <compare>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400282#include <cstring>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000283#include <initializer_list>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400284#include <iosfwd> // for forward declaration of vector
285#include <limits>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000286#include <memory>
287#include <stdexcept>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400288#include <type_traits>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000289#include <version>
Howard Hinnante6ff0b62013-08-02 00:26:35 +0000290
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000291#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000292#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000293#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000294
Eric Fiselierf4433a32017-05-31 22:07:49 +0000295_LIBCPP_PUSH_MACROS
296#include <__undef_macros>
297
298
Howard Hinnantc51e1022010-05-11 19:42:16 +0000299_LIBCPP_BEGIN_NAMESPACE_STD
300
301template <bool>
Louis Dionne53dca682019-08-28 18:10:39 +0000302class _LIBCPP_TEMPLATE_VIS __vector_base_common
Howard Hinnantc51e1022010-05-11 19:42:16 +0000303{
304protected:
Louis Dionne16fe2952018-07-11 23:14:33 +0000305 _LIBCPP_INLINE_VISIBILITY __vector_base_common() {}
Marshall Clow8fea1612016-08-25 15:09:01 +0000306 _LIBCPP_NORETURN void __throw_length_error() const;
307 _LIBCPP_NORETURN void __throw_out_of_range() const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000308};
309
310template <bool __b>
311void
312__vector_base_common<__b>::__throw_length_error() const
313{
Marshall Clow8fea1612016-08-25 15:09:01 +0000314 _VSTD::__throw_length_error("vector");
Howard Hinnantc51e1022010-05-11 19:42:16 +0000315}
316
317template <bool __b>
318void
319__vector_base_common<__b>::__throw_out_of_range() const
320{
Marshall Clow8fea1612016-08-25 15:09:01 +0000321 _VSTD::__throw_out_of_range("vector");
Howard Hinnantc51e1022010-05-11 19:42:16 +0000322}
323
Eric Fiselier1b57fa82016-09-15 22:27:07 +0000324_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __vector_base_common<true>)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000325
326template <class _Tp, class _Allocator>
327class __vector_base
328 : protected __vector_base_common<true>
329{
Marshall Clowf0ca1492018-05-21 21:30:12 +0000330public:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000331 typedef _Allocator allocator_type;
332 typedef allocator_traits<allocator_type> __alloc_traits;
Marshall Clowf0ca1492018-05-21 21:30:12 +0000333 typedef typename __alloc_traits::size_type size_type;
334protected:
335 typedef _Tp value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000336 typedef value_type& reference;
337 typedef const value_type& const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000338 typedef typename __alloc_traits::difference_type difference_type;
339 typedef typename __alloc_traits::pointer pointer;
340 typedef typename __alloc_traits::const_pointer const_pointer;
341 typedef pointer iterator;
342 typedef const_pointer const_iterator;
343
344 pointer __begin_;
345 pointer __end_;
346 __compressed_pair<pointer, allocator_type> __end_cap_;
347
Howard Hinnant1c936782011-06-03 19:40:40 +0000348 _LIBCPP_INLINE_VISIBILITY
349 allocator_type& __alloc() _NOEXCEPT
350 {return __end_cap_.second();}
351 _LIBCPP_INLINE_VISIBILITY
352 const allocator_type& __alloc() const _NOEXCEPT
353 {return __end_cap_.second();}
354 _LIBCPP_INLINE_VISIBILITY
355 pointer& __end_cap() _NOEXCEPT
356 {return __end_cap_.first();}
357 _LIBCPP_INLINE_VISIBILITY
358 const pointer& __end_cap() const _NOEXCEPT
359 {return __end_cap_.first();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000360
Howard Hinnant1c936782011-06-03 19:40:40 +0000361 _LIBCPP_INLINE_VISIBILITY
362 __vector_base()
363 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnantcf823322010-12-17 14:46:43 +0000364 _LIBCPP_INLINE_VISIBILITY __vector_base(const allocator_type& __a);
Eric Fiselier4f1534c2018-06-05 22:32:52 +0000365#ifndef _LIBCPP_CXX03_LANG
366 _LIBCPP_INLINE_VISIBILITY __vector_base(allocator_type&& __a) _NOEXCEPT;
367#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000368 ~__vector_base();
369
Howard Hinnant1c936782011-06-03 19:40:40 +0000370 _LIBCPP_INLINE_VISIBILITY
371 void clear() _NOEXCEPT {__destruct_at_end(__begin_);}
372 _LIBCPP_INLINE_VISIBILITY
373 size_type capacity() const _NOEXCEPT
374 {return static_cast<size_type>(__end_cap() - __begin_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000375
Howard Hinnant1c936782011-06-03 19:40:40 +0000376 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76053d72013-06-27 19:35:32 +0000377 void __destruct_at_end(pointer __new_last) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000378
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000379 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000380 void __copy_assign_alloc(const __vector_base& __c)
381 {__copy_assign_alloc(__c, integral_constant<bool,
382 __alloc_traits::propagate_on_container_copy_assignment::value>());}
383
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000384 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000385 void __move_assign_alloc(__vector_base& __c)
Howard Hinnant1c936782011-06-03 19:40:40 +0000386 _NOEXCEPT_(
387 !__alloc_traits::propagate_on_container_move_assignment::value ||
388 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000389 {__move_assign_alloc(__c, integral_constant<bool,
390 __alloc_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000391private:
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000392 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000393 void __copy_assign_alloc(const __vector_base& __c, true_type)
394 {
395 if (__alloc() != __c.__alloc())
396 {
397 clear();
398 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
399 __begin_ = __end_ = __end_cap() = nullptr;
400 }
401 __alloc() = __c.__alloc();
402 }
403
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000404 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +0000405 void __copy_assign_alloc(const __vector_base&, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000406 {}
407
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000408 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +0000409 void __move_assign_alloc(__vector_base& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +0000410 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000411 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000412 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000413 }
414
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000415 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +0000416 void __move_assign_alloc(__vector_base&, false_type)
Howard Hinnant1c936782011-06-03 19:40:40 +0000417 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000418 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000419};
420
421template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000422inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000423void
Howard Hinnant76053d72013-06-27 19:35:32 +0000424__vector_base<_Tp, _Allocator>::__destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000425{
Bruce Mitchener405fdcd2017-03-23 14:39:23 +0000426 pointer __soon_to_be_end = __end_;
427 while (__new_last != __soon_to_be_end)
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500428 __alloc_traits::destroy(__alloc(), _VSTD::__to_address(--__soon_to_be_end));
Bruce Mitchener405fdcd2017-03-23 14:39:23 +0000429 __end_ = __new_last;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000430}
431
432template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000433inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000434__vector_base<_Tp, _Allocator>::__vector_base()
Howard Hinnant1c936782011-06-03 19:40:40 +0000435 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant76053d72013-06-27 19:35:32 +0000436 : __begin_(nullptr),
437 __end_(nullptr),
Eric Fiselier33ebfb62019-12-16 18:23:39 -0500438 __end_cap_(nullptr, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +0000439{
440}
441
442template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000443inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000444__vector_base<_Tp, _Allocator>::__vector_base(const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +0000445 : __begin_(nullptr),
446 __end_(nullptr),
447 __end_cap_(nullptr, __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000448{
449}
450
Eric Fiselier4f1534c2018-06-05 22:32:52 +0000451#ifndef _LIBCPP_CXX03_LANG
452template <class _Tp, class _Allocator>
453inline _LIBCPP_INLINE_VISIBILITY
454__vector_base<_Tp, _Allocator>::__vector_base(allocator_type&& __a) _NOEXCEPT
455 : __begin_(nullptr),
456 __end_(nullptr),
Arthur O'Dwyer07b22492020-11-27 11:02:06 -0500457 __end_cap_(nullptr, _VSTD::move(__a)) {}
Eric Fiselier4f1534c2018-06-05 22:32:52 +0000458#endif
459
Howard Hinnantc51e1022010-05-11 19:42:16 +0000460template <class _Tp, class _Allocator>
461__vector_base<_Tp, _Allocator>::~__vector_base()
462{
Howard Hinnant76053d72013-06-27 19:35:32 +0000463 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000464 {
465 clear();
466 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
467 }
468}
469
Eric Fiselier876c6862016-02-20 00:19:45 +0000470template <class _Tp, class _Allocator /* = allocator<_Tp> */>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000471class _LIBCPP_TEMPLATE_VIS vector
Howard Hinnantc51e1022010-05-11 19:42:16 +0000472 : private __vector_base<_Tp, _Allocator>
473{
474private:
475 typedef __vector_base<_Tp, _Allocator> __base;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000476 typedef allocator<_Tp> __default_allocator_type;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000477public:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000478 typedef vector __self;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000479 typedef _Tp value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000480 typedef _Allocator allocator_type;
481 typedef typename __base::__alloc_traits __alloc_traits;
482 typedef typename __base::reference reference;
483 typedef typename __base::const_reference const_reference;
484 typedef typename __base::size_type size_type;
485 typedef typename __base::difference_type difference_type;
486 typedef typename __base::pointer pointer;
487 typedef typename __base::const_pointer const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000488 typedef __wrap_iter<pointer> iterator;
489 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000490 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
491 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000492
Howard Hinnanta416ff02013-03-26 19:04:56 +0000493 static_assert((is_same<typename allocator_type::value_type, value_type>::value),
494 "Allocator::value_type must be same type as value_type");
495
Howard Hinnant1c936782011-06-03 19:40:40 +0000496 _LIBCPP_INLINE_VISIBILITY
Marshall Clowe546cbb2015-06-04 02:05:41 +0000497 vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant27e0e772011-09-14 18:33:51 +0000498 {
Louis Dionneba400782020-10-02 15:02:52 -0400499#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000500 __get_db()->__insert_c(this);
501#endif
502 }
503 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
Marshall Clow8f282f42015-06-04 00:10:20 +0000504#if _LIBCPP_STD_VER <= 14
505 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
506#else
507 _NOEXCEPT
508#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +0000509 : __base(__a)
510 {
Louis Dionneba400782020-10-02 15:02:52 -0400511#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000512 __get_db()->__insert_c(this);
513#endif
514 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000515 explicit vector(size_type __n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +0000516#if _LIBCPP_STD_VER > 11
517 explicit vector(size_type __n, const allocator_type& __a);
518#endif
Marshall Clowf0ca1492018-05-21 21:30:12 +0000519 vector(size_type __n, const value_type& __x);
520 vector(size_type __n, const value_type& __x, const allocator_type& __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000521 template <class _InputIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +0000522 vector(_InputIterator __first,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500523 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
524 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000525 is_constructible<
526 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +0000527 typename iterator_traits<_InputIterator>::reference>::value,
528 _InputIterator>::type __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000529 template <class _InputIterator>
530 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500531 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
532 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000533 is_constructible<
534 value_type,
535 typename iterator_traits<_InputIterator>::reference>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000536 template <class _ForwardIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +0000537 vector(_ForwardIterator __first,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500538 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000539 is_constructible<
540 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +0000541 typename iterator_traits<_ForwardIterator>::reference>::value,
542 _ForwardIterator>::type __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000543 template <class _ForwardIterator>
544 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500545 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000546 is_constructible<
547 value_type,
548 typename iterator_traits<_ForwardIterator>::reference>::value>::type* = 0);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000549
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000550 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant27e0e772011-09-14 18:33:51 +0000551 ~vector()
552 {
Marshall Clow68af4f32018-09-07 15:47:59 +0000553 __annotate_delete();
Louis Dionneba400782020-10-02 15:02:52 -0400554#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000555 __get_db()->__erase_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000556#endif
Marshall Clow68af4f32018-09-07 15:47:59 +0000557 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000558
559 vector(const vector& __x);
560 vector(const vector& __x, const allocator_type& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000561 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000562 vector& operator=(const vector& __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000563
564#ifndef _LIBCPP_CXX03_LANG
565 _LIBCPP_INLINE_VISIBILITY
566 vector(initializer_list<value_type> __il);
567
568 _LIBCPP_INLINE_VISIBILITY
569 vector(initializer_list<value_type> __il, const allocator_type& __a);
570
Howard Hinnantcf823322010-12-17 14:46:43 +0000571 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000572 vector(vector&& __x)
Marshall Clowe5108202015-07-14 14:46:32 +0000573#if _LIBCPP_STD_VER > 14
574 _NOEXCEPT;
575#else
Howard Hinnant1c936782011-06-03 19:40:40 +0000576 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowe5108202015-07-14 14:46:32 +0000577#endif
Eric Fiseliered9e9362017-04-16 02:40:45 +0000578
Howard Hinnantcf823322010-12-17 14:46:43 +0000579 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000580 vector(vector&& __x, const allocator_type& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000581 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000582 vector& operator=(vector&& __x)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000583 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiseliered9e9362017-04-16 02:40:45 +0000584
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000585 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000586 vector& operator=(initializer_list<value_type> __il)
587 {assign(__il.begin(), __il.end()); return *this;}
Eric Fiseliered9e9362017-04-16 02:40:45 +0000588
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400589#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000590
591 template <class _InputIterator>
592 typename enable_if
593 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500594 __is_cpp17_input_iterator <_InputIterator>::value &&
595 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000596 is_constructible<
597 value_type,
598 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000599 void
600 >::type
601 assign(_InputIterator __first, _InputIterator __last);
602 template <class _ForwardIterator>
603 typename enable_if
604 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500605 __is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000606 is_constructible<
607 value_type,
608 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000609 void
610 >::type
611 assign(_ForwardIterator __first, _ForwardIterator __last);
612
613 void assign(size_type __n, const_reference __u);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000614
615#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000616 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000617 void assign(initializer_list<value_type> __il)
618 {assign(__il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +0000619#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000620
Howard Hinnant1c936782011-06-03 19:40:40 +0000621 _LIBCPP_INLINE_VISIBILITY
622 allocator_type get_allocator() const _NOEXCEPT
623 {return this->__alloc();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000624
Howard Hinnant1c936782011-06-03 19:40:40 +0000625 _LIBCPP_INLINE_VISIBILITY iterator begin() _NOEXCEPT;
626 _LIBCPP_INLINE_VISIBILITY const_iterator begin() const _NOEXCEPT;
627 _LIBCPP_INLINE_VISIBILITY iterator end() _NOEXCEPT;
628 _LIBCPP_INLINE_VISIBILITY const_iterator end() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000629
Howard Hinnant1c936782011-06-03 19:40:40 +0000630 _LIBCPP_INLINE_VISIBILITY
631 reverse_iterator rbegin() _NOEXCEPT
632 {return reverse_iterator(end());}
633 _LIBCPP_INLINE_VISIBILITY
634 const_reverse_iterator rbegin() const _NOEXCEPT
635 {return const_reverse_iterator(end());}
636 _LIBCPP_INLINE_VISIBILITY
637 reverse_iterator rend() _NOEXCEPT
638 {return reverse_iterator(begin());}
639 _LIBCPP_INLINE_VISIBILITY
640 const_reverse_iterator rend() const _NOEXCEPT
641 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000642
Howard Hinnant1c936782011-06-03 19:40:40 +0000643 _LIBCPP_INLINE_VISIBILITY
644 const_iterator cbegin() const _NOEXCEPT
645 {return begin();}
646 _LIBCPP_INLINE_VISIBILITY
647 const_iterator cend() const _NOEXCEPT
648 {return end();}
649 _LIBCPP_INLINE_VISIBILITY
650 const_reverse_iterator crbegin() const _NOEXCEPT
651 {return rbegin();}
652 _LIBCPP_INLINE_VISIBILITY
653 const_reverse_iterator crend() const _NOEXCEPT
654 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000655
Howard Hinnant1c936782011-06-03 19:40:40 +0000656 _LIBCPP_INLINE_VISIBILITY
657 size_type size() const _NOEXCEPT
658 {return static_cast<size_type>(this->__end_ - this->__begin_);}
659 _LIBCPP_INLINE_VISIBILITY
660 size_type capacity() const _NOEXCEPT
661 {return __base::capacity();}
Marshall Clow425f5752017-11-15 05:51:26 +0000662 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000663 bool empty() const _NOEXCEPT
664 {return this->__begin_ == this->__end_;}
665 size_type max_size() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000666 void reserve(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +0000667 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000668
Marshall Clowd6470492019-03-15 00:29:35 +0000669 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) _NOEXCEPT;
670 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000671 reference at(size_type __n);
672 const_reference at(size_type __n) const;
673
Marshall Clowd6470492019-03-15 00:29:35 +0000674 _LIBCPP_INLINE_VISIBILITY reference front() _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000675 {
Kristina Bessonovaaeeaa7e2021-05-09 19:29:56 +0200676 _LIBCPP_ASSERT(!empty(), "front() called on an empty vector");
Howard Hinnant27e0e772011-09-14 18:33:51 +0000677 return *this->__begin_;
678 }
Marshall Clowd6470492019-03-15 00:29:35 +0000679 _LIBCPP_INLINE_VISIBILITY const_reference front() const _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000680 {
Kristina Bessonovaaeeaa7e2021-05-09 19:29:56 +0200681 _LIBCPP_ASSERT(!empty(), "front() called on an empty vector");
Howard Hinnant27e0e772011-09-14 18:33:51 +0000682 return *this->__begin_;
683 }
Marshall Clowd6470492019-03-15 00:29:35 +0000684 _LIBCPP_INLINE_VISIBILITY reference back() _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000685 {
Kristina Bessonovaaeeaa7e2021-05-09 19:29:56 +0200686 _LIBCPP_ASSERT(!empty(), "back() called on an empty vector");
Howard Hinnant27e0e772011-09-14 18:33:51 +0000687 return *(this->__end_ - 1);
688 }
Marshall Clowd6470492019-03-15 00:29:35 +0000689 _LIBCPP_INLINE_VISIBILITY const_reference back() const _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000690 {
Kristina Bessonovaaeeaa7e2021-05-09 19:29:56 +0200691 _LIBCPP_ASSERT(!empty(), "back() called on an empty vector");
Howard Hinnant27e0e772011-09-14 18:33:51 +0000692 return *(this->__end_ - 1);
693 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000694
Howard Hinnant1c936782011-06-03 19:40:40 +0000695 _LIBCPP_INLINE_VISIBILITY
696 value_type* data() _NOEXCEPT
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500697 {return _VSTD::__to_address(this->__begin_);}
Howard Hinnant1c936782011-06-03 19:40:40 +0000698 _LIBCPP_INLINE_VISIBILITY
699 const value_type* data() const _NOEXCEPT
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500700 {return _VSTD::__to_address(this->__begin_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000701
Eric Fiselier96919722017-10-17 13:03:17 +0000702#ifdef _LIBCPP_CXX03_LANG
703 _LIBCPP_INLINE_VISIBILITY
704 void __emplace_back(const value_type& __x) { push_back(__x); }
705#else
706 template <class _Arg>
707 _LIBCPP_INLINE_VISIBILITY
708 void __emplace_back(_Arg&& __arg) {
709 emplace_back(_VSTD::forward<_Arg>(__arg));
710 }
711#endif
712
Howard Hinnantcf823322010-12-17 14:46:43 +0000713 _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000714
715#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000716 _LIBCPP_INLINE_VISIBILITY void push_back(value_type&& __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000717
Howard Hinnantc51e1022010-05-11 19:42:16 +0000718 template <class... _Args>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000719 _LIBCPP_INLINE_VISIBILITY
Marshall Clowea52cc42017-01-24 23:09:12 +0000720#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +0000721 reference emplace_back(_Args&&... __args);
Marshall Clowea52cc42017-01-24 23:09:12 +0000722#else
723 void emplace_back(_Args&&... __args);
724#endif
Eric Fiseliered9e9362017-04-16 02:40:45 +0000725#endif // !_LIBCPP_CXX03_LANG
726
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000727 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000728 void pop_back();
729
730 iterator insert(const_iterator __position, const_reference __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000731
732#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000733 iterator insert(const_iterator __position, value_type&& __x);
734 template <class... _Args>
735 iterator emplace(const_iterator __position, _Args&&... __args);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400736#endif // !_LIBCPP_CXX03_LANG
Eric Fiseliered9e9362017-04-16 02:40:45 +0000737
Howard Hinnantc51e1022010-05-11 19:42:16 +0000738 iterator insert(const_iterator __position, size_type __n, const_reference __x);
739 template <class _InputIterator>
740 typename enable_if
741 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500742 __is_cpp17_input_iterator <_InputIterator>::value &&
743 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000744 is_constructible<
745 value_type,
746 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000747 iterator
748 >::type
749 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
750 template <class _ForwardIterator>
751 typename enable_if
752 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500753 __is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000754 is_constructible<
755 value_type,
756 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000757 iterator
758 >::type
759 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000760
761#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000762 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000763 iterator insert(const_iterator __position, initializer_list<value_type> __il)
764 {return insert(__position, __il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +0000765#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000766
Howard Hinnantcf823322010-12-17 14:46:43 +0000767 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000768 iterator erase(const_iterator __first, const_iterator __last);
769
Howard Hinnant1c936782011-06-03 19:40:40 +0000770 _LIBCPP_INLINE_VISIBILITY
771 void clear() _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000772 {
Marshall Clow2cd9d372014-05-08 14:14:06 +0000773 size_type __old_size = size();
Howard Hinnant27e0e772011-09-14 18:33:51 +0000774 __base::clear();
Marshall Clow2cd9d372014-05-08 14:14:06 +0000775 __annotate_shrink(__old_size);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000776 __invalidate_all_iterators();
777 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000778
779 void resize(size_type __sz);
780 void resize(size_type __sz, const_reference __x);
781
Howard Hinnant1c936782011-06-03 19:40:40 +0000782 void swap(vector&)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000783#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +0000784 _NOEXCEPT;
Marshall Clow8982dcd2015-07-13 20:04:56 +0000785#else
Eric Fiselier873b8d32019-03-18 21:50:12 +0000786 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +0000787 __is_nothrow_swappable<allocator_type>::value);
788#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000789
790 bool __invariants() const;
791
Louis Dionneba400782020-10-02 15:02:52 -0400792#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000793
794 bool __dereferenceable(const const_iterator* __i) const;
795 bool __decrementable(const const_iterator* __i) const;
796 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
797 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
798
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400799#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000800
Howard Hinnantc51e1022010-05-11 19:42:16 +0000801private:
Howard Hinnantcf823322010-12-17 14:46:43 +0000802 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Eric Fiselier69c51982016-12-28 06:06:09 +0000803 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(pointer __new_last);
Marshall Clow3ff48e02018-05-22 16:20:28 +0000804 void __vallocate(size_type __n);
805 void __vdeallocate() _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000806 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
Howard Hinnanta5589382011-01-04 19:53:31 +0000807 void __construct_at_end(size_type __n);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000808 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000809 void __construct_at_end(size_type __n, const_reference __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000810 template <class _ForwardIterator>
811 typename enable_if
812 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500813 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000814 void
815 >::type
Eric Fiselieracfe6f02015-03-31 16:54:19 +0000816 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000817 void __append(size_type __n);
818 void __append(size_type __n, const_reference __x);
Howard Hinnantcf823322010-12-17 14:46:43 +0000819 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000820 iterator __make_iter(pointer __p) _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000821 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000822 const_iterator __make_iter(const_pointer __p) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000823 void __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v);
824 pointer __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p);
825 void __move_range(pointer __from_s, pointer __from_e, pointer __to);
Howard Hinnant1c936782011-06-03 19:40:40 +0000826 void __move_assign(vector& __c, true_type)
827 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000828 void __move_assign(vector& __c, false_type)
829 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000830 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76053d72013-06-27 19:35:32 +0000831 void __destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000832 {
Eric Fiselier69c51982016-12-28 06:06:09 +0000833 __invalidate_iterators_past(__new_last);
Marshall Clow2cd9d372014-05-08 14:14:06 +0000834 size_type __old_size = size();
Howard Hinnant27e0e772011-09-14 18:33:51 +0000835 __base::__destruct_at_end(__new_last);
Marshall Clow2cd9d372014-05-08 14:14:06 +0000836 __annotate_shrink(__old_size);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000837 }
Eric Fiseliered9e9362017-04-16 02:40:45 +0000838
839#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier7110f442019-03-19 19:19:44 +0000840 template <class _Up>
841 _LIBCPP_INLINE_VISIBILITY
842 inline void __push_back_slow_path(_Up&& __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000843
Howard Hinnantb6c49562012-02-26 15:30:12 +0000844 template <class... _Args>
Eric Fiselier7110f442019-03-19 19:19:44 +0000845 _LIBCPP_INLINE_VISIBILITY
846 inline void __emplace_back_slow_path(_Args&&... __args);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000847#else
Eric Fiselier7110f442019-03-19 19:19:44 +0000848 template <class _Up>
849 _LIBCPP_INLINE_VISIBILITY
850 inline void __push_back_slow_path(_Up& __x);
Howard Hinnantb6c49562012-02-26 15:30:12 +0000851#endif
Eric Fiseliered9e9362017-04-16 02:40:45 +0000852
Marshall Clow2cd9d372014-05-08 14:14:06 +0000853 // The following functions are no-ops outside of AddressSanitizer mode.
854 // We call annotatations only for the default Allocator because other allocators
855 // may not meet the AddressSanitizer alignment constraints.
856 // See the documentation for __sanitizer_annotate_contiguous_container for more details.
Marshall Clow2cd9d372014-05-08 14:14:06 +0000857#ifndef _LIBCPP_HAS_NO_ASAN
Eric Fiselier6003c772016-12-23 23:37:52 +0000858 void __annotate_contiguous_container(const void *__beg, const void *__end,
859 const void *__old_mid,
860 const void *__new_mid) const
861 {
862
Marshall Clow2cd9d372014-05-08 14:14:06 +0000863 if (__beg && is_same<allocator_type, __default_allocator_type>::value)
864 __sanitizer_annotate_contiguous_container(__beg, __end, __old_mid, __new_mid);
Marshall Clow2cd9d372014-05-08 14:14:06 +0000865 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000866#else
867 _LIBCPP_INLINE_VISIBILITY
868 void __annotate_contiguous_container(const void*, const void*, const void*,
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000869 const void*) const _NOEXCEPT {}
Eric Fiselier6003c772016-12-23 23:37:52 +0000870#endif
871 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000872 void __annotate_new(size_type __current_size) const _NOEXCEPT {
Marshall Clow2cd9d372014-05-08 14:14:06 +0000873 __annotate_contiguous_container(data(), data() + capacity(),
874 data() + capacity(), data() + __current_size);
875 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000876
877 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000878 void __annotate_delete() const _NOEXCEPT {
Marshall Clow2cd9d372014-05-08 14:14:06 +0000879 __annotate_contiguous_container(data(), data() + capacity(),
880 data() + size(), data() + capacity());
881 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000882
883 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000884 void __annotate_increase(size_type __n) const _NOEXCEPT
Marshall Clow2cd9d372014-05-08 14:14:06 +0000885 {
886 __annotate_contiguous_container(data(), data() + capacity(),
887 data() + size(), data() + size() + __n);
888 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000889
890 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000891 void __annotate_shrink(size_type __old_size) const _NOEXCEPT
Marshall Clow2cd9d372014-05-08 14:14:06 +0000892 {
893 __annotate_contiguous_container(data(), data() + capacity(),
894 data() + __old_size, data() + size());
895 }
Marshall Clowc78ffa52014-09-03 21:37:43 +0000896
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000897 struct _ConstructTransaction {
898 explicit _ConstructTransaction(vector &__v, size_type __n)
899 : __v_(__v), __pos_(__v.__end_), __new_end_(__v.__end_ + __n) {
900#ifndef _LIBCPP_HAS_NO_ASAN
901 __v_.__annotate_increase(__n);
902#endif
903 }
904 ~_ConstructTransaction() {
905 __v_.__end_ = __pos_;
906#ifndef _LIBCPP_HAS_NO_ASAN
907 if (__pos_ != __new_end_) {
908 __v_.__annotate_shrink(__new_end_ - __v_.__begin_);
909 }
910#endif
911 }
912
913 vector &__v_;
914 pointer __pos_;
915 const_pointer const __new_end_;
916
917 private:
918 _ConstructTransaction(_ConstructTransaction const&) = delete;
919 _ConstructTransaction& operator=(_ConstructTransaction const&) = delete;
920 };
921
922 template <class ..._Args>
923 _LIBCPP_INLINE_VISIBILITY
924 void __construct_one_at_end(_Args&& ...__args) {
925 _ConstructTransaction __tx(*this, 1);
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500926 __alloc_traits::construct(this->__alloc(), _VSTD::__to_address(__tx.__pos_),
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000927 _VSTD::forward<_Args>(__args)...);
928 ++__tx.__pos_;
929 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000930};
931
Marshall Clowf0ca1492018-05-21 21:30:12 +0000932#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
933template<class _InputIterator,
Arthur O'Dwyer56226762021-03-03 23:02:20 -0500934 class _Alloc = allocator<__iter_value_type<_InputIterator>>,
935 class = _EnableIf<__is_allocator<_Alloc>::value>
Marshall Clowf0ca1492018-05-21 21:30:12 +0000936 >
937vector(_InputIterator, _InputIterator)
Arthur O'Dwyer56226762021-03-03 23:02:20 -0500938 -> vector<__iter_value_type<_InputIterator>, _Alloc>;
Marshall Clowf0ca1492018-05-21 21:30:12 +0000939
940template<class _InputIterator,
941 class _Alloc,
Arthur O'Dwyer56226762021-03-03 23:02:20 -0500942 class = _EnableIf<__is_allocator<_Alloc>::value>
Marshall Clowf0ca1492018-05-21 21:30:12 +0000943 >
944vector(_InputIterator, _InputIterator, _Alloc)
Arthur O'Dwyer56226762021-03-03 23:02:20 -0500945 -> vector<__iter_value_type<_InputIterator>, _Alloc>;
Marshall Clowf0ca1492018-05-21 21:30:12 +0000946#endif
947
Howard Hinnantc51e1022010-05-11 19:42:16 +0000948template <class _Tp, class _Allocator>
949void
950vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v)
951{
Eric Fiselier909fe962019-09-13 16:09:33 +0000952
Marshall Clow2cd9d372014-05-08 14:14:06 +0000953 __annotate_delete();
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -0500954 _VSTD::__construct_backward_with_exception_guarantees(this->__alloc(), this->__begin_, this->__end_, __v.__begin_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000955 _VSTD::swap(this->__begin_, __v.__begin_);
956 _VSTD::swap(this->__end_, __v.__end_);
957 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000958 __v.__first_ = __v.__begin_;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000959 __annotate_new(size());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000960 __invalidate_all_iterators();
961}
962
963template <class _Tp, class _Allocator>
964typename vector<_Tp, _Allocator>::pointer
965vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p)
966{
Marshall Clow2cd9d372014-05-08 14:14:06 +0000967 __annotate_delete();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000968 pointer __r = __v.__begin_;
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -0500969 _VSTD::__construct_backward_with_exception_guarantees(this->__alloc(), this->__begin_, __p, __v.__begin_);
970 _VSTD::__construct_forward_with_exception_guarantees(this->__alloc(), __p, this->__end_, __v.__end_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000971 _VSTD::swap(this->__begin_, __v.__begin_);
972 _VSTD::swap(this->__end_, __v.__end_);
973 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000974 __v.__first_ = __v.__begin_;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000975 __annotate_new(size());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000976 __invalidate_all_iterators();
977 return __r;
978}
979
980// Allocate space for __n objects
981// throws length_error if __n > max_size()
982// throws (probably bad_alloc) if memory run out
983// Precondition: __begin_ == __end_ == __end_cap() == 0
984// Precondition: __n > 0
985// Postcondition: capacity() == __n
986// Postcondition: size() == 0
987template <class _Tp, class _Allocator>
988void
Marshall Clow3ff48e02018-05-22 16:20:28 +0000989vector<_Tp, _Allocator>::__vallocate(size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000990{
991 if (__n > max_size())
992 this->__throw_length_error();
993 this->__begin_ = this->__end_ = __alloc_traits::allocate(this->__alloc(), __n);
994 this->__end_cap() = this->__begin_ + __n;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000995 __annotate_new(0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000996}
997
998template <class _Tp, class _Allocator>
999void
Marshall Clow3ff48e02018-05-22 16:20:28 +00001000vector<_Tp, _Allocator>::__vdeallocate() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001001{
Howard Hinnant76053d72013-06-27 19:35:32 +00001002 if (this->__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001003 {
1004 clear();
1005 __alloc_traits::deallocate(this->__alloc(), this->__begin_, capacity());
Howard Hinnant76053d72013-06-27 19:35:32 +00001006 this->__begin_ = this->__end_ = this->__end_cap() = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001007 }
1008}
1009
1010template <class _Tp, class _Allocator>
1011typename vector<_Tp, _Allocator>::size_type
Howard Hinnant1c936782011-06-03 19:40:40 +00001012vector<_Tp, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001013{
Eric Fiselierb5d9f442016-11-23 01:18:56 +00001014 return _VSTD::min<size_type>(__alloc_traits::max_size(this->__alloc()),
1015 numeric_limits<difference_type>::max());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001016}
1017
1018// Precondition: __new_size > capacity()
1019template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001020inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001021typename vector<_Tp, _Allocator>::size_type
1022vector<_Tp, _Allocator>::__recommend(size_type __new_size) const
1023{
1024 const size_type __ms = max_size();
1025 if (__new_size > __ms)
1026 this->__throw_length_error();
1027 const size_type __cap = capacity();
1028 if (__cap >= __ms / 2)
1029 return __ms;
Alexis Hunt991d29b2011-07-29 23:31:58 +00001030 return _VSTD::max<size_type>(2*__cap, __new_size);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001031}
1032
1033// Default constructs __n objects starting at __end_
1034// throws if construction throws
1035// Precondition: __n > 0
1036// Precondition: size() + __n <= capacity()
1037// Postcondition: size() == size() + __n
1038template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001039void
1040vector<_Tp, _Allocator>::__construct_at_end(size_type __n)
1041{
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001042 _ConstructTransaction __tx(*this, __n);
Martijn Vels1e2e1b82020-06-18 13:14:02 -04001043 const_pointer __new_end = __tx.__new_end_;
1044 for (pointer __pos = __tx.__pos_; __pos != __new_end; ++__pos, __tx.__pos_ = __pos) {
1045 __alloc_traits::construct(this->__alloc(), _VSTD::__to_address(__pos));
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001046 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001047}
1048
Howard Hinnantc51e1022010-05-11 19:42:16 +00001049// Copy constructs __n objects starting at __end_ from __x
1050// throws if construction throws
1051// Precondition: __n > 0
1052// Precondition: size() + __n <= capacity()
1053// Postcondition: size() == old size() + __n
1054// Postcondition: [i] == __x for all i in [size() - __n, __n)
1055template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001056inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001057void
1058vector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
1059{
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001060 _ConstructTransaction __tx(*this, __n);
Martijn Vels1e2e1b82020-06-18 13:14:02 -04001061 const_pointer __new_end = __tx.__new_end_;
1062 for (pointer __pos = __tx.__pos_; __pos != __new_end; ++__pos, __tx.__pos_ = __pos) {
1063 __alloc_traits::construct(this->__alloc(), _VSTD::__to_address(__pos), __x);
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001064 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001065}
1066
1067template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001068template <class _ForwardIterator>
1069typename enable_if
1070<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001071 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001072 void
1073>::type
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001074vector<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001075{
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001076 _ConstructTransaction __tx(*this, __n);
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05001077 _VSTD::__construct_range_forward(this->__alloc(), __first, __last, __tx.__pos_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001078}
1079
1080// Default constructs __n objects starting at __end_
1081// throws if construction throws
1082// Postcondition: size() == size() + __n
Howard Hinnant1c936782011-06-03 19:40:40 +00001083// Exception safety: strong.
Howard Hinnantc51e1022010-05-11 19:42:16 +00001084template <class _Tp, class _Allocator>
1085void
1086vector<_Tp, _Allocator>::__append(size_type __n)
1087{
1088 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1089 this->__construct_at_end(__n);
1090 else
1091 {
1092 allocator_type& __a = this->__alloc();
1093 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1094 __v.__construct_at_end(__n);
1095 __swap_out_circular_buffer(__v);
1096 }
1097}
1098
1099// Default constructs __n objects starting at __end_
1100// throws if construction throws
1101// Postcondition: size() == size() + __n
Howard Hinnant1c936782011-06-03 19:40:40 +00001102// Exception safety: strong.
Howard Hinnantc51e1022010-05-11 19:42:16 +00001103template <class _Tp, class _Allocator>
1104void
1105vector<_Tp, _Allocator>::__append(size_type __n, const_reference __x)
1106{
1107 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1108 this->__construct_at_end(__n, __x);
1109 else
1110 {
1111 allocator_type& __a = this->__alloc();
1112 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1113 __v.__construct_at_end(__n, __x);
1114 __swap_out_circular_buffer(__v);
1115 }
1116}
1117
1118template <class _Tp, class _Allocator>
1119vector<_Tp, _Allocator>::vector(size_type __n)
1120{
Louis Dionneba400782020-10-02 15:02:52 -04001121#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001122 __get_db()->__insert_c(this);
1123#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001124 if (__n > 0)
1125 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001126 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001127 __construct_at_end(__n);
1128 }
1129}
1130
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00001131#if _LIBCPP_STD_VER > 11
1132template <class _Tp, class _Allocator>
1133vector<_Tp, _Allocator>::vector(size_type __n, const allocator_type& __a)
1134 : __base(__a)
1135{
Louis Dionneba400782020-10-02 15:02:52 -04001136#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00001137 __get_db()->__insert_c(this);
1138#endif
1139 if (__n > 0)
1140 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001141 __vallocate(__n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00001142 __construct_at_end(__n);
1143 }
1144}
1145#endif
1146
Howard Hinnantc51e1022010-05-11 19:42:16 +00001147template <class _Tp, class _Allocator>
Marshall Clowf0ca1492018-05-21 21:30:12 +00001148vector<_Tp, _Allocator>::vector(size_type __n, const value_type& __x)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001149{
Louis Dionneba400782020-10-02 15:02:52 -04001150#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001151 __get_db()->__insert_c(this);
1152#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001153 if (__n > 0)
1154 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001155 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001156 __construct_at_end(__n, __x);
1157 }
1158}
1159
1160template <class _Tp, class _Allocator>
Marshall Clowf0ca1492018-05-21 21:30:12 +00001161vector<_Tp, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001162 : __base(__a)
1163{
Louis Dionneba400782020-10-02 15:02:52 -04001164#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001165 __get_db()->__insert_c(this);
1166#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001167 if (__n > 0)
1168 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001169 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001170 __construct_at_end(__n, __x);
1171 }
1172}
1173
1174template <class _Tp, class _Allocator>
1175template <class _InputIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +00001176vector<_Tp, _Allocator>::vector(_InputIterator __first,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001177 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
1178 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001179 is_constructible<
1180 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +00001181 typename iterator_traits<_InputIterator>::reference>::value,
1182 _InputIterator>::type __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001183{
Louis Dionneba400782020-10-02 15:02:52 -04001184#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001185 __get_db()->__insert_c(this);
1186#endif
Howard Hinnant9cd22302011-09-16 18:41:29 +00001187 for (; __first != __last; ++__first)
Eric Fiselier96919722017-10-17 13:03:17 +00001188 __emplace_back(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001189}
1190
1191template <class _Tp, class _Allocator>
1192template <class _InputIterator>
1193vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001194 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
1195 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001196 is_constructible<
1197 value_type,
1198 typename iterator_traits<_InputIterator>::reference>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001199 : __base(__a)
1200{
Louis Dionneba400782020-10-02 15:02:52 -04001201#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001202 __get_db()->__insert_c(this);
1203#endif
Howard Hinnant9cd22302011-09-16 18:41:29 +00001204 for (; __first != __last; ++__first)
Eric Fiselier96919722017-10-17 13:03:17 +00001205 __emplace_back(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001206}
1207
1208template <class _Tp, class _Allocator>
1209template <class _ForwardIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +00001210vector<_Tp, _Allocator>::vector(_ForwardIterator __first,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001211 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001212 is_constructible<
1213 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +00001214 typename iterator_traits<_ForwardIterator>::reference>::value,
1215 _ForwardIterator>::type __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001216{
Louis Dionneba400782020-10-02 15:02:52 -04001217#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001218 __get_db()->__insert_c(this);
1219#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001220 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001221 if (__n > 0)
1222 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001223 __vallocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001224 __construct_at_end(__first, __last, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001225 }
1226}
1227
1228template <class _Tp, class _Allocator>
1229template <class _ForwardIterator>
1230vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001231 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001232 is_constructible<
1233 value_type,
1234 typename iterator_traits<_ForwardIterator>::reference>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001235 : __base(__a)
1236{
Louis Dionneba400782020-10-02 15:02:52 -04001237#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001238 __get_db()->__insert_c(this);
1239#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001240 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001241 if (__n > 0)
1242 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001243 __vallocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001244 __construct_at_end(__first, __last, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001245 }
1246}
1247
1248template <class _Tp, class _Allocator>
1249vector<_Tp, _Allocator>::vector(const vector& __x)
1250 : __base(__alloc_traits::select_on_container_copy_construction(__x.__alloc()))
1251{
Louis Dionneba400782020-10-02 15:02:52 -04001252#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001253 __get_db()->__insert_c(this);
1254#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001255 size_type __n = __x.size();
1256 if (__n > 0)
1257 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001258 __vallocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001259 __construct_at_end(__x.__begin_, __x.__end_, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001260 }
1261}
1262
1263template <class _Tp, class _Allocator>
1264vector<_Tp, _Allocator>::vector(const vector& __x, const allocator_type& __a)
1265 : __base(__a)
1266{
Louis Dionneba400782020-10-02 15:02:52 -04001267#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001268 __get_db()->__insert_c(this);
1269#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001270 size_type __n = __x.size();
1271 if (__n > 0)
1272 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001273 __vallocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001274 __construct_at_end(__x.__begin_, __x.__end_, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001275 }
1276}
1277
Eric Fiseliered9e9362017-04-16 02:40:45 +00001278#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001279
1280template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001281inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001282vector<_Tp, _Allocator>::vector(vector&& __x)
Marshall Clowe5108202015-07-14 14:46:32 +00001283#if _LIBCPP_STD_VER > 14
1284 _NOEXCEPT
1285#else
Howard Hinnant1c936782011-06-03 19:40:40 +00001286 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowe5108202015-07-14 14:46:32 +00001287#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001288 : __base(_VSTD::move(__x.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001289{
Louis Dionneba400782020-10-02 15:02:52 -04001290#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001291 __get_db()->__insert_c(this);
Howard Hinnanta1e60cd2011-09-19 16:34:29 +00001292 __get_db()->swap(this, &__x);
Howard Hinnant27e0e772011-09-14 18:33:51 +00001293#endif
Howard Hinnant9cd22302011-09-16 18:41:29 +00001294 this->__begin_ = __x.__begin_;
1295 this->__end_ = __x.__end_;
1296 this->__end_cap() = __x.__end_cap();
Howard Hinnant76053d72013-06-27 19:35:32 +00001297 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001298}
1299
1300template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001301inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001302vector<_Tp, _Allocator>::vector(vector&& __x, const allocator_type& __a)
1303 : __base(__a)
1304{
Louis Dionneba400782020-10-02 15:02:52 -04001305#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001306 __get_db()->__insert_c(this);
1307#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001308 if (__a == __x.__alloc())
1309 {
1310 this->__begin_ = __x.__begin_;
1311 this->__end_ = __x.__end_;
1312 this->__end_cap() = __x.__end_cap();
1313 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Louis Dionneba400782020-10-02 15:02:52 -04001314#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnanta1e60cd2011-09-19 16:34:29 +00001315 __get_db()->swap(this, &__x);
1316#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001317 }
1318 else
1319 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001320 typedef move_iterator<iterator> _Ip;
1321 assign(_Ip(__x.begin()), _Ip(__x.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001322 }
1323}
1324
1325template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001326inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001327vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il)
1328{
Louis Dionneba400782020-10-02 15:02:52 -04001329#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001330 __get_db()->__insert_c(this);
1331#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001332 if (__il.size() > 0)
1333 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001334 __vallocate(__il.size());
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001335 __construct_at_end(__il.begin(), __il.end(), __il.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001336 }
1337}
1338
1339template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001340inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001341vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
1342 : __base(__a)
1343{
Louis Dionneba400782020-10-02 15:02:52 -04001344#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001345 __get_db()->__insert_c(this);
1346#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001347 if (__il.size() > 0)
1348 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001349 __vallocate(__il.size());
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001350 __construct_at_end(__il.begin(), __il.end(), __il.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001351 }
1352}
1353
1354template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001355inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001356vector<_Tp, _Allocator>&
1357vector<_Tp, _Allocator>::operator=(vector&& __x)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001358 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001359{
1360 __move_assign(__x, integral_constant<bool,
1361 __alloc_traits::propagate_on_container_move_assignment::value>());
1362 return *this;
1363}
1364
1365template <class _Tp, class _Allocator>
1366void
1367vector<_Tp, _Allocator>::__move_assign(vector& __c, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001368 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001369{
1370 if (__base::__alloc() != __c.__alloc())
1371 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001372 typedef move_iterator<iterator> _Ip;
1373 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001374 }
1375 else
1376 __move_assign(__c, true_type());
1377}
1378
1379template <class _Tp, class _Allocator>
1380void
1381vector<_Tp, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00001382 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001383{
Marshall Clow3ff48e02018-05-22 16:20:28 +00001384 __vdeallocate();
Marshall Clow136d45c2014-07-21 15:11:13 +00001385 __base::__move_assign_alloc(__c); // this can throw
Howard Hinnantc51e1022010-05-11 19:42:16 +00001386 this->__begin_ = __c.__begin_;
1387 this->__end_ = __c.__end_;
1388 this->__end_cap() = __c.__end_cap();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001389 __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
Louis Dionneba400782020-10-02 15:02:52 -04001390#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnanta1e60cd2011-09-19 16:34:29 +00001391 __get_db()->swap(this, &__c);
1392#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001393}
1394
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001395#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001396
1397template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001398inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001399vector<_Tp, _Allocator>&
1400vector<_Tp, _Allocator>::operator=(const vector& __x)
1401{
1402 if (this != &__x)
1403 {
1404 __base::__copy_assign_alloc(__x);
1405 assign(__x.__begin_, __x.__end_);
1406 }
1407 return *this;
1408}
1409
1410template <class _Tp, class _Allocator>
1411template <class _InputIterator>
1412typename enable_if
1413<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001414 __is_cpp17_input_iterator <_InputIterator>::value &&
1415 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001416 is_constructible<
1417 _Tp,
1418 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001419 void
1420>::type
1421vector<_Tp, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
1422{
1423 clear();
1424 for (; __first != __last; ++__first)
Eric Fiselier96919722017-10-17 13:03:17 +00001425 __emplace_back(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001426}
1427
1428template <class _Tp, class _Allocator>
1429template <class _ForwardIterator>
1430typename enable_if
1431<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001432 __is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001433 is_constructible<
1434 _Tp,
1435 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001436 void
1437>::type
1438vector<_Tp, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
1439{
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001440 size_type __new_size = static_cast<size_type>(_VSTD::distance(__first, __last));
1441 if (__new_size <= capacity())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001442 {
1443 _ForwardIterator __mid = __last;
1444 bool __growing = false;
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001445 if (__new_size > size())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001446 {
1447 __growing = true;
1448 __mid = __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001449 _VSTD::advance(__mid, size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001450 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001451 pointer __m = _VSTD::copy(__first, __mid, this->__begin_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001452 if (__growing)
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001453 __construct_at_end(__mid, __last, __new_size - size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001454 else
1455 this->__destruct_at_end(__m);
1456 }
1457 else
1458 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001459 __vdeallocate();
1460 __vallocate(__recommend(__new_size));
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001461 __construct_at_end(__first, __last, __new_size);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001462 }
Eric Fiselier69c51982016-12-28 06:06:09 +00001463 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001464}
1465
1466template <class _Tp, class _Allocator>
1467void
1468vector<_Tp, _Allocator>::assign(size_type __n, const_reference __u)
1469{
1470 if (__n <= capacity())
1471 {
1472 size_type __s = size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001473 _VSTD::fill_n(this->__begin_, _VSTD::min(__n, __s), __u);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001474 if (__n > __s)
1475 __construct_at_end(__n - __s, __u);
1476 else
Howard Hinnant155c2af2010-05-24 17:49:41 +00001477 this->__destruct_at_end(this->__begin_ + __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001478 }
1479 else
1480 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001481 __vdeallocate();
1482 __vallocate(__recommend(static_cast<size_type>(__n)));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001483 __construct_at_end(__n, __u);
1484 }
Eric Fiselier69c51982016-12-28 06:06:09 +00001485 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001486}
1487
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001488template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001489inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001490typename vector<_Tp, _Allocator>::iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001491vector<_Tp, _Allocator>::__make_iter(pointer __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001492{
Louis Dionneba400782020-10-02 15:02:52 -04001493#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001494 return iterator(this, __p);
1495#else
1496 return iterator(__p);
1497#endif
1498}
1499
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001500template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001501inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001502typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001503vector<_Tp, _Allocator>::__make_iter(const_pointer __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001504{
Louis Dionneba400782020-10-02 15:02:52 -04001505#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001506 return const_iterator(this, __p);
1507#else
1508 return const_iterator(__p);
1509#endif
1510}
1511
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001512template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001513inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001514typename vector<_Tp, _Allocator>::iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001515vector<_Tp, _Allocator>::begin() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001516{
1517 return __make_iter(this->__begin_);
1518}
1519
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001520template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001521inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001522typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001523vector<_Tp, _Allocator>::begin() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001524{
1525 return __make_iter(this->__begin_);
1526}
1527
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001528template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001529inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001530typename vector<_Tp, _Allocator>::iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001531vector<_Tp, _Allocator>::end() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001532{
1533 return __make_iter(this->__end_);
1534}
1535
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001536template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001537inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001538typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001539vector<_Tp, _Allocator>::end() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001540{
1541 return __make_iter(this->__end_);
1542}
1543
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001544template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001545inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001546typename vector<_Tp, _Allocator>::reference
Marshall Clowd6470492019-03-15 00:29:35 +00001547vector<_Tp, _Allocator>::operator[](size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001548{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001549 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001550 return this->__begin_[__n];
1551}
1552
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001553template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001554inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001555typename vector<_Tp, _Allocator>::const_reference
Marshall Clowd6470492019-03-15 00:29:35 +00001556vector<_Tp, _Allocator>::operator[](size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001557{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001558 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001559 return this->__begin_[__n];
1560}
1561
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001562template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001563typename vector<_Tp, _Allocator>::reference
1564vector<_Tp, _Allocator>::at(size_type __n)
1565{
1566 if (__n >= size())
1567 this->__throw_out_of_range();
1568 return this->__begin_[__n];
1569}
1570
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001571template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001572typename vector<_Tp, _Allocator>::const_reference
1573vector<_Tp, _Allocator>::at(size_type __n) const
1574{
1575 if (__n >= size())
1576 this->__throw_out_of_range();
1577 return this->__begin_[__n];
1578}
1579
1580template <class _Tp, class _Allocator>
1581void
1582vector<_Tp, _Allocator>::reserve(size_type __n)
1583{
1584 if (__n > capacity())
1585 {
1586 allocator_type& __a = this->__alloc();
Howard Hinnant1c936782011-06-03 19:40:40 +00001587 __split_buffer<value_type, allocator_type&> __v(__n, size(), __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001588 __swap_out_circular_buffer(__v);
1589 }
1590}
1591
1592template <class _Tp, class _Allocator>
1593void
Howard Hinnant1c936782011-06-03 19:40:40 +00001594vector<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001595{
1596 if (capacity() > size())
1597 {
1598#ifndef _LIBCPP_NO_EXCEPTIONS
1599 try
1600 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001601#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001602 allocator_type& __a = this->__alloc();
Howard Hinnant1c936782011-06-03 19:40:40 +00001603 __split_buffer<value_type, allocator_type&> __v(size(), size(), __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001604 __swap_out_circular_buffer(__v);
1605#ifndef _LIBCPP_NO_EXCEPTIONS
1606 }
1607 catch (...)
1608 {
1609 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001610#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001611 }
1612}
1613
1614template <class _Tp, class _Allocator>
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001615template <class _Up>
1616void
Eric Fiseliered9e9362017-04-16 02:40:45 +00001617#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001618vector<_Tp, _Allocator>::__push_back_slow_path(_Up&& __x)
1619#else
1620vector<_Tp, _Allocator>::__push_back_slow_path(_Up& __x)
1621#endif
1622{
1623 allocator_type& __a = this->__alloc();
1624 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1625 // __v.push_back(_VSTD::forward<_Up>(__x));
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001626 __alloc_traits::construct(__a, _VSTD::__to_address(__v.__end_), _VSTD::forward<_Up>(__x));
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001627 __v.__end_++;
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001628 __swap_out_circular_buffer(__v);
1629}
1630
1631template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001632inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001633void
1634vector<_Tp, _Allocator>::push_back(const_reference __x)
1635{
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001636 if (this->__end_ != this->__end_cap())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001637 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001638 __construct_one_at_end(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001639 }
1640 else
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001641 __push_back_slow_path(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001642}
1643
Eric Fiseliered9e9362017-04-16 02:40:45 +00001644#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001645
1646template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001647inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001648void
1649vector<_Tp, _Allocator>::push_back(value_type&& __x)
1650{
1651 if (this->__end_ < this->__end_cap())
1652 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001653 __construct_one_at_end(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001654 }
1655 else
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001656 __push_back_slow_path(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001657}
1658
1659template <class _Tp, class _Allocator>
1660template <class... _Args>
1661void
Howard Hinnantb6c49562012-02-26 15:30:12 +00001662vector<_Tp, _Allocator>::__emplace_back_slow_path(_Args&&... __args)
1663{
1664 allocator_type& __a = this->__alloc();
1665 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1666// __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001667 __alloc_traits::construct(__a, _VSTD::__to_address(__v.__end_), _VSTD::forward<_Args>(__args)...);
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001668 __v.__end_++;
Howard Hinnantb6c49562012-02-26 15:30:12 +00001669 __swap_out_circular_buffer(__v);
1670}
1671
1672template <class _Tp, class _Allocator>
1673template <class... _Args>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001674inline
Marshall Clowea52cc42017-01-24 23:09:12 +00001675#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001676typename vector<_Tp, _Allocator>::reference
Marshall Clowea52cc42017-01-24 23:09:12 +00001677#else
1678void
1679#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001680vector<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1681{
1682 if (this->__end_ < this->__end_cap())
1683 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001684 __construct_one_at_end(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001685 }
1686 else
Howard Hinnantb6c49562012-02-26 15:30:12 +00001687 __emplace_back_slow_path(_VSTD::forward<_Args>(__args)...);
Marshall Clowea52cc42017-01-24 23:09:12 +00001688#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001689 return this->back();
Marshall Clowea52cc42017-01-24 23:09:12 +00001690#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001691}
1692
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001693#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001694
1695template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001696inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001697void
1698vector<_Tp, _Allocator>::pop_back()
1699{
Kristina Bessonovaaeeaa7e2021-05-09 19:29:56 +02001700 _LIBCPP_ASSERT(!empty(), "vector::pop_back called on an empty vector");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001701 this->__destruct_at_end(this->__end_ - 1);
1702}
1703
1704template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001705inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001706typename vector<_Tp, _Allocator>::iterator
1707vector<_Tp, _Allocator>::erase(const_iterator __position)
1708{
Louis Dionneba400782020-10-02 15:02:52 -04001709#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001710 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1711 "vector::erase(iterator) called with an iterator not"
1712 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001713#endif
Howard Hinnant58d52d52013-03-25 22:12:26 +00001714 _LIBCPP_ASSERT(__position != end(),
1715 "vector::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnant76053d72013-06-27 19:35:32 +00001716 difference_type __ps = __position - cbegin();
1717 pointer __p = this->__begin_ + __ps;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001718 this->__destruct_at_end(_VSTD::move(__p + 1, this->__end_, __p));
Eric Fiselier69c51982016-12-28 06:06:09 +00001719 this->__invalidate_iterators_past(__p-1);
1720 iterator __r = __make_iter(__p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001721 return __r;
1722}
1723
1724template <class _Tp, class _Allocator>
1725typename vector<_Tp, _Allocator>::iterator
1726vector<_Tp, _Allocator>::erase(const_iterator __first, const_iterator __last)
1727{
Louis Dionneba400782020-10-02 15:02:52 -04001728#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001729 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
1730 "vector::erase(iterator, iterator) called with an iterator not"
1731 " referring to this vector");
Eric Fiselier69c51982016-12-28 06:06:09 +00001732 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__last) == this,
1733 "vector::erase(iterator, iterator) called with an iterator not"
1734 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001735#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001736 _LIBCPP_ASSERT(__first <= __last, "vector::erase(first, last) called with invalid range");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001737 pointer __p = this->__begin_ + (__first - begin());
Eric Fiselier69c51982016-12-28 06:06:09 +00001738 if (__first != __last) {
Howard Hinnantc580cc32013-04-18 15:02:57 +00001739 this->__destruct_at_end(_VSTD::move(__p + (__last - __first), this->__end_, __p));
Eric Fiselier69c51982016-12-28 06:06:09 +00001740 this->__invalidate_iterators_past(__p - 1);
1741 }
1742 iterator __r = __make_iter(__p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001743 return __r;
1744}
1745
1746template <class _Tp, class _Allocator>
1747void
1748vector<_Tp, _Allocator>::__move_range(pointer __from_s, pointer __from_e, pointer __to)
1749{
1750 pointer __old_last = this->__end_;
1751 difference_type __n = __old_last - __to;
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001752 {
1753 pointer __i = __from_s + __n;
1754 _ConstructTransaction __tx(*this, __from_e - __i);
Martijn Vels1e2e1b82020-06-18 13:14:02 -04001755 for (pointer __pos = __tx.__pos_; __i < __from_e;
1756 ++__i, ++__pos, __tx.__pos_ = __pos) {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001757 __alloc_traits::construct(this->__alloc(),
Martijn Vels1e2e1b82020-06-18 13:14:02 -04001758 _VSTD::__to_address(__pos),
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001759 _VSTD::move(*__i));
1760 }
1761 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001762 _VSTD::move_backward(__from_s, __from_s + __n, __old_last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001763}
1764
1765template <class _Tp, class _Allocator>
1766typename vector<_Tp, _Allocator>::iterator
1767vector<_Tp, _Allocator>::insert(const_iterator __position, const_reference __x)
1768{
Louis Dionneba400782020-10-02 15:02:52 -04001769#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001770 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1771 "vector::insert(iterator, x) called with an iterator not"
1772 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001773#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001774 pointer __p = this->__begin_ + (__position - begin());
1775 if (this->__end_ < this->__end_cap())
1776 {
1777 if (__p == this->__end_)
1778 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001779 __construct_one_at_end(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001780 }
1781 else
1782 {
1783 __move_range(__p, this->__end_, __p + 1);
1784 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1785 if (__p <= __xr && __xr < this->__end_)
1786 ++__xr;
1787 *__p = *__xr;
1788 }
1789 }
1790 else
1791 {
1792 allocator_type& __a = this->__alloc();
1793 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
1794 __v.push_back(__x);
1795 __p = __swap_out_circular_buffer(__v, __p);
1796 }
1797 return __make_iter(__p);
1798}
1799
Eric Fiseliered9e9362017-04-16 02:40:45 +00001800#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001801
1802template <class _Tp, class _Allocator>
1803typename vector<_Tp, _Allocator>::iterator
1804vector<_Tp, _Allocator>::insert(const_iterator __position, value_type&& __x)
1805{
Louis Dionneba400782020-10-02 15:02:52 -04001806#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001807 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1808 "vector::insert(iterator, x) called with an iterator not"
1809 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001810#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001811 pointer __p = this->__begin_ + (__position - begin());
1812 if (this->__end_ < this->__end_cap())
1813 {
1814 if (__p == this->__end_)
1815 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001816 __construct_one_at_end(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001817 }
1818 else
1819 {
1820 __move_range(__p, this->__end_, __p + 1);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001821 *__p = _VSTD::move(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001822 }
1823 }
1824 else
1825 {
1826 allocator_type& __a = this->__alloc();
1827 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001828 __v.push_back(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001829 __p = __swap_out_circular_buffer(__v, __p);
1830 }
1831 return __make_iter(__p);
1832}
1833
1834template <class _Tp, class _Allocator>
1835template <class... _Args>
1836typename vector<_Tp, _Allocator>::iterator
1837vector<_Tp, _Allocator>::emplace(const_iterator __position, _Args&&... __args)
1838{
Louis Dionneba400782020-10-02 15:02:52 -04001839#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001840 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1841 "vector::emplace(iterator, x) called with an iterator not"
1842 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001843#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001844 pointer __p = this->__begin_ + (__position - begin());
1845 if (this->__end_ < this->__end_cap())
1846 {
1847 if (__p == this->__end_)
1848 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001849 __construct_one_at_end(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001850 }
1851 else
1852 {
Marshall Clowa591b9a2016-07-11 21:38:08 +00001853 __temp_value<value_type, _Allocator> __tmp(this->__alloc(), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001854 __move_range(__p, this->__end_, __p + 1);
Marshall Clowa591b9a2016-07-11 21:38:08 +00001855 *__p = _VSTD::move(__tmp.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001856 }
1857 }
1858 else
1859 {
1860 allocator_type& __a = this->__alloc();
1861 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001862 __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001863 __p = __swap_out_circular_buffer(__v, __p);
1864 }
1865 return __make_iter(__p);
1866}
1867
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001868#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001869
1870template <class _Tp, class _Allocator>
1871typename vector<_Tp, _Allocator>::iterator
1872vector<_Tp, _Allocator>::insert(const_iterator __position, size_type __n, const_reference __x)
1873{
Louis Dionneba400782020-10-02 15:02:52 -04001874#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001875 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1876 "vector::insert(iterator, n, x) called with an iterator not"
1877 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001878#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001879 pointer __p = this->__begin_ + (__position - begin());
1880 if (__n > 0)
1881 {
1882 if (__n <= static_cast<size_type>(this->__end_cap() - this->__end_))
1883 {
1884 size_type __old_n = __n;
1885 pointer __old_last = this->__end_;
1886 if (__n > static_cast<size_type>(this->__end_ - __p))
1887 {
1888 size_type __cx = __n - (this->__end_ - __p);
1889 __construct_at_end(__cx, __x);
1890 __n -= __cx;
1891 }
1892 if (__n > 0)
1893 {
1894 __move_range(__p, __old_last, __p + __old_n);
1895 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1896 if (__p <= __xr && __xr < this->__end_)
1897 __xr += __old_n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001898 _VSTD::fill_n(__p, __n, *__xr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001899 }
1900 }
1901 else
1902 {
1903 allocator_type& __a = this->__alloc();
1904 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1905 __v.__construct_at_end(__n, __x);
1906 __p = __swap_out_circular_buffer(__v, __p);
1907 }
1908 }
1909 return __make_iter(__p);
1910}
1911
1912template <class _Tp, class _Allocator>
1913template <class _InputIterator>
1914typename enable_if
1915<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001916 __is_cpp17_input_iterator <_InputIterator>::value &&
1917 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001918 is_constructible<
1919 _Tp,
1920 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001921 typename vector<_Tp, _Allocator>::iterator
1922>::type
1923vector<_Tp, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
1924{
Louis Dionneba400782020-10-02 15:02:52 -04001925#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001926 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1927 "vector::insert(iterator, range) called with an iterator not"
1928 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001929#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001930 difference_type __off = __position - begin();
1931 pointer __p = this->__begin_ + __off;
1932 allocator_type& __a = this->__alloc();
1933 pointer __old_last = this->__end_;
1934 for (; this->__end_ != this->__end_cap() && __first != __last; ++__first)
1935 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001936 __construct_one_at_end(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001937 }
1938 __split_buffer<value_type, allocator_type&> __v(__a);
1939 if (__first != __last)
1940 {
1941#ifndef _LIBCPP_NO_EXCEPTIONS
1942 try
1943 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001944#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001945 __v.__construct_at_end(__first, __last);
1946 difference_type __old_size = __old_last - this->__begin_;
1947 difference_type __old_p = __p - this->__begin_;
1948 reserve(__recommend(size() + __v.size()));
1949 __p = this->__begin_ + __old_p;
1950 __old_last = this->__begin_ + __old_size;
1951#ifndef _LIBCPP_NO_EXCEPTIONS
1952 }
1953 catch (...)
1954 {
1955 erase(__make_iter(__old_last), end());
1956 throw;
1957 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001958#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001959 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001960 __p = _VSTD::rotate(__p, __old_last, this->__end_);
Louis Dionne202a4cb2020-02-11 10:59:12 +01001961 insert(__make_iter(__p), _VSTD::make_move_iterator(__v.begin()),
1962 _VSTD::make_move_iterator(__v.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001963 return begin() + __off;
1964}
1965
1966template <class _Tp, class _Allocator>
1967template <class _ForwardIterator>
1968typename enable_if
1969<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001970 __is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001971 is_constructible<
1972 _Tp,
1973 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001974 typename vector<_Tp, _Allocator>::iterator
1975>::type
1976vector<_Tp, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
1977{
Louis Dionneba400782020-10-02 15:02:52 -04001978#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001979 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1980 "vector::insert(iterator, range) called with an iterator not"
1981 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001982#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001983 pointer __p = this->__begin_ + (__position - begin());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001984 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001985 if (__n > 0)
1986 {
1987 if (__n <= this->__end_cap() - this->__end_)
1988 {
1989 size_type __old_n = __n;
1990 pointer __old_last = this->__end_;
1991 _ForwardIterator __m = __last;
1992 difference_type __dx = this->__end_ - __p;
1993 if (__n > __dx)
1994 {
1995 __m = __first;
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001996 difference_type __diff = this->__end_ - __p;
1997 _VSTD::advance(__m, __diff);
1998 __construct_at_end(__m, __last, __n - __diff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001999 __n = __dx;
2000 }
2001 if (__n > 0)
2002 {
2003 __move_range(__p, __old_last, __p + __old_n);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002004 _VSTD::copy(__first, __m, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002005 }
2006 }
2007 else
2008 {
2009 allocator_type& __a = this->__alloc();
2010 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
2011 __v.__construct_at_end(__first, __last);
2012 __p = __swap_out_circular_buffer(__v, __p);
2013 }
2014 }
2015 return __make_iter(__p);
2016}
2017
2018template <class _Tp, class _Allocator>
2019void
2020vector<_Tp, _Allocator>::resize(size_type __sz)
2021{
2022 size_type __cs = size();
2023 if (__cs < __sz)
2024 this->__append(__sz - __cs);
2025 else if (__cs > __sz)
2026 this->__destruct_at_end(this->__begin_ + __sz);
2027}
2028
2029template <class _Tp, class _Allocator>
2030void
2031vector<_Tp, _Allocator>::resize(size_type __sz, const_reference __x)
2032{
2033 size_type __cs = size();
2034 if (__cs < __sz)
2035 this->__append(__sz - __cs, __x);
2036 else if (__cs > __sz)
2037 this->__destruct_at_end(this->__begin_ + __sz);
2038}
2039
2040template <class _Tp, class _Allocator>
2041void
2042vector<_Tp, _Allocator>::swap(vector& __x)
Marshall Clow8982dcd2015-07-13 20:04:56 +00002043#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00002044 _NOEXCEPT
Marshall Clow8982dcd2015-07-13 20:04:56 +00002045#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00002046 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00002047 __is_nothrow_swappable<allocator_type>::value)
2048#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002049{
Howard Hinnant27e0e772011-09-14 18:33:51 +00002050 _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
2051 this->__alloc() == __x.__alloc(),
2052 "vector::swap: Either propagate_on_container_swap must be true"
2053 " or the allocators must compare equal");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002054 _VSTD::swap(this->__begin_, __x.__begin_);
2055 _VSTD::swap(this->__end_, __x.__end_);
2056 _VSTD::swap(this->__end_cap(), __x.__end_cap());
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05002057 _VSTD::__swap_allocator(this->__alloc(), __x.__alloc(),
Marshall Clow8982dcd2015-07-13 20:04:56 +00002058 integral_constant<bool,__alloc_traits::propagate_on_container_swap::value>());
Louis Dionneba400782020-10-02 15:02:52 -04002059#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002060 __get_db()->swap(this, &__x);
Louis Dionneba400782020-10-02 15:02:52 -04002061#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002062}
2063
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002064template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002065bool
2066vector<_Tp, _Allocator>::__invariants() const
2067{
Howard Hinnant76053d72013-06-27 19:35:32 +00002068 if (this->__begin_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002069 {
Howard Hinnant76053d72013-06-27 19:35:32 +00002070 if (this->__end_ != nullptr || this->__end_cap() != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002071 return false;
2072 }
2073 else
2074 {
2075 if (this->__begin_ > this->__end_)
2076 return false;
2077 if (this->__begin_ == this->__end_cap())
2078 return false;
2079 if (this->__end_ > this->__end_cap())
2080 return false;
2081 }
2082 return true;
2083}
2084
Louis Dionneba400782020-10-02 15:02:52 -04002085#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002086
Howard Hinnantc51e1022010-05-11 19:42:16 +00002087template <class _Tp, class _Allocator>
Howard Hinnant27e0e772011-09-14 18:33:51 +00002088bool
2089vector<_Tp, _Allocator>::__dereferenceable(const const_iterator* __i) const
2090{
2091 return this->__begin_ <= __i->base() && __i->base() < this->__end_;
2092}
2093
2094template <class _Tp, class _Allocator>
2095bool
2096vector<_Tp, _Allocator>::__decrementable(const const_iterator* __i) const
2097{
2098 return this->__begin_ < __i->base() && __i->base() <= this->__end_;
2099}
2100
2101template <class _Tp, class _Allocator>
2102bool
2103vector<_Tp, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
2104{
2105 const_pointer __p = __i->base() + __n;
2106 return this->__begin_ <= __p && __p <= this->__end_;
2107}
2108
2109template <class _Tp, class _Allocator>
2110bool
2111vector<_Tp, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
2112{
2113 const_pointer __p = __i->base() + __n;
2114 return this->__begin_ <= __p && __p < this->__end_;
2115}
2116
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002117#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002118
2119template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002120inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002121void
2122vector<_Tp, _Allocator>::__invalidate_all_iterators()
2123{
Louis Dionneba400782020-10-02 15:02:52 -04002124#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002125 __get_db()->__invalidate_all(this);
Louis Dionneba400782020-10-02 15:02:52 -04002126#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002127}
2128
Eric Fiselier69c51982016-12-28 06:06:09 +00002129
2130template <class _Tp, class _Allocator>
2131inline _LIBCPP_INLINE_VISIBILITY
2132void
2133vector<_Tp, _Allocator>::__invalidate_iterators_past(pointer __new_last) {
Louis Dionneba400782020-10-02 15:02:52 -04002134#if _LIBCPP_DEBUG_LEVEL == 2
Eric Fiselier69c51982016-12-28 06:06:09 +00002135 __c_node* __c = __get_db()->__find_c_and_lock(this);
2136 for (__i_node** __p = __c->end_; __p != __c->beg_; ) {
2137 --__p;
2138 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
2139 if (__i->base() > __new_last) {
2140 (*__p)->__c_ = nullptr;
2141 if (--__c->end_ != __p)
Arthur O'Dwyer22236632020-12-07 21:50:15 -05002142 _VSTD::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
Eric Fiselier69c51982016-12-28 06:06:09 +00002143 }
2144 }
2145 __get_db()->unlock();
2146#else
2147 ((void)__new_last);
2148#endif
2149}
2150
Howard Hinnantc51e1022010-05-11 19:42:16 +00002151// vector<bool>
2152
2153template <class _Allocator> class vector<bool, _Allocator>;
2154
2155template <class _Allocator> struct hash<vector<bool, _Allocator> >;
2156
2157template <class _Allocator>
Howard Hinnantb4ef6482011-07-02 20:33:23 +00002158struct __has_storage_type<vector<bool, _Allocator> >
2159{
2160 static const bool value = true;
2161};
2162
2163template <class _Allocator>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002164class _LIBCPP_TEMPLATE_VIS vector<bool, _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002165 : private __vector_base_common<true>
2166{
2167public:
2168 typedef vector __self;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002169 typedef bool value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002170 typedef _Allocator allocator_type;
2171 typedef allocator_traits<allocator_type> __alloc_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002172 typedef typename __alloc_traits::size_type size_type;
2173 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant896b7622012-05-07 16:50:38 +00002174 typedef size_type __storage_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002175 typedef __bit_iterator<vector, false> pointer;
2176 typedef __bit_iterator<vector, true> const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002177 typedef pointer iterator;
2178 typedef const_pointer const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002179 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
2180 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002181
2182private:
Marshall Clow940e01c2015-04-07 05:21:38 +00002183 typedef typename __rebind_alloc_helper<__alloc_traits, __storage_type>::type __storage_allocator;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002184 typedef allocator_traits<__storage_allocator> __storage_traits;
2185 typedef typename __storage_traits::pointer __storage_pointer;
2186 typedef typename __storage_traits::const_pointer __const_storage_pointer;
2187
2188 __storage_pointer __begin_;
2189 size_type __size_;
2190 __compressed_pair<size_type, __storage_allocator> __cap_alloc_;
Howard Hinnant71d1c192011-07-09 15:50:42 +00002191public:
Howard Hinnantb4ef6482011-07-02 20:33:23 +00002192 typedef __bit_reference<vector> reference;
2193 typedef __bit_const_reference<vector> const_reference;
Howard Hinnant71d1c192011-07-09 15:50:42 +00002194private:
Howard Hinnant1c936782011-06-03 19:40:40 +00002195 _LIBCPP_INLINE_VISIBILITY
2196 size_type& __cap() _NOEXCEPT
2197 {return __cap_alloc_.first();}
2198 _LIBCPP_INLINE_VISIBILITY
2199 const size_type& __cap() const _NOEXCEPT
2200 {return __cap_alloc_.first();}
2201 _LIBCPP_INLINE_VISIBILITY
2202 __storage_allocator& __alloc() _NOEXCEPT
2203 {return __cap_alloc_.second();}
2204 _LIBCPP_INLINE_VISIBILITY
2205 const __storage_allocator& __alloc() const _NOEXCEPT
2206 {return __cap_alloc_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002207
2208 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
2209
Howard Hinnant1c936782011-06-03 19:40:40 +00002210 _LIBCPP_INLINE_VISIBILITY
2211 static size_type __internal_cap_to_external(size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002212 {return __n * __bits_per_word;}
Howard Hinnant1c936782011-06-03 19:40:40 +00002213 _LIBCPP_INLINE_VISIBILITY
2214 static size_type __external_cap_to_internal(size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002215 {return (__n - 1) / __bits_per_word + 1;}
2216
2217public:
Howard Hinnant1c936782011-06-03 19:40:40 +00002218 _LIBCPP_INLINE_VISIBILITY
Marshall Clowe546cbb2015-06-04 02:05:41 +00002219 vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clow8f282f42015-06-04 00:10:20 +00002220
2221 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
2222#if _LIBCPP_STD_VER <= 14
2223 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
2224#else
2225 _NOEXCEPT;
2226#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002227 ~vector();
2228 explicit vector(size_type __n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00002229#if _LIBCPP_STD_VER > 11
2230 explicit vector(size_type __n, const allocator_type& __a);
2231#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002232 vector(size_type __n, const value_type& __v);
2233 vector(size_type __n, const value_type& __v, const allocator_type& __a);
2234 template <class _InputIterator>
2235 vector(_InputIterator __first, _InputIterator __last,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002236 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
2237 !__is_cpp17_forward_iterator<_InputIterator>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002238 template <class _InputIterator>
2239 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002240 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
2241 !__is_cpp17_forward_iterator<_InputIterator>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002242 template <class _ForwardIterator>
2243 vector(_ForwardIterator __first, _ForwardIterator __last,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002244 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002245 template <class _ForwardIterator>
2246 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002247 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002248
2249 vector(const vector& __v);
2250 vector(const vector& __v, const allocator_type& __a);
2251 vector& operator=(const vector& __v);
Eric Fiseliered9e9362017-04-16 02:40:45 +00002252
2253#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002254 vector(initializer_list<value_type> __il);
2255 vector(initializer_list<value_type> __il, const allocator_type& __a);
2256
Howard Hinnant1c936782011-06-03 19:40:40 +00002257 _LIBCPP_INLINE_VISIBILITY
2258 vector(vector&& __v)
Marshall Clowe5108202015-07-14 14:46:32 +00002259#if _LIBCPP_STD_VER > 14
2260 _NOEXCEPT;
2261#else
Howard Hinnant1c936782011-06-03 19:40:40 +00002262 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowe5108202015-07-14 14:46:32 +00002263#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002264 vector(vector&& __v, const allocator_type& __a);
Howard Hinnant1c936782011-06-03 19:40:40 +00002265 _LIBCPP_INLINE_VISIBILITY
2266 vector& operator=(vector&& __v)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002267 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiseliered9e9362017-04-16 02:40:45 +00002268
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002269 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002270 vector& operator=(initializer_list<value_type> __il)
2271 {assign(__il.begin(), __il.end()); return *this;}
Eric Fiseliered9e9362017-04-16 02:40:45 +00002272
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002273#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002274
2275 template <class _InputIterator>
2276 typename enable_if
2277 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002278 __is_cpp17_input_iterator<_InputIterator>::value &&
2279 !__is_cpp17_forward_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002280 void
2281 >::type
2282 assign(_InputIterator __first, _InputIterator __last);
2283 template <class _ForwardIterator>
2284 typename enable_if
2285 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002286 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002287 void
2288 >::type
2289 assign(_ForwardIterator __first, _ForwardIterator __last);
2290
2291 void assign(size_type __n, const value_type& __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +00002292
2293#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002294 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002295 void assign(initializer_list<value_type> __il)
2296 {assign(__il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +00002297#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002298
Howard Hinnant1c936782011-06-03 19:40:40 +00002299 _LIBCPP_INLINE_VISIBILITY allocator_type get_allocator() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002300 {return allocator_type(this->__alloc());}
2301
Howard Hinnant1c936782011-06-03 19:40:40 +00002302 size_type max_size() const _NOEXCEPT;
2303 _LIBCPP_INLINE_VISIBILITY
2304 size_type capacity() const _NOEXCEPT
2305 {return __internal_cap_to_external(__cap());}
2306 _LIBCPP_INLINE_VISIBILITY
2307 size_type size() const _NOEXCEPT
2308 {return __size_;}
Marshall Clow425f5752017-11-15 05:51:26 +00002309 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +00002310 bool empty() const _NOEXCEPT
2311 {return __size_ == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002312 void reserve(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +00002313 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002314
Howard Hinnant1c936782011-06-03 19:40:40 +00002315 _LIBCPP_INLINE_VISIBILITY
2316 iterator begin() _NOEXCEPT
2317 {return __make_iter(0);}
2318 _LIBCPP_INLINE_VISIBILITY
2319 const_iterator begin() const _NOEXCEPT
2320 {return __make_iter(0);}
2321 _LIBCPP_INLINE_VISIBILITY
2322 iterator end() _NOEXCEPT
2323 {return __make_iter(__size_);}
2324 _LIBCPP_INLINE_VISIBILITY
2325 const_iterator end() const _NOEXCEPT
2326 {return __make_iter(__size_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002327
Howard Hinnant1c936782011-06-03 19:40:40 +00002328 _LIBCPP_INLINE_VISIBILITY
2329 reverse_iterator rbegin() _NOEXCEPT
2330 {return reverse_iterator(end());}
2331 _LIBCPP_INLINE_VISIBILITY
2332 const_reverse_iterator rbegin() const _NOEXCEPT
2333 {return const_reverse_iterator(end());}
2334 _LIBCPP_INLINE_VISIBILITY
2335 reverse_iterator rend() _NOEXCEPT
2336 {return reverse_iterator(begin());}
2337 _LIBCPP_INLINE_VISIBILITY
2338 const_reverse_iterator rend() const _NOEXCEPT
2339 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002340
Howard Hinnant1c936782011-06-03 19:40:40 +00002341 _LIBCPP_INLINE_VISIBILITY
2342 const_iterator cbegin() const _NOEXCEPT
2343 {return __make_iter(0);}
2344 _LIBCPP_INLINE_VISIBILITY
2345 const_iterator cend() const _NOEXCEPT
2346 {return __make_iter(__size_);}
2347 _LIBCPP_INLINE_VISIBILITY
2348 const_reverse_iterator crbegin() const _NOEXCEPT
2349 {return rbegin();}
2350 _LIBCPP_INLINE_VISIBILITY
2351 const_reverse_iterator crend() const _NOEXCEPT
2352 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002353
2354 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) {return __make_ref(__n);}
2355 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const {return __make_ref(__n);}
2356 reference at(size_type __n);
2357 const_reference at(size_type __n) const;
2358
2359 _LIBCPP_INLINE_VISIBILITY reference front() {return __make_ref(0);}
2360 _LIBCPP_INLINE_VISIBILITY const_reference front() const {return __make_ref(0);}
2361 _LIBCPP_INLINE_VISIBILITY reference back() {return __make_ref(__size_ - 1);}
2362 _LIBCPP_INLINE_VISIBILITY const_reference back() const {return __make_ref(__size_ - 1);}
2363
2364 void push_back(const value_type& __x);
Marshall Clowc46bb8e2013-08-13 23:54:12 +00002365#if _LIBCPP_STD_VER > 11
2366 template <class... _Args>
Marshall Clowea52cc42017-01-24 23:09:12 +00002367#if _LIBCPP_STD_VER > 14
2368 _LIBCPP_INLINE_VISIBILITY reference emplace_back(_Args&&... __args)
2369#else
2370 _LIBCPP_INLINE_VISIBILITY void emplace_back(_Args&&... __args)
2371#endif
2372 {
Eric Fiselier34ba5b92016-07-21 03:20:17 +00002373 push_back ( value_type ( _VSTD::forward<_Args>(__args)... ));
Marshall Clowea52cc42017-01-24 23:09:12 +00002374#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00002375 return this->back();
Marshall Clowea52cc42017-01-24 23:09:12 +00002376#endif
Eric Fiselier34ba5b92016-07-21 03:20:17 +00002377 }
Marshall Clowc46bb8e2013-08-13 23:54:12 +00002378#endif
2379
Howard Hinnantc51e1022010-05-11 19:42:16 +00002380 _LIBCPP_INLINE_VISIBILITY void pop_back() {--__size_;}
2381
Marshall Clowc46bb8e2013-08-13 23:54:12 +00002382#if _LIBCPP_STD_VER > 11
2383 template <class... _Args>
2384 _LIBCPP_INLINE_VISIBILITY iterator emplace(const_iterator position, _Args&&... __args)
2385 { return insert ( position, value_type ( _VSTD::forward<_Args>(__args)... )); }
2386#endif
2387
Howard Hinnantc51e1022010-05-11 19:42:16 +00002388 iterator insert(const_iterator __position, const value_type& __x);
2389 iterator insert(const_iterator __position, size_type __n, const value_type& __x);
2390 iterator insert(const_iterator __position, size_type __n, const_reference __x);
2391 template <class _InputIterator>
2392 typename enable_if
2393 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002394 __is_cpp17_input_iterator <_InputIterator>::value &&
2395 !__is_cpp17_forward_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002396 iterator
2397 >::type
2398 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
2399 template <class _ForwardIterator>
2400 typename enable_if
2401 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002402 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002403 iterator
2404 >::type
2405 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiseliered9e9362017-04-16 02:40:45 +00002406
2407#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002408 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002409 iterator insert(const_iterator __position, initializer_list<value_type> __il)
2410 {return insert(__position, __il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +00002411#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002412
Howard Hinnantcf823322010-12-17 14:46:43 +00002413 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002414 iterator erase(const_iterator __first, const_iterator __last);
2415
Howard Hinnant1c936782011-06-03 19:40:40 +00002416 _LIBCPP_INLINE_VISIBILITY
2417 void clear() _NOEXCEPT {__size_ = 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002418
Howard Hinnant1c936782011-06-03 19:40:40 +00002419 void swap(vector&)
Marshall Clow8982dcd2015-07-13 20:04:56 +00002420#if _LIBCPP_STD_VER >= 14
2421 _NOEXCEPT;
2422#else
Eric Fiselier69c51982016-12-28 06:06:09 +00002423 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00002424 __is_nothrow_swappable<allocator_type>::value);
2425#endif
Marshall Clow89eb3822016-04-07 14:20:31 +00002426 static void swap(reference __x, reference __y) _NOEXCEPT { _VSTD::swap(__x, __y); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002427
2428 void resize(size_type __sz, value_type __x = false);
Howard Hinnant1c936782011-06-03 19:40:40 +00002429 void flip() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002430
2431 bool __invariants() const;
2432
2433private:
Howard Hinnantcf823322010-12-17 14:46:43 +00002434 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Marshall Clow3ff48e02018-05-22 16:20:28 +00002435 void __vallocate(size_type __n);
2436 void __vdeallocate() _NOEXCEPT;
Howard Hinnant1c936782011-06-03 19:40:40 +00002437 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantea382952013-08-14 18:00:20 +00002438 static size_type __align_it(size_type __new_size) _NOEXCEPT
Eric Fiselierb41db9a2018-10-01 01:59:37 +00002439 {return __new_size + (__bits_per_word-1) & ~((size_type)__bits_per_word-1);}
Howard Hinnantcf823322010-12-17 14:46:43 +00002440 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
2441 _LIBCPP_INLINE_VISIBILITY void __construct_at_end(size_type __n, bool __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002442 template <class _ForwardIterator>
2443 typename enable_if
2444 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002445 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002446 void
2447 >::type
2448 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
2449 void __append(size_type __n, const_reference __x);
Howard Hinnant1c936782011-06-03 19:40:40 +00002450 _LIBCPP_INLINE_VISIBILITY
2451 reference __make_ref(size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002452 {return reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnant1c936782011-06-03 19:40:40 +00002453 _LIBCPP_INLINE_VISIBILITY
2454 const_reference __make_ref(size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002455 {return const_reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnant1c936782011-06-03 19:40:40 +00002456 _LIBCPP_INLINE_VISIBILITY
2457 iterator __make_iter(size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002458 {return iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnant1c936782011-06-03 19:40:40 +00002459 _LIBCPP_INLINE_VISIBILITY
2460 const_iterator __make_iter(size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002461 {return const_iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnant1c936782011-06-03 19:40:40 +00002462 _LIBCPP_INLINE_VISIBILITY
2463 iterator __const_iterator_cast(const_iterator __p) _NOEXCEPT
Howard Hinnant76053d72013-06-27 19:35:32 +00002464 {return begin() + (__p - cbegin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002465
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002466 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002467 void __copy_assign_alloc(const vector& __v)
2468 {__copy_assign_alloc(__v, integral_constant<bool,
2469 __storage_traits::propagate_on_container_copy_assignment::value>());}
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002470 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002471 void __copy_assign_alloc(const vector& __c, true_type)
2472 {
2473 if (__alloc() != __c.__alloc())
Marshall Clow3ff48e02018-05-22 16:20:28 +00002474 __vdeallocate();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002475 __alloc() = __c.__alloc();
2476 }
2477
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002478 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00002479 void __copy_assign_alloc(const vector&, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002480 {}
2481
2482 void __move_assign(vector& __c, false_type);
Howard Hinnant1c936782011-06-03 19:40:40 +00002483 void __move_assign(vector& __c, true_type)
2484 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002485 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002486 void __move_assign_alloc(vector& __c)
Howard Hinnant1c936782011-06-03 19:40:40 +00002487 _NOEXCEPT_(
2488 !__storage_traits::propagate_on_container_move_assignment::value ||
2489 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002490 {__move_assign_alloc(__c, integral_constant<bool,
2491 __storage_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002492 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +00002493 void __move_assign_alloc(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002494 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002495 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002496 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002497 }
2498
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002499 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00002500 void __move_assign_alloc(vector&, false_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002501 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002502 {}
2503
Howard Hinnant1c936782011-06-03 19:40:40 +00002504 size_t __hash_code() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002505
2506 friend class __bit_reference<vector>;
2507 friend class __bit_const_reference<vector>;
2508 friend class __bit_iterator<vector, false>;
2509 friend class __bit_iterator<vector, true>;
Howard Hinnant4210a0f2012-08-17 17:10:18 +00002510 friend struct __bit_array<vector>;
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002511 friend struct _LIBCPP_TEMPLATE_VIS hash<vector>;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002512};
2513
2514template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002515inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002516void
2517vector<bool, _Allocator>::__invalidate_all_iterators()
2518{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002519}
2520
2521// Allocate space for __n objects
2522// throws length_error if __n > max_size()
2523// throws (probably bad_alloc) if memory run out
2524// Precondition: __begin_ == __end_ == __cap() == 0
2525// Precondition: __n > 0
2526// Postcondition: capacity() == __n
2527// Postcondition: size() == 0
2528template <class _Allocator>
2529void
Marshall Clow3ff48e02018-05-22 16:20:28 +00002530vector<bool, _Allocator>::__vallocate(size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002531{
2532 if (__n > max_size())
2533 this->__throw_length_error();
2534 __n = __external_cap_to_internal(__n);
2535 this->__begin_ = __storage_traits::allocate(this->__alloc(), __n);
2536 this->__size_ = 0;
2537 this->__cap() = __n;
2538}
2539
2540template <class _Allocator>
2541void
Marshall Clow3ff48e02018-05-22 16:20:28 +00002542vector<bool, _Allocator>::__vdeallocate() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002543{
Howard Hinnant76053d72013-06-27 19:35:32 +00002544 if (this->__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002545 {
2546 __storage_traits::deallocate(this->__alloc(), this->__begin_, __cap());
2547 __invalidate_all_iterators();
Howard Hinnant76053d72013-06-27 19:35:32 +00002548 this->__begin_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002549 this->__size_ = this->__cap() = 0;
2550 }
2551}
2552
2553template <class _Allocator>
2554typename vector<bool, _Allocator>::size_type
Howard Hinnant1c936782011-06-03 19:40:40 +00002555vector<bool, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002556{
2557 size_type __amax = __storage_traits::max_size(__alloc());
2558 size_type __nmax = numeric_limits<size_type>::max() / 2; // end() >= begin(), always
2559 if (__nmax / __bits_per_word <= __amax)
2560 return __nmax;
2561 return __internal_cap_to_external(__amax);
2562}
2563
2564// Precondition: __new_size > capacity()
2565template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002566inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002567typename vector<bool, _Allocator>::size_type
2568vector<bool, _Allocator>::__recommend(size_type __new_size) const
2569{
2570 const size_type __ms = max_size();
2571 if (__new_size > __ms)
2572 this->__throw_length_error();
2573 const size_type __cap = capacity();
2574 if (__cap >= __ms / 2)
2575 return __ms;
Howard Hinnantea382952013-08-14 18:00:20 +00002576 return _VSTD::max(2*__cap, __align_it(__new_size));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002577}
2578
2579// Default constructs __n objects starting at __end_
2580// Precondition: __n > 0
2581// Precondition: size() + __n <= capacity()
2582// Postcondition: size() == size() + __n
2583template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002584inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002585void
2586vector<bool, _Allocator>::__construct_at_end(size_type __n, bool __x)
2587{
2588 size_type __old_size = this->__size_;
2589 this->__size_ += __n;
Marshall Clow1893ec72018-10-23 20:07:45 +00002590 if (__old_size == 0 || ((__old_size - 1) / __bits_per_word) != ((this->__size_ - 1) / __bits_per_word))
2591 {
2592 if (this->__size_ <= __bits_per_word)
2593 this->__begin_[0] = __storage_type(0);
2594 else
2595 this->__begin_[(this->__size_ - 1) / __bits_per_word] = __storage_type(0);
2596 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002597 _VSTD::fill_n(__make_iter(__old_size), __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002598}
2599
2600template <class _Allocator>
2601template <class _ForwardIterator>
2602typename enable_if
2603<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002604 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002605 void
2606>::type
2607vector<bool, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
2608{
2609 size_type __old_size = this->__size_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002610 this->__size_ += _VSTD::distance(__first, __last);
Marshall Clow1893ec72018-10-23 20:07:45 +00002611 if (__old_size == 0 || ((__old_size - 1) / __bits_per_word) != ((this->__size_ - 1) / __bits_per_word))
2612 {
2613 if (this->__size_ <= __bits_per_word)
2614 this->__begin_[0] = __storage_type(0);
2615 else
2616 this->__begin_[(this->__size_ - 1) / __bits_per_word] = __storage_type(0);
2617 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002618 _VSTD::copy(__first, __last, __make_iter(__old_size));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002619}
2620
2621template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002622inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002623vector<bool, _Allocator>::vector()
Marshall Clowe546cbb2015-06-04 02:05:41 +00002624 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant76053d72013-06-27 19:35:32 +00002625 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002626 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002627 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002628{
2629}
2630
2631template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002632inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002633vector<bool, _Allocator>::vector(const allocator_type& __a)
Marshall Clow8f282f42015-06-04 00:10:20 +00002634#if _LIBCPP_STD_VER <= 14
2635 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
2636#else
2637 _NOEXCEPT
2638#endif
Howard Hinnant76053d72013-06-27 19:35:32 +00002639 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002640 __size_(0),
2641 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2642{
2643}
2644
2645template <class _Allocator>
2646vector<bool, _Allocator>::vector(size_type __n)
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 if (__n > 0)
2652 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002653 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002654 __construct_at_end(__n, false);
2655 }
2656}
2657
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00002658#if _LIBCPP_STD_VER > 11
2659template <class _Allocator>
2660vector<bool, _Allocator>::vector(size_type __n, const allocator_type& __a)
2661 : __begin_(nullptr),
2662 __size_(0),
2663 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2664{
2665 if (__n > 0)
2666 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002667 __vallocate(__n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00002668 __construct_at_end(__n, false);
2669 }
2670}
2671#endif
2672
Howard Hinnantc51e1022010-05-11 19:42:16 +00002673template <class _Allocator>
2674vector<bool, _Allocator>::vector(size_type __n, const value_type& __x)
Howard Hinnant76053d72013-06-27 19:35:32 +00002675 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002676 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002677 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002678{
2679 if (__n > 0)
2680 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002681 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002682 __construct_at_end(__n, __x);
2683 }
2684}
2685
2686template <class _Allocator>
2687vector<bool, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002688 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002689 __size_(0),
2690 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2691{
2692 if (__n > 0)
2693 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002694 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002695 __construct_at_end(__n, __x);
2696 }
2697}
2698
2699template <class _Allocator>
2700template <class _InputIterator>
2701vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002702 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
2703 !__is_cpp17_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002704 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002705 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002706 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002707{
2708#ifndef _LIBCPP_NO_EXCEPTIONS
2709 try
2710 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002711#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002712 for (; __first != __last; ++__first)
2713 push_back(*__first);
2714#ifndef _LIBCPP_NO_EXCEPTIONS
2715 }
2716 catch (...)
2717 {
Howard Hinnant76053d72013-06-27 19:35:32 +00002718 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002719 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2720 __invalidate_all_iterators();
2721 throw;
2722 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002723#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002724}
2725
2726template <class _Allocator>
2727template <class _InputIterator>
2728vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002729 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
2730 !__is_cpp17_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002731 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002732 __size_(0),
2733 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2734{
2735#ifndef _LIBCPP_NO_EXCEPTIONS
2736 try
2737 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002738#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002739 for (; __first != __last; ++__first)
2740 push_back(*__first);
2741#ifndef _LIBCPP_NO_EXCEPTIONS
2742 }
2743 catch (...)
2744 {
Howard Hinnant76053d72013-06-27 19:35:32 +00002745 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002746 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2747 __invalidate_all_iterators();
2748 throw;
2749 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002750#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002751}
2752
2753template <class _Allocator>
2754template <class _ForwardIterator>
2755vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002756 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002757 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002758 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002759 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002760{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002761 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002762 if (__n > 0)
2763 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002764 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002765 __construct_at_end(__first, __last);
2766 }
2767}
2768
2769template <class _Allocator>
2770template <class _ForwardIterator>
2771vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002772 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002773 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002774 __size_(0),
2775 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2776{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002777 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002778 if (__n > 0)
2779 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002780 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002781 __construct_at_end(__first, __last);
2782 }
2783}
2784
Eric Fiseliered9e9362017-04-16 02:40:45 +00002785#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002786
Howard Hinnantc51e1022010-05-11 19:42:16 +00002787template <class _Allocator>
2788vector<bool, _Allocator>::vector(initializer_list<value_type> __il)
Howard Hinnant76053d72013-06-27 19:35:32 +00002789 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002790 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002791 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002792{
2793 size_type __n = static_cast<size_type>(__il.size());
2794 if (__n > 0)
2795 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002796 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002797 __construct_at_end(__il.begin(), __il.end());
2798 }
2799}
2800
2801template <class _Allocator>
2802vector<bool, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002803 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002804 __size_(0),
2805 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2806{
2807 size_type __n = static_cast<size_type>(__il.size());
2808 if (__n > 0)
2809 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002810 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002811 __construct_at_end(__il.begin(), __il.end());
2812 }
2813}
2814
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002815#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002816
Howard Hinnantc51e1022010-05-11 19:42:16 +00002817template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002818vector<bool, _Allocator>::~vector()
2819{
Howard Hinnant76053d72013-06-27 19:35:32 +00002820 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002821 __storage_traits::deallocate(__alloc(), __begin_, __cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002822 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002823}
2824
2825template <class _Allocator>
2826vector<bool, _Allocator>::vector(const vector& __v)
Howard Hinnant76053d72013-06-27 19:35:32 +00002827 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002828 __size_(0),
2829 __cap_alloc_(0, __storage_traits::select_on_container_copy_construction(__v.__alloc()))
2830{
2831 if (__v.size() > 0)
2832 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002833 __vallocate(__v.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002834 __construct_at_end(__v.begin(), __v.end());
2835 }
2836}
2837
2838template <class _Allocator>
2839vector<bool, _Allocator>::vector(const vector& __v, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002840 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002841 __size_(0),
2842 __cap_alloc_(0, __a)
2843{
2844 if (__v.size() > 0)
2845 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002846 __vallocate(__v.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002847 __construct_at_end(__v.begin(), __v.end());
2848 }
2849}
2850
2851template <class _Allocator>
2852vector<bool, _Allocator>&
2853vector<bool, _Allocator>::operator=(const vector& __v)
2854{
2855 if (this != &__v)
2856 {
2857 __copy_assign_alloc(__v);
2858 if (__v.__size_)
2859 {
2860 if (__v.__size_ > capacity())
2861 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002862 __vdeallocate();
2863 __vallocate(__v.__size_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002864 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002865 _VSTD::copy(__v.__begin_, __v.__begin_ + __external_cap_to_internal(__v.__size_), __begin_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002866 }
2867 __size_ = __v.__size_;
2868 }
2869 return *this;
2870}
2871
Eric Fiseliered9e9362017-04-16 02:40:45 +00002872#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant74279a52010-09-04 23:28:19 +00002873
Howard Hinnantc51e1022010-05-11 19:42:16 +00002874template <class _Allocator>
Eric Fiselier4f1534c2018-06-05 22:32:52 +00002875inline _LIBCPP_INLINE_VISIBILITY vector<bool, _Allocator>::vector(vector&& __v)
Marshall Clowe5108202015-07-14 14:46:32 +00002876#if _LIBCPP_STD_VER > 14
Eric Fiselier4f1534c2018-06-05 22:32:52 +00002877 _NOEXCEPT
Marshall Clowe5108202015-07-14 14:46:32 +00002878#else
Eric Fiselier4f1534c2018-06-05 22:32:52 +00002879 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowe5108202015-07-14 14:46:32 +00002880#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002881 : __begin_(__v.__begin_),
2882 __size_(__v.__size_),
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05002883 __cap_alloc_(_VSTD::move(__v.__cap_alloc_)) {
Howard Hinnant76053d72013-06-27 19:35:32 +00002884 __v.__begin_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002885 __v.__size_ = 0;
2886 __v.__cap() = 0;
2887}
2888
2889template <class _Allocator>
2890vector<bool, _Allocator>::vector(vector&& __v, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002891 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002892 __size_(0),
2893 __cap_alloc_(0, __a)
2894{
2895 if (__a == allocator_type(__v.__alloc()))
2896 {
2897 this->__begin_ = __v.__begin_;
2898 this->__size_ = __v.__size_;
2899 this->__cap() = __v.__cap();
2900 __v.__begin_ = nullptr;
2901 __v.__cap() = __v.__size_ = 0;
2902 }
2903 else if (__v.size() > 0)
2904 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002905 __vallocate(__v.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002906 __construct_at_end(__v.begin(), __v.end());
2907 }
2908}
2909
2910template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002911inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002912vector<bool, _Allocator>&
2913vector<bool, _Allocator>::operator=(vector&& __v)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002914 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002915{
2916 __move_assign(__v, integral_constant<bool,
2917 __storage_traits::propagate_on_container_move_assignment::value>());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002918 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002919}
2920
2921template <class _Allocator>
2922void
2923vector<bool, _Allocator>::__move_assign(vector& __c, false_type)
2924{
2925 if (__alloc() != __c.__alloc())
2926 assign(__c.begin(), __c.end());
2927 else
2928 __move_assign(__c, true_type());
2929}
2930
2931template <class _Allocator>
2932void
2933vector<bool, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002934 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002935{
Marshall Clow3ff48e02018-05-22 16:20:28 +00002936 __vdeallocate();
Marshall Clowb4871fa2014-07-21 15:15:15 +00002937 __move_assign_alloc(__c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002938 this->__begin_ = __c.__begin_;
2939 this->__size_ = __c.__size_;
2940 this->__cap() = __c.__cap();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002941 __c.__begin_ = nullptr;
2942 __c.__cap() = __c.__size_ = 0;
2943}
Howard Hinnant74279a52010-09-04 23:28:19 +00002944
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002945#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002946
2947template <class _Allocator>
2948void
2949vector<bool, _Allocator>::assign(size_type __n, const value_type& __x)
2950{
2951 __size_ = 0;
2952 if (__n > 0)
2953 {
2954 size_type __c = capacity();
2955 if (__n <= __c)
2956 __size_ = __n;
2957 else
2958 {
2959 vector __v(__alloc());
2960 __v.reserve(__recommend(__n));
2961 __v.__size_ = __n;
2962 swap(__v);
2963 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002964 _VSTD::fill_n(begin(), __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002965 }
Eric Fiselier69c51982016-12-28 06:06:09 +00002966 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002967}
2968
2969template <class _Allocator>
2970template <class _InputIterator>
2971typename enable_if
2972<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002973 __is_cpp17_input_iterator<_InputIterator>::value &&
2974 !__is_cpp17_forward_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002975 void
2976>::type
2977vector<bool, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2978{
2979 clear();
2980 for (; __first != __last; ++__first)
2981 push_back(*__first);
2982}
2983
2984template <class _Allocator>
2985template <class _ForwardIterator>
2986typename enable_if
2987<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002988 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002989 void
2990>::type
2991vector<bool, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2992{
2993 clear();
Eric Fiselier6003c772016-12-23 23:37:52 +00002994 difference_type __ns = _VSTD::distance(__first, __last);
2995 _LIBCPP_ASSERT(__ns >= 0, "invalid range specified");
2996 const size_t __n = static_cast<size_type>(__ns);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002997 if (__n)
2998 {
2999 if (__n > capacity())
3000 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00003001 __vdeallocate();
3002 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003003 }
3004 __construct_at_end(__first, __last);
3005 }
3006}
3007
3008template <class _Allocator>
3009void
3010vector<bool, _Allocator>::reserve(size_type __n)
3011{
3012 if (__n > capacity())
3013 {
3014 vector __v(this->__alloc());
Marshall Clow3ff48e02018-05-22 16:20:28 +00003015 __v.__vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003016 __v.__construct_at_end(this->begin(), this->end());
3017 swap(__v);
3018 __invalidate_all_iterators();
3019 }
3020}
3021
3022template <class _Allocator>
3023void
Howard Hinnant1c936782011-06-03 19:40:40 +00003024vector<bool, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003025{
3026 if (__external_cap_to_internal(size()) > __cap())
3027 {
3028#ifndef _LIBCPP_NO_EXCEPTIONS
3029 try
3030 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003031#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003032 vector(*this, allocator_type(__alloc())).swap(*this);
3033#ifndef _LIBCPP_NO_EXCEPTIONS
3034 }
3035 catch (...)
3036 {
3037 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003038#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003039 }
3040}
3041
3042template <class _Allocator>
3043typename vector<bool, _Allocator>::reference
3044vector<bool, _Allocator>::at(size_type __n)
3045{
3046 if (__n >= size())
3047 this->__throw_out_of_range();
3048 return (*this)[__n];
3049}
3050
3051template <class _Allocator>
3052typename vector<bool, _Allocator>::const_reference
3053vector<bool, _Allocator>::at(size_type __n) const
3054{
3055 if (__n >= size())
3056 this->__throw_out_of_range();
3057 return (*this)[__n];
3058}
3059
3060template <class _Allocator>
3061void
3062vector<bool, _Allocator>::push_back(const value_type& __x)
3063{
3064 if (this->__size_ == this->capacity())
3065 reserve(__recommend(this->__size_ + 1));
3066 ++this->__size_;
3067 back() = __x;
3068}
3069
3070template <class _Allocator>
3071typename vector<bool, _Allocator>::iterator
3072vector<bool, _Allocator>::insert(const_iterator __position, const value_type& __x)
3073{
3074 iterator __r;
3075 if (size() < capacity())
3076 {
3077 const_iterator __old_end = end();
3078 ++__size_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003079 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003080 __r = __const_iterator_cast(__position);
3081 }
3082 else
3083 {
3084 vector __v(__alloc());
3085 __v.reserve(__recommend(__size_ + 1));
3086 __v.__size_ = __size_ + 1;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003087 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3088 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003089 swap(__v);
3090 }
3091 *__r = __x;
3092 return __r;
3093}
3094
3095template <class _Allocator>
3096typename vector<bool, _Allocator>::iterator
3097vector<bool, _Allocator>::insert(const_iterator __position, size_type __n, const value_type& __x)
3098{
3099 iterator __r;
3100 size_type __c = capacity();
3101 if (__n <= __c && size() <= __c - __n)
3102 {
3103 const_iterator __old_end = end();
3104 __size_ += __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003105 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003106 __r = __const_iterator_cast(__position);
3107 }
3108 else
3109 {
3110 vector __v(__alloc());
3111 __v.reserve(__recommend(__size_ + __n));
3112 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003113 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3114 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003115 swap(__v);
3116 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003117 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003118 return __r;
3119}
3120
3121template <class _Allocator>
3122template <class _InputIterator>
3123typename enable_if
3124<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05003125 __is_cpp17_input_iterator <_InputIterator>::value &&
3126 !__is_cpp17_forward_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003127 typename vector<bool, _Allocator>::iterator
3128>::type
3129vector<bool, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
3130{
3131 difference_type __off = __position - begin();
3132 iterator __p = __const_iterator_cast(__position);
3133 iterator __old_end = end();
3134 for (; size() != capacity() && __first != __last; ++__first)
3135 {
3136 ++this->__size_;
3137 back() = *__first;
3138 }
3139 vector __v(__alloc());
3140 if (__first != __last)
3141 {
3142#ifndef _LIBCPP_NO_EXCEPTIONS
3143 try
3144 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003145#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003146 __v.assign(__first, __last);
3147 difference_type __old_size = static_cast<difference_type>(__old_end - begin());
3148 difference_type __old_p = __p - begin();
3149 reserve(__recommend(size() + __v.size()));
3150 __p = begin() + __old_p;
3151 __old_end = begin() + __old_size;
3152#ifndef _LIBCPP_NO_EXCEPTIONS
3153 }
3154 catch (...)
3155 {
3156 erase(__old_end, end());
3157 throw;
3158 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003159#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003160 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003161 __p = _VSTD::rotate(__p, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003162 insert(__p, __v.begin(), __v.end());
3163 return begin() + __off;
3164}
3165
3166template <class _Allocator>
3167template <class _ForwardIterator>
3168typename enable_if
3169<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05003170 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003171 typename vector<bool, _Allocator>::iterator
3172>::type
3173vector<bool, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
3174{
Eric Fiselier654dd332016-12-11 05:31:00 +00003175 const difference_type __n_signed = _VSTD::distance(__first, __last);
3176 _LIBCPP_ASSERT(__n_signed >= 0, "invalid range specified");
3177 const size_type __n = static_cast<size_type>(__n_signed);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003178 iterator __r;
3179 size_type __c = capacity();
3180 if (__n <= __c && size() <= __c - __n)
3181 {
3182 const_iterator __old_end = end();
3183 __size_ += __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003184 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003185 __r = __const_iterator_cast(__position);
3186 }
3187 else
3188 {
3189 vector __v(__alloc());
3190 __v.reserve(__recommend(__size_ + __n));
3191 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003192 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3193 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003194 swap(__v);
3195 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003196 _VSTD::copy(__first, __last, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003197 return __r;
3198}
3199
3200template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003201inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003202typename vector<bool, _Allocator>::iterator
3203vector<bool, _Allocator>::erase(const_iterator __position)
3204{
3205 iterator __r = __const_iterator_cast(__position);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003206 _VSTD::copy(__position + 1, this->cend(), __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003207 --__size_;
3208 return __r;
3209}
3210
3211template <class _Allocator>
3212typename vector<bool, _Allocator>::iterator
3213vector<bool, _Allocator>::erase(const_iterator __first, const_iterator __last)
3214{
3215 iterator __r = __const_iterator_cast(__first);
3216 difference_type __d = __last - __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003217 _VSTD::copy(__last, this->cend(), __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003218 __size_ -= __d;
3219 return __r;
3220}
3221
3222template <class _Allocator>
3223void
3224vector<bool, _Allocator>::swap(vector& __x)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003225#if _LIBCPP_STD_VER >= 14
3226 _NOEXCEPT
3227#else
Eric Fiselier69c51982016-12-28 06:06:09 +00003228 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003229 __is_nothrow_swappable<allocator_type>::value)
3230#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003231{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003232 _VSTD::swap(this->__begin_, __x.__begin_);
3233 _VSTD::swap(this->__size_, __x.__size_);
3234 _VSTD::swap(this->__cap(), __x.__cap());
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05003235 _VSTD::__swap_allocator(this->__alloc(), __x.__alloc(),
Marshall Clow8982dcd2015-07-13 20:04:56 +00003236 integral_constant<bool, __alloc_traits::propagate_on_container_swap::value>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003237}
3238
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003239template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003240void
3241vector<bool, _Allocator>::resize(size_type __sz, value_type __x)
3242{
3243 size_type __cs = size();
3244 if (__cs < __sz)
3245 {
3246 iterator __r;
3247 size_type __c = capacity();
3248 size_type __n = __sz - __cs;
3249 if (__n <= __c && __cs <= __c - __n)
3250 {
3251 __r = end();
3252 __size_ += __n;
3253 }
3254 else
3255 {
3256 vector __v(__alloc());
3257 __v.reserve(__recommend(__size_ + __n));
3258 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003259 __r = _VSTD::copy(cbegin(), cend(), __v.begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003260 swap(__v);
3261 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003262 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003263 }
3264 else
3265 __size_ = __sz;
3266}
3267
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003268template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003269void
Howard Hinnant1c936782011-06-03 19:40:40 +00003270vector<bool, _Allocator>::flip() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003271{
3272 // do middle whole words
3273 size_type __n = __size_;
3274 __storage_pointer __p = __begin_;
3275 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3276 *__p = ~*__p;
3277 // do last partial word
3278 if (__n > 0)
3279 {
3280 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3281 __storage_type __b = *__p & __m;
3282 *__p &= ~__m;
3283 *__p |= ~__b & __m;
3284 }
3285}
3286
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003287template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003288bool
3289vector<bool, _Allocator>::__invariants() const
3290{
Howard Hinnant76053d72013-06-27 19:35:32 +00003291 if (this->__begin_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003292 {
3293 if (this->__size_ != 0 || this->__cap() != 0)
3294 return false;
3295 }
3296 else
3297 {
3298 if (this->__cap() == 0)
3299 return false;
3300 if (this->__size_ > this->capacity())
3301 return false;
3302 }
3303 return true;
3304}
3305
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003306template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003307size_t
Howard Hinnant1c936782011-06-03 19:40:40 +00003308vector<bool, _Allocator>::__hash_code() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003309{
3310 size_t __h = 0;
3311 // do middle whole words
3312 size_type __n = __size_;
3313 __storage_pointer __p = __begin_;
3314 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3315 __h ^= *__p;
3316 // do last partial word
3317 if (__n > 0)
3318 {
3319 const __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3320 __h ^= *__p & __m;
3321 }
3322 return __h;
3323}
3324
3325template <class _Allocator>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003326struct _LIBCPP_TEMPLATE_VIS hash<vector<bool, _Allocator> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00003327 : public unary_function<vector<bool, _Allocator>, size_t>
3328{
Howard Hinnant1c265cd2010-09-23 18:58:28 +00003329 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +00003330 size_t operator()(const vector<bool, _Allocator>& __vec) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003331 {return __vec.__hash_code();}
3332};
3333
3334template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003335inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003336bool
3337operator==(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3338{
3339 const typename vector<_Tp, _Allocator>::size_type __sz = __x.size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003340 return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003341}
3342
3343template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003344inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003345bool
3346operator!=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3347{
3348 return !(__x == __y);
3349}
3350
3351template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003352inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003353bool
3354operator< (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3355{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003356 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003357}
3358
3359template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003360inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003361bool
3362operator> (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3363{
3364 return __y < __x;
3365}
3366
3367template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003368inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003369bool
3370operator>=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3371{
3372 return !(__x < __y);
3373}
3374
3375template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003376inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003377bool
3378operator<=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3379{
3380 return !(__y < __x);
3381}
3382
3383template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003384inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003385void
3386swap(vector<_Tp, _Allocator>& __x, vector<_Tp, _Allocator>& __y)
Howard Hinnant1c936782011-06-03 19:40:40 +00003387 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00003388{
3389 __x.swap(__y);
3390}
3391
Marshall Clow29b53f22018-12-14 18:49:35 +00003392#if _LIBCPP_STD_VER > 17
3393template <class _Tp, class _Allocator, class _Up>
Marek Kurdeja98b1412020-05-02 13:58:03 +02003394inline _LIBCPP_INLINE_VISIBILITY typename vector<_Tp, _Allocator>::size_type
3395erase(vector<_Tp, _Allocator>& __c, const _Up& __v) {
3396 auto __old_size = __c.size();
3397 __c.erase(_VSTD::remove(__c.begin(), __c.end(), __v), __c.end());
3398 return __old_size - __c.size();
3399}
Marshall Clow29b53f22018-12-14 18:49:35 +00003400
3401template <class _Tp, class _Allocator, class _Predicate>
Marek Kurdeja98b1412020-05-02 13:58:03 +02003402inline _LIBCPP_INLINE_VISIBILITY typename vector<_Tp, _Allocator>::size_type
3403erase_if(vector<_Tp, _Allocator>& __c, _Predicate __pred) {
3404 auto __old_size = __c.size();
3405 __c.erase(_VSTD::remove_if(__c.begin(), __c.end(), __pred), __c.end());
3406 return __old_size - __c.size();
3407}
Marshall Clow29b53f22018-12-14 18:49:35 +00003408#endif
3409
Howard Hinnantc51e1022010-05-11 19:42:16 +00003410_LIBCPP_END_NAMESPACE_STD
3411
Eric Fiselierf4433a32017-05-31 22:07:49 +00003412_LIBCPP_POP_MACROS
3413
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003414#endif // _LIBCPP_VECTOR