blob: 6759dbd8a58490936bbc408a748bdfcd88f37862 [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
Howard Hinnantc5a5fbd2011-11-29 16:45:27 +0000278#include <__undef_min_max>
279
Eric Fiselier14b6de92014-08-10 23:53:08 +0000280#include <__debug>
Howard Hinnante6ff0b62013-08-02 00:26:35 +0000281
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000282#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000283#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000284#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000285
286_LIBCPP_BEGIN_NAMESPACE_STD
287
288template <bool>
289class __vector_base_common
290{
291protected:
292 _LIBCPP_ALWAYS_INLINE __vector_base_common() {}
Marshall Clow8fea1612016-08-25 15:09:01 +0000293 _LIBCPP_NORETURN void __throw_length_error() const;
294 _LIBCPP_NORETURN void __throw_out_of_range() const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000295};
296
297template <bool __b>
298void
299__vector_base_common<__b>::__throw_length_error() const
300{
Marshall Clow8fea1612016-08-25 15:09:01 +0000301 _VSTD::__throw_length_error("vector");
Howard Hinnantc51e1022010-05-11 19:42:16 +0000302}
303
304template <bool __b>
305void
306__vector_base_common<__b>::__throw_out_of_range() const
307{
Marshall Clow8fea1612016-08-25 15:09:01 +0000308 _VSTD::__throw_out_of_range("vector");
Howard Hinnantc51e1022010-05-11 19:42:16 +0000309}
310
Howard Hinnant13d8bc12013-08-01 18:17:34 +0000311#ifdef _LIBCPP_MSVC
Howard Hinnantbf074022011-10-22 20:59:45 +0000312#pragma warning( push )
313#pragma warning( disable: 4231 )
Howard Hinnant13d8bc12013-08-01 18:17:34 +0000314#endif // _LIBCPP_MSVC
Eric Fiselier1b57fa82016-09-15 22:27:07 +0000315_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __vector_base_common<true>)
Howard Hinnant13d8bc12013-08-01 18:17:34 +0000316#ifdef _LIBCPP_MSVC
Howard Hinnantbf074022011-10-22 20:59:45 +0000317#pragma warning( pop )
Howard Hinnant13d8bc12013-08-01 18:17:34 +0000318#endif // _LIBCPP_MSVC
Howard Hinnantc51e1022010-05-11 19:42:16 +0000319
320template <class _Tp, class _Allocator>
321class __vector_base
322 : protected __vector_base_common<true>
323{
324protected:
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000325 typedef _Tp value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000326 typedef _Allocator allocator_type;
327 typedef allocator_traits<allocator_type> __alloc_traits;
328 typedef value_type& reference;
329 typedef const value_type& const_reference;
330 typedef typename __alloc_traits::size_type size_type;
331 typedef typename __alloc_traits::difference_type difference_type;
332 typedef typename __alloc_traits::pointer pointer;
333 typedef typename __alloc_traits::const_pointer const_pointer;
334 typedef pointer iterator;
335 typedef const_pointer const_iterator;
336
337 pointer __begin_;
338 pointer __end_;
339 __compressed_pair<pointer, allocator_type> __end_cap_;
340
Howard Hinnant1c936782011-06-03 19:40:40 +0000341 _LIBCPP_INLINE_VISIBILITY
342 allocator_type& __alloc() _NOEXCEPT
343 {return __end_cap_.second();}
344 _LIBCPP_INLINE_VISIBILITY
345 const allocator_type& __alloc() const _NOEXCEPT
346 {return __end_cap_.second();}
347 _LIBCPP_INLINE_VISIBILITY
348 pointer& __end_cap() _NOEXCEPT
349 {return __end_cap_.first();}
350 _LIBCPP_INLINE_VISIBILITY
351 const pointer& __end_cap() const _NOEXCEPT
352 {return __end_cap_.first();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000353
Howard Hinnant1c936782011-06-03 19:40:40 +0000354 _LIBCPP_INLINE_VISIBILITY
355 __vector_base()
356 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnantcf823322010-12-17 14:46:43 +0000357 _LIBCPP_INLINE_VISIBILITY __vector_base(const allocator_type& __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000358 ~__vector_base();
359
Howard Hinnant1c936782011-06-03 19:40:40 +0000360 _LIBCPP_INLINE_VISIBILITY
361 void clear() _NOEXCEPT {__destruct_at_end(__begin_);}
362 _LIBCPP_INLINE_VISIBILITY
363 size_type capacity() const _NOEXCEPT
364 {return static_cast<size_type>(__end_cap() - __begin_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000365
Howard Hinnant1c936782011-06-03 19:40:40 +0000366 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76053d72013-06-27 19:35:32 +0000367 void __destruct_at_end(pointer __new_last) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000368
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000369 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000370 void __copy_assign_alloc(const __vector_base& __c)
371 {__copy_assign_alloc(__c, integral_constant<bool,
372 __alloc_traits::propagate_on_container_copy_assignment::value>());}
373
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000374 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000375 void __move_assign_alloc(__vector_base& __c)
Howard Hinnant1c936782011-06-03 19:40:40 +0000376 _NOEXCEPT_(
377 !__alloc_traits::propagate_on_container_move_assignment::value ||
378 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000379 {__move_assign_alloc(__c, integral_constant<bool,
380 __alloc_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000381private:
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000382 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000383 void __copy_assign_alloc(const __vector_base& __c, true_type)
384 {
385 if (__alloc() != __c.__alloc())
386 {
387 clear();
388 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
389 __begin_ = __end_ = __end_cap() = nullptr;
390 }
391 __alloc() = __c.__alloc();
392 }
393
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000394 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +0000395 void __copy_assign_alloc(const __vector_base&, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000396 {}
397
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000398 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +0000399 void __move_assign_alloc(__vector_base& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +0000400 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000401 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000402 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000403 }
404
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000405 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +0000406 void __move_assign_alloc(__vector_base&, false_type)
Howard Hinnant1c936782011-06-03 19:40:40 +0000407 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000408 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000409};
410
411template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000412inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000413void
Howard Hinnant76053d72013-06-27 19:35:32 +0000414__vector_base<_Tp, _Allocator>::__destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000415{
Bruce Mitchener405fdcd2017-03-23 14:39:23 +0000416 pointer __soon_to_be_end = __end_;
417 while (__new_last != __soon_to_be_end)
418 __alloc_traits::destroy(__alloc(), _VSTD::__to_raw_pointer(--__soon_to_be_end));
419 __end_ = __new_last;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000420}
421
422template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000423inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000424__vector_base<_Tp, _Allocator>::__vector_base()
Howard Hinnant1c936782011-06-03 19:40:40 +0000425 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant76053d72013-06-27 19:35:32 +0000426 : __begin_(nullptr),
427 __end_(nullptr),
428 __end_cap_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000429{
430}
431
432template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000433inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000434__vector_base<_Tp, _Allocator>::__vector_base(const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +0000435 : __begin_(nullptr),
436 __end_(nullptr),
437 __end_cap_(nullptr, __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000438{
439}
440
441template <class _Tp, class _Allocator>
442__vector_base<_Tp, _Allocator>::~__vector_base()
443{
Howard Hinnant76053d72013-06-27 19:35:32 +0000444 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000445 {
446 clear();
447 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
448 }
449}
450
Eric Fiselier876c6862016-02-20 00:19:45 +0000451template <class _Tp, class _Allocator /* = allocator<_Tp> */>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000452class _LIBCPP_TEMPLATE_VIS vector
Howard Hinnantc51e1022010-05-11 19:42:16 +0000453 : private __vector_base<_Tp, _Allocator>
454{
455private:
456 typedef __vector_base<_Tp, _Allocator> __base;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000457 typedef allocator<_Tp> __default_allocator_type;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000458public:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000459 typedef vector __self;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000460 typedef _Tp value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000461 typedef _Allocator allocator_type;
462 typedef typename __base::__alloc_traits __alloc_traits;
463 typedef typename __base::reference reference;
464 typedef typename __base::const_reference const_reference;
465 typedef typename __base::size_type size_type;
466 typedef typename __base::difference_type difference_type;
467 typedef typename __base::pointer pointer;
468 typedef typename __base::const_pointer const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000469 typedef __wrap_iter<pointer> iterator;
470 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000471 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
472 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000473
Howard Hinnanta416ff02013-03-26 19:04:56 +0000474 static_assert((is_same<typename allocator_type::value_type, value_type>::value),
475 "Allocator::value_type must be same type as value_type");
476
Howard Hinnant1c936782011-06-03 19:40:40 +0000477 _LIBCPP_INLINE_VISIBILITY
Marshall Clowe546cbb2015-06-04 02:05:41 +0000478 vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant27e0e772011-09-14 18:33:51 +0000479 {
Howard Hinnanta47c6d52011-09-16 17:29:17 +0000480#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000481 __get_db()->__insert_c(this);
482#endif
483 }
484 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
Marshall Clow8f282f42015-06-04 00:10:20 +0000485#if _LIBCPP_STD_VER <= 14
486 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
487#else
488 _NOEXCEPT
489#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +0000490 : __base(__a)
491 {
Howard Hinnanta47c6d52011-09-16 17:29:17 +0000492#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000493 __get_db()->__insert_c(this);
494#endif
495 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000496 explicit vector(size_type __n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +0000497#if _LIBCPP_STD_VER > 11
498 explicit vector(size_type __n, const allocator_type& __a);
499#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000500 vector(size_type __n, const_reference __x);
501 vector(size_type __n, const_reference __x, const allocator_type& __a);
502 template <class _InputIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +0000503 vector(_InputIterator __first,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000504 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000505 !__is_forward_iterator<_InputIterator>::value &&
506 is_constructible<
507 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +0000508 typename iterator_traits<_InputIterator>::reference>::value,
509 _InputIterator>::type __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000510 template <class _InputIterator>
511 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
512 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000513 !__is_forward_iterator<_InputIterator>::value &&
514 is_constructible<
515 value_type,
516 typename iterator_traits<_InputIterator>::reference>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000517 template <class _ForwardIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +0000518 vector(_ForwardIterator __first,
Howard Hinnant88010252013-03-28 17:44:32 +0000519 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
520 is_constructible<
521 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +0000522 typename iterator_traits<_ForwardIterator>::reference>::value,
523 _ForwardIterator>::type __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000524 template <class _ForwardIterator>
525 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Howard Hinnant88010252013-03-28 17:44:32 +0000526 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
527 is_constructible<
528 value_type,
529 typename iterator_traits<_ForwardIterator>::reference>::value>::type* = 0);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000530
Howard Hinnanta47c6d52011-09-16 17:29:17 +0000531#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000532 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant27e0e772011-09-14 18:33:51 +0000533 ~vector()
534 {
535 __get_db()->__erase_c(this);
536 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000537#endif
538
539 vector(const vector& __x);
540 vector(const vector& __x, const allocator_type& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000541 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000542 vector& operator=(const vector& __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000543
544#ifndef _LIBCPP_CXX03_LANG
545 _LIBCPP_INLINE_VISIBILITY
546 vector(initializer_list<value_type> __il);
547
548 _LIBCPP_INLINE_VISIBILITY
549 vector(initializer_list<value_type> __il, const allocator_type& __a);
550
Howard Hinnantcf823322010-12-17 14:46:43 +0000551 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000552 vector(vector&& __x)
Marshall Clowe5108202015-07-14 14:46:32 +0000553#if _LIBCPP_STD_VER > 14
554 _NOEXCEPT;
555#else
Howard Hinnant1c936782011-06-03 19:40:40 +0000556 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowe5108202015-07-14 14:46:32 +0000557#endif
Eric Fiseliered9e9362017-04-16 02:40:45 +0000558
Howard Hinnantcf823322010-12-17 14:46:43 +0000559 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000560 vector(vector&& __x, const allocator_type& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000561 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000562 vector& operator=(vector&& __x)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000563 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiseliered9e9362017-04-16 02:40:45 +0000564
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000565 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000566 vector& operator=(initializer_list<value_type> __il)
567 {assign(__il.begin(), __il.end()); return *this;}
Eric Fiseliered9e9362017-04-16 02:40:45 +0000568
569#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000570
571 template <class _InputIterator>
572 typename enable_if
573 <
574 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000575 !__is_forward_iterator<_InputIterator>::value &&
576 is_constructible<
577 value_type,
578 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000579 void
580 >::type
581 assign(_InputIterator __first, _InputIterator __last);
582 template <class _ForwardIterator>
583 typename enable_if
584 <
Howard Hinnant88010252013-03-28 17:44:32 +0000585 __is_forward_iterator<_ForwardIterator>::value &&
586 is_constructible<
587 value_type,
588 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000589 void
590 >::type
591 assign(_ForwardIterator __first, _ForwardIterator __last);
592
593 void assign(size_type __n, const_reference __u);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000594
595#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000596 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000597 void assign(initializer_list<value_type> __il)
598 {assign(__il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +0000599#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000600
Howard Hinnant1c936782011-06-03 19:40:40 +0000601 _LIBCPP_INLINE_VISIBILITY
602 allocator_type get_allocator() const _NOEXCEPT
603 {return this->__alloc();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000604
Howard Hinnant1c936782011-06-03 19:40:40 +0000605 _LIBCPP_INLINE_VISIBILITY iterator begin() _NOEXCEPT;
606 _LIBCPP_INLINE_VISIBILITY const_iterator begin() const _NOEXCEPT;
607 _LIBCPP_INLINE_VISIBILITY iterator end() _NOEXCEPT;
608 _LIBCPP_INLINE_VISIBILITY const_iterator end() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000609
Howard Hinnant1c936782011-06-03 19:40:40 +0000610 _LIBCPP_INLINE_VISIBILITY
611 reverse_iterator rbegin() _NOEXCEPT
612 {return reverse_iterator(end());}
613 _LIBCPP_INLINE_VISIBILITY
614 const_reverse_iterator rbegin() const _NOEXCEPT
615 {return const_reverse_iterator(end());}
616 _LIBCPP_INLINE_VISIBILITY
617 reverse_iterator rend() _NOEXCEPT
618 {return reverse_iterator(begin());}
619 _LIBCPP_INLINE_VISIBILITY
620 const_reverse_iterator rend() const _NOEXCEPT
621 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000622
Howard Hinnant1c936782011-06-03 19:40:40 +0000623 _LIBCPP_INLINE_VISIBILITY
624 const_iterator cbegin() const _NOEXCEPT
625 {return begin();}
626 _LIBCPP_INLINE_VISIBILITY
627 const_iterator cend() const _NOEXCEPT
628 {return end();}
629 _LIBCPP_INLINE_VISIBILITY
630 const_reverse_iterator crbegin() const _NOEXCEPT
631 {return rbegin();}
632 _LIBCPP_INLINE_VISIBILITY
633 const_reverse_iterator crend() const _NOEXCEPT
634 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000635
Howard Hinnant1c936782011-06-03 19:40:40 +0000636 _LIBCPP_INLINE_VISIBILITY
637 size_type size() const _NOEXCEPT
638 {return static_cast<size_type>(this->__end_ - this->__begin_);}
639 _LIBCPP_INLINE_VISIBILITY
640 size_type capacity() const _NOEXCEPT
641 {return __base::capacity();}
642 _LIBCPP_INLINE_VISIBILITY
643 bool empty() const _NOEXCEPT
644 {return this->__begin_ == this->__end_;}
645 size_type max_size() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000646 void reserve(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +0000647 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000648
649 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n);
650 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const;
651 reference at(size_type __n);
652 const_reference at(size_type __n) const;
653
Howard Hinnant27e0e772011-09-14 18:33:51 +0000654 _LIBCPP_INLINE_VISIBILITY reference front()
655 {
656 _LIBCPP_ASSERT(!empty(), "front() called for empty vector");
657 return *this->__begin_;
658 }
659 _LIBCPP_INLINE_VISIBILITY const_reference front() const
660 {
661 _LIBCPP_ASSERT(!empty(), "front() called for empty vector");
662 return *this->__begin_;
663 }
664 _LIBCPP_INLINE_VISIBILITY reference back()
665 {
666 _LIBCPP_ASSERT(!empty(), "back() called for empty vector");
667 return *(this->__end_ - 1);
668 }
669 _LIBCPP_INLINE_VISIBILITY const_reference back() const
670 {
671 _LIBCPP_ASSERT(!empty(), "back() called for empty vector");
672 return *(this->__end_ - 1);
673 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000674
Howard Hinnant1c936782011-06-03 19:40:40 +0000675 _LIBCPP_INLINE_VISIBILITY
676 value_type* data() _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000677 {return _VSTD::__to_raw_pointer(this->__begin_);}
Howard Hinnant1c936782011-06-03 19:40:40 +0000678 _LIBCPP_INLINE_VISIBILITY
679 const value_type* data() const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000680 {return _VSTD::__to_raw_pointer(this->__begin_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000681
Howard Hinnantcf823322010-12-17 14:46:43 +0000682 _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000683
684#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000685 _LIBCPP_INLINE_VISIBILITY void push_back(value_type&& __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000686
Howard Hinnantc51e1022010-05-11 19:42:16 +0000687 template <class... _Args>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000688 _LIBCPP_INLINE_VISIBILITY
Marshall Clowea52cc42017-01-24 23:09:12 +0000689#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +0000690 reference emplace_back(_Args&&... __args);
Marshall Clowea52cc42017-01-24 23:09:12 +0000691#else
692 void emplace_back(_Args&&... __args);
693#endif
Eric Fiseliered9e9362017-04-16 02:40:45 +0000694#endif // !_LIBCPP_CXX03_LANG
695
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000696 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000697 void pop_back();
698
699 iterator insert(const_iterator __position, const_reference __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000700
701#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000702 iterator insert(const_iterator __position, value_type&& __x);
703 template <class... _Args>
704 iterator emplace(const_iterator __position, _Args&&... __args);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000705#endif // !_LIBCPP_CXX03_LANG
706
Howard Hinnantc51e1022010-05-11 19:42:16 +0000707 iterator insert(const_iterator __position, size_type __n, const_reference __x);
708 template <class _InputIterator>
709 typename enable_if
710 <
711 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000712 !__is_forward_iterator<_InputIterator>::value &&
713 is_constructible<
714 value_type,
715 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000716 iterator
717 >::type
718 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
719 template <class _ForwardIterator>
720 typename enable_if
721 <
Howard Hinnant88010252013-03-28 17:44:32 +0000722 __is_forward_iterator<_ForwardIterator>::value &&
723 is_constructible<
724 value_type,
725 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000726 iterator
727 >::type
728 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000729
730#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000731 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000732 iterator insert(const_iterator __position, initializer_list<value_type> __il)
733 {return insert(__position, __il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +0000734#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000735
Howard Hinnantcf823322010-12-17 14:46:43 +0000736 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000737 iterator erase(const_iterator __first, const_iterator __last);
738
Howard Hinnant1c936782011-06-03 19:40:40 +0000739 _LIBCPP_INLINE_VISIBILITY
740 void clear() _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000741 {
Marshall Clow2cd9d372014-05-08 14:14:06 +0000742 size_type __old_size = size();
Howard Hinnant27e0e772011-09-14 18:33:51 +0000743 __base::clear();
Marshall Clow2cd9d372014-05-08 14:14:06 +0000744 __annotate_shrink(__old_size);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000745 __invalidate_all_iterators();
746 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000747
748 void resize(size_type __sz);
749 void resize(size_type __sz, const_reference __x);
750
Howard Hinnant1c936782011-06-03 19:40:40 +0000751 void swap(vector&)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000752#if _LIBCPP_STD_VER >= 14
Eric Fiselier69c51982016-12-28 06:06:09 +0000753 _NOEXCEPT_DEBUG;
Marshall Clow8982dcd2015-07-13 20:04:56 +0000754#else
Eric Fiselier69c51982016-12-28 06:06:09 +0000755 _NOEXCEPT_DEBUG_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +0000756 __is_nothrow_swappable<allocator_type>::value);
757#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000758
759 bool __invariants() const;
760
Howard Hinnanta47c6d52011-09-16 17:29:17 +0000761#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000762
763 bool __dereferenceable(const const_iterator* __i) const;
764 bool __decrementable(const const_iterator* __i) const;
765 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
766 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
767
Howard Hinnanta47c6d52011-09-16 17:29:17 +0000768#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000769
Howard Hinnantc51e1022010-05-11 19:42:16 +0000770private:
Howard Hinnantcf823322010-12-17 14:46:43 +0000771 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Eric Fiselier69c51982016-12-28 06:06:09 +0000772 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(pointer __new_last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000773 void allocate(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +0000774 void deallocate() _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000775 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
Howard Hinnanta5589382011-01-04 19:53:31 +0000776 void __construct_at_end(size_type __n);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000777 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000778 void __construct_at_end(size_type __n, const_reference __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000779 template <class _ForwardIterator>
780 typename enable_if
781 <
782 __is_forward_iterator<_ForwardIterator>::value,
783 void
784 >::type
Eric Fiselieracfe6f02015-03-31 16:54:19 +0000785 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000786 void __append(size_type __n);
787 void __append(size_type __n, const_reference __x);
Howard Hinnantcf823322010-12-17 14:46:43 +0000788 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000789 iterator __make_iter(pointer __p) _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000790 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000791 const_iterator __make_iter(const_pointer __p) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000792 void __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v);
793 pointer __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p);
794 void __move_range(pointer __from_s, pointer __from_e, pointer __to);
Howard Hinnant1c936782011-06-03 19:40:40 +0000795 void __move_assign(vector& __c, true_type)
796 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000797 void __move_assign(vector& __c, false_type)
798 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000799 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76053d72013-06-27 19:35:32 +0000800 void __destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000801 {
Eric Fiselier69c51982016-12-28 06:06:09 +0000802 __invalidate_iterators_past(__new_last);
Marshall Clow2cd9d372014-05-08 14:14:06 +0000803 size_type __old_size = size();
Howard Hinnant27e0e772011-09-14 18:33:51 +0000804 __base::__destruct_at_end(__new_last);
Marshall Clow2cd9d372014-05-08 14:14:06 +0000805 __annotate_shrink(__old_size);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000806 }
Eric Fiseliered9e9362017-04-16 02:40:45 +0000807
808#ifndef _LIBCPP_CXX03_LANG
809 template <class _Up> void __push_back_slow_path(_Up&& __x);
810
Howard Hinnantb6c49562012-02-26 15:30:12 +0000811 template <class... _Args>
Eric Fiseliered9e9362017-04-16 02:40:45 +0000812 void __emplace_back_slow_path(_Args&&... __args);
813#else
814 template <class _Up> void __push_back_slow_path(_Up& __x);
Howard Hinnantb6c49562012-02-26 15:30:12 +0000815#endif
Eric Fiseliered9e9362017-04-16 02:40:45 +0000816
Marshall Clow2cd9d372014-05-08 14:14:06 +0000817 // The following functions are no-ops outside of AddressSanitizer mode.
818 // We call annotatations only for the default Allocator because other allocators
819 // may not meet the AddressSanitizer alignment constraints.
820 // See the documentation for __sanitizer_annotate_contiguous_container for more details.
Marshall Clow2cd9d372014-05-08 14:14:06 +0000821#ifndef _LIBCPP_HAS_NO_ASAN
Eric Fiselier6003c772016-12-23 23:37:52 +0000822 void __annotate_contiguous_container(const void *__beg, const void *__end,
823 const void *__old_mid,
824 const void *__new_mid) const
825 {
826
Marshall Clow2cd9d372014-05-08 14:14:06 +0000827 if (__beg && is_same<allocator_type, __default_allocator_type>::value)
828 __sanitizer_annotate_contiguous_container(__beg, __end, __old_mid, __new_mid);
Marshall Clow2cd9d372014-05-08 14:14:06 +0000829 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000830#else
831 _LIBCPP_INLINE_VISIBILITY
832 void __annotate_contiguous_container(const void*, const void*, const void*,
833 const void*) const {}
834#endif
835 _LIBCPP_INLINE_VISIBILITY
836 void __annotate_new(size_type __current_size) const {
Marshall Clow2cd9d372014-05-08 14:14:06 +0000837 __annotate_contiguous_container(data(), data() + capacity(),
838 data() + capacity(), data() + __current_size);
839 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000840
841 _LIBCPP_INLINE_VISIBILITY
842 void __annotate_delete() const {
Marshall Clow2cd9d372014-05-08 14:14:06 +0000843 __annotate_contiguous_container(data(), data() + capacity(),
844 data() + size(), data() + capacity());
845 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000846
847 _LIBCPP_INLINE_VISIBILITY
Kostya Serebryany4963c252014-09-02 23:43:38 +0000848 void __annotate_increase(size_type __n) const
Marshall Clow2cd9d372014-05-08 14:14:06 +0000849 {
850 __annotate_contiguous_container(data(), data() + capacity(),
851 data() + size(), data() + size() + __n);
852 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000853
854 _LIBCPP_INLINE_VISIBILITY
Kostya Serebryany4963c252014-09-02 23:43:38 +0000855 void __annotate_shrink(size_type __old_size) const
Marshall Clow2cd9d372014-05-08 14:14:06 +0000856 {
857 __annotate_contiguous_container(data(), data() + capacity(),
858 data() + __old_size, data() + size());
859 }
Marshall Clowc78ffa52014-09-03 21:37:43 +0000860#ifndef _LIBCPP_HAS_NO_ASAN
Kostya Serebryany4963c252014-09-02 23:43:38 +0000861 // The annotation for size increase should happen before the actual increase,
862 // but if an exception is thrown after that the annotation has to be undone.
863 struct __RAII_IncreaseAnnotator {
864 __RAII_IncreaseAnnotator(const vector &__v, size_type __n = 1)
Eric Fiseliera7c043e2015-03-10 00:25:20 +0000865 : __commit(false), __v(__v), __old_size(__v.size() + __n) {
Kostya Serebryany4963c252014-09-02 23:43:38 +0000866 __v.__annotate_increase(__n);
867 }
868 void __done() { __commit = true; }
869 ~__RAII_IncreaseAnnotator() {
870 if (__commit) return;
Eric Fiseliera7c043e2015-03-10 00:25:20 +0000871 __v.__annotate_shrink(__old_size);
Kostya Serebryany4963c252014-09-02 23:43:38 +0000872 }
873 bool __commit;
Kostya Serebryany4963c252014-09-02 23:43:38 +0000874 const vector &__v;
Eric Fiseliera7c043e2015-03-10 00:25:20 +0000875 size_type __old_size;
Kostya Serebryany4963c252014-09-02 23:43:38 +0000876 };
Marshall Clowc78ffa52014-09-03 21:37:43 +0000877#else
878 struct __RAII_IncreaseAnnotator {
Eric Fiselier6003c772016-12-23 23:37:52 +0000879 _LIBCPP_INLINE_VISIBILITY
880 __RAII_IncreaseAnnotator(const vector &, size_type = 1) {}
881 _LIBCPP_INLINE_VISIBILITY void __done() {}
Marshall Clowc78ffa52014-09-03 21:37:43 +0000882 };
883#endif
884
Howard Hinnantc51e1022010-05-11 19:42:16 +0000885};
886
887template <class _Tp, class _Allocator>
888void
889vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v)
890{
Marshall Clow2cd9d372014-05-08 14:14:06 +0000891 __annotate_delete();
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000892 __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, this->__end_, __v.__begin_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000893 _VSTD::swap(this->__begin_, __v.__begin_);
894 _VSTD::swap(this->__end_, __v.__end_);
895 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000896 __v.__first_ = __v.__begin_;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000897 __annotate_new(size());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000898 __invalidate_all_iterators();
899}
900
901template <class _Tp, class _Allocator>
902typename vector<_Tp, _Allocator>::pointer
903vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p)
904{
Marshall Clow2cd9d372014-05-08 14:14:06 +0000905 __annotate_delete();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000906 pointer __r = __v.__begin_;
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000907 __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, __p, __v.__begin_);
908 __alloc_traits::__construct_forward(this->__alloc(), __p, this->__end_, __v.__end_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000909 _VSTD::swap(this->__begin_, __v.__begin_);
910 _VSTD::swap(this->__end_, __v.__end_);
911 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000912 __v.__first_ = __v.__begin_;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000913 __annotate_new(size());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000914 __invalidate_all_iterators();
915 return __r;
916}
917
918// Allocate space for __n objects
919// throws length_error if __n > max_size()
920// throws (probably bad_alloc) if memory run out
921// Precondition: __begin_ == __end_ == __end_cap() == 0
922// Precondition: __n > 0
923// Postcondition: capacity() == __n
924// Postcondition: size() == 0
925template <class _Tp, class _Allocator>
926void
927vector<_Tp, _Allocator>::allocate(size_type __n)
928{
929 if (__n > max_size())
930 this->__throw_length_error();
931 this->__begin_ = this->__end_ = __alloc_traits::allocate(this->__alloc(), __n);
932 this->__end_cap() = this->__begin_ + __n;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000933 __annotate_new(0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000934}
935
936template <class _Tp, class _Allocator>
937void
Howard Hinnant1c936782011-06-03 19:40:40 +0000938vector<_Tp, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000939{
Howard Hinnant76053d72013-06-27 19:35:32 +0000940 if (this->__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000941 {
942 clear();
943 __alloc_traits::deallocate(this->__alloc(), this->__begin_, capacity());
Howard Hinnant76053d72013-06-27 19:35:32 +0000944 this->__begin_ = this->__end_ = this->__end_cap() = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000945 }
946}
947
948template <class _Tp, class _Allocator>
949typename vector<_Tp, _Allocator>::size_type
Howard Hinnant1c936782011-06-03 19:40:40 +0000950vector<_Tp, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000951{
Eric Fiselierb5d9f442016-11-23 01:18:56 +0000952 return _VSTD::min<size_type>(__alloc_traits::max_size(this->__alloc()),
953 numeric_limits<difference_type>::max());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000954}
955
956// Precondition: __new_size > capacity()
957template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000958inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000959typename vector<_Tp, _Allocator>::size_type
960vector<_Tp, _Allocator>::__recommend(size_type __new_size) const
961{
962 const size_type __ms = max_size();
963 if (__new_size > __ms)
964 this->__throw_length_error();
965 const size_type __cap = capacity();
966 if (__cap >= __ms / 2)
967 return __ms;
Alexis Hunt991d29b2011-07-29 23:31:58 +0000968 return _VSTD::max<size_type>(2*__cap, __new_size);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000969}
970
971// Default constructs __n objects starting at __end_
972// throws if construction throws
973// Precondition: __n > 0
974// Precondition: size() + __n <= capacity()
975// Postcondition: size() == size() + __n
976template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000977void
978vector<_Tp, _Allocator>::__construct_at_end(size_type __n)
979{
Howard Hinnantc51e1022010-05-11 19:42:16 +0000980 allocator_type& __a = this->__alloc();
981 do
982 {
Kostya Serebryany4963c252014-09-02 23:43:38 +0000983 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000984 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000985 ++this->__end_;
986 --__n;
Kostya Serebryany4963c252014-09-02 23:43:38 +0000987 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000988 } while (__n > 0);
989}
990
Howard Hinnantc51e1022010-05-11 19:42:16 +0000991// Copy constructs __n objects starting at __end_ from __x
992// throws if construction throws
993// Precondition: __n > 0
994// Precondition: size() + __n <= capacity()
995// Postcondition: size() == old size() + __n
996// Postcondition: [i] == __x for all i in [size() - __n, __n)
997template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000998inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000999void
1000vector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
1001{
Howard Hinnantc51e1022010-05-11 19:42:16 +00001002 allocator_type& __a = this->__alloc();
1003 do
1004 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001005 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001006 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001007 ++this->__end_;
1008 --__n;
Kostya Serebryany4963c252014-09-02 23:43:38 +00001009 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001010 } while (__n > 0);
1011}
1012
1013template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001014template <class _ForwardIterator>
1015typename enable_if
1016<
1017 __is_forward_iterator<_ForwardIterator>::value,
1018 void
1019>::type
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001020vector<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001021{
1022 allocator_type& __a = this->__alloc();
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001023 __RAII_IncreaseAnnotator __annotator(*this, __n);
1024 __alloc_traits::__construct_range_forward(__a, __first, __last, this->__end_);
1025 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001026}
1027
1028// Default constructs __n objects starting at __end_
1029// throws if construction throws
1030// Postcondition: size() == size() + __n
Howard Hinnant1c936782011-06-03 19:40:40 +00001031// Exception safety: strong.
Howard Hinnantc51e1022010-05-11 19:42:16 +00001032template <class _Tp, class _Allocator>
1033void
1034vector<_Tp, _Allocator>::__append(size_type __n)
1035{
1036 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1037 this->__construct_at_end(__n);
1038 else
1039 {
1040 allocator_type& __a = this->__alloc();
1041 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1042 __v.__construct_at_end(__n);
1043 __swap_out_circular_buffer(__v);
1044 }
1045}
1046
1047// Default constructs __n objects starting at __end_
1048// throws if construction throws
1049// Postcondition: size() == size() + __n
Howard Hinnant1c936782011-06-03 19:40:40 +00001050// Exception safety: strong.
Howard Hinnantc51e1022010-05-11 19:42:16 +00001051template <class _Tp, class _Allocator>
1052void
1053vector<_Tp, _Allocator>::__append(size_type __n, const_reference __x)
1054{
1055 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1056 this->__construct_at_end(__n, __x);
1057 else
1058 {
1059 allocator_type& __a = this->__alloc();
1060 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1061 __v.__construct_at_end(__n, __x);
1062 __swap_out_circular_buffer(__v);
1063 }
1064}
1065
1066template <class _Tp, class _Allocator>
1067vector<_Tp, _Allocator>::vector(size_type __n)
1068{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001069#if _LIBCPP_DEBUG_LEVEL >= 2
1070 __get_db()->__insert_c(this);
1071#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001072 if (__n > 0)
1073 {
1074 allocate(__n);
1075 __construct_at_end(__n);
1076 }
1077}
1078
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00001079#if _LIBCPP_STD_VER > 11
1080template <class _Tp, class _Allocator>
1081vector<_Tp, _Allocator>::vector(size_type __n, const allocator_type& __a)
1082 : __base(__a)
1083{
1084#if _LIBCPP_DEBUG_LEVEL >= 2
1085 __get_db()->__insert_c(this);
1086#endif
1087 if (__n > 0)
1088 {
1089 allocate(__n);
1090 __construct_at_end(__n);
1091 }
1092}
1093#endif
1094
Howard Hinnantc51e1022010-05-11 19:42:16 +00001095template <class _Tp, class _Allocator>
1096vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x)
1097{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001098#if _LIBCPP_DEBUG_LEVEL >= 2
1099 __get_db()->__insert_c(this);
1100#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001101 if (__n > 0)
1102 {
1103 allocate(__n);
1104 __construct_at_end(__n, __x);
1105 }
1106}
1107
1108template <class _Tp, class _Allocator>
1109vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x, const allocator_type& __a)
1110 : __base(__a)
1111{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001112#if _LIBCPP_DEBUG_LEVEL >= 2
1113 __get_db()->__insert_c(this);
1114#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001115 if (__n > 0)
1116 {
1117 allocate(__n);
1118 __construct_at_end(__n, __x);
1119 }
1120}
1121
1122template <class _Tp, class _Allocator>
1123template <class _InputIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +00001124vector<_Tp, _Allocator>::vector(_InputIterator __first,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001125 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001126 !__is_forward_iterator<_InputIterator>::value &&
1127 is_constructible<
1128 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +00001129 typename iterator_traits<_InputIterator>::reference>::value,
1130 _InputIterator>::type __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001131{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001132#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001133 __get_db()->__insert_c(this);
1134#endif
Howard Hinnant9cd22302011-09-16 18:41:29 +00001135 for (; __first != __last; ++__first)
1136 push_back(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001137}
1138
1139template <class _Tp, class _Allocator>
1140template <class _InputIterator>
1141vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
1142 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001143 !__is_forward_iterator<_InputIterator>::value &&
1144 is_constructible<
1145 value_type,
1146 typename iterator_traits<_InputIterator>::reference>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001147 : __base(__a)
1148{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001149#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001150 __get_db()->__insert_c(this);
1151#endif
Howard Hinnant9cd22302011-09-16 18:41:29 +00001152 for (; __first != __last; ++__first)
1153 push_back(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001154}
1155
1156template <class _Tp, class _Allocator>
1157template <class _ForwardIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +00001158vector<_Tp, _Allocator>::vector(_ForwardIterator __first,
Howard Hinnant88010252013-03-28 17:44:32 +00001159 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
1160 is_constructible<
1161 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +00001162 typename iterator_traits<_ForwardIterator>::reference>::value,
1163 _ForwardIterator>::type __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001164{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001165#if _LIBCPP_DEBUG_LEVEL >= 2
1166 __get_db()->__insert_c(this);
1167#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001168 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001169 if (__n > 0)
1170 {
1171 allocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001172 __construct_at_end(__first, __last, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001173 }
1174}
1175
1176template <class _Tp, class _Allocator>
1177template <class _ForwardIterator>
1178vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Howard Hinnant88010252013-03-28 17:44:32 +00001179 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
1180 is_constructible<
1181 value_type,
1182 typename iterator_traits<_ForwardIterator>::reference>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001183 : __base(__a)
1184{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001185#if _LIBCPP_DEBUG_LEVEL >= 2
1186 __get_db()->__insert_c(this);
1187#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001188 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001189 if (__n > 0)
1190 {
1191 allocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001192 __construct_at_end(__first, __last, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001193 }
1194}
1195
1196template <class _Tp, class _Allocator>
1197vector<_Tp, _Allocator>::vector(const vector& __x)
1198 : __base(__alloc_traits::select_on_container_copy_construction(__x.__alloc()))
1199{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001200#if _LIBCPP_DEBUG_LEVEL >= 2
1201 __get_db()->__insert_c(this);
1202#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001203 size_type __n = __x.size();
1204 if (__n > 0)
1205 {
1206 allocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001207 __construct_at_end(__x.__begin_, __x.__end_, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001208 }
1209}
1210
1211template <class _Tp, class _Allocator>
1212vector<_Tp, _Allocator>::vector(const vector& __x, const allocator_type& __a)
1213 : __base(__a)
1214{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001215#if _LIBCPP_DEBUG_LEVEL >= 2
1216 __get_db()->__insert_c(this);
1217#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001218 size_type __n = __x.size();
1219 if (__n > 0)
1220 {
1221 allocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001222 __construct_at_end(__x.__begin_, __x.__end_, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001223 }
1224}
1225
Eric Fiseliered9e9362017-04-16 02:40:45 +00001226#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001227
1228template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001229inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001230vector<_Tp, _Allocator>::vector(vector&& __x)
Marshall Clowe5108202015-07-14 14:46:32 +00001231#if _LIBCPP_STD_VER > 14
1232 _NOEXCEPT
1233#else
Howard Hinnant1c936782011-06-03 19:40:40 +00001234 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowe5108202015-07-14 14:46:32 +00001235#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001236 : __base(_VSTD::move(__x.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001237{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001238#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001239 __get_db()->__insert_c(this);
Howard Hinnanta1e60cd2011-09-19 16:34:29 +00001240 __get_db()->swap(this, &__x);
Howard Hinnant27e0e772011-09-14 18:33:51 +00001241#endif
Howard Hinnant9cd22302011-09-16 18:41:29 +00001242 this->__begin_ = __x.__begin_;
1243 this->__end_ = __x.__end_;
1244 this->__end_cap() = __x.__end_cap();
Howard Hinnant76053d72013-06-27 19:35:32 +00001245 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001246}
1247
1248template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001249inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001250vector<_Tp, _Allocator>::vector(vector&& __x, const allocator_type& __a)
1251 : __base(__a)
1252{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001253#if _LIBCPP_DEBUG_LEVEL >= 2
1254 __get_db()->__insert_c(this);
1255#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001256 if (__a == __x.__alloc())
1257 {
1258 this->__begin_ = __x.__begin_;
1259 this->__end_ = __x.__end_;
1260 this->__end_cap() = __x.__end_cap();
1261 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnanta1e60cd2011-09-19 16:34:29 +00001262#if _LIBCPP_DEBUG_LEVEL >= 2
1263 __get_db()->swap(this, &__x);
1264#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001265 }
1266 else
1267 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001268 typedef move_iterator<iterator> _Ip;
1269 assign(_Ip(__x.begin()), _Ip(__x.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001270 }
1271}
1272
1273template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001274inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001275vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il)
1276{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001277#if _LIBCPP_DEBUG_LEVEL >= 2
1278 __get_db()->__insert_c(this);
1279#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001280 if (__il.size() > 0)
1281 {
1282 allocate(__il.size());
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001283 __construct_at_end(__il.begin(), __il.end(), __il.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001284 }
1285}
1286
1287template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001288inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001289vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
1290 : __base(__a)
1291{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001292#if _LIBCPP_DEBUG_LEVEL >= 2
1293 __get_db()->__insert_c(this);
1294#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001295 if (__il.size() > 0)
1296 {
1297 allocate(__il.size());
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001298 __construct_at_end(__il.begin(), __il.end(), __il.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001299 }
1300}
1301
1302template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001303inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001304vector<_Tp, _Allocator>&
1305vector<_Tp, _Allocator>::operator=(vector&& __x)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001306 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001307{
1308 __move_assign(__x, integral_constant<bool,
1309 __alloc_traits::propagate_on_container_move_assignment::value>());
1310 return *this;
1311}
1312
1313template <class _Tp, class _Allocator>
1314void
1315vector<_Tp, _Allocator>::__move_assign(vector& __c, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001316 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001317{
1318 if (__base::__alloc() != __c.__alloc())
1319 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001320 typedef move_iterator<iterator> _Ip;
1321 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001322 }
1323 else
1324 __move_assign(__c, true_type());
1325}
1326
1327template <class _Tp, class _Allocator>
1328void
1329vector<_Tp, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00001330 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001331{
1332 deallocate();
Marshall Clow136d45c2014-07-21 15:11:13 +00001333 __base::__move_assign_alloc(__c); // this can throw
Howard Hinnantc51e1022010-05-11 19:42:16 +00001334 this->__begin_ = __c.__begin_;
1335 this->__end_ = __c.__end_;
1336 this->__end_cap() = __c.__end_cap();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001337 __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
Howard Hinnanta1e60cd2011-09-19 16:34:29 +00001338#if _LIBCPP_DEBUG_LEVEL >= 2
1339 __get_db()->swap(this, &__c);
1340#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001341}
1342
Eric Fiseliered9e9362017-04-16 02:40:45 +00001343#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001344
1345template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001346inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001347vector<_Tp, _Allocator>&
1348vector<_Tp, _Allocator>::operator=(const vector& __x)
1349{
1350 if (this != &__x)
1351 {
1352 __base::__copy_assign_alloc(__x);
1353 assign(__x.__begin_, __x.__end_);
1354 }
1355 return *this;
1356}
1357
1358template <class _Tp, class _Allocator>
1359template <class _InputIterator>
1360typename enable_if
1361<
1362 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001363 !__is_forward_iterator<_InputIterator>::value &&
1364 is_constructible<
1365 _Tp,
1366 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001367 void
1368>::type
1369vector<_Tp, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
1370{
1371 clear();
1372 for (; __first != __last; ++__first)
1373 push_back(*__first);
1374}
1375
1376template <class _Tp, class _Allocator>
1377template <class _ForwardIterator>
1378typename enable_if
1379<
Howard Hinnant88010252013-03-28 17:44:32 +00001380 __is_forward_iterator<_ForwardIterator>::value &&
1381 is_constructible<
1382 _Tp,
1383 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001384 void
1385>::type
1386vector<_Tp, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
1387{
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001388 size_type __new_size = static_cast<size_type>(_VSTD::distance(__first, __last));
1389 if (__new_size <= capacity())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001390 {
1391 _ForwardIterator __mid = __last;
1392 bool __growing = false;
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001393 if (__new_size > size())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001394 {
1395 __growing = true;
1396 __mid = __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001397 _VSTD::advance(__mid, size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001398 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001399 pointer __m = _VSTD::copy(__first, __mid, this->__begin_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001400 if (__growing)
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001401 __construct_at_end(__mid, __last, __new_size - size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001402 else
1403 this->__destruct_at_end(__m);
1404 }
1405 else
1406 {
1407 deallocate();
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001408 allocate(__recommend(__new_size));
1409 __construct_at_end(__first, __last, __new_size);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001410 }
Eric Fiselier69c51982016-12-28 06:06:09 +00001411 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001412}
1413
1414template <class _Tp, class _Allocator>
1415void
1416vector<_Tp, _Allocator>::assign(size_type __n, const_reference __u)
1417{
1418 if (__n <= capacity())
1419 {
1420 size_type __s = size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001421 _VSTD::fill_n(this->__begin_, _VSTD::min(__n, __s), __u);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001422 if (__n > __s)
1423 __construct_at_end(__n - __s, __u);
1424 else
Howard Hinnant155c2af2010-05-24 17:49:41 +00001425 this->__destruct_at_end(this->__begin_ + __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001426 }
1427 else
1428 {
1429 deallocate();
1430 allocate(__recommend(static_cast<size_type>(__n)));
1431 __construct_at_end(__n, __u);
1432 }
Eric Fiselier69c51982016-12-28 06:06:09 +00001433 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001434}
1435
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001436template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001437inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001438typename vector<_Tp, _Allocator>::iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001439vector<_Tp, _Allocator>::__make_iter(pointer __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001440{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001441#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001442 return iterator(this, __p);
1443#else
1444 return iterator(__p);
1445#endif
1446}
1447
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001448template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001449inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001450typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001451vector<_Tp, _Allocator>::__make_iter(const_pointer __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001452{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001453#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001454 return const_iterator(this, __p);
1455#else
1456 return const_iterator(__p);
1457#endif
1458}
1459
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001460template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001461inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001462typename vector<_Tp, _Allocator>::iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001463vector<_Tp, _Allocator>::begin() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001464{
1465 return __make_iter(this->__begin_);
1466}
1467
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001468template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001469inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001470typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001471vector<_Tp, _Allocator>::begin() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001472{
1473 return __make_iter(this->__begin_);
1474}
1475
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001476template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001477inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001478typename vector<_Tp, _Allocator>::iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001479vector<_Tp, _Allocator>::end() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001480{
1481 return __make_iter(this->__end_);
1482}
1483
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001484template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001485inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001486typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001487vector<_Tp, _Allocator>::end() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001488{
1489 return __make_iter(this->__end_);
1490}
1491
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001492template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001493inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001494typename vector<_Tp, _Allocator>::reference
1495vector<_Tp, _Allocator>::operator[](size_type __n)
1496{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001497 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001498 return this->__begin_[__n];
1499}
1500
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001501template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001502inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001503typename vector<_Tp, _Allocator>::const_reference
1504vector<_Tp, _Allocator>::operator[](size_type __n) const
1505{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001506 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001507 return this->__begin_[__n];
1508}
1509
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001510template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001511typename vector<_Tp, _Allocator>::reference
1512vector<_Tp, _Allocator>::at(size_type __n)
1513{
1514 if (__n >= size())
1515 this->__throw_out_of_range();
1516 return this->__begin_[__n];
1517}
1518
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001519template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001520typename vector<_Tp, _Allocator>::const_reference
1521vector<_Tp, _Allocator>::at(size_type __n) const
1522{
1523 if (__n >= size())
1524 this->__throw_out_of_range();
1525 return this->__begin_[__n];
1526}
1527
1528template <class _Tp, class _Allocator>
1529void
1530vector<_Tp, _Allocator>::reserve(size_type __n)
1531{
1532 if (__n > capacity())
1533 {
1534 allocator_type& __a = this->__alloc();
Howard Hinnant1c936782011-06-03 19:40:40 +00001535 __split_buffer<value_type, allocator_type&> __v(__n, size(), __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001536 __swap_out_circular_buffer(__v);
1537 }
1538}
1539
1540template <class _Tp, class _Allocator>
1541void
Howard Hinnant1c936782011-06-03 19:40:40 +00001542vector<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001543{
1544 if (capacity() > size())
1545 {
1546#ifndef _LIBCPP_NO_EXCEPTIONS
1547 try
1548 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001549#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001550 allocator_type& __a = this->__alloc();
Howard Hinnant1c936782011-06-03 19:40:40 +00001551 __split_buffer<value_type, allocator_type&> __v(size(), size(), __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001552 __swap_out_circular_buffer(__v);
1553#ifndef _LIBCPP_NO_EXCEPTIONS
1554 }
1555 catch (...)
1556 {
1557 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001558#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001559 }
1560}
1561
1562template <class _Tp, class _Allocator>
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001563template <class _Up>
1564void
Eric Fiseliered9e9362017-04-16 02:40:45 +00001565#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001566vector<_Tp, _Allocator>::__push_back_slow_path(_Up&& __x)
1567#else
1568vector<_Tp, _Allocator>::__push_back_slow_path(_Up& __x)
1569#endif
1570{
1571 allocator_type& __a = this->__alloc();
1572 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1573 // __v.push_back(_VSTD::forward<_Up>(__x));
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001574 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), _VSTD::forward<_Up>(__x));
1575 __v.__end_++;
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001576 __swap_out_circular_buffer(__v);
1577}
1578
1579template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001580inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001581void
1582vector<_Tp, _Allocator>::push_back(const_reference __x)
1583{
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001584 if (this->__end_ != this->__end_cap())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001585 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001586 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001587 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001588 _VSTD::__to_raw_pointer(this->__end_), __x);
Kostya Serebryany4963c252014-09-02 23:43:38 +00001589 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001590 ++this->__end_;
1591 }
1592 else
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001593 __push_back_slow_path(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001594}
1595
Eric Fiseliered9e9362017-04-16 02:40:45 +00001596#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001597
1598template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001599inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001600void
1601vector<_Tp, _Allocator>::push_back(value_type&& __x)
1602{
1603 if (this->__end_ < this->__end_cap())
1604 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001605 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001606 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001607 _VSTD::__to_raw_pointer(this->__end_),
1608 _VSTD::move(__x));
Kostya Serebryany4963c252014-09-02 23:43:38 +00001609 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001610 ++this->__end_;
1611 }
1612 else
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001613 __push_back_slow_path(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001614}
1615
1616template <class _Tp, class _Allocator>
1617template <class... _Args>
1618void
Howard Hinnantb6c49562012-02-26 15:30:12 +00001619vector<_Tp, _Allocator>::__emplace_back_slow_path(_Args&&... __args)
1620{
1621 allocator_type& __a = this->__alloc();
1622 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1623// __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001624 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), _VSTD::forward<_Args>(__args)...);
1625 __v.__end_++;
Howard Hinnantb6c49562012-02-26 15:30:12 +00001626 __swap_out_circular_buffer(__v);
1627}
1628
1629template <class _Tp, class _Allocator>
1630template <class... _Args>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001631inline
Marshall Clowea52cc42017-01-24 23:09:12 +00001632#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001633typename vector<_Tp, _Allocator>::reference
Marshall Clowea52cc42017-01-24 23:09:12 +00001634#else
1635void
1636#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001637vector<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1638{
1639 if (this->__end_ < this->__end_cap())
1640 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001641 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001642 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001643 _VSTD::__to_raw_pointer(this->__end_),
1644 _VSTD::forward<_Args>(__args)...);
Kostya Serebryany4963c252014-09-02 23:43:38 +00001645 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001646 ++this->__end_;
1647 }
1648 else
Howard Hinnantb6c49562012-02-26 15:30:12 +00001649 __emplace_back_slow_path(_VSTD::forward<_Args>(__args)...);
Marshall Clowea52cc42017-01-24 23:09:12 +00001650#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001651 return this->back();
Marshall Clowea52cc42017-01-24 23:09:12 +00001652#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001653}
1654
Eric Fiseliered9e9362017-04-16 02:40:45 +00001655#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001656
1657template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001658inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001659void
1660vector<_Tp, _Allocator>::pop_back()
1661{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001662 _LIBCPP_ASSERT(!empty(), "vector::pop_back called for empty vector");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001663 this->__destruct_at_end(this->__end_ - 1);
1664}
1665
1666template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001667inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001668typename vector<_Tp, _Allocator>::iterator
1669vector<_Tp, _Allocator>::erase(const_iterator __position)
1670{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001671#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001672 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1673 "vector::erase(iterator) called with an iterator not"
1674 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001675#endif
Howard Hinnant58d52d52013-03-25 22:12:26 +00001676 _LIBCPP_ASSERT(__position != end(),
1677 "vector::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnant76053d72013-06-27 19:35:32 +00001678 difference_type __ps = __position - cbegin();
1679 pointer __p = this->__begin_ + __ps;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001680 this->__destruct_at_end(_VSTD::move(__p + 1, this->__end_, __p));
Eric Fiselier69c51982016-12-28 06:06:09 +00001681 this->__invalidate_iterators_past(__p-1);
1682 iterator __r = __make_iter(__p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001683 return __r;
1684}
1685
1686template <class _Tp, class _Allocator>
1687typename vector<_Tp, _Allocator>::iterator
1688vector<_Tp, _Allocator>::erase(const_iterator __first, const_iterator __last)
1689{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001690#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001691 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
1692 "vector::erase(iterator, iterator) called with an iterator not"
1693 " referring to this vector");
Eric Fiselier69c51982016-12-28 06:06:09 +00001694 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__last) == this,
1695 "vector::erase(iterator, iterator) called with an iterator not"
1696 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001697#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001698 _LIBCPP_ASSERT(__first <= __last, "vector::erase(first, last) called with invalid range");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001699 pointer __p = this->__begin_ + (__first - begin());
Eric Fiselier69c51982016-12-28 06:06:09 +00001700 if (__first != __last) {
Howard Hinnantc580cc32013-04-18 15:02:57 +00001701 this->__destruct_at_end(_VSTD::move(__p + (__last - __first), this->__end_, __p));
Eric Fiselier69c51982016-12-28 06:06:09 +00001702 this->__invalidate_iterators_past(__p - 1);
1703 }
1704 iterator __r = __make_iter(__p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001705 return __r;
1706}
1707
1708template <class _Tp, class _Allocator>
1709void
1710vector<_Tp, _Allocator>::__move_range(pointer __from_s, pointer __from_e, pointer __to)
1711{
1712 pointer __old_last = this->__end_;
1713 difference_type __n = __old_last - __to;
1714 for (pointer __i = __from_s + __n; __i < __from_e; ++__i, ++this->__end_)
1715 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001716 _VSTD::__to_raw_pointer(this->__end_),
1717 _VSTD::move(*__i));
1718 _VSTD::move_backward(__from_s, __from_s + __n, __old_last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001719}
1720
1721template <class _Tp, class _Allocator>
1722typename vector<_Tp, _Allocator>::iterator
1723vector<_Tp, _Allocator>::insert(const_iterator __position, const_reference __x)
1724{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001725#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001726 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1727 "vector::insert(iterator, x) called with an iterator not"
1728 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001729#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001730 pointer __p = this->__begin_ + (__position - begin());
1731 if (this->__end_ < this->__end_cap())
1732 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001733 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001734 if (__p == this->__end_)
1735 {
1736 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001737 _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001738 ++this->__end_;
1739 }
1740 else
1741 {
1742 __move_range(__p, this->__end_, __p + 1);
1743 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1744 if (__p <= __xr && __xr < this->__end_)
1745 ++__xr;
1746 *__p = *__xr;
1747 }
Kostya Serebryany4963c252014-09-02 23:43:38 +00001748 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001749 }
1750 else
1751 {
1752 allocator_type& __a = this->__alloc();
1753 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
1754 __v.push_back(__x);
1755 __p = __swap_out_circular_buffer(__v, __p);
1756 }
1757 return __make_iter(__p);
1758}
1759
Eric Fiseliered9e9362017-04-16 02:40:45 +00001760#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001761
1762template <class _Tp, class _Allocator>
1763typename vector<_Tp, _Allocator>::iterator
1764vector<_Tp, _Allocator>::insert(const_iterator __position, value_type&& __x)
1765{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001766#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001767 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1768 "vector::insert(iterator, x) called with an iterator not"
1769 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001770#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001771 pointer __p = this->__begin_ + (__position - begin());
1772 if (this->__end_ < this->__end_cap())
1773 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001774 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001775 if (__p == this->__end_)
1776 {
1777 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001778 _VSTD::__to_raw_pointer(this->__end_),
1779 _VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001780 ++this->__end_;
1781 }
1782 else
1783 {
1784 __move_range(__p, this->__end_, __p + 1);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001785 *__p = _VSTD::move(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001786 }
Kostya Serebryany4963c252014-09-02 23:43:38 +00001787 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001788 }
1789 else
1790 {
1791 allocator_type& __a = this->__alloc();
1792 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001793 __v.push_back(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001794 __p = __swap_out_circular_buffer(__v, __p);
1795 }
1796 return __make_iter(__p);
1797}
1798
1799template <class _Tp, class _Allocator>
1800template <class... _Args>
1801typename vector<_Tp, _Allocator>::iterator
1802vector<_Tp, _Allocator>::emplace(const_iterator __position, _Args&&... __args)
1803{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001804#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001805 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1806 "vector::emplace(iterator, x) called with an iterator not"
1807 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001808#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001809 pointer __p = this->__begin_ + (__position - begin());
1810 if (this->__end_ < this->__end_cap())
1811 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001812 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001813 if (__p == this->__end_)
1814 {
1815 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001816 _VSTD::__to_raw_pointer(this->__end_),
1817 _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001818 ++this->__end_;
1819 }
1820 else
1821 {
Marshall Clowa591b9a2016-07-11 21:38:08 +00001822 __temp_value<value_type, _Allocator> __tmp(this->__alloc(), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001823 __move_range(__p, this->__end_, __p + 1);
Marshall Clowa591b9a2016-07-11 21:38:08 +00001824 *__p = _VSTD::move(__tmp.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001825 }
Kostya Serebryany4963c252014-09-02 23:43:38 +00001826 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001827 }
1828 else
1829 {
1830 allocator_type& __a = this->__alloc();
1831 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001832 __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001833 __p = __swap_out_circular_buffer(__v, __p);
1834 }
1835 return __make_iter(__p);
1836}
1837
Eric Fiseliered9e9362017-04-16 02:40:45 +00001838#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001839
1840template <class _Tp, class _Allocator>
1841typename vector<_Tp, _Allocator>::iterator
1842vector<_Tp, _Allocator>::insert(const_iterator __position, size_type __n, const_reference __x)
1843{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001844#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001845 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1846 "vector::insert(iterator, n, x) called with an iterator not"
1847 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001848#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001849 pointer __p = this->__begin_ + (__position - begin());
1850 if (__n > 0)
1851 {
1852 if (__n <= static_cast<size_type>(this->__end_cap() - this->__end_))
1853 {
1854 size_type __old_n = __n;
1855 pointer __old_last = this->__end_;
1856 if (__n > static_cast<size_type>(this->__end_ - __p))
1857 {
1858 size_type __cx = __n - (this->__end_ - __p);
1859 __construct_at_end(__cx, __x);
1860 __n -= __cx;
1861 }
1862 if (__n > 0)
1863 {
Eric Fiselier2a649652014-11-14 18:28:36 +00001864 __RAII_IncreaseAnnotator __annotator(*this, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001865 __move_range(__p, __old_last, __p + __old_n);
Kostya Serebryany4963c252014-09-02 23:43:38 +00001866 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001867 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1868 if (__p <= __xr && __xr < this->__end_)
1869 __xr += __old_n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001870 _VSTD::fill_n(__p, __n, *__xr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001871 }
1872 }
1873 else
1874 {
1875 allocator_type& __a = this->__alloc();
1876 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1877 __v.__construct_at_end(__n, __x);
1878 __p = __swap_out_circular_buffer(__v, __p);
1879 }
1880 }
1881 return __make_iter(__p);
1882}
1883
1884template <class _Tp, class _Allocator>
1885template <class _InputIterator>
1886typename enable_if
1887<
1888 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001889 !__is_forward_iterator<_InputIterator>::value &&
1890 is_constructible<
1891 _Tp,
1892 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001893 typename vector<_Tp, _Allocator>::iterator
1894>::type
1895vector<_Tp, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
1896{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001897#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001898 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1899 "vector::insert(iterator, range) called with an iterator not"
1900 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001901#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001902 difference_type __off = __position - begin();
1903 pointer __p = this->__begin_ + __off;
1904 allocator_type& __a = this->__alloc();
1905 pointer __old_last = this->__end_;
1906 for (; this->__end_ != this->__end_cap() && __first != __last; ++__first)
1907 {
Eric Fiselier489fd502015-07-18 18:22:12 +00001908 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001909 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001910 *__first);
1911 ++this->__end_;
Eric Fiselier489fd502015-07-18 18:22:12 +00001912 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001913 }
1914 __split_buffer<value_type, allocator_type&> __v(__a);
1915 if (__first != __last)
1916 {
1917#ifndef _LIBCPP_NO_EXCEPTIONS
1918 try
1919 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001920#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001921 __v.__construct_at_end(__first, __last);
1922 difference_type __old_size = __old_last - this->__begin_;
1923 difference_type __old_p = __p - this->__begin_;
1924 reserve(__recommend(size() + __v.size()));
1925 __p = this->__begin_ + __old_p;
1926 __old_last = this->__begin_ + __old_size;
1927#ifndef _LIBCPP_NO_EXCEPTIONS
1928 }
1929 catch (...)
1930 {
1931 erase(__make_iter(__old_last), end());
1932 throw;
1933 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001934#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001935 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001936 __p = _VSTD::rotate(__p, __old_last, this->__end_);
Howard Hinnant9cd22302011-09-16 18:41:29 +00001937 insert(__make_iter(__p), make_move_iterator(__v.begin()),
1938 make_move_iterator(__v.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001939 return begin() + __off;
1940}
1941
1942template <class _Tp, class _Allocator>
1943template <class _ForwardIterator>
1944typename enable_if
1945<
Howard Hinnant88010252013-03-28 17:44:32 +00001946 __is_forward_iterator<_ForwardIterator>::value &&
1947 is_constructible<
1948 _Tp,
1949 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001950 typename vector<_Tp, _Allocator>::iterator
1951>::type
1952vector<_Tp, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
1953{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001954#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001955 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1956 "vector::insert(iterator, range) called with an iterator not"
1957 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001958#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001959 pointer __p = this->__begin_ + (__position - begin());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001960 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001961 if (__n > 0)
1962 {
1963 if (__n <= this->__end_cap() - this->__end_)
1964 {
1965 size_type __old_n = __n;
1966 pointer __old_last = this->__end_;
1967 _ForwardIterator __m = __last;
1968 difference_type __dx = this->__end_ - __p;
1969 if (__n > __dx)
1970 {
1971 __m = __first;
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001972 difference_type __diff = this->__end_ - __p;
1973 _VSTD::advance(__m, __diff);
1974 __construct_at_end(__m, __last, __n - __diff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001975 __n = __dx;
1976 }
1977 if (__n > 0)
1978 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001979 __RAII_IncreaseAnnotator __annotator(*this, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001980 __move_range(__p, __old_last, __p + __old_n);
Kostya Serebryany4963c252014-09-02 23:43:38 +00001981 __annotator.__done();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001982 _VSTD::copy(__first, __m, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001983 }
1984 }
1985 else
1986 {
1987 allocator_type& __a = this->__alloc();
1988 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1989 __v.__construct_at_end(__first, __last);
1990 __p = __swap_out_circular_buffer(__v, __p);
1991 }
1992 }
1993 return __make_iter(__p);
1994}
1995
1996template <class _Tp, class _Allocator>
1997void
1998vector<_Tp, _Allocator>::resize(size_type __sz)
1999{
2000 size_type __cs = size();
2001 if (__cs < __sz)
2002 this->__append(__sz - __cs);
2003 else if (__cs > __sz)
2004 this->__destruct_at_end(this->__begin_ + __sz);
2005}
2006
2007template <class _Tp, class _Allocator>
2008void
2009vector<_Tp, _Allocator>::resize(size_type __sz, const_reference __x)
2010{
2011 size_type __cs = size();
2012 if (__cs < __sz)
2013 this->__append(__sz - __cs, __x);
2014 else if (__cs > __sz)
2015 this->__destruct_at_end(this->__begin_ + __sz);
2016}
2017
2018template <class _Tp, class _Allocator>
2019void
2020vector<_Tp, _Allocator>::swap(vector& __x)
Marshall Clow8982dcd2015-07-13 20:04:56 +00002021#if _LIBCPP_STD_VER >= 14
Eric Fiselier69c51982016-12-28 06:06:09 +00002022 _NOEXCEPT_DEBUG
Marshall Clow8982dcd2015-07-13 20:04:56 +00002023#else
Eric Fiselier69c51982016-12-28 06:06:09 +00002024 _NOEXCEPT_DEBUG_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00002025 __is_nothrow_swappable<allocator_type>::value)
2026#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002027{
Howard Hinnant27e0e772011-09-14 18:33:51 +00002028 _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
2029 this->__alloc() == __x.__alloc(),
2030 "vector::swap: Either propagate_on_container_swap must be true"
2031 " or the allocators must compare equal");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002032 _VSTD::swap(this->__begin_, __x.__begin_);
2033 _VSTD::swap(this->__end_, __x.__end_);
2034 _VSTD::swap(this->__end_cap(), __x.__end_cap());
Eric Fiseliered9e9362017-04-16 02:40:45 +00002035 __swap_allocator(this->__alloc(), __x.__alloc(),
Marshall Clow8982dcd2015-07-13 20:04:56 +00002036 integral_constant<bool,__alloc_traits::propagate_on_container_swap::value>());
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002037#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002038 __get_db()->swap(this, &__x);
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002039#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00002040}
2041
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002042template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002043bool
2044vector<_Tp, _Allocator>::__invariants() const
2045{
Howard Hinnant76053d72013-06-27 19:35:32 +00002046 if (this->__begin_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002047 {
Howard Hinnant76053d72013-06-27 19:35:32 +00002048 if (this->__end_ != nullptr || this->__end_cap() != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002049 return false;
2050 }
2051 else
2052 {
2053 if (this->__begin_ > this->__end_)
2054 return false;
2055 if (this->__begin_ == this->__end_cap())
2056 return false;
2057 if (this->__end_ > this->__end_cap())
2058 return false;
2059 }
2060 return true;
2061}
2062
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002063#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002064
Howard Hinnantc51e1022010-05-11 19:42:16 +00002065template <class _Tp, class _Allocator>
Howard Hinnant27e0e772011-09-14 18:33:51 +00002066bool
2067vector<_Tp, _Allocator>::__dereferenceable(const const_iterator* __i) const
2068{
2069 return this->__begin_ <= __i->base() && __i->base() < this->__end_;
2070}
2071
2072template <class _Tp, class _Allocator>
2073bool
2074vector<_Tp, _Allocator>::__decrementable(const const_iterator* __i) const
2075{
2076 return this->__begin_ < __i->base() && __i->base() <= this->__end_;
2077}
2078
2079template <class _Tp, class _Allocator>
2080bool
2081vector<_Tp, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
2082{
2083 const_pointer __p = __i->base() + __n;
2084 return this->__begin_ <= __p && __p <= this->__end_;
2085}
2086
2087template <class _Tp, class _Allocator>
2088bool
2089vector<_Tp, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
2090{
2091 const_pointer __p = __i->base() + __n;
2092 return this->__begin_ <= __p && __p < this->__end_;
2093}
2094
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002095#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002096
2097template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002098inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002099void
2100vector<_Tp, _Allocator>::__invalidate_all_iterators()
2101{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002102#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002103 __get_db()->__invalidate_all(this);
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002104#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00002105}
2106
Eric Fiselier69c51982016-12-28 06:06:09 +00002107
2108template <class _Tp, class _Allocator>
2109inline _LIBCPP_INLINE_VISIBILITY
2110void
2111vector<_Tp, _Allocator>::__invalidate_iterators_past(pointer __new_last) {
2112#if _LIBCPP_DEBUG_LEVEL >= 2
2113 __c_node* __c = __get_db()->__find_c_and_lock(this);
2114 for (__i_node** __p = __c->end_; __p != __c->beg_; ) {
2115 --__p;
2116 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
2117 if (__i->base() > __new_last) {
2118 (*__p)->__c_ = nullptr;
2119 if (--__c->end_ != __p)
2120 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
2121 }
2122 }
2123 __get_db()->unlock();
2124#else
2125 ((void)__new_last);
2126#endif
2127}
2128
Howard Hinnantc51e1022010-05-11 19:42:16 +00002129// vector<bool>
2130
2131template <class _Allocator> class vector<bool, _Allocator>;
2132
2133template <class _Allocator> struct hash<vector<bool, _Allocator> >;
2134
2135template <class _Allocator>
Howard Hinnantb4ef6482011-07-02 20:33:23 +00002136struct __has_storage_type<vector<bool, _Allocator> >
2137{
2138 static const bool value = true;
2139};
2140
2141template <class _Allocator>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002142class _LIBCPP_TEMPLATE_VIS vector<bool, _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002143 : private __vector_base_common<true>
2144{
2145public:
2146 typedef vector __self;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002147 typedef bool value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002148 typedef _Allocator allocator_type;
2149 typedef allocator_traits<allocator_type> __alloc_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002150 typedef typename __alloc_traits::size_type size_type;
2151 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant896b7622012-05-07 16:50:38 +00002152 typedef size_type __storage_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002153 typedef __bit_iterator<vector, false> pointer;
2154 typedef __bit_iterator<vector, true> const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002155 typedef pointer iterator;
2156 typedef const_pointer const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002157 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
2158 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002159
2160private:
Marshall Clow940e01c2015-04-07 05:21:38 +00002161 typedef typename __rebind_alloc_helper<__alloc_traits, __storage_type>::type __storage_allocator;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002162 typedef allocator_traits<__storage_allocator> __storage_traits;
2163 typedef typename __storage_traits::pointer __storage_pointer;
2164 typedef typename __storage_traits::const_pointer __const_storage_pointer;
2165
2166 __storage_pointer __begin_;
2167 size_type __size_;
2168 __compressed_pair<size_type, __storage_allocator> __cap_alloc_;
Howard Hinnant71d1c192011-07-09 15:50:42 +00002169public:
Howard Hinnantb4ef6482011-07-02 20:33:23 +00002170 typedef __bit_reference<vector> reference;
2171 typedef __bit_const_reference<vector> const_reference;
Howard Hinnant71d1c192011-07-09 15:50:42 +00002172private:
Howard Hinnant1c936782011-06-03 19:40:40 +00002173 _LIBCPP_INLINE_VISIBILITY
2174 size_type& __cap() _NOEXCEPT
2175 {return __cap_alloc_.first();}
2176 _LIBCPP_INLINE_VISIBILITY
2177 const size_type& __cap() const _NOEXCEPT
2178 {return __cap_alloc_.first();}
2179 _LIBCPP_INLINE_VISIBILITY
2180 __storage_allocator& __alloc() _NOEXCEPT
2181 {return __cap_alloc_.second();}
2182 _LIBCPP_INLINE_VISIBILITY
2183 const __storage_allocator& __alloc() const _NOEXCEPT
2184 {return __cap_alloc_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002185
2186 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
2187
Howard Hinnant1c936782011-06-03 19:40:40 +00002188 _LIBCPP_INLINE_VISIBILITY
2189 static size_type __internal_cap_to_external(size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002190 {return __n * __bits_per_word;}
Howard Hinnant1c936782011-06-03 19:40:40 +00002191 _LIBCPP_INLINE_VISIBILITY
2192 static size_type __external_cap_to_internal(size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002193 {return (__n - 1) / __bits_per_word + 1;}
2194
2195public:
Howard Hinnant1c936782011-06-03 19:40:40 +00002196 _LIBCPP_INLINE_VISIBILITY
Marshall Clowe546cbb2015-06-04 02:05:41 +00002197 vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clow8f282f42015-06-04 00:10:20 +00002198
2199 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
2200#if _LIBCPP_STD_VER <= 14
2201 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
2202#else
2203 _NOEXCEPT;
2204#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002205 ~vector();
2206 explicit vector(size_type __n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00002207#if _LIBCPP_STD_VER > 11
2208 explicit vector(size_type __n, const allocator_type& __a);
2209#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002210 vector(size_type __n, const value_type& __v);
2211 vector(size_type __n, const value_type& __v, const allocator_type& __a);
2212 template <class _InputIterator>
2213 vector(_InputIterator __first, _InputIterator __last,
2214 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2215 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2216 template <class _InputIterator>
2217 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2218 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2219 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2220 template <class _ForwardIterator>
2221 vector(_ForwardIterator __first, _ForwardIterator __last,
2222 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2223 template <class _ForwardIterator>
2224 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2225 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2226
2227 vector(const vector& __v);
2228 vector(const vector& __v, const allocator_type& __a);
2229 vector& operator=(const vector& __v);
Eric Fiseliered9e9362017-04-16 02:40:45 +00002230
2231#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002232 vector(initializer_list<value_type> __il);
2233 vector(initializer_list<value_type> __il, const allocator_type& __a);
2234
Howard Hinnant1c936782011-06-03 19:40:40 +00002235 _LIBCPP_INLINE_VISIBILITY
2236 vector(vector&& __v)
Marshall Clowe5108202015-07-14 14:46:32 +00002237#if _LIBCPP_STD_VER > 14
2238 _NOEXCEPT;
2239#else
Howard Hinnant1c936782011-06-03 19:40:40 +00002240 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowe5108202015-07-14 14:46:32 +00002241#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002242 vector(vector&& __v, const allocator_type& __a);
Howard Hinnant1c936782011-06-03 19:40:40 +00002243 _LIBCPP_INLINE_VISIBILITY
2244 vector& operator=(vector&& __v)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002245 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiseliered9e9362017-04-16 02:40:45 +00002246
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002247 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002248 vector& operator=(initializer_list<value_type> __il)
2249 {assign(__il.begin(), __il.end()); return *this;}
Eric Fiseliered9e9362017-04-16 02:40:45 +00002250
2251#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002252
2253 template <class _InputIterator>
2254 typename enable_if
2255 <
2256 __is_input_iterator<_InputIterator>::value &&
2257 !__is_forward_iterator<_InputIterator>::value,
2258 void
2259 >::type
2260 assign(_InputIterator __first, _InputIterator __last);
2261 template <class _ForwardIterator>
2262 typename enable_if
2263 <
2264 __is_forward_iterator<_ForwardIterator>::value,
2265 void
2266 >::type
2267 assign(_ForwardIterator __first, _ForwardIterator __last);
2268
2269 void assign(size_type __n, const value_type& __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +00002270
2271#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002272 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002273 void assign(initializer_list<value_type> __il)
2274 {assign(__il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +00002275#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002276
Howard Hinnant1c936782011-06-03 19:40:40 +00002277 _LIBCPP_INLINE_VISIBILITY allocator_type get_allocator() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002278 {return allocator_type(this->__alloc());}
2279
Howard Hinnant1c936782011-06-03 19:40:40 +00002280 size_type max_size() const _NOEXCEPT;
2281 _LIBCPP_INLINE_VISIBILITY
2282 size_type capacity() const _NOEXCEPT
2283 {return __internal_cap_to_external(__cap());}
2284 _LIBCPP_INLINE_VISIBILITY
2285 size_type size() const _NOEXCEPT
2286 {return __size_;}
2287 _LIBCPP_INLINE_VISIBILITY
2288 bool empty() const _NOEXCEPT
2289 {return __size_ == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002290 void reserve(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +00002291 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002292
Howard Hinnant1c936782011-06-03 19:40:40 +00002293 _LIBCPP_INLINE_VISIBILITY
2294 iterator begin() _NOEXCEPT
2295 {return __make_iter(0);}
2296 _LIBCPP_INLINE_VISIBILITY
2297 const_iterator begin() const _NOEXCEPT
2298 {return __make_iter(0);}
2299 _LIBCPP_INLINE_VISIBILITY
2300 iterator end() _NOEXCEPT
2301 {return __make_iter(__size_);}
2302 _LIBCPP_INLINE_VISIBILITY
2303 const_iterator end() const _NOEXCEPT
2304 {return __make_iter(__size_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002305
Howard Hinnant1c936782011-06-03 19:40:40 +00002306 _LIBCPP_INLINE_VISIBILITY
2307 reverse_iterator rbegin() _NOEXCEPT
2308 {return reverse_iterator(end());}
2309 _LIBCPP_INLINE_VISIBILITY
2310 const_reverse_iterator rbegin() const _NOEXCEPT
2311 {return const_reverse_iterator(end());}
2312 _LIBCPP_INLINE_VISIBILITY
2313 reverse_iterator rend() _NOEXCEPT
2314 {return reverse_iterator(begin());}
2315 _LIBCPP_INLINE_VISIBILITY
2316 const_reverse_iterator rend() const _NOEXCEPT
2317 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002318
Howard Hinnant1c936782011-06-03 19:40:40 +00002319 _LIBCPP_INLINE_VISIBILITY
2320 const_iterator cbegin() const _NOEXCEPT
2321 {return __make_iter(0);}
2322 _LIBCPP_INLINE_VISIBILITY
2323 const_iterator cend() const _NOEXCEPT
2324 {return __make_iter(__size_);}
2325 _LIBCPP_INLINE_VISIBILITY
2326 const_reverse_iterator crbegin() const _NOEXCEPT
2327 {return rbegin();}
2328 _LIBCPP_INLINE_VISIBILITY
2329 const_reverse_iterator crend() const _NOEXCEPT
2330 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002331
2332 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) {return __make_ref(__n);}
2333 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const {return __make_ref(__n);}
2334 reference at(size_type __n);
2335 const_reference at(size_type __n) const;
2336
2337 _LIBCPP_INLINE_VISIBILITY reference front() {return __make_ref(0);}
2338 _LIBCPP_INLINE_VISIBILITY const_reference front() const {return __make_ref(0);}
2339 _LIBCPP_INLINE_VISIBILITY reference back() {return __make_ref(__size_ - 1);}
2340 _LIBCPP_INLINE_VISIBILITY const_reference back() const {return __make_ref(__size_ - 1);}
2341
2342 void push_back(const value_type& __x);
Marshall Clowc46bb8e2013-08-13 23:54:12 +00002343#if _LIBCPP_STD_VER > 11
2344 template <class... _Args>
Marshall Clowea52cc42017-01-24 23:09:12 +00002345#if _LIBCPP_STD_VER > 14
2346 _LIBCPP_INLINE_VISIBILITY reference emplace_back(_Args&&... __args)
2347#else
2348 _LIBCPP_INLINE_VISIBILITY void emplace_back(_Args&&... __args)
2349#endif
2350 {
Eric Fiselier34ba5b92016-07-21 03:20:17 +00002351 push_back ( value_type ( _VSTD::forward<_Args>(__args)... ));
Marshall Clowea52cc42017-01-24 23:09:12 +00002352#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00002353 return this->back();
Marshall Clowea52cc42017-01-24 23:09:12 +00002354#endif
Eric Fiselier34ba5b92016-07-21 03:20:17 +00002355 }
Marshall Clowc46bb8e2013-08-13 23:54:12 +00002356#endif
2357
Howard Hinnantc51e1022010-05-11 19:42:16 +00002358 _LIBCPP_INLINE_VISIBILITY void pop_back() {--__size_;}
2359
Marshall Clowc46bb8e2013-08-13 23:54:12 +00002360#if _LIBCPP_STD_VER > 11
2361 template <class... _Args>
2362 _LIBCPP_INLINE_VISIBILITY iterator emplace(const_iterator position, _Args&&... __args)
2363 { return insert ( position, value_type ( _VSTD::forward<_Args>(__args)... )); }
2364#endif
2365
Howard Hinnantc51e1022010-05-11 19:42:16 +00002366 iterator insert(const_iterator __position, const value_type& __x);
2367 iterator insert(const_iterator __position, size_type __n, const value_type& __x);
2368 iterator insert(const_iterator __position, size_type __n, const_reference __x);
2369 template <class _InputIterator>
2370 typename enable_if
2371 <
2372 __is_input_iterator <_InputIterator>::value &&
2373 !__is_forward_iterator<_InputIterator>::value,
2374 iterator
2375 >::type
2376 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
2377 template <class _ForwardIterator>
2378 typename enable_if
2379 <
2380 __is_forward_iterator<_ForwardIterator>::value,
2381 iterator
2382 >::type
2383 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiseliered9e9362017-04-16 02:40:45 +00002384
2385#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002386 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002387 iterator insert(const_iterator __position, initializer_list<value_type> __il)
2388 {return insert(__position, __il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +00002389#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002390
Howard Hinnantcf823322010-12-17 14:46:43 +00002391 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002392 iterator erase(const_iterator __first, const_iterator __last);
2393
Howard Hinnant1c936782011-06-03 19:40:40 +00002394 _LIBCPP_INLINE_VISIBILITY
2395 void clear() _NOEXCEPT {__size_ = 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002396
Howard Hinnant1c936782011-06-03 19:40:40 +00002397 void swap(vector&)
Marshall Clow8982dcd2015-07-13 20:04:56 +00002398#if _LIBCPP_STD_VER >= 14
2399 _NOEXCEPT;
2400#else
Eric Fiselier69c51982016-12-28 06:06:09 +00002401 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00002402 __is_nothrow_swappable<allocator_type>::value);
2403#endif
Marshall Clow89eb3822016-04-07 14:20:31 +00002404 static void swap(reference __x, reference __y) _NOEXCEPT { _VSTD::swap(__x, __y); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002405
2406 void resize(size_type __sz, value_type __x = false);
Howard Hinnant1c936782011-06-03 19:40:40 +00002407 void flip() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002408
2409 bool __invariants() const;
2410
2411private:
Howard Hinnantcf823322010-12-17 14:46:43 +00002412 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002413 void allocate(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +00002414 void deallocate() _NOEXCEPT;
2415 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantea382952013-08-14 18:00:20 +00002416 static size_type __align_it(size_type __new_size) _NOEXCEPT
Marshall Clow66f34152014-07-28 15:02:42 +00002417 {return __new_size + (__bits_per_word-1) & ~((size_type)__bits_per_word-1);};
Howard Hinnantcf823322010-12-17 14:46:43 +00002418 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
2419 _LIBCPP_INLINE_VISIBILITY void __construct_at_end(size_type __n, bool __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002420 template <class _ForwardIterator>
2421 typename enable_if
2422 <
2423 __is_forward_iterator<_ForwardIterator>::value,
2424 void
2425 >::type
2426 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
2427 void __append(size_type __n, const_reference __x);
Howard Hinnant1c936782011-06-03 19:40:40 +00002428 _LIBCPP_INLINE_VISIBILITY
2429 reference __make_ref(size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002430 {return reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnant1c936782011-06-03 19:40:40 +00002431 _LIBCPP_INLINE_VISIBILITY
2432 const_reference __make_ref(size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002433 {return const_reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnant1c936782011-06-03 19:40:40 +00002434 _LIBCPP_INLINE_VISIBILITY
2435 iterator __make_iter(size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002436 {return iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnant1c936782011-06-03 19:40:40 +00002437 _LIBCPP_INLINE_VISIBILITY
2438 const_iterator __make_iter(size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002439 {return const_iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnant1c936782011-06-03 19:40:40 +00002440 _LIBCPP_INLINE_VISIBILITY
2441 iterator __const_iterator_cast(const_iterator __p) _NOEXCEPT
Howard Hinnant76053d72013-06-27 19:35:32 +00002442 {return begin() + (__p - cbegin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002443
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002444 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002445 void __copy_assign_alloc(const vector& __v)
2446 {__copy_assign_alloc(__v, integral_constant<bool,
2447 __storage_traits::propagate_on_container_copy_assignment::value>());}
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002448 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002449 void __copy_assign_alloc(const vector& __c, true_type)
2450 {
2451 if (__alloc() != __c.__alloc())
2452 deallocate();
2453 __alloc() = __c.__alloc();
2454 }
2455
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002456 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00002457 void __copy_assign_alloc(const vector&, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002458 {}
2459
2460 void __move_assign(vector& __c, false_type);
Howard Hinnant1c936782011-06-03 19:40:40 +00002461 void __move_assign(vector& __c, true_type)
2462 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002463 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002464 void __move_assign_alloc(vector& __c)
Howard Hinnant1c936782011-06-03 19:40:40 +00002465 _NOEXCEPT_(
2466 !__storage_traits::propagate_on_container_move_assignment::value ||
2467 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002468 {__move_assign_alloc(__c, integral_constant<bool,
2469 __storage_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002470 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +00002471 void __move_assign_alloc(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002472 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002473 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002474 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002475 }
2476
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002477 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00002478 void __move_assign_alloc(vector&, false_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002479 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002480 {}
2481
Howard Hinnant1c936782011-06-03 19:40:40 +00002482 size_t __hash_code() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002483
2484 friend class __bit_reference<vector>;
2485 friend class __bit_const_reference<vector>;
2486 friend class __bit_iterator<vector, false>;
2487 friend class __bit_iterator<vector, true>;
Howard Hinnant4210a0f2012-08-17 17:10:18 +00002488 friend struct __bit_array<vector>;
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002489 friend struct _LIBCPP_TEMPLATE_VIS hash<vector>;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002490};
2491
2492template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002493inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002494void
2495vector<bool, _Allocator>::__invalidate_all_iterators()
2496{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002497}
2498
2499// Allocate space for __n objects
2500// throws length_error if __n > max_size()
2501// throws (probably bad_alloc) if memory run out
2502// Precondition: __begin_ == __end_ == __cap() == 0
2503// Precondition: __n > 0
2504// Postcondition: capacity() == __n
2505// Postcondition: size() == 0
2506template <class _Allocator>
2507void
2508vector<bool, _Allocator>::allocate(size_type __n)
2509{
2510 if (__n > max_size())
2511 this->__throw_length_error();
2512 __n = __external_cap_to_internal(__n);
2513 this->__begin_ = __storage_traits::allocate(this->__alloc(), __n);
2514 this->__size_ = 0;
2515 this->__cap() = __n;
2516}
2517
2518template <class _Allocator>
2519void
Howard Hinnant1c936782011-06-03 19:40:40 +00002520vector<bool, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002521{
Howard Hinnant76053d72013-06-27 19:35:32 +00002522 if (this->__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002523 {
2524 __storage_traits::deallocate(this->__alloc(), this->__begin_, __cap());
2525 __invalidate_all_iterators();
Howard Hinnant76053d72013-06-27 19:35:32 +00002526 this->__begin_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002527 this->__size_ = this->__cap() = 0;
2528 }
2529}
2530
2531template <class _Allocator>
2532typename vector<bool, _Allocator>::size_type
Howard Hinnant1c936782011-06-03 19:40:40 +00002533vector<bool, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002534{
2535 size_type __amax = __storage_traits::max_size(__alloc());
2536 size_type __nmax = numeric_limits<size_type>::max() / 2; // end() >= begin(), always
2537 if (__nmax / __bits_per_word <= __amax)
2538 return __nmax;
2539 return __internal_cap_to_external(__amax);
2540}
2541
2542// Precondition: __new_size > capacity()
2543template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002544inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002545typename vector<bool, _Allocator>::size_type
2546vector<bool, _Allocator>::__recommend(size_type __new_size) const
2547{
2548 const size_type __ms = max_size();
2549 if (__new_size > __ms)
2550 this->__throw_length_error();
2551 const size_type __cap = capacity();
2552 if (__cap >= __ms / 2)
2553 return __ms;
Howard Hinnantea382952013-08-14 18:00:20 +00002554 return _VSTD::max(2*__cap, __align_it(__new_size));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002555}
2556
2557// Default constructs __n objects starting at __end_
2558// Precondition: __n > 0
2559// Precondition: size() + __n <= capacity()
2560// Postcondition: size() == size() + __n
2561template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002562inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002563void
2564vector<bool, _Allocator>::__construct_at_end(size_type __n, bool __x)
2565{
2566 size_type __old_size = this->__size_;
2567 this->__size_ += __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002568 _VSTD::fill_n(__make_iter(__old_size), __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002569}
2570
2571template <class _Allocator>
2572template <class _ForwardIterator>
2573typename enable_if
2574<
2575 __is_forward_iterator<_ForwardIterator>::value,
2576 void
2577>::type
2578vector<bool, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
2579{
2580 size_type __old_size = this->__size_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002581 this->__size_ += _VSTD::distance(__first, __last);
2582 _VSTD::copy(__first, __last, __make_iter(__old_size));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002583}
2584
2585template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002586inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002587vector<bool, _Allocator>::vector()
Marshall Clowe546cbb2015-06-04 02:05:41 +00002588 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant76053d72013-06-27 19:35:32 +00002589 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002590 __size_(0),
2591 __cap_alloc_(0)
2592{
2593}
2594
2595template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002596inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002597vector<bool, _Allocator>::vector(const allocator_type& __a)
Marshall Clow8f282f42015-06-04 00:10:20 +00002598#if _LIBCPP_STD_VER <= 14
2599 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
2600#else
2601 _NOEXCEPT
2602#endif
Howard Hinnant76053d72013-06-27 19:35:32 +00002603 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002604 __size_(0),
2605 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2606{
2607}
2608
2609template <class _Allocator>
2610vector<bool, _Allocator>::vector(size_type __n)
Howard Hinnant76053d72013-06-27 19:35:32 +00002611 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002612 __size_(0),
2613 __cap_alloc_(0)
2614{
2615 if (__n > 0)
2616 {
2617 allocate(__n);
2618 __construct_at_end(__n, false);
2619 }
2620}
2621
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00002622#if _LIBCPP_STD_VER > 11
2623template <class _Allocator>
2624vector<bool, _Allocator>::vector(size_type __n, const allocator_type& __a)
2625 : __begin_(nullptr),
2626 __size_(0),
2627 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2628{
2629 if (__n > 0)
2630 {
2631 allocate(__n);
2632 __construct_at_end(__n, false);
2633 }
2634}
2635#endif
2636
Howard Hinnantc51e1022010-05-11 19:42:16 +00002637template <class _Allocator>
2638vector<bool, _Allocator>::vector(size_type __n, const value_type& __x)
Howard Hinnant76053d72013-06-27 19:35:32 +00002639 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002640 __size_(0),
2641 __cap_alloc_(0)
2642{
2643 if (__n > 0)
2644 {
2645 allocate(__n);
2646 __construct_at_end(__n, __x);
2647 }
2648}
2649
2650template <class _Allocator>
2651vector<bool, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002652 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002653 __size_(0),
2654 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2655{
2656 if (__n > 0)
2657 {
2658 allocate(__n);
2659 __construct_at_end(__n, __x);
2660 }
2661}
2662
2663template <class _Allocator>
2664template <class _InputIterator>
2665vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last,
2666 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2667 !__is_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002668 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002669 __size_(0),
2670 __cap_alloc_(0)
2671{
2672#ifndef _LIBCPP_NO_EXCEPTIONS
2673 try
2674 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002675#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002676 for (; __first != __last; ++__first)
2677 push_back(*__first);
2678#ifndef _LIBCPP_NO_EXCEPTIONS
2679 }
2680 catch (...)
2681 {
Howard Hinnant76053d72013-06-27 19:35:32 +00002682 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002683 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2684 __invalidate_all_iterators();
2685 throw;
2686 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002687#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002688}
2689
2690template <class _Allocator>
2691template <class _InputIterator>
2692vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2693 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2694 !__is_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002695 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002696 __size_(0),
2697 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2698{
2699#ifndef _LIBCPP_NO_EXCEPTIONS
2700 try
2701 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002702#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002703 for (; __first != __last; ++__first)
2704 push_back(*__first);
2705#ifndef _LIBCPP_NO_EXCEPTIONS
2706 }
2707 catch (...)
2708 {
Howard Hinnant76053d72013-06-27 19:35:32 +00002709 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002710 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2711 __invalidate_all_iterators();
2712 throw;
2713 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002714#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002715}
2716
2717template <class _Allocator>
2718template <class _ForwardIterator>
2719vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last,
2720 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002721 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002722 __size_(0),
2723 __cap_alloc_(0)
2724{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002725 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002726 if (__n > 0)
2727 {
2728 allocate(__n);
2729 __construct_at_end(__first, __last);
2730 }
2731}
2732
2733template <class _Allocator>
2734template <class _ForwardIterator>
2735vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2736 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002737 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002738 __size_(0),
2739 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2740{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002741 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002742 if (__n > 0)
2743 {
2744 allocate(__n);
2745 __construct_at_end(__first, __last);
2746 }
2747}
2748
Eric Fiseliered9e9362017-04-16 02:40:45 +00002749#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002750
Howard Hinnantc51e1022010-05-11 19:42:16 +00002751template <class _Allocator>
2752vector<bool, _Allocator>::vector(initializer_list<value_type> __il)
Howard Hinnant76053d72013-06-27 19:35:32 +00002753 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002754 __size_(0),
2755 __cap_alloc_(0)
2756{
2757 size_type __n = static_cast<size_type>(__il.size());
2758 if (__n > 0)
2759 {
2760 allocate(__n);
2761 __construct_at_end(__il.begin(), __il.end());
2762 }
2763}
2764
2765template <class _Allocator>
2766vector<bool, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002767 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002768 __size_(0),
2769 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2770{
2771 size_type __n = static_cast<size_type>(__il.size());
2772 if (__n > 0)
2773 {
2774 allocate(__n);
2775 __construct_at_end(__il.begin(), __il.end());
2776 }
2777}
2778
Eric Fiseliered9e9362017-04-16 02:40:45 +00002779#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002780
Howard Hinnantc51e1022010-05-11 19:42:16 +00002781template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002782vector<bool, _Allocator>::~vector()
2783{
Howard Hinnant76053d72013-06-27 19:35:32 +00002784 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002785 __storage_traits::deallocate(__alloc(), __begin_, __cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002786 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002787}
2788
2789template <class _Allocator>
2790vector<bool, _Allocator>::vector(const vector& __v)
Howard Hinnant76053d72013-06-27 19:35:32 +00002791 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002792 __size_(0),
2793 __cap_alloc_(0, __storage_traits::select_on_container_copy_construction(__v.__alloc()))
2794{
2795 if (__v.size() > 0)
2796 {
2797 allocate(__v.size());
2798 __construct_at_end(__v.begin(), __v.end());
2799 }
2800}
2801
2802template <class _Allocator>
2803vector<bool, _Allocator>::vector(const vector& __v, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002804 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002805 __size_(0),
2806 __cap_alloc_(0, __a)
2807{
2808 if (__v.size() > 0)
2809 {
2810 allocate(__v.size());
2811 __construct_at_end(__v.begin(), __v.end());
2812 }
2813}
2814
2815template <class _Allocator>
2816vector<bool, _Allocator>&
2817vector<bool, _Allocator>::operator=(const vector& __v)
2818{
2819 if (this != &__v)
2820 {
2821 __copy_assign_alloc(__v);
2822 if (__v.__size_)
2823 {
2824 if (__v.__size_ > capacity())
2825 {
2826 deallocate();
2827 allocate(__v.__size_);
2828 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002829 _VSTD::copy(__v.__begin_, __v.__begin_ + __external_cap_to_internal(__v.__size_), __begin_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002830 }
2831 __size_ = __v.__size_;
2832 }
2833 return *this;
2834}
2835
Eric Fiseliered9e9362017-04-16 02:40:45 +00002836#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant74279a52010-09-04 23:28:19 +00002837
Howard Hinnantc51e1022010-05-11 19:42:16 +00002838template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002839inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002840vector<bool, _Allocator>::vector(vector&& __v)
Marshall Clowe5108202015-07-14 14:46:32 +00002841#if _LIBCPP_STD_VER > 14
2842 _NOEXCEPT
2843#else
Howard Hinnant1c936782011-06-03 19:40:40 +00002844 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowe5108202015-07-14 14:46:32 +00002845#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002846 : __begin_(__v.__begin_),
2847 __size_(__v.__size_),
2848 __cap_alloc_(__v.__cap_alloc_)
2849{
Howard Hinnant76053d72013-06-27 19:35:32 +00002850 __v.__begin_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002851 __v.__size_ = 0;
2852 __v.__cap() = 0;
2853}
2854
2855template <class _Allocator>
2856vector<bool, _Allocator>::vector(vector&& __v, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002857 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002858 __size_(0),
2859 __cap_alloc_(0, __a)
2860{
2861 if (__a == allocator_type(__v.__alloc()))
2862 {
2863 this->__begin_ = __v.__begin_;
2864 this->__size_ = __v.__size_;
2865 this->__cap() = __v.__cap();
2866 __v.__begin_ = nullptr;
2867 __v.__cap() = __v.__size_ = 0;
2868 }
2869 else if (__v.size() > 0)
2870 {
2871 allocate(__v.size());
2872 __construct_at_end(__v.begin(), __v.end());
2873 }
2874}
2875
2876template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002877inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002878vector<bool, _Allocator>&
2879vector<bool, _Allocator>::operator=(vector&& __v)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002880 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002881{
2882 __move_assign(__v, integral_constant<bool,
2883 __storage_traits::propagate_on_container_move_assignment::value>());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002884 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002885}
2886
2887template <class _Allocator>
2888void
2889vector<bool, _Allocator>::__move_assign(vector& __c, false_type)
2890{
2891 if (__alloc() != __c.__alloc())
2892 assign(__c.begin(), __c.end());
2893 else
2894 __move_assign(__c, true_type());
2895}
2896
2897template <class _Allocator>
2898void
2899vector<bool, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002900 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002901{
2902 deallocate();
Marshall Clowb4871fa2014-07-21 15:15:15 +00002903 __move_assign_alloc(__c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002904 this->__begin_ = __c.__begin_;
2905 this->__size_ = __c.__size_;
2906 this->__cap() = __c.__cap();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002907 __c.__begin_ = nullptr;
2908 __c.__cap() = __c.__size_ = 0;
2909}
Howard Hinnant74279a52010-09-04 23:28:19 +00002910
Eric Fiseliered9e9362017-04-16 02:40:45 +00002911#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002912
2913template <class _Allocator>
2914void
2915vector<bool, _Allocator>::assign(size_type __n, const value_type& __x)
2916{
2917 __size_ = 0;
2918 if (__n > 0)
2919 {
2920 size_type __c = capacity();
2921 if (__n <= __c)
2922 __size_ = __n;
2923 else
2924 {
2925 vector __v(__alloc());
2926 __v.reserve(__recommend(__n));
2927 __v.__size_ = __n;
2928 swap(__v);
2929 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002930 _VSTD::fill_n(begin(), __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002931 }
Eric Fiselier69c51982016-12-28 06:06:09 +00002932 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002933}
2934
2935template <class _Allocator>
2936template <class _InputIterator>
2937typename enable_if
2938<
2939 __is_input_iterator<_InputIterator>::value &&
2940 !__is_forward_iterator<_InputIterator>::value,
2941 void
2942>::type
2943vector<bool, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2944{
2945 clear();
2946 for (; __first != __last; ++__first)
2947 push_back(*__first);
2948}
2949
2950template <class _Allocator>
2951template <class _ForwardIterator>
2952typename enable_if
2953<
2954 __is_forward_iterator<_ForwardIterator>::value,
2955 void
2956>::type
2957vector<bool, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2958{
2959 clear();
Eric Fiselier6003c772016-12-23 23:37:52 +00002960 difference_type __ns = _VSTD::distance(__first, __last);
2961 _LIBCPP_ASSERT(__ns >= 0, "invalid range specified");
2962 const size_t __n = static_cast<size_type>(__ns);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002963 if (__n)
2964 {
2965 if (__n > capacity())
2966 {
2967 deallocate();
2968 allocate(__n);
2969 }
2970 __construct_at_end(__first, __last);
2971 }
2972}
2973
2974template <class _Allocator>
2975void
2976vector<bool, _Allocator>::reserve(size_type __n)
2977{
2978 if (__n > capacity())
2979 {
2980 vector __v(this->__alloc());
2981 __v.allocate(__n);
2982 __v.__construct_at_end(this->begin(), this->end());
2983 swap(__v);
2984 __invalidate_all_iterators();
2985 }
2986}
2987
2988template <class _Allocator>
2989void
Howard Hinnant1c936782011-06-03 19:40:40 +00002990vector<bool, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002991{
2992 if (__external_cap_to_internal(size()) > __cap())
2993 {
2994#ifndef _LIBCPP_NO_EXCEPTIONS
2995 try
2996 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002997#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002998 vector(*this, allocator_type(__alloc())).swap(*this);
2999#ifndef _LIBCPP_NO_EXCEPTIONS
3000 }
3001 catch (...)
3002 {
3003 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003004#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003005 }
3006}
3007
3008template <class _Allocator>
3009typename vector<bool, _Allocator>::reference
3010vector<bool, _Allocator>::at(size_type __n)
3011{
3012 if (__n >= size())
3013 this->__throw_out_of_range();
3014 return (*this)[__n];
3015}
3016
3017template <class _Allocator>
3018typename vector<bool, _Allocator>::const_reference
3019vector<bool, _Allocator>::at(size_type __n) const
3020{
3021 if (__n >= size())
3022 this->__throw_out_of_range();
3023 return (*this)[__n];
3024}
3025
3026template <class _Allocator>
3027void
3028vector<bool, _Allocator>::push_back(const value_type& __x)
3029{
3030 if (this->__size_ == this->capacity())
3031 reserve(__recommend(this->__size_ + 1));
3032 ++this->__size_;
3033 back() = __x;
3034}
3035
3036template <class _Allocator>
3037typename vector<bool, _Allocator>::iterator
3038vector<bool, _Allocator>::insert(const_iterator __position, const value_type& __x)
3039{
3040 iterator __r;
3041 if (size() < capacity())
3042 {
3043 const_iterator __old_end = end();
3044 ++__size_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003045 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003046 __r = __const_iterator_cast(__position);
3047 }
3048 else
3049 {
3050 vector __v(__alloc());
3051 __v.reserve(__recommend(__size_ + 1));
3052 __v.__size_ = __size_ + 1;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003053 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3054 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003055 swap(__v);
3056 }
3057 *__r = __x;
3058 return __r;
3059}
3060
3061template <class _Allocator>
3062typename vector<bool, _Allocator>::iterator
3063vector<bool, _Allocator>::insert(const_iterator __position, size_type __n, const value_type& __x)
3064{
3065 iterator __r;
3066 size_type __c = capacity();
3067 if (__n <= __c && size() <= __c - __n)
3068 {
3069 const_iterator __old_end = end();
3070 __size_ += __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003071 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003072 __r = __const_iterator_cast(__position);
3073 }
3074 else
3075 {
3076 vector __v(__alloc());
3077 __v.reserve(__recommend(__size_ + __n));
3078 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003079 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3080 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003081 swap(__v);
3082 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003083 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003084 return __r;
3085}
3086
3087template <class _Allocator>
3088template <class _InputIterator>
3089typename enable_if
3090<
3091 __is_input_iterator <_InputIterator>::value &&
3092 !__is_forward_iterator<_InputIterator>::value,
3093 typename vector<bool, _Allocator>::iterator
3094>::type
3095vector<bool, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
3096{
3097 difference_type __off = __position - begin();
3098 iterator __p = __const_iterator_cast(__position);
3099 iterator __old_end = end();
3100 for (; size() != capacity() && __first != __last; ++__first)
3101 {
3102 ++this->__size_;
3103 back() = *__first;
3104 }
3105 vector __v(__alloc());
3106 if (__first != __last)
3107 {
3108#ifndef _LIBCPP_NO_EXCEPTIONS
3109 try
3110 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003111#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003112 __v.assign(__first, __last);
3113 difference_type __old_size = static_cast<difference_type>(__old_end - begin());
3114 difference_type __old_p = __p - begin();
3115 reserve(__recommend(size() + __v.size()));
3116 __p = begin() + __old_p;
3117 __old_end = begin() + __old_size;
3118#ifndef _LIBCPP_NO_EXCEPTIONS
3119 }
3120 catch (...)
3121 {
3122 erase(__old_end, end());
3123 throw;
3124 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003125#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003126 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003127 __p = _VSTD::rotate(__p, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003128 insert(__p, __v.begin(), __v.end());
3129 return begin() + __off;
3130}
3131
3132template <class _Allocator>
3133template <class _ForwardIterator>
3134typename enable_if
3135<
3136 __is_forward_iterator<_ForwardIterator>::value,
3137 typename vector<bool, _Allocator>::iterator
3138>::type
3139vector<bool, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
3140{
Eric Fiselier654dd332016-12-11 05:31:00 +00003141 const difference_type __n_signed = _VSTD::distance(__first, __last);
3142 _LIBCPP_ASSERT(__n_signed >= 0, "invalid range specified");
3143 const size_type __n = static_cast<size_type>(__n_signed);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003144 iterator __r;
3145 size_type __c = capacity();
3146 if (__n <= __c && size() <= __c - __n)
3147 {
3148 const_iterator __old_end = end();
3149 __size_ += __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003150 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003151 __r = __const_iterator_cast(__position);
3152 }
3153 else
3154 {
3155 vector __v(__alloc());
3156 __v.reserve(__recommend(__size_ + __n));
3157 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003158 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3159 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003160 swap(__v);
3161 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003162 _VSTD::copy(__first, __last, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003163 return __r;
3164}
3165
3166template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003167inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003168typename vector<bool, _Allocator>::iterator
3169vector<bool, _Allocator>::erase(const_iterator __position)
3170{
3171 iterator __r = __const_iterator_cast(__position);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003172 _VSTD::copy(__position + 1, this->cend(), __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003173 --__size_;
3174 return __r;
3175}
3176
3177template <class _Allocator>
3178typename vector<bool, _Allocator>::iterator
3179vector<bool, _Allocator>::erase(const_iterator __first, const_iterator __last)
3180{
3181 iterator __r = __const_iterator_cast(__first);
3182 difference_type __d = __last - __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003183 _VSTD::copy(__last, this->cend(), __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003184 __size_ -= __d;
3185 return __r;
3186}
3187
3188template <class _Allocator>
3189void
3190vector<bool, _Allocator>::swap(vector& __x)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003191#if _LIBCPP_STD_VER >= 14
3192 _NOEXCEPT
3193#else
Eric Fiselier69c51982016-12-28 06:06:09 +00003194 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003195 __is_nothrow_swappable<allocator_type>::value)
3196#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003197{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003198 _VSTD::swap(this->__begin_, __x.__begin_);
3199 _VSTD::swap(this->__size_, __x.__size_);
3200 _VSTD::swap(this->__cap(), __x.__cap());
Eric Fiseliered9e9362017-04-16 02:40:45 +00003201 __swap_allocator(this->__alloc(), __x.__alloc(),
Marshall Clow8982dcd2015-07-13 20:04:56 +00003202 integral_constant<bool, __alloc_traits::propagate_on_container_swap::value>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003203}
3204
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003205template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003206void
3207vector<bool, _Allocator>::resize(size_type __sz, value_type __x)
3208{
3209 size_type __cs = size();
3210 if (__cs < __sz)
3211 {
3212 iterator __r;
3213 size_type __c = capacity();
3214 size_type __n = __sz - __cs;
3215 if (__n <= __c && __cs <= __c - __n)
3216 {
3217 __r = end();
3218 __size_ += __n;
3219 }
3220 else
3221 {
3222 vector __v(__alloc());
3223 __v.reserve(__recommend(__size_ + __n));
3224 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003225 __r = _VSTD::copy(cbegin(), cend(), __v.begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003226 swap(__v);
3227 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003228 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003229 }
3230 else
3231 __size_ = __sz;
3232}
3233
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003234template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003235void
Howard Hinnant1c936782011-06-03 19:40:40 +00003236vector<bool, _Allocator>::flip() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003237{
3238 // do middle whole words
3239 size_type __n = __size_;
3240 __storage_pointer __p = __begin_;
3241 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3242 *__p = ~*__p;
3243 // do last partial word
3244 if (__n > 0)
3245 {
3246 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3247 __storage_type __b = *__p & __m;
3248 *__p &= ~__m;
3249 *__p |= ~__b & __m;
3250 }
3251}
3252
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003253template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003254bool
3255vector<bool, _Allocator>::__invariants() const
3256{
Howard Hinnant76053d72013-06-27 19:35:32 +00003257 if (this->__begin_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003258 {
3259 if (this->__size_ != 0 || this->__cap() != 0)
3260 return false;
3261 }
3262 else
3263 {
3264 if (this->__cap() == 0)
3265 return false;
3266 if (this->__size_ > this->capacity())
3267 return false;
3268 }
3269 return true;
3270}
3271
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003272template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003273size_t
Howard Hinnant1c936782011-06-03 19:40:40 +00003274vector<bool, _Allocator>::__hash_code() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003275{
3276 size_t __h = 0;
3277 // do middle whole words
3278 size_type __n = __size_;
3279 __storage_pointer __p = __begin_;
3280 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3281 __h ^= *__p;
3282 // do last partial word
3283 if (__n > 0)
3284 {
3285 const __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3286 __h ^= *__p & __m;
3287 }
3288 return __h;
3289}
3290
3291template <class _Allocator>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003292struct _LIBCPP_TEMPLATE_VIS hash<vector<bool, _Allocator> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00003293 : public unary_function<vector<bool, _Allocator>, size_t>
3294{
Howard Hinnant1c265cd2010-09-23 18:58:28 +00003295 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +00003296 size_t operator()(const vector<bool, _Allocator>& __vec) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003297 {return __vec.__hash_code();}
3298};
3299
3300template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003301inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003302bool
3303operator==(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3304{
3305 const typename vector<_Tp, _Allocator>::size_type __sz = __x.size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003306 return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003307}
3308
3309template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003310inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003311bool
3312operator!=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3313{
3314 return !(__x == __y);
3315}
3316
3317template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003318inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003319bool
3320operator< (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3321{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003322 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003323}
3324
3325template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003326inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003327bool
3328operator> (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3329{
3330 return __y < __x;
3331}
3332
3333template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003334inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003335bool
3336operator>=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3337{
3338 return !(__x < __y);
3339}
3340
3341template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003342inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003343bool
3344operator<=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3345{
3346 return !(__y < __x);
3347}
3348
3349template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003350inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003351void
3352swap(vector<_Tp, _Allocator>& __x, vector<_Tp, _Allocator>& __y)
Howard Hinnant1c936782011-06-03 19:40:40 +00003353 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00003354{
3355 __x.swap(__y);
3356}
3357
3358_LIBCPP_END_NAMESPACE_STD
3359
3360#endif // _LIBCPP_VECTOR