blob: 1007beeaafd033ff8064e174a3a7e2ec3f2825fb [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>
279#include <limits>
280#include <initializer_list>
281#include <memory>
282#include <stdexcept>
283#include <algorithm>
284#include <cstring>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000285#include <version>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000286#include <__split_buffer>
287#include <__functional_base>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000288
Eric Fiselier14b6de92014-08-10 23:53:08 +0000289#include <__debug>
Howard Hinnante6ff0b62013-08-02 00:26:35 +0000290
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000291#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000292#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000293#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000294
Eric Fiselierf4433a32017-05-31 22:07:49 +0000295_LIBCPP_PUSH_MACROS
296#include <__undef_macros>
297
298
Howard Hinnantc51e1022010-05-11 19:42:16 +0000299_LIBCPP_BEGIN_NAMESPACE_STD
300
301template <bool>
Louis Dionne53dca682019-08-28 18:10:39 +0000302class _LIBCPP_TEMPLATE_VIS __vector_base_common
Howard Hinnantc51e1022010-05-11 19:42:16 +0000303{
304protected:
Louis Dionne16fe2952018-07-11 23:14:33 +0000305 _LIBCPP_INLINE_VISIBILITY __vector_base_common() {}
Marshall Clow8fea1612016-08-25 15:09:01 +0000306 _LIBCPP_NORETURN void __throw_length_error() const;
307 _LIBCPP_NORETURN void __throw_out_of_range() const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000308};
309
310template <bool __b>
311void
312__vector_base_common<__b>::__throw_length_error() const
313{
Marshall Clow8fea1612016-08-25 15:09:01 +0000314 _VSTD::__throw_length_error("vector");
Howard Hinnantc51e1022010-05-11 19:42:16 +0000315}
316
317template <bool __b>
318void
319__vector_base_common<__b>::__throw_out_of_range() const
320{
Marshall Clow8fea1612016-08-25 15:09:01 +0000321 _VSTD::__throw_out_of_range("vector");
Howard Hinnantc51e1022010-05-11 19:42:16 +0000322}
323
Eric Fiselier1b57fa82016-09-15 22:27:07 +0000324_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __vector_base_common<true>)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000325
326template <class _Tp, class _Allocator>
327class __vector_base
328 : protected __vector_base_common<true>
329{
Marshall Clowf0ca1492018-05-21 21:30:12 +0000330public:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000331 typedef _Allocator allocator_type;
332 typedef allocator_traits<allocator_type> __alloc_traits;
Marshall Clowf0ca1492018-05-21 21:30:12 +0000333 typedef typename __alloc_traits::size_type size_type;
334protected:
335 typedef _Tp value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000336 typedef value_type& reference;
337 typedef const value_type& const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000338 typedef typename __alloc_traits::difference_type difference_type;
339 typedef typename __alloc_traits::pointer pointer;
340 typedef typename __alloc_traits::const_pointer const_pointer;
341 typedef pointer iterator;
342 typedef const_pointer const_iterator;
343
344 pointer __begin_;
345 pointer __end_;
346 __compressed_pair<pointer, allocator_type> __end_cap_;
347
Howard Hinnant1c936782011-06-03 19:40:40 +0000348 _LIBCPP_INLINE_VISIBILITY
349 allocator_type& __alloc() _NOEXCEPT
350 {return __end_cap_.second();}
351 _LIBCPP_INLINE_VISIBILITY
352 const allocator_type& __alloc() const _NOEXCEPT
353 {return __end_cap_.second();}
354 _LIBCPP_INLINE_VISIBILITY
355 pointer& __end_cap() _NOEXCEPT
356 {return __end_cap_.first();}
357 _LIBCPP_INLINE_VISIBILITY
358 const pointer& __end_cap() const _NOEXCEPT
359 {return __end_cap_.first();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000360
Howard Hinnant1c936782011-06-03 19:40:40 +0000361 _LIBCPP_INLINE_VISIBILITY
362 __vector_base()
363 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnantcf823322010-12-17 14:46:43 +0000364 _LIBCPP_INLINE_VISIBILITY __vector_base(const allocator_type& __a);
Eric Fiselier4f1534c2018-06-05 22:32:52 +0000365#ifndef _LIBCPP_CXX03_LANG
366 _LIBCPP_INLINE_VISIBILITY __vector_base(allocator_type&& __a) _NOEXCEPT;
367#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000368 ~__vector_base();
369
Howard Hinnant1c936782011-06-03 19:40:40 +0000370 _LIBCPP_INLINE_VISIBILITY
371 void clear() _NOEXCEPT {__destruct_at_end(__begin_);}
372 _LIBCPP_INLINE_VISIBILITY
373 size_type capacity() const _NOEXCEPT
374 {return static_cast<size_type>(__end_cap() - __begin_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000375
Howard Hinnant1c936782011-06-03 19:40:40 +0000376 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76053d72013-06-27 19:35:32 +0000377 void __destruct_at_end(pointer __new_last) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000378
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000379 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000380 void __copy_assign_alloc(const __vector_base& __c)
381 {__copy_assign_alloc(__c, integral_constant<bool,
382 __alloc_traits::propagate_on_container_copy_assignment::value>());}
383
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000384 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000385 void __move_assign_alloc(__vector_base& __c)
Howard Hinnant1c936782011-06-03 19:40:40 +0000386 _NOEXCEPT_(
387 !__alloc_traits::propagate_on_container_move_assignment::value ||
388 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000389 {__move_assign_alloc(__c, integral_constant<bool,
390 __alloc_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000391private:
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000392 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000393 void __copy_assign_alloc(const __vector_base& __c, true_type)
394 {
395 if (__alloc() != __c.__alloc())
396 {
397 clear();
398 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
399 __begin_ = __end_ = __end_cap() = nullptr;
400 }
401 __alloc() = __c.__alloc();
402 }
403
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000404 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +0000405 void __copy_assign_alloc(const __vector_base&, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000406 {}
407
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000408 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +0000409 void __move_assign_alloc(__vector_base& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +0000410 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000411 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000412 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000413 }
414
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000415 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +0000416 void __move_assign_alloc(__vector_base&, false_type)
Howard Hinnant1c936782011-06-03 19:40:40 +0000417 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000418 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000419};
420
421template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000422inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000423void
Howard Hinnant76053d72013-06-27 19:35:32 +0000424__vector_base<_Tp, _Allocator>::__destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000425{
Bruce Mitchener405fdcd2017-03-23 14:39:23 +0000426 pointer __soon_to_be_end = __end_;
427 while (__new_last != __soon_to_be_end)
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500428 __alloc_traits::destroy(__alloc(), _VSTD::__to_address(--__soon_to_be_end));
Bruce Mitchener405fdcd2017-03-23 14:39:23 +0000429 __end_ = __new_last;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000430}
431
432template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000433inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000434__vector_base<_Tp, _Allocator>::__vector_base()
Howard Hinnant1c936782011-06-03 19:40:40 +0000435 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant76053d72013-06-27 19:35:32 +0000436 : __begin_(nullptr),
437 __end_(nullptr),
Eric Fiselier33ebfb62019-12-16 18:23:39 -0500438 __end_cap_(nullptr, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +0000439{
440}
441
442template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000443inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000444__vector_base<_Tp, _Allocator>::__vector_base(const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +0000445 : __begin_(nullptr),
446 __end_(nullptr),
447 __end_cap_(nullptr, __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000448{
449}
450
Eric Fiselier4f1534c2018-06-05 22:32:52 +0000451#ifndef _LIBCPP_CXX03_LANG
452template <class _Tp, class _Allocator>
453inline _LIBCPP_INLINE_VISIBILITY
454__vector_base<_Tp, _Allocator>::__vector_base(allocator_type&& __a) _NOEXCEPT
455 : __begin_(nullptr),
456 __end_(nullptr),
457 __end_cap_(nullptr, std::move(__a)) {}
458#endif
459
Howard Hinnantc51e1022010-05-11 19:42:16 +0000460template <class _Tp, class _Allocator>
461__vector_base<_Tp, _Allocator>::~__vector_base()
462{
Howard Hinnant76053d72013-06-27 19:35:32 +0000463 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000464 {
465 clear();
466 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
467 }
468}
469
Eric Fiselier876c6862016-02-20 00:19:45 +0000470template <class _Tp, class _Allocator /* = allocator<_Tp> */>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000471class _LIBCPP_TEMPLATE_VIS vector
Howard Hinnantc51e1022010-05-11 19:42:16 +0000472 : private __vector_base<_Tp, _Allocator>
473{
474private:
475 typedef __vector_base<_Tp, _Allocator> __base;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000476 typedef allocator<_Tp> __default_allocator_type;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000477public:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000478 typedef vector __self;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000479 typedef _Tp value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000480 typedef _Allocator allocator_type;
481 typedef typename __base::__alloc_traits __alloc_traits;
482 typedef typename __base::reference reference;
483 typedef typename __base::const_reference const_reference;
484 typedef typename __base::size_type size_type;
485 typedef typename __base::difference_type difference_type;
486 typedef typename __base::pointer pointer;
487 typedef typename __base::const_pointer const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000488 typedef __wrap_iter<pointer> iterator;
489 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000490 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
491 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000492
Howard Hinnanta416ff02013-03-26 19:04:56 +0000493 static_assert((is_same<typename allocator_type::value_type, value_type>::value),
494 "Allocator::value_type must be same type as value_type");
495
Howard Hinnant1c936782011-06-03 19:40:40 +0000496 _LIBCPP_INLINE_VISIBILITY
Marshall Clowe546cbb2015-06-04 02:05:41 +0000497 vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant27e0e772011-09-14 18:33:51 +0000498 {
Howard Hinnanta47c6d52011-09-16 17:29:17 +0000499#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000500 __get_db()->__insert_c(this);
501#endif
502 }
503 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
Marshall Clow8f282f42015-06-04 00:10:20 +0000504#if _LIBCPP_STD_VER <= 14
505 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
506#else
507 _NOEXCEPT
508#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +0000509 : __base(__a)
510 {
Howard Hinnanta47c6d52011-09-16 17:29:17 +0000511#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000512 __get_db()->__insert_c(this);
513#endif
514 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000515 explicit vector(size_type __n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +0000516#if _LIBCPP_STD_VER > 11
517 explicit vector(size_type __n, const allocator_type& __a);
518#endif
Marshall Clowf0ca1492018-05-21 21:30:12 +0000519 vector(size_type __n, const value_type& __x);
520 vector(size_type __n, const value_type& __x, const allocator_type& __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000521 template <class _InputIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +0000522 vector(_InputIterator __first,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500523 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
524 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000525 is_constructible<
526 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +0000527 typename iterator_traits<_InputIterator>::reference>::value,
528 _InputIterator>::type __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000529 template <class _InputIterator>
530 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500531 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
532 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000533 is_constructible<
534 value_type,
535 typename iterator_traits<_InputIterator>::reference>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000536 template <class _ForwardIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +0000537 vector(_ForwardIterator __first,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500538 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000539 is_constructible<
540 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +0000541 typename iterator_traits<_ForwardIterator>::reference>::value,
542 _ForwardIterator>::type __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000543 template <class _ForwardIterator>
544 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500545 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000546 is_constructible<
547 value_type,
548 typename iterator_traits<_ForwardIterator>::reference>::value>::type* = 0);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000549
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000550 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant27e0e772011-09-14 18:33:51 +0000551 ~vector()
552 {
Marshall Clow68af4f32018-09-07 15:47:59 +0000553 __annotate_delete();
554#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000555 __get_db()->__erase_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000556#endif
Marshall Clow68af4f32018-09-07 15:47:59 +0000557 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000558
559 vector(const vector& __x);
560 vector(const vector& __x, const allocator_type& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000561 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000562 vector& operator=(const vector& __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000563
564#ifndef _LIBCPP_CXX03_LANG
565 _LIBCPP_INLINE_VISIBILITY
566 vector(initializer_list<value_type> __il);
567
568 _LIBCPP_INLINE_VISIBILITY
569 vector(initializer_list<value_type> __il, const allocator_type& __a);
570
Howard Hinnantcf823322010-12-17 14:46:43 +0000571 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000572 vector(vector&& __x)
Marshall Clowe5108202015-07-14 14:46:32 +0000573#if _LIBCPP_STD_VER > 14
574 _NOEXCEPT;
575#else
Howard Hinnant1c936782011-06-03 19:40:40 +0000576 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowe5108202015-07-14 14:46:32 +0000577#endif
Eric Fiseliered9e9362017-04-16 02:40:45 +0000578
Howard Hinnantcf823322010-12-17 14:46:43 +0000579 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000580 vector(vector&& __x, const allocator_type& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000581 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000582 vector& operator=(vector&& __x)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000583 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiseliered9e9362017-04-16 02:40:45 +0000584
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000585 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000586 vector& operator=(initializer_list<value_type> __il)
587 {assign(__il.begin(), __il.end()); return *this;}
Eric Fiseliered9e9362017-04-16 02:40:45 +0000588
589#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000590
591 template <class _InputIterator>
592 typename enable_if
593 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500594 __is_cpp17_input_iterator <_InputIterator>::value &&
595 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000596 is_constructible<
597 value_type,
598 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000599 void
600 >::type
601 assign(_InputIterator __first, _InputIterator __last);
602 template <class _ForwardIterator>
603 typename enable_if
604 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500605 __is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000606 is_constructible<
607 value_type,
608 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000609 void
610 >::type
611 assign(_ForwardIterator __first, _ForwardIterator __last);
612
613 void assign(size_type __n, const_reference __u);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000614
615#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000616 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000617 void assign(initializer_list<value_type> __il)
618 {assign(__il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +0000619#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000620
Howard Hinnant1c936782011-06-03 19:40:40 +0000621 _LIBCPP_INLINE_VISIBILITY
622 allocator_type get_allocator() const _NOEXCEPT
623 {return this->__alloc();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000624
Howard Hinnant1c936782011-06-03 19:40:40 +0000625 _LIBCPP_INLINE_VISIBILITY iterator begin() _NOEXCEPT;
626 _LIBCPP_INLINE_VISIBILITY const_iterator begin() const _NOEXCEPT;
627 _LIBCPP_INLINE_VISIBILITY iterator end() _NOEXCEPT;
628 _LIBCPP_INLINE_VISIBILITY const_iterator end() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000629
Howard Hinnant1c936782011-06-03 19:40:40 +0000630 _LIBCPP_INLINE_VISIBILITY
631 reverse_iterator rbegin() _NOEXCEPT
632 {return reverse_iterator(end());}
633 _LIBCPP_INLINE_VISIBILITY
634 const_reverse_iterator rbegin() const _NOEXCEPT
635 {return const_reverse_iterator(end());}
636 _LIBCPP_INLINE_VISIBILITY
637 reverse_iterator rend() _NOEXCEPT
638 {return reverse_iterator(begin());}
639 _LIBCPP_INLINE_VISIBILITY
640 const_reverse_iterator rend() const _NOEXCEPT
641 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000642
Howard Hinnant1c936782011-06-03 19:40:40 +0000643 _LIBCPP_INLINE_VISIBILITY
644 const_iterator cbegin() const _NOEXCEPT
645 {return begin();}
646 _LIBCPP_INLINE_VISIBILITY
647 const_iterator cend() const _NOEXCEPT
648 {return end();}
649 _LIBCPP_INLINE_VISIBILITY
650 const_reverse_iterator crbegin() const _NOEXCEPT
651 {return rbegin();}
652 _LIBCPP_INLINE_VISIBILITY
653 const_reverse_iterator crend() const _NOEXCEPT
654 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000655
Howard Hinnant1c936782011-06-03 19:40:40 +0000656 _LIBCPP_INLINE_VISIBILITY
657 size_type size() const _NOEXCEPT
658 {return static_cast<size_type>(this->__end_ - this->__begin_);}
659 _LIBCPP_INLINE_VISIBILITY
660 size_type capacity() const _NOEXCEPT
661 {return __base::capacity();}
Marshall Clow425f5752017-11-15 05:51:26 +0000662 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000663 bool empty() const _NOEXCEPT
664 {return this->__begin_ == this->__end_;}
665 size_type max_size() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000666 void reserve(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +0000667 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000668
Marshall Clowd6470492019-03-15 00:29:35 +0000669 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) _NOEXCEPT;
670 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000671 reference at(size_type __n);
672 const_reference at(size_type __n) const;
673
Marshall Clowd6470492019-03-15 00:29:35 +0000674 _LIBCPP_INLINE_VISIBILITY reference front() _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000675 {
676 _LIBCPP_ASSERT(!empty(), "front() called for empty vector");
677 return *this->__begin_;
678 }
Marshall Clowd6470492019-03-15 00:29:35 +0000679 _LIBCPP_INLINE_VISIBILITY const_reference front() const _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000680 {
681 _LIBCPP_ASSERT(!empty(), "front() called for empty vector");
682 return *this->__begin_;
683 }
Marshall Clowd6470492019-03-15 00:29:35 +0000684 _LIBCPP_INLINE_VISIBILITY reference back() _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000685 {
686 _LIBCPP_ASSERT(!empty(), "back() called for empty vector");
687 return *(this->__end_ - 1);
688 }
Marshall Clowd6470492019-03-15 00:29:35 +0000689 _LIBCPP_INLINE_VISIBILITY const_reference back() const _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000690 {
691 _LIBCPP_ASSERT(!empty(), "back() called for empty vector");
692 return *(this->__end_ - 1);
693 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000694
Howard Hinnant1c936782011-06-03 19:40:40 +0000695 _LIBCPP_INLINE_VISIBILITY
696 value_type* data() _NOEXCEPT
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500697 {return _VSTD::__to_address(this->__begin_);}
Howard Hinnant1c936782011-06-03 19:40:40 +0000698 _LIBCPP_INLINE_VISIBILITY
699 const value_type* data() const _NOEXCEPT
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500700 {return _VSTD::__to_address(this->__begin_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000701
Eric Fiselier96919722017-10-17 13:03:17 +0000702#ifdef _LIBCPP_CXX03_LANG
703 _LIBCPP_INLINE_VISIBILITY
704 void __emplace_back(const value_type& __x) { push_back(__x); }
705#else
706 template <class _Arg>
707 _LIBCPP_INLINE_VISIBILITY
708 void __emplace_back(_Arg&& __arg) {
709 emplace_back(_VSTD::forward<_Arg>(__arg));
710 }
711#endif
712
Howard Hinnantcf823322010-12-17 14:46:43 +0000713 _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000714
715#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000716 _LIBCPP_INLINE_VISIBILITY void push_back(value_type&& __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000717
Howard Hinnantc51e1022010-05-11 19:42:16 +0000718 template <class... _Args>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000719 _LIBCPP_INLINE_VISIBILITY
Marshall Clowea52cc42017-01-24 23:09:12 +0000720#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +0000721 reference emplace_back(_Args&&... __args);
Marshall Clowea52cc42017-01-24 23:09:12 +0000722#else
723 void emplace_back(_Args&&... __args);
724#endif
Eric Fiseliered9e9362017-04-16 02:40:45 +0000725#endif // !_LIBCPP_CXX03_LANG
726
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000727 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000728 void pop_back();
729
730 iterator insert(const_iterator __position, const_reference __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000731
732#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000733 iterator insert(const_iterator __position, value_type&& __x);
734 template <class... _Args>
735 iterator emplace(const_iterator __position, _Args&&... __args);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000736#endif // !_LIBCPP_CXX03_LANG
737
Howard Hinnantc51e1022010-05-11 19:42:16 +0000738 iterator insert(const_iterator __position, size_type __n, const_reference __x);
739 template <class _InputIterator>
740 typename enable_if
741 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500742 __is_cpp17_input_iterator <_InputIterator>::value &&
743 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000744 is_constructible<
745 value_type,
746 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000747 iterator
748 >::type
749 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
750 template <class _ForwardIterator>
751 typename enable_if
752 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500753 __is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000754 is_constructible<
755 value_type,
756 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000757 iterator
758 >::type
759 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000760
761#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000762 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000763 iterator insert(const_iterator __position, initializer_list<value_type> __il)
764 {return insert(__position, __il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +0000765#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000766
Howard Hinnantcf823322010-12-17 14:46:43 +0000767 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000768 iterator erase(const_iterator __first, const_iterator __last);
769
Howard Hinnant1c936782011-06-03 19:40:40 +0000770 _LIBCPP_INLINE_VISIBILITY
771 void clear() _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000772 {
Marshall Clow2cd9d372014-05-08 14:14:06 +0000773 size_type __old_size = size();
Howard Hinnant27e0e772011-09-14 18:33:51 +0000774 __base::clear();
Marshall Clow2cd9d372014-05-08 14:14:06 +0000775 __annotate_shrink(__old_size);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000776 __invalidate_all_iterators();
777 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000778
779 void resize(size_type __sz);
780 void resize(size_type __sz, const_reference __x);
781
Howard Hinnant1c936782011-06-03 19:40:40 +0000782 void swap(vector&)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000783#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +0000784 _NOEXCEPT;
Marshall Clow8982dcd2015-07-13 20:04:56 +0000785#else
Eric Fiselier873b8d32019-03-18 21:50:12 +0000786 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +0000787 __is_nothrow_swappable<allocator_type>::value);
788#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000789
790 bool __invariants() const;
791
Howard Hinnanta47c6d52011-09-16 17:29:17 +0000792#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000793
794 bool __dereferenceable(const const_iterator* __i) const;
795 bool __decrementable(const const_iterator* __i) const;
796 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
797 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
798
Howard Hinnanta47c6d52011-09-16 17:29:17 +0000799#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000800
Howard Hinnantc51e1022010-05-11 19:42:16 +0000801private:
Howard Hinnantcf823322010-12-17 14:46:43 +0000802 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Eric Fiselier69c51982016-12-28 06:06:09 +0000803 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(pointer __new_last);
Marshall Clow3ff48e02018-05-22 16:20:28 +0000804 void __vallocate(size_type __n);
805 void __vdeallocate() _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000806 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
Howard Hinnanta5589382011-01-04 19:53:31 +0000807 void __construct_at_end(size_type __n);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000808 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000809 void __construct_at_end(size_type __n, const_reference __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000810 template <class _ForwardIterator>
811 typename enable_if
812 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500813 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000814 void
815 >::type
Eric Fiselieracfe6f02015-03-31 16:54:19 +0000816 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000817 void __append(size_type __n);
818 void __append(size_type __n, const_reference __x);
Howard Hinnantcf823322010-12-17 14:46:43 +0000819 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000820 iterator __make_iter(pointer __p) _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000821 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000822 const_iterator __make_iter(const_pointer __p) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000823 void __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v);
824 pointer __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p);
825 void __move_range(pointer __from_s, pointer __from_e, pointer __to);
Howard Hinnant1c936782011-06-03 19:40:40 +0000826 void __move_assign(vector& __c, true_type)
827 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000828 void __move_assign(vector& __c, false_type)
829 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000830 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76053d72013-06-27 19:35:32 +0000831 void __destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000832 {
Eric Fiselier69c51982016-12-28 06:06:09 +0000833 __invalidate_iterators_past(__new_last);
Marshall Clow2cd9d372014-05-08 14:14:06 +0000834 size_type __old_size = size();
Howard Hinnant27e0e772011-09-14 18:33:51 +0000835 __base::__destruct_at_end(__new_last);
Marshall Clow2cd9d372014-05-08 14:14:06 +0000836 __annotate_shrink(__old_size);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000837 }
Eric Fiseliered9e9362017-04-16 02:40:45 +0000838
839#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier7110f442019-03-19 19:19:44 +0000840 template <class _Up>
841 _LIBCPP_INLINE_VISIBILITY
842 inline void __push_back_slow_path(_Up&& __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000843
Howard Hinnantb6c49562012-02-26 15:30:12 +0000844 template <class... _Args>
Eric Fiselier7110f442019-03-19 19:19:44 +0000845 _LIBCPP_INLINE_VISIBILITY
846 inline void __emplace_back_slow_path(_Args&&... __args);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000847#else
Eric Fiselier7110f442019-03-19 19:19:44 +0000848 template <class _Up>
849 _LIBCPP_INLINE_VISIBILITY
850 inline void __push_back_slow_path(_Up& __x);
Howard Hinnantb6c49562012-02-26 15:30:12 +0000851#endif
Eric Fiseliered9e9362017-04-16 02:40:45 +0000852
Marshall Clow2cd9d372014-05-08 14:14:06 +0000853 // The following functions are no-ops outside of AddressSanitizer mode.
854 // We call annotatations only for the default Allocator because other allocators
855 // may not meet the AddressSanitizer alignment constraints.
856 // See the documentation for __sanitizer_annotate_contiguous_container for more details.
Marshall Clow2cd9d372014-05-08 14:14:06 +0000857#ifndef _LIBCPP_HAS_NO_ASAN
Eric Fiselier6003c772016-12-23 23:37:52 +0000858 void __annotate_contiguous_container(const void *__beg, const void *__end,
859 const void *__old_mid,
860 const void *__new_mid) const
861 {
862
Marshall Clow2cd9d372014-05-08 14:14:06 +0000863 if (__beg && is_same<allocator_type, __default_allocator_type>::value)
864 __sanitizer_annotate_contiguous_container(__beg, __end, __old_mid, __new_mid);
Marshall Clow2cd9d372014-05-08 14:14:06 +0000865 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000866#else
867 _LIBCPP_INLINE_VISIBILITY
868 void __annotate_contiguous_container(const void*, const void*, const void*,
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000869 const void*) const _NOEXCEPT {}
Eric Fiselier6003c772016-12-23 23:37:52 +0000870#endif
871 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000872 void __annotate_new(size_type __current_size) const _NOEXCEPT {
Marshall Clow2cd9d372014-05-08 14:14:06 +0000873 __annotate_contiguous_container(data(), data() + capacity(),
874 data() + capacity(), data() + __current_size);
875 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000876
877 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000878 void __annotate_delete() const _NOEXCEPT {
Marshall Clow2cd9d372014-05-08 14:14:06 +0000879 __annotate_contiguous_container(data(), data() + capacity(),
880 data() + size(), data() + capacity());
881 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000882
883 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000884 void __annotate_increase(size_type __n) const _NOEXCEPT
Marshall Clow2cd9d372014-05-08 14:14:06 +0000885 {
886 __annotate_contiguous_container(data(), data() + capacity(),
887 data() + size(), data() + size() + __n);
888 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000889
890 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000891 void __annotate_shrink(size_type __old_size) const _NOEXCEPT
Marshall Clow2cd9d372014-05-08 14:14:06 +0000892 {
893 __annotate_contiguous_container(data(), data() + capacity(),
894 data() + __old_size, data() + size());
895 }
Marshall Clowc78ffa52014-09-03 21:37:43 +0000896
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000897 struct _ConstructTransaction {
898 explicit _ConstructTransaction(vector &__v, size_type __n)
899 : __v_(__v), __pos_(__v.__end_), __new_end_(__v.__end_ + __n) {
900#ifndef _LIBCPP_HAS_NO_ASAN
901 __v_.__annotate_increase(__n);
902#endif
903 }
904 ~_ConstructTransaction() {
905 __v_.__end_ = __pos_;
906#ifndef _LIBCPP_HAS_NO_ASAN
907 if (__pos_ != __new_end_) {
908 __v_.__annotate_shrink(__new_end_ - __v_.__begin_);
909 }
910#endif
911 }
912
913 vector &__v_;
914 pointer __pos_;
915 const_pointer const __new_end_;
916
917 private:
918 _ConstructTransaction(_ConstructTransaction const&) = delete;
919 _ConstructTransaction& operator=(_ConstructTransaction const&) = delete;
920 };
921
922 template <class ..._Args>
923 _LIBCPP_INLINE_VISIBILITY
924 void __construct_one_at_end(_Args&& ...__args) {
925 _ConstructTransaction __tx(*this, 1);
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500926 __alloc_traits::construct(this->__alloc(), _VSTD::__to_address(__tx.__pos_),
Eric Fiseliere7afbd32019-07-28 04:37:02 +0000927 _VSTD::forward<_Args>(__args)...);
928 ++__tx.__pos_;
929 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000930};
931
Marshall Clowf0ca1492018-05-21 21:30:12 +0000932#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
933template<class _InputIterator,
934 class _Alloc = typename std::allocator<typename iterator_traits<_InputIterator>::value_type>,
935 class = typename enable_if<__is_allocator<_Alloc>::value, void>::type
936 >
937vector(_InputIterator, _InputIterator)
938 -> vector<typename iterator_traits<_InputIterator>::value_type, _Alloc>;
939
940template<class _InputIterator,
941 class _Alloc,
942 class = typename enable_if<__is_allocator<_Alloc>::value, void>::type
943 >
944vector(_InputIterator, _InputIterator, _Alloc)
945 -> vector<typename iterator_traits<_InputIterator>::value_type, _Alloc>;
946#endif
947
Howard Hinnantc51e1022010-05-11 19:42:16 +0000948template <class _Tp, class _Allocator>
949void
950vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v)
951{
Eric Fiselier909fe962019-09-13 16:09:33 +0000952
Marshall Clow2cd9d372014-05-08 14:14:06 +0000953 __annotate_delete();
Eric Fiselier909fe962019-09-13 16:09:33 +0000954 __alloc_traits::__construct_backward_with_exception_guarantees(
955 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_;
Eric Fiselier909fe962019-09-13 16:09:33 +0000970 __alloc_traits::__construct_backward_with_exception_guarantees(
971 this->__alloc(), this->__begin_, __p, __v.__begin_);
972 __alloc_traits::__construct_forward_with_exception_guarantees(
973 this->__alloc(), __p, this->__end_, __v.__end_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000974 _VSTD::swap(this->__begin_, __v.__begin_);
975 _VSTD::swap(this->__end_, __v.__end_);
976 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000977 __v.__first_ = __v.__begin_;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000978 __annotate_new(size());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000979 __invalidate_all_iterators();
980 return __r;
981}
982
983// Allocate space for __n objects
984// throws length_error if __n > max_size()
985// throws (probably bad_alloc) if memory run out
986// Precondition: __begin_ == __end_ == __end_cap() == 0
987// Precondition: __n > 0
988// Postcondition: capacity() == __n
989// Postcondition: size() == 0
990template <class _Tp, class _Allocator>
991void
Marshall Clow3ff48e02018-05-22 16:20:28 +0000992vector<_Tp, _Allocator>::__vallocate(size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000993{
994 if (__n > max_size())
995 this->__throw_length_error();
996 this->__begin_ = this->__end_ = __alloc_traits::allocate(this->__alloc(), __n);
997 this->__end_cap() = this->__begin_ + __n;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000998 __annotate_new(0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000999}
1000
1001template <class _Tp, class _Allocator>
1002void
Marshall Clow3ff48e02018-05-22 16:20:28 +00001003vector<_Tp, _Allocator>::__vdeallocate() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001004{
Howard Hinnant76053d72013-06-27 19:35:32 +00001005 if (this->__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001006 {
1007 clear();
1008 __alloc_traits::deallocate(this->__alloc(), this->__begin_, capacity());
Howard Hinnant76053d72013-06-27 19:35:32 +00001009 this->__begin_ = this->__end_ = this->__end_cap() = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001010 }
1011}
1012
1013template <class _Tp, class _Allocator>
1014typename vector<_Tp, _Allocator>::size_type
Howard Hinnant1c936782011-06-03 19:40:40 +00001015vector<_Tp, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001016{
Eric Fiselierb5d9f442016-11-23 01:18:56 +00001017 return _VSTD::min<size_type>(__alloc_traits::max_size(this->__alloc()),
1018 numeric_limits<difference_type>::max());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001019}
1020
1021// Precondition: __new_size > capacity()
1022template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001023inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001024typename vector<_Tp, _Allocator>::size_type
1025vector<_Tp, _Allocator>::__recommend(size_type __new_size) const
1026{
1027 const size_type __ms = max_size();
1028 if (__new_size > __ms)
1029 this->__throw_length_error();
1030 const size_type __cap = capacity();
1031 if (__cap >= __ms / 2)
1032 return __ms;
Alexis Hunt991d29b2011-07-29 23:31:58 +00001033 return _VSTD::max<size_type>(2*__cap, __new_size);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001034}
1035
1036// Default constructs __n objects starting at __end_
1037// throws if construction throws
1038// Precondition: __n > 0
1039// Precondition: size() + __n <= capacity()
1040// Postcondition: size() == size() + __n
1041template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001042void
1043vector<_Tp, _Allocator>::__construct_at_end(size_type __n)
1044{
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001045 _ConstructTransaction __tx(*this, __n);
Martijn Vels1e2e1b82020-06-18 13:14:02 -04001046 const_pointer __new_end = __tx.__new_end_;
1047 for (pointer __pos = __tx.__pos_; __pos != __new_end; ++__pos, __tx.__pos_ = __pos) {
1048 __alloc_traits::construct(this->__alloc(), _VSTD::__to_address(__pos));
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001049 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001050}
1051
Howard Hinnantc51e1022010-05-11 19:42:16 +00001052// Copy constructs __n objects starting at __end_ from __x
1053// throws if construction throws
1054// Precondition: __n > 0
1055// Precondition: size() + __n <= capacity()
1056// Postcondition: size() == old size() + __n
1057// Postcondition: [i] == __x for all i in [size() - __n, __n)
1058template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001059inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001060void
1061vector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
1062{
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001063 _ConstructTransaction __tx(*this, __n);
Martijn Vels1e2e1b82020-06-18 13:14:02 -04001064 const_pointer __new_end = __tx.__new_end_;
1065 for (pointer __pos = __tx.__pos_; __pos != __new_end; ++__pos, __tx.__pos_ = __pos) {
1066 __alloc_traits::construct(this->__alloc(), _VSTD::__to_address(__pos), __x);
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001067 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001068}
1069
1070template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001071template <class _ForwardIterator>
1072typename enable_if
1073<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001074 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001075 void
1076>::type
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001077vector<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001078{
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001079 _ConstructTransaction __tx(*this, __n);
1080 __alloc_traits::__construct_range_forward(this->__alloc(), __first, __last, __tx.__pos_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001081}
1082
1083// Default constructs __n objects starting at __end_
1084// throws if construction throws
1085// Postcondition: size() == size() + __n
Howard Hinnant1c936782011-06-03 19:40:40 +00001086// Exception safety: strong.
Howard Hinnantc51e1022010-05-11 19:42:16 +00001087template <class _Tp, class _Allocator>
1088void
1089vector<_Tp, _Allocator>::__append(size_type __n)
1090{
1091 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1092 this->__construct_at_end(__n);
1093 else
1094 {
1095 allocator_type& __a = this->__alloc();
1096 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1097 __v.__construct_at_end(__n);
1098 __swap_out_circular_buffer(__v);
1099 }
1100}
1101
1102// Default constructs __n objects starting at __end_
1103// throws if construction throws
1104// Postcondition: size() == size() + __n
Howard Hinnant1c936782011-06-03 19:40:40 +00001105// Exception safety: strong.
Howard Hinnantc51e1022010-05-11 19:42:16 +00001106template <class _Tp, class _Allocator>
1107void
1108vector<_Tp, _Allocator>::__append(size_type __n, const_reference __x)
1109{
1110 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1111 this->__construct_at_end(__n, __x);
1112 else
1113 {
1114 allocator_type& __a = this->__alloc();
1115 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1116 __v.__construct_at_end(__n, __x);
1117 __swap_out_circular_buffer(__v);
1118 }
1119}
1120
1121template <class _Tp, class _Allocator>
1122vector<_Tp, _Allocator>::vector(size_type __n)
1123{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001124#if _LIBCPP_DEBUG_LEVEL >= 2
1125 __get_db()->__insert_c(this);
1126#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001127 if (__n > 0)
1128 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001129 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001130 __construct_at_end(__n);
1131 }
1132}
1133
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00001134#if _LIBCPP_STD_VER > 11
1135template <class _Tp, class _Allocator>
1136vector<_Tp, _Allocator>::vector(size_type __n, const allocator_type& __a)
1137 : __base(__a)
1138{
1139#if _LIBCPP_DEBUG_LEVEL >= 2
1140 __get_db()->__insert_c(this);
1141#endif
1142 if (__n > 0)
1143 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001144 __vallocate(__n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00001145 __construct_at_end(__n);
1146 }
1147}
1148#endif
1149
Howard Hinnantc51e1022010-05-11 19:42:16 +00001150template <class _Tp, class _Allocator>
Marshall Clowf0ca1492018-05-21 21:30:12 +00001151vector<_Tp, _Allocator>::vector(size_type __n, const value_type& __x)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001152{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001153#if _LIBCPP_DEBUG_LEVEL >= 2
1154 __get_db()->__insert_c(this);
1155#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001156 if (__n > 0)
1157 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001158 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001159 __construct_at_end(__n, __x);
1160 }
1161}
1162
1163template <class _Tp, class _Allocator>
Marshall Clowf0ca1492018-05-21 21:30:12 +00001164vector<_Tp, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001165 : __base(__a)
1166{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001167#if _LIBCPP_DEBUG_LEVEL >= 2
1168 __get_db()->__insert_c(this);
1169#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001170 if (__n > 0)
1171 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001172 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001173 __construct_at_end(__n, __x);
1174 }
1175}
1176
1177template <class _Tp, class _Allocator>
1178template <class _InputIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +00001179vector<_Tp, _Allocator>::vector(_InputIterator __first,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001180 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
1181 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001182 is_constructible<
1183 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +00001184 typename iterator_traits<_InputIterator>::reference>::value,
1185 _InputIterator>::type __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001186{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001187#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001188 __get_db()->__insert_c(this);
1189#endif
Howard Hinnant9cd22302011-09-16 18:41:29 +00001190 for (; __first != __last; ++__first)
Eric Fiselier96919722017-10-17 13:03:17 +00001191 __emplace_back(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001192}
1193
1194template <class _Tp, class _Allocator>
1195template <class _InputIterator>
1196vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001197 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
1198 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001199 is_constructible<
1200 value_type,
1201 typename iterator_traits<_InputIterator>::reference>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001202 : __base(__a)
1203{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001204#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001205 __get_db()->__insert_c(this);
1206#endif
Howard Hinnant9cd22302011-09-16 18:41:29 +00001207 for (; __first != __last; ++__first)
Eric Fiselier96919722017-10-17 13:03:17 +00001208 __emplace_back(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001209}
1210
1211template <class _Tp, class _Allocator>
1212template <class _ForwardIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +00001213vector<_Tp, _Allocator>::vector(_ForwardIterator __first,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001214 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001215 is_constructible<
1216 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +00001217 typename iterator_traits<_ForwardIterator>::reference>::value,
1218 _ForwardIterator>::type __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001219{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001220#if _LIBCPP_DEBUG_LEVEL >= 2
1221 __get_db()->__insert_c(this);
1222#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001223 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001224 if (__n > 0)
1225 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001226 __vallocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001227 __construct_at_end(__first, __last, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001228 }
1229}
1230
1231template <class _Tp, class _Allocator>
1232template <class _ForwardIterator>
1233vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001234 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001235 is_constructible<
1236 value_type,
1237 typename iterator_traits<_ForwardIterator>::reference>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001238 : __base(__a)
1239{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001240#if _LIBCPP_DEBUG_LEVEL >= 2
1241 __get_db()->__insert_c(this);
1242#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001243 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001244 if (__n > 0)
1245 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001246 __vallocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001247 __construct_at_end(__first, __last, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001248 }
1249}
1250
1251template <class _Tp, class _Allocator>
1252vector<_Tp, _Allocator>::vector(const vector& __x)
1253 : __base(__alloc_traits::select_on_container_copy_construction(__x.__alloc()))
1254{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001255#if _LIBCPP_DEBUG_LEVEL >= 2
1256 __get_db()->__insert_c(this);
1257#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001258 size_type __n = __x.size();
1259 if (__n > 0)
1260 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001261 __vallocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001262 __construct_at_end(__x.__begin_, __x.__end_, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001263 }
1264}
1265
1266template <class _Tp, class _Allocator>
1267vector<_Tp, _Allocator>::vector(const vector& __x, const allocator_type& __a)
1268 : __base(__a)
1269{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001270#if _LIBCPP_DEBUG_LEVEL >= 2
1271 __get_db()->__insert_c(this);
1272#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001273 size_type __n = __x.size();
1274 if (__n > 0)
1275 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001276 __vallocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001277 __construct_at_end(__x.__begin_, __x.__end_, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001278 }
1279}
1280
Eric Fiseliered9e9362017-04-16 02:40:45 +00001281#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001282
1283template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001284inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001285vector<_Tp, _Allocator>::vector(vector&& __x)
Marshall Clowe5108202015-07-14 14:46:32 +00001286#if _LIBCPP_STD_VER > 14
1287 _NOEXCEPT
1288#else
Howard Hinnant1c936782011-06-03 19:40:40 +00001289 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowe5108202015-07-14 14:46:32 +00001290#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001291 : __base(_VSTD::move(__x.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001292{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001293#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001294 __get_db()->__insert_c(this);
Howard Hinnanta1e60cd2011-09-19 16:34:29 +00001295 __get_db()->swap(this, &__x);
Howard Hinnant27e0e772011-09-14 18:33:51 +00001296#endif
Howard Hinnant9cd22302011-09-16 18:41:29 +00001297 this->__begin_ = __x.__begin_;
1298 this->__end_ = __x.__end_;
1299 this->__end_cap() = __x.__end_cap();
Howard Hinnant76053d72013-06-27 19:35:32 +00001300 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001301}
1302
1303template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001304inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001305vector<_Tp, _Allocator>::vector(vector&& __x, const allocator_type& __a)
1306 : __base(__a)
1307{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001308#if _LIBCPP_DEBUG_LEVEL >= 2
1309 __get_db()->__insert_c(this);
1310#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001311 if (__a == __x.__alloc())
1312 {
1313 this->__begin_ = __x.__begin_;
1314 this->__end_ = __x.__end_;
1315 this->__end_cap() = __x.__end_cap();
1316 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnanta1e60cd2011-09-19 16:34:29 +00001317#if _LIBCPP_DEBUG_LEVEL >= 2
1318 __get_db()->swap(this, &__x);
1319#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001320 }
1321 else
1322 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001323 typedef move_iterator<iterator> _Ip;
1324 assign(_Ip(__x.begin()), _Ip(__x.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001325 }
1326}
1327
1328template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001329inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001330vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il)
1331{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001332#if _LIBCPP_DEBUG_LEVEL >= 2
1333 __get_db()->__insert_c(this);
1334#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001335 if (__il.size() > 0)
1336 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001337 __vallocate(__il.size());
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001338 __construct_at_end(__il.begin(), __il.end(), __il.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001339 }
1340}
1341
1342template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001343inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001344vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
1345 : __base(__a)
1346{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001347#if _LIBCPP_DEBUG_LEVEL >= 2
1348 __get_db()->__insert_c(this);
1349#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001350 if (__il.size() > 0)
1351 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001352 __vallocate(__il.size());
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001353 __construct_at_end(__il.begin(), __il.end(), __il.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001354 }
1355}
1356
1357template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001358inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001359vector<_Tp, _Allocator>&
1360vector<_Tp, _Allocator>::operator=(vector&& __x)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001361 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001362{
1363 __move_assign(__x, integral_constant<bool,
1364 __alloc_traits::propagate_on_container_move_assignment::value>());
1365 return *this;
1366}
1367
1368template <class _Tp, class _Allocator>
1369void
1370vector<_Tp, _Allocator>::__move_assign(vector& __c, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001371 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001372{
1373 if (__base::__alloc() != __c.__alloc())
1374 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001375 typedef move_iterator<iterator> _Ip;
1376 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001377 }
1378 else
1379 __move_assign(__c, true_type());
1380}
1381
1382template <class _Tp, class _Allocator>
1383void
1384vector<_Tp, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00001385 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001386{
Marshall Clow3ff48e02018-05-22 16:20:28 +00001387 __vdeallocate();
Marshall Clow136d45c2014-07-21 15:11:13 +00001388 __base::__move_assign_alloc(__c); // this can throw
Howard Hinnantc51e1022010-05-11 19:42:16 +00001389 this->__begin_ = __c.__begin_;
1390 this->__end_ = __c.__end_;
1391 this->__end_cap() = __c.__end_cap();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001392 __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
Howard Hinnanta1e60cd2011-09-19 16:34:29 +00001393#if _LIBCPP_DEBUG_LEVEL >= 2
1394 __get_db()->swap(this, &__c);
1395#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001396}
1397
Eric Fiseliered9e9362017-04-16 02:40:45 +00001398#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001399
1400template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001401inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001402vector<_Tp, _Allocator>&
1403vector<_Tp, _Allocator>::operator=(const vector& __x)
1404{
1405 if (this != &__x)
1406 {
1407 __base::__copy_assign_alloc(__x);
1408 assign(__x.__begin_, __x.__end_);
1409 }
1410 return *this;
1411}
1412
1413template <class _Tp, class _Allocator>
1414template <class _InputIterator>
1415typename enable_if
1416<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001417 __is_cpp17_input_iterator <_InputIterator>::value &&
1418 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001419 is_constructible<
1420 _Tp,
1421 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001422 void
1423>::type
1424vector<_Tp, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
1425{
1426 clear();
1427 for (; __first != __last; ++__first)
Eric Fiselier96919722017-10-17 13:03:17 +00001428 __emplace_back(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001429}
1430
1431template <class _Tp, class _Allocator>
1432template <class _ForwardIterator>
1433typename enable_if
1434<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001435 __is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001436 is_constructible<
1437 _Tp,
1438 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001439 void
1440>::type
1441vector<_Tp, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
1442{
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001443 size_type __new_size = static_cast<size_type>(_VSTD::distance(__first, __last));
1444 if (__new_size <= capacity())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001445 {
1446 _ForwardIterator __mid = __last;
1447 bool __growing = false;
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001448 if (__new_size > size())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001449 {
1450 __growing = true;
1451 __mid = __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001452 _VSTD::advance(__mid, size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001453 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001454 pointer __m = _VSTD::copy(__first, __mid, this->__begin_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001455 if (__growing)
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001456 __construct_at_end(__mid, __last, __new_size - size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001457 else
1458 this->__destruct_at_end(__m);
1459 }
1460 else
1461 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001462 __vdeallocate();
1463 __vallocate(__recommend(__new_size));
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001464 __construct_at_end(__first, __last, __new_size);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001465 }
Eric Fiselier69c51982016-12-28 06:06:09 +00001466 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001467}
1468
1469template <class _Tp, class _Allocator>
1470void
1471vector<_Tp, _Allocator>::assign(size_type __n, const_reference __u)
1472{
1473 if (__n <= capacity())
1474 {
1475 size_type __s = size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001476 _VSTD::fill_n(this->__begin_, _VSTD::min(__n, __s), __u);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001477 if (__n > __s)
1478 __construct_at_end(__n - __s, __u);
1479 else
Howard Hinnant155c2af2010-05-24 17:49:41 +00001480 this->__destruct_at_end(this->__begin_ + __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001481 }
1482 else
1483 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00001484 __vdeallocate();
1485 __vallocate(__recommend(static_cast<size_type>(__n)));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001486 __construct_at_end(__n, __u);
1487 }
Eric Fiselier69c51982016-12-28 06:06:09 +00001488 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001489}
1490
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001491template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001492inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001493typename vector<_Tp, _Allocator>::iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001494vector<_Tp, _Allocator>::__make_iter(pointer __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001495{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001496#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001497 return iterator(this, __p);
1498#else
1499 return iterator(__p);
1500#endif
1501}
1502
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001503template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001504inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001505typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001506vector<_Tp, _Allocator>::__make_iter(const_pointer __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001507{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001508#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001509 return const_iterator(this, __p);
1510#else
1511 return const_iterator(__p);
1512#endif
1513}
1514
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001515template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001516inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001517typename vector<_Tp, _Allocator>::iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001518vector<_Tp, _Allocator>::begin() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001519{
1520 return __make_iter(this->__begin_);
1521}
1522
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001523template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001524inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001525typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001526vector<_Tp, _Allocator>::begin() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001527{
1528 return __make_iter(this->__begin_);
1529}
1530
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001531template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001532inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001533typename vector<_Tp, _Allocator>::iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001534vector<_Tp, _Allocator>::end() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001535{
1536 return __make_iter(this->__end_);
1537}
1538
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001539template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001540inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001541typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001542vector<_Tp, _Allocator>::end() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001543{
1544 return __make_iter(this->__end_);
1545}
1546
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001547template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001548inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001549typename vector<_Tp, _Allocator>::reference
Marshall Clowd6470492019-03-15 00:29:35 +00001550vector<_Tp, _Allocator>::operator[](size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001551{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001552 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001553 return this->__begin_[__n];
1554}
1555
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001556template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001557inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001558typename vector<_Tp, _Allocator>::const_reference
Marshall Clowd6470492019-03-15 00:29:35 +00001559vector<_Tp, _Allocator>::operator[](size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001560{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001561 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001562 return this->__begin_[__n];
1563}
1564
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001565template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001566typename vector<_Tp, _Allocator>::reference
1567vector<_Tp, _Allocator>::at(size_type __n)
1568{
1569 if (__n >= size())
1570 this->__throw_out_of_range();
1571 return this->__begin_[__n];
1572}
1573
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001574template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001575typename vector<_Tp, _Allocator>::const_reference
1576vector<_Tp, _Allocator>::at(size_type __n) const
1577{
1578 if (__n >= size())
1579 this->__throw_out_of_range();
1580 return this->__begin_[__n];
1581}
1582
1583template <class _Tp, class _Allocator>
1584void
1585vector<_Tp, _Allocator>::reserve(size_type __n)
1586{
1587 if (__n > capacity())
1588 {
1589 allocator_type& __a = this->__alloc();
Howard Hinnant1c936782011-06-03 19:40:40 +00001590 __split_buffer<value_type, allocator_type&> __v(__n, size(), __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001591 __swap_out_circular_buffer(__v);
1592 }
1593}
1594
1595template <class _Tp, class _Allocator>
1596void
Howard Hinnant1c936782011-06-03 19:40:40 +00001597vector<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001598{
1599 if (capacity() > size())
1600 {
1601#ifndef _LIBCPP_NO_EXCEPTIONS
1602 try
1603 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001604#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001605 allocator_type& __a = this->__alloc();
Howard Hinnant1c936782011-06-03 19:40:40 +00001606 __split_buffer<value_type, allocator_type&> __v(size(), size(), __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001607 __swap_out_circular_buffer(__v);
1608#ifndef _LIBCPP_NO_EXCEPTIONS
1609 }
1610 catch (...)
1611 {
1612 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001613#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001614 }
1615}
1616
1617template <class _Tp, class _Allocator>
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001618template <class _Up>
1619void
Eric Fiseliered9e9362017-04-16 02:40:45 +00001620#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001621vector<_Tp, _Allocator>::__push_back_slow_path(_Up&& __x)
1622#else
1623vector<_Tp, _Allocator>::__push_back_slow_path(_Up& __x)
1624#endif
1625{
1626 allocator_type& __a = this->__alloc();
1627 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1628 // __v.push_back(_VSTD::forward<_Up>(__x));
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001629 __alloc_traits::construct(__a, _VSTD::__to_address(__v.__end_), _VSTD::forward<_Up>(__x));
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001630 __v.__end_++;
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001631 __swap_out_circular_buffer(__v);
1632}
1633
1634template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001635inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001636void
1637vector<_Tp, _Allocator>::push_back(const_reference __x)
1638{
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001639 if (this->__end_ != this->__end_cap())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001640 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001641 __construct_one_at_end(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001642 }
1643 else
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001644 __push_back_slow_path(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001645}
1646
Eric Fiseliered9e9362017-04-16 02:40:45 +00001647#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001648
1649template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001650inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001651void
1652vector<_Tp, _Allocator>::push_back(value_type&& __x)
1653{
1654 if (this->__end_ < this->__end_cap())
1655 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001656 __construct_one_at_end(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001657 }
1658 else
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001659 __push_back_slow_path(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001660}
1661
1662template <class _Tp, class _Allocator>
1663template <class... _Args>
1664void
Howard Hinnantb6c49562012-02-26 15:30:12 +00001665vector<_Tp, _Allocator>::__emplace_back_slow_path(_Args&&... __args)
1666{
1667 allocator_type& __a = this->__alloc();
1668 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1669// __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001670 __alloc_traits::construct(__a, _VSTD::__to_address(__v.__end_), _VSTD::forward<_Args>(__args)...);
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001671 __v.__end_++;
Howard Hinnantb6c49562012-02-26 15:30:12 +00001672 __swap_out_circular_buffer(__v);
1673}
1674
1675template <class _Tp, class _Allocator>
1676template <class... _Args>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001677inline
Marshall Clowea52cc42017-01-24 23:09:12 +00001678#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001679typename vector<_Tp, _Allocator>::reference
Marshall Clowea52cc42017-01-24 23:09:12 +00001680#else
1681void
1682#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001683vector<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1684{
1685 if (this->__end_ < this->__end_cap())
1686 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001687 __construct_one_at_end(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001688 }
1689 else
Howard Hinnantb6c49562012-02-26 15:30:12 +00001690 __emplace_back_slow_path(_VSTD::forward<_Args>(__args)...);
Marshall Clowea52cc42017-01-24 23:09:12 +00001691#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001692 return this->back();
Marshall Clowea52cc42017-01-24 23:09:12 +00001693#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001694}
1695
Eric Fiseliered9e9362017-04-16 02:40:45 +00001696#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001697
1698template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001699inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001700void
1701vector<_Tp, _Allocator>::pop_back()
1702{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001703 _LIBCPP_ASSERT(!empty(), "vector::pop_back called for empty vector");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001704 this->__destruct_at_end(this->__end_ - 1);
1705}
1706
1707template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001708inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001709typename vector<_Tp, _Allocator>::iterator
1710vector<_Tp, _Allocator>::erase(const_iterator __position)
1711{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001712#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001713 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1714 "vector::erase(iterator) called with an iterator not"
1715 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001716#endif
Howard Hinnant58d52d52013-03-25 22:12:26 +00001717 _LIBCPP_ASSERT(__position != end(),
1718 "vector::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnant76053d72013-06-27 19:35:32 +00001719 difference_type __ps = __position - cbegin();
1720 pointer __p = this->__begin_ + __ps;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001721 this->__destruct_at_end(_VSTD::move(__p + 1, this->__end_, __p));
Eric Fiselier69c51982016-12-28 06:06:09 +00001722 this->__invalidate_iterators_past(__p-1);
1723 iterator __r = __make_iter(__p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001724 return __r;
1725}
1726
1727template <class _Tp, class _Allocator>
1728typename vector<_Tp, _Allocator>::iterator
1729vector<_Tp, _Allocator>::erase(const_iterator __first, const_iterator __last)
1730{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001731#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001732 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
1733 "vector::erase(iterator, iterator) called with an iterator not"
1734 " referring to this vector");
Eric Fiselier69c51982016-12-28 06:06:09 +00001735 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__last) == this,
1736 "vector::erase(iterator, iterator) called with an iterator not"
1737 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001738#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001739 _LIBCPP_ASSERT(__first <= __last, "vector::erase(first, last) called with invalid range");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001740 pointer __p = this->__begin_ + (__first - begin());
Eric Fiselier69c51982016-12-28 06:06:09 +00001741 if (__first != __last) {
Howard Hinnantc580cc32013-04-18 15:02:57 +00001742 this->__destruct_at_end(_VSTD::move(__p + (__last - __first), this->__end_, __p));
Eric Fiselier69c51982016-12-28 06:06:09 +00001743 this->__invalidate_iterators_past(__p - 1);
1744 }
1745 iterator __r = __make_iter(__p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001746 return __r;
1747}
1748
1749template <class _Tp, class _Allocator>
1750void
1751vector<_Tp, _Allocator>::__move_range(pointer __from_s, pointer __from_e, pointer __to)
1752{
1753 pointer __old_last = this->__end_;
1754 difference_type __n = __old_last - __to;
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001755 {
1756 pointer __i = __from_s + __n;
1757 _ConstructTransaction __tx(*this, __from_e - __i);
Martijn Vels1e2e1b82020-06-18 13:14:02 -04001758 for (pointer __pos = __tx.__pos_; __i < __from_e;
1759 ++__i, ++__pos, __tx.__pos_ = __pos) {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001760 __alloc_traits::construct(this->__alloc(),
Martijn Vels1e2e1b82020-06-18 13:14:02 -04001761 _VSTD::__to_address(__pos),
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001762 _VSTD::move(*__i));
1763 }
1764 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001765 _VSTD::move_backward(__from_s, __from_s + __n, __old_last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001766}
1767
1768template <class _Tp, class _Allocator>
1769typename vector<_Tp, _Allocator>::iterator
1770vector<_Tp, _Allocator>::insert(const_iterator __position, const_reference __x)
1771{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001772#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001773 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1774 "vector::insert(iterator, x) called with an iterator not"
1775 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001776#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001777 pointer __p = this->__begin_ + (__position - begin());
1778 if (this->__end_ < this->__end_cap())
1779 {
1780 if (__p == this->__end_)
1781 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001782 __construct_one_at_end(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001783 }
1784 else
1785 {
1786 __move_range(__p, this->__end_, __p + 1);
1787 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1788 if (__p <= __xr && __xr < this->__end_)
1789 ++__xr;
1790 *__p = *__xr;
1791 }
1792 }
1793 else
1794 {
1795 allocator_type& __a = this->__alloc();
1796 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
1797 __v.push_back(__x);
1798 __p = __swap_out_circular_buffer(__v, __p);
1799 }
1800 return __make_iter(__p);
1801}
1802
Eric Fiseliered9e9362017-04-16 02:40:45 +00001803#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001804
1805template <class _Tp, class _Allocator>
1806typename vector<_Tp, _Allocator>::iterator
1807vector<_Tp, _Allocator>::insert(const_iterator __position, value_type&& __x)
1808{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001809#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001810 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1811 "vector::insert(iterator, x) called with an iterator not"
1812 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001813#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001814 pointer __p = this->__begin_ + (__position - begin());
1815 if (this->__end_ < this->__end_cap())
1816 {
1817 if (__p == this->__end_)
1818 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001819 __construct_one_at_end(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001820 }
1821 else
1822 {
1823 __move_range(__p, this->__end_, __p + 1);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001824 *__p = _VSTD::move(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001825 }
1826 }
1827 else
1828 {
1829 allocator_type& __a = this->__alloc();
1830 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001831 __v.push_back(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001832 __p = __swap_out_circular_buffer(__v, __p);
1833 }
1834 return __make_iter(__p);
1835}
1836
1837template <class _Tp, class _Allocator>
1838template <class... _Args>
1839typename vector<_Tp, _Allocator>::iterator
1840vector<_Tp, _Allocator>::emplace(const_iterator __position, _Args&&... __args)
1841{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001842#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001843 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1844 "vector::emplace(iterator, x) called with an iterator not"
1845 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001846#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001847 pointer __p = this->__begin_ + (__position - begin());
1848 if (this->__end_ < this->__end_cap())
1849 {
1850 if (__p == this->__end_)
1851 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001852 __construct_one_at_end(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001853 }
1854 else
1855 {
Marshall Clowa591b9a2016-07-11 21:38:08 +00001856 __temp_value<value_type, _Allocator> __tmp(this->__alloc(), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001857 __move_range(__p, this->__end_, __p + 1);
Marshall Clowa591b9a2016-07-11 21:38:08 +00001858 *__p = _VSTD::move(__tmp.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001859 }
1860 }
1861 else
1862 {
1863 allocator_type& __a = this->__alloc();
1864 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001865 __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001866 __p = __swap_out_circular_buffer(__v, __p);
1867 }
1868 return __make_iter(__p);
1869}
1870
Eric Fiseliered9e9362017-04-16 02:40:45 +00001871#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001872
1873template <class _Tp, class _Allocator>
1874typename vector<_Tp, _Allocator>::iterator
1875vector<_Tp, _Allocator>::insert(const_iterator __position, size_type __n, const_reference __x)
1876{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001877#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001878 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1879 "vector::insert(iterator, n, x) called with an iterator not"
1880 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001881#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001882 pointer __p = this->__begin_ + (__position - begin());
1883 if (__n > 0)
1884 {
1885 if (__n <= static_cast<size_type>(this->__end_cap() - this->__end_))
1886 {
1887 size_type __old_n = __n;
1888 pointer __old_last = this->__end_;
1889 if (__n > static_cast<size_type>(this->__end_ - __p))
1890 {
1891 size_type __cx = __n - (this->__end_ - __p);
1892 __construct_at_end(__cx, __x);
1893 __n -= __cx;
1894 }
1895 if (__n > 0)
1896 {
1897 __move_range(__p, __old_last, __p + __old_n);
1898 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1899 if (__p <= __xr && __xr < this->__end_)
1900 __xr += __old_n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001901 _VSTD::fill_n(__p, __n, *__xr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001902 }
1903 }
1904 else
1905 {
1906 allocator_type& __a = this->__alloc();
1907 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1908 __v.__construct_at_end(__n, __x);
1909 __p = __swap_out_circular_buffer(__v, __p);
1910 }
1911 }
1912 return __make_iter(__p);
1913}
1914
1915template <class _Tp, class _Allocator>
1916template <class _InputIterator>
1917typename enable_if
1918<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001919 __is_cpp17_input_iterator <_InputIterator>::value &&
1920 !__is_cpp17_forward_iterator<_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001921 is_constructible<
1922 _Tp,
1923 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001924 typename vector<_Tp, _Allocator>::iterator
1925>::type
1926vector<_Tp, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
1927{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001928#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001929 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1930 "vector::insert(iterator, range) called with an iterator not"
1931 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001932#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001933 difference_type __off = __position - begin();
1934 pointer __p = this->__begin_ + __off;
1935 allocator_type& __a = this->__alloc();
1936 pointer __old_last = this->__end_;
1937 for (; this->__end_ != this->__end_cap() && __first != __last; ++__first)
1938 {
Eric Fiseliere7afbd32019-07-28 04:37:02 +00001939 __construct_one_at_end(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001940 }
1941 __split_buffer<value_type, allocator_type&> __v(__a);
1942 if (__first != __last)
1943 {
1944#ifndef _LIBCPP_NO_EXCEPTIONS
1945 try
1946 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001947#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001948 __v.__construct_at_end(__first, __last);
1949 difference_type __old_size = __old_last - this->__begin_;
1950 difference_type __old_p = __p - this->__begin_;
1951 reserve(__recommend(size() + __v.size()));
1952 __p = this->__begin_ + __old_p;
1953 __old_last = this->__begin_ + __old_size;
1954#ifndef _LIBCPP_NO_EXCEPTIONS
1955 }
1956 catch (...)
1957 {
1958 erase(__make_iter(__old_last), end());
1959 throw;
1960 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001961#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001962 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001963 __p = _VSTD::rotate(__p, __old_last, this->__end_);
Louis Dionne202a4cb2020-02-11 10:59:12 +01001964 insert(__make_iter(__p), _VSTD::make_move_iterator(__v.begin()),
1965 _VSTD::make_move_iterator(__v.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001966 return begin() + __off;
1967}
1968
1969template <class _Tp, class _Allocator>
1970template <class _ForwardIterator>
1971typename enable_if
1972<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001973 __is_cpp17_forward_iterator<_ForwardIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001974 is_constructible<
1975 _Tp,
1976 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001977 typename vector<_Tp, _Allocator>::iterator
1978>::type
1979vector<_Tp, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
1980{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001981#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001982 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1983 "vector::insert(iterator, range) called with an iterator not"
1984 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001985#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001986 pointer __p = this->__begin_ + (__position - begin());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001987 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001988 if (__n > 0)
1989 {
1990 if (__n <= this->__end_cap() - this->__end_)
1991 {
1992 size_type __old_n = __n;
1993 pointer __old_last = this->__end_;
1994 _ForwardIterator __m = __last;
1995 difference_type __dx = this->__end_ - __p;
1996 if (__n > __dx)
1997 {
1998 __m = __first;
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001999 difference_type __diff = this->__end_ - __p;
2000 _VSTD::advance(__m, __diff);
2001 __construct_at_end(__m, __last, __n - __diff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002002 __n = __dx;
2003 }
2004 if (__n > 0)
2005 {
2006 __move_range(__p, __old_last, __p + __old_n);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002007 _VSTD::copy(__first, __m, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002008 }
2009 }
2010 else
2011 {
2012 allocator_type& __a = this->__alloc();
2013 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
2014 __v.__construct_at_end(__first, __last);
2015 __p = __swap_out_circular_buffer(__v, __p);
2016 }
2017 }
2018 return __make_iter(__p);
2019}
2020
2021template <class _Tp, class _Allocator>
2022void
2023vector<_Tp, _Allocator>::resize(size_type __sz)
2024{
2025 size_type __cs = size();
2026 if (__cs < __sz)
2027 this->__append(__sz - __cs);
2028 else if (__cs > __sz)
2029 this->__destruct_at_end(this->__begin_ + __sz);
2030}
2031
2032template <class _Tp, class _Allocator>
2033void
2034vector<_Tp, _Allocator>::resize(size_type __sz, const_reference __x)
2035{
2036 size_type __cs = size();
2037 if (__cs < __sz)
2038 this->__append(__sz - __cs, __x);
2039 else if (__cs > __sz)
2040 this->__destruct_at_end(this->__begin_ + __sz);
2041}
2042
2043template <class _Tp, class _Allocator>
2044void
2045vector<_Tp, _Allocator>::swap(vector& __x)
Marshall Clow8982dcd2015-07-13 20:04:56 +00002046#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00002047 _NOEXCEPT
Marshall Clow8982dcd2015-07-13 20:04:56 +00002048#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00002049 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00002050 __is_nothrow_swappable<allocator_type>::value)
2051#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002052{
Howard Hinnant27e0e772011-09-14 18:33:51 +00002053 _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
2054 this->__alloc() == __x.__alloc(),
2055 "vector::swap: Either propagate_on_container_swap must be true"
2056 " or the allocators must compare equal");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002057 _VSTD::swap(this->__begin_, __x.__begin_);
2058 _VSTD::swap(this->__end_, __x.__end_);
2059 _VSTD::swap(this->__end_cap(), __x.__end_cap());
Eric Fiseliered9e9362017-04-16 02:40:45 +00002060 __swap_allocator(this->__alloc(), __x.__alloc(),
Marshall Clow8982dcd2015-07-13 20:04:56 +00002061 integral_constant<bool,__alloc_traits::propagate_on_container_swap::value>());
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002062#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002063 __get_db()->swap(this, &__x);
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002064#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00002065}
2066
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002067template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002068bool
2069vector<_Tp, _Allocator>::__invariants() const
2070{
Howard Hinnant76053d72013-06-27 19:35:32 +00002071 if (this->__begin_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002072 {
Howard Hinnant76053d72013-06-27 19:35:32 +00002073 if (this->__end_ != nullptr || this->__end_cap() != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002074 return false;
2075 }
2076 else
2077 {
2078 if (this->__begin_ > this->__end_)
2079 return false;
2080 if (this->__begin_ == this->__end_cap())
2081 return false;
2082 if (this->__end_ > this->__end_cap())
2083 return false;
2084 }
2085 return true;
2086}
2087
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002088#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002089
Howard Hinnantc51e1022010-05-11 19:42:16 +00002090template <class _Tp, class _Allocator>
Howard Hinnant27e0e772011-09-14 18:33:51 +00002091bool
2092vector<_Tp, _Allocator>::__dereferenceable(const const_iterator* __i) const
2093{
2094 return this->__begin_ <= __i->base() && __i->base() < this->__end_;
2095}
2096
2097template <class _Tp, class _Allocator>
2098bool
2099vector<_Tp, _Allocator>::__decrementable(const const_iterator* __i) const
2100{
2101 return this->__begin_ < __i->base() && __i->base() <= this->__end_;
2102}
2103
2104template <class _Tp, class _Allocator>
2105bool
2106vector<_Tp, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
2107{
2108 const_pointer __p = __i->base() + __n;
2109 return this->__begin_ <= __p && __p <= this->__end_;
2110}
2111
2112template <class _Tp, class _Allocator>
2113bool
2114vector<_Tp, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
2115{
2116 const_pointer __p = __i->base() + __n;
2117 return this->__begin_ <= __p && __p < this->__end_;
2118}
2119
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002120#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002121
2122template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002123inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002124void
2125vector<_Tp, _Allocator>::__invalidate_all_iterators()
2126{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002127#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002128 __get_db()->__invalidate_all(this);
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002129#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00002130}
2131
Eric Fiselier69c51982016-12-28 06:06:09 +00002132
2133template <class _Tp, class _Allocator>
2134inline _LIBCPP_INLINE_VISIBILITY
2135void
2136vector<_Tp, _Allocator>::__invalidate_iterators_past(pointer __new_last) {
2137#if _LIBCPP_DEBUG_LEVEL >= 2
2138 __c_node* __c = __get_db()->__find_c_and_lock(this);
2139 for (__i_node** __p = __c->end_; __p != __c->beg_; ) {
2140 --__p;
2141 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
2142 if (__i->base() > __new_last) {
2143 (*__p)->__c_ = nullptr;
2144 if (--__c->end_ != __p)
2145 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
2146 }
2147 }
2148 __get_db()->unlock();
2149#else
2150 ((void)__new_last);
2151#endif
2152}
2153
Howard Hinnantc51e1022010-05-11 19:42:16 +00002154// vector<bool>
2155
2156template <class _Allocator> class vector<bool, _Allocator>;
2157
2158template <class _Allocator> struct hash<vector<bool, _Allocator> >;
2159
2160template <class _Allocator>
Howard Hinnantb4ef6482011-07-02 20:33:23 +00002161struct __has_storage_type<vector<bool, _Allocator> >
2162{
2163 static const bool value = true;
2164};
2165
2166template <class _Allocator>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002167class _LIBCPP_TEMPLATE_VIS vector<bool, _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002168 : private __vector_base_common<true>
2169{
2170public:
2171 typedef vector __self;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002172 typedef bool value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002173 typedef _Allocator allocator_type;
2174 typedef allocator_traits<allocator_type> __alloc_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002175 typedef typename __alloc_traits::size_type size_type;
2176 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant896b7622012-05-07 16:50:38 +00002177 typedef size_type __storage_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002178 typedef __bit_iterator<vector, false> pointer;
2179 typedef __bit_iterator<vector, true> const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002180 typedef pointer iterator;
2181 typedef const_pointer const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002182 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
2183 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002184
2185private:
Marshall Clow940e01c2015-04-07 05:21:38 +00002186 typedef typename __rebind_alloc_helper<__alloc_traits, __storage_type>::type __storage_allocator;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002187 typedef allocator_traits<__storage_allocator> __storage_traits;
2188 typedef typename __storage_traits::pointer __storage_pointer;
2189 typedef typename __storage_traits::const_pointer __const_storage_pointer;
2190
2191 __storage_pointer __begin_;
2192 size_type __size_;
2193 __compressed_pair<size_type, __storage_allocator> __cap_alloc_;
Howard Hinnant71d1c192011-07-09 15:50:42 +00002194public:
Howard Hinnantb4ef6482011-07-02 20:33:23 +00002195 typedef __bit_reference<vector> reference;
2196 typedef __bit_const_reference<vector> const_reference;
Howard Hinnant71d1c192011-07-09 15:50:42 +00002197private:
Howard Hinnant1c936782011-06-03 19:40:40 +00002198 _LIBCPP_INLINE_VISIBILITY
2199 size_type& __cap() _NOEXCEPT
2200 {return __cap_alloc_.first();}
2201 _LIBCPP_INLINE_VISIBILITY
2202 const size_type& __cap() const _NOEXCEPT
2203 {return __cap_alloc_.first();}
2204 _LIBCPP_INLINE_VISIBILITY
2205 __storage_allocator& __alloc() _NOEXCEPT
2206 {return __cap_alloc_.second();}
2207 _LIBCPP_INLINE_VISIBILITY
2208 const __storage_allocator& __alloc() const _NOEXCEPT
2209 {return __cap_alloc_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002210
2211 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
2212
Howard Hinnant1c936782011-06-03 19:40:40 +00002213 _LIBCPP_INLINE_VISIBILITY
2214 static size_type __internal_cap_to_external(size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002215 {return __n * __bits_per_word;}
Howard Hinnant1c936782011-06-03 19:40:40 +00002216 _LIBCPP_INLINE_VISIBILITY
2217 static size_type __external_cap_to_internal(size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002218 {return (__n - 1) / __bits_per_word + 1;}
2219
2220public:
Howard Hinnant1c936782011-06-03 19:40:40 +00002221 _LIBCPP_INLINE_VISIBILITY
Marshall Clowe546cbb2015-06-04 02:05:41 +00002222 vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clow8f282f42015-06-04 00:10:20 +00002223
2224 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
2225#if _LIBCPP_STD_VER <= 14
2226 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
2227#else
2228 _NOEXCEPT;
2229#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002230 ~vector();
2231 explicit vector(size_type __n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00002232#if _LIBCPP_STD_VER > 11
2233 explicit vector(size_type __n, const allocator_type& __a);
2234#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002235 vector(size_type __n, const value_type& __v);
2236 vector(size_type __n, const value_type& __v, const allocator_type& __a);
2237 template <class _InputIterator>
2238 vector(_InputIterator __first, _InputIterator __last,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002239 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
2240 !__is_cpp17_forward_iterator<_InputIterator>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002241 template <class _InputIterator>
2242 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002243 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
2244 !__is_cpp17_forward_iterator<_InputIterator>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002245 template <class _ForwardIterator>
2246 vector(_ForwardIterator __first, _ForwardIterator __last,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002247 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002248 template <class _ForwardIterator>
2249 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002250 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002251
2252 vector(const vector& __v);
2253 vector(const vector& __v, const allocator_type& __a);
2254 vector& operator=(const vector& __v);
Eric Fiseliered9e9362017-04-16 02:40:45 +00002255
2256#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002257 vector(initializer_list<value_type> __il);
2258 vector(initializer_list<value_type> __il, const allocator_type& __a);
2259
Howard Hinnant1c936782011-06-03 19:40:40 +00002260 _LIBCPP_INLINE_VISIBILITY
2261 vector(vector&& __v)
Marshall Clowe5108202015-07-14 14:46:32 +00002262#if _LIBCPP_STD_VER > 14
2263 _NOEXCEPT;
2264#else
Howard Hinnant1c936782011-06-03 19:40:40 +00002265 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowe5108202015-07-14 14:46:32 +00002266#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002267 vector(vector&& __v, const allocator_type& __a);
Howard Hinnant1c936782011-06-03 19:40:40 +00002268 _LIBCPP_INLINE_VISIBILITY
2269 vector& operator=(vector&& __v)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002270 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiseliered9e9362017-04-16 02:40:45 +00002271
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002272 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002273 vector& operator=(initializer_list<value_type> __il)
2274 {assign(__il.begin(), __il.end()); return *this;}
Eric Fiseliered9e9362017-04-16 02:40:45 +00002275
2276#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002277
2278 template <class _InputIterator>
2279 typename enable_if
2280 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002281 __is_cpp17_input_iterator<_InputIterator>::value &&
2282 !__is_cpp17_forward_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002283 void
2284 >::type
2285 assign(_InputIterator __first, _InputIterator __last);
2286 template <class _ForwardIterator>
2287 typename enable_if
2288 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002289 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002290 void
2291 >::type
2292 assign(_ForwardIterator __first, _ForwardIterator __last);
2293
2294 void assign(size_type __n, const value_type& __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +00002295
2296#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002297 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002298 void assign(initializer_list<value_type> __il)
2299 {assign(__il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +00002300#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002301
Howard Hinnant1c936782011-06-03 19:40:40 +00002302 _LIBCPP_INLINE_VISIBILITY allocator_type get_allocator() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002303 {return allocator_type(this->__alloc());}
2304
Howard Hinnant1c936782011-06-03 19:40:40 +00002305 size_type max_size() const _NOEXCEPT;
2306 _LIBCPP_INLINE_VISIBILITY
2307 size_type capacity() const _NOEXCEPT
2308 {return __internal_cap_to_external(__cap());}
2309 _LIBCPP_INLINE_VISIBILITY
2310 size_type size() const _NOEXCEPT
2311 {return __size_;}
Marshall Clow425f5752017-11-15 05:51:26 +00002312 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +00002313 bool empty() const _NOEXCEPT
2314 {return __size_ == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002315 void reserve(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +00002316 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002317
Howard Hinnant1c936782011-06-03 19:40:40 +00002318 _LIBCPP_INLINE_VISIBILITY
2319 iterator begin() _NOEXCEPT
2320 {return __make_iter(0);}
2321 _LIBCPP_INLINE_VISIBILITY
2322 const_iterator begin() const _NOEXCEPT
2323 {return __make_iter(0);}
2324 _LIBCPP_INLINE_VISIBILITY
2325 iterator end() _NOEXCEPT
2326 {return __make_iter(__size_);}
2327 _LIBCPP_INLINE_VISIBILITY
2328 const_iterator end() const _NOEXCEPT
2329 {return __make_iter(__size_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002330
Howard Hinnant1c936782011-06-03 19:40:40 +00002331 _LIBCPP_INLINE_VISIBILITY
2332 reverse_iterator rbegin() _NOEXCEPT
2333 {return reverse_iterator(end());}
2334 _LIBCPP_INLINE_VISIBILITY
2335 const_reverse_iterator rbegin() const _NOEXCEPT
2336 {return const_reverse_iterator(end());}
2337 _LIBCPP_INLINE_VISIBILITY
2338 reverse_iterator rend() _NOEXCEPT
2339 {return reverse_iterator(begin());}
2340 _LIBCPP_INLINE_VISIBILITY
2341 const_reverse_iterator rend() const _NOEXCEPT
2342 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002343
Howard Hinnant1c936782011-06-03 19:40:40 +00002344 _LIBCPP_INLINE_VISIBILITY
2345 const_iterator cbegin() const _NOEXCEPT
2346 {return __make_iter(0);}
2347 _LIBCPP_INLINE_VISIBILITY
2348 const_iterator cend() const _NOEXCEPT
2349 {return __make_iter(__size_);}
2350 _LIBCPP_INLINE_VISIBILITY
2351 const_reverse_iterator crbegin() const _NOEXCEPT
2352 {return rbegin();}
2353 _LIBCPP_INLINE_VISIBILITY
2354 const_reverse_iterator crend() const _NOEXCEPT
2355 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002356
2357 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) {return __make_ref(__n);}
2358 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const {return __make_ref(__n);}
2359 reference at(size_type __n);
2360 const_reference at(size_type __n) const;
2361
2362 _LIBCPP_INLINE_VISIBILITY reference front() {return __make_ref(0);}
2363 _LIBCPP_INLINE_VISIBILITY const_reference front() const {return __make_ref(0);}
2364 _LIBCPP_INLINE_VISIBILITY reference back() {return __make_ref(__size_ - 1);}
2365 _LIBCPP_INLINE_VISIBILITY const_reference back() const {return __make_ref(__size_ - 1);}
2366
2367 void push_back(const value_type& __x);
Marshall Clowc46bb8e2013-08-13 23:54:12 +00002368#if _LIBCPP_STD_VER > 11
2369 template <class... _Args>
Marshall Clowea52cc42017-01-24 23:09:12 +00002370#if _LIBCPP_STD_VER > 14
2371 _LIBCPP_INLINE_VISIBILITY reference emplace_back(_Args&&... __args)
2372#else
2373 _LIBCPP_INLINE_VISIBILITY void emplace_back(_Args&&... __args)
2374#endif
2375 {
Eric Fiselier34ba5b92016-07-21 03:20:17 +00002376 push_back ( value_type ( _VSTD::forward<_Args>(__args)... ));
Marshall Clowea52cc42017-01-24 23:09:12 +00002377#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00002378 return this->back();
Marshall Clowea52cc42017-01-24 23:09:12 +00002379#endif
Eric Fiselier34ba5b92016-07-21 03:20:17 +00002380 }
Marshall Clowc46bb8e2013-08-13 23:54:12 +00002381#endif
2382
Howard Hinnantc51e1022010-05-11 19:42:16 +00002383 _LIBCPP_INLINE_VISIBILITY void pop_back() {--__size_;}
2384
Marshall Clowc46bb8e2013-08-13 23:54:12 +00002385#if _LIBCPP_STD_VER > 11
2386 template <class... _Args>
2387 _LIBCPP_INLINE_VISIBILITY iterator emplace(const_iterator position, _Args&&... __args)
2388 { return insert ( position, value_type ( _VSTD::forward<_Args>(__args)... )); }
2389#endif
2390
Howard Hinnantc51e1022010-05-11 19:42:16 +00002391 iterator insert(const_iterator __position, const value_type& __x);
2392 iterator insert(const_iterator __position, size_type __n, const value_type& __x);
2393 iterator insert(const_iterator __position, size_type __n, const_reference __x);
2394 template <class _InputIterator>
2395 typename enable_if
2396 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002397 __is_cpp17_input_iterator <_InputIterator>::value &&
2398 !__is_cpp17_forward_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002399 iterator
2400 >::type
2401 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
2402 template <class _ForwardIterator>
2403 typename enable_if
2404 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002405 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002406 iterator
2407 >::type
2408 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiseliered9e9362017-04-16 02:40:45 +00002409
2410#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002411 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002412 iterator insert(const_iterator __position, initializer_list<value_type> __il)
2413 {return insert(__position, __il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +00002414#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002415
Howard Hinnantcf823322010-12-17 14:46:43 +00002416 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002417 iterator erase(const_iterator __first, const_iterator __last);
2418
Howard Hinnant1c936782011-06-03 19:40:40 +00002419 _LIBCPP_INLINE_VISIBILITY
2420 void clear() _NOEXCEPT {__size_ = 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002421
Howard Hinnant1c936782011-06-03 19:40:40 +00002422 void swap(vector&)
Marshall Clow8982dcd2015-07-13 20:04:56 +00002423#if _LIBCPP_STD_VER >= 14
2424 _NOEXCEPT;
2425#else
Eric Fiselier69c51982016-12-28 06:06:09 +00002426 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00002427 __is_nothrow_swappable<allocator_type>::value);
2428#endif
Marshall Clow89eb3822016-04-07 14:20:31 +00002429 static void swap(reference __x, reference __y) _NOEXCEPT { _VSTD::swap(__x, __y); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002430
2431 void resize(size_type __sz, value_type __x = false);
Howard Hinnant1c936782011-06-03 19:40:40 +00002432 void flip() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002433
2434 bool __invariants() const;
2435
2436private:
Howard Hinnantcf823322010-12-17 14:46:43 +00002437 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Marshall Clow3ff48e02018-05-22 16:20:28 +00002438 void __vallocate(size_type __n);
2439 void __vdeallocate() _NOEXCEPT;
Howard Hinnant1c936782011-06-03 19:40:40 +00002440 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantea382952013-08-14 18:00:20 +00002441 static size_type __align_it(size_type __new_size) _NOEXCEPT
Eric Fiselierb41db9a2018-10-01 01:59:37 +00002442 {return __new_size + (__bits_per_word-1) & ~((size_type)__bits_per_word-1);}
Howard Hinnantcf823322010-12-17 14:46:43 +00002443 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
2444 _LIBCPP_INLINE_VISIBILITY void __construct_at_end(size_type __n, bool __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002445 template <class _ForwardIterator>
2446 typename enable_if
2447 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002448 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002449 void
2450 >::type
2451 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
2452 void __append(size_type __n, const_reference __x);
Howard Hinnant1c936782011-06-03 19:40:40 +00002453 _LIBCPP_INLINE_VISIBILITY
2454 reference __make_ref(size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002455 {return reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnant1c936782011-06-03 19:40:40 +00002456 _LIBCPP_INLINE_VISIBILITY
2457 const_reference __make_ref(size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002458 {return const_reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnant1c936782011-06-03 19:40:40 +00002459 _LIBCPP_INLINE_VISIBILITY
2460 iterator __make_iter(size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002461 {return iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnant1c936782011-06-03 19:40:40 +00002462 _LIBCPP_INLINE_VISIBILITY
2463 const_iterator __make_iter(size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002464 {return const_iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnant1c936782011-06-03 19:40:40 +00002465 _LIBCPP_INLINE_VISIBILITY
2466 iterator __const_iterator_cast(const_iterator __p) _NOEXCEPT
Howard Hinnant76053d72013-06-27 19:35:32 +00002467 {return begin() + (__p - cbegin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002468
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002469 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002470 void __copy_assign_alloc(const vector& __v)
2471 {__copy_assign_alloc(__v, integral_constant<bool,
2472 __storage_traits::propagate_on_container_copy_assignment::value>());}
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002473 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002474 void __copy_assign_alloc(const vector& __c, true_type)
2475 {
2476 if (__alloc() != __c.__alloc())
Marshall Clow3ff48e02018-05-22 16:20:28 +00002477 __vdeallocate();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002478 __alloc() = __c.__alloc();
2479 }
2480
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002481 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00002482 void __copy_assign_alloc(const vector&, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002483 {}
2484
2485 void __move_assign(vector& __c, false_type);
Howard Hinnant1c936782011-06-03 19:40:40 +00002486 void __move_assign(vector& __c, true_type)
2487 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002488 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002489 void __move_assign_alloc(vector& __c)
Howard Hinnant1c936782011-06-03 19:40:40 +00002490 _NOEXCEPT_(
2491 !__storage_traits::propagate_on_container_move_assignment::value ||
2492 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002493 {__move_assign_alloc(__c, integral_constant<bool,
2494 __storage_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002495 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +00002496 void __move_assign_alloc(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002497 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002498 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002499 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002500 }
2501
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002502 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00002503 void __move_assign_alloc(vector&, false_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002504 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002505 {}
2506
Howard Hinnant1c936782011-06-03 19:40:40 +00002507 size_t __hash_code() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002508
2509 friend class __bit_reference<vector>;
2510 friend class __bit_const_reference<vector>;
2511 friend class __bit_iterator<vector, false>;
2512 friend class __bit_iterator<vector, true>;
Howard Hinnant4210a0f2012-08-17 17:10:18 +00002513 friend struct __bit_array<vector>;
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002514 friend struct _LIBCPP_TEMPLATE_VIS hash<vector>;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002515};
2516
2517template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002518inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002519void
2520vector<bool, _Allocator>::__invalidate_all_iterators()
2521{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002522}
2523
2524// Allocate space for __n objects
2525// throws length_error if __n > max_size()
2526// throws (probably bad_alloc) if memory run out
2527// Precondition: __begin_ == __end_ == __cap() == 0
2528// Precondition: __n > 0
2529// Postcondition: capacity() == __n
2530// Postcondition: size() == 0
2531template <class _Allocator>
2532void
Marshall Clow3ff48e02018-05-22 16:20:28 +00002533vector<bool, _Allocator>::__vallocate(size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002534{
2535 if (__n > max_size())
2536 this->__throw_length_error();
2537 __n = __external_cap_to_internal(__n);
2538 this->__begin_ = __storage_traits::allocate(this->__alloc(), __n);
2539 this->__size_ = 0;
2540 this->__cap() = __n;
2541}
2542
2543template <class _Allocator>
2544void
Marshall Clow3ff48e02018-05-22 16:20:28 +00002545vector<bool, _Allocator>::__vdeallocate() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002546{
Howard Hinnant76053d72013-06-27 19:35:32 +00002547 if (this->__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002548 {
2549 __storage_traits::deallocate(this->__alloc(), this->__begin_, __cap());
2550 __invalidate_all_iterators();
Howard Hinnant76053d72013-06-27 19:35:32 +00002551 this->__begin_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002552 this->__size_ = this->__cap() = 0;
2553 }
2554}
2555
2556template <class _Allocator>
2557typename vector<bool, _Allocator>::size_type
Howard Hinnant1c936782011-06-03 19:40:40 +00002558vector<bool, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002559{
2560 size_type __amax = __storage_traits::max_size(__alloc());
2561 size_type __nmax = numeric_limits<size_type>::max() / 2; // end() >= begin(), always
2562 if (__nmax / __bits_per_word <= __amax)
2563 return __nmax;
2564 return __internal_cap_to_external(__amax);
2565}
2566
2567// Precondition: __new_size > capacity()
2568template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002569inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002570typename vector<bool, _Allocator>::size_type
2571vector<bool, _Allocator>::__recommend(size_type __new_size) const
2572{
2573 const size_type __ms = max_size();
2574 if (__new_size > __ms)
2575 this->__throw_length_error();
2576 const size_type __cap = capacity();
2577 if (__cap >= __ms / 2)
2578 return __ms;
Howard Hinnantea382952013-08-14 18:00:20 +00002579 return _VSTD::max(2*__cap, __align_it(__new_size));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002580}
2581
2582// Default constructs __n objects starting at __end_
2583// Precondition: __n > 0
2584// Precondition: size() + __n <= capacity()
2585// Postcondition: size() == size() + __n
2586template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002587inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002588void
2589vector<bool, _Allocator>::__construct_at_end(size_type __n, bool __x)
2590{
2591 size_type __old_size = this->__size_;
2592 this->__size_ += __n;
Marshall Clow1893ec72018-10-23 20:07:45 +00002593 if (__old_size == 0 || ((__old_size - 1) / __bits_per_word) != ((this->__size_ - 1) / __bits_per_word))
2594 {
2595 if (this->__size_ <= __bits_per_word)
2596 this->__begin_[0] = __storage_type(0);
2597 else
2598 this->__begin_[(this->__size_ - 1) / __bits_per_word] = __storage_type(0);
2599 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002600 _VSTD::fill_n(__make_iter(__old_size), __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002601}
2602
2603template <class _Allocator>
2604template <class _ForwardIterator>
2605typename enable_if
2606<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002607 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002608 void
2609>::type
2610vector<bool, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
2611{
2612 size_type __old_size = this->__size_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002613 this->__size_ += _VSTD::distance(__first, __last);
Marshall Clow1893ec72018-10-23 20:07:45 +00002614 if (__old_size == 0 || ((__old_size - 1) / __bits_per_word) != ((this->__size_ - 1) / __bits_per_word))
2615 {
2616 if (this->__size_ <= __bits_per_word)
2617 this->__begin_[0] = __storage_type(0);
2618 else
2619 this->__begin_[(this->__size_ - 1) / __bits_per_word] = __storage_type(0);
2620 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002621 _VSTD::copy(__first, __last, __make_iter(__old_size));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002622}
2623
2624template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002625inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002626vector<bool, _Allocator>::vector()
Marshall Clowe546cbb2015-06-04 02:05:41 +00002627 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant76053d72013-06-27 19:35:32 +00002628 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002629 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002630 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002631{
2632}
2633
2634template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002635inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002636vector<bool, _Allocator>::vector(const allocator_type& __a)
Marshall Clow8f282f42015-06-04 00:10:20 +00002637#if _LIBCPP_STD_VER <= 14
2638 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
2639#else
2640 _NOEXCEPT
2641#endif
Howard Hinnant76053d72013-06-27 19:35:32 +00002642 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002643 __size_(0),
2644 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2645{
2646}
2647
2648template <class _Allocator>
2649vector<bool, _Allocator>::vector(size_type __n)
Howard Hinnant76053d72013-06-27 19:35:32 +00002650 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002651 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002652 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002653{
2654 if (__n > 0)
2655 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002656 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002657 __construct_at_end(__n, false);
2658 }
2659}
2660
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00002661#if _LIBCPP_STD_VER > 11
2662template <class _Allocator>
2663vector<bool, _Allocator>::vector(size_type __n, const allocator_type& __a)
2664 : __begin_(nullptr),
2665 __size_(0),
2666 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2667{
2668 if (__n > 0)
2669 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002670 __vallocate(__n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00002671 __construct_at_end(__n, false);
2672 }
2673}
2674#endif
2675
Howard Hinnantc51e1022010-05-11 19:42:16 +00002676template <class _Allocator>
2677vector<bool, _Allocator>::vector(size_type __n, const value_type& __x)
Howard Hinnant76053d72013-06-27 19:35:32 +00002678 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002679 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002680 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002681{
2682 if (__n > 0)
2683 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002684 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002685 __construct_at_end(__n, __x);
2686 }
2687}
2688
2689template <class _Allocator>
2690vector<bool, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002691 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002692 __size_(0),
2693 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2694{
2695 if (__n > 0)
2696 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002697 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002698 __construct_at_end(__n, __x);
2699 }
2700}
2701
2702template <class _Allocator>
2703template <class _InputIterator>
2704vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002705 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
2706 !__is_cpp17_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002707 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002708 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002709 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002710{
2711#ifndef _LIBCPP_NO_EXCEPTIONS
2712 try
2713 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002714#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002715 for (; __first != __last; ++__first)
2716 push_back(*__first);
2717#ifndef _LIBCPP_NO_EXCEPTIONS
2718 }
2719 catch (...)
2720 {
Howard Hinnant76053d72013-06-27 19:35:32 +00002721 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002722 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2723 __invalidate_all_iterators();
2724 throw;
2725 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002726#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002727}
2728
2729template <class _Allocator>
2730template <class _InputIterator>
2731vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002732 typename enable_if<__is_cpp17_input_iterator <_InputIterator>::value &&
2733 !__is_cpp17_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002734 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002735 __size_(0),
2736 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2737{
2738#ifndef _LIBCPP_NO_EXCEPTIONS
2739 try
2740 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002741#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002742 for (; __first != __last; ++__first)
2743 push_back(*__first);
2744#ifndef _LIBCPP_NO_EXCEPTIONS
2745 }
2746 catch (...)
2747 {
Howard Hinnant76053d72013-06-27 19:35:32 +00002748 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002749 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2750 __invalidate_all_iterators();
2751 throw;
2752 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002753#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002754}
2755
2756template <class _Allocator>
2757template <class _ForwardIterator>
2758vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002759 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002760 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002761 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002762 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002763{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002764 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002765 if (__n > 0)
2766 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002767 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002768 __construct_at_end(__first, __last);
2769 }
2770}
2771
2772template <class _Allocator>
2773template <class _ForwardIterator>
2774vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002775 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002776 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002777 __size_(0),
2778 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2779{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002780 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002781 if (__n > 0)
2782 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002783 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002784 __construct_at_end(__first, __last);
2785 }
2786}
2787
Eric Fiseliered9e9362017-04-16 02:40:45 +00002788#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002789
Howard Hinnantc51e1022010-05-11 19:42:16 +00002790template <class _Allocator>
2791vector<bool, _Allocator>::vector(initializer_list<value_type> __il)
Howard Hinnant76053d72013-06-27 19:35:32 +00002792 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002793 __size_(0),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002794 __cap_alloc_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002795{
2796 size_type __n = static_cast<size_type>(__il.size());
2797 if (__n > 0)
2798 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002799 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002800 __construct_at_end(__il.begin(), __il.end());
2801 }
2802}
2803
2804template <class _Allocator>
2805vector<bool, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002806 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002807 __size_(0),
2808 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2809{
2810 size_type __n = static_cast<size_type>(__il.size());
2811 if (__n > 0)
2812 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002813 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002814 __construct_at_end(__il.begin(), __il.end());
2815 }
2816}
2817
Eric Fiseliered9e9362017-04-16 02:40:45 +00002818#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002819
Howard Hinnantc51e1022010-05-11 19:42:16 +00002820template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002821vector<bool, _Allocator>::~vector()
2822{
Howard Hinnant76053d72013-06-27 19:35:32 +00002823 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002824 __storage_traits::deallocate(__alloc(), __begin_, __cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002825 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002826}
2827
2828template <class _Allocator>
2829vector<bool, _Allocator>::vector(const vector& __v)
Howard Hinnant76053d72013-06-27 19:35:32 +00002830 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002831 __size_(0),
2832 __cap_alloc_(0, __storage_traits::select_on_container_copy_construction(__v.__alloc()))
2833{
2834 if (__v.size() > 0)
2835 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002836 __vallocate(__v.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002837 __construct_at_end(__v.begin(), __v.end());
2838 }
2839}
2840
2841template <class _Allocator>
2842vector<bool, _Allocator>::vector(const vector& __v, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002843 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002844 __size_(0),
2845 __cap_alloc_(0, __a)
2846{
2847 if (__v.size() > 0)
2848 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002849 __vallocate(__v.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002850 __construct_at_end(__v.begin(), __v.end());
2851 }
2852}
2853
2854template <class _Allocator>
2855vector<bool, _Allocator>&
2856vector<bool, _Allocator>::operator=(const vector& __v)
2857{
2858 if (this != &__v)
2859 {
2860 __copy_assign_alloc(__v);
2861 if (__v.__size_)
2862 {
2863 if (__v.__size_ > capacity())
2864 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002865 __vdeallocate();
2866 __vallocate(__v.__size_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002867 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002868 _VSTD::copy(__v.__begin_, __v.__begin_ + __external_cap_to_internal(__v.__size_), __begin_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002869 }
2870 __size_ = __v.__size_;
2871 }
2872 return *this;
2873}
2874
Eric Fiseliered9e9362017-04-16 02:40:45 +00002875#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant74279a52010-09-04 23:28:19 +00002876
Howard Hinnantc51e1022010-05-11 19:42:16 +00002877template <class _Allocator>
Eric Fiselier4f1534c2018-06-05 22:32:52 +00002878inline _LIBCPP_INLINE_VISIBILITY vector<bool, _Allocator>::vector(vector&& __v)
Marshall Clowe5108202015-07-14 14:46:32 +00002879#if _LIBCPP_STD_VER > 14
Eric Fiselier4f1534c2018-06-05 22:32:52 +00002880 _NOEXCEPT
Marshall Clowe5108202015-07-14 14:46:32 +00002881#else
Eric Fiselier4f1534c2018-06-05 22:32:52 +00002882 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowe5108202015-07-14 14:46:32 +00002883#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002884 : __begin_(__v.__begin_),
2885 __size_(__v.__size_),
Eric Fiselier4f1534c2018-06-05 22:32:52 +00002886 __cap_alloc_(std::move(__v.__cap_alloc_)) {
Howard Hinnant76053d72013-06-27 19:35:32 +00002887 __v.__begin_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002888 __v.__size_ = 0;
2889 __v.__cap() = 0;
2890}
2891
2892template <class _Allocator>
2893vector<bool, _Allocator>::vector(vector&& __v, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002894 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002895 __size_(0),
2896 __cap_alloc_(0, __a)
2897{
2898 if (__a == allocator_type(__v.__alloc()))
2899 {
2900 this->__begin_ = __v.__begin_;
2901 this->__size_ = __v.__size_;
2902 this->__cap() = __v.__cap();
2903 __v.__begin_ = nullptr;
2904 __v.__cap() = __v.__size_ = 0;
2905 }
2906 else if (__v.size() > 0)
2907 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00002908 __vallocate(__v.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002909 __construct_at_end(__v.begin(), __v.end());
2910 }
2911}
2912
2913template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002914inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002915vector<bool, _Allocator>&
2916vector<bool, _Allocator>::operator=(vector&& __v)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002917 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002918{
2919 __move_assign(__v, integral_constant<bool,
2920 __storage_traits::propagate_on_container_move_assignment::value>());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002921 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002922}
2923
2924template <class _Allocator>
2925void
2926vector<bool, _Allocator>::__move_assign(vector& __c, false_type)
2927{
2928 if (__alloc() != __c.__alloc())
2929 assign(__c.begin(), __c.end());
2930 else
2931 __move_assign(__c, true_type());
2932}
2933
2934template <class _Allocator>
2935void
2936vector<bool, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002937 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002938{
Marshall Clow3ff48e02018-05-22 16:20:28 +00002939 __vdeallocate();
Marshall Clowb4871fa2014-07-21 15:15:15 +00002940 __move_assign_alloc(__c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002941 this->__begin_ = __c.__begin_;
2942 this->__size_ = __c.__size_;
2943 this->__cap() = __c.__cap();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002944 __c.__begin_ = nullptr;
2945 __c.__cap() = __c.__size_ = 0;
2946}
Howard Hinnant74279a52010-09-04 23:28:19 +00002947
Eric Fiseliered9e9362017-04-16 02:40:45 +00002948#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002949
2950template <class _Allocator>
2951void
2952vector<bool, _Allocator>::assign(size_type __n, const value_type& __x)
2953{
2954 __size_ = 0;
2955 if (__n > 0)
2956 {
2957 size_type __c = capacity();
2958 if (__n <= __c)
2959 __size_ = __n;
2960 else
2961 {
2962 vector __v(__alloc());
2963 __v.reserve(__recommend(__n));
2964 __v.__size_ = __n;
2965 swap(__v);
2966 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002967 _VSTD::fill_n(begin(), __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002968 }
Eric Fiselier69c51982016-12-28 06:06:09 +00002969 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002970}
2971
2972template <class _Allocator>
2973template <class _InputIterator>
2974typename enable_if
2975<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002976 __is_cpp17_input_iterator<_InputIterator>::value &&
2977 !__is_cpp17_forward_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002978 void
2979>::type
2980vector<bool, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2981{
2982 clear();
2983 for (; __first != __last; ++__first)
2984 push_back(*__first);
2985}
2986
2987template <class _Allocator>
2988template <class _ForwardIterator>
2989typename enable_if
2990<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002991 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002992 void
2993>::type
2994vector<bool, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2995{
2996 clear();
Eric Fiselier6003c772016-12-23 23:37:52 +00002997 difference_type __ns = _VSTD::distance(__first, __last);
2998 _LIBCPP_ASSERT(__ns >= 0, "invalid range specified");
2999 const size_t __n = static_cast<size_type>(__ns);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003000 if (__n)
3001 {
3002 if (__n > capacity())
3003 {
Marshall Clow3ff48e02018-05-22 16:20:28 +00003004 __vdeallocate();
3005 __vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003006 }
3007 __construct_at_end(__first, __last);
3008 }
3009}
3010
3011template <class _Allocator>
3012void
3013vector<bool, _Allocator>::reserve(size_type __n)
3014{
3015 if (__n > capacity())
3016 {
3017 vector __v(this->__alloc());
Marshall Clow3ff48e02018-05-22 16:20:28 +00003018 __v.__vallocate(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003019 __v.__construct_at_end(this->begin(), this->end());
3020 swap(__v);
3021 __invalidate_all_iterators();
3022 }
3023}
3024
3025template <class _Allocator>
3026void
Howard Hinnant1c936782011-06-03 19:40:40 +00003027vector<bool, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003028{
3029 if (__external_cap_to_internal(size()) > __cap())
3030 {
3031#ifndef _LIBCPP_NO_EXCEPTIONS
3032 try
3033 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003034#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003035 vector(*this, allocator_type(__alloc())).swap(*this);
3036#ifndef _LIBCPP_NO_EXCEPTIONS
3037 }
3038 catch (...)
3039 {
3040 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003041#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003042 }
3043}
3044
3045template <class _Allocator>
3046typename vector<bool, _Allocator>::reference
3047vector<bool, _Allocator>::at(size_type __n)
3048{
3049 if (__n >= size())
3050 this->__throw_out_of_range();
3051 return (*this)[__n];
3052}
3053
3054template <class _Allocator>
3055typename vector<bool, _Allocator>::const_reference
3056vector<bool, _Allocator>::at(size_type __n) const
3057{
3058 if (__n >= size())
3059 this->__throw_out_of_range();
3060 return (*this)[__n];
3061}
3062
3063template <class _Allocator>
3064void
3065vector<bool, _Allocator>::push_back(const value_type& __x)
3066{
3067 if (this->__size_ == this->capacity())
3068 reserve(__recommend(this->__size_ + 1));
3069 ++this->__size_;
3070 back() = __x;
3071}
3072
3073template <class _Allocator>
3074typename vector<bool, _Allocator>::iterator
3075vector<bool, _Allocator>::insert(const_iterator __position, const value_type& __x)
3076{
3077 iterator __r;
3078 if (size() < capacity())
3079 {
3080 const_iterator __old_end = end();
3081 ++__size_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003082 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003083 __r = __const_iterator_cast(__position);
3084 }
3085 else
3086 {
3087 vector __v(__alloc());
3088 __v.reserve(__recommend(__size_ + 1));
3089 __v.__size_ = __size_ + 1;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003090 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3091 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003092 swap(__v);
3093 }
3094 *__r = __x;
3095 return __r;
3096}
3097
3098template <class _Allocator>
3099typename vector<bool, _Allocator>::iterator
3100vector<bool, _Allocator>::insert(const_iterator __position, size_type __n, const value_type& __x)
3101{
3102 iterator __r;
3103 size_type __c = capacity();
3104 if (__n <= __c && size() <= __c - __n)
3105 {
3106 const_iterator __old_end = end();
3107 __size_ += __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003108 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003109 __r = __const_iterator_cast(__position);
3110 }
3111 else
3112 {
3113 vector __v(__alloc());
3114 __v.reserve(__recommend(__size_ + __n));
3115 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003116 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3117 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003118 swap(__v);
3119 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003120 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003121 return __r;
3122}
3123
3124template <class _Allocator>
3125template <class _InputIterator>
3126typename enable_if
3127<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05003128 __is_cpp17_input_iterator <_InputIterator>::value &&
3129 !__is_cpp17_forward_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003130 typename vector<bool, _Allocator>::iterator
3131>::type
3132vector<bool, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
3133{
3134 difference_type __off = __position - begin();
3135 iterator __p = __const_iterator_cast(__position);
3136 iterator __old_end = end();
3137 for (; size() != capacity() && __first != __last; ++__first)
3138 {
3139 ++this->__size_;
3140 back() = *__first;
3141 }
3142 vector __v(__alloc());
3143 if (__first != __last)
3144 {
3145#ifndef _LIBCPP_NO_EXCEPTIONS
3146 try
3147 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003148#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003149 __v.assign(__first, __last);
3150 difference_type __old_size = static_cast<difference_type>(__old_end - begin());
3151 difference_type __old_p = __p - begin();
3152 reserve(__recommend(size() + __v.size()));
3153 __p = begin() + __old_p;
3154 __old_end = begin() + __old_size;
3155#ifndef _LIBCPP_NO_EXCEPTIONS
3156 }
3157 catch (...)
3158 {
3159 erase(__old_end, end());
3160 throw;
3161 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003162#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003163 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003164 __p = _VSTD::rotate(__p, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003165 insert(__p, __v.begin(), __v.end());
3166 return begin() + __off;
3167}
3168
3169template <class _Allocator>
3170template <class _ForwardIterator>
3171typename enable_if
3172<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05003173 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003174 typename vector<bool, _Allocator>::iterator
3175>::type
3176vector<bool, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
3177{
Eric Fiselier654dd332016-12-11 05:31:00 +00003178 const difference_type __n_signed = _VSTD::distance(__first, __last);
3179 _LIBCPP_ASSERT(__n_signed >= 0, "invalid range specified");
3180 const size_type __n = static_cast<size_type>(__n_signed);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003181 iterator __r;
3182 size_type __c = capacity();
3183 if (__n <= __c && size() <= __c - __n)
3184 {
3185 const_iterator __old_end = end();
3186 __size_ += __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003187 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003188 __r = __const_iterator_cast(__position);
3189 }
3190 else
3191 {
3192 vector __v(__alloc());
3193 __v.reserve(__recommend(__size_ + __n));
3194 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003195 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3196 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003197 swap(__v);
3198 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003199 _VSTD::copy(__first, __last, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003200 return __r;
3201}
3202
3203template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003204inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003205typename vector<bool, _Allocator>::iterator
3206vector<bool, _Allocator>::erase(const_iterator __position)
3207{
3208 iterator __r = __const_iterator_cast(__position);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003209 _VSTD::copy(__position + 1, this->cend(), __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003210 --__size_;
3211 return __r;
3212}
3213
3214template <class _Allocator>
3215typename vector<bool, _Allocator>::iterator
3216vector<bool, _Allocator>::erase(const_iterator __first, const_iterator __last)
3217{
3218 iterator __r = __const_iterator_cast(__first);
3219 difference_type __d = __last - __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003220 _VSTD::copy(__last, this->cend(), __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003221 __size_ -= __d;
3222 return __r;
3223}
3224
3225template <class _Allocator>
3226void
3227vector<bool, _Allocator>::swap(vector& __x)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003228#if _LIBCPP_STD_VER >= 14
3229 _NOEXCEPT
3230#else
Eric Fiselier69c51982016-12-28 06:06:09 +00003231 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003232 __is_nothrow_swappable<allocator_type>::value)
3233#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003234{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003235 _VSTD::swap(this->__begin_, __x.__begin_);
3236 _VSTD::swap(this->__size_, __x.__size_);
3237 _VSTD::swap(this->__cap(), __x.__cap());
Eric Fiseliered9e9362017-04-16 02:40:45 +00003238 __swap_allocator(this->__alloc(), __x.__alloc(),
Marshall Clow8982dcd2015-07-13 20:04:56 +00003239 integral_constant<bool, __alloc_traits::propagate_on_container_swap::value>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003240}
3241
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003242template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003243void
3244vector<bool, _Allocator>::resize(size_type __sz, value_type __x)
3245{
3246 size_type __cs = size();
3247 if (__cs < __sz)
3248 {
3249 iterator __r;
3250 size_type __c = capacity();
3251 size_type __n = __sz - __cs;
3252 if (__n <= __c && __cs <= __c - __n)
3253 {
3254 __r = end();
3255 __size_ += __n;
3256 }
3257 else
3258 {
3259 vector __v(__alloc());
3260 __v.reserve(__recommend(__size_ + __n));
3261 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003262 __r = _VSTD::copy(cbegin(), cend(), __v.begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003263 swap(__v);
3264 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003265 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003266 }
3267 else
3268 __size_ = __sz;
3269}
3270
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003271template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003272void
Howard Hinnant1c936782011-06-03 19:40:40 +00003273vector<bool, _Allocator>::flip() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003274{
3275 // do middle whole words
3276 size_type __n = __size_;
3277 __storage_pointer __p = __begin_;
3278 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3279 *__p = ~*__p;
3280 // do last partial word
3281 if (__n > 0)
3282 {
3283 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3284 __storage_type __b = *__p & __m;
3285 *__p &= ~__m;
3286 *__p |= ~__b & __m;
3287 }
3288}
3289
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003290template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003291bool
3292vector<bool, _Allocator>::__invariants() const
3293{
Howard Hinnant76053d72013-06-27 19:35:32 +00003294 if (this->__begin_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003295 {
3296 if (this->__size_ != 0 || this->__cap() != 0)
3297 return false;
3298 }
3299 else
3300 {
3301 if (this->__cap() == 0)
3302 return false;
3303 if (this->__size_ > this->capacity())
3304 return false;
3305 }
3306 return true;
3307}
3308
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003309template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003310size_t
Howard Hinnant1c936782011-06-03 19:40:40 +00003311vector<bool, _Allocator>::__hash_code() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003312{
3313 size_t __h = 0;
3314 // do middle whole words
3315 size_type __n = __size_;
3316 __storage_pointer __p = __begin_;
3317 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3318 __h ^= *__p;
3319 // do last partial word
3320 if (__n > 0)
3321 {
3322 const __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3323 __h ^= *__p & __m;
3324 }
3325 return __h;
3326}
3327
3328template <class _Allocator>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003329struct _LIBCPP_TEMPLATE_VIS hash<vector<bool, _Allocator> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00003330 : public unary_function<vector<bool, _Allocator>, size_t>
3331{
Howard Hinnant1c265cd2010-09-23 18:58:28 +00003332 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +00003333 size_t operator()(const vector<bool, _Allocator>& __vec) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003334 {return __vec.__hash_code();}
3335};
3336
3337template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003338inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003339bool
3340operator==(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3341{
3342 const typename vector<_Tp, _Allocator>::size_type __sz = __x.size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003343 return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003344}
3345
3346template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003347inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003348bool
3349operator!=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3350{
3351 return !(__x == __y);
3352}
3353
3354template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003355inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003356bool
3357operator< (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3358{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003359 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003360}
3361
3362template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003363inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003364bool
3365operator> (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3366{
3367 return __y < __x;
3368}
3369
3370template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003371inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003372bool
3373operator>=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3374{
3375 return !(__x < __y);
3376}
3377
3378template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003379inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003380bool
3381operator<=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3382{
3383 return !(__y < __x);
3384}
3385
3386template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003387inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003388void
3389swap(vector<_Tp, _Allocator>& __x, vector<_Tp, _Allocator>& __y)
Howard Hinnant1c936782011-06-03 19:40:40 +00003390 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00003391{
3392 __x.swap(__y);
3393}
3394
Marshall Clow29b53f22018-12-14 18:49:35 +00003395#if _LIBCPP_STD_VER > 17
3396template <class _Tp, class _Allocator, class _Up>
Marek Kurdeja98b1412020-05-02 13:58:03 +02003397inline _LIBCPP_INLINE_VISIBILITY typename vector<_Tp, _Allocator>::size_type
3398erase(vector<_Tp, _Allocator>& __c, const _Up& __v) {
3399 auto __old_size = __c.size();
3400 __c.erase(_VSTD::remove(__c.begin(), __c.end(), __v), __c.end());
3401 return __old_size - __c.size();
3402}
Marshall Clow29b53f22018-12-14 18:49:35 +00003403
3404template <class _Tp, class _Allocator, class _Predicate>
Marek Kurdeja98b1412020-05-02 13:58:03 +02003405inline _LIBCPP_INLINE_VISIBILITY typename vector<_Tp, _Allocator>::size_type
3406erase_if(vector<_Tp, _Allocator>& __c, _Predicate __pred) {
3407 auto __old_size = __c.size();
3408 __c.erase(_VSTD::remove_if(__c.begin(), __c.end(), __pred), __c.end());
3409 return __old_size - __c.size();
3410}
Marshall Clow29b53f22018-12-14 18:49:35 +00003411#endif
3412
Howard Hinnantc51e1022010-05-11 19:42:16 +00003413_LIBCPP_END_NAMESPACE_STD
3414
Eric Fiselierf4433a32017-05-31 22:07:49 +00003415_LIBCPP_POP_MACROS
3416
Howard Hinnantc51e1022010-05-11 19:42:16 +00003417#endif // _LIBCPP_VECTOR