blob: ee19fb7081a2d2c366b86e327652354cd2ec3cfa [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===------------------------------ vector --------------------------------===//
3//
Howard Hinnantc566dc32010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantc51e1022010-05-11 19:42:16 +00005//
Howard Hinnantee11c312010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantc51e1022010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_VECTOR
12#define _LIBCPP_VECTOR
13
14/*
15 vector synopsis
16
17namespace std
18{
19
Howard Hinnant3b6579a2010-08-22 00:02:43 +000020template <class T, class Allocator = allocator<T> >
Howard Hinnantc51e1022010-05-11 19:42:16 +000021class vector
Howard Hinnant3b6579a2010-08-22 00:02:43 +000022{
23public:
24 typedef T value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +000025 typedef Allocator allocator_type;
26 typedef typename allocator_type::reference reference;
27 typedef typename allocator_type::const_reference const_reference;
28 typedef implementation-defined iterator;
29 typedef implementation-defined const_iterator;
30 typedef typename allocator_type::size_type size_type;
31 typedef typename allocator_type::difference_type difference_type;
32 typedef typename allocator_type::pointer pointer;
33 typedef typename allocator_type::const_pointer const_pointer;
34 typedef std::reverse_iterator<iterator> reverse_iterator;
35 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
36
Howard Hinnant1c936782011-06-03 19:40:40 +000037 vector()
38 noexcept(is_nothrow_default_constructible<allocator_type>::value);
39 explicit vector(const allocator_type&);
Howard Hinnantc51e1022010-05-11 19:42:16 +000040 explicit vector(size_type n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +000041 explicit vector(size_type n, const allocator_type&); // C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +000042 vector(size_type n, const value_type& value, const allocator_type& = allocator_type());
43 template <class InputIterator>
44 vector(InputIterator first, InputIterator last, const allocator_type& = allocator_type());
45 vector(const vector& x);
Howard Hinnant1c936782011-06-03 19:40:40 +000046 vector(vector&& x)
47 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +000048 vector(initializer_list<value_type> il);
49 vector(initializer_list<value_type> il, const allocator_type& a);
50 ~vector();
51 vector& operator=(const vector& x);
Howard Hinnant1c936782011-06-03 19:40:40 +000052 vector& operator=(vector&& x)
53 noexcept(
Marshall Clow2fe8a8d2015-08-18 18:57:00 +000054 allocator_type::propagate_on_container_move_assignment::value ||
55 allocator_type::is_always_equal::value); // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +000056 vector& operator=(initializer_list<value_type> il);
57 template <class InputIterator>
58 void assign(InputIterator first, InputIterator last);
59 void assign(size_type n, const value_type& u);
60 void assign(initializer_list<value_type> il);
61
Howard Hinnant1c936782011-06-03 19:40:40 +000062 allocator_type get_allocator() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000063
Howard Hinnant1c936782011-06-03 19:40:40 +000064 iterator begin() noexcept;
65 const_iterator begin() const noexcept;
66 iterator end() noexcept;
67 const_iterator end() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000068
Howard Hinnant1c936782011-06-03 19:40:40 +000069 reverse_iterator rbegin() noexcept;
70 const_reverse_iterator rbegin() const noexcept;
71 reverse_iterator rend() noexcept;
72 const_reverse_iterator rend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000073
Howard Hinnant1c936782011-06-03 19:40:40 +000074 const_iterator cbegin() const noexcept;
75 const_iterator cend() const noexcept;
76 const_reverse_iterator crbegin() const noexcept;
77 const_reverse_iterator crend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000078
Howard Hinnant1c936782011-06-03 19:40:40 +000079 size_type size() const noexcept;
80 size_type max_size() const noexcept;
81 size_type capacity() const noexcept;
82 bool empty() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000083 void reserve(size_type n);
Howard Hinnant1c936782011-06-03 19:40:40 +000084 void shrink_to_fit() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000085
86 reference operator[](size_type n);
87 const_reference operator[](size_type n) const;
88 reference at(size_type n);
89 const_reference at(size_type n) const;
90
91 reference front();
92 const_reference front() const;
93 reference back();
94 const_reference back() const;
95
Howard Hinnant1c936782011-06-03 19:40:40 +000096 value_type* data() noexcept;
97 const value_type* data() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000098
99 void push_back(const value_type& x);
100 void push_back(value_type&& x);
101 template <class... Args>
Marshall Clowea52cc42017-01-24 23:09:12 +0000102 reference emplace_back(Args&&... args); // reference in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000103 void pop_back();
104
105 template <class... Args> iterator emplace(const_iterator position, Args&&... args);
106 iterator insert(const_iterator position, const value_type& x);
107 iterator insert(const_iterator position, value_type&& x);
108 iterator insert(const_iterator position, size_type n, const value_type& x);
109 template <class InputIterator>
110 iterator insert(const_iterator position, InputIterator first, InputIterator last);
111 iterator insert(const_iterator position, initializer_list<value_type> il);
112
113 iterator erase(const_iterator position);
114 iterator erase(const_iterator first, const_iterator last);
115
Howard Hinnant1c936782011-06-03 19:40:40 +0000116 void clear() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000117
118 void resize(size_type sz);
119 void resize(size_type sz, const value_type& c);
120
Howard Hinnant1c936782011-06-03 19:40:40 +0000121 void swap(vector&)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000122 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
123 allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000124
125 bool __invariants() const;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000126};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000127
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000128template <class Allocator = allocator<T> >
Howard Hinnantc51e1022010-05-11 19:42:16 +0000129class vector<bool, Allocator>
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000130{
131public:
132 typedef bool value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000133 typedef Allocator allocator_type;
134 typedef implementation-defined iterator;
135 typedef implementation-defined const_iterator;
136 typedef typename allocator_type::size_type size_type;
137 typedef typename allocator_type::difference_type difference_type;
138 typedef iterator pointer;
139 typedef const_iterator const_pointer;
140 typedef std::reverse_iterator<iterator> reverse_iterator;
141 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
142
143 class reference
144 {
145 public:
Howard Hinnant1c936782011-06-03 19:40:40 +0000146 reference(const reference&) noexcept;
147 operator bool() const noexcept;
148 reference& operator=(const bool x) noexcept;
149 reference& operator=(const reference& x) noexcept;
150 iterator operator&() const noexcept;
151 void flip() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000152 };
153
154 class const_reference
155 {
156 public:
Howard Hinnant1c936782011-06-03 19:40:40 +0000157 const_reference(const reference&) noexcept;
158 operator bool() const noexcept;
159 const_iterator operator&() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000160 };
161
Howard Hinnant1c936782011-06-03 19:40:40 +0000162 vector()
163 noexcept(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnantc2734962011-09-02 20:42:31 +0000164 explicit vector(const allocator_type&);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +0000165 explicit vector(size_type n, const allocator_type& a = allocator_type()); // C++14
166 vector(size_type n, const value_type& value, const allocator_type& = allocator_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000167 template <class InputIterator>
168 vector(InputIterator first, InputIterator last, const allocator_type& = allocator_type());
169 vector(const vector& x);
Howard Hinnant1c936782011-06-03 19:40:40 +0000170 vector(vector&& x)
171 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000172 vector(initializer_list<value_type> il);
173 vector(initializer_list<value_type> il, const allocator_type& a);
174 ~vector();
175 vector& operator=(const vector& x);
Howard Hinnant1c936782011-06-03 19:40:40 +0000176 vector& operator=(vector&& x)
177 noexcept(
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000178 allocator_type::propagate_on_container_move_assignment::value ||
179 allocator_type::is_always_equal::value); // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000180 vector& operator=(initializer_list<value_type> il);
181 template <class InputIterator>
182 void assign(InputIterator first, InputIterator last);
183 void assign(size_type n, const value_type& u);
184 void assign(initializer_list<value_type> il);
185
Howard Hinnant1c936782011-06-03 19:40:40 +0000186 allocator_type get_allocator() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000187
Howard Hinnant1c936782011-06-03 19:40:40 +0000188 iterator begin() noexcept;
189 const_iterator begin() const noexcept;
190 iterator end() noexcept;
191 const_iterator end() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000192
Howard Hinnant1c936782011-06-03 19:40:40 +0000193 reverse_iterator rbegin() noexcept;
194 const_reverse_iterator rbegin() const noexcept;
195 reverse_iterator rend() noexcept;
196 const_reverse_iterator rend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000197
Howard Hinnant1c936782011-06-03 19:40:40 +0000198 const_iterator cbegin() const noexcept;
199 const_iterator cend() const noexcept;
200 const_reverse_iterator crbegin() const noexcept;
201 const_reverse_iterator crend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000202
Howard Hinnant1c936782011-06-03 19:40:40 +0000203 size_type size() const noexcept;
204 size_type max_size() const noexcept;
205 size_type capacity() const noexcept;
206 bool empty() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000207 void reserve(size_type n);
Howard Hinnant1c936782011-06-03 19:40:40 +0000208 void shrink_to_fit() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000209
210 reference operator[](size_type n);
211 const_reference operator[](size_type n) const;
212 reference at(size_type n);
213 const_reference at(size_type n) const;
214
215 reference front();
216 const_reference front() const;
217 reference back();
218 const_reference back() const;
219
220 void push_back(const value_type& x);
Marshall Clowea52cc42017-01-24 23:09:12 +0000221 template <class... Args> reference emplace_back(Args&&... args); // C++14; reference in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000222 void pop_back();
223
Marshall Clowc46bb8e2013-08-13 23:54:12 +0000224 template <class... Args> iterator emplace(const_iterator position, Args&&... args); // C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000225 iterator insert(const_iterator position, const value_type& x);
226 iterator insert(const_iterator position, size_type n, const value_type& x);
227 template <class InputIterator>
228 iterator insert(const_iterator position, InputIterator first, InputIterator last);
229 iterator insert(const_iterator position, initializer_list<value_type> il);
230
231 iterator erase(const_iterator position);
232 iterator erase(const_iterator first, const_iterator last);
233
Howard Hinnant1c936782011-06-03 19:40:40 +0000234 void clear() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000235
236 void resize(size_type sz);
237 void resize(size_type sz, value_type x);
238
Howard Hinnant1c936782011-06-03 19:40:40 +0000239 void swap(vector&)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000240 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
241 allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnant1c936782011-06-03 19:40:40 +0000242 void flip() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000243
244 bool __invariants() const;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000245};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000246
247template <class Allocator> struct hash<std::vector<bool, Allocator>>;
248
249template <class T, class Allocator> bool operator==(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
250template <class T, class Allocator> bool operator< (const vector<T,Allocator>& x, const vector<T,Allocator>& y);
251template <class T, class Allocator> bool operator!=(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
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);
255
Howard Hinnant1c936782011-06-03 19:40:40 +0000256template <class T, class Allocator>
257void swap(vector<T,Allocator>& x, vector<T,Allocator>& y)
258 noexcept(noexcept(x.swap(y)));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000259
260} // std
261
262*/
263
264#include <__config>
Eric Fiselier876c6862016-02-20 00:19:45 +0000265#include <iosfwd> // for forward declaration of vector
Howard Hinnantc51e1022010-05-11 19:42:16 +0000266#include <__bit_reference>
267#include <type_traits>
268#include <climits>
269#include <limits>
270#include <initializer_list>
271#include <memory>
272#include <stdexcept>
273#include <algorithm>
274#include <cstring>
275#include <__split_buffer>
276#include <__functional_base>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000277
Eric Fiselier14b6de92014-08-10 23:53:08 +0000278#include <__debug>
Howard Hinnante6ff0b62013-08-02 00:26:35 +0000279
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000280#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000281#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000282#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000283
Eric Fiselierf4433a32017-05-31 22:07:49 +0000284_LIBCPP_PUSH_MACROS
285#include <__undef_macros>
286
287
Howard Hinnantc51e1022010-05-11 19:42:16 +0000288_LIBCPP_BEGIN_NAMESPACE_STD
289
290template <bool>
291class __vector_base_common
292{
293protected:
294 _LIBCPP_ALWAYS_INLINE __vector_base_common() {}
Marshall Clow8fea1612016-08-25 15:09:01 +0000295 _LIBCPP_NORETURN void __throw_length_error() const;
296 _LIBCPP_NORETURN void __throw_out_of_range() const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000297};
298
299template <bool __b>
300void
301__vector_base_common<__b>::__throw_length_error() const
302{
Marshall Clow8fea1612016-08-25 15:09:01 +0000303 _VSTD::__throw_length_error("vector");
Howard Hinnantc51e1022010-05-11 19:42:16 +0000304}
305
306template <bool __b>
307void
308__vector_base_common<__b>::__throw_out_of_range() const
309{
Marshall Clow8fea1612016-08-25 15:09:01 +0000310 _VSTD::__throw_out_of_range("vector");
Howard Hinnantc51e1022010-05-11 19:42:16 +0000311}
312
Howard Hinnant13d8bc12013-08-01 18:17:34 +0000313#ifdef _LIBCPP_MSVC
Howard Hinnantbf074022011-10-22 20:59:45 +0000314#pragma warning( push )
315#pragma warning( disable: 4231 )
Howard Hinnant13d8bc12013-08-01 18:17:34 +0000316#endif // _LIBCPP_MSVC
Eric Fiselier1b57fa82016-09-15 22:27:07 +0000317_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __vector_base_common<true>)
Howard Hinnant13d8bc12013-08-01 18:17:34 +0000318#ifdef _LIBCPP_MSVC
Howard Hinnantbf074022011-10-22 20:59:45 +0000319#pragma warning( pop )
Howard Hinnant13d8bc12013-08-01 18:17:34 +0000320#endif // _LIBCPP_MSVC
Howard Hinnantc51e1022010-05-11 19:42:16 +0000321
322template <class _Tp, class _Allocator>
323class __vector_base
324 : protected __vector_base_common<true>
325{
326protected:
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000327 typedef _Tp value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000328 typedef _Allocator allocator_type;
329 typedef allocator_traits<allocator_type> __alloc_traits;
330 typedef value_type& reference;
331 typedef const value_type& const_reference;
332 typedef typename __alloc_traits::size_type size_type;
333 typedef typename __alloc_traits::difference_type difference_type;
334 typedef typename __alloc_traits::pointer pointer;
335 typedef typename __alloc_traits::const_pointer const_pointer;
336 typedef pointer iterator;
337 typedef const_pointer const_iterator;
338
339 pointer __begin_;
340 pointer __end_;
341 __compressed_pair<pointer, allocator_type> __end_cap_;
342
Howard Hinnant1c936782011-06-03 19:40:40 +0000343 _LIBCPP_INLINE_VISIBILITY
344 allocator_type& __alloc() _NOEXCEPT
345 {return __end_cap_.second();}
346 _LIBCPP_INLINE_VISIBILITY
347 const allocator_type& __alloc() const _NOEXCEPT
348 {return __end_cap_.second();}
349 _LIBCPP_INLINE_VISIBILITY
350 pointer& __end_cap() _NOEXCEPT
351 {return __end_cap_.first();}
352 _LIBCPP_INLINE_VISIBILITY
353 const pointer& __end_cap() const _NOEXCEPT
354 {return __end_cap_.first();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000355
Howard Hinnant1c936782011-06-03 19:40:40 +0000356 _LIBCPP_INLINE_VISIBILITY
357 __vector_base()
358 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnantcf823322010-12-17 14:46:43 +0000359 _LIBCPP_INLINE_VISIBILITY __vector_base(const allocator_type& __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000360 ~__vector_base();
361
Howard Hinnant1c936782011-06-03 19:40:40 +0000362 _LIBCPP_INLINE_VISIBILITY
363 void clear() _NOEXCEPT {__destruct_at_end(__begin_);}
364 _LIBCPP_INLINE_VISIBILITY
365 size_type capacity() const _NOEXCEPT
366 {return static_cast<size_type>(__end_cap() - __begin_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000367
Howard Hinnant1c936782011-06-03 19:40:40 +0000368 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76053d72013-06-27 19:35:32 +0000369 void __destruct_at_end(pointer __new_last) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000370
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000371 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000372 void __copy_assign_alloc(const __vector_base& __c)
373 {__copy_assign_alloc(__c, integral_constant<bool,
374 __alloc_traits::propagate_on_container_copy_assignment::value>());}
375
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000376 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000377 void __move_assign_alloc(__vector_base& __c)
Howard Hinnant1c936782011-06-03 19:40:40 +0000378 _NOEXCEPT_(
379 !__alloc_traits::propagate_on_container_move_assignment::value ||
380 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000381 {__move_assign_alloc(__c, integral_constant<bool,
382 __alloc_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000383private:
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000384 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000385 void __copy_assign_alloc(const __vector_base& __c, true_type)
386 {
387 if (__alloc() != __c.__alloc())
388 {
389 clear();
390 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
391 __begin_ = __end_ = __end_cap() = nullptr;
392 }
393 __alloc() = __c.__alloc();
394 }
395
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000396 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +0000397 void __copy_assign_alloc(const __vector_base&, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000398 {}
399
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000400 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +0000401 void __move_assign_alloc(__vector_base& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +0000402 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000403 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000404 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000405 }
406
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000407 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +0000408 void __move_assign_alloc(__vector_base&, false_type)
Howard Hinnant1c936782011-06-03 19:40:40 +0000409 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000410 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000411};
412
413template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000414inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000415void
Howard Hinnant76053d72013-06-27 19:35:32 +0000416__vector_base<_Tp, _Allocator>::__destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000417{
Bruce Mitchener405fdcd2017-03-23 14:39:23 +0000418 pointer __soon_to_be_end = __end_;
419 while (__new_last != __soon_to_be_end)
420 __alloc_traits::destroy(__alloc(), _VSTD::__to_raw_pointer(--__soon_to_be_end));
421 __end_ = __new_last;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000422}
423
424template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000425inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000426__vector_base<_Tp, _Allocator>::__vector_base()
Howard Hinnant1c936782011-06-03 19:40:40 +0000427 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant76053d72013-06-27 19:35:32 +0000428 : __begin_(nullptr),
429 __end_(nullptr),
430 __end_cap_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000431{
432}
433
434template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000435inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000436__vector_base<_Tp, _Allocator>::__vector_base(const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +0000437 : __begin_(nullptr),
438 __end_(nullptr),
439 __end_cap_(nullptr, __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000440{
441}
442
443template <class _Tp, class _Allocator>
444__vector_base<_Tp, _Allocator>::~__vector_base()
445{
Howard Hinnant76053d72013-06-27 19:35:32 +0000446 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000447 {
448 clear();
449 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
450 }
451}
452
Eric Fiselier876c6862016-02-20 00:19:45 +0000453template <class _Tp, class _Allocator /* = allocator<_Tp> */>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000454class _LIBCPP_TEMPLATE_VIS vector
Howard Hinnantc51e1022010-05-11 19:42:16 +0000455 : private __vector_base<_Tp, _Allocator>
456{
457private:
458 typedef __vector_base<_Tp, _Allocator> __base;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000459 typedef allocator<_Tp> __default_allocator_type;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000460public:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000461 typedef vector __self;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000462 typedef _Tp value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000463 typedef _Allocator allocator_type;
464 typedef typename __base::__alloc_traits __alloc_traits;
465 typedef typename __base::reference reference;
466 typedef typename __base::const_reference const_reference;
467 typedef typename __base::size_type size_type;
468 typedef typename __base::difference_type difference_type;
469 typedef typename __base::pointer pointer;
470 typedef typename __base::const_pointer const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000471 typedef __wrap_iter<pointer> iterator;
472 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000473 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
474 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000475
Howard Hinnanta416ff02013-03-26 19:04:56 +0000476 static_assert((is_same<typename allocator_type::value_type, value_type>::value),
477 "Allocator::value_type must be same type as value_type");
478
Howard Hinnant1c936782011-06-03 19:40:40 +0000479 _LIBCPP_INLINE_VISIBILITY
Marshall Clowe546cbb2015-06-04 02:05:41 +0000480 vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant27e0e772011-09-14 18:33:51 +0000481 {
Howard Hinnanta47c6d52011-09-16 17:29:17 +0000482#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000483 __get_db()->__insert_c(this);
484#endif
485 }
486 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
Marshall Clow8f282f42015-06-04 00:10:20 +0000487#if _LIBCPP_STD_VER <= 14
488 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
489#else
490 _NOEXCEPT
491#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +0000492 : __base(__a)
493 {
Howard Hinnanta47c6d52011-09-16 17:29:17 +0000494#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000495 __get_db()->__insert_c(this);
496#endif
497 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000498 explicit vector(size_type __n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +0000499#if _LIBCPP_STD_VER > 11
500 explicit vector(size_type __n, const allocator_type& __a);
501#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000502 vector(size_type __n, const_reference __x);
503 vector(size_type __n, const_reference __x, const allocator_type& __a);
504 template <class _InputIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +0000505 vector(_InputIterator __first,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000506 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000507 !__is_forward_iterator<_InputIterator>::value &&
508 is_constructible<
509 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +0000510 typename iterator_traits<_InputIterator>::reference>::value,
511 _InputIterator>::type __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000512 template <class _InputIterator>
513 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
514 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000515 !__is_forward_iterator<_InputIterator>::value &&
516 is_constructible<
517 value_type,
518 typename iterator_traits<_InputIterator>::reference>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000519 template <class _ForwardIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +0000520 vector(_ForwardIterator __first,
Howard Hinnant88010252013-03-28 17:44:32 +0000521 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
522 is_constructible<
523 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +0000524 typename iterator_traits<_ForwardIterator>::reference>::value,
525 _ForwardIterator>::type __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000526 template <class _ForwardIterator>
527 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Howard Hinnant88010252013-03-28 17:44:32 +0000528 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
529 is_constructible<
530 value_type,
531 typename iterator_traits<_ForwardIterator>::reference>::value>::type* = 0);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000532
Howard Hinnanta47c6d52011-09-16 17:29:17 +0000533#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000534 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant27e0e772011-09-14 18:33:51 +0000535 ~vector()
536 {
537 __get_db()->__erase_c(this);
538 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000539#endif
540
541 vector(const vector& __x);
542 vector(const vector& __x, const allocator_type& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000543 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000544 vector& operator=(const vector& __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000545
546#ifndef _LIBCPP_CXX03_LANG
547 _LIBCPP_INLINE_VISIBILITY
548 vector(initializer_list<value_type> __il);
549
550 _LIBCPP_INLINE_VISIBILITY
551 vector(initializer_list<value_type> __il, const allocator_type& __a);
552
Howard Hinnantcf823322010-12-17 14:46:43 +0000553 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000554 vector(vector&& __x)
Marshall Clowe5108202015-07-14 14:46:32 +0000555#if _LIBCPP_STD_VER > 14
556 _NOEXCEPT;
557#else
Howard Hinnant1c936782011-06-03 19:40:40 +0000558 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowe5108202015-07-14 14:46:32 +0000559#endif
Eric Fiseliered9e9362017-04-16 02:40:45 +0000560
Howard Hinnantcf823322010-12-17 14:46:43 +0000561 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000562 vector(vector&& __x, const allocator_type& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000563 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000564 vector& operator=(vector&& __x)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000565 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiseliered9e9362017-04-16 02:40:45 +0000566
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000567 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000568 vector& operator=(initializer_list<value_type> __il)
569 {assign(__il.begin(), __il.end()); return *this;}
Eric Fiseliered9e9362017-04-16 02:40:45 +0000570
571#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000572
573 template <class _InputIterator>
574 typename enable_if
575 <
576 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000577 !__is_forward_iterator<_InputIterator>::value &&
578 is_constructible<
579 value_type,
580 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000581 void
582 >::type
583 assign(_InputIterator __first, _InputIterator __last);
584 template <class _ForwardIterator>
585 typename enable_if
586 <
Howard Hinnant88010252013-03-28 17:44:32 +0000587 __is_forward_iterator<_ForwardIterator>::value &&
588 is_constructible<
589 value_type,
590 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000591 void
592 >::type
593 assign(_ForwardIterator __first, _ForwardIterator __last);
594
595 void assign(size_type __n, const_reference __u);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000596
597#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000598 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000599 void assign(initializer_list<value_type> __il)
600 {assign(__il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +0000601#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000602
Howard Hinnant1c936782011-06-03 19:40:40 +0000603 _LIBCPP_INLINE_VISIBILITY
604 allocator_type get_allocator() const _NOEXCEPT
605 {return this->__alloc();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000606
Howard Hinnant1c936782011-06-03 19:40:40 +0000607 _LIBCPP_INLINE_VISIBILITY iterator begin() _NOEXCEPT;
608 _LIBCPP_INLINE_VISIBILITY const_iterator begin() const _NOEXCEPT;
609 _LIBCPP_INLINE_VISIBILITY iterator end() _NOEXCEPT;
610 _LIBCPP_INLINE_VISIBILITY const_iterator end() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000611
Howard Hinnant1c936782011-06-03 19:40:40 +0000612 _LIBCPP_INLINE_VISIBILITY
613 reverse_iterator rbegin() _NOEXCEPT
614 {return reverse_iterator(end());}
615 _LIBCPP_INLINE_VISIBILITY
616 const_reverse_iterator rbegin() const _NOEXCEPT
617 {return const_reverse_iterator(end());}
618 _LIBCPP_INLINE_VISIBILITY
619 reverse_iterator rend() _NOEXCEPT
620 {return reverse_iterator(begin());}
621 _LIBCPP_INLINE_VISIBILITY
622 const_reverse_iterator rend() const _NOEXCEPT
623 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000624
Howard Hinnant1c936782011-06-03 19:40:40 +0000625 _LIBCPP_INLINE_VISIBILITY
626 const_iterator cbegin() const _NOEXCEPT
627 {return begin();}
628 _LIBCPP_INLINE_VISIBILITY
629 const_iterator cend() const _NOEXCEPT
630 {return end();}
631 _LIBCPP_INLINE_VISIBILITY
632 const_reverse_iterator crbegin() const _NOEXCEPT
633 {return rbegin();}
634 _LIBCPP_INLINE_VISIBILITY
635 const_reverse_iterator crend() const _NOEXCEPT
636 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000637
Howard Hinnant1c936782011-06-03 19:40:40 +0000638 _LIBCPP_INLINE_VISIBILITY
639 size_type size() const _NOEXCEPT
640 {return static_cast<size_type>(this->__end_ - this->__begin_);}
641 _LIBCPP_INLINE_VISIBILITY
642 size_type capacity() const _NOEXCEPT
643 {return __base::capacity();}
644 _LIBCPP_INLINE_VISIBILITY
645 bool empty() const _NOEXCEPT
646 {return this->__begin_ == this->__end_;}
647 size_type max_size() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000648 void reserve(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +0000649 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000650
651 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n);
652 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const;
653 reference at(size_type __n);
654 const_reference at(size_type __n) const;
655
Howard Hinnant27e0e772011-09-14 18:33:51 +0000656 _LIBCPP_INLINE_VISIBILITY reference front()
657 {
658 _LIBCPP_ASSERT(!empty(), "front() called for empty vector");
659 return *this->__begin_;
660 }
661 _LIBCPP_INLINE_VISIBILITY const_reference front() const
662 {
663 _LIBCPP_ASSERT(!empty(), "front() called for empty vector");
664 return *this->__begin_;
665 }
666 _LIBCPP_INLINE_VISIBILITY reference back()
667 {
668 _LIBCPP_ASSERT(!empty(), "back() called for empty vector");
669 return *(this->__end_ - 1);
670 }
671 _LIBCPP_INLINE_VISIBILITY const_reference back() const
672 {
673 _LIBCPP_ASSERT(!empty(), "back() called for empty vector");
674 return *(this->__end_ - 1);
675 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000676
Howard Hinnant1c936782011-06-03 19:40:40 +0000677 _LIBCPP_INLINE_VISIBILITY
678 value_type* data() _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000679 {return _VSTD::__to_raw_pointer(this->__begin_);}
Howard Hinnant1c936782011-06-03 19:40:40 +0000680 _LIBCPP_INLINE_VISIBILITY
681 const value_type* data() const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000682 {return _VSTD::__to_raw_pointer(this->__begin_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000683
Howard Hinnantcf823322010-12-17 14:46:43 +0000684 _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000685
686#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000687 _LIBCPP_INLINE_VISIBILITY void push_back(value_type&& __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000688
Howard Hinnantc51e1022010-05-11 19:42:16 +0000689 template <class... _Args>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000690 _LIBCPP_INLINE_VISIBILITY
Marshall Clowea52cc42017-01-24 23:09:12 +0000691#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +0000692 reference emplace_back(_Args&&... __args);
Marshall Clowea52cc42017-01-24 23:09:12 +0000693#else
694 void emplace_back(_Args&&... __args);
695#endif
Eric Fiseliered9e9362017-04-16 02:40:45 +0000696#endif // !_LIBCPP_CXX03_LANG
697
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000698 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000699 void pop_back();
700
701 iterator insert(const_iterator __position, const_reference __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000702
703#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000704 iterator insert(const_iterator __position, value_type&& __x);
705 template <class... _Args>
706 iterator emplace(const_iterator __position, _Args&&... __args);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000707#endif // !_LIBCPP_CXX03_LANG
708
Howard Hinnantc51e1022010-05-11 19:42:16 +0000709 iterator insert(const_iterator __position, size_type __n, const_reference __x);
710 template <class _InputIterator>
711 typename enable_if
712 <
713 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000714 !__is_forward_iterator<_InputIterator>::value &&
715 is_constructible<
716 value_type,
717 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000718 iterator
719 >::type
720 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
721 template <class _ForwardIterator>
722 typename enable_if
723 <
Howard Hinnant88010252013-03-28 17:44:32 +0000724 __is_forward_iterator<_ForwardIterator>::value &&
725 is_constructible<
726 value_type,
727 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000728 iterator
729 >::type
730 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000731
732#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000733 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000734 iterator insert(const_iterator __position, initializer_list<value_type> __il)
735 {return insert(__position, __il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +0000736#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000737
Howard Hinnantcf823322010-12-17 14:46:43 +0000738 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000739 iterator erase(const_iterator __first, const_iterator __last);
740
Howard Hinnant1c936782011-06-03 19:40:40 +0000741 _LIBCPP_INLINE_VISIBILITY
742 void clear() _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000743 {
Marshall Clow2cd9d372014-05-08 14:14:06 +0000744 size_type __old_size = size();
Howard Hinnant27e0e772011-09-14 18:33:51 +0000745 __base::clear();
Marshall Clow2cd9d372014-05-08 14:14:06 +0000746 __annotate_shrink(__old_size);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000747 __invalidate_all_iterators();
748 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000749
750 void resize(size_type __sz);
751 void resize(size_type __sz, const_reference __x);
752
Howard Hinnant1c936782011-06-03 19:40:40 +0000753 void swap(vector&)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000754#if _LIBCPP_STD_VER >= 14
Eric Fiselier69c51982016-12-28 06:06:09 +0000755 _NOEXCEPT_DEBUG;
Marshall Clow8982dcd2015-07-13 20:04:56 +0000756#else
Eric Fiselier69c51982016-12-28 06:06:09 +0000757 _NOEXCEPT_DEBUG_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +0000758 __is_nothrow_swappable<allocator_type>::value);
759#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000760
761 bool __invariants() const;
762
Howard Hinnanta47c6d52011-09-16 17:29:17 +0000763#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000764
765 bool __dereferenceable(const const_iterator* __i) const;
766 bool __decrementable(const const_iterator* __i) const;
767 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
768 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
769
Howard Hinnanta47c6d52011-09-16 17:29:17 +0000770#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000771
Howard Hinnantc51e1022010-05-11 19:42:16 +0000772private:
Howard Hinnantcf823322010-12-17 14:46:43 +0000773 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Eric Fiselier69c51982016-12-28 06:06:09 +0000774 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(pointer __new_last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000775 void allocate(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +0000776 void deallocate() _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000777 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
Howard Hinnanta5589382011-01-04 19:53:31 +0000778 void __construct_at_end(size_type __n);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000779 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000780 void __construct_at_end(size_type __n, const_reference __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000781 template <class _ForwardIterator>
782 typename enable_if
783 <
784 __is_forward_iterator<_ForwardIterator>::value,
785 void
786 >::type
Eric Fiselieracfe6f02015-03-31 16:54:19 +0000787 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000788 void __append(size_type __n);
789 void __append(size_type __n, const_reference __x);
Howard Hinnantcf823322010-12-17 14:46:43 +0000790 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000791 iterator __make_iter(pointer __p) _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000792 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000793 const_iterator __make_iter(const_pointer __p) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000794 void __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v);
795 pointer __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p);
796 void __move_range(pointer __from_s, pointer __from_e, pointer __to);
Howard Hinnant1c936782011-06-03 19:40:40 +0000797 void __move_assign(vector& __c, true_type)
798 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000799 void __move_assign(vector& __c, false_type)
800 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000801 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76053d72013-06-27 19:35:32 +0000802 void __destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000803 {
Eric Fiselier69c51982016-12-28 06:06:09 +0000804 __invalidate_iterators_past(__new_last);
Marshall Clow2cd9d372014-05-08 14:14:06 +0000805 size_type __old_size = size();
Howard Hinnant27e0e772011-09-14 18:33:51 +0000806 __base::__destruct_at_end(__new_last);
Marshall Clow2cd9d372014-05-08 14:14:06 +0000807 __annotate_shrink(__old_size);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000808 }
Eric Fiseliered9e9362017-04-16 02:40:45 +0000809
810#ifndef _LIBCPP_CXX03_LANG
811 template <class _Up> void __push_back_slow_path(_Up&& __x);
812
Howard Hinnantb6c49562012-02-26 15:30:12 +0000813 template <class... _Args>
Eric Fiseliered9e9362017-04-16 02:40:45 +0000814 void __emplace_back_slow_path(_Args&&... __args);
815#else
816 template <class _Up> void __push_back_slow_path(_Up& __x);
Howard Hinnantb6c49562012-02-26 15:30:12 +0000817#endif
Eric Fiseliered9e9362017-04-16 02:40:45 +0000818
Marshall Clow2cd9d372014-05-08 14:14:06 +0000819 // The following functions are no-ops outside of AddressSanitizer mode.
820 // We call annotatations only for the default Allocator because other allocators
821 // may not meet the AddressSanitizer alignment constraints.
822 // See the documentation for __sanitizer_annotate_contiguous_container for more details.
Marshall Clow2cd9d372014-05-08 14:14:06 +0000823#ifndef _LIBCPP_HAS_NO_ASAN
Eric Fiselier6003c772016-12-23 23:37:52 +0000824 void __annotate_contiguous_container(const void *__beg, const void *__end,
825 const void *__old_mid,
826 const void *__new_mid) const
827 {
828
Marshall Clow2cd9d372014-05-08 14:14:06 +0000829 if (__beg && is_same<allocator_type, __default_allocator_type>::value)
830 __sanitizer_annotate_contiguous_container(__beg, __end, __old_mid, __new_mid);
Marshall Clow2cd9d372014-05-08 14:14:06 +0000831 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000832#else
833 _LIBCPP_INLINE_VISIBILITY
834 void __annotate_contiguous_container(const void*, const void*, const void*,
835 const void*) const {}
836#endif
837 _LIBCPP_INLINE_VISIBILITY
838 void __annotate_new(size_type __current_size) const {
Marshall Clow2cd9d372014-05-08 14:14:06 +0000839 __annotate_contiguous_container(data(), data() + capacity(),
840 data() + capacity(), data() + __current_size);
841 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000842
843 _LIBCPP_INLINE_VISIBILITY
844 void __annotate_delete() const {
Marshall Clow2cd9d372014-05-08 14:14:06 +0000845 __annotate_contiguous_container(data(), data() + capacity(),
846 data() + size(), data() + capacity());
847 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000848
849 _LIBCPP_INLINE_VISIBILITY
Kostya Serebryany4963c252014-09-02 23:43:38 +0000850 void __annotate_increase(size_type __n) const
Marshall Clow2cd9d372014-05-08 14:14:06 +0000851 {
852 __annotate_contiguous_container(data(), data() + capacity(),
853 data() + size(), data() + size() + __n);
854 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000855
856 _LIBCPP_INLINE_VISIBILITY
Kostya Serebryany4963c252014-09-02 23:43:38 +0000857 void __annotate_shrink(size_type __old_size) const
Marshall Clow2cd9d372014-05-08 14:14:06 +0000858 {
859 __annotate_contiguous_container(data(), data() + capacity(),
860 data() + __old_size, data() + size());
861 }
Marshall Clowc78ffa52014-09-03 21:37:43 +0000862#ifndef _LIBCPP_HAS_NO_ASAN
Kostya Serebryany4963c252014-09-02 23:43:38 +0000863 // The annotation for size increase should happen before the actual increase,
864 // but if an exception is thrown after that the annotation has to be undone.
865 struct __RAII_IncreaseAnnotator {
866 __RAII_IncreaseAnnotator(const vector &__v, size_type __n = 1)
Eric Fiseliera7c043e2015-03-10 00:25:20 +0000867 : __commit(false), __v(__v), __old_size(__v.size() + __n) {
Kostya Serebryany4963c252014-09-02 23:43:38 +0000868 __v.__annotate_increase(__n);
869 }
870 void __done() { __commit = true; }
871 ~__RAII_IncreaseAnnotator() {
872 if (__commit) return;
Eric Fiseliera7c043e2015-03-10 00:25:20 +0000873 __v.__annotate_shrink(__old_size);
Kostya Serebryany4963c252014-09-02 23:43:38 +0000874 }
875 bool __commit;
Kostya Serebryany4963c252014-09-02 23:43:38 +0000876 const vector &__v;
Eric Fiseliera7c043e2015-03-10 00:25:20 +0000877 size_type __old_size;
Kostya Serebryany4963c252014-09-02 23:43:38 +0000878 };
Marshall Clowc78ffa52014-09-03 21:37:43 +0000879#else
880 struct __RAII_IncreaseAnnotator {
Eric Fiselier6003c772016-12-23 23:37:52 +0000881 _LIBCPP_INLINE_VISIBILITY
882 __RAII_IncreaseAnnotator(const vector &, size_type = 1) {}
883 _LIBCPP_INLINE_VISIBILITY void __done() {}
Marshall Clowc78ffa52014-09-03 21:37:43 +0000884 };
885#endif
886
Howard Hinnantc51e1022010-05-11 19:42:16 +0000887};
888
889template <class _Tp, class _Allocator>
890void
891vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v)
892{
Marshall Clow2cd9d372014-05-08 14:14:06 +0000893 __annotate_delete();
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000894 __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, this->__end_, __v.__begin_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000895 _VSTD::swap(this->__begin_, __v.__begin_);
896 _VSTD::swap(this->__end_, __v.__end_);
897 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000898 __v.__first_ = __v.__begin_;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000899 __annotate_new(size());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000900 __invalidate_all_iterators();
901}
902
903template <class _Tp, class _Allocator>
904typename vector<_Tp, _Allocator>::pointer
905vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p)
906{
Marshall Clow2cd9d372014-05-08 14:14:06 +0000907 __annotate_delete();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000908 pointer __r = __v.__begin_;
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000909 __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, __p, __v.__begin_);
910 __alloc_traits::__construct_forward(this->__alloc(), __p, this->__end_, __v.__end_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000911 _VSTD::swap(this->__begin_, __v.__begin_);
912 _VSTD::swap(this->__end_, __v.__end_);
913 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000914 __v.__first_ = __v.__begin_;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000915 __annotate_new(size());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000916 __invalidate_all_iterators();
917 return __r;
918}
919
920// Allocate space for __n objects
921// throws length_error if __n > max_size()
922// throws (probably bad_alloc) if memory run out
923// Precondition: __begin_ == __end_ == __end_cap() == 0
924// Precondition: __n > 0
925// Postcondition: capacity() == __n
926// Postcondition: size() == 0
927template <class _Tp, class _Allocator>
928void
929vector<_Tp, _Allocator>::allocate(size_type __n)
930{
931 if (__n > max_size())
932 this->__throw_length_error();
933 this->__begin_ = this->__end_ = __alloc_traits::allocate(this->__alloc(), __n);
934 this->__end_cap() = this->__begin_ + __n;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000935 __annotate_new(0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000936}
937
938template <class _Tp, class _Allocator>
939void
Howard Hinnant1c936782011-06-03 19:40:40 +0000940vector<_Tp, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000941{
Howard Hinnant76053d72013-06-27 19:35:32 +0000942 if (this->__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000943 {
944 clear();
945 __alloc_traits::deallocate(this->__alloc(), this->__begin_, capacity());
Howard Hinnant76053d72013-06-27 19:35:32 +0000946 this->__begin_ = this->__end_ = this->__end_cap() = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000947 }
948}
949
950template <class _Tp, class _Allocator>
951typename vector<_Tp, _Allocator>::size_type
Howard Hinnant1c936782011-06-03 19:40:40 +0000952vector<_Tp, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000953{
Eric Fiselierb5d9f442016-11-23 01:18:56 +0000954 return _VSTD::min<size_type>(__alloc_traits::max_size(this->__alloc()),
955 numeric_limits<difference_type>::max());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000956}
957
958// Precondition: __new_size > capacity()
959template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000960inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000961typename vector<_Tp, _Allocator>::size_type
962vector<_Tp, _Allocator>::__recommend(size_type __new_size) const
963{
964 const size_type __ms = max_size();
965 if (__new_size > __ms)
966 this->__throw_length_error();
967 const size_type __cap = capacity();
968 if (__cap >= __ms / 2)
969 return __ms;
Alexis Hunt991d29b2011-07-29 23:31:58 +0000970 return _VSTD::max<size_type>(2*__cap, __new_size);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000971}
972
973// Default constructs __n objects starting at __end_
974// throws if construction throws
975// Precondition: __n > 0
976// Precondition: size() + __n <= capacity()
977// Postcondition: size() == size() + __n
978template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000979void
980vector<_Tp, _Allocator>::__construct_at_end(size_type __n)
981{
Howard Hinnantc51e1022010-05-11 19:42:16 +0000982 allocator_type& __a = this->__alloc();
983 do
984 {
Kostya Serebryany4963c252014-09-02 23:43:38 +0000985 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000986 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000987 ++this->__end_;
988 --__n;
Kostya Serebryany4963c252014-09-02 23:43:38 +0000989 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000990 } while (__n > 0);
991}
992
Howard Hinnantc51e1022010-05-11 19:42:16 +0000993// Copy constructs __n objects starting at __end_ from __x
994// throws if construction throws
995// Precondition: __n > 0
996// Precondition: size() + __n <= capacity()
997// Postcondition: size() == old size() + __n
998// Postcondition: [i] == __x for all i in [size() - __n, __n)
999template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001000inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001001void
1002vector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
1003{
Howard Hinnantc51e1022010-05-11 19:42:16 +00001004 allocator_type& __a = this->__alloc();
1005 do
1006 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001007 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001008 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001009 ++this->__end_;
1010 --__n;
Kostya Serebryany4963c252014-09-02 23:43:38 +00001011 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001012 } while (__n > 0);
1013}
1014
1015template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001016template <class _ForwardIterator>
1017typename enable_if
1018<
1019 __is_forward_iterator<_ForwardIterator>::value,
1020 void
1021>::type
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001022vector<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001023{
1024 allocator_type& __a = this->__alloc();
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001025 __RAII_IncreaseAnnotator __annotator(*this, __n);
1026 __alloc_traits::__construct_range_forward(__a, __first, __last, this->__end_);
1027 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001028}
1029
1030// Default constructs __n objects starting at __end_
1031// throws if construction throws
1032// Postcondition: size() == size() + __n
Howard Hinnant1c936782011-06-03 19:40:40 +00001033// Exception safety: strong.
Howard Hinnantc51e1022010-05-11 19:42:16 +00001034template <class _Tp, class _Allocator>
1035void
1036vector<_Tp, _Allocator>::__append(size_type __n)
1037{
1038 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1039 this->__construct_at_end(__n);
1040 else
1041 {
1042 allocator_type& __a = this->__alloc();
1043 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1044 __v.__construct_at_end(__n);
1045 __swap_out_circular_buffer(__v);
1046 }
1047}
1048
1049// Default constructs __n objects starting at __end_
1050// throws if construction throws
1051// Postcondition: size() == size() + __n
Howard Hinnant1c936782011-06-03 19:40:40 +00001052// Exception safety: strong.
Howard Hinnantc51e1022010-05-11 19:42:16 +00001053template <class _Tp, class _Allocator>
1054void
1055vector<_Tp, _Allocator>::__append(size_type __n, const_reference __x)
1056{
1057 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1058 this->__construct_at_end(__n, __x);
1059 else
1060 {
1061 allocator_type& __a = this->__alloc();
1062 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1063 __v.__construct_at_end(__n, __x);
1064 __swap_out_circular_buffer(__v);
1065 }
1066}
1067
1068template <class _Tp, class _Allocator>
1069vector<_Tp, _Allocator>::vector(size_type __n)
1070{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001071#if _LIBCPP_DEBUG_LEVEL >= 2
1072 __get_db()->__insert_c(this);
1073#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001074 if (__n > 0)
1075 {
1076 allocate(__n);
1077 __construct_at_end(__n);
1078 }
1079}
1080
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00001081#if _LIBCPP_STD_VER > 11
1082template <class _Tp, class _Allocator>
1083vector<_Tp, _Allocator>::vector(size_type __n, const allocator_type& __a)
1084 : __base(__a)
1085{
1086#if _LIBCPP_DEBUG_LEVEL >= 2
1087 __get_db()->__insert_c(this);
1088#endif
1089 if (__n > 0)
1090 {
1091 allocate(__n);
1092 __construct_at_end(__n);
1093 }
1094}
1095#endif
1096
Howard Hinnantc51e1022010-05-11 19:42:16 +00001097template <class _Tp, class _Allocator>
1098vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x)
1099{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001100#if _LIBCPP_DEBUG_LEVEL >= 2
1101 __get_db()->__insert_c(this);
1102#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001103 if (__n > 0)
1104 {
1105 allocate(__n);
1106 __construct_at_end(__n, __x);
1107 }
1108}
1109
1110template <class _Tp, class _Allocator>
1111vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x, const allocator_type& __a)
1112 : __base(__a)
1113{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001114#if _LIBCPP_DEBUG_LEVEL >= 2
1115 __get_db()->__insert_c(this);
1116#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001117 if (__n > 0)
1118 {
1119 allocate(__n);
1120 __construct_at_end(__n, __x);
1121 }
1122}
1123
1124template <class _Tp, class _Allocator>
1125template <class _InputIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +00001126vector<_Tp, _Allocator>::vector(_InputIterator __first,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001127 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001128 !__is_forward_iterator<_InputIterator>::value &&
1129 is_constructible<
1130 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +00001131 typename iterator_traits<_InputIterator>::reference>::value,
1132 _InputIterator>::type __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001133{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001134#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001135 __get_db()->__insert_c(this);
1136#endif
Howard Hinnant9cd22302011-09-16 18:41:29 +00001137 for (; __first != __last; ++__first)
1138 push_back(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001139}
1140
1141template <class _Tp, class _Allocator>
1142template <class _InputIterator>
1143vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
1144 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001145 !__is_forward_iterator<_InputIterator>::value &&
1146 is_constructible<
1147 value_type,
1148 typename iterator_traits<_InputIterator>::reference>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001149 : __base(__a)
1150{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001151#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001152 __get_db()->__insert_c(this);
1153#endif
Howard Hinnant9cd22302011-09-16 18:41:29 +00001154 for (; __first != __last; ++__first)
1155 push_back(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001156}
1157
1158template <class _Tp, class _Allocator>
1159template <class _ForwardIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +00001160vector<_Tp, _Allocator>::vector(_ForwardIterator __first,
Howard Hinnant88010252013-03-28 17:44:32 +00001161 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
1162 is_constructible<
1163 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +00001164 typename iterator_traits<_ForwardIterator>::reference>::value,
1165 _ForwardIterator>::type __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001166{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001167#if _LIBCPP_DEBUG_LEVEL >= 2
1168 __get_db()->__insert_c(this);
1169#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001170 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001171 if (__n > 0)
1172 {
1173 allocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001174 __construct_at_end(__first, __last, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001175 }
1176}
1177
1178template <class _Tp, class _Allocator>
1179template <class _ForwardIterator>
1180vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Howard Hinnant88010252013-03-28 17:44:32 +00001181 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
1182 is_constructible<
1183 value_type,
1184 typename iterator_traits<_ForwardIterator>::reference>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001185 : __base(__a)
1186{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001187#if _LIBCPP_DEBUG_LEVEL >= 2
1188 __get_db()->__insert_c(this);
1189#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001190 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001191 if (__n > 0)
1192 {
1193 allocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001194 __construct_at_end(__first, __last, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001195 }
1196}
1197
1198template <class _Tp, class _Allocator>
1199vector<_Tp, _Allocator>::vector(const vector& __x)
1200 : __base(__alloc_traits::select_on_container_copy_construction(__x.__alloc()))
1201{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001202#if _LIBCPP_DEBUG_LEVEL >= 2
1203 __get_db()->__insert_c(this);
1204#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001205 size_type __n = __x.size();
1206 if (__n > 0)
1207 {
1208 allocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001209 __construct_at_end(__x.__begin_, __x.__end_, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001210 }
1211}
1212
1213template <class _Tp, class _Allocator>
1214vector<_Tp, _Allocator>::vector(const vector& __x, const allocator_type& __a)
1215 : __base(__a)
1216{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001217#if _LIBCPP_DEBUG_LEVEL >= 2
1218 __get_db()->__insert_c(this);
1219#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001220 size_type __n = __x.size();
1221 if (__n > 0)
1222 {
1223 allocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001224 __construct_at_end(__x.__begin_, __x.__end_, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001225 }
1226}
1227
Eric Fiseliered9e9362017-04-16 02:40:45 +00001228#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001229
1230template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001231inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001232vector<_Tp, _Allocator>::vector(vector&& __x)
Marshall Clowe5108202015-07-14 14:46:32 +00001233#if _LIBCPP_STD_VER > 14
1234 _NOEXCEPT
1235#else
Howard Hinnant1c936782011-06-03 19:40:40 +00001236 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowe5108202015-07-14 14:46:32 +00001237#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001238 : __base(_VSTD::move(__x.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001239{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001240#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001241 __get_db()->__insert_c(this);
Howard Hinnanta1e60cd2011-09-19 16:34:29 +00001242 __get_db()->swap(this, &__x);
Howard Hinnant27e0e772011-09-14 18:33:51 +00001243#endif
Howard Hinnant9cd22302011-09-16 18:41:29 +00001244 this->__begin_ = __x.__begin_;
1245 this->__end_ = __x.__end_;
1246 this->__end_cap() = __x.__end_cap();
Howard Hinnant76053d72013-06-27 19:35:32 +00001247 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001248}
1249
1250template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001251inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001252vector<_Tp, _Allocator>::vector(vector&& __x, const allocator_type& __a)
1253 : __base(__a)
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 if (__a == __x.__alloc())
1259 {
1260 this->__begin_ = __x.__begin_;
1261 this->__end_ = __x.__end_;
1262 this->__end_cap() = __x.__end_cap();
1263 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnanta1e60cd2011-09-19 16:34:29 +00001264#if _LIBCPP_DEBUG_LEVEL >= 2
1265 __get_db()->swap(this, &__x);
1266#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001267 }
1268 else
1269 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001270 typedef move_iterator<iterator> _Ip;
1271 assign(_Ip(__x.begin()), _Ip(__x.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001272 }
1273}
1274
1275template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001276inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001277vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il)
1278{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001279#if _LIBCPP_DEBUG_LEVEL >= 2
1280 __get_db()->__insert_c(this);
1281#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001282 if (__il.size() > 0)
1283 {
1284 allocate(__il.size());
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001285 __construct_at_end(__il.begin(), __il.end(), __il.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001286 }
1287}
1288
1289template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001290inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001291vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
1292 : __base(__a)
1293{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001294#if _LIBCPP_DEBUG_LEVEL >= 2
1295 __get_db()->__insert_c(this);
1296#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001297 if (__il.size() > 0)
1298 {
1299 allocate(__il.size());
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001300 __construct_at_end(__il.begin(), __il.end(), __il.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001301 }
1302}
1303
1304template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001305inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001306vector<_Tp, _Allocator>&
1307vector<_Tp, _Allocator>::operator=(vector&& __x)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001308 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001309{
1310 __move_assign(__x, integral_constant<bool,
1311 __alloc_traits::propagate_on_container_move_assignment::value>());
1312 return *this;
1313}
1314
1315template <class _Tp, class _Allocator>
1316void
1317vector<_Tp, _Allocator>::__move_assign(vector& __c, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001318 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001319{
1320 if (__base::__alloc() != __c.__alloc())
1321 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001322 typedef move_iterator<iterator> _Ip;
1323 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001324 }
1325 else
1326 __move_assign(__c, true_type());
1327}
1328
1329template <class _Tp, class _Allocator>
1330void
1331vector<_Tp, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00001332 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001333{
1334 deallocate();
Marshall Clow136d45c2014-07-21 15:11:13 +00001335 __base::__move_assign_alloc(__c); // this can throw
Howard Hinnantc51e1022010-05-11 19:42:16 +00001336 this->__begin_ = __c.__begin_;
1337 this->__end_ = __c.__end_;
1338 this->__end_cap() = __c.__end_cap();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001339 __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
Howard Hinnanta1e60cd2011-09-19 16:34:29 +00001340#if _LIBCPP_DEBUG_LEVEL >= 2
1341 __get_db()->swap(this, &__c);
1342#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001343}
1344
Eric Fiseliered9e9362017-04-16 02:40:45 +00001345#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001346
1347template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001348inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001349vector<_Tp, _Allocator>&
1350vector<_Tp, _Allocator>::operator=(const vector& __x)
1351{
1352 if (this != &__x)
1353 {
1354 __base::__copy_assign_alloc(__x);
1355 assign(__x.__begin_, __x.__end_);
1356 }
1357 return *this;
1358}
1359
1360template <class _Tp, class _Allocator>
1361template <class _InputIterator>
1362typename enable_if
1363<
1364 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001365 !__is_forward_iterator<_InputIterator>::value &&
1366 is_constructible<
1367 _Tp,
1368 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001369 void
1370>::type
1371vector<_Tp, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
1372{
1373 clear();
1374 for (; __first != __last; ++__first)
1375 push_back(*__first);
1376}
1377
1378template <class _Tp, class _Allocator>
1379template <class _ForwardIterator>
1380typename enable_if
1381<
Howard Hinnant88010252013-03-28 17:44:32 +00001382 __is_forward_iterator<_ForwardIterator>::value &&
1383 is_constructible<
1384 _Tp,
1385 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001386 void
1387>::type
1388vector<_Tp, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
1389{
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001390 size_type __new_size = static_cast<size_type>(_VSTD::distance(__first, __last));
1391 if (__new_size <= capacity())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001392 {
1393 _ForwardIterator __mid = __last;
1394 bool __growing = false;
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001395 if (__new_size > size())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001396 {
1397 __growing = true;
1398 __mid = __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001399 _VSTD::advance(__mid, size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001400 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001401 pointer __m = _VSTD::copy(__first, __mid, this->__begin_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001402 if (__growing)
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001403 __construct_at_end(__mid, __last, __new_size - size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001404 else
1405 this->__destruct_at_end(__m);
1406 }
1407 else
1408 {
1409 deallocate();
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001410 allocate(__recommend(__new_size));
1411 __construct_at_end(__first, __last, __new_size);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001412 }
Eric Fiselier69c51982016-12-28 06:06:09 +00001413 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001414}
1415
1416template <class _Tp, class _Allocator>
1417void
1418vector<_Tp, _Allocator>::assign(size_type __n, const_reference __u)
1419{
1420 if (__n <= capacity())
1421 {
1422 size_type __s = size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001423 _VSTD::fill_n(this->__begin_, _VSTD::min(__n, __s), __u);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001424 if (__n > __s)
1425 __construct_at_end(__n - __s, __u);
1426 else
Howard Hinnant155c2af2010-05-24 17:49:41 +00001427 this->__destruct_at_end(this->__begin_ + __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001428 }
1429 else
1430 {
1431 deallocate();
1432 allocate(__recommend(static_cast<size_type>(__n)));
1433 __construct_at_end(__n, __u);
1434 }
Eric Fiselier69c51982016-12-28 06:06:09 +00001435 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001436}
1437
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001438template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001439inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001440typename vector<_Tp, _Allocator>::iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001441vector<_Tp, _Allocator>::__make_iter(pointer __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001442{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001443#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001444 return iterator(this, __p);
1445#else
1446 return iterator(__p);
1447#endif
1448}
1449
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001450template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001451inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001452typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001453vector<_Tp, _Allocator>::__make_iter(const_pointer __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001454{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001455#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001456 return const_iterator(this, __p);
1457#else
1458 return const_iterator(__p);
1459#endif
1460}
1461
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001462template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001463inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001464typename vector<_Tp, _Allocator>::iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001465vector<_Tp, _Allocator>::begin() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001466{
1467 return __make_iter(this->__begin_);
1468}
1469
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001470template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001471inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001472typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001473vector<_Tp, _Allocator>::begin() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001474{
1475 return __make_iter(this->__begin_);
1476}
1477
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001478template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001479inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001480typename vector<_Tp, _Allocator>::iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001481vector<_Tp, _Allocator>::end() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001482{
1483 return __make_iter(this->__end_);
1484}
1485
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001486template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001487inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001488typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001489vector<_Tp, _Allocator>::end() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001490{
1491 return __make_iter(this->__end_);
1492}
1493
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001494template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001495inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001496typename vector<_Tp, _Allocator>::reference
1497vector<_Tp, _Allocator>::operator[](size_type __n)
1498{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001499 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001500 return this->__begin_[__n];
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_reference
1506vector<_Tp, _Allocator>::operator[](size_type __n) const
1507{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001508 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001509 return this->__begin_[__n];
1510}
1511
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001512template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001513typename vector<_Tp, _Allocator>::reference
1514vector<_Tp, _Allocator>::at(size_type __n)
1515{
1516 if (__n >= size())
1517 this->__throw_out_of_range();
1518 return this->__begin_[__n];
1519}
1520
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001521template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001522typename vector<_Tp, _Allocator>::const_reference
1523vector<_Tp, _Allocator>::at(size_type __n) const
1524{
1525 if (__n >= size())
1526 this->__throw_out_of_range();
1527 return this->__begin_[__n];
1528}
1529
1530template <class _Tp, class _Allocator>
1531void
1532vector<_Tp, _Allocator>::reserve(size_type __n)
1533{
1534 if (__n > capacity())
1535 {
1536 allocator_type& __a = this->__alloc();
Howard Hinnant1c936782011-06-03 19:40:40 +00001537 __split_buffer<value_type, allocator_type&> __v(__n, size(), __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001538 __swap_out_circular_buffer(__v);
1539 }
1540}
1541
1542template <class _Tp, class _Allocator>
1543void
Howard Hinnant1c936782011-06-03 19:40:40 +00001544vector<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001545{
1546 if (capacity() > size())
1547 {
1548#ifndef _LIBCPP_NO_EXCEPTIONS
1549 try
1550 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001551#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001552 allocator_type& __a = this->__alloc();
Howard Hinnant1c936782011-06-03 19:40:40 +00001553 __split_buffer<value_type, allocator_type&> __v(size(), size(), __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001554 __swap_out_circular_buffer(__v);
1555#ifndef _LIBCPP_NO_EXCEPTIONS
1556 }
1557 catch (...)
1558 {
1559 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001560#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001561 }
1562}
1563
1564template <class _Tp, class _Allocator>
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001565template <class _Up>
1566void
Eric Fiseliered9e9362017-04-16 02:40:45 +00001567#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001568vector<_Tp, _Allocator>::__push_back_slow_path(_Up&& __x)
1569#else
1570vector<_Tp, _Allocator>::__push_back_slow_path(_Up& __x)
1571#endif
1572{
1573 allocator_type& __a = this->__alloc();
1574 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1575 // __v.push_back(_VSTD::forward<_Up>(__x));
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001576 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), _VSTD::forward<_Up>(__x));
1577 __v.__end_++;
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001578 __swap_out_circular_buffer(__v);
1579}
1580
1581template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001582inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001583void
1584vector<_Tp, _Allocator>::push_back(const_reference __x)
1585{
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001586 if (this->__end_ != this->__end_cap())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001587 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001588 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001589 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001590 _VSTD::__to_raw_pointer(this->__end_), __x);
Kostya Serebryany4963c252014-09-02 23:43:38 +00001591 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001592 ++this->__end_;
1593 }
1594 else
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001595 __push_back_slow_path(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001596}
1597
Eric Fiseliered9e9362017-04-16 02:40:45 +00001598#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001599
1600template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001601inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001602void
1603vector<_Tp, _Allocator>::push_back(value_type&& __x)
1604{
1605 if (this->__end_ < this->__end_cap())
1606 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001607 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001608 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001609 _VSTD::__to_raw_pointer(this->__end_),
1610 _VSTD::move(__x));
Kostya Serebryany4963c252014-09-02 23:43:38 +00001611 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001612 ++this->__end_;
1613 }
1614 else
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001615 __push_back_slow_path(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001616}
1617
1618template <class _Tp, class _Allocator>
1619template <class... _Args>
1620void
Howard Hinnantb6c49562012-02-26 15:30:12 +00001621vector<_Tp, _Allocator>::__emplace_back_slow_path(_Args&&... __args)
1622{
1623 allocator_type& __a = this->__alloc();
1624 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1625// __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001626 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), _VSTD::forward<_Args>(__args)...);
1627 __v.__end_++;
Howard Hinnantb6c49562012-02-26 15:30:12 +00001628 __swap_out_circular_buffer(__v);
1629}
1630
1631template <class _Tp, class _Allocator>
1632template <class... _Args>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001633inline
Marshall Clowea52cc42017-01-24 23:09:12 +00001634#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001635typename vector<_Tp, _Allocator>::reference
Marshall Clowea52cc42017-01-24 23:09:12 +00001636#else
1637void
1638#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001639vector<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1640{
1641 if (this->__end_ < this->__end_cap())
1642 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001643 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001644 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001645 _VSTD::__to_raw_pointer(this->__end_),
1646 _VSTD::forward<_Args>(__args)...);
Kostya Serebryany4963c252014-09-02 23:43:38 +00001647 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001648 ++this->__end_;
1649 }
1650 else
Howard Hinnantb6c49562012-02-26 15:30:12 +00001651 __emplace_back_slow_path(_VSTD::forward<_Args>(__args)...);
Marshall Clowea52cc42017-01-24 23:09:12 +00001652#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001653 return this->back();
Marshall Clowea52cc42017-01-24 23:09:12 +00001654#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001655}
1656
Eric Fiseliered9e9362017-04-16 02:40:45 +00001657#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001658
1659template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001660inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001661void
1662vector<_Tp, _Allocator>::pop_back()
1663{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001664 _LIBCPP_ASSERT(!empty(), "vector::pop_back called for empty vector");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001665 this->__destruct_at_end(this->__end_ - 1);
1666}
1667
1668template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001669inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001670typename vector<_Tp, _Allocator>::iterator
1671vector<_Tp, _Allocator>::erase(const_iterator __position)
1672{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001673#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001674 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1675 "vector::erase(iterator) called with an iterator not"
1676 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001677#endif
Howard Hinnant58d52d52013-03-25 22:12:26 +00001678 _LIBCPP_ASSERT(__position != end(),
1679 "vector::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnant76053d72013-06-27 19:35:32 +00001680 difference_type __ps = __position - cbegin();
1681 pointer __p = this->__begin_ + __ps;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001682 this->__destruct_at_end(_VSTD::move(__p + 1, this->__end_, __p));
Eric Fiselier69c51982016-12-28 06:06:09 +00001683 this->__invalidate_iterators_past(__p-1);
1684 iterator __r = __make_iter(__p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001685 return __r;
1686}
1687
1688template <class _Tp, class _Allocator>
1689typename vector<_Tp, _Allocator>::iterator
1690vector<_Tp, _Allocator>::erase(const_iterator __first, const_iterator __last)
1691{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001692#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001693 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
1694 "vector::erase(iterator, iterator) called with an iterator not"
1695 " referring to this vector");
Eric Fiselier69c51982016-12-28 06:06:09 +00001696 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__last) == this,
1697 "vector::erase(iterator, iterator) called with an iterator not"
1698 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001699#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001700 _LIBCPP_ASSERT(__first <= __last, "vector::erase(first, last) called with invalid range");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001701 pointer __p = this->__begin_ + (__first - begin());
Eric Fiselier69c51982016-12-28 06:06:09 +00001702 if (__first != __last) {
Howard Hinnantc580cc32013-04-18 15:02:57 +00001703 this->__destruct_at_end(_VSTD::move(__p + (__last - __first), this->__end_, __p));
Eric Fiselier69c51982016-12-28 06:06:09 +00001704 this->__invalidate_iterators_past(__p - 1);
1705 }
1706 iterator __r = __make_iter(__p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001707 return __r;
1708}
1709
1710template <class _Tp, class _Allocator>
1711void
1712vector<_Tp, _Allocator>::__move_range(pointer __from_s, pointer __from_e, pointer __to)
1713{
1714 pointer __old_last = this->__end_;
1715 difference_type __n = __old_last - __to;
1716 for (pointer __i = __from_s + __n; __i < __from_e; ++__i, ++this->__end_)
1717 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001718 _VSTD::__to_raw_pointer(this->__end_),
1719 _VSTD::move(*__i));
1720 _VSTD::move_backward(__from_s, __from_s + __n, __old_last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001721}
1722
1723template <class _Tp, class _Allocator>
1724typename vector<_Tp, _Allocator>::iterator
1725vector<_Tp, _Allocator>::insert(const_iterator __position, const_reference __x)
1726{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001727#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001728 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1729 "vector::insert(iterator, x) called with an iterator not"
1730 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001731#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001732 pointer __p = this->__begin_ + (__position - begin());
1733 if (this->__end_ < this->__end_cap())
1734 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001735 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001736 if (__p == this->__end_)
1737 {
1738 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001739 _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001740 ++this->__end_;
1741 }
1742 else
1743 {
1744 __move_range(__p, this->__end_, __p + 1);
1745 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1746 if (__p <= __xr && __xr < this->__end_)
1747 ++__xr;
1748 *__p = *__xr;
1749 }
Kostya Serebryany4963c252014-09-02 23:43:38 +00001750 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001751 }
1752 else
1753 {
1754 allocator_type& __a = this->__alloc();
1755 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
1756 __v.push_back(__x);
1757 __p = __swap_out_circular_buffer(__v, __p);
1758 }
1759 return __make_iter(__p);
1760}
1761
Eric Fiseliered9e9362017-04-16 02:40:45 +00001762#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001763
1764template <class _Tp, class _Allocator>
1765typename vector<_Tp, _Allocator>::iterator
1766vector<_Tp, _Allocator>::insert(const_iterator __position, value_type&& __x)
1767{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001768#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001769 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1770 "vector::insert(iterator, x) called with an iterator not"
1771 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001772#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001773 pointer __p = this->__begin_ + (__position - begin());
1774 if (this->__end_ < this->__end_cap())
1775 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001776 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001777 if (__p == this->__end_)
1778 {
1779 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001780 _VSTD::__to_raw_pointer(this->__end_),
1781 _VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001782 ++this->__end_;
1783 }
1784 else
1785 {
1786 __move_range(__p, this->__end_, __p + 1);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001787 *__p = _VSTD::move(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001788 }
Kostya Serebryany4963c252014-09-02 23:43:38 +00001789 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001790 }
1791 else
1792 {
1793 allocator_type& __a = this->__alloc();
1794 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001795 __v.push_back(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001796 __p = __swap_out_circular_buffer(__v, __p);
1797 }
1798 return __make_iter(__p);
1799}
1800
1801template <class _Tp, class _Allocator>
1802template <class... _Args>
1803typename vector<_Tp, _Allocator>::iterator
1804vector<_Tp, _Allocator>::emplace(const_iterator __position, _Args&&... __args)
1805{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001806#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001807 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1808 "vector::emplace(iterator, x) called with an iterator not"
1809 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001810#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001811 pointer __p = this->__begin_ + (__position - begin());
1812 if (this->__end_ < this->__end_cap())
1813 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001814 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001815 if (__p == this->__end_)
1816 {
1817 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001818 _VSTD::__to_raw_pointer(this->__end_),
1819 _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001820 ++this->__end_;
1821 }
1822 else
1823 {
Marshall Clowa591b9a2016-07-11 21:38:08 +00001824 __temp_value<value_type, _Allocator> __tmp(this->__alloc(), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001825 __move_range(__p, this->__end_, __p + 1);
Marshall Clowa591b9a2016-07-11 21:38:08 +00001826 *__p = _VSTD::move(__tmp.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001827 }
Kostya Serebryany4963c252014-09-02 23:43:38 +00001828 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001829 }
1830 else
1831 {
1832 allocator_type& __a = this->__alloc();
1833 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001834 __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001835 __p = __swap_out_circular_buffer(__v, __p);
1836 }
1837 return __make_iter(__p);
1838}
1839
Eric Fiseliered9e9362017-04-16 02:40:45 +00001840#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001841
1842template <class _Tp, class _Allocator>
1843typename vector<_Tp, _Allocator>::iterator
1844vector<_Tp, _Allocator>::insert(const_iterator __position, size_type __n, const_reference __x)
1845{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001846#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001847 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1848 "vector::insert(iterator, n, x) called with an iterator not"
1849 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001850#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001851 pointer __p = this->__begin_ + (__position - begin());
1852 if (__n > 0)
1853 {
1854 if (__n <= static_cast<size_type>(this->__end_cap() - this->__end_))
1855 {
1856 size_type __old_n = __n;
1857 pointer __old_last = this->__end_;
1858 if (__n > static_cast<size_type>(this->__end_ - __p))
1859 {
1860 size_type __cx = __n - (this->__end_ - __p);
1861 __construct_at_end(__cx, __x);
1862 __n -= __cx;
1863 }
1864 if (__n > 0)
1865 {
Eric Fiselier2a649652014-11-14 18:28:36 +00001866 __RAII_IncreaseAnnotator __annotator(*this, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001867 __move_range(__p, __old_last, __p + __old_n);
Kostya Serebryany4963c252014-09-02 23:43:38 +00001868 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001869 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1870 if (__p <= __xr && __xr < this->__end_)
1871 __xr += __old_n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001872 _VSTD::fill_n(__p, __n, *__xr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001873 }
1874 }
1875 else
1876 {
1877 allocator_type& __a = this->__alloc();
1878 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1879 __v.__construct_at_end(__n, __x);
1880 __p = __swap_out_circular_buffer(__v, __p);
1881 }
1882 }
1883 return __make_iter(__p);
1884}
1885
1886template <class _Tp, class _Allocator>
1887template <class _InputIterator>
1888typename enable_if
1889<
1890 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001891 !__is_forward_iterator<_InputIterator>::value &&
1892 is_constructible<
1893 _Tp,
1894 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001895 typename vector<_Tp, _Allocator>::iterator
1896>::type
1897vector<_Tp, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
1898{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001899#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001900 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1901 "vector::insert(iterator, range) called with an iterator not"
1902 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001903#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001904 difference_type __off = __position - begin();
1905 pointer __p = this->__begin_ + __off;
1906 allocator_type& __a = this->__alloc();
1907 pointer __old_last = this->__end_;
1908 for (; this->__end_ != this->__end_cap() && __first != __last; ++__first)
1909 {
Eric Fiselier489fd502015-07-18 18:22:12 +00001910 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001911 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001912 *__first);
1913 ++this->__end_;
Eric Fiselier489fd502015-07-18 18:22:12 +00001914 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001915 }
1916 __split_buffer<value_type, allocator_type&> __v(__a);
1917 if (__first != __last)
1918 {
1919#ifndef _LIBCPP_NO_EXCEPTIONS
1920 try
1921 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001922#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001923 __v.__construct_at_end(__first, __last);
1924 difference_type __old_size = __old_last - this->__begin_;
1925 difference_type __old_p = __p - this->__begin_;
1926 reserve(__recommend(size() + __v.size()));
1927 __p = this->__begin_ + __old_p;
1928 __old_last = this->__begin_ + __old_size;
1929#ifndef _LIBCPP_NO_EXCEPTIONS
1930 }
1931 catch (...)
1932 {
1933 erase(__make_iter(__old_last), end());
1934 throw;
1935 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001936#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001937 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001938 __p = _VSTD::rotate(__p, __old_last, this->__end_);
Howard Hinnant9cd22302011-09-16 18:41:29 +00001939 insert(__make_iter(__p), make_move_iterator(__v.begin()),
1940 make_move_iterator(__v.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001941 return begin() + __off;
1942}
1943
1944template <class _Tp, class _Allocator>
1945template <class _ForwardIterator>
1946typename enable_if
1947<
Howard Hinnant88010252013-03-28 17:44:32 +00001948 __is_forward_iterator<_ForwardIterator>::value &&
1949 is_constructible<
1950 _Tp,
1951 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001952 typename vector<_Tp, _Allocator>::iterator
1953>::type
1954vector<_Tp, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
1955{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001956#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001957 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1958 "vector::insert(iterator, range) called with an iterator not"
1959 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001960#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001961 pointer __p = this->__begin_ + (__position - begin());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001962 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001963 if (__n > 0)
1964 {
1965 if (__n <= this->__end_cap() - this->__end_)
1966 {
1967 size_type __old_n = __n;
1968 pointer __old_last = this->__end_;
1969 _ForwardIterator __m = __last;
1970 difference_type __dx = this->__end_ - __p;
1971 if (__n > __dx)
1972 {
1973 __m = __first;
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001974 difference_type __diff = this->__end_ - __p;
1975 _VSTD::advance(__m, __diff);
1976 __construct_at_end(__m, __last, __n - __diff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001977 __n = __dx;
1978 }
1979 if (__n > 0)
1980 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001981 __RAII_IncreaseAnnotator __annotator(*this, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001982 __move_range(__p, __old_last, __p + __old_n);
Kostya Serebryany4963c252014-09-02 23:43:38 +00001983 __annotator.__done();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001984 _VSTD::copy(__first, __m, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001985 }
1986 }
1987 else
1988 {
1989 allocator_type& __a = this->__alloc();
1990 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1991 __v.__construct_at_end(__first, __last);
1992 __p = __swap_out_circular_buffer(__v, __p);
1993 }
1994 }
1995 return __make_iter(__p);
1996}
1997
1998template <class _Tp, class _Allocator>
1999void
2000vector<_Tp, _Allocator>::resize(size_type __sz)
2001{
2002 size_type __cs = size();
2003 if (__cs < __sz)
2004 this->__append(__sz - __cs);
2005 else if (__cs > __sz)
2006 this->__destruct_at_end(this->__begin_ + __sz);
2007}
2008
2009template <class _Tp, class _Allocator>
2010void
2011vector<_Tp, _Allocator>::resize(size_type __sz, const_reference __x)
2012{
2013 size_type __cs = size();
2014 if (__cs < __sz)
2015 this->__append(__sz - __cs, __x);
2016 else if (__cs > __sz)
2017 this->__destruct_at_end(this->__begin_ + __sz);
2018}
2019
2020template <class _Tp, class _Allocator>
2021void
2022vector<_Tp, _Allocator>::swap(vector& __x)
Marshall Clow8982dcd2015-07-13 20:04:56 +00002023#if _LIBCPP_STD_VER >= 14
Eric Fiselier69c51982016-12-28 06:06:09 +00002024 _NOEXCEPT_DEBUG
Marshall Clow8982dcd2015-07-13 20:04:56 +00002025#else
Eric Fiselier69c51982016-12-28 06:06:09 +00002026 _NOEXCEPT_DEBUG_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00002027 __is_nothrow_swappable<allocator_type>::value)
2028#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002029{
Howard Hinnant27e0e772011-09-14 18:33:51 +00002030 _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
2031 this->__alloc() == __x.__alloc(),
2032 "vector::swap: Either propagate_on_container_swap must be true"
2033 " or the allocators must compare equal");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002034 _VSTD::swap(this->__begin_, __x.__begin_);
2035 _VSTD::swap(this->__end_, __x.__end_);
2036 _VSTD::swap(this->__end_cap(), __x.__end_cap());
Eric Fiseliered9e9362017-04-16 02:40:45 +00002037 __swap_allocator(this->__alloc(), __x.__alloc(),
Marshall Clow8982dcd2015-07-13 20:04:56 +00002038 integral_constant<bool,__alloc_traits::propagate_on_container_swap::value>());
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002039#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002040 __get_db()->swap(this, &__x);
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002041#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00002042}
2043
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002044template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002045bool
2046vector<_Tp, _Allocator>::__invariants() const
2047{
Howard Hinnant76053d72013-06-27 19:35:32 +00002048 if (this->__begin_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002049 {
Howard Hinnant76053d72013-06-27 19:35:32 +00002050 if (this->__end_ != nullptr || this->__end_cap() != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002051 return false;
2052 }
2053 else
2054 {
2055 if (this->__begin_ > this->__end_)
2056 return false;
2057 if (this->__begin_ == this->__end_cap())
2058 return false;
2059 if (this->__end_ > this->__end_cap())
2060 return false;
2061 }
2062 return true;
2063}
2064
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002065#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002066
Howard Hinnantc51e1022010-05-11 19:42:16 +00002067template <class _Tp, class _Allocator>
Howard Hinnant27e0e772011-09-14 18:33:51 +00002068bool
2069vector<_Tp, _Allocator>::__dereferenceable(const const_iterator* __i) const
2070{
2071 return this->__begin_ <= __i->base() && __i->base() < this->__end_;
2072}
2073
2074template <class _Tp, class _Allocator>
2075bool
2076vector<_Tp, _Allocator>::__decrementable(const const_iterator* __i) const
2077{
2078 return this->__begin_ < __i->base() && __i->base() <= this->__end_;
2079}
2080
2081template <class _Tp, class _Allocator>
2082bool
2083vector<_Tp, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
2084{
2085 const_pointer __p = __i->base() + __n;
2086 return this->__begin_ <= __p && __p <= this->__end_;
2087}
2088
2089template <class _Tp, class _Allocator>
2090bool
2091vector<_Tp, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
2092{
2093 const_pointer __p = __i->base() + __n;
2094 return this->__begin_ <= __p && __p < this->__end_;
2095}
2096
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002097#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002098
2099template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002100inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002101void
2102vector<_Tp, _Allocator>::__invalidate_all_iterators()
2103{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002104#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002105 __get_db()->__invalidate_all(this);
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002106#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00002107}
2108
Eric Fiselier69c51982016-12-28 06:06:09 +00002109
2110template <class _Tp, class _Allocator>
2111inline _LIBCPP_INLINE_VISIBILITY
2112void
2113vector<_Tp, _Allocator>::__invalidate_iterators_past(pointer __new_last) {
2114#if _LIBCPP_DEBUG_LEVEL >= 2
2115 __c_node* __c = __get_db()->__find_c_and_lock(this);
2116 for (__i_node** __p = __c->end_; __p != __c->beg_; ) {
2117 --__p;
2118 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
2119 if (__i->base() > __new_last) {
2120 (*__p)->__c_ = nullptr;
2121 if (--__c->end_ != __p)
2122 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
2123 }
2124 }
2125 __get_db()->unlock();
2126#else
2127 ((void)__new_last);
2128#endif
2129}
2130
Howard Hinnantc51e1022010-05-11 19:42:16 +00002131// vector<bool>
2132
2133template <class _Allocator> class vector<bool, _Allocator>;
2134
2135template <class _Allocator> struct hash<vector<bool, _Allocator> >;
2136
2137template <class _Allocator>
Howard Hinnantb4ef6482011-07-02 20:33:23 +00002138struct __has_storage_type<vector<bool, _Allocator> >
2139{
2140 static const bool value = true;
2141};
2142
2143template <class _Allocator>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002144class _LIBCPP_TEMPLATE_VIS vector<bool, _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002145 : private __vector_base_common<true>
2146{
2147public:
2148 typedef vector __self;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002149 typedef bool value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002150 typedef _Allocator allocator_type;
2151 typedef allocator_traits<allocator_type> __alloc_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002152 typedef typename __alloc_traits::size_type size_type;
2153 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant896b7622012-05-07 16:50:38 +00002154 typedef size_type __storage_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002155 typedef __bit_iterator<vector, false> pointer;
2156 typedef __bit_iterator<vector, true> const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002157 typedef pointer iterator;
2158 typedef const_pointer const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002159 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
2160 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002161
2162private:
Marshall Clow940e01c2015-04-07 05:21:38 +00002163 typedef typename __rebind_alloc_helper<__alloc_traits, __storage_type>::type __storage_allocator;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002164 typedef allocator_traits<__storage_allocator> __storage_traits;
2165 typedef typename __storage_traits::pointer __storage_pointer;
2166 typedef typename __storage_traits::const_pointer __const_storage_pointer;
2167
2168 __storage_pointer __begin_;
2169 size_type __size_;
2170 __compressed_pair<size_type, __storage_allocator> __cap_alloc_;
Howard Hinnant71d1c192011-07-09 15:50:42 +00002171public:
Howard Hinnantb4ef6482011-07-02 20:33:23 +00002172 typedef __bit_reference<vector> reference;
2173 typedef __bit_const_reference<vector> const_reference;
Howard Hinnant71d1c192011-07-09 15:50:42 +00002174private:
Howard Hinnant1c936782011-06-03 19:40:40 +00002175 _LIBCPP_INLINE_VISIBILITY
2176 size_type& __cap() _NOEXCEPT
2177 {return __cap_alloc_.first();}
2178 _LIBCPP_INLINE_VISIBILITY
2179 const size_type& __cap() const _NOEXCEPT
2180 {return __cap_alloc_.first();}
2181 _LIBCPP_INLINE_VISIBILITY
2182 __storage_allocator& __alloc() _NOEXCEPT
2183 {return __cap_alloc_.second();}
2184 _LIBCPP_INLINE_VISIBILITY
2185 const __storage_allocator& __alloc() const _NOEXCEPT
2186 {return __cap_alloc_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002187
2188 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
2189
Howard Hinnant1c936782011-06-03 19:40:40 +00002190 _LIBCPP_INLINE_VISIBILITY
2191 static size_type __internal_cap_to_external(size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002192 {return __n * __bits_per_word;}
Howard Hinnant1c936782011-06-03 19:40:40 +00002193 _LIBCPP_INLINE_VISIBILITY
2194 static size_type __external_cap_to_internal(size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002195 {return (__n - 1) / __bits_per_word + 1;}
2196
2197public:
Howard Hinnant1c936782011-06-03 19:40:40 +00002198 _LIBCPP_INLINE_VISIBILITY
Marshall Clowe546cbb2015-06-04 02:05:41 +00002199 vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clow8f282f42015-06-04 00:10:20 +00002200
2201 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
2202#if _LIBCPP_STD_VER <= 14
2203 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
2204#else
2205 _NOEXCEPT;
2206#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002207 ~vector();
2208 explicit vector(size_type __n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00002209#if _LIBCPP_STD_VER > 11
2210 explicit vector(size_type __n, const allocator_type& __a);
2211#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002212 vector(size_type __n, const value_type& __v);
2213 vector(size_type __n, const value_type& __v, const allocator_type& __a);
2214 template <class _InputIterator>
2215 vector(_InputIterator __first, _InputIterator __last,
2216 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2217 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2218 template <class _InputIterator>
2219 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2220 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2221 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2222 template <class _ForwardIterator>
2223 vector(_ForwardIterator __first, _ForwardIterator __last,
2224 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2225 template <class _ForwardIterator>
2226 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2227 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2228
2229 vector(const vector& __v);
2230 vector(const vector& __v, const allocator_type& __a);
2231 vector& operator=(const vector& __v);
Eric Fiseliered9e9362017-04-16 02:40:45 +00002232
2233#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002234 vector(initializer_list<value_type> __il);
2235 vector(initializer_list<value_type> __il, const allocator_type& __a);
2236
Howard Hinnant1c936782011-06-03 19:40:40 +00002237 _LIBCPP_INLINE_VISIBILITY
2238 vector(vector&& __v)
Marshall Clowe5108202015-07-14 14:46:32 +00002239#if _LIBCPP_STD_VER > 14
2240 _NOEXCEPT;
2241#else
Howard Hinnant1c936782011-06-03 19:40:40 +00002242 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowe5108202015-07-14 14:46:32 +00002243#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002244 vector(vector&& __v, const allocator_type& __a);
Howard Hinnant1c936782011-06-03 19:40:40 +00002245 _LIBCPP_INLINE_VISIBILITY
2246 vector& operator=(vector&& __v)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002247 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiseliered9e9362017-04-16 02:40:45 +00002248
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002249 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002250 vector& operator=(initializer_list<value_type> __il)
2251 {assign(__il.begin(), __il.end()); return *this;}
Eric Fiseliered9e9362017-04-16 02:40:45 +00002252
2253#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002254
2255 template <class _InputIterator>
2256 typename enable_if
2257 <
2258 __is_input_iterator<_InputIterator>::value &&
2259 !__is_forward_iterator<_InputIterator>::value,
2260 void
2261 >::type
2262 assign(_InputIterator __first, _InputIterator __last);
2263 template <class _ForwardIterator>
2264 typename enable_if
2265 <
2266 __is_forward_iterator<_ForwardIterator>::value,
2267 void
2268 >::type
2269 assign(_ForwardIterator __first, _ForwardIterator __last);
2270
2271 void assign(size_type __n, const value_type& __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +00002272
2273#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002274 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002275 void assign(initializer_list<value_type> __il)
2276 {assign(__il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +00002277#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002278
Howard Hinnant1c936782011-06-03 19:40:40 +00002279 _LIBCPP_INLINE_VISIBILITY allocator_type get_allocator() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002280 {return allocator_type(this->__alloc());}
2281
Howard Hinnant1c936782011-06-03 19:40:40 +00002282 size_type max_size() const _NOEXCEPT;
2283 _LIBCPP_INLINE_VISIBILITY
2284 size_type capacity() const _NOEXCEPT
2285 {return __internal_cap_to_external(__cap());}
2286 _LIBCPP_INLINE_VISIBILITY
2287 size_type size() const _NOEXCEPT
2288 {return __size_;}
2289 _LIBCPP_INLINE_VISIBILITY
2290 bool empty() const _NOEXCEPT
2291 {return __size_ == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002292 void reserve(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +00002293 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002294
Howard Hinnant1c936782011-06-03 19:40:40 +00002295 _LIBCPP_INLINE_VISIBILITY
2296 iterator begin() _NOEXCEPT
2297 {return __make_iter(0);}
2298 _LIBCPP_INLINE_VISIBILITY
2299 const_iterator begin() const _NOEXCEPT
2300 {return __make_iter(0);}
2301 _LIBCPP_INLINE_VISIBILITY
2302 iterator end() _NOEXCEPT
2303 {return __make_iter(__size_);}
2304 _LIBCPP_INLINE_VISIBILITY
2305 const_iterator end() const _NOEXCEPT
2306 {return __make_iter(__size_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002307
Howard Hinnant1c936782011-06-03 19:40:40 +00002308 _LIBCPP_INLINE_VISIBILITY
2309 reverse_iterator rbegin() _NOEXCEPT
2310 {return reverse_iterator(end());}
2311 _LIBCPP_INLINE_VISIBILITY
2312 const_reverse_iterator rbegin() const _NOEXCEPT
2313 {return const_reverse_iterator(end());}
2314 _LIBCPP_INLINE_VISIBILITY
2315 reverse_iterator rend() _NOEXCEPT
2316 {return reverse_iterator(begin());}
2317 _LIBCPP_INLINE_VISIBILITY
2318 const_reverse_iterator rend() const _NOEXCEPT
2319 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002320
Howard Hinnant1c936782011-06-03 19:40:40 +00002321 _LIBCPP_INLINE_VISIBILITY
2322 const_iterator cbegin() const _NOEXCEPT
2323 {return __make_iter(0);}
2324 _LIBCPP_INLINE_VISIBILITY
2325 const_iterator cend() const _NOEXCEPT
2326 {return __make_iter(__size_);}
2327 _LIBCPP_INLINE_VISIBILITY
2328 const_reverse_iterator crbegin() const _NOEXCEPT
2329 {return rbegin();}
2330 _LIBCPP_INLINE_VISIBILITY
2331 const_reverse_iterator crend() const _NOEXCEPT
2332 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002333
2334 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) {return __make_ref(__n);}
2335 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const {return __make_ref(__n);}
2336 reference at(size_type __n);
2337 const_reference at(size_type __n) const;
2338
2339 _LIBCPP_INLINE_VISIBILITY reference front() {return __make_ref(0);}
2340 _LIBCPP_INLINE_VISIBILITY const_reference front() const {return __make_ref(0);}
2341 _LIBCPP_INLINE_VISIBILITY reference back() {return __make_ref(__size_ - 1);}
2342 _LIBCPP_INLINE_VISIBILITY const_reference back() const {return __make_ref(__size_ - 1);}
2343
2344 void push_back(const value_type& __x);
Marshall Clowc46bb8e2013-08-13 23:54:12 +00002345#if _LIBCPP_STD_VER > 11
2346 template <class... _Args>
Marshall Clowea52cc42017-01-24 23:09:12 +00002347#if _LIBCPP_STD_VER > 14
2348 _LIBCPP_INLINE_VISIBILITY reference emplace_back(_Args&&... __args)
2349#else
2350 _LIBCPP_INLINE_VISIBILITY void emplace_back(_Args&&... __args)
2351#endif
2352 {
Eric Fiselier34ba5b92016-07-21 03:20:17 +00002353 push_back ( value_type ( _VSTD::forward<_Args>(__args)... ));
Marshall Clowea52cc42017-01-24 23:09:12 +00002354#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00002355 return this->back();
Marshall Clowea52cc42017-01-24 23:09:12 +00002356#endif
Eric Fiselier34ba5b92016-07-21 03:20:17 +00002357 }
Marshall Clowc46bb8e2013-08-13 23:54:12 +00002358#endif
2359
Howard Hinnantc51e1022010-05-11 19:42:16 +00002360 _LIBCPP_INLINE_VISIBILITY void pop_back() {--__size_;}
2361
Marshall Clowc46bb8e2013-08-13 23:54:12 +00002362#if _LIBCPP_STD_VER > 11
2363 template <class... _Args>
2364 _LIBCPP_INLINE_VISIBILITY iterator emplace(const_iterator position, _Args&&... __args)
2365 { return insert ( position, value_type ( _VSTD::forward<_Args>(__args)... )); }
2366#endif
2367
Howard Hinnantc51e1022010-05-11 19:42:16 +00002368 iterator insert(const_iterator __position, const value_type& __x);
2369 iterator insert(const_iterator __position, size_type __n, const value_type& __x);
2370 iterator insert(const_iterator __position, size_type __n, const_reference __x);
2371 template <class _InputIterator>
2372 typename enable_if
2373 <
2374 __is_input_iterator <_InputIterator>::value &&
2375 !__is_forward_iterator<_InputIterator>::value,
2376 iterator
2377 >::type
2378 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
2379 template <class _ForwardIterator>
2380 typename enable_if
2381 <
2382 __is_forward_iterator<_ForwardIterator>::value,
2383 iterator
2384 >::type
2385 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiseliered9e9362017-04-16 02:40:45 +00002386
2387#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002388 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002389 iterator insert(const_iterator __position, initializer_list<value_type> __il)
2390 {return insert(__position, __il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +00002391#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002392
Howard Hinnantcf823322010-12-17 14:46:43 +00002393 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002394 iterator erase(const_iterator __first, const_iterator __last);
2395
Howard Hinnant1c936782011-06-03 19:40:40 +00002396 _LIBCPP_INLINE_VISIBILITY
2397 void clear() _NOEXCEPT {__size_ = 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002398
Howard Hinnant1c936782011-06-03 19:40:40 +00002399 void swap(vector&)
Marshall Clow8982dcd2015-07-13 20:04:56 +00002400#if _LIBCPP_STD_VER >= 14
2401 _NOEXCEPT;
2402#else
Eric Fiselier69c51982016-12-28 06:06:09 +00002403 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00002404 __is_nothrow_swappable<allocator_type>::value);
2405#endif
Marshall Clow89eb3822016-04-07 14:20:31 +00002406 static void swap(reference __x, reference __y) _NOEXCEPT { _VSTD::swap(__x, __y); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002407
2408 void resize(size_type __sz, value_type __x = false);
Howard Hinnant1c936782011-06-03 19:40:40 +00002409 void flip() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002410
2411 bool __invariants() const;
2412
2413private:
Howard Hinnantcf823322010-12-17 14:46:43 +00002414 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002415 void allocate(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +00002416 void deallocate() _NOEXCEPT;
2417 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantea382952013-08-14 18:00:20 +00002418 static size_type __align_it(size_type __new_size) _NOEXCEPT
Marshall Clow66f34152014-07-28 15:02:42 +00002419 {return __new_size + (__bits_per_word-1) & ~((size_type)__bits_per_word-1);};
Howard Hinnantcf823322010-12-17 14:46:43 +00002420 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
2421 _LIBCPP_INLINE_VISIBILITY void __construct_at_end(size_type __n, bool __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002422 template <class _ForwardIterator>
2423 typename enable_if
2424 <
2425 __is_forward_iterator<_ForwardIterator>::value,
2426 void
2427 >::type
2428 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
2429 void __append(size_type __n, const_reference __x);
Howard Hinnant1c936782011-06-03 19:40:40 +00002430 _LIBCPP_INLINE_VISIBILITY
2431 reference __make_ref(size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002432 {return reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnant1c936782011-06-03 19:40:40 +00002433 _LIBCPP_INLINE_VISIBILITY
2434 const_reference __make_ref(size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002435 {return const_reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnant1c936782011-06-03 19:40:40 +00002436 _LIBCPP_INLINE_VISIBILITY
2437 iterator __make_iter(size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002438 {return iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnant1c936782011-06-03 19:40:40 +00002439 _LIBCPP_INLINE_VISIBILITY
2440 const_iterator __make_iter(size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002441 {return const_iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnant1c936782011-06-03 19:40:40 +00002442 _LIBCPP_INLINE_VISIBILITY
2443 iterator __const_iterator_cast(const_iterator __p) _NOEXCEPT
Howard Hinnant76053d72013-06-27 19:35:32 +00002444 {return begin() + (__p - cbegin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002445
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002446 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002447 void __copy_assign_alloc(const vector& __v)
2448 {__copy_assign_alloc(__v, integral_constant<bool,
2449 __storage_traits::propagate_on_container_copy_assignment::value>());}
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002450 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002451 void __copy_assign_alloc(const vector& __c, true_type)
2452 {
2453 if (__alloc() != __c.__alloc())
2454 deallocate();
2455 __alloc() = __c.__alloc();
2456 }
2457
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002458 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00002459 void __copy_assign_alloc(const vector&, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002460 {}
2461
2462 void __move_assign(vector& __c, false_type);
Howard Hinnant1c936782011-06-03 19:40:40 +00002463 void __move_assign(vector& __c, true_type)
2464 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002465 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002466 void __move_assign_alloc(vector& __c)
Howard Hinnant1c936782011-06-03 19:40:40 +00002467 _NOEXCEPT_(
2468 !__storage_traits::propagate_on_container_move_assignment::value ||
2469 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002470 {__move_assign_alloc(__c, integral_constant<bool,
2471 __storage_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002472 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +00002473 void __move_assign_alloc(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002474 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002475 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002476 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002477 }
2478
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002479 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00002480 void __move_assign_alloc(vector&, false_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002481 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002482 {}
2483
Howard Hinnant1c936782011-06-03 19:40:40 +00002484 size_t __hash_code() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002485
2486 friend class __bit_reference<vector>;
2487 friend class __bit_const_reference<vector>;
2488 friend class __bit_iterator<vector, false>;
2489 friend class __bit_iterator<vector, true>;
Howard Hinnant4210a0f2012-08-17 17:10:18 +00002490 friend struct __bit_array<vector>;
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002491 friend struct _LIBCPP_TEMPLATE_VIS hash<vector>;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002492};
2493
2494template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002495inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002496void
2497vector<bool, _Allocator>::__invalidate_all_iterators()
2498{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002499}
2500
2501// Allocate space for __n objects
2502// throws length_error if __n > max_size()
2503// throws (probably bad_alloc) if memory run out
2504// Precondition: __begin_ == __end_ == __cap() == 0
2505// Precondition: __n > 0
2506// Postcondition: capacity() == __n
2507// Postcondition: size() == 0
2508template <class _Allocator>
2509void
2510vector<bool, _Allocator>::allocate(size_type __n)
2511{
2512 if (__n > max_size())
2513 this->__throw_length_error();
2514 __n = __external_cap_to_internal(__n);
2515 this->__begin_ = __storage_traits::allocate(this->__alloc(), __n);
2516 this->__size_ = 0;
2517 this->__cap() = __n;
2518}
2519
2520template <class _Allocator>
2521void
Howard Hinnant1c936782011-06-03 19:40:40 +00002522vector<bool, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002523{
Howard Hinnant76053d72013-06-27 19:35:32 +00002524 if (this->__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002525 {
2526 __storage_traits::deallocate(this->__alloc(), this->__begin_, __cap());
2527 __invalidate_all_iterators();
Howard Hinnant76053d72013-06-27 19:35:32 +00002528 this->__begin_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002529 this->__size_ = this->__cap() = 0;
2530 }
2531}
2532
2533template <class _Allocator>
2534typename vector<bool, _Allocator>::size_type
Howard Hinnant1c936782011-06-03 19:40:40 +00002535vector<bool, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002536{
2537 size_type __amax = __storage_traits::max_size(__alloc());
2538 size_type __nmax = numeric_limits<size_type>::max() / 2; // end() >= begin(), always
2539 if (__nmax / __bits_per_word <= __amax)
2540 return __nmax;
2541 return __internal_cap_to_external(__amax);
2542}
2543
2544// Precondition: __new_size > capacity()
2545template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002546inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002547typename vector<bool, _Allocator>::size_type
2548vector<bool, _Allocator>::__recommend(size_type __new_size) const
2549{
2550 const size_type __ms = max_size();
2551 if (__new_size > __ms)
2552 this->__throw_length_error();
2553 const size_type __cap = capacity();
2554 if (__cap >= __ms / 2)
2555 return __ms;
Howard Hinnantea382952013-08-14 18:00:20 +00002556 return _VSTD::max(2*__cap, __align_it(__new_size));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002557}
2558
2559// Default constructs __n objects starting at __end_
2560// Precondition: __n > 0
2561// Precondition: size() + __n <= capacity()
2562// Postcondition: size() == size() + __n
2563template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002564inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002565void
2566vector<bool, _Allocator>::__construct_at_end(size_type __n, bool __x)
2567{
2568 size_type __old_size = this->__size_;
2569 this->__size_ += __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002570 _VSTD::fill_n(__make_iter(__old_size), __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002571}
2572
2573template <class _Allocator>
2574template <class _ForwardIterator>
2575typename enable_if
2576<
2577 __is_forward_iterator<_ForwardIterator>::value,
2578 void
2579>::type
2580vector<bool, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
2581{
2582 size_type __old_size = this->__size_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002583 this->__size_ += _VSTD::distance(__first, __last);
2584 _VSTD::copy(__first, __last, __make_iter(__old_size));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002585}
2586
2587template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002588inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002589vector<bool, _Allocator>::vector()
Marshall Clowe546cbb2015-06-04 02:05:41 +00002590 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant76053d72013-06-27 19:35:32 +00002591 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002592 __size_(0),
2593 __cap_alloc_(0)
2594{
2595}
2596
2597template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002598inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002599vector<bool, _Allocator>::vector(const allocator_type& __a)
Marshall Clow8f282f42015-06-04 00:10:20 +00002600#if _LIBCPP_STD_VER <= 14
2601 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
2602#else
2603 _NOEXCEPT
2604#endif
Howard Hinnant76053d72013-06-27 19:35:32 +00002605 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002606 __size_(0),
2607 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2608{
2609}
2610
2611template <class _Allocator>
2612vector<bool, _Allocator>::vector(size_type __n)
Howard Hinnant76053d72013-06-27 19:35:32 +00002613 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002614 __size_(0),
2615 __cap_alloc_(0)
2616{
2617 if (__n > 0)
2618 {
2619 allocate(__n);
2620 __construct_at_end(__n, false);
2621 }
2622}
2623
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00002624#if _LIBCPP_STD_VER > 11
2625template <class _Allocator>
2626vector<bool, _Allocator>::vector(size_type __n, const allocator_type& __a)
2627 : __begin_(nullptr),
2628 __size_(0),
2629 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2630{
2631 if (__n > 0)
2632 {
2633 allocate(__n);
2634 __construct_at_end(__n, false);
2635 }
2636}
2637#endif
2638
Howard Hinnantc51e1022010-05-11 19:42:16 +00002639template <class _Allocator>
2640vector<bool, _Allocator>::vector(size_type __n, const value_type& __x)
Howard Hinnant76053d72013-06-27 19:35:32 +00002641 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002642 __size_(0),
2643 __cap_alloc_(0)
2644{
2645 if (__n > 0)
2646 {
2647 allocate(__n);
2648 __construct_at_end(__n, __x);
2649 }
2650}
2651
2652template <class _Allocator>
2653vector<bool, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002654 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002655 __size_(0),
2656 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2657{
2658 if (__n > 0)
2659 {
2660 allocate(__n);
2661 __construct_at_end(__n, __x);
2662 }
2663}
2664
2665template <class _Allocator>
2666template <class _InputIterator>
2667vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last,
2668 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2669 !__is_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002670 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002671 __size_(0),
2672 __cap_alloc_(0)
2673{
2674#ifndef _LIBCPP_NO_EXCEPTIONS
2675 try
2676 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002677#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002678 for (; __first != __last; ++__first)
2679 push_back(*__first);
2680#ifndef _LIBCPP_NO_EXCEPTIONS
2681 }
2682 catch (...)
2683 {
Howard Hinnant76053d72013-06-27 19:35:32 +00002684 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002685 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2686 __invalidate_all_iterators();
2687 throw;
2688 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002689#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002690}
2691
2692template <class _Allocator>
2693template <class _InputIterator>
2694vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2695 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2696 !__is_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002697 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002698 __size_(0),
2699 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2700{
2701#ifndef _LIBCPP_NO_EXCEPTIONS
2702 try
2703 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002704#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002705 for (; __first != __last; ++__first)
2706 push_back(*__first);
2707#ifndef _LIBCPP_NO_EXCEPTIONS
2708 }
2709 catch (...)
2710 {
Howard Hinnant76053d72013-06-27 19:35:32 +00002711 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002712 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2713 __invalidate_all_iterators();
2714 throw;
2715 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002716#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002717}
2718
2719template <class _Allocator>
2720template <class _ForwardIterator>
2721vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last,
2722 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002723 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002724 __size_(0),
2725 __cap_alloc_(0)
2726{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002727 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002728 if (__n > 0)
2729 {
2730 allocate(__n);
2731 __construct_at_end(__first, __last);
2732 }
2733}
2734
2735template <class _Allocator>
2736template <class _ForwardIterator>
2737vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2738 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002739 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002740 __size_(0),
2741 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2742{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002743 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002744 if (__n > 0)
2745 {
2746 allocate(__n);
2747 __construct_at_end(__first, __last);
2748 }
2749}
2750
Eric Fiseliered9e9362017-04-16 02:40:45 +00002751#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002752
Howard Hinnantc51e1022010-05-11 19:42:16 +00002753template <class _Allocator>
2754vector<bool, _Allocator>::vector(initializer_list<value_type> __il)
Howard Hinnant76053d72013-06-27 19:35:32 +00002755 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002756 __size_(0),
2757 __cap_alloc_(0)
2758{
2759 size_type __n = static_cast<size_type>(__il.size());
2760 if (__n > 0)
2761 {
2762 allocate(__n);
2763 __construct_at_end(__il.begin(), __il.end());
2764 }
2765}
2766
2767template <class _Allocator>
2768vector<bool, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002769 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002770 __size_(0),
2771 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2772{
2773 size_type __n = static_cast<size_type>(__il.size());
2774 if (__n > 0)
2775 {
2776 allocate(__n);
2777 __construct_at_end(__il.begin(), __il.end());
2778 }
2779}
2780
Eric Fiseliered9e9362017-04-16 02:40:45 +00002781#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002782
Howard Hinnantc51e1022010-05-11 19:42:16 +00002783template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002784vector<bool, _Allocator>::~vector()
2785{
Howard Hinnant76053d72013-06-27 19:35:32 +00002786 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002787 __storage_traits::deallocate(__alloc(), __begin_, __cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002788 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002789}
2790
2791template <class _Allocator>
2792vector<bool, _Allocator>::vector(const vector& __v)
Howard Hinnant76053d72013-06-27 19:35:32 +00002793 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002794 __size_(0),
2795 __cap_alloc_(0, __storage_traits::select_on_container_copy_construction(__v.__alloc()))
2796{
2797 if (__v.size() > 0)
2798 {
2799 allocate(__v.size());
2800 __construct_at_end(__v.begin(), __v.end());
2801 }
2802}
2803
2804template <class _Allocator>
2805vector<bool, _Allocator>::vector(const vector& __v, 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, __a)
2809{
2810 if (__v.size() > 0)
2811 {
2812 allocate(__v.size());
2813 __construct_at_end(__v.begin(), __v.end());
2814 }
2815}
2816
2817template <class _Allocator>
2818vector<bool, _Allocator>&
2819vector<bool, _Allocator>::operator=(const vector& __v)
2820{
2821 if (this != &__v)
2822 {
2823 __copy_assign_alloc(__v);
2824 if (__v.__size_)
2825 {
2826 if (__v.__size_ > capacity())
2827 {
2828 deallocate();
2829 allocate(__v.__size_);
2830 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002831 _VSTD::copy(__v.__begin_, __v.__begin_ + __external_cap_to_internal(__v.__size_), __begin_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002832 }
2833 __size_ = __v.__size_;
2834 }
2835 return *this;
2836}
2837
Eric Fiseliered9e9362017-04-16 02:40:45 +00002838#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant74279a52010-09-04 23:28:19 +00002839
Howard Hinnantc51e1022010-05-11 19:42:16 +00002840template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002841inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002842vector<bool, _Allocator>::vector(vector&& __v)
Marshall Clowe5108202015-07-14 14:46:32 +00002843#if _LIBCPP_STD_VER > 14
2844 _NOEXCEPT
2845#else
Howard Hinnant1c936782011-06-03 19:40:40 +00002846 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowe5108202015-07-14 14:46:32 +00002847#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002848 : __begin_(__v.__begin_),
2849 __size_(__v.__size_),
2850 __cap_alloc_(__v.__cap_alloc_)
2851{
Howard Hinnant76053d72013-06-27 19:35:32 +00002852 __v.__begin_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002853 __v.__size_ = 0;
2854 __v.__cap() = 0;
2855}
2856
2857template <class _Allocator>
2858vector<bool, _Allocator>::vector(vector&& __v, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002859 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002860 __size_(0),
2861 __cap_alloc_(0, __a)
2862{
2863 if (__a == allocator_type(__v.__alloc()))
2864 {
2865 this->__begin_ = __v.__begin_;
2866 this->__size_ = __v.__size_;
2867 this->__cap() = __v.__cap();
2868 __v.__begin_ = nullptr;
2869 __v.__cap() = __v.__size_ = 0;
2870 }
2871 else if (__v.size() > 0)
2872 {
2873 allocate(__v.size());
2874 __construct_at_end(__v.begin(), __v.end());
2875 }
2876}
2877
2878template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002879inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002880vector<bool, _Allocator>&
2881vector<bool, _Allocator>::operator=(vector&& __v)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002882 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002883{
2884 __move_assign(__v, integral_constant<bool,
2885 __storage_traits::propagate_on_container_move_assignment::value>());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002886 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002887}
2888
2889template <class _Allocator>
2890void
2891vector<bool, _Allocator>::__move_assign(vector& __c, false_type)
2892{
2893 if (__alloc() != __c.__alloc())
2894 assign(__c.begin(), __c.end());
2895 else
2896 __move_assign(__c, true_type());
2897}
2898
2899template <class _Allocator>
2900void
2901vector<bool, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002902 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002903{
2904 deallocate();
Marshall Clowb4871fa2014-07-21 15:15:15 +00002905 __move_assign_alloc(__c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002906 this->__begin_ = __c.__begin_;
2907 this->__size_ = __c.__size_;
2908 this->__cap() = __c.__cap();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002909 __c.__begin_ = nullptr;
2910 __c.__cap() = __c.__size_ = 0;
2911}
Howard Hinnant74279a52010-09-04 23:28:19 +00002912
Eric Fiseliered9e9362017-04-16 02:40:45 +00002913#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002914
2915template <class _Allocator>
2916void
2917vector<bool, _Allocator>::assign(size_type __n, const value_type& __x)
2918{
2919 __size_ = 0;
2920 if (__n > 0)
2921 {
2922 size_type __c = capacity();
2923 if (__n <= __c)
2924 __size_ = __n;
2925 else
2926 {
2927 vector __v(__alloc());
2928 __v.reserve(__recommend(__n));
2929 __v.__size_ = __n;
2930 swap(__v);
2931 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002932 _VSTD::fill_n(begin(), __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002933 }
Eric Fiselier69c51982016-12-28 06:06:09 +00002934 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002935}
2936
2937template <class _Allocator>
2938template <class _InputIterator>
2939typename enable_if
2940<
2941 __is_input_iterator<_InputIterator>::value &&
2942 !__is_forward_iterator<_InputIterator>::value,
2943 void
2944>::type
2945vector<bool, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2946{
2947 clear();
2948 for (; __first != __last; ++__first)
2949 push_back(*__first);
2950}
2951
2952template <class _Allocator>
2953template <class _ForwardIterator>
2954typename enable_if
2955<
2956 __is_forward_iterator<_ForwardIterator>::value,
2957 void
2958>::type
2959vector<bool, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2960{
2961 clear();
Eric Fiselier6003c772016-12-23 23:37:52 +00002962 difference_type __ns = _VSTD::distance(__first, __last);
2963 _LIBCPP_ASSERT(__ns >= 0, "invalid range specified");
2964 const size_t __n = static_cast<size_type>(__ns);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002965 if (__n)
2966 {
2967 if (__n > capacity())
2968 {
2969 deallocate();
2970 allocate(__n);
2971 }
2972 __construct_at_end(__first, __last);
2973 }
2974}
2975
2976template <class _Allocator>
2977void
2978vector<bool, _Allocator>::reserve(size_type __n)
2979{
2980 if (__n > capacity())
2981 {
2982 vector __v(this->__alloc());
2983 __v.allocate(__n);
2984 __v.__construct_at_end(this->begin(), this->end());
2985 swap(__v);
2986 __invalidate_all_iterators();
2987 }
2988}
2989
2990template <class _Allocator>
2991void
Howard Hinnant1c936782011-06-03 19:40:40 +00002992vector<bool, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002993{
2994 if (__external_cap_to_internal(size()) > __cap())
2995 {
2996#ifndef _LIBCPP_NO_EXCEPTIONS
2997 try
2998 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002999#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003000 vector(*this, allocator_type(__alloc())).swap(*this);
3001#ifndef _LIBCPP_NO_EXCEPTIONS
3002 }
3003 catch (...)
3004 {
3005 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003006#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003007 }
3008}
3009
3010template <class _Allocator>
3011typename vector<bool, _Allocator>::reference
3012vector<bool, _Allocator>::at(size_type __n)
3013{
3014 if (__n >= size())
3015 this->__throw_out_of_range();
3016 return (*this)[__n];
3017}
3018
3019template <class _Allocator>
3020typename vector<bool, _Allocator>::const_reference
3021vector<bool, _Allocator>::at(size_type __n) const
3022{
3023 if (__n >= size())
3024 this->__throw_out_of_range();
3025 return (*this)[__n];
3026}
3027
3028template <class _Allocator>
3029void
3030vector<bool, _Allocator>::push_back(const value_type& __x)
3031{
3032 if (this->__size_ == this->capacity())
3033 reserve(__recommend(this->__size_ + 1));
3034 ++this->__size_;
3035 back() = __x;
3036}
3037
3038template <class _Allocator>
3039typename vector<bool, _Allocator>::iterator
3040vector<bool, _Allocator>::insert(const_iterator __position, const value_type& __x)
3041{
3042 iterator __r;
3043 if (size() < capacity())
3044 {
3045 const_iterator __old_end = end();
3046 ++__size_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003047 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003048 __r = __const_iterator_cast(__position);
3049 }
3050 else
3051 {
3052 vector __v(__alloc());
3053 __v.reserve(__recommend(__size_ + 1));
3054 __v.__size_ = __size_ + 1;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003055 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3056 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003057 swap(__v);
3058 }
3059 *__r = __x;
3060 return __r;
3061}
3062
3063template <class _Allocator>
3064typename vector<bool, _Allocator>::iterator
3065vector<bool, _Allocator>::insert(const_iterator __position, size_type __n, const value_type& __x)
3066{
3067 iterator __r;
3068 size_type __c = capacity();
3069 if (__n <= __c && size() <= __c - __n)
3070 {
3071 const_iterator __old_end = end();
3072 __size_ += __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003073 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003074 __r = __const_iterator_cast(__position);
3075 }
3076 else
3077 {
3078 vector __v(__alloc());
3079 __v.reserve(__recommend(__size_ + __n));
3080 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003081 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3082 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003083 swap(__v);
3084 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003085 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003086 return __r;
3087}
3088
3089template <class _Allocator>
3090template <class _InputIterator>
3091typename enable_if
3092<
3093 __is_input_iterator <_InputIterator>::value &&
3094 !__is_forward_iterator<_InputIterator>::value,
3095 typename vector<bool, _Allocator>::iterator
3096>::type
3097vector<bool, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
3098{
3099 difference_type __off = __position - begin();
3100 iterator __p = __const_iterator_cast(__position);
3101 iterator __old_end = end();
3102 for (; size() != capacity() && __first != __last; ++__first)
3103 {
3104 ++this->__size_;
3105 back() = *__first;
3106 }
3107 vector __v(__alloc());
3108 if (__first != __last)
3109 {
3110#ifndef _LIBCPP_NO_EXCEPTIONS
3111 try
3112 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003113#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003114 __v.assign(__first, __last);
3115 difference_type __old_size = static_cast<difference_type>(__old_end - begin());
3116 difference_type __old_p = __p - begin();
3117 reserve(__recommend(size() + __v.size()));
3118 __p = begin() + __old_p;
3119 __old_end = begin() + __old_size;
3120#ifndef _LIBCPP_NO_EXCEPTIONS
3121 }
3122 catch (...)
3123 {
3124 erase(__old_end, end());
3125 throw;
3126 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003127#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003128 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003129 __p = _VSTD::rotate(__p, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003130 insert(__p, __v.begin(), __v.end());
3131 return begin() + __off;
3132}
3133
3134template <class _Allocator>
3135template <class _ForwardIterator>
3136typename enable_if
3137<
3138 __is_forward_iterator<_ForwardIterator>::value,
3139 typename vector<bool, _Allocator>::iterator
3140>::type
3141vector<bool, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
3142{
Eric Fiselier654dd332016-12-11 05:31:00 +00003143 const difference_type __n_signed = _VSTD::distance(__first, __last);
3144 _LIBCPP_ASSERT(__n_signed >= 0, "invalid range specified");
3145 const size_type __n = static_cast<size_type>(__n_signed);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003146 iterator __r;
3147 size_type __c = capacity();
3148 if (__n <= __c && size() <= __c - __n)
3149 {
3150 const_iterator __old_end = end();
3151 __size_ += __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003152 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003153 __r = __const_iterator_cast(__position);
3154 }
3155 else
3156 {
3157 vector __v(__alloc());
3158 __v.reserve(__recommend(__size_ + __n));
3159 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003160 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3161 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003162 swap(__v);
3163 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003164 _VSTD::copy(__first, __last, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003165 return __r;
3166}
3167
3168template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003169inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003170typename vector<bool, _Allocator>::iterator
3171vector<bool, _Allocator>::erase(const_iterator __position)
3172{
3173 iterator __r = __const_iterator_cast(__position);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003174 _VSTD::copy(__position + 1, this->cend(), __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003175 --__size_;
3176 return __r;
3177}
3178
3179template <class _Allocator>
3180typename vector<bool, _Allocator>::iterator
3181vector<bool, _Allocator>::erase(const_iterator __first, const_iterator __last)
3182{
3183 iterator __r = __const_iterator_cast(__first);
3184 difference_type __d = __last - __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003185 _VSTD::copy(__last, this->cend(), __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003186 __size_ -= __d;
3187 return __r;
3188}
3189
3190template <class _Allocator>
3191void
3192vector<bool, _Allocator>::swap(vector& __x)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003193#if _LIBCPP_STD_VER >= 14
3194 _NOEXCEPT
3195#else
Eric Fiselier69c51982016-12-28 06:06:09 +00003196 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003197 __is_nothrow_swappable<allocator_type>::value)
3198#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003199{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003200 _VSTD::swap(this->__begin_, __x.__begin_);
3201 _VSTD::swap(this->__size_, __x.__size_);
3202 _VSTD::swap(this->__cap(), __x.__cap());
Eric Fiseliered9e9362017-04-16 02:40:45 +00003203 __swap_allocator(this->__alloc(), __x.__alloc(),
Marshall Clow8982dcd2015-07-13 20:04:56 +00003204 integral_constant<bool, __alloc_traits::propagate_on_container_swap::value>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003205}
3206
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003207template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003208void
3209vector<bool, _Allocator>::resize(size_type __sz, value_type __x)
3210{
3211 size_type __cs = size();
3212 if (__cs < __sz)
3213 {
3214 iterator __r;
3215 size_type __c = capacity();
3216 size_type __n = __sz - __cs;
3217 if (__n <= __c && __cs <= __c - __n)
3218 {
3219 __r = end();
3220 __size_ += __n;
3221 }
3222 else
3223 {
3224 vector __v(__alloc());
3225 __v.reserve(__recommend(__size_ + __n));
3226 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003227 __r = _VSTD::copy(cbegin(), cend(), __v.begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003228 swap(__v);
3229 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003230 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003231 }
3232 else
3233 __size_ = __sz;
3234}
3235
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003236template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003237void
Howard Hinnant1c936782011-06-03 19:40:40 +00003238vector<bool, _Allocator>::flip() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003239{
3240 // do middle whole words
3241 size_type __n = __size_;
3242 __storage_pointer __p = __begin_;
3243 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3244 *__p = ~*__p;
3245 // do last partial word
3246 if (__n > 0)
3247 {
3248 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3249 __storage_type __b = *__p & __m;
3250 *__p &= ~__m;
3251 *__p |= ~__b & __m;
3252 }
3253}
3254
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003255template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003256bool
3257vector<bool, _Allocator>::__invariants() const
3258{
Howard Hinnant76053d72013-06-27 19:35:32 +00003259 if (this->__begin_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003260 {
3261 if (this->__size_ != 0 || this->__cap() != 0)
3262 return false;
3263 }
3264 else
3265 {
3266 if (this->__cap() == 0)
3267 return false;
3268 if (this->__size_ > this->capacity())
3269 return false;
3270 }
3271 return true;
3272}
3273
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003274template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003275size_t
Howard Hinnant1c936782011-06-03 19:40:40 +00003276vector<bool, _Allocator>::__hash_code() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003277{
3278 size_t __h = 0;
3279 // do middle whole words
3280 size_type __n = __size_;
3281 __storage_pointer __p = __begin_;
3282 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3283 __h ^= *__p;
3284 // do last partial word
3285 if (__n > 0)
3286 {
3287 const __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3288 __h ^= *__p & __m;
3289 }
3290 return __h;
3291}
3292
3293template <class _Allocator>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003294struct _LIBCPP_TEMPLATE_VIS hash<vector<bool, _Allocator> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00003295 : public unary_function<vector<bool, _Allocator>, size_t>
3296{
Howard Hinnant1c265cd2010-09-23 18:58:28 +00003297 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +00003298 size_t operator()(const vector<bool, _Allocator>& __vec) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003299 {return __vec.__hash_code();}
3300};
3301
3302template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003303inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003304bool
3305operator==(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3306{
3307 const typename vector<_Tp, _Allocator>::size_type __sz = __x.size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003308 return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003309}
3310
3311template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003312inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003313bool
3314operator!=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3315{
3316 return !(__x == __y);
3317}
3318
3319template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003320inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003321bool
3322operator< (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3323{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003324 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003325}
3326
3327template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003328inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003329bool
3330operator> (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3331{
3332 return __y < __x;
3333}
3334
3335template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003336inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003337bool
3338operator>=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3339{
3340 return !(__x < __y);
3341}
3342
3343template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003344inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003345bool
3346operator<=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3347{
3348 return !(__y < __x);
3349}
3350
3351template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003352inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003353void
3354swap(vector<_Tp, _Allocator>& __x, vector<_Tp, _Allocator>& __y)
Howard Hinnant1c936782011-06-03 19:40:40 +00003355 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00003356{
3357 __x.swap(__y);
3358}
3359
3360_LIBCPP_END_NAMESPACE_STD
3361
Eric Fiselierf4433a32017-05-31 22:07:49 +00003362_LIBCPP_POP_MACROS
3363
Howard Hinnantc51e1022010-05-11 19:42:16 +00003364#endif // _LIBCPP_VECTOR