blob: beec1b7351859a355ef078b96f9a48aa1812bac3 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===------------------------------ vector --------------------------------===//
3//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_VECTOR
11#define _LIBCPP_VECTOR
12
13/*
14 vector synopsis
15
16namespace std
17{
18
Howard Hinnant3b6579a2010-08-22 00:02:43 +000019template <class T, class Allocator = allocator<T> >
Howard Hinnantc51e1022010-05-11 19:42:16 +000020class vector
Howard Hinnant3b6579a2010-08-22 00:02:43 +000021{
22public:
23 typedef T value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +000024 typedef Allocator allocator_type;
25 typedef typename allocator_type::reference reference;
26 typedef typename allocator_type::const_reference const_reference;
27 typedef implementation-defined iterator;
28 typedef implementation-defined const_iterator;
29 typedef typename allocator_type::size_type size_type;
30 typedef typename allocator_type::difference_type difference_type;
31 typedef typename allocator_type::pointer pointer;
32 typedef typename allocator_type::const_pointer const_pointer;
33 typedef std::reverse_iterator<iterator> reverse_iterator;
34 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
35
Howard Hinnant1c936782011-06-03 19:40:40 +000036 vector()
37 noexcept(is_nothrow_default_constructible<allocator_type>::value);
38 explicit vector(const allocator_type&);
Howard Hinnantc51e1022010-05-11 19:42:16 +000039 explicit vector(size_type n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +000040 explicit vector(size_type n, const allocator_type&); // C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +000041 vector(size_type n, const value_type& value, const allocator_type& = allocator_type());
42 template <class InputIterator>
43 vector(InputIterator first, InputIterator last, const allocator_type& = allocator_type());
44 vector(const vector& x);
Howard Hinnant1c936782011-06-03 19:40:40 +000045 vector(vector&& x)
46 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +000047 vector(initializer_list<value_type> il);
48 vector(initializer_list<value_type> il, const allocator_type& a);
49 ~vector();
50 vector& operator=(const vector& x);
Howard Hinnant1c936782011-06-03 19:40:40 +000051 vector& operator=(vector&& x)
52 noexcept(
Marshall Clow2fe8a8d2015-08-18 18:57:00 +000053 allocator_type::propagate_on_container_move_assignment::value ||
54 allocator_type::is_always_equal::value); // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +000055 vector& operator=(initializer_list<value_type> il);
56 template <class InputIterator>
57 void assign(InputIterator first, InputIterator last);
58 void assign(size_type n, const value_type& u);
59 void assign(initializer_list<value_type> il);
60
Howard Hinnant1c936782011-06-03 19:40:40 +000061 allocator_type get_allocator() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000062
Howard Hinnant1c936782011-06-03 19:40:40 +000063 iterator begin() noexcept;
64 const_iterator begin() const noexcept;
65 iterator end() noexcept;
66 const_iterator end() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000067
Howard Hinnant1c936782011-06-03 19:40:40 +000068 reverse_iterator rbegin() noexcept;
69 const_reverse_iterator rbegin() const noexcept;
70 reverse_iterator rend() noexcept;
71 const_reverse_iterator rend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000072
Howard Hinnant1c936782011-06-03 19:40:40 +000073 const_iterator cbegin() const noexcept;
74 const_iterator cend() const noexcept;
75 const_reverse_iterator crbegin() const noexcept;
76 const_reverse_iterator crend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000077
Howard Hinnant1c936782011-06-03 19:40:40 +000078 size_type size() const noexcept;
79 size_type max_size() const noexcept;
80 size_type capacity() const noexcept;
81 bool empty() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000082 void reserve(size_type n);
Howard Hinnant1c936782011-06-03 19:40:40 +000083 void shrink_to_fit() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000084
85 reference operator[](size_type n);
86 const_reference operator[](size_type n) const;
87 reference at(size_type n);
88 const_reference at(size_type n) const;
89
90 reference front();
91 const_reference front() const;
92 reference back();
93 const_reference back() const;
94
Howard Hinnant1c936782011-06-03 19:40:40 +000095 value_type* data() noexcept;
96 const value_type* data() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000097
98 void push_back(const value_type& x);
99 void push_back(value_type&& x);
100 template <class... Args>
Marshall Clowea52cc42017-01-24 23:09:12 +0000101 reference emplace_back(Args&&... args); // reference in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000102 void pop_back();
103
104 template <class... Args> iterator emplace(const_iterator position, Args&&... args);
105 iterator insert(const_iterator position, const value_type& x);
106 iterator insert(const_iterator position, value_type&& x);
107 iterator insert(const_iterator position, size_type n, const value_type& x);
108 template <class InputIterator>
109 iterator insert(const_iterator position, InputIterator first, InputIterator last);
110 iterator insert(const_iterator position, initializer_list<value_type> il);
111
112 iterator erase(const_iterator position);
113 iterator erase(const_iterator first, const_iterator last);
114
Howard Hinnant1c936782011-06-03 19:40:40 +0000115 void clear() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000116
117 void resize(size_type sz);
118 void resize(size_type sz, const value_type& c);
119
Howard Hinnant1c936782011-06-03 19:40:40 +0000120 void swap(vector&)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000121 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
122 allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000123
124 bool __invariants() const;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000125};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000126
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000127template <class Allocator = allocator<T> >
Howard Hinnantc51e1022010-05-11 19:42:16 +0000128class vector<bool, Allocator>
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000129{
130public:
131 typedef bool value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000132 typedef Allocator allocator_type;
133 typedef implementation-defined iterator;
134 typedef implementation-defined const_iterator;
135 typedef typename allocator_type::size_type size_type;
136 typedef typename allocator_type::difference_type difference_type;
137 typedef iterator pointer;
138 typedef const_iterator const_pointer;
139 typedef std::reverse_iterator<iterator> reverse_iterator;
140 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
141
142 class reference
143 {
144 public:
Howard Hinnant1c936782011-06-03 19:40:40 +0000145 reference(const reference&) noexcept;
146 operator bool() const noexcept;
Louis Dionned9f528e2021-06-29 13:52:26 -0400147 reference& operator=(bool x) noexcept;
Howard Hinnant1c936782011-06-03 19:40:40 +0000148 reference& operator=(const reference& x) noexcept;
149 iterator operator&() const noexcept;
150 void flip() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000151 };
152
153 class const_reference
154 {
155 public:
Howard Hinnant1c936782011-06-03 19:40:40 +0000156 const_reference(const reference&) noexcept;
157 operator bool() const noexcept;
158 const_iterator operator&() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000159 };
160
Howard Hinnant1c936782011-06-03 19:40:40 +0000161 vector()
162 noexcept(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnantc2734962011-09-02 20:42:31 +0000163 explicit vector(const allocator_type&);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +0000164 explicit vector(size_type n, const allocator_type& a = allocator_type()); // C++14
165 vector(size_type n, const value_type& value, const allocator_type& = allocator_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000166 template <class InputIterator>
167 vector(InputIterator first, InputIterator last, const allocator_type& = allocator_type());
168 vector(const vector& x);
Howard Hinnant1c936782011-06-03 19:40:40 +0000169 vector(vector&& x)
170 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000171 vector(initializer_list<value_type> il);
172 vector(initializer_list<value_type> il, const allocator_type& a);
173 ~vector();
174 vector& operator=(const vector& x);
Howard Hinnant1c936782011-06-03 19:40:40 +0000175 vector& operator=(vector&& x)
176 noexcept(
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000177 allocator_type::propagate_on_container_move_assignment::value ||
178 allocator_type::is_always_equal::value); // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000179 vector& operator=(initializer_list<value_type> il);
180 template <class InputIterator>
181 void assign(InputIterator first, InputIterator last);
182 void assign(size_type n, const value_type& u);
183 void assign(initializer_list<value_type> il);
184
Howard Hinnant1c936782011-06-03 19:40:40 +0000185 allocator_type get_allocator() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000186
Howard Hinnant1c936782011-06-03 19:40:40 +0000187 iterator begin() noexcept;
188 const_iterator begin() const noexcept;
189 iterator end() noexcept;
190 const_iterator end() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000191
Howard Hinnant1c936782011-06-03 19:40:40 +0000192 reverse_iterator rbegin() noexcept;
193 const_reverse_iterator rbegin() const noexcept;
194 reverse_iterator rend() noexcept;
195 const_reverse_iterator rend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000196
Howard Hinnant1c936782011-06-03 19:40:40 +0000197 const_iterator cbegin() const noexcept;
198 const_iterator cend() const noexcept;
199 const_reverse_iterator crbegin() const noexcept;
200 const_reverse_iterator crend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000201
Howard Hinnant1c936782011-06-03 19:40:40 +0000202 size_type size() const noexcept;
203 size_type max_size() const noexcept;
204 size_type capacity() const noexcept;
205 bool empty() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000206 void reserve(size_type n);
Howard Hinnant1c936782011-06-03 19:40:40 +0000207 void shrink_to_fit() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000208
209 reference operator[](size_type n);
210 const_reference operator[](size_type n) const;
211 reference at(size_type n);
212 const_reference at(size_type n) const;
213
214 reference front();
215 const_reference front() const;
216 reference back();
217 const_reference back() const;
218
219 void push_back(const value_type& x);
Marshall Clowea52cc42017-01-24 23:09:12 +0000220 template <class... Args> reference emplace_back(Args&&... args); // C++14; reference in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000221 void pop_back();
222
Marshall Clowc46bb8e2013-08-13 23:54:12 +0000223 template <class... Args> iterator emplace(const_iterator position, Args&&... args); // C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000224 iterator insert(const_iterator position, const value_type& x);
225 iterator insert(const_iterator position, size_type n, const value_type& x);
226 template <class InputIterator>
227 iterator insert(const_iterator position, InputIterator first, InputIterator last);
228 iterator insert(const_iterator position, initializer_list<value_type> il);
229
230 iterator erase(const_iterator position);
231 iterator erase(const_iterator first, const_iterator last);
232
Howard Hinnant1c936782011-06-03 19:40:40 +0000233 void clear() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000234
235 void resize(size_type sz);
236 void resize(size_type sz, value_type x);
237
Howard Hinnant1c936782011-06-03 19:40:40 +0000238 void swap(vector&)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000239 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
240 allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnant1c936782011-06-03 19:40:40 +0000241 void flip() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000242
243 bool __invariants() const;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000244};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000245
Marshall Clowf0ca1492018-05-21 21:30:12 +0000246template <class InputIterator, class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
247 vector(InputIterator, InputIterator, Allocator = Allocator())
248 -> vector<typename iterator_traits<InputIterator>::value_type, Allocator>;
249
Howard Hinnantc51e1022010-05-11 19:42:16 +0000250template <class Allocator> struct hash<std::vector<bool, Allocator>>;
251
252template <class T, class Allocator> bool operator==(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
253template <class T, class Allocator> bool operator< (const vector<T,Allocator>& x, const vector<T,Allocator>& y);
254template <class T, class Allocator> bool operator!=(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
255template <class T, class Allocator> bool operator> (const vector<T,Allocator>& x, const vector<T,Allocator>& y);
256template <class T, class Allocator> bool operator>=(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
257template <class T, class Allocator> bool operator<=(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
258
Howard Hinnant1c936782011-06-03 19:40:40 +0000259template <class T, class Allocator>
260void swap(vector<T,Allocator>& x, vector<T,Allocator>& y)
261 noexcept(noexcept(x.swap(y)));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000262
Marshall Clow29b53f22018-12-14 18:49:35 +0000263template <class T, class Allocator, class U>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200264typename vector<T, Allocator>::size_type
265erase(vector<T, Allocator>& c, const U& value); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000266template <class T, class Allocator, class Predicate>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200267typename vector<T, Allocator>::size_type
268erase_if(vector<T, Allocator>& c, Predicate pred); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000269
Howard Hinnantc51e1022010-05-11 19:42:16 +0000270} // std
271
272*/
273
274#include <__config>
275#include <__bit_reference>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400276#include <__debug>
277#include <__functional_base>
Louis Dionne77249522021-06-11 09:55:11 -0400278#include <__iterator/wrap_iter.h>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400279#include <__split_buffer>
Christopher Di Bella41f26e82021-06-05 02:47:47 +0000280#include <__utility/forward.h>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400281#include <algorithm>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000282#include <climits>
Arthur O'Dwyer7deec122021-03-24 18:19:12 -0400283#include <compare>
Louis Dionned24191c2021-08-19 12:39:16 -0400284#include <cstdlib>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400285#include <cstring>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000286#include <initializer_list>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400287#include <iosfwd> // for forward declaration of vector
288#include <limits>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000289#include <memory>
290#include <stdexcept>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400291#include <type_traits>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000292#include <version>
Howard Hinnante6ff0b62013-08-02 00:26:35 +0000293
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000294#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000295#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000296#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000297
Eric Fiselierf4433a32017-05-31 22:07:49 +0000298_LIBCPP_PUSH_MACROS
299#include <__undef_macros>
300
301
Howard Hinnantc51e1022010-05-11 19:42:16 +0000302_LIBCPP_BEGIN_NAMESPACE_STD
303
304template <bool>
Louis Dionneb6aabd92021-08-19 12:21:06 -0400305struct __vector_base_common;
306
307template <>
308struct __vector_base_common<true> {
309 // Both are defined in vector.cpp
310 _LIBCPP_NORETURN _LIBCPP_EXPORTED_FROM_ABI void __throw_length_error() const;
311 _LIBCPP_NORETURN _LIBCPP_EXPORTED_FROM_ABI void __throw_out_of_range() const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000312};
313
Howard Hinnantc51e1022010-05-11 19:42:16 +0000314template <class _Tp, class _Allocator>
315class __vector_base
Louis Dionneb6aabd92021-08-19 12:21:06 -0400316 : protected __vector_base_common<true> // This base class is historical, but it needs to remain for ABI compatibility
Howard Hinnantc51e1022010-05-11 19:42:16 +0000317{
Marshall Clowf0ca1492018-05-21 21:30:12 +0000318public:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000319 typedef _Allocator allocator_type;
320 typedef allocator_traits<allocator_type> __alloc_traits;
Marshall Clowf0ca1492018-05-21 21:30:12 +0000321 typedef typename __alloc_traits::size_type size_type;
322protected:
323 typedef _Tp value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000324 typedef value_type& reference;
325 typedef const value_type& const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000326 typedef typename __alloc_traits::difference_type difference_type;
327 typedef typename __alloc_traits::pointer pointer;
328 typedef typename __alloc_traits::const_pointer const_pointer;
329 typedef pointer iterator;
330 typedef const_pointer const_iterator;
331
332 pointer __begin_;
333 pointer __end_;
334 __compressed_pair<pointer, allocator_type> __end_cap_;
335
Howard Hinnant1c936782011-06-03 19:40:40 +0000336 _LIBCPP_INLINE_VISIBILITY
337 allocator_type& __alloc() _NOEXCEPT
338 {return __end_cap_.second();}
339 _LIBCPP_INLINE_VISIBILITY
340 const allocator_type& __alloc() const _NOEXCEPT
341 {return __end_cap_.second();}
342 _LIBCPP_INLINE_VISIBILITY
343 pointer& __end_cap() _NOEXCEPT
344 {return __end_cap_.first();}
345 _LIBCPP_INLINE_VISIBILITY
346 const pointer& __end_cap() const _NOEXCEPT
347 {return __end_cap_.first();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000348
Howard Hinnant1c936782011-06-03 19:40:40 +0000349 _LIBCPP_INLINE_VISIBILITY
350 __vector_base()
351 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnantcf823322010-12-17 14:46:43 +0000352 _LIBCPP_INLINE_VISIBILITY __vector_base(const allocator_type& __a);
Eric Fiselier4f1534c2018-06-05 22:32:52 +0000353#ifndef _LIBCPP_CXX03_LANG
354 _LIBCPP_INLINE_VISIBILITY __vector_base(allocator_type&& __a) _NOEXCEPT;
355#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000356 ~__vector_base();
357
Howard Hinnant1c936782011-06-03 19:40:40 +0000358 _LIBCPP_INLINE_VISIBILITY
359 void clear() _NOEXCEPT {__destruct_at_end(__begin_);}
360 _LIBCPP_INLINE_VISIBILITY
361 size_type capacity() const _NOEXCEPT
362 {return static_cast<size_type>(__end_cap() - __begin_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000363
Howard Hinnant1c936782011-06-03 19:40:40 +0000364 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76053d72013-06-27 19:35:32 +0000365 void __destruct_at_end(pointer __new_last) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000366
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000367 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000368 void __copy_assign_alloc(const __vector_base& __c)
369 {__copy_assign_alloc(__c, integral_constant<bool,
370 __alloc_traits::propagate_on_container_copy_assignment::value>());}
371
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000372 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000373 void __move_assign_alloc(__vector_base& __c)
Howard Hinnant1c936782011-06-03 19:40:40 +0000374 _NOEXCEPT_(
375 !__alloc_traits::propagate_on_container_move_assignment::value ||
376 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000377 {__move_assign_alloc(__c, integral_constant<bool,
378 __alloc_traits::propagate_on_container_move_assignment::value>());}
Louis Dionned24191c2021-08-19 12:39:16 -0400379
380 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
381 void __throw_length_error() const {
382#ifndef _LIBCPP_NO_EXCEPTIONS
383 __vector_base_common<true>::__throw_length_error();
384#else
385 _VSTD::abort();
386#endif
387 }
388
389 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
390 void __throw_out_of_range() const {
391#ifndef _LIBCPP_NO_EXCEPTIONS
392 __vector_base_common<true>::__throw_out_of_range();
393#else
394 _VSTD::abort();
395#endif
396 }
397
Howard Hinnantc51e1022010-05-11 19:42:16 +0000398private:
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000399 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000400 void __copy_assign_alloc(const __vector_base& __c, true_type)
401 {
402 if (__alloc() != __c.__alloc())
403 {
404 clear();
405 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
406 __begin_ = __end_ = __end_cap() = nullptr;
407 }
408 __alloc() = __c.__alloc();
409 }
410
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000411 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +0000412 void __copy_assign_alloc(const __vector_base&, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000413 {}
414
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000415 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +0000416 void __move_assign_alloc(__vector_base& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +0000417 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000418 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000419 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000420 }
421
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000422 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +0000423 void __move_assign_alloc(__vector_base&, false_type)
Howard Hinnant1c936782011-06-03 19:40:40 +0000424 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000425 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000426};
427
428template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000429inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000430void
Howard Hinnant76053d72013-06-27 19:35:32 +0000431__vector_base<_Tp, _Allocator>::__destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000432{
Bruce Mitchener405fdcd2017-03-23 14:39:23 +0000433 pointer __soon_to_be_end = __end_;
434 while (__new_last != __soon_to_be_end)
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500435 __alloc_traits::destroy(__alloc(), _VSTD::__to_address(--__soon_to_be_end));
Bruce Mitchener405fdcd2017-03-23 14:39:23 +0000436 __end_ = __new_last;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000437}
438
439template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000440inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000441__vector_base<_Tp, _Allocator>::__vector_base()
Howard Hinnant1c936782011-06-03 19:40:40 +0000442 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant76053d72013-06-27 19:35:32 +0000443 : __begin_(nullptr),
444 __end_(nullptr),
Eric Fiselier33ebfb62019-12-16 18:23:39 -0500445 __end_cap_(nullptr, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +0000446{
447}
448
449template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000450inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000451__vector_base<_Tp, _Allocator>::__vector_base(const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +0000452 : __begin_(nullptr),
453 __end_(nullptr),
454 __end_cap_(nullptr, __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000455{
456}
457
Eric Fiselier4f1534c2018-06-05 22:32:52 +0000458#ifndef _LIBCPP_CXX03_LANG
459template <class _Tp, class _Allocator>
460inline _LIBCPP_INLINE_VISIBILITY
461__vector_base<_Tp, _Allocator>::__vector_base(allocator_type&& __a) _NOEXCEPT
462 : __begin_(nullptr),
463 __end_(nullptr),
Arthur O'Dwyer07b22492020-11-27 11:02:06 -0500464 __end_cap_(nullptr, _VSTD::move(__a)) {}
Eric Fiselier4f1534c2018-06-05 22:32:52 +0000465#endif
466
Howard Hinnantc51e1022010-05-11 19:42:16 +0000467template <class _Tp, class _Allocator>
468__vector_base<_Tp, _Allocator>::~__vector_base()
469{
Howard Hinnant76053d72013-06-27 19:35:32 +0000470 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000471 {
472 clear();
473 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
474 }
475}
476
Eric Fiselier876c6862016-02-20 00:19:45 +0000477template <class _Tp, class _Allocator /* = allocator<_Tp> */>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000478class _LIBCPP_TEMPLATE_VIS vector
Howard Hinnantc51e1022010-05-11 19:42:16 +0000479 : private __vector_base<_Tp, _Allocator>
480{
481private:
482 typedef __vector_base<_Tp, _Allocator> __base;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000483 typedef allocator<_Tp> __default_allocator_type;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000484public:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000485 typedef vector __self;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000486 typedef _Tp value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000487 typedef _Allocator allocator_type;
488 typedef typename __base::__alloc_traits __alloc_traits;
489 typedef typename __base::reference reference;
490 typedef typename __base::const_reference const_reference;
491 typedef typename __base::size_type size_type;
492 typedef typename __base::difference_type difference_type;
493 typedef typename __base::pointer pointer;
494 typedef typename __base::const_pointer const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000495 typedef __wrap_iter<pointer> iterator;
496 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000497 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
498 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000499
Howard Hinnanta416ff02013-03-26 19:04:56 +0000500 static_assert((is_same<typename allocator_type::value_type, value_type>::value),
501 "Allocator::value_type must be same type as value_type");
502
Howard Hinnant1c936782011-06-03 19:40:40 +0000503 _LIBCPP_INLINE_VISIBILITY
Marshall Clowe546cbb2015-06-04 02:05:41 +0000504 vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant27e0e772011-09-14 18:33:51 +0000505 {
Louis Dionneba400782020-10-02 15:02:52 -0400506#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000507 __get_db()->__insert_c(this);
508#endif
509 }
510 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
Marshall Clow8f282f42015-06-04 00:10:20 +0000511#if _LIBCPP_STD_VER <= 14
512 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
513#else
514 _NOEXCEPT
515#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +0000516 : __base(__a)
517 {
Louis Dionneba400782020-10-02 15:02:52 -0400518#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000519 __get_db()->__insert_c(this);
520#endif
521 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000522 explicit vector(size_type __n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +0000523#if _LIBCPP_STD_VER > 11
524 explicit vector(size_type __n, const allocator_type& __a);
525#endif
Marshall Clowf0ca1492018-05-21 21:30:12 +0000526 vector(size_type __n, const value_type& __x);
527 vector(size_type __n, const value_type& __x, const allocator_type& __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000528 template <class _InputIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +0000529 vector(_InputIterator __first,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500530 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
531 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000532 is_constructible<
533 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +0000534 typename iterator_traits<_InputIterator>::reference>::value,
535 _InputIterator>::type __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000536 template <class _InputIterator>
537 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500538 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
539 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000540 is_constructible<
541 value_type,
542 typename iterator_traits<_InputIterator>::reference>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000543 template <class _ForwardIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +0000544 vector(_ForwardIterator __first,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500545 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000546 is_constructible<
547 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +0000548 typename iterator_traits<_ForwardIterator>::reference>::value,
549 _ForwardIterator>::type __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000550 template <class _ForwardIterator>
551 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500552 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000553 is_constructible<
554 value_type,
555 typename iterator_traits<_ForwardIterator>::reference>::value>::type* = 0);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000556
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000557 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant27e0e772011-09-14 18:33:51 +0000558 ~vector()
559 {
Marshall Clow68af4f32018-09-07 15:47:59 +0000560 __annotate_delete();
Louis Dionneba400782020-10-02 15:02:52 -0400561#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000562 __get_db()->__erase_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000563#endif
Marshall Clow68af4f32018-09-07 15:47:59 +0000564 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000565
566 vector(const vector& __x);
Arthur O'Dwyer9b9662f2021-03-01 17:08:24 -0500567 vector(const vector& __x, const __identity_t<allocator_type>& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000568 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000569 vector& operator=(const vector& __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000570
571#ifndef _LIBCPP_CXX03_LANG
572 _LIBCPP_INLINE_VISIBILITY
573 vector(initializer_list<value_type> __il);
574
575 _LIBCPP_INLINE_VISIBILITY
576 vector(initializer_list<value_type> __il, const allocator_type& __a);
577
Howard Hinnantcf823322010-12-17 14:46:43 +0000578 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000579 vector(vector&& __x)
Marshall Clowe5108202015-07-14 14:46:32 +0000580#if _LIBCPP_STD_VER > 14
581 _NOEXCEPT;
582#else
Howard Hinnant1c936782011-06-03 19:40:40 +0000583 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowe5108202015-07-14 14:46:32 +0000584#endif
Eric Fiseliered9e9362017-04-16 02:40:45 +0000585
Howard Hinnantcf823322010-12-17 14:46:43 +0000586 _LIBCPP_INLINE_VISIBILITY
Arthur O'Dwyer9b9662f2021-03-01 17:08:24 -0500587 vector(vector&& __x, const __identity_t<allocator_type>& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000588 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000589 vector& operator=(vector&& __x)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000590 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiseliered9e9362017-04-16 02:40:45 +0000591
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000592 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000593 vector& operator=(initializer_list<value_type> __il)
594 {assign(__il.begin(), __il.end()); return *this;}
Eric Fiseliered9e9362017-04-16 02:40:45 +0000595
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400596#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000597
598 template <class _InputIterator>
599 typename enable_if
600 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500601 __is_cpp17_input_iterator <_InputIterator>::value &&
602 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000603 is_constructible<
604 value_type,
605 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000606 void
607 >::type
608 assign(_InputIterator __first, _InputIterator __last);
609 template <class _ForwardIterator>
610 typename enable_if
611 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500612 __is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000613 is_constructible<
614 value_type,
615 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000616 void
617 >::type
618 assign(_ForwardIterator __first, _ForwardIterator __last);
619
620 void assign(size_type __n, const_reference __u);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000621
622#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000623 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000624 void assign(initializer_list<value_type> __il)
625 {assign(__il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +0000626#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000627
Howard Hinnant1c936782011-06-03 19:40:40 +0000628 _LIBCPP_INLINE_VISIBILITY
629 allocator_type get_allocator() const _NOEXCEPT
630 {return this->__alloc();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000631
Howard Hinnant1c936782011-06-03 19:40:40 +0000632 _LIBCPP_INLINE_VISIBILITY iterator begin() _NOEXCEPT;
633 _LIBCPP_INLINE_VISIBILITY const_iterator begin() const _NOEXCEPT;
634 _LIBCPP_INLINE_VISIBILITY iterator end() _NOEXCEPT;
635 _LIBCPP_INLINE_VISIBILITY const_iterator end() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000636
Howard Hinnant1c936782011-06-03 19:40:40 +0000637 _LIBCPP_INLINE_VISIBILITY
638 reverse_iterator rbegin() _NOEXCEPT
639 {return reverse_iterator(end());}
640 _LIBCPP_INLINE_VISIBILITY
641 const_reverse_iterator rbegin() const _NOEXCEPT
642 {return const_reverse_iterator(end());}
643 _LIBCPP_INLINE_VISIBILITY
644 reverse_iterator rend() _NOEXCEPT
645 {return reverse_iterator(begin());}
646 _LIBCPP_INLINE_VISIBILITY
647 const_reverse_iterator rend() const _NOEXCEPT
648 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000649
Howard Hinnant1c936782011-06-03 19:40:40 +0000650 _LIBCPP_INLINE_VISIBILITY
651 const_iterator cbegin() const _NOEXCEPT
652 {return begin();}
653 _LIBCPP_INLINE_VISIBILITY
654 const_iterator cend() const _NOEXCEPT
655 {return end();}
656 _LIBCPP_INLINE_VISIBILITY
657 const_reverse_iterator crbegin() const _NOEXCEPT
658 {return rbegin();}
659 _LIBCPP_INLINE_VISIBILITY
660 const_reverse_iterator crend() const _NOEXCEPT
661 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000662
Howard Hinnant1c936782011-06-03 19:40:40 +0000663 _LIBCPP_INLINE_VISIBILITY
664 size_type size() const _NOEXCEPT
665 {return static_cast<size_type>(this->__end_ - this->__begin_);}
666 _LIBCPP_INLINE_VISIBILITY
667 size_type capacity() const _NOEXCEPT
668 {return __base::capacity();}
Marshall Clow425f5752017-11-15 05:51:26 +0000669 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000670 bool empty() const _NOEXCEPT
671 {return this->__begin_ == this->__end_;}
672 size_type max_size() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000673 void reserve(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +0000674 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000675
Marshall Clowd6470492019-03-15 00:29:35 +0000676 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) _NOEXCEPT;
677 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000678 reference at(size_type __n);
679 const_reference at(size_type __n) const;
680
Marshall Clowd6470492019-03-15 00:29:35 +0000681 _LIBCPP_INLINE_VISIBILITY reference front() _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 const_reference front() const _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000687 {
Kristina Bessonovaaeeaa7e2021-05-09 19:29:56 +0200688 _LIBCPP_ASSERT(!empty(), "front() called on an empty vector");
Howard Hinnant27e0e772011-09-14 18:33:51 +0000689 return *this->__begin_;
690 }
Marshall Clowd6470492019-03-15 00:29:35 +0000691 _LIBCPP_INLINE_VISIBILITY reference back() _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 }
Marshall Clowd6470492019-03-15 00:29:35 +0000696 _LIBCPP_INLINE_VISIBILITY const_reference back() const _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000697 {
Kristina Bessonovaaeeaa7e2021-05-09 19:29:56 +0200698 _LIBCPP_ASSERT(!empty(), "back() called on an empty vector");
Howard Hinnant27e0e772011-09-14 18:33:51 +0000699 return *(this->__end_ - 1);
700 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000701
Howard Hinnant1c936782011-06-03 19:40:40 +0000702 _LIBCPP_INLINE_VISIBILITY
703 value_type* data() _NOEXCEPT
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500704 {return _VSTD::__to_address(this->__begin_);}
Howard Hinnant1c936782011-06-03 19:40:40 +0000705 _LIBCPP_INLINE_VISIBILITY
706 const value_type* data() const _NOEXCEPT
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500707 {return _VSTD::__to_address(this->__begin_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000708
Eric Fiselier96919722017-10-17 13:03:17 +0000709#ifdef _LIBCPP_CXX03_LANG
710 _LIBCPP_INLINE_VISIBILITY
711 void __emplace_back(const value_type& __x) { push_back(__x); }
712#else
713 template <class _Arg>
714 _LIBCPP_INLINE_VISIBILITY
715 void __emplace_back(_Arg&& __arg) {
716 emplace_back(_VSTD::forward<_Arg>(__arg));
717 }
718#endif
719
Howard Hinnantcf823322010-12-17 14:46:43 +0000720 _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000721
722#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000723 _LIBCPP_INLINE_VISIBILITY void push_back(value_type&& __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000724
Howard Hinnantc51e1022010-05-11 19:42:16 +0000725 template <class... _Args>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000726 _LIBCPP_INLINE_VISIBILITY
Marshall Clowea52cc42017-01-24 23:09:12 +0000727#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +0000728 reference emplace_back(_Args&&... __args);
Marshall Clowea52cc42017-01-24 23:09:12 +0000729#else
730 void emplace_back(_Args&&... __args);
731#endif
Eric Fiseliered9e9362017-04-16 02:40:45 +0000732#endif // !_LIBCPP_CXX03_LANG
733
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000734 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000735 void pop_back();
736
737 iterator insert(const_iterator __position, const_reference __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000738
739#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000740 iterator insert(const_iterator __position, value_type&& __x);
741 template <class... _Args>
742 iterator emplace(const_iterator __position, _Args&&... __args);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400743#endif // !_LIBCPP_CXX03_LANG
Eric Fiseliered9e9362017-04-16 02:40:45 +0000744
Howard Hinnantc51e1022010-05-11 19:42:16 +0000745 iterator insert(const_iterator __position, size_type __n, const_reference __x);
746 template <class _InputIterator>
747 typename enable_if
748 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500749 __is_cpp17_input_iterator <_InputIterator>::value &&
750 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000751 is_constructible<
752 value_type,
753 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000754 iterator
755 >::type
756 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
757 template <class _ForwardIterator>
758 typename enable_if
759 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500760 __is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000761 is_constructible<
762 value_type,
763 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000764 iterator
765 >::type
766 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000767
768#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000769 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000770 iterator insert(const_iterator __position, initializer_list<value_type> __il)
771 {return insert(__position, __il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +0000772#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000773
Howard Hinnantcf823322010-12-17 14:46:43 +0000774 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000775 iterator erase(const_iterator __first, const_iterator __last);
776
Howard Hinnant1c936782011-06-03 19:40:40 +0000777 _LIBCPP_INLINE_VISIBILITY
778 void clear() _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000779 {
Marshall Clow2cd9d372014-05-08 14:14:06 +0000780 size_type __old_size = size();
Howard Hinnant27e0e772011-09-14 18:33:51 +0000781 __base::clear();
Marshall Clow2cd9d372014-05-08 14:14:06 +0000782 __annotate_shrink(__old_size);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000783 __invalidate_all_iterators();
784 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000785
786 void resize(size_type __sz);
787 void resize(size_type __sz, const_reference __x);
788
Howard Hinnant1c936782011-06-03 19:40:40 +0000789 void swap(vector&)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000790#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +0000791 _NOEXCEPT;
Marshall Clow8982dcd2015-07-13 20:04:56 +0000792#else
Eric Fiselier873b8d32019-03-18 21:50:12 +0000793 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +0000794 __is_nothrow_swappable<allocator_type>::value);
795#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000796
797 bool __invariants() const;
798
Louis Dionneba400782020-10-02 15:02:52 -0400799#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000800
801 bool __dereferenceable(const const_iterator* __i) const;
802 bool __decrementable(const const_iterator* __i) const;
803 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
804 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
805
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400806#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000807
Howard Hinnantc51e1022010-05-11 19:42:16 +0000808private:
Howard Hinnantcf823322010-12-17 14:46:43 +0000809 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Eric Fiselier69c51982016-12-28 06:06:09 +0000810 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(pointer __new_last);
Marshall Clow3ff48e02018-05-22 16:20:28 +0000811 void __vallocate(size_type __n);
812 void __vdeallocate() _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000813 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
Howard Hinnanta5589382011-01-04 19:53:31 +0000814 void __construct_at_end(size_type __n);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000815 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000816 void __construct_at_end(size_type __n, const_reference __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000817 template <class _ForwardIterator>
818 typename enable_if
819 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500820 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000821 void
822 >::type
Eric Fiselieracfe6f02015-03-31 16:54:19 +0000823 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000824 void __append(size_type __n);
825 void __append(size_type __n, const_reference __x);
Howard Hinnantcf823322010-12-17 14:46:43 +0000826 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000827 iterator __make_iter(pointer __p) _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000828 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000829 const_iterator __make_iter(const_pointer __p) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000830 void __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v);
831 pointer __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p);
832 void __move_range(pointer __from_s, pointer __from_e, pointer __to);
Howard Hinnant1c936782011-06-03 19:40:40 +0000833 void __move_assign(vector& __c, true_type)
834 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000835 void __move_assign(vector& __c, false_type)
836 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000837 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76053d72013-06-27 19:35:32 +0000838 void __destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000839 {
Eric Fiselier69c51982016-12-28 06:06:09 +0000840 __invalidate_iterators_past(__new_last);
Marshall Clow2cd9d372014-05-08 14:14:06 +0000841 size_type __old_size = size();
Howard Hinnant27e0e772011-09-14 18:33:51 +0000842 __base::__destruct_at_end(__new_last);
Marshall Clow2cd9d372014-05-08 14:14:06 +0000843 __annotate_shrink(__old_size);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000844 }
Eric Fiseliered9e9362017-04-16 02:40:45 +0000845
846#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier7110f442019-03-19 19:19:44 +0000847 template <class _Up>
848 _LIBCPP_INLINE_VISIBILITY
849 inline void __push_back_slow_path(_Up&& __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000850
Howard Hinnantb6c49562012-02-26 15:30:12 +0000851 template <class... _Args>
Eric Fiselier7110f442019-03-19 19:19:44 +0000852 _LIBCPP_INLINE_VISIBILITY
853 inline void __emplace_back_slow_path(_Args&&... __args);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000854#else
Eric Fiselier7110f442019-03-19 19:19:44 +0000855 template <class _Up>
856 _LIBCPP_INLINE_VISIBILITY
857 inline void __push_back_slow_path(_Up& __x);
Howard Hinnantb6c49562012-02-26 15:30:12 +0000858#endif
Eric Fiseliered9e9362017-04-16 02:40:45 +0000859
Marshall Clow2cd9d372014-05-08 14:14:06 +0000860 // The following functions are no-ops outside of AddressSanitizer mode.
861 // We call annotatations only for the default Allocator because other allocators
862 // may not meet the AddressSanitizer alignment constraints.
863 // See the documentation for __sanitizer_annotate_contiguous_container for more details.
Marshall Clow2cd9d372014-05-08 14:14:06 +0000864#ifndef _LIBCPP_HAS_NO_ASAN
Eric Fiselier6003c772016-12-23 23:37:52 +0000865 void __annotate_contiguous_container(const void *__beg, const void *__end,
866 const void *__old_mid,
867 const void *__new_mid) const
868 {
869
Marshall Clow2cd9d372014-05-08 14:14:06 +0000870 if (__beg && is_same<allocator_type, __default_allocator_type>::value)
871 __sanitizer_annotate_contiguous_container(__beg, __end, __old_mid, __new_mid);
Marshall Clow2cd9d372014-05-08 14:14:06 +0000872 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000873#else
874 _LIBCPP_INLINE_VISIBILITY
875 void __annotate_contiguous_container(const void*, const void*, const void*,
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000876 const void*) const _NOEXCEPT {}
Eric Fiselier6003c772016-12-23 23:37:52 +0000877#endif
878 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000879 void __annotate_new(size_type __current_size) const _NOEXCEPT {
Marshall Clow2cd9d372014-05-08 14:14:06 +0000880 __annotate_contiguous_container(data(), data() + capacity(),
881 data() + capacity(), data() + __current_size);
882 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000883
884 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000885 void __annotate_delete() const _NOEXCEPT {
Marshall Clow2cd9d372014-05-08 14:14:06 +0000886 __annotate_contiguous_container(data(), data() + capacity(),
887 data() + size(), data() + capacity());
888 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000889
890 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000891 void __annotate_increase(size_type __n) const _NOEXCEPT
Marshall Clow2cd9d372014-05-08 14:14:06 +0000892 {
893 __annotate_contiguous_container(data(), data() + capacity(),
894 data() + size(), data() + size() + __n);
895 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000896
897 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000898 void __annotate_shrink(size_type __old_size) const _NOEXCEPT
Marshall Clow2cd9d372014-05-08 14:14:06 +0000899 {
900 __annotate_contiguous_container(data(), data() + capacity(),
901 data() + __old_size, data() + size());
902 }
Marshall Clowc78ffa52014-09-03 21:37:43 +0000903
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000904 struct _ConstructTransaction {
905 explicit _ConstructTransaction(vector &__v, size_type __n)
906 : __v_(__v), __pos_(__v.__end_), __new_end_(__v.__end_ + __n) {
907#ifndef _LIBCPP_HAS_NO_ASAN
908 __v_.__annotate_increase(__n);
909#endif
910 }
911 ~_ConstructTransaction() {
912 __v_.__end_ = __pos_;
913#ifndef _LIBCPP_HAS_NO_ASAN
914 if (__pos_ != __new_end_) {
915 __v_.__annotate_shrink(__new_end_ - __v_.__begin_);
916 }
917#endif
918 }
919
920 vector &__v_;
921 pointer __pos_;
922 const_pointer const __new_end_;
923
924 private:
925 _ConstructTransaction(_ConstructTransaction const&) = delete;
926 _ConstructTransaction& operator=(_ConstructTransaction const&) = delete;
927 };
928
929 template <class ..._Args>
930 _LIBCPP_INLINE_VISIBILITY
931 void __construct_one_at_end(_Args&& ...__args) {
932 _ConstructTransaction __tx(*this, 1);
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500933 __alloc_traits::construct(this->__alloc(), _VSTD::__to_address(__tx.__pos_),
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000934 _VSTD::forward<_Args>(__args)...);
935 ++__tx.__pos_;
936 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000937};
938
Louis Dionned59f8a52021-08-17 11:59:07 -0400939#if _LIBCPP_STD_VER >= 17
Marshall Clowf0ca1492018-05-21 21:30:12 +0000940template<class _InputIterator,
Arthur O'Dwyer56226762021-03-03 23:02:20 -0500941 class _Alloc = allocator<__iter_value_type<_InputIterator>>,
Louis Dionne25547162021-08-17 12:26:09 -0400942 class = enable_if_t<__is_allocator<_Alloc>::value>
Marshall Clowf0ca1492018-05-21 21:30:12 +0000943 >
944vector(_InputIterator, _InputIterator)
Arthur O'Dwyer56226762021-03-03 23:02:20 -0500945 -> vector<__iter_value_type<_InputIterator>, _Alloc>;
Marshall Clowf0ca1492018-05-21 21:30:12 +0000946
947template<class _InputIterator,
948 class _Alloc,
Louis Dionne25547162021-08-17 12:26:09 -0400949 class = enable_if_t<__is_allocator<_Alloc>::value>
Marshall Clowf0ca1492018-05-21 21:30:12 +0000950 >
951vector(_InputIterator, _InputIterator, _Alloc)
Arthur O'Dwyer56226762021-03-03 23:02:20 -0500952 -> vector<__iter_value_type<_InputIterator>, _Alloc>;
Marshall Clowf0ca1492018-05-21 21:30:12 +0000953#endif
954
Howard Hinnantc51e1022010-05-11 19:42:16 +0000955template <class _Tp, class _Allocator>
956void
957vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v)
958{
Eric Fiselier909fe962019-09-13 16:09:33 +0000959
Marshall Clow2cd9d372014-05-08 14:14:06 +0000960 __annotate_delete();
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -0500961 _VSTD::__construct_backward_with_exception_guarantees(this->__alloc(), this->__begin_, this->__end_, __v.__begin_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000962 _VSTD::swap(this->__begin_, __v.__begin_);
963 _VSTD::swap(this->__end_, __v.__end_);
964 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000965 __v.__first_ = __v.__begin_;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000966 __annotate_new(size());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000967 __invalidate_all_iterators();
968}
969
970template <class _Tp, class _Allocator>
971typename vector<_Tp, _Allocator>::pointer
972vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p)
973{
Marshall Clow2cd9d372014-05-08 14:14:06 +0000974 __annotate_delete();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000975 pointer __r = __v.__begin_;
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -0500976 _VSTD::__construct_backward_with_exception_guarantees(this->__alloc(), this->__begin_, __p, __v.__begin_);
977 _VSTD::__construct_forward_with_exception_guarantees(this->__alloc(), __p, this->__end_, __v.__end_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000978 _VSTD::swap(this->__begin_, __v.__begin_);
979 _VSTD::swap(this->__end_, __v.__end_);
980 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000981 __v.__first_ = __v.__begin_;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000982 __annotate_new(size());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000983 __invalidate_all_iterators();
984 return __r;
985}
986
987// Allocate space for __n objects
988// throws length_error if __n > max_size()
989// throws (probably bad_alloc) if memory run out
990// Precondition: __begin_ == __end_ == __end_cap() == 0
991// Precondition: __n > 0
992// Postcondition: capacity() == __n
993// Postcondition: size() == 0
994template <class _Tp, class _Allocator>
995void
Marshall Clow3ff48e02018-05-22 16:20:28 +0000996vector<_Tp, _Allocator>::__vallocate(size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000997{
998 if (__n > max_size())
999 this->__throw_length_error();
1000 this->__begin_ = this->__end_ = __alloc_traits::allocate(this->__alloc(), __n);
1001 this->__end_cap() = this->__begin_ + __n;
Marshall Clow2cd9d372014-05-08 14:14:06 +00001002 __annotate_new(0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001003}
1004
1005template <class _Tp, class _Allocator>
1006void
Marshall Clow3ff48e02018-05-22 16:20:28 +00001007vector<_Tp, _Allocator>::__vdeallocate() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001008{
Howard Hinnant76053d72013-06-27 19:35:32 +00001009 if (this->__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001010 {
1011 clear();
1012 __alloc_traits::deallocate(this->__alloc(), this->__begin_, capacity());
Howard Hinnant76053d72013-06-27 19:35:32 +00001013 this->__begin_ = this->__end_ = this->__end_cap() = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001014 }
1015}
1016
1017template <class _Tp, class _Allocator>
1018typename vector<_Tp, _Allocator>::size_type
Howard Hinnant1c936782011-06-03 19:40:40 +00001019vector<_Tp, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001020{
Eric Fiselierb5d9f442016-11-23 01:18:56 +00001021 return _VSTD::min<size_type>(__alloc_traits::max_size(this->__alloc()),
1022 numeric_limits<difference_type>::max());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001023}
1024
1025// Precondition: __new_size > capacity()
1026template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001027inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001028typename vector<_Tp, _Allocator>::size_type
1029vector<_Tp, _Allocator>::__recommend(size_type __new_size) const
1030{
1031 const size_type __ms = max_size();
1032 if (__new_size > __ms)
1033 this->__throw_length_error();
1034 const size_type __cap = capacity();
1035 if (__cap >= __ms / 2)
1036 return __ms;
Arthur O'Dwyerc0fb14b2021-07-26 18:23:00 -04001037 return _VSTD::max<size_type>(2 * __cap, __new_size);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001038}
1039
1040// Default constructs __n objects starting at __end_
1041// throws if construction throws
1042// Precondition: __n > 0
1043// Precondition: size() + __n <= capacity()
1044// Postcondition: size() == size() + __n
1045template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001046void
1047vector<_Tp, _Allocator>::__construct_at_end(size_type __n)
1048{
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001049 _ConstructTransaction __tx(*this, __n);
Martijn Vels1e2e1b82020-06-18 13:14:02 -04001050 const_pointer __new_end = __tx.__new_end_;
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04001051 for (pointer __pos = __tx.__pos_; __pos != __new_end; __tx.__pos_ = ++__pos) {
Martijn Vels1e2e1b82020-06-18 13:14:02 -04001052 __alloc_traits::construct(this->__alloc(), _VSTD::__to_address(__pos));
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001053 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001054}
1055
Howard Hinnantc51e1022010-05-11 19:42:16 +00001056// Copy constructs __n objects starting at __end_ from __x
1057// throws if construction throws
1058// Precondition: __n > 0
1059// Precondition: size() + __n <= capacity()
1060// Postcondition: size() == old size() + __n
1061// Postcondition: [i] == __x for all i in [size() - __n, __n)
1062template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001063inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001064void
1065vector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
1066{
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001067 _ConstructTransaction __tx(*this, __n);
Martijn Vels1e2e1b82020-06-18 13:14:02 -04001068 const_pointer __new_end = __tx.__new_end_;
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04001069 for (pointer __pos = __tx.__pos_; __pos != __new_end; __tx.__pos_ = ++__pos) {
Martijn Vels1e2e1b82020-06-18 13:14:02 -04001070 __alloc_traits::construct(this->__alloc(), _VSTD::__to_address(__pos), __x);
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001071 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001072}
1073
1074template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001075template <class _ForwardIterator>
1076typename enable_if
1077<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001078 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001079 void
1080>::type
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001081vector<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001082{
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001083 _ConstructTransaction __tx(*this, __n);
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05001084 _VSTD::__construct_range_forward(this->__alloc(), __first, __last, __tx.__pos_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001085}
1086
1087// Default constructs __n objects starting at __end_
1088// throws if construction throws
1089// Postcondition: size() == size() + __n
Howard Hinnant1c936782011-06-03 19:40:40 +00001090// Exception safety: strong.
Howard Hinnantc51e1022010-05-11 19:42:16 +00001091template <class _Tp, class _Allocator>
1092void
1093vector<_Tp, _Allocator>::__append(size_type __n)
1094{
1095 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1096 this->__construct_at_end(__n);
1097 else
1098 {
1099 allocator_type& __a = this->__alloc();
1100 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1101 __v.__construct_at_end(__n);
1102 __swap_out_circular_buffer(__v);
1103 }
1104}
1105
1106// Default constructs __n objects starting at __end_
1107// throws if construction throws
1108// Postcondition: size() == size() + __n
Howard Hinnant1c936782011-06-03 19:40:40 +00001109// Exception safety: strong.
Howard Hinnantc51e1022010-05-11 19:42:16 +00001110template <class _Tp, class _Allocator>
1111void
1112vector<_Tp, _Allocator>::__append(size_type __n, const_reference __x)
1113{
1114 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1115 this->__construct_at_end(__n, __x);
1116 else
1117 {
1118 allocator_type& __a = this->__alloc();
1119 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1120 __v.__construct_at_end(__n, __x);
1121 __swap_out_circular_buffer(__v);
1122 }
1123}
1124
1125template <class _Tp, class _Allocator>
1126vector<_Tp, _Allocator>::vector(size_type __n)
1127{
Louis Dionneba400782020-10-02 15:02:52 -04001128#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001129 __get_db()->__insert_c(this);
1130#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001131 if (__n > 0)
1132 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001133 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001134 __construct_at_end(__n);
1135 }
1136}
1137
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00001138#if _LIBCPP_STD_VER > 11
1139template <class _Tp, class _Allocator>
1140vector<_Tp, _Allocator>::vector(size_type __n, const allocator_type& __a)
1141 : __base(__a)
1142{
Louis Dionneba400782020-10-02 15:02:52 -04001143#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00001144 __get_db()->__insert_c(this);
1145#endif
1146 if (__n > 0)
1147 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001148 __vallocate(__n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00001149 __construct_at_end(__n);
1150 }
1151}
1152#endif
1153
Howard Hinnantc51e1022010-05-11 19:42:16 +00001154template <class _Tp, class _Allocator>
Marshall Clowf0ca1492018-05-21 21:30:12 +00001155vector<_Tp, _Allocator>::vector(size_type __n, const value_type& __x)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001156{
Louis Dionneba400782020-10-02 15:02:52 -04001157#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001158 __get_db()->__insert_c(this);
1159#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001160 if (__n > 0)
1161 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001162 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001163 __construct_at_end(__n, __x);
1164 }
1165}
1166
1167template <class _Tp, class _Allocator>
Marshall Clowf0ca1492018-05-21 21:30:12 +00001168vector<_Tp, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001169 : __base(__a)
1170{
Louis Dionneba400782020-10-02 15:02:52 -04001171#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001172 __get_db()->__insert_c(this);
1173#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001174 if (__n > 0)
1175 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001176 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001177 __construct_at_end(__n, __x);
1178 }
1179}
1180
1181template <class _Tp, class _Allocator>
1182template <class _InputIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +00001183vector<_Tp, _Allocator>::vector(_InputIterator __first,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001184 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
1185 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001186 is_constructible<
1187 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +00001188 typename iterator_traits<_InputIterator>::reference>::value,
1189 _InputIterator>::type __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001190{
Louis Dionneba400782020-10-02 15:02:52 -04001191#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001192 __get_db()->__insert_c(this);
1193#endif
Howard Hinnant9cd22302011-09-16 18:41:29 +00001194 for (; __first != __last; ++__first)
Eric Fiselier96919722017-10-17 13:03:17 +00001195 __emplace_back(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001196}
1197
1198template <class _Tp, class _Allocator>
1199template <class _InputIterator>
1200vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001201 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
1202 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001203 is_constructible<
1204 value_type,
1205 typename iterator_traits<_InputIterator>::reference>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001206 : __base(__a)
1207{
Louis Dionneba400782020-10-02 15:02:52 -04001208#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001209 __get_db()->__insert_c(this);
1210#endif
Howard Hinnant9cd22302011-09-16 18:41:29 +00001211 for (; __first != __last; ++__first)
Eric Fiselier96919722017-10-17 13:03:17 +00001212 __emplace_back(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001213}
1214
1215template <class _Tp, class _Allocator>
1216template <class _ForwardIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +00001217vector<_Tp, _Allocator>::vector(_ForwardIterator __first,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001218 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001219 is_constructible<
1220 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +00001221 typename iterator_traits<_ForwardIterator>::reference>::value,
1222 _ForwardIterator>::type __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001223{
Louis Dionneba400782020-10-02 15:02:52 -04001224#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001225 __get_db()->__insert_c(this);
1226#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001227 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001228 if (__n > 0)
1229 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001230 __vallocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001231 __construct_at_end(__first, __last, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001232 }
1233}
1234
1235template <class _Tp, class _Allocator>
1236template <class _ForwardIterator>
1237vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001238 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001239 is_constructible<
1240 value_type,
1241 typename iterator_traits<_ForwardIterator>::reference>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001242 : __base(__a)
1243{
Louis Dionneba400782020-10-02 15:02:52 -04001244#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001245 __get_db()->__insert_c(this);
1246#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001247 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001248 if (__n > 0)
1249 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001250 __vallocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001251 __construct_at_end(__first, __last, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001252 }
1253}
1254
1255template <class _Tp, class _Allocator>
1256vector<_Tp, _Allocator>::vector(const vector& __x)
1257 : __base(__alloc_traits::select_on_container_copy_construction(__x.__alloc()))
1258{
Louis Dionneba400782020-10-02 15:02:52 -04001259#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001260 __get_db()->__insert_c(this);
1261#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001262 size_type __n = __x.size();
1263 if (__n > 0)
1264 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001265 __vallocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001266 __construct_at_end(__x.__begin_, __x.__end_, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001267 }
1268}
1269
1270template <class _Tp, class _Allocator>
Arthur O'Dwyer9b9662f2021-03-01 17:08:24 -05001271vector<_Tp, _Allocator>::vector(const vector& __x, const __identity_t<allocator_type>& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001272 : __base(__a)
1273{
Louis Dionneba400782020-10-02 15:02:52 -04001274#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001275 __get_db()->__insert_c(this);
1276#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001277 size_type __n = __x.size();
1278 if (__n > 0)
1279 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001280 __vallocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001281 __construct_at_end(__x.__begin_, __x.__end_, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001282 }
1283}
1284
Eric Fiseliered9e9362017-04-16 02:40:45 +00001285#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001286
1287template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001288inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001289vector<_Tp, _Allocator>::vector(vector&& __x)
Marshall Clowe5108202015-07-14 14:46:32 +00001290#if _LIBCPP_STD_VER > 14
1291 _NOEXCEPT
1292#else
Howard Hinnant1c936782011-06-03 19:40:40 +00001293 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowe5108202015-07-14 14:46:32 +00001294#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001295 : __base(_VSTD::move(__x.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001296{
Louis Dionneba400782020-10-02 15:02:52 -04001297#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001298 __get_db()->__insert_c(this);
Howard Hinnanta1e60cd2011-09-19 16:34:29 +00001299 __get_db()->swap(this, &__x);
Howard Hinnant27e0e772011-09-14 18:33:51 +00001300#endif
Howard Hinnant9cd22302011-09-16 18:41:29 +00001301 this->__begin_ = __x.__begin_;
1302 this->__end_ = __x.__end_;
1303 this->__end_cap() = __x.__end_cap();
Howard Hinnant76053d72013-06-27 19:35:32 +00001304 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001305}
1306
1307template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001308inline _LIBCPP_INLINE_VISIBILITY
Arthur O'Dwyer9b9662f2021-03-01 17:08:24 -05001309vector<_Tp, _Allocator>::vector(vector&& __x, const __identity_t<allocator_type>& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001310 : __base(__a)
1311{
Louis Dionneba400782020-10-02 15:02:52 -04001312#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001313 __get_db()->__insert_c(this);
1314#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001315 if (__a == __x.__alloc())
1316 {
1317 this->__begin_ = __x.__begin_;
1318 this->__end_ = __x.__end_;
1319 this->__end_cap() = __x.__end_cap();
1320 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Louis Dionneba400782020-10-02 15:02:52 -04001321#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnanta1e60cd2011-09-19 16:34:29 +00001322 __get_db()->swap(this, &__x);
1323#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001324 }
1325 else
1326 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001327 typedef move_iterator<iterator> _Ip;
1328 assign(_Ip(__x.begin()), _Ip(__x.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001329 }
1330}
1331
1332template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001333inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001334vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il)
1335{
Louis Dionneba400782020-10-02 15:02:52 -04001336#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001337 __get_db()->__insert_c(this);
1338#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001339 if (__il.size() > 0)
1340 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001341 __vallocate(__il.size());
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001342 __construct_at_end(__il.begin(), __il.end(), __il.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001343 }
1344}
1345
1346template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001347inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001348vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
1349 : __base(__a)
1350{
Louis Dionneba400782020-10-02 15:02:52 -04001351#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001352 __get_db()->__insert_c(this);
1353#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001354 if (__il.size() > 0)
1355 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001356 __vallocate(__il.size());
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001357 __construct_at_end(__il.begin(), __il.end(), __il.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001358 }
1359}
1360
1361template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001362inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001363vector<_Tp, _Allocator>&
1364vector<_Tp, _Allocator>::operator=(vector&& __x)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001365 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001366{
1367 __move_assign(__x, integral_constant<bool,
1368 __alloc_traits::propagate_on_container_move_assignment::value>());
1369 return *this;
1370}
1371
1372template <class _Tp, class _Allocator>
1373void
1374vector<_Tp, _Allocator>::__move_assign(vector& __c, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001375 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001376{
1377 if (__base::__alloc() != __c.__alloc())
1378 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001379 typedef move_iterator<iterator> _Ip;
1380 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001381 }
1382 else
1383 __move_assign(__c, true_type());
1384}
1385
1386template <class _Tp, class _Allocator>
1387void
1388vector<_Tp, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00001389 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001390{
Marshall Clow3ff48e02018-05-22 16:20:28 +00001391 __vdeallocate();
Marshall Clow136d45c2014-07-21 15:11:13 +00001392 __base::__move_assign_alloc(__c); // this can throw
Howard Hinnantc51e1022010-05-11 19:42:16 +00001393 this->__begin_ = __c.__begin_;
1394 this->__end_ = __c.__end_;
1395 this->__end_cap() = __c.__end_cap();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001396 __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
Louis Dionneba400782020-10-02 15:02:52 -04001397#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnanta1e60cd2011-09-19 16:34:29 +00001398 __get_db()->swap(this, &__c);
1399#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001400}
1401
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001402#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001403
1404template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001405inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001406vector<_Tp, _Allocator>&
1407vector<_Tp, _Allocator>::operator=(const vector& __x)
1408{
Mark de Wever357a1fc2021-09-28 19:15:18 +02001409 if (this != _VSTD::addressof(__x))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001410 {
1411 __base::__copy_assign_alloc(__x);
1412 assign(__x.__begin_, __x.__end_);
1413 }
1414 return *this;
1415}
1416
1417template <class _Tp, class _Allocator>
1418template <class _InputIterator>
1419typename enable_if
1420<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001421 __is_cpp17_input_iterator <_InputIterator>::value &&
1422 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001423 is_constructible<
1424 _Tp,
1425 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001426 void
1427>::type
1428vector<_Tp, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
1429{
1430 clear();
1431 for (; __first != __last; ++__first)
Eric Fiselier96919722017-10-17 13:03:17 +00001432 __emplace_back(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001433}
1434
1435template <class _Tp, class _Allocator>
1436template <class _ForwardIterator>
1437typename enable_if
1438<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001439 __is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001440 is_constructible<
1441 _Tp,
1442 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001443 void
1444>::type
1445vector<_Tp, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
1446{
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001447 size_type __new_size = static_cast<size_type>(_VSTD::distance(__first, __last));
1448 if (__new_size <= capacity())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001449 {
1450 _ForwardIterator __mid = __last;
1451 bool __growing = false;
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001452 if (__new_size > size())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001453 {
1454 __growing = true;
1455 __mid = __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001456 _VSTD::advance(__mid, size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001457 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001458 pointer __m = _VSTD::copy(__first, __mid, this->__begin_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001459 if (__growing)
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001460 __construct_at_end(__mid, __last, __new_size - size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001461 else
1462 this->__destruct_at_end(__m);
1463 }
1464 else
1465 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001466 __vdeallocate();
1467 __vallocate(__recommend(__new_size));
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001468 __construct_at_end(__first, __last, __new_size);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001469 }
Eric Fiselier69c51982016-12-28 06:06:09 +00001470 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001471}
1472
1473template <class _Tp, class _Allocator>
1474void
1475vector<_Tp, _Allocator>::assign(size_type __n, const_reference __u)
1476{
1477 if (__n <= capacity())
1478 {
1479 size_type __s = size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001480 _VSTD::fill_n(this->__begin_, _VSTD::min(__n, __s), __u);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001481 if (__n > __s)
1482 __construct_at_end(__n - __s, __u);
1483 else
Howard Hinnant155c2af2010-05-24 17:49:41 +00001484 this->__destruct_at_end(this->__begin_ + __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001485 }
1486 else
1487 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001488 __vdeallocate();
1489 __vallocate(__recommend(static_cast<size_type>(__n)));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001490 __construct_at_end(__n, __u);
1491 }
Eric Fiselier69c51982016-12-28 06:06:09 +00001492 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001493}
1494
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001495template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001496inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001497typename vector<_Tp, _Allocator>::iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001498vector<_Tp, _Allocator>::__make_iter(pointer __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001499{
Louis Dionneba400782020-10-02 15:02:52 -04001500#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001501 return iterator(this, __p);
1502#else
1503 return iterator(__p);
1504#endif
1505}
1506
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001507template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001508inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001509typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001510vector<_Tp, _Allocator>::__make_iter(const_pointer __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001511{
Louis Dionneba400782020-10-02 15:02:52 -04001512#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001513 return const_iterator(this, __p);
1514#else
1515 return const_iterator(__p);
1516#endif
1517}
1518
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001519template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001520inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001521typename vector<_Tp, _Allocator>::iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001522vector<_Tp, _Allocator>::begin() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001523{
1524 return __make_iter(this->__begin_);
1525}
1526
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001527template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001528inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001529typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001530vector<_Tp, _Allocator>::begin() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001531{
1532 return __make_iter(this->__begin_);
1533}
1534
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001535template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001536inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001537typename vector<_Tp, _Allocator>::iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001538vector<_Tp, _Allocator>::end() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001539{
1540 return __make_iter(this->__end_);
1541}
1542
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001543template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001544inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001545typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001546vector<_Tp, _Allocator>::end() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001547{
1548 return __make_iter(this->__end_);
1549}
1550
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001551template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001552inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001553typename vector<_Tp, _Allocator>::reference
Marshall Clowd6470492019-03-15 00:29:35 +00001554vector<_Tp, _Allocator>::operator[](size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001555{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001556 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001557 return this->__begin_[__n];
1558}
1559
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001560template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001561inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001562typename vector<_Tp, _Allocator>::const_reference
Marshall Clowd6470492019-03-15 00:29:35 +00001563vector<_Tp, _Allocator>::operator[](size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001564{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001565 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001566 return this->__begin_[__n];
1567}
1568
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001569template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001570typename vector<_Tp, _Allocator>::reference
1571vector<_Tp, _Allocator>::at(size_type __n)
1572{
1573 if (__n >= size())
1574 this->__throw_out_of_range();
1575 return this->__begin_[__n];
1576}
1577
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001578template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001579typename vector<_Tp, _Allocator>::const_reference
1580vector<_Tp, _Allocator>::at(size_type __n) const
1581{
1582 if (__n >= size())
1583 this->__throw_out_of_range();
1584 return this->__begin_[__n];
1585}
1586
1587template <class _Tp, class _Allocator>
1588void
1589vector<_Tp, _Allocator>::reserve(size_type __n)
1590{
1591 if (__n > capacity())
1592 {
1593 allocator_type& __a = this->__alloc();
Howard Hinnant1c936782011-06-03 19:40:40 +00001594 __split_buffer<value_type, allocator_type&> __v(__n, size(), __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001595 __swap_out_circular_buffer(__v);
1596 }
1597}
1598
1599template <class _Tp, class _Allocator>
1600void
Howard Hinnant1c936782011-06-03 19:40:40 +00001601vector<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001602{
1603 if (capacity() > size())
1604 {
1605#ifndef _LIBCPP_NO_EXCEPTIONS
1606 try
1607 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001608#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001609 allocator_type& __a = this->__alloc();
Howard Hinnant1c936782011-06-03 19:40:40 +00001610 __split_buffer<value_type, allocator_type&> __v(size(), size(), __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001611 __swap_out_circular_buffer(__v);
1612#ifndef _LIBCPP_NO_EXCEPTIONS
1613 }
1614 catch (...)
1615 {
1616 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001617#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001618 }
1619}
1620
1621template <class _Tp, class _Allocator>
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001622template <class _Up>
1623void
Eric Fiseliered9e9362017-04-16 02:40:45 +00001624#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001625vector<_Tp, _Allocator>::__push_back_slow_path(_Up&& __x)
1626#else
1627vector<_Tp, _Allocator>::__push_back_slow_path(_Up& __x)
1628#endif
1629{
1630 allocator_type& __a = this->__alloc();
1631 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1632 // __v.push_back(_VSTD::forward<_Up>(__x));
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001633 __alloc_traits::construct(__a, _VSTD::__to_address(__v.__end_), _VSTD::forward<_Up>(__x));
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001634 __v.__end_++;
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001635 __swap_out_circular_buffer(__v);
1636}
1637
1638template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001639inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001640void
1641vector<_Tp, _Allocator>::push_back(const_reference __x)
1642{
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001643 if (this->__end_ != this->__end_cap())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001644 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001645 __construct_one_at_end(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001646 }
1647 else
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001648 __push_back_slow_path(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001649}
1650
Eric Fiseliered9e9362017-04-16 02:40:45 +00001651#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001652
1653template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001654inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001655void
1656vector<_Tp, _Allocator>::push_back(value_type&& __x)
1657{
1658 if (this->__end_ < this->__end_cap())
1659 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001660 __construct_one_at_end(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001661 }
1662 else
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001663 __push_back_slow_path(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001664}
1665
1666template <class _Tp, class _Allocator>
1667template <class... _Args>
1668void
Howard Hinnantb6c49562012-02-26 15:30:12 +00001669vector<_Tp, _Allocator>::__emplace_back_slow_path(_Args&&... __args)
1670{
1671 allocator_type& __a = this->__alloc();
1672 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1673// __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001674 __alloc_traits::construct(__a, _VSTD::__to_address(__v.__end_), _VSTD::forward<_Args>(__args)...);
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001675 __v.__end_++;
Howard Hinnantb6c49562012-02-26 15:30:12 +00001676 __swap_out_circular_buffer(__v);
1677}
1678
1679template <class _Tp, class _Allocator>
1680template <class... _Args>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001681inline
Marshall Clowea52cc42017-01-24 23:09:12 +00001682#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001683typename vector<_Tp, _Allocator>::reference
Marshall Clowea52cc42017-01-24 23:09:12 +00001684#else
1685void
1686#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001687vector<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1688{
1689 if (this->__end_ < this->__end_cap())
1690 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001691 __construct_one_at_end(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001692 }
1693 else
Howard Hinnantb6c49562012-02-26 15:30:12 +00001694 __emplace_back_slow_path(_VSTD::forward<_Args>(__args)...);
Marshall Clowea52cc42017-01-24 23:09:12 +00001695#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001696 return this->back();
Marshall Clowea52cc42017-01-24 23:09:12 +00001697#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001698}
1699
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001700#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001701
1702template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001703inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001704void
1705vector<_Tp, _Allocator>::pop_back()
1706{
Kristina Bessonovaaeeaa7e2021-05-09 19:29:56 +02001707 _LIBCPP_ASSERT(!empty(), "vector::pop_back called on an empty vector");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001708 this->__destruct_at_end(this->__end_ - 1);
1709}
1710
1711template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001712inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001713typename vector<_Tp, _Allocator>::iterator
1714vector<_Tp, _Allocator>::erase(const_iterator __position)
1715{
Louis Dionneba400782020-10-02 15:02:52 -04001716#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001717 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1718 "vector::erase(iterator) called with an iterator not"
1719 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001720#endif
Howard Hinnant58d52d52013-03-25 22:12:26 +00001721 _LIBCPP_ASSERT(__position != end(),
1722 "vector::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnant76053d72013-06-27 19:35:32 +00001723 difference_type __ps = __position - cbegin();
1724 pointer __p = this->__begin_ + __ps;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001725 this->__destruct_at_end(_VSTD::move(__p + 1, this->__end_, __p));
Eric Fiselier69c51982016-12-28 06:06:09 +00001726 this->__invalidate_iterators_past(__p-1);
1727 iterator __r = __make_iter(__p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001728 return __r;
1729}
1730
1731template <class _Tp, class _Allocator>
1732typename vector<_Tp, _Allocator>::iterator
1733vector<_Tp, _Allocator>::erase(const_iterator __first, const_iterator __last)
1734{
Louis Dionneba400782020-10-02 15:02:52 -04001735#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001736 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
1737 "vector::erase(iterator, iterator) called with an iterator not"
1738 " referring to this vector");
Eric Fiselier69c51982016-12-28 06:06:09 +00001739 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__last) == this,
1740 "vector::erase(iterator, iterator) called with an iterator not"
1741 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001742#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001743 _LIBCPP_ASSERT(__first <= __last, "vector::erase(first, last) called with invalid range");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001744 pointer __p = this->__begin_ + (__first - begin());
Eric Fiselier69c51982016-12-28 06:06:09 +00001745 if (__first != __last) {
Howard Hinnantc580cc32013-04-18 15:02:57 +00001746 this->__destruct_at_end(_VSTD::move(__p + (__last - __first), this->__end_, __p));
Eric Fiselier69c51982016-12-28 06:06:09 +00001747 this->__invalidate_iterators_past(__p - 1);
1748 }
1749 iterator __r = __make_iter(__p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001750 return __r;
1751}
1752
1753template <class _Tp, class _Allocator>
1754void
1755vector<_Tp, _Allocator>::__move_range(pointer __from_s, pointer __from_e, pointer __to)
1756{
1757 pointer __old_last = this->__end_;
1758 difference_type __n = __old_last - __to;
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001759 {
1760 pointer __i = __from_s + __n;
1761 _ConstructTransaction __tx(*this, __from_e - __i);
Martijn Vels1e2e1b82020-06-18 13:14:02 -04001762 for (pointer __pos = __tx.__pos_; __i < __from_e;
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04001763 ++__i, (void) ++__pos, __tx.__pos_ = __pos) {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001764 __alloc_traits::construct(this->__alloc(),
Martijn Vels1e2e1b82020-06-18 13:14:02 -04001765 _VSTD::__to_address(__pos),
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001766 _VSTD::move(*__i));
1767 }
1768 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001769 _VSTD::move_backward(__from_s, __from_s + __n, __old_last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001770}
1771
1772template <class _Tp, class _Allocator>
1773typename vector<_Tp, _Allocator>::iterator
1774vector<_Tp, _Allocator>::insert(const_iterator __position, const_reference __x)
1775{
Louis Dionneba400782020-10-02 15:02:52 -04001776#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001777 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1778 "vector::insert(iterator, x) called with an iterator not"
1779 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001780#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001781 pointer __p = this->__begin_ + (__position - begin());
1782 if (this->__end_ < this->__end_cap())
1783 {
1784 if (__p == this->__end_)
1785 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001786 __construct_one_at_end(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001787 }
1788 else
1789 {
1790 __move_range(__p, this->__end_, __p + 1);
1791 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1792 if (__p <= __xr && __xr < this->__end_)
1793 ++__xr;
1794 *__p = *__xr;
1795 }
1796 }
1797 else
1798 {
1799 allocator_type& __a = this->__alloc();
1800 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
1801 __v.push_back(__x);
1802 __p = __swap_out_circular_buffer(__v, __p);
1803 }
1804 return __make_iter(__p);
1805}
1806
Eric Fiseliered9e9362017-04-16 02:40:45 +00001807#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001808
1809template <class _Tp, class _Allocator>
1810typename vector<_Tp, _Allocator>::iterator
1811vector<_Tp, _Allocator>::insert(const_iterator __position, value_type&& __x)
1812{
Louis Dionneba400782020-10-02 15:02:52 -04001813#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001814 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1815 "vector::insert(iterator, x) called with an iterator not"
1816 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001817#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001818 pointer __p = this->__begin_ + (__position - begin());
1819 if (this->__end_ < this->__end_cap())
1820 {
1821 if (__p == this->__end_)
1822 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001823 __construct_one_at_end(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001824 }
1825 else
1826 {
1827 __move_range(__p, this->__end_, __p + 1);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001828 *__p = _VSTD::move(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001829 }
1830 }
1831 else
1832 {
1833 allocator_type& __a = this->__alloc();
1834 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001835 __v.push_back(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001836 __p = __swap_out_circular_buffer(__v, __p);
1837 }
1838 return __make_iter(__p);
1839}
1840
1841template <class _Tp, class _Allocator>
1842template <class... _Args>
1843typename vector<_Tp, _Allocator>::iterator
1844vector<_Tp, _Allocator>::emplace(const_iterator __position, _Args&&... __args)
1845{
Louis Dionneba400782020-10-02 15:02:52 -04001846#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001847 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1848 "vector::emplace(iterator, x) called with an iterator not"
1849 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001850#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001851 pointer __p = this->__begin_ + (__position - begin());
1852 if (this->__end_ < this->__end_cap())
1853 {
1854 if (__p == this->__end_)
1855 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001856 __construct_one_at_end(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001857 }
1858 else
1859 {
Marshall Clowa591b9a2016-07-11 21:38:08 +00001860 __temp_value<value_type, _Allocator> __tmp(this->__alloc(), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001861 __move_range(__p, this->__end_, __p + 1);
Marshall Clowa591b9a2016-07-11 21:38:08 +00001862 *__p = _VSTD::move(__tmp.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001863 }
1864 }
1865 else
1866 {
1867 allocator_type& __a = this->__alloc();
1868 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001869 __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001870 __p = __swap_out_circular_buffer(__v, __p);
1871 }
1872 return __make_iter(__p);
1873}
1874
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001875#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001876
1877template <class _Tp, class _Allocator>
1878typename vector<_Tp, _Allocator>::iterator
1879vector<_Tp, _Allocator>::insert(const_iterator __position, size_type __n, const_reference __x)
1880{
Louis Dionneba400782020-10-02 15:02:52 -04001881#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001882 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1883 "vector::insert(iterator, n, x) called with an iterator not"
1884 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001885#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001886 pointer __p = this->__begin_ + (__position - begin());
1887 if (__n > 0)
1888 {
1889 if (__n <= static_cast<size_type>(this->__end_cap() - this->__end_))
1890 {
1891 size_type __old_n = __n;
1892 pointer __old_last = this->__end_;
1893 if (__n > static_cast<size_type>(this->__end_ - __p))
1894 {
1895 size_type __cx = __n - (this->__end_ - __p);
1896 __construct_at_end(__cx, __x);
1897 __n -= __cx;
1898 }
1899 if (__n > 0)
1900 {
1901 __move_range(__p, __old_last, __p + __old_n);
1902 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1903 if (__p <= __xr && __xr < this->__end_)
1904 __xr += __old_n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001905 _VSTD::fill_n(__p, __n, *__xr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001906 }
1907 }
1908 else
1909 {
1910 allocator_type& __a = this->__alloc();
1911 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1912 __v.__construct_at_end(__n, __x);
1913 __p = __swap_out_circular_buffer(__v, __p);
1914 }
1915 }
1916 return __make_iter(__p);
1917}
1918
1919template <class _Tp, class _Allocator>
1920template <class _InputIterator>
1921typename enable_if
1922<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001923 __is_cpp17_input_iterator <_InputIterator>::value &&
1924 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001925 is_constructible<
1926 _Tp,
1927 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001928 typename vector<_Tp, _Allocator>::iterator
1929>::type
1930vector<_Tp, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
1931{
Louis Dionneba400782020-10-02 15:02:52 -04001932#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001933 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1934 "vector::insert(iterator, range) called with an iterator not"
1935 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001936#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001937 difference_type __off = __position - begin();
1938 pointer __p = this->__begin_ + __off;
1939 allocator_type& __a = this->__alloc();
1940 pointer __old_last = this->__end_;
1941 for (; this->__end_ != this->__end_cap() && __first != __last; ++__first)
1942 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001943 __construct_one_at_end(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001944 }
1945 __split_buffer<value_type, allocator_type&> __v(__a);
1946 if (__first != __last)
1947 {
1948#ifndef _LIBCPP_NO_EXCEPTIONS
1949 try
1950 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001951#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001952 __v.__construct_at_end(__first, __last);
1953 difference_type __old_size = __old_last - this->__begin_;
1954 difference_type __old_p = __p - this->__begin_;
1955 reserve(__recommend(size() + __v.size()));
1956 __p = this->__begin_ + __old_p;
1957 __old_last = this->__begin_ + __old_size;
1958#ifndef _LIBCPP_NO_EXCEPTIONS
1959 }
1960 catch (...)
1961 {
1962 erase(__make_iter(__old_last), end());
1963 throw;
1964 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001965#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001966 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001967 __p = _VSTD::rotate(__p, __old_last, this->__end_);
Louis Dionne202a4cb2020-02-11 10:59:12 +01001968 insert(__make_iter(__p), _VSTD::make_move_iterator(__v.begin()),
1969 _VSTD::make_move_iterator(__v.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001970 return begin() + __off;
1971}
1972
1973template <class _Tp, class _Allocator>
1974template <class _ForwardIterator>
1975typename enable_if
1976<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001977 __is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001978 is_constructible<
1979 _Tp,
1980 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001981 typename vector<_Tp, _Allocator>::iterator
1982>::type
1983vector<_Tp, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
1984{
Louis Dionneba400782020-10-02 15:02:52 -04001985#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001986 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1987 "vector::insert(iterator, range) called with an iterator not"
1988 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001989#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001990 pointer __p = this->__begin_ + (__position - begin());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001991 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001992 if (__n > 0)
1993 {
1994 if (__n <= this->__end_cap() - this->__end_)
1995 {
1996 size_type __old_n = __n;
1997 pointer __old_last = this->__end_;
1998 _ForwardIterator __m = __last;
1999 difference_type __dx = this->__end_ - __p;
2000 if (__n > __dx)
2001 {
2002 __m = __first;
Eric Fiselieracfe6f02015-03-31 16:54:19 +00002003 difference_type __diff = this->__end_ - __p;
2004 _VSTD::advance(__m, __diff);
2005 __construct_at_end(__m, __last, __n - __diff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002006 __n = __dx;
2007 }
2008 if (__n > 0)
2009 {
2010 __move_range(__p, __old_last, __p + __old_n);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002011 _VSTD::copy(__first, __m, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002012 }
2013 }
2014 else
2015 {
2016 allocator_type& __a = this->__alloc();
2017 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
2018 __v.__construct_at_end(__first, __last);
2019 __p = __swap_out_circular_buffer(__v, __p);
2020 }
2021 }
2022 return __make_iter(__p);
2023}
2024
2025template <class _Tp, class _Allocator>
2026void
2027vector<_Tp, _Allocator>::resize(size_type __sz)
2028{
2029 size_type __cs = size();
2030 if (__cs < __sz)
2031 this->__append(__sz - __cs);
2032 else if (__cs > __sz)
2033 this->__destruct_at_end(this->__begin_ + __sz);
2034}
2035
2036template <class _Tp, class _Allocator>
2037void
2038vector<_Tp, _Allocator>::resize(size_type __sz, const_reference __x)
2039{
2040 size_type __cs = size();
2041 if (__cs < __sz)
2042 this->__append(__sz - __cs, __x);
2043 else if (__cs > __sz)
2044 this->__destruct_at_end(this->__begin_ + __sz);
2045}
2046
2047template <class _Tp, class _Allocator>
2048void
2049vector<_Tp, _Allocator>::swap(vector& __x)
Marshall Clow8982dcd2015-07-13 20:04:56 +00002050#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00002051 _NOEXCEPT
Marshall Clow8982dcd2015-07-13 20:04:56 +00002052#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00002053 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00002054 __is_nothrow_swappable<allocator_type>::value)
2055#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002056{
Howard Hinnant27e0e772011-09-14 18:33:51 +00002057 _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
2058 this->__alloc() == __x.__alloc(),
2059 "vector::swap: Either propagate_on_container_swap must be true"
2060 " or the allocators must compare equal");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002061 _VSTD::swap(this->__begin_, __x.__begin_);
2062 _VSTD::swap(this->__end_, __x.__end_);
2063 _VSTD::swap(this->__end_cap(), __x.__end_cap());
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05002064 _VSTD::__swap_allocator(this->__alloc(), __x.__alloc(),
Marshall Clow8982dcd2015-07-13 20:04:56 +00002065 integral_constant<bool,__alloc_traits::propagate_on_container_swap::value>());
Louis Dionneba400782020-10-02 15:02:52 -04002066#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002067 __get_db()->swap(this, &__x);
Louis Dionneba400782020-10-02 15:02:52 -04002068#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002069}
2070
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002071template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002072bool
2073vector<_Tp, _Allocator>::__invariants() const
2074{
Howard Hinnant76053d72013-06-27 19:35:32 +00002075 if (this->__begin_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002076 {
Howard Hinnant76053d72013-06-27 19:35:32 +00002077 if (this->__end_ != nullptr || this->__end_cap() != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002078 return false;
2079 }
2080 else
2081 {
2082 if (this->__begin_ > this->__end_)
2083 return false;
2084 if (this->__begin_ == this->__end_cap())
2085 return false;
2086 if (this->__end_ > this->__end_cap())
2087 return false;
2088 }
2089 return true;
2090}
2091
Louis Dionneba400782020-10-02 15:02:52 -04002092#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002093
Howard Hinnantc51e1022010-05-11 19:42:16 +00002094template <class _Tp, class _Allocator>
Howard Hinnant27e0e772011-09-14 18:33:51 +00002095bool
2096vector<_Tp, _Allocator>::__dereferenceable(const const_iterator* __i) const
2097{
2098 return this->__begin_ <= __i->base() && __i->base() < this->__end_;
2099}
2100
2101template <class _Tp, class _Allocator>
2102bool
2103vector<_Tp, _Allocator>::__decrementable(const const_iterator* __i) const
2104{
2105 return this->__begin_ < __i->base() && __i->base() <= this->__end_;
2106}
2107
2108template <class _Tp, class _Allocator>
2109bool
2110vector<_Tp, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
2111{
2112 const_pointer __p = __i->base() + __n;
2113 return this->__begin_ <= __p && __p <= this->__end_;
2114}
2115
2116template <class _Tp, class _Allocator>
2117bool
2118vector<_Tp, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
2119{
2120 const_pointer __p = __i->base() + __n;
2121 return this->__begin_ <= __p && __p < this->__end_;
2122}
2123
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002124#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002125
2126template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002127inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002128void
2129vector<_Tp, _Allocator>::__invalidate_all_iterators()
2130{
Louis Dionneba400782020-10-02 15:02:52 -04002131#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002132 __get_db()->__invalidate_all(this);
Louis Dionneba400782020-10-02 15:02:52 -04002133#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002134}
2135
Eric Fiselier69c51982016-12-28 06:06:09 +00002136
2137template <class _Tp, class _Allocator>
2138inline _LIBCPP_INLINE_VISIBILITY
2139void
2140vector<_Tp, _Allocator>::__invalidate_iterators_past(pointer __new_last) {
Louis Dionneba400782020-10-02 15:02:52 -04002141#if _LIBCPP_DEBUG_LEVEL == 2
Eric Fiselier69c51982016-12-28 06:06:09 +00002142 __c_node* __c = __get_db()->__find_c_and_lock(this);
2143 for (__i_node** __p = __c->end_; __p != __c->beg_; ) {
2144 --__p;
2145 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
2146 if (__i->base() > __new_last) {
2147 (*__p)->__c_ = nullptr;
2148 if (--__c->end_ != __p)
Arthur O'Dwyer22236632020-12-07 21:50:15 -05002149 _VSTD::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
Eric Fiselier69c51982016-12-28 06:06:09 +00002150 }
2151 }
2152 __get_db()->unlock();
2153#else
2154 ((void)__new_last);
2155#endif
2156}
2157
Howard Hinnantc51e1022010-05-11 19:42:16 +00002158// vector<bool>
2159
2160template <class _Allocator> class vector<bool, _Allocator>;
2161
2162template <class _Allocator> struct hash<vector<bool, _Allocator> >;
2163
2164template <class _Allocator>
Howard Hinnantb4ef6482011-07-02 20:33:23 +00002165struct __has_storage_type<vector<bool, _Allocator> >
2166{
2167 static const bool value = true;
2168};
2169
2170template <class _Allocator>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002171class _LIBCPP_TEMPLATE_VIS vector<bool, _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002172 : private __vector_base_common<true>
2173{
2174public:
2175 typedef vector __self;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002176 typedef bool value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002177 typedef _Allocator allocator_type;
2178 typedef allocator_traits<allocator_type> __alloc_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002179 typedef typename __alloc_traits::size_type size_type;
2180 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant896b7622012-05-07 16:50:38 +00002181 typedef size_type __storage_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002182 typedef __bit_iterator<vector, false> pointer;
2183 typedef __bit_iterator<vector, true> const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002184 typedef pointer iterator;
2185 typedef const_pointer const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002186 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
2187 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002188
2189private:
Marshall Clow940e01c2015-04-07 05:21:38 +00002190 typedef typename __rebind_alloc_helper<__alloc_traits, __storage_type>::type __storage_allocator;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002191 typedef allocator_traits<__storage_allocator> __storage_traits;
2192 typedef typename __storage_traits::pointer __storage_pointer;
2193 typedef typename __storage_traits::const_pointer __const_storage_pointer;
2194
2195 __storage_pointer __begin_;
2196 size_type __size_;
2197 __compressed_pair<size_type, __storage_allocator> __cap_alloc_;
Howard Hinnant71d1c192011-07-09 15:50:42 +00002198public:
Howard Hinnantb4ef6482011-07-02 20:33:23 +00002199 typedef __bit_reference<vector> reference;
2200 typedef __bit_const_reference<vector> const_reference;
Howard Hinnant71d1c192011-07-09 15:50:42 +00002201private:
Howard Hinnant1c936782011-06-03 19:40:40 +00002202 _LIBCPP_INLINE_VISIBILITY
2203 size_type& __cap() _NOEXCEPT
2204 {return __cap_alloc_.first();}
2205 _LIBCPP_INLINE_VISIBILITY
2206 const size_type& __cap() const _NOEXCEPT
2207 {return __cap_alloc_.first();}
2208 _LIBCPP_INLINE_VISIBILITY
2209 __storage_allocator& __alloc() _NOEXCEPT
2210 {return __cap_alloc_.second();}
2211 _LIBCPP_INLINE_VISIBILITY
2212 const __storage_allocator& __alloc() const _NOEXCEPT
2213 {return __cap_alloc_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002214
2215 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
2216
Howard Hinnant1c936782011-06-03 19:40:40 +00002217 _LIBCPP_INLINE_VISIBILITY
2218 static size_type __internal_cap_to_external(size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002219 {return __n * __bits_per_word;}
Howard Hinnant1c936782011-06-03 19:40:40 +00002220 _LIBCPP_INLINE_VISIBILITY
2221 static size_type __external_cap_to_internal(size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002222 {return (__n - 1) / __bits_per_word + 1;}
2223
2224public:
Howard Hinnant1c936782011-06-03 19:40:40 +00002225 _LIBCPP_INLINE_VISIBILITY
Marshall Clowe546cbb2015-06-04 02:05:41 +00002226 vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clow8f282f42015-06-04 00:10:20 +00002227
2228 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
2229#if _LIBCPP_STD_VER <= 14
2230 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
2231#else
2232 _NOEXCEPT;
2233#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002234 ~vector();
2235 explicit vector(size_type __n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00002236#if _LIBCPP_STD_VER > 11
2237 explicit vector(size_type __n, const allocator_type& __a);
2238#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002239 vector(size_type __n, const value_type& __v);
2240 vector(size_type __n, const value_type& __v, const allocator_type& __a);
2241 template <class _InputIterator>
2242 vector(_InputIterator __first, _InputIterator __last,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002243 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
2244 !__is_cpp17_forward_iterator<_InputIterator>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002245 template <class _InputIterator>
2246 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002247 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
2248 !__is_cpp17_forward_iterator<_InputIterator>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002249 template <class _ForwardIterator>
2250 vector(_ForwardIterator __first, _ForwardIterator __last,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002251 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002252 template <class _ForwardIterator>
2253 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002254 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002255
2256 vector(const vector& __v);
2257 vector(const vector& __v, const allocator_type& __a);
2258 vector& operator=(const vector& __v);
Eric Fiseliered9e9362017-04-16 02:40:45 +00002259
2260#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002261 vector(initializer_list<value_type> __il);
2262 vector(initializer_list<value_type> __il, const allocator_type& __a);
2263
Howard Hinnant1c936782011-06-03 19:40:40 +00002264 _LIBCPP_INLINE_VISIBILITY
2265 vector(vector&& __v)
Marshall Clowe5108202015-07-14 14:46:32 +00002266#if _LIBCPP_STD_VER > 14
2267 _NOEXCEPT;
2268#else
Howard Hinnant1c936782011-06-03 19:40:40 +00002269 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowe5108202015-07-14 14:46:32 +00002270#endif
Arthur O'Dwyer9b9662f2021-03-01 17:08:24 -05002271 vector(vector&& __v, const __identity_t<allocator_type>& __a);
Howard Hinnant1c936782011-06-03 19:40:40 +00002272 _LIBCPP_INLINE_VISIBILITY
2273 vector& operator=(vector&& __v)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002274 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiseliered9e9362017-04-16 02:40:45 +00002275
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002276 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002277 vector& operator=(initializer_list<value_type> __il)
2278 {assign(__il.begin(), __il.end()); return *this;}
Eric Fiseliered9e9362017-04-16 02:40:45 +00002279
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002280#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002281
2282 template <class _InputIterator>
2283 typename enable_if
2284 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002285 __is_cpp17_input_iterator<_InputIterator>::value &&
2286 !__is_cpp17_forward_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002287 void
2288 >::type
2289 assign(_InputIterator __first, _InputIterator __last);
2290 template <class _ForwardIterator>
2291 typename enable_if
2292 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002293 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002294 void
2295 >::type
2296 assign(_ForwardIterator __first, _ForwardIterator __last);
2297
2298 void assign(size_type __n, const value_type& __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +00002299
2300#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002301 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002302 void assign(initializer_list<value_type> __il)
2303 {assign(__il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +00002304#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002305
Howard Hinnant1c936782011-06-03 19:40:40 +00002306 _LIBCPP_INLINE_VISIBILITY allocator_type get_allocator() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002307 {return allocator_type(this->__alloc());}
2308
Howard Hinnant1c936782011-06-03 19:40:40 +00002309 size_type max_size() const _NOEXCEPT;
2310 _LIBCPP_INLINE_VISIBILITY
2311 size_type capacity() const _NOEXCEPT
2312 {return __internal_cap_to_external(__cap());}
2313 _LIBCPP_INLINE_VISIBILITY
2314 size_type size() const _NOEXCEPT
2315 {return __size_;}
Marshall Clow425f5752017-11-15 05:51:26 +00002316 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +00002317 bool empty() const _NOEXCEPT
2318 {return __size_ == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002319 void reserve(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +00002320 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002321
Howard Hinnant1c936782011-06-03 19:40:40 +00002322 _LIBCPP_INLINE_VISIBILITY
2323 iterator begin() _NOEXCEPT
2324 {return __make_iter(0);}
2325 _LIBCPP_INLINE_VISIBILITY
2326 const_iterator begin() const _NOEXCEPT
2327 {return __make_iter(0);}
2328 _LIBCPP_INLINE_VISIBILITY
2329 iterator end() _NOEXCEPT
2330 {return __make_iter(__size_);}
2331 _LIBCPP_INLINE_VISIBILITY
2332 const_iterator end() const _NOEXCEPT
2333 {return __make_iter(__size_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002334
Howard Hinnant1c936782011-06-03 19:40:40 +00002335 _LIBCPP_INLINE_VISIBILITY
2336 reverse_iterator rbegin() _NOEXCEPT
2337 {return reverse_iterator(end());}
2338 _LIBCPP_INLINE_VISIBILITY
2339 const_reverse_iterator rbegin() const _NOEXCEPT
2340 {return const_reverse_iterator(end());}
2341 _LIBCPP_INLINE_VISIBILITY
2342 reverse_iterator rend() _NOEXCEPT
2343 {return reverse_iterator(begin());}
2344 _LIBCPP_INLINE_VISIBILITY
2345 const_reverse_iterator rend() const _NOEXCEPT
2346 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002347
Howard Hinnant1c936782011-06-03 19:40:40 +00002348 _LIBCPP_INLINE_VISIBILITY
2349 const_iterator cbegin() const _NOEXCEPT
2350 {return __make_iter(0);}
2351 _LIBCPP_INLINE_VISIBILITY
2352 const_iterator cend() const _NOEXCEPT
2353 {return __make_iter(__size_);}
2354 _LIBCPP_INLINE_VISIBILITY
2355 const_reverse_iterator crbegin() const _NOEXCEPT
2356 {return rbegin();}
2357 _LIBCPP_INLINE_VISIBILITY
2358 const_reverse_iterator crend() const _NOEXCEPT
2359 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002360
2361 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) {return __make_ref(__n);}
2362 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const {return __make_ref(__n);}
2363 reference at(size_type __n);
2364 const_reference at(size_type __n) const;
2365
2366 _LIBCPP_INLINE_VISIBILITY reference front() {return __make_ref(0);}
2367 _LIBCPP_INLINE_VISIBILITY const_reference front() const {return __make_ref(0);}
2368 _LIBCPP_INLINE_VISIBILITY reference back() {return __make_ref(__size_ - 1);}
2369 _LIBCPP_INLINE_VISIBILITY const_reference back() const {return __make_ref(__size_ - 1);}
2370
2371 void push_back(const value_type& __x);
Marshall Clowc46bb8e2013-08-13 23:54:12 +00002372#if _LIBCPP_STD_VER > 11
2373 template <class... _Args>
Marshall Clowea52cc42017-01-24 23:09:12 +00002374#if _LIBCPP_STD_VER > 14
2375 _LIBCPP_INLINE_VISIBILITY reference emplace_back(_Args&&... __args)
2376#else
2377 _LIBCPP_INLINE_VISIBILITY void emplace_back(_Args&&... __args)
2378#endif
2379 {
Eric Fiselier34ba5b92016-07-21 03:20:17 +00002380 push_back ( value_type ( _VSTD::forward<_Args>(__args)... ));
Marshall Clowea52cc42017-01-24 23:09:12 +00002381#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00002382 return this->back();
Marshall Clowea52cc42017-01-24 23:09:12 +00002383#endif
Eric Fiselier34ba5b92016-07-21 03:20:17 +00002384 }
Marshall Clowc46bb8e2013-08-13 23:54:12 +00002385#endif
2386
Howard Hinnantc51e1022010-05-11 19:42:16 +00002387 _LIBCPP_INLINE_VISIBILITY void pop_back() {--__size_;}
2388
Marshall Clowc46bb8e2013-08-13 23:54:12 +00002389#if _LIBCPP_STD_VER > 11
2390 template <class... _Args>
2391 _LIBCPP_INLINE_VISIBILITY iterator emplace(const_iterator position, _Args&&... __args)
2392 { return insert ( position, value_type ( _VSTD::forward<_Args>(__args)... )); }
2393#endif
2394
Howard Hinnantc51e1022010-05-11 19:42:16 +00002395 iterator insert(const_iterator __position, const value_type& __x);
2396 iterator insert(const_iterator __position, size_type __n, const value_type& __x);
2397 iterator insert(const_iterator __position, size_type __n, const_reference __x);
2398 template <class _InputIterator>
2399 typename enable_if
2400 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002401 __is_cpp17_input_iterator <_InputIterator>::value &&
2402 !__is_cpp17_forward_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002403 iterator
2404 >::type
2405 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
2406 template <class _ForwardIterator>
2407 typename enable_if
2408 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002409 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002410 iterator
2411 >::type
2412 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiseliered9e9362017-04-16 02:40:45 +00002413
2414#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002415 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002416 iterator insert(const_iterator __position, initializer_list<value_type> __il)
2417 {return insert(__position, __il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +00002418#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002419
Howard Hinnantcf823322010-12-17 14:46:43 +00002420 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002421 iterator erase(const_iterator __first, const_iterator __last);
2422
Howard Hinnant1c936782011-06-03 19:40:40 +00002423 _LIBCPP_INLINE_VISIBILITY
2424 void clear() _NOEXCEPT {__size_ = 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002425
Howard Hinnant1c936782011-06-03 19:40:40 +00002426 void swap(vector&)
Marshall Clow8982dcd2015-07-13 20:04:56 +00002427#if _LIBCPP_STD_VER >= 14
2428 _NOEXCEPT;
2429#else
Eric Fiselier69c51982016-12-28 06:06:09 +00002430 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00002431 __is_nothrow_swappable<allocator_type>::value);
2432#endif
Marshall Clow89eb3822016-04-07 14:20:31 +00002433 static void swap(reference __x, reference __y) _NOEXCEPT { _VSTD::swap(__x, __y); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002434
2435 void resize(size_type __sz, value_type __x = false);
Howard Hinnant1c936782011-06-03 19:40:40 +00002436 void flip() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002437
2438 bool __invariants() const;
2439
2440private:
Howard Hinnantcf823322010-12-17 14:46:43 +00002441 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Marshall Clow3ff48e02018-05-22 16:20:28 +00002442 void __vallocate(size_type __n);
2443 void __vdeallocate() _NOEXCEPT;
Howard Hinnant1c936782011-06-03 19:40:40 +00002444 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantea382952013-08-14 18:00:20 +00002445 static size_type __align_it(size_type __new_size) _NOEXCEPT
Eric Fiselierb41db9a2018-10-01 01:59:37 +00002446 {return __new_size + (__bits_per_word-1) & ~((size_type)__bits_per_word-1);}
Howard Hinnantcf823322010-12-17 14:46:43 +00002447 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
2448 _LIBCPP_INLINE_VISIBILITY void __construct_at_end(size_type __n, bool __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002449 template <class _ForwardIterator>
2450 typename enable_if
2451 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002452 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002453 void
2454 >::type
2455 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
2456 void __append(size_type __n, const_reference __x);
Howard Hinnant1c936782011-06-03 19:40:40 +00002457 _LIBCPP_INLINE_VISIBILITY
2458 reference __make_ref(size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002459 {return reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnant1c936782011-06-03 19:40:40 +00002460 _LIBCPP_INLINE_VISIBILITY
2461 const_reference __make_ref(size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002462 {return const_reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnant1c936782011-06-03 19:40:40 +00002463 _LIBCPP_INLINE_VISIBILITY
2464 iterator __make_iter(size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002465 {return iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnant1c936782011-06-03 19:40:40 +00002466 _LIBCPP_INLINE_VISIBILITY
2467 const_iterator __make_iter(size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002468 {return const_iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnant1c936782011-06-03 19:40:40 +00002469 _LIBCPP_INLINE_VISIBILITY
2470 iterator __const_iterator_cast(const_iterator __p) _NOEXCEPT
Howard Hinnant76053d72013-06-27 19:35:32 +00002471 {return begin() + (__p - cbegin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002472
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002473 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002474 void __copy_assign_alloc(const vector& __v)
2475 {__copy_assign_alloc(__v, integral_constant<bool,
2476 __storage_traits::propagate_on_container_copy_assignment::value>());}
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002477 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002478 void __copy_assign_alloc(const vector& __c, true_type)
2479 {
2480 if (__alloc() != __c.__alloc())
Marshall Clow3ff48e02018-05-22 16:20:28 +00002481 __vdeallocate();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002482 __alloc() = __c.__alloc();
2483 }
2484
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002485 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00002486 void __copy_assign_alloc(const vector&, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002487 {}
2488
2489 void __move_assign(vector& __c, false_type);
Howard Hinnant1c936782011-06-03 19:40:40 +00002490 void __move_assign(vector& __c, true_type)
2491 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002492 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002493 void __move_assign_alloc(vector& __c)
Howard Hinnant1c936782011-06-03 19:40:40 +00002494 _NOEXCEPT_(
2495 !__storage_traits::propagate_on_container_move_assignment::value ||
2496 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002497 {__move_assign_alloc(__c, integral_constant<bool,
2498 __storage_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002499 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +00002500 void __move_assign_alloc(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002501 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002502 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002503 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002504 }
2505
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002506 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00002507 void __move_assign_alloc(vector&, false_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002508 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002509 {}
2510
Howard Hinnant1c936782011-06-03 19:40:40 +00002511 size_t __hash_code() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002512
2513 friend class __bit_reference<vector>;
2514 friend class __bit_const_reference<vector>;
2515 friend class __bit_iterator<vector, false>;
2516 friend class __bit_iterator<vector, true>;
Howard Hinnant4210a0f2012-08-17 17:10:18 +00002517 friend struct __bit_array<vector>;
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002518 friend struct _LIBCPP_TEMPLATE_VIS hash<vector>;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002519};
2520
2521template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002522inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002523void
2524vector<bool, _Allocator>::__invalidate_all_iterators()
2525{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002526}
2527
2528// Allocate space for __n objects
2529// throws length_error if __n > max_size()
2530// throws (probably bad_alloc) if memory run out
2531// Precondition: __begin_ == __end_ == __cap() == 0
2532// Precondition: __n > 0
2533// Postcondition: capacity() == __n
2534// Postcondition: size() == 0
2535template <class _Allocator>
2536void
Marshall Clow3ff48e02018-05-22 16:20:28 +00002537vector<bool, _Allocator>::__vallocate(size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002538{
2539 if (__n > max_size())
2540 this->__throw_length_error();
2541 __n = __external_cap_to_internal(__n);
2542 this->__begin_ = __storage_traits::allocate(this->__alloc(), __n);
2543 this->__size_ = 0;
2544 this->__cap() = __n;
2545}
2546
2547template <class _Allocator>
2548void
Marshall Clow3ff48e02018-05-22 16:20:28 +00002549vector<bool, _Allocator>::__vdeallocate() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002550{
Howard Hinnant76053d72013-06-27 19:35:32 +00002551 if (this->__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002552 {
2553 __storage_traits::deallocate(this->__alloc(), this->__begin_, __cap());
2554 __invalidate_all_iterators();
Howard Hinnant76053d72013-06-27 19:35:32 +00002555 this->__begin_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002556 this->__size_ = this->__cap() = 0;
2557 }
2558}
2559
2560template <class _Allocator>
2561typename vector<bool, _Allocator>::size_type
Howard Hinnant1c936782011-06-03 19:40:40 +00002562vector<bool, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002563{
2564 size_type __amax = __storage_traits::max_size(__alloc());
2565 size_type __nmax = numeric_limits<size_type>::max() / 2; // end() >= begin(), always
2566 if (__nmax / __bits_per_word <= __amax)
2567 return __nmax;
2568 return __internal_cap_to_external(__amax);
2569}
2570
2571// Precondition: __new_size > capacity()
2572template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002573inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002574typename vector<bool, _Allocator>::size_type
2575vector<bool, _Allocator>::__recommend(size_type __new_size) const
2576{
2577 const size_type __ms = max_size();
2578 if (__new_size > __ms)
2579 this->__throw_length_error();
2580 const size_type __cap = capacity();
2581 if (__cap >= __ms / 2)
2582 return __ms;
Arthur O'Dwyerc0fb14b2021-07-26 18:23:00 -04002583 return _VSTD::max(2 * __cap, __align_it(__new_size));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002584}
2585
2586// Default constructs __n objects starting at __end_
2587// Precondition: __n > 0
2588// Precondition: size() + __n <= capacity()
2589// Postcondition: size() == size() + __n
2590template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002591inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002592void
2593vector<bool, _Allocator>::__construct_at_end(size_type __n, bool __x)
2594{
2595 size_type __old_size = this->__size_;
2596 this->__size_ += __n;
Marshall Clow1893ec72018-10-23 20:07:45 +00002597 if (__old_size == 0 || ((__old_size - 1) / __bits_per_word) != ((this->__size_ - 1) / __bits_per_word))
2598 {
2599 if (this->__size_ <= __bits_per_word)
2600 this->__begin_[0] = __storage_type(0);
2601 else
2602 this->__begin_[(this->__size_ - 1) / __bits_per_word] = __storage_type(0);
2603 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002604 _VSTD::fill_n(__make_iter(__old_size), __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002605}
2606
2607template <class _Allocator>
2608template <class _ForwardIterator>
2609typename enable_if
2610<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002611 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002612 void
2613>::type
2614vector<bool, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
2615{
2616 size_type __old_size = this->__size_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002617 this->__size_ += _VSTD::distance(__first, __last);
Marshall Clow1893ec72018-10-23 20:07:45 +00002618 if (__old_size == 0 || ((__old_size - 1) / __bits_per_word) != ((this->__size_ - 1) / __bits_per_word))
2619 {
2620 if (this->__size_ <= __bits_per_word)
2621 this->__begin_[0] = __storage_type(0);
2622 else
2623 this->__begin_[(this->__size_ - 1) / __bits_per_word] = __storage_type(0);
2624 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002625 _VSTD::copy(__first, __last, __make_iter(__old_size));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002626}
2627
2628template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002629inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002630vector<bool, _Allocator>::vector()
Marshall Clowe546cbb2015-06-04 02:05:41 +00002631 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant76053d72013-06-27 19:35:32 +00002632 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002633 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002634 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002635{
2636}
2637
2638template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002639inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002640vector<bool, _Allocator>::vector(const allocator_type& __a)
Marshall Clow8f282f42015-06-04 00:10:20 +00002641#if _LIBCPP_STD_VER <= 14
2642 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
2643#else
2644 _NOEXCEPT
2645#endif
Howard Hinnant76053d72013-06-27 19:35:32 +00002646 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002647 __size_(0),
2648 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2649{
2650}
2651
2652template <class _Allocator>
2653vector<bool, _Allocator>::vector(size_type __n)
Howard Hinnant76053d72013-06-27 19:35:32 +00002654 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002655 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002656 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002657{
2658 if (__n > 0)
2659 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002660 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002661 __construct_at_end(__n, false);
2662 }
2663}
2664
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00002665#if _LIBCPP_STD_VER > 11
2666template <class _Allocator>
2667vector<bool, _Allocator>::vector(size_type __n, const allocator_type& __a)
2668 : __begin_(nullptr),
2669 __size_(0),
2670 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2671{
2672 if (__n > 0)
2673 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002674 __vallocate(__n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00002675 __construct_at_end(__n, false);
2676 }
2677}
2678#endif
2679
Howard Hinnantc51e1022010-05-11 19:42:16 +00002680template <class _Allocator>
2681vector<bool, _Allocator>::vector(size_type __n, const value_type& __x)
Howard Hinnant76053d72013-06-27 19:35:32 +00002682 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002683 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002684 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002685{
2686 if (__n > 0)
2687 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002688 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002689 __construct_at_end(__n, __x);
2690 }
2691}
2692
2693template <class _Allocator>
2694vector<bool, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002695 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002696 __size_(0),
2697 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2698{
2699 if (__n > 0)
2700 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002701 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002702 __construct_at_end(__n, __x);
2703 }
2704}
2705
2706template <class _Allocator>
2707template <class _InputIterator>
2708vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002709 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
2710 !__is_cpp17_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002711 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002712 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002713 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002714{
2715#ifndef _LIBCPP_NO_EXCEPTIONS
2716 try
2717 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002718#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002719 for (; __first != __last; ++__first)
2720 push_back(*__first);
2721#ifndef _LIBCPP_NO_EXCEPTIONS
2722 }
2723 catch (...)
2724 {
Howard Hinnant76053d72013-06-27 19:35:32 +00002725 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002726 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2727 __invalidate_all_iterators();
2728 throw;
2729 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002730#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002731}
2732
2733template <class _Allocator>
2734template <class _InputIterator>
2735vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002736 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
2737 !__is_cpp17_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002738 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002739 __size_(0),
2740 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2741{
2742#ifndef _LIBCPP_NO_EXCEPTIONS
2743 try
2744 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002745#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002746 for (; __first != __last; ++__first)
2747 push_back(*__first);
2748#ifndef _LIBCPP_NO_EXCEPTIONS
2749 }
2750 catch (...)
2751 {
Howard Hinnant76053d72013-06-27 19:35:32 +00002752 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002753 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2754 __invalidate_all_iterators();
2755 throw;
2756 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002757#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002758}
2759
2760template <class _Allocator>
2761template <class _ForwardIterator>
2762vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002763 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002764 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002765 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002766 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002767{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002768 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002769 if (__n > 0)
2770 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002771 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002772 __construct_at_end(__first, __last);
2773 }
2774}
2775
2776template <class _Allocator>
2777template <class _ForwardIterator>
2778vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002779 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002780 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002781 __size_(0),
2782 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2783{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002784 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002785 if (__n > 0)
2786 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002787 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002788 __construct_at_end(__first, __last);
2789 }
2790}
2791
Eric Fiseliered9e9362017-04-16 02:40:45 +00002792#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002793
Howard Hinnantc51e1022010-05-11 19:42:16 +00002794template <class _Allocator>
2795vector<bool, _Allocator>::vector(initializer_list<value_type> __il)
Howard Hinnant76053d72013-06-27 19:35:32 +00002796 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002797 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002798 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002799{
2800 size_type __n = static_cast<size_type>(__il.size());
2801 if (__n > 0)
2802 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002803 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002804 __construct_at_end(__il.begin(), __il.end());
2805 }
2806}
2807
2808template <class _Allocator>
2809vector<bool, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002810 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002811 __size_(0),
2812 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2813{
2814 size_type __n = static_cast<size_type>(__il.size());
2815 if (__n > 0)
2816 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002817 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002818 __construct_at_end(__il.begin(), __il.end());
2819 }
2820}
2821
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002822#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002823
Howard Hinnantc51e1022010-05-11 19:42:16 +00002824template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002825vector<bool, _Allocator>::~vector()
2826{
Howard Hinnant76053d72013-06-27 19:35:32 +00002827 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002828 __storage_traits::deallocate(__alloc(), __begin_, __cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002829 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002830}
2831
2832template <class _Allocator>
2833vector<bool, _Allocator>::vector(const vector& __v)
Howard Hinnant76053d72013-06-27 19:35:32 +00002834 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002835 __size_(0),
2836 __cap_alloc_(0, __storage_traits::select_on_container_copy_construction(__v.__alloc()))
2837{
2838 if (__v.size() > 0)
2839 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002840 __vallocate(__v.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002841 __construct_at_end(__v.begin(), __v.end());
2842 }
2843}
2844
2845template <class _Allocator>
2846vector<bool, _Allocator>::vector(const vector& __v, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002847 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002848 __size_(0),
2849 __cap_alloc_(0, __a)
2850{
2851 if (__v.size() > 0)
2852 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002853 __vallocate(__v.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002854 __construct_at_end(__v.begin(), __v.end());
2855 }
2856}
2857
2858template <class _Allocator>
2859vector<bool, _Allocator>&
2860vector<bool, _Allocator>::operator=(const vector& __v)
2861{
Mark de Wever357a1fc2021-09-28 19:15:18 +02002862 if (this != _VSTD::addressof(__v))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002863 {
2864 __copy_assign_alloc(__v);
2865 if (__v.__size_)
2866 {
2867 if (__v.__size_ > capacity())
2868 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002869 __vdeallocate();
2870 __vallocate(__v.__size_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002871 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002872 _VSTD::copy(__v.__begin_, __v.__begin_ + __external_cap_to_internal(__v.__size_), __begin_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002873 }
2874 __size_ = __v.__size_;
2875 }
2876 return *this;
2877}
2878
Eric Fiseliered9e9362017-04-16 02:40:45 +00002879#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant74279a52010-09-04 23:28:19 +00002880
Howard Hinnantc51e1022010-05-11 19:42:16 +00002881template <class _Allocator>
Eric Fiselier4f1534c2018-06-05 22:32:52 +00002882inline _LIBCPP_INLINE_VISIBILITY vector<bool, _Allocator>::vector(vector&& __v)
Marshall Clowe5108202015-07-14 14:46:32 +00002883#if _LIBCPP_STD_VER > 14
Eric Fiselier4f1534c2018-06-05 22:32:52 +00002884 _NOEXCEPT
Marshall Clowe5108202015-07-14 14:46:32 +00002885#else
Eric Fiselier4f1534c2018-06-05 22:32:52 +00002886 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowe5108202015-07-14 14:46:32 +00002887#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002888 : __begin_(__v.__begin_),
2889 __size_(__v.__size_),
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05002890 __cap_alloc_(_VSTD::move(__v.__cap_alloc_)) {
Howard Hinnant76053d72013-06-27 19:35:32 +00002891 __v.__begin_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002892 __v.__size_ = 0;
2893 __v.__cap() = 0;
2894}
2895
2896template <class _Allocator>
Arthur O'Dwyer9b9662f2021-03-01 17:08:24 -05002897vector<bool, _Allocator>::vector(vector&& __v, const __identity_t<allocator_type>& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002898 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002899 __size_(0),
2900 __cap_alloc_(0, __a)
2901{
2902 if (__a == allocator_type(__v.__alloc()))
2903 {
2904 this->__begin_ = __v.__begin_;
2905 this->__size_ = __v.__size_;
2906 this->__cap() = __v.__cap();
2907 __v.__begin_ = nullptr;
2908 __v.__cap() = __v.__size_ = 0;
2909 }
2910 else if (__v.size() > 0)
2911 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002912 __vallocate(__v.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002913 __construct_at_end(__v.begin(), __v.end());
2914 }
2915}
2916
2917template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002918inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002919vector<bool, _Allocator>&
2920vector<bool, _Allocator>::operator=(vector&& __v)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002921 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002922{
2923 __move_assign(__v, integral_constant<bool,
2924 __storage_traits::propagate_on_container_move_assignment::value>());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002925 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002926}
2927
2928template <class _Allocator>
2929void
2930vector<bool, _Allocator>::__move_assign(vector& __c, false_type)
2931{
2932 if (__alloc() != __c.__alloc())
2933 assign(__c.begin(), __c.end());
2934 else
2935 __move_assign(__c, true_type());
2936}
2937
2938template <class _Allocator>
2939void
2940vector<bool, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002941 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002942{
Marshall Clow3ff48e02018-05-22 16:20:28 +00002943 __vdeallocate();
Marshall Clowb4871fa2014-07-21 15:15:15 +00002944 __move_assign_alloc(__c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002945 this->__begin_ = __c.__begin_;
2946 this->__size_ = __c.__size_;
2947 this->__cap() = __c.__cap();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002948 __c.__begin_ = nullptr;
2949 __c.__cap() = __c.__size_ = 0;
2950}
Howard Hinnant74279a52010-09-04 23:28:19 +00002951
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002952#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002953
2954template <class _Allocator>
2955void
2956vector<bool, _Allocator>::assign(size_type __n, const value_type& __x)
2957{
2958 __size_ = 0;
2959 if (__n > 0)
2960 {
2961 size_type __c = capacity();
2962 if (__n <= __c)
2963 __size_ = __n;
2964 else
2965 {
2966 vector __v(__alloc());
2967 __v.reserve(__recommend(__n));
2968 __v.__size_ = __n;
2969 swap(__v);
2970 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002971 _VSTD::fill_n(begin(), __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002972 }
Eric Fiselier69c51982016-12-28 06:06:09 +00002973 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002974}
2975
2976template <class _Allocator>
2977template <class _InputIterator>
2978typename enable_if
2979<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002980 __is_cpp17_input_iterator<_InputIterator>::value &&
2981 !__is_cpp17_forward_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002982 void
2983>::type
2984vector<bool, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2985{
2986 clear();
2987 for (; __first != __last; ++__first)
2988 push_back(*__first);
2989}
2990
2991template <class _Allocator>
2992template <class _ForwardIterator>
2993typename enable_if
2994<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002995 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002996 void
2997>::type
2998vector<bool, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2999{
3000 clear();
Eric Fiselier6003c772016-12-23 23:37:52 +00003001 difference_type __ns = _VSTD::distance(__first, __last);
3002 _LIBCPP_ASSERT(__ns >= 0, "invalid range specified");
3003 const size_t __n = static_cast<size_type>(__ns);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003004 if (__n)
3005 {
3006 if (__n > capacity())
3007 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00003008 __vdeallocate();
3009 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003010 }
3011 __construct_at_end(__first, __last);
3012 }
3013}
3014
3015template <class _Allocator>
3016void
3017vector<bool, _Allocator>::reserve(size_type __n)
3018{
3019 if (__n > capacity())
3020 {
3021 vector __v(this->__alloc());
Marshall Clow3ff48e02018-05-22 16:20:28 +00003022 __v.__vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003023 __v.__construct_at_end(this->begin(), this->end());
3024 swap(__v);
3025 __invalidate_all_iterators();
3026 }
3027}
3028
3029template <class _Allocator>
3030void
Howard Hinnant1c936782011-06-03 19:40:40 +00003031vector<bool, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003032{
3033 if (__external_cap_to_internal(size()) > __cap())
3034 {
3035#ifndef _LIBCPP_NO_EXCEPTIONS
3036 try
3037 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003038#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003039 vector(*this, allocator_type(__alloc())).swap(*this);
3040#ifndef _LIBCPP_NO_EXCEPTIONS
3041 }
3042 catch (...)
3043 {
3044 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003045#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003046 }
3047}
3048
3049template <class _Allocator>
3050typename vector<bool, _Allocator>::reference
3051vector<bool, _Allocator>::at(size_type __n)
3052{
3053 if (__n >= size())
3054 this->__throw_out_of_range();
3055 return (*this)[__n];
3056}
3057
3058template <class _Allocator>
3059typename vector<bool, _Allocator>::const_reference
3060vector<bool, _Allocator>::at(size_type __n) const
3061{
3062 if (__n >= size())
3063 this->__throw_out_of_range();
3064 return (*this)[__n];
3065}
3066
3067template <class _Allocator>
3068void
3069vector<bool, _Allocator>::push_back(const value_type& __x)
3070{
3071 if (this->__size_ == this->capacity())
3072 reserve(__recommend(this->__size_ + 1));
3073 ++this->__size_;
3074 back() = __x;
3075}
3076
3077template <class _Allocator>
3078typename vector<bool, _Allocator>::iterator
3079vector<bool, _Allocator>::insert(const_iterator __position, const value_type& __x)
3080{
3081 iterator __r;
3082 if (size() < capacity())
3083 {
3084 const_iterator __old_end = end();
3085 ++__size_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003086 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003087 __r = __const_iterator_cast(__position);
3088 }
3089 else
3090 {
3091 vector __v(__alloc());
3092 __v.reserve(__recommend(__size_ + 1));
3093 __v.__size_ = __size_ + 1;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003094 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3095 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003096 swap(__v);
3097 }
3098 *__r = __x;
3099 return __r;
3100}
3101
3102template <class _Allocator>
3103typename vector<bool, _Allocator>::iterator
3104vector<bool, _Allocator>::insert(const_iterator __position, size_type __n, const value_type& __x)
3105{
3106 iterator __r;
3107 size_type __c = capacity();
3108 if (__n <= __c && size() <= __c - __n)
3109 {
3110 const_iterator __old_end = end();
3111 __size_ += __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003112 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003113 __r = __const_iterator_cast(__position);
3114 }
3115 else
3116 {
3117 vector __v(__alloc());
3118 __v.reserve(__recommend(__size_ + __n));
3119 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003120 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3121 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003122 swap(__v);
3123 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003124 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003125 return __r;
3126}
3127
3128template <class _Allocator>
3129template <class _InputIterator>
3130typename enable_if
3131<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05003132 __is_cpp17_input_iterator <_InputIterator>::value &&
3133 !__is_cpp17_forward_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003134 typename vector<bool, _Allocator>::iterator
3135>::type
3136vector<bool, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
3137{
3138 difference_type __off = __position - begin();
3139 iterator __p = __const_iterator_cast(__position);
3140 iterator __old_end = end();
3141 for (; size() != capacity() && __first != __last; ++__first)
3142 {
3143 ++this->__size_;
3144 back() = *__first;
3145 }
3146 vector __v(__alloc());
3147 if (__first != __last)
3148 {
3149#ifndef _LIBCPP_NO_EXCEPTIONS
3150 try
3151 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003152#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003153 __v.assign(__first, __last);
3154 difference_type __old_size = static_cast<difference_type>(__old_end - begin());
3155 difference_type __old_p = __p - begin();
3156 reserve(__recommend(size() + __v.size()));
3157 __p = begin() + __old_p;
3158 __old_end = begin() + __old_size;
3159#ifndef _LIBCPP_NO_EXCEPTIONS
3160 }
3161 catch (...)
3162 {
3163 erase(__old_end, end());
3164 throw;
3165 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003166#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003167 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003168 __p = _VSTD::rotate(__p, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003169 insert(__p, __v.begin(), __v.end());
3170 return begin() + __off;
3171}
3172
3173template <class _Allocator>
3174template <class _ForwardIterator>
3175typename enable_if
3176<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05003177 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003178 typename vector<bool, _Allocator>::iterator
3179>::type
3180vector<bool, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
3181{
Eric Fiselier654dd332016-12-11 05:31:00 +00003182 const difference_type __n_signed = _VSTD::distance(__first, __last);
3183 _LIBCPP_ASSERT(__n_signed >= 0, "invalid range specified");
3184 const size_type __n = static_cast<size_type>(__n_signed);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003185 iterator __r;
3186 size_type __c = capacity();
3187 if (__n <= __c && size() <= __c - __n)
3188 {
3189 const_iterator __old_end = end();
3190 __size_ += __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003191 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003192 __r = __const_iterator_cast(__position);
3193 }
3194 else
3195 {
3196 vector __v(__alloc());
3197 __v.reserve(__recommend(__size_ + __n));
3198 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003199 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3200 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003201 swap(__v);
3202 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003203 _VSTD::copy(__first, __last, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003204 return __r;
3205}
3206
3207template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003208inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003209typename vector<bool, _Allocator>::iterator
3210vector<bool, _Allocator>::erase(const_iterator __position)
3211{
3212 iterator __r = __const_iterator_cast(__position);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003213 _VSTD::copy(__position + 1, this->cend(), __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003214 --__size_;
3215 return __r;
3216}
3217
3218template <class _Allocator>
3219typename vector<bool, _Allocator>::iterator
3220vector<bool, _Allocator>::erase(const_iterator __first, const_iterator __last)
3221{
3222 iterator __r = __const_iterator_cast(__first);
3223 difference_type __d = __last - __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003224 _VSTD::copy(__last, this->cend(), __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003225 __size_ -= __d;
3226 return __r;
3227}
3228
3229template <class _Allocator>
3230void
3231vector<bool, _Allocator>::swap(vector& __x)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003232#if _LIBCPP_STD_VER >= 14
3233 _NOEXCEPT
3234#else
Eric Fiselier69c51982016-12-28 06:06:09 +00003235 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003236 __is_nothrow_swappable<allocator_type>::value)
3237#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003238{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003239 _VSTD::swap(this->__begin_, __x.__begin_);
3240 _VSTD::swap(this->__size_, __x.__size_);
3241 _VSTD::swap(this->__cap(), __x.__cap());
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05003242 _VSTD::__swap_allocator(this->__alloc(), __x.__alloc(),
Marshall Clow8982dcd2015-07-13 20:04:56 +00003243 integral_constant<bool, __alloc_traits::propagate_on_container_swap::value>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003244}
3245
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003246template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003247void
3248vector<bool, _Allocator>::resize(size_type __sz, value_type __x)
3249{
3250 size_type __cs = size();
3251 if (__cs < __sz)
3252 {
3253 iterator __r;
3254 size_type __c = capacity();
3255 size_type __n = __sz - __cs;
3256 if (__n <= __c && __cs <= __c - __n)
3257 {
3258 __r = end();
3259 __size_ += __n;
3260 }
3261 else
3262 {
3263 vector __v(__alloc());
3264 __v.reserve(__recommend(__size_ + __n));
3265 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003266 __r = _VSTD::copy(cbegin(), cend(), __v.begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003267 swap(__v);
3268 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003269 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003270 }
3271 else
3272 __size_ = __sz;
3273}
3274
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003275template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003276void
Howard Hinnant1c936782011-06-03 19:40:40 +00003277vector<bool, _Allocator>::flip() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003278{
3279 // do middle whole words
3280 size_type __n = __size_;
3281 __storage_pointer __p = __begin_;
3282 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3283 *__p = ~*__p;
3284 // do last partial word
3285 if (__n > 0)
3286 {
3287 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3288 __storage_type __b = *__p & __m;
3289 *__p &= ~__m;
3290 *__p |= ~__b & __m;
3291 }
3292}
3293
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003294template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003295bool
3296vector<bool, _Allocator>::__invariants() const
3297{
Howard Hinnant76053d72013-06-27 19:35:32 +00003298 if (this->__begin_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003299 {
3300 if (this->__size_ != 0 || this->__cap() != 0)
3301 return false;
3302 }
3303 else
3304 {
3305 if (this->__cap() == 0)
3306 return false;
3307 if (this->__size_ > this->capacity())
3308 return false;
3309 }
3310 return true;
3311}
3312
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003313template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003314size_t
Howard Hinnant1c936782011-06-03 19:40:40 +00003315vector<bool, _Allocator>::__hash_code() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003316{
3317 size_t __h = 0;
3318 // do middle whole words
3319 size_type __n = __size_;
3320 __storage_pointer __p = __begin_;
3321 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3322 __h ^= *__p;
3323 // do last partial word
3324 if (__n > 0)
3325 {
3326 const __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3327 __h ^= *__p & __m;
3328 }
3329 return __h;
3330}
3331
3332template <class _Allocator>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003333struct _LIBCPP_TEMPLATE_VIS hash<vector<bool, _Allocator> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00003334 : public unary_function<vector<bool, _Allocator>, size_t>
3335{
Howard Hinnant1c265cd2010-09-23 18:58:28 +00003336 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +00003337 size_t operator()(const vector<bool, _Allocator>& __vec) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003338 {return __vec.__hash_code();}
3339};
3340
3341template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003342inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003343bool
3344operator==(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3345{
3346 const typename vector<_Tp, _Allocator>::size_type __sz = __x.size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003347 return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003348}
3349
3350template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003351inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003352bool
3353operator!=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3354{
3355 return !(__x == __y);
3356}
3357
3358template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003359inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003360bool
3361operator< (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3362{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003363 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003364}
3365
3366template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003367inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003368bool
3369operator> (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3370{
3371 return __y < __x;
3372}
3373
3374template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003375inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003376bool
3377operator>=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3378{
3379 return !(__x < __y);
3380}
3381
3382template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003383inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003384bool
3385operator<=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3386{
3387 return !(__y < __x);
3388}
3389
3390template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003391inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003392void
3393swap(vector<_Tp, _Allocator>& __x, vector<_Tp, _Allocator>& __y)
Howard Hinnant1c936782011-06-03 19:40:40 +00003394 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00003395{
3396 __x.swap(__y);
3397}
3398
Marshall Clow29b53f22018-12-14 18:49:35 +00003399#if _LIBCPP_STD_VER > 17
3400template <class _Tp, class _Allocator, class _Up>
Marek Kurdeja98b1412020-05-02 13:58:03 +02003401inline _LIBCPP_INLINE_VISIBILITY typename vector<_Tp, _Allocator>::size_type
3402erase(vector<_Tp, _Allocator>& __c, const _Up& __v) {
3403 auto __old_size = __c.size();
3404 __c.erase(_VSTD::remove(__c.begin(), __c.end(), __v), __c.end());
3405 return __old_size - __c.size();
3406}
Marshall Clow29b53f22018-12-14 18:49:35 +00003407
3408template <class _Tp, class _Allocator, class _Predicate>
Marek Kurdeja98b1412020-05-02 13:58:03 +02003409inline _LIBCPP_INLINE_VISIBILITY typename vector<_Tp, _Allocator>::size_type
3410erase_if(vector<_Tp, _Allocator>& __c, _Predicate __pred) {
3411 auto __old_size = __c.size();
3412 __c.erase(_VSTD::remove_if(__c.begin(), __c.end(), __pred), __c.end());
3413 return __old_size - __c.size();
3414}
Marshall Clow29b53f22018-12-14 18:49:35 +00003415#endif
3416
Howard Hinnantc51e1022010-05-11 19:42:16 +00003417_LIBCPP_END_NAMESPACE_STD
3418
Eric Fiselierf4433a32017-05-31 22:07:49 +00003419_LIBCPP_POP_MACROS
3420
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003421#endif // _LIBCPP_VECTOR