blob: cfd2fc4aff2cf31e3a79ed0c7a077790208bb478 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===------------------------------ vector --------------------------------===//
3//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_VECTOR
11#define _LIBCPP_VECTOR
12
13/*
14 vector synopsis
15
16namespace std
17{
18
Howard Hinnant3b6579a2010-08-22 00:02:43 +000019template <class T, class Allocator = allocator<T> >
Howard Hinnantc51e1022010-05-11 19:42:16 +000020class vector
Howard Hinnant3b6579a2010-08-22 00:02:43 +000021{
22public:
23 typedef T value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +000024 typedef Allocator allocator_type;
25 typedef typename allocator_type::reference reference;
26 typedef typename allocator_type::const_reference const_reference;
27 typedef implementation-defined iterator;
28 typedef implementation-defined const_iterator;
29 typedef typename allocator_type::size_type size_type;
30 typedef typename allocator_type::difference_type difference_type;
31 typedef typename allocator_type::pointer pointer;
32 typedef typename allocator_type::const_pointer const_pointer;
33 typedef std::reverse_iterator<iterator> reverse_iterator;
34 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
35
Howard Hinnant1c936782011-06-03 19:40:40 +000036 vector()
37 noexcept(is_nothrow_default_constructible<allocator_type>::value);
38 explicit vector(const allocator_type&);
Howard Hinnantc51e1022010-05-11 19:42:16 +000039 explicit vector(size_type n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +000040 explicit vector(size_type n, const allocator_type&); // C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +000041 vector(size_type n, const value_type& value, const allocator_type& = allocator_type());
42 template <class InputIterator>
43 vector(InputIterator first, InputIterator last, const allocator_type& = allocator_type());
44 vector(const vector& x);
Howard Hinnant1c936782011-06-03 19:40:40 +000045 vector(vector&& x)
46 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +000047 vector(initializer_list<value_type> il);
48 vector(initializer_list<value_type> il, const allocator_type& a);
49 ~vector();
50 vector& operator=(const vector& x);
Howard Hinnant1c936782011-06-03 19:40:40 +000051 vector& operator=(vector&& x)
52 noexcept(
Marshall Clow2fe8a8d2015-08-18 18:57:00 +000053 allocator_type::propagate_on_container_move_assignment::value ||
54 allocator_type::is_always_equal::value); // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +000055 vector& operator=(initializer_list<value_type> il);
56 template <class InputIterator>
57 void assign(InputIterator first, InputIterator last);
58 void assign(size_type n, const value_type& u);
59 void assign(initializer_list<value_type> il);
60
Howard Hinnant1c936782011-06-03 19:40:40 +000061 allocator_type get_allocator() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000062
Howard Hinnant1c936782011-06-03 19:40:40 +000063 iterator begin() noexcept;
64 const_iterator begin() const noexcept;
65 iterator end() noexcept;
66 const_iterator end() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000067
Howard Hinnant1c936782011-06-03 19:40:40 +000068 reverse_iterator rbegin() noexcept;
69 const_reverse_iterator rbegin() const noexcept;
70 reverse_iterator rend() noexcept;
71 const_reverse_iterator rend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000072
Howard Hinnant1c936782011-06-03 19:40:40 +000073 const_iterator cbegin() const noexcept;
74 const_iterator cend() const noexcept;
75 const_reverse_iterator crbegin() const noexcept;
76 const_reverse_iterator crend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000077
Howard Hinnant1c936782011-06-03 19:40:40 +000078 size_type size() const noexcept;
79 size_type max_size() const noexcept;
80 size_type capacity() const noexcept;
81 bool empty() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000082 void reserve(size_type n);
Howard Hinnant1c936782011-06-03 19:40:40 +000083 void shrink_to_fit() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000084
85 reference operator[](size_type n);
86 const_reference operator[](size_type n) const;
87 reference at(size_type n);
88 const_reference at(size_type n) const;
89
90 reference front();
91 const_reference front() const;
92 reference back();
93 const_reference back() const;
94
Howard Hinnant1c936782011-06-03 19:40:40 +000095 value_type* data() noexcept;
96 const value_type* data() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000097
98 void push_back(const value_type& x);
99 void push_back(value_type&& x);
100 template <class... Args>
Marshall Clowea52cc42017-01-24 23:09:12 +0000101 reference emplace_back(Args&&... args); // reference in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000102 void pop_back();
103
104 template <class... Args> iterator emplace(const_iterator position, Args&&... args);
105 iterator insert(const_iterator position, const value_type& x);
106 iterator insert(const_iterator position, value_type&& x);
107 iterator insert(const_iterator position, size_type n, const value_type& x);
108 template <class InputIterator>
109 iterator insert(const_iterator position, InputIterator first, InputIterator last);
110 iterator insert(const_iterator position, initializer_list<value_type> il);
111
112 iterator erase(const_iterator position);
113 iterator erase(const_iterator first, const_iterator last);
114
Howard Hinnant1c936782011-06-03 19:40:40 +0000115 void clear() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000116
117 void resize(size_type sz);
118 void resize(size_type sz, const value_type& c);
119
Howard Hinnant1c936782011-06-03 19:40:40 +0000120 void swap(vector&)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000121 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
122 allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000123
124 bool __invariants() const;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000125};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000126
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000127template <class Allocator = allocator<T> >
Howard Hinnantc51e1022010-05-11 19:42:16 +0000128class vector<bool, Allocator>
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000129{
130public:
131 typedef bool value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000132 typedef Allocator allocator_type;
133 typedef implementation-defined iterator;
134 typedef implementation-defined const_iterator;
135 typedef typename allocator_type::size_type size_type;
136 typedef typename allocator_type::difference_type difference_type;
137 typedef iterator pointer;
138 typedef const_iterator const_pointer;
139 typedef std::reverse_iterator<iterator> reverse_iterator;
140 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
141
142 class reference
143 {
144 public:
Howard Hinnant1c936782011-06-03 19:40:40 +0000145 reference(const reference&) noexcept;
146 operator bool() const noexcept;
Louis Dionned9f528e2021-06-29 13:52:26 -0400147 reference& operator=(bool x) noexcept;
Howard Hinnant1c936782011-06-03 19:40:40 +0000148 reference& operator=(const reference& x) noexcept;
149 iterator operator&() const noexcept;
150 void flip() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000151 };
152
153 class const_reference
154 {
155 public:
Howard Hinnant1c936782011-06-03 19:40:40 +0000156 const_reference(const reference&) noexcept;
157 operator bool() const noexcept;
158 const_iterator operator&() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000159 };
160
Howard Hinnant1c936782011-06-03 19:40:40 +0000161 vector()
162 noexcept(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnantc2734962011-09-02 20:42:31 +0000163 explicit vector(const allocator_type&);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +0000164 explicit vector(size_type n, const allocator_type& a = allocator_type()); // C++14
165 vector(size_type n, const value_type& value, const allocator_type& = allocator_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000166 template <class InputIterator>
167 vector(InputIterator first, InputIterator last, const allocator_type& = allocator_type());
168 vector(const vector& x);
Howard Hinnant1c936782011-06-03 19:40:40 +0000169 vector(vector&& x)
170 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000171 vector(initializer_list<value_type> il);
172 vector(initializer_list<value_type> il, const allocator_type& a);
173 ~vector();
174 vector& operator=(const vector& x);
Howard Hinnant1c936782011-06-03 19:40:40 +0000175 vector& operator=(vector&& x)
176 noexcept(
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000177 allocator_type::propagate_on_container_move_assignment::value ||
178 allocator_type::is_always_equal::value); // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000179 vector& operator=(initializer_list<value_type> il);
180 template <class InputIterator>
181 void assign(InputIterator first, InputIterator last);
182 void assign(size_type n, const value_type& u);
183 void assign(initializer_list<value_type> il);
184
Howard Hinnant1c936782011-06-03 19:40:40 +0000185 allocator_type get_allocator() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000186
Howard Hinnant1c936782011-06-03 19:40:40 +0000187 iterator begin() noexcept;
188 const_iterator begin() const noexcept;
189 iterator end() noexcept;
190 const_iterator end() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000191
Howard Hinnant1c936782011-06-03 19:40:40 +0000192 reverse_iterator rbegin() noexcept;
193 const_reverse_iterator rbegin() const noexcept;
194 reverse_iterator rend() noexcept;
195 const_reverse_iterator rend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000196
Howard Hinnant1c936782011-06-03 19:40:40 +0000197 const_iterator cbegin() const noexcept;
198 const_iterator cend() const noexcept;
199 const_reverse_iterator crbegin() const noexcept;
200 const_reverse_iterator crend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000201
Howard Hinnant1c936782011-06-03 19:40:40 +0000202 size_type size() const noexcept;
203 size_type max_size() const noexcept;
204 size_type capacity() const noexcept;
205 bool empty() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000206 void reserve(size_type n);
Howard Hinnant1c936782011-06-03 19:40:40 +0000207 void shrink_to_fit() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000208
209 reference operator[](size_type n);
210 const_reference operator[](size_type n) const;
211 reference at(size_type n);
212 const_reference at(size_type n) const;
213
214 reference front();
215 const_reference front() const;
216 reference back();
217 const_reference back() const;
218
219 void push_back(const value_type& x);
Marshall Clowea52cc42017-01-24 23:09:12 +0000220 template <class... Args> reference emplace_back(Args&&... args); // C++14; reference in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000221 void pop_back();
222
Marshall Clowc46bb8e2013-08-13 23:54:12 +0000223 template <class... Args> iterator emplace(const_iterator position, Args&&... args); // C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000224 iterator insert(const_iterator position, const value_type& x);
225 iterator insert(const_iterator position, size_type n, const value_type& x);
226 template <class InputIterator>
227 iterator insert(const_iterator position, InputIterator first, InputIterator last);
228 iterator insert(const_iterator position, initializer_list<value_type> il);
229
230 iterator erase(const_iterator position);
231 iterator erase(const_iterator first, const_iterator last);
232
Howard Hinnant1c936782011-06-03 19:40:40 +0000233 void clear() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000234
235 void resize(size_type sz);
236 void resize(size_type sz, value_type x);
237
Howard Hinnant1c936782011-06-03 19:40:40 +0000238 void swap(vector&)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000239 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
240 allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnant1c936782011-06-03 19:40:40 +0000241 void flip() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000242
243 bool __invariants() const;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000244};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000245
Marshall Clowf0ca1492018-05-21 21:30:12 +0000246template <class InputIterator, class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
247 vector(InputIterator, InputIterator, Allocator = Allocator())
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
274#include <__config>
275#include <__bit_reference>
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 Varlamov53b543c2021-11-09 09:21:02 -0800353 typedef __vector_base<_Tp, _Allocator> __base;
354 typedef allocator<_Tp> __default_allocator_type;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000355public:
Konstantin Varlamov53b543c2021-11-09 09:21:02 -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 __allocator_traits<allocator_type>::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;
Konstantin Varlamov5f23d662021-11-08 00:10:13 -0800369 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)
Howard Hinnant27e0e772011-09-14 18:33:51 +0000376 {
Louis Dionneba400782020-10-02 15:02:52 -0400377#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000378 __get_db()->__insert_c(this);
379#endif
380 }
381 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
Marshall Clow8f282f42015-06-04 00:10:20 +0000382#if _LIBCPP_STD_VER <= 14
383 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
384#else
385 _NOEXCEPT
386#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +0000387 : __base(__a)
388 {
Louis Dionneba400782020-10-02 15:02:52 -0400389#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000390 __get_db()->__insert_c(this);
391#endif
392 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000393 explicit vector(size_type __n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +0000394#if _LIBCPP_STD_VER > 11
395 explicit vector(size_type __n, const allocator_type& __a);
396#endif
Marshall Clowf0ca1492018-05-21 21:30:12 +0000397 vector(size_type __n, const value_type& __x);
398 vector(size_type __n, const value_type& __x, const allocator_type& __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000399 template <class _InputIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +0000400 vector(_InputIterator __first,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500401 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
402 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000403 is_constructible<
404 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +0000405 typename iterator_traits<_InputIterator>::reference>::value,
406 _InputIterator>::type __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000407 template <class _InputIterator>
408 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
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,
413 typename iterator_traits<_InputIterator>::reference>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000414 template <class _ForwardIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +0000415 vector(_ForwardIterator __first,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500416 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000417 is_constructible<
418 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +0000419 typename iterator_traits<_ForwardIterator>::reference>::value,
420 _ForwardIterator>::type __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000421 template <class _ForwardIterator>
422 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500423 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000424 is_constructible<
425 value_type,
426 typename iterator_traits<_ForwardIterator>::reference>::value>::type* = 0);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000427
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000428 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant27e0e772011-09-14 18:33:51 +0000429 ~vector()
430 {
Konstantin Varlamov5f23d662021-11-08 00:10:13 -0800431 __annotate_delete();
Louis Dionneba400782020-10-02 15:02:52 -0400432#if _LIBCPP_DEBUG_LEVEL == 2
Konstantin Varlamov5f23d662021-11-08 00:10:13 -0800433 __get_db()->__erase_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000434#endif
Konstantin Varlamov5f23d662021-11-08 00:10:13 -0800435
436 if (this->__begin_ != nullptr)
437 {
438 __clear();
439 __alloc_traits::deallocate(__alloc(), this->__begin_, capacity());
440 }
Marshall Clow68af4f32018-09-07 15:47:59 +0000441 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000442
443 vector(const vector& __x);
Arthur O'Dwyer9b9662f2021-03-01 17:08:24 -0500444 vector(const vector& __x, const __identity_t<allocator_type>& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000445 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000446 vector& operator=(const vector& __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000447
448#ifndef _LIBCPP_CXX03_LANG
449 _LIBCPP_INLINE_VISIBILITY
450 vector(initializer_list<value_type> __il);
451
452 _LIBCPP_INLINE_VISIBILITY
453 vector(initializer_list<value_type> __il, const allocator_type& __a);
454
Howard Hinnantcf823322010-12-17 14:46:43 +0000455 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000456 vector(vector&& __x)
Marshall Clowe5108202015-07-14 14:46:32 +0000457#if _LIBCPP_STD_VER > 14
458 _NOEXCEPT;
459#else
Howard Hinnant1c936782011-06-03 19:40:40 +0000460 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowe5108202015-07-14 14:46:32 +0000461#endif
Eric Fiseliered9e9362017-04-16 02:40:45 +0000462
Howard Hinnantcf823322010-12-17 14:46:43 +0000463 _LIBCPP_INLINE_VISIBILITY
Arthur O'Dwyer9b9662f2021-03-01 17:08:24 -0500464 vector(vector&& __x, const __identity_t<allocator_type>& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000465 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000466 vector& operator=(vector&& __x)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000467 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiseliered9e9362017-04-16 02:40:45 +0000468
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000469 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000470 vector& operator=(initializer_list<value_type> __il)
471 {assign(__il.begin(), __il.end()); return *this;}
Eric Fiseliered9e9362017-04-16 02:40:45 +0000472
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400473#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000474
475 template <class _InputIterator>
476 typename enable_if
477 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500478 __is_cpp17_input_iterator <_InputIterator>::value &&
479 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000480 is_constructible<
481 value_type,
482 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000483 void
484 >::type
485 assign(_InputIterator __first, _InputIterator __last);
486 template <class _ForwardIterator>
487 typename enable_if
488 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500489 __is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000490 is_constructible<
491 value_type,
492 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000493 void
494 >::type
495 assign(_ForwardIterator __first, _ForwardIterator __last);
496
497 void assign(size_type __n, const_reference __u);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000498
499#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000500 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000501 void assign(initializer_list<value_type> __il)
502 {assign(__il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +0000503#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000504
Howard Hinnant1c936782011-06-03 19:40:40 +0000505 _LIBCPP_INLINE_VISIBILITY
506 allocator_type get_allocator() const _NOEXCEPT
507 {return this->__alloc();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000508
Howard Hinnant1c936782011-06-03 19:40:40 +0000509 _LIBCPP_INLINE_VISIBILITY iterator begin() _NOEXCEPT;
510 _LIBCPP_INLINE_VISIBILITY const_iterator begin() const _NOEXCEPT;
511 _LIBCPP_INLINE_VISIBILITY iterator end() _NOEXCEPT;
512 _LIBCPP_INLINE_VISIBILITY const_iterator end() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000513
Howard Hinnant1c936782011-06-03 19:40:40 +0000514 _LIBCPP_INLINE_VISIBILITY
515 reverse_iterator rbegin() _NOEXCEPT
516 {return reverse_iterator(end());}
517 _LIBCPP_INLINE_VISIBILITY
518 const_reverse_iterator rbegin() const _NOEXCEPT
519 {return const_reverse_iterator(end());}
520 _LIBCPP_INLINE_VISIBILITY
521 reverse_iterator rend() _NOEXCEPT
522 {return reverse_iterator(begin());}
523 _LIBCPP_INLINE_VISIBILITY
524 const_reverse_iterator rend() const _NOEXCEPT
525 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000526
Howard Hinnant1c936782011-06-03 19:40:40 +0000527 _LIBCPP_INLINE_VISIBILITY
528 const_iterator cbegin() const _NOEXCEPT
529 {return begin();}
530 _LIBCPP_INLINE_VISIBILITY
531 const_iterator cend() const _NOEXCEPT
532 {return end();}
533 _LIBCPP_INLINE_VISIBILITY
534 const_reverse_iterator crbegin() const _NOEXCEPT
535 {return rbegin();}
536 _LIBCPP_INLINE_VISIBILITY
537 const_reverse_iterator crend() const _NOEXCEPT
538 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000539
Howard Hinnant1c936782011-06-03 19:40:40 +0000540 _LIBCPP_INLINE_VISIBILITY
541 size_type size() const _NOEXCEPT
542 {return static_cast<size_type>(this->__end_ - this->__begin_);}
543 _LIBCPP_INLINE_VISIBILITY
544 size_type capacity() const _NOEXCEPT
Konstantin Varlamov5f23d662021-11-08 00:10:13 -0800545 {return static_cast<size_type>(__end_cap() - this->__begin_);}
Marshall Clow425f5752017-11-15 05:51:26 +0000546 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000547 bool empty() const _NOEXCEPT
548 {return this->__begin_ == this->__end_;}
549 size_type max_size() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000550 void reserve(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +0000551 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000552
Marshall Clowd6470492019-03-15 00:29:35 +0000553 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) _NOEXCEPT;
554 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000555 reference at(size_type __n);
556 const_reference at(size_type __n) const;
557
Marshall Clowd6470492019-03-15 00:29:35 +0000558 _LIBCPP_INLINE_VISIBILITY reference front() _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000559 {
Kristina Bessonovaaeeaa7e2021-05-09 19:29:56 +0200560 _LIBCPP_ASSERT(!empty(), "front() called on an empty vector");
Howard Hinnant27e0e772011-09-14 18:33:51 +0000561 return *this->__begin_;
562 }
Marshall Clowd6470492019-03-15 00:29:35 +0000563 _LIBCPP_INLINE_VISIBILITY const_reference front() const _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000564 {
Kristina Bessonovaaeeaa7e2021-05-09 19:29:56 +0200565 _LIBCPP_ASSERT(!empty(), "front() called on an empty vector");
Howard Hinnant27e0e772011-09-14 18:33:51 +0000566 return *this->__begin_;
567 }
Marshall Clowd6470492019-03-15 00:29:35 +0000568 _LIBCPP_INLINE_VISIBILITY reference back() _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000569 {
Kristina Bessonovaaeeaa7e2021-05-09 19:29:56 +0200570 _LIBCPP_ASSERT(!empty(), "back() called on an empty vector");
Howard Hinnant27e0e772011-09-14 18:33:51 +0000571 return *(this->__end_ - 1);
572 }
Marshall Clowd6470492019-03-15 00:29:35 +0000573 _LIBCPP_INLINE_VISIBILITY const_reference back() const _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000574 {
Kristina Bessonovaaeeaa7e2021-05-09 19:29:56 +0200575 _LIBCPP_ASSERT(!empty(), "back() called on an empty vector");
Howard Hinnant27e0e772011-09-14 18:33:51 +0000576 return *(this->__end_ - 1);
577 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000578
Howard Hinnant1c936782011-06-03 19:40:40 +0000579 _LIBCPP_INLINE_VISIBILITY
580 value_type* data() _NOEXCEPT
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500581 {return _VSTD::__to_address(this->__begin_);}
Howard Hinnant1c936782011-06-03 19:40:40 +0000582 _LIBCPP_INLINE_VISIBILITY
583 const value_type* data() const _NOEXCEPT
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500584 {return _VSTD::__to_address(this->__begin_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000585
Eric Fiselier96919722017-10-17 13:03:17 +0000586#ifdef _LIBCPP_CXX03_LANG
587 _LIBCPP_INLINE_VISIBILITY
588 void __emplace_back(const value_type& __x) { push_back(__x); }
589#else
590 template <class _Arg>
591 _LIBCPP_INLINE_VISIBILITY
592 void __emplace_back(_Arg&& __arg) {
593 emplace_back(_VSTD::forward<_Arg>(__arg));
594 }
595#endif
596
Howard Hinnantcf823322010-12-17 14:46:43 +0000597 _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000598
599#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000600 _LIBCPP_INLINE_VISIBILITY void push_back(value_type&& __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000601
Howard Hinnantc51e1022010-05-11 19:42:16 +0000602 template <class... _Args>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000603 _LIBCPP_INLINE_VISIBILITY
Marshall Clowea52cc42017-01-24 23:09:12 +0000604#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +0000605 reference emplace_back(_Args&&... __args);
Marshall Clowea52cc42017-01-24 23:09:12 +0000606#else
607 void emplace_back(_Args&&... __args);
608#endif
Eric Fiseliered9e9362017-04-16 02:40:45 +0000609#endif // !_LIBCPP_CXX03_LANG
610
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000611 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000612 void pop_back();
613
614 iterator insert(const_iterator __position, const_reference __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000615
616#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000617 iterator insert(const_iterator __position, value_type&& __x);
618 template <class... _Args>
619 iterator emplace(const_iterator __position, _Args&&... __args);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400620#endif // !_LIBCPP_CXX03_LANG
Eric Fiseliered9e9362017-04-16 02:40:45 +0000621
Howard Hinnantc51e1022010-05-11 19:42:16 +0000622 iterator insert(const_iterator __position, size_type __n, const_reference __x);
623 template <class _InputIterator>
624 typename enable_if
625 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500626 __is_cpp17_input_iterator <_InputIterator>::value &&
627 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000628 is_constructible<
629 value_type,
630 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000631 iterator
632 >::type
633 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
634 template <class _ForwardIterator>
635 typename enable_if
636 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500637 __is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000638 is_constructible<
639 value_type,
640 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000641 iterator
642 >::type
643 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000644
645#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000646 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000647 iterator insert(const_iterator __position, initializer_list<value_type> __il)
648 {return insert(__position, __il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +0000649#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000650
Howard Hinnantcf823322010-12-17 14:46:43 +0000651 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000652 iterator erase(const_iterator __first, const_iterator __last);
653
Howard Hinnant1c936782011-06-03 19:40:40 +0000654 _LIBCPP_INLINE_VISIBILITY
655 void clear() _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000656 {
Marshall Clow2cd9d372014-05-08 14:14:06 +0000657 size_type __old_size = size();
Konstantin Varlamov5f23d662021-11-08 00:10:13 -0800658 __clear();
Marshall Clow2cd9d372014-05-08 14:14:06 +0000659 __annotate_shrink(__old_size);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000660 __invalidate_all_iterators();
661 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000662
663 void resize(size_type __sz);
664 void resize(size_type __sz, const_reference __x);
665
Howard Hinnant1c936782011-06-03 19:40:40 +0000666 void swap(vector&)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000667#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +0000668 _NOEXCEPT;
Marshall Clow8982dcd2015-07-13 20:04:56 +0000669#else
Eric Fiselier873b8d32019-03-18 21:50:12 +0000670 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +0000671 __is_nothrow_swappable<allocator_type>::value);
672#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000673
674 bool __invariants() const;
675
Louis Dionneba400782020-10-02 15:02:52 -0400676#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000677
678 bool __dereferenceable(const const_iterator* __i) const;
679 bool __decrementable(const const_iterator* __i) const;
680 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
681 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
682
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400683#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000684
Howard Hinnantc51e1022010-05-11 19:42:16 +0000685private:
Howard Hinnantcf823322010-12-17 14:46:43 +0000686 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Eric Fiselier69c51982016-12-28 06:06:09 +0000687 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(pointer __new_last);
Marshall Clow3ff48e02018-05-22 16:20:28 +0000688 void __vallocate(size_type __n);
689 void __vdeallocate() _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000690 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
Howard Hinnanta5589382011-01-04 19:53:31 +0000691 void __construct_at_end(size_type __n);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000692 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000693 void __construct_at_end(size_type __n, const_reference __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000694 template <class _ForwardIterator>
695 typename enable_if
696 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500697 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000698 void
699 >::type
Eric Fiselieracfe6f02015-03-31 16:54:19 +0000700 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000701 void __append(size_type __n);
702 void __append(size_type __n, const_reference __x);
Howard Hinnantcf823322010-12-17 14:46:43 +0000703 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000704 iterator __make_iter(pointer __p) _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000705 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000706 const_iterator __make_iter(const_pointer __p) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000707 void __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v);
708 pointer __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p);
709 void __move_range(pointer __from_s, pointer __from_e, pointer __to);
Howard Hinnant1c936782011-06-03 19:40:40 +0000710 void __move_assign(vector& __c, true_type)
711 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000712 void __move_assign(vector& __c, false_type)
713 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000714 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76053d72013-06-27 19:35:32 +0000715 void __destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000716 {
Eric Fiselier69c51982016-12-28 06:06:09 +0000717 __invalidate_iterators_past(__new_last);
Marshall Clow2cd9d372014-05-08 14:14:06 +0000718 size_type __old_size = size();
Konstantin Varlamov5f23d662021-11-08 00:10:13 -0800719 __base_destruct_at_end(__new_last);
Marshall Clow2cd9d372014-05-08 14:14:06 +0000720 __annotate_shrink(__old_size);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000721 }
Eric Fiseliered9e9362017-04-16 02:40:45 +0000722
723#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier7110f442019-03-19 19:19:44 +0000724 template <class _Up>
725 _LIBCPP_INLINE_VISIBILITY
726 inline void __push_back_slow_path(_Up&& __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000727
Howard Hinnantb6c49562012-02-26 15:30:12 +0000728 template <class... _Args>
Eric Fiselier7110f442019-03-19 19:19:44 +0000729 _LIBCPP_INLINE_VISIBILITY
730 inline void __emplace_back_slow_path(_Args&&... __args);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000731#else
Eric Fiselier7110f442019-03-19 19:19:44 +0000732 template <class _Up>
733 _LIBCPP_INLINE_VISIBILITY
734 inline void __push_back_slow_path(_Up& __x);
Howard Hinnantb6c49562012-02-26 15:30:12 +0000735#endif
Eric Fiseliered9e9362017-04-16 02:40:45 +0000736
Marshall Clow2cd9d372014-05-08 14:14:06 +0000737 // The following functions are no-ops outside of AddressSanitizer mode.
738 // We call annotatations only for the default Allocator because other allocators
739 // may not meet the AddressSanitizer alignment constraints.
740 // See the documentation for __sanitizer_annotate_contiguous_container for more details.
Marshall Clow2cd9d372014-05-08 14:14:06 +0000741#ifndef _LIBCPP_HAS_NO_ASAN
Eric Fiselier6003c772016-12-23 23:37:52 +0000742 void __annotate_contiguous_container(const void *__beg, const void *__end,
743 const void *__old_mid,
744 const void *__new_mid) const
745 {
746
Marshall Clow2cd9d372014-05-08 14:14:06 +0000747 if (__beg && is_same<allocator_type, __default_allocator_type>::value)
748 __sanitizer_annotate_contiguous_container(__beg, __end, __old_mid, __new_mid);
Marshall Clow2cd9d372014-05-08 14:14:06 +0000749 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000750#else
751 _LIBCPP_INLINE_VISIBILITY
752 void __annotate_contiguous_container(const void*, const void*, const void*,
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000753 const void*) const _NOEXCEPT {}
Eric Fiselier6003c772016-12-23 23:37:52 +0000754#endif
755 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000756 void __annotate_new(size_type __current_size) const _NOEXCEPT {
Marshall Clow2cd9d372014-05-08 14:14:06 +0000757 __annotate_contiguous_container(data(), data() + capacity(),
758 data() + capacity(), data() + __current_size);
759 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000760
761 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000762 void __annotate_delete() const _NOEXCEPT {
Marshall Clow2cd9d372014-05-08 14:14:06 +0000763 __annotate_contiguous_container(data(), data() + capacity(),
764 data() + size(), data() + capacity());
765 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000766
767 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000768 void __annotate_increase(size_type __n) const _NOEXCEPT
Marshall Clow2cd9d372014-05-08 14:14:06 +0000769 {
770 __annotate_contiguous_container(data(), data() + capacity(),
771 data() + size(), data() + size() + __n);
772 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000773
774 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000775 void __annotate_shrink(size_type __old_size) const _NOEXCEPT
Marshall Clow2cd9d372014-05-08 14:14:06 +0000776 {
777 __annotate_contiguous_container(data(), data() + capacity(),
778 data() + __old_size, data() + size());
779 }
Marshall Clowc78ffa52014-09-03 21:37:43 +0000780
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000781 struct _ConstructTransaction {
782 explicit _ConstructTransaction(vector &__v, size_type __n)
Mark de Weverdcafc462021-10-21 18:29:14 +0200783 : __v_(__v), __pos_(__v.__end_), __new_end_(__v.__end_ + __n) {
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000784#ifndef _LIBCPP_HAS_NO_ASAN
785 __v_.__annotate_increase(__n);
786#endif
787 }
788 ~_ConstructTransaction() {
789 __v_.__end_ = __pos_;
790#ifndef _LIBCPP_HAS_NO_ASAN
791 if (__pos_ != __new_end_) {
792 __v_.__annotate_shrink(__new_end_ - __v_.__begin_);
793 }
794#endif
795 }
796
797 vector &__v_;
798 pointer __pos_;
799 const_pointer const __new_end_;
800
801 private:
802 _ConstructTransaction(_ConstructTransaction const&) = delete;
803 _ConstructTransaction& operator=(_ConstructTransaction const&) = delete;
804 };
805
806 template <class ..._Args>
807 _LIBCPP_INLINE_VISIBILITY
808 void __construct_one_at_end(_Args&& ...__args) {
809 _ConstructTransaction __tx(*this, 1);
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500810 __alloc_traits::construct(this->__alloc(), _VSTD::__to_address(__tx.__pos_),
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000811 _VSTD::forward<_Args>(__args)...);
812 ++__tx.__pos_;
813 }
Konstantin Varlamov5f23d662021-11-08 00:10:13 -0800814
815 _LIBCPP_INLINE_VISIBILITY
816 allocator_type& __alloc() _NOEXCEPT
817 {return this->__end_cap_.second();}
818 _LIBCPP_INLINE_VISIBILITY
819 const allocator_type& __alloc() const _NOEXCEPT
820 {return this->__end_cap_.second();}
821 _LIBCPP_INLINE_VISIBILITY
822 pointer& __end_cap() _NOEXCEPT
823 {return this->__end_cap_.first();}
824 _LIBCPP_INLINE_VISIBILITY
825 const pointer& __end_cap() const _NOEXCEPT
826 {return this->__end_cap_.first();}
827
828 _LIBCPP_INLINE_VISIBILITY
829 void __clear() _NOEXCEPT {__base_destruct_at_end(this->__begin_);}
830
831 _LIBCPP_INLINE_VISIBILITY
832 void __base_destruct_at_end(pointer __new_last) _NOEXCEPT {
833 pointer __soon_to_be_end = this->__end_;
834 while (__new_last != __soon_to_be_end)
835 __alloc_traits::destroy(__alloc(), _VSTD::__to_address(--__soon_to_be_end));
836 this->__end_ = __new_last;
837 }
838
839 _LIBCPP_INLINE_VISIBILITY
840 void __copy_assign_alloc(const vector& __c)
841 {__copy_assign_alloc(__c, integral_constant<bool,
842 __alloc_traits::propagate_on_container_copy_assignment::value>());}
843
844 _LIBCPP_INLINE_VISIBILITY
845 void __move_assign_alloc(vector& __c)
846 _NOEXCEPT_(
847 !__alloc_traits::propagate_on_container_move_assignment::value ||
848 is_nothrow_move_assignable<allocator_type>::value)
849 {__move_assign_alloc(__c, integral_constant<bool,
850 __alloc_traits::propagate_on_container_move_assignment::value>());}
851
852 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
853 void __throw_length_error() const {
854#ifndef _LIBCPP_NO_EXCEPTIONS
855 __vector_base_common<true>::__throw_length_error();
856#else
857 _VSTD::abort();
858#endif
859 }
860
861 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
862 void __throw_out_of_range() const {
863#ifndef _LIBCPP_NO_EXCEPTIONS
864 __vector_base_common<true>::__throw_out_of_range();
865#else
866 _VSTD::abort();
867#endif
868 }
869
870 _LIBCPP_INLINE_VISIBILITY
871 void __copy_assign_alloc(const vector& __c, true_type)
872 {
873 if (__alloc() != __c.__alloc())
874 {
875 __clear();
876 __alloc_traits::deallocate(__alloc(), this->__begin_, capacity());
877 this->__begin_ = this->__end_ = __end_cap() = nullptr;
878 }
879 __alloc() = __c.__alloc();
880 }
881
882 _LIBCPP_INLINE_VISIBILITY
883 void __copy_assign_alloc(const vector&, false_type)
884 {}
885
886 _LIBCPP_INLINE_VISIBILITY
887 void __move_assign_alloc(vector& __c, true_type)
888 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
889 {
890 __alloc() = _VSTD::move(__c.__alloc());
891 }
892
893 _LIBCPP_INLINE_VISIBILITY
894 void __move_assign_alloc(vector&, false_type)
895 _NOEXCEPT
896 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000897};
898
Louis Dionned59f8a52021-08-17 11:59:07 -0400899#if _LIBCPP_STD_VER >= 17
Marshall Clowf0ca1492018-05-21 21:30:12 +0000900template<class _InputIterator,
Arthur O'Dwyer56226762021-03-03 23:02:20 -0500901 class _Alloc = allocator<__iter_value_type<_InputIterator>>,
Konstantin Varlamov53b543c2021-11-09 09:21:02 -0800902 class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
Louis Dionne25547162021-08-17 12:26:09 -0400903 class = enable_if_t<__is_allocator<_Alloc>::value>
Marshall Clowf0ca1492018-05-21 21:30:12 +0000904 >
905vector(_InputIterator, _InputIterator)
Arthur O'Dwyer56226762021-03-03 23:02:20 -0500906 -> vector<__iter_value_type<_InputIterator>, _Alloc>;
Marshall Clowf0ca1492018-05-21 21:30:12 +0000907
908template<class _InputIterator,
909 class _Alloc,
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, _Alloc)
Arthur O'Dwyer56226762021-03-03 23:02:20 -0500914 -> vector<__iter_value_type<_InputIterator>, _Alloc>;
Marshall Clowf0ca1492018-05-21 21:30:12 +0000915#endif
916
Howard Hinnantc51e1022010-05-11 19:42:16 +0000917template <class _Tp, class _Allocator>
918void
919vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v)
920{
Eric Fiselier909fe962019-09-13 16:09:33 +0000921
Marshall Clow2cd9d372014-05-08 14:14:06 +0000922 __annotate_delete();
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -0500923 _VSTD::__construct_backward_with_exception_guarantees(this->__alloc(), this->__begin_, this->__end_, __v.__begin_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000924 _VSTD::swap(this->__begin_, __v.__begin_);
925 _VSTD::swap(this->__end_, __v.__end_);
926 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000927 __v.__first_ = __v.__begin_;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000928 __annotate_new(size());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000929 __invalidate_all_iterators();
930}
931
932template <class _Tp, class _Allocator>
933typename vector<_Tp, _Allocator>::pointer
934vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p)
935{
Marshall Clow2cd9d372014-05-08 14:14:06 +0000936 __annotate_delete();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000937 pointer __r = __v.__begin_;
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -0500938 _VSTD::__construct_backward_with_exception_guarantees(this->__alloc(), this->__begin_, __p, __v.__begin_);
939 _VSTD::__construct_forward_with_exception_guarantees(this->__alloc(), __p, this->__end_, __v.__end_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000940 _VSTD::swap(this->__begin_, __v.__begin_);
941 _VSTD::swap(this->__end_, __v.__end_);
942 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000943 __v.__first_ = __v.__begin_;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000944 __annotate_new(size());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000945 __invalidate_all_iterators();
946 return __r;
947}
948
949// Allocate space for __n objects
950// throws length_error if __n > max_size()
951// throws (probably bad_alloc) if memory run out
952// Precondition: __begin_ == __end_ == __end_cap() == 0
953// Precondition: __n > 0
954// Postcondition: capacity() == __n
955// Postcondition: size() == 0
956template <class _Tp, class _Allocator>
957void
Marshall Clow3ff48e02018-05-22 16:20:28 +0000958vector<_Tp, _Allocator>::__vallocate(size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000959{
960 if (__n > max_size())
961 this->__throw_length_error();
962 this->__begin_ = this->__end_ = __alloc_traits::allocate(this->__alloc(), __n);
963 this->__end_cap() = this->__begin_ + __n;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000964 __annotate_new(0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000965}
966
967template <class _Tp, class _Allocator>
968void
Marshall Clow3ff48e02018-05-22 16:20:28 +0000969vector<_Tp, _Allocator>::__vdeallocate() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000970{
Howard Hinnant76053d72013-06-27 19:35:32 +0000971 if (this->__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000972 {
973 clear();
974 __alloc_traits::deallocate(this->__alloc(), this->__begin_, capacity());
Howard Hinnant76053d72013-06-27 19:35:32 +0000975 this->__begin_ = this->__end_ = this->__end_cap() = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000976 }
977}
978
979template <class _Tp, class _Allocator>
980typename vector<_Tp, _Allocator>::size_type
Howard Hinnant1c936782011-06-03 19:40:40 +0000981vector<_Tp, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000982{
Eric Fiselierb5d9f442016-11-23 01:18:56 +0000983 return _VSTD::min<size_type>(__alloc_traits::max_size(this->__alloc()),
984 numeric_limits<difference_type>::max());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000985}
986
987// Precondition: __new_size > capacity()
988template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000989inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000990typename vector<_Tp, _Allocator>::size_type
991vector<_Tp, _Allocator>::__recommend(size_type __new_size) const
992{
993 const size_type __ms = max_size();
994 if (__new_size > __ms)
995 this->__throw_length_error();
996 const size_type __cap = capacity();
997 if (__cap >= __ms / 2)
998 return __ms;
Arthur O'Dwyerc0fb14b2021-07-26 18:23:00 -0400999 return _VSTD::max<size_type>(2 * __cap, __new_size);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001000}
1001
1002// Default constructs __n objects starting at __end_
1003// throws if construction throws
1004// Precondition: __n > 0
1005// Precondition: size() + __n <= capacity()
1006// Postcondition: size() == size() + __n
1007template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001008void
1009vector<_Tp, _Allocator>::__construct_at_end(size_type __n)
1010{
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001011 _ConstructTransaction __tx(*this, __n);
Martijn Vels1e2e1b82020-06-18 13:14:02 -04001012 const_pointer __new_end = __tx.__new_end_;
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04001013 for (pointer __pos = __tx.__pos_; __pos != __new_end; __tx.__pos_ = ++__pos) {
Martijn Vels1e2e1b82020-06-18 13:14:02 -04001014 __alloc_traits::construct(this->__alloc(), _VSTD::__to_address(__pos));
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001015 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001016}
1017
Howard Hinnantc51e1022010-05-11 19:42:16 +00001018// Copy constructs __n objects starting at __end_ from __x
1019// throws if construction throws
1020// Precondition: __n > 0
1021// Precondition: size() + __n <= capacity()
1022// Postcondition: size() == old size() + __n
1023// Postcondition: [i] == __x for all i in [size() - __n, __n)
1024template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001025inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001026void
1027vector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
1028{
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001029 _ConstructTransaction __tx(*this, __n);
Martijn Vels1e2e1b82020-06-18 13:14:02 -04001030 const_pointer __new_end = __tx.__new_end_;
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04001031 for (pointer __pos = __tx.__pos_; __pos != __new_end; __tx.__pos_ = ++__pos) {
Martijn Vels1e2e1b82020-06-18 13:14:02 -04001032 __alloc_traits::construct(this->__alloc(), _VSTD::__to_address(__pos), __x);
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001033 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001034}
1035
1036template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001037template <class _ForwardIterator>
1038typename enable_if
1039<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001040 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001041 void
1042>::type
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001043vector<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001044{
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001045 _ConstructTransaction __tx(*this, __n);
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05001046 _VSTD::__construct_range_forward(this->__alloc(), __first, __last, __tx.__pos_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001047}
1048
1049// Default constructs __n objects starting at __end_
1050// throws if construction throws
1051// Postcondition: size() == size() + __n
Howard Hinnant1c936782011-06-03 19:40:40 +00001052// Exception safety: strong.
Howard Hinnantc51e1022010-05-11 19:42:16 +00001053template <class _Tp, class _Allocator>
1054void
1055vector<_Tp, _Allocator>::__append(size_type __n)
1056{
1057 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1058 this->__construct_at_end(__n);
1059 else
1060 {
1061 allocator_type& __a = this->__alloc();
1062 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1063 __v.__construct_at_end(__n);
1064 __swap_out_circular_buffer(__v);
1065 }
1066}
1067
1068// Default constructs __n objects starting at __end_
1069// throws if construction throws
1070// Postcondition: size() == size() + __n
Howard Hinnant1c936782011-06-03 19:40:40 +00001071// Exception safety: strong.
Howard Hinnantc51e1022010-05-11 19:42:16 +00001072template <class _Tp, class _Allocator>
1073void
1074vector<_Tp, _Allocator>::__append(size_type __n, const_reference __x)
1075{
1076 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1077 this->__construct_at_end(__n, __x);
1078 else
1079 {
1080 allocator_type& __a = this->__alloc();
1081 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1082 __v.__construct_at_end(__n, __x);
1083 __swap_out_circular_buffer(__v);
1084 }
1085}
1086
1087template <class _Tp, class _Allocator>
1088vector<_Tp, _Allocator>::vector(size_type __n)
1089{
Louis Dionneba400782020-10-02 15:02:52 -04001090#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001091 __get_db()->__insert_c(this);
1092#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001093 if (__n > 0)
1094 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001095 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001096 __construct_at_end(__n);
1097 }
1098}
1099
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00001100#if _LIBCPP_STD_VER > 11
1101template <class _Tp, class _Allocator>
1102vector<_Tp, _Allocator>::vector(size_type __n, const allocator_type& __a)
1103 : __base(__a)
1104{
Louis Dionneba400782020-10-02 15:02:52 -04001105#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00001106 __get_db()->__insert_c(this);
1107#endif
1108 if (__n > 0)
1109 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001110 __vallocate(__n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00001111 __construct_at_end(__n);
1112 }
1113}
1114#endif
1115
Howard Hinnantc51e1022010-05-11 19:42:16 +00001116template <class _Tp, class _Allocator>
Marshall Clowf0ca1492018-05-21 21:30:12 +00001117vector<_Tp, _Allocator>::vector(size_type __n, const value_type& __x)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001118{
Louis Dionneba400782020-10-02 15:02:52 -04001119#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001120 __get_db()->__insert_c(this);
1121#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001122 if (__n > 0)
1123 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001124 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001125 __construct_at_end(__n, __x);
1126 }
1127}
1128
1129template <class _Tp, class _Allocator>
Marshall Clowf0ca1492018-05-21 21:30:12 +00001130vector<_Tp, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001131 : __base(__a)
1132{
Louis Dionneba400782020-10-02 15:02:52 -04001133#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001134 __get_db()->__insert_c(this);
1135#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001136 if (__n > 0)
1137 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001138 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001139 __construct_at_end(__n, __x);
1140 }
1141}
1142
1143template <class _Tp, class _Allocator>
1144template <class _InputIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +00001145vector<_Tp, _Allocator>::vector(_InputIterator __first,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001146 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
1147 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001148 is_constructible<
1149 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +00001150 typename iterator_traits<_InputIterator>::reference>::value,
1151 _InputIterator>::type __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001152{
Louis Dionneba400782020-10-02 15:02:52 -04001153#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001154 __get_db()->__insert_c(this);
1155#endif
Howard Hinnant9cd22302011-09-16 18:41:29 +00001156 for (; __first != __last; ++__first)
Eric Fiselier96919722017-10-17 13:03:17 +00001157 __emplace_back(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001158}
1159
1160template <class _Tp, class _Allocator>
1161template <class _InputIterator>
1162vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001163 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
1164 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001165 is_constructible<
1166 value_type,
1167 typename iterator_traits<_InputIterator>::reference>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001168 : __base(__a)
1169{
Louis Dionneba400782020-10-02 15:02:52 -04001170#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001171 __get_db()->__insert_c(this);
1172#endif
Howard Hinnant9cd22302011-09-16 18:41:29 +00001173 for (; __first != __last; ++__first)
Eric Fiselier96919722017-10-17 13:03:17 +00001174 __emplace_back(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001175}
1176
1177template <class _Tp, class _Allocator>
1178template <class _ForwardIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +00001179vector<_Tp, _Allocator>::vector(_ForwardIterator __first,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001180 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001181 is_constructible<
1182 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +00001183 typename iterator_traits<_ForwardIterator>::reference>::value,
1184 _ForwardIterator>::type __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001185{
Louis Dionneba400782020-10-02 15:02:52 -04001186#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001187 __get_db()->__insert_c(this);
1188#endif
Howard 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>
1198template <class _ForwardIterator>
1199vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001200 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001201 is_constructible<
1202 value_type,
1203 typename iterator_traits<_ForwardIterator>::reference>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001204 : __base(__a)
1205{
Louis Dionneba400782020-10-02 15:02:52 -04001206#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001207 __get_db()->__insert_c(this);
1208#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001209 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001210 if (__n > 0)
1211 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001212 __vallocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001213 __construct_at_end(__first, __last, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001214 }
1215}
1216
1217template <class _Tp, class _Allocator>
1218vector<_Tp, _Allocator>::vector(const vector& __x)
1219 : __base(__alloc_traits::select_on_container_copy_construction(__x.__alloc()))
1220{
Louis Dionneba400782020-10-02 15:02:52 -04001221#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001222 __get_db()->__insert_c(this);
1223#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001224 size_type __n = __x.size();
1225 if (__n > 0)
1226 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001227 __vallocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001228 __construct_at_end(__x.__begin_, __x.__end_, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001229 }
1230}
1231
1232template <class _Tp, class _Allocator>
Arthur O'Dwyer9b9662f2021-03-01 17:08:24 -05001233vector<_Tp, _Allocator>::vector(const vector& __x, const __identity_t<allocator_type>& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001234 : __base(__a)
1235{
Louis Dionneba400782020-10-02 15:02:52 -04001236#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001237 __get_db()->__insert_c(this);
1238#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001239 size_type __n = __x.size();
1240 if (__n > 0)
1241 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001242 __vallocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001243 __construct_at_end(__x.__begin_, __x.__end_, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001244 }
1245}
1246
Eric Fiseliered9e9362017-04-16 02:40:45 +00001247#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001248
1249template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001250inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001251vector<_Tp, _Allocator>::vector(vector&& __x)
Marshall Clowe5108202015-07-14 14:46:32 +00001252#if _LIBCPP_STD_VER > 14
1253 _NOEXCEPT
1254#else
Howard Hinnant1c936782011-06-03 19:40:40 +00001255 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowe5108202015-07-14 14:46:32 +00001256#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001257 : __base(_VSTD::move(__x.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001258{
Louis Dionneba400782020-10-02 15:02:52 -04001259#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001260 __get_db()->__insert_c(this);
Mark de Wever8f72c892021-10-10 15:40:50 +02001261 __get_db()->swap(this, _VSTD::addressof(__x));
Howard Hinnant27e0e772011-09-14 18:33:51 +00001262#endif
Howard Hinnant9cd22302011-09-16 18:41:29 +00001263 this->__begin_ = __x.__begin_;
1264 this->__end_ = __x.__end_;
1265 this->__end_cap() = __x.__end_cap();
Howard Hinnant76053d72013-06-27 19:35:32 +00001266 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001267}
1268
1269template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001270inline _LIBCPP_INLINE_VISIBILITY
Arthur O'Dwyer9b9662f2021-03-01 17:08:24 -05001271vector<_Tp, _Allocator>::vector(vector&& __x, const __identity_t<allocator_type>& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001272 : __base(__a)
1273{
Louis Dionneba400782020-10-02 15:02:52 -04001274#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001275 __get_db()->__insert_c(this);
1276#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001277 if (__a == __x.__alloc())
1278 {
1279 this->__begin_ = __x.__begin_;
1280 this->__end_ = __x.__end_;
1281 this->__end_cap() = __x.__end_cap();
1282 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Louis Dionneba400782020-10-02 15:02:52 -04001283#if _LIBCPP_DEBUG_LEVEL == 2
Mark de Wever8f72c892021-10-10 15:40:50 +02001284 __get_db()->swap(this, _VSTD::addressof(__x));
Howard Hinnanta1e60cd2011-09-19 16:34:29 +00001285#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001286 }
1287 else
1288 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001289 typedef move_iterator<iterator> _Ip;
1290 assign(_Ip(__x.begin()), _Ip(__x.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001291 }
1292}
1293
1294template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001295inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001296vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il)
1297{
Louis Dionneba400782020-10-02 15:02:52 -04001298#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001299 __get_db()->__insert_c(this);
1300#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001301 if (__il.size() > 0)
1302 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001303 __vallocate(__il.size());
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001304 __construct_at_end(__il.begin(), __il.end(), __il.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001305 }
1306}
1307
1308template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001309inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001310vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
1311 : __base(__a)
1312{
Louis Dionneba400782020-10-02 15:02:52 -04001313#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001314 __get_db()->__insert_c(this);
1315#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001316 if (__il.size() > 0)
1317 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001318 __vallocate(__il.size());
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001319 __construct_at_end(__il.begin(), __il.end(), __il.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001320 }
1321}
1322
1323template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001324inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001325vector<_Tp, _Allocator>&
1326vector<_Tp, _Allocator>::operator=(vector&& __x)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001327 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001328{
1329 __move_assign(__x, integral_constant<bool,
1330 __alloc_traits::propagate_on_container_move_assignment::value>());
1331 return *this;
1332}
1333
1334template <class _Tp, class _Allocator>
1335void
1336vector<_Tp, _Allocator>::__move_assign(vector& __c, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001337 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001338{
Konstantin Varlamov5f23d662021-11-08 00:10:13 -08001339 if (__alloc() != __c.__alloc())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001340 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001341 typedef move_iterator<iterator> _Ip;
1342 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001343 }
1344 else
1345 __move_assign(__c, true_type());
1346}
1347
1348template <class _Tp, class _Allocator>
1349void
1350vector<_Tp, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00001351 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001352{
Marshall Clow3ff48e02018-05-22 16:20:28 +00001353 __vdeallocate();
Konstantin Varlamov5f23d662021-11-08 00:10:13 -08001354 __move_assign_alloc(__c); // this can throw
Howard Hinnantc51e1022010-05-11 19:42:16 +00001355 this->__begin_ = __c.__begin_;
1356 this->__end_ = __c.__end_;
1357 this->__end_cap() = __c.__end_cap();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001358 __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
Louis Dionneba400782020-10-02 15:02:52 -04001359#if _LIBCPP_DEBUG_LEVEL == 2
Mark de Wever8f72c892021-10-10 15:40:50 +02001360 __get_db()->swap(this, _VSTD::addressof(__c));
Howard Hinnanta1e60cd2011-09-19 16:34:29 +00001361#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001362}
1363
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001364#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001365
1366template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001367inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001368vector<_Tp, _Allocator>&
1369vector<_Tp, _Allocator>::operator=(const vector& __x)
1370{
Mark de Wever357a1fc2021-09-28 19:15:18 +02001371 if (this != _VSTD::addressof(__x))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001372 {
Konstantin Varlamov5f23d662021-11-08 00:10:13 -08001373 __copy_assign_alloc(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001374 assign(__x.__begin_, __x.__end_);
1375 }
1376 return *this;
1377}
1378
1379template <class _Tp, class _Allocator>
1380template <class _InputIterator>
1381typename enable_if
1382<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001383 __is_cpp17_input_iterator <_InputIterator>::value &&
1384 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001385 is_constructible<
1386 _Tp,
1387 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001388 void
1389>::type
1390vector<_Tp, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
1391{
1392 clear();
1393 for (; __first != __last; ++__first)
Eric Fiselier96919722017-10-17 13:03:17 +00001394 __emplace_back(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001395}
1396
1397template <class _Tp, class _Allocator>
1398template <class _ForwardIterator>
1399typename enable_if
1400<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001401 __is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001402 is_constructible<
1403 _Tp,
1404 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001405 void
1406>::type
1407vector<_Tp, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
1408{
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001409 size_type __new_size = static_cast<size_type>(_VSTD::distance(__first, __last));
1410 if (__new_size <= capacity())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001411 {
1412 _ForwardIterator __mid = __last;
1413 bool __growing = false;
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001414 if (__new_size > size())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001415 {
1416 __growing = true;
1417 __mid = __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001418 _VSTD::advance(__mid, size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001419 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001420 pointer __m = _VSTD::copy(__first, __mid, this->__begin_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001421 if (__growing)
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001422 __construct_at_end(__mid, __last, __new_size - size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001423 else
1424 this->__destruct_at_end(__m);
1425 }
1426 else
1427 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001428 __vdeallocate();
1429 __vallocate(__recommend(__new_size));
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001430 __construct_at_end(__first, __last, __new_size);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001431 }
Eric Fiselier69c51982016-12-28 06:06:09 +00001432 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001433}
1434
1435template <class _Tp, class _Allocator>
1436void
1437vector<_Tp, _Allocator>::assign(size_type __n, const_reference __u)
1438{
1439 if (__n <= capacity())
1440 {
1441 size_type __s = size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001442 _VSTD::fill_n(this->__begin_, _VSTD::min(__n, __s), __u);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001443 if (__n > __s)
1444 __construct_at_end(__n - __s, __u);
1445 else
Howard Hinnant155c2af2010-05-24 17:49:41 +00001446 this->__destruct_at_end(this->__begin_ + __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001447 }
1448 else
1449 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001450 __vdeallocate();
1451 __vallocate(__recommend(static_cast<size_type>(__n)));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001452 __construct_at_end(__n, __u);
1453 }
Eric Fiselier69c51982016-12-28 06:06:09 +00001454 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001455}
1456
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001457template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001458inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001459typename vector<_Tp, _Allocator>::iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001460vector<_Tp, _Allocator>::__make_iter(pointer __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001461{
Louis Dionneba400782020-10-02 15:02:52 -04001462#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001463 return iterator(this, __p);
1464#else
1465 return iterator(__p);
1466#endif
1467}
1468
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001469template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001470inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001471typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001472vector<_Tp, _Allocator>::__make_iter(const_pointer __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001473{
Louis Dionneba400782020-10-02 15:02:52 -04001474#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001475 return const_iterator(this, __p);
1476#else
1477 return const_iterator(__p);
1478#endif
1479}
1480
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001481template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001482inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001483typename vector<_Tp, _Allocator>::iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001484vector<_Tp, _Allocator>::begin() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001485{
1486 return __make_iter(this->__begin_);
1487}
1488
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001489template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001490inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001491typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001492vector<_Tp, _Allocator>::begin() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001493{
1494 return __make_iter(this->__begin_);
1495}
1496
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001497template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001498inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001499typename vector<_Tp, _Allocator>::iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001500vector<_Tp, _Allocator>::end() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001501{
1502 return __make_iter(this->__end_);
1503}
1504
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001505template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001506inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001507typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001508vector<_Tp, _Allocator>::end() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001509{
1510 return __make_iter(this->__end_);
1511}
1512
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001513template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001514inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001515typename vector<_Tp, _Allocator>::reference
Marshall Clowd6470492019-03-15 00:29:35 +00001516vector<_Tp, _Allocator>::operator[](size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001517{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001518 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001519 return this->__begin_[__n];
1520}
1521
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001522template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001523inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001524typename vector<_Tp, _Allocator>::const_reference
Marshall Clowd6470492019-03-15 00:29:35 +00001525vector<_Tp, _Allocator>::operator[](size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001526{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001527 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001528 return this->__begin_[__n];
1529}
1530
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001531template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001532typename vector<_Tp, _Allocator>::reference
1533vector<_Tp, _Allocator>::at(size_type __n)
1534{
1535 if (__n >= size())
1536 this->__throw_out_of_range();
1537 return this->__begin_[__n];
1538}
1539
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001540template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001541typename vector<_Tp, _Allocator>::const_reference
1542vector<_Tp, _Allocator>::at(size_type __n) const
1543{
1544 if (__n >= size())
1545 this->__throw_out_of_range();
1546 return this->__begin_[__n];
1547}
1548
1549template <class _Tp, class _Allocator>
1550void
1551vector<_Tp, _Allocator>::reserve(size_type __n)
1552{
1553 if (__n > capacity())
1554 {
Mikhail Maltsev0b0a51d2021-10-21 10:40:05 +01001555 if (__n > max_size())
1556 this->__throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001557 allocator_type& __a = this->__alloc();
Howard Hinnant1c936782011-06-03 19:40:40 +00001558 __split_buffer<value_type, allocator_type&> __v(__n, size(), __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001559 __swap_out_circular_buffer(__v);
1560 }
1561}
1562
1563template <class _Tp, class _Allocator>
1564void
Howard Hinnant1c936782011-06-03 19:40:40 +00001565vector<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001566{
1567 if (capacity() > size())
1568 {
1569#ifndef _LIBCPP_NO_EXCEPTIONS
1570 try
1571 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001572#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001573 allocator_type& __a = this->__alloc();
Howard Hinnant1c936782011-06-03 19:40:40 +00001574 __split_buffer<value_type, allocator_type&> __v(size(), size(), __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001575 __swap_out_circular_buffer(__v);
1576#ifndef _LIBCPP_NO_EXCEPTIONS
1577 }
1578 catch (...)
1579 {
1580 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001581#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001582 }
1583}
1584
1585template <class _Tp, class _Allocator>
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001586template <class _Up>
1587void
Eric Fiseliered9e9362017-04-16 02:40:45 +00001588#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001589vector<_Tp, _Allocator>::__push_back_slow_path(_Up&& __x)
1590#else
1591vector<_Tp, _Allocator>::__push_back_slow_path(_Up& __x)
1592#endif
1593{
1594 allocator_type& __a = this->__alloc();
1595 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1596 // __v.push_back(_VSTD::forward<_Up>(__x));
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001597 __alloc_traits::construct(__a, _VSTD::__to_address(__v.__end_), _VSTD::forward<_Up>(__x));
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001598 __v.__end_++;
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001599 __swap_out_circular_buffer(__v);
1600}
1601
1602template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001603inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001604void
1605vector<_Tp, _Allocator>::push_back(const_reference __x)
1606{
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001607 if (this->__end_ != this->__end_cap())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001608 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001609 __construct_one_at_end(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001610 }
1611 else
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001612 __push_back_slow_path(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001613}
1614
Eric Fiseliered9e9362017-04-16 02:40:45 +00001615#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001616
1617template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001618inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001619void
1620vector<_Tp, _Allocator>::push_back(value_type&& __x)
1621{
1622 if (this->__end_ < this->__end_cap())
1623 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001624 __construct_one_at_end(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001625 }
1626 else
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001627 __push_back_slow_path(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001628}
1629
1630template <class _Tp, class _Allocator>
1631template <class... _Args>
1632void
Howard Hinnantb6c49562012-02-26 15:30:12 +00001633vector<_Tp, _Allocator>::__emplace_back_slow_path(_Args&&... __args)
1634{
1635 allocator_type& __a = this->__alloc();
1636 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1637// __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001638 __alloc_traits::construct(__a, _VSTD::__to_address(__v.__end_), _VSTD::forward<_Args>(__args)...);
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001639 __v.__end_++;
Howard Hinnantb6c49562012-02-26 15:30:12 +00001640 __swap_out_circular_buffer(__v);
1641}
1642
1643template <class _Tp, class _Allocator>
1644template <class... _Args>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001645inline
Marshall Clowea52cc42017-01-24 23:09:12 +00001646#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001647typename vector<_Tp, _Allocator>::reference
Marshall Clowea52cc42017-01-24 23:09:12 +00001648#else
1649void
1650#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001651vector<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1652{
1653 if (this->__end_ < this->__end_cap())
1654 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001655 __construct_one_at_end(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001656 }
1657 else
Howard Hinnantb6c49562012-02-26 15:30:12 +00001658 __emplace_back_slow_path(_VSTD::forward<_Args>(__args)...);
Marshall Clowea52cc42017-01-24 23:09:12 +00001659#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001660 return this->back();
Marshall Clowea52cc42017-01-24 23:09:12 +00001661#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001662}
1663
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001664#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001665
1666template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001667inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001668void
1669vector<_Tp, _Allocator>::pop_back()
1670{
Kristina Bessonovaaeeaa7e2021-05-09 19:29:56 +02001671 _LIBCPP_ASSERT(!empty(), "vector::pop_back called on an empty vector");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001672 this->__destruct_at_end(this->__end_ - 1);
1673}
1674
1675template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001676inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001677typename vector<_Tp, _Allocator>::iterator
1678vector<_Tp, _Allocator>::erase(const_iterator __position)
1679{
Louis Dionneba400782020-10-02 15:02:52 -04001680#if _LIBCPP_DEBUG_LEVEL == 2
Mark de Wever8f72c892021-10-10 15:40:50 +02001681 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__position)) == this,
Howard Hinnant27e0e772011-09-14 18:33:51 +00001682 "vector::erase(iterator) called with an iterator not"
1683 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001684#endif
Howard Hinnant58d52d52013-03-25 22:12:26 +00001685 _LIBCPP_ASSERT(__position != end(),
1686 "vector::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnant76053d72013-06-27 19:35:32 +00001687 difference_type __ps = __position - cbegin();
1688 pointer __p = this->__begin_ + __ps;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001689 this->__destruct_at_end(_VSTD::move(__p + 1, this->__end_, __p));
Eric Fiselier69c51982016-12-28 06:06:09 +00001690 this->__invalidate_iterators_past(__p-1);
1691 iterator __r = __make_iter(__p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001692 return __r;
1693}
1694
1695template <class _Tp, class _Allocator>
1696typename vector<_Tp, _Allocator>::iterator
1697vector<_Tp, _Allocator>::erase(const_iterator __first, const_iterator __last)
1698{
Louis Dionneba400782020-10-02 15:02:52 -04001699#if _LIBCPP_DEBUG_LEVEL == 2
Mark de Wever8f72c892021-10-10 15:40:50 +02001700 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__first)) == this,
Mark de Weverdcafc462021-10-21 18:29:14 +02001701 "vector::erase(iterator, iterator) called with an iterator not"
Howard Hinnant27e0e772011-09-14 18:33:51 +00001702 " referring to this vector");
Mark de Wever8f72c892021-10-10 15:40:50 +02001703 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__last)) == this,
Mark de Weverdcafc462021-10-21 18:29:14 +02001704 "vector::erase(iterator, iterator) called with an iterator not"
Eric Fiselier69c51982016-12-28 06:06:09 +00001705 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001706#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001707 _LIBCPP_ASSERT(__first <= __last, "vector::erase(first, last) called with invalid range");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001708 pointer __p = this->__begin_ + (__first - begin());
Eric Fiselier69c51982016-12-28 06:06:09 +00001709 if (__first != __last) {
Howard Hinnantc580cc32013-04-18 15:02:57 +00001710 this->__destruct_at_end(_VSTD::move(__p + (__last - __first), this->__end_, __p));
Eric Fiselier69c51982016-12-28 06:06:09 +00001711 this->__invalidate_iterators_past(__p - 1);
1712 }
1713 iterator __r = __make_iter(__p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001714 return __r;
1715}
1716
1717template <class _Tp, class _Allocator>
1718void
1719vector<_Tp, _Allocator>::__move_range(pointer __from_s, pointer __from_e, pointer __to)
1720{
1721 pointer __old_last = this->__end_;
1722 difference_type __n = __old_last - __to;
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001723 {
1724 pointer __i = __from_s + __n;
1725 _ConstructTransaction __tx(*this, __from_e - __i);
Martijn Vels1e2e1b82020-06-18 13:14:02 -04001726 for (pointer __pos = __tx.__pos_; __i < __from_e;
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04001727 ++__i, (void) ++__pos, __tx.__pos_ = __pos) {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001728 __alloc_traits::construct(this->__alloc(),
Martijn Vels1e2e1b82020-06-18 13:14:02 -04001729 _VSTD::__to_address(__pos),
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001730 _VSTD::move(*__i));
1731 }
1732 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001733 _VSTD::move_backward(__from_s, __from_s + __n, __old_last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001734}
1735
1736template <class _Tp, class _Allocator>
1737typename vector<_Tp, _Allocator>::iterator
1738vector<_Tp, _Allocator>::insert(const_iterator __position, const_reference __x)
1739{
Louis Dionneba400782020-10-02 15:02:52 -04001740#if _LIBCPP_DEBUG_LEVEL == 2
Mark de Wever8f72c892021-10-10 15:40:50 +02001741 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__position)) == this,
Howard Hinnant27e0e772011-09-14 18:33:51 +00001742 "vector::insert(iterator, x) called with an iterator not"
1743 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001744#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001745 pointer __p = this->__begin_ + (__position - begin());
1746 if (this->__end_ < this->__end_cap())
1747 {
1748 if (__p == this->__end_)
1749 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001750 __construct_one_at_end(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001751 }
1752 else
1753 {
1754 __move_range(__p, this->__end_, __p + 1);
1755 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1756 if (__p <= __xr && __xr < this->__end_)
1757 ++__xr;
1758 *__p = *__xr;
1759 }
1760 }
1761 else
1762 {
1763 allocator_type& __a = this->__alloc();
1764 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
1765 __v.push_back(__x);
1766 __p = __swap_out_circular_buffer(__v, __p);
1767 }
1768 return __make_iter(__p);
1769}
1770
Eric Fiseliered9e9362017-04-16 02:40:45 +00001771#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001772
1773template <class _Tp, class _Allocator>
1774typename vector<_Tp, _Allocator>::iterator
1775vector<_Tp, _Allocator>::insert(const_iterator __position, value_type&& __x)
1776{
Louis Dionneba400782020-10-02 15:02:52 -04001777#if _LIBCPP_DEBUG_LEVEL == 2
Mark de Wever8f72c892021-10-10 15:40:50 +02001778 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__position)) == this,
Howard Hinnant27e0e772011-09-14 18:33:51 +00001779 "vector::insert(iterator, x) called with an iterator not"
1780 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001781#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001782 pointer __p = this->__begin_ + (__position - begin());
1783 if (this->__end_ < this->__end_cap())
1784 {
1785 if (__p == this->__end_)
1786 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001787 __construct_one_at_end(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001788 }
1789 else
1790 {
1791 __move_range(__p, this->__end_, __p + 1);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001792 *__p = _VSTD::move(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001793 }
1794 }
1795 else
1796 {
1797 allocator_type& __a = this->__alloc();
1798 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001799 __v.push_back(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001800 __p = __swap_out_circular_buffer(__v, __p);
1801 }
1802 return __make_iter(__p);
1803}
1804
1805template <class _Tp, class _Allocator>
1806template <class... _Args>
1807typename vector<_Tp, _Allocator>::iterator
1808vector<_Tp, _Allocator>::emplace(const_iterator __position, _Args&&... __args)
1809{
Louis Dionneba400782020-10-02 15:02:52 -04001810#if _LIBCPP_DEBUG_LEVEL == 2
Mark de Wever8f72c892021-10-10 15:40:50 +02001811 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__position)) == this,
Howard Hinnant27e0e772011-09-14 18:33:51 +00001812 "vector::emplace(iterator, x) called with an iterator not"
1813 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001814#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001815 pointer __p = this->__begin_ + (__position - begin());
1816 if (this->__end_ < this->__end_cap())
1817 {
1818 if (__p == this->__end_)
1819 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001820 __construct_one_at_end(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001821 }
1822 else
1823 {
Marshall Clowa591b9a2016-07-11 21:38:08 +00001824 __temp_value<value_type, _Allocator> __tmp(this->__alloc(), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001825 __move_range(__p, this->__end_, __p + 1);
Marshall Clowa591b9a2016-07-11 21:38:08 +00001826 *__p = _VSTD::move(__tmp.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001827 }
1828 }
1829 else
1830 {
1831 allocator_type& __a = this->__alloc();
1832 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001833 __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001834 __p = __swap_out_circular_buffer(__v, __p);
1835 }
1836 return __make_iter(__p);
1837}
1838
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001839#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001840
1841template <class _Tp, class _Allocator>
1842typename vector<_Tp, _Allocator>::iterator
1843vector<_Tp, _Allocator>::insert(const_iterator __position, size_type __n, const_reference __x)
1844{
Louis Dionneba400782020-10-02 15:02:52 -04001845#if _LIBCPP_DEBUG_LEVEL == 2
Mark de Wever8f72c892021-10-10 15:40:50 +02001846 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__position)) == this,
Howard Hinnant27e0e772011-09-14 18:33:51 +00001847 "vector::insert(iterator, n, x) called with an iterator not"
1848 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001849#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001850 pointer __p = this->__begin_ + (__position - begin());
1851 if (__n > 0)
1852 {
1853 if (__n <= static_cast<size_type>(this->__end_cap() - this->__end_))
1854 {
1855 size_type __old_n = __n;
1856 pointer __old_last = this->__end_;
1857 if (__n > static_cast<size_type>(this->__end_ - __p))
1858 {
1859 size_type __cx = __n - (this->__end_ - __p);
1860 __construct_at_end(__cx, __x);
1861 __n -= __cx;
1862 }
1863 if (__n > 0)
1864 {
1865 __move_range(__p, __old_last, __p + __old_n);
1866 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1867 if (__p <= __xr && __xr < this->__end_)
1868 __xr += __old_n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001869 _VSTD::fill_n(__p, __n, *__xr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001870 }
1871 }
1872 else
1873 {
1874 allocator_type& __a = this->__alloc();
1875 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1876 __v.__construct_at_end(__n, __x);
1877 __p = __swap_out_circular_buffer(__v, __p);
1878 }
1879 }
1880 return __make_iter(__p);
1881}
1882
1883template <class _Tp, class _Allocator>
1884template <class _InputIterator>
1885typename enable_if
1886<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001887 __is_cpp17_input_iterator <_InputIterator>::value &&
1888 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001889 is_constructible<
1890 _Tp,
1891 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001892 typename vector<_Tp, _Allocator>::iterator
1893>::type
1894vector<_Tp, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
1895{
Louis Dionneba400782020-10-02 15:02:52 -04001896#if _LIBCPP_DEBUG_LEVEL == 2
Mark de Wever8f72c892021-10-10 15:40:50 +02001897 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__position)) == this,
Howard Hinnant27e0e772011-09-14 18:33:51 +00001898 "vector::insert(iterator, range) called with an iterator not"
1899 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001900#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001901 difference_type __off = __position - begin();
1902 pointer __p = this->__begin_ + __off;
1903 allocator_type& __a = this->__alloc();
1904 pointer __old_last = this->__end_;
1905 for (; this->__end_ != this->__end_cap() && __first != __last; ++__first)
1906 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001907 __construct_one_at_end(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001908 }
1909 __split_buffer<value_type, allocator_type&> __v(__a);
1910 if (__first != __last)
1911 {
1912#ifndef _LIBCPP_NO_EXCEPTIONS
1913 try
1914 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001915#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001916 __v.__construct_at_end(__first, __last);
1917 difference_type __old_size = __old_last - this->__begin_;
1918 difference_type __old_p = __p - this->__begin_;
1919 reserve(__recommend(size() + __v.size()));
1920 __p = this->__begin_ + __old_p;
1921 __old_last = this->__begin_ + __old_size;
1922#ifndef _LIBCPP_NO_EXCEPTIONS
1923 }
1924 catch (...)
1925 {
1926 erase(__make_iter(__old_last), end());
1927 throw;
1928 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001929#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001930 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001931 __p = _VSTD::rotate(__p, __old_last, this->__end_);
Louis Dionne202a4cb2020-02-11 10:59:12 +01001932 insert(__make_iter(__p), _VSTD::make_move_iterator(__v.begin()),
1933 _VSTD::make_move_iterator(__v.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001934 return begin() + __off;
1935}
1936
1937template <class _Tp, class _Allocator>
1938template <class _ForwardIterator>
1939typename enable_if
1940<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001941 __is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001942 is_constructible<
1943 _Tp,
1944 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001945 typename vector<_Tp, _Allocator>::iterator
1946>::type
1947vector<_Tp, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
1948{
Louis Dionneba400782020-10-02 15:02:52 -04001949#if _LIBCPP_DEBUG_LEVEL == 2
Mark de Wever8f72c892021-10-10 15:40:50 +02001950 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__position)) == this,
Howard Hinnant27e0e772011-09-14 18:33:51 +00001951 "vector::insert(iterator, range) called with an iterator not"
1952 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001953#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001954 pointer __p = this->__begin_ + (__position - begin());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001955 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001956 if (__n > 0)
1957 {
1958 if (__n <= this->__end_cap() - this->__end_)
1959 {
1960 size_type __old_n = __n;
1961 pointer __old_last = this->__end_;
1962 _ForwardIterator __m = __last;
1963 difference_type __dx = this->__end_ - __p;
1964 if (__n > __dx)
1965 {
1966 __m = __first;
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001967 difference_type __diff = this->__end_ - __p;
1968 _VSTD::advance(__m, __diff);
1969 __construct_at_end(__m, __last, __n - __diff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001970 __n = __dx;
1971 }
1972 if (__n > 0)
1973 {
1974 __move_range(__p, __old_last, __p + __old_n);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001975 _VSTD::copy(__first, __m, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001976 }
1977 }
1978 else
1979 {
1980 allocator_type& __a = this->__alloc();
1981 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1982 __v.__construct_at_end(__first, __last);
1983 __p = __swap_out_circular_buffer(__v, __p);
1984 }
1985 }
1986 return __make_iter(__p);
1987}
1988
1989template <class _Tp, class _Allocator>
1990void
1991vector<_Tp, _Allocator>::resize(size_type __sz)
1992{
1993 size_type __cs = size();
1994 if (__cs < __sz)
1995 this->__append(__sz - __cs);
1996 else if (__cs > __sz)
1997 this->__destruct_at_end(this->__begin_ + __sz);
1998}
1999
2000template <class _Tp, class _Allocator>
2001void
2002vector<_Tp, _Allocator>::resize(size_type __sz, const_reference __x)
2003{
2004 size_type __cs = size();
2005 if (__cs < __sz)
2006 this->__append(__sz - __cs, __x);
2007 else if (__cs > __sz)
2008 this->__destruct_at_end(this->__begin_ + __sz);
2009}
2010
2011template <class _Tp, class _Allocator>
2012void
2013vector<_Tp, _Allocator>::swap(vector& __x)
Marshall Clow8982dcd2015-07-13 20:04:56 +00002014#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00002015 _NOEXCEPT
Marshall Clow8982dcd2015-07-13 20:04:56 +00002016#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00002017 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00002018 __is_nothrow_swappable<allocator_type>::value)
2019#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002020{
Howard Hinnant27e0e772011-09-14 18:33:51 +00002021 _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
2022 this->__alloc() == __x.__alloc(),
2023 "vector::swap: Either propagate_on_container_swap must be true"
2024 " or the allocators must compare equal");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002025 _VSTD::swap(this->__begin_, __x.__begin_);
2026 _VSTD::swap(this->__end_, __x.__end_);
2027 _VSTD::swap(this->__end_cap(), __x.__end_cap());
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05002028 _VSTD::__swap_allocator(this->__alloc(), __x.__alloc(),
Marshall Clow8982dcd2015-07-13 20:04:56 +00002029 integral_constant<bool,__alloc_traits::propagate_on_container_swap::value>());
Louis Dionneba400782020-10-02 15:02:52 -04002030#if _LIBCPP_DEBUG_LEVEL == 2
Mark de Wever8f72c892021-10-10 15:40:50 +02002031 __get_db()->swap(this, _VSTD::addressof(__x));
Louis Dionneba400782020-10-02 15:02:52 -04002032#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002033}
2034
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002035template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002036bool
2037vector<_Tp, _Allocator>::__invariants() const
2038{
Howard Hinnant76053d72013-06-27 19:35:32 +00002039 if (this->__begin_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002040 {
Howard Hinnant76053d72013-06-27 19:35:32 +00002041 if (this->__end_ != nullptr || this->__end_cap() != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002042 return false;
2043 }
2044 else
2045 {
2046 if (this->__begin_ > this->__end_)
2047 return false;
2048 if (this->__begin_ == this->__end_cap())
2049 return false;
2050 if (this->__end_ > this->__end_cap())
2051 return false;
2052 }
2053 return true;
2054}
2055
Louis Dionneba400782020-10-02 15:02:52 -04002056#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002057
Howard Hinnantc51e1022010-05-11 19:42:16 +00002058template <class _Tp, class _Allocator>
Howard Hinnant27e0e772011-09-14 18:33:51 +00002059bool
2060vector<_Tp, _Allocator>::__dereferenceable(const const_iterator* __i) const
2061{
2062 return this->__begin_ <= __i->base() && __i->base() < this->__end_;
2063}
2064
2065template <class _Tp, class _Allocator>
2066bool
2067vector<_Tp, _Allocator>::__decrementable(const const_iterator* __i) const
2068{
2069 return this->__begin_ < __i->base() && __i->base() <= this->__end_;
2070}
2071
2072template <class _Tp, class _Allocator>
2073bool
2074vector<_Tp, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
2075{
2076 const_pointer __p = __i->base() + __n;
2077 return this->__begin_ <= __p && __p <= this->__end_;
2078}
2079
2080template <class _Tp, class _Allocator>
2081bool
2082vector<_Tp, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
2083{
2084 const_pointer __p = __i->base() + __n;
2085 return this->__begin_ <= __p && __p < this->__end_;
2086}
2087
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002088#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002089
2090template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002091inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002092void
2093vector<_Tp, _Allocator>::__invalidate_all_iterators()
2094{
Louis Dionneba400782020-10-02 15:02:52 -04002095#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002096 __get_db()->__invalidate_all(this);
Louis Dionneba400782020-10-02 15:02:52 -04002097#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002098}
2099
Eric Fiselier69c51982016-12-28 06:06:09 +00002100
2101template <class _Tp, class _Allocator>
2102inline _LIBCPP_INLINE_VISIBILITY
2103void
2104vector<_Tp, _Allocator>::__invalidate_iterators_past(pointer __new_last) {
Louis Dionneba400782020-10-02 15:02:52 -04002105#if _LIBCPP_DEBUG_LEVEL == 2
Eric Fiselier69c51982016-12-28 06:06:09 +00002106 __c_node* __c = __get_db()->__find_c_and_lock(this);
2107 for (__i_node** __p = __c->end_; __p != __c->beg_; ) {
2108 --__p;
2109 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
2110 if (__i->base() > __new_last) {
2111 (*__p)->__c_ = nullptr;
2112 if (--__c->end_ != __p)
Arthur O'Dwyer22236632020-12-07 21:50:15 -05002113 _VSTD::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
Eric Fiselier69c51982016-12-28 06:06:09 +00002114 }
2115 }
2116 __get_db()->unlock();
2117#else
2118 ((void)__new_last);
2119#endif
2120}
2121
Howard Hinnantc51e1022010-05-11 19:42:16 +00002122// vector<bool>
2123
2124template <class _Allocator> class vector<bool, _Allocator>;
2125
2126template <class _Allocator> struct hash<vector<bool, _Allocator> >;
2127
2128template <class _Allocator>
Howard Hinnantb4ef6482011-07-02 20:33:23 +00002129struct __has_storage_type<vector<bool, _Allocator> >
2130{
2131 static const bool value = true;
2132};
2133
2134template <class _Allocator>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002135class _LIBCPP_TEMPLATE_VIS vector<bool, _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002136 : private __vector_base_common<true>
2137{
2138public:
2139 typedef vector __self;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002140 typedef bool value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002141 typedef _Allocator allocator_type;
2142 typedef allocator_traits<allocator_type> __alloc_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002143 typedef typename __alloc_traits::size_type size_type;
2144 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant896b7622012-05-07 16:50:38 +00002145 typedef size_type __storage_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002146 typedef __bit_iterator<vector, false> pointer;
2147 typedef __bit_iterator<vector, true> const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002148 typedef pointer iterator;
2149 typedef const_pointer const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002150 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
2151 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002152
2153private:
Marshall Clow940e01c2015-04-07 05:21:38 +00002154 typedef typename __rebind_alloc_helper<__alloc_traits, __storage_type>::type __storage_allocator;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002155 typedef allocator_traits<__storage_allocator> __storage_traits;
2156 typedef typename __storage_traits::pointer __storage_pointer;
2157 typedef typename __storage_traits::const_pointer __const_storage_pointer;
2158
2159 __storage_pointer __begin_;
2160 size_type __size_;
2161 __compressed_pair<size_type, __storage_allocator> __cap_alloc_;
Howard Hinnant71d1c192011-07-09 15:50:42 +00002162public:
Howard Hinnantb4ef6482011-07-02 20:33:23 +00002163 typedef __bit_reference<vector> reference;
2164 typedef __bit_const_reference<vector> const_reference;
Howard Hinnant71d1c192011-07-09 15:50:42 +00002165private:
Howard Hinnant1c936782011-06-03 19:40:40 +00002166 _LIBCPP_INLINE_VISIBILITY
2167 size_type& __cap() _NOEXCEPT
2168 {return __cap_alloc_.first();}
2169 _LIBCPP_INLINE_VISIBILITY
2170 const size_type& __cap() const _NOEXCEPT
2171 {return __cap_alloc_.first();}
2172 _LIBCPP_INLINE_VISIBILITY
2173 __storage_allocator& __alloc() _NOEXCEPT
2174 {return __cap_alloc_.second();}
2175 _LIBCPP_INLINE_VISIBILITY
2176 const __storage_allocator& __alloc() const _NOEXCEPT
2177 {return __cap_alloc_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002178
2179 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
2180
Howard Hinnant1c936782011-06-03 19:40:40 +00002181 _LIBCPP_INLINE_VISIBILITY
2182 static size_type __internal_cap_to_external(size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002183 {return __n * __bits_per_word;}
Howard Hinnant1c936782011-06-03 19:40:40 +00002184 _LIBCPP_INLINE_VISIBILITY
2185 static size_type __external_cap_to_internal(size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002186 {return (__n - 1) / __bits_per_word + 1;}
2187
2188public:
Howard Hinnant1c936782011-06-03 19:40:40 +00002189 _LIBCPP_INLINE_VISIBILITY
Marshall Clowe546cbb2015-06-04 02:05:41 +00002190 vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clow8f282f42015-06-04 00:10:20 +00002191
2192 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
2193#if _LIBCPP_STD_VER <= 14
2194 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
2195#else
2196 _NOEXCEPT;
2197#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002198 ~vector();
2199 explicit vector(size_type __n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00002200#if _LIBCPP_STD_VER > 11
2201 explicit vector(size_type __n, const allocator_type& __a);
2202#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002203 vector(size_type __n, const value_type& __v);
2204 vector(size_type __n, const value_type& __v, const allocator_type& __a);
2205 template <class _InputIterator>
2206 vector(_InputIterator __first, _InputIterator __last,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002207 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
2208 !__is_cpp17_forward_iterator<_InputIterator>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002209 template <class _InputIterator>
2210 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002211 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
2212 !__is_cpp17_forward_iterator<_InputIterator>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002213 template <class _ForwardIterator>
2214 vector(_ForwardIterator __first, _ForwardIterator __last,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002215 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002216 template <class _ForwardIterator>
2217 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002218 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002219
2220 vector(const vector& __v);
2221 vector(const vector& __v, const allocator_type& __a);
2222 vector& operator=(const vector& __v);
Eric Fiseliered9e9362017-04-16 02:40:45 +00002223
2224#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002225 vector(initializer_list<value_type> __il);
2226 vector(initializer_list<value_type> __il, const allocator_type& __a);
2227
Howard Hinnant1c936782011-06-03 19:40:40 +00002228 _LIBCPP_INLINE_VISIBILITY
2229 vector(vector&& __v)
Marshall Clowe5108202015-07-14 14:46:32 +00002230#if _LIBCPP_STD_VER > 14
2231 _NOEXCEPT;
2232#else
Howard Hinnant1c936782011-06-03 19:40:40 +00002233 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowe5108202015-07-14 14:46:32 +00002234#endif
Arthur O'Dwyer9b9662f2021-03-01 17:08:24 -05002235 vector(vector&& __v, const __identity_t<allocator_type>& __a);
Howard Hinnant1c936782011-06-03 19:40:40 +00002236 _LIBCPP_INLINE_VISIBILITY
2237 vector& operator=(vector&& __v)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002238 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiseliered9e9362017-04-16 02:40:45 +00002239
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002240 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002241 vector& operator=(initializer_list<value_type> __il)
2242 {assign(__il.begin(), __il.end()); return *this;}
Eric Fiseliered9e9362017-04-16 02:40:45 +00002243
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002244#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002245
2246 template <class _InputIterator>
2247 typename enable_if
2248 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002249 __is_cpp17_input_iterator<_InputIterator>::value &&
2250 !__is_cpp17_forward_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002251 void
2252 >::type
2253 assign(_InputIterator __first, _InputIterator __last);
2254 template <class _ForwardIterator>
2255 typename enable_if
2256 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002257 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002258 void
2259 >::type
2260 assign(_ForwardIterator __first, _ForwardIterator __last);
2261
2262 void assign(size_type __n, const value_type& __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +00002263
2264#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002265 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002266 void assign(initializer_list<value_type> __il)
2267 {assign(__il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +00002268#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002269
Howard Hinnant1c936782011-06-03 19:40:40 +00002270 _LIBCPP_INLINE_VISIBILITY allocator_type get_allocator() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002271 {return allocator_type(this->__alloc());}
2272
Howard Hinnant1c936782011-06-03 19:40:40 +00002273 size_type max_size() const _NOEXCEPT;
2274 _LIBCPP_INLINE_VISIBILITY
2275 size_type capacity() const _NOEXCEPT
2276 {return __internal_cap_to_external(__cap());}
2277 _LIBCPP_INLINE_VISIBILITY
2278 size_type size() const _NOEXCEPT
2279 {return __size_;}
Marshall Clow425f5752017-11-15 05:51:26 +00002280 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +00002281 bool empty() const _NOEXCEPT
2282 {return __size_ == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002283 void reserve(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +00002284 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002285
Howard Hinnant1c936782011-06-03 19:40:40 +00002286 _LIBCPP_INLINE_VISIBILITY
2287 iterator begin() _NOEXCEPT
2288 {return __make_iter(0);}
2289 _LIBCPP_INLINE_VISIBILITY
2290 const_iterator begin() const _NOEXCEPT
2291 {return __make_iter(0);}
2292 _LIBCPP_INLINE_VISIBILITY
2293 iterator end() _NOEXCEPT
2294 {return __make_iter(__size_);}
2295 _LIBCPP_INLINE_VISIBILITY
2296 const_iterator end() const _NOEXCEPT
2297 {return __make_iter(__size_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002298
Howard Hinnant1c936782011-06-03 19:40:40 +00002299 _LIBCPP_INLINE_VISIBILITY
2300 reverse_iterator rbegin() _NOEXCEPT
2301 {return reverse_iterator(end());}
2302 _LIBCPP_INLINE_VISIBILITY
2303 const_reverse_iterator rbegin() const _NOEXCEPT
2304 {return const_reverse_iterator(end());}
2305 _LIBCPP_INLINE_VISIBILITY
2306 reverse_iterator rend() _NOEXCEPT
2307 {return reverse_iterator(begin());}
2308 _LIBCPP_INLINE_VISIBILITY
2309 const_reverse_iterator rend() const _NOEXCEPT
2310 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002311
Howard Hinnant1c936782011-06-03 19:40:40 +00002312 _LIBCPP_INLINE_VISIBILITY
2313 const_iterator cbegin() const _NOEXCEPT
2314 {return __make_iter(0);}
2315 _LIBCPP_INLINE_VISIBILITY
2316 const_iterator cend() const _NOEXCEPT
2317 {return __make_iter(__size_);}
2318 _LIBCPP_INLINE_VISIBILITY
2319 const_reverse_iterator crbegin() const _NOEXCEPT
2320 {return rbegin();}
2321 _LIBCPP_INLINE_VISIBILITY
2322 const_reverse_iterator crend() const _NOEXCEPT
2323 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002324
2325 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) {return __make_ref(__n);}
2326 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const {return __make_ref(__n);}
2327 reference at(size_type __n);
2328 const_reference at(size_type __n) const;
2329
2330 _LIBCPP_INLINE_VISIBILITY reference front() {return __make_ref(0);}
2331 _LIBCPP_INLINE_VISIBILITY const_reference front() const {return __make_ref(0);}
2332 _LIBCPP_INLINE_VISIBILITY reference back() {return __make_ref(__size_ - 1);}
2333 _LIBCPP_INLINE_VISIBILITY const_reference back() const {return __make_ref(__size_ - 1);}
2334
2335 void push_back(const value_type& __x);
Marshall Clowc46bb8e2013-08-13 23:54:12 +00002336#if _LIBCPP_STD_VER > 11
2337 template <class... _Args>
Marshall Clowea52cc42017-01-24 23:09:12 +00002338#if _LIBCPP_STD_VER > 14
2339 _LIBCPP_INLINE_VISIBILITY reference emplace_back(_Args&&... __args)
2340#else
2341 _LIBCPP_INLINE_VISIBILITY void emplace_back(_Args&&... __args)
2342#endif
2343 {
Eric Fiselier34ba5b92016-07-21 03:20:17 +00002344 push_back ( value_type ( _VSTD::forward<_Args>(__args)... ));
Marshall Clowea52cc42017-01-24 23:09:12 +00002345#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00002346 return this->back();
Marshall Clowea52cc42017-01-24 23:09:12 +00002347#endif
Eric Fiselier34ba5b92016-07-21 03:20:17 +00002348 }
Marshall Clowc46bb8e2013-08-13 23:54:12 +00002349#endif
2350
Howard Hinnantc51e1022010-05-11 19:42:16 +00002351 _LIBCPP_INLINE_VISIBILITY void pop_back() {--__size_;}
2352
Marshall Clowc46bb8e2013-08-13 23:54:12 +00002353#if _LIBCPP_STD_VER > 11
2354 template <class... _Args>
2355 _LIBCPP_INLINE_VISIBILITY iterator emplace(const_iterator position, _Args&&... __args)
2356 { return insert ( position, value_type ( _VSTD::forward<_Args>(__args)... )); }
2357#endif
2358
Howard Hinnantc51e1022010-05-11 19:42:16 +00002359 iterator insert(const_iterator __position, const value_type& __x);
2360 iterator insert(const_iterator __position, size_type __n, const value_type& __x);
2361 iterator insert(const_iterator __position, size_type __n, const_reference __x);
2362 template <class _InputIterator>
2363 typename enable_if
2364 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002365 __is_cpp17_input_iterator <_InputIterator>::value &&
2366 !__is_cpp17_forward_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002367 iterator
2368 >::type
2369 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
2370 template <class _ForwardIterator>
2371 typename enable_if
2372 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002373 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002374 iterator
2375 >::type
2376 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiseliered9e9362017-04-16 02:40:45 +00002377
2378#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002379 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002380 iterator insert(const_iterator __position, initializer_list<value_type> __il)
2381 {return insert(__position, __il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +00002382#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002383
Howard Hinnantcf823322010-12-17 14:46:43 +00002384 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002385 iterator erase(const_iterator __first, const_iterator __last);
2386
Howard Hinnant1c936782011-06-03 19:40:40 +00002387 _LIBCPP_INLINE_VISIBILITY
2388 void clear() _NOEXCEPT {__size_ = 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002389
Howard Hinnant1c936782011-06-03 19:40:40 +00002390 void swap(vector&)
Marshall Clow8982dcd2015-07-13 20:04:56 +00002391#if _LIBCPP_STD_VER >= 14
2392 _NOEXCEPT;
2393#else
Eric Fiselier69c51982016-12-28 06:06:09 +00002394 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00002395 __is_nothrow_swappable<allocator_type>::value);
2396#endif
Marshall Clow89eb3822016-04-07 14:20:31 +00002397 static void swap(reference __x, reference __y) _NOEXCEPT { _VSTD::swap(__x, __y); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002398
2399 void resize(size_type __sz, value_type __x = false);
Howard Hinnant1c936782011-06-03 19:40:40 +00002400 void flip() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002401
2402 bool __invariants() const;
2403
2404private:
Howard Hinnantcf823322010-12-17 14:46:43 +00002405 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Marshall Clow3ff48e02018-05-22 16:20:28 +00002406 void __vallocate(size_type __n);
2407 void __vdeallocate() _NOEXCEPT;
Howard Hinnant1c936782011-06-03 19:40:40 +00002408 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantea382952013-08-14 18:00:20 +00002409 static size_type __align_it(size_type __new_size) _NOEXCEPT
Eric Fiselierb41db9a2018-10-01 01:59:37 +00002410 {return __new_size + (__bits_per_word-1) & ~((size_type)__bits_per_word-1);}
Howard Hinnantcf823322010-12-17 14:46:43 +00002411 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
2412 _LIBCPP_INLINE_VISIBILITY void __construct_at_end(size_type __n, bool __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002413 template <class _ForwardIterator>
2414 typename enable_if
2415 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002416 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002417 void
2418 >::type
2419 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
2420 void __append(size_type __n, const_reference __x);
Howard Hinnant1c936782011-06-03 19:40:40 +00002421 _LIBCPP_INLINE_VISIBILITY
2422 reference __make_ref(size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002423 {return reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnant1c936782011-06-03 19:40:40 +00002424 _LIBCPP_INLINE_VISIBILITY
2425 const_reference __make_ref(size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002426 {return const_reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnant1c936782011-06-03 19:40:40 +00002427 _LIBCPP_INLINE_VISIBILITY
2428 iterator __make_iter(size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002429 {return iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnant1c936782011-06-03 19:40:40 +00002430 _LIBCPP_INLINE_VISIBILITY
2431 const_iterator __make_iter(size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002432 {return const_iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnant1c936782011-06-03 19:40:40 +00002433 _LIBCPP_INLINE_VISIBILITY
2434 iterator __const_iterator_cast(const_iterator __p) _NOEXCEPT
Howard Hinnant76053d72013-06-27 19:35:32 +00002435 {return begin() + (__p - cbegin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002436
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002437 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002438 void __copy_assign_alloc(const vector& __v)
2439 {__copy_assign_alloc(__v, integral_constant<bool,
2440 __storage_traits::propagate_on_container_copy_assignment::value>());}
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002441 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002442 void __copy_assign_alloc(const vector& __c, true_type)
2443 {
2444 if (__alloc() != __c.__alloc())
Marshall Clow3ff48e02018-05-22 16:20:28 +00002445 __vdeallocate();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002446 __alloc() = __c.__alloc();
2447 }
2448
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002449 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00002450 void __copy_assign_alloc(const vector&, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002451 {}
2452
2453 void __move_assign(vector& __c, false_type);
Howard Hinnant1c936782011-06-03 19:40:40 +00002454 void __move_assign(vector& __c, true_type)
2455 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002456 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002457 void __move_assign_alloc(vector& __c)
Howard Hinnant1c936782011-06-03 19:40:40 +00002458 _NOEXCEPT_(
2459 !__storage_traits::propagate_on_container_move_assignment::value ||
2460 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002461 {__move_assign_alloc(__c, integral_constant<bool,
2462 __storage_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002463 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +00002464 void __move_assign_alloc(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002465 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002466 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002467 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002468 }
2469
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002470 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00002471 void __move_assign_alloc(vector&, false_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002472 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002473 {}
2474
Howard Hinnant1c936782011-06-03 19:40:40 +00002475 size_t __hash_code() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002476
2477 friend class __bit_reference<vector>;
2478 friend class __bit_const_reference<vector>;
2479 friend class __bit_iterator<vector, false>;
2480 friend class __bit_iterator<vector, true>;
Howard Hinnant4210a0f2012-08-17 17:10:18 +00002481 friend struct __bit_array<vector>;
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002482 friend struct _LIBCPP_TEMPLATE_VIS hash<vector>;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002483};
2484
2485template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002486inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002487void
2488vector<bool, _Allocator>::__invalidate_all_iterators()
2489{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002490}
2491
2492// Allocate space for __n objects
2493// throws length_error if __n > max_size()
2494// throws (probably bad_alloc) if memory run out
2495// Precondition: __begin_ == __end_ == __cap() == 0
2496// Precondition: __n > 0
2497// Postcondition: capacity() == __n
2498// Postcondition: size() == 0
2499template <class _Allocator>
2500void
Marshall Clow3ff48e02018-05-22 16:20:28 +00002501vector<bool, _Allocator>::__vallocate(size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002502{
2503 if (__n > max_size())
2504 this->__throw_length_error();
2505 __n = __external_cap_to_internal(__n);
2506 this->__begin_ = __storage_traits::allocate(this->__alloc(), __n);
2507 this->__size_ = 0;
2508 this->__cap() = __n;
2509}
2510
2511template <class _Allocator>
2512void
Marshall Clow3ff48e02018-05-22 16:20:28 +00002513vector<bool, _Allocator>::__vdeallocate() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002514{
Howard Hinnant76053d72013-06-27 19:35:32 +00002515 if (this->__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002516 {
2517 __storage_traits::deallocate(this->__alloc(), this->__begin_, __cap());
2518 __invalidate_all_iterators();
Howard Hinnant76053d72013-06-27 19:35:32 +00002519 this->__begin_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002520 this->__size_ = this->__cap() = 0;
2521 }
2522}
2523
2524template <class _Allocator>
2525typename vector<bool, _Allocator>::size_type
Howard Hinnant1c936782011-06-03 19:40:40 +00002526vector<bool, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002527{
2528 size_type __amax = __storage_traits::max_size(__alloc());
2529 size_type __nmax = numeric_limits<size_type>::max() / 2; // end() >= begin(), always
2530 if (__nmax / __bits_per_word <= __amax)
2531 return __nmax;
2532 return __internal_cap_to_external(__amax);
2533}
2534
2535// Precondition: __new_size > capacity()
2536template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002537inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002538typename vector<bool, _Allocator>::size_type
2539vector<bool, _Allocator>::__recommend(size_type __new_size) const
2540{
2541 const size_type __ms = max_size();
2542 if (__new_size > __ms)
2543 this->__throw_length_error();
2544 const size_type __cap = capacity();
2545 if (__cap >= __ms / 2)
2546 return __ms;
Arthur O'Dwyerc0fb14b2021-07-26 18:23:00 -04002547 return _VSTD::max(2 * __cap, __align_it(__new_size));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002548}
2549
2550// Default constructs __n objects starting at __end_
2551// Precondition: __n > 0
2552// Precondition: size() + __n <= capacity()
2553// Postcondition: size() == size() + __n
2554template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002555inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002556void
2557vector<bool, _Allocator>::__construct_at_end(size_type __n, bool __x)
2558{
2559 size_type __old_size = this->__size_;
2560 this->__size_ += __n;
Marshall Clow1893ec72018-10-23 20:07:45 +00002561 if (__old_size == 0 || ((__old_size - 1) / __bits_per_word) != ((this->__size_ - 1) / __bits_per_word))
2562 {
2563 if (this->__size_ <= __bits_per_word)
2564 this->__begin_[0] = __storage_type(0);
2565 else
2566 this->__begin_[(this->__size_ - 1) / __bits_per_word] = __storage_type(0);
2567 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002568 _VSTD::fill_n(__make_iter(__old_size), __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002569}
2570
2571template <class _Allocator>
2572template <class _ForwardIterator>
2573typename enable_if
2574<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002575 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002576 void
2577>::type
2578vector<bool, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
2579{
2580 size_type __old_size = this->__size_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002581 this->__size_ += _VSTD::distance(__first, __last);
Marshall Clow1893ec72018-10-23 20:07:45 +00002582 if (__old_size == 0 || ((__old_size - 1) / __bits_per_word) != ((this->__size_ - 1) / __bits_per_word))
2583 {
2584 if (this->__size_ <= __bits_per_word)
2585 this->__begin_[0] = __storage_type(0);
2586 else
2587 this->__begin_[(this->__size_ - 1) / __bits_per_word] = __storage_type(0);
2588 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002589 _VSTD::copy(__first, __last, __make_iter(__old_size));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002590}
2591
2592template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002593inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002594vector<bool, _Allocator>::vector()
Marshall Clowe546cbb2015-06-04 02:05:41 +00002595 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant76053d72013-06-27 19:35:32 +00002596 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002597 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002598 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002599{
2600}
2601
2602template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002603inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002604vector<bool, _Allocator>::vector(const allocator_type& __a)
Marshall Clow8f282f42015-06-04 00:10:20 +00002605#if _LIBCPP_STD_VER <= 14
2606 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
2607#else
2608 _NOEXCEPT
2609#endif
Howard Hinnant76053d72013-06-27 19:35:32 +00002610 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002611 __size_(0),
2612 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2613{
2614}
2615
2616template <class _Allocator>
2617vector<bool, _Allocator>::vector(size_type __n)
Howard Hinnant76053d72013-06-27 19:35:32 +00002618 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002619 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002620 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002621{
2622 if (__n > 0)
2623 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002624 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002625 __construct_at_end(__n, false);
2626 }
2627}
2628
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00002629#if _LIBCPP_STD_VER > 11
2630template <class _Allocator>
2631vector<bool, _Allocator>::vector(size_type __n, const allocator_type& __a)
2632 : __begin_(nullptr),
2633 __size_(0),
2634 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2635{
2636 if (__n > 0)
2637 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002638 __vallocate(__n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00002639 __construct_at_end(__n, false);
2640 }
2641}
2642#endif
2643
Howard Hinnantc51e1022010-05-11 19:42:16 +00002644template <class _Allocator>
2645vector<bool, _Allocator>::vector(size_type __n, const value_type& __x)
Howard Hinnant76053d72013-06-27 19:35:32 +00002646 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002647 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002648 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002649{
2650 if (__n > 0)
2651 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002652 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002653 __construct_at_end(__n, __x);
2654 }
2655}
2656
2657template <class _Allocator>
2658vector<bool, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002659 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002660 __size_(0),
2661 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2662{
2663 if (__n > 0)
2664 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002665 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002666 __construct_at_end(__n, __x);
2667 }
2668}
2669
2670template <class _Allocator>
2671template <class _InputIterator>
2672vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002673 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
2674 !__is_cpp17_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002675 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002676 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002677 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002678{
2679#ifndef _LIBCPP_NO_EXCEPTIONS
2680 try
2681 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002682#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002683 for (; __first != __last; ++__first)
2684 push_back(*__first);
2685#ifndef _LIBCPP_NO_EXCEPTIONS
2686 }
2687 catch (...)
2688 {
Howard Hinnant76053d72013-06-27 19:35:32 +00002689 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002690 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2691 __invalidate_all_iterators();
2692 throw;
2693 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002694#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002695}
2696
2697template <class _Allocator>
2698template <class _InputIterator>
2699vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002700 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
2701 !__is_cpp17_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002702 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002703 __size_(0),
2704 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2705{
2706#ifndef _LIBCPP_NO_EXCEPTIONS
2707 try
2708 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002709#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002710 for (; __first != __last; ++__first)
2711 push_back(*__first);
2712#ifndef _LIBCPP_NO_EXCEPTIONS
2713 }
2714 catch (...)
2715 {
Howard Hinnant76053d72013-06-27 19:35:32 +00002716 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002717 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2718 __invalidate_all_iterators();
2719 throw;
2720 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002721#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002722}
2723
2724template <class _Allocator>
2725template <class _ForwardIterator>
2726vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002727 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002728 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002729 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002730 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002731{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002732 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002733 if (__n > 0)
2734 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002735 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002736 __construct_at_end(__first, __last);
2737 }
2738}
2739
2740template <class _Allocator>
2741template <class _ForwardIterator>
2742vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002743 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value>::type*)
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{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002748 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002749 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(__first, __last);
2753 }
2754}
2755
Eric Fiseliered9e9362017-04-16 02:40:45 +00002756#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002757
Howard Hinnantc51e1022010-05-11 19:42:16 +00002758template <class _Allocator>
2759vector<bool, _Allocator>::vector(initializer_list<value_type> __il)
Howard Hinnant76053d72013-06-27 19:35:32 +00002760 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002761 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002762 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002763{
2764 size_type __n = static_cast<size_type>(__il.size());
2765 if (__n > 0)
2766 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002767 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002768 __construct_at_end(__il.begin(), __il.end());
2769 }
2770}
2771
2772template <class _Allocator>
2773vector<bool, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002774 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002775 __size_(0),
2776 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2777{
2778 size_type __n = static_cast<size_type>(__il.size());
2779 if (__n > 0)
2780 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002781 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002782 __construct_at_end(__il.begin(), __il.end());
2783 }
2784}
2785
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002786#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002787
Howard Hinnantc51e1022010-05-11 19:42:16 +00002788template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002789vector<bool, _Allocator>::~vector()
2790{
Howard Hinnant76053d72013-06-27 19:35:32 +00002791 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002792 __storage_traits::deallocate(__alloc(), __begin_, __cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002793 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002794}
2795
2796template <class _Allocator>
2797vector<bool, _Allocator>::vector(const vector& __v)
Howard Hinnant76053d72013-06-27 19:35:32 +00002798 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002799 __size_(0),
2800 __cap_alloc_(0, __storage_traits::select_on_container_copy_construction(__v.__alloc()))
2801{
2802 if (__v.size() > 0)
2803 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002804 __vallocate(__v.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002805 __construct_at_end(__v.begin(), __v.end());
2806 }
2807}
2808
2809template <class _Allocator>
2810vector<bool, _Allocator>::vector(const vector& __v, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002811 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002812 __size_(0),
2813 __cap_alloc_(0, __a)
2814{
2815 if (__v.size() > 0)
2816 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002817 __vallocate(__v.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002818 __construct_at_end(__v.begin(), __v.end());
2819 }
2820}
2821
2822template <class _Allocator>
2823vector<bool, _Allocator>&
2824vector<bool, _Allocator>::operator=(const vector& __v)
2825{
Mark de Wever357a1fc2021-09-28 19:15:18 +02002826 if (this != _VSTD::addressof(__v))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002827 {
2828 __copy_assign_alloc(__v);
2829 if (__v.__size_)
2830 {
2831 if (__v.__size_ > capacity())
2832 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002833 __vdeallocate();
2834 __vallocate(__v.__size_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002835 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002836 _VSTD::copy(__v.__begin_, __v.__begin_ + __external_cap_to_internal(__v.__size_), __begin_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002837 }
2838 __size_ = __v.__size_;
2839 }
2840 return *this;
2841}
2842
Eric Fiseliered9e9362017-04-16 02:40:45 +00002843#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant74279a52010-09-04 23:28:19 +00002844
Howard Hinnantc51e1022010-05-11 19:42:16 +00002845template <class _Allocator>
Eric Fiselier4f1534c2018-06-05 22:32:52 +00002846inline _LIBCPP_INLINE_VISIBILITY vector<bool, _Allocator>::vector(vector&& __v)
Marshall Clowe5108202015-07-14 14:46:32 +00002847#if _LIBCPP_STD_VER > 14
Eric Fiselier4f1534c2018-06-05 22:32:52 +00002848 _NOEXCEPT
Marshall Clowe5108202015-07-14 14:46:32 +00002849#else
Eric Fiselier4f1534c2018-06-05 22:32:52 +00002850 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowe5108202015-07-14 14:46:32 +00002851#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002852 : __begin_(__v.__begin_),
2853 __size_(__v.__size_),
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05002854 __cap_alloc_(_VSTD::move(__v.__cap_alloc_)) {
Howard Hinnant76053d72013-06-27 19:35:32 +00002855 __v.__begin_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002856 __v.__size_ = 0;
2857 __v.__cap() = 0;
2858}
2859
2860template <class _Allocator>
Arthur O'Dwyer9b9662f2021-03-01 17:08:24 -05002861vector<bool, _Allocator>::vector(vector&& __v, const __identity_t<allocator_type>& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002862 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002863 __size_(0),
2864 __cap_alloc_(0, __a)
2865{
2866 if (__a == allocator_type(__v.__alloc()))
2867 {
2868 this->__begin_ = __v.__begin_;
2869 this->__size_ = __v.__size_;
2870 this->__cap() = __v.__cap();
2871 __v.__begin_ = nullptr;
2872 __v.__cap() = __v.__size_ = 0;
2873 }
2874 else if (__v.size() > 0)
2875 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002876 __vallocate(__v.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002877 __construct_at_end(__v.begin(), __v.end());
2878 }
2879}
2880
2881template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002882inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002883vector<bool, _Allocator>&
2884vector<bool, _Allocator>::operator=(vector&& __v)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002885 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002886{
2887 __move_assign(__v, integral_constant<bool,
2888 __storage_traits::propagate_on_container_move_assignment::value>());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002889 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002890}
2891
2892template <class _Allocator>
2893void
2894vector<bool, _Allocator>::__move_assign(vector& __c, false_type)
2895{
2896 if (__alloc() != __c.__alloc())
2897 assign(__c.begin(), __c.end());
2898 else
2899 __move_assign(__c, true_type());
2900}
2901
2902template <class _Allocator>
2903void
2904vector<bool, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002905 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002906{
Marshall Clow3ff48e02018-05-22 16:20:28 +00002907 __vdeallocate();
Marshall Clowb4871fa2014-07-21 15:15:15 +00002908 __move_assign_alloc(__c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002909 this->__begin_ = __c.__begin_;
2910 this->__size_ = __c.__size_;
2911 this->__cap() = __c.__cap();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002912 __c.__begin_ = nullptr;
2913 __c.__cap() = __c.__size_ = 0;
2914}
Howard Hinnant74279a52010-09-04 23:28:19 +00002915
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002916#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002917
2918template <class _Allocator>
2919void
2920vector<bool, _Allocator>::assign(size_type __n, const value_type& __x)
2921{
2922 __size_ = 0;
2923 if (__n > 0)
2924 {
2925 size_type __c = capacity();
2926 if (__n <= __c)
2927 __size_ = __n;
2928 else
2929 {
Mikhail Maltsevf56b0692021-10-21 10:38:56 +01002930 vector __v(get_allocator());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002931 __v.reserve(__recommend(__n));
2932 __v.__size_ = __n;
2933 swap(__v);
2934 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002935 _VSTD::fill_n(begin(), __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002936 }
Eric Fiselier69c51982016-12-28 06:06:09 +00002937 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002938}
2939
2940template <class _Allocator>
2941template <class _InputIterator>
2942typename enable_if
2943<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002944 __is_cpp17_input_iterator<_InputIterator>::value &&
2945 !__is_cpp17_forward_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002946 void
2947>::type
2948vector<bool, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2949{
2950 clear();
2951 for (; __first != __last; ++__first)
2952 push_back(*__first);
2953}
2954
2955template <class _Allocator>
2956template <class _ForwardIterator>
2957typename enable_if
2958<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002959 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002960 void
2961>::type
2962vector<bool, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2963{
2964 clear();
Eric Fiselier6003c772016-12-23 23:37:52 +00002965 difference_type __ns = _VSTD::distance(__first, __last);
2966 _LIBCPP_ASSERT(__ns >= 0, "invalid range specified");
2967 const size_t __n = static_cast<size_type>(__ns);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002968 if (__n)
2969 {
2970 if (__n > capacity())
2971 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002972 __vdeallocate();
2973 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002974 }
2975 __construct_at_end(__first, __last);
2976 }
2977}
2978
2979template <class _Allocator>
2980void
2981vector<bool, _Allocator>::reserve(size_type __n)
2982{
2983 if (__n > capacity())
2984 {
Mikhail Maltsev0b0a51d2021-10-21 10:40:05 +01002985 if (__n > max_size())
2986 this->__throw_length_error();
Mikhail Maltsevf56b0692021-10-21 10:38:56 +01002987 vector __v(this->get_allocator());
Marshall Clow3ff48e02018-05-22 16:20:28 +00002988 __v.__vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002989 __v.__construct_at_end(this->begin(), this->end());
2990 swap(__v);
2991 __invalidate_all_iterators();
2992 }
2993}
2994
2995template <class _Allocator>
2996void
Howard Hinnant1c936782011-06-03 19:40:40 +00002997vector<bool, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002998{
2999 if (__external_cap_to_internal(size()) > __cap())
3000 {
3001#ifndef _LIBCPP_NO_EXCEPTIONS
3002 try
3003 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003004#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003005 vector(*this, allocator_type(__alloc())).swap(*this);
3006#ifndef _LIBCPP_NO_EXCEPTIONS
3007 }
3008 catch (...)
3009 {
3010 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003011#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003012 }
3013}
3014
3015template <class _Allocator>
3016typename vector<bool, _Allocator>::reference
3017vector<bool, _Allocator>::at(size_type __n)
3018{
3019 if (__n >= size())
3020 this->__throw_out_of_range();
3021 return (*this)[__n];
3022}
3023
3024template <class _Allocator>
3025typename vector<bool, _Allocator>::const_reference
3026vector<bool, _Allocator>::at(size_type __n) const
3027{
3028 if (__n >= size())
3029 this->__throw_out_of_range();
3030 return (*this)[__n];
3031}
3032
3033template <class _Allocator>
3034void
3035vector<bool, _Allocator>::push_back(const value_type& __x)
3036{
3037 if (this->__size_ == this->capacity())
3038 reserve(__recommend(this->__size_ + 1));
3039 ++this->__size_;
3040 back() = __x;
3041}
3042
3043template <class _Allocator>
3044typename vector<bool, _Allocator>::iterator
3045vector<bool, _Allocator>::insert(const_iterator __position, const value_type& __x)
3046{
3047 iterator __r;
3048 if (size() < capacity())
3049 {
3050 const_iterator __old_end = end();
3051 ++__size_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003052 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003053 __r = __const_iterator_cast(__position);
3054 }
3055 else
3056 {
Mikhail Maltsevf56b0692021-10-21 10:38:56 +01003057 vector __v(get_allocator());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003058 __v.reserve(__recommend(__size_ + 1));
3059 __v.__size_ = __size_ + 1;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003060 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3061 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003062 swap(__v);
3063 }
3064 *__r = __x;
3065 return __r;
3066}
3067
3068template <class _Allocator>
3069typename vector<bool, _Allocator>::iterator
3070vector<bool, _Allocator>::insert(const_iterator __position, size_type __n, const value_type& __x)
3071{
3072 iterator __r;
3073 size_type __c = capacity();
3074 if (__n <= __c && size() <= __c - __n)
3075 {
3076 const_iterator __old_end = end();
3077 __size_ += __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003078 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003079 __r = __const_iterator_cast(__position);
3080 }
3081 else
3082 {
Mikhail Maltsevf56b0692021-10-21 10:38:56 +01003083 vector __v(get_allocator());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003084 __v.reserve(__recommend(__size_ + __n));
3085 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003086 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3087 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003088 swap(__v);
3089 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003090 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003091 return __r;
3092}
3093
3094template <class _Allocator>
3095template <class _InputIterator>
3096typename enable_if
3097<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05003098 __is_cpp17_input_iterator <_InputIterator>::value &&
3099 !__is_cpp17_forward_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003100 typename vector<bool, _Allocator>::iterator
3101>::type
3102vector<bool, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
3103{
3104 difference_type __off = __position - begin();
3105 iterator __p = __const_iterator_cast(__position);
3106 iterator __old_end = end();
3107 for (; size() != capacity() && __first != __last; ++__first)
3108 {
3109 ++this->__size_;
3110 back() = *__first;
3111 }
Mikhail Maltsevf56b0692021-10-21 10:38:56 +01003112 vector __v(get_allocator());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003113 if (__first != __last)
3114 {
3115#ifndef _LIBCPP_NO_EXCEPTIONS
3116 try
3117 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003118#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003119 __v.assign(__first, __last);
3120 difference_type __old_size = static_cast<difference_type>(__old_end - begin());
3121 difference_type __old_p = __p - begin();
3122 reserve(__recommend(size() + __v.size()));
3123 __p = begin() + __old_p;
3124 __old_end = begin() + __old_size;
3125#ifndef _LIBCPP_NO_EXCEPTIONS
3126 }
3127 catch (...)
3128 {
3129 erase(__old_end, end());
3130 throw;
3131 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003132#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003133 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003134 __p = _VSTD::rotate(__p, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003135 insert(__p, __v.begin(), __v.end());
3136 return begin() + __off;
3137}
3138
3139template <class _Allocator>
3140template <class _ForwardIterator>
3141typename enable_if
3142<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05003143 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003144 typename vector<bool, _Allocator>::iterator
3145>::type
3146vector<bool, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
3147{
Eric Fiselier654dd332016-12-11 05:31:00 +00003148 const difference_type __n_signed = _VSTD::distance(__first, __last);
3149 _LIBCPP_ASSERT(__n_signed >= 0, "invalid range specified");
3150 const size_type __n = static_cast<size_type>(__n_signed);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003151 iterator __r;
3152 size_type __c = capacity();
3153 if (__n <= __c && size() <= __c - __n)
3154 {
3155 const_iterator __old_end = end();
3156 __size_ += __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003157 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003158 __r = __const_iterator_cast(__position);
3159 }
3160 else
3161 {
Mikhail Maltsevf56b0692021-10-21 10:38:56 +01003162 vector __v(get_allocator());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003163 __v.reserve(__recommend(__size_ + __n));
3164 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003165 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3166 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003167 swap(__v);
3168 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003169 _VSTD::copy(__first, __last, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003170 return __r;
3171}
3172
3173template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003174inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003175typename vector<bool, _Allocator>::iterator
3176vector<bool, _Allocator>::erase(const_iterator __position)
3177{
3178 iterator __r = __const_iterator_cast(__position);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003179 _VSTD::copy(__position + 1, this->cend(), __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003180 --__size_;
3181 return __r;
3182}
3183
3184template <class _Allocator>
3185typename vector<bool, _Allocator>::iterator
3186vector<bool, _Allocator>::erase(const_iterator __first, const_iterator __last)
3187{
3188 iterator __r = __const_iterator_cast(__first);
3189 difference_type __d = __last - __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003190 _VSTD::copy(__last, this->cend(), __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003191 __size_ -= __d;
3192 return __r;
3193}
3194
3195template <class _Allocator>
3196void
3197vector<bool, _Allocator>::swap(vector& __x)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003198#if _LIBCPP_STD_VER >= 14
3199 _NOEXCEPT
3200#else
Eric Fiselier69c51982016-12-28 06:06:09 +00003201 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003202 __is_nothrow_swappable<allocator_type>::value)
3203#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003204{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003205 _VSTD::swap(this->__begin_, __x.__begin_);
3206 _VSTD::swap(this->__size_, __x.__size_);
3207 _VSTD::swap(this->__cap(), __x.__cap());
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05003208 _VSTD::__swap_allocator(this->__alloc(), __x.__alloc(),
Marshall Clow8982dcd2015-07-13 20:04:56 +00003209 integral_constant<bool, __alloc_traits::propagate_on_container_swap::value>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003210}
3211
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003212template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003213void
3214vector<bool, _Allocator>::resize(size_type __sz, value_type __x)
3215{
3216 size_type __cs = size();
3217 if (__cs < __sz)
3218 {
3219 iterator __r;
3220 size_type __c = capacity();
3221 size_type __n = __sz - __cs;
3222 if (__n <= __c && __cs <= __c - __n)
3223 {
3224 __r = end();
3225 __size_ += __n;
3226 }
3227 else
3228 {
Mikhail Maltsevf56b0692021-10-21 10:38:56 +01003229 vector __v(get_allocator());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003230 __v.reserve(__recommend(__size_ + __n));
3231 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003232 __r = _VSTD::copy(cbegin(), cend(), __v.begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003233 swap(__v);
3234 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003235 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003236 }
3237 else
3238 __size_ = __sz;
3239}
3240
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003241template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003242void
Howard Hinnant1c936782011-06-03 19:40:40 +00003243vector<bool, _Allocator>::flip() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003244{
3245 // do middle whole words
3246 size_type __n = __size_;
3247 __storage_pointer __p = __begin_;
3248 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3249 *__p = ~*__p;
3250 // do last partial word
3251 if (__n > 0)
3252 {
3253 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3254 __storage_type __b = *__p & __m;
3255 *__p &= ~__m;
3256 *__p |= ~__b & __m;
3257 }
3258}
3259
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003260template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003261bool
3262vector<bool, _Allocator>::__invariants() const
3263{
Howard Hinnant76053d72013-06-27 19:35:32 +00003264 if (this->__begin_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003265 {
3266 if (this->__size_ != 0 || this->__cap() != 0)
3267 return false;
3268 }
3269 else
3270 {
3271 if (this->__cap() == 0)
3272 return false;
3273 if (this->__size_ > this->capacity())
3274 return false;
3275 }
3276 return true;
3277}
3278
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003279template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003280size_t
Howard Hinnant1c936782011-06-03 19:40:40 +00003281vector<bool, _Allocator>::__hash_code() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003282{
3283 size_t __h = 0;
3284 // do middle whole words
3285 size_type __n = __size_;
3286 __storage_pointer __p = __begin_;
3287 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3288 __h ^= *__p;
3289 // do last partial word
3290 if (__n > 0)
3291 {
3292 const __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3293 __h ^= *__p & __m;
3294 }
3295 return __h;
3296}
3297
3298template <class _Allocator>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003299struct _LIBCPP_TEMPLATE_VIS hash<vector<bool, _Allocator> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00003300 : public unary_function<vector<bool, _Allocator>, size_t>
3301{
Howard Hinnant1c265cd2010-09-23 18:58:28 +00003302 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +00003303 size_t operator()(const vector<bool, _Allocator>& __vec) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003304 {return __vec.__hash_code();}
3305};
3306
3307template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003308inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003309bool
3310operator==(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3311{
3312 const typename vector<_Tp, _Allocator>::size_type __sz = __x.size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003313 return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003314}
3315
3316template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003317inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003318bool
3319operator!=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3320{
3321 return !(__x == __y);
3322}
3323
3324template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003325inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003326bool
3327operator< (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3328{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003329 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003330}
3331
3332template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003333inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003334bool
3335operator> (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3336{
3337 return __y < __x;
3338}
3339
3340template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003341inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003342bool
3343operator>=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3344{
3345 return !(__x < __y);
3346}
3347
3348template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003349inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003350bool
3351operator<=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3352{
3353 return !(__y < __x);
3354}
3355
3356template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003357inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003358void
3359swap(vector<_Tp, _Allocator>& __x, vector<_Tp, _Allocator>& __y)
Howard Hinnant1c936782011-06-03 19:40:40 +00003360 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00003361{
3362 __x.swap(__y);
3363}
3364
Marshall Clow29b53f22018-12-14 18:49:35 +00003365#if _LIBCPP_STD_VER > 17
3366template <class _Tp, class _Allocator, class _Up>
Marek Kurdeja98b1412020-05-02 13:58:03 +02003367inline _LIBCPP_INLINE_VISIBILITY typename vector<_Tp, _Allocator>::size_type
3368erase(vector<_Tp, _Allocator>& __c, const _Up& __v) {
3369 auto __old_size = __c.size();
3370 __c.erase(_VSTD::remove(__c.begin(), __c.end(), __v), __c.end());
3371 return __old_size - __c.size();
3372}
Marshall Clow29b53f22018-12-14 18:49:35 +00003373
3374template <class _Tp, class _Allocator, class _Predicate>
Marek Kurdeja98b1412020-05-02 13:58:03 +02003375inline _LIBCPP_INLINE_VISIBILITY typename vector<_Tp, _Allocator>::size_type
3376erase_if(vector<_Tp, _Allocator>& __c, _Predicate __pred) {
3377 auto __old_size = __c.size();
3378 __c.erase(_VSTD::remove_if(__c.begin(), __c.end(), __pred), __c.end());
3379 return __old_size - __c.size();
3380}
Marshall Clow29b53f22018-12-14 18:49:35 +00003381#endif
3382
Howard Hinnantc51e1022010-05-11 19:42:16 +00003383_LIBCPP_END_NAMESPACE_STD
3384
Eric Fiselierf4433a32017-05-31 22:07:49 +00003385_LIBCPP_POP_MACROS
3386
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003387#endif // _LIBCPP_VECTOR