blob: 2cd243ae87df4b6eaec7908c2abc52c42e72d8f0 [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;
147 reference& operator=(const bool x) noexcept;
148 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>
Eric Fiselier876c6862016-02-20 00:19:45 +0000275#include <iosfwd> // for forward declaration of vector
Howard Hinnantc51e1022010-05-11 19:42:16 +0000276#include <__bit_reference>
277#include <type_traits>
278#include <climits>
Arthur O'Dwyer7deec122021-03-24 18:19:12 -0400279#include <compare>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000280#include <limits>
281#include <initializer_list>
282#include <memory>
283#include <stdexcept>
284#include <algorithm>
285#include <cstring>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000286#include <version>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000287#include <__split_buffer>
288#include <__functional_base>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000289
Eric Fiselier14b6de92014-08-10 23:53:08 +0000290#include <__debug>
Howard Hinnante6ff0b62013-08-02 00:26:35 +0000291
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000292#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000293#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000294#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000295
Eric Fiselierf4433a32017-05-31 22:07:49 +0000296_LIBCPP_PUSH_MACROS
297#include <__undef_macros>
298
299
Howard Hinnantc51e1022010-05-11 19:42:16 +0000300_LIBCPP_BEGIN_NAMESPACE_STD
301
302template <bool>
Louis Dionne53dca682019-08-28 18:10:39 +0000303class _LIBCPP_TEMPLATE_VIS __vector_base_common
Howard Hinnantc51e1022010-05-11 19:42:16 +0000304{
305protected:
Louis Dionne16fe2952018-07-11 23:14:33 +0000306 _LIBCPP_INLINE_VISIBILITY __vector_base_common() {}
Marshall Clow8fea1612016-08-25 15:09:01 +0000307 _LIBCPP_NORETURN void __throw_length_error() const;
308 _LIBCPP_NORETURN void __throw_out_of_range() const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000309};
310
311template <bool __b>
312void
313__vector_base_common<__b>::__throw_length_error() const
314{
Marshall Clow8fea1612016-08-25 15:09:01 +0000315 _VSTD::__throw_length_error("vector");
Howard Hinnantc51e1022010-05-11 19:42:16 +0000316}
317
318template <bool __b>
319void
320__vector_base_common<__b>::__throw_out_of_range() const
321{
Marshall Clow8fea1612016-08-25 15:09:01 +0000322 _VSTD::__throw_out_of_range("vector");
Howard Hinnantc51e1022010-05-11 19:42:16 +0000323}
324
Eric Fiselier1b57fa82016-09-15 22:27:07 +0000325_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __vector_base_common<true>)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000326
327template <class _Tp, class _Allocator>
328class __vector_base
329 : protected __vector_base_common<true>
330{
Marshall Clowf0ca1492018-05-21 21:30:12 +0000331public:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000332 typedef _Allocator allocator_type;
333 typedef allocator_traits<allocator_type> __alloc_traits;
Marshall Clowf0ca1492018-05-21 21:30:12 +0000334 typedef typename __alloc_traits::size_type size_type;
335protected:
336 typedef _Tp value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000337 typedef value_type& reference;
338 typedef const value_type& const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000339 typedef typename __alloc_traits::difference_type difference_type;
340 typedef typename __alloc_traits::pointer pointer;
341 typedef typename __alloc_traits::const_pointer const_pointer;
342 typedef pointer iterator;
343 typedef const_pointer const_iterator;
344
345 pointer __begin_;
346 pointer __end_;
347 __compressed_pair<pointer, allocator_type> __end_cap_;
348
Howard Hinnant1c936782011-06-03 19:40:40 +0000349 _LIBCPP_INLINE_VISIBILITY
350 allocator_type& __alloc() _NOEXCEPT
351 {return __end_cap_.second();}
352 _LIBCPP_INLINE_VISIBILITY
353 const allocator_type& __alloc() const _NOEXCEPT
354 {return __end_cap_.second();}
355 _LIBCPP_INLINE_VISIBILITY
356 pointer& __end_cap() _NOEXCEPT
357 {return __end_cap_.first();}
358 _LIBCPP_INLINE_VISIBILITY
359 const pointer& __end_cap() const _NOEXCEPT
360 {return __end_cap_.first();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000361
Howard Hinnant1c936782011-06-03 19:40:40 +0000362 _LIBCPP_INLINE_VISIBILITY
363 __vector_base()
364 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnantcf823322010-12-17 14:46:43 +0000365 _LIBCPP_INLINE_VISIBILITY __vector_base(const allocator_type& __a);
Eric Fiselier4f1534c2018-06-05 22:32:52 +0000366#ifndef _LIBCPP_CXX03_LANG
367 _LIBCPP_INLINE_VISIBILITY __vector_base(allocator_type&& __a) _NOEXCEPT;
368#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000369 ~__vector_base();
370
Howard Hinnant1c936782011-06-03 19:40:40 +0000371 _LIBCPP_INLINE_VISIBILITY
372 void clear() _NOEXCEPT {__destruct_at_end(__begin_);}
373 _LIBCPP_INLINE_VISIBILITY
374 size_type capacity() const _NOEXCEPT
375 {return static_cast<size_type>(__end_cap() - __begin_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000376
Howard Hinnant1c936782011-06-03 19:40:40 +0000377 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76053d72013-06-27 19:35:32 +0000378 void __destruct_at_end(pointer __new_last) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000379
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000380 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000381 void __copy_assign_alloc(const __vector_base& __c)
382 {__copy_assign_alloc(__c, integral_constant<bool,
383 __alloc_traits::propagate_on_container_copy_assignment::value>());}
384
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000385 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000386 void __move_assign_alloc(__vector_base& __c)
Howard Hinnant1c936782011-06-03 19:40:40 +0000387 _NOEXCEPT_(
388 !__alloc_traits::propagate_on_container_move_assignment::value ||
389 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000390 {__move_assign_alloc(__c, integral_constant<bool,
391 __alloc_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000392private:
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000393 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000394 void __copy_assign_alloc(const __vector_base& __c, true_type)
395 {
396 if (__alloc() != __c.__alloc())
397 {
398 clear();
399 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
400 __begin_ = __end_ = __end_cap() = nullptr;
401 }
402 __alloc() = __c.__alloc();
403 }
404
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000405 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +0000406 void __copy_assign_alloc(const __vector_base&, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000407 {}
408
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000409 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +0000410 void __move_assign_alloc(__vector_base& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +0000411 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000412 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000413 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000414 }
415
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000416 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +0000417 void __move_assign_alloc(__vector_base&, false_type)
Howard Hinnant1c936782011-06-03 19:40:40 +0000418 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000419 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000420};
421
422template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000423inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000424void
Howard Hinnant76053d72013-06-27 19:35:32 +0000425__vector_base<_Tp, _Allocator>::__destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000426{
Bruce Mitchener405fdcd2017-03-23 14:39:23 +0000427 pointer __soon_to_be_end = __end_;
428 while (__new_last != __soon_to_be_end)
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500429 __alloc_traits::destroy(__alloc(), _VSTD::__to_address(--__soon_to_be_end));
Bruce Mitchener405fdcd2017-03-23 14:39:23 +0000430 __end_ = __new_last;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000431}
432
433template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000434inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000435__vector_base<_Tp, _Allocator>::__vector_base()
Howard Hinnant1c936782011-06-03 19:40:40 +0000436 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant76053d72013-06-27 19:35:32 +0000437 : __begin_(nullptr),
438 __end_(nullptr),
Eric Fiselier33ebfb62019-12-16 18:23:39 -0500439 __end_cap_(nullptr, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +0000440{
441}
442
443template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000444inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000445__vector_base<_Tp, _Allocator>::__vector_base(const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +0000446 : __begin_(nullptr),
447 __end_(nullptr),
448 __end_cap_(nullptr, __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000449{
450}
451
Eric Fiselier4f1534c2018-06-05 22:32:52 +0000452#ifndef _LIBCPP_CXX03_LANG
453template <class _Tp, class _Allocator>
454inline _LIBCPP_INLINE_VISIBILITY
455__vector_base<_Tp, _Allocator>::__vector_base(allocator_type&& __a) _NOEXCEPT
456 : __begin_(nullptr),
457 __end_(nullptr),
Arthur O'Dwyer07b22492020-11-27 11:02:06 -0500458 __end_cap_(nullptr, _VSTD::move(__a)) {}
Eric Fiselier4f1534c2018-06-05 22:32:52 +0000459#endif
460
Howard Hinnantc51e1022010-05-11 19:42:16 +0000461template <class _Tp, class _Allocator>
462__vector_base<_Tp, _Allocator>::~__vector_base()
463{
Howard Hinnant76053d72013-06-27 19:35:32 +0000464 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000465 {
466 clear();
467 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
468 }
469}
470
Eric Fiselier876c6862016-02-20 00:19:45 +0000471template <class _Tp, class _Allocator /* = allocator<_Tp> */>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000472class _LIBCPP_TEMPLATE_VIS vector
Howard Hinnantc51e1022010-05-11 19:42:16 +0000473 : private __vector_base<_Tp, _Allocator>
474{
475private:
476 typedef __vector_base<_Tp, _Allocator> __base;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000477 typedef allocator<_Tp> __default_allocator_type;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000478public:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000479 typedef vector __self;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000480 typedef _Tp value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000481 typedef _Allocator allocator_type;
482 typedef typename __base::__alloc_traits __alloc_traits;
483 typedef typename __base::reference reference;
484 typedef typename __base::const_reference const_reference;
485 typedef typename __base::size_type size_type;
486 typedef typename __base::difference_type difference_type;
487 typedef typename __base::pointer pointer;
488 typedef typename __base::const_pointer const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000489 typedef __wrap_iter<pointer> iterator;
490 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000491 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
492 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000493
Howard Hinnanta416ff02013-03-26 19:04:56 +0000494 static_assert((is_same<typename allocator_type::value_type, value_type>::value),
495 "Allocator::value_type must be same type as value_type");
496
Howard Hinnant1c936782011-06-03 19:40:40 +0000497 _LIBCPP_INLINE_VISIBILITY
Marshall Clowe546cbb2015-06-04 02:05:41 +0000498 vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant27e0e772011-09-14 18:33:51 +0000499 {
Louis Dionneba400782020-10-02 15:02:52 -0400500#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000501 __get_db()->__insert_c(this);
502#endif
503 }
504 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
Marshall Clow8f282f42015-06-04 00:10:20 +0000505#if _LIBCPP_STD_VER <= 14
506 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
507#else
508 _NOEXCEPT
509#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +0000510 : __base(__a)
511 {
Louis Dionneba400782020-10-02 15:02:52 -0400512#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000513 __get_db()->__insert_c(this);
514#endif
515 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000516 explicit vector(size_type __n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +0000517#if _LIBCPP_STD_VER > 11
518 explicit vector(size_type __n, const allocator_type& __a);
519#endif
Marshall Clowf0ca1492018-05-21 21:30:12 +0000520 vector(size_type __n, const value_type& __x);
521 vector(size_type __n, const value_type& __x, const allocator_type& __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000522 template <class _InputIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +0000523 vector(_InputIterator __first,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500524 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
525 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000526 is_constructible<
527 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +0000528 typename iterator_traits<_InputIterator>::reference>::value,
529 _InputIterator>::type __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000530 template <class _InputIterator>
531 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500532 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
533 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000534 is_constructible<
535 value_type,
536 typename iterator_traits<_InputIterator>::reference>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000537 template <class _ForwardIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +0000538 vector(_ForwardIterator __first,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500539 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000540 is_constructible<
541 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +0000542 typename iterator_traits<_ForwardIterator>::reference>::value,
543 _ForwardIterator>::type __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000544 template <class _ForwardIterator>
545 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500546 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000547 is_constructible<
548 value_type,
549 typename iterator_traits<_ForwardIterator>::reference>::value>::type* = 0);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000550
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000551 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant27e0e772011-09-14 18:33:51 +0000552 ~vector()
553 {
Marshall Clow68af4f32018-09-07 15:47:59 +0000554 __annotate_delete();
Louis Dionneba400782020-10-02 15:02:52 -0400555#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000556 __get_db()->__erase_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000557#endif
Marshall Clow68af4f32018-09-07 15:47:59 +0000558 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000559
560 vector(const vector& __x);
561 vector(const vector& __x, const allocator_type& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000562 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000563 vector& operator=(const vector& __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000564
565#ifndef _LIBCPP_CXX03_LANG
566 _LIBCPP_INLINE_VISIBILITY
567 vector(initializer_list<value_type> __il);
568
569 _LIBCPP_INLINE_VISIBILITY
570 vector(initializer_list<value_type> __il, const allocator_type& __a);
571
Howard Hinnantcf823322010-12-17 14:46:43 +0000572 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000573 vector(vector&& __x)
Marshall Clowe5108202015-07-14 14:46:32 +0000574#if _LIBCPP_STD_VER > 14
575 _NOEXCEPT;
576#else
Howard Hinnant1c936782011-06-03 19:40:40 +0000577 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowe5108202015-07-14 14:46:32 +0000578#endif
Eric Fiseliered9e9362017-04-16 02:40:45 +0000579
Howard Hinnantcf823322010-12-17 14:46:43 +0000580 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000581 vector(vector&& __x, const allocator_type& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000582 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000583 vector& operator=(vector&& __x)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000584 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiseliered9e9362017-04-16 02:40:45 +0000585
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000586 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000587 vector& operator=(initializer_list<value_type> __il)
588 {assign(__il.begin(), __il.end()); return *this;}
Eric Fiseliered9e9362017-04-16 02:40:45 +0000589
590#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000591
592 template <class _InputIterator>
593 typename enable_if
594 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500595 __is_cpp17_input_iterator <_InputIterator>::value &&
596 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000597 is_constructible<
598 value_type,
599 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000600 void
601 >::type
602 assign(_InputIterator __first, _InputIterator __last);
603 template <class _ForwardIterator>
604 typename enable_if
605 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500606 __is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000607 is_constructible<
608 value_type,
609 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000610 void
611 >::type
612 assign(_ForwardIterator __first, _ForwardIterator __last);
613
614 void assign(size_type __n, const_reference __u);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000615
616#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000617 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000618 void assign(initializer_list<value_type> __il)
619 {assign(__il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +0000620#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000621
Howard Hinnant1c936782011-06-03 19:40:40 +0000622 _LIBCPP_INLINE_VISIBILITY
623 allocator_type get_allocator() const _NOEXCEPT
624 {return this->__alloc();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000625
Howard Hinnant1c936782011-06-03 19:40:40 +0000626 _LIBCPP_INLINE_VISIBILITY iterator begin() _NOEXCEPT;
627 _LIBCPP_INLINE_VISIBILITY const_iterator begin() const _NOEXCEPT;
628 _LIBCPP_INLINE_VISIBILITY iterator end() _NOEXCEPT;
629 _LIBCPP_INLINE_VISIBILITY const_iterator end() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000630
Howard Hinnant1c936782011-06-03 19:40:40 +0000631 _LIBCPP_INLINE_VISIBILITY
632 reverse_iterator rbegin() _NOEXCEPT
633 {return reverse_iterator(end());}
634 _LIBCPP_INLINE_VISIBILITY
635 const_reverse_iterator rbegin() const _NOEXCEPT
636 {return const_reverse_iterator(end());}
637 _LIBCPP_INLINE_VISIBILITY
638 reverse_iterator rend() _NOEXCEPT
639 {return reverse_iterator(begin());}
640 _LIBCPP_INLINE_VISIBILITY
641 const_reverse_iterator rend() const _NOEXCEPT
642 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000643
Howard Hinnant1c936782011-06-03 19:40:40 +0000644 _LIBCPP_INLINE_VISIBILITY
645 const_iterator cbegin() const _NOEXCEPT
646 {return begin();}
647 _LIBCPP_INLINE_VISIBILITY
648 const_iterator cend() const _NOEXCEPT
649 {return end();}
650 _LIBCPP_INLINE_VISIBILITY
651 const_reverse_iterator crbegin() const _NOEXCEPT
652 {return rbegin();}
653 _LIBCPP_INLINE_VISIBILITY
654 const_reverse_iterator crend() const _NOEXCEPT
655 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000656
Howard Hinnant1c936782011-06-03 19:40:40 +0000657 _LIBCPP_INLINE_VISIBILITY
658 size_type size() const _NOEXCEPT
659 {return static_cast<size_type>(this->__end_ - this->__begin_);}
660 _LIBCPP_INLINE_VISIBILITY
661 size_type capacity() const _NOEXCEPT
662 {return __base::capacity();}
Marshall Clow425f5752017-11-15 05:51:26 +0000663 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000664 bool empty() const _NOEXCEPT
665 {return this->__begin_ == this->__end_;}
666 size_type max_size() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000667 void reserve(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +0000668 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000669
Marshall Clowd6470492019-03-15 00:29:35 +0000670 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) _NOEXCEPT;
671 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000672 reference at(size_type __n);
673 const_reference at(size_type __n) const;
674
Marshall Clowd6470492019-03-15 00:29:35 +0000675 _LIBCPP_INLINE_VISIBILITY reference front() _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000676 {
677 _LIBCPP_ASSERT(!empty(), "front() called for empty vector");
678 return *this->__begin_;
679 }
Marshall Clowd6470492019-03-15 00:29:35 +0000680 _LIBCPP_INLINE_VISIBILITY const_reference front() const _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000681 {
682 _LIBCPP_ASSERT(!empty(), "front() called for empty vector");
683 return *this->__begin_;
684 }
Marshall Clowd6470492019-03-15 00:29:35 +0000685 _LIBCPP_INLINE_VISIBILITY reference back() _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000686 {
687 _LIBCPP_ASSERT(!empty(), "back() called for empty vector");
688 return *(this->__end_ - 1);
689 }
Marshall Clowd6470492019-03-15 00:29:35 +0000690 _LIBCPP_INLINE_VISIBILITY const_reference back() const _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000691 {
692 _LIBCPP_ASSERT(!empty(), "back() called for empty vector");
693 return *(this->__end_ - 1);
694 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000695
Howard Hinnant1c936782011-06-03 19:40:40 +0000696 _LIBCPP_INLINE_VISIBILITY
697 value_type* data() _NOEXCEPT
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500698 {return _VSTD::__to_address(this->__begin_);}
Howard Hinnant1c936782011-06-03 19:40:40 +0000699 _LIBCPP_INLINE_VISIBILITY
700 const value_type* data() const _NOEXCEPT
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500701 {return _VSTD::__to_address(this->__begin_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000702
Eric Fiselier96919722017-10-17 13:03:17 +0000703#ifdef _LIBCPP_CXX03_LANG
704 _LIBCPP_INLINE_VISIBILITY
705 void __emplace_back(const value_type& __x) { push_back(__x); }
706#else
707 template <class _Arg>
708 _LIBCPP_INLINE_VISIBILITY
709 void __emplace_back(_Arg&& __arg) {
710 emplace_back(_VSTD::forward<_Arg>(__arg));
711 }
712#endif
713
Howard Hinnantcf823322010-12-17 14:46:43 +0000714 _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000715
716#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000717 _LIBCPP_INLINE_VISIBILITY void push_back(value_type&& __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000718
Howard Hinnantc51e1022010-05-11 19:42:16 +0000719 template <class... _Args>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000720 _LIBCPP_INLINE_VISIBILITY
Marshall Clowea52cc42017-01-24 23:09:12 +0000721#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +0000722 reference emplace_back(_Args&&... __args);
Marshall Clowea52cc42017-01-24 23:09:12 +0000723#else
724 void emplace_back(_Args&&... __args);
725#endif
Eric Fiseliered9e9362017-04-16 02:40:45 +0000726#endif // !_LIBCPP_CXX03_LANG
727
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000728 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000729 void pop_back();
730
731 iterator insert(const_iterator __position, const_reference __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000732
733#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000734 iterator insert(const_iterator __position, value_type&& __x);
735 template <class... _Args>
736 iterator emplace(const_iterator __position, _Args&&... __args);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000737#endif // !_LIBCPP_CXX03_LANG
738
Howard Hinnantc51e1022010-05-11 19:42:16 +0000739 iterator insert(const_iterator __position, size_type __n, const_reference __x);
740 template <class _InputIterator>
741 typename enable_if
742 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500743 __is_cpp17_input_iterator <_InputIterator>::value &&
744 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000745 is_constructible<
746 value_type,
747 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000748 iterator
749 >::type
750 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
751 template <class _ForwardIterator>
752 typename enable_if
753 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500754 __is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000755 is_constructible<
756 value_type,
757 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000758 iterator
759 >::type
760 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000761
762#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000763 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000764 iterator insert(const_iterator __position, initializer_list<value_type> __il)
765 {return insert(__position, __il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +0000766#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000767
Howard Hinnantcf823322010-12-17 14:46:43 +0000768 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000769 iterator erase(const_iterator __first, const_iterator __last);
770
Howard Hinnant1c936782011-06-03 19:40:40 +0000771 _LIBCPP_INLINE_VISIBILITY
772 void clear() _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000773 {
Marshall Clow2cd9d372014-05-08 14:14:06 +0000774 size_type __old_size = size();
Howard Hinnant27e0e772011-09-14 18:33:51 +0000775 __base::clear();
Marshall Clow2cd9d372014-05-08 14:14:06 +0000776 __annotate_shrink(__old_size);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000777 __invalidate_all_iterators();
778 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000779
780 void resize(size_type __sz);
781 void resize(size_type __sz, const_reference __x);
782
Howard Hinnant1c936782011-06-03 19:40:40 +0000783 void swap(vector&)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000784#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +0000785 _NOEXCEPT;
Marshall Clow8982dcd2015-07-13 20:04:56 +0000786#else
Eric Fiselier873b8d32019-03-18 21:50:12 +0000787 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +0000788 __is_nothrow_swappable<allocator_type>::value);
789#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000790
791 bool __invariants() const;
792
Louis Dionneba400782020-10-02 15:02:52 -0400793#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000794
795 bool __dereferenceable(const const_iterator* __i) const;
796 bool __decrementable(const const_iterator* __i) const;
797 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
798 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
799
Louis Dionneba400782020-10-02 15:02:52 -0400800#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000801
Howard Hinnantc51e1022010-05-11 19:42:16 +0000802private:
Howard Hinnantcf823322010-12-17 14:46:43 +0000803 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Eric Fiselier69c51982016-12-28 06:06:09 +0000804 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(pointer __new_last);
Marshall Clow3ff48e02018-05-22 16:20:28 +0000805 void __vallocate(size_type __n);
806 void __vdeallocate() _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000807 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
Howard Hinnanta5589382011-01-04 19:53:31 +0000808 void __construct_at_end(size_type __n);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000809 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000810 void __construct_at_end(size_type __n, const_reference __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000811 template <class _ForwardIterator>
812 typename enable_if
813 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500814 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000815 void
816 >::type
Eric Fiselieracfe6f02015-03-31 16:54:19 +0000817 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000818 void __append(size_type __n);
819 void __append(size_type __n, const_reference __x);
Howard Hinnantcf823322010-12-17 14:46:43 +0000820 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000821 iterator __make_iter(pointer __p) _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000822 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000823 const_iterator __make_iter(const_pointer __p) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000824 void __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v);
825 pointer __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p);
826 void __move_range(pointer __from_s, pointer __from_e, pointer __to);
Howard Hinnant1c936782011-06-03 19:40:40 +0000827 void __move_assign(vector& __c, true_type)
828 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000829 void __move_assign(vector& __c, false_type)
830 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000831 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76053d72013-06-27 19:35:32 +0000832 void __destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000833 {
Eric Fiselier69c51982016-12-28 06:06:09 +0000834 __invalidate_iterators_past(__new_last);
Marshall Clow2cd9d372014-05-08 14:14:06 +0000835 size_type __old_size = size();
Howard Hinnant27e0e772011-09-14 18:33:51 +0000836 __base::__destruct_at_end(__new_last);
Marshall Clow2cd9d372014-05-08 14:14:06 +0000837 __annotate_shrink(__old_size);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000838 }
Eric Fiseliered9e9362017-04-16 02:40:45 +0000839
840#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier7110f442019-03-19 19:19:44 +0000841 template <class _Up>
842 _LIBCPP_INLINE_VISIBILITY
843 inline void __push_back_slow_path(_Up&& __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000844
Howard Hinnantb6c49562012-02-26 15:30:12 +0000845 template <class... _Args>
Eric Fiselier7110f442019-03-19 19:19:44 +0000846 _LIBCPP_INLINE_VISIBILITY
847 inline void __emplace_back_slow_path(_Args&&... __args);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000848#else
Eric Fiselier7110f442019-03-19 19:19:44 +0000849 template <class _Up>
850 _LIBCPP_INLINE_VISIBILITY
851 inline void __push_back_slow_path(_Up& __x);
Howard Hinnantb6c49562012-02-26 15:30:12 +0000852#endif
Eric Fiseliered9e9362017-04-16 02:40:45 +0000853
Marshall Clow2cd9d372014-05-08 14:14:06 +0000854 // The following functions are no-ops outside of AddressSanitizer mode.
855 // We call annotatations only for the default Allocator because other allocators
856 // may not meet the AddressSanitizer alignment constraints.
857 // See the documentation for __sanitizer_annotate_contiguous_container for more details.
Marshall Clow2cd9d372014-05-08 14:14:06 +0000858#ifndef _LIBCPP_HAS_NO_ASAN
Eric Fiselier6003c772016-12-23 23:37:52 +0000859 void __annotate_contiguous_container(const void *__beg, const void *__end,
860 const void *__old_mid,
861 const void *__new_mid) const
862 {
863
Marshall Clow2cd9d372014-05-08 14:14:06 +0000864 if (__beg && is_same<allocator_type, __default_allocator_type>::value)
865 __sanitizer_annotate_contiguous_container(__beg, __end, __old_mid, __new_mid);
Marshall Clow2cd9d372014-05-08 14:14:06 +0000866 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000867#else
868 _LIBCPP_INLINE_VISIBILITY
869 void __annotate_contiguous_container(const void*, const void*, const void*,
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000870 const void*) const _NOEXCEPT {}
Eric Fiselier6003c772016-12-23 23:37:52 +0000871#endif
872 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000873 void __annotate_new(size_type __current_size) const _NOEXCEPT {
Marshall Clow2cd9d372014-05-08 14:14:06 +0000874 __annotate_contiguous_container(data(), data() + capacity(),
875 data() + capacity(), data() + __current_size);
876 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000877
878 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000879 void __annotate_delete() const _NOEXCEPT {
Marshall Clow2cd9d372014-05-08 14:14:06 +0000880 __annotate_contiguous_container(data(), data() + capacity(),
881 data() + size(), data() + capacity());
882 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000883
884 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000885 void __annotate_increase(size_type __n) const _NOEXCEPT
Marshall Clow2cd9d372014-05-08 14:14:06 +0000886 {
887 __annotate_contiguous_container(data(), data() + capacity(),
888 data() + size(), data() + size() + __n);
889 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000890
891 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000892 void __annotate_shrink(size_type __old_size) const _NOEXCEPT
Marshall Clow2cd9d372014-05-08 14:14:06 +0000893 {
894 __annotate_contiguous_container(data(), data() + capacity(),
895 data() + __old_size, data() + size());
896 }
Marshall Clowc78ffa52014-09-03 21:37:43 +0000897
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000898 struct _ConstructTransaction {
899 explicit _ConstructTransaction(vector &__v, size_type __n)
900 : __v_(__v), __pos_(__v.__end_), __new_end_(__v.__end_ + __n) {
901#ifndef _LIBCPP_HAS_NO_ASAN
902 __v_.__annotate_increase(__n);
903#endif
904 }
905 ~_ConstructTransaction() {
906 __v_.__end_ = __pos_;
907#ifndef _LIBCPP_HAS_NO_ASAN
908 if (__pos_ != __new_end_) {
909 __v_.__annotate_shrink(__new_end_ - __v_.__begin_);
910 }
911#endif
912 }
913
914 vector &__v_;
915 pointer __pos_;
916 const_pointer const __new_end_;
917
918 private:
919 _ConstructTransaction(_ConstructTransaction const&) = delete;
920 _ConstructTransaction& operator=(_ConstructTransaction const&) = delete;
921 };
922
923 template <class ..._Args>
924 _LIBCPP_INLINE_VISIBILITY
925 void __construct_one_at_end(_Args&& ...__args) {
926 _ConstructTransaction __tx(*this, 1);
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500927 __alloc_traits::construct(this->__alloc(), _VSTD::__to_address(__tx.__pos_),
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000928 _VSTD::forward<_Args>(__args)...);
929 ++__tx.__pos_;
930 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000931};
932
Marshall Clowf0ca1492018-05-21 21:30:12 +0000933#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
934template<class _InputIterator,
Arthur O'Dwyer56226762021-03-03 23:02:20 -0500935 class _Alloc = allocator<__iter_value_type<_InputIterator>>,
936 class = _EnableIf<__is_allocator<_Alloc>::value>
Marshall Clowf0ca1492018-05-21 21:30:12 +0000937 >
938vector(_InputIterator, _InputIterator)
Arthur O'Dwyer56226762021-03-03 23:02:20 -0500939 -> vector<__iter_value_type<_InputIterator>, _Alloc>;
Marshall Clowf0ca1492018-05-21 21:30:12 +0000940
941template<class _InputIterator,
942 class _Alloc,
Arthur O'Dwyer56226762021-03-03 23:02:20 -0500943 class = _EnableIf<__is_allocator<_Alloc>::value>
Marshall Clowf0ca1492018-05-21 21:30:12 +0000944 >
945vector(_InputIterator, _InputIterator, _Alloc)
Arthur O'Dwyer56226762021-03-03 23:02:20 -0500946 -> vector<__iter_value_type<_InputIterator>, _Alloc>;
Marshall Clowf0ca1492018-05-21 21:30:12 +0000947#endif
948
Howard Hinnantc51e1022010-05-11 19:42:16 +0000949template <class _Tp, class _Allocator>
950void
951vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v)
952{
Eric Fiselier909fe962019-09-13 16:09:33 +0000953
Marshall Clow2cd9d372014-05-08 14:14:06 +0000954 __annotate_delete();
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -0500955 _VSTD::__construct_backward_with_exception_guarantees(this->__alloc(), this->__begin_, this->__end_, __v.__begin_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000956 _VSTD::swap(this->__begin_, __v.__begin_);
957 _VSTD::swap(this->__end_, __v.__end_);
958 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000959 __v.__first_ = __v.__begin_;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000960 __annotate_new(size());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000961 __invalidate_all_iterators();
962}
963
964template <class _Tp, class _Allocator>
965typename vector<_Tp, _Allocator>::pointer
966vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p)
967{
Marshall Clow2cd9d372014-05-08 14:14:06 +0000968 __annotate_delete();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000969 pointer __r = __v.__begin_;
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -0500970 _VSTD::__construct_backward_with_exception_guarantees(this->__alloc(), this->__begin_, __p, __v.__begin_);
971 _VSTD::__construct_forward_with_exception_guarantees(this->__alloc(), __p, this->__end_, __v.__end_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000972 _VSTD::swap(this->__begin_, __v.__begin_);
973 _VSTD::swap(this->__end_, __v.__end_);
974 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000975 __v.__first_ = __v.__begin_;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000976 __annotate_new(size());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000977 __invalidate_all_iterators();
978 return __r;
979}
980
981// Allocate space for __n objects
982// throws length_error if __n > max_size()
983// throws (probably bad_alloc) if memory run out
984// Precondition: __begin_ == __end_ == __end_cap() == 0
985// Precondition: __n > 0
986// Postcondition: capacity() == __n
987// Postcondition: size() == 0
988template <class _Tp, class _Allocator>
989void
Marshall Clow3ff48e02018-05-22 16:20:28 +0000990vector<_Tp, _Allocator>::__vallocate(size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000991{
992 if (__n > max_size())
993 this->__throw_length_error();
994 this->__begin_ = this->__end_ = __alloc_traits::allocate(this->__alloc(), __n);
995 this->__end_cap() = this->__begin_ + __n;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000996 __annotate_new(0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000997}
998
999template <class _Tp, class _Allocator>
1000void
Marshall Clow3ff48e02018-05-22 16:20:28 +00001001vector<_Tp, _Allocator>::__vdeallocate() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001002{
Howard Hinnant76053d72013-06-27 19:35:32 +00001003 if (this->__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001004 {
1005 clear();
1006 __alloc_traits::deallocate(this->__alloc(), this->__begin_, capacity());
Howard Hinnant76053d72013-06-27 19:35:32 +00001007 this->__begin_ = this->__end_ = this->__end_cap() = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001008 }
1009}
1010
1011template <class _Tp, class _Allocator>
1012typename vector<_Tp, _Allocator>::size_type
Howard Hinnant1c936782011-06-03 19:40:40 +00001013vector<_Tp, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001014{
Eric Fiselierb5d9f442016-11-23 01:18:56 +00001015 return _VSTD::min<size_type>(__alloc_traits::max_size(this->__alloc()),
1016 numeric_limits<difference_type>::max());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001017}
1018
1019// Precondition: __new_size > capacity()
1020template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001021inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001022typename vector<_Tp, _Allocator>::size_type
1023vector<_Tp, _Allocator>::__recommend(size_type __new_size) const
1024{
1025 const size_type __ms = max_size();
1026 if (__new_size > __ms)
1027 this->__throw_length_error();
1028 const size_type __cap = capacity();
1029 if (__cap >= __ms / 2)
1030 return __ms;
Alexis Hunt991d29b2011-07-29 23:31:58 +00001031 return _VSTD::max<size_type>(2*__cap, __new_size);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001032}
1033
1034// Default constructs __n objects starting at __end_
1035// throws if construction throws
1036// Precondition: __n > 0
1037// Precondition: size() + __n <= capacity()
1038// Postcondition: size() == size() + __n
1039template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001040void
1041vector<_Tp, _Allocator>::__construct_at_end(size_type __n)
1042{
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001043 _ConstructTransaction __tx(*this, __n);
Martijn Vels1e2e1b82020-06-18 13:14:02 -04001044 const_pointer __new_end = __tx.__new_end_;
1045 for (pointer __pos = __tx.__pos_; __pos != __new_end; ++__pos, __tx.__pos_ = __pos) {
1046 __alloc_traits::construct(this->__alloc(), _VSTD::__to_address(__pos));
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001047 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001048}
1049
Howard Hinnantc51e1022010-05-11 19:42:16 +00001050// Copy constructs __n objects starting at __end_ from __x
1051// throws if construction throws
1052// Precondition: __n > 0
1053// Precondition: size() + __n <= capacity()
1054// Postcondition: size() == old size() + __n
1055// Postcondition: [i] == __x for all i in [size() - __n, __n)
1056template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001057inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001058void
1059vector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
1060{
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001061 _ConstructTransaction __tx(*this, __n);
Martijn Vels1e2e1b82020-06-18 13:14:02 -04001062 const_pointer __new_end = __tx.__new_end_;
1063 for (pointer __pos = __tx.__pos_; __pos != __new_end; ++__pos, __tx.__pos_ = __pos) {
1064 __alloc_traits::construct(this->__alloc(), _VSTD::__to_address(__pos), __x);
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001065 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001066}
1067
1068template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001069template <class _ForwardIterator>
1070typename enable_if
1071<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001072 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001073 void
1074>::type
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001075vector<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001076{
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001077 _ConstructTransaction __tx(*this, __n);
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05001078 _VSTD::__construct_range_forward(this->__alloc(), __first, __last, __tx.__pos_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001079}
1080
1081// Default constructs __n objects starting at __end_
1082// throws if construction throws
1083// Postcondition: size() == size() + __n
Howard Hinnant1c936782011-06-03 19:40:40 +00001084// Exception safety: strong.
Howard Hinnantc51e1022010-05-11 19:42:16 +00001085template <class _Tp, class _Allocator>
1086void
1087vector<_Tp, _Allocator>::__append(size_type __n)
1088{
1089 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1090 this->__construct_at_end(__n);
1091 else
1092 {
1093 allocator_type& __a = this->__alloc();
1094 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1095 __v.__construct_at_end(__n);
1096 __swap_out_circular_buffer(__v);
1097 }
1098}
1099
1100// Default constructs __n objects starting at __end_
1101// throws if construction throws
1102// Postcondition: size() == size() + __n
Howard Hinnant1c936782011-06-03 19:40:40 +00001103// Exception safety: strong.
Howard Hinnantc51e1022010-05-11 19:42:16 +00001104template <class _Tp, class _Allocator>
1105void
1106vector<_Tp, _Allocator>::__append(size_type __n, const_reference __x)
1107{
1108 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1109 this->__construct_at_end(__n, __x);
1110 else
1111 {
1112 allocator_type& __a = this->__alloc();
1113 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1114 __v.__construct_at_end(__n, __x);
1115 __swap_out_circular_buffer(__v);
1116 }
1117}
1118
1119template <class _Tp, class _Allocator>
1120vector<_Tp, _Allocator>::vector(size_type __n)
1121{
Louis Dionneba400782020-10-02 15:02:52 -04001122#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001123 __get_db()->__insert_c(this);
1124#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001125 if (__n > 0)
1126 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001127 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001128 __construct_at_end(__n);
1129 }
1130}
1131
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00001132#if _LIBCPP_STD_VER > 11
1133template <class _Tp, class _Allocator>
1134vector<_Tp, _Allocator>::vector(size_type __n, const allocator_type& __a)
1135 : __base(__a)
1136{
Louis Dionneba400782020-10-02 15:02:52 -04001137#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00001138 __get_db()->__insert_c(this);
1139#endif
1140 if (__n > 0)
1141 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001142 __vallocate(__n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00001143 __construct_at_end(__n);
1144 }
1145}
1146#endif
1147
Howard Hinnantc51e1022010-05-11 19:42:16 +00001148template <class _Tp, class _Allocator>
Marshall Clowf0ca1492018-05-21 21:30:12 +00001149vector<_Tp, _Allocator>::vector(size_type __n, const value_type& __x)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001150{
Louis Dionneba400782020-10-02 15:02:52 -04001151#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001152 __get_db()->__insert_c(this);
1153#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001154 if (__n > 0)
1155 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001156 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001157 __construct_at_end(__n, __x);
1158 }
1159}
1160
1161template <class _Tp, class _Allocator>
Marshall Clowf0ca1492018-05-21 21:30:12 +00001162vector<_Tp, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001163 : __base(__a)
1164{
Louis Dionneba400782020-10-02 15:02:52 -04001165#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001166 __get_db()->__insert_c(this);
1167#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001168 if (__n > 0)
1169 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001170 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001171 __construct_at_end(__n, __x);
1172 }
1173}
1174
1175template <class _Tp, class _Allocator>
1176template <class _InputIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +00001177vector<_Tp, _Allocator>::vector(_InputIterator __first,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001178 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
1179 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001180 is_constructible<
1181 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +00001182 typename iterator_traits<_InputIterator>::reference>::value,
1183 _InputIterator>::type __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001184{
Louis Dionneba400782020-10-02 15:02:52 -04001185#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001186 __get_db()->__insert_c(this);
1187#endif
Howard Hinnant9cd22302011-09-16 18:41:29 +00001188 for (; __first != __last; ++__first)
Eric Fiselier96919722017-10-17 13:03:17 +00001189 __emplace_back(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001190}
1191
1192template <class _Tp, class _Allocator>
1193template <class _InputIterator>
1194vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001195 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
1196 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001197 is_constructible<
1198 value_type,
1199 typename iterator_traits<_InputIterator>::reference>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001200 : __base(__a)
1201{
Louis Dionneba400782020-10-02 15:02:52 -04001202#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001203 __get_db()->__insert_c(this);
1204#endif
Howard Hinnant9cd22302011-09-16 18:41:29 +00001205 for (; __first != __last; ++__first)
Eric Fiselier96919722017-10-17 13:03:17 +00001206 __emplace_back(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001207}
1208
1209template <class _Tp, class _Allocator>
1210template <class _ForwardIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +00001211vector<_Tp, _Allocator>::vector(_ForwardIterator __first,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001212 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001213 is_constructible<
1214 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +00001215 typename iterator_traits<_ForwardIterator>::reference>::value,
1216 _ForwardIterator>::type __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001217{
Louis Dionneba400782020-10-02 15:02:52 -04001218#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001219 __get_db()->__insert_c(this);
1220#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001221 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001222 if (__n > 0)
1223 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001224 __vallocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001225 __construct_at_end(__first, __last, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001226 }
1227}
1228
1229template <class _Tp, class _Allocator>
1230template <class _ForwardIterator>
1231vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001232 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001233 is_constructible<
1234 value_type,
1235 typename iterator_traits<_ForwardIterator>::reference>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001236 : __base(__a)
1237{
Louis Dionneba400782020-10-02 15:02:52 -04001238#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001239 __get_db()->__insert_c(this);
1240#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001241 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001242 if (__n > 0)
1243 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001244 __vallocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001245 __construct_at_end(__first, __last, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001246 }
1247}
1248
1249template <class _Tp, class _Allocator>
1250vector<_Tp, _Allocator>::vector(const vector& __x)
1251 : __base(__alloc_traits::select_on_container_copy_construction(__x.__alloc()))
1252{
Louis Dionneba400782020-10-02 15:02:52 -04001253#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001254 __get_db()->__insert_c(this);
1255#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001256 size_type __n = __x.size();
1257 if (__n > 0)
1258 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001259 __vallocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001260 __construct_at_end(__x.__begin_, __x.__end_, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001261 }
1262}
1263
1264template <class _Tp, class _Allocator>
1265vector<_Tp, _Allocator>::vector(const vector& __x, const allocator_type& __a)
1266 : __base(__a)
1267{
Louis Dionneba400782020-10-02 15:02:52 -04001268#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001269 __get_db()->__insert_c(this);
1270#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001271 size_type __n = __x.size();
1272 if (__n > 0)
1273 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001274 __vallocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001275 __construct_at_end(__x.__begin_, __x.__end_, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001276 }
1277}
1278
Eric Fiseliered9e9362017-04-16 02:40:45 +00001279#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001280
1281template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001282inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001283vector<_Tp, _Allocator>::vector(vector&& __x)
Marshall Clowe5108202015-07-14 14:46:32 +00001284#if _LIBCPP_STD_VER > 14
1285 _NOEXCEPT
1286#else
Howard Hinnant1c936782011-06-03 19:40:40 +00001287 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowe5108202015-07-14 14:46:32 +00001288#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001289 : __base(_VSTD::move(__x.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001290{
Louis Dionneba400782020-10-02 15:02:52 -04001291#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001292 __get_db()->__insert_c(this);
Howard Hinnanta1e60cd2011-09-19 16:34:29 +00001293 __get_db()->swap(this, &__x);
Howard Hinnant27e0e772011-09-14 18:33:51 +00001294#endif
Howard Hinnant9cd22302011-09-16 18:41:29 +00001295 this->__begin_ = __x.__begin_;
1296 this->__end_ = __x.__end_;
1297 this->__end_cap() = __x.__end_cap();
Howard Hinnant76053d72013-06-27 19:35:32 +00001298 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001299}
1300
1301template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001302inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001303vector<_Tp, _Allocator>::vector(vector&& __x, const allocator_type& __a)
1304 : __base(__a)
1305{
Louis Dionneba400782020-10-02 15:02:52 -04001306#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001307 __get_db()->__insert_c(this);
1308#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001309 if (__a == __x.__alloc())
1310 {
1311 this->__begin_ = __x.__begin_;
1312 this->__end_ = __x.__end_;
1313 this->__end_cap() = __x.__end_cap();
1314 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Louis Dionneba400782020-10-02 15:02:52 -04001315#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnanta1e60cd2011-09-19 16:34:29 +00001316 __get_db()->swap(this, &__x);
1317#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001318 }
1319 else
1320 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001321 typedef move_iterator<iterator> _Ip;
1322 assign(_Ip(__x.begin()), _Ip(__x.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001323 }
1324}
1325
1326template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001327inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001328vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il)
1329{
Louis Dionneba400782020-10-02 15:02:52 -04001330#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001331 __get_db()->__insert_c(this);
1332#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001333 if (__il.size() > 0)
1334 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001335 __vallocate(__il.size());
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001336 __construct_at_end(__il.begin(), __il.end(), __il.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001337 }
1338}
1339
1340template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001341inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001342vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
1343 : __base(__a)
1344{
Louis Dionneba400782020-10-02 15:02:52 -04001345#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant9cd22302011-09-16 18:41:29 +00001346 __get_db()->__insert_c(this);
1347#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001348 if (__il.size() > 0)
1349 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001350 __vallocate(__il.size());
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001351 __construct_at_end(__il.begin(), __il.end(), __il.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001352 }
1353}
1354
1355template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001356inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001357vector<_Tp, _Allocator>&
1358vector<_Tp, _Allocator>::operator=(vector&& __x)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001359 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001360{
1361 __move_assign(__x, integral_constant<bool,
1362 __alloc_traits::propagate_on_container_move_assignment::value>());
1363 return *this;
1364}
1365
1366template <class _Tp, class _Allocator>
1367void
1368vector<_Tp, _Allocator>::__move_assign(vector& __c, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001369 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001370{
1371 if (__base::__alloc() != __c.__alloc())
1372 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001373 typedef move_iterator<iterator> _Ip;
1374 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001375 }
1376 else
1377 __move_assign(__c, true_type());
1378}
1379
1380template <class _Tp, class _Allocator>
1381void
1382vector<_Tp, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00001383 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001384{
Marshall Clow3ff48e02018-05-22 16:20:28 +00001385 __vdeallocate();
Marshall Clow136d45c2014-07-21 15:11:13 +00001386 __base::__move_assign_alloc(__c); // this can throw
Howard Hinnantc51e1022010-05-11 19:42:16 +00001387 this->__begin_ = __c.__begin_;
1388 this->__end_ = __c.__end_;
1389 this->__end_cap() = __c.__end_cap();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001390 __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
Louis Dionneba400782020-10-02 15:02:52 -04001391#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnanta1e60cd2011-09-19 16:34:29 +00001392 __get_db()->swap(this, &__c);
1393#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001394}
1395
Eric Fiseliered9e9362017-04-16 02:40:45 +00001396#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001397
1398template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001399inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001400vector<_Tp, _Allocator>&
1401vector<_Tp, _Allocator>::operator=(const vector& __x)
1402{
1403 if (this != &__x)
1404 {
1405 __base::__copy_assign_alloc(__x);
1406 assign(__x.__begin_, __x.__end_);
1407 }
1408 return *this;
1409}
1410
1411template <class _Tp, class _Allocator>
1412template <class _InputIterator>
1413typename enable_if
1414<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001415 __is_cpp17_input_iterator <_InputIterator>::value &&
1416 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001417 is_constructible<
1418 _Tp,
1419 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001420 void
1421>::type
1422vector<_Tp, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
1423{
1424 clear();
1425 for (; __first != __last; ++__first)
Eric Fiselier96919722017-10-17 13:03:17 +00001426 __emplace_back(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001427}
1428
1429template <class _Tp, class _Allocator>
1430template <class _ForwardIterator>
1431typename enable_if
1432<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001433 __is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001434 is_constructible<
1435 _Tp,
1436 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001437 void
1438>::type
1439vector<_Tp, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
1440{
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001441 size_type __new_size = static_cast<size_type>(_VSTD::distance(__first, __last));
1442 if (__new_size <= capacity())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001443 {
1444 _ForwardIterator __mid = __last;
1445 bool __growing = false;
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001446 if (__new_size > size())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001447 {
1448 __growing = true;
1449 __mid = __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001450 _VSTD::advance(__mid, size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001451 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001452 pointer __m = _VSTD::copy(__first, __mid, this->__begin_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001453 if (__growing)
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001454 __construct_at_end(__mid, __last, __new_size - size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001455 else
1456 this->__destruct_at_end(__m);
1457 }
1458 else
1459 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001460 __vdeallocate();
1461 __vallocate(__recommend(__new_size));
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001462 __construct_at_end(__first, __last, __new_size);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001463 }
Eric Fiselier69c51982016-12-28 06:06:09 +00001464 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001465}
1466
1467template <class _Tp, class _Allocator>
1468void
1469vector<_Tp, _Allocator>::assign(size_type __n, const_reference __u)
1470{
1471 if (__n <= capacity())
1472 {
1473 size_type __s = size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001474 _VSTD::fill_n(this->__begin_, _VSTD::min(__n, __s), __u);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001475 if (__n > __s)
1476 __construct_at_end(__n - __s, __u);
1477 else
Howard Hinnant155c2af2010-05-24 17:49:41 +00001478 this->__destruct_at_end(this->__begin_ + __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001479 }
1480 else
1481 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001482 __vdeallocate();
1483 __vallocate(__recommend(static_cast<size_type>(__n)));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001484 __construct_at_end(__n, __u);
1485 }
Eric Fiselier69c51982016-12-28 06:06:09 +00001486 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001487}
1488
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001489template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001490inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001491typename vector<_Tp, _Allocator>::iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001492vector<_Tp, _Allocator>::__make_iter(pointer __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001493{
Louis Dionneba400782020-10-02 15:02:52 -04001494#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001495 return iterator(this, __p);
1496#else
1497 return iterator(__p);
1498#endif
1499}
1500
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001501template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001502inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001503typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001504vector<_Tp, _Allocator>::__make_iter(const_pointer __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001505{
Louis Dionneba400782020-10-02 15:02:52 -04001506#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001507 return const_iterator(this, __p);
1508#else
1509 return const_iterator(__p);
1510#endif
1511}
1512
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001513template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001514inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001515typename vector<_Tp, _Allocator>::iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001516vector<_Tp, _Allocator>::begin() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001517{
1518 return __make_iter(this->__begin_);
1519}
1520
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001521template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001522inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001523typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001524vector<_Tp, _Allocator>::begin() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001525{
1526 return __make_iter(this->__begin_);
1527}
1528
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001529template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001530inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001531typename vector<_Tp, _Allocator>::iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001532vector<_Tp, _Allocator>::end() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001533{
1534 return __make_iter(this->__end_);
1535}
1536
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001537template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001538inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001539typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001540vector<_Tp, _Allocator>::end() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001541{
1542 return __make_iter(this->__end_);
1543}
1544
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001545template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001546inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001547typename vector<_Tp, _Allocator>::reference
Marshall Clowd6470492019-03-15 00:29:35 +00001548vector<_Tp, _Allocator>::operator[](size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001549{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001550 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001551 return this->__begin_[__n];
1552}
1553
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001554template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001555inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001556typename vector<_Tp, _Allocator>::const_reference
Marshall Clowd6470492019-03-15 00:29:35 +00001557vector<_Tp, _Allocator>::operator[](size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001558{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001559 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001560 return this->__begin_[__n];
1561}
1562
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001563template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001564typename vector<_Tp, _Allocator>::reference
1565vector<_Tp, _Allocator>::at(size_type __n)
1566{
1567 if (__n >= size())
1568 this->__throw_out_of_range();
1569 return this->__begin_[__n];
1570}
1571
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001572template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001573typename vector<_Tp, _Allocator>::const_reference
1574vector<_Tp, _Allocator>::at(size_type __n) const
1575{
1576 if (__n >= size())
1577 this->__throw_out_of_range();
1578 return this->__begin_[__n];
1579}
1580
1581template <class _Tp, class _Allocator>
1582void
1583vector<_Tp, _Allocator>::reserve(size_type __n)
1584{
1585 if (__n > capacity())
1586 {
1587 allocator_type& __a = this->__alloc();
Howard Hinnant1c936782011-06-03 19:40:40 +00001588 __split_buffer<value_type, allocator_type&> __v(__n, size(), __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001589 __swap_out_circular_buffer(__v);
1590 }
1591}
1592
1593template <class _Tp, class _Allocator>
1594void
Howard Hinnant1c936782011-06-03 19:40:40 +00001595vector<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001596{
1597 if (capacity() > size())
1598 {
1599#ifndef _LIBCPP_NO_EXCEPTIONS
1600 try
1601 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001602#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001603 allocator_type& __a = this->__alloc();
Howard Hinnant1c936782011-06-03 19:40:40 +00001604 __split_buffer<value_type, allocator_type&> __v(size(), size(), __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001605 __swap_out_circular_buffer(__v);
1606#ifndef _LIBCPP_NO_EXCEPTIONS
1607 }
1608 catch (...)
1609 {
1610 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001611#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001612 }
1613}
1614
1615template <class _Tp, class _Allocator>
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001616template <class _Up>
1617void
Eric Fiseliered9e9362017-04-16 02:40:45 +00001618#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001619vector<_Tp, _Allocator>::__push_back_slow_path(_Up&& __x)
1620#else
1621vector<_Tp, _Allocator>::__push_back_slow_path(_Up& __x)
1622#endif
1623{
1624 allocator_type& __a = this->__alloc();
1625 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1626 // __v.push_back(_VSTD::forward<_Up>(__x));
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001627 __alloc_traits::construct(__a, _VSTD::__to_address(__v.__end_), _VSTD::forward<_Up>(__x));
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001628 __v.__end_++;
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001629 __swap_out_circular_buffer(__v);
1630}
1631
1632template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001633inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001634void
1635vector<_Tp, _Allocator>::push_back(const_reference __x)
1636{
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001637 if (this->__end_ != this->__end_cap())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001638 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001639 __construct_one_at_end(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001640 }
1641 else
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001642 __push_back_slow_path(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001643}
1644
Eric Fiseliered9e9362017-04-16 02:40:45 +00001645#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001646
1647template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001648inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001649void
1650vector<_Tp, _Allocator>::push_back(value_type&& __x)
1651{
1652 if (this->__end_ < this->__end_cap())
1653 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001654 __construct_one_at_end(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001655 }
1656 else
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001657 __push_back_slow_path(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001658}
1659
1660template <class _Tp, class _Allocator>
1661template <class... _Args>
1662void
Howard Hinnantb6c49562012-02-26 15:30:12 +00001663vector<_Tp, _Allocator>::__emplace_back_slow_path(_Args&&... __args)
1664{
1665 allocator_type& __a = this->__alloc();
1666 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1667// __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001668 __alloc_traits::construct(__a, _VSTD::__to_address(__v.__end_), _VSTD::forward<_Args>(__args)...);
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001669 __v.__end_++;
Howard Hinnantb6c49562012-02-26 15:30:12 +00001670 __swap_out_circular_buffer(__v);
1671}
1672
1673template <class _Tp, class _Allocator>
1674template <class... _Args>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001675inline
Marshall Clowea52cc42017-01-24 23:09:12 +00001676#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001677typename vector<_Tp, _Allocator>::reference
Marshall Clowea52cc42017-01-24 23:09:12 +00001678#else
1679void
1680#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001681vector<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1682{
1683 if (this->__end_ < this->__end_cap())
1684 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001685 __construct_one_at_end(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001686 }
1687 else
Howard Hinnantb6c49562012-02-26 15:30:12 +00001688 __emplace_back_slow_path(_VSTD::forward<_Args>(__args)...);
Marshall Clowea52cc42017-01-24 23:09:12 +00001689#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001690 return this->back();
Marshall Clowea52cc42017-01-24 23:09:12 +00001691#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001692}
1693
Eric Fiseliered9e9362017-04-16 02:40:45 +00001694#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001695
1696template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001697inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001698void
1699vector<_Tp, _Allocator>::pop_back()
1700{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001701 _LIBCPP_ASSERT(!empty(), "vector::pop_back called for empty vector");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001702 this->__destruct_at_end(this->__end_ - 1);
1703}
1704
1705template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001706inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001707typename vector<_Tp, _Allocator>::iterator
1708vector<_Tp, _Allocator>::erase(const_iterator __position)
1709{
Louis Dionneba400782020-10-02 15:02:52 -04001710#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001711 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1712 "vector::erase(iterator) called with an iterator not"
1713 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001714#endif
Howard Hinnant58d52d52013-03-25 22:12:26 +00001715 _LIBCPP_ASSERT(__position != end(),
1716 "vector::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnant76053d72013-06-27 19:35:32 +00001717 difference_type __ps = __position - cbegin();
1718 pointer __p = this->__begin_ + __ps;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001719 this->__destruct_at_end(_VSTD::move(__p + 1, this->__end_, __p));
Eric Fiselier69c51982016-12-28 06:06:09 +00001720 this->__invalidate_iterators_past(__p-1);
1721 iterator __r = __make_iter(__p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001722 return __r;
1723}
1724
1725template <class _Tp, class _Allocator>
1726typename vector<_Tp, _Allocator>::iterator
1727vector<_Tp, _Allocator>::erase(const_iterator __first, const_iterator __last)
1728{
Louis Dionneba400782020-10-02 15:02:52 -04001729#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001730 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
1731 "vector::erase(iterator, iterator) called with an iterator not"
1732 " referring to this vector");
Eric Fiselier69c51982016-12-28 06:06:09 +00001733 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__last) == this,
1734 "vector::erase(iterator, iterator) called with an iterator not"
1735 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001736#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001737 _LIBCPP_ASSERT(__first <= __last, "vector::erase(first, last) called with invalid range");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001738 pointer __p = this->__begin_ + (__first - begin());
Eric Fiselier69c51982016-12-28 06:06:09 +00001739 if (__first != __last) {
Howard Hinnantc580cc32013-04-18 15:02:57 +00001740 this->__destruct_at_end(_VSTD::move(__p + (__last - __first), this->__end_, __p));
Eric Fiselier69c51982016-12-28 06:06:09 +00001741 this->__invalidate_iterators_past(__p - 1);
1742 }
1743 iterator __r = __make_iter(__p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001744 return __r;
1745}
1746
1747template <class _Tp, class _Allocator>
1748void
1749vector<_Tp, _Allocator>::__move_range(pointer __from_s, pointer __from_e, pointer __to)
1750{
1751 pointer __old_last = this->__end_;
1752 difference_type __n = __old_last - __to;
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001753 {
1754 pointer __i = __from_s + __n;
1755 _ConstructTransaction __tx(*this, __from_e - __i);
Martijn Vels1e2e1b82020-06-18 13:14:02 -04001756 for (pointer __pos = __tx.__pos_; __i < __from_e;
1757 ++__i, ++__pos, __tx.__pos_ = __pos) {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001758 __alloc_traits::construct(this->__alloc(),
Martijn Vels1e2e1b82020-06-18 13:14:02 -04001759 _VSTD::__to_address(__pos),
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001760 _VSTD::move(*__i));
1761 }
1762 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001763 _VSTD::move_backward(__from_s, __from_s + __n, __old_last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001764}
1765
1766template <class _Tp, class _Allocator>
1767typename vector<_Tp, _Allocator>::iterator
1768vector<_Tp, _Allocator>::insert(const_iterator __position, const_reference __x)
1769{
Louis Dionneba400782020-10-02 15:02:52 -04001770#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001771 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1772 "vector::insert(iterator, x) called with an iterator not"
1773 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001774#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001775 pointer __p = this->__begin_ + (__position - begin());
1776 if (this->__end_ < this->__end_cap())
1777 {
1778 if (__p == this->__end_)
1779 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001780 __construct_one_at_end(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001781 }
1782 else
1783 {
1784 __move_range(__p, this->__end_, __p + 1);
1785 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1786 if (__p <= __xr && __xr < this->__end_)
1787 ++__xr;
1788 *__p = *__xr;
1789 }
1790 }
1791 else
1792 {
1793 allocator_type& __a = this->__alloc();
1794 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
1795 __v.push_back(__x);
1796 __p = __swap_out_circular_buffer(__v, __p);
1797 }
1798 return __make_iter(__p);
1799}
1800
Eric Fiseliered9e9362017-04-16 02:40:45 +00001801#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001802
1803template <class _Tp, class _Allocator>
1804typename vector<_Tp, _Allocator>::iterator
1805vector<_Tp, _Allocator>::insert(const_iterator __position, value_type&& __x)
1806{
Louis Dionneba400782020-10-02 15:02:52 -04001807#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001808 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1809 "vector::insert(iterator, x) called with an iterator not"
1810 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001811#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001812 pointer __p = this->__begin_ + (__position - begin());
1813 if (this->__end_ < this->__end_cap())
1814 {
1815 if (__p == this->__end_)
1816 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001817 __construct_one_at_end(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001818 }
1819 else
1820 {
1821 __move_range(__p, this->__end_, __p + 1);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001822 *__p = _VSTD::move(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001823 }
1824 }
1825 else
1826 {
1827 allocator_type& __a = this->__alloc();
1828 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001829 __v.push_back(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001830 __p = __swap_out_circular_buffer(__v, __p);
1831 }
1832 return __make_iter(__p);
1833}
1834
1835template <class _Tp, class _Allocator>
1836template <class... _Args>
1837typename vector<_Tp, _Allocator>::iterator
1838vector<_Tp, _Allocator>::emplace(const_iterator __position, _Args&&... __args)
1839{
Louis Dionneba400782020-10-02 15:02:52 -04001840#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001841 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1842 "vector::emplace(iterator, x) called with an iterator not"
1843 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001844#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001845 pointer __p = this->__begin_ + (__position - begin());
1846 if (this->__end_ < this->__end_cap())
1847 {
1848 if (__p == this->__end_)
1849 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001850 __construct_one_at_end(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001851 }
1852 else
1853 {
Marshall Clowa591b9a2016-07-11 21:38:08 +00001854 __temp_value<value_type, _Allocator> __tmp(this->__alloc(), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001855 __move_range(__p, this->__end_, __p + 1);
Marshall Clowa591b9a2016-07-11 21:38:08 +00001856 *__p = _VSTD::move(__tmp.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001857 }
1858 }
1859 else
1860 {
1861 allocator_type& __a = this->__alloc();
1862 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001863 __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001864 __p = __swap_out_circular_buffer(__v, __p);
1865 }
1866 return __make_iter(__p);
1867}
1868
Eric Fiseliered9e9362017-04-16 02:40:45 +00001869#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001870
1871template <class _Tp, class _Allocator>
1872typename vector<_Tp, _Allocator>::iterator
1873vector<_Tp, _Allocator>::insert(const_iterator __position, size_type __n, const_reference __x)
1874{
Louis Dionneba400782020-10-02 15:02:52 -04001875#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001876 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1877 "vector::insert(iterator, n, x) called with an iterator not"
1878 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001879#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001880 pointer __p = this->__begin_ + (__position - begin());
1881 if (__n > 0)
1882 {
1883 if (__n <= static_cast<size_type>(this->__end_cap() - this->__end_))
1884 {
1885 size_type __old_n = __n;
1886 pointer __old_last = this->__end_;
1887 if (__n > static_cast<size_type>(this->__end_ - __p))
1888 {
1889 size_type __cx = __n - (this->__end_ - __p);
1890 __construct_at_end(__cx, __x);
1891 __n -= __cx;
1892 }
1893 if (__n > 0)
1894 {
1895 __move_range(__p, __old_last, __p + __old_n);
1896 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1897 if (__p <= __xr && __xr < this->__end_)
1898 __xr += __old_n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001899 _VSTD::fill_n(__p, __n, *__xr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001900 }
1901 }
1902 else
1903 {
1904 allocator_type& __a = this->__alloc();
1905 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1906 __v.__construct_at_end(__n, __x);
1907 __p = __swap_out_circular_buffer(__v, __p);
1908 }
1909 }
1910 return __make_iter(__p);
1911}
1912
1913template <class _Tp, class _Allocator>
1914template <class _InputIterator>
1915typename enable_if
1916<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001917 __is_cpp17_input_iterator <_InputIterator>::value &&
1918 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001919 is_constructible<
1920 _Tp,
1921 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001922 typename vector<_Tp, _Allocator>::iterator
1923>::type
1924vector<_Tp, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
1925{
Louis Dionneba400782020-10-02 15:02:52 -04001926#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001927 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1928 "vector::insert(iterator, range) called with an iterator not"
1929 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001930#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001931 difference_type __off = __position - begin();
1932 pointer __p = this->__begin_ + __off;
1933 allocator_type& __a = this->__alloc();
1934 pointer __old_last = this->__end_;
1935 for (; this->__end_ != this->__end_cap() && __first != __last; ++__first)
1936 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001937 __construct_one_at_end(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001938 }
1939 __split_buffer<value_type, allocator_type&> __v(__a);
1940 if (__first != __last)
1941 {
1942#ifndef _LIBCPP_NO_EXCEPTIONS
1943 try
1944 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001945#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001946 __v.__construct_at_end(__first, __last);
1947 difference_type __old_size = __old_last - this->__begin_;
1948 difference_type __old_p = __p - this->__begin_;
1949 reserve(__recommend(size() + __v.size()));
1950 __p = this->__begin_ + __old_p;
1951 __old_last = this->__begin_ + __old_size;
1952#ifndef _LIBCPP_NO_EXCEPTIONS
1953 }
1954 catch (...)
1955 {
1956 erase(__make_iter(__old_last), end());
1957 throw;
1958 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001959#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001960 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001961 __p = _VSTD::rotate(__p, __old_last, this->__end_);
Louis Dionne202a4cb2020-02-11 10:59:12 +01001962 insert(__make_iter(__p), _VSTD::make_move_iterator(__v.begin()),
1963 _VSTD::make_move_iterator(__v.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001964 return begin() + __off;
1965}
1966
1967template <class _Tp, class _Allocator>
1968template <class _ForwardIterator>
1969typename enable_if
1970<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001971 __is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001972 is_constructible<
1973 _Tp,
1974 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001975 typename vector<_Tp, _Allocator>::iterator
1976>::type
1977vector<_Tp, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
1978{
Louis Dionneba400782020-10-02 15:02:52 -04001979#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001980 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1981 "vector::insert(iterator, range) called with an iterator not"
1982 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001983#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001984 pointer __p = this->__begin_ + (__position - begin());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001985 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001986 if (__n > 0)
1987 {
1988 if (__n <= this->__end_cap() - this->__end_)
1989 {
1990 size_type __old_n = __n;
1991 pointer __old_last = this->__end_;
1992 _ForwardIterator __m = __last;
1993 difference_type __dx = this->__end_ - __p;
1994 if (__n > __dx)
1995 {
1996 __m = __first;
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001997 difference_type __diff = this->__end_ - __p;
1998 _VSTD::advance(__m, __diff);
1999 __construct_at_end(__m, __last, __n - __diff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002000 __n = __dx;
2001 }
2002 if (__n > 0)
2003 {
2004 __move_range(__p, __old_last, __p + __old_n);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002005 _VSTD::copy(__first, __m, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002006 }
2007 }
2008 else
2009 {
2010 allocator_type& __a = this->__alloc();
2011 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
2012 __v.__construct_at_end(__first, __last);
2013 __p = __swap_out_circular_buffer(__v, __p);
2014 }
2015 }
2016 return __make_iter(__p);
2017}
2018
2019template <class _Tp, class _Allocator>
2020void
2021vector<_Tp, _Allocator>::resize(size_type __sz)
2022{
2023 size_type __cs = size();
2024 if (__cs < __sz)
2025 this->__append(__sz - __cs);
2026 else if (__cs > __sz)
2027 this->__destruct_at_end(this->__begin_ + __sz);
2028}
2029
2030template <class _Tp, class _Allocator>
2031void
2032vector<_Tp, _Allocator>::resize(size_type __sz, const_reference __x)
2033{
2034 size_type __cs = size();
2035 if (__cs < __sz)
2036 this->__append(__sz - __cs, __x);
2037 else if (__cs > __sz)
2038 this->__destruct_at_end(this->__begin_ + __sz);
2039}
2040
2041template <class _Tp, class _Allocator>
2042void
2043vector<_Tp, _Allocator>::swap(vector& __x)
Marshall Clow8982dcd2015-07-13 20:04:56 +00002044#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00002045 _NOEXCEPT
Marshall Clow8982dcd2015-07-13 20:04:56 +00002046#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00002047 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00002048 __is_nothrow_swappable<allocator_type>::value)
2049#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002050{
Howard Hinnant27e0e772011-09-14 18:33:51 +00002051 _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
2052 this->__alloc() == __x.__alloc(),
2053 "vector::swap: Either propagate_on_container_swap must be true"
2054 " or the allocators must compare equal");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002055 _VSTD::swap(this->__begin_, __x.__begin_);
2056 _VSTD::swap(this->__end_, __x.__end_);
2057 _VSTD::swap(this->__end_cap(), __x.__end_cap());
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05002058 _VSTD::__swap_allocator(this->__alloc(), __x.__alloc(),
Marshall Clow8982dcd2015-07-13 20:04:56 +00002059 integral_constant<bool,__alloc_traits::propagate_on_container_swap::value>());
Louis Dionneba400782020-10-02 15:02:52 -04002060#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002061 __get_db()->swap(this, &__x);
Louis Dionneba400782020-10-02 15:02:52 -04002062#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002063}
2064
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002065template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002066bool
2067vector<_Tp, _Allocator>::__invariants() const
2068{
Howard Hinnant76053d72013-06-27 19:35:32 +00002069 if (this->__begin_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002070 {
Howard Hinnant76053d72013-06-27 19:35:32 +00002071 if (this->__end_ != nullptr || this->__end_cap() != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002072 return false;
2073 }
2074 else
2075 {
2076 if (this->__begin_ > this->__end_)
2077 return false;
2078 if (this->__begin_ == this->__end_cap())
2079 return false;
2080 if (this->__end_ > this->__end_cap())
2081 return false;
2082 }
2083 return true;
2084}
2085
Louis Dionneba400782020-10-02 15:02:52 -04002086#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002087
Howard Hinnantc51e1022010-05-11 19:42:16 +00002088template <class _Tp, class _Allocator>
Howard Hinnant27e0e772011-09-14 18:33:51 +00002089bool
2090vector<_Tp, _Allocator>::__dereferenceable(const const_iterator* __i) const
2091{
2092 return this->__begin_ <= __i->base() && __i->base() < this->__end_;
2093}
2094
2095template <class _Tp, class _Allocator>
2096bool
2097vector<_Tp, _Allocator>::__decrementable(const const_iterator* __i) const
2098{
2099 return this->__begin_ < __i->base() && __i->base() <= this->__end_;
2100}
2101
2102template <class _Tp, class _Allocator>
2103bool
2104vector<_Tp, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
2105{
2106 const_pointer __p = __i->base() + __n;
2107 return this->__begin_ <= __p && __p <= this->__end_;
2108}
2109
2110template <class _Tp, class _Allocator>
2111bool
2112vector<_Tp, _Allocator>::__subscriptable(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
Louis Dionneba400782020-10-02 15:02:52 -04002118#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002119
2120template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002121inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002122void
2123vector<_Tp, _Allocator>::__invalidate_all_iterators()
2124{
Louis Dionneba400782020-10-02 15:02:52 -04002125#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002126 __get_db()->__invalidate_all(this);
Louis Dionneba400782020-10-02 15:02:52 -04002127#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002128}
2129
Eric Fiselier69c51982016-12-28 06:06:09 +00002130
2131template <class _Tp, class _Allocator>
2132inline _LIBCPP_INLINE_VISIBILITY
2133void
2134vector<_Tp, _Allocator>::__invalidate_iterators_past(pointer __new_last) {
Louis Dionneba400782020-10-02 15:02:52 -04002135#if _LIBCPP_DEBUG_LEVEL == 2
Eric Fiselier69c51982016-12-28 06:06:09 +00002136 __c_node* __c = __get_db()->__find_c_and_lock(this);
2137 for (__i_node** __p = __c->end_; __p != __c->beg_; ) {
2138 --__p;
2139 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
2140 if (__i->base() > __new_last) {
2141 (*__p)->__c_ = nullptr;
2142 if (--__c->end_ != __p)
Arthur O'Dwyer22236632020-12-07 21:50:15 -05002143 _VSTD::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
Eric Fiselier69c51982016-12-28 06:06:09 +00002144 }
2145 }
2146 __get_db()->unlock();
2147#else
2148 ((void)__new_last);
2149#endif
2150}
2151
Howard Hinnantc51e1022010-05-11 19:42:16 +00002152// vector<bool>
2153
2154template <class _Allocator> class vector<bool, _Allocator>;
2155
2156template <class _Allocator> struct hash<vector<bool, _Allocator> >;
2157
2158template <class _Allocator>
Howard Hinnantb4ef6482011-07-02 20:33:23 +00002159struct __has_storage_type<vector<bool, _Allocator> >
2160{
2161 static const bool value = true;
2162};
2163
2164template <class _Allocator>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002165class _LIBCPP_TEMPLATE_VIS vector<bool, _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002166 : private __vector_base_common<true>
2167{
2168public:
2169 typedef vector __self;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002170 typedef bool value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002171 typedef _Allocator allocator_type;
2172 typedef allocator_traits<allocator_type> __alloc_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002173 typedef typename __alloc_traits::size_type size_type;
2174 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant896b7622012-05-07 16:50:38 +00002175 typedef size_type __storage_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002176 typedef __bit_iterator<vector, false> pointer;
2177 typedef __bit_iterator<vector, true> const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002178 typedef pointer iterator;
2179 typedef const_pointer const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002180 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
2181 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002182
2183private:
Marshall Clow940e01c2015-04-07 05:21:38 +00002184 typedef typename __rebind_alloc_helper<__alloc_traits, __storage_type>::type __storage_allocator;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002185 typedef allocator_traits<__storage_allocator> __storage_traits;
2186 typedef typename __storage_traits::pointer __storage_pointer;
2187 typedef typename __storage_traits::const_pointer __const_storage_pointer;
2188
2189 __storage_pointer __begin_;
2190 size_type __size_;
2191 __compressed_pair<size_type, __storage_allocator> __cap_alloc_;
Howard Hinnant71d1c192011-07-09 15:50:42 +00002192public:
Howard Hinnantb4ef6482011-07-02 20:33:23 +00002193 typedef __bit_reference<vector> reference;
2194 typedef __bit_const_reference<vector> const_reference;
Howard Hinnant71d1c192011-07-09 15:50:42 +00002195private:
Howard Hinnant1c936782011-06-03 19:40:40 +00002196 _LIBCPP_INLINE_VISIBILITY
2197 size_type& __cap() _NOEXCEPT
2198 {return __cap_alloc_.first();}
2199 _LIBCPP_INLINE_VISIBILITY
2200 const size_type& __cap() const _NOEXCEPT
2201 {return __cap_alloc_.first();}
2202 _LIBCPP_INLINE_VISIBILITY
2203 __storage_allocator& __alloc() _NOEXCEPT
2204 {return __cap_alloc_.second();}
2205 _LIBCPP_INLINE_VISIBILITY
2206 const __storage_allocator& __alloc() const _NOEXCEPT
2207 {return __cap_alloc_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002208
2209 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
2210
Howard Hinnant1c936782011-06-03 19:40:40 +00002211 _LIBCPP_INLINE_VISIBILITY
2212 static size_type __internal_cap_to_external(size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002213 {return __n * __bits_per_word;}
Howard Hinnant1c936782011-06-03 19:40:40 +00002214 _LIBCPP_INLINE_VISIBILITY
2215 static size_type __external_cap_to_internal(size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002216 {return (__n - 1) / __bits_per_word + 1;}
2217
2218public:
Howard Hinnant1c936782011-06-03 19:40:40 +00002219 _LIBCPP_INLINE_VISIBILITY
Marshall Clowe546cbb2015-06-04 02:05:41 +00002220 vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clow8f282f42015-06-04 00:10:20 +00002221
2222 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
2223#if _LIBCPP_STD_VER <= 14
2224 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
2225#else
2226 _NOEXCEPT;
2227#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002228 ~vector();
2229 explicit vector(size_type __n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00002230#if _LIBCPP_STD_VER > 11
2231 explicit vector(size_type __n, const allocator_type& __a);
2232#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002233 vector(size_type __n, const value_type& __v);
2234 vector(size_type __n, const value_type& __v, const allocator_type& __a);
2235 template <class _InputIterator>
2236 vector(_InputIterator __first, _InputIterator __last,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002237 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
2238 !__is_cpp17_forward_iterator<_InputIterator>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002239 template <class _InputIterator>
2240 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002241 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
2242 !__is_cpp17_forward_iterator<_InputIterator>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002243 template <class _ForwardIterator>
2244 vector(_ForwardIterator __first, _ForwardIterator __last,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002245 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002246 template <class _ForwardIterator>
2247 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002248 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002249
2250 vector(const vector& __v);
2251 vector(const vector& __v, const allocator_type& __a);
2252 vector& operator=(const vector& __v);
Eric Fiseliered9e9362017-04-16 02:40:45 +00002253
2254#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002255 vector(initializer_list<value_type> __il);
2256 vector(initializer_list<value_type> __il, const allocator_type& __a);
2257
Howard Hinnant1c936782011-06-03 19:40:40 +00002258 _LIBCPP_INLINE_VISIBILITY
2259 vector(vector&& __v)
Marshall Clowe5108202015-07-14 14:46:32 +00002260#if _LIBCPP_STD_VER > 14
2261 _NOEXCEPT;
2262#else
Howard Hinnant1c936782011-06-03 19:40:40 +00002263 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowe5108202015-07-14 14:46:32 +00002264#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002265 vector(vector&& __v, const allocator_type& __a);
Howard Hinnant1c936782011-06-03 19:40:40 +00002266 _LIBCPP_INLINE_VISIBILITY
2267 vector& operator=(vector&& __v)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002268 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiseliered9e9362017-04-16 02:40:45 +00002269
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002270 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002271 vector& operator=(initializer_list<value_type> __il)
2272 {assign(__il.begin(), __il.end()); return *this;}
Eric Fiseliered9e9362017-04-16 02:40:45 +00002273
2274#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002275
2276 template <class _InputIterator>
2277 typename enable_if
2278 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002279 __is_cpp17_input_iterator<_InputIterator>::value &&
2280 !__is_cpp17_forward_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002281 void
2282 >::type
2283 assign(_InputIterator __first, _InputIterator __last);
2284 template <class _ForwardIterator>
2285 typename enable_if
2286 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002287 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002288 void
2289 >::type
2290 assign(_ForwardIterator __first, _ForwardIterator __last);
2291
2292 void assign(size_type __n, const value_type& __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +00002293
2294#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002295 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002296 void assign(initializer_list<value_type> __il)
2297 {assign(__il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +00002298#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002299
Howard Hinnant1c936782011-06-03 19:40:40 +00002300 _LIBCPP_INLINE_VISIBILITY allocator_type get_allocator() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002301 {return allocator_type(this->__alloc());}
2302
Howard Hinnant1c936782011-06-03 19:40:40 +00002303 size_type max_size() const _NOEXCEPT;
2304 _LIBCPP_INLINE_VISIBILITY
2305 size_type capacity() const _NOEXCEPT
2306 {return __internal_cap_to_external(__cap());}
2307 _LIBCPP_INLINE_VISIBILITY
2308 size_type size() const _NOEXCEPT
2309 {return __size_;}
Marshall Clow425f5752017-11-15 05:51:26 +00002310 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +00002311 bool empty() const _NOEXCEPT
2312 {return __size_ == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002313 void reserve(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +00002314 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002315
Howard Hinnant1c936782011-06-03 19:40:40 +00002316 _LIBCPP_INLINE_VISIBILITY
2317 iterator begin() _NOEXCEPT
2318 {return __make_iter(0);}
2319 _LIBCPP_INLINE_VISIBILITY
2320 const_iterator begin() const _NOEXCEPT
2321 {return __make_iter(0);}
2322 _LIBCPP_INLINE_VISIBILITY
2323 iterator end() _NOEXCEPT
2324 {return __make_iter(__size_);}
2325 _LIBCPP_INLINE_VISIBILITY
2326 const_iterator end() const _NOEXCEPT
2327 {return __make_iter(__size_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002328
Howard Hinnant1c936782011-06-03 19:40:40 +00002329 _LIBCPP_INLINE_VISIBILITY
2330 reverse_iterator rbegin() _NOEXCEPT
2331 {return reverse_iterator(end());}
2332 _LIBCPP_INLINE_VISIBILITY
2333 const_reverse_iterator rbegin() const _NOEXCEPT
2334 {return const_reverse_iterator(end());}
2335 _LIBCPP_INLINE_VISIBILITY
2336 reverse_iterator rend() _NOEXCEPT
2337 {return reverse_iterator(begin());}
2338 _LIBCPP_INLINE_VISIBILITY
2339 const_reverse_iterator rend() const _NOEXCEPT
2340 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002341
Howard Hinnant1c936782011-06-03 19:40:40 +00002342 _LIBCPP_INLINE_VISIBILITY
2343 const_iterator cbegin() const _NOEXCEPT
2344 {return __make_iter(0);}
2345 _LIBCPP_INLINE_VISIBILITY
2346 const_iterator cend() const _NOEXCEPT
2347 {return __make_iter(__size_);}
2348 _LIBCPP_INLINE_VISIBILITY
2349 const_reverse_iterator crbegin() const _NOEXCEPT
2350 {return rbegin();}
2351 _LIBCPP_INLINE_VISIBILITY
2352 const_reverse_iterator crend() const _NOEXCEPT
2353 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002354
2355 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) {return __make_ref(__n);}
2356 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const {return __make_ref(__n);}
2357 reference at(size_type __n);
2358 const_reference at(size_type __n) const;
2359
2360 _LIBCPP_INLINE_VISIBILITY reference front() {return __make_ref(0);}
2361 _LIBCPP_INLINE_VISIBILITY const_reference front() const {return __make_ref(0);}
2362 _LIBCPP_INLINE_VISIBILITY reference back() {return __make_ref(__size_ - 1);}
2363 _LIBCPP_INLINE_VISIBILITY const_reference back() const {return __make_ref(__size_ - 1);}
2364
2365 void push_back(const value_type& __x);
Marshall Clowc46bb8e2013-08-13 23:54:12 +00002366#if _LIBCPP_STD_VER > 11
2367 template <class... _Args>
Marshall Clowea52cc42017-01-24 23:09:12 +00002368#if _LIBCPP_STD_VER > 14
2369 _LIBCPP_INLINE_VISIBILITY reference emplace_back(_Args&&... __args)
2370#else
2371 _LIBCPP_INLINE_VISIBILITY void emplace_back(_Args&&... __args)
2372#endif
2373 {
Eric Fiselier34ba5b92016-07-21 03:20:17 +00002374 push_back ( value_type ( _VSTD::forward<_Args>(__args)... ));
Marshall Clowea52cc42017-01-24 23:09:12 +00002375#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00002376 return this->back();
Marshall Clowea52cc42017-01-24 23:09:12 +00002377#endif
Eric Fiselier34ba5b92016-07-21 03:20:17 +00002378 }
Marshall Clowc46bb8e2013-08-13 23:54:12 +00002379#endif
2380
Howard Hinnantc51e1022010-05-11 19:42:16 +00002381 _LIBCPP_INLINE_VISIBILITY void pop_back() {--__size_;}
2382
Marshall Clowc46bb8e2013-08-13 23:54:12 +00002383#if _LIBCPP_STD_VER > 11
2384 template <class... _Args>
2385 _LIBCPP_INLINE_VISIBILITY iterator emplace(const_iterator position, _Args&&... __args)
2386 { return insert ( position, value_type ( _VSTD::forward<_Args>(__args)... )); }
2387#endif
2388
Howard Hinnantc51e1022010-05-11 19:42:16 +00002389 iterator insert(const_iterator __position, const value_type& __x);
2390 iterator insert(const_iterator __position, size_type __n, const value_type& __x);
2391 iterator insert(const_iterator __position, size_type __n, const_reference __x);
2392 template <class _InputIterator>
2393 typename enable_if
2394 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002395 __is_cpp17_input_iterator <_InputIterator>::value &&
2396 !__is_cpp17_forward_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002397 iterator
2398 >::type
2399 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
2400 template <class _ForwardIterator>
2401 typename enable_if
2402 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002403 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002404 iterator
2405 >::type
2406 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiseliered9e9362017-04-16 02:40:45 +00002407
2408#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002409 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002410 iterator insert(const_iterator __position, initializer_list<value_type> __il)
2411 {return insert(__position, __il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +00002412#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002413
Howard Hinnantcf823322010-12-17 14:46:43 +00002414 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002415 iterator erase(const_iterator __first, const_iterator __last);
2416
Howard Hinnant1c936782011-06-03 19:40:40 +00002417 _LIBCPP_INLINE_VISIBILITY
2418 void clear() _NOEXCEPT {__size_ = 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002419
Howard Hinnant1c936782011-06-03 19:40:40 +00002420 void swap(vector&)
Marshall Clow8982dcd2015-07-13 20:04:56 +00002421#if _LIBCPP_STD_VER >= 14
2422 _NOEXCEPT;
2423#else
Eric Fiselier69c51982016-12-28 06:06:09 +00002424 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00002425 __is_nothrow_swappable<allocator_type>::value);
2426#endif
Marshall Clow89eb3822016-04-07 14:20:31 +00002427 static void swap(reference __x, reference __y) _NOEXCEPT { _VSTD::swap(__x, __y); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002428
2429 void resize(size_type __sz, value_type __x = false);
Howard Hinnant1c936782011-06-03 19:40:40 +00002430 void flip() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002431
2432 bool __invariants() const;
2433
2434private:
Howard Hinnantcf823322010-12-17 14:46:43 +00002435 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Marshall Clow3ff48e02018-05-22 16:20:28 +00002436 void __vallocate(size_type __n);
2437 void __vdeallocate() _NOEXCEPT;
Howard Hinnant1c936782011-06-03 19:40:40 +00002438 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantea382952013-08-14 18:00:20 +00002439 static size_type __align_it(size_type __new_size) _NOEXCEPT
Eric Fiselierb41db9a2018-10-01 01:59:37 +00002440 {return __new_size + (__bits_per_word-1) & ~((size_type)__bits_per_word-1);}
Howard Hinnantcf823322010-12-17 14:46:43 +00002441 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
2442 _LIBCPP_INLINE_VISIBILITY void __construct_at_end(size_type __n, bool __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002443 template <class _ForwardIterator>
2444 typename enable_if
2445 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002446 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002447 void
2448 >::type
2449 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
2450 void __append(size_type __n, const_reference __x);
Howard Hinnant1c936782011-06-03 19:40:40 +00002451 _LIBCPP_INLINE_VISIBILITY
2452 reference __make_ref(size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002453 {return reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnant1c936782011-06-03 19:40:40 +00002454 _LIBCPP_INLINE_VISIBILITY
2455 const_reference __make_ref(size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002456 {return const_reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnant1c936782011-06-03 19:40:40 +00002457 _LIBCPP_INLINE_VISIBILITY
2458 iterator __make_iter(size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002459 {return iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnant1c936782011-06-03 19:40:40 +00002460 _LIBCPP_INLINE_VISIBILITY
2461 const_iterator __make_iter(size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002462 {return const_iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnant1c936782011-06-03 19:40:40 +00002463 _LIBCPP_INLINE_VISIBILITY
2464 iterator __const_iterator_cast(const_iterator __p) _NOEXCEPT
Howard Hinnant76053d72013-06-27 19:35:32 +00002465 {return begin() + (__p - cbegin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002466
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002467 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002468 void __copy_assign_alloc(const vector& __v)
2469 {__copy_assign_alloc(__v, integral_constant<bool,
2470 __storage_traits::propagate_on_container_copy_assignment::value>());}
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002471 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002472 void __copy_assign_alloc(const vector& __c, true_type)
2473 {
2474 if (__alloc() != __c.__alloc())
Marshall Clow3ff48e02018-05-22 16:20:28 +00002475 __vdeallocate();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002476 __alloc() = __c.__alloc();
2477 }
2478
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002479 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00002480 void __copy_assign_alloc(const vector&, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002481 {}
2482
2483 void __move_assign(vector& __c, false_type);
Howard Hinnant1c936782011-06-03 19:40:40 +00002484 void __move_assign(vector& __c, true_type)
2485 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002486 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002487 void __move_assign_alloc(vector& __c)
Howard Hinnant1c936782011-06-03 19:40:40 +00002488 _NOEXCEPT_(
2489 !__storage_traits::propagate_on_container_move_assignment::value ||
2490 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002491 {__move_assign_alloc(__c, integral_constant<bool,
2492 __storage_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002493 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +00002494 void __move_assign_alloc(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002495 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002496 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002497 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002498 }
2499
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002500 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00002501 void __move_assign_alloc(vector&, false_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002502 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002503 {}
2504
Howard Hinnant1c936782011-06-03 19:40:40 +00002505 size_t __hash_code() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002506
2507 friend class __bit_reference<vector>;
2508 friend class __bit_const_reference<vector>;
2509 friend class __bit_iterator<vector, false>;
2510 friend class __bit_iterator<vector, true>;
Howard Hinnant4210a0f2012-08-17 17:10:18 +00002511 friend struct __bit_array<vector>;
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002512 friend struct _LIBCPP_TEMPLATE_VIS hash<vector>;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002513};
2514
2515template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002516inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002517void
2518vector<bool, _Allocator>::__invalidate_all_iterators()
2519{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002520}
2521
2522// Allocate space for __n objects
2523// throws length_error if __n > max_size()
2524// throws (probably bad_alloc) if memory run out
2525// Precondition: __begin_ == __end_ == __cap() == 0
2526// Precondition: __n > 0
2527// Postcondition: capacity() == __n
2528// Postcondition: size() == 0
2529template <class _Allocator>
2530void
Marshall Clow3ff48e02018-05-22 16:20:28 +00002531vector<bool, _Allocator>::__vallocate(size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002532{
2533 if (__n > max_size())
2534 this->__throw_length_error();
2535 __n = __external_cap_to_internal(__n);
2536 this->__begin_ = __storage_traits::allocate(this->__alloc(), __n);
2537 this->__size_ = 0;
2538 this->__cap() = __n;
2539}
2540
2541template <class _Allocator>
2542void
Marshall Clow3ff48e02018-05-22 16:20:28 +00002543vector<bool, _Allocator>::__vdeallocate() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002544{
Howard Hinnant76053d72013-06-27 19:35:32 +00002545 if (this->__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002546 {
2547 __storage_traits::deallocate(this->__alloc(), this->__begin_, __cap());
2548 __invalidate_all_iterators();
Howard Hinnant76053d72013-06-27 19:35:32 +00002549 this->__begin_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002550 this->__size_ = this->__cap() = 0;
2551 }
2552}
2553
2554template <class _Allocator>
2555typename vector<bool, _Allocator>::size_type
Howard Hinnant1c936782011-06-03 19:40:40 +00002556vector<bool, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002557{
2558 size_type __amax = __storage_traits::max_size(__alloc());
2559 size_type __nmax = numeric_limits<size_type>::max() / 2; // end() >= begin(), always
2560 if (__nmax / __bits_per_word <= __amax)
2561 return __nmax;
2562 return __internal_cap_to_external(__amax);
2563}
2564
2565// Precondition: __new_size > capacity()
2566template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002567inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002568typename vector<bool, _Allocator>::size_type
2569vector<bool, _Allocator>::__recommend(size_type __new_size) const
2570{
2571 const size_type __ms = max_size();
2572 if (__new_size > __ms)
2573 this->__throw_length_error();
2574 const size_type __cap = capacity();
2575 if (__cap >= __ms / 2)
2576 return __ms;
Howard Hinnantea382952013-08-14 18:00:20 +00002577 return _VSTD::max(2*__cap, __align_it(__new_size));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002578}
2579
2580// Default constructs __n objects starting at __end_
2581// Precondition: __n > 0
2582// Precondition: size() + __n <= capacity()
2583// Postcondition: size() == size() + __n
2584template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002585inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002586void
2587vector<bool, _Allocator>::__construct_at_end(size_type __n, bool __x)
2588{
2589 size_type __old_size = this->__size_;
2590 this->__size_ += __n;
Marshall Clow1893ec72018-10-23 20:07:45 +00002591 if (__old_size == 0 || ((__old_size - 1) / __bits_per_word) != ((this->__size_ - 1) / __bits_per_word))
2592 {
2593 if (this->__size_ <= __bits_per_word)
2594 this->__begin_[0] = __storage_type(0);
2595 else
2596 this->__begin_[(this->__size_ - 1) / __bits_per_word] = __storage_type(0);
2597 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002598 _VSTD::fill_n(__make_iter(__old_size), __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002599}
2600
2601template <class _Allocator>
2602template <class _ForwardIterator>
2603typename enable_if
2604<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002605 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002606 void
2607>::type
2608vector<bool, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
2609{
2610 size_type __old_size = this->__size_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002611 this->__size_ += _VSTD::distance(__first, __last);
Marshall Clow1893ec72018-10-23 20:07:45 +00002612 if (__old_size == 0 || ((__old_size - 1) / __bits_per_word) != ((this->__size_ - 1) / __bits_per_word))
2613 {
2614 if (this->__size_ <= __bits_per_word)
2615 this->__begin_[0] = __storage_type(0);
2616 else
2617 this->__begin_[(this->__size_ - 1) / __bits_per_word] = __storage_type(0);
2618 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002619 _VSTD::copy(__first, __last, __make_iter(__old_size));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002620}
2621
2622template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002623inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002624vector<bool, _Allocator>::vector()
Marshall Clowe546cbb2015-06-04 02:05:41 +00002625 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant76053d72013-06-27 19:35:32 +00002626 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002627 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002628 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002629{
2630}
2631
2632template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002633inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002634vector<bool, _Allocator>::vector(const allocator_type& __a)
Marshall Clow8f282f42015-06-04 00:10:20 +00002635#if _LIBCPP_STD_VER <= 14
2636 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
2637#else
2638 _NOEXCEPT
2639#endif
Howard Hinnant76053d72013-06-27 19:35:32 +00002640 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002641 __size_(0),
2642 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2643{
2644}
2645
2646template <class _Allocator>
2647vector<bool, _Allocator>::vector(size_type __n)
Howard Hinnant76053d72013-06-27 19:35:32 +00002648 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002649 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002650 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002651{
2652 if (__n > 0)
2653 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002654 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002655 __construct_at_end(__n, false);
2656 }
2657}
2658
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00002659#if _LIBCPP_STD_VER > 11
2660template <class _Allocator>
2661vector<bool, _Allocator>::vector(size_type __n, const allocator_type& __a)
2662 : __begin_(nullptr),
2663 __size_(0),
2664 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2665{
2666 if (__n > 0)
2667 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002668 __vallocate(__n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00002669 __construct_at_end(__n, false);
2670 }
2671}
2672#endif
2673
Howard Hinnantc51e1022010-05-11 19:42:16 +00002674template <class _Allocator>
2675vector<bool, _Allocator>::vector(size_type __n, const value_type& __x)
Howard Hinnant76053d72013-06-27 19:35:32 +00002676 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002677 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002678 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002679{
2680 if (__n > 0)
2681 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002682 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002683 __construct_at_end(__n, __x);
2684 }
2685}
2686
2687template <class _Allocator>
2688vector<bool, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002689 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002690 __size_(0),
2691 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2692{
2693 if (__n > 0)
2694 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002695 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002696 __construct_at_end(__n, __x);
2697 }
2698}
2699
2700template <class _Allocator>
2701template <class _InputIterator>
2702vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002703 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
2704 !__is_cpp17_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002705 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002706 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002707 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002708{
2709#ifndef _LIBCPP_NO_EXCEPTIONS
2710 try
2711 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002712#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002713 for (; __first != __last; ++__first)
2714 push_back(*__first);
2715#ifndef _LIBCPP_NO_EXCEPTIONS
2716 }
2717 catch (...)
2718 {
Howard Hinnant76053d72013-06-27 19:35:32 +00002719 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002720 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2721 __invalidate_all_iterators();
2722 throw;
2723 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002724#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002725}
2726
2727template <class _Allocator>
2728template <class _InputIterator>
2729vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002730 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
2731 !__is_cpp17_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002732 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002733 __size_(0),
2734 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2735{
2736#ifndef _LIBCPP_NO_EXCEPTIONS
2737 try
2738 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002739#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002740 for (; __first != __last; ++__first)
2741 push_back(*__first);
2742#ifndef _LIBCPP_NO_EXCEPTIONS
2743 }
2744 catch (...)
2745 {
Howard Hinnant76053d72013-06-27 19:35:32 +00002746 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002747 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2748 __invalidate_all_iterators();
2749 throw;
2750 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002751#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002752}
2753
2754template <class _Allocator>
2755template <class _ForwardIterator>
2756vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002757 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002758 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002759 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002760 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002761{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002762 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002763 if (__n > 0)
2764 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002765 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002766 __construct_at_end(__first, __last);
2767 }
2768}
2769
2770template <class _Allocator>
2771template <class _ForwardIterator>
2772vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002773 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002774 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002775 __size_(0),
2776 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2777{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002778 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002779 if (__n > 0)
2780 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002781 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002782 __construct_at_end(__first, __last);
2783 }
2784}
2785
Eric Fiseliered9e9362017-04-16 02:40:45 +00002786#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002787
Howard Hinnantc51e1022010-05-11 19:42:16 +00002788template <class _Allocator>
2789vector<bool, _Allocator>::vector(initializer_list<value_type> __il)
Howard Hinnant76053d72013-06-27 19:35:32 +00002790 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002791 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002792 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002793{
2794 size_type __n = static_cast<size_type>(__il.size());
2795 if (__n > 0)
2796 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002797 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002798 __construct_at_end(__il.begin(), __il.end());
2799 }
2800}
2801
2802template <class _Allocator>
2803vector<bool, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002804 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002805 __size_(0),
2806 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2807{
2808 size_type __n = static_cast<size_type>(__il.size());
2809 if (__n > 0)
2810 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002811 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002812 __construct_at_end(__il.begin(), __il.end());
2813 }
2814}
2815
Eric Fiseliered9e9362017-04-16 02:40:45 +00002816#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002817
Howard Hinnantc51e1022010-05-11 19:42:16 +00002818template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002819vector<bool, _Allocator>::~vector()
2820{
Howard Hinnant76053d72013-06-27 19:35:32 +00002821 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002822 __storage_traits::deallocate(__alloc(), __begin_, __cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002823 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002824}
2825
2826template <class _Allocator>
2827vector<bool, _Allocator>::vector(const vector& __v)
Howard Hinnant76053d72013-06-27 19:35:32 +00002828 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002829 __size_(0),
2830 __cap_alloc_(0, __storage_traits::select_on_container_copy_construction(__v.__alloc()))
2831{
2832 if (__v.size() > 0)
2833 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002834 __vallocate(__v.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002835 __construct_at_end(__v.begin(), __v.end());
2836 }
2837}
2838
2839template <class _Allocator>
2840vector<bool, _Allocator>::vector(const vector& __v, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002841 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002842 __size_(0),
2843 __cap_alloc_(0, __a)
2844{
2845 if (__v.size() > 0)
2846 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002847 __vallocate(__v.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002848 __construct_at_end(__v.begin(), __v.end());
2849 }
2850}
2851
2852template <class _Allocator>
2853vector<bool, _Allocator>&
2854vector<bool, _Allocator>::operator=(const vector& __v)
2855{
2856 if (this != &__v)
2857 {
2858 __copy_assign_alloc(__v);
2859 if (__v.__size_)
2860 {
2861 if (__v.__size_ > capacity())
2862 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002863 __vdeallocate();
2864 __vallocate(__v.__size_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002865 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002866 _VSTD::copy(__v.__begin_, __v.__begin_ + __external_cap_to_internal(__v.__size_), __begin_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002867 }
2868 __size_ = __v.__size_;
2869 }
2870 return *this;
2871}
2872
Eric Fiseliered9e9362017-04-16 02:40:45 +00002873#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant74279a52010-09-04 23:28:19 +00002874
Howard Hinnantc51e1022010-05-11 19:42:16 +00002875template <class _Allocator>
Eric Fiselier4f1534c2018-06-05 22:32:52 +00002876inline _LIBCPP_INLINE_VISIBILITY vector<bool, _Allocator>::vector(vector&& __v)
Marshall Clowe5108202015-07-14 14:46:32 +00002877#if _LIBCPP_STD_VER > 14
Eric Fiselier4f1534c2018-06-05 22:32:52 +00002878 _NOEXCEPT
Marshall Clowe5108202015-07-14 14:46:32 +00002879#else
Eric Fiselier4f1534c2018-06-05 22:32:52 +00002880 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowe5108202015-07-14 14:46:32 +00002881#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002882 : __begin_(__v.__begin_),
2883 __size_(__v.__size_),
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05002884 __cap_alloc_(_VSTD::move(__v.__cap_alloc_)) {
Howard Hinnant76053d72013-06-27 19:35:32 +00002885 __v.__begin_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002886 __v.__size_ = 0;
2887 __v.__cap() = 0;
2888}
2889
2890template <class _Allocator>
2891vector<bool, _Allocator>::vector(vector&& __v, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002892 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002893 __size_(0),
2894 __cap_alloc_(0, __a)
2895{
2896 if (__a == allocator_type(__v.__alloc()))
2897 {
2898 this->__begin_ = __v.__begin_;
2899 this->__size_ = __v.__size_;
2900 this->__cap() = __v.__cap();
2901 __v.__begin_ = nullptr;
2902 __v.__cap() = __v.__size_ = 0;
2903 }
2904 else if (__v.size() > 0)
2905 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002906 __vallocate(__v.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002907 __construct_at_end(__v.begin(), __v.end());
2908 }
2909}
2910
2911template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002912inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002913vector<bool, _Allocator>&
2914vector<bool, _Allocator>::operator=(vector&& __v)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002915 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002916{
2917 __move_assign(__v, integral_constant<bool,
2918 __storage_traits::propagate_on_container_move_assignment::value>());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002919 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002920}
2921
2922template <class _Allocator>
2923void
2924vector<bool, _Allocator>::__move_assign(vector& __c, false_type)
2925{
2926 if (__alloc() != __c.__alloc())
2927 assign(__c.begin(), __c.end());
2928 else
2929 __move_assign(__c, true_type());
2930}
2931
2932template <class _Allocator>
2933void
2934vector<bool, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002935 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002936{
Marshall Clow3ff48e02018-05-22 16:20:28 +00002937 __vdeallocate();
Marshall Clowb4871fa2014-07-21 15:15:15 +00002938 __move_assign_alloc(__c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002939 this->__begin_ = __c.__begin_;
2940 this->__size_ = __c.__size_;
2941 this->__cap() = __c.__cap();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002942 __c.__begin_ = nullptr;
2943 __c.__cap() = __c.__size_ = 0;
2944}
Howard Hinnant74279a52010-09-04 23:28:19 +00002945
Eric Fiseliered9e9362017-04-16 02:40:45 +00002946#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002947
2948template <class _Allocator>
2949void
2950vector<bool, _Allocator>::assign(size_type __n, const value_type& __x)
2951{
2952 __size_ = 0;
2953 if (__n > 0)
2954 {
2955 size_type __c = capacity();
2956 if (__n <= __c)
2957 __size_ = __n;
2958 else
2959 {
2960 vector __v(__alloc());
2961 __v.reserve(__recommend(__n));
2962 __v.__size_ = __n;
2963 swap(__v);
2964 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002965 _VSTD::fill_n(begin(), __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002966 }
Eric Fiselier69c51982016-12-28 06:06:09 +00002967 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002968}
2969
2970template <class _Allocator>
2971template <class _InputIterator>
2972typename enable_if
2973<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002974 __is_cpp17_input_iterator<_InputIterator>::value &&
2975 !__is_cpp17_forward_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002976 void
2977>::type
2978vector<bool, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2979{
2980 clear();
2981 for (; __first != __last; ++__first)
2982 push_back(*__first);
2983}
2984
2985template <class _Allocator>
2986template <class _ForwardIterator>
2987typename enable_if
2988<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002989 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002990 void
2991>::type
2992vector<bool, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2993{
2994 clear();
Eric Fiselier6003c772016-12-23 23:37:52 +00002995 difference_type __ns = _VSTD::distance(__first, __last);
2996 _LIBCPP_ASSERT(__ns >= 0, "invalid range specified");
2997 const size_t __n = static_cast<size_type>(__ns);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002998 if (__n)
2999 {
3000 if (__n > capacity())
3001 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00003002 __vdeallocate();
3003 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003004 }
3005 __construct_at_end(__first, __last);
3006 }
3007}
3008
3009template <class _Allocator>
3010void
3011vector<bool, _Allocator>::reserve(size_type __n)
3012{
3013 if (__n > capacity())
3014 {
3015 vector __v(this->__alloc());
Marshall Clow3ff48e02018-05-22 16:20:28 +00003016 __v.__vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003017 __v.__construct_at_end(this->begin(), this->end());
3018 swap(__v);
3019 __invalidate_all_iterators();
3020 }
3021}
3022
3023template <class _Allocator>
3024void
Howard Hinnant1c936782011-06-03 19:40:40 +00003025vector<bool, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003026{
3027 if (__external_cap_to_internal(size()) > __cap())
3028 {
3029#ifndef _LIBCPP_NO_EXCEPTIONS
3030 try
3031 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003032#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003033 vector(*this, allocator_type(__alloc())).swap(*this);
3034#ifndef _LIBCPP_NO_EXCEPTIONS
3035 }
3036 catch (...)
3037 {
3038 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003039#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003040 }
3041}
3042
3043template <class _Allocator>
3044typename vector<bool, _Allocator>::reference
3045vector<bool, _Allocator>::at(size_type __n)
3046{
3047 if (__n >= size())
3048 this->__throw_out_of_range();
3049 return (*this)[__n];
3050}
3051
3052template <class _Allocator>
3053typename vector<bool, _Allocator>::const_reference
3054vector<bool, _Allocator>::at(size_type __n) const
3055{
3056 if (__n >= size())
3057 this->__throw_out_of_range();
3058 return (*this)[__n];
3059}
3060
3061template <class _Allocator>
3062void
3063vector<bool, _Allocator>::push_back(const value_type& __x)
3064{
3065 if (this->__size_ == this->capacity())
3066 reserve(__recommend(this->__size_ + 1));
3067 ++this->__size_;
3068 back() = __x;
3069}
3070
3071template <class _Allocator>
3072typename vector<bool, _Allocator>::iterator
3073vector<bool, _Allocator>::insert(const_iterator __position, const value_type& __x)
3074{
3075 iterator __r;
3076 if (size() < capacity())
3077 {
3078 const_iterator __old_end = end();
3079 ++__size_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003080 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003081 __r = __const_iterator_cast(__position);
3082 }
3083 else
3084 {
3085 vector __v(__alloc());
3086 __v.reserve(__recommend(__size_ + 1));
3087 __v.__size_ = __size_ + 1;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003088 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3089 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003090 swap(__v);
3091 }
3092 *__r = __x;
3093 return __r;
3094}
3095
3096template <class _Allocator>
3097typename vector<bool, _Allocator>::iterator
3098vector<bool, _Allocator>::insert(const_iterator __position, size_type __n, const value_type& __x)
3099{
3100 iterator __r;
3101 size_type __c = capacity();
3102 if (__n <= __c && size() <= __c - __n)
3103 {
3104 const_iterator __old_end = end();
3105 __size_ += __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003106 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003107 __r = __const_iterator_cast(__position);
3108 }
3109 else
3110 {
3111 vector __v(__alloc());
3112 __v.reserve(__recommend(__size_ + __n));
3113 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003114 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3115 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003116 swap(__v);
3117 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003118 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003119 return __r;
3120}
3121
3122template <class _Allocator>
3123template <class _InputIterator>
3124typename enable_if
3125<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05003126 __is_cpp17_input_iterator <_InputIterator>::value &&
3127 !__is_cpp17_forward_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003128 typename vector<bool, _Allocator>::iterator
3129>::type
3130vector<bool, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
3131{
3132 difference_type __off = __position - begin();
3133 iterator __p = __const_iterator_cast(__position);
3134 iterator __old_end = end();
3135 for (; size() != capacity() && __first != __last; ++__first)
3136 {
3137 ++this->__size_;
3138 back() = *__first;
3139 }
3140 vector __v(__alloc());
3141 if (__first != __last)
3142 {
3143#ifndef _LIBCPP_NO_EXCEPTIONS
3144 try
3145 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003146#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003147 __v.assign(__first, __last);
3148 difference_type __old_size = static_cast<difference_type>(__old_end - begin());
3149 difference_type __old_p = __p - begin();
3150 reserve(__recommend(size() + __v.size()));
3151 __p = begin() + __old_p;
3152 __old_end = begin() + __old_size;
3153#ifndef _LIBCPP_NO_EXCEPTIONS
3154 }
3155 catch (...)
3156 {
3157 erase(__old_end, end());
3158 throw;
3159 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003160#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003161 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003162 __p = _VSTD::rotate(__p, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003163 insert(__p, __v.begin(), __v.end());
3164 return begin() + __off;
3165}
3166
3167template <class _Allocator>
3168template <class _ForwardIterator>
3169typename enable_if
3170<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05003171 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003172 typename vector<bool, _Allocator>::iterator
3173>::type
3174vector<bool, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
3175{
Eric Fiselier654dd332016-12-11 05:31:00 +00003176 const difference_type __n_signed = _VSTD::distance(__first, __last);
3177 _LIBCPP_ASSERT(__n_signed >= 0, "invalid range specified");
3178 const size_type __n = static_cast<size_type>(__n_signed);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003179 iterator __r;
3180 size_type __c = capacity();
3181 if (__n <= __c && size() <= __c - __n)
3182 {
3183 const_iterator __old_end = end();
3184 __size_ += __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003185 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003186 __r = __const_iterator_cast(__position);
3187 }
3188 else
3189 {
3190 vector __v(__alloc());
3191 __v.reserve(__recommend(__size_ + __n));
3192 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003193 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3194 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003195 swap(__v);
3196 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003197 _VSTD::copy(__first, __last, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003198 return __r;
3199}
3200
3201template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003202inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003203typename vector<bool, _Allocator>::iterator
3204vector<bool, _Allocator>::erase(const_iterator __position)
3205{
3206 iterator __r = __const_iterator_cast(__position);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003207 _VSTD::copy(__position + 1, this->cend(), __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003208 --__size_;
3209 return __r;
3210}
3211
3212template <class _Allocator>
3213typename vector<bool, _Allocator>::iterator
3214vector<bool, _Allocator>::erase(const_iterator __first, const_iterator __last)
3215{
3216 iterator __r = __const_iterator_cast(__first);
3217 difference_type __d = __last - __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003218 _VSTD::copy(__last, this->cend(), __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003219 __size_ -= __d;
3220 return __r;
3221}
3222
3223template <class _Allocator>
3224void
3225vector<bool, _Allocator>::swap(vector& __x)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003226#if _LIBCPP_STD_VER >= 14
3227 _NOEXCEPT
3228#else
Eric Fiselier69c51982016-12-28 06:06:09 +00003229 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003230 __is_nothrow_swappable<allocator_type>::value)
3231#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003232{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003233 _VSTD::swap(this->__begin_, __x.__begin_);
3234 _VSTD::swap(this->__size_, __x.__size_);
3235 _VSTD::swap(this->__cap(), __x.__cap());
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05003236 _VSTD::__swap_allocator(this->__alloc(), __x.__alloc(),
Marshall Clow8982dcd2015-07-13 20:04:56 +00003237 integral_constant<bool, __alloc_traits::propagate_on_container_swap::value>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003238}
3239
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003240template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003241void
3242vector<bool, _Allocator>::resize(size_type __sz, value_type __x)
3243{
3244 size_type __cs = size();
3245 if (__cs < __sz)
3246 {
3247 iterator __r;
3248 size_type __c = capacity();
3249 size_type __n = __sz - __cs;
3250 if (__n <= __c && __cs <= __c - __n)
3251 {
3252 __r = end();
3253 __size_ += __n;
3254 }
3255 else
3256 {
3257 vector __v(__alloc());
3258 __v.reserve(__recommend(__size_ + __n));
3259 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003260 __r = _VSTD::copy(cbegin(), cend(), __v.begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003261 swap(__v);
3262 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003263 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003264 }
3265 else
3266 __size_ = __sz;
3267}
3268
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003269template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003270void
Howard Hinnant1c936782011-06-03 19:40:40 +00003271vector<bool, _Allocator>::flip() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003272{
3273 // do middle whole words
3274 size_type __n = __size_;
3275 __storage_pointer __p = __begin_;
3276 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3277 *__p = ~*__p;
3278 // do last partial word
3279 if (__n > 0)
3280 {
3281 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3282 __storage_type __b = *__p & __m;
3283 *__p &= ~__m;
3284 *__p |= ~__b & __m;
3285 }
3286}
3287
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003288template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003289bool
3290vector<bool, _Allocator>::__invariants() const
3291{
Howard Hinnant76053d72013-06-27 19:35:32 +00003292 if (this->__begin_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003293 {
3294 if (this->__size_ != 0 || this->__cap() != 0)
3295 return false;
3296 }
3297 else
3298 {
3299 if (this->__cap() == 0)
3300 return false;
3301 if (this->__size_ > this->capacity())
3302 return false;
3303 }
3304 return true;
3305}
3306
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003307template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003308size_t
Howard Hinnant1c936782011-06-03 19:40:40 +00003309vector<bool, _Allocator>::__hash_code() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003310{
3311 size_t __h = 0;
3312 // do middle whole words
3313 size_type __n = __size_;
3314 __storage_pointer __p = __begin_;
3315 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3316 __h ^= *__p;
3317 // do last partial word
3318 if (__n > 0)
3319 {
3320 const __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3321 __h ^= *__p & __m;
3322 }
3323 return __h;
3324}
3325
3326template <class _Allocator>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003327struct _LIBCPP_TEMPLATE_VIS hash<vector<bool, _Allocator> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00003328 : public unary_function<vector<bool, _Allocator>, size_t>
3329{
Howard Hinnant1c265cd2010-09-23 18:58:28 +00003330 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +00003331 size_t operator()(const vector<bool, _Allocator>& __vec) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003332 {return __vec.__hash_code();}
3333};
3334
3335template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003336inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003337bool
3338operator==(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3339{
3340 const typename vector<_Tp, _Allocator>::size_type __sz = __x.size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003341 return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003342}
3343
3344template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003345inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003346bool
3347operator!=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3348{
3349 return !(__x == __y);
3350}
3351
3352template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003353inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003354bool
3355operator< (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3356{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003357 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003358}
3359
3360template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003361inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003362bool
3363operator> (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3364{
3365 return __y < __x;
3366}
3367
3368template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003369inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003370bool
3371operator>=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3372{
3373 return !(__x < __y);
3374}
3375
3376template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003377inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003378bool
3379operator<=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3380{
3381 return !(__y < __x);
3382}
3383
3384template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003385inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003386void
3387swap(vector<_Tp, _Allocator>& __x, vector<_Tp, _Allocator>& __y)
Howard Hinnant1c936782011-06-03 19:40:40 +00003388 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00003389{
3390 __x.swap(__y);
3391}
3392
Marshall Clow29b53f22018-12-14 18:49:35 +00003393#if _LIBCPP_STD_VER > 17
3394template <class _Tp, class _Allocator, class _Up>
Marek Kurdeja98b1412020-05-02 13:58:03 +02003395inline _LIBCPP_INLINE_VISIBILITY typename vector<_Tp, _Allocator>::size_type
3396erase(vector<_Tp, _Allocator>& __c, const _Up& __v) {
3397 auto __old_size = __c.size();
3398 __c.erase(_VSTD::remove(__c.begin(), __c.end(), __v), __c.end());
3399 return __old_size - __c.size();
3400}
Marshall Clow29b53f22018-12-14 18:49:35 +00003401
3402template <class _Tp, class _Allocator, class _Predicate>
Marek Kurdeja98b1412020-05-02 13:58:03 +02003403inline _LIBCPP_INLINE_VISIBILITY typename vector<_Tp, _Allocator>::size_type
3404erase_if(vector<_Tp, _Allocator>& __c, _Predicate __pred) {
3405 auto __old_size = __c.size();
3406 __c.erase(_VSTD::remove_if(__c.begin(), __c.end(), __pred), __c.end());
3407 return __old_size - __c.size();
3408}
Marshall Clow29b53f22018-12-14 18:49:35 +00003409#endif
3410
Howard Hinnantc51e1022010-05-11 19:42:16 +00003411_LIBCPP_END_NAMESPACE_STD
3412
Eric Fiselierf4433a32017-05-31 22:07:49 +00003413_LIBCPP_POP_MACROS
3414
Howard Hinnantc51e1022010-05-11 19:42:16 +00003415#endif // _LIBCPP_VECTOR