blob: 9189ed44a80c73d5b8523b60fe739ef87544ee98 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===------------------------------ vector --------------------------------===//
3//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_VECTOR
11#define _LIBCPP_VECTOR
12
13/*
14 vector synopsis
15
16namespace std
17{
18
Howard Hinnant3b6579a2010-08-22 00:02:43 +000019template <class T, class Allocator = allocator<T> >
Howard Hinnantc51e1022010-05-11 19:42:16 +000020class vector
Howard Hinnant3b6579a2010-08-22 00:02:43 +000021{
22public:
23 typedef T value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +000024 typedef Allocator allocator_type;
25 typedef typename allocator_type::reference reference;
26 typedef typename allocator_type::const_reference const_reference;
27 typedef implementation-defined iterator;
28 typedef implementation-defined const_iterator;
29 typedef typename allocator_type::size_type size_type;
30 typedef typename allocator_type::difference_type difference_type;
31 typedef typename allocator_type::pointer pointer;
32 typedef typename allocator_type::const_pointer const_pointer;
33 typedef std::reverse_iterator<iterator> reverse_iterator;
34 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
35
Howard Hinnant1c936782011-06-03 19:40:40 +000036 vector()
37 noexcept(is_nothrow_default_constructible<allocator_type>::value);
38 explicit vector(const allocator_type&);
Howard Hinnantc51e1022010-05-11 19:42:16 +000039 explicit vector(size_type n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +000040 explicit vector(size_type n, const allocator_type&); // C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +000041 vector(size_type n, const value_type& value, const allocator_type& = allocator_type());
42 template <class InputIterator>
43 vector(InputIterator first, InputIterator last, const allocator_type& = allocator_type());
44 vector(const vector& x);
Howard Hinnant1c936782011-06-03 19:40:40 +000045 vector(vector&& x)
46 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +000047 vector(initializer_list<value_type> il);
48 vector(initializer_list<value_type> il, const allocator_type& a);
49 ~vector();
50 vector& operator=(const vector& x);
Howard Hinnant1c936782011-06-03 19:40:40 +000051 vector& operator=(vector&& x)
52 noexcept(
Marshall Clow2fe8a8d2015-08-18 18:57:00 +000053 allocator_type::propagate_on_container_move_assignment::value ||
54 allocator_type::is_always_equal::value); // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +000055 vector& operator=(initializer_list<value_type> il);
56 template <class InputIterator>
57 void assign(InputIterator first, InputIterator last);
58 void assign(size_type n, const value_type& u);
59 void assign(initializer_list<value_type> il);
60
Howard Hinnant1c936782011-06-03 19:40:40 +000061 allocator_type get_allocator() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000062
Howard Hinnant1c936782011-06-03 19:40:40 +000063 iterator begin() noexcept;
64 const_iterator begin() const noexcept;
65 iterator end() noexcept;
66 const_iterator end() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000067
Howard Hinnant1c936782011-06-03 19:40:40 +000068 reverse_iterator rbegin() noexcept;
69 const_reverse_iterator rbegin() const noexcept;
70 reverse_iterator rend() noexcept;
71 const_reverse_iterator rend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000072
Howard Hinnant1c936782011-06-03 19:40:40 +000073 const_iterator cbegin() const noexcept;
74 const_iterator cend() const noexcept;
75 const_reverse_iterator crbegin() const noexcept;
76 const_reverse_iterator crend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000077
Howard Hinnant1c936782011-06-03 19:40:40 +000078 size_type size() const noexcept;
79 size_type max_size() const noexcept;
80 size_type capacity() const noexcept;
81 bool empty() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000082 void reserve(size_type n);
Howard Hinnant1c936782011-06-03 19:40:40 +000083 void shrink_to_fit() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000084
85 reference operator[](size_type n);
86 const_reference operator[](size_type n) const;
87 reference at(size_type n);
88 const_reference at(size_type n) const;
89
90 reference front();
91 const_reference front() const;
92 reference back();
93 const_reference back() const;
94
Howard Hinnant1c936782011-06-03 19:40:40 +000095 value_type* data() noexcept;
96 const value_type* data() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000097
98 void push_back(const value_type& x);
99 void push_back(value_type&& x);
100 template <class... Args>
Marshall Clowea52cc42017-01-24 23:09:12 +0000101 reference emplace_back(Args&&... args); // reference in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000102 void pop_back();
103
104 template <class... Args> iterator emplace(const_iterator position, Args&&... args);
105 iterator insert(const_iterator position, const value_type& x);
106 iterator insert(const_iterator position, value_type&& x);
107 iterator insert(const_iterator position, size_type n, const value_type& x);
108 template <class InputIterator>
109 iterator insert(const_iterator position, InputIterator first, InputIterator last);
110 iterator insert(const_iterator position, initializer_list<value_type> il);
111
112 iterator erase(const_iterator position);
113 iterator erase(const_iterator first, const_iterator last);
114
Howard Hinnant1c936782011-06-03 19:40:40 +0000115 void clear() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000116
117 void resize(size_type sz);
118 void resize(size_type sz, const value_type& c);
119
Howard Hinnant1c936782011-06-03 19:40:40 +0000120 void swap(vector&)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000121 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
122 allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000123
124 bool __invariants() const;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000125};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000126
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000127template <class Allocator = allocator<T> >
Howard Hinnantc51e1022010-05-11 19:42:16 +0000128class vector<bool, Allocator>
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000129{
130public:
131 typedef bool value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000132 typedef Allocator allocator_type;
133 typedef implementation-defined iterator;
134 typedef implementation-defined const_iterator;
135 typedef typename allocator_type::size_type size_type;
136 typedef typename allocator_type::difference_type difference_type;
137 typedef iterator pointer;
138 typedef const_iterator const_pointer;
139 typedef std::reverse_iterator<iterator> reverse_iterator;
140 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
141
142 class reference
143 {
144 public:
Howard Hinnant1c936782011-06-03 19:40:40 +0000145 reference(const reference&) noexcept;
146 operator bool() const noexcept;
Louis Dionned9f528e2021-06-29 13:52:26 -0400147 reference& operator=(bool x) noexcept;
Howard Hinnant1c936782011-06-03 19:40:40 +0000148 reference& operator=(const reference& x) noexcept;
149 iterator operator&() const noexcept;
150 void flip() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000151 };
152
153 class const_reference
154 {
155 public:
Howard Hinnant1c936782011-06-03 19:40:40 +0000156 const_reference(const reference&) noexcept;
157 operator bool() const noexcept;
158 const_iterator operator&() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000159 };
160
Howard Hinnant1c936782011-06-03 19:40:40 +0000161 vector()
162 noexcept(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnantc2734962011-09-02 20:42:31 +0000163 explicit vector(const allocator_type&);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +0000164 explicit vector(size_type n, const allocator_type& a = allocator_type()); // C++14
165 vector(size_type n, const value_type& value, const allocator_type& = allocator_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000166 template <class InputIterator>
167 vector(InputIterator first, InputIterator last, const allocator_type& = allocator_type());
168 vector(const vector& x);
Howard Hinnant1c936782011-06-03 19:40:40 +0000169 vector(vector&& x)
170 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000171 vector(initializer_list<value_type> il);
172 vector(initializer_list<value_type> il, const allocator_type& a);
173 ~vector();
174 vector& operator=(const vector& x);
Howard Hinnant1c936782011-06-03 19:40:40 +0000175 vector& operator=(vector&& x)
176 noexcept(
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000177 allocator_type::propagate_on_container_move_assignment::value ||
178 allocator_type::is_always_equal::value); // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000179 vector& operator=(initializer_list<value_type> il);
180 template <class InputIterator>
181 void assign(InputIterator first, InputIterator last);
182 void assign(size_type n, const value_type& u);
183 void assign(initializer_list<value_type> il);
184
Howard Hinnant1c936782011-06-03 19:40:40 +0000185 allocator_type get_allocator() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000186
Howard Hinnant1c936782011-06-03 19:40:40 +0000187 iterator begin() noexcept;
188 const_iterator begin() const noexcept;
189 iterator end() noexcept;
190 const_iterator end() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000191
Howard Hinnant1c936782011-06-03 19:40:40 +0000192 reverse_iterator rbegin() noexcept;
193 const_reverse_iterator rbegin() const noexcept;
194 reverse_iterator rend() noexcept;
195 const_reverse_iterator rend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000196
Howard Hinnant1c936782011-06-03 19:40:40 +0000197 const_iterator cbegin() const noexcept;
198 const_iterator cend() const noexcept;
199 const_reverse_iterator crbegin() const noexcept;
200 const_reverse_iterator crend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000201
Howard Hinnant1c936782011-06-03 19:40:40 +0000202 size_type size() const noexcept;
203 size_type max_size() const noexcept;
204 size_type capacity() const noexcept;
205 bool empty() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000206 void reserve(size_type n);
Howard Hinnant1c936782011-06-03 19:40:40 +0000207 void shrink_to_fit() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000208
209 reference operator[](size_type n);
210 const_reference operator[](size_type n) const;
211 reference at(size_type n);
212 const_reference at(size_type n) const;
213
214 reference front();
215 const_reference front() const;
216 reference back();
217 const_reference back() const;
218
219 void push_back(const value_type& x);
Marshall Clowea52cc42017-01-24 23:09:12 +0000220 template <class... Args> reference emplace_back(Args&&... args); // C++14; reference in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000221 void pop_back();
222
Marshall Clowc46bb8e2013-08-13 23:54:12 +0000223 template <class... Args> iterator emplace(const_iterator position, Args&&... args); // C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000224 iterator insert(const_iterator position, const value_type& x);
225 iterator insert(const_iterator position, size_type n, const value_type& x);
226 template <class InputIterator>
227 iterator insert(const_iterator position, InputIterator first, InputIterator last);
228 iterator insert(const_iterator position, initializer_list<value_type> il);
229
230 iterator erase(const_iterator position);
231 iterator erase(const_iterator first, const_iterator last);
232
Howard Hinnant1c936782011-06-03 19:40:40 +0000233 void clear() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000234
235 void resize(size_type sz);
236 void resize(size_type sz, value_type x);
237
Howard Hinnant1c936782011-06-03 19:40:40 +0000238 void swap(vector&)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000239 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
240 allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnant1c936782011-06-03 19:40:40 +0000241 void flip() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000242
243 bool __invariants() const;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000244};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000245
Marshall Clowf0ca1492018-05-21 21:30:12 +0000246template <class InputIterator, class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
247 vector(InputIterator, InputIterator, Allocator = Allocator())
248 -> vector<typename iterator_traits<InputIterator>::value_type, Allocator>;
249
Howard Hinnantc51e1022010-05-11 19:42:16 +0000250template <class Allocator> struct hash<std::vector<bool, Allocator>>;
251
252template <class T, class Allocator> bool operator==(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
253template <class T, class Allocator> bool operator< (const vector<T,Allocator>& x, const vector<T,Allocator>& y);
254template <class T, class Allocator> bool operator!=(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
255template <class T, class Allocator> bool operator> (const vector<T,Allocator>& x, const vector<T,Allocator>& y);
256template <class T, class Allocator> bool operator>=(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
257template <class T, class Allocator> bool operator<=(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
258
Howard Hinnant1c936782011-06-03 19:40:40 +0000259template <class T, class Allocator>
260void swap(vector<T,Allocator>& x, vector<T,Allocator>& y)
261 noexcept(noexcept(x.swap(y)));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000262
Marshall Clow29b53f22018-12-14 18:49:35 +0000263template <class T, class Allocator, class U>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200264typename vector<T, Allocator>::size_type
265erase(vector<T, Allocator>& c, const U& value); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000266template <class T, class Allocator, class Predicate>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200267typename vector<T, Allocator>::size_type
268erase_if(vector<T, Allocator>& c, Predicate pred); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000269
Howard Hinnantc51e1022010-05-11 19:42:16 +0000270} // std
271
272*/
273
274#include <__config>
275#include <__bit_reference>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400276#include <__debug>
277#include <__functional_base>
Louis Dionne77249522021-06-11 09:55:11 -0400278#include <__iterator/wrap_iter.h>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400279#include <__split_buffer>
Christopher Di Bella41f26e82021-06-05 02:47:47 +0000280#include <__utility/forward.h>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400281#include <algorithm>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000282#include <climits>
Arthur O'Dwyer7deec122021-03-24 18:19:12 -0400283#include <compare>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400284#include <cstring>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000285#include <initializer_list>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400286#include <iosfwd> // for forward declaration of vector
287#include <limits>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000288#include <memory>
289#include <stdexcept>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400290#include <type_traits>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000291#include <version>
Howard Hinnante6ff0b62013-08-02 00:26:35 +0000292
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000293#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000294#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000295#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000296
Eric Fiselierf4433a32017-05-31 22:07:49 +0000297_LIBCPP_PUSH_MACROS
298#include <__undef_macros>
299
300
Howard Hinnantc51e1022010-05-11 19:42:16 +0000301_LIBCPP_BEGIN_NAMESPACE_STD
302
303template <bool>
Louis Dionne53dca682019-08-28 18:10:39 +0000304class _LIBCPP_TEMPLATE_VIS __vector_base_common
Howard Hinnantc51e1022010-05-11 19:42:16 +0000305{
306protected:
Louis Dionne16fe2952018-07-11 23:14:33 +0000307 _LIBCPP_INLINE_VISIBILITY __vector_base_common() {}
Marshall Clow8fea1612016-08-25 15:09:01 +0000308 _LIBCPP_NORETURN void __throw_length_error() const;
309 _LIBCPP_NORETURN void __throw_out_of_range() const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000310};
311
312template <bool __b>
313void
314__vector_base_common<__b>::__throw_length_error() const
315{
Marshall Clow8fea1612016-08-25 15:09:01 +0000316 _VSTD::__throw_length_error("vector");
Howard Hinnantc51e1022010-05-11 19:42:16 +0000317}
318
319template <bool __b>
320void
321__vector_base_common<__b>::__throw_out_of_range() const
322{
Marshall Clow8fea1612016-08-25 15:09:01 +0000323 _VSTD::__throw_out_of_range("vector");
Howard Hinnantc51e1022010-05-11 19:42:16 +0000324}
325
Eric Fiselier1b57fa82016-09-15 22:27:07 +0000326_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __vector_base_common<true>)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000327
328template <class _Tp, class _Allocator>
329class __vector_base
330 : protected __vector_base_common<true>
331{
Marshall Clowf0ca1492018-05-21 21:30:12 +0000332public:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000333 typedef _Allocator allocator_type;
334 typedef allocator_traits<allocator_type> __alloc_traits;
Marshall Clowf0ca1492018-05-21 21:30:12 +0000335 typedef typename __alloc_traits::size_type size_type;
336protected:
337 typedef _Tp value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000338 typedef value_type& reference;
339 typedef const value_type& const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000340 typedef typename __alloc_traits::difference_type difference_type;
341 typedef typename __alloc_traits::pointer pointer;
342 typedef typename __alloc_traits::const_pointer const_pointer;
343 typedef pointer iterator;
344 typedef const_pointer const_iterator;
345
346 pointer __begin_;
347 pointer __end_;
348 __compressed_pair<pointer, allocator_type> __end_cap_;
349
Howard Hinnant1c936782011-06-03 19:40:40 +0000350 _LIBCPP_INLINE_VISIBILITY
351 allocator_type& __alloc() _NOEXCEPT
352 {return __end_cap_.second();}
353 _LIBCPP_INLINE_VISIBILITY
354 const allocator_type& __alloc() const _NOEXCEPT
355 {return __end_cap_.second();}
356 _LIBCPP_INLINE_VISIBILITY
357 pointer& __end_cap() _NOEXCEPT
358 {return __end_cap_.first();}
359 _LIBCPP_INLINE_VISIBILITY
360 const pointer& __end_cap() const _NOEXCEPT
361 {return __end_cap_.first();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000362
Howard Hinnant1c936782011-06-03 19:40:40 +0000363 _LIBCPP_INLINE_VISIBILITY
364 __vector_base()
365 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnantcf823322010-12-17 14:46:43 +0000366 _LIBCPP_INLINE_VISIBILITY __vector_base(const allocator_type& __a);
Eric Fiselier4f1534c2018-06-05 22:32:52 +0000367#ifndef _LIBCPP_CXX03_LANG
368 _LIBCPP_INLINE_VISIBILITY __vector_base(allocator_type&& __a) _NOEXCEPT;
369#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000370 ~__vector_base();
371
Howard Hinnant1c936782011-06-03 19:40:40 +0000372 _LIBCPP_INLINE_VISIBILITY
373 void clear() _NOEXCEPT {__destruct_at_end(__begin_);}
374 _LIBCPP_INLINE_VISIBILITY
375 size_type capacity() const _NOEXCEPT
376 {return static_cast<size_type>(__end_cap() - __begin_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000377
Howard Hinnant1c936782011-06-03 19:40:40 +0000378 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76053d72013-06-27 19:35:32 +0000379 void __destruct_at_end(pointer __new_last) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000380
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000381 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000382 void __copy_assign_alloc(const __vector_base& __c)
383 {__copy_assign_alloc(__c, integral_constant<bool,
384 __alloc_traits::propagate_on_container_copy_assignment::value>());}
385
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000386 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000387 void __move_assign_alloc(__vector_base& __c)
Howard Hinnant1c936782011-06-03 19:40:40 +0000388 _NOEXCEPT_(
389 !__alloc_traits::propagate_on_container_move_assignment::value ||
390 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000391 {__move_assign_alloc(__c, integral_constant<bool,
392 __alloc_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000393private:
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000394 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000395 void __copy_assign_alloc(const __vector_base& __c, true_type)
396 {
397 if (__alloc() != __c.__alloc())
398 {
399 clear();
400 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
401 __begin_ = __end_ = __end_cap() = nullptr;
402 }
403 __alloc() = __c.__alloc();
404 }
405
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000406 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +0000407 void __copy_assign_alloc(const __vector_base&, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000408 {}
409
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000410 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +0000411 void __move_assign_alloc(__vector_base& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +0000412 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000413 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000414 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000415 }
416
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000417 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +0000418 void __move_assign_alloc(__vector_base&, false_type)
Howard Hinnant1c936782011-06-03 19:40:40 +0000419 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000420 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000421};
422
423template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000424inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000425void
Howard Hinnant76053d72013-06-27 19:35:32 +0000426__vector_base<_Tp, _Allocator>::__destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000427{
Bruce Mitchener405fdcd2017-03-23 14:39:23 +0000428 pointer __soon_to_be_end = __end_;
429 while (__new_last != __soon_to_be_end)
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500430 __alloc_traits::destroy(__alloc(), _VSTD::__to_address(--__soon_to_be_end));
Bruce Mitchener405fdcd2017-03-23 14:39:23 +0000431 __end_ = __new_last;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000432}
433
434template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000435inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000436__vector_base<_Tp, _Allocator>::__vector_base()
Howard Hinnant1c936782011-06-03 19:40:40 +0000437 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant76053d72013-06-27 19:35:32 +0000438 : __begin_(nullptr),
439 __end_(nullptr),
Eric Fiselier33ebfb62019-12-16 18:23:39 -0500440 __end_cap_(nullptr, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +0000441{
442}
443
444template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000445inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000446__vector_base<_Tp, _Allocator>::__vector_base(const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +0000447 : __begin_(nullptr),
448 __end_(nullptr),
449 __end_cap_(nullptr, __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000450{
451}
452
Eric Fiselier4f1534c2018-06-05 22:32:52 +0000453#ifndef _LIBCPP_CXX03_LANG
454template <class _Tp, class _Allocator>
455inline _LIBCPP_INLINE_VISIBILITY
456__vector_base<_Tp, _Allocator>::__vector_base(allocator_type&& __a) _NOEXCEPT
457 : __begin_(nullptr),
458 __end_(nullptr),
Arthur O'Dwyer07b22492020-11-27 11:02:06 -0500459 __end_cap_(nullptr, _VSTD::move(__a)) {}
Eric Fiselier4f1534c2018-06-05 22:32:52 +0000460#endif
461
Howard Hinnantc51e1022010-05-11 19:42:16 +0000462template <class _Tp, class _Allocator>
463__vector_base<_Tp, _Allocator>::~__vector_base()
464{
Howard Hinnant76053d72013-06-27 19:35:32 +0000465 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000466 {
467 clear();
468 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
469 }
470}
471
Eric Fiselier876c6862016-02-20 00:19:45 +0000472template <class _Tp, class _Allocator /* = allocator<_Tp> */>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000473class _LIBCPP_TEMPLATE_VIS vector
Howard Hinnantc51e1022010-05-11 19:42:16 +0000474 : private __vector_base<_Tp, _Allocator>
475{
476private:
477 typedef __vector_base<_Tp, _Allocator> __base;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000478 typedef allocator<_Tp> __default_allocator_type;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000479public:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000480 typedef vector __self;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000481 typedef _Tp value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000482 typedef _Allocator allocator_type;
483 typedef typename __base::__alloc_traits __alloc_traits;
484 typedef typename __base::reference reference;
485 typedef typename __base::const_reference const_reference;
486 typedef typename __base::size_type size_type;
487 typedef typename __base::difference_type difference_type;
488 typedef typename __base::pointer pointer;
489 typedef typename __base::const_pointer const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000490 typedef __wrap_iter<pointer> iterator;
491 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000492 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
493 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000494
Howard Hinnanta416ff02013-03-26 19:04:56 +0000495 static_assert((is_same<typename allocator_type::value_type, value_type>::value),
496 "Allocator::value_type must be same type as value_type");
497
Howard Hinnant1c936782011-06-03 19:40:40 +0000498 _LIBCPP_INLINE_VISIBILITY
Marshall Clowe546cbb2015-06-04 02:05:41 +0000499 vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant27e0e772011-09-14 18:33:51 +0000500 {
Louis Dionneba400782020-10-02 15:02:52 -0400501#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000502 __get_db()->__insert_c(this);
503#endif
504 }
505 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
Marshall Clow8f282f42015-06-04 00:10:20 +0000506#if _LIBCPP_STD_VER <= 14
507 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
508#else
509 _NOEXCEPT
510#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +0000511 : __base(__a)
512 {
Louis Dionneba400782020-10-02 15:02:52 -0400513#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000514 __get_db()->__insert_c(this);
515#endif
516 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000517 explicit vector(size_type __n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +0000518#if _LIBCPP_STD_VER > 11
519 explicit vector(size_type __n, const allocator_type& __a);
520#endif
Marshall Clowf0ca1492018-05-21 21:30:12 +0000521 vector(size_type __n, const value_type& __x);
522 vector(size_type __n, const value_type& __x, const allocator_type& __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000523 template <class _InputIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +0000524 vector(_InputIterator __first,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500525 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
526 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000527 is_constructible<
528 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +0000529 typename iterator_traits<_InputIterator>::reference>::value,
530 _InputIterator>::type __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000531 template <class _InputIterator>
532 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500533 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
534 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000535 is_constructible<
536 value_type,
537 typename iterator_traits<_InputIterator>::reference>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000538 template <class _ForwardIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +0000539 vector(_ForwardIterator __first,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500540 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000541 is_constructible<
542 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +0000543 typename iterator_traits<_ForwardIterator>::reference>::value,
544 _ForwardIterator>::type __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000545 template <class _ForwardIterator>
546 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500547 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000548 is_constructible<
549 value_type,
550 typename iterator_traits<_ForwardIterator>::reference>::value>::type* = 0);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000551
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000552 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant27e0e772011-09-14 18:33:51 +0000553 ~vector()
554 {
Marshall Clow68af4f32018-09-07 15:47:59 +0000555 __annotate_delete();
Louis Dionneba400782020-10-02 15:02:52 -0400556#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000557 __get_db()->__erase_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000558#endif
Marshall Clow68af4f32018-09-07 15:47:59 +0000559 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000560
561 vector(const vector& __x);
Arthur O'Dwyer9b9662f2021-03-01 17:08:24 -0500562 vector(const vector& __x, const __identity_t<allocator_type>& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000563 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000564 vector& operator=(const vector& __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000565
566#ifndef _LIBCPP_CXX03_LANG
567 _LIBCPP_INLINE_VISIBILITY
568 vector(initializer_list<value_type> __il);
569
570 _LIBCPP_INLINE_VISIBILITY
571 vector(initializer_list<value_type> __il, const allocator_type& __a);
572
Howard Hinnantcf823322010-12-17 14:46:43 +0000573 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000574 vector(vector&& __x)
Marshall Clowe5108202015-07-14 14:46:32 +0000575#if _LIBCPP_STD_VER > 14
576 _NOEXCEPT;
577#else
Howard Hinnant1c936782011-06-03 19:40:40 +0000578 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowe5108202015-07-14 14:46:32 +0000579#endif
Eric Fiseliered9e9362017-04-16 02:40:45 +0000580
Howard Hinnantcf823322010-12-17 14:46:43 +0000581 _LIBCPP_INLINE_VISIBILITY
Arthur O'Dwyer9b9662f2021-03-01 17:08:24 -0500582 vector(vector&& __x, const __identity_t<allocator_type>& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000583 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000584 vector& operator=(vector&& __x)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000585 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiseliered9e9362017-04-16 02:40:45 +0000586
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000587 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000588 vector& operator=(initializer_list<value_type> __il)
589 {assign(__il.begin(), __il.end()); return *this;}
Eric Fiseliered9e9362017-04-16 02:40:45 +0000590
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400591#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000592
593 template <class _InputIterator>
594 typename enable_if
595 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500596 __is_cpp17_input_iterator <_InputIterator>::value &&
597 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000598 is_constructible<
599 value_type,
600 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000601 void
602 >::type
603 assign(_InputIterator __first, _InputIterator __last);
604 template <class _ForwardIterator>
605 typename enable_if
606 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500607 __is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000608 is_constructible<
609 value_type,
610 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000611 void
612 >::type
613 assign(_ForwardIterator __first, _ForwardIterator __last);
614
615 void assign(size_type __n, const_reference __u);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000616
617#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000618 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000619 void assign(initializer_list<value_type> __il)
620 {assign(__il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +0000621#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000622
Howard Hinnant1c936782011-06-03 19:40:40 +0000623 _LIBCPP_INLINE_VISIBILITY
624 allocator_type get_allocator() const _NOEXCEPT
625 {return this->__alloc();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000626
Howard Hinnant1c936782011-06-03 19:40:40 +0000627 _LIBCPP_INLINE_VISIBILITY iterator begin() _NOEXCEPT;
628 _LIBCPP_INLINE_VISIBILITY const_iterator begin() const _NOEXCEPT;
629 _LIBCPP_INLINE_VISIBILITY iterator end() _NOEXCEPT;
630 _LIBCPP_INLINE_VISIBILITY const_iterator end() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000631
Howard Hinnant1c936782011-06-03 19:40:40 +0000632 _LIBCPP_INLINE_VISIBILITY
633 reverse_iterator rbegin() _NOEXCEPT
634 {return reverse_iterator(end());}
635 _LIBCPP_INLINE_VISIBILITY
636 const_reverse_iterator rbegin() const _NOEXCEPT
637 {return const_reverse_iterator(end());}
638 _LIBCPP_INLINE_VISIBILITY
639 reverse_iterator rend() _NOEXCEPT
640 {return reverse_iterator(begin());}
641 _LIBCPP_INLINE_VISIBILITY
642 const_reverse_iterator rend() const _NOEXCEPT
643 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000644
Howard Hinnant1c936782011-06-03 19:40:40 +0000645 _LIBCPP_INLINE_VISIBILITY
646 const_iterator cbegin() const _NOEXCEPT
647 {return begin();}
648 _LIBCPP_INLINE_VISIBILITY
649 const_iterator cend() const _NOEXCEPT
650 {return end();}
651 _LIBCPP_INLINE_VISIBILITY
652 const_reverse_iterator crbegin() const _NOEXCEPT
653 {return rbegin();}
654 _LIBCPP_INLINE_VISIBILITY
655 const_reverse_iterator crend() const _NOEXCEPT
656 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000657
Howard Hinnant1c936782011-06-03 19:40:40 +0000658 _LIBCPP_INLINE_VISIBILITY
659 size_type size() const _NOEXCEPT
660 {return static_cast<size_type>(this->__end_ - this->__begin_);}
661 _LIBCPP_INLINE_VISIBILITY
662 size_type capacity() const _NOEXCEPT
663 {return __base::capacity();}
Marshall Clow425f5752017-11-15 05:51:26 +0000664 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000665 bool empty() const _NOEXCEPT
666 {return this->__begin_ == this->__end_;}
667 size_type max_size() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000668 void reserve(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +0000669 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000670
Marshall Clowd6470492019-03-15 00:29:35 +0000671 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) _NOEXCEPT;
672 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000673 reference at(size_type __n);
674 const_reference at(size_type __n) const;
675
Marshall Clowd6470492019-03-15 00:29:35 +0000676 _LIBCPP_INLINE_VISIBILITY reference front() _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000677 {
Kristina Bessonovaaeeaa7e2021-05-09 19:29:56 +0200678 _LIBCPP_ASSERT(!empty(), "front() called on an empty vector");
Howard Hinnant27e0e772011-09-14 18:33:51 +0000679 return *this->__begin_;
680 }
Marshall Clowd6470492019-03-15 00:29:35 +0000681 _LIBCPP_INLINE_VISIBILITY const_reference front() const _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000682 {
Kristina Bessonovaaeeaa7e2021-05-09 19:29:56 +0200683 _LIBCPP_ASSERT(!empty(), "front() called on an empty vector");
Howard Hinnant27e0e772011-09-14 18:33:51 +0000684 return *this->__begin_;
685 }
Marshall Clowd6470492019-03-15 00:29:35 +0000686 _LIBCPP_INLINE_VISIBILITY reference back() _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000687 {
Kristina Bessonovaaeeaa7e2021-05-09 19:29:56 +0200688 _LIBCPP_ASSERT(!empty(), "back() called on an empty vector");
Howard Hinnant27e0e772011-09-14 18:33:51 +0000689 return *(this->__end_ - 1);
690 }
Marshall Clowd6470492019-03-15 00:29:35 +0000691 _LIBCPP_INLINE_VISIBILITY const_reference back() const _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000692 {
Kristina Bessonovaaeeaa7e2021-05-09 19:29:56 +0200693 _LIBCPP_ASSERT(!empty(), "back() called on an empty vector");
Howard Hinnant27e0e772011-09-14 18:33:51 +0000694 return *(this->__end_ - 1);
695 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000696
Howard Hinnant1c936782011-06-03 19:40:40 +0000697 _LIBCPP_INLINE_VISIBILITY
698 value_type* data() _NOEXCEPT
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500699 {return _VSTD::__to_address(this->__begin_);}
Howard Hinnant1c936782011-06-03 19:40:40 +0000700 _LIBCPP_INLINE_VISIBILITY
701 const value_type* data() const _NOEXCEPT
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500702 {return _VSTD::__to_address(this->__begin_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000703
Eric Fiselier96919722017-10-17 13:03:17 +0000704#ifdef _LIBCPP_CXX03_LANG
705 _LIBCPP_INLINE_VISIBILITY
706 void __emplace_back(const value_type& __x) { push_back(__x); }
707#else
708 template <class _Arg>
709 _LIBCPP_INLINE_VISIBILITY
710 void __emplace_back(_Arg&& __arg) {
711 emplace_back(_VSTD::forward<_Arg>(__arg));
712 }
713#endif
714
Howard Hinnantcf823322010-12-17 14:46:43 +0000715 _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000716
717#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000718 _LIBCPP_INLINE_VISIBILITY void push_back(value_type&& __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000719
Howard Hinnantc51e1022010-05-11 19:42:16 +0000720 template <class... _Args>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000721 _LIBCPP_INLINE_VISIBILITY
Marshall Clowea52cc42017-01-24 23:09:12 +0000722#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +0000723 reference emplace_back(_Args&&... __args);
Marshall Clowea52cc42017-01-24 23:09:12 +0000724#else
725 void emplace_back(_Args&&... __args);
726#endif
Eric Fiseliered9e9362017-04-16 02:40:45 +0000727#endif // !_LIBCPP_CXX03_LANG
728
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000729 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000730 void pop_back();
731
732 iterator insert(const_iterator __position, const_reference __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000733
734#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000735 iterator insert(const_iterator __position, value_type&& __x);
736 template <class... _Args>
737 iterator emplace(const_iterator __position, _Args&&... __args);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400738#endif // !_LIBCPP_CXX03_LANG
Eric Fiseliered9e9362017-04-16 02:40:45 +0000739
Howard Hinnantc51e1022010-05-11 19:42:16 +0000740 iterator insert(const_iterator __position, size_type __n, const_reference __x);
741 template <class _InputIterator>
742 typename enable_if
743 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500744 __is_cpp17_input_iterator <_InputIterator>::value &&
745 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000746 is_constructible<
747 value_type,
748 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000749 iterator
750 >::type
751 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
752 template <class _ForwardIterator>
753 typename enable_if
754 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500755 __is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000756 is_constructible<
757 value_type,
758 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000759 iterator
760 >::type
761 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000762
763#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000764 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000765 iterator insert(const_iterator __position, initializer_list<value_type> __il)
766 {return insert(__position, __il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +0000767#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000768
Howard Hinnantcf823322010-12-17 14:46:43 +0000769 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000770 iterator erase(const_iterator __first, const_iterator __last);
771
Howard Hinnant1c936782011-06-03 19:40:40 +0000772 _LIBCPP_INLINE_VISIBILITY
773 void clear() _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000774 {
Marshall Clow2cd9d372014-05-08 14:14:06 +0000775 size_type __old_size = size();
Howard Hinnant27e0e772011-09-14 18:33:51 +0000776 __base::clear();
Marshall Clow2cd9d372014-05-08 14:14:06 +0000777 __annotate_shrink(__old_size);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000778 __invalidate_all_iterators();
779 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000780
781 void resize(size_type __sz);
782 void resize(size_type __sz, const_reference __x);
783
Howard Hinnant1c936782011-06-03 19:40:40 +0000784 void swap(vector&)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000785#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +0000786 _NOEXCEPT;
Marshall Clow8982dcd2015-07-13 20:04:56 +0000787#else
Eric Fiselier873b8d32019-03-18 21:50:12 +0000788 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +0000789 __is_nothrow_swappable<allocator_type>::value);
790#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000791
792 bool __invariants() const;
793
Louis Dionneba400782020-10-02 15:02:52 -0400794#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000795
796 bool __dereferenceable(const const_iterator* __i) const;
797 bool __decrementable(const const_iterator* __i) const;
798 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
799 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
800
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400801#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000802
Howard Hinnantc51e1022010-05-11 19:42:16 +0000803private:
Howard Hinnantcf823322010-12-17 14:46:43 +0000804 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Eric Fiselier69c51982016-12-28 06:06:09 +0000805 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(pointer __new_last);
Marshall Clow3ff48e02018-05-22 16:20:28 +0000806 void __vallocate(size_type __n);
807 void __vdeallocate() _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000808 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
Howard Hinnanta5589382011-01-04 19:53:31 +0000809 void __construct_at_end(size_type __n);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000810 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000811 void __construct_at_end(size_type __n, const_reference __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000812 template <class _ForwardIterator>
813 typename enable_if
814 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500815 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000816 void
817 >::type
Eric Fiselieracfe6f02015-03-31 16:54:19 +0000818 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000819 void __append(size_type __n);
820 void __append(size_type __n, const_reference __x);
Howard Hinnantcf823322010-12-17 14:46:43 +0000821 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000822 iterator __make_iter(pointer __p) _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000823 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000824 const_iterator __make_iter(const_pointer __p) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000825 void __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v);
826 pointer __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p);
827 void __move_range(pointer __from_s, pointer __from_e, pointer __to);
Howard Hinnant1c936782011-06-03 19:40:40 +0000828 void __move_assign(vector& __c, true_type)
829 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000830 void __move_assign(vector& __c, false_type)
831 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000832 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76053d72013-06-27 19:35:32 +0000833 void __destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000834 {
Eric Fiselier69c51982016-12-28 06:06:09 +0000835 __invalidate_iterators_past(__new_last);
Marshall Clow2cd9d372014-05-08 14:14:06 +0000836 size_type __old_size = size();
Howard Hinnant27e0e772011-09-14 18:33:51 +0000837 __base::__destruct_at_end(__new_last);
Marshall Clow2cd9d372014-05-08 14:14:06 +0000838 __annotate_shrink(__old_size);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000839 }
Eric Fiseliered9e9362017-04-16 02:40:45 +0000840
841#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier7110f442019-03-19 19:19:44 +0000842 template <class _Up>
843 _LIBCPP_INLINE_VISIBILITY
844 inline void __push_back_slow_path(_Up&& __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000845
Howard Hinnantb6c49562012-02-26 15:30:12 +0000846 template <class... _Args>
Eric Fiselier7110f442019-03-19 19:19:44 +0000847 _LIBCPP_INLINE_VISIBILITY
848 inline void __emplace_back_slow_path(_Args&&... __args);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000849#else
Eric Fiselier7110f442019-03-19 19:19:44 +0000850 template <class _Up>
851 _LIBCPP_INLINE_VISIBILITY
852 inline void __push_back_slow_path(_Up& __x);
Howard Hinnantb6c49562012-02-26 15:30:12 +0000853#endif
Eric Fiseliered9e9362017-04-16 02:40:45 +0000854
Marshall Clow2cd9d372014-05-08 14:14:06 +0000855 // The following functions are no-ops outside of AddressSanitizer mode.
856 // We call annotatations only for the default Allocator because other allocators
857 // may not meet the AddressSanitizer alignment constraints.
858 // See the documentation for __sanitizer_annotate_contiguous_container for more details.
Marshall Clow2cd9d372014-05-08 14:14:06 +0000859#ifndef _LIBCPP_HAS_NO_ASAN
Eric Fiselier6003c772016-12-23 23:37:52 +0000860 void __annotate_contiguous_container(const void *__beg, const void *__end,
861 const void *__old_mid,
862 const void *__new_mid) const
863 {
864
Marshall Clow2cd9d372014-05-08 14:14:06 +0000865 if (__beg && is_same<allocator_type, __default_allocator_type>::value)
866 __sanitizer_annotate_contiguous_container(__beg, __end, __old_mid, __new_mid);
Marshall Clow2cd9d372014-05-08 14:14:06 +0000867 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000868#else
869 _LIBCPP_INLINE_VISIBILITY
870 void __annotate_contiguous_container(const void*, const void*, const void*,
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000871 const void*) const _NOEXCEPT {}
Eric Fiselier6003c772016-12-23 23:37:52 +0000872#endif
873 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000874 void __annotate_new(size_type __current_size) const _NOEXCEPT {
Marshall Clow2cd9d372014-05-08 14:14:06 +0000875 __annotate_contiguous_container(data(), data() + capacity(),
876 data() + capacity(), data() + __current_size);
877 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000878
879 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000880 void __annotate_delete() const _NOEXCEPT {
Marshall Clow2cd9d372014-05-08 14:14:06 +0000881 __annotate_contiguous_container(data(), data() + capacity(),
882 data() + size(), data() + capacity());
883 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000884
885 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000886 void __annotate_increase(size_type __n) const _NOEXCEPT
Marshall Clow2cd9d372014-05-08 14:14:06 +0000887 {
888 __annotate_contiguous_container(data(), data() + capacity(),
889 data() + size(), data() + size() + __n);
890 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000891
892 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000893 void __annotate_shrink(size_type __old_size) const _NOEXCEPT
Marshall Clow2cd9d372014-05-08 14:14:06 +0000894 {
895 __annotate_contiguous_container(data(), data() + capacity(),
896 data() + __old_size, data() + size());
897 }
Marshall Clowc78ffa52014-09-03 21:37:43 +0000898
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000899 struct _ConstructTransaction {
900 explicit _ConstructTransaction(vector &__v, size_type __n)
901 : __v_(__v), __pos_(__v.__end_), __new_end_(__v.__end_ + __n) {
902#ifndef _LIBCPP_HAS_NO_ASAN
903 __v_.__annotate_increase(__n);
904#endif
905 }
906 ~_ConstructTransaction() {
907 __v_.__end_ = __pos_;
908#ifndef _LIBCPP_HAS_NO_ASAN
909 if (__pos_ != __new_end_) {
910 __v_.__annotate_shrink(__new_end_ - __v_.__begin_);
911 }
912#endif
913 }
914
915 vector &__v_;
916 pointer __pos_;
917 const_pointer const __new_end_;
918
919 private:
920 _ConstructTransaction(_ConstructTransaction const&) = delete;
921 _ConstructTransaction& operator=(_ConstructTransaction const&) = delete;
922 };
923
924 template <class ..._Args>
925 _LIBCPP_INLINE_VISIBILITY
926 void __construct_one_at_end(_Args&& ...__args) {
927 _ConstructTransaction __tx(*this, 1);
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500928 __alloc_traits::construct(this->__alloc(), _VSTD::__to_address(__tx.__pos_),
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000929 _VSTD::forward<_Args>(__args)...);
930 ++__tx.__pos_;
931 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000932};
933
Marshall Clowf0ca1492018-05-21 21:30:12 +0000934#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
935template<class _InputIterator,
Arthur O'Dwyer56226762021-03-03 23:02:20 -0500936 class _Alloc = allocator<__iter_value_type<_InputIterator>>,
937 class = _EnableIf<__is_allocator<_Alloc>::value>
Marshall Clowf0ca1492018-05-21 21:30:12 +0000938 >
939vector(_InputIterator, _InputIterator)
Arthur O'Dwyer56226762021-03-03 23:02:20 -0500940 -> vector<__iter_value_type<_InputIterator>, _Alloc>;
Marshall Clowf0ca1492018-05-21 21:30:12 +0000941
942template<class _InputIterator,
943 class _Alloc,
Arthur O'Dwyer56226762021-03-03 23:02:20 -0500944 class = _EnableIf<__is_allocator<_Alloc>::value>
Marshall Clowf0ca1492018-05-21 21:30:12 +0000945 >
946vector(_InputIterator, _InputIterator, _Alloc)
Arthur O'Dwyer56226762021-03-03 23:02:20 -0500947 -> vector<__iter_value_type<_InputIterator>, _Alloc>;
Marshall Clowf0ca1492018-05-21 21:30:12 +0000948#endif
949
Howard Hinnantc51e1022010-05-11 19:42:16 +0000950template <class _Tp, class _Allocator>
951void
952vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v)
953{
Eric Fiselier909fe962019-09-13 16:09:33 +0000954
Marshall Clow2cd9d372014-05-08 14:14:06 +0000955 __annotate_delete();
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -0500956 _VSTD::__construct_backward_with_exception_guarantees(this->__alloc(), this->__begin_, this->__end_, __v.__begin_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000957 _VSTD::swap(this->__begin_, __v.__begin_);
958 _VSTD::swap(this->__end_, __v.__end_);
959 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000960 __v.__first_ = __v.__begin_;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000961 __annotate_new(size());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000962 __invalidate_all_iterators();
963}
964
965template <class _Tp, class _Allocator>
966typename vector<_Tp, _Allocator>::pointer
967vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p)
968{
Marshall Clow2cd9d372014-05-08 14:14:06 +0000969 __annotate_delete();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000970 pointer __r = __v.__begin_;
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -0500971 _VSTD::__construct_backward_with_exception_guarantees(this->__alloc(), this->__begin_, __p, __v.__begin_);
972 _VSTD::__construct_forward_with_exception_guarantees(this->__alloc(), __p, this->__end_, __v.__end_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000973 _VSTD::swap(this->__begin_, __v.__begin_);
974 _VSTD::swap(this->__end_, __v.__end_);
975 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000976 __v.__first_ = __v.__begin_;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000977 __annotate_new(size());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000978 __invalidate_all_iterators();
979 return __r;
980}
981
982// Allocate space for __n objects
983// throws length_error if __n > max_size()
984// throws (probably bad_alloc) if memory run out
985// Precondition: __begin_ == __end_ == __end_cap() == 0
986// Precondition: __n > 0
987// Postcondition: capacity() == __n
988// Postcondition: size() == 0
989template <class _Tp, class _Allocator>
990void
Marshall Clow3ff48e02018-05-22 16:20:28 +0000991vector<_Tp, _Allocator>::__vallocate(size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000992{
993 if (__n > max_size())
994 this->__throw_length_error();
995 this->__begin_ = this->__end_ = __alloc_traits::allocate(this->__alloc(), __n);
996 this->__end_cap() = this->__begin_ + __n;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000997 __annotate_new(0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000998}
999
1000template <class _Tp, class _Allocator>
1001void
Marshall Clow3ff48e02018-05-22 16:20:28 +00001002vector<_Tp, _Allocator>::__vdeallocate() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001003{
Howard Hinnant76053d72013-06-27 19:35:32 +00001004 if (this->__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001005 {
1006 clear();
1007 __alloc_traits::deallocate(this->__alloc(), this->__begin_, capacity());
Howard Hinnant76053d72013-06-27 19:35:32 +00001008 this->__begin_ = this->__end_ = this->__end_cap() = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001009 }
1010}
1011
1012template <class _Tp, class _Allocator>
1013typename vector<_Tp, _Allocator>::size_type
Howard Hinnant1c936782011-06-03 19:40:40 +00001014vector<_Tp, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001015{
Eric Fiselierb5d9f442016-11-23 01:18:56 +00001016 return _VSTD::min<size_type>(__alloc_traits::max_size(this->__alloc()),
1017 numeric_limits<difference_type>::max());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001018}
1019
1020// Precondition: __new_size > capacity()
1021template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001022inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001023typename vector<_Tp, _Allocator>::size_type
1024vector<_Tp, _Allocator>::__recommend(size_type __new_size) const
1025{
1026 const size_type __ms = max_size();
1027 if (__new_size > __ms)
1028 this->__throw_length_error();
1029 const size_type __cap = capacity();
1030 if (__cap >= __ms / 2)
1031 return __ms;
Arthur O'Dwyerc0fb14b2021-07-26 18:23:00 -04001032 return _VSTD::max<size_type>(2 * __cap, __new_size);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001033}
1034
1035// Default constructs __n objects starting at __end_
1036// throws if construction throws
1037// Precondition: __n > 0
1038// Precondition: size() + __n <= capacity()
1039// Postcondition: size() == size() + __n
1040template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001041void
1042vector<_Tp, _Allocator>::__construct_at_end(size_type __n)
1043{
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001044 _ConstructTransaction __tx(*this, __n);
Martijn Vels1e2e1b82020-06-18 13:14:02 -04001045 const_pointer __new_end = __tx.__new_end_;
1046 for (pointer __pos = __tx.__pos_; __pos != __new_end; ++__pos, __tx.__pos_ = __pos) {
1047 __alloc_traits::construct(this->__alloc(), _VSTD::__to_address(__pos));
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001048 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001049}
1050
Howard Hinnantc51e1022010-05-11 19:42:16 +00001051// Copy constructs __n objects starting at __end_ from __x
1052// throws if construction throws
1053// Precondition: __n > 0
1054// Precondition: size() + __n <= capacity()
1055// Postcondition: size() == old size() + __n
1056// Postcondition: [i] == __x for all i in [size() - __n, __n)
1057template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001058inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001059void
1060vector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
1061{
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001062 _ConstructTransaction __tx(*this, __n);
Martijn Vels1e2e1b82020-06-18 13:14:02 -04001063 const_pointer __new_end = __tx.__new_end_;
1064 for (pointer __pos = __tx.__pos_; __pos != __new_end; ++__pos, __tx.__pos_ = __pos) {
1065 __alloc_traits::construct(this->__alloc(), _VSTD::__to_address(__pos), __x);
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001066 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001067}
1068
1069template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001070template <class _ForwardIterator>
1071typename enable_if
1072<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001073 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001074 void
1075>::type
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001076vector<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001077{
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001078 _ConstructTransaction __tx(*this, __n);
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05001079 _VSTD::__construct_range_forward(this->__alloc(), __first, __last, __tx.__pos_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001080}
1081
1082// Default constructs __n objects starting at __end_
1083// throws if construction throws
1084// Postcondition: size() == size() + __n
Howard Hinnant1c936782011-06-03 19:40:40 +00001085// Exception safety: strong.
Howard Hinnantc51e1022010-05-11 19:42:16 +00001086template <class _Tp, class _Allocator>
1087void
1088vector<_Tp, _Allocator>::__append(size_type __n)
1089{
1090 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1091 this->__construct_at_end(__n);
1092 else
1093 {
1094 allocator_type& __a = this->__alloc();
1095 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1096 __v.__construct_at_end(__n);
1097 __swap_out_circular_buffer(__v);
1098 }
1099}
1100
1101// Default constructs __n objects starting at __end_
1102// throws if construction throws
1103// Postcondition: size() == size() + __n
Howard Hinnant1c936782011-06-03 19:40:40 +00001104// Exception safety: strong.
Howard Hinnantc51e1022010-05-11 19:42:16 +00001105template <class _Tp, class _Allocator>
1106void
1107vector<_Tp, _Allocator>::__append(size_type __n, const_reference __x)
1108{
1109 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1110 this->__construct_at_end(__n, __x);
1111 else
1112 {
1113 allocator_type& __a = this->__alloc();
1114 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1115 __v.__construct_at_end(__n, __x);
1116 __swap_out_circular_buffer(__v);
1117 }
1118}
1119
1120template <class _Tp, class _Allocator>
1121vector<_Tp, _Allocator>::vector(size_type __n)
1122{
Louis Dionneba400782020-10-02 15:02:52 -04001123#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001124 __get_db()->__insert_c(this);
1125#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001126 if (__n > 0)
1127 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001128 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001129 __construct_at_end(__n);
1130 }
1131}
1132
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00001133#if _LIBCPP_STD_VER > 11
1134template <class _Tp, class _Allocator>
1135vector<_Tp, _Allocator>::vector(size_type __n, const allocator_type& __a)
1136 : __base(__a)
1137{
Louis Dionneba400782020-10-02 15:02:52 -04001138#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00001139 __get_db()->__insert_c(this);
1140#endif
1141 if (__n > 0)
1142 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001143 __vallocate(__n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00001144 __construct_at_end(__n);
1145 }
1146}
1147#endif
1148
Howard Hinnantc51e1022010-05-11 19:42:16 +00001149template <class _Tp, class _Allocator>
Marshall Clowf0ca1492018-05-21 21:30:12 +00001150vector<_Tp, _Allocator>::vector(size_type __n, const value_type& __x)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001151{
Louis Dionneba400782020-10-02 15:02:52 -04001152#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001153 __get_db()->__insert_c(this);
1154#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001155 if (__n > 0)
1156 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001157 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001158 __construct_at_end(__n, __x);
1159 }
1160}
1161
1162template <class _Tp, class _Allocator>
Marshall Clowf0ca1492018-05-21 21:30:12 +00001163vector<_Tp, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001164 : __base(__a)
1165{
Louis Dionneba400782020-10-02 15:02:52 -04001166#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001167 __get_db()->__insert_c(this);
1168#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001169 if (__n > 0)
1170 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001171 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001172 __construct_at_end(__n, __x);
1173 }
1174}
1175
1176template <class _Tp, class _Allocator>
1177template <class _InputIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +00001178vector<_Tp, _Allocator>::vector(_InputIterator __first,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001179 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
1180 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001181 is_constructible<
1182 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +00001183 typename iterator_traits<_InputIterator>::reference>::value,
1184 _InputIterator>::type __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001185{
Louis Dionneba400782020-10-02 15:02:52 -04001186#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001187 __get_db()->__insert_c(this);
1188#endif
Howard Hinnant9cd22302011-09-16 18:41:29 +00001189 for (; __first != __last; ++__first)
Eric Fiselier96919722017-10-17 13:03:17 +00001190 __emplace_back(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001191}
1192
1193template <class _Tp, class _Allocator>
1194template <class _InputIterator>
1195vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001196 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
1197 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001198 is_constructible<
1199 value_type,
1200 typename iterator_traits<_InputIterator>::reference>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001201 : __base(__a)
1202{
Louis Dionneba400782020-10-02 15:02:52 -04001203#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001204 __get_db()->__insert_c(this);
1205#endif
Howard Hinnant9cd22302011-09-16 18:41:29 +00001206 for (; __first != __last; ++__first)
Eric Fiselier96919722017-10-17 13:03:17 +00001207 __emplace_back(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001208}
1209
1210template <class _Tp, class _Allocator>
1211template <class _ForwardIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +00001212vector<_Tp, _Allocator>::vector(_ForwardIterator __first,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001213 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001214 is_constructible<
1215 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +00001216 typename iterator_traits<_ForwardIterator>::reference>::value,
1217 _ForwardIterator>::type __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001218{
Louis Dionneba400782020-10-02 15:02:52 -04001219#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001220 __get_db()->__insert_c(this);
1221#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001222 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001223 if (__n > 0)
1224 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001225 __vallocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001226 __construct_at_end(__first, __last, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001227 }
1228}
1229
1230template <class _Tp, class _Allocator>
1231template <class _ForwardIterator>
1232vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001233 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001234 is_constructible<
1235 value_type,
1236 typename iterator_traits<_ForwardIterator>::reference>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001237 : __base(__a)
1238{
Louis Dionneba400782020-10-02 15:02:52 -04001239#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001240 __get_db()->__insert_c(this);
1241#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001242 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001243 if (__n > 0)
1244 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001245 __vallocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001246 __construct_at_end(__first, __last, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001247 }
1248}
1249
1250template <class _Tp, class _Allocator>
1251vector<_Tp, _Allocator>::vector(const vector& __x)
1252 : __base(__alloc_traits::select_on_container_copy_construction(__x.__alloc()))
1253{
Louis Dionneba400782020-10-02 15:02:52 -04001254#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001255 __get_db()->__insert_c(this);
1256#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001257 size_type __n = __x.size();
1258 if (__n > 0)
1259 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001260 __vallocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001261 __construct_at_end(__x.__begin_, __x.__end_, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001262 }
1263}
1264
1265template <class _Tp, class _Allocator>
Arthur O'Dwyer9b9662f2021-03-01 17:08:24 -05001266vector<_Tp, _Allocator>::vector(const vector& __x, const __identity_t<allocator_type>& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001267 : __base(__a)
1268{
Louis Dionneba400782020-10-02 15:02:52 -04001269#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001270 __get_db()->__insert_c(this);
1271#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001272 size_type __n = __x.size();
1273 if (__n > 0)
1274 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001275 __vallocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001276 __construct_at_end(__x.__begin_, __x.__end_, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001277 }
1278}
1279
Eric Fiseliered9e9362017-04-16 02:40:45 +00001280#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001281
1282template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001283inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001284vector<_Tp, _Allocator>::vector(vector&& __x)
Marshall Clowe5108202015-07-14 14:46:32 +00001285#if _LIBCPP_STD_VER > 14
1286 _NOEXCEPT
1287#else
Howard Hinnant1c936782011-06-03 19:40:40 +00001288 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowe5108202015-07-14 14:46:32 +00001289#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001290 : __base(_VSTD::move(__x.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001291{
Louis Dionneba400782020-10-02 15:02:52 -04001292#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001293 __get_db()->__insert_c(this);
Howard Hinnanta1e60cd2011-09-19 16:34:29 +00001294 __get_db()->swap(this, &__x);
Howard Hinnant27e0e772011-09-14 18:33:51 +00001295#endif
Howard Hinnant9cd22302011-09-16 18:41:29 +00001296 this->__begin_ = __x.__begin_;
1297 this->__end_ = __x.__end_;
1298 this->__end_cap() = __x.__end_cap();
Howard Hinnant76053d72013-06-27 19:35:32 +00001299 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001300}
1301
1302template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001303inline _LIBCPP_INLINE_VISIBILITY
Arthur O'Dwyer9b9662f2021-03-01 17:08:24 -05001304vector<_Tp, _Allocator>::vector(vector&& __x, const __identity_t<allocator_type>& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001305 : __base(__a)
1306{
Louis Dionneba400782020-10-02 15:02:52 -04001307#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001308 __get_db()->__insert_c(this);
1309#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001310 if (__a == __x.__alloc())
1311 {
1312 this->__begin_ = __x.__begin_;
1313 this->__end_ = __x.__end_;
1314 this->__end_cap() = __x.__end_cap();
1315 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Louis Dionneba400782020-10-02 15:02:52 -04001316#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnanta1e60cd2011-09-19 16:34:29 +00001317 __get_db()->swap(this, &__x);
1318#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001319 }
1320 else
1321 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001322 typedef move_iterator<iterator> _Ip;
1323 assign(_Ip(__x.begin()), _Ip(__x.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001324 }
1325}
1326
1327template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001328inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001329vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il)
1330{
Louis Dionneba400782020-10-02 15:02:52 -04001331#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001332 __get_db()->__insert_c(this);
1333#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001334 if (__il.size() > 0)
1335 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001336 __vallocate(__il.size());
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001337 __construct_at_end(__il.begin(), __il.end(), __il.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001338 }
1339}
1340
1341template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001342inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001343vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
1344 : __base(__a)
1345{
Louis Dionneba400782020-10-02 15:02:52 -04001346#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001347 __get_db()->__insert_c(this);
1348#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001349 if (__il.size() > 0)
1350 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001351 __vallocate(__il.size());
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001352 __construct_at_end(__il.begin(), __il.end(), __il.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001353 }
1354}
1355
1356template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001357inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001358vector<_Tp, _Allocator>&
1359vector<_Tp, _Allocator>::operator=(vector&& __x)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001360 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001361{
1362 __move_assign(__x, integral_constant<bool,
1363 __alloc_traits::propagate_on_container_move_assignment::value>());
1364 return *this;
1365}
1366
1367template <class _Tp, class _Allocator>
1368void
1369vector<_Tp, _Allocator>::__move_assign(vector& __c, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001370 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001371{
1372 if (__base::__alloc() != __c.__alloc())
1373 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001374 typedef move_iterator<iterator> _Ip;
1375 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001376 }
1377 else
1378 __move_assign(__c, true_type());
1379}
1380
1381template <class _Tp, class _Allocator>
1382void
1383vector<_Tp, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00001384 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001385{
Marshall Clow3ff48e02018-05-22 16:20:28 +00001386 __vdeallocate();
Marshall Clow136d45c2014-07-21 15:11:13 +00001387 __base::__move_assign_alloc(__c); // this can throw
Howard Hinnantc51e1022010-05-11 19:42:16 +00001388 this->__begin_ = __c.__begin_;
1389 this->__end_ = __c.__end_;
1390 this->__end_cap() = __c.__end_cap();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001391 __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
Louis Dionneba400782020-10-02 15:02:52 -04001392#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnanta1e60cd2011-09-19 16:34:29 +00001393 __get_db()->swap(this, &__c);
1394#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001395}
1396
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001397#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001398
1399template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001400inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001401vector<_Tp, _Allocator>&
1402vector<_Tp, _Allocator>::operator=(const vector& __x)
1403{
1404 if (this != &__x)
1405 {
1406 __base::__copy_assign_alloc(__x);
1407 assign(__x.__begin_, __x.__end_);
1408 }
1409 return *this;
1410}
1411
1412template <class _Tp, class _Allocator>
1413template <class _InputIterator>
1414typename enable_if
1415<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001416 __is_cpp17_input_iterator <_InputIterator>::value &&
1417 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001418 is_constructible<
1419 _Tp,
1420 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001421 void
1422>::type
1423vector<_Tp, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
1424{
1425 clear();
1426 for (; __first != __last; ++__first)
Eric Fiselier96919722017-10-17 13:03:17 +00001427 __emplace_back(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001428}
1429
1430template <class _Tp, class _Allocator>
1431template <class _ForwardIterator>
1432typename enable_if
1433<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001434 __is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001435 is_constructible<
1436 _Tp,
1437 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001438 void
1439>::type
1440vector<_Tp, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
1441{
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001442 size_type __new_size = static_cast<size_type>(_VSTD::distance(__first, __last));
1443 if (__new_size <= capacity())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001444 {
1445 _ForwardIterator __mid = __last;
1446 bool __growing = false;
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001447 if (__new_size > size())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001448 {
1449 __growing = true;
1450 __mid = __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001451 _VSTD::advance(__mid, size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001452 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001453 pointer __m = _VSTD::copy(__first, __mid, this->__begin_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001454 if (__growing)
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001455 __construct_at_end(__mid, __last, __new_size - size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001456 else
1457 this->__destruct_at_end(__m);
1458 }
1459 else
1460 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001461 __vdeallocate();
1462 __vallocate(__recommend(__new_size));
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001463 __construct_at_end(__first, __last, __new_size);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001464 }
Eric Fiselier69c51982016-12-28 06:06:09 +00001465 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001466}
1467
1468template <class _Tp, class _Allocator>
1469void
1470vector<_Tp, _Allocator>::assign(size_type __n, const_reference __u)
1471{
1472 if (__n <= capacity())
1473 {
1474 size_type __s = size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001475 _VSTD::fill_n(this->__begin_, _VSTD::min(__n, __s), __u);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001476 if (__n > __s)
1477 __construct_at_end(__n - __s, __u);
1478 else
Howard Hinnant155c2af2010-05-24 17:49:41 +00001479 this->__destruct_at_end(this->__begin_ + __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001480 }
1481 else
1482 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001483 __vdeallocate();
1484 __vallocate(__recommend(static_cast<size_type>(__n)));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001485 __construct_at_end(__n, __u);
1486 }
Eric Fiselier69c51982016-12-28 06:06:09 +00001487 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001488}
1489
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001490template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001491inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001492typename vector<_Tp, _Allocator>::iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001493vector<_Tp, _Allocator>::__make_iter(pointer __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001494{
Louis Dionneba400782020-10-02 15:02:52 -04001495#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001496 return iterator(this, __p);
1497#else
1498 return iterator(__p);
1499#endif
1500}
1501
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001502template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001503inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001504typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001505vector<_Tp, _Allocator>::__make_iter(const_pointer __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001506{
Louis Dionneba400782020-10-02 15:02:52 -04001507#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001508 return const_iterator(this, __p);
1509#else
1510 return const_iterator(__p);
1511#endif
1512}
1513
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001514template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001515inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001516typename vector<_Tp, _Allocator>::iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001517vector<_Tp, _Allocator>::begin() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001518{
1519 return __make_iter(this->__begin_);
1520}
1521
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001522template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001523inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001524typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001525vector<_Tp, _Allocator>::begin() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001526{
1527 return __make_iter(this->__begin_);
1528}
1529
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001530template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001531inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001532typename vector<_Tp, _Allocator>::iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001533vector<_Tp, _Allocator>::end() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001534{
1535 return __make_iter(this->__end_);
1536}
1537
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001538template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001539inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001540typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001541vector<_Tp, _Allocator>::end() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001542{
1543 return __make_iter(this->__end_);
1544}
1545
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001546template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001547inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001548typename vector<_Tp, _Allocator>::reference
Marshall Clowd6470492019-03-15 00:29:35 +00001549vector<_Tp, _Allocator>::operator[](size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001550{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001551 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001552 return this->__begin_[__n];
1553}
1554
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001555template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001556inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001557typename vector<_Tp, _Allocator>::const_reference
Marshall Clowd6470492019-03-15 00:29:35 +00001558vector<_Tp, _Allocator>::operator[](size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001559{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001560 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001561 return this->__begin_[__n];
1562}
1563
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001564template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001565typename vector<_Tp, _Allocator>::reference
1566vector<_Tp, _Allocator>::at(size_type __n)
1567{
1568 if (__n >= size())
1569 this->__throw_out_of_range();
1570 return this->__begin_[__n];
1571}
1572
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001573template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001574typename vector<_Tp, _Allocator>::const_reference
1575vector<_Tp, _Allocator>::at(size_type __n) const
1576{
1577 if (__n >= size())
1578 this->__throw_out_of_range();
1579 return this->__begin_[__n];
1580}
1581
1582template <class _Tp, class _Allocator>
1583void
1584vector<_Tp, _Allocator>::reserve(size_type __n)
1585{
1586 if (__n > capacity())
1587 {
1588 allocator_type& __a = this->__alloc();
Howard Hinnant1c936782011-06-03 19:40:40 +00001589 __split_buffer<value_type, allocator_type&> __v(__n, size(), __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001590 __swap_out_circular_buffer(__v);
1591 }
1592}
1593
1594template <class _Tp, class _Allocator>
1595void
Howard Hinnant1c936782011-06-03 19:40:40 +00001596vector<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001597{
1598 if (capacity() > size())
1599 {
1600#ifndef _LIBCPP_NO_EXCEPTIONS
1601 try
1602 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001603#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001604 allocator_type& __a = this->__alloc();
Howard Hinnant1c936782011-06-03 19:40:40 +00001605 __split_buffer<value_type, allocator_type&> __v(size(), size(), __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001606 __swap_out_circular_buffer(__v);
1607#ifndef _LIBCPP_NO_EXCEPTIONS
1608 }
1609 catch (...)
1610 {
1611 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001612#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001613 }
1614}
1615
1616template <class _Tp, class _Allocator>
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001617template <class _Up>
1618void
Eric Fiseliered9e9362017-04-16 02:40:45 +00001619#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001620vector<_Tp, _Allocator>::__push_back_slow_path(_Up&& __x)
1621#else
1622vector<_Tp, _Allocator>::__push_back_slow_path(_Up& __x)
1623#endif
1624{
1625 allocator_type& __a = this->__alloc();
1626 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1627 // __v.push_back(_VSTD::forward<_Up>(__x));
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001628 __alloc_traits::construct(__a, _VSTD::__to_address(__v.__end_), _VSTD::forward<_Up>(__x));
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001629 __v.__end_++;
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001630 __swap_out_circular_buffer(__v);
1631}
1632
1633template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001634inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001635void
1636vector<_Tp, _Allocator>::push_back(const_reference __x)
1637{
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001638 if (this->__end_ != this->__end_cap())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001639 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001640 __construct_one_at_end(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001641 }
1642 else
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001643 __push_back_slow_path(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001644}
1645
Eric Fiseliered9e9362017-04-16 02:40:45 +00001646#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001647
1648template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001649inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001650void
1651vector<_Tp, _Allocator>::push_back(value_type&& __x)
1652{
1653 if (this->__end_ < this->__end_cap())
1654 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001655 __construct_one_at_end(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001656 }
1657 else
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001658 __push_back_slow_path(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001659}
1660
1661template <class _Tp, class _Allocator>
1662template <class... _Args>
1663void
Howard Hinnantb6c49562012-02-26 15:30:12 +00001664vector<_Tp, _Allocator>::__emplace_back_slow_path(_Args&&... __args)
1665{
1666 allocator_type& __a = this->__alloc();
1667 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1668// __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001669 __alloc_traits::construct(__a, _VSTD::__to_address(__v.__end_), _VSTD::forward<_Args>(__args)...);
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001670 __v.__end_++;
Howard Hinnantb6c49562012-02-26 15:30:12 +00001671 __swap_out_circular_buffer(__v);
1672}
1673
1674template <class _Tp, class _Allocator>
1675template <class... _Args>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001676inline
Marshall Clowea52cc42017-01-24 23:09:12 +00001677#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001678typename vector<_Tp, _Allocator>::reference
Marshall Clowea52cc42017-01-24 23:09:12 +00001679#else
1680void
1681#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001682vector<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1683{
1684 if (this->__end_ < this->__end_cap())
1685 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001686 __construct_one_at_end(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001687 }
1688 else
Howard Hinnantb6c49562012-02-26 15:30:12 +00001689 __emplace_back_slow_path(_VSTD::forward<_Args>(__args)...);
Marshall Clowea52cc42017-01-24 23:09:12 +00001690#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001691 return this->back();
Marshall Clowea52cc42017-01-24 23:09:12 +00001692#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001693}
1694
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001695#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001696
1697template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001698inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001699void
1700vector<_Tp, _Allocator>::pop_back()
1701{
Kristina Bessonovaaeeaa7e2021-05-09 19:29:56 +02001702 _LIBCPP_ASSERT(!empty(), "vector::pop_back called on an empty vector");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001703 this->__destruct_at_end(this->__end_ - 1);
1704}
1705
1706template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001707inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001708typename vector<_Tp, _Allocator>::iterator
1709vector<_Tp, _Allocator>::erase(const_iterator __position)
1710{
Louis Dionneba400782020-10-02 15:02:52 -04001711#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001712 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1713 "vector::erase(iterator) called with an iterator not"
1714 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001715#endif
Howard Hinnant58d52d52013-03-25 22:12:26 +00001716 _LIBCPP_ASSERT(__position != end(),
1717 "vector::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnant76053d72013-06-27 19:35:32 +00001718 difference_type __ps = __position - cbegin();
1719 pointer __p = this->__begin_ + __ps;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001720 this->__destruct_at_end(_VSTD::move(__p + 1, this->__end_, __p));
Eric Fiselier69c51982016-12-28 06:06:09 +00001721 this->__invalidate_iterators_past(__p-1);
1722 iterator __r = __make_iter(__p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001723 return __r;
1724}
1725
1726template <class _Tp, class _Allocator>
1727typename vector<_Tp, _Allocator>::iterator
1728vector<_Tp, _Allocator>::erase(const_iterator __first, const_iterator __last)
1729{
Louis Dionneba400782020-10-02 15:02:52 -04001730#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001731 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
1732 "vector::erase(iterator, iterator) called with an iterator not"
1733 " referring to this vector");
Eric Fiselier69c51982016-12-28 06:06:09 +00001734 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__last) == this,
1735 "vector::erase(iterator, iterator) called with an iterator not"
1736 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001737#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001738 _LIBCPP_ASSERT(__first <= __last, "vector::erase(first, last) called with invalid range");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001739 pointer __p = this->__begin_ + (__first - begin());
Eric Fiselier69c51982016-12-28 06:06:09 +00001740 if (__first != __last) {
Howard Hinnantc580cc32013-04-18 15:02:57 +00001741 this->__destruct_at_end(_VSTD::move(__p + (__last - __first), this->__end_, __p));
Eric Fiselier69c51982016-12-28 06:06:09 +00001742 this->__invalidate_iterators_past(__p - 1);
1743 }
1744 iterator __r = __make_iter(__p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001745 return __r;
1746}
1747
1748template <class _Tp, class _Allocator>
1749void
1750vector<_Tp, _Allocator>::__move_range(pointer __from_s, pointer __from_e, pointer __to)
1751{
1752 pointer __old_last = this->__end_;
1753 difference_type __n = __old_last - __to;
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001754 {
1755 pointer __i = __from_s + __n;
1756 _ConstructTransaction __tx(*this, __from_e - __i);
Martijn Vels1e2e1b82020-06-18 13:14:02 -04001757 for (pointer __pos = __tx.__pos_; __i < __from_e;
1758 ++__i, ++__pos, __tx.__pos_ = __pos) {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001759 __alloc_traits::construct(this->__alloc(),
Martijn Vels1e2e1b82020-06-18 13:14:02 -04001760 _VSTD::__to_address(__pos),
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001761 _VSTD::move(*__i));
1762 }
1763 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001764 _VSTD::move_backward(__from_s, __from_s + __n, __old_last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001765}
1766
1767template <class _Tp, class _Allocator>
1768typename vector<_Tp, _Allocator>::iterator
1769vector<_Tp, _Allocator>::insert(const_iterator __position, const_reference __x)
1770{
Louis Dionneba400782020-10-02 15:02:52 -04001771#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001772 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1773 "vector::insert(iterator, x) called with an iterator not"
1774 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001775#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001776 pointer __p = this->__begin_ + (__position - begin());
1777 if (this->__end_ < this->__end_cap())
1778 {
1779 if (__p == this->__end_)
1780 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001781 __construct_one_at_end(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001782 }
1783 else
1784 {
1785 __move_range(__p, this->__end_, __p + 1);
1786 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1787 if (__p <= __xr && __xr < this->__end_)
1788 ++__xr;
1789 *__p = *__xr;
1790 }
1791 }
1792 else
1793 {
1794 allocator_type& __a = this->__alloc();
1795 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
1796 __v.push_back(__x);
1797 __p = __swap_out_circular_buffer(__v, __p);
1798 }
1799 return __make_iter(__p);
1800}
1801
Eric Fiseliered9e9362017-04-16 02:40:45 +00001802#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001803
1804template <class _Tp, class _Allocator>
1805typename vector<_Tp, _Allocator>::iterator
1806vector<_Tp, _Allocator>::insert(const_iterator __position, value_type&& __x)
1807{
Louis Dionneba400782020-10-02 15:02:52 -04001808#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001809 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1810 "vector::insert(iterator, x) called with an iterator not"
1811 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001812#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001813 pointer __p = this->__begin_ + (__position - begin());
1814 if (this->__end_ < this->__end_cap())
1815 {
1816 if (__p == this->__end_)
1817 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001818 __construct_one_at_end(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001819 }
1820 else
1821 {
1822 __move_range(__p, this->__end_, __p + 1);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001823 *__p = _VSTD::move(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001824 }
1825 }
1826 else
1827 {
1828 allocator_type& __a = this->__alloc();
1829 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001830 __v.push_back(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001831 __p = __swap_out_circular_buffer(__v, __p);
1832 }
1833 return __make_iter(__p);
1834}
1835
1836template <class _Tp, class _Allocator>
1837template <class... _Args>
1838typename vector<_Tp, _Allocator>::iterator
1839vector<_Tp, _Allocator>::emplace(const_iterator __position, _Args&&... __args)
1840{
Louis Dionneba400782020-10-02 15:02:52 -04001841#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001842 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1843 "vector::emplace(iterator, x) called with an iterator not"
1844 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001845#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001846 pointer __p = this->__begin_ + (__position - begin());
1847 if (this->__end_ < this->__end_cap())
1848 {
1849 if (__p == this->__end_)
1850 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001851 __construct_one_at_end(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001852 }
1853 else
1854 {
Marshall Clowa591b9a2016-07-11 21:38:08 +00001855 __temp_value<value_type, _Allocator> __tmp(this->__alloc(), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001856 __move_range(__p, this->__end_, __p + 1);
Marshall Clowa591b9a2016-07-11 21:38:08 +00001857 *__p = _VSTD::move(__tmp.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001858 }
1859 }
1860 else
1861 {
1862 allocator_type& __a = this->__alloc();
1863 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001864 __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001865 __p = __swap_out_circular_buffer(__v, __p);
1866 }
1867 return __make_iter(__p);
1868}
1869
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001870#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001871
1872template <class _Tp, class _Allocator>
1873typename vector<_Tp, _Allocator>::iterator
1874vector<_Tp, _Allocator>::insert(const_iterator __position, size_type __n, const_reference __x)
1875{
Louis Dionneba400782020-10-02 15:02:52 -04001876#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001877 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1878 "vector::insert(iterator, n, x) called with an iterator not"
1879 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001880#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001881 pointer __p = this->__begin_ + (__position - begin());
1882 if (__n > 0)
1883 {
1884 if (__n <= static_cast<size_type>(this->__end_cap() - this->__end_))
1885 {
1886 size_type __old_n = __n;
1887 pointer __old_last = this->__end_;
1888 if (__n > static_cast<size_type>(this->__end_ - __p))
1889 {
1890 size_type __cx = __n - (this->__end_ - __p);
1891 __construct_at_end(__cx, __x);
1892 __n -= __cx;
1893 }
1894 if (__n > 0)
1895 {
1896 __move_range(__p, __old_last, __p + __old_n);
1897 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1898 if (__p <= __xr && __xr < this->__end_)
1899 __xr += __old_n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001900 _VSTD::fill_n(__p, __n, *__xr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001901 }
1902 }
1903 else
1904 {
1905 allocator_type& __a = this->__alloc();
1906 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1907 __v.__construct_at_end(__n, __x);
1908 __p = __swap_out_circular_buffer(__v, __p);
1909 }
1910 }
1911 return __make_iter(__p);
1912}
1913
1914template <class _Tp, class _Allocator>
1915template <class _InputIterator>
1916typename enable_if
1917<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001918 __is_cpp17_input_iterator <_InputIterator>::value &&
1919 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001920 is_constructible<
1921 _Tp,
1922 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001923 typename vector<_Tp, _Allocator>::iterator
1924>::type
1925vector<_Tp, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
1926{
Louis Dionneba400782020-10-02 15:02:52 -04001927#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001928 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1929 "vector::insert(iterator, range) called with an iterator not"
1930 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001931#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001932 difference_type __off = __position - begin();
1933 pointer __p = this->__begin_ + __off;
1934 allocator_type& __a = this->__alloc();
1935 pointer __old_last = this->__end_;
1936 for (; this->__end_ != this->__end_cap() && __first != __last; ++__first)
1937 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001938 __construct_one_at_end(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001939 }
1940 __split_buffer<value_type, allocator_type&> __v(__a);
1941 if (__first != __last)
1942 {
1943#ifndef _LIBCPP_NO_EXCEPTIONS
1944 try
1945 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001946#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001947 __v.__construct_at_end(__first, __last);
1948 difference_type __old_size = __old_last - this->__begin_;
1949 difference_type __old_p = __p - this->__begin_;
1950 reserve(__recommend(size() + __v.size()));
1951 __p = this->__begin_ + __old_p;
1952 __old_last = this->__begin_ + __old_size;
1953#ifndef _LIBCPP_NO_EXCEPTIONS
1954 }
1955 catch (...)
1956 {
1957 erase(__make_iter(__old_last), end());
1958 throw;
1959 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001960#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001961 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001962 __p = _VSTD::rotate(__p, __old_last, this->__end_);
Louis Dionne202a4cb2020-02-11 10:59:12 +01001963 insert(__make_iter(__p), _VSTD::make_move_iterator(__v.begin()),
1964 _VSTD::make_move_iterator(__v.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001965 return begin() + __off;
1966}
1967
1968template <class _Tp, class _Allocator>
1969template <class _ForwardIterator>
1970typename enable_if
1971<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001972 __is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001973 is_constructible<
1974 _Tp,
1975 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001976 typename vector<_Tp, _Allocator>::iterator
1977>::type
1978vector<_Tp, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
1979{
Louis Dionneba400782020-10-02 15:02:52 -04001980#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001981 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1982 "vector::insert(iterator, range) called with an iterator not"
1983 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001984#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001985 pointer __p = this->__begin_ + (__position - begin());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001986 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001987 if (__n > 0)
1988 {
1989 if (__n <= this->__end_cap() - this->__end_)
1990 {
1991 size_type __old_n = __n;
1992 pointer __old_last = this->__end_;
1993 _ForwardIterator __m = __last;
1994 difference_type __dx = this->__end_ - __p;
1995 if (__n > __dx)
1996 {
1997 __m = __first;
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001998 difference_type __diff = this->__end_ - __p;
1999 _VSTD::advance(__m, __diff);
2000 __construct_at_end(__m, __last, __n - __diff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002001 __n = __dx;
2002 }
2003 if (__n > 0)
2004 {
2005 __move_range(__p, __old_last, __p + __old_n);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002006 _VSTD::copy(__first, __m, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002007 }
2008 }
2009 else
2010 {
2011 allocator_type& __a = this->__alloc();
2012 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
2013 __v.__construct_at_end(__first, __last);
2014 __p = __swap_out_circular_buffer(__v, __p);
2015 }
2016 }
2017 return __make_iter(__p);
2018}
2019
2020template <class _Tp, class _Allocator>
2021void
2022vector<_Tp, _Allocator>::resize(size_type __sz)
2023{
2024 size_type __cs = size();
2025 if (__cs < __sz)
2026 this->__append(__sz - __cs);
2027 else if (__cs > __sz)
2028 this->__destruct_at_end(this->__begin_ + __sz);
2029}
2030
2031template <class _Tp, class _Allocator>
2032void
2033vector<_Tp, _Allocator>::resize(size_type __sz, const_reference __x)
2034{
2035 size_type __cs = size();
2036 if (__cs < __sz)
2037 this->__append(__sz - __cs, __x);
2038 else if (__cs > __sz)
2039 this->__destruct_at_end(this->__begin_ + __sz);
2040}
2041
2042template <class _Tp, class _Allocator>
2043void
2044vector<_Tp, _Allocator>::swap(vector& __x)
Marshall Clow8982dcd2015-07-13 20:04:56 +00002045#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00002046 _NOEXCEPT
Marshall Clow8982dcd2015-07-13 20:04:56 +00002047#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00002048 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00002049 __is_nothrow_swappable<allocator_type>::value)
2050#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002051{
Howard Hinnant27e0e772011-09-14 18:33:51 +00002052 _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
2053 this->__alloc() == __x.__alloc(),
2054 "vector::swap: Either propagate_on_container_swap must be true"
2055 " or the allocators must compare equal");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002056 _VSTD::swap(this->__begin_, __x.__begin_);
2057 _VSTD::swap(this->__end_, __x.__end_);
2058 _VSTD::swap(this->__end_cap(), __x.__end_cap());
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05002059 _VSTD::__swap_allocator(this->__alloc(), __x.__alloc(),
Marshall Clow8982dcd2015-07-13 20:04:56 +00002060 integral_constant<bool,__alloc_traits::propagate_on_container_swap::value>());
Louis Dionneba400782020-10-02 15:02:52 -04002061#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002062 __get_db()->swap(this, &__x);
Louis Dionneba400782020-10-02 15:02:52 -04002063#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002064}
2065
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002066template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002067bool
2068vector<_Tp, _Allocator>::__invariants() const
2069{
Howard Hinnant76053d72013-06-27 19:35:32 +00002070 if (this->__begin_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002071 {
Howard Hinnant76053d72013-06-27 19:35:32 +00002072 if (this->__end_ != nullptr || this->__end_cap() != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002073 return false;
2074 }
2075 else
2076 {
2077 if (this->__begin_ > this->__end_)
2078 return false;
2079 if (this->__begin_ == this->__end_cap())
2080 return false;
2081 if (this->__end_ > this->__end_cap())
2082 return false;
2083 }
2084 return true;
2085}
2086
Louis Dionneba400782020-10-02 15:02:52 -04002087#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002088
Howard Hinnantc51e1022010-05-11 19:42:16 +00002089template <class _Tp, class _Allocator>
Howard Hinnant27e0e772011-09-14 18:33:51 +00002090bool
2091vector<_Tp, _Allocator>::__dereferenceable(const const_iterator* __i) const
2092{
2093 return this->__begin_ <= __i->base() && __i->base() < this->__end_;
2094}
2095
2096template <class _Tp, class _Allocator>
2097bool
2098vector<_Tp, _Allocator>::__decrementable(const const_iterator* __i) const
2099{
2100 return this->__begin_ < __i->base() && __i->base() <= this->__end_;
2101}
2102
2103template <class _Tp, class _Allocator>
2104bool
2105vector<_Tp, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
2106{
2107 const_pointer __p = __i->base() + __n;
2108 return this->__begin_ <= __p && __p <= this->__end_;
2109}
2110
2111template <class _Tp, class _Allocator>
2112bool
2113vector<_Tp, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
2114{
2115 const_pointer __p = __i->base() + __n;
2116 return this->__begin_ <= __p && __p < this->__end_;
2117}
2118
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002119#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002120
2121template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002122inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002123void
2124vector<_Tp, _Allocator>::__invalidate_all_iterators()
2125{
Louis Dionneba400782020-10-02 15:02:52 -04002126#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002127 __get_db()->__invalidate_all(this);
Louis Dionneba400782020-10-02 15:02:52 -04002128#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002129}
2130
Eric Fiselier69c51982016-12-28 06:06:09 +00002131
2132template <class _Tp, class _Allocator>
2133inline _LIBCPP_INLINE_VISIBILITY
2134void
2135vector<_Tp, _Allocator>::__invalidate_iterators_past(pointer __new_last) {
Louis Dionneba400782020-10-02 15:02:52 -04002136#if _LIBCPP_DEBUG_LEVEL == 2
Eric Fiselier69c51982016-12-28 06:06:09 +00002137 __c_node* __c = __get_db()->__find_c_and_lock(this);
2138 for (__i_node** __p = __c->end_; __p != __c->beg_; ) {
2139 --__p;
2140 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
2141 if (__i->base() > __new_last) {
2142 (*__p)->__c_ = nullptr;
2143 if (--__c->end_ != __p)
Arthur O'Dwyer22236632020-12-07 21:50:15 -05002144 _VSTD::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
Eric Fiselier69c51982016-12-28 06:06:09 +00002145 }
2146 }
2147 __get_db()->unlock();
2148#else
2149 ((void)__new_last);
2150#endif
2151}
2152
Howard Hinnantc51e1022010-05-11 19:42:16 +00002153// vector<bool>
2154
2155template <class _Allocator> class vector<bool, _Allocator>;
2156
2157template <class _Allocator> struct hash<vector<bool, _Allocator> >;
2158
2159template <class _Allocator>
Howard Hinnantb4ef6482011-07-02 20:33:23 +00002160struct __has_storage_type<vector<bool, _Allocator> >
2161{
2162 static const bool value = true;
2163};
2164
2165template <class _Allocator>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002166class _LIBCPP_TEMPLATE_VIS vector<bool, _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002167 : private __vector_base_common<true>
2168{
2169public:
2170 typedef vector __self;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002171 typedef bool value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002172 typedef _Allocator allocator_type;
2173 typedef allocator_traits<allocator_type> __alloc_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002174 typedef typename __alloc_traits::size_type size_type;
2175 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant896b7622012-05-07 16:50:38 +00002176 typedef size_type __storage_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002177 typedef __bit_iterator<vector, false> pointer;
2178 typedef __bit_iterator<vector, true> const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002179 typedef pointer iterator;
2180 typedef const_pointer const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002181 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
2182 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002183
2184private:
Marshall Clow940e01c2015-04-07 05:21:38 +00002185 typedef typename __rebind_alloc_helper<__alloc_traits, __storage_type>::type __storage_allocator;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002186 typedef allocator_traits<__storage_allocator> __storage_traits;
2187 typedef typename __storage_traits::pointer __storage_pointer;
2188 typedef typename __storage_traits::const_pointer __const_storage_pointer;
2189
2190 __storage_pointer __begin_;
2191 size_type __size_;
2192 __compressed_pair<size_type, __storage_allocator> __cap_alloc_;
Howard Hinnant71d1c192011-07-09 15:50:42 +00002193public:
Howard Hinnantb4ef6482011-07-02 20:33:23 +00002194 typedef __bit_reference<vector> reference;
2195 typedef __bit_const_reference<vector> const_reference;
Howard Hinnant71d1c192011-07-09 15:50:42 +00002196private:
Howard Hinnant1c936782011-06-03 19:40:40 +00002197 _LIBCPP_INLINE_VISIBILITY
2198 size_type& __cap() _NOEXCEPT
2199 {return __cap_alloc_.first();}
2200 _LIBCPP_INLINE_VISIBILITY
2201 const size_type& __cap() const _NOEXCEPT
2202 {return __cap_alloc_.first();}
2203 _LIBCPP_INLINE_VISIBILITY
2204 __storage_allocator& __alloc() _NOEXCEPT
2205 {return __cap_alloc_.second();}
2206 _LIBCPP_INLINE_VISIBILITY
2207 const __storage_allocator& __alloc() const _NOEXCEPT
2208 {return __cap_alloc_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002209
2210 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
2211
Howard Hinnant1c936782011-06-03 19:40:40 +00002212 _LIBCPP_INLINE_VISIBILITY
2213 static size_type __internal_cap_to_external(size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002214 {return __n * __bits_per_word;}
Howard Hinnant1c936782011-06-03 19:40:40 +00002215 _LIBCPP_INLINE_VISIBILITY
2216 static size_type __external_cap_to_internal(size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002217 {return (__n - 1) / __bits_per_word + 1;}
2218
2219public:
Howard Hinnant1c936782011-06-03 19:40:40 +00002220 _LIBCPP_INLINE_VISIBILITY
Marshall Clowe546cbb2015-06-04 02:05:41 +00002221 vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clow8f282f42015-06-04 00:10:20 +00002222
2223 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
2224#if _LIBCPP_STD_VER <= 14
2225 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
2226#else
2227 _NOEXCEPT;
2228#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002229 ~vector();
2230 explicit vector(size_type __n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00002231#if _LIBCPP_STD_VER > 11
2232 explicit vector(size_type __n, const allocator_type& __a);
2233#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002234 vector(size_type __n, const value_type& __v);
2235 vector(size_type __n, const value_type& __v, const allocator_type& __a);
2236 template <class _InputIterator>
2237 vector(_InputIterator __first, _InputIterator __last,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002238 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
2239 !__is_cpp17_forward_iterator<_InputIterator>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002240 template <class _InputIterator>
2241 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002242 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
2243 !__is_cpp17_forward_iterator<_InputIterator>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002244 template <class _ForwardIterator>
2245 vector(_ForwardIterator __first, _ForwardIterator __last,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002246 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002247 template <class _ForwardIterator>
2248 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002249 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002250
2251 vector(const vector& __v);
2252 vector(const vector& __v, const allocator_type& __a);
2253 vector& operator=(const vector& __v);
Eric Fiseliered9e9362017-04-16 02:40:45 +00002254
2255#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002256 vector(initializer_list<value_type> __il);
2257 vector(initializer_list<value_type> __il, const allocator_type& __a);
2258
Howard Hinnant1c936782011-06-03 19:40:40 +00002259 _LIBCPP_INLINE_VISIBILITY
2260 vector(vector&& __v)
Marshall Clowe5108202015-07-14 14:46:32 +00002261#if _LIBCPP_STD_VER > 14
2262 _NOEXCEPT;
2263#else
Howard Hinnant1c936782011-06-03 19:40:40 +00002264 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowe5108202015-07-14 14:46:32 +00002265#endif
Arthur O'Dwyer9b9662f2021-03-01 17:08:24 -05002266 vector(vector&& __v, const __identity_t<allocator_type>& __a);
Howard Hinnant1c936782011-06-03 19:40:40 +00002267 _LIBCPP_INLINE_VISIBILITY
2268 vector& operator=(vector&& __v)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002269 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiseliered9e9362017-04-16 02:40:45 +00002270
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002271 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002272 vector& operator=(initializer_list<value_type> __il)
2273 {assign(__il.begin(), __il.end()); return *this;}
Eric Fiseliered9e9362017-04-16 02:40:45 +00002274
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002275#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002276
2277 template <class _InputIterator>
2278 typename enable_if
2279 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002280 __is_cpp17_input_iterator<_InputIterator>::value &&
2281 !__is_cpp17_forward_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002282 void
2283 >::type
2284 assign(_InputIterator __first, _InputIterator __last);
2285 template <class _ForwardIterator>
2286 typename enable_if
2287 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002288 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002289 void
2290 >::type
2291 assign(_ForwardIterator __first, _ForwardIterator __last);
2292
2293 void assign(size_type __n, const value_type& __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +00002294
2295#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002296 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002297 void assign(initializer_list<value_type> __il)
2298 {assign(__il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +00002299#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002300
Howard Hinnant1c936782011-06-03 19:40:40 +00002301 _LIBCPP_INLINE_VISIBILITY allocator_type get_allocator() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002302 {return allocator_type(this->__alloc());}
2303
Howard Hinnant1c936782011-06-03 19:40:40 +00002304 size_type max_size() const _NOEXCEPT;
2305 _LIBCPP_INLINE_VISIBILITY
2306 size_type capacity() const _NOEXCEPT
2307 {return __internal_cap_to_external(__cap());}
2308 _LIBCPP_INLINE_VISIBILITY
2309 size_type size() const _NOEXCEPT
2310 {return __size_;}
Marshall Clow425f5752017-11-15 05:51:26 +00002311 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +00002312 bool empty() const _NOEXCEPT
2313 {return __size_ == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002314 void reserve(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +00002315 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002316
Howard Hinnant1c936782011-06-03 19:40:40 +00002317 _LIBCPP_INLINE_VISIBILITY
2318 iterator begin() _NOEXCEPT
2319 {return __make_iter(0);}
2320 _LIBCPP_INLINE_VISIBILITY
2321 const_iterator begin() const _NOEXCEPT
2322 {return __make_iter(0);}
2323 _LIBCPP_INLINE_VISIBILITY
2324 iterator end() _NOEXCEPT
2325 {return __make_iter(__size_);}
2326 _LIBCPP_INLINE_VISIBILITY
2327 const_iterator end() const _NOEXCEPT
2328 {return __make_iter(__size_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002329
Howard Hinnant1c936782011-06-03 19:40:40 +00002330 _LIBCPP_INLINE_VISIBILITY
2331 reverse_iterator rbegin() _NOEXCEPT
2332 {return reverse_iterator(end());}
2333 _LIBCPP_INLINE_VISIBILITY
2334 const_reverse_iterator rbegin() const _NOEXCEPT
2335 {return const_reverse_iterator(end());}
2336 _LIBCPP_INLINE_VISIBILITY
2337 reverse_iterator rend() _NOEXCEPT
2338 {return reverse_iterator(begin());}
2339 _LIBCPP_INLINE_VISIBILITY
2340 const_reverse_iterator rend() const _NOEXCEPT
2341 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002342
Howard Hinnant1c936782011-06-03 19:40:40 +00002343 _LIBCPP_INLINE_VISIBILITY
2344 const_iterator cbegin() const _NOEXCEPT
2345 {return __make_iter(0);}
2346 _LIBCPP_INLINE_VISIBILITY
2347 const_iterator cend() const _NOEXCEPT
2348 {return __make_iter(__size_);}
2349 _LIBCPP_INLINE_VISIBILITY
2350 const_reverse_iterator crbegin() const _NOEXCEPT
2351 {return rbegin();}
2352 _LIBCPP_INLINE_VISIBILITY
2353 const_reverse_iterator crend() const _NOEXCEPT
2354 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002355
2356 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) {return __make_ref(__n);}
2357 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const {return __make_ref(__n);}
2358 reference at(size_type __n);
2359 const_reference at(size_type __n) const;
2360
2361 _LIBCPP_INLINE_VISIBILITY reference front() {return __make_ref(0);}
2362 _LIBCPP_INLINE_VISIBILITY const_reference front() const {return __make_ref(0);}
2363 _LIBCPP_INLINE_VISIBILITY reference back() {return __make_ref(__size_ - 1);}
2364 _LIBCPP_INLINE_VISIBILITY const_reference back() const {return __make_ref(__size_ - 1);}
2365
2366 void push_back(const value_type& __x);
Marshall Clowc46bb8e2013-08-13 23:54:12 +00002367#if _LIBCPP_STD_VER > 11
2368 template <class... _Args>
Marshall Clowea52cc42017-01-24 23:09:12 +00002369#if _LIBCPP_STD_VER > 14
2370 _LIBCPP_INLINE_VISIBILITY reference emplace_back(_Args&&... __args)
2371#else
2372 _LIBCPP_INLINE_VISIBILITY void emplace_back(_Args&&... __args)
2373#endif
2374 {
Eric Fiselier34ba5b92016-07-21 03:20:17 +00002375 push_back ( value_type ( _VSTD::forward<_Args>(__args)... ));
Marshall Clowea52cc42017-01-24 23:09:12 +00002376#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00002377 return this->back();
Marshall Clowea52cc42017-01-24 23:09:12 +00002378#endif
Eric Fiselier34ba5b92016-07-21 03:20:17 +00002379 }
Marshall Clowc46bb8e2013-08-13 23:54:12 +00002380#endif
2381
Howard Hinnantc51e1022010-05-11 19:42:16 +00002382 _LIBCPP_INLINE_VISIBILITY void pop_back() {--__size_;}
2383
Marshall Clowc46bb8e2013-08-13 23:54:12 +00002384#if _LIBCPP_STD_VER > 11
2385 template <class... _Args>
2386 _LIBCPP_INLINE_VISIBILITY iterator emplace(const_iterator position, _Args&&... __args)
2387 { return insert ( position, value_type ( _VSTD::forward<_Args>(__args)... )); }
2388#endif
2389
Howard Hinnantc51e1022010-05-11 19:42:16 +00002390 iterator insert(const_iterator __position, const value_type& __x);
2391 iterator insert(const_iterator __position, size_type __n, const value_type& __x);
2392 iterator insert(const_iterator __position, size_type __n, const_reference __x);
2393 template <class _InputIterator>
2394 typename enable_if
2395 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002396 __is_cpp17_input_iterator <_InputIterator>::value &&
2397 !__is_cpp17_forward_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002398 iterator
2399 >::type
2400 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
2401 template <class _ForwardIterator>
2402 typename enable_if
2403 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002404 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002405 iterator
2406 >::type
2407 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiseliered9e9362017-04-16 02:40:45 +00002408
2409#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002410 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002411 iterator insert(const_iterator __position, initializer_list<value_type> __il)
2412 {return insert(__position, __il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +00002413#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002414
Howard Hinnantcf823322010-12-17 14:46:43 +00002415 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002416 iterator erase(const_iterator __first, const_iterator __last);
2417
Howard Hinnant1c936782011-06-03 19:40:40 +00002418 _LIBCPP_INLINE_VISIBILITY
2419 void clear() _NOEXCEPT {__size_ = 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002420
Howard Hinnant1c936782011-06-03 19:40:40 +00002421 void swap(vector&)
Marshall Clow8982dcd2015-07-13 20:04:56 +00002422#if _LIBCPP_STD_VER >= 14
2423 _NOEXCEPT;
2424#else
Eric Fiselier69c51982016-12-28 06:06:09 +00002425 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00002426 __is_nothrow_swappable<allocator_type>::value);
2427#endif
Marshall Clow89eb3822016-04-07 14:20:31 +00002428 static void swap(reference __x, reference __y) _NOEXCEPT { _VSTD::swap(__x, __y); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002429
2430 void resize(size_type __sz, value_type __x = false);
Howard Hinnant1c936782011-06-03 19:40:40 +00002431 void flip() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002432
2433 bool __invariants() const;
2434
2435private:
Howard Hinnantcf823322010-12-17 14:46:43 +00002436 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Marshall Clow3ff48e02018-05-22 16:20:28 +00002437 void __vallocate(size_type __n);
2438 void __vdeallocate() _NOEXCEPT;
Howard Hinnant1c936782011-06-03 19:40:40 +00002439 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantea382952013-08-14 18:00:20 +00002440 static size_type __align_it(size_type __new_size) _NOEXCEPT
Eric Fiselierb41db9a2018-10-01 01:59:37 +00002441 {return __new_size + (__bits_per_word-1) & ~((size_type)__bits_per_word-1);}
Howard Hinnantcf823322010-12-17 14:46:43 +00002442 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
2443 _LIBCPP_INLINE_VISIBILITY void __construct_at_end(size_type __n, bool __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002444 template <class _ForwardIterator>
2445 typename enable_if
2446 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002447 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002448 void
2449 >::type
2450 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
2451 void __append(size_type __n, const_reference __x);
Howard Hinnant1c936782011-06-03 19:40:40 +00002452 _LIBCPP_INLINE_VISIBILITY
2453 reference __make_ref(size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002454 {return reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnant1c936782011-06-03 19:40:40 +00002455 _LIBCPP_INLINE_VISIBILITY
2456 const_reference __make_ref(size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002457 {return const_reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnant1c936782011-06-03 19:40:40 +00002458 _LIBCPP_INLINE_VISIBILITY
2459 iterator __make_iter(size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002460 {return iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnant1c936782011-06-03 19:40:40 +00002461 _LIBCPP_INLINE_VISIBILITY
2462 const_iterator __make_iter(size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002463 {return const_iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnant1c936782011-06-03 19:40:40 +00002464 _LIBCPP_INLINE_VISIBILITY
2465 iterator __const_iterator_cast(const_iterator __p) _NOEXCEPT
Howard Hinnant76053d72013-06-27 19:35:32 +00002466 {return begin() + (__p - cbegin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002467
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002468 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002469 void __copy_assign_alloc(const vector& __v)
2470 {__copy_assign_alloc(__v, integral_constant<bool,
2471 __storage_traits::propagate_on_container_copy_assignment::value>());}
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002472 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002473 void __copy_assign_alloc(const vector& __c, true_type)
2474 {
2475 if (__alloc() != __c.__alloc())
Marshall Clow3ff48e02018-05-22 16:20:28 +00002476 __vdeallocate();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002477 __alloc() = __c.__alloc();
2478 }
2479
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002480 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00002481 void __copy_assign_alloc(const vector&, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002482 {}
2483
2484 void __move_assign(vector& __c, false_type);
Howard Hinnant1c936782011-06-03 19:40:40 +00002485 void __move_assign(vector& __c, true_type)
2486 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002487 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002488 void __move_assign_alloc(vector& __c)
Howard Hinnant1c936782011-06-03 19:40:40 +00002489 _NOEXCEPT_(
2490 !__storage_traits::propagate_on_container_move_assignment::value ||
2491 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002492 {__move_assign_alloc(__c, integral_constant<bool,
2493 __storage_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002494 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +00002495 void __move_assign_alloc(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002496 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002497 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002498 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002499 }
2500
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002501 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00002502 void __move_assign_alloc(vector&, false_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002503 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002504 {}
2505
Howard Hinnant1c936782011-06-03 19:40:40 +00002506 size_t __hash_code() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002507
2508 friend class __bit_reference<vector>;
2509 friend class __bit_const_reference<vector>;
2510 friend class __bit_iterator<vector, false>;
2511 friend class __bit_iterator<vector, true>;
Howard Hinnant4210a0f2012-08-17 17:10:18 +00002512 friend struct __bit_array<vector>;
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002513 friend struct _LIBCPP_TEMPLATE_VIS hash<vector>;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002514};
2515
2516template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002517inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002518void
2519vector<bool, _Allocator>::__invalidate_all_iterators()
2520{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002521}
2522
2523// Allocate space for __n objects
2524// throws length_error if __n > max_size()
2525// throws (probably bad_alloc) if memory run out
2526// Precondition: __begin_ == __end_ == __cap() == 0
2527// Precondition: __n > 0
2528// Postcondition: capacity() == __n
2529// Postcondition: size() == 0
2530template <class _Allocator>
2531void
Marshall Clow3ff48e02018-05-22 16:20:28 +00002532vector<bool, _Allocator>::__vallocate(size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002533{
2534 if (__n > max_size())
2535 this->__throw_length_error();
2536 __n = __external_cap_to_internal(__n);
2537 this->__begin_ = __storage_traits::allocate(this->__alloc(), __n);
2538 this->__size_ = 0;
2539 this->__cap() = __n;
2540}
2541
2542template <class _Allocator>
2543void
Marshall Clow3ff48e02018-05-22 16:20:28 +00002544vector<bool, _Allocator>::__vdeallocate() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002545{
Howard Hinnant76053d72013-06-27 19:35:32 +00002546 if (this->__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002547 {
2548 __storage_traits::deallocate(this->__alloc(), this->__begin_, __cap());
2549 __invalidate_all_iterators();
Howard Hinnant76053d72013-06-27 19:35:32 +00002550 this->__begin_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002551 this->__size_ = this->__cap() = 0;
2552 }
2553}
2554
2555template <class _Allocator>
2556typename vector<bool, _Allocator>::size_type
Howard Hinnant1c936782011-06-03 19:40:40 +00002557vector<bool, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002558{
2559 size_type __amax = __storage_traits::max_size(__alloc());
2560 size_type __nmax = numeric_limits<size_type>::max() / 2; // end() >= begin(), always
2561 if (__nmax / __bits_per_word <= __amax)
2562 return __nmax;
2563 return __internal_cap_to_external(__amax);
2564}
2565
2566// Precondition: __new_size > capacity()
2567template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002568inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002569typename vector<bool, _Allocator>::size_type
2570vector<bool, _Allocator>::__recommend(size_type __new_size) const
2571{
2572 const size_type __ms = max_size();
2573 if (__new_size > __ms)
2574 this->__throw_length_error();
2575 const size_type __cap = capacity();
2576 if (__cap >= __ms / 2)
2577 return __ms;
Arthur O'Dwyerc0fb14b2021-07-26 18:23:00 -04002578 return _VSTD::max(2 * __cap, __align_it(__new_size));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002579}
2580
2581// Default constructs __n objects starting at __end_
2582// Precondition: __n > 0
2583// Precondition: size() + __n <= capacity()
2584// Postcondition: size() == size() + __n
2585template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002586inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002587void
2588vector<bool, _Allocator>::__construct_at_end(size_type __n, bool __x)
2589{
2590 size_type __old_size = this->__size_;
2591 this->__size_ += __n;
Marshall Clow1893ec72018-10-23 20:07:45 +00002592 if (__old_size == 0 || ((__old_size - 1) / __bits_per_word) != ((this->__size_ - 1) / __bits_per_word))
2593 {
2594 if (this->__size_ <= __bits_per_word)
2595 this->__begin_[0] = __storage_type(0);
2596 else
2597 this->__begin_[(this->__size_ - 1) / __bits_per_word] = __storage_type(0);
2598 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002599 _VSTD::fill_n(__make_iter(__old_size), __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002600}
2601
2602template <class _Allocator>
2603template <class _ForwardIterator>
2604typename enable_if
2605<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002606 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002607 void
2608>::type
2609vector<bool, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
2610{
2611 size_type __old_size = this->__size_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002612 this->__size_ += _VSTD::distance(__first, __last);
Marshall Clow1893ec72018-10-23 20:07:45 +00002613 if (__old_size == 0 || ((__old_size - 1) / __bits_per_word) != ((this->__size_ - 1) / __bits_per_word))
2614 {
2615 if (this->__size_ <= __bits_per_word)
2616 this->__begin_[0] = __storage_type(0);
2617 else
2618 this->__begin_[(this->__size_ - 1) / __bits_per_word] = __storage_type(0);
2619 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002620 _VSTD::copy(__first, __last, __make_iter(__old_size));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002621}
2622
2623template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002624inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002625vector<bool, _Allocator>::vector()
Marshall Clowe546cbb2015-06-04 02:05:41 +00002626 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant76053d72013-06-27 19:35:32 +00002627 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002628 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002629 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002630{
2631}
2632
2633template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002634inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002635vector<bool, _Allocator>::vector(const allocator_type& __a)
Marshall Clow8f282f42015-06-04 00:10:20 +00002636#if _LIBCPP_STD_VER <= 14
2637 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
2638#else
2639 _NOEXCEPT
2640#endif
Howard Hinnant76053d72013-06-27 19:35:32 +00002641 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002642 __size_(0),
2643 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2644{
2645}
2646
2647template <class _Allocator>
2648vector<bool, _Allocator>::vector(size_type __n)
Howard Hinnant76053d72013-06-27 19:35:32 +00002649 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002650 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002651 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002652{
2653 if (__n > 0)
2654 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002655 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002656 __construct_at_end(__n, false);
2657 }
2658}
2659
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00002660#if _LIBCPP_STD_VER > 11
2661template <class _Allocator>
2662vector<bool, _Allocator>::vector(size_type __n, const allocator_type& __a)
2663 : __begin_(nullptr),
2664 __size_(0),
2665 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2666{
2667 if (__n > 0)
2668 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002669 __vallocate(__n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00002670 __construct_at_end(__n, false);
2671 }
2672}
2673#endif
2674
Howard Hinnantc51e1022010-05-11 19:42:16 +00002675template <class _Allocator>
2676vector<bool, _Allocator>::vector(size_type __n, const value_type& __x)
Howard Hinnant76053d72013-06-27 19:35:32 +00002677 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002678 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002679 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002680{
2681 if (__n > 0)
2682 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002683 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002684 __construct_at_end(__n, __x);
2685 }
2686}
2687
2688template <class _Allocator>
2689vector<bool, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002690 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002691 __size_(0),
2692 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2693{
2694 if (__n > 0)
2695 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002696 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002697 __construct_at_end(__n, __x);
2698 }
2699}
2700
2701template <class _Allocator>
2702template <class _InputIterator>
2703vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002704 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
2705 !__is_cpp17_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002706 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002707 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002708 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002709{
2710#ifndef _LIBCPP_NO_EXCEPTIONS
2711 try
2712 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002713#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002714 for (; __first != __last; ++__first)
2715 push_back(*__first);
2716#ifndef _LIBCPP_NO_EXCEPTIONS
2717 }
2718 catch (...)
2719 {
Howard Hinnant76053d72013-06-27 19:35:32 +00002720 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002721 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2722 __invalidate_all_iterators();
2723 throw;
2724 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002725#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002726}
2727
2728template <class _Allocator>
2729template <class _InputIterator>
2730vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002731 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
2732 !__is_cpp17_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002733 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002734 __size_(0),
2735 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2736{
2737#ifndef _LIBCPP_NO_EXCEPTIONS
2738 try
2739 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002740#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002741 for (; __first != __last; ++__first)
2742 push_back(*__first);
2743#ifndef _LIBCPP_NO_EXCEPTIONS
2744 }
2745 catch (...)
2746 {
Howard Hinnant76053d72013-06-27 19:35:32 +00002747 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002748 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2749 __invalidate_all_iterators();
2750 throw;
2751 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002752#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002753}
2754
2755template <class _Allocator>
2756template <class _ForwardIterator>
2757vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002758 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002759 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002760 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002761 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002762{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002763 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002764 if (__n > 0)
2765 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002766 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002767 __construct_at_end(__first, __last);
2768 }
2769}
2770
2771template <class _Allocator>
2772template <class _ForwardIterator>
2773vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002774 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002775 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002776 __size_(0),
2777 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2778{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002779 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002780 if (__n > 0)
2781 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002782 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002783 __construct_at_end(__first, __last);
2784 }
2785}
2786
Eric Fiseliered9e9362017-04-16 02:40:45 +00002787#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002788
Howard Hinnantc51e1022010-05-11 19:42:16 +00002789template <class _Allocator>
2790vector<bool, _Allocator>::vector(initializer_list<value_type> __il)
Howard Hinnant76053d72013-06-27 19:35:32 +00002791 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002792 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002793 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002794{
2795 size_type __n = static_cast<size_type>(__il.size());
2796 if (__n > 0)
2797 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002798 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002799 __construct_at_end(__il.begin(), __il.end());
2800 }
2801}
2802
2803template <class _Allocator>
2804vector<bool, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002805 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002806 __size_(0),
2807 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2808{
2809 size_type __n = static_cast<size_type>(__il.size());
2810 if (__n > 0)
2811 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002812 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002813 __construct_at_end(__il.begin(), __il.end());
2814 }
2815}
2816
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002817#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002818
Howard Hinnantc51e1022010-05-11 19:42:16 +00002819template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002820vector<bool, _Allocator>::~vector()
2821{
Howard Hinnant76053d72013-06-27 19:35:32 +00002822 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002823 __storage_traits::deallocate(__alloc(), __begin_, __cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002824 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002825}
2826
2827template <class _Allocator>
2828vector<bool, _Allocator>::vector(const vector& __v)
Howard Hinnant76053d72013-06-27 19:35:32 +00002829 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002830 __size_(0),
2831 __cap_alloc_(0, __storage_traits::select_on_container_copy_construction(__v.__alloc()))
2832{
2833 if (__v.size() > 0)
2834 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002835 __vallocate(__v.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002836 __construct_at_end(__v.begin(), __v.end());
2837 }
2838}
2839
2840template <class _Allocator>
2841vector<bool, _Allocator>::vector(const vector& __v, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002842 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002843 __size_(0),
2844 __cap_alloc_(0, __a)
2845{
2846 if (__v.size() > 0)
2847 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002848 __vallocate(__v.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002849 __construct_at_end(__v.begin(), __v.end());
2850 }
2851}
2852
2853template <class _Allocator>
2854vector<bool, _Allocator>&
2855vector<bool, _Allocator>::operator=(const vector& __v)
2856{
2857 if (this != &__v)
2858 {
2859 __copy_assign_alloc(__v);
2860 if (__v.__size_)
2861 {
2862 if (__v.__size_ > capacity())
2863 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002864 __vdeallocate();
2865 __vallocate(__v.__size_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002866 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002867 _VSTD::copy(__v.__begin_, __v.__begin_ + __external_cap_to_internal(__v.__size_), __begin_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002868 }
2869 __size_ = __v.__size_;
2870 }
2871 return *this;
2872}
2873
Eric Fiseliered9e9362017-04-16 02:40:45 +00002874#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant74279a52010-09-04 23:28:19 +00002875
Howard Hinnantc51e1022010-05-11 19:42:16 +00002876template <class _Allocator>
Eric Fiselier4f1534c2018-06-05 22:32:52 +00002877inline _LIBCPP_INLINE_VISIBILITY vector<bool, _Allocator>::vector(vector&& __v)
Marshall Clowe5108202015-07-14 14:46:32 +00002878#if _LIBCPP_STD_VER > 14
Eric Fiselier4f1534c2018-06-05 22:32:52 +00002879 _NOEXCEPT
Marshall Clowe5108202015-07-14 14:46:32 +00002880#else
Eric Fiselier4f1534c2018-06-05 22:32:52 +00002881 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowe5108202015-07-14 14:46:32 +00002882#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002883 : __begin_(__v.__begin_),
2884 __size_(__v.__size_),
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05002885 __cap_alloc_(_VSTD::move(__v.__cap_alloc_)) {
Howard Hinnant76053d72013-06-27 19:35:32 +00002886 __v.__begin_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002887 __v.__size_ = 0;
2888 __v.__cap() = 0;
2889}
2890
2891template <class _Allocator>
Arthur O'Dwyer9b9662f2021-03-01 17:08:24 -05002892vector<bool, _Allocator>::vector(vector&& __v, const __identity_t<allocator_type>& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002893 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002894 __size_(0),
2895 __cap_alloc_(0, __a)
2896{
2897 if (__a == allocator_type(__v.__alloc()))
2898 {
2899 this->__begin_ = __v.__begin_;
2900 this->__size_ = __v.__size_;
2901 this->__cap() = __v.__cap();
2902 __v.__begin_ = nullptr;
2903 __v.__cap() = __v.__size_ = 0;
2904 }
2905 else if (__v.size() > 0)
2906 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002907 __vallocate(__v.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002908 __construct_at_end(__v.begin(), __v.end());
2909 }
2910}
2911
2912template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002913inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002914vector<bool, _Allocator>&
2915vector<bool, _Allocator>::operator=(vector&& __v)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002916 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002917{
2918 __move_assign(__v, integral_constant<bool,
2919 __storage_traits::propagate_on_container_move_assignment::value>());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002920 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002921}
2922
2923template <class _Allocator>
2924void
2925vector<bool, _Allocator>::__move_assign(vector& __c, false_type)
2926{
2927 if (__alloc() != __c.__alloc())
2928 assign(__c.begin(), __c.end());
2929 else
2930 __move_assign(__c, true_type());
2931}
2932
2933template <class _Allocator>
2934void
2935vector<bool, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002936 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002937{
Marshall Clow3ff48e02018-05-22 16:20:28 +00002938 __vdeallocate();
Marshall Clowb4871fa2014-07-21 15:15:15 +00002939 __move_assign_alloc(__c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002940 this->__begin_ = __c.__begin_;
2941 this->__size_ = __c.__size_;
2942 this->__cap() = __c.__cap();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002943 __c.__begin_ = nullptr;
2944 __c.__cap() = __c.__size_ = 0;
2945}
Howard Hinnant74279a52010-09-04 23:28:19 +00002946
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002947#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002948
2949template <class _Allocator>
2950void
2951vector<bool, _Allocator>::assign(size_type __n, const value_type& __x)
2952{
2953 __size_ = 0;
2954 if (__n > 0)
2955 {
2956 size_type __c = capacity();
2957 if (__n <= __c)
2958 __size_ = __n;
2959 else
2960 {
2961 vector __v(__alloc());
2962 __v.reserve(__recommend(__n));
2963 __v.__size_ = __n;
2964 swap(__v);
2965 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002966 _VSTD::fill_n(begin(), __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002967 }
Eric Fiselier69c51982016-12-28 06:06:09 +00002968 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002969}
2970
2971template <class _Allocator>
2972template <class _InputIterator>
2973typename enable_if
2974<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002975 __is_cpp17_input_iterator<_InputIterator>::value &&
2976 !__is_cpp17_forward_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002977 void
2978>::type
2979vector<bool, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2980{
2981 clear();
2982 for (; __first != __last; ++__first)
2983 push_back(*__first);
2984}
2985
2986template <class _Allocator>
2987template <class _ForwardIterator>
2988typename enable_if
2989<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002990 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002991 void
2992>::type
2993vector<bool, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2994{
2995 clear();
Eric Fiselier6003c772016-12-23 23:37:52 +00002996 difference_type __ns = _VSTD::distance(__first, __last);
2997 _LIBCPP_ASSERT(__ns >= 0, "invalid range specified");
2998 const size_t __n = static_cast<size_type>(__ns);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002999 if (__n)
3000 {
3001 if (__n > capacity())
3002 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00003003 __vdeallocate();
3004 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003005 }
3006 __construct_at_end(__first, __last);
3007 }
3008}
3009
3010template <class _Allocator>
3011void
3012vector<bool, _Allocator>::reserve(size_type __n)
3013{
3014 if (__n > capacity())
3015 {
3016 vector __v(this->__alloc());
Marshall Clow3ff48e02018-05-22 16:20:28 +00003017 __v.__vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003018 __v.__construct_at_end(this->begin(), this->end());
3019 swap(__v);
3020 __invalidate_all_iterators();
3021 }
3022}
3023
3024template <class _Allocator>
3025void
Howard Hinnant1c936782011-06-03 19:40:40 +00003026vector<bool, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003027{
3028 if (__external_cap_to_internal(size()) > __cap())
3029 {
3030#ifndef _LIBCPP_NO_EXCEPTIONS
3031 try
3032 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003033#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003034 vector(*this, allocator_type(__alloc())).swap(*this);
3035#ifndef _LIBCPP_NO_EXCEPTIONS
3036 }
3037 catch (...)
3038 {
3039 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003040#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003041 }
3042}
3043
3044template <class _Allocator>
3045typename vector<bool, _Allocator>::reference
3046vector<bool, _Allocator>::at(size_type __n)
3047{
3048 if (__n >= size())
3049 this->__throw_out_of_range();
3050 return (*this)[__n];
3051}
3052
3053template <class _Allocator>
3054typename vector<bool, _Allocator>::const_reference
3055vector<bool, _Allocator>::at(size_type __n) const
3056{
3057 if (__n >= size())
3058 this->__throw_out_of_range();
3059 return (*this)[__n];
3060}
3061
3062template <class _Allocator>
3063void
3064vector<bool, _Allocator>::push_back(const value_type& __x)
3065{
3066 if (this->__size_ == this->capacity())
3067 reserve(__recommend(this->__size_ + 1));
3068 ++this->__size_;
3069 back() = __x;
3070}
3071
3072template <class _Allocator>
3073typename vector<bool, _Allocator>::iterator
3074vector<bool, _Allocator>::insert(const_iterator __position, const value_type& __x)
3075{
3076 iterator __r;
3077 if (size() < capacity())
3078 {
3079 const_iterator __old_end = end();
3080 ++__size_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003081 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003082 __r = __const_iterator_cast(__position);
3083 }
3084 else
3085 {
3086 vector __v(__alloc());
3087 __v.reserve(__recommend(__size_ + 1));
3088 __v.__size_ = __size_ + 1;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003089 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3090 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003091 swap(__v);
3092 }
3093 *__r = __x;
3094 return __r;
3095}
3096
3097template <class _Allocator>
3098typename vector<bool, _Allocator>::iterator
3099vector<bool, _Allocator>::insert(const_iterator __position, size_type __n, const value_type& __x)
3100{
3101 iterator __r;
3102 size_type __c = capacity();
3103 if (__n <= __c && size() <= __c - __n)
3104 {
3105 const_iterator __old_end = end();
3106 __size_ += __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003107 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003108 __r = __const_iterator_cast(__position);
3109 }
3110 else
3111 {
3112 vector __v(__alloc());
3113 __v.reserve(__recommend(__size_ + __n));
3114 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003115 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3116 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003117 swap(__v);
3118 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003119 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003120 return __r;
3121}
3122
3123template <class _Allocator>
3124template <class _InputIterator>
3125typename enable_if
3126<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05003127 __is_cpp17_input_iterator <_InputIterator>::value &&
3128 !__is_cpp17_forward_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003129 typename vector<bool, _Allocator>::iterator
3130>::type
3131vector<bool, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
3132{
3133 difference_type __off = __position - begin();
3134 iterator __p = __const_iterator_cast(__position);
3135 iterator __old_end = end();
3136 for (; size() != capacity() && __first != __last; ++__first)
3137 {
3138 ++this->__size_;
3139 back() = *__first;
3140 }
3141 vector __v(__alloc());
3142 if (__first != __last)
3143 {
3144#ifndef _LIBCPP_NO_EXCEPTIONS
3145 try
3146 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003147#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003148 __v.assign(__first, __last);
3149 difference_type __old_size = static_cast<difference_type>(__old_end - begin());
3150 difference_type __old_p = __p - begin();
3151 reserve(__recommend(size() + __v.size()));
3152 __p = begin() + __old_p;
3153 __old_end = begin() + __old_size;
3154#ifndef _LIBCPP_NO_EXCEPTIONS
3155 }
3156 catch (...)
3157 {
3158 erase(__old_end, end());
3159 throw;
3160 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003161#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003162 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003163 __p = _VSTD::rotate(__p, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003164 insert(__p, __v.begin(), __v.end());
3165 return begin() + __off;
3166}
3167
3168template <class _Allocator>
3169template <class _ForwardIterator>
3170typename enable_if
3171<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05003172 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003173 typename vector<bool, _Allocator>::iterator
3174>::type
3175vector<bool, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
3176{
Eric Fiselier654dd332016-12-11 05:31:00 +00003177 const difference_type __n_signed = _VSTD::distance(__first, __last);
3178 _LIBCPP_ASSERT(__n_signed >= 0, "invalid range specified");
3179 const size_type __n = static_cast<size_type>(__n_signed);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003180 iterator __r;
3181 size_type __c = capacity();
3182 if (__n <= __c && size() <= __c - __n)
3183 {
3184 const_iterator __old_end = end();
3185 __size_ += __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003186 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003187 __r = __const_iterator_cast(__position);
3188 }
3189 else
3190 {
3191 vector __v(__alloc());
3192 __v.reserve(__recommend(__size_ + __n));
3193 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003194 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3195 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003196 swap(__v);
3197 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003198 _VSTD::copy(__first, __last, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003199 return __r;
3200}
3201
3202template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003203inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003204typename vector<bool, _Allocator>::iterator
3205vector<bool, _Allocator>::erase(const_iterator __position)
3206{
3207 iterator __r = __const_iterator_cast(__position);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003208 _VSTD::copy(__position + 1, this->cend(), __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003209 --__size_;
3210 return __r;
3211}
3212
3213template <class _Allocator>
3214typename vector<bool, _Allocator>::iterator
3215vector<bool, _Allocator>::erase(const_iterator __first, const_iterator __last)
3216{
3217 iterator __r = __const_iterator_cast(__first);
3218 difference_type __d = __last - __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003219 _VSTD::copy(__last, this->cend(), __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003220 __size_ -= __d;
3221 return __r;
3222}
3223
3224template <class _Allocator>
3225void
3226vector<bool, _Allocator>::swap(vector& __x)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003227#if _LIBCPP_STD_VER >= 14
3228 _NOEXCEPT
3229#else
Eric Fiselier69c51982016-12-28 06:06:09 +00003230 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003231 __is_nothrow_swappable<allocator_type>::value)
3232#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003233{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003234 _VSTD::swap(this->__begin_, __x.__begin_);
3235 _VSTD::swap(this->__size_, __x.__size_);
3236 _VSTD::swap(this->__cap(), __x.__cap());
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05003237 _VSTD::__swap_allocator(this->__alloc(), __x.__alloc(),
Marshall Clow8982dcd2015-07-13 20:04:56 +00003238 integral_constant<bool, __alloc_traits::propagate_on_container_swap::value>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003239}
3240
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003241template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003242void
3243vector<bool, _Allocator>::resize(size_type __sz, value_type __x)
3244{
3245 size_type __cs = size();
3246 if (__cs < __sz)
3247 {
3248 iterator __r;
3249 size_type __c = capacity();
3250 size_type __n = __sz - __cs;
3251 if (__n <= __c && __cs <= __c - __n)
3252 {
3253 __r = end();
3254 __size_ += __n;
3255 }
3256 else
3257 {
3258 vector __v(__alloc());
3259 __v.reserve(__recommend(__size_ + __n));
3260 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003261 __r = _VSTD::copy(cbegin(), cend(), __v.begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003262 swap(__v);
3263 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003264 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003265 }
3266 else
3267 __size_ = __sz;
3268}
3269
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003270template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003271void
Howard Hinnant1c936782011-06-03 19:40:40 +00003272vector<bool, _Allocator>::flip() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003273{
3274 // do middle whole words
3275 size_type __n = __size_;
3276 __storage_pointer __p = __begin_;
3277 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3278 *__p = ~*__p;
3279 // do last partial word
3280 if (__n > 0)
3281 {
3282 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3283 __storage_type __b = *__p & __m;
3284 *__p &= ~__m;
3285 *__p |= ~__b & __m;
3286 }
3287}
3288
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003289template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003290bool
3291vector<bool, _Allocator>::__invariants() const
3292{
Howard Hinnant76053d72013-06-27 19:35:32 +00003293 if (this->__begin_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003294 {
3295 if (this->__size_ != 0 || this->__cap() != 0)
3296 return false;
3297 }
3298 else
3299 {
3300 if (this->__cap() == 0)
3301 return false;
3302 if (this->__size_ > this->capacity())
3303 return false;
3304 }
3305 return true;
3306}
3307
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003308template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003309size_t
Howard Hinnant1c936782011-06-03 19:40:40 +00003310vector<bool, _Allocator>::__hash_code() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003311{
3312 size_t __h = 0;
3313 // do middle whole words
3314 size_type __n = __size_;
3315 __storage_pointer __p = __begin_;
3316 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3317 __h ^= *__p;
3318 // do last partial word
3319 if (__n > 0)
3320 {
3321 const __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3322 __h ^= *__p & __m;
3323 }
3324 return __h;
3325}
3326
3327template <class _Allocator>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003328struct _LIBCPP_TEMPLATE_VIS hash<vector<bool, _Allocator> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00003329 : public unary_function<vector<bool, _Allocator>, size_t>
3330{
Howard Hinnant1c265cd2010-09-23 18:58:28 +00003331 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +00003332 size_t operator()(const vector<bool, _Allocator>& __vec) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003333 {return __vec.__hash_code();}
3334};
3335
3336template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003337inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003338bool
3339operator==(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3340{
3341 const typename vector<_Tp, _Allocator>::size_type __sz = __x.size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003342 return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003343}
3344
3345template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003346inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003347bool
3348operator!=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3349{
3350 return !(__x == __y);
3351}
3352
3353template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003354inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003355bool
3356operator< (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3357{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003358 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003359}
3360
3361template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003362inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003363bool
3364operator> (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3365{
3366 return __y < __x;
3367}
3368
3369template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003370inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003371bool
3372operator>=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3373{
3374 return !(__x < __y);
3375}
3376
3377template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003378inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003379bool
3380operator<=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3381{
3382 return !(__y < __x);
3383}
3384
3385template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003386inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003387void
3388swap(vector<_Tp, _Allocator>& __x, vector<_Tp, _Allocator>& __y)
Howard Hinnant1c936782011-06-03 19:40:40 +00003389 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00003390{
3391 __x.swap(__y);
3392}
3393
Marshall Clow29b53f22018-12-14 18:49:35 +00003394#if _LIBCPP_STD_VER > 17
3395template <class _Tp, class _Allocator, class _Up>
Marek Kurdeja98b1412020-05-02 13:58:03 +02003396inline _LIBCPP_INLINE_VISIBILITY typename vector<_Tp, _Allocator>::size_type
3397erase(vector<_Tp, _Allocator>& __c, const _Up& __v) {
3398 auto __old_size = __c.size();
3399 __c.erase(_VSTD::remove(__c.begin(), __c.end(), __v), __c.end());
3400 return __old_size - __c.size();
3401}
Marshall Clow29b53f22018-12-14 18:49:35 +00003402
3403template <class _Tp, class _Allocator, class _Predicate>
Marek Kurdeja98b1412020-05-02 13:58:03 +02003404inline _LIBCPP_INLINE_VISIBILITY typename vector<_Tp, _Allocator>::size_type
3405erase_if(vector<_Tp, _Allocator>& __c, _Predicate __pred) {
3406 auto __old_size = __c.size();
3407 __c.erase(_VSTD::remove_if(__c.begin(), __c.end(), __pred), __c.end());
3408 return __old_size - __c.size();
3409}
Marshall Clow29b53f22018-12-14 18:49:35 +00003410#endif
3411
Howard Hinnantc51e1022010-05-11 19:42:16 +00003412_LIBCPP_END_NAMESPACE_STD
3413
Eric Fiselierf4433a32017-05-31 22:07:49 +00003414_LIBCPP_POP_MACROS
3415
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003416#endif // _LIBCPP_VECTOR