blob: 80819080a9fbe40b9573dac64c73bd2552c5e4cd [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===------------------------------ vector --------------------------------===//
3//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_VECTOR
11#define _LIBCPP_VECTOR
12
13/*
14 vector synopsis
15
16namespace std
17{
18
Howard Hinnant3b6579a2010-08-22 00:02:43 +000019template <class T, class Allocator = allocator<T> >
Howard Hinnantc51e1022010-05-11 19:42:16 +000020class vector
Howard Hinnant3b6579a2010-08-22 00:02:43 +000021{
22public:
23 typedef T value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +000024 typedef Allocator allocator_type;
25 typedef typename allocator_type::reference reference;
26 typedef typename allocator_type::const_reference const_reference;
27 typedef implementation-defined iterator;
28 typedef implementation-defined const_iterator;
29 typedef typename allocator_type::size_type size_type;
30 typedef typename allocator_type::difference_type difference_type;
31 typedef typename allocator_type::pointer pointer;
32 typedef typename allocator_type::const_pointer const_pointer;
33 typedef std::reverse_iterator<iterator> reverse_iterator;
34 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
35
Howard Hinnant1c936782011-06-03 19:40:40 +000036 vector()
37 noexcept(is_nothrow_default_constructible<allocator_type>::value);
38 explicit vector(const allocator_type&);
Howard Hinnantc51e1022010-05-11 19:42:16 +000039 explicit vector(size_type n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +000040 explicit vector(size_type n, const allocator_type&); // C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +000041 vector(size_type n, const value_type& value, const allocator_type& = allocator_type());
42 template <class InputIterator>
43 vector(InputIterator first, InputIterator last, const allocator_type& = allocator_type());
44 vector(const vector& x);
Howard Hinnant1c936782011-06-03 19:40:40 +000045 vector(vector&& x)
46 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +000047 vector(initializer_list<value_type> il);
48 vector(initializer_list<value_type> il, const allocator_type& a);
49 ~vector();
50 vector& operator=(const vector& x);
Howard Hinnant1c936782011-06-03 19:40:40 +000051 vector& operator=(vector&& x)
52 noexcept(
Marshall Clow2fe8a8d2015-08-18 18:57:00 +000053 allocator_type::propagate_on_container_move_assignment::value ||
54 allocator_type::is_always_equal::value); // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +000055 vector& operator=(initializer_list<value_type> il);
56 template <class InputIterator>
57 void assign(InputIterator first, InputIterator last);
58 void assign(size_type n, const value_type& u);
59 void assign(initializer_list<value_type> il);
60
Howard Hinnant1c936782011-06-03 19:40:40 +000061 allocator_type get_allocator() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000062
Howard Hinnant1c936782011-06-03 19:40:40 +000063 iterator begin() noexcept;
64 const_iterator begin() const noexcept;
65 iterator end() noexcept;
66 const_iterator end() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000067
Howard Hinnant1c936782011-06-03 19:40:40 +000068 reverse_iterator rbegin() noexcept;
69 const_reverse_iterator rbegin() const noexcept;
70 reverse_iterator rend() noexcept;
71 const_reverse_iterator rend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000072
Howard Hinnant1c936782011-06-03 19:40:40 +000073 const_iterator cbegin() const noexcept;
74 const_iterator cend() const noexcept;
75 const_reverse_iterator crbegin() const noexcept;
76 const_reverse_iterator crend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000077
Howard Hinnant1c936782011-06-03 19:40:40 +000078 size_type size() const noexcept;
79 size_type max_size() const noexcept;
80 size_type capacity() const noexcept;
81 bool empty() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000082 void reserve(size_type n);
Howard Hinnant1c936782011-06-03 19:40:40 +000083 void shrink_to_fit() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000084
85 reference operator[](size_type n);
86 const_reference operator[](size_type n) const;
87 reference at(size_type n);
88 const_reference at(size_type n) const;
89
90 reference front();
91 const_reference front() const;
92 reference back();
93 const_reference back() const;
94
Howard Hinnant1c936782011-06-03 19:40:40 +000095 value_type* data() noexcept;
96 const value_type* data() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000097
98 void push_back(const value_type& x);
99 void push_back(value_type&& x);
100 template <class... Args>
Marshall Clowea52cc42017-01-24 23:09:12 +0000101 reference emplace_back(Args&&... args); // reference in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000102 void pop_back();
103
104 template <class... Args> iterator emplace(const_iterator position, Args&&... args);
105 iterator insert(const_iterator position, const value_type& x);
106 iterator insert(const_iterator position, value_type&& x);
107 iterator insert(const_iterator position, size_type n, const value_type& x);
108 template <class InputIterator>
109 iterator insert(const_iterator position, InputIterator first, InputIterator last);
110 iterator insert(const_iterator position, initializer_list<value_type> il);
111
112 iterator erase(const_iterator position);
113 iterator erase(const_iterator first, const_iterator last);
114
Howard Hinnant1c936782011-06-03 19:40:40 +0000115 void clear() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000116
117 void resize(size_type sz);
118 void resize(size_type sz, const value_type& c);
119
Howard Hinnant1c936782011-06-03 19:40:40 +0000120 void swap(vector&)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000121 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
122 allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000123
124 bool __invariants() const;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000125};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000126
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000127template <class Allocator = allocator<T> >
Howard Hinnantc51e1022010-05-11 19:42:16 +0000128class vector<bool, Allocator>
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000129{
130public:
131 typedef bool value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000132 typedef Allocator allocator_type;
133 typedef implementation-defined iterator;
134 typedef implementation-defined const_iterator;
135 typedef typename allocator_type::size_type size_type;
136 typedef typename allocator_type::difference_type difference_type;
137 typedef iterator pointer;
138 typedef const_iterator const_pointer;
139 typedef std::reverse_iterator<iterator> reverse_iterator;
140 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
141
142 class reference
143 {
144 public:
Howard Hinnant1c936782011-06-03 19:40:40 +0000145 reference(const reference&) noexcept;
146 operator bool() const noexcept;
Louis Dionned9f528e2021-06-29 13:52:26 -0400147 reference& operator=(bool x) noexcept;
Howard Hinnant1c936782011-06-03 19:40:40 +0000148 reference& operator=(const reference& x) noexcept;
149 iterator operator&() const noexcept;
150 void flip() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000151 };
152
153 class const_reference
154 {
155 public:
Howard Hinnant1c936782011-06-03 19:40:40 +0000156 const_reference(const reference&) noexcept;
157 operator bool() const noexcept;
158 const_iterator operator&() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000159 };
160
Howard Hinnant1c936782011-06-03 19:40:40 +0000161 vector()
162 noexcept(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnantc2734962011-09-02 20:42:31 +0000163 explicit vector(const allocator_type&);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +0000164 explicit vector(size_type n, const allocator_type& a = allocator_type()); // C++14
165 vector(size_type n, const value_type& value, const allocator_type& = allocator_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000166 template <class InputIterator>
167 vector(InputIterator first, InputIterator last, const allocator_type& = allocator_type());
168 vector(const vector& x);
Howard Hinnant1c936782011-06-03 19:40:40 +0000169 vector(vector&& x)
170 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000171 vector(initializer_list<value_type> il);
172 vector(initializer_list<value_type> il, const allocator_type& a);
173 ~vector();
174 vector& operator=(const vector& x);
Howard Hinnant1c936782011-06-03 19:40:40 +0000175 vector& operator=(vector&& x)
176 noexcept(
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000177 allocator_type::propagate_on_container_move_assignment::value ||
178 allocator_type::is_always_equal::value); // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000179 vector& operator=(initializer_list<value_type> il);
180 template <class InputIterator>
181 void assign(InputIterator first, InputIterator last);
182 void assign(size_type n, const value_type& u);
183 void assign(initializer_list<value_type> il);
184
Howard Hinnant1c936782011-06-03 19:40:40 +0000185 allocator_type get_allocator() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000186
Howard Hinnant1c936782011-06-03 19:40:40 +0000187 iterator begin() noexcept;
188 const_iterator begin() const noexcept;
189 iterator end() noexcept;
190 const_iterator end() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000191
Howard Hinnant1c936782011-06-03 19:40:40 +0000192 reverse_iterator rbegin() noexcept;
193 const_reverse_iterator rbegin() const noexcept;
194 reverse_iterator rend() noexcept;
195 const_reverse_iterator rend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000196
Howard Hinnant1c936782011-06-03 19:40:40 +0000197 const_iterator cbegin() const noexcept;
198 const_iterator cend() const noexcept;
199 const_reverse_iterator crbegin() const noexcept;
200 const_reverse_iterator crend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000201
Howard Hinnant1c936782011-06-03 19:40:40 +0000202 size_type size() const noexcept;
203 size_type max_size() const noexcept;
204 size_type capacity() const noexcept;
205 bool empty() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000206 void reserve(size_type n);
Howard Hinnant1c936782011-06-03 19:40:40 +0000207 void shrink_to_fit() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000208
209 reference operator[](size_type n);
210 const_reference operator[](size_type n) const;
211 reference at(size_type n);
212 const_reference at(size_type n) const;
213
214 reference front();
215 const_reference front() const;
216 reference back();
217 const_reference back() const;
218
219 void push_back(const value_type& x);
Marshall Clowea52cc42017-01-24 23:09:12 +0000220 template <class... Args> reference emplace_back(Args&&... args); // C++14; reference in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000221 void pop_back();
222
Marshall Clowc46bb8e2013-08-13 23:54:12 +0000223 template <class... Args> iterator emplace(const_iterator position, Args&&... args); // C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000224 iterator insert(const_iterator position, const value_type& x);
225 iterator insert(const_iterator position, size_type n, const value_type& x);
226 template <class InputIterator>
227 iterator insert(const_iterator position, InputIterator first, InputIterator last);
228 iterator insert(const_iterator position, initializer_list<value_type> il);
229
230 iterator erase(const_iterator position);
231 iterator erase(const_iterator first, const_iterator last);
232
Howard Hinnant1c936782011-06-03 19:40:40 +0000233 void clear() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000234
235 void resize(size_type sz);
236 void resize(size_type sz, value_type x);
237
Howard Hinnant1c936782011-06-03 19:40:40 +0000238 void swap(vector&)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000239 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
240 allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnant1c936782011-06-03 19:40:40 +0000241 void flip() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000242
243 bool __invariants() const;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000244};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000245
Marshall Clowf0ca1492018-05-21 21:30:12 +0000246template <class InputIterator, class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
247 vector(InputIterator, InputIterator, Allocator = Allocator())
248 -> vector<typename iterator_traits<InputIterator>::value_type, Allocator>;
249
Howard Hinnantc51e1022010-05-11 19:42:16 +0000250template <class Allocator> struct hash<std::vector<bool, Allocator>>;
251
252template <class T, class Allocator> bool operator==(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
253template <class T, class Allocator> bool operator< (const vector<T,Allocator>& x, const vector<T,Allocator>& y);
254template <class T, class Allocator> bool operator!=(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
255template <class T, class Allocator> bool operator> (const vector<T,Allocator>& x, const vector<T,Allocator>& y);
256template <class T, class Allocator> bool operator>=(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
257template <class T, class Allocator> bool operator<=(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
258
Howard Hinnant1c936782011-06-03 19:40:40 +0000259template <class T, class Allocator>
260void swap(vector<T,Allocator>& x, vector<T,Allocator>& y)
261 noexcept(noexcept(x.swap(y)));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000262
Marshall Clow29b53f22018-12-14 18:49:35 +0000263template <class T, class Allocator, class U>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200264typename vector<T, Allocator>::size_type
265erase(vector<T, Allocator>& c, const U& value); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000266template <class T, class Allocator, class Predicate>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200267typename vector<T, Allocator>::size_type
268erase_if(vector<T, Allocator>& c, Predicate pred); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000269
Howard Hinnantc51e1022010-05-11 19:42:16 +0000270} // std
271
272*/
273
274#include <__config>
275#include <__bit_reference>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400276#include <__debug>
277#include <__functional_base>
Louis Dionne77249522021-06-11 09:55:11 -0400278#include <__iterator/wrap_iter.h>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400279#include <__split_buffer>
Christopher Di Bella41f26e82021-06-05 02:47:47 +0000280#include <__utility/forward.h>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400281#include <algorithm>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000282#include <climits>
Arthur O'Dwyer7deec122021-03-24 18:19:12 -0400283#include <compare>
Louis Dionned24191c2021-08-19 12:39:16 -0400284#include <cstdlib>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400285#include <cstring>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000286#include <initializer_list>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400287#include <iosfwd> // for forward declaration of vector
288#include <limits>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000289#include <memory>
290#include <stdexcept>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400291#include <type_traits>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000292#include <version>
Howard Hinnante6ff0b62013-08-02 00:26:35 +0000293
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000294#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000295#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000296#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000297
Eric Fiselierf4433a32017-05-31 22:07:49 +0000298_LIBCPP_PUSH_MACROS
299#include <__undef_macros>
300
301
Howard Hinnantc51e1022010-05-11 19:42:16 +0000302_LIBCPP_BEGIN_NAMESPACE_STD
303
304template <bool>
Louis Dionneb6aabd92021-08-19 12:21:06 -0400305struct __vector_base_common;
306
307template <>
308struct __vector_base_common<true> {
309 // Both are defined in vector.cpp
310 _LIBCPP_NORETURN _LIBCPP_EXPORTED_FROM_ABI void __throw_length_error() const;
311 _LIBCPP_NORETURN _LIBCPP_EXPORTED_FROM_ABI void __throw_out_of_range() const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000312};
313
Howard Hinnantc51e1022010-05-11 19:42:16 +0000314template <class _Tp, class _Allocator>
315class __vector_base
Louis Dionneb6aabd92021-08-19 12:21:06 -0400316 : protected __vector_base_common<true> // This base class is historical, but it needs to remain for ABI compatibility
Howard Hinnantc51e1022010-05-11 19:42:16 +0000317{
Marshall Clowf0ca1492018-05-21 21:30:12 +0000318public:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000319 typedef _Allocator allocator_type;
320 typedef allocator_traits<allocator_type> __alloc_traits;
Marshall Clowf0ca1492018-05-21 21:30:12 +0000321 typedef typename __alloc_traits::size_type size_type;
322protected:
323 typedef _Tp value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000324 typedef value_type& reference;
325 typedef const value_type& const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000326 typedef typename __alloc_traits::difference_type difference_type;
327 typedef typename __alloc_traits::pointer pointer;
328 typedef typename __alloc_traits::const_pointer const_pointer;
329 typedef pointer iterator;
330 typedef const_pointer const_iterator;
331
332 pointer __begin_;
333 pointer __end_;
334 __compressed_pair<pointer, allocator_type> __end_cap_;
335
Howard Hinnant1c936782011-06-03 19:40:40 +0000336 _LIBCPP_INLINE_VISIBILITY
337 allocator_type& __alloc() _NOEXCEPT
338 {return __end_cap_.second();}
339 _LIBCPP_INLINE_VISIBILITY
340 const allocator_type& __alloc() const _NOEXCEPT
341 {return __end_cap_.second();}
342 _LIBCPP_INLINE_VISIBILITY
343 pointer& __end_cap() _NOEXCEPT
344 {return __end_cap_.first();}
345 _LIBCPP_INLINE_VISIBILITY
346 const pointer& __end_cap() const _NOEXCEPT
347 {return __end_cap_.first();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000348
Howard Hinnant1c936782011-06-03 19:40:40 +0000349 _LIBCPP_INLINE_VISIBILITY
350 __vector_base()
351 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnantcf823322010-12-17 14:46:43 +0000352 _LIBCPP_INLINE_VISIBILITY __vector_base(const allocator_type& __a);
Eric Fiselier4f1534c2018-06-05 22:32:52 +0000353#ifndef _LIBCPP_CXX03_LANG
354 _LIBCPP_INLINE_VISIBILITY __vector_base(allocator_type&& __a) _NOEXCEPT;
355#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000356 ~__vector_base();
357
Howard Hinnant1c936782011-06-03 19:40:40 +0000358 _LIBCPP_INLINE_VISIBILITY
359 void clear() _NOEXCEPT {__destruct_at_end(__begin_);}
360 _LIBCPP_INLINE_VISIBILITY
361 size_type capacity() const _NOEXCEPT
362 {return static_cast<size_type>(__end_cap() - __begin_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000363
Howard Hinnant1c936782011-06-03 19:40:40 +0000364 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76053d72013-06-27 19:35:32 +0000365 void __destruct_at_end(pointer __new_last) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000366
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000367 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000368 void __copy_assign_alloc(const __vector_base& __c)
369 {__copy_assign_alloc(__c, integral_constant<bool,
370 __alloc_traits::propagate_on_container_copy_assignment::value>());}
371
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000372 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000373 void __move_assign_alloc(__vector_base& __c)
Howard Hinnant1c936782011-06-03 19:40:40 +0000374 _NOEXCEPT_(
375 !__alloc_traits::propagate_on_container_move_assignment::value ||
376 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000377 {__move_assign_alloc(__c, integral_constant<bool,
378 __alloc_traits::propagate_on_container_move_assignment::value>());}
Louis Dionned24191c2021-08-19 12:39:16 -0400379
380 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
381 void __throw_length_error() const {
382#ifndef _LIBCPP_NO_EXCEPTIONS
383 __vector_base_common<true>::__throw_length_error();
384#else
385 _VSTD::abort();
386#endif
387 }
388
389 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
390 void __throw_out_of_range() const {
391#ifndef _LIBCPP_NO_EXCEPTIONS
392 __vector_base_common<true>::__throw_out_of_range();
393#else
394 _VSTD::abort();
395#endif
396 }
397
Howard Hinnantc51e1022010-05-11 19:42:16 +0000398private:
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000399 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000400 void __copy_assign_alloc(const __vector_base& __c, true_type)
401 {
402 if (__alloc() != __c.__alloc())
403 {
404 clear();
405 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
406 __begin_ = __end_ = __end_cap() = nullptr;
407 }
408 __alloc() = __c.__alloc();
409 }
410
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000411 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +0000412 void __copy_assign_alloc(const __vector_base&, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000413 {}
414
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000415 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +0000416 void __move_assign_alloc(__vector_base& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +0000417 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000418 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000419 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000420 }
421
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000422 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +0000423 void __move_assign_alloc(__vector_base&, false_type)
Howard Hinnant1c936782011-06-03 19:40:40 +0000424 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000425 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000426};
427
428template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000429inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000430void
Howard Hinnant76053d72013-06-27 19:35:32 +0000431__vector_base<_Tp, _Allocator>::__destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000432{
Bruce Mitchener405fdcd2017-03-23 14:39:23 +0000433 pointer __soon_to_be_end = __end_;
434 while (__new_last != __soon_to_be_end)
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500435 __alloc_traits::destroy(__alloc(), _VSTD::__to_address(--__soon_to_be_end));
Bruce Mitchener405fdcd2017-03-23 14:39:23 +0000436 __end_ = __new_last;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000437}
438
439template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000440inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000441__vector_base<_Tp, _Allocator>::__vector_base()
Howard Hinnant1c936782011-06-03 19:40:40 +0000442 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant76053d72013-06-27 19:35:32 +0000443 : __begin_(nullptr),
444 __end_(nullptr),
Eric Fiselier33ebfb62019-12-16 18:23:39 -0500445 __end_cap_(nullptr, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +0000446{
447}
448
449template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000450inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000451__vector_base<_Tp, _Allocator>::__vector_base(const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +0000452 : __begin_(nullptr),
453 __end_(nullptr),
454 __end_cap_(nullptr, __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000455{
456}
457
Eric Fiselier4f1534c2018-06-05 22:32:52 +0000458#ifndef _LIBCPP_CXX03_LANG
459template <class _Tp, class _Allocator>
460inline _LIBCPP_INLINE_VISIBILITY
461__vector_base<_Tp, _Allocator>::__vector_base(allocator_type&& __a) _NOEXCEPT
462 : __begin_(nullptr),
463 __end_(nullptr),
Arthur O'Dwyer07b22492020-11-27 11:02:06 -0500464 __end_cap_(nullptr, _VSTD::move(__a)) {}
Eric Fiselier4f1534c2018-06-05 22:32:52 +0000465#endif
466
Howard Hinnantc51e1022010-05-11 19:42:16 +0000467template <class _Tp, class _Allocator>
468__vector_base<_Tp, _Allocator>::~__vector_base()
469{
Howard Hinnant76053d72013-06-27 19:35:32 +0000470 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000471 {
472 clear();
473 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
474 }
475}
476
Eric Fiselier876c6862016-02-20 00:19:45 +0000477template <class _Tp, class _Allocator /* = allocator<_Tp> */>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000478class _LIBCPP_TEMPLATE_VIS vector
Howard Hinnantc51e1022010-05-11 19:42:16 +0000479 : private __vector_base<_Tp, _Allocator>
480{
481private:
482 typedef __vector_base<_Tp, _Allocator> __base;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000483 typedef allocator<_Tp> __default_allocator_type;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000484public:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000485 typedef vector __self;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000486 typedef _Tp value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000487 typedef _Allocator allocator_type;
488 typedef typename __base::__alloc_traits __alloc_traits;
489 typedef typename __base::reference reference;
490 typedef typename __base::const_reference const_reference;
491 typedef typename __base::size_type size_type;
492 typedef typename __base::difference_type difference_type;
493 typedef typename __base::pointer pointer;
494 typedef typename __base::const_pointer const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000495 typedef __wrap_iter<pointer> iterator;
496 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000497 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
498 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000499
Howard Hinnanta416ff02013-03-26 19:04:56 +0000500 static_assert((is_same<typename allocator_type::value_type, value_type>::value),
501 "Allocator::value_type must be same type as value_type");
502
Howard Hinnant1c936782011-06-03 19:40:40 +0000503 _LIBCPP_INLINE_VISIBILITY
Marshall Clowe546cbb2015-06-04 02:05:41 +0000504 vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant27e0e772011-09-14 18:33:51 +0000505 {
Louis Dionneba400782020-10-02 15:02:52 -0400506#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000507 __get_db()->__insert_c(this);
508#endif
509 }
510 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
Marshall Clow8f282f42015-06-04 00:10:20 +0000511#if _LIBCPP_STD_VER <= 14
512 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
513#else
514 _NOEXCEPT
515#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +0000516 : __base(__a)
517 {
Louis Dionneba400782020-10-02 15:02:52 -0400518#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000519 __get_db()->__insert_c(this);
520#endif
521 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000522 explicit vector(size_type __n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +0000523#if _LIBCPP_STD_VER > 11
524 explicit vector(size_type __n, const allocator_type& __a);
525#endif
Marshall Clowf0ca1492018-05-21 21:30:12 +0000526 vector(size_type __n, const value_type& __x);
527 vector(size_type __n, const value_type& __x, const allocator_type& __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000528 template <class _InputIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +0000529 vector(_InputIterator __first,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500530 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
531 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000532 is_constructible<
533 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +0000534 typename iterator_traits<_InputIterator>::reference>::value,
535 _InputIterator>::type __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000536 template <class _InputIterator>
537 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500538 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
539 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000540 is_constructible<
541 value_type,
542 typename iterator_traits<_InputIterator>::reference>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000543 template <class _ForwardIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +0000544 vector(_ForwardIterator __first,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500545 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000546 is_constructible<
547 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +0000548 typename iterator_traits<_ForwardIterator>::reference>::value,
549 _ForwardIterator>::type __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000550 template <class _ForwardIterator>
551 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500552 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000553 is_constructible<
554 value_type,
555 typename iterator_traits<_ForwardIterator>::reference>::value>::type* = 0);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000556
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000557 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant27e0e772011-09-14 18:33:51 +0000558 ~vector()
559 {
Marshall Clow68af4f32018-09-07 15:47:59 +0000560 __annotate_delete();
Louis Dionneba400782020-10-02 15:02:52 -0400561#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000562 __get_db()->__erase_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000563#endif
Marshall Clow68af4f32018-09-07 15:47:59 +0000564 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000565
566 vector(const vector& __x);
Arthur O'Dwyer9b9662f2021-03-01 17:08:24 -0500567 vector(const vector& __x, const __identity_t<allocator_type>& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000568 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000569 vector& operator=(const vector& __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000570
571#ifndef _LIBCPP_CXX03_LANG
572 _LIBCPP_INLINE_VISIBILITY
573 vector(initializer_list<value_type> __il);
574
575 _LIBCPP_INLINE_VISIBILITY
576 vector(initializer_list<value_type> __il, const allocator_type& __a);
577
Howard Hinnantcf823322010-12-17 14:46:43 +0000578 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000579 vector(vector&& __x)
Marshall Clowe5108202015-07-14 14:46:32 +0000580#if _LIBCPP_STD_VER > 14
581 _NOEXCEPT;
582#else
Howard Hinnant1c936782011-06-03 19:40:40 +0000583 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowe5108202015-07-14 14:46:32 +0000584#endif
Eric Fiseliered9e9362017-04-16 02:40:45 +0000585
Howard Hinnantcf823322010-12-17 14:46:43 +0000586 _LIBCPP_INLINE_VISIBILITY
Arthur O'Dwyer9b9662f2021-03-01 17:08:24 -0500587 vector(vector&& __x, const __identity_t<allocator_type>& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000588 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000589 vector& operator=(vector&& __x)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000590 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiseliered9e9362017-04-16 02:40:45 +0000591
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000592 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000593 vector& operator=(initializer_list<value_type> __il)
594 {assign(__il.begin(), __il.end()); return *this;}
Eric Fiseliered9e9362017-04-16 02:40:45 +0000595
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400596#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000597
598 template <class _InputIterator>
599 typename enable_if
600 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500601 __is_cpp17_input_iterator <_InputIterator>::value &&
602 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000603 is_constructible<
604 value_type,
605 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000606 void
607 >::type
608 assign(_InputIterator __first, _InputIterator __last);
609 template <class _ForwardIterator>
610 typename enable_if
611 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500612 __is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000613 is_constructible<
614 value_type,
615 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000616 void
617 >::type
618 assign(_ForwardIterator __first, _ForwardIterator __last);
619
620 void assign(size_type __n, const_reference __u);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000621
622#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000623 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000624 void assign(initializer_list<value_type> __il)
625 {assign(__il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +0000626#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000627
Howard Hinnant1c936782011-06-03 19:40:40 +0000628 _LIBCPP_INLINE_VISIBILITY
629 allocator_type get_allocator() const _NOEXCEPT
630 {return this->__alloc();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000631
Howard Hinnant1c936782011-06-03 19:40:40 +0000632 _LIBCPP_INLINE_VISIBILITY iterator begin() _NOEXCEPT;
633 _LIBCPP_INLINE_VISIBILITY const_iterator begin() const _NOEXCEPT;
634 _LIBCPP_INLINE_VISIBILITY iterator end() _NOEXCEPT;
635 _LIBCPP_INLINE_VISIBILITY const_iterator end() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000636
Howard Hinnant1c936782011-06-03 19:40:40 +0000637 _LIBCPP_INLINE_VISIBILITY
638 reverse_iterator rbegin() _NOEXCEPT
639 {return reverse_iterator(end());}
640 _LIBCPP_INLINE_VISIBILITY
641 const_reverse_iterator rbegin() const _NOEXCEPT
642 {return const_reverse_iterator(end());}
643 _LIBCPP_INLINE_VISIBILITY
644 reverse_iterator rend() _NOEXCEPT
645 {return reverse_iterator(begin());}
646 _LIBCPP_INLINE_VISIBILITY
647 const_reverse_iterator rend() const _NOEXCEPT
648 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000649
Howard Hinnant1c936782011-06-03 19:40:40 +0000650 _LIBCPP_INLINE_VISIBILITY
651 const_iterator cbegin() const _NOEXCEPT
652 {return begin();}
653 _LIBCPP_INLINE_VISIBILITY
654 const_iterator cend() const _NOEXCEPT
655 {return end();}
656 _LIBCPP_INLINE_VISIBILITY
657 const_reverse_iterator crbegin() const _NOEXCEPT
658 {return rbegin();}
659 _LIBCPP_INLINE_VISIBILITY
660 const_reverse_iterator crend() const _NOEXCEPT
661 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000662
Howard Hinnant1c936782011-06-03 19:40:40 +0000663 _LIBCPP_INLINE_VISIBILITY
664 size_type size() const _NOEXCEPT
665 {return static_cast<size_type>(this->__end_ - this->__begin_);}
666 _LIBCPP_INLINE_VISIBILITY
667 size_type capacity() const _NOEXCEPT
668 {return __base::capacity();}
Marshall Clow425f5752017-11-15 05:51:26 +0000669 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000670 bool empty() const _NOEXCEPT
671 {return this->__begin_ == this->__end_;}
672 size_type max_size() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000673 void reserve(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +0000674 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000675
Marshall Clowd6470492019-03-15 00:29:35 +0000676 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) _NOEXCEPT;
677 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000678 reference at(size_type __n);
679 const_reference at(size_type __n) const;
680
Marshall Clowd6470492019-03-15 00:29:35 +0000681 _LIBCPP_INLINE_VISIBILITY reference front() _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000682 {
Kristina Bessonovaaeeaa7e2021-05-09 19:29:56 +0200683 _LIBCPP_ASSERT(!empty(), "front() called on an empty vector");
Howard Hinnant27e0e772011-09-14 18:33:51 +0000684 return *this->__begin_;
685 }
Marshall Clowd6470492019-03-15 00:29:35 +0000686 _LIBCPP_INLINE_VISIBILITY const_reference front() const _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000687 {
Kristina Bessonovaaeeaa7e2021-05-09 19:29:56 +0200688 _LIBCPP_ASSERT(!empty(), "front() called on an empty vector");
Howard Hinnant27e0e772011-09-14 18:33:51 +0000689 return *this->__begin_;
690 }
Marshall Clowd6470492019-03-15 00:29:35 +0000691 _LIBCPP_INLINE_VISIBILITY reference back() _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000692 {
Kristina Bessonovaaeeaa7e2021-05-09 19:29:56 +0200693 _LIBCPP_ASSERT(!empty(), "back() called on an empty vector");
Howard Hinnant27e0e772011-09-14 18:33:51 +0000694 return *(this->__end_ - 1);
695 }
Marshall Clowd6470492019-03-15 00:29:35 +0000696 _LIBCPP_INLINE_VISIBILITY const_reference back() const _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000697 {
Kristina Bessonovaaeeaa7e2021-05-09 19:29:56 +0200698 _LIBCPP_ASSERT(!empty(), "back() called on an empty vector");
Howard Hinnant27e0e772011-09-14 18:33:51 +0000699 return *(this->__end_ - 1);
700 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000701
Howard Hinnant1c936782011-06-03 19:40:40 +0000702 _LIBCPP_INLINE_VISIBILITY
703 value_type* data() _NOEXCEPT
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500704 {return _VSTD::__to_address(this->__begin_);}
Howard Hinnant1c936782011-06-03 19:40:40 +0000705 _LIBCPP_INLINE_VISIBILITY
706 const value_type* data() const _NOEXCEPT
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500707 {return _VSTD::__to_address(this->__begin_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000708
Eric Fiselier96919722017-10-17 13:03:17 +0000709#ifdef _LIBCPP_CXX03_LANG
710 _LIBCPP_INLINE_VISIBILITY
711 void __emplace_back(const value_type& __x) { push_back(__x); }
712#else
713 template <class _Arg>
714 _LIBCPP_INLINE_VISIBILITY
715 void __emplace_back(_Arg&& __arg) {
716 emplace_back(_VSTD::forward<_Arg>(__arg));
717 }
718#endif
719
Howard Hinnantcf823322010-12-17 14:46:43 +0000720 _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000721
722#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000723 _LIBCPP_INLINE_VISIBILITY void push_back(value_type&& __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000724
Howard Hinnantc51e1022010-05-11 19:42:16 +0000725 template <class... _Args>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000726 _LIBCPP_INLINE_VISIBILITY
Marshall Clowea52cc42017-01-24 23:09:12 +0000727#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +0000728 reference emplace_back(_Args&&... __args);
Marshall Clowea52cc42017-01-24 23:09:12 +0000729#else
730 void emplace_back(_Args&&... __args);
731#endif
Eric Fiseliered9e9362017-04-16 02:40:45 +0000732#endif // !_LIBCPP_CXX03_LANG
733
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000734 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000735 void pop_back();
736
737 iterator insert(const_iterator __position, const_reference __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000738
739#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000740 iterator insert(const_iterator __position, value_type&& __x);
741 template <class... _Args>
742 iterator emplace(const_iterator __position, _Args&&... __args);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400743#endif // !_LIBCPP_CXX03_LANG
Eric Fiseliered9e9362017-04-16 02:40:45 +0000744
Howard Hinnantc51e1022010-05-11 19:42:16 +0000745 iterator insert(const_iterator __position, size_type __n, const_reference __x);
746 template <class _InputIterator>
747 typename enable_if
748 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500749 __is_cpp17_input_iterator <_InputIterator>::value &&
750 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000751 is_constructible<
752 value_type,
753 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000754 iterator
755 >::type
756 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
757 template <class _ForwardIterator>
758 typename enable_if
759 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500760 __is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000761 is_constructible<
762 value_type,
763 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000764 iterator
765 >::type
766 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000767
768#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000769 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000770 iterator insert(const_iterator __position, initializer_list<value_type> __il)
771 {return insert(__position, __il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +0000772#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000773
Howard Hinnantcf823322010-12-17 14:46:43 +0000774 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000775 iterator erase(const_iterator __first, const_iterator __last);
776
Howard Hinnant1c936782011-06-03 19:40:40 +0000777 _LIBCPP_INLINE_VISIBILITY
778 void clear() _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000779 {
Marshall Clow2cd9d372014-05-08 14:14:06 +0000780 size_type __old_size = size();
Howard Hinnant27e0e772011-09-14 18:33:51 +0000781 __base::clear();
Marshall Clow2cd9d372014-05-08 14:14:06 +0000782 __annotate_shrink(__old_size);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000783 __invalidate_all_iterators();
784 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000785
786 void resize(size_type __sz);
787 void resize(size_type __sz, const_reference __x);
788
Howard Hinnant1c936782011-06-03 19:40:40 +0000789 void swap(vector&)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000790#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +0000791 _NOEXCEPT;
Marshall Clow8982dcd2015-07-13 20:04:56 +0000792#else
Eric Fiselier873b8d32019-03-18 21:50:12 +0000793 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +0000794 __is_nothrow_swappable<allocator_type>::value);
795#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000796
797 bool __invariants() const;
798
Louis Dionneba400782020-10-02 15:02:52 -0400799#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000800
801 bool __dereferenceable(const const_iterator* __i) const;
802 bool __decrementable(const const_iterator* __i) const;
803 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
804 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
805
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400806#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000807
Howard Hinnantc51e1022010-05-11 19:42:16 +0000808private:
Howard Hinnantcf823322010-12-17 14:46:43 +0000809 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Eric Fiselier69c51982016-12-28 06:06:09 +0000810 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(pointer __new_last);
Marshall Clow3ff48e02018-05-22 16:20:28 +0000811 void __vallocate(size_type __n);
812 void __vdeallocate() _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000813 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
Howard Hinnanta5589382011-01-04 19:53:31 +0000814 void __construct_at_end(size_type __n);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000815 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000816 void __construct_at_end(size_type __n, const_reference __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000817 template <class _ForwardIterator>
818 typename enable_if
819 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500820 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000821 void
822 >::type
Eric Fiselieracfe6f02015-03-31 16:54:19 +0000823 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000824 void __append(size_type __n);
825 void __append(size_type __n, const_reference __x);
Howard Hinnantcf823322010-12-17 14:46:43 +0000826 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000827 iterator __make_iter(pointer __p) _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000828 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000829 const_iterator __make_iter(const_pointer __p) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000830 void __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v);
831 pointer __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p);
832 void __move_range(pointer __from_s, pointer __from_e, pointer __to);
Howard Hinnant1c936782011-06-03 19:40:40 +0000833 void __move_assign(vector& __c, true_type)
834 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000835 void __move_assign(vector& __c, false_type)
836 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000837 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76053d72013-06-27 19:35:32 +0000838 void __destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000839 {
Eric Fiselier69c51982016-12-28 06:06:09 +0000840 __invalidate_iterators_past(__new_last);
Marshall Clow2cd9d372014-05-08 14:14:06 +0000841 size_type __old_size = size();
Howard Hinnant27e0e772011-09-14 18:33:51 +0000842 __base::__destruct_at_end(__new_last);
Marshall Clow2cd9d372014-05-08 14:14:06 +0000843 __annotate_shrink(__old_size);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000844 }
Eric Fiseliered9e9362017-04-16 02:40:45 +0000845
846#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier7110f442019-03-19 19:19:44 +0000847 template <class _Up>
848 _LIBCPP_INLINE_VISIBILITY
849 inline void __push_back_slow_path(_Up&& __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000850
Howard Hinnantb6c49562012-02-26 15:30:12 +0000851 template <class... _Args>
Eric Fiselier7110f442019-03-19 19:19:44 +0000852 _LIBCPP_INLINE_VISIBILITY
853 inline void __emplace_back_slow_path(_Args&&... __args);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000854#else
Eric Fiselier7110f442019-03-19 19:19:44 +0000855 template <class _Up>
856 _LIBCPP_INLINE_VISIBILITY
857 inline void __push_back_slow_path(_Up& __x);
Howard Hinnantb6c49562012-02-26 15:30:12 +0000858#endif
Eric Fiseliered9e9362017-04-16 02:40:45 +0000859
Marshall Clow2cd9d372014-05-08 14:14:06 +0000860 // The following functions are no-ops outside of AddressSanitizer mode.
861 // We call annotatations only for the default Allocator because other allocators
862 // may not meet the AddressSanitizer alignment constraints.
863 // See the documentation for __sanitizer_annotate_contiguous_container for more details.
Marshall Clow2cd9d372014-05-08 14:14:06 +0000864#ifndef _LIBCPP_HAS_NO_ASAN
Eric Fiselier6003c772016-12-23 23:37:52 +0000865 void __annotate_contiguous_container(const void *__beg, const void *__end,
866 const void *__old_mid,
867 const void *__new_mid) const
868 {
869
Marshall Clow2cd9d372014-05-08 14:14:06 +0000870 if (__beg && is_same<allocator_type, __default_allocator_type>::value)
871 __sanitizer_annotate_contiguous_container(__beg, __end, __old_mid, __new_mid);
Marshall Clow2cd9d372014-05-08 14:14:06 +0000872 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000873#else
874 _LIBCPP_INLINE_VISIBILITY
875 void __annotate_contiguous_container(const void*, const void*, const void*,
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000876 const void*) const _NOEXCEPT {}
Eric Fiselier6003c772016-12-23 23:37:52 +0000877#endif
878 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000879 void __annotate_new(size_type __current_size) const _NOEXCEPT {
Marshall Clow2cd9d372014-05-08 14:14:06 +0000880 __annotate_contiguous_container(data(), data() + capacity(),
881 data() + capacity(), data() + __current_size);
882 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000883
884 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000885 void __annotate_delete() const _NOEXCEPT {
Marshall Clow2cd9d372014-05-08 14:14:06 +0000886 __annotate_contiguous_container(data(), data() + capacity(),
887 data() + size(), data() + capacity());
888 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000889
890 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000891 void __annotate_increase(size_type __n) const _NOEXCEPT
Marshall Clow2cd9d372014-05-08 14:14:06 +0000892 {
893 __annotate_contiguous_container(data(), data() + capacity(),
894 data() + size(), data() + size() + __n);
895 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000896
897 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000898 void __annotate_shrink(size_type __old_size) const _NOEXCEPT
Marshall Clow2cd9d372014-05-08 14:14:06 +0000899 {
900 __annotate_contiguous_container(data(), data() + capacity(),
901 data() + __old_size, data() + size());
902 }
Marshall Clowc78ffa52014-09-03 21:37:43 +0000903
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000904 struct _ConstructTransaction {
905 explicit _ConstructTransaction(vector &__v, size_type __n)
Mark de Weverdcafc462021-10-21 18:29:14 +0200906 : __v_(__v), __pos_(__v.__end_), __new_end_(__v.__end_ + __n) {
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000907#ifndef _LIBCPP_HAS_NO_ASAN
908 __v_.__annotate_increase(__n);
909#endif
910 }
911 ~_ConstructTransaction() {
912 __v_.__end_ = __pos_;
913#ifndef _LIBCPP_HAS_NO_ASAN
914 if (__pos_ != __new_end_) {
915 __v_.__annotate_shrink(__new_end_ - __v_.__begin_);
916 }
917#endif
918 }
919
920 vector &__v_;
921 pointer __pos_;
922 const_pointer const __new_end_;
923
924 private:
925 _ConstructTransaction(_ConstructTransaction const&) = delete;
926 _ConstructTransaction& operator=(_ConstructTransaction const&) = delete;
927 };
928
929 template <class ..._Args>
930 _LIBCPP_INLINE_VISIBILITY
931 void __construct_one_at_end(_Args&& ...__args) {
932 _ConstructTransaction __tx(*this, 1);
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500933 __alloc_traits::construct(this->__alloc(), _VSTD::__to_address(__tx.__pos_),
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000934 _VSTD::forward<_Args>(__args)...);
935 ++__tx.__pos_;
936 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000937};
938
Louis Dionned59f8a52021-08-17 11:59:07 -0400939#if _LIBCPP_STD_VER >= 17
Marshall Clowf0ca1492018-05-21 21:30:12 +0000940template<class _InputIterator,
Arthur O'Dwyer56226762021-03-03 23:02:20 -0500941 class _Alloc = allocator<__iter_value_type<_InputIterator>>,
Louis Dionne25547162021-08-17 12:26:09 -0400942 class = enable_if_t<__is_allocator<_Alloc>::value>
Marshall Clowf0ca1492018-05-21 21:30:12 +0000943 >
944vector(_InputIterator, _InputIterator)
Arthur O'Dwyer56226762021-03-03 23:02:20 -0500945 -> vector<__iter_value_type<_InputIterator>, _Alloc>;
Marshall Clowf0ca1492018-05-21 21:30:12 +0000946
947template<class _InputIterator,
948 class _Alloc,
Louis Dionne25547162021-08-17 12:26:09 -0400949 class = enable_if_t<__is_allocator<_Alloc>::value>
Marshall Clowf0ca1492018-05-21 21:30:12 +0000950 >
951vector(_InputIterator, _InputIterator, _Alloc)
Arthur O'Dwyer56226762021-03-03 23:02:20 -0500952 -> vector<__iter_value_type<_InputIterator>, _Alloc>;
Marshall Clowf0ca1492018-05-21 21:30:12 +0000953#endif
954
Howard Hinnantc51e1022010-05-11 19:42:16 +0000955template <class _Tp, class _Allocator>
956void
957vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v)
958{
Eric Fiselier909fe962019-09-13 16:09:33 +0000959
Marshall Clow2cd9d372014-05-08 14:14:06 +0000960 __annotate_delete();
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -0500961 _VSTD::__construct_backward_with_exception_guarantees(this->__alloc(), this->__begin_, this->__end_, __v.__begin_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000962 _VSTD::swap(this->__begin_, __v.__begin_);
963 _VSTD::swap(this->__end_, __v.__end_);
964 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000965 __v.__first_ = __v.__begin_;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000966 __annotate_new(size());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000967 __invalidate_all_iterators();
968}
969
970template <class _Tp, class _Allocator>
971typename vector<_Tp, _Allocator>::pointer
972vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p)
973{
Marshall Clow2cd9d372014-05-08 14:14:06 +0000974 __annotate_delete();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000975 pointer __r = __v.__begin_;
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -0500976 _VSTD::__construct_backward_with_exception_guarantees(this->__alloc(), this->__begin_, __p, __v.__begin_);
977 _VSTD::__construct_forward_with_exception_guarantees(this->__alloc(), __p, this->__end_, __v.__end_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000978 _VSTD::swap(this->__begin_, __v.__begin_);
979 _VSTD::swap(this->__end_, __v.__end_);
980 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000981 __v.__first_ = __v.__begin_;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000982 __annotate_new(size());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000983 __invalidate_all_iterators();
984 return __r;
985}
986
987// Allocate space for __n objects
988// throws length_error if __n > max_size()
989// throws (probably bad_alloc) if memory run out
990// Precondition: __begin_ == __end_ == __end_cap() == 0
991// Precondition: __n > 0
992// Postcondition: capacity() == __n
993// Postcondition: size() == 0
994template <class _Tp, class _Allocator>
995void
Marshall Clow3ff48e02018-05-22 16:20:28 +0000996vector<_Tp, _Allocator>::__vallocate(size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000997{
998 if (__n > max_size())
999 this->__throw_length_error();
1000 this->__begin_ = this->__end_ = __alloc_traits::allocate(this->__alloc(), __n);
1001 this->__end_cap() = this->__begin_ + __n;
Marshall Clow2cd9d372014-05-08 14:14:06 +00001002 __annotate_new(0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001003}
1004
1005template <class _Tp, class _Allocator>
1006void
Marshall Clow3ff48e02018-05-22 16:20:28 +00001007vector<_Tp, _Allocator>::__vdeallocate() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001008{
Howard Hinnant76053d72013-06-27 19:35:32 +00001009 if (this->__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001010 {
1011 clear();
1012 __alloc_traits::deallocate(this->__alloc(), this->__begin_, capacity());
Howard Hinnant76053d72013-06-27 19:35:32 +00001013 this->__begin_ = this->__end_ = this->__end_cap() = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001014 }
1015}
1016
1017template <class _Tp, class _Allocator>
1018typename vector<_Tp, _Allocator>::size_type
Howard Hinnant1c936782011-06-03 19:40:40 +00001019vector<_Tp, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001020{
Eric Fiselierb5d9f442016-11-23 01:18:56 +00001021 return _VSTD::min<size_type>(__alloc_traits::max_size(this->__alloc()),
1022 numeric_limits<difference_type>::max());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001023}
1024
1025// Precondition: __new_size > capacity()
1026template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001027inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001028typename vector<_Tp, _Allocator>::size_type
1029vector<_Tp, _Allocator>::__recommend(size_type __new_size) const
1030{
1031 const size_type __ms = max_size();
1032 if (__new_size > __ms)
1033 this->__throw_length_error();
1034 const size_type __cap = capacity();
1035 if (__cap >= __ms / 2)
1036 return __ms;
Arthur O'Dwyerc0fb14b2021-07-26 18:23:00 -04001037 return _VSTD::max<size_type>(2 * __cap, __new_size);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001038}
1039
1040// Default constructs __n objects starting at __end_
1041// throws if construction throws
1042// Precondition: __n > 0
1043// Precondition: size() + __n <= capacity()
1044// Postcondition: size() == size() + __n
1045template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001046void
1047vector<_Tp, _Allocator>::__construct_at_end(size_type __n)
1048{
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001049 _ConstructTransaction __tx(*this, __n);
Martijn Vels1e2e1b82020-06-18 13:14:02 -04001050 const_pointer __new_end = __tx.__new_end_;
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04001051 for (pointer __pos = __tx.__pos_; __pos != __new_end; __tx.__pos_ = ++__pos) {
Martijn Vels1e2e1b82020-06-18 13:14:02 -04001052 __alloc_traits::construct(this->__alloc(), _VSTD::__to_address(__pos));
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001053 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001054}
1055
Howard Hinnantc51e1022010-05-11 19:42:16 +00001056// Copy constructs __n objects starting at __end_ from __x
1057// throws if construction throws
1058// Precondition: __n > 0
1059// Precondition: size() + __n <= capacity()
1060// Postcondition: size() == old size() + __n
1061// Postcondition: [i] == __x for all i in [size() - __n, __n)
1062template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001063inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001064void
1065vector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
1066{
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001067 _ConstructTransaction __tx(*this, __n);
Martijn Vels1e2e1b82020-06-18 13:14:02 -04001068 const_pointer __new_end = __tx.__new_end_;
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04001069 for (pointer __pos = __tx.__pos_; __pos != __new_end; __tx.__pos_ = ++__pos) {
Martijn Vels1e2e1b82020-06-18 13:14:02 -04001070 __alloc_traits::construct(this->__alloc(), _VSTD::__to_address(__pos), __x);
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001071 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001072}
1073
1074template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001075template <class _ForwardIterator>
1076typename enable_if
1077<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001078 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001079 void
1080>::type
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001081vector<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001082{
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001083 _ConstructTransaction __tx(*this, __n);
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05001084 _VSTD::__construct_range_forward(this->__alloc(), __first, __last, __tx.__pos_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001085}
1086
1087// Default constructs __n objects starting at __end_
1088// throws if construction throws
1089// Postcondition: size() == size() + __n
Howard Hinnant1c936782011-06-03 19:40:40 +00001090// Exception safety: strong.
Howard Hinnantc51e1022010-05-11 19:42:16 +00001091template <class _Tp, class _Allocator>
1092void
1093vector<_Tp, _Allocator>::__append(size_type __n)
1094{
1095 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1096 this->__construct_at_end(__n);
1097 else
1098 {
1099 allocator_type& __a = this->__alloc();
1100 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1101 __v.__construct_at_end(__n);
1102 __swap_out_circular_buffer(__v);
1103 }
1104}
1105
1106// Default constructs __n objects starting at __end_
1107// throws if construction throws
1108// Postcondition: size() == size() + __n
Howard Hinnant1c936782011-06-03 19:40:40 +00001109// Exception safety: strong.
Howard Hinnantc51e1022010-05-11 19:42:16 +00001110template <class _Tp, class _Allocator>
1111void
1112vector<_Tp, _Allocator>::__append(size_type __n, const_reference __x)
1113{
1114 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1115 this->__construct_at_end(__n, __x);
1116 else
1117 {
1118 allocator_type& __a = this->__alloc();
1119 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1120 __v.__construct_at_end(__n, __x);
1121 __swap_out_circular_buffer(__v);
1122 }
1123}
1124
1125template <class _Tp, class _Allocator>
1126vector<_Tp, _Allocator>::vector(size_type __n)
1127{
Louis Dionneba400782020-10-02 15:02:52 -04001128#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001129 __get_db()->__insert_c(this);
1130#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001131 if (__n > 0)
1132 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001133 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001134 __construct_at_end(__n);
1135 }
1136}
1137
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00001138#if _LIBCPP_STD_VER > 11
1139template <class _Tp, class _Allocator>
1140vector<_Tp, _Allocator>::vector(size_type __n, const allocator_type& __a)
1141 : __base(__a)
1142{
Louis Dionneba400782020-10-02 15:02:52 -04001143#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00001144 __get_db()->__insert_c(this);
1145#endif
1146 if (__n > 0)
1147 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001148 __vallocate(__n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00001149 __construct_at_end(__n);
1150 }
1151}
1152#endif
1153
Howard Hinnantc51e1022010-05-11 19:42:16 +00001154template <class _Tp, class _Allocator>
Marshall Clowf0ca1492018-05-21 21:30:12 +00001155vector<_Tp, _Allocator>::vector(size_type __n, const value_type& __x)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001156{
Louis Dionneba400782020-10-02 15:02:52 -04001157#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001158 __get_db()->__insert_c(this);
1159#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001160 if (__n > 0)
1161 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001162 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001163 __construct_at_end(__n, __x);
1164 }
1165}
1166
1167template <class _Tp, class _Allocator>
Marshall Clowf0ca1492018-05-21 21:30:12 +00001168vector<_Tp, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001169 : __base(__a)
1170{
Louis Dionneba400782020-10-02 15:02:52 -04001171#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001172 __get_db()->__insert_c(this);
1173#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001174 if (__n > 0)
1175 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001176 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001177 __construct_at_end(__n, __x);
1178 }
1179}
1180
1181template <class _Tp, class _Allocator>
1182template <class _InputIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +00001183vector<_Tp, _Allocator>::vector(_InputIterator __first,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001184 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
1185 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001186 is_constructible<
1187 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +00001188 typename iterator_traits<_InputIterator>::reference>::value,
1189 _InputIterator>::type __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001190{
Louis Dionneba400782020-10-02 15:02:52 -04001191#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001192 __get_db()->__insert_c(this);
1193#endif
Howard Hinnant9cd22302011-09-16 18:41:29 +00001194 for (; __first != __last; ++__first)
Eric Fiselier96919722017-10-17 13:03:17 +00001195 __emplace_back(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001196}
1197
1198template <class _Tp, class _Allocator>
1199template <class _InputIterator>
1200vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001201 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
1202 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001203 is_constructible<
1204 value_type,
1205 typename iterator_traits<_InputIterator>::reference>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001206 : __base(__a)
1207{
Louis Dionneba400782020-10-02 15:02:52 -04001208#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001209 __get_db()->__insert_c(this);
1210#endif
Howard Hinnant9cd22302011-09-16 18:41:29 +00001211 for (; __first != __last; ++__first)
Eric Fiselier96919722017-10-17 13:03:17 +00001212 __emplace_back(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001213}
1214
1215template <class _Tp, class _Allocator>
1216template <class _ForwardIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +00001217vector<_Tp, _Allocator>::vector(_ForwardIterator __first,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001218 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001219 is_constructible<
1220 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +00001221 typename iterator_traits<_ForwardIterator>::reference>::value,
1222 _ForwardIterator>::type __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001223{
Louis Dionneba400782020-10-02 15:02:52 -04001224#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001225 __get_db()->__insert_c(this);
1226#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001227 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001228 if (__n > 0)
1229 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001230 __vallocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001231 __construct_at_end(__first, __last, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001232 }
1233}
1234
1235template <class _Tp, class _Allocator>
1236template <class _ForwardIterator>
1237vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001238 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001239 is_constructible<
1240 value_type,
1241 typename iterator_traits<_ForwardIterator>::reference>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001242 : __base(__a)
1243{
Louis Dionneba400782020-10-02 15:02:52 -04001244#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001245 __get_db()->__insert_c(this);
1246#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001247 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001248 if (__n > 0)
1249 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001250 __vallocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001251 __construct_at_end(__first, __last, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001252 }
1253}
1254
1255template <class _Tp, class _Allocator>
1256vector<_Tp, _Allocator>::vector(const vector& __x)
1257 : __base(__alloc_traits::select_on_container_copy_construction(__x.__alloc()))
1258{
Louis Dionneba400782020-10-02 15:02:52 -04001259#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001260 __get_db()->__insert_c(this);
1261#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001262 size_type __n = __x.size();
1263 if (__n > 0)
1264 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001265 __vallocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001266 __construct_at_end(__x.__begin_, __x.__end_, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001267 }
1268}
1269
1270template <class _Tp, class _Allocator>
Arthur O'Dwyer9b9662f2021-03-01 17:08:24 -05001271vector<_Tp, _Allocator>::vector(const vector& __x, const __identity_t<allocator_type>& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001272 : __base(__a)
1273{
Louis Dionneba400782020-10-02 15:02:52 -04001274#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001275 __get_db()->__insert_c(this);
1276#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001277 size_type __n = __x.size();
1278 if (__n > 0)
1279 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001280 __vallocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001281 __construct_at_end(__x.__begin_, __x.__end_, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001282 }
1283}
1284
Eric Fiseliered9e9362017-04-16 02:40:45 +00001285#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001286
1287template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001288inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001289vector<_Tp, _Allocator>::vector(vector&& __x)
Marshall Clowe5108202015-07-14 14:46:32 +00001290#if _LIBCPP_STD_VER > 14
1291 _NOEXCEPT
1292#else
Howard Hinnant1c936782011-06-03 19:40:40 +00001293 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowe5108202015-07-14 14:46:32 +00001294#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001295 : __base(_VSTD::move(__x.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001296{
Louis Dionneba400782020-10-02 15:02:52 -04001297#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001298 __get_db()->__insert_c(this);
Mark de Wever8f72c892021-10-10 15:40:50 +02001299 __get_db()->swap(this, _VSTD::addressof(__x));
Howard Hinnant27e0e772011-09-14 18:33:51 +00001300#endif
Howard Hinnant9cd22302011-09-16 18:41:29 +00001301 this->__begin_ = __x.__begin_;
1302 this->__end_ = __x.__end_;
1303 this->__end_cap() = __x.__end_cap();
Howard Hinnant76053d72013-06-27 19:35:32 +00001304 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001305}
1306
1307template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001308inline _LIBCPP_INLINE_VISIBILITY
Arthur O'Dwyer9b9662f2021-03-01 17:08:24 -05001309vector<_Tp, _Allocator>::vector(vector&& __x, const __identity_t<allocator_type>& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001310 : __base(__a)
1311{
Louis Dionneba400782020-10-02 15:02:52 -04001312#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001313 __get_db()->__insert_c(this);
1314#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001315 if (__a == __x.__alloc())
1316 {
1317 this->__begin_ = __x.__begin_;
1318 this->__end_ = __x.__end_;
1319 this->__end_cap() = __x.__end_cap();
1320 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Louis Dionneba400782020-10-02 15:02:52 -04001321#if _LIBCPP_DEBUG_LEVEL == 2
Mark de Wever8f72c892021-10-10 15:40:50 +02001322 __get_db()->swap(this, _VSTD::addressof(__x));
Howard Hinnanta1e60cd2011-09-19 16:34:29 +00001323#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001324 }
1325 else
1326 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001327 typedef move_iterator<iterator> _Ip;
1328 assign(_Ip(__x.begin()), _Ip(__x.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001329 }
1330}
1331
1332template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001333inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001334vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il)
1335{
Louis Dionneba400782020-10-02 15:02:52 -04001336#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001337 __get_db()->__insert_c(this);
1338#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001339 if (__il.size() > 0)
1340 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001341 __vallocate(__il.size());
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001342 __construct_at_end(__il.begin(), __il.end(), __il.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001343 }
1344}
1345
1346template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001347inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001348vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
1349 : __base(__a)
1350{
Louis Dionneba400782020-10-02 15:02:52 -04001351#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001352 __get_db()->__insert_c(this);
1353#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001354 if (__il.size() > 0)
1355 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001356 __vallocate(__il.size());
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001357 __construct_at_end(__il.begin(), __il.end(), __il.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001358 }
1359}
1360
1361template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001362inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001363vector<_Tp, _Allocator>&
1364vector<_Tp, _Allocator>::operator=(vector&& __x)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001365 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001366{
1367 __move_assign(__x, integral_constant<bool,
1368 __alloc_traits::propagate_on_container_move_assignment::value>());
1369 return *this;
1370}
1371
1372template <class _Tp, class _Allocator>
1373void
1374vector<_Tp, _Allocator>::__move_assign(vector& __c, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001375 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001376{
1377 if (__base::__alloc() != __c.__alloc())
1378 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001379 typedef move_iterator<iterator> _Ip;
1380 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001381 }
1382 else
1383 __move_assign(__c, true_type());
1384}
1385
1386template <class _Tp, class _Allocator>
1387void
1388vector<_Tp, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00001389 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001390{
Marshall Clow3ff48e02018-05-22 16:20:28 +00001391 __vdeallocate();
Marshall Clow136d45c2014-07-21 15:11:13 +00001392 __base::__move_assign_alloc(__c); // this can throw
Howard Hinnantc51e1022010-05-11 19:42:16 +00001393 this->__begin_ = __c.__begin_;
1394 this->__end_ = __c.__end_;
1395 this->__end_cap() = __c.__end_cap();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001396 __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
Louis Dionneba400782020-10-02 15:02:52 -04001397#if _LIBCPP_DEBUG_LEVEL == 2
Mark de Wever8f72c892021-10-10 15:40:50 +02001398 __get_db()->swap(this, _VSTD::addressof(__c));
Howard Hinnanta1e60cd2011-09-19 16:34:29 +00001399#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001400}
1401
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001402#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001403
1404template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001405inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001406vector<_Tp, _Allocator>&
1407vector<_Tp, _Allocator>::operator=(const vector& __x)
1408{
Mark de Wever357a1fc2021-09-28 19:15:18 +02001409 if (this != _VSTD::addressof(__x))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001410 {
1411 __base::__copy_assign_alloc(__x);
1412 assign(__x.__begin_, __x.__end_);
1413 }
1414 return *this;
1415}
1416
1417template <class _Tp, class _Allocator>
1418template <class _InputIterator>
1419typename enable_if
1420<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001421 __is_cpp17_input_iterator <_InputIterator>::value &&
1422 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001423 is_constructible<
1424 _Tp,
1425 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001426 void
1427>::type
1428vector<_Tp, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
1429{
1430 clear();
1431 for (; __first != __last; ++__first)
Eric Fiselier96919722017-10-17 13:03:17 +00001432 __emplace_back(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001433}
1434
1435template <class _Tp, class _Allocator>
1436template <class _ForwardIterator>
1437typename enable_if
1438<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001439 __is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001440 is_constructible<
1441 _Tp,
1442 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001443 void
1444>::type
1445vector<_Tp, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
1446{
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001447 size_type __new_size = static_cast<size_type>(_VSTD::distance(__first, __last));
1448 if (__new_size <= capacity())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001449 {
1450 _ForwardIterator __mid = __last;
1451 bool __growing = false;
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001452 if (__new_size > size())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001453 {
1454 __growing = true;
1455 __mid = __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001456 _VSTD::advance(__mid, size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001457 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001458 pointer __m = _VSTD::copy(__first, __mid, this->__begin_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001459 if (__growing)
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001460 __construct_at_end(__mid, __last, __new_size - size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001461 else
1462 this->__destruct_at_end(__m);
1463 }
1464 else
1465 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001466 __vdeallocate();
1467 __vallocate(__recommend(__new_size));
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001468 __construct_at_end(__first, __last, __new_size);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001469 }
Eric Fiselier69c51982016-12-28 06:06:09 +00001470 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001471}
1472
1473template <class _Tp, class _Allocator>
1474void
1475vector<_Tp, _Allocator>::assign(size_type __n, const_reference __u)
1476{
1477 if (__n <= capacity())
1478 {
1479 size_type __s = size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001480 _VSTD::fill_n(this->__begin_, _VSTD::min(__n, __s), __u);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001481 if (__n > __s)
1482 __construct_at_end(__n - __s, __u);
1483 else
Howard Hinnant155c2af2010-05-24 17:49:41 +00001484 this->__destruct_at_end(this->__begin_ + __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001485 }
1486 else
1487 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001488 __vdeallocate();
1489 __vallocate(__recommend(static_cast<size_type>(__n)));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001490 __construct_at_end(__n, __u);
1491 }
Eric Fiselier69c51982016-12-28 06:06:09 +00001492 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001493}
1494
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001495template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001496inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001497typename vector<_Tp, _Allocator>::iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001498vector<_Tp, _Allocator>::__make_iter(pointer __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001499{
Louis Dionneba400782020-10-02 15:02:52 -04001500#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001501 return iterator(this, __p);
1502#else
1503 return iterator(__p);
1504#endif
1505}
1506
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001507template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001508inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001509typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001510vector<_Tp, _Allocator>::__make_iter(const_pointer __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001511{
Louis Dionneba400782020-10-02 15:02:52 -04001512#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001513 return const_iterator(this, __p);
1514#else
1515 return const_iterator(__p);
1516#endif
1517}
1518
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001519template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001520inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001521typename vector<_Tp, _Allocator>::iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001522vector<_Tp, _Allocator>::begin() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001523{
1524 return __make_iter(this->__begin_);
1525}
1526
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001527template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001528inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001529typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001530vector<_Tp, _Allocator>::begin() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001531{
1532 return __make_iter(this->__begin_);
1533}
1534
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001535template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001536inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001537typename vector<_Tp, _Allocator>::iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001538vector<_Tp, _Allocator>::end() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001539{
1540 return __make_iter(this->__end_);
1541}
1542
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001543template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001544inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001545typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001546vector<_Tp, _Allocator>::end() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001547{
1548 return __make_iter(this->__end_);
1549}
1550
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001551template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001552inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001553typename vector<_Tp, _Allocator>::reference
Marshall Clowd6470492019-03-15 00:29:35 +00001554vector<_Tp, _Allocator>::operator[](size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001555{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001556 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001557 return this->__begin_[__n];
1558}
1559
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001560template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001561inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001562typename vector<_Tp, _Allocator>::const_reference
Marshall Clowd6470492019-03-15 00:29:35 +00001563vector<_Tp, _Allocator>::operator[](size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001564{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001565 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001566 return this->__begin_[__n];
1567}
1568
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001569template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001570typename vector<_Tp, _Allocator>::reference
1571vector<_Tp, _Allocator>::at(size_type __n)
1572{
1573 if (__n >= size())
1574 this->__throw_out_of_range();
1575 return this->__begin_[__n];
1576}
1577
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001578template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001579typename vector<_Tp, _Allocator>::const_reference
1580vector<_Tp, _Allocator>::at(size_type __n) const
1581{
1582 if (__n >= size())
1583 this->__throw_out_of_range();
1584 return this->__begin_[__n];
1585}
1586
1587template <class _Tp, class _Allocator>
1588void
1589vector<_Tp, _Allocator>::reserve(size_type __n)
1590{
1591 if (__n > capacity())
1592 {
Mikhail Maltsev0b0a51d2021-10-21 10:40:05 +01001593 if (__n > max_size())
1594 this->__throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001595 allocator_type& __a = this->__alloc();
Howard Hinnant1c936782011-06-03 19:40:40 +00001596 __split_buffer<value_type, allocator_type&> __v(__n, size(), __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001597 __swap_out_circular_buffer(__v);
1598 }
1599}
1600
1601template <class _Tp, class _Allocator>
1602void
Howard Hinnant1c936782011-06-03 19:40:40 +00001603vector<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001604{
1605 if (capacity() > size())
1606 {
1607#ifndef _LIBCPP_NO_EXCEPTIONS
1608 try
1609 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001610#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001611 allocator_type& __a = this->__alloc();
Howard Hinnant1c936782011-06-03 19:40:40 +00001612 __split_buffer<value_type, allocator_type&> __v(size(), size(), __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001613 __swap_out_circular_buffer(__v);
1614#ifndef _LIBCPP_NO_EXCEPTIONS
1615 }
1616 catch (...)
1617 {
1618 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001619#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001620 }
1621}
1622
1623template <class _Tp, class _Allocator>
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001624template <class _Up>
1625void
Eric Fiseliered9e9362017-04-16 02:40:45 +00001626#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001627vector<_Tp, _Allocator>::__push_back_slow_path(_Up&& __x)
1628#else
1629vector<_Tp, _Allocator>::__push_back_slow_path(_Up& __x)
1630#endif
1631{
1632 allocator_type& __a = this->__alloc();
1633 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1634 // __v.push_back(_VSTD::forward<_Up>(__x));
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001635 __alloc_traits::construct(__a, _VSTD::__to_address(__v.__end_), _VSTD::forward<_Up>(__x));
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001636 __v.__end_++;
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001637 __swap_out_circular_buffer(__v);
1638}
1639
1640template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001641inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001642void
1643vector<_Tp, _Allocator>::push_back(const_reference __x)
1644{
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001645 if (this->__end_ != this->__end_cap())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001646 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001647 __construct_one_at_end(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001648 }
1649 else
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001650 __push_back_slow_path(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001651}
1652
Eric Fiseliered9e9362017-04-16 02:40:45 +00001653#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001654
1655template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001656inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001657void
1658vector<_Tp, _Allocator>::push_back(value_type&& __x)
1659{
1660 if (this->__end_ < this->__end_cap())
1661 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001662 __construct_one_at_end(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001663 }
1664 else
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001665 __push_back_slow_path(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001666}
1667
1668template <class _Tp, class _Allocator>
1669template <class... _Args>
1670void
Howard Hinnantb6c49562012-02-26 15:30:12 +00001671vector<_Tp, _Allocator>::__emplace_back_slow_path(_Args&&... __args)
1672{
1673 allocator_type& __a = this->__alloc();
1674 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1675// __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001676 __alloc_traits::construct(__a, _VSTD::__to_address(__v.__end_), _VSTD::forward<_Args>(__args)...);
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001677 __v.__end_++;
Howard Hinnantb6c49562012-02-26 15:30:12 +00001678 __swap_out_circular_buffer(__v);
1679}
1680
1681template <class _Tp, class _Allocator>
1682template <class... _Args>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001683inline
Marshall Clowea52cc42017-01-24 23:09:12 +00001684#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001685typename vector<_Tp, _Allocator>::reference
Marshall Clowea52cc42017-01-24 23:09:12 +00001686#else
1687void
1688#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001689vector<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1690{
1691 if (this->__end_ < this->__end_cap())
1692 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001693 __construct_one_at_end(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001694 }
1695 else
Howard Hinnantb6c49562012-02-26 15:30:12 +00001696 __emplace_back_slow_path(_VSTD::forward<_Args>(__args)...);
Marshall Clowea52cc42017-01-24 23:09:12 +00001697#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001698 return this->back();
Marshall Clowea52cc42017-01-24 23:09:12 +00001699#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001700}
1701
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001702#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001703
1704template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001705inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001706void
1707vector<_Tp, _Allocator>::pop_back()
1708{
Kristina Bessonovaaeeaa7e2021-05-09 19:29:56 +02001709 _LIBCPP_ASSERT(!empty(), "vector::pop_back called on an empty vector");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001710 this->__destruct_at_end(this->__end_ - 1);
1711}
1712
1713template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001714inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001715typename vector<_Tp, _Allocator>::iterator
1716vector<_Tp, _Allocator>::erase(const_iterator __position)
1717{
Louis Dionneba400782020-10-02 15:02:52 -04001718#if _LIBCPP_DEBUG_LEVEL == 2
Mark de Wever8f72c892021-10-10 15:40:50 +02001719 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__position)) == this,
Howard Hinnant27e0e772011-09-14 18:33:51 +00001720 "vector::erase(iterator) called with an iterator not"
1721 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001722#endif
Howard Hinnant58d52d52013-03-25 22:12:26 +00001723 _LIBCPP_ASSERT(__position != end(),
1724 "vector::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnant76053d72013-06-27 19:35:32 +00001725 difference_type __ps = __position - cbegin();
1726 pointer __p = this->__begin_ + __ps;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001727 this->__destruct_at_end(_VSTD::move(__p + 1, this->__end_, __p));
Eric Fiselier69c51982016-12-28 06:06:09 +00001728 this->__invalidate_iterators_past(__p-1);
1729 iterator __r = __make_iter(__p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001730 return __r;
1731}
1732
1733template <class _Tp, class _Allocator>
1734typename vector<_Tp, _Allocator>::iterator
1735vector<_Tp, _Allocator>::erase(const_iterator __first, const_iterator __last)
1736{
Louis Dionneba400782020-10-02 15:02:52 -04001737#if _LIBCPP_DEBUG_LEVEL == 2
Mark de Wever8f72c892021-10-10 15:40:50 +02001738 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__first)) == this,
Mark de Weverdcafc462021-10-21 18:29:14 +02001739 "vector::erase(iterator, iterator) called with an iterator not"
Howard Hinnant27e0e772011-09-14 18:33:51 +00001740 " referring to this vector");
Mark de Wever8f72c892021-10-10 15:40:50 +02001741 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__last)) == this,
Mark de Weverdcafc462021-10-21 18:29:14 +02001742 "vector::erase(iterator, iterator) called with an iterator not"
Eric Fiselier69c51982016-12-28 06:06:09 +00001743 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001744#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001745 _LIBCPP_ASSERT(__first <= __last, "vector::erase(first, last) called with invalid range");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001746 pointer __p = this->__begin_ + (__first - begin());
Eric Fiselier69c51982016-12-28 06:06:09 +00001747 if (__first != __last) {
Howard Hinnantc580cc32013-04-18 15:02:57 +00001748 this->__destruct_at_end(_VSTD::move(__p + (__last - __first), this->__end_, __p));
Eric Fiselier69c51982016-12-28 06:06:09 +00001749 this->__invalidate_iterators_past(__p - 1);
1750 }
1751 iterator __r = __make_iter(__p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001752 return __r;
1753}
1754
1755template <class _Tp, class _Allocator>
1756void
1757vector<_Tp, _Allocator>::__move_range(pointer __from_s, pointer __from_e, pointer __to)
1758{
1759 pointer __old_last = this->__end_;
1760 difference_type __n = __old_last - __to;
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001761 {
1762 pointer __i = __from_s + __n;
1763 _ConstructTransaction __tx(*this, __from_e - __i);
Martijn Vels1e2e1b82020-06-18 13:14:02 -04001764 for (pointer __pos = __tx.__pos_; __i < __from_e;
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04001765 ++__i, (void) ++__pos, __tx.__pos_ = __pos) {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001766 __alloc_traits::construct(this->__alloc(),
Martijn Vels1e2e1b82020-06-18 13:14:02 -04001767 _VSTD::__to_address(__pos),
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001768 _VSTD::move(*__i));
1769 }
1770 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001771 _VSTD::move_backward(__from_s, __from_s + __n, __old_last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001772}
1773
1774template <class _Tp, class _Allocator>
1775typename vector<_Tp, _Allocator>::iterator
1776vector<_Tp, _Allocator>::insert(const_iterator __position, const_reference __x)
1777{
Louis Dionneba400782020-10-02 15:02:52 -04001778#if _LIBCPP_DEBUG_LEVEL == 2
Mark de Wever8f72c892021-10-10 15:40:50 +02001779 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__position)) == this,
Howard Hinnant27e0e772011-09-14 18:33:51 +00001780 "vector::insert(iterator, x) called with an iterator not"
1781 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001782#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001783 pointer __p = this->__begin_ + (__position - begin());
1784 if (this->__end_ < this->__end_cap())
1785 {
1786 if (__p == this->__end_)
1787 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001788 __construct_one_at_end(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001789 }
1790 else
1791 {
1792 __move_range(__p, this->__end_, __p + 1);
1793 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1794 if (__p <= __xr && __xr < this->__end_)
1795 ++__xr;
1796 *__p = *__xr;
1797 }
1798 }
1799 else
1800 {
1801 allocator_type& __a = this->__alloc();
1802 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
1803 __v.push_back(__x);
1804 __p = __swap_out_circular_buffer(__v, __p);
1805 }
1806 return __make_iter(__p);
1807}
1808
Eric Fiseliered9e9362017-04-16 02:40:45 +00001809#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001810
1811template <class _Tp, class _Allocator>
1812typename vector<_Tp, _Allocator>::iterator
1813vector<_Tp, _Allocator>::insert(const_iterator __position, value_type&& __x)
1814{
Louis Dionneba400782020-10-02 15:02:52 -04001815#if _LIBCPP_DEBUG_LEVEL == 2
Mark de Wever8f72c892021-10-10 15:40:50 +02001816 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__position)) == this,
Howard Hinnant27e0e772011-09-14 18:33:51 +00001817 "vector::insert(iterator, x) called with an iterator not"
1818 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001819#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001820 pointer __p = this->__begin_ + (__position - begin());
1821 if (this->__end_ < this->__end_cap())
1822 {
1823 if (__p == this->__end_)
1824 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001825 __construct_one_at_end(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001826 }
1827 else
1828 {
1829 __move_range(__p, this->__end_, __p + 1);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001830 *__p = _VSTD::move(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001831 }
1832 }
1833 else
1834 {
1835 allocator_type& __a = this->__alloc();
1836 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001837 __v.push_back(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001838 __p = __swap_out_circular_buffer(__v, __p);
1839 }
1840 return __make_iter(__p);
1841}
1842
1843template <class _Tp, class _Allocator>
1844template <class... _Args>
1845typename vector<_Tp, _Allocator>::iterator
1846vector<_Tp, _Allocator>::emplace(const_iterator __position, _Args&&... __args)
1847{
Louis Dionneba400782020-10-02 15:02:52 -04001848#if _LIBCPP_DEBUG_LEVEL == 2
Mark de Wever8f72c892021-10-10 15:40:50 +02001849 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__position)) == this,
Howard Hinnant27e0e772011-09-14 18:33:51 +00001850 "vector::emplace(iterator, x) called with an iterator not"
1851 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001852#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001853 pointer __p = this->__begin_ + (__position - begin());
1854 if (this->__end_ < this->__end_cap())
1855 {
1856 if (__p == this->__end_)
1857 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001858 __construct_one_at_end(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001859 }
1860 else
1861 {
Marshall Clowa591b9a2016-07-11 21:38:08 +00001862 __temp_value<value_type, _Allocator> __tmp(this->__alloc(), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001863 __move_range(__p, this->__end_, __p + 1);
Marshall Clowa591b9a2016-07-11 21:38:08 +00001864 *__p = _VSTD::move(__tmp.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001865 }
1866 }
1867 else
1868 {
1869 allocator_type& __a = this->__alloc();
1870 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001871 __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001872 __p = __swap_out_circular_buffer(__v, __p);
1873 }
1874 return __make_iter(__p);
1875}
1876
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001877#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001878
1879template <class _Tp, class _Allocator>
1880typename vector<_Tp, _Allocator>::iterator
1881vector<_Tp, _Allocator>::insert(const_iterator __position, size_type __n, const_reference __x)
1882{
Louis Dionneba400782020-10-02 15:02:52 -04001883#if _LIBCPP_DEBUG_LEVEL == 2
Mark de Wever8f72c892021-10-10 15:40:50 +02001884 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__position)) == this,
Howard Hinnant27e0e772011-09-14 18:33:51 +00001885 "vector::insert(iterator, n, x) called with an iterator not"
1886 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001887#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001888 pointer __p = this->__begin_ + (__position - begin());
1889 if (__n > 0)
1890 {
1891 if (__n <= static_cast<size_type>(this->__end_cap() - this->__end_))
1892 {
1893 size_type __old_n = __n;
1894 pointer __old_last = this->__end_;
1895 if (__n > static_cast<size_type>(this->__end_ - __p))
1896 {
1897 size_type __cx = __n - (this->__end_ - __p);
1898 __construct_at_end(__cx, __x);
1899 __n -= __cx;
1900 }
1901 if (__n > 0)
1902 {
1903 __move_range(__p, __old_last, __p + __old_n);
1904 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1905 if (__p <= __xr && __xr < this->__end_)
1906 __xr += __old_n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001907 _VSTD::fill_n(__p, __n, *__xr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001908 }
1909 }
1910 else
1911 {
1912 allocator_type& __a = this->__alloc();
1913 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1914 __v.__construct_at_end(__n, __x);
1915 __p = __swap_out_circular_buffer(__v, __p);
1916 }
1917 }
1918 return __make_iter(__p);
1919}
1920
1921template <class _Tp, class _Allocator>
1922template <class _InputIterator>
1923typename enable_if
1924<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001925 __is_cpp17_input_iterator <_InputIterator>::value &&
1926 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001927 is_constructible<
1928 _Tp,
1929 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001930 typename vector<_Tp, _Allocator>::iterator
1931>::type
1932vector<_Tp, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
1933{
Louis Dionneba400782020-10-02 15:02:52 -04001934#if _LIBCPP_DEBUG_LEVEL == 2
Mark de Wever8f72c892021-10-10 15:40:50 +02001935 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__position)) == this,
Howard Hinnant27e0e772011-09-14 18:33:51 +00001936 "vector::insert(iterator, range) called with an iterator not"
1937 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001938#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001939 difference_type __off = __position - begin();
1940 pointer __p = this->__begin_ + __off;
1941 allocator_type& __a = this->__alloc();
1942 pointer __old_last = this->__end_;
1943 for (; this->__end_ != this->__end_cap() && __first != __last; ++__first)
1944 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001945 __construct_one_at_end(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001946 }
1947 __split_buffer<value_type, allocator_type&> __v(__a);
1948 if (__first != __last)
1949 {
1950#ifndef _LIBCPP_NO_EXCEPTIONS
1951 try
1952 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001953#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001954 __v.__construct_at_end(__first, __last);
1955 difference_type __old_size = __old_last - this->__begin_;
1956 difference_type __old_p = __p - this->__begin_;
1957 reserve(__recommend(size() + __v.size()));
1958 __p = this->__begin_ + __old_p;
1959 __old_last = this->__begin_ + __old_size;
1960#ifndef _LIBCPP_NO_EXCEPTIONS
1961 }
1962 catch (...)
1963 {
1964 erase(__make_iter(__old_last), end());
1965 throw;
1966 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001967#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001968 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001969 __p = _VSTD::rotate(__p, __old_last, this->__end_);
Louis Dionne202a4cb2020-02-11 10:59:12 +01001970 insert(__make_iter(__p), _VSTD::make_move_iterator(__v.begin()),
1971 _VSTD::make_move_iterator(__v.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001972 return begin() + __off;
1973}
1974
1975template <class _Tp, class _Allocator>
1976template <class _ForwardIterator>
1977typename enable_if
1978<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001979 __is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001980 is_constructible<
1981 _Tp,
1982 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001983 typename vector<_Tp, _Allocator>::iterator
1984>::type
1985vector<_Tp, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
1986{
Louis Dionneba400782020-10-02 15:02:52 -04001987#if _LIBCPP_DEBUG_LEVEL == 2
Mark de Wever8f72c892021-10-10 15:40:50 +02001988 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__position)) == this,
Howard Hinnant27e0e772011-09-14 18:33:51 +00001989 "vector::insert(iterator, range) called with an iterator not"
1990 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001991#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001992 pointer __p = this->__begin_ + (__position - begin());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001993 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001994 if (__n > 0)
1995 {
1996 if (__n <= this->__end_cap() - this->__end_)
1997 {
1998 size_type __old_n = __n;
1999 pointer __old_last = this->__end_;
2000 _ForwardIterator __m = __last;
2001 difference_type __dx = this->__end_ - __p;
2002 if (__n > __dx)
2003 {
2004 __m = __first;
Eric Fiselieracfe6f02015-03-31 16:54:19 +00002005 difference_type __diff = this->__end_ - __p;
2006 _VSTD::advance(__m, __diff);
2007 __construct_at_end(__m, __last, __n - __diff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002008 __n = __dx;
2009 }
2010 if (__n > 0)
2011 {
2012 __move_range(__p, __old_last, __p + __old_n);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002013 _VSTD::copy(__first, __m, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002014 }
2015 }
2016 else
2017 {
2018 allocator_type& __a = this->__alloc();
2019 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
2020 __v.__construct_at_end(__first, __last);
2021 __p = __swap_out_circular_buffer(__v, __p);
2022 }
2023 }
2024 return __make_iter(__p);
2025}
2026
2027template <class _Tp, class _Allocator>
2028void
2029vector<_Tp, _Allocator>::resize(size_type __sz)
2030{
2031 size_type __cs = size();
2032 if (__cs < __sz)
2033 this->__append(__sz - __cs);
2034 else if (__cs > __sz)
2035 this->__destruct_at_end(this->__begin_ + __sz);
2036}
2037
2038template <class _Tp, class _Allocator>
2039void
2040vector<_Tp, _Allocator>::resize(size_type __sz, const_reference __x)
2041{
2042 size_type __cs = size();
2043 if (__cs < __sz)
2044 this->__append(__sz - __cs, __x);
2045 else if (__cs > __sz)
2046 this->__destruct_at_end(this->__begin_ + __sz);
2047}
2048
2049template <class _Tp, class _Allocator>
2050void
2051vector<_Tp, _Allocator>::swap(vector& __x)
Marshall Clow8982dcd2015-07-13 20:04:56 +00002052#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00002053 _NOEXCEPT
Marshall Clow8982dcd2015-07-13 20:04:56 +00002054#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00002055 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00002056 __is_nothrow_swappable<allocator_type>::value)
2057#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002058{
Howard Hinnant27e0e772011-09-14 18:33:51 +00002059 _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
2060 this->__alloc() == __x.__alloc(),
2061 "vector::swap: Either propagate_on_container_swap must be true"
2062 " or the allocators must compare equal");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002063 _VSTD::swap(this->__begin_, __x.__begin_);
2064 _VSTD::swap(this->__end_, __x.__end_);
2065 _VSTD::swap(this->__end_cap(), __x.__end_cap());
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05002066 _VSTD::__swap_allocator(this->__alloc(), __x.__alloc(),
Marshall Clow8982dcd2015-07-13 20:04:56 +00002067 integral_constant<bool,__alloc_traits::propagate_on_container_swap::value>());
Louis Dionneba400782020-10-02 15:02:52 -04002068#if _LIBCPP_DEBUG_LEVEL == 2
Mark de Wever8f72c892021-10-10 15:40:50 +02002069 __get_db()->swap(this, _VSTD::addressof(__x));
Louis Dionneba400782020-10-02 15:02:52 -04002070#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002071}
2072
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002073template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002074bool
2075vector<_Tp, _Allocator>::__invariants() const
2076{
Howard Hinnant76053d72013-06-27 19:35:32 +00002077 if (this->__begin_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002078 {
Howard Hinnant76053d72013-06-27 19:35:32 +00002079 if (this->__end_ != nullptr || this->__end_cap() != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002080 return false;
2081 }
2082 else
2083 {
2084 if (this->__begin_ > this->__end_)
2085 return false;
2086 if (this->__begin_ == this->__end_cap())
2087 return false;
2088 if (this->__end_ > this->__end_cap())
2089 return false;
2090 }
2091 return true;
2092}
2093
Louis Dionneba400782020-10-02 15:02:52 -04002094#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002095
Howard Hinnantc51e1022010-05-11 19:42:16 +00002096template <class _Tp, class _Allocator>
Howard Hinnant27e0e772011-09-14 18:33:51 +00002097bool
2098vector<_Tp, _Allocator>::__dereferenceable(const const_iterator* __i) const
2099{
2100 return this->__begin_ <= __i->base() && __i->base() < this->__end_;
2101}
2102
2103template <class _Tp, class _Allocator>
2104bool
2105vector<_Tp, _Allocator>::__decrementable(const const_iterator* __i) const
2106{
2107 return this->__begin_ < __i->base() && __i->base() <= this->__end_;
2108}
2109
2110template <class _Tp, class _Allocator>
2111bool
2112vector<_Tp, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
2113{
2114 const_pointer __p = __i->base() + __n;
2115 return this->__begin_ <= __p && __p <= this->__end_;
2116}
2117
2118template <class _Tp, class _Allocator>
2119bool
2120vector<_Tp, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
2121{
2122 const_pointer __p = __i->base() + __n;
2123 return this->__begin_ <= __p && __p < this->__end_;
2124}
2125
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002126#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002127
2128template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002129inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002130void
2131vector<_Tp, _Allocator>::__invalidate_all_iterators()
2132{
Louis Dionneba400782020-10-02 15:02:52 -04002133#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002134 __get_db()->__invalidate_all(this);
Louis Dionneba400782020-10-02 15:02:52 -04002135#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002136}
2137
Eric Fiselier69c51982016-12-28 06:06:09 +00002138
2139template <class _Tp, class _Allocator>
2140inline _LIBCPP_INLINE_VISIBILITY
2141void
2142vector<_Tp, _Allocator>::__invalidate_iterators_past(pointer __new_last) {
Louis Dionneba400782020-10-02 15:02:52 -04002143#if _LIBCPP_DEBUG_LEVEL == 2
Eric Fiselier69c51982016-12-28 06:06:09 +00002144 __c_node* __c = __get_db()->__find_c_and_lock(this);
2145 for (__i_node** __p = __c->end_; __p != __c->beg_; ) {
2146 --__p;
2147 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
2148 if (__i->base() > __new_last) {
2149 (*__p)->__c_ = nullptr;
2150 if (--__c->end_ != __p)
Arthur O'Dwyer22236632020-12-07 21:50:15 -05002151 _VSTD::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
Eric Fiselier69c51982016-12-28 06:06:09 +00002152 }
2153 }
2154 __get_db()->unlock();
2155#else
2156 ((void)__new_last);
2157#endif
2158}
2159
Howard Hinnantc51e1022010-05-11 19:42:16 +00002160// vector<bool>
2161
2162template <class _Allocator> class vector<bool, _Allocator>;
2163
2164template <class _Allocator> struct hash<vector<bool, _Allocator> >;
2165
2166template <class _Allocator>
Howard Hinnantb4ef6482011-07-02 20:33:23 +00002167struct __has_storage_type<vector<bool, _Allocator> >
2168{
2169 static const bool value = true;
2170};
2171
2172template <class _Allocator>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002173class _LIBCPP_TEMPLATE_VIS vector<bool, _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002174 : private __vector_base_common<true>
2175{
2176public:
2177 typedef vector __self;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002178 typedef bool value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002179 typedef _Allocator allocator_type;
2180 typedef allocator_traits<allocator_type> __alloc_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002181 typedef typename __alloc_traits::size_type size_type;
2182 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant896b7622012-05-07 16:50:38 +00002183 typedef size_type __storage_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002184 typedef __bit_iterator<vector, false> pointer;
2185 typedef __bit_iterator<vector, true> const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002186 typedef pointer iterator;
2187 typedef const_pointer const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002188 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
2189 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002190
2191private:
Marshall Clow940e01c2015-04-07 05:21:38 +00002192 typedef typename __rebind_alloc_helper<__alloc_traits, __storage_type>::type __storage_allocator;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002193 typedef allocator_traits<__storage_allocator> __storage_traits;
2194 typedef typename __storage_traits::pointer __storage_pointer;
2195 typedef typename __storage_traits::const_pointer __const_storage_pointer;
2196
2197 __storage_pointer __begin_;
2198 size_type __size_;
2199 __compressed_pair<size_type, __storage_allocator> __cap_alloc_;
Howard Hinnant71d1c192011-07-09 15:50:42 +00002200public:
Howard Hinnantb4ef6482011-07-02 20:33:23 +00002201 typedef __bit_reference<vector> reference;
2202 typedef __bit_const_reference<vector> const_reference;
Howard Hinnant71d1c192011-07-09 15:50:42 +00002203private:
Howard Hinnant1c936782011-06-03 19:40:40 +00002204 _LIBCPP_INLINE_VISIBILITY
2205 size_type& __cap() _NOEXCEPT
2206 {return __cap_alloc_.first();}
2207 _LIBCPP_INLINE_VISIBILITY
2208 const size_type& __cap() const _NOEXCEPT
2209 {return __cap_alloc_.first();}
2210 _LIBCPP_INLINE_VISIBILITY
2211 __storage_allocator& __alloc() _NOEXCEPT
2212 {return __cap_alloc_.second();}
2213 _LIBCPP_INLINE_VISIBILITY
2214 const __storage_allocator& __alloc() const _NOEXCEPT
2215 {return __cap_alloc_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002216
2217 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
2218
Howard Hinnant1c936782011-06-03 19:40:40 +00002219 _LIBCPP_INLINE_VISIBILITY
2220 static size_type __internal_cap_to_external(size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002221 {return __n * __bits_per_word;}
Howard Hinnant1c936782011-06-03 19:40:40 +00002222 _LIBCPP_INLINE_VISIBILITY
2223 static size_type __external_cap_to_internal(size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002224 {return (__n - 1) / __bits_per_word + 1;}
2225
2226public:
Howard Hinnant1c936782011-06-03 19:40:40 +00002227 _LIBCPP_INLINE_VISIBILITY
Marshall Clowe546cbb2015-06-04 02:05:41 +00002228 vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clow8f282f42015-06-04 00:10:20 +00002229
2230 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
2231#if _LIBCPP_STD_VER <= 14
2232 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
2233#else
2234 _NOEXCEPT;
2235#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002236 ~vector();
2237 explicit vector(size_type __n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00002238#if _LIBCPP_STD_VER > 11
2239 explicit vector(size_type __n, const allocator_type& __a);
2240#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002241 vector(size_type __n, const value_type& __v);
2242 vector(size_type __n, const value_type& __v, const allocator_type& __a);
2243 template <class _InputIterator>
2244 vector(_InputIterator __first, _InputIterator __last,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002245 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
2246 !__is_cpp17_forward_iterator<_InputIterator>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002247 template <class _InputIterator>
2248 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002249 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
2250 !__is_cpp17_forward_iterator<_InputIterator>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002251 template <class _ForwardIterator>
2252 vector(_ForwardIterator __first, _ForwardIterator __last,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002253 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002254 template <class _ForwardIterator>
2255 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002256 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002257
2258 vector(const vector& __v);
2259 vector(const vector& __v, const allocator_type& __a);
2260 vector& operator=(const vector& __v);
Eric Fiseliered9e9362017-04-16 02:40:45 +00002261
2262#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002263 vector(initializer_list<value_type> __il);
2264 vector(initializer_list<value_type> __il, const allocator_type& __a);
2265
Howard Hinnant1c936782011-06-03 19:40:40 +00002266 _LIBCPP_INLINE_VISIBILITY
2267 vector(vector&& __v)
Marshall Clowe5108202015-07-14 14:46:32 +00002268#if _LIBCPP_STD_VER > 14
2269 _NOEXCEPT;
2270#else
Howard Hinnant1c936782011-06-03 19:40:40 +00002271 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowe5108202015-07-14 14:46:32 +00002272#endif
Arthur O'Dwyer9b9662f2021-03-01 17:08:24 -05002273 vector(vector&& __v, const __identity_t<allocator_type>& __a);
Howard Hinnant1c936782011-06-03 19:40:40 +00002274 _LIBCPP_INLINE_VISIBILITY
2275 vector& operator=(vector&& __v)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002276 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiseliered9e9362017-04-16 02:40:45 +00002277
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002278 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002279 vector& operator=(initializer_list<value_type> __il)
2280 {assign(__il.begin(), __il.end()); return *this;}
Eric Fiseliered9e9362017-04-16 02:40:45 +00002281
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002282#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002283
2284 template <class _InputIterator>
2285 typename enable_if
2286 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002287 __is_cpp17_input_iterator<_InputIterator>::value &&
2288 !__is_cpp17_forward_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002289 void
2290 >::type
2291 assign(_InputIterator __first, _InputIterator __last);
2292 template <class _ForwardIterator>
2293 typename enable_if
2294 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002295 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002296 void
2297 >::type
2298 assign(_ForwardIterator __first, _ForwardIterator __last);
2299
2300 void assign(size_type __n, const value_type& __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +00002301
2302#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002303 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002304 void assign(initializer_list<value_type> __il)
2305 {assign(__il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +00002306#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002307
Howard Hinnant1c936782011-06-03 19:40:40 +00002308 _LIBCPP_INLINE_VISIBILITY allocator_type get_allocator() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002309 {return allocator_type(this->__alloc());}
2310
Howard Hinnant1c936782011-06-03 19:40:40 +00002311 size_type max_size() const _NOEXCEPT;
2312 _LIBCPP_INLINE_VISIBILITY
2313 size_type capacity() const _NOEXCEPT
2314 {return __internal_cap_to_external(__cap());}
2315 _LIBCPP_INLINE_VISIBILITY
2316 size_type size() const _NOEXCEPT
2317 {return __size_;}
Marshall Clow425f5752017-11-15 05:51:26 +00002318 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +00002319 bool empty() const _NOEXCEPT
2320 {return __size_ == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002321 void reserve(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +00002322 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002323
Howard Hinnant1c936782011-06-03 19:40:40 +00002324 _LIBCPP_INLINE_VISIBILITY
2325 iterator begin() _NOEXCEPT
2326 {return __make_iter(0);}
2327 _LIBCPP_INLINE_VISIBILITY
2328 const_iterator begin() const _NOEXCEPT
2329 {return __make_iter(0);}
2330 _LIBCPP_INLINE_VISIBILITY
2331 iterator end() _NOEXCEPT
2332 {return __make_iter(__size_);}
2333 _LIBCPP_INLINE_VISIBILITY
2334 const_iterator end() const _NOEXCEPT
2335 {return __make_iter(__size_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002336
Howard Hinnant1c936782011-06-03 19:40:40 +00002337 _LIBCPP_INLINE_VISIBILITY
2338 reverse_iterator rbegin() _NOEXCEPT
2339 {return reverse_iterator(end());}
2340 _LIBCPP_INLINE_VISIBILITY
2341 const_reverse_iterator rbegin() const _NOEXCEPT
2342 {return const_reverse_iterator(end());}
2343 _LIBCPP_INLINE_VISIBILITY
2344 reverse_iterator rend() _NOEXCEPT
2345 {return reverse_iterator(begin());}
2346 _LIBCPP_INLINE_VISIBILITY
2347 const_reverse_iterator rend() const _NOEXCEPT
2348 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002349
Howard Hinnant1c936782011-06-03 19:40:40 +00002350 _LIBCPP_INLINE_VISIBILITY
2351 const_iterator cbegin() const _NOEXCEPT
2352 {return __make_iter(0);}
2353 _LIBCPP_INLINE_VISIBILITY
2354 const_iterator cend() const _NOEXCEPT
2355 {return __make_iter(__size_);}
2356 _LIBCPP_INLINE_VISIBILITY
2357 const_reverse_iterator crbegin() const _NOEXCEPT
2358 {return rbegin();}
2359 _LIBCPP_INLINE_VISIBILITY
2360 const_reverse_iterator crend() const _NOEXCEPT
2361 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002362
2363 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) {return __make_ref(__n);}
2364 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const {return __make_ref(__n);}
2365 reference at(size_type __n);
2366 const_reference at(size_type __n) const;
2367
2368 _LIBCPP_INLINE_VISIBILITY reference front() {return __make_ref(0);}
2369 _LIBCPP_INLINE_VISIBILITY const_reference front() const {return __make_ref(0);}
2370 _LIBCPP_INLINE_VISIBILITY reference back() {return __make_ref(__size_ - 1);}
2371 _LIBCPP_INLINE_VISIBILITY const_reference back() const {return __make_ref(__size_ - 1);}
2372
2373 void push_back(const value_type& __x);
Marshall Clowc46bb8e2013-08-13 23:54:12 +00002374#if _LIBCPP_STD_VER > 11
2375 template <class... _Args>
Marshall Clowea52cc42017-01-24 23:09:12 +00002376#if _LIBCPP_STD_VER > 14
2377 _LIBCPP_INLINE_VISIBILITY reference emplace_back(_Args&&... __args)
2378#else
2379 _LIBCPP_INLINE_VISIBILITY void emplace_back(_Args&&... __args)
2380#endif
2381 {
Eric Fiselier34ba5b92016-07-21 03:20:17 +00002382 push_back ( value_type ( _VSTD::forward<_Args>(__args)... ));
Marshall Clowea52cc42017-01-24 23:09:12 +00002383#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00002384 return this->back();
Marshall Clowea52cc42017-01-24 23:09:12 +00002385#endif
Eric Fiselier34ba5b92016-07-21 03:20:17 +00002386 }
Marshall Clowc46bb8e2013-08-13 23:54:12 +00002387#endif
2388
Howard Hinnantc51e1022010-05-11 19:42:16 +00002389 _LIBCPP_INLINE_VISIBILITY void pop_back() {--__size_;}
2390
Marshall Clowc46bb8e2013-08-13 23:54:12 +00002391#if _LIBCPP_STD_VER > 11
2392 template <class... _Args>
2393 _LIBCPP_INLINE_VISIBILITY iterator emplace(const_iterator position, _Args&&... __args)
2394 { return insert ( position, value_type ( _VSTD::forward<_Args>(__args)... )); }
2395#endif
2396
Howard Hinnantc51e1022010-05-11 19:42:16 +00002397 iterator insert(const_iterator __position, const value_type& __x);
2398 iterator insert(const_iterator __position, size_type __n, const value_type& __x);
2399 iterator insert(const_iterator __position, size_type __n, const_reference __x);
2400 template <class _InputIterator>
2401 typename enable_if
2402 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002403 __is_cpp17_input_iterator <_InputIterator>::value &&
2404 !__is_cpp17_forward_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002405 iterator
2406 >::type
2407 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
2408 template <class _ForwardIterator>
2409 typename enable_if
2410 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002411 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002412 iterator
2413 >::type
2414 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiseliered9e9362017-04-16 02:40:45 +00002415
2416#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002417 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002418 iterator insert(const_iterator __position, initializer_list<value_type> __il)
2419 {return insert(__position, __il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +00002420#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002421
Howard Hinnantcf823322010-12-17 14:46:43 +00002422 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002423 iterator erase(const_iterator __first, const_iterator __last);
2424
Howard Hinnant1c936782011-06-03 19:40:40 +00002425 _LIBCPP_INLINE_VISIBILITY
2426 void clear() _NOEXCEPT {__size_ = 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002427
Howard Hinnant1c936782011-06-03 19:40:40 +00002428 void swap(vector&)
Marshall Clow8982dcd2015-07-13 20:04:56 +00002429#if _LIBCPP_STD_VER >= 14
2430 _NOEXCEPT;
2431#else
Eric Fiselier69c51982016-12-28 06:06:09 +00002432 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00002433 __is_nothrow_swappable<allocator_type>::value);
2434#endif
Marshall Clow89eb3822016-04-07 14:20:31 +00002435 static void swap(reference __x, reference __y) _NOEXCEPT { _VSTD::swap(__x, __y); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002436
2437 void resize(size_type __sz, value_type __x = false);
Howard Hinnant1c936782011-06-03 19:40:40 +00002438 void flip() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002439
2440 bool __invariants() const;
2441
2442private:
Howard Hinnantcf823322010-12-17 14:46:43 +00002443 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Marshall Clow3ff48e02018-05-22 16:20:28 +00002444 void __vallocate(size_type __n);
2445 void __vdeallocate() _NOEXCEPT;
Howard Hinnant1c936782011-06-03 19:40:40 +00002446 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantea382952013-08-14 18:00:20 +00002447 static size_type __align_it(size_type __new_size) _NOEXCEPT
Eric Fiselierb41db9a2018-10-01 01:59:37 +00002448 {return __new_size + (__bits_per_word-1) & ~((size_type)__bits_per_word-1);}
Howard Hinnantcf823322010-12-17 14:46:43 +00002449 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
2450 _LIBCPP_INLINE_VISIBILITY void __construct_at_end(size_type __n, bool __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002451 template <class _ForwardIterator>
2452 typename enable_if
2453 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002454 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002455 void
2456 >::type
2457 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
2458 void __append(size_type __n, const_reference __x);
Howard Hinnant1c936782011-06-03 19:40:40 +00002459 _LIBCPP_INLINE_VISIBILITY
2460 reference __make_ref(size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002461 {return reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnant1c936782011-06-03 19:40:40 +00002462 _LIBCPP_INLINE_VISIBILITY
2463 const_reference __make_ref(size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002464 {return const_reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnant1c936782011-06-03 19:40:40 +00002465 _LIBCPP_INLINE_VISIBILITY
2466 iterator __make_iter(size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002467 {return iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnant1c936782011-06-03 19:40:40 +00002468 _LIBCPP_INLINE_VISIBILITY
2469 const_iterator __make_iter(size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002470 {return const_iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnant1c936782011-06-03 19:40:40 +00002471 _LIBCPP_INLINE_VISIBILITY
2472 iterator __const_iterator_cast(const_iterator __p) _NOEXCEPT
Howard Hinnant76053d72013-06-27 19:35:32 +00002473 {return begin() + (__p - cbegin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002474
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002475 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002476 void __copy_assign_alloc(const vector& __v)
2477 {__copy_assign_alloc(__v, integral_constant<bool,
2478 __storage_traits::propagate_on_container_copy_assignment::value>());}
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002479 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002480 void __copy_assign_alloc(const vector& __c, true_type)
2481 {
2482 if (__alloc() != __c.__alloc())
Marshall Clow3ff48e02018-05-22 16:20:28 +00002483 __vdeallocate();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002484 __alloc() = __c.__alloc();
2485 }
2486
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002487 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00002488 void __copy_assign_alloc(const vector&, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002489 {}
2490
2491 void __move_assign(vector& __c, false_type);
Howard Hinnant1c936782011-06-03 19:40:40 +00002492 void __move_assign(vector& __c, true_type)
2493 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002494 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002495 void __move_assign_alloc(vector& __c)
Howard Hinnant1c936782011-06-03 19:40:40 +00002496 _NOEXCEPT_(
2497 !__storage_traits::propagate_on_container_move_assignment::value ||
2498 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002499 {__move_assign_alloc(__c, integral_constant<bool,
2500 __storage_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002501 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +00002502 void __move_assign_alloc(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002503 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002504 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002505 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002506 }
2507
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002508 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00002509 void __move_assign_alloc(vector&, false_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002510 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002511 {}
2512
Howard Hinnant1c936782011-06-03 19:40:40 +00002513 size_t __hash_code() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002514
2515 friend class __bit_reference<vector>;
2516 friend class __bit_const_reference<vector>;
2517 friend class __bit_iterator<vector, false>;
2518 friend class __bit_iterator<vector, true>;
Howard Hinnant4210a0f2012-08-17 17:10:18 +00002519 friend struct __bit_array<vector>;
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002520 friend struct _LIBCPP_TEMPLATE_VIS hash<vector>;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002521};
2522
2523template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002524inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002525void
2526vector<bool, _Allocator>::__invalidate_all_iterators()
2527{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002528}
2529
2530// Allocate space for __n objects
2531// throws length_error if __n > max_size()
2532// throws (probably bad_alloc) if memory run out
2533// Precondition: __begin_ == __end_ == __cap() == 0
2534// Precondition: __n > 0
2535// Postcondition: capacity() == __n
2536// Postcondition: size() == 0
2537template <class _Allocator>
2538void
Marshall Clow3ff48e02018-05-22 16:20:28 +00002539vector<bool, _Allocator>::__vallocate(size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002540{
2541 if (__n > max_size())
2542 this->__throw_length_error();
2543 __n = __external_cap_to_internal(__n);
2544 this->__begin_ = __storage_traits::allocate(this->__alloc(), __n);
2545 this->__size_ = 0;
2546 this->__cap() = __n;
2547}
2548
2549template <class _Allocator>
2550void
Marshall Clow3ff48e02018-05-22 16:20:28 +00002551vector<bool, _Allocator>::__vdeallocate() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002552{
Howard Hinnant76053d72013-06-27 19:35:32 +00002553 if (this->__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002554 {
2555 __storage_traits::deallocate(this->__alloc(), this->__begin_, __cap());
2556 __invalidate_all_iterators();
Howard Hinnant76053d72013-06-27 19:35:32 +00002557 this->__begin_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002558 this->__size_ = this->__cap() = 0;
2559 }
2560}
2561
2562template <class _Allocator>
2563typename vector<bool, _Allocator>::size_type
Howard Hinnant1c936782011-06-03 19:40:40 +00002564vector<bool, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002565{
2566 size_type __amax = __storage_traits::max_size(__alloc());
2567 size_type __nmax = numeric_limits<size_type>::max() / 2; // end() >= begin(), always
2568 if (__nmax / __bits_per_word <= __amax)
2569 return __nmax;
2570 return __internal_cap_to_external(__amax);
2571}
2572
2573// Precondition: __new_size > capacity()
2574template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002575inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002576typename vector<bool, _Allocator>::size_type
2577vector<bool, _Allocator>::__recommend(size_type __new_size) const
2578{
2579 const size_type __ms = max_size();
2580 if (__new_size > __ms)
2581 this->__throw_length_error();
2582 const size_type __cap = capacity();
2583 if (__cap >= __ms / 2)
2584 return __ms;
Arthur O'Dwyerc0fb14b2021-07-26 18:23:00 -04002585 return _VSTD::max(2 * __cap, __align_it(__new_size));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002586}
2587
2588// Default constructs __n objects starting at __end_
2589// Precondition: __n > 0
2590// Precondition: size() + __n <= capacity()
2591// Postcondition: size() == size() + __n
2592template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002593inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002594void
2595vector<bool, _Allocator>::__construct_at_end(size_type __n, bool __x)
2596{
2597 size_type __old_size = this->__size_;
2598 this->__size_ += __n;
Marshall Clow1893ec72018-10-23 20:07:45 +00002599 if (__old_size == 0 || ((__old_size - 1) / __bits_per_word) != ((this->__size_ - 1) / __bits_per_word))
2600 {
2601 if (this->__size_ <= __bits_per_word)
2602 this->__begin_[0] = __storage_type(0);
2603 else
2604 this->__begin_[(this->__size_ - 1) / __bits_per_word] = __storage_type(0);
2605 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002606 _VSTD::fill_n(__make_iter(__old_size), __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002607}
2608
2609template <class _Allocator>
2610template <class _ForwardIterator>
2611typename enable_if
2612<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002613 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002614 void
2615>::type
2616vector<bool, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
2617{
2618 size_type __old_size = this->__size_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002619 this->__size_ += _VSTD::distance(__first, __last);
Marshall Clow1893ec72018-10-23 20:07:45 +00002620 if (__old_size == 0 || ((__old_size - 1) / __bits_per_word) != ((this->__size_ - 1) / __bits_per_word))
2621 {
2622 if (this->__size_ <= __bits_per_word)
2623 this->__begin_[0] = __storage_type(0);
2624 else
2625 this->__begin_[(this->__size_ - 1) / __bits_per_word] = __storage_type(0);
2626 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002627 _VSTD::copy(__first, __last, __make_iter(__old_size));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002628}
2629
2630template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002631inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002632vector<bool, _Allocator>::vector()
Marshall Clowe546cbb2015-06-04 02:05:41 +00002633 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant76053d72013-06-27 19:35:32 +00002634 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002635 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002636 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002637{
2638}
2639
2640template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002641inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002642vector<bool, _Allocator>::vector(const allocator_type& __a)
Marshall Clow8f282f42015-06-04 00:10:20 +00002643#if _LIBCPP_STD_VER <= 14
2644 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
2645#else
2646 _NOEXCEPT
2647#endif
Howard Hinnant76053d72013-06-27 19:35:32 +00002648 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002649 __size_(0),
2650 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2651{
2652}
2653
2654template <class _Allocator>
2655vector<bool, _Allocator>::vector(size_type __n)
Howard Hinnant76053d72013-06-27 19:35:32 +00002656 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002657 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002658 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002659{
2660 if (__n > 0)
2661 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002662 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002663 __construct_at_end(__n, false);
2664 }
2665}
2666
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00002667#if _LIBCPP_STD_VER > 11
2668template <class _Allocator>
2669vector<bool, _Allocator>::vector(size_type __n, const allocator_type& __a)
2670 : __begin_(nullptr),
2671 __size_(0),
2672 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2673{
2674 if (__n > 0)
2675 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002676 __vallocate(__n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00002677 __construct_at_end(__n, false);
2678 }
2679}
2680#endif
2681
Howard Hinnantc51e1022010-05-11 19:42:16 +00002682template <class _Allocator>
2683vector<bool, _Allocator>::vector(size_type __n, const value_type& __x)
Howard Hinnant76053d72013-06-27 19:35:32 +00002684 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002685 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002686 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002687{
2688 if (__n > 0)
2689 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002690 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002691 __construct_at_end(__n, __x);
2692 }
2693}
2694
2695template <class _Allocator>
2696vector<bool, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002697 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002698 __size_(0),
2699 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2700{
2701 if (__n > 0)
2702 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002703 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002704 __construct_at_end(__n, __x);
2705 }
2706}
2707
2708template <class _Allocator>
2709template <class _InputIterator>
2710vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002711 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
2712 !__is_cpp17_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002713 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002714 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002715 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002716{
2717#ifndef _LIBCPP_NO_EXCEPTIONS
2718 try
2719 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002720#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002721 for (; __first != __last; ++__first)
2722 push_back(*__first);
2723#ifndef _LIBCPP_NO_EXCEPTIONS
2724 }
2725 catch (...)
2726 {
Howard Hinnant76053d72013-06-27 19:35:32 +00002727 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002728 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2729 __invalidate_all_iterators();
2730 throw;
2731 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002732#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002733}
2734
2735template <class _Allocator>
2736template <class _InputIterator>
2737vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002738 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
2739 !__is_cpp17_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002740 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002741 __size_(0),
2742 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2743{
2744#ifndef _LIBCPP_NO_EXCEPTIONS
2745 try
2746 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002747#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002748 for (; __first != __last; ++__first)
2749 push_back(*__first);
2750#ifndef _LIBCPP_NO_EXCEPTIONS
2751 }
2752 catch (...)
2753 {
Howard Hinnant76053d72013-06-27 19:35:32 +00002754 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002755 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2756 __invalidate_all_iterators();
2757 throw;
2758 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002759#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002760}
2761
2762template <class _Allocator>
2763template <class _ForwardIterator>
2764vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002765 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002766 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002767 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002768 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002769{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002770 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002771 if (__n > 0)
2772 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002773 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002774 __construct_at_end(__first, __last);
2775 }
2776}
2777
2778template <class _Allocator>
2779template <class _ForwardIterator>
2780vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002781 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002782 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002783 __size_(0),
2784 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2785{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002786 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002787 if (__n > 0)
2788 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002789 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002790 __construct_at_end(__first, __last);
2791 }
2792}
2793
Eric Fiseliered9e9362017-04-16 02:40:45 +00002794#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002795
Howard Hinnantc51e1022010-05-11 19:42:16 +00002796template <class _Allocator>
2797vector<bool, _Allocator>::vector(initializer_list<value_type> __il)
Howard Hinnant76053d72013-06-27 19:35:32 +00002798 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002799 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002800 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002801{
2802 size_type __n = static_cast<size_type>(__il.size());
2803 if (__n > 0)
2804 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002805 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002806 __construct_at_end(__il.begin(), __il.end());
2807 }
2808}
2809
2810template <class _Allocator>
2811vector<bool, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002812 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002813 __size_(0),
2814 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2815{
2816 size_type __n = static_cast<size_type>(__il.size());
2817 if (__n > 0)
2818 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002819 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002820 __construct_at_end(__il.begin(), __il.end());
2821 }
2822}
2823
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002824#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002825
Howard Hinnantc51e1022010-05-11 19:42:16 +00002826template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002827vector<bool, _Allocator>::~vector()
2828{
Howard Hinnant76053d72013-06-27 19:35:32 +00002829 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002830 __storage_traits::deallocate(__alloc(), __begin_, __cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002831 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002832}
2833
2834template <class _Allocator>
2835vector<bool, _Allocator>::vector(const vector& __v)
Howard Hinnant76053d72013-06-27 19:35:32 +00002836 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002837 __size_(0),
2838 __cap_alloc_(0, __storage_traits::select_on_container_copy_construction(__v.__alloc()))
2839{
2840 if (__v.size() > 0)
2841 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002842 __vallocate(__v.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002843 __construct_at_end(__v.begin(), __v.end());
2844 }
2845}
2846
2847template <class _Allocator>
2848vector<bool, _Allocator>::vector(const vector& __v, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002849 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002850 __size_(0),
2851 __cap_alloc_(0, __a)
2852{
2853 if (__v.size() > 0)
2854 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002855 __vallocate(__v.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002856 __construct_at_end(__v.begin(), __v.end());
2857 }
2858}
2859
2860template <class _Allocator>
2861vector<bool, _Allocator>&
2862vector<bool, _Allocator>::operator=(const vector& __v)
2863{
Mark de Wever357a1fc2021-09-28 19:15:18 +02002864 if (this != _VSTD::addressof(__v))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002865 {
2866 __copy_assign_alloc(__v);
2867 if (__v.__size_)
2868 {
2869 if (__v.__size_ > capacity())
2870 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002871 __vdeallocate();
2872 __vallocate(__v.__size_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002873 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002874 _VSTD::copy(__v.__begin_, __v.__begin_ + __external_cap_to_internal(__v.__size_), __begin_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002875 }
2876 __size_ = __v.__size_;
2877 }
2878 return *this;
2879}
2880
Eric Fiseliered9e9362017-04-16 02:40:45 +00002881#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant74279a52010-09-04 23:28:19 +00002882
Howard Hinnantc51e1022010-05-11 19:42:16 +00002883template <class _Allocator>
Eric Fiselier4f1534c2018-06-05 22:32:52 +00002884inline _LIBCPP_INLINE_VISIBILITY vector<bool, _Allocator>::vector(vector&& __v)
Marshall Clowe5108202015-07-14 14:46:32 +00002885#if _LIBCPP_STD_VER > 14
Eric Fiselier4f1534c2018-06-05 22:32:52 +00002886 _NOEXCEPT
Marshall Clowe5108202015-07-14 14:46:32 +00002887#else
Eric Fiselier4f1534c2018-06-05 22:32:52 +00002888 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowe5108202015-07-14 14:46:32 +00002889#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002890 : __begin_(__v.__begin_),
2891 __size_(__v.__size_),
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05002892 __cap_alloc_(_VSTD::move(__v.__cap_alloc_)) {
Howard Hinnant76053d72013-06-27 19:35:32 +00002893 __v.__begin_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002894 __v.__size_ = 0;
2895 __v.__cap() = 0;
2896}
2897
2898template <class _Allocator>
Arthur O'Dwyer9b9662f2021-03-01 17:08:24 -05002899vector<bool, _Allocator>::vector(vector&& __v, const __identity_t<allocator_type>& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002900 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002901 __size_(0),
2902 __cap_alloc_(0, __a)
2903{
2904 if (__a == allocator_type(__v.__alloc()))
2905 {
2906 this->__begin_ = __v.__begin_;
2907 this->__size_ = __v.__size_;
2908 this->__cap() = __v.__cap();
2909 __v.__begin_ = nullptr;
2910 __v.__cap() = __v.__size_ = 0;
2911 }
2912 else if (__v.size() > 0)
2913 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002914 __vallocate(__v.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002915 __construct_at_end(__v.begin(), __v.end());
2916 }
2917}
2918
2919template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002920inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002921vector<bool, _Allocator>&
2922vector<bool, _Allocator>::operator=(vector&& __v)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002923 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002924{
2925 __move_assign(__v, integral_constant<bool,
2926 __storage_traits::propagate_on_container_move_assignment::value>());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002927 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002928}
2929
2930template <class _Allocator>
2931void
2932vector<bool, _Allocator>::__move_assign(vector& __c, false_type)
2933{
2934 if (__alloc() != __c.__alloc())
2935 assign(__c.begin(), __c.end());
2936 else
2937 __move_assign(__c, true_type());
2938}
2939
2940template <class _Allocator>
2941void
2942vector<bool, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002943 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002944{
Marshall Clow3ff48e02018-05-22 16:20:28 +00002945 __vdeallocate();
Marshall Clowb4871fa2014-07-21 15:15:15 +00002946 __move_assign_alloc(__c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002947 this->__begin_ = __c.__begin_;
2948 this->__size_ = __c.__size_;
2949 this->__cap() = __c.__cap();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002950 __c.__begin_ = nullptr;
2951 __c.__cap() = __c.__size_ = 0;
2952}
Howard Hinnant74279a52010-09-04 23:28:19 +00002953
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002954#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002955
2956template <class _Allocator>
2957void
2958vector<bool, _Allocator>::assign(size_type __n, const value_type& __x)
2959{
2960 __size_ = 0;
2961 if (__n > 0)
2962 {
2963 size_type __c = capacity();
2964 if (__n <= __c)
2965 __size_ = __n;
2966 else
2967 {
Mikhail Maltsevf56b0692021-10-21 10:38:56 +01002968 vector __v(get_allocator());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002969 __v.reserve(__recommend(__n));
2970 __v.__size_ = __n;
2971 swap(__v);
2972 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002973 _VSTD::fill_n(begin(), __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002974 }
Eric Fiselier69c51982016-12-28 06:06:09 +00002975 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002976}
2977
2978template <class _Allocator>
2979template <class _InputIterator>
2980typename enable_if
2981<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002982 __is_cpp17_input_iterator<_InputIterator>::value &&
2983 !__is_cpp17_forward_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002984 void
2985>::type
2986vector<bool, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2987{
2988 clear();
2989 for (; __first != __last; ++__first)
2990 push_back(*__first);
2991}
2992
2993template <class _Allocator>
2994template <class _ForwardIterator>
2995typename enable_if
2996<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002997 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002998 void
2999>::type
3000vector<bool, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
3001{
3002 clear();
Eric Fiselier6003c772016-12-23 23:37:52 +00003003 difference_type __ns = _VSTD::distance(__first, __last);
3004 _LIBCPP_ASSERT(__ns >= 0, "invalid range specified");
3005 const size_t __n = static_cast<size_type>(__ns);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003006 if (__n)
3007 {
3008 if (__n > capacity())
3009 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00003010 __vdeallocate();
3011 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003012 }
3013 __construct_at_end(__first, __last);
3014 }
3015}
3016
3017template <class _Allocator>
3018void
3019vector<bool, _Allocator>::reserve(size_type __n)
3020{
3021 if (__n > capacity())
3022 {
Mikhail Maltsev0b0a51d2021-10-21 10:40:05 +01003023 if (__n > max_size())
3024 this->__throw_length_error();
Mikhail Maltsevf56b0692021-10-21 10:38:56 +01003025 vector __v(this->get_allocator());
Marshall Clow3ff48e02018-05-22 16:20:28 +00003026 __v.__vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003027 __v.__construct_at_end(this->begin(), this->end());
3028 swap(__v);
3029 __invalidate_all_iterators();
3030 }
3031}
3032
3033template <class _Allocator>
3034void
Howard Hinnant1c936782011-06-03 19:40:40 +00003035vector<bool, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003036{
3037 if (__external_cap_to_internal(size()) > __cap())
3038 {
3039#ifndef _LIBCPP_NO_EXCEPTIONS
3040 try
3041 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003042#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003043 vector(*this, allocator_type(__alloc())).swap(*this);
3044#ifndef _LIBCPP_NO_EXCEPTIONS
3045 }
3046 catch (...)
3047 {
3048 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003049#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003050 }
3051}
3052
3053template <class _Allocator>
3054typename vector<bool, _Allocator>::reference
3055vector<bool, _Allocator>::at(size_type __n)
3056{
3057 if (__n >= size())
3058 this->__throw_out_of_range();
3059 return (*this)[__n];
3060}
3061
3062template <class _Allocator>
3063typename vector<bool, _Allocator>::const_reference
3064vector<bool, _Allocator>::at(size_type __n) const
3065{
3066 if (__n >= size())
3067 this->__throw_out_of_range();
3068 return (*this)[__n];
3069}
3070
3071template <class _Allocator>
3072void
3073vector<bool, _Allocator>::push_back(const value_type& __x)
3074{
3075 if (this->__size_ == this->capacity())
3076 reserve(__recommend(this->__size_ + 1));
3077 ++this->__size_;
3078 back() = __x;
3079}
3080
3081template <class _Allocator>
3082typename vector<bool, _Allocator>::iterator
3083vector<bool, _Allocator>::insert(const_iterator __position, const value_type& __x)
3084{
3085 iterator __r;
3086 if (size() < capacity())
3087 {
3088 const_iterator __old_end = end();
3089 ++__size_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003090 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003091 __r = __const_iterator_cast(__position);
3092 }
3093 else
3094 {
Mikhail Maltsevf56b0692021-10-21 10:38:56 +01003095 vector __v(get_allocator());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003096 __v.reserve(__recommend(__size_ + 1));
3097 __v.__size_ = __size_ + 1;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003098 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3099 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003100 swap(__v);
3101 }
3102 *__r = __x;
3103 return __r;
3104}
3105
3106template <class _Allocator>
3107typename vector<bool, _Allocator>::iterator
3108vector<bool, _Allocator>::insert(const_iterator __position, size_type __n, const value_type& __x)
3109{
3110 iterator __r;
3111 size_type __c = capacity();
3112 if (__n <= __c && size() <= __c - __n)
3113 {
3114 const_iterator __old_end = end();
3115 __size_ += __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003116 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003117 __r = __const_iterator_cast(__position);
3118 }
3119 else
3120 {
Mikhail Maltsevf56b0692021-10-21 10:38:56 +01003121 vector __v(get_allocator());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003122 __v.reserve(__recommend(__size_ + __n));
3123 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003124 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3125 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003126 swap(__v);
3127 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003128 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003129 return __r;
3130}
3131
3132template <class _Allocator>
3133template <class _InputIterator>
3134typename enable_if
3135<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05003136 __is_cpp17_input_iterator <_InputIterator>::value &&
3137 !__is_cpp17_forward_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003138 typename vector<bool, _Allocator>::iterator
3139>::type
3140vector<bool, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
3141{
3142 difference_type __off = __position - begin();
3143 iterator __p = __const_iterator_cast(__position);
3144 iterator __old_end = end();
3145 for (; size() != capacity() && __first != __last; ++__first)
3146 {
3147 ++this->__size_;
3148 back() = *__first;
3149 }
Mikhail Maltsevf56b0692021-10-21 10:38:56 +01003150 vector __v(get_allocator());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003151 if (__first != __last)
3152 {
3153#ifndef _LIBCPP_NO_EXCEPTIONS
3154 try
3155 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003156#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003157 __v.assign(__first, __last);
3158 difference_type __old_size = static_cast<difference_type>(__old_end - begin());
3159 difference_type __old_p = __p - begin();
3160 reserve(__recommend(size() + __v.size()));
3161 __p = begin() + __old_p;
3162 __old_end = begin() + __old_size;
3163#ifndef _LIBCPP_NO_EXCEPTIONS
3164 }
3165 catch (...)
3166 {
3167 erase(__old_end, end());
3168 throw;
3169 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003170#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003171 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003172 __p = _VSTD::rotate(__p, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003173 insert(__p, __v.begin(), __v.end());
3174 return begin() + __off;
3175}
3176
3177template <class _Allocator>
3178template <class _ForwardIterator>
3179typename enable_if
3180<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05003181 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003182 typename vector<bool, _Allocator>::iterator
3183>::type
3184vector<bool, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
3185{
Eric Fiselier654dd332016-12-11 05:31:00 +00003186 const difference_type __n_signed = _VSTD::distance(__first, __last);
3187 _LIBCPP_ASSERT(__n_signed >= 0, "invalid range specified");
3188 const size_type __n = static_cast<size_type>(__n_signed);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003189 iterator __r;
3190 size_type __c = capacity();
3191 if (__n <= __c && size() <= __c - __n)
3192 {
3193 const_iterator __old_end = end();
3194 __size_ += __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003195 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003196 __r = __const_iterator_cast(__position);
3197 }
3198 else
3199 {
Mikhail Maltsevf56b0692021-10-21 10:38:56 +01003200 vector __v(get_allocator());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003201 __v.reserve(__recommend(__size_ + __n));
3202 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003203 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3204 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003205 swap(__v);
3206 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003207 _VSTD::copy(__first, __last, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003208 return __r;
3209}
3210
3211template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003212inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003213typename vector<bool, _Allocator>::iterator
3214vector<bool, _Allocator>::erase(const_iterator __position)
3215{
3216 iterator __r = __const_iterator_cast(__position);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003217 _VSTD::copy(__position + 1, this->cend(), __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003218 --__size_;
3219 return __r;
3220}
3221
3222template <class _Allocator>
3223typename vector<bool, _Allocator>::iterator
3224vector<bool, _Allocator>::erase(const_iterator __first, const_iterator __last)
3225{
3226 iterator __r = __const_iterator_cast(__first);
3227 difference_type __d = __last - __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003228 _VSTD::copy(__last, this->cend(), __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003229 __size_ -= __d;
3230 return __r;
3231}
3232
3233template <class _Allocator>
3234void
3235vector<bool, _Allocator>::swap(vector& __x)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003236#if _LIBCPP_STD_VER >= 14
3237 _NOEXCEPT
3238#else
Eric Fiselier69c51982016-12-28 06:06:09 +00003239 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003240 __is_nothrow_swappable<allocator_type>::value)
3241#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003242{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003243 _VSTD::swap(this->__begin_, __x.__begin_);
3244 _VSTD::swap(this->__size_, __x.__size_);
3245 _VSTD::swap(this->__cap(), __x.__cap());
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05003246 _VSTD::__swap_allocator(this->__alloc(), __x.__alloc(),
Marshall Clow8982dcd2015-07-13 20:04:56 +00003247 integral_constant<bool, __alloc_traits::propagate_on_container_swap::value>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003248}
3249
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003250template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003251void
3252vector<bool, _Allocator>::resize(size_type __sz, value_type __x)
3253{
3254 size_type __cs = size();
3255 if (__cs < __sz)
3256 {
3257 iterator __r;
3258 size_type __c = capacity();
3259 size_type __n = __sz - __cs;
3260 if (__n <= __c && __cs <= __c - __n)
3261 {
3262 __r = end();
3263 __size_ += __n;
3264 }
3265 else
3266 {
Mikhail Maltsevf56b0692021-10-21 10:38:56 +01003267 vector __v(get_allocator());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003268 __v.reserve(__recommend(__size_ + __n));
3269 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003270 __r = _VSTD::copy(cbegin(), cend(), __v.begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003271 swap(__v);
3272 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003273 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003274 }
3275 else
3276 __size_ = __sz;
3277}
3278
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003279template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003280void
Howard Hinnant1c936782011-06-03 19:40:40 +00003281vector<bool, _Allocator>::flip() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003282{
3283 // do middle whole words
3284 size_type __n = __size_;
3285 __storage_pointer __p = __begin_;
3286 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3287 *__p = ~*__p;
3288 // do last partial word
3289 if (__n > 0)
3290 {
3291 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3292 __storage_type __b = *__p & __m;
3293 *__p &= ~__m;
3294 *__p |= ~__b & __m;
3295 }
3296}
3297
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003298template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003299bool
3300vector<bool, _Allocator>::__invariants() const
3301{
Howard Hinnant76053d72013-06-27 19:35:32 +00003302 if (this->__begin_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003303 {
3304 if (this->__size_ != 0 || this->__cap() != 0)
3305 return false;
3306 }
3307 else
3308 {
3309 if (this->__cap() == 0)
3310 return false;
3311 if (this->__size_ > this->capacity())
3312 return false;
3313 }
3314 return true;
3315}
3316
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003317template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003318size_t
Howard Hinnant1c936782011-06-03 19:40:40 +00003319vector<bool, _Allocator>::__hash_code() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003320{
3321 size_t __h = 0;
3322 // do middle whole words
3323 size_type __n = __size_;
3324 __storage_pointer __p = __begin_;
3325 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3326 __h ^= *__p;
3327 // do last partial word
3328 if (__n > 0)
3329 {
3330 const __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3331 __h ^= *__p & __m;
3332 }
3333 return __h;
3334}
3335
3336template <class _Allocator>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003337struct _LIBCPP_TEMPLATE_VIS hash<vector<bool, _Allocator> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00003338 : public unary_function<vector<bool, _Allocator>, size_t>
3339{
Howard Hinnant1c265cd2010-09-23 18:58:28 +00003340 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +00003341 size_t operator()(const vector<bool, _Allocator>& __vec) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003342 {return __vec.__hash_code();}
3343};
3344
3345template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003346inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003347bool
3348operator==(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3349{
3350 const typename vector<_Tp, _Allocator>::size_type __sz = __x.size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003351 return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003352}
3353
3354template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003355inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003356bool
3357operator!=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3358{
3359 return !(__x == __y);
3360}
3361
3362template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003363inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003364bool
3365operator< (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3366{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003367 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003368}
3369
3370template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003371inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003372bool
3373operator> (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3374{
3375 return __y < __x;
3376}
3377
3378template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003379inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003380bool
3381operator>=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3382{
3383 return !(__x < __y);
3384}
3385
3386template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003387inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003388bool
3389operator<=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3390{
3391 return !(__y < __x);
3392}
3393
3394template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003395inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003396void
3397swap(vector<_Tp, _Allocator>& __x, vector<_Tp, _Allocator>& __y)
Howard Hinnant1c936782011-06-03 19:40:40 +00003398 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00003399{
3400 __x.swap(__y);
3401}
3402
Marshall Clow29b53f22018-12-14 18:49:35 +00003403#if _LIBCPP_STD_VER > 17
3404template <class _Tp, class _Allocator, class _Up>
Marek Kurdeja98b1412020-05-02 13:58:03 +02003405inline _LIBCPP_INLINE_VISIBILITY typename vector<_Tp, _Allocator>::size_type
3406erase(vector<_Tp, _Allocator>& __c, const _Up& __v) {
3407 auto __old_size = __c.size();
3408 __c.erase(_VSTD::remove(__c.begin(), __c.end(), __v), __c.end());
3409 return __old_size - __c.size();
3410}
Marshall Clow29b53f22018-12-14 18:49:35 +00003411
3412template <class _Tp, class _Allocator, class _Predicate>
Marek Kurdeja98b1412020-05-02 13:58:03 +02003413inline _LIBCPP_INLINE_VISIBILITY typename vector<_Tp, _Allocator>::size_type
3414erase_if(vector<_Tp, _Allocator>& __c, _Predicate __pred) {
3415 auto __old_size = __c.size();
3416 __c.erase(_VSTD::remove_if(__c.begin(), __c.end(), __pred), __c.end());
3417 return __old_size - __c.size();
3418}
Marshall Clow29b53f22018-12-14 18:49:35 +00003419#endif
3420
Howard Hinnantc51e1022010-05-11 19:42:16 +00003421_LIBCPP_END_NAMESPACE_STD
3422
Eric Fiselierf4433a32017-05-31 22:07:49 +00003423_LIBCPP_POP_MACROS
3424
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003425#endif // _LIBCPP_VECTOR