blob: 8c65e7f9bec84d38a6c9131c5ae589c76076cacd [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
Louis Dionne9bd93882021-11-17 16:25:01 -05002//===----------------------------------------------------------------------===//
Howard Hinnantc51e1022010-05-11 19:42:16 +00003//
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())
Konstantin Varlamov53b543c2021-11-09 09:21:02 -0800248 -> vector<typename iterator_traits<InputIterator>::value_type, Allocator>; // C++17
Marshall Clowf0ca1492018-05-21 21:30:12 +0000249
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
Howard Hinnantc51e1022010-05-11 19:42:16 +0000274#include <__bit_reference>
Arthur O'Dwyer65077c02022-01-07 09:45:05 -0500275#include <__config>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400276#include <__debug>
277#include <__functional_base>
Konstantin Varlamov53b543c2021-11-09 09:21:02 -0800278#include <__iterator/iterator_traits.h>
Louis Dionne77249522021-06-11 09:55:11 -0400279#include <__iterator/wrap_iter.h>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400280#include <__split_buffer>
Christopher Di Bella41f26e82021-06-05 02:47:47 +0000281#include <__utility/forward.h>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400282#include <algorithm>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000283#include <climits>
Arthur O'Dwyer7deec122021-03-24 18:19:12 -0400284#include <compare>
Louis Dionned24191c2021-08-19 12:39:16 -0400285#include <cstdlib>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400286#include <cstring>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000287#include <initializer_list>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400288#include <iosfwd> // for forward declaration of vector
289#include <limits>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000290#include <memory>
291#include <stdexcept>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400292#include <type_traits>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000293#include <version>
Howard Hinnante6ff0b62013-08-02 00:26:35 +0000294
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000295#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000296#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000297#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000298
Eric Fiselierf4433a32017-05-31 22:07:49 +0000299_LIBCPP_PUSH_MACROS
300#include <__undef_macros>
301
302
Howard Hinnantc51e1022010-05-11 19:42:16 +0000303_LIBCPP_BEGIN_NAMESPACE_STD
304
305template <bool>
Louis Dionneb6aabd92021-08-19 12:21:06 -0400306struct __vector_base_common;
307
308template <>
309struct __vector_base_common<true> {
310 // Both are defined in vector.cpp
311 _LIBCPP_NORETURN _LIBCPP_EXPORTED_FROM_ABI void __throw_length_error() const;
312 _LIBCPP_NORETURN _LIBCPP_EXPORTED_FROM_ABI void __throw_out_of_range() const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000313};
314
Howard Hinnantc51e1022010-05-11 19:42:16 +0000315template <class _Tp, class _Allocator>
316class __vector_base
Louis Dionneb6aabd92021-08-19 12:21:06 -0400317 : 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 +0000318{
Konstantin Varlamov5f23d662021-11-08 00:10:13 -0800319 typedef _Allocator allocator_type;
320 typedef typename allocator_traits<allocator_type>::pointer pointer;
321
Marshall Clowf0ca1492018-05-21 21:30:12 +0000322protected:
Konstantin Varlamov5f23d662021-11-08 00:10:13 -0800323 pointer __begin_;
324 pointer __end_;
325 __compressed_pair<pointer, allocator_type> __end_cap_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000326
Howard Hinnant1c936782011-06-03 19:40:40 +0000327 _LIBCPP_INLINE_VISIBILITY
328 __vector_base()
Howard Hinnant1c936782011-06-03 19:40:40 +0000329 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Konstantin Varlamov5f23d662021-11-08 00:10:13 -0800330 : __begin_(nullptr),
331 __end_(nullptr),
332 __end_cap_(nullptr, __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000333
Konstantin Varlamov5f23d662021-11-08 00:10:13 -0800334 _LIBCPP_INLINE_VISIBILITY __vector_base(const allocator_type& __a)
335 : __begin_(nullptr),
336 __end_(nullptr),
337 __end_cap_(nullptr, __a) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000338
Eric Fiselier4f1534c2018-06-05 22:32:52 +0000339#ifndef _LIBCPP_CXX03_LANG
Konstantin Varlamov5f23d662021-11-08 00:10:13 -0800340 _LIBCPP_INLINE_VISIBILITY __vector_base(allocator_type&& __a) _NOEXCEPT
341 : __begin_(nullptr),
342 __end_(nullptr),
343 __end_cap_(nullptr, _VSTD::move(__a)) {}
Eric Fiselier4f1534c2018-06-05 22:32:52 +0000344#endif
Konstantin Varlamov5f23d662021-11-08 00:10:13 -0800345};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000346
Eric Fiselier876c6862016-02-20 00:19:45 +0000347template <class _Tp, class _Allocator /* = allocator<_Tp> */>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000348class _LIBCPP_TEMPLATE_VIS vector
Konstantin Varlamov5f23d662021-11-08 00:10:13 -0800349 // This base class is historical, but it needs to remain for ABI compatibility.
Howard Hinnantc51e1022010-05-11 19:42:16 +0000350 : private __vector_base<_Tp, _Allocator>
351{
352private:
Konstantin Varlamovf2872972021-12-01 11:55:46 -0800353 typedef __vector_base<_Tp, _Allocator> __base;
354 typedef allocator<_Tp> __default_allocator_type;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000355public:
Konstantin Varlamovf2872972021-12-01 11:55:46 -0800356 typedef vector __self;
357 typedef _Tp value_type;
358 typedef _Allocator allocator_type;
359 typedef allocator_traits<allocator_type> __alloc_traits;
360 typedef value_type& reference;
361 typedef const value_type& const_reference;
362 typedef typename __alloc_traits::size_type size_type;
363 typedef typename __alloc_traits::difference_type difference_type;
364 typedef typename __alloc_traits::pointer pointer;
365 typedef typename __alloc_traits::const_pointer const_pointer;
366 typedef __wrap_iter<pointer> iterator;
367 typedef __wrap_iter<const_pointer> const_iterator;
368 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
369 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000370
Howard Hinnanta416ff02013-03-26 19:04:56 +0000371 static_assert((is_same<typename allocator_type::value_type, value_type>::value),
372 "Allocator::value_type must be same type as value_type");
373
Howard Hinnant1c936782011-06-03 19:40:40 +0000374 _LIBCPP_INLINE_VISIBILITY
Marshall Clowe546cbb2015-06-04 02:05:41 +0000375 vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Nikolas Klauserf2807732022-01-11 00:33:35 +0100376 {
377 _VSTD::__debug_db_insert_c(this);
378 }
Howard Hinnant27e0e772011-09-14 18:33:51 +0000379 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
Marshall Clow8f282f42015-06-04 00:10:20 +0000380#if _LIBCPP_STD_VER <= 14
381 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
382#else
383 _NOEXCEPT
384#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +0000385 : __base(__a)
386 {
Nikolas Klauserf2807732022-01-11 00:33:35 +0100387 _VSTD::__debug_db_insert_c(this);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000388 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000389 explicit vector(size_type __n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +0000390#if _LIBCPP_STD_VER > 11
391 explicit vector(size_type __n, const allocator_type& __a);
392#endif
Marshall Clowf0ca1492018-05-21 21:30:12 +0000393 vector(size_type __n, const value_type& __x);
Konstantin Varlamovf2872972021-12-01 11:55:46 -0800394
395 template <class = __enable_if_t<__is_allocator<_Allocator>::value> >
396 vector(size_type __n, const value_type& __x, const allocator_type& __a)
397 : __base(__a)
398 {
Nikolas Klauserf2807732022-01-11 00:33:35 +0100399 _VSTD::__debug_db_insert_c(this);
Konstantin Varlamovf2872972021-12-01 11:55:46 -0800400 if (__n > 0)
401 {
402 __vallocate(__n);
403 __construct_at_end(__n, __x);
404 }
405 }
406
Howard Hinnantc51e1022010-05-11 19:42:16 +0000407 template <class _InputIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +0000408 vector(_InputIterator __first,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500409 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
410 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000411 is_constructible<
412 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +0000413 typename iterator_traits<_InputIterator>::reference>::value,
414 _InputIterator>::type __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000415 template <class _InputIterator>
416 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500417 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
418 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000419 is_constructible<
420 value_type,
421 typename iterator_traits<_InputIterator>::reference>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000422 template <class _ForwardIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +0000423 vector(_ForwardIterator __first,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500424 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000425 is_constructible<
426 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +0000427 typename iterator_traits<_ForwardIterator>::reference>::value,
428 _ForwardIterator>::type __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000429 template <class _ForwardIterator>
430 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500431 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000432 is_constructible<
433 value_type,
434 typename iterator_traits<_ForwardIterator>::reference>::value>::type* = 0);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000435
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000436 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant27e0e772011-09-14 18:33:51 +0000437 ~vector()
438 {
Konstantin Varlamov5f23d662021-11-08 00:10:13 -0800439 __annotate_delete();
Louis Dionneba400782020-10-02 15:02:52 -0400440#if _LIBCPP_DEBUG_LEVEL == 2
Konstantin Varlamov5f23d662021-11-08 00:10:13 -0800441 __get_db()->__erase_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000442#endif
Konstantin Varlamov5f23d662021-11-08 00:10:13 -0800443
444 if (this->__begin_ != nullptr)
445 {
446 __clear();
447 __alloc_traits::deallocate(__alloc(), this->__begin_, capacity());
448 }
Marshall Clow68af4f32018-09-07 15:47:59 +0000449 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000450
451 vector(const vector& __x);
Arthur O'Dwyer9b9662f2021-03-01 17:08:24 -0500452 vector(const vector& __x, const __identity_t<allocator_type>& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000453 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000454 vector& operator=(const vector& __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000455
456#ifndef _LIBCPP_CXX03_LANG
457 _LIBCPP_INLINE_VISIBILITY
458 vector(initializer_list<value_type> __il);
459
460 _LIBCPP_INLINE_VISIBILITY
461 vector(initializer_list<value_type> __il, const allocator_type& __a);
462
Howard Hinnantcf823322010-12-17 14:46:43 +0000463 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000464 vector(vector&& __x)
Marshall Clowe5108202015-07-14 14:46:32 +0000465#if _LIBCPP_STD_VER > 14
466 _NOEXCEPT;
467#else
Howard Hinnant1c936782011-06-03 19:40:40 +0000468 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowe5108202015-07-14 14:46:32 +0000469#endif
Eric Fiseliered9e9362017-04-16 02:40:45 +0000470
Howard Hinnantcf823322010-12-17 14:46:43 +0000471 _LIBCPP_INLINE_VISIBILITY
Arthur O'Dwyer9b9662f2021-03-01 17:08:24 -0500472 vector(vector&& __x, const __identity_t<allocator_type>& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000473 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000474 vector& operator=(vector&& __x)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000475 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiseliered9e9362017-04-16 02:40:45 +0000476
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000477 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000478 vector& operator=(initializer_list<value_type> __il)
479 {assign(__il.begin(), __il.end()); return *this;}
Eric Fiseliered9e9362017-04-16 02:40:45 +0000480
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400481#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000482
483 template <class _InputIterator>
484 typename enable_if
485 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500486 __is_cpp17_input_iterator <_InputIterator>::value &&
487 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000488 is_constructible<
489 value_type,
490 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000491 void
492 >::type
493 assign(_InputIterator __first, _InputIterator __last);
494 template <class _ForwardIterator>
495 typename enable_if
496 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500497 __is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000498 is_constructible<
499 value_type,
500 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000501 void
502 >::type
503 assign(_ForwardIterator __first, _ForwardIterator __last);
504
505 void assign(size_type __n, const_reference __u);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000506
507#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000508 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000509 void assign(initializer_list<value_type> __il)
510 {assign(__il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +0000511#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000512
Howard Hinnant1c936782011-06-03 19:40:40 +0000513 _LIBCPP_INLINE_VISIBILITY
514 allocator_type get_allocator() const _NOEXCEPT
515 {return this->__alloc();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000516
Howard Hinnant1c936782011-06-03 19:40:40 +0000517 _LIBCPP_INLINE_VISIBILITY iterator begin() _NOEXCEPT;
518 _LIBCPP_INLINE_VISIBILITY const_iterator begin() const _NOEXCEPT;
519 _LIBCPP_INLINE_VISIBILITY iterator end() _NOEXCEPT;
520 _LIBCPP_INLINE_VISIBILITY const_iterator end() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000521
Howard Hinnant1c936782011-06-03 19:40:40 +0000522 _LIBCPP_INLINE_VISIBILITY
523 reverse_iterator rbegin() _NOEXCEPT
524 {return reverse_iterator(end());}
525 _LIBCPP_INLINE_VISIBILITY
526 const_reverse_iterator rbegin() const _NOEXCEPT
527 {return const_reverse_iterator(end());}
528 _LIBCPP_INLINE_VISIBILITY
529 reverse_iterator rend() _NOEXCEPT
530 {return reverse_iterator(begin());}
531 _LIBCPP_INLINE_VISIBILITY
532 const_reverse_iterator rend() const _NOEXCEPT
533 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000534
Howard Hinnant1c936782011-06-03 19:40:40 +0000535 _LIBCPP_INLINE_VISIBILITY
536 const_iterator cbegin() const _NOEXCEPT
537 {return begin();}
538 _LIBCPP_INLINE_VISIBILITY
539 const_iterator cend() const _NOEXCEPT
540 {return end();}
541 _LIBCPP_INLINE_VISIBILITY
542 const_reverse_iterator crbegin() const _NOEXCEPT
543 {return rbegin();}
544 _LIBCPP_INLINE_VISIBILITY
545 const_reverse_iterator crend() const _NOEXCEPT
546 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000547
Howard Hinnant1c936782011-06-03 19:40:40 +0000548 _LIBCPP_INLINE_VISIBILITY
549 size_type size() const _NOEXCEPT
550 {return static_cast<size_type>(this->__end_ - this->__begin_);}
551 _LIBCPP_INLINE_VISIBILITY
552 size_type capacity() const _NOEXCEPT
Konstantin Varlamov5f23d662021-11-08 00:10:13 -0800553 {return static_cast<size_type>(__end_cap() - this->__begin_);}
Marshall Clow425f5752017-11-15 05:51:26 +0000554 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000555 bool empty() const _NOEXCEPT
556 {return this->__begin_ == this->__end_;}
557 size_type max_size() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000558 void reserve(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +0000559 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000560
Marshall Clowd6470492019-03-15 00:29:35 +0000561 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) _NOEXCEPT;
562 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000563 reference at(size_type __n);
564 const_reference at(size_type __n) const;
565
Marshall Clowd6470492019-03-15 00:29:35 +0000566 _LIBCPP_INLINE_VISIBILITY reference front() _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000567 {
Kristina Bessonovaaeeaa7e2021-05-09 19:29:56 +0200568 _LIBCPP_ASSERT(!empty(), "front() called on an empty vector");
Howard Hinnant27e0e772011-09-14 18:33:51 +0000569 return *this->__begin_;
570 }
Marshall Clowd6470492019-03-15 00:29:35 +0000571 _LIBCPP_INLINE_VISIBILITY const_reference front() const _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000572 {
Kristina Bessonovaaeeaa7e2021-05-09 19:29:56 +0200573 _LIBCPP_ASSERT(!empty(), "front() called on an empty vector");
Howard Hinnant27e0e772011-09-14 18:33:51 +0000574 return *this->__begin_;
575 }
Marshall Clowd6470492019-03-15 00:29:35 +0000576 _LIBCPP_INLINE_VISIBILITY reference back() _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000577 {
Kristina Bessonovaaeeaa7e2021-05-09 19:29:56 +0200578 _LIBCPP_ASSERT(!empty(), "back() called on an empty vector");
Howard Hinnant27e0e772011-09-14 18:33:51 +0000579 return *(this->__end_ - 1);
580 }
Marshall Clowd6470492019-03-15 00:29:35 +0000581 _LIBCPP_INLINE_VISIBILITY const_reference back() const _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000582 {
Kristina Bessonovaaeeaa7e2021-05-09 19:29:56 +0200583 _LIBCPP_ASSERT(!empty(), "back() called on an empty vector");
Howard Hinnant27e0e772011-09-14 18:33:51 +0000584 return *(this->__end_ - 1);
585 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000586
Howard Hinnant1c936782011-06-03 19:40:40 +0000587 _LIBCPP_INLINE_VISIBILITY
588 value_type* data() _NOEXCEPT
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500589 {return _VSTD::__to_address(this->__begin_);}
Howard Hinnant1c936782011-06-03 19:40:40 +0000590 _LIBCPP_INLINE_VISIBILITY
591 const value_type* data() const _NOEXCEPT
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500592 {return _VSTD::__to_address(this->__begin_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000593
Eric Fiselier96919722017-10-17 13:03:17 +0000594#ifdef _LIBCPP_CXX03_LANG
595 _LIBCPP_INLINE_VISIBILITY
596 void __emplace_back(const value_type& __x) { push_back(__x); }
597#else
598 template <class _Arg>
599 _LIBCPP_INLINE_VISIBILITY
600 void __emplace_back(_Arg&& __arg) {
601 emplace_back(_VSTD::forward<_Arg>(__arg));
602 }
603#endif
604
Howard Hinnantcf823322010-12-17 14:46:43 +0000605 _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000606
607#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000608 _LIBCPP_INLINE_VISIBILITY void push_back(value_type&& __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000609
Howard Hinnantc51e1022010-05-11 19:42:16 +0000610 template <class... _Args>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000611 _LIBCPP_INLINE_VISIBILITY
Marshall Clowea52cc42017-01-24 23:09:12 +0000612#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +0000613 reference emplace_back(_Args&&... __args);
Marshall Clowea52cc42017-01-24 23:09:12 +0000614#else
615 void emplace_back(_Args&&... __args);
616#endif
Eric Fiseliered9e9362017-04-16 02:40:45 +0000617#endif // !_LIBCPP_CXX03_LANG
618
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000619 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000620 void pop_back();
621
622 iterator insert(const_iterator __position, const_reference __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000623
624#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000625 iterator insert(const_iterator __position, value_type&& __x);
626 template <class... _Args>
627 iterator emplace(const_iterator __position, _Args&&... __args);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400628#endif // !_LIBCPP_CXX03_LANG
Eric Fiseliered9e9362017-04-16 02:40:45 +0000629
Howard Hinnantc51e1022010-05-11 19:42:16 +0000630 iterator insert(const_iterator __position, size_type __n, const_reference __x);
631 template <class _InputIterator>
632 typename enable_if
633 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500634 __is_cpp17_input_iterator <_InputIterator>::value &&
635 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000636 is_constructible<
637 value_type,
638 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000639 iterator
640 >::type
641 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
642 template <class _ForwardIterator>
643 typename enable_if
644 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500645 __is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000646 is_constructible<
647 value_type,
648 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000649 iterator
650 >::type
651 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000652
653#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000654 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000655 iterator insert(const_iterator __position, initializer_list<value_type> __il)
656 {return insert(__position, __il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +0000657#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000658
Howard Hinnantcf823322010-12-17 14:46:43 +0000659 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000660 iterator erase(const_iterator __first, const_iterator __last);
661
Howard Hinnant1c936782011-06-03 19:40:40 +0000662 _LIBCPP_INLINE_VISIBILITY
663 void clear() _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000664 {
Marshall Clow2cd9d372014-05-08 14:14:06 +0000665 size_type __old_size = size();
Konstantin Varlamov5f23d662021-11-08 00:10:13 -0800666 __clear();
Marshall Clow2cd9d372014-05-08 14:14:06 +0000667 __annotate_shrink(__old_size);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000668 __invalidate_all_iterators();
669 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000670
671 void resize(size_type __sz);
672 void resize(size_type __sz, const_reference __x);
673
Howard Hinnant1c936782011-06-03 19:40:40 +0000674 void swap(vector&)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000675#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +0000676 _NOEXCEPT;
Marshall Clow8982dcd2015-07-13 20:04:56 +0000677#else
Eric Fiselier873b8d32019-03-18 21:50:12 +0000678 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +0000679 __is_nothrow_swappable<allocator_type>::value);
680#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000681
682 bool __invariants() const;
683
Louis Dionneba400782020-10-02 15:02:52 -0400684#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000685
686 bool __dereferenceable(const const_iterator* __i) const;
687 bool __decrementable(const const_iterator* __i) const;
688 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
689 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
690
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400691#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000692
Howard Hinnantc51e1022010-05-11 19:42:16 +0000693private:
Howard Hinnantcf823322010-12-17 14:46:43 +0000694 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Eric Fiselier69c51982016-12-28 06:06:09 +0000695 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(pointer __new_last);
Marshall Clow3ff48e02018-05-22 16:20:28 +0000696 void __vallocate(size_type __n);
697 void __vdeallocate() _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000698 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
Howard Hinnanta5589382011-01-04 19:53:31 +0000699 void __construct_at_end(size_type __n);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000700 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000701 void __construct_at_end(size_type __n, const_reference __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000702 template <class _ForwardIterator>
703 typename enable_if
704 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500705 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000706 void
707 >::type
Eric Fiselieracfe6f02015-03-31 16:54:19 +0000708 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000709 void __append(size_type __n);
710 void __append(size_type __n, const_reference __x);
Howard Hinnantcf823322010-12-17 14:46:43 +0000711 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000712 iterator __make_iter(pointer __p) _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000713 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000714 const_iterator __make_iter(const_pointer __p) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000715 void __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v);
716 pointer __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p);
717 void __move_range(pointer __from_s, pointer __from_e, pointer __to);
Howard Hinnant1c936782011-06-03 19:40:40 +0000718 void __move_assign(vector& __c, true_type)
719 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000720 void __move_assign(vector& __c, false_type)
721 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000722 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76053d72013-06-27 19:35:32 +0000723 void __destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000724 {
Eric Fiselier69c51982016-12-28 06:06:09 +0000725 __invalidate_iterators_past(__new_last);
Marshall Clow2cd9d372014-05-08 14:14:06 +0000726 size_type __old_size = size();
Konstantin Varlamov5f23d662021-11-08 00:10:13 -0800727 __base_destruct_at_end(__new_last);
Marshall Clow2cd9d372014-05-08 14:14:06 +0000728 __annotate_shrink(__old_size);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000729 }
Eric Fiseliered9e9362017-04-16 02:40:45 +0000730
731#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier7110f442019-03-19 19:19:44 +0000732 template <class _Up>
733 _LIBCPP_INLINE_VISIBILITY
734 inline void __push_back_slow_path(_Up&& __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000735
Howard Hinnantb6c49562012-02-26 15:30:12 +0000736 template <class... _Args>
Eric Fiselier7110f442019-03-19 19:19:44 +0000737 _LIBCPP_INLINE_VISIBILITY
738 inline void __emplace_back_slow_path(_Args&&... __args);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000739#else
Eric Fiselier7110f442019-03-19 19:19:44 +0000740 template <class _Up>
741 _LIBCPP_INLINE_VISIBILITY
742 inline void __push_back_slow_path(_Up& __x);
Howard Hinnantb6c49562012-02-26 15:30:12 +0000743#endif
Eric Fiseliered9e9362017-04-16 02:40:45 +0000744
Marshall Clow2cd9d372014-05-08 14:14:06 +0000745 // The following functions are no-ops outside of AddressSanitizer mode.
746 // We call annotatations only for the default Allocator because other allocators
747 // may not meet the AddressSanitizer alignment constraints.
748 // See the documentation for __sanitizer_annotate_contiguous_container for more details.
Marshall Clow2cd9d372014-05-08 14:14:06 +0000749#ifndef _LIBCPP_HAS_NO_ASAN
Eric Fiselier6003c772016-12-23 23:37:52 +0000750 void __annotate_contiguous_container(const void *__beg, const void *__end,
751 const void *__old_mid,
752 const void *__new_mid) const
753 {
754
Marshall Clow2cd9d372014-05-08 14:14:06 +0000755 if (__beg && is_same<allocator_type, __default_allocator_type>::value)
756 __sanitizer_annotate_contiguous_container(__beg, __end, __old_mid, __new_mid);
Marshall Clow2cd9d372014-05-08 14:14:06 +0000757 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000758#else
759 _LIBCPP_INLINE_VISIBILITY
760 void __annotate_contiguous_container(const void*, const void*, const void*,
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000761 const void*) const _NOEXCEPT {}
Eric Fiselier6003c772016-12-23 23:37:52 +0000762#endif
763 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000764 void __annotate_new(size_type __current_size) const _NOEXCEPT {
Marshall Clow2cd9d372014-05-08 14:14:06 +0000765 __annotate_contiguous_container(data(), data() + capacity(),
766 data() + capacity(), data() + __current_size);
767 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000768
769 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000770 void __annotate_delete() const _NOEXCEPT {
Marshall Clow2cd9d372014-05-08 14:14:06 +0000771 __annotate_contiguous_container(data(), data() + capacity(),
772 data() + size(), data() + capacity());
773 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000774
775 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000776 void __annotate_increase(size_type __n) const _NOEXCEPT
Marshall Clow2cd9d372014-05-08 14:14:06 +0000777 {
778 __annotate_contiguous_container(data(), data() + capacity(),
779 data() + size(), data() + size() + __n);
780 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000781
782 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000783 void __annotate_shrink(size_type __old_size) const _NOEXCEPT
Marshall Clow2cd9d372014-05-08 14:14:06 +0000784 {
785 __annotate_contiguous_container(data(), data() + capacity(),
786 data() + __old_size, data() + size());
787 }
Marshall Clowc78ffa52014-09-03 21:37:43 +0000788
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000789 struct _ConstructTransaction {
790 explicit _ConstructTransaction(vector &__v, size_type __n)
Mark de Weverdcafc462021-10-21 18:29:14 +0200791 : __v_(__v), __pos_(__v.__end_), __new_end_(__v.__end_ + __n) {
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000792#ifndef _LIBCPP_HAS_NO_ASAN
793 __v_.__annotate_increase(__n);
794#endif
795 }
796 ~_ConstructTransaction() {
797 __v_.__end_ = __pos_;
798#ifndef _LIBCPP_HAS_NO_ASAN
799 if (__pos_ != __new_end_) {
800 __v_.__annotate_shrink(__new_end_ - __v_.__begin_);
801 }
802#endif
803 }
804
805 vector &__v_;
806 pointer __pos_;
807 const_pointer const __new_end_;
808
809 private:
810 _ConstructTransaction(_ConstructTransaction const&) = delete;
811 _ConstructTransaction& operator=(_ConstructTransaction const&) = delete;
812 };
813
814 template <class ..._Args>
815 _LIBCPP_INLINE_VISIBILITY
816 void __construct_one_at_end(_Args&& ...__args) {
817 _ConstructTransaction __tx(*this, 1);
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500818 __alloc_traits::construct(this->__alloc(), _VSTD::__to_address(__tx.__pos_),
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000819 _VSTD::forward<_Args>(__args)...);
820 ++__tx.__pos_;
821 }
Konstantin Varlamov5f23d662021-11-08 00:10:13 -0800822
823 _LIBCPP_INLINE_VISIBILITY
824 allocator_type& __alloc() _NOEXCEPT
825 {return this->__end_cap_.second();}
826 _LIBCPP_INLINE_VISIBILITY
827 const allocator_type& __alloc() const _NOEXCEPT
828 {return this->__end_cap_.second();}
829 _LIBCPP_INLINE_VISIBILITY
830 pointer& __end_cap() _NOEXCEPT
831 {return this->__end_cap_.first();}
832 _LIBCPP_INLINE_VISIBILITY
833 const pointer& __end_cap() const _NOEXCEPT
834 {return this->__end_cap_.first();}
835
836 _LIBCPP_INLINE_VISIBILITY
837 void __clear() _NOEXCEPT {__base_destruct_at_end(this->__begin_);}
838
839 _LIBCPP_INLINE_VISIBILITY
840 void __base_destruct_at_end(pointer __new_last) _NOEXCEPT {
841 pointer __soon_to_be_end = this->__end_;
842 while (__new_last != __soon_to_be_end)
843 __alloc_traits::destroy(__alloc(), _VSTD::__to_address(--__soon_to_be_end));
844 this->__end_ = __new_last;
845 }
846
847 _LIBCPP_INLINE_VISIBILITY
848 void __copy_assign_alloc(const vector& __c)
849 {__copy_assign_alloc(__c, integral_constant<bool,
850 __alloc_traits::propagate_on_container_copy_assignment::value>());}
851
852 _LIBCPP_INLINE_VISIBILITY
853 void __move_assign_alloc(vector& __c)
854 _NOEXCEPT_(
855 !__alloc_traits::propagate_on_container_move_assignment::value ||
856 is_nothrow_move_assignable<allocator_type>::value)
857 {__move_assign_alloc(__c, integral_constant<bool,
858 __alloc_traits::propagate_on_container_move_assignment::value>());}
859
860 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
861 void __throw_length_error() const {
862#ifndef _LIBCPP_NO_EXCEPTIONS
863 __vector_base_common<true>::__throw_length_error();
864#else
865 _VSTD::abort();
866#endif
867 }
868
869 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
870 void __throw_out_of_range() const {
871#ifndef _LIBCPP_NO_EXCEPTIONS
872 __vector_base_common<true>::__throw_out_of_range();
873#else
874 _VSTD::abort();
875#endif
876 }
877
878 _LIBCPP_INLINE_VISIBILITY
879 void __copy_assign_alloc(const vector& __c, true_type)
880 {
881 if (__alloc() != __c.__alloc())
882 {
883 __clear();
884 __alloc_traits::deallocate(__alloc(), this->__begin_, capacity());
885 this->__begin_ = this->__end_ = __end_cap() = nullptr;
886 }
887 __alloc() = __c.__alloc();
888 }
889
890 _LIBCPP_INLINE_VISIBILITY
891 void __copy_assign_alloc(const vector&, false_type)
892 {}
893
894 _LIBCPP_INLINE_VISIBILITY
895 void __move_assign_alloc(vector& __c, true_type)
896 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
897 {
898 __alloc() = _VSTD::move(__c.__alloc());
899 }
900
901 _LIBCPP_INLINE_VISIBILITY
902 void __move_assign_alloc(vector&, false_type)
903 _NOEXCEPT
904 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000905};
906
Louis Dionned59f8a52021-08-17 11:59:07 -0400907#if _LIBCPP_STD_VER >= 17
Marshall Clowf0ca1492018-05-21 21:30:12 +0000908template<class _InputIterator,
Arthur O'Dwyer56226762021-03-03 23:02:20 -0500909 class _Alloc = allocator<__iter_value_type<_InputIterator>>,
Konstantin Varlamov53b543c2021-11-09 09:21:02 -0800910 class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
Louis Dionne25547162021-08-17 12:26:09 -0400911 class = enable_if_t<__is_allocator<_Alloc>::value>
Marshall Clowf0ca1492018-05-21 21:30:12 +0000912 >
913vector(_InputIterator, _InputIterator)
Arthur O'Dwyer56226762021-03-03 23:02:20 -0500914 -> vector<__iter_value_type<_InputIterator>, _Alloc>;
Marshall Clowf0ca1492018-05-21 21:30:12 +0000915
916template<class _InputIterator,
917 class _Alloc,
Konstantin Varlamov53b543c2021-11-09 09:21:02 -0800918 class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
Louis Dionne25547162021-08-17 12:26:09 -0400919 class = enable_if_t<__is_allocator<_Alloc>::value>
Marshall Clowf0ca1492018-05-21 21:30:12 +0000920 >
921vector(_InputIterator, _InputIterator, _Alloc)
Arthur O'Dwyer56226762021-03-03 23:02:20 -0500922 -> vector<__iter_value_type<_InputIterator>, _Alloc>;
Marshall Clowf0ca1492018-05-21 21:30:12 +0000923#endif
924
Howard Hinnantc51e1022010-05-11 19:42:16 +0000925template <class _Tp, class _Allocator>
926void
927vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v)
928{
Eric Fiselier909fe962019-09-13 16:09:33 +0000929
Marshall Clow2cd9d372014-05-08 14:14:06 +0000930 __annotate_delete();
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -0500931 _VSTD::__construct_backward_with_exception_guarantees(this->__alloc(), this->__begin_, this->__end_, __v.__begin_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000932 _VSTD::swap(this->__begin_, __v.__begin_);
933 _VSTD::swap(this->__end_, __v.__end_);
934 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000935 __v.__first_ = __v.__begin_;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000936 __annotate_new(size());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000937 __invalidate_all_iterators();
938}
939
940template <class _Tp, class _Allocator>
941typename vector<_Tp, _Allocator>::pointer
942vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p)
943{
Marshall Clow2cd9d372014-05-08 14:14:06 +0000944 __annotate_delete();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000945 pointer __r = __v.__begin_;
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -0500946 _VSTD::__construct_backward_with_exception_guarantees(this->__alloc(), this->__begin_, __p, __v.__begin_);
947 _VSTD::__construct_forward_with_exception_guarantees(this->__alloc(), __p, this->__end_, __v.__end_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000948 _VSTD::swap(this->__begin_, __v.__begin_);
949 _VSTD::swap(this->__end_, __v.__end_);
950 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000951 __v.__first_ = __v.__begin_;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000952 __annotate_new(size());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000953 __invalidate_all_iterators();
954 return __r;
955}
956
957// Allocate space for __n objects
958// throws length_error if __n > max_size()
959// throws (probably bad_alloc) if memory run out
960// Precondition: __begin_ == __end_ == __end_cap() == 0
961// Precondition: __n > 0
962// Postcondition: capacity() == __n
963// Postcondition: size() == 0
964template <class _Tp, class _Allocator>
965void
Marshall Clow3ff48e02018-05-22 16:20:28 +0000966vector<_Tp, _Allocator>::__vallocate(size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000967{
968 if (__n > max_size())
969 this->__throw_length_error();
970 this->__begin_ = this->__end_ = __alloc_traits::allocate(this->__alloc(), __n);
971 this->__end_cap() = this->__begin_ + __n;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000972 __annotate_new(0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000973}
974
975template <class _Tp, class _Allocator>
976void
Marshall Clow3ff48e02018-05-22 16:20:28 +0000977vector<_Tp, _Allocator>::__vdeallocate() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000978{
Howard Hinnant76053d72013-06-27 19:35:32 +0000979 if (this->__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000980 {
981 clear();
982 __alloc_traits::deallocate(this->__alloc(), this->__begin_, capacity());
Howard Hinnant76053d72013-06-27 19:35:32 +0000983 this->__begin_ = this->__end_ = this->__end_cap() = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000984 }
985}
986
987template <class _Tp, class _Allocator>
988typename vector<_Tp, _Allocator>::size_type
Howard Hinnant1c936782011-06-03 19:40:40 +0000989vector<_Tp, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000990{
Eric Fiselierb5d9f442016-11-23 01:18:56 +0000991 return _VSTD::min<size_type>(__alloc_traits::max_size(this->__alloc()),
992 numeric_limits<difference_type>::max());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000993}
994
995// Precondition: __new_size > capacity()
996template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000997inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000998typename vector<_Tp, _Allocator>::size_type
999vector<_Tp, _Allocator>::__recommend(size_type __new_size) const
1000{
1001 const size_type __ms = max_size();
1002 if (__new_size > __ms)
1003 this->__throw_length_error();
1004 const size_type __cap = capacity();
1005 if (__cap >= __ms / 2)
1006 return __ms;
Arthur O'Dwyerc0fb14b2021-07-26 18:23:00 -04001007 return _VSTD::max<size_type>(2 * __cap, __new_size);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001008}
1009
1010// Default constructs __n objects starting at __end_
1011// throws if construction throws
1012// Precondition: __n > 0
1013// Precondition: size() + __n <= capacity()
1014// Postcondition: size() == size() + __n
1015template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001016void
1017vector<_Tp, _Allocator>::__construct_at_end(size_type __n)
1018{
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001019 _ConstructTransaction __tx(*this, __n);
Martijn Vels1e2e1b82020-06-18 13:14:02 -04001020 const_pointer __new_end = __tx.__new_end_;
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04001021 for (pointer __pos = __tx.__pos_; __pos != __new_end; __tx.__pos_ = ++__pos) {
Martijn Vels1e2e1b82020-06-18 13:14:02 -04001022 __alloc_traits::construct(this->__alloc(), _VSTD::__to_address(__pos));
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001023 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001024}
1025
Howard Hinnantc51e1022010-05-11 19:42:16 +00001026// Copy constructs __n objects starting at __end_ from __x
1027// throws if construction throws
1028// Precondition: __n > 0
1029// Precondition: size() + __n <= capacity()
1030// Postcondition: size() == old size() + __n
1031// Postcondition: [i] == __x for all i in [size() - __n, __n)
1032template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001033inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001034void
1035vector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
1036{
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001037 _ConstructTransaction __tx(*this, __n);
Martijn Vels1e2e1b82020-06-18 13:14:02 -04001038 const_pointer __new_end = __tx.__new_end_;
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04001039 for (pointer __pos = __tx.__pos_; __pos != __new_end; __tx.__pos_ = ++__pos) {
Martijn Vels1e2e1b82020-06-18 13:14:02 -04001040 __alloc_traits::construct(this->__alloc(), _VSTD::__to_address(__pos), __x);
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001041 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001042}
1043
1044template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001045template <class _ForwardIterator>
1046typename enable_if
1047<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001048 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001049 void
1050>::type
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001051vector<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001052{
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001053 _ConstructTransaction __tx(*this, __n);
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05001054 _VSTD::__construct_range_forward(this->__alloc(), __first, __last, __tx.__pos_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001055}
1056
1057// Default constructs __n objects starting at __end_
1058// throws if construction throws
1059// Postcondition: size() == size() + __n
Howard Hinnant1c936782011-06-03 19:40:40 +00001060// Exception safety: strong.
Howard Hinnantc51e1022010-05-11 19:42:16 +00001061template <class _Tp, class _Allocator>
1062void
1063vector<_Tp, _Allocator>::__append(size_type __n)
1064{
1065 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1066 this->__construct_at_end(__n);
1067 else
1068 {
1069 allocator_type& __a = this->__alloc();
1070 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1071 __v.__construct_at_end(__n);
1072 __swap_out_circular_buffer(__v);
1073 }
1074}
1075
1076// Default constructs __n objects starting at __end_
1077// throws if construction throws
1078// Postcondition: size() == size() + __n
Howard Hinnant1c936782011-06-03 19:40:40 +00001079// Exception safety: strong.
Howard Hinnantc51e1022010-05-11 19:42:16 +00001080template <class _Tp, class _Allocator>
1081void
1082vector<_Tp, _Allocator>::__append(size_type __n, const_reference __x)
1083{
1084 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1085 this->__construct_at_end(__n, __x);
1086 else
1087 {
1088 allocator_type& __a = this->__alloc();
1089 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1090 __v.__construct_at_end(__n, __x);
1091 __swap_out_circular_buffer(__v);
1092 }
1093}
1094
1095template <class _Tp, class _Allocator>
1096vector<_Tp, _Allocator>::vector(size_type __n)
1097{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001098 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001099 if (__n > 0)
1100 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001101 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001102 __construct_at_end(__n);
1103 }
1104}
1105
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00001106#if _LIBCPP_STD_VER > 11
1107template <class _Tp, class _Allocator>
1108vector<_Tp, _Allocator>::vector(size_type __n, const allocator_type& __a)
1109 : __base(__a)
1110{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001111 _VSTD::__debug_db_insert_c(this);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00001112 if (__n > 0)
1113 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001114 __vallocate(__n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00001115 __construct_at_end(__n);
1116 }
1117}
1118#endif
1119
Howard Hinnantc51e1022010-05-11 19:42:16 +00001120template <class _Tp, class _Allocator>
Marshall Clowf0ca1492018-05-21 21:30:12 +00001121vector<_Tp, _Allocator>::vector(size_type __n, const value_type& __x)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001122{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001123 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001124 if (__n > 0)
1125 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001126 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001127 __construct_at_end(__n, __x);
1128 }
1129}
1130
1131template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001132template <class _InputIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +00001133vector<_Tp, _Allocator>::vector(_InputIterator __first,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001134 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
1135 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001136 is_constructible<
1137 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +00001138 typename iterator_traits<_InputIterator>::reference>::value,
1139 _InputIterator>::type __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001140{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001141 _VSTD::__debug_db_insert_c(this);
Howard Hinnant9cd22302011-09-16 18:41:29 +00001142 for (; __first != __last; ++__first)
Eric Fiselier96919722017-10-17 13:03:17 +00001143 __emplace_back(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001144}
1145
1146template <class _Tp, class _Allocator>
1147template <class _InputIterator>
1148vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001149 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
1150 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001151 is_constructible<
1152 value_type,
1153 typename iterator_traits<_InputIterator>::reference>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001154 : __base(__a)
1155{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001156 _VSTD::__debug_db_insert_c(this);
Howard Hinnant9cd22302011-09-16 18:41:29 +00001157 for (; __first != __last; ++__first)
Eric Fiselier96919722017-10-17 13:03:17 +00001158 __emplace_back(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001159}
1160
1161template <class _Tp, class _Allocator>
1162template <class _ForwardIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +00001163vector<_Tp, _Allocator>::vector(_ForwardIterator __first,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001164 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001165 is_constructible<
1166 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +00001167 typename iterator_traits<_ForwardIterator>::reference>::value,
1168 _ForwardIterator>::type __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001169{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001170 _VSTD::__debug_db_insert_c(this);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001171 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001172 if (__n > 0)
1173 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001174 __vallocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001175 __construct_at_end(__first, __last, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001176 }
1177}
1178
1179template <class _Tp, class _Allocator>
1180template <class _ForwardIterator>
1181vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001182 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001183 is_constructible<
1184 value_type,
1185 typename iterator_traits<_ForwardIterator>::reference>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001186 : __base(__a)
1187{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001188 _VSTD::__debug_db_insert_c(this);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001189 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001190 if (__n > 0)
1191 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001192 __vallocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001193 __construct_at_end(__first, __last, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001194 }
1195}
1196
1197template <class _Tp, class _Allocator>
1198vector<_Tp, _Allocator>::vector(const vector& __x)
1199 : __base(__alloc_traits::select_on_container_copy_construction(__x.__alloc()))
1200{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001201 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001202 size_type __n = __x.size();
1203 if (__n > 0)
1204 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001205 __vallocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001206 __construct_at_end(__x.__begin_, __x.__end_, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001207 }
1208}
1209
1210template <class _Tp, class _Allocator>
Arthur O'Dwyer9b9662f2021-03-01 17:08:24 -05001211vector<_Tp, _Allocator>::vector(const vector& __x, const __identity_t<allocator_type>& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001212 : __base(__a)
1213{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001214 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001215 size_type __n = __x.size();
1216 if (__n > 0)
1217 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001218 __vallocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001219 __construct_at_end(__x.__begin_, __x.__end_, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001220 }
1221}
1222
Eric Fiseliered9e9362017-04-16 02:40:45 +00001223#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001224
1225template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001226inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001227vector<_Tp, _Allocator>::vector(vector&& __x)
Marshall Clowe5108202015-07-14 14:46:32 +00001228#if _LIBCPP_STD_VER > 14
1229 _NOEXCEPT
1230#else
Howard Hinnant1c936782011-06-03 19:40:40 +00001231 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowe5108202015-07-14 14:46:32 +00001232#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001233 : __base(_VSTD::move(__x.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001234{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001235 _VSTD::__debug_db_insert_c(this);
Louis Dionneba400782020-10-02 15:02:52 -04001236#if _LIBCPP_DEBUG_LEVEL == 2
Mark de Wever8f72c892021-10-10 15:40:50 +02001237 __get_db()->swap(this, _VSTD::addressof(__x));
Howard Hinnant27e0e772011-09-14 18:33:51 +00001238#endif
Howard Hinnant9cd22302011-09-16 18:41:29 +00001239 this->__begin_ = __x.__begin_;
1240 this->__end_ = __x.__end_;
1241 this->__end_cap() = __x.__end_cap();
Howard Hinnant76053d72013-06-27 19:35:32 +00001242 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001243}
1244
1245template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001246inline _LIBCPP_INLINE_VISIBILITY
Arthur O'Dwyer9b9662f2021-03-01 17:08:24 -05001247vector<_Tp, _Allocator>::vector(vector&& __x, const __identity_t<allocator_type>& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001248 : __base(__a)
1249{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001250 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001251 if (__a == __x.__alloc())
1252 {
1253 this->__begin_ = __x.__begin_;
1254 this->__end_ = __x.__end_;
1255 this->__end_cap() = __x.__end_cap();
1256 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Louis Dionneba400782020-10-02 15:02:52 -04001257#if _LIBCPP_DEBUG_LEVEL == 2
Mark de Wever8f72c892021-10-10 15:40:50 +02001258 __get_db()->swap(this, _VSTD::addressof(__x));
Howard Hinnanta1e60cd2011-09-19 16:34:29 +00001259#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001260 }
1261 else
1262 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001263 typedef move_iterator<iterator> _Ip;
1264 assign(_Ip(__x.begin()), _Ip(__x.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001265 }
1266}
1267
1268template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001269inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001270vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il)
1271{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001272 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001273 if (__il.size() > 0)
1274 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001275 __vallocate(__il.size());
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001276 __construct_at_end(__il.begin(), __il.end(), __il.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001277 }
1278}
1279
1280template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001281inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001282vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
1283 : __base(__a)
1284{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001285 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001286 if (__il.size() > 0)
1287 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001288 __vallocate(__il.size());
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001289 __construct_at_end(__il.begin(), __il.end(), __il.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001290 }
1291}
1292
1293template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001294inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001295vector<_Tp, _Allocator>&
1296vector<_Tp, _Allocator>::operator=(vector&& __x)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001297 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001298{
1299 __move_assign(__x, integral_constant<bool,
1300 __alloc_traits::propagate_on_container_move_assignment::value>());
1301 return *this;
1302}
1303
1304template <class _Tp, class _Allocator>
1305void
1306vector<_Tp, _Allocator>::__move_assign(vector& __c, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001307 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001308{
Konstantin Varlamov5f23d662021-11-08 00:10:13 -08001309 if (__alloc() != __c.__alloc())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001310 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001311 typedef move_iterator<iterator> _Ip;
1312 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001313 }
1314 else
1315 __move_assign(__c, true_type());
1316}
1317
1318template <class _Tp, class _Allocator>
1319void
1320vector<_Tp, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00001321 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001322{
Marshall Clow3ff48e02018-05-22 16:20:28 +00001323 __vdeallocate();
Konstantin Varlamov5f23d662021-11-08 00:10:13 -08001324 __move_assign_alloc(__c); // this can throw
Howard Hinnantc51e1022010-05-11 19:42:16 +00001325 this->__begin_ = __c.__begin_;
1326 this->__end_ = __c.__end_;
1327 this->__end_cap() = __c.__end_cap();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001328 __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
Louis Dionneba400782020-10-02 15:02:52 -04001329#if _LIBCPP_DEBUG_LEVEL == 2
Mark de Wever8f72c892021-10-10 15:40:50 +02001330 __get_db()->swap(this, _VSTD::addressof(__c));
Howard Hinnanta1e60cd2011-09-19 16:34:29 +00001331#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001332}
1333
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001334#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001335
1336template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001337inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001338vector<_Tp, _Allocator>&
1339vector<_Tp, _Allocator>::operator=(const vector& __x)
1340{
Mark de Wever357a1fc2021-09-28 19:15:18 +02001341 if (this != _VSTD::addressof(__x))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001342 {
Konstantin Varlamov5f23d662021-11-08 00:10:13 -08001343 __copy_assign_alloc(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001344 assign(__x.__begin_, __x.__end_);
1345 }
1346 return *this;
1347}
1348
1349template <class _Tp, class _Allocator>
1350template <class _InputIterator>
1351typename enable_if
1352<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001353 __is_cpp17_input_iterator <_InputIterator>::value &&
1354 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001355 is_constructible<
1356 _Tp,
1357 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001358 void
1359>::type
1360vector<_Tp, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
1361{
1362 clear();
1363 for (; __first != __last; ++__first)
Eric Fiselier96919722017-10-17 13:03:17 +00001364 __emplace_back(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001365}
1366
1367template <class _Tp, class _Allocator>
1368template <class _ForwardIterator>
1369typename enable_if
1370<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001371 __is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001372 is_constructible<
1373 _Tp,
1374 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001375 void
1376>::type
1377vector<_Tp, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
1378{
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001379 size_type __new_size = static_cast<size_type>(_VSTD::distance(__first, __last));
1380 if (__new_size <= capacity())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001381 {
1382 _ForwardIterator __mid = __last;
1383 bool __growing = false;
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001384 if (__new_size > size())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001385 {
1386 __growing = true;
1387 __mid = __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001388 _VSTD::advance(__mid, size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001389 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001390 pointer __m = _VSTD::copy(__first, __mid, this->__begin_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001391 if (__growing)
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001392 __construct_at_end(__mid, __last, __new_size - size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001393 else
1394 this->__destruct_at_end(__m);
1395 }
1396 else
1397 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001398 __vdeallocate();
1399 __vallocate(__recommend(__new_size));
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001400 __construct_at_end(__first, __last, __new_size);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001401 }
Eric Fiselier69c51982016-12-28 06:06:09 +00001402 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001403}
1404
1405template <class _Tp, class _Allocator>
1406void
1407vector<_Tp, _Allocator>::assign(size_type __n, const_reference __u)
1408{
1409 if (__n <= capacity())
1410 {
1411 size_type __s = size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001412 _VSTD::fill_n(this->__begin_, _VSTD::min(__n, __s), __u);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001413 if (__n > __s)
1414 __construct_at_end(__n - __s, __u);
1415 else
Howard Hinnant155c2af2010-05-24 17:49:41 +00001416 this->__destruct_at_end(this->__begin_ + __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001417 }
1418 else
1419 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001420 __vdeallocate();
1421 __vallocate(__recommend(static_cast<size_type>(__n)));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001422 __construct_at_end(__n, __u);
1423 }
Eric Fiselier69c51982016-12-28 06:06:09 +00001424 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001425}
1426
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001427template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001428inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001429typename vector<_Tp, _Allocator>::iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001430vector<_Tp, _Allocator>::__make_iter(pointer __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001431{
Louis Dionneba400782020-10-02 15:02:52 -04001432#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001433 return iterator(this, __p);
1434#else
1435 return iterator(__p);
1436#endif
1437}
1438
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001439template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001440inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001441typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001442vector<_Tp, _Allocator>::__make_iter(const_pointer __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001443{
Louis Dionneba400782020-10-02 15:02:52 -04001444#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001445 return const_iterator(this, __p);
1446#else
1447 return const_iterator(__p);
1448#endif
1449}
1450
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001451template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001452inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001453typename vector<_Tp, _Allocator>::iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001454vector<_Tp, _Allocator>::begin() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001455{
1456 return __make_iter(this->__begin_);
1457}
1458
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001459template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001460inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001461typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001462vector<_Tp, _Allocator>::begin() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001463{
1464 return __make_iter(this->__begin_);
1465}
1466
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001467template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001468inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001469typename vector<_Tp, _Allocator>::iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001470vector<_Tp, _Allocator>::end() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001471{
1472 return __make_iter(this->__end_);
1473}
1474
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001475template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001476inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001477typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001478vector<_Tp, _Allocator>::end() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001479{
1480 return __make_iter(this->__end_);
1481}
1482
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001483template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001484inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001485typename vector<_Tp, _Allocator>::reference
Marshall Clowd6470492019-03-15 00:29:35 +00001486vector<_Tp, _Allocator>::operator[](size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001487{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001488 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001489 return this->__begin_[__n];
1490}
1491
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001492template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001493inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001494typename vector<_Tp, _Allocator>::const_reference
Marshall Clowd6470492019-03-15 00:29:35 +00001495vector<_Tp, _Allocator>::operator[](size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001496{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001497 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001498 return this->__begin_[__n];
1499}
1500
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001501template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001502typename vector<_Tp, _Allocator>::reference
1503vector<_Tp, _Allocator>::at(size_type __n)
1504{
1505 if (__n >= size())
1506 this->__throw_out_of_range();
1507 return this->__begin_[__n];
1508}
1509
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001510template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001511typename vector<_Tp, _Allocator>::const_reference
1512vector<_Tp, _Allocator>::at(size_type __n) const
1513{
1514 if (__n >= size())
1515 this->__throw_out_of_range();
1516 return this->__begin_[__n];
1517}
1518
1519template <class _Tp, class _Allocator>
1520void
1521vector<_Tp, _Allocator>::reserve(size_type __n)
1522{
1523 if (__n > capacity())
1524 {
Mikhail Maltsev0b0a51d2021-10-21 10:40:05 +01001525 if (__n > max_size())
1526 this->__throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001527 allocator_type& __a = this->__alloc();
Howard Hinnant1c936782011-06-03 19:40:40 +00001528 __split_buffer<value_type, allocator_type&> __v(__n, size(), __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001529 __swap_out_circular_buffer(__v);
1530 }
1531}
1532
1533template <class _Tp, class _Allocator>
1534void
Howard Hinnant1c936782011-06-03 19:40:40 +00001535vector<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001536{
1537 if (capacity() > size())
1538 {
1539#ifndef _LIBCPP_NO_EXCEPTIONS
1540 try
1541 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001542#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001543 allocator_type& __a = this->__alloc();
Howard Hinnant1c936782011-06-03 19:40:40 +00001544 __split_buffer<value_type, allocator_type&> __v(size(), size(), __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001545 __swap_out_circular_buffer(__v);
1546#ifndef _LIBCPP_NO_EXCEPTIONS
1547 }
1548 catch (...)
1549 {
1550 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001551#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001552 }
1553}
1554
1555template <class _Tp, class _Allocator>
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001556template <class _Up>
1557void
Eric Fiseliered9e9362017-04-16 02:40:45 +00001558#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001559vector<_Tp, _Allocator>::__push_back_slow_path(_Up&& __x)
1560#else
1561vector<_Tp, _Allocator>::__push_back_slow_path(_Up& __x)
1562#endif
1563{
1564 allocator_type& __a = this->__alloc();
1565 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1566 // __v.push_back(_VSTD::forward<_Up>(__x));
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001567 __alloc_traits::construct(__a, _VSTD::__to_address(__v.__end_), _VSTD::forward<_Up>(__x));
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001568 __v.__end_++;
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001569 __swap_out_circular_buffer(__v);
1570}
1571
1572template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001573inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001574void
1575vector<_Tp, _Allocator>::push_back(const_reference __x)
1576{
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001577 if (this->__end_ != this->__end_cap())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001578 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001579 __construct_one_at_end(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001580 }
1581 else
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001582 __push_back_slow_path(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001583}
1584
Eric Fiseliered9e9362017-04-16 02:40:45 +00001585#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001586
1587template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001588inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001589void
1590vector<_Tp, _Allocator>::push_back(value_type&& __x)
1591{
1592 if (this->__end_ < this->__end_cap())
1593 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001594 __construct_one_at_end(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001595 }
1596 else
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001597 __push_back_slow_path(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001598}
1599
1600template <class _Tp, class _Allocator>
1601template <class... _Args>
1602void
Howard Hinnantb6c49562012-02-26 15:30:12 +00001603vector<_Tp, _Allocator>::__emplace_back_slow_path(_Args&&... __args)
1604{
1605 allocator_type& __a = this->__alloc();
1606 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1607// __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001608 __alloc_traits::construct(__a, _VSTD::__to_address(__v.__end_), _VSTD::forward<_Args>(__args)...);
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001609 __v.__end_++;
Howard Hinnantb6c49562012-02-26 15:30:12 +00001610 __swap_out_circular_buffer(__v);
1611}
1612
1613template <class _Tp, class _Allocator>
1614template <class... _Args>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001615inline
Marshall Clowea52cc42017-01-24 23:09:12 +00001616#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001617typename vector<_Tp, _Allocator>::reference
Marshall Clowea52cc42017-01-24 23:09:12 +00001618#else
1619void
1620#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001621vector<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1622{
1623 if (this->__end_ < this->__end_cap())
1624 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001625 __construct_one_at_end(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001626 }
1627 else
Howard Hinnantb6c49562012-02-26 15:30:12 +00001628 __emplace_back_slow_path(_VSTD::forward<_Args>(__args)...);
Marshall Clowea52cc42017-01-24 23:09:12 +00001629#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001630 return this->back();
Marshall Clowea52cc42017-01-24 23:09:12 +00001631#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001632}
1633
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001634#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001635
1636template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001637inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001638void
1639vector<_Tp, _Allocator>::pop_back()
1640{
Kristina Bessonovaaeeaa7e2021-05-09 19:29:56 +02001641 _LIBCPP_ASSERT(!empty(), "vector::pop_back called on an empty vector");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001642 this->__destruct_at_end(this->__end_ - 1);
1643}
1644
1645template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001646inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001647typename vector<_Tp, _Allocator>::iterator
1648vector<_Tp, _Allocator>::erase(const_iterator __position)
1649{
Louis Dionneba400782020-10-02 15:02:52 -04001650#if _LIBCPP_DEBUG_LEVEL == 2
Mark de Wever8f72c892021-10-10 15:40:50 +02001651 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__position)) == this,
Howard Hinnant27e0e772011-09-14 18:33:51 +00001652 "vector::erase(iterator) called with an iterator not"
1653 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001654#endif
Howard Hinnant58d52d52013-03-25 22:12:26 +00001655 _LIBCPP_ASSERT(__position != end(),
1656 "vector::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnant76053d72013-06-27 19:35:32 +00001657 difference_type __ps = __position - cbegin();
1658 pointer __p = this->__begin_ + __ps;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001659 this->__destruct_at_end(_VSTD::move(__p + 1, this->__end_, __p));
Eric Fiselier69c51982016-12-28 06:06:09 +00001660 this->__invalidate_iterators_past(__p-1);
1661 iterator __r = __make_iter(__p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001662 return __r;
1663}
1664
1665template <class _Tp, class _Allocator>
1666typename vector<_Tp, _Allocator>::iterator
1667vector<_Tp, _Allocator>::erase(const_iterator __first, const_iterator __last)
1668{
Louis Dionneba400782020-10-02 15:02:52 -04001669#if _LIBCPP_DEBUG_LEVEL == 2
Mark de Wever8f72c892021-10-10 15:40:50 +02001670 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__first)) == this,
Mark de Weverdcafc462021-10-21 18:29:14 +02001671 "vector::erase(iterator, iterator) called with an iterator not"
Howard Hinnant27e0e772011-09-14 18:33:51 +00001672 " referring to this vector");
Mark de Wever8f72c892021-10-10 15:40:50 +02001673 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__last)) == this,
Mark de Weverdcafc462021-10-21 18:29:14 +02001674 "vector::erase(iterator, iterator) called with an iterator not"
Eric Fiselier69c51982016-12-28 06:06:09 +00001675 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001676#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001677 _LIBCPP_ASSERT(__first <= __last, "vector::erase(first, last) called with invalid range");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001678 pointer __p = this->__begin_ + (__first - begin());
Eric Fiselier69c51982016-12-28 06:06:09 +00001679 if (__first != __last) {
Howard Hinnantc580cc32013-04-18 15:02:57 +00001680 this->__destruct_at_end(_VSTD::move(__p + (__last - __first), this->__end_, __p));
Eric Fiselier69c51982016-12-28 06:06:09 +00001681 this->__invalidate_iterators_past(__p - 1);
1682 }
1683 iterator __r = __make_iter(__p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001684 return __r;
1685}
1686
1687template <class _Tp, class _Allocator>
1688void
1689vector<_Tp, _Allocator>::__move_range(pointer __from_s, pointer __from_e, pointer __to)
1690{
1691 pointer __old_last = this->__end_;
1692 difference_type __n = __old_last - __to;
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001693 {
1694 pointer __i = __from_s + __n;
1695 _ConstructTransaction __tx(*this, __from_e - __i);
Martijn Vels1e2e1b82020-06-18 13:14:02 -04001696 for (pointer __pos = __tx.__pos_; __i < __from_e;
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04001697 ++__i, (void) ++__pos, __tx.__pos_ = __pos) {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001698 __alloc_traits::construct(this->__alloc(),
Martijn Vels1e2e1b82020-06-18 13:14:02 -04001699 _VSTD::__to_address(__pos),
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001700 _VSTD::move(*__i));
1701 }
1702 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001703 _VSTD::move_backward(__from_s, __from_s + __n, __old_last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001704}
1705
1706template <class _Tp, class _Allocator>
1707typename vector<_Tp, _Allocator>::iterator
1708vector<_Tp, _Allocator>::insert(const_iterator __position, const_reference __x)
1709{
Louis Dionneba400782020-10-02 15:02:52 -04001710#if _LIBCPP_DEBUG_LEVEL == 2
Mark de Wever8f72c892021-10-10 15:40:50 +02001711 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__position)) == this,
Howard Hinnant27e0e772011-09-14 18:33:51 +00001712 "vector::insert(iterator, x) called with an iterator not"
1713 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001714#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001715 pointer __p = this->__begin_ + (__position - begin());
1716 if (this->__end_ < this->__end_cap())
1717 {
1718 if (__p == this->__end_)
1719 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001720 __construct_one_at_end(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001721 }
1722 else
1723 {
1724 __move_range(__p, this->__end_, __p + 1);
1725 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1726 if (__p <= __xr && __xr < this->__end_)
1727 ++__xr;
1728 *__p = *__xr;
1729 }
1730 }
1731 else
1732 {
1733 allocator_type& __a = this->__alloc();
1734 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
1735 __v.push_back(__x);
1736 __p = __swap_out_circular_buffer(__v, __p);
1737 }
1738 return __make_iter(__p);
1739}
1740
Eric Fiseliered9e9362017-04-16 02:40:45 +00001741#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001742
1743template <class _Tp, class _Allocator>
1744typename vector<_Tp, _Allocator>::iterator
1745vector<_Tp, _Allocator>::insert(const_iterator __position, value_type&& __x)
1746{
Louis Dionneba400782020-10-02 15:02:52 -04001747#if _LIBCPP_DEBUG_LEVEL == 2
Mark de Wever8f72c892021-10-10 15:40:50 +02001748 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__position)) == this,
Howard Hinnant27e0e772011-09-14 18:33:51 +00001749 "vector::insert(iterator, x) called with an iterator not"
1750 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001751#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001752 pointer __p = this->__begin_ + (__position - begin());
1753 if (this->__end_ < this->__end_cap())
1754 {
1755 if (__p == this->__end_)
1756 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001757 __construct_one_at_end(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001758 }
1759 else
1760 {
1761 __move_range(__p, this->__end_, __p + 1);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001762 *__p = _VSTD::move(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001763 }
1764 }
1765 else
1766 {
1767 allocator_type& __a = this->__alloc();
1768 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001769 __v.push_back(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001770 __p = __swap_out_circular_buffer(__v, __p);
1771 }
1772 return __make_iter(__p);
1773}
1774
1775template <class _Tp, class _Allocator>
1776template <class... _Args>
1777typename vector<_Tp, _Allocator>::iterator
1778vector<_Tp, _Allocator>::emplace(const_iterator __position, _Args&&... __args)
1779{
Louis Dionneba400782020-10-02 15:02:52 -04001780#if _LIBCPP_DEBUG_LEVEL == 2
Mark de Wever8f72c892021-10-10 15:40:50 +02001781 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__position)) == this,
Howard Hinnant27e0e772011-09-14 18:33:51 +00001782 "vector::emplace(iterator, x) called with an iterator not"
1783 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001784#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001785 pointer __p = this->__begin_ + (__position - begin());
1786 if (this->__end_ < this->__end_cap())
1787 {
1788 if (__p == this->__end_)
1789 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001790 __construct_one_at_end(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001791 }
1792 else
1793 {
Marshall Clowa591b9a2016-07-11 21:38:08 +00001794 __temp_value<value_type, _Allocator> __tmp(this->__alloc(), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001795 __move_range(__p, this->__end_, __p + 1);
Marshall Clowa591b9a2016-07-11 21:38:08 +00001796 *__p = _VSTD::move(__tmp.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001797 }
1798 }
1799 else
1800 {
1801 allocator_type& __a = this->__alloc();
1802 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001803 __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001804 __p = __swap_out_circular_buffer(__v, __p);
1805 }
1806 return __make_iter(__p);
1807}
1808
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001809#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001810
1811template <class _Tp, class _Allocator>
1812typename vector<_Tp, _Allocator>::iterator
1813vector<_Tp, _Allocator>::insert(const_iterator __position, size_type __n, const_reference __x)
1814{
Louis Dionneba400782020-10-02 15:02:52 -04001815#if _LIBCPP_DEBUG_LEVEL == 2
Mark de Wever8f72c892021-10-10 15:40:50 +02001816 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__position)) == this,
Howard Hinnant27e0e772011-09-14 18:33:51 +00001817 "vector::insert(iterator, n, x) called with an iterator not"
1818 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001819#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001820 pointer __p = this->__begin_ + (__position - begin());
1821 if (__n > 0)
1822 {
1823 if (__n <= static_cast<size_type>(this->__end_cap() - this->__end_))
1824 {
1825 size_type __old_n = __n;
1826 pointer __old_last = this->__end_;
1827 if (__n > static_cast<size_type>(this->__end_ - __p))
1828 {
1829 size_type __cx = __n - (this->__end_ - __p);
1830 __construct_at_end(__cx, __x);
1831 __n -= __cx;
1832 }
1833 if (__n > 0)
1834 {
1835 __move_range(__p, __old_last, __p + __old_n);
1836 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1837 if (__p <= __xr && __xr < this->__end_)
1838 __xr += __old_n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001839 _VSTD::fill_n(__p, __n, *__xr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001840 }
1841 }
1842 else
1843 {
1844 allocator_type& __a = this->__alloc();
1845 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1846 __v.__construct_at_end(__n, __x);
1847 __p = __swap_out_circular_buffer(__v, __p);
1848 }
1849 }
1850 return __make_iter(__p);
1851}
1852
1853template <class _Tp, class _Allocator>
1854template <class _InputIterator>
1855typename enable_if
1856<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001857 __is_cpp17_input_iterator <_InputIterator>::value &&
1858 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001859 is_constructible<
1860 _Tp,
1861 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001862 typename vector<_Tp, _Allocator>::iterator
1863>::type
1864vector<_Tp, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
1865{
Louis Dionneba400782020-10-02 15:02:52 -04001866#if _LIBCPP_DEBUG_LEVEL == 2
Mark de Wever8f72c892021-10-10 15:40:50 +02001867 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__position)) == this,
Howard Hinnant27e0e772011-09-14 18:33:51 +00001868 "vector::insert(iterator, range) called with an iterator not"
1869 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001870#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001871 difference_type __off = __position - begin();
1872 pointer __p = this->__begin_ + __off;
1873 allocator_type& __a = this->__alloc();
1874 pointer __old_last = this->__end_;
1875 for (; this->__end_ != this->__end_cap() && __first != __last; ++__first)
1876 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001877 __construct_one_at_end(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001878 }
1879 __split_buffer<value_type, allocator_type&> __v(__a);
1880 if (__first != __last)
1881 {
1882#ifndef _LIBCPP_NO_EXCEPTIONS
1883 try
1884 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001885#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001886 __v.__construct_at_end(__first, __last);
1887 difference_type __old_size = __old_last - this->__begin_;
1888 difference_type __old_p = __p - this->__begin_;
1889 reserve(__recommend(size() + __v.size()));
1890 __p = this->__begin_ + __old_p;
1891 __old_last = this->__begin_ + __old_size;
1892#ifndef _LIBCPP_NO_EXCEPTIONS
1893 }
1894 catch (...)
1895 {
1896 erase(__make_iter(__old_last), end());
1897 throw;
1898 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001899#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001900 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001901 __p = _VSTD::rotate(__p, __old_last, this->__end_);
Louis Dionne202a4cb2020-02-11 10:59:12 +01001902 insert(__make_iter(__p), _VSTD::make_move_iterator(__v.begin()),
1903 _VSTD::make_move_iterator(__v.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001904 return begin() + __off;
1905}
1906
1907template <class _Tp, class _Allocator>
1908template <class _ForwardIterator>
1909typename enable_if
1910<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001911 __is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001912 is_constructible<
1913 _Tp,
1914 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001915 typename vector<_Tp, _Allocator>::iterator
1916>::type
1917vector<_Tp, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
1918{
Louis Dionneba400782020-10-02 15:02:52 -04001919#if _LIBCPP_DEBUG_LEVEL == 2
Mark de Wever8f72c892021-10-10 15:40:50 +02001920 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__position)) == this,
Howard Hinnant27e0e772011-09-14 18:33:51 +00001921 "vector::insert(iterator, range) called with an iterator not"
1922 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001923#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001924 pointer __p = this->__begin_ + (__position - begin());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001925 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001926 if (__n > 0)
1927 {
1928 if (__n <= this->__end_cap() - this->__end_)
1929 {
1930 size_type __old_n = __n;
1931 pointer __old_last = this->__end_;
1932 _ForwardIterator __m = __last;
1933 difference_type __dx = this->__end_ - __p;
1934 if (__n > __dx)
1935 {
1936 __m = __first;
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001937 difference_type __diff = this->__end_ - __p;
1938 _VSTD::advance(__m, __diff);
1939 __construct_at_end(__m, __last, __n - __diff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001940 __n = __dx;
1941 }
1942 if (__n > 0)
1943 {
1944 __move_range(__p, __old_last, __p + __old_n);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001945 _VSTD::copy(__first, __m, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001946 }
1947 }
1948 else
1949 {
1950 allocator_type& __a = this->__alloc();
1951 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1952 __v.__construct_at_end(__first, __last);
1953 __p = __swap_out_circular_buffer(__v, __p);
1954 }
1955 }
1956 return __make_iter(__p);
1957}
1958
1959template <class _Tp, class _Allocator>
1960void
1961vector<_Tp, _Allocator>::resize(size_type __sz)
1962{
1963 size_type __cs = size();
1964 if (__cs < __sz)
1965 this->__append(__sz - __cs);
1966 else if (__cs > __sz)
1967 this->__destruct_at_end(this->__begin_ + __sz);
1968}
1969
1970template <class _Tp, class _Allocator>
1971void
1972vector<_Tp, _Allocator>::resize(size_type __sz, const_reference __x)
1973{
1974 size_type __cs = size();
1975 if (__cs < __sz)
1976 this->__append(__sz - __cs, __x);
1977 else if (__cs > __sz)
1978 this->__destruct_at_end(this->__begin_ + __sz);
1979}
1980
1981template <class _Tp, class _Allocator>
1982void
1983vector<_Tp, _Allocator>::swap(vector& __x)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001984#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00001985 _NOEXCEPT
Marshall Clow8982dcd2015-07-13 20:04:56 +00001986#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00001987 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00001988 __is_nothrow_swappable<allocator_type>::value)
1989#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001990{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001991 _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
1992 this->__alloc() == __x.__alloc(),
1993 "vector::swap: Either propagate_on_container_swap must be true"
1994 " or the allocators must compare equal");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001995 _VSTD::swap(this->__begin_, __x.__begin_);
1996 _VSTD::swap(this->__end_, __x.__end_);
1997 _VSTD::swap(this->__end_cap(), __x.__end_cap());
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05001998 _VSTD::__swap_allocator(this->__alloc(), __x.__alloc(),
Marshall Clow8982dcd2015-07-13 20:04:56 +00001999 integral_constant<bool,__alloc_traits::propagate_on_container_swap::value>());
Louis Dionneba400782020-10-02 15:02:52 -04002000#if _LIBCPP_DEBUG_LEVEL == 2
Mark de Wever8f72c892021-10-10 15:40:50 +02002001 __get_db()->swap(this, _VSTD::addressof(__x));
Louis Dionneba400782020-10-02 15:02:52 -04002002#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002003}
2004
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002005template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002006bool
2007vector<_Tp, _Allocator>::__invariants() const
2008{
Howard Hinnant76053d72013-06-27 19:35:32 +00002009 if (this->__begin_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002010 {
Howard Hinnant76053d72013-06-27 19:35:32 +00002011 if (this->__end_ != nullptr || this->__end_cap() != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002012 return false;
2013 }
2014 else
2015 {
2016 if (this->__begin_ > this->__end_)
2017 return false;
2018 if (this->__begin_ == this->__end_cap())
2019 return false;
2020 if (this->__end_ > this->__end_cap())
2021 return false;
2022 }
2023 return true;
2024}
2025
Louis Dionneba400782020-10-02 15:02:52 -04002026#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002027
Howard Hinnantc51e1022010-05-11 19:42:16 +00002028template <class _Tp, class _Allocator>
Howard Hinnant27e0e772011-09-14 18:33:51 +00002029bool
2030vector<_Tp, _Allocator>::__dereferenceable(const const_iterator* __i) const
2031{
2032 return this->__begin_ <= __i->base() && __i->base() < this->__end_;
2033}
2034
2035template <class _Tp, class _Allocator>
2036bool
2037vector<_Tp, _Allocator>::__decrementable(const const_iterator* __i) const
2038{
2039 return this->__begin_ < __i->base() && __i->base() <= this->__end_;
2040}
2041
2042template <class _Tp, class _Allocator>
2043bool
2044vector<_Tp, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
2045{
2046 const_pointer __p = __i->base() + __n;
2047 return this->__begin_ <= __p && __p <= this->__end_;
2048}
2049
2050template <class _Tp, class _Allocator>
2051bool
2052vector<_Tp, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
2053{
2054 const_pointer __p = __i->base() + __n;
2055 return this->__begin_ <= __p && __p < this->__end_;
2056}
2057
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002058#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002059
2060template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002061inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002062void
2063vector<_Tp, _Allocator>::__invalidate_all_iterators()
2064{
Louis Dionneba400782020-10-02 15:02:52 -04002065#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002066 __get_db()->__invalidate_all(this);
Louis Dionneba400782020-10-02 15:02:52 -04002067#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002068}
2069
Eric Fiselier69c51982016-12-28 06:06:09 +00002070
2071template <class _Tp, class _Allocator>
2072inline _LIBCPP_INLINE_VISIBILITY
2073void
2074vector<_Tp, _Allocator>::__invalidate_iterators_past(pointer __new_last) {
Louis Dionneba400782020-10-02 15:02:52 -04002075#if _LIBCPP_DEBUG_LEVEL == 2
Eric Fiselier69c51982016-12-28 06:06:09 +00002076 __c_node* __c = __get_db()->__find_c_and_lock(this);
2077 for (__i_node** __p = __c->end_; __p != __c->beg_; ) {
2078 --__p;
2079 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
2080 if (__i->base() > __new_last) {
2081 (*__p)->__c_ = nullptr;
2082 if (--__c->end_ != __p)
Arthur O'Dwyer22236632020-12-07 21:50:15 -05002083 _VSTD::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
Eric Fiselier69c51982016-12-28 06:06:09 +00002084 }
2085 }
2086 __get_db()->unlock();
2087#else
2088 ((void)__new_last);
2089#endif
2090}
2091
Howard Hinnantc51e1022010-05-11 19:42:16 +00002092// vector<bool>
2093
2094template <class _Allocator> class vector<bool, _Allocator>;
2095
2096template <class _Allocator> struct hash<vector<bool, _Allocator> >;
2097
2098template <class _Allocator>
Howard Hinnantb4ef6482011-07-02 20:33:23 +00002099struct __has_storage_type<vector<bool, _Allocator> >
2100{
2101 static const bool value = true;
2102};
2103
2104template <class _Allocator>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002105class _LIBCPP_TEMPLATE_VIS vector<bool, _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002106 : private __vector_base_common<true>
2107{
2108public:
2109 typedef vector __self;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002110 typedef bool value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002111 typedef _Allocator allocator_type;
2112 typedef allocator_traits<allocator_type> __alloc_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002113 typedef typename __alloc_traits::size_type size_type;
2114 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant896b7622012-05-07 16:50:38 +00002115 typedef size_type __storage_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002116 typedef __bit_iterator<vector, false> pointer;
2117 typedef __bit_iterator<vector, true> const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002118 typedef pointer iterator;
2119 typedef const_pointer const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002120 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
2121 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002122
2123private:
Marshall Clow940e01c2015-04-07 05:21:38 +00002124 typedef typename __rebind_alloc_helper<__alloc_traits, __storage_type>::type __storage_allocator;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002125 typedef allocator_traits<__storage_allocator> __storage_traits;
2126 typedef typename __storage_traits::pointer __storage_pointer;
2127 typedef typename __storage_traits::const_pointer __const_storage_pointer;
2128
2129 __storage_pointer __begin_;
2130 size_type __size_;
2131 __compressed_pair<size_type, __storage_allocator> __cap_alloc_;
Howard Hinnant71d1c192011-07-09 15:50:42 +00002132public:
Howard Hinnantb4ef6482011-07-02 20:33:23 +00002133 typedef __bit_reference<vector> reference;
2134 typedef __bit_const_reference<vector> const_reference;
Howard Hinnant71d1c192011-07-09 15:50:42 +00002135private:
Howard Hinnant1c936782011-06-03 19:40:40 +00002136 _LIBCPP_INLINE_VISIBILITY
2137 size_type& __cap() _NOEXCEPT
2138 {return __cap_alloc_.first();}
2139 _LIBCPP_INLINE_VISIBILITY
2140 const size_type& __cap() const _NOEXCEPT
2141 {return __cap_alloc_.first();}
2142 _LIBCPP_INLINE_VISIBILITY
2143 __storage_allocator& __alloc() _NOEXCEPT
2144 {return __cap_alloc_.second();}
2145 _LIBCPP_INLINE_VISIBILITY
2146 const __storage_allocator& __alloc() const _NOEXCEPT
2147 {return __cap_alloc_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002148
2149 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
2150
Howard Hinnant1c936782011-06-03 19:40:40 +00002151 _LIBCPP_INLINE_VISIBILITY
2152 static size_type __internal_cap_to_external(size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002153 {return __n * __bits_per_word;}
Howard Hinnant1c936782011-06-03 19:40:40 +00002154 _LIBCPP_INLINE_VISIBILITY
2155 static size_type __external_cap_to_internal(size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002156 {return (__n - 1) / __bits_per_word + 1;}
2157
2158public:
Howard Hinnant1c936782011-06-03 19:40:40 +00002159 _LIBCPP_INLINE_VISIBILITY
Marshall Clowe546cbb2015-06-04 02:05:41 +00002160 vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clow8f282f42015-06-04 00:10:20 +00002161
2162 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
2163#if _LIBCPP_STD_VER <= 14
2164 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
2165#else
2166 _NOEXCEPT;
2167#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002168 ~vector();
2169 explicit vector(size_type __n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00002170#if _LIBCPP_STD_VER > 11
2171 explicit vector(size_type __n, const allocator_type& __a);
2172#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002173 vector(size_type __n, const value_type& __v);
2174 vector(size_type __n, const value_type& __v, const allocator_type& __a);
2175 template <class _InputIterator>
2176 vector(_InputIterator __first, _InputIterator __last,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002177 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
2178 !__is_cpp17_forward_iterator<_InputIterator>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002179 template <class _InputIterator>
2180 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002181 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
2182 !__is_cpp17_forward_iterator<_InputIterator>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002183 template <class _ForwardIterator>
2184 vector(_ForwardIterator __first, _ForwardIterator __last,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002185 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002186 template <class _ForwardIterator>
2187 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002188 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002189
2190 vector(const vector& __v);
2191 vector(const vector& __v, const allocator_type& __a);
2192 vector& operator=(const vector& __v);
Eric Fiseliered9e9362017-04-16 02:40:45 +00002193
2194#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002195 vector(initializer_list<value_type> __il);
2196 vector(initializer_list<value_type> __il, const allocator_type& __a);
2197
Howard Hinnant1c936782011-06-03 19:40:40 +00002198 _LIBCPP_INLINE_VISIBILITY
2199 vector(vector&& __v)
Marshall Clowe5108202015-07-14 14:46:32 +00002200#if _LIBCPP_STD_VER > 14
2201 _NOEXCEPT;
2202#else
Howard Hinnant1c936782011-06-03 19:40:40 +00002203 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowe5108202015-07-14 14:46:32 +00002204#endif
Arthur O'Dwyer9b9662f2021-03-01 17:08:24 -05002205 vector(vector&& __v, const __identity_t<allocator_type>& __a);
Howard Hinnant1c936782011-06-03 19:40:40 +00002206 _LIBCPP_INLINE_VISIBILITY
2207 vector& operator=(vector&& __v)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002208 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiseliered9e9362017-04-16 02:40:45 +00002209
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002210 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002211 vector& operator=(initializer_list<value_type> __il)
2212 {assign(__il.begin(), __il.end()); return *this;}
Eric Fiseliered9e9362017-04-16 02:40:45 +00002213
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002214#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002215
2216 template <class _InputIterator>
2217 typename enable_if
2218 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002219 __is_cpp17_input_iterator<_InputIterator>::value &&
2220 !__is_cpp17_forward_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002221 void
2222 >::type
2223 assign(_InputIterator __first, _InputIterator __last);
2224 template <class _ForwardIterator>
2225 typename enable_if
2226 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002227 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002228 void
2229 >::type
2230 assign(_ForwardIterator __first, _ForwardIterator __last);
2231
2232 void assign(size_type __n, const value_type& __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +00002233
2234#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002235 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002236 void assign(initializer_list<value_type> __il)
2237 {assign(__il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +00002238#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002239
Howard Hinnant1c936782011-06-03 19:40:40 +00002240 _LIBCPP_INLINE_VISIBILITY allocator_type get_allocator() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002241 {return allocator_type(this->__alloc());}
2242
Howard Hinnant1c936782011-06-03 19:40:40 +00002243 size_type max_size() const _NOEXCEPT;
2244 _LIBCPP_INLINE_VISIBILITY
2245 size_type capacity() const _NOEXCEPT
2246 {return __internal_cap_to_external(__cap());}
2247 _LIBCPP_INLINE_VISIBILITY
2248 size_type size() const _NOEXCEPT
2249 {return __size_;}
Marshall Clow425f5752017-11-15 05:51:26 +00002250 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +00002251 bool empty() const _NOEXCEPT
2252 {return __size_ == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002253 void reserve(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +00002254 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002255
Howard Hinnant1c936782011-06-03 19:40:40 +00002256 _LIBCPP_INLINE_VISIBILITY
2257 iterator begin() _NOEXCEPT
2258 {return __make_iter(0);}
2259 _LIBCPP_INLINE_VISIBILITY
2260 const_iterator begin() const _NOEXCEPT
2261 {return __make_iter(0);}
2262 _LIBCPP_INLINE_VISIBILITY
2263 iterator end() _NOEXCEPT
2264 {return __make_iter(__size_);}
2265 _LIBCPP_INLINE_VISIBILITY
2266 const_iterator end() const _NOEXCEPT
2267 {return __make_iter(__size_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002268
Howard Hinnant1c936782011-06-03 19:40:40 +00002269 _LIBCPP_INLINE_VISIBILITY
2270 reverse_iterator rbegin() _NOEXCEPT
2271 {return reverse_iterator(end());}
2272 _LIBCPP_INLINE_VISIBILITY
2273 const_reverse_iterator rbegin() const _NOEXCEPT
2274 {return const_reverse_iterator(end());}
2275 _LIBCPP_INLINE_VISIBILITY
2276 reverse_iterator rend() _NOEXCEPT
2277 {return reverse_iterator(begin());}
2278 _LIBCPP_INLINE_VISIBILITY
2279 const_reverse_iterator rend() const _NOEXCEPT
2280 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002281
Howard Hinnant1c936782011-06-03 19:40:40 +00002282 _LIBCPP_INLINE_VISIBILITY
2283 const_iterator cbegin() const _NOEXCEPT
2284 {return __make_iter(0);}
2285 _LIBCPP_INLINE_VISIBILITY
2286 const_iterator cend() const _NOEXCEPT
2287 {return __make_iter(__size_);}
2288 _LIBCPP_INLINE_VISIBILITY
2289 const_reverse_iterator crbegin() const _NOEXCEPT
2290 {return rbegin();}
2291 _LIBCPP_INLINE_VISIBILITY
2292 const_reverse_iterator crend() const _NOEXCEPT
2293 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002294
2295 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) {return __make_ref(__n);}
2296 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const {return __make_ref(__n);}
2297 reference at(size_type __n);
2298 const_reference at(size_type __n) const;
2299
2300 _LIBCPP_INLINE_VISIBILITY reference front() {return __make_ref(0);}
2301 _LIBCPP_INLINE_VISIBILITY const_reference front() const {return __make_ref(0);}
2302 _LIBCPP_INLINE_VISIBILITY reference back() {return __make_ref(__size_ - 1);}
2303 _LIBCPP_INLINE_VISIBILITY const_reference back() const {return __make_ref(__size_ - 1);}
2304
2305 void push_back(const value_type& __x);
Marshall Clowc46bb8e2013-08-13 23:54:12 +00002306#if _LIBCPP_STD_VER > 11
2307 template <class... _Args>
Marshall Clowea52cc42017-01-24 23:09:12 +00002308#if _LIBCPP_STD_VER > 14
2309 _LIBCPP_INLINE_VISIBILITY reference emplace_back(_Args&&... __args)
2310#else
2311 _LIBCPP_INLINE_VISIBILITY void emplace_back(_Args&&... __args)
2312#endif
2313 {
Eric Fiselier34ba5b92016-07-21 03:20:17 +00002314 push_back ( value_type ( _VSTD::forward<_Args>(__args)... ));
Marshall Clowea52cc42017-01-24 23:09:12 +00002315#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00002316 return this->back();
Marshall Clowea52cc42017-01-24 23:09:12 +00002317#endif
Eric Fiselier34ba5b92016-07-21 03:20:17 +00002318 }
Marshall Clowc46bb8e2013-08-13 23:54:12 +00002319#endif
2320
Howard Hinnantc51e1022010-05-11 19:42:16 +00002321 _LIBCPP_INLINE_VISIBILITY void pop_back() {--__size_;}
2322
Marshall Clowc46bb8e2013-08-13 23:54:12 +00002323#if _LIBCPP_STD_VER > 11
2324 template <class... _Args>
2325 _LIBCPP_INLINE_VISIBILITY iterator emplace(const_iterator position, _Args&&... __args)
2326 { return insert ( position, value_type ( _VSTD::forward<_Args>(__args)... )); }
2327#endif
2328
Howard Hinnantc51e1022010-05-11 19:42:16 +00002329 iterator insert(const_iterator __position, const value_type& __x);
2330 iterator insert(const_iterator __position, size_type __n, const value_type& __x);
2331 iterator insert(const_iterator __position, size_type __n, const_reference __x);
2332 template <class _InputIterator>
2333 typename enable_if
2334 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002335 __is_cpp17_input_iterator <_InputIterator>::value &&
2336 !__is_cpp17_forward_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002337 iterator
2338 >::type
2339 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
2340 template <class _ForwardIterator>
2341 typename enable_if
2342 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002343 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002344 iterator
2345 >::type
2346 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiseliered9e9362017-04-16 02:40:45 +00002347
2348#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002349 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002350 iterator insert(const_iterator __position, initializer_list<value_type> __il)
2351 {return insert(__position, __il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +00002352#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002353
Howard Hinnantcf823322010-12-17 14:46:43 +00002354 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002355 iterator erase(const_iterator __first, const_iterator __last);
2356
Howard Hinnant1c936782011-06-03 19:40:40 +00002357 _LIBCPP_INLINE_VISIBILITY
2358 void clear() _NOEXCEPT {__size_ = 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002359
Howard Hinnant1c936782011-06-03 19:40:40 +00002360 void swap(vector&)
Marshall Clow8982dcd2015-07-13 20:04:56 +00002361#if _LIBCPP_STD_VER >= 14
2362 _NOEXCEPT;
2363#else
Eric Fiselier69c51982016-12-28 06:06:09 +00002364 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00002365 __is_nothrow_swappable<allocator_type>::value);
2366#endif
Marshall Clow89eb3822016-04-07 14:20:31 +00002367 static void swap(reference __x, reference __y) _NOEXCEPT { _VSTD::swap(__x, __y); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002368
2369 void resize(size_type __sz, value_type __x = false);
Howard Hinnant1c936782011-06-03 19:40:40 +00002370 void flip() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002371
2372 bool __invariants() const;
2373
2374private:
Howard Hinnantcf823322010-12-17 14:46:43 +00002375 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Marshall Clow3ff48e02018-05-22 16:20:28 +00002376 void __vallocate(size_type __n);
2377 void __vdeallocate() _NOEXCEPT;
Howard Hinnant1c936782011-06-03 19:40:40 +00002378 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantea382952013-08-14 18:00:20 +00002379 static size_type __align_it(size_type __new_size) _NOEXCEPT
Eric Fiselierb41db9a2018-10-01 01:59:37 +00002380 {return __new_size + (__bits_per_word-1) & ~((size_type)__bits_per_word-1);}
Howard Hinnantcf823322010-12-17 14:46:43 +00002381 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
2382 _LIBCPP_INLINE_VISIBILITY void __construct_at_end(size_type __n, bool __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002383 template <class _ForwardIterator>
2384 typename enable_if
2385 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002386 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002387 void
2388 >::type
2389 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
2390 void __append(size_type __n, const_reference __x);
Howard Hinnant1c936782011-06-03 19:40:40 +00002391 _LIBCPP_INLINE_VISIBILITY
2392 reference __make_ref(size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002393 {return reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnant1c936782011-06-03 19:40:40 +00002394 _LIBCPP_INLINE_VISIBILITY
2395 const_reference __make_ref(size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002396 {return const_reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnant1c936782011-06-03 19:40:40 +00002397 _LIBCPP_INLINE_VISIBILITY
2398 iterator __make_iter(size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002399 {return iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnant1c936782011-06-03 19:40:40 +00002400 _LIBCPP_INLINE_VISIBILITY
2401 const_iterator __make_iter(size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002402 {return const_iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnant1c936782011-06-03 19:40:40 +00002403 _LIBCPP_INLINE_VISIBILITY
2404 iterator __const_iterator_cast(const_iterator __p) _NOEXCEPT
Howard Hinnant76053d72013-06-27 19:35:32 +00002405 {return begin() + (__p - cbegin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002406
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002407 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002408 void __copy_assign_alloc(const vector& __v)
2409 {__copy_assign_alloc(__v, integral_constant<bool,
2410 __storage_traits::propagate_on_container_copy_assignment::value>());}
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002411 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002412 void __copy_assign_alloc(const vector& __c, true_type)
2413 {
2414 if (__alloc() != __c.__alloc())
Marshall Clow3ff48e02018-05-22 16:20:28 +00002415 __vdeallocate();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002416 __alloc() = __c.__alloc();
2417 }
2418
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002419 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00002420 void __copy_assign_alloc(const vector&, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002421 {}
2422
2423 void __move_assign(vector& __c, false_type);
Howard Hinnant1c936782011-06-03 19:40:40 +00002424 void __move_assign(vector& __c, true_type)
2425 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002426 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002427 void __move_assign_alloc(vector& __c)
Howard Hinnant1c936782011-06-03 19:40:40 +00002428 _NOEXCEPT_(
2429 !__storage_traits::propagate_on_container_move_assignment::value ||
2430 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002431 {__move_assign_alloc(__c, integral_constant<bool,
2432 __storage_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002433 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +00002434 void __move_assign_alloc(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002435 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002436 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002437 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002438 }
2439
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002440 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00002441 void __move_assign_alloc(vector&, false_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002442 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002443 {}
2444
Howard Hinnant1c936782011-06-03 19:40:40 +00002445 size_t __hash_code() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002446
2447 friend class __bit_reference<vector>;
2448 friend class __bit_const_reference<vector>;
2449 friend class __bit_iterator<vector, false>;
2450 friend class __bit_iterator<vector, true>;
Howard Hinnant4210a0f2012-08-17 17:10:18 +00002451 friend struct __bit_array<vector>;
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002452 friend struct _LIBCPP_TEMPLATE_VIS hash<vector>;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002453};
2454
2455template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002456inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002457void
2458vector<bool, _Allocator>::__invalidate_all_iterators()
2459{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002460}
2461
2462// Allocate space for __n objects
2463// throws length_error if __n > max_size()
2464// throws (probably bad_alloc) if memory run out
2465// Precondition: __begin_ == __end_ == __cap() == 0
2466// Precondition: __n > 0
2467// Postcondition: capacity() == __n
2468// Postcondition: size() == 0
2469template <class _Allocator>
2470void
Marshall Clow3ff48e02018-05-22 16:20:28 +00002471vector<bool, _Allocator>::__vallocate(size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002472{
2473 if (__n > max_size())
2474 this->__throw_length_error();
2475 __n = __external_cap_to_internal(__n);
2476 this->__begin_ = __storage_traits::allocate(this->__alloc(), __n);
2477 this->__size_ = 0;
2478 this->__cap() = __n;
2479}
2480
2481template <class _Allocator>
2482void
Marshall Clow3ff48e02018-05-22 16:20:28 +00002483vector<bool, _Allocator>::__vdeallocate() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002484{
Howard Hinnant76053d72013-06-27 19:35:32 +00002485 if (this->__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002486 {
2487 __storage_traits::deallocate(this->__alloc(), this->__begin_, __cap());
2488 __invalidate_all_iterators();
Howard Hinnant76053d72013-06-27 19:35:32 +00002489 this->__begin_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002490 this->__size_ = this->__cap() = 0;
2491 }
2492}
2493
2494template <class _Allocator>
2495typename vector<bool, _Allocator>::size_type
Howard Hinnant1c936782011-06-03 19:40:40 +00002496vector<bool, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002497{
2498 size_type __amax = __storage_traits::max_size(__alloc());
2499 size_type __nmax = numeric_limits<size_type>::max() / 2; // end() >= begin(), always
2500 if (__nmax / __bits_per_word <= __amax)
2501 return __nmax;
2502 return __internal_cap_to_external(__amax);
2503}
2504
2505// Precondition: __new_size > capacity()
2506template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002507inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002508typename vector<bool, _Allocator>::size_type
2509vector<bool, _Allocator>::__recommend(size_type __new_size) const
2510{
2511 const size_type __ms = max_size();
2512 if (__new_size > __ms)
2513 this->__throw_length_error();
2514 const size_type __cap = capacity();
2515 if (__cap >= __ms / 2)
2516 return __ms;
Arthur O'Dwyerc0fb14b2021-07-26 18:23:00 -04002517 return _VSTD::max(2 * __cap, __align_it(__new_size));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002518}
2519
2520// Default constructs __n objects starting at __end_
2521// Precondition: __n > 0
2522// Precondition: size() + __n <= capacity()
2523// Postcondition: size() == size() + __n
2524template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002525inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002526void
2527vector<bool, _Allocator>::__construct_at_end(size_type __n, bool __x)
2528{
2529 size_type __old_size = this->__size_;
2530 this->__size_ += __n;
Marshall Clow1893ec72018-10-23 20:07:45 +00002531 if (__old_size == 0 || ((__old_size - 1) / __bits_per_word) != ((this->__size_ - 1) / __bits_per_word))
2532 {
2533 if (this->__size_ <= __bits_per_word)
2534 this->__begin_[0] = __storage_type(0);
2535 else
2536 this->__begin_[(this->__size_ - 1) / __bits_per_word] = __storage_type(0);
2537 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002538 _VSTD::fill_n(__make_iter(__old_size), __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002539}
2540
2541template <class _Allocator>
2542template <class _ForwardIterator>
2543typename enable_if
2544<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002545 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002546 void
2547>::type
2548vector<bool, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
2549{
2550 size_type __old_size = this->__size_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002551 this->__size_ += _VSTD::distance(__first, __last);
Marshall Clow1893ec72018-10-23 20:07:45 +00002552 if (__old_size == 0 || ((__old_size - 1) / __bits_per_word) != ((this->__size_ - 1) / __bits_per_word))
2553 {
2554 if (this->__size_ <= __bits_per_word)
2555 this->__begin_[0] = __storage_type(0);
2556 else
2557 this->__begin_[(this->__size_ - 1) / __bits_per_word] = __storage_type(0);
2558 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002559 _VSTD::copy(__first, __last, __make_iter(__old_size));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002560}
2561
2562template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002563inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002564vector<bool, _Allocator>::vector()
Marshall Clowe546cbb2015-06-04 02:05:41 +00002565 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant76053d72013-06-27 19:35:32 +00002566 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002567 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002568 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002569{
2570}
2571
2572template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002573inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002574vector<bool, _Allocator>::vector(const allocator_type& __a)
Marshall Clow8f282f42015-06-04 00:10:20 +00002575#if _LIBCPP_STD_VER <= 14
2576 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
2577#else
2578 _NOEXCEPT
2579#endif
Howard Hinnant76053d72013-06-27 19:35:32 +00002580 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002581 __size_(0),
2582 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2583{
2584}
2585
2586template <class _Allocator>
2587vector<bool, _Allocator>::vector(size_type __n)
Howard Hinnant76053d72013-06-27 19:35:32 +00002588 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002589 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002590 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002591{
2592 if (__n > 0)
2593 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002594 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002595 __construct_at_end(__n, false);
2596 }
2597}
2598
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00002599#if _LIBCPP_STD_VER > 11
2600template <class _Allocator>
2601vector<bool, _Allocator>::vector(size_type __n, const allocator_type& __a)
2602 : __begin_(nullptr),
2603 __size_(0),
2604 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2605{
2606 if (__n > 0)
2607 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002608 __vallocate(__n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00002609 __construct_at_end(__n, false);
2610 }
2611}
2612#endif
2613
Howard Hinnantc51e1022010-05-11 19:42:16 +00002614template <class _Allocator>
2615vector<bool, _Allocator>::vector(size_type __n, const value_type& __x)
Howard Hinnant76053d72013-06-27 19:35:32 +00002616 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002617 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002618 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002619{
2620 if (__n > 0)
2621 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002622 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002623 __construct_at_end(__n, __x);
2624 }
2625}
2626
2627template <class _Allocator>
2628vector<bool, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002629 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002630 __size_(0),
2631 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2632{
2633 if (__n > 0)
2634 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002635 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002636 __construct_at_end(__n, __x);
2637 }
2638}
2639
2640template <class _Allocator>
2641template <class _InputIterator>
2642vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002643 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
2644 !__is_cpp17_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002645 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002646 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002647 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002648{
2649#ifndef _LIBCPP_NO_EXCEPTIONS
2650 try
2651 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002652#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002653 for (; __first != __last; ++__first)
2654 push_back(*__first);
2655#ifndef _LIBCPP_NO_EXCEPTIONS
2656 }
2657 catch (...)
2658 {
Howard Hinnant76053d72013-06-27 19:35:32 +00002659 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002660 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2661 __invalidate_all_iterators();
2662 throw;
2663 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002664#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002665}
2666
2667template <class _Allocator>
2668template <class _InputIterator>
2669vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002670 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
2671 !__is_cpp17_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002672 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002673 __size_(0),
2674 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2675{
2676#ifndef _LIBCPP_NO_EXCEPTIONS
2677 try
2678 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002679#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002680 for (; __first != __last; ++__first)
2681 push_back(*__first);
2682#ifndef _LIBCPP_NO_EXCEPTIONS
2683 }
2684 catch (...)
2685 {
Howard Hinnant76053d72013-06-27 19:35:32 +00002686 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002687 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2688 __invalidate_all_iterators();
2689 throw;
2690 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002691#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002692}
2693
2694template <class _Allocator>
2695template <class _ForwardIterator>
2696vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002697 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002698 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002699 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002700 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002701{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002702 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002703 if (__n > 0)
2704 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002705 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002706 __construct_at_end(__first, __last);
2707 }
2708}
2709
2710template <class _Allocator>
2711template <class _ForwardIterator>
2712vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002713 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002714 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002715 __size_(0),
2716 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2717{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002718 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002719 if (__n > 0)
2720 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002721 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002722 __construct_at_end(__first, __last);
2723 }
2724}
2725
Eric Fiseliered9e9362017-04-16 02:40:45 +00002726#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002727
Howard Hinnantc51e1022010-05-11 19:42:16 +00002728template <class _Allocator>
2729vector<bool, _Allocator>::vector(initializer_list<value_type> __il)
Howard Hinnant76053d72013-06-27 19:35:32 +00002730 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002731 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002732 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002733{
2734 size_type __n = static_cast<size_type>(__il.size());
2735 if (__n > 0)
2736 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002737 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002738 __construct_at_end(__il.begin(), __il.end());
2739 }
2740}
2741
2742template <class _Allocator>
2743vector<bool, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002744 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002745 __size_(0),
2746 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2747{
2748 size_type __n = static_cast<size_type>(__il.size());
2749 if (__n > 0)
2750 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002751 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002752 __construct_at_end(__il.begin(), __il.end());
2753 }
2754}
2755
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002756#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002757
Howard Hinnantc51e1022010-05-11 19:42:16 +00002758template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002759vector<bool, _Allocator>::~vector()
2760{
Howard Hinnant76053d72013-06-27 19:35:32 +00002761 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002762 __storage_traits::deallocate(__alloc(), __begin_, __cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002763 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002764}
2765
2766template <class _Allocator>
2767vector<bool, _Allocator>::vector(const vector& __v)
Howard Hinnant76053d72013-06-27 19:35:32 +00002768 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002769 __size_(0),
2770 __cap_alloc_(0, __storage_traits::select_on_container_copy_construction(__v.__alloc()))
2771{
2772 if (__v.size() > 0)
2773 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002774 __vallocate(__v.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002775 __construct_at_end(__v.begin(), __v.end());
2776 }
2777}
2778
2779template <class _Allocator>
2780vector<bool, _Allocator>::vector(const vector& __v, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002781 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002782 __size_(0),
2783 __cap_alloc_(0, __a)
2784{
2785 if (__v.size() > 0)
2786 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002787 __vallocate(__v.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002788 __construct_at_end(__v.begin(), __v.end());
2789 }
2790}
2791
2792template <class _Allocator>
2793vector<bool, _Allocator>&
2794vector<bool, _Allocator>::operator=(const vector& __v)
2795{
Mark de Wever357a1fc2021-09-28 19:15:18 +02002796 if (this != _VSTD::addressof(__v))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002797 {
2798 __copy_assign_alloc(__v);
2799 if (__v.__size_)
2800 {
2801 if (__v.__size_ > capacity())
2802 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002803 __vdeallocate();
2804 __vallocate(__v.__size_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002805 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002806 _VSTD::copy(__v.__begin_, __v.__begin_ + __external_cap_to_internal(__v.__size_), __begin_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002807 }
2808 __size_ = __v.__size_;
2809 }
2810 return *this;
2811}
2812
Eric Fiseliered9e9362017-04-16 02:40:45 +00002813#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant74279a52010-09-04 23:28:19 +00002814
Howard Hinnantc51e1022010-05-11 19:42:16 +00002815template <class _Allocator>
Eric Fiselier4f1534c2018-06-05 22:32:52 +00002816inline _LIBCPP_INLINE_VISIBILITY vector<bool, _Allocator>::vector(vector&& __v)
Marshall Clowe5108202015-07-14 14:46:32 +00002817#if _LIBCPP_STD_VER > 14
Eric Fiselier4f1534c2018-06-05 22:32:52 +00002818 _NOEXCEPT
Marshall Clowe5108202015-07-14 14:46:32 +00002819#else
Eric Fiselier4f1534c2018-06-05 22:32:52 +00002820 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowe5108202015-07-14 14:46:32 +00002821#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002822 : __begin_(__v.__begin_),
2823 __size_(__v.__size_),
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05002824 __cap_alloc_(_VSTD::move(__v.__cap_alloc_)) {
Howard Hinnant76053d72013-06-27 19:35:32 +00002825 __v.__begin_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002826 __v.__size_ = 0;
2827 __v.__cap() = 0;
2828}
2829
2830template <class _Allocator>
Arthur O'Dwyer9b9662f2021-03-01 17:08:24 -05002831vector<bool, _Allocator>::vector(vector&& __v, const __identity_t<allocator_type>& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002832 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002833 __size_(0),
2834 __cap_alloc_(0, __a)
2835{
2836 if (__a == allocator_type(__v.__alloc()))
2837 {
2838 this->__begin_ = __v.__begin_;
2839 this->__size_ = __v.__size_;
2840 this->__cap() = __v.__cap();
2841 __v.__begin_ = nullptr;
2842 __v.__cap() = __v.__size_ = 0;
2843 }
2844 else if (__v.size() > 0)
2845 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002846 __vallocate(__v.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002847 __construct_at_end(__v.begin(), __v.end());
2848 }
2849}
2850
2851template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002852inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002853vector<bool, _Allocator>&
2854vector<bool, _Allocator>::operator=(vector&& __v)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002855 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002856{
2857 __move_assign(__v, integral_constant<bool,
2858 __storage_traits::propagate_on_container_move_assignment::value>());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002859 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002860}
2861
2862template <class _Allocator>
2863void
2864vector<bool, _Allocator>::__move_assign(vector& __c, false_type)
2865{
2866 if (__alloc() != __c.__alloc())
2867 assign(__c.begin(), __c.end());
2868 else
2869 __move_assign(__c, true_type());
2870}
2871
2872template <class _Allocator>
2873void
2874vector<bool, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002875 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002876{
Marshall Clow3ff48e02018-05-22 16:20:28 +00002877 __vdeallocate();
Marshall Clowb4871fa2014-07-21 15:15:15 +00002878 __move_assign_alloc(__c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002879 this->__begin_ = __c.__begin_;
2880 this->__size_ = __c.__size_;
2881 this->__cap() = __c.__cap();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002882 __c.__begin_ = nullptr;
2883 __c.__cap() = __c.__size_ = 0;
2884}
Howard Hinnant74279a52010-09-04 23:28:19 +00002885
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002886#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002887
2888template <class _Allocator>
2889void
2890vector<bool, _Allocator>::assign(size_type __n, const value_type& __x)
2891{
2892 __size_ = 0;
2893 if (__n > 0)
2894 {
2895 size_type __c = capacity();
2896 if (__n <= __c)
2897 __size_ = __n;
2898 else
2899 {
Mikhail Maltsevf56b0692021-10-21 10:38:56 +01002900 vector __v(get_allocator());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002901 __v.reserve(__recommend(__n));
2902 __v.__size_ = __n;
2903 swap(__v);
2904 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002905 _VSTD::fill_n(begin(), __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002906 }
Eric Fiselier69c51982016-12-28 06:06:09 +00002907 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002908}
2909
2910template <class _Allocator>
2911template <class _InputIterator>
2912typename enable_if
2913<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002914 __is_cpp17_input_iterator<_InputIterator>::value &&
2915 !__is_cpp17_forward_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002916 void
2917>::type
2918vector<bool, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2919{
2920 clear();
2921 for (; __first != __last; ++__first)
2922 push_back(*__first);
2923}
2924
2925template <class _Allocator>
2926template <class _ForwardIterator>
2927typename enable_if
2928<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002929 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002930 void
2931>::type
2932vector<bool, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2933{
2934 clear();
Eric Fiselier6003c772016-12-23 23:37:52 +00002935 difference_type __ns = _VSTD::distance(__first, __last);
2936 _LIBCPP_ASSERT(__ns >= 0, "invalid range specified");
2937 const size_t __n = static_cast<size_type>(__ns);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002938 if (__n)
2939 {
2940 if (__n > capacity())
2941 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002942 __vdeallocate();
2943 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002944 }
2945 __construct_at_end(__first, __last);
2946 }
2947}
2948
2949template <class _Allocator>
2950void
2951vector<bool, _Allocator>::reserve(size_type __n)
2952{
2953 if (__n > capacity())
2954 {
Mikhail Maltsev0b0a51d2021-10-21 10:40:05 +01002955 if (__n > max_size())
2956 this->__throw_length_error();
Mikhail Maltsevf56b0692021-10-21 10:38:56 +01002957 vector __v(this->get_allocator());
Marshall Clow3ff48e02018-05-22 16:20:28 +00002958 __v.__vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002959 __v.__construct_at_end(this->begin(), this->end());
2960 swap(__v);
2961 __invalidate_all_iterators();
2962 }
2963}
2964
2965template <class _Allocator>
2966void
Howard Hinnant1c936782011-06-03 19:40:40 +00002967vector<bool, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002968{
2969 if (__external_cap_to_internal(size()) > __cap())
2970 {
2971#ifndef _LIBCPP_NO_EXCEPTIONS
2972 try
2973 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002974#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002975 vector(*this, allocator_type(__alloc())).swap(*this);
2976#ifndef _LIBCPP_NO_EXCEPTIONS
2977 }
2978 catch (...)
2979 {
2980 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002981#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002982 }
2983}
2984
2985template <class _Allocator>
2986typename vector<bool, _Allocator>::reference
2987vector<bool, _Allocator>::at(size_type __n)
2988{
2989 if (__n >= size())
2990 this->__throw_out_of_range();
2991 return (*this)[__n];
2992}
2993
2994template <class _Allocator>
2995typename vector<bool, _Allocator>::const_reference
2996vector<bool, _Allocator>::at(size_type __n) const
2997{
2998 if (__n >= size())
2999 this->__throw_out_of_range();
3000 return (*this)[__n];
3001}
3002
3003template <class _Allocator>
3004void
3005vector<bool, _Allocator>::push_back(const value_type& __x)
3006{
3007 if (this->__size_ == this->capacity())
3008 reserve(__recommend(this->__size_ + 1));
3009 ++this->__size_;
3010 back() = __x;
3011}
3012
3013template <class _Allocator>
3014typename vector<bool, _Allocator>::iterator
3015vector<bool, _Allocator>::insert(const_iterator __position, const value_type& __x)
3016{
3017 iterator __r;
3018 if (size() < capacity())
3019 {
3020 const_iterator __old_end = end();
3021 ++__size_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003022 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003023 __r = __const_iterator_cast(__position);
3024 }
3025 else
3026 {
Mikhail Maltsevf56b0692021-10-21 10:38:56 +01003027 vector __v(get_allocator());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003028 __v.reserve(__recommend(__size_ + 1));
3029 __v.__size_ = __size_ + 1;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003030 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3031 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003032 swap(__v);
3033 }
3034 *__r = __x;
3035 return __r;
3036}
3037
3038template <class _Allocator>
3039typename vector<bool, _Allocator>::iterator
3040vector<bool, _Allocator>::insert(const_iterator __position, size_type __n, const value_type& __x)
3041{
3042 iterator __r;
3043 size_type __c = capacity();
3044 if (__n <= __c && size() <= __c - __n)
3045 {
3046 const_iterator __old_end = end();
3047 __size_ += __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003048 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003049 __r = __const_iterator_cast(__position);
3050 }
3051 else
3052 {
Mikhail Maltsevf56b0692021-10-21 10:38:56 +01003053 vector __v(get_allocator());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003054 __v.reserve(__recommend(__size_ + __n));
3055 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003056 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3057 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003058 swap(__v);
3059 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003060 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003061 return __r;
3062}
3063
3064template <class _Allocator>
3065template <class _InputIterator>
3066typename enable_if
3067<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05003068 __is_cpp17_input_iterator <_InputIterator>::value &&
3069 !__is_cpp17_forward_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003070 typename vector<bool, _Allocator>::iterator
3071>::type
3072vector<bool, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
3073{
3074 difference_type __off = __position - begin();
3075 iterator __p = __const_iterator_cast(__position);
3076 iterator __old_end = end();
3077 for (; size() != capacity() && __first != __last; ++__first)
3078 {
3079 ++this->__size_;
3080 back() = *__first;
3081 }
Mikhail Maltsevf56b0692021-10-21 10:38:56 +01003082 vector __v(get_allocator());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003083 if (__first != __last)
3084 {
3085#ifndef _LIBCPP_NO_EXCEPTIONS
3086 try
3087 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003088#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003089 __v.assign(__first, __last);
3090 difference_type __old_size = static_cast<difference_type>(__old_end - begin());
3091 difference_type __old_p = __p - begin();
3092 reserve(__recommend(size() + __v.size()));
3093 __p = begin() + __old_p;
3094 __old_end = begin() + __old_size;
3095#ifndef _LIBCPP_NO_EXCEPTIONS
3096 }
3097 catch (...)
3098 {
3099 erase(__old_end, end());
3100 throw;
3101 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003102#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003103 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003104 __p = _VSTD::rotate(__p, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003105 insert(__p, __v.begin(), __v.end());
3106 return begin() + __off;
3107}
3108
3109template <class _Allocator>
3110template <class _ForwardIterator>
3111typename enable_if
3112<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05003113 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003114 typename vector<bool, _Allocator>::iterator
3115>::type
3116vector<bool, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
3117{
Eric Fiselier654dd332016-12-11 05:31:00 +00003118 const difference_type __n_signed = _VSTD::distance(__first, __last);
3119 _LIBCPP_ASSERT(__n_signed >= 0, "invalid range specified");
3120 const size_type __n = static_cast<size_type>(__n_signed);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003121 iterator __r;
3122 size_type __c = capacity();
3123 if (__n <= __c && size() <= __c - __n)
3124 {
3125 const_iterator __old_end = end();
3126 __size_ += __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003127 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003128 __r = __const_iterator_cast(__position);
3129 }
3130 else
3131 {
Mikhail Maltsevf56b0692021-10-21 10:38:56 +01003132 vector __v(get_allocator());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003133 __v.reserve(__recommend(__size_ + __n));
3134 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003135 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3136 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003137 swap(__v);
3138 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003139 _VSTD::copy(__first, __last, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003140 return __r;
3141}
3142
3143template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003144inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003145typename vector<bool, _Allocator>::iterator
3146vector<bool, _Allocator>::erase(const_iterator __position)
3147{
3148 iterator __r = __const_iterator_cast(__position);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003149 _VSTD::copy(__position + 1, this->cend(), __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003150 --__size_;
3151 return __r;
3152}
3153
3154template <class _Allocator>
3155typename vector<bool, _Allocator>::iterator
3156vector<bool, _Allocator>::erase(const_iterator __first, const_iterator __last)
3157{
3158 iterator __r = __const_iterator_cast(__first);
3159 difference_type __d = __last - __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003160 _VSTD::copy(__last, this->cend(), __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003161 __size_ -= __d;
3162 return __r;
3163}
3164
3165template <class _Allocator>
3166void
3167vector<bool, _Allocator>::swap(vector& __x)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003168#if _LIBCPP_STD_VER >= 14
3169 _NOEXCEPT
3170#else
Eric Fiselier69c51982016-12-28 06:06:09 +00003171 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003172 __is_nothrow_swappable<allocator_type>::value)
3173#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003174{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003175 _VSTD::swap(this->__begin_, __x.__begin_);
3176 _VSTD::swap(this->__size_, __x.__size_);
3177 _VSTD::swap(this->__cap(), __x.__cap());
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05003178 _VSTD::__swap_allocator(this->__alloc(), __x.__alloc(),
Marshall Clow8982dcd2015-07-13 20:04:56 +00003179 integral_constant<bool, __alloc_traits::propagate_on_container_swap::value>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003180}
3181
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003182template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003183void
3184vector<bool, _Allocator>::resize(size_type __sz, value_type __x)
3185{
3186 size_type __cs = size();
3187 if (__cs < __sz)
3188 {
3189 iterator __r;
3190 size_type __c = capacity();
3191 size_type __n = __sz - __cs;
3192 if (__n <= __c && __cs <= __c - __n)
3193 {
3194 __r = end();
3195 __size_ += __n;
3196 }
3197 else
3198 {
Mikhail Maltsevf56b0692021-10-21 10:38:56 +01003199 vector __v(get_allocator());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003200 __v.reserve(__recommend(__size_ + __n));
3201 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003202 __r = _VSTD::copy(cbegin(), cend(), __v.begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003203 swap(__v);
3204 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003205 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003206 }
3207 else
3208 __size_ = __sz;
3209}
3210
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003211template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003212void
Howard Hinnant1c936782011-06-03 19:40:40 +00003213vector<bool, _Allocator>::flip() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003214{
3215 // do middle whole words
3216 size_type __n = __size_;
3217 __storage_pointer __p = __begin_;
3218 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3219 *__p = ~*__p;
3220 // do last partial word
3221 if (__n > 0)
3222 {
3223 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3224 __storage_type __b = *__p & __m;
3225 *__p &= ~__m;
3226 *__p |= ~__b & __m;
3227 }
3228}
3229
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003230template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003231bool
3232vector<bool, _Allocator>::__invariants() const
3233{
Howard Hinnant76053d72013-06-27 19:35:32 +00003234 if (this->__begin_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003235 {
3236 if (this->__size_ != 0 || this->__cap() != 0)
3237 return false;
3238 }
3239 else
3240 {
3241 if (this->__cap() == 0)
3242 return false;
3243 if (this->__size_ > this->capacity())
3244 return false;
3245 }
3246 return true;
3247}
3248
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003249template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003250size_t
Howard Hinnant1c936782011-06-03 19:40:40 +00003251vector<bool, _Allocator>::__hash_code() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003252{
3253 size_t __h = 0;
3254 // do middle whole words
3255 size_type __n = __size_;
3256 __storage_pointer __p = __begin_;
3257 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3258 __h ^= *__p;
3259 // do last partial word
3260 if (__n > 0)
3261 {
3262 const __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3263 __h ^= *__p & __m;
3264 }
3265 return __h;
3266}
3267
3268template <class _Allocator>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003269struct _LIBCPP_TEMPLATE_VIS hash<vector<bool, _Allocator> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00003270 : public unary_function<vector<bool, _Allocator>, size_t>
3271{
Howard Hinnant1c265cd2010-09-23 18:58:28 +00003272 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +00003273 size_t operator()(const vector<bool, _Allocator>& __vec) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003274 {return __vec.__hash_code();}
3275};
3276
3277template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003278inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003279bool
3280operator==(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3281{
3282 const typename vector<_Tp, _Allocator>::size_type __sz = __x.size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003283 return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003284}
3285
3286template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003287inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003288bool
3289operator!=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3290{
3291 return !(__x == __y);
3292}
3293
3294template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003295inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003296bool
3297operator< (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3298{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003299 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003300}
3301
3302template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003303inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003304bool
3305operator> (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3306{
3307 return __y < __x;
3308}
3309
3310template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003311inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003312bool
3313operator>=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3314{
3315 return !(__x < __y);
3316}
3317
3318template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003319inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003320bool
3321operator<=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3322{
3323 return !(__y < __x);
3324}
3325
3326template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003327inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003328void
3329swap(vector<_Tp, _Allocator>& __x, vector<_Tp, _Allocator>& __y)
Howard Hinnant1c936782011-06-03 19:40:40 +00003330 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00003331{
3332 __x.swap(__y);
3333}
3334
Marshall Clow29b53f22018-12-14 18:49:35 +00003335#if _LIBCPP_STD_VER > 17
3336template <class _Tp, class _Allocator, class _Up>
Marek Kurdeja98b1412020-05-02 13:58:03 +02003337inline _LIBCPP_INLINE_VISIBILITY typename vector<_Tp, _Allocator>::size_type
3338erase(vector<_Tp, _Allocator>& __c, const _Up& __v) {
3339 auto __old_size = __c.size();
3340 __c.erase(_VSTD::remove(__c.begin(), __c.end(), __v), __c.end());
3341 return __old_size - __c.size();
3342}
Marshall Clow29b53f22018-12-14 18:49:35 +00003343
3344template <class _Tp, class _Allocator, class _Predicate>
Marek Kurdeja98b1412020-05-02 13:58:03 +02003345inline _LIBCPP_INLINE_VISIBILITY typename vector<_Tp, _Allocator>::size_type
3346erase_if(vector<_Tp, _Allocator>& __c, _Predicate __pred) {
3347 auto __old_size = __c.size();
3348 __c.erase(_VSTD::remove_if(__c.begin(), __c.end(), __pred), __c.end());
3349 return __old_size - __c.size();
3350}
Marshall Clow29b53f22018-12-14 18:49:35 +00003351#endif
3352
Howard Hinnantc51e1022010-05-11 19:42:16 +00003353_LIBCPP_END_NAMESPACE_STD
3354
Eric Fiselierf4433a32017-05-31 22:07:49 +00003355_LIBCPP_POP_MACROS
3356
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003357#endif // _LIBCPP_VECTOR