blob: 8f404dc4211e6ca38b90086e19f4fd48e44d7ede [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);
Howard Hinnant33711792011-08-12 21:56:02 +0000530#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantcf823322010-12-17 14:46:43 +0000531 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000532 vector(initializer_list<value_type> __il);
Howard Hinnantcf823322010-12-17 14:46:43 +0000533 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000534 vector(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnant33711792011-08-12 21:56:02 +0000535#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnanta47c6d52011-09-16 17:29:17 +0000536#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000537 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant27e0e772011-09-14 18:33:51 +0000538 ~vector()
539 {
540 __get_db()->__erase_c(this);
541 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000542#endif
543
544 vector(const vector& __x);
545 vector(const vector& __x, const allocator_type& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000546 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000547 vector& operator=(const vector& __x);
Howard Hinnant74279a52010-09-04 23:28:19 +0000548#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantcf823322010-12-17 14:46:43 +0000549 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000550 vector(vector&& __x)
Marshall Clowe5108202015-07-14 14:46:32 +0000551#if _LIBCPP_STD_VER > 14
552 _NOEXCEPT;
553#else
Howard Hinnant1c936782011-06-03 19:40:40 +0000554 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowe5108202015-07-14 14:46:32 +0000555#endif
Howard Hinnantcf823322010-12-17 14:46:43 +0000556 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000557 vector(vector&& __x, const allocator_type& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000558 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000559 vector& operator=(vector&& __x)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000560 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Howard Hinnant74279a52010-09-04 23:28:19 +0000561#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant33711792011-08-12 21:56:02 +0000562#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000563 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000564 vector& operator=(initializer_list<value_type> __il)
565 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnant33711792011-08-12 21:56:02 +0000566#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000567
568 template <class _InputIterator>
569 typename enable_if
570 <
571 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000572 !__is_forward_iterator<_InputIterator>::value &&
573 is_constructible<
574 value_type,
575 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000576 void
577 >::type
578 assign(_InputIterator __first, _InputIterator __last);
579 template <class _ForwardIterator>
580 typename enable_if
581 <
Howard Hinnant88010252013-03-28 17:44:32 +0000582 __is_forward_iterator<_ForwardIterator>::value &&
583 is_constructible<
584 value_type,
585 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000586 void
587 >::type
588 assign(_ForwardIterator __first, _ForwardIterator __last);
589
590 void assign(size_type __n, const_reference __u);
Howard Hinnant33711792011-08-12 21:56:02 +0000591#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000592 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000593 void assign(initializer_list<value_type> __il)
594 {assign(__il.begin(), __il.end());}
Howard Hinnant33711792011-08-12 21:56:02 +0000595#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000596
Howard Hinnant1c936782011-06-03 19:40:40 +0000597 _LIBCPP_INLINE_VISIBILITY
598 allocator_type get_allocator() const _NOEXCEPT
599 {return this->__alloc();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000600
Howard Hinnant1c936782011-06-03 19:40:40 +0000601 _LIBCPP_INLINE_VISIBILITY iterator begin() _NOEXCEPT;
602 _LIBCPP_INLINE_VISIBILITY const_iterator begin() const _NOEXCEPT;
603 _LIBCPP_INLINE_VISIBILITY iterator end() _NOEXCEPT;
604 _LIBCPP_INLINE_VISIBILITY const_iterator end() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000605
Howard Hinnant1c936782011-06-03 19:40:40 +0000606 _LIBCPP_INLINE_VISIBILITY
607 reverse_iterator rbegin() _NOEXCEPT
608 {return reverse_iterator(end());}
609 _LIBCPP_INLINE_VISIBILITY
610 const_reverse_iterator rbegin() const _NOEXCEPT
611 {return const_reverse_iterator(end());}
612 _LIBCPP_INLINE_VISIBILITY
613 reverse_iterator rend() _NOEXCEPT
614 {return reverse_iterator(begin());}
615 _LIBCPP_INLINE_VISIBILITY
616 const_reverse_iterator rend() const _NOEXCEPT
617 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000618
Howard Hinnant1c936782011-06-03 19:40:40 +0000619 _LIBCPP_INLINE_VISIBILITY
620 const_iterator cbegin() const _NOEXCEPT
621 {return begin();}
622 _LIBCPP_INLINE_VISIBILITY
623 const_iterator cend() const _NOEXCEPT
624 {return end();}
625 _LIBCPP_INLINE_VISIBILITY
626 const_reverse_iterator crbegin() const _NOEXCEPT
627 {return rbegin();}
628 _LIBCPP_INLINE_VISIBILITY
629 const_reverse_iterator crend() const _NOEXCEPT
630 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000631
Howard Hinnant1c936782011-06-03 19:40:40 +0000632 _LIBCPP_INLINE_VISIBILITY
633 size_type size() const _NOEXCEPT
634 {return static_cast<size_type>(this->__end_ - this->__begin_);}
635 _LIBCPP_INLINE_VISIBILITY
636 size_type capacity() const _NOEXCEPT
637 {return __base::capacity();}
638 _LIBCPP_INLINE_VISIBILITY
639 bool empty() const _NOEXCEPT
640 {return this->__begin_ == this->__end_;}
641 size_type max_size() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000642 void reserve(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +0000643 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000644
645 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n);
646 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const;
647 reference at(size_type __n);
648 const_reference at(size_type __n) const;
649
Howard Hinnant27e0e772011-09-14 18:33:51 +0000650 _LIBCPP_INLINE_VISIBILITY reference front()
651 {
652 _LIBCPP_ASSERT(!empty(), "front() called for empty vector");
653 return *this->__begin_;
654 }
655 _LIBCPP_INLINE_VISIBILITY const_reference front() const
656 {
657 _LIBCPP_ASSERT(!empty(), "front() called for empty vector");
658 return *this->__begin_;
659 }
660 _LIBCPP_INLINE_VISIBILITY reference back()
661 {
662 _LIBCPP_ASSERT(!empty(), "back() called for empty vector");
663 return *(this->__end_ - 1);
664 }
665 _LIBCPP_INLINE_VISIBILITY const_reference back() const
666 {
667 _LIBCPP_ASSERT(!empty(), "back() called for empty vector");
668 return *(this->__end_ - 1);
669 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000670
Howard Hinnant1c936782011-06-03 19:40:40 +0000671 _LIBCPP_INLINE_VISIBILITY
672 value_type* data() _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000673 {return _VSTD::__to_raw_pointer(this->__begin_);}
Howard Hinnant1c936782011-06-03 19:40:40 +0000674 _LIBCPP_INLINE_VISIBILITY
675 const value_type* data() const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000676 {return _VSTD::__to_raw_pointer(this->__begin_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000677
Howard Hinnantcf823322010-12-17 14:46:43 +0000678 _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
Howard Hinnant74279a52010-09-04 23:28:19 +0000679#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000680 _LIBCPP_INLINE_VISIBILITY void push_back(value_type&& __x);
Howard Hinnant74279a52010-09-04 23:28:19 +0000681#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000682 template <class... _Args>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000683 _LIBCPP_INLINE_VISIBILITY
Marshall Clowea52cc42017-01-24 23:09:12 +0000684#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +0000685 reference emplace_back(_Args&&... __args);
Marshall Clowea52cc42017-01-24 23:09:12 +0000686#else
687 void emplace_back(_Args&&... __args);
688#endif
Howard Hinnant74279a52010-09-04 23:28:19 +0000689#endif // _LIBCPP_HAS_NO_VARIADICS
690#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000691 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000692 void pop_back();
693
694 iterator insert(const_iterator __position, const_reference __x);
Howard Hinnant74279a52010-09-04 23:28:19 +0000695#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +0000696 iterator insert(const_iterator __position, value_type&& __x);
Howard Hinnant74279a52010-09-04 23:28:19 +0000697#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000698 template <class... _Args>
699 iterator emplace(const_iterator __position, _Args&&... __args);
Howard Hinnant74279a52010-09-04 23:28:19 +0000700#endif // _LIBCPP_HAS_NO_VARIADICS
701#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +0000702 iterator insert(const_iterator __position, size_type __n, const_reference __x);
703 template <class _InputIterator>
704 typename enable_if
705 <
706 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000707 !__is_forward_iterator<_InputIterator>::value &&
708 is_constructible<
709 value_type,
710 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000711 iterator
712 >::type
713 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
714 template <class _ForwardIterator>
715 typename enable_if
716 <
Howard Hinnant88010252013-03-28 17:44:32 +0000717 __is_forward_iterator<_ForwardIterator>::value &&
718 is_constructible<
719 value_type,
720 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000721 iterator
722 >::type
723 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnant33711792011-08-12 21:56:02 +0000724#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000725 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000726 iterator insert(const_iterator __position, initializer_list<value_type> __il)
727 {return insert(__position, __il.begin(), __il.end());}
Howard Hinnant33711792011-08-12 21:56:02 +0000728#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000729
Howard Hinnantcf823322010-12-17 14:46:43 +0000730 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000731 iterator erase(const_iterator __first, const_iterator __last);
732
Howard Hinnant1c936782011-06-03 19:40:40 +0000733 _LIBCPP_INLINE_VISIBILITY
734 void clear() _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000735 {
Marshall Clow2cd9d372014-05-08 14:14:06 +0000736 size_type __old_size = size();
Howard Hinnant27e0e772011-09-14 18:33:51 +0000737 __base::clear();
Marshall Clow2cd9d372014-05-08 14:14:06 +0000738 __annotate_shrink(__old_size);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000739 __invalidate_all_iterators();
740 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000741
742 void resize(size_type __sz);
743 void resize(size_type __sz, const_reference __x);
744
Howard Hinnant1c936782011-06-03 19:40:40 +0000745 void swap(vector&)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000746#if _LIBCPP_STD_VER >= 14
Eric Fiselier69c51982016-12-28 06:06:09 +0000747 _NOEXCEPT_DEBUG;
Marshall Clow8982dcd2015-07-13 20:04:56 +0000748#else
Eric Fiselier69c51982016-12-28 06:06:09 +0000749 _NOEXCEPT_DEBUG_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +0000750 __is_nothrow_swappable<allocator_type>::value);
751#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000752
753 bool __invariants() const;
754
Howard Hinnanta47c6d52011-09-16 17:29:17 +0000755#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000756
757 bool __dereferenceable(const const_iterator* __i) const;
758 bool __decrementable(const const_iterator* __i) const;
759 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
760 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
761
Howard Hinnanta47c6d52011-09-16 17:29:17 +0000762#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000763
Howard Hinnantc51e1022010-05-11 19:42:16 +0000764private:
Howard Hinnantcf823322010-12-17 14:46:43 +0000765 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Eric Fiselier69c51982016-12-28 06:06:09 +0000766 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(pointer __new_last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000767 void allocate(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +0000768 void deallocate() _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000769 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
Howard Hinnanta5589382011-01-04 19:53:31 +0000770 void __construct_at_end(size_type __n);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000771 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000772 void __construct_at_end(size_type __n, const_reference __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000773 template <class _ForwardIterator>
774 typename enable_if
775 <
776 __is_forward_iterator<_ForwardIterator>::value,
777 void
778 >::type
Eric Fiselieracfe6f02015-03-31 16:54:19 +0000779 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000780 void __append(size_type __n);
781 void __append(size_type __n, const_reference __x);
Howard Hinnantcf823322010-12-17 14:46:43 +0000782 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000783 iterator __make_iter(pointer __p) _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000784 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000785 const_iterator __make_iter(const_pointer __p) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000786 void __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v);
787 pointer __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p);
788 void __move_range(pointer __from_s, pointer __from_e, pointer __to);
Howard Hinnant1c936782011-06-03 19:40:40 +0000789 void __move_assign(vector& __c, true_type)
790 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000791 void __move_assign(vector& __c, false_type)
792 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000793 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76053d72013-06-27 19:35:32 +0000794 void __destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000795 {
Eric Fiselier69c51982016-12-28 06:06:09 +0000796 __invalidate_iterators_past(__new_last);
Marshall Clow2cd9d372014-05-08 14:14:06 +0000797 size_type __old_size = size();
Howard Hinnant27e0e772011-09-14 18:33:51 +0000798 __base::__destruct_at_end(__new_last);
Marshall Clow2cd9d372014-05-08 14:14:06 +0000799 __annotate_shrink(__old_size);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000800 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000801 template <class _Up>
802 void
803#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
804 __push_back_slow_path(_Up&& __x);
805#else
806 __push_back_slow_path(_Up& __x);
807#endif
Howard Hinnantb6c49562012-02-26 15:30:12 +0000808#if !defined(_LIBCPP_HAS_NO_VARIADICS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
809 template <class... _Args>
810 void
811 __emplace_back_slow_path(_Args&&... __args);
812#endif
Marshall Clow2cd9d372014-05-08 14:14:06 +0000813 // The following functions are no-ops outside of AddressSanitizer mode.
814 // We call annotatations only for the default Allocator because other allocators
815 // may not meet the AddressSanitizer alignment constraints.
816 // See the documentation for __sanitizer_annotate_contiguous_container for more details.
Marshall Clow2cd9d372014-05-08 14:14:06 +0000817#ifndef _LIBCPP_HAS_NO_ASAN
Eric Fiselier6003c772016-12-23 23:37:52 +0000818 void __annotate_contiguous_container(const void *__beg, const void *__end,
819 const void *__old_mid,
820 const void *__new_mid) const
821 {
822
Marshall Clow2cd9d372014-05-08 14:14:06 +0000823 if (__beg && is_same<allocator_type, __default_allocator_type>::value)
824 __sanitizer_annotate_contiguous_container(__beg, __end, __old_mid, __new_mid);
Marshall Clow2cd9d372014-05-08 14:14:06 +0000825 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000826#else
827 _LIBCPP_INLINE_VISIBILITY
828 void __annotate_contiguous_container(const void*, const void*, const void*,
829 const void*) const {}
830#endif
831 _LIBCPP_INLINE_VISIBILITY
832 void __annotate_new(size_type __current_size) const {
Marshall Clow2cd9d372014-05-08 14:14:06 +0000833 __annotate_contiguous_container(data(), data() + capacity(),
834 data() + capacity(), data() + __current_size);
835 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000836
837 _LIBCPP_INLINE_VISIBILITY
838 void __annotate_delete() const {
Marshall Clow2cd9d372014-05-08 14:14:06 +0000839 __annotate_contiguous_container(data(), data() + capacity(),
840 data() + size(), data() + capacity());
841 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000842
843 _LIBCPP_INLINE_VISIBILITY
Kostya Serebryany4963c252014-09-02 23:43:38 +0000844 void __annotate_increase(size_type __n) const
Marshall Clow2cd9d372014-05-08 14:14:06 +0000845 {
846 __annotate_contiguous_container(data(), data() + capacity(),
847 data() + size(), data() + size() + __n);
848 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000849
850 _LIBCPP_INLINE_VISIBILITY
Kostya Serebryany4963c252014-09-02 23:43:38 +0000851 void __annotate_shrink(size_type __old_size) const
Marshall Clow2cd9d372014-05-08 14:14:06 +0000852 {
853 __annotate_contiguous_container(data(), data() + capacity(),
854 data() + __old_size, data() + size());
855 }
Marshall Clowc78ffa52014-09-03 21:37:43 +0000856#ifndef _LIBCPP_HAS_NO_ASAN
Kostya Serebryany4963c252014-09-02 23:43:38 +0000857 // The annotation for size increase should happen before the actual increase,
858 // but if an exception is thrown after that the annotation has to be undone.
859 struct __RAII_IncreaseAnnotator {
860 __RAII_IncreaseAnnotator(const vector &__v, size_type __n = 1)
Eric Fiseliera7c043e2015-03-10 00:25:20 +0000861 : __commit(false), __v(__v), __old_size(__v.size() + __n) {
Kostya Serebryany4963c252014-09-02 23:43:38 +0000862 __v.__annotate_increase(__n);
863 }
864 void __done() { __commit = true; }
865 ~__RAII_IncreaseAnnotator() {
866 if (__commit) return;
Eric Fiseliera7c043e2015-03-10 00:25:20 +0000867 __v.__annotate_shrink(__old_size);
Kostya Serebryany4963c252014-09-02 23:43:38 +0000868 }
869 bool __commit;
Kostya Serebryany4963c252014-09-02 23:43:38 +0000870 const vector &__v;
Eric Fiseliera7c043e2015-03-10 00:25:20 +0000871 size_type __old_size;
Kostya Serebryany4963c252014-09-02 23:43:38 +0000872 };
Marshall Clowc78ffa52014-09-03 21:37:43 +0000873#else
874 struct __RAII_IncreaseAnnotator {
Eric Fiselier6003c772016-12-23 23:37:52 +0000875 _LIBCPP_INLINE_VISIBILITY
876 __RAII_IncreaseAnnotator(const vector &, size_type = 1) {}
877 _LIBCPP_INLINE_VISIBILITY void __done() {}
Marshall Clowc78ffa52014-09-03 21:37:43 +0000878 };
879#endif
880
Howard Hinnantc51e1022010-05-11 19:42:16 +0000881};
882
883template <class _Tp, class _Allocator>
884void
885vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v)
886{
Marshall Clow2cd9d372014-05-08 14:14:06 +0000887 __annotate_delete();
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000888 __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, this->__end_, __v.__begin_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000889 _VSTD::swap(this->__begin_, __v.__begin_);
890 _VSTD::swap(this->__end_, __v.__end_);
891 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000892 __v.__first_ = __v.__begin_;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000893 __annotate_new(size());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000894 __invalidate_all_iterators();
895}
896
897template <class _Tp, class _Allocator>
898typename vector<_Tp, _Allocator>::pointer
899vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p)
900{
Marshall Clow2cd9d372014-05-08 14:14:06 +0000901 __annotate_delete();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000902 pointer __r = __v.__begin_;
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000903 __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, __p, __v.__begin_);
904 __alloc_traits::__construct_forward(this->__alloc(), __p, this->__end_, __v.__end_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000905 _VSTD::swap(this->__begin_, __v.__begin_);
906 _VSTD::swap(this->__end_, __v.__end_);
907 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000908 __v.__first_ = __v.__begin_;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000909 __annotate_new(size());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000910 __invalidate_all_iterators();
911 return __r;
912}
913
914// Allocate space for __n objects
915// throws length_error if __n > max_size()
916// throws (probably bad_alloc) if memory run out
917// Precondition: __begin_ == __end_ == __end_cap() == 0
918// Precondition: __n > 0
919// Postcondition: capacity() == __n
920// Postcondition: size() == 0
921template <class _Tp, class _Allocator>
922void
923vector<_Tp, _Allocator>::allocate(size_type __n)
924{
925 if (__n > max_size())
926 this->__throw_length_error();
927 this->__begin_ = this->__end_ = __alloc_traits::allocate(this->__alloc(), __n);
928 this->__end_cap() = this->__begin_ + __n;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000929 __annotate_new(0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000930}
931
932template <class _Tp, class _Allocator>
933void
Howard Hinnant1c936782011-06-03 19:40:40 +0000934vector<_Tp, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000935{
Howard Hinnant76053d72013-06-27 19:35:32 +0000936 if (this->__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000937 {
938 clear();
939 __alloc_traits::deallocate(this->__alloc(), this->__begin_, capacity());
Howard Hinnant76053d72013-06-27 19:35:32 +0000940 this->__begin_ = this->__end_ = this->__end_cap() = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000941 }
942}
943
944template <class _Tp, class _Allocator>
945typename vector<_Tp, _Allocator>::size_type
Howard Hinnant1c936782011-06-03 19:40:40 +0000946vector<_Tp, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000947{
Eric Fiselierb5d9f442016-11-23 01:18:56 +0000948 return _VSTD::min<size_type>(__alloc_traits::max_size(this->__alloc()),
949 numeric_limits<difference_type>::max());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000950}
951
952// Precondition: __new_size > capacity()
953template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000954inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000955typename vector<_Tp, _Allocator>::size_type
956vector<_Tp, _Allocator>::__recommend(size_type __new_size) const
957{
958 const size_type __ms = max_size();
959 if (__new_size > __ms)
960 this->__throw_length_error();
961 const size_type __cap = capacity();
962 if (__cap >= __ms / 2)
963 return __ms;
Alexis Hunt991d29b2011-07-29 23:31:58 +0000964 return _VSTD::max<size_type>(2*__cap, __new_size);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000965}
966
967// Default constructs __n objects starting at __end_
968// throws if construction throws
969// Precondition: __n > 0
970// Precondition: size() + __n <= capacity()
971// Postcondition: size() == size() + __n
972template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000973void
974vector<_Tp, _Allocator>::__construct_at_end(size_type __n)
975{
Howard Hinnantc51e1022010-05-11 19:42:16 +0000976 allocator_type& __a = this->__alloc();
977 do
978 {
Kostya Serebryany4963c252014-09-02 23:43:38 +0000979 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000980 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000981 ++this->__end_;
982 --__n;
Kostya Serebryany4963c252014-09-02 23:43:38 +0000983 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000984 } while (__n > 0);
985}
986
Howard Hinnantc51e1022010-05-11 19:42:16 +0000987// Copy constructs __n objects starting at __end_ from __x
988// throws if construction throws
989// Precondition: __n > 0
990// Precondition: size() + __n <= capacity()
991// Postcondition: size() == old size() + __n
992// Postcondition: [i] == __x for all i in [size() - __n, __n)
993template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000994inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000995void
996vector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
997{
Howard Hinnantc51e1022010-05-11 19:42:16 +0000998 allocator_type& __a = this->__alloc();
999 do
1000 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001001 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001002 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001003 ++this->__end_;
1004 --__n;
Kostya Serebryany4963c252014-09-02 23:43:38 +00001005 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001006 } while (__n > 0);
1007}
1008
1009template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001010template <class _ForwardIterator>
1011typename enable_if
1012<
1013 __is_forward_iterator<_ForwardIterator>::value,
1014 void
1015>::type
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001016vector<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001017{
1018 allocator_type& __a = this->__alloc();
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001019 __RAII_IncreaseAnnotator __annotator(*this, __n);
1020 __alloc_traits::__construct_range_forward(__a, __first, __last, this->__end_);
1021 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001022}
1023
1024// Default constructs __n objects starting at __end_
1025// throws if construction throws
1026// Postcondition: size() == size() + __n
Howard Hinnant1c936782011-06-03 19:40:40 +00001027// Exception safety: strong.
Howard Hinnantc51e1022010-05-11 19:42:16 +00001028template <class _Tp, class _Allocator>
1029void
1030vector<_Tp, _Allocator>::__append(size_type __n)
1031{
1032 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1033 this->__construct_at_end(__n);
1034 else
1035 {
1036 allocator_type& __a = this->__alloc();
1037 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1038 __v.__construct_at_end(__n);
1039 __swap_out_circular_buffer(__v);
1040 }
1041}
1042
1043// Default constructs __n objects starting at __end_
1044// throws if construction throws
1045// Postcondition: size() == size() + __n
Howard Hinnant1c936782011-06-03 19:40:40 +00001046// Exception safety: strong.
Howard Hinnantc51e1022010-05-11 19:42:16 +00001047template <class _Tp, class _Allocator>
1048void
1049vector<_Tp, _Allocator>::__append(size_type __n, const_reference __x)
1050{
1051 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1052 this->__construct_at_end(__n, __x);
1053 else
1054 {
1055 allocator_type& __a = this->__alloc();
1056 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1057 __v.__construct_at_end(__n, __x);
1058 __swap_out_circular_buffer(__v);
1059 }
1060}
1061
1062template <class _Tp, class _Allocator>
1063vector<_Tp, _Allocator>::vector(size_type __n)
1064{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001065#if _LIBCPP_DEBUG_LEVEL >= 2
1066 __get_db()->__insert_c(this);
1067#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001068 if (__n > 0)
1069 {
1070 allocate(__n);
1071 __construct_at_end(__n);
1072 }
1073}
1074
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00001075#if _LIBCPP_STD_VER > 11
1076template <class _Tp, class _Allocator>
1077vector<_Tp, _Allocator>::vector(size_type __n, const allocator_type& __a)
1078 : __base(__a)
1079{
1080#if _LIBCPP_DEBUG_LEVEL >= 2
1081 __get_db()->__insert_c(this);
1082#endif
1083 if (__n > 0)
1084 {
1085 allocate(__n);
1086 __construct_at_end(__n);
1087 }
1088}
1089#endif
1090
Howard Hinnantc51e1022010-05-11 19:42:16 +00001091template <class _Tp, class _Allocator>
1092vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x)
1093{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001094#if _LIBCPP_DEBUG_LEVEL >= 2
1095 __get_db()->__insert_c(this);
1096#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001097 if (__n > 0)
1098 {
1099 allocate(__n);
1100 __construct_at_end(__n, __x);
1101 }
1102}
1103
1104template <class _Tp, class _Allocator>
1105vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x, const allocator_type& __a)
1106 : __base(__a)
1107{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001108#if _LIBCPP_DEBUG_LEVEL >= 2
1109 __get_db()->__insert_c(this);
1110#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001111 if (__n > 0)
1112 {
1113 allocate(__n);
1114 __construct_at_end(__n, __x);
1115 }
1116}
1117
1118template <class _Tp, class _Allocator>
1119template <class _InputIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +00001120vector<_Tp, _Allocator>::vector(_InputIterator __first,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001121 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001122 !__is_forward_iterator<_InputIterator>::value &&
1123 is_constructible<
1124 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +00001125 typename iterator_traits<_InputIterator>::reference>::value,
1126 _InputIterator>::type __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001127{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001128#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001129 __get_db()->__insert_c(this);
1130#endif
Howard Hinnant9cd22302011-09-16 18:41:29 +00001131 for (; __first != __last; ++__first)
1132 push_back(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001133}
1134
1135template <class _Tp, class _Allocator>
1136template <class _InputIterator>
1137vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
1138 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001139 !__is_forward_iterator<_InputIterator>::value &&
1140 is_constructible<
1141 value_type,
1142 typename iterator_traits<_InputIterator>::reference>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001143 : __base(__a)
1144{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001145#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001146 __get_db()->__insert_c(this);
1147#endif
Howard Hinnant9cd22302011-09-16 18:41:29 +00001148 for (; __first != __last; ++__first)
1149 push_back(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001150}
1151
1152template <class _Tp, class _Allocator>
1153template <class _ForwardIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +00001154vector<_Tp, _Allocator>::vector(_ForwardIterator __first,
Howard Hinnant88010252013-03-28 17:44:32 +00001155 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
1156 is_constructible<
1157 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +00001158 typename iterator_traits<_ForwardIterator>::reference>::value,
1159 _ForwardIterator>::type __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001160{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001161#if _LIBCPP_DEBUG_LEVEL >= 2
1162 __get_db()->__insert_c(this);
1163#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001164 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001165 if (__n > 0)
1166 {
1167 allocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001168 __construct_at_end(__first, __last, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001169 }
1170}
1171
1172template <class _Tp, class _Allocator>
1173template <class _ForwardIterator>
1174vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Howard Hinnant88010252013-03-28 17:44:32 +00001175 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
1176 is_constructible<
1177 value_type,
1178 typename iterator_traits<_ForwardIterator>::reference>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001179 : __base(__a)
1180{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001181#if _LIBCPP_DEBUG_LEVEL >= 2
1182 __get_db()->__insert_c(this);
1183#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001184 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001185 if (__n > 0)
1186 {
1187 allocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001188 __construct_at_end(__first, __last, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001189 }
1190}
1191
1192template <class _Tp, class _Allocator>
1193vector<_Tp, _Allocator>::vector(const vector& __x)
1194 : __base(__alloc_traits::select_on_container_copy_construction(__x.__alloc()))
1195{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001196#if _LIBCPP_DEBUG_LEVEL >= 2
1197 __get_db()->__insert_c(this);
1198#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001199 size_type __n = __x.size();
1200 if (__n > 0)
1201 {
1202 allocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001203 __construct_at_end(__x.__begin_, __x.__end_, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001204 }
1205}
1206
1207template <class _Tp, class _Allocator>
1208vector<_Tp, _Allocator>::vector(const vector& __x, const allocator_type& __a)
1209 : __base(__a)
1210{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001211#if _LIBCPP_DEBUG_LEVEL >= 2
1212 __get_db()->__insert_c(this);
1213#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001214 size_type __n = __x.size();
1215 if (__n > 0)
1216 {
1217 allocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001218 __construct_at_end(__x.__begin_, __x.__end_, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001219 }
1220}
1221
Howard Hinnant74279a52010-09-04 23:28:19 +00001222#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001223
1224template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001225inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001226vector<_Tp, _Allocator>::vector(vector&& __x)
Marshall Clowe5108202015-07-14 14:46:32 +00001227#if _LIBCPP_STD_VER > 14
1228 _NOEXCEPT
1229#else
Howard Hinnant1c936782011-06-03 19:40:40 +00001230 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowe5108202015-07-14 14:46:32 +00001231#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001232 : __base(_VSTD::move(__x.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001233{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001234#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001235 __get_db()->__insert_c(this);
Howard Hinnanta1e60cd2011-09-19 16:34:29 +00001236 __get_db()->swap(this, &__x);
Howard Hinnant27e0e772011-09-14 18:33:51 +00001237#endif
Howard Hinnant9cd22302011-09-16 18:41:29 +00001238 this->__begin_ = __x.__begin_;
1239 this->__end_ = __x.__end_;
1240 this->__end_cap() = __x.__end_cap();
Howard Hinnant76053d72013-06-27 19:35:32 +00001241 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001242}
1243
1244template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001245inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001246vector<_Tp, _Allocator>::vector(vector&& __x, const allocator_type& __a)
1247 : __base(__a)
1248{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001249#if _LIBCPP_DEBUG_LEVEL >= 2
1250 __get_db()->__insert_c(this);
1251#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001252 if (__a == __x.__alloc())
1253 {
1254 this->__begin_ = __x.__begin_;
1255 this->__end_ = __x.__end_;
1256 this->__end_cap() = __x.__end_cap();
1257 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnanta1e60cd2011-09-19 16:34:29 +00001258#if _LIBCPP_DEBUG_LEVEL >= 2
1259 __get_db()->swap(this, &__x);
1260#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001261 }
1262 else
1263 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001264 typedef move_iterator<iterator> _Ip;
1265 assign(_Ip(__x.begin()), _Ip(__x.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001266 }
1267}
1268
Howard Hinnant33711792011-08-12 21:56:02 +00001269#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1270
Howard Hinnantc51e1022010-05-11 19:42:16 +00001271template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001272inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001273vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il)
1274{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001275#if _LIBCPP_DEBUG_LEVEL >= 2
1276 __get_db()->__insert_c(this);
1277#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001278 if (__il.size() > 0)
1279 {
1280 allocate(__il.size());
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001281 __construct_at_end(__il.begin(), __il.end(), __il.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001282 }
1283}
1284
1285template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001286inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001287vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
1288 : __base(__a)
1289{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001290#if _LIBCPP_DEBUG_LEVEL >= 2
1291 __get_db()->__insert_c(this);
1292#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001293 if (__il.size() > 0)
1294 {
1295 allocate(__il.size());
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001296 __construct_at_end(__il.begin(), __il.end(), __il.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001297 }
1298}
1299
Howard Hinnant33711792011-08-12 21:56:02 +00001300#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1301
Howard Hinnantc51e1022010-05-11 19:42:16 +00001302template <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
Howard Hinnant74279a52010-09-04 23:28:19 +00001343#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
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
1565#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1566vector<_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
Howard Hinnant74279a52010-09-04 23:28:19 +00001596#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
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
Howard Hinnant74279a52010-09-04 23:28:19 +00001616#ifndef _LIBCPP_HAS_NO_VARIADICS
1617
Howard Hinnantc51e1022010-05-11 19:42:16 +00001618template <class _Tp, class _Allocator>
1619template <class... _Args>
1620void
Howard Hinnantb6c49562012-02-26 15:30:12 +00001621vector<_Tp, _Allocator>::__emplace_back_slow_path(_Args&&... __args)
1622{
1623 allocator_type& __a = this->__alloc();
1624 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1625// __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001626 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), _VSTD::forward<_Args>(__args)...);
1627 __v.__end_++;
Howard Hinnantb6c49562012-02-26 15:30:12 +00001628 __swap_out_circular_buffer(__v);
1629}
1630
1631template <class _Tp, class _Allocator>
1632template <class... _Args>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001633inline
Marshall Clowea52cc42017-01-24 23:09:12 +00001634#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001635typename vector<_Tp, _Allocator>::reference
Marshall Clowea52cc42017-01-24 23:09:12 +00001636#else
1637void
1638#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001639vector<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1640{
1641 if (this->__end_ < this->__end_cap())
1642 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001643 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001644 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001645 _VSTD::__to_raw_pointer(this->__end_),
1646 _VSTD::forward<_Args>(__args)...);
Kostya Serebryany4963c252014-09-02 23:43:38 +00001647 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001648 ++this->__end_;
1649 }
1650 else
Howard Hinnantb6c49562012-02-26 15:30:12 +00001651 __emplace_back_slow_path(_VSTD::forward<_Args>(__args)...);
Marshall Clowea52cc42017-01-24 23:09:12 +00001652#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001653 return this->back();
Marshall Clowea52cc42017-01-24 23:09:12 +00001654#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001655}
1656
Howard Hinnant74279a52010-09-04 23:28:19 +00001657#endif // _LIBCPP_HAS_NO_VARIADICS
1658#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001659
1660template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001661inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001662void
1663vector<_Tp, _Allocator>::pop_back()
1664{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001665 _LIBCPP_ASSERT(!empty(), "vector::pop_back called for empty vector");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001666 this->__destruct_at_end(this->__end_ - 1);
1667}
1668
1669template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001670inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001671typename vector<_Tp, _Allocator>::iterator
1672vector<_Tp, _Allocator>::erase(const_iterator __position)
1673{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001674#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001675 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1676 "vector::erase(iterator) called with an iterator not"
1677 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001678#endif
Howard Hinnant58d52d52013-03-25 22:12:26 +00001679 _LIBCPP_ASSERT(__position != end(),
1680 "vector::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnant76053d72013-06-27 19:35:32 +00001681 difference_type __ps = __position - cbegin();
1682 pointer __p = this->__begin_ + __ps;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001683 this->__destruct_at_end(_VSTD::move(__p + 1, this->__end_, __p));
Eric Fiselier69c51982016-12-28 06:06:09 +00001684 this->__invalidate_iterators_past(__p-1);
1685 iterator __r = __make_iter(__p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001686 return __r;
1687}
1688
1689template <class _Tp, class _Allocator>
1690typename vector<_Tp, _Allocator>::iterator
1691vector<_Tp, _Allocator>::erase(const_iterator __first, const_iterator __last)
1692{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001693#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001694 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
1695 "vector::erase(iterator, iterator) called with an iterator not"
1696 " referring to this vector");
Eric Fiselier69c51982016-12-28 06:06:09 +00001697 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__last) == this,
1698 "vector::erase(iterator, iterator) called with an iterator not"
1699 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001700#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001701 _LIBCPP_ASSERT(__first <= __last, "vector::erase(first, last) called with invalid range");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001702 pointer __p = this->__begin_ + (__first - begin());
Eric Fiselier69c51982016-12-28 06:06:09 +00001703 if (__first != __last) {
Howard Hinnantc580cc32013-04-18 15:02:57 +00001704 this->__destruct_at_end(_VSTD::move(__p + (__last - __first), this->__end_, __p));
Eric Fiselier69c51982016-12-28 06:06:09 +00001705 this->__invalidate_iterators_past(__p - 1);
1706 }
1707 iterator __r = __make_iter(__p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001708 return __r;
1709}
1710
1711template <class _Tp, class _Allocator>
1712void
1713vector<_Tp, _Allocator>::__move_range(pointer __from_s, pointer __from_e, pointer __to)
1714{
1715 pointer __old_last = this->__end_;
1716 difference_type __n = __old_last - __to;
1717 for (pointer __i = __from_s + __n; __i < __from_e; ++__i, ++this->__end_)
1718 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001719 _VSTD::__to_raw_pointer(this->__end_),
1720 _VSTD::move(*__i));
1721 _VSTD::move_backward(__from_s, __from_s + __n, __old_last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001722}
1723
1724template <class _Tp, class _Allocator>
1725typename vector<_Tp, _Allocator>::iterator
1726vector<_Tp, _Allocator>::insert(const_iterator __position, const_reference __x)
1727{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001728#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001729 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1730 "vector::insert(iterator, x) called with an iterator not"
1731 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001732#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001733 pointer __p = this->__begin_ + (__position - begin());
1734 if (this->__end_ < this->__end_cap())
1735 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001736 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001737 if (__p == this->__end_)
1738 {
1739 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001740 _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001741 ++this->__end_;
1742 }
1743 else
1744 {
1745 __move_range(__p, this->__end_, __p + 1);
1746 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1747 if (__p <= __xr && __xr < this->__end_)
1748 ++__xr;
1749 *__p = *__xr;
1750 }
Kostya Serebryany4963c252014-09-02 23:43:38 +00001751 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001752 }
1753 else
1754 {
1755 allocator_type& __a = this->__alloc();
1756 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
1757 __v.push_back(__x);
1758 __p = __swap_out_circular_buffer(__v, __p);
1759 }
1760 return __make_iter(__p);
1761}
1762
Howard Hinnant74279a52010-09-04 23:28:19 +00001763#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001764
1765template <class _Tp, class _Allocator>
1766typename vector<_Tp, _Allocator>::iterator
1767vector<_Tp, _Allocator>::insert(const_iterator __position, value_type&& __x)
1768{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001769#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001770 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1771 "vector::insert(iterator, x) called with an iterator not"
1772 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001773#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001774 pointer __p = this->__begin_ + (__position - begin());
1775 if (this->__end_ < this->__end_cap())
1776 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001777 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001778 if (__p == this->__end_)
1779 {
1780 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001781 _VSTD::__to_raw_pointer(this->__end_),
1782 _VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001783 ++this->__end_;
1784 }
1785 else
1786 {
1787 __move_range(__p, this->__end_, __p + 1);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001788 *__p = _VSTD::move(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001789 }
Kostya Serebryany4963c252014-09-02 23:43:38 +00001790 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001791 }
1792 else
1793 {
1794 allocator_type& __a = this->__alloc();
1795 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001796 __v.push_back(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001797 __p = __swap_out_circular_buffer(__v, __p);
1798 }
1799 return __make_iter(__p);
1800}
1801
Howard Hinnant74279a52010-09-04 23:28:19 +00001802#ifndef _LIBCPP_HAS_NO_VARIADICS
1803
Howard Hinnantc51e1022010-05-11 19:42:16 +00001804template <class _Tp, class _Allocator>
1805template <class... _Args>
1806typename vector<_Tp, _Allocator>::iterator
1807vector<_Tp, _Allocator>::emplace(const_iterator __position, _Args&&... __args)
1808{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001809#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001810 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1811 "vector::emplace(iterator, x) called with an iterator not"
1812 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001813#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001814 pointer __p = this->__begin_ + (__position - begin());
1815 if (this->__end_ < this->__end_cap())
1816 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001817 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001818 if (__p == this->__end_)
1819 {
1820 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001821 _VSTD::__to_raw_pointer(this->__end_),
1822 _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001823 ++this->__end_;
1824 }
1825 else
1826 {
Marshall Clowa591b9a2016-07-11 21:38:08 +00001827 __temp_value<value_type, _Allocator> __tmp(this->__alloc(), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001828 __move_range(__p, this->__end_, __p + 1);
Marshall Clowa591b9a2016-07-11 21:38:08 +00001829 *__p = _VSTD::move(__tmp.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001830 }
Kostya Serebryany4963c252014-09-02 23:43:38 +00001831 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001832 }
1833 else
1834 {
1835 allocator_type& __a = this->__alloc();
1836 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001837 __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001838 __p = __swap_out_circular_buffer(__v, __p);
1839 }
1840 return __make_iter(__p);
1841}
1842
Howard Hinnant74279a52010-09-04 23:28:19 +00001843#endif // _LIBCPP_HAS_NO_VARIADICS
1844#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001845
1846template <class _Tp, class _Allocator>
1847typename vector<_Tp, _Allocator>::iterator
1848vector<_Tp, _Allocator>::insert(const_iterator __position, size_type __n, const_reference __x)
1849{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001850#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001851 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1852 "vector::insert(iterator, n, x) called with an iterator not"
1853 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001854#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001855 pointer __p = this->__begin_ + (__position - begin());
1856 if (__n > 0)
1857 {
1858 if (__n <= static_cast<size_type>(this->__end_cap() - this->__end_))
1859 {
1860 size_type __old_n = __n;
1861 pointer __old_last = this->__end_;
1862 if (__n > static_cast<size_type>(this->__end_ - __p))
1863 {
1864 size_type __cx = __n - (this->__end_ - __p);
1865 __construct_at_end(__cx, __x);
1866 __n -= __cx;
1867 }
1868 if (__n > 0)
1869 {
Eric Fiselier2a649652014-11-14 18:28:36 +00001870 __RAII_IncreaseAnnotator __annotator(*this, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001871 __move_range(__p, __old_last, __p + __old_n);
Kostya Serebryany4963c252014-09-02 23:43:38 +00001872 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001873 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1874 if (__p <= __xr && __xr < this->__end_)
1875 __xr += __old_n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001876 _VSTD::fill_n(__p, __n, *__xr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001877 }
1878 }
1879 else
1880 {
1881 allocator_type& __a = this->__alloc();
1882 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1883 __v.__construct_at_end(__n, __x);
1884 __p = __swap_out_circular_buffer(__v, __p);
1885 }
1886 }
1887 return __make_iter(__p);
1888}
1889
1890template <class _Tp, class _Allocator>
1891template <class _InputIterator>
1892typename enable_if
1893<
1894 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001895 !__is_forward_iterator<_InputIterator>::value &&
1896 is_constructible<
1897 _Tp,
1898 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001899 typename vector<_Tp, _Allocator>::iterator
1900>::type
1901vector<_Tp, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
1902{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001903#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001904 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1905 "vector::insert(iterator, range) called with an iterator not"
1906 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001907#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001908 difference_type __off = __position - begin();
1909 pointer __p = this->__begin_ + __off;
1910 allocator_type& __a = this->__alloc();
1911 pointer __old_last = this->__end_;
1912 for (; this->__end_ != this->__end_cap() && __first != __last; ++__first)
1913 {
Eric Fiselier489fd502015-07-18 18:22:12 +00001914 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001915 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001916 *__first);
1917 ++this->__end_;
Eric Fiselier489fd502015-07-18 18:22:12 +00001918 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001919 }
1920 __split_buffer<value_type, allocator_type&> __v(__a);
1921 if (__first != __last)
1922 {
1923#ifndef _LIBCPP_NO_EXCEPTIONS
1924 try
1925 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001926#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001927 __v.__construct_at_end(__first, __last);
1928 difference_type __old_size = __old_last - this->__begin_;
1929 difference_type __old_p = __p - this->__begin_;
1930 reserve(__recommend(size() + __v.size()));
1931 __p = this->__begin_ + __old_p;
1932 __old_last = this->__begin_ + __old_size;
1933#ifndef _LIBCPP_NO_EXCEPTIONS
1934 }
1935 catch (...)
1936 {
1937 erase(__make_iter(__old_last), end());
1938 throw;
1939 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001940#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001941 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001942 __p = _VSTD::rotate(__p, __old_last, this->__end_);
Howard Hinnant9cd22302011-09-16 18:41:29 +00001943 insert(__make_iter(__p), make_move_iterator(__v.begin()),
1944 make_move_iterator(__v.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001945 return begin() + __off;
1946}
1947
1948template <class _Tp, class _Allocator>
1949template <class _ForwardIterator>
1950typename enable_if
1951<
Howard Hinnant88010252013-03-28 17:44:32 +00001952 __is_forward_iterator<_ForwardIterator>::value &&
1953 is_constructible<
1954 _Tp,
1955 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001956 typename vector<_Tp, _Allocator>::iterator
1957>::type
1958vector<_Tp, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
1959{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001960#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001961 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1962 "vector::insert(iterator, range) called with an iterator not"
1963 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001964#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001965 pointer __p = this->__begin_ + (__position - begin());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001966 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001967 if (__n > 0)
1968 {
1969 if (__n <= this->__end_cap() - this->__end_)
1970 {
1971 size_type __old_n = __n;
1972 pointer __old_last = this->__end_;
1973 _ForwardIterator __m = __last;
1974 difference_type __dx = this->__end_ - __p;
1975 if (__n > __dx)
1976 {
1977 __m = __first;
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001978 difference_type __diff = this->__end_ - __p;
1979 _VSTD::advance(__m, __diff);
1980 __construct_at_end(__m, __last, __n - __diff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001981 __n = __dx;
1982 }
1983 if (__n > 0)
1984 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001985 __RAII_IncreaseAnnotator __annotator(*this, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001986 __move_range(__p, __old_last, __p + __old_n);
Kostya Serebryany4963c252014-09-02 23:43:38 +00001987 __annotator.__done();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001988 _VSTD::copy(__first, __m, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001989 }
1990 }
1991 else
1992 {
1993 allocator_type& __a = this->__alloc();
1994 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1995 __v.__construct_at_end(__first, __last);
1996 __p = __swap_out_circular_buffer(__v, __p);
1997 }
1998 }
1999 return __make_iter(__p);
2000}
2001
2002template <class _Tp, class _Allocator>
2003void
2004vector<_Tp, _Allocator>::resize(size_type __sz)
2005{
2006 size_type __cs = size();
2007 if (__cs < __sz)
2008 this->__append(__sz - __cs);
2009 else if (__cs > __sz)
2010 this->__destruct_at_end(this->__begin_ + __sz);
2011}
2012
2013template <class _Tp, class _Allocator>
2014void
2015vector<_Tp, _Allocator>::resize(size_type __sz, const_reference __x)
2016{
2017 size_type __cs = size();
2018 if (__cs < __sz)
2019 this->__append(__sz - __cs, __x);
2020 else if (__cs > __sz)
2021 this->__destruct_at_end(this->__begin_ + __sz);
2022}
2023
2024template <class _Tp, class _Allocator>
2025void
2026vector<_Tp, _Allocator>::swap(vector& __x)
Marshall Clow8982dcd2015-07-13 20:04:56 +00002027#if _LIBCPP_STD_VER >= 14
Eric Fiselier69c51982016-12-28 06:06:09 +00002028 _NOEXCEPT_DEBUG
Marshall Clow8982dcd2015-07-13 20:04:56 +00002029#else
Eric Fiselier69c51982016-12-28 06:06:09 +00002030 _NOEXCEPT_DEBUG_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00002031 __is_nothrow_swappable<allocator_type>::value)
2032#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002033{
Howard Hinnant27e0e772011-09-14 18:33:51 +00002034 _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
2035 this->__alloc() == __x.__alloc(),
2036 "vector::swap: Either propagate_on_container_swap must be true"
2037 " or the allocators must compare equal");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002038 _VSTD::swap(this->__begin_, __x.__begin_);
2039 _VSTD::swap(this->__end_, __x.__end_);
2040 _VSTD::swap(this->__end_cap(), __x.__end_cap());
Marshall Clow8982dcd2015-07-13 20:04:56 +00002041 __swap_allocator(this->__alloc(), __x.__alloc(),
2042 integral_constant<bool,__alloc_traits::propagate_on_container_swap::value>());
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002043#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002044 __get_db()->swap(this, &__x);
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002045#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00002046}
2047
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002048template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002049bool
2050vector<_Tp, _Allocator>::__invariants() const
2051{
Howard Hinnant76053d72013-06-27 19:35:32 +00002052 if (this->__begin_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002053 {
Howard Hinnant76053d72013-06-27 19:35:32 +00002054 if (this->__end_ != nullptr || this->__end_cap() != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002055 return false;
2056 }
2057 else
2058 {
2059 if (this->__begin_ > this->__end_)
2060 return false;
2061 if (this->__begin_ == this->__end_cap())
2062 return false;
2063 if (this->__end_ > this->__end_cap())
2064 return false;
2065 }
2066 return true;
2067}
2068
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002069#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002070
Howard Hinnantc51e1022010-05-11 19:42:16 +00002071template <class _Tp, class _Allocator>
Howard Hinnant27e0e772011-09-14 18:33:51 +00002072bool
2073vector<_Tp, _Allocator>::__dereferenceable(const const_iterator* __i) const
2074{
2075 return this->__begin_ <= __i->base() && __i->base() < this->__end_;
2076}
2077
2078template <class _Tp, class _Allocator>
2079bool
2080vector<_Tp, _Allocator>::__decrementable(const const_iterator* __i) const
2081{
2082 return this->__begin_ < __i->base() && __i->base() <= this->__end_;
2083}
2084
2085template <class _Tp, class _Allocator>
2086bool
2087vector<_Tp, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
2088{
2089 const_pointer __p = __i->base() + __n;
2090 return this->__begin_ <= __p && __p <= this->__end_;
2091}
2092
2093template <class _Tp, class _Allocator>
2094bool
2095vector<_Tp, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
2096{
2097 const_pointer __p = __i->base() + __n;
2098 return this->__begin_ <= __p && __p < this->__end_;
2099}
2100
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002101#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002102
2103template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002104inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002105void
2106vector<_Tp, _Allocator>::__invalidate_all_iterators()
2107{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002108#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002109 __get_db()->__invalidate_all(this);
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002110#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00002111}
2112
Eric Fiselier69c51982016-12-28 06:06:09 +00002113
2114template <class _Tp, class _Allocator>
2115inline _LIBCPP_INLINE_VISIBILITY
2116void
2117vector<_Tp, _Allocator>::__invalidate_iterators_past(pointer __new_last) {
2118#if _LIBCPP_DEBUG_LEVEL >= 2
2119 __c_node* __c = __get_db()->__find_c_and_lock(this);
2120 for (__i_node** __p = __c->end_; __p != __c->beg_; ) {
2121 --__p;
2122 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
2123 if (__i->base() > __new_last) {
2124 (*__p)->__c_ = nullptr;
2125 if (--__c->end_ != __p)
2126 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
2127 }
2128 }
2129 __get_db()->unlock();
2130#else
2131 ((void)__new_last);
2132#endif
2133}
2134
Howard Hinnantc51e1022010-05-11 19:42:16 +00002135// vector<bool>
2136
2137template <class _Allocator> class vector<bool, _Allocator>;
2138
2139template <class _Allocator> struct hash<vector<bool, _Allocator> >;
2140
2141template <class _Allocator>
Howard Hinnantb4ef6482011-07-02 20:33:23 +00002142struct __has_storage_type<vector<bool, _Allocator> >
2143{
2144 static const bool value = true;
2145};
2146
2147template <class _Allocator>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002148class _LIBCPP_TEMPLATE_VIS vector<bool, _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002149 : private __vector_base_common<true>
2150{
2151public:
2152 typedef vector __self;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002153 typedef bool value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002154 typedef _Allocator allocator_type;
2155 typedef allocator_traits<allocator_type> __alloc_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002156 typedef typename __alloc_traits::size_type size_type;
2157 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant896b7622012-05-07 16:50:38 +00002158 typedef size_type __storage_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002159 typedef __bit_iterator<vector, false> pointer;
2160 typedef __bit_iterator<vector, true> const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002161 typedef pointer iterator;
2162 typedef const_pointer const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002163 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
2164 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002165
2166private:
Marshall Clow940e01c2015-04-07 05:21:38 +00002167 typedef typename __rebind_alloc_helper<__alloc_traits, __storage_type>::type __storage_allocator;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002168 typedef allocator_traits<__storage_allocator> __storage_traits;
2169 typedef typename __storage_traits::pointer __storage_pointer;
2170 typedef typename __storage_traits::const_pointer __const_storage_pointer;
2171
2172 __storage_pointer __begin_;
2173 size_type __size_;
2174 __compressed_pair<size_type, __storage_allocator> __cap_alloc_;
Howard Hinnant71d1c192011-07-09 15:50:42 +00002175public:
Howard Hinnantb4ef6482011-07-02 20:33:23 +00002176 typedef __bit_reference<vector> reference;
2177 typedef __bit_const_reference<vector> const_reference;
Howard Hinnant71d1c192011-07-09 15:50:42 +00002178private:
Howard Hinnant1c936782011-06-03 19:40:40 +00002179 _LIBCPP_INLINE_VISIBILITY
2180 size_type& __cap() _NOEXCEPT
2181 {return __cap_alloc_.first();}
2182 _LIBCPP_INLINE_VISIBILITY
2183 const size_type& __cap() const _NOEXCEPT
2184 {return __cap_alloc_.first();}
2185 _LIBCPP_INLINE_VISIBILITY
2186 __storage_allocator& __alloc() _NOEXCEPT
2187 {return __cap_alloc_.second();}
2188 _LIBCPP_INLINE_VISIBILITY
2189 const __storage_allocator& __alloc() const _NOEXCEPT
2190 {return __cap_alloc_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002191
2192 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
2193
Howard Hinnant1c936782011-06-03 19:40:40 +00002194 _LIBCPP_INLINE_VISIBILITY
2195 static size_type __internal_cap_to_external(size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002196 {return __n * __bits_per_word;}
Howard Hinnant1c936782011-06-03 19:40:40 +00002197 _LIBCPP_INLINE_VISIBILITY
2198 static size_type __external_cap_to_internal(size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002199 {return (__n - 1) / __bits_per_word + 1;}
2200
2201public:
Howard Hinnant1c936782011-06-03 19:40:40 +00002202 _LIBCPP_INLINE_VISIBILITY
Marshall Clowe546cbb2015-06-04 02:05:41 +00002203 vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clow8f282f42015-06-04 00:10:20 +00002204
2205 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
2206#if _LIBCPP_STD_VER <= 14
2207 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
2208#else
2209 _NOEXCEPT;
2210#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002211 ~vector();
2212 explicit vector(size_type __n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00002213#if _LIBCPP_STD_VER > 11
2214 explicit vector(size_type __n, const allocator_type& __a);
2215#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002216 vector(size_type __n, const value_type& __v);
2217 vector(size_type __n, const value_type& __v, const allocator_type& __a);
2218 template <class _InputIterator>
2219 vector(_InputIterator __first, _InputIterator __last,
2220 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2221 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2222 template <class _InputIterator>
2223 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2224 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2225 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2226 template <class _ForwardIterator>
2227 vector(_ForwardIterator __first, _ForwardIterator __last,
2228 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2229 template <class _ForwardIterator>
2230 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2231 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2232
2233 vector(const vector& __v);
2234 vector(const vector& __v, const allocator_type& __a);
2235 vector& operator=(const vector& __v);
Howard Hinnant33711792011-08-12 21:56:02 +00002236#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002237 vector(initializer_list<value_type> __il);
2238 vector(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnant33711792011-08-12 21:56:02 +00002239#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002240
Howard Hinnant74279a52010-09-04 23:28:19 +00002241#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant1c936782011-06-03 19:40:40 +00002242 _LIBCPP_INLINE_VISIBILITY
2243 vector(vector&& __v)
Marshall Clowe5108202015-07-14 14:46:32 +00002244#if _LIBCPP_STD_VER > 14
2245 _NOEXCEPT;
2246#else
Howard Hinnant1c936782011-06-03 19:40:40 +00002247 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowe5108202015-07-14 14:46:32 +00002248#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002249 vector(vector&& __v, const allocator_type& __a);
Howard Hinnant1c936782011-06-03 19:40:40 +00002250 _LIBCPP_INLINE_VISIBILITY
2251 vector& operator=(vector&& __v)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002252 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Howard Hinnant74279a52010-09-04 23:28:19 +00002253#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant33711792011-08-12 21:56:02 +00002254#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002255 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002256 vector& operator=(initializer_list<value_type> __il)
2257 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnant33711792011-08-12 21:56:02 +00002258#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002259
2260 template <class _InputIterator>
2261 typename enable_if
2262 <
2263 __is_input_iterator<_InputIterator>::value &&
2264 !__is_forward_iterator<_InputIterator>::value,
2265 void
2266 >::type
2267 assign(_InputIterator __first, _InputIterator __last);
2268 template <class _ForwardIterator>
2269 typename enable_if
2270 <
2271 __is_forward_iterator<_ForwardIterator>::value,
2272 void
2273 >::type
2274 assign(_ForwardIterator __first, _ForwardIterator __last);
2275
2276 void assign(size_type __n, const value_type& __x);
Howard Hinnant33711792011-08-12 21:56:02 +00002277#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002278 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002279 void assign(initializer_list<value_type> __il)
2280 {assign(__il.begin(), __il.end());}
Howard Hinnant33711792011-08-12 21:56:02 +00002281#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002282
Howard Hinnant1c936782011-06-03 19:40:40 +00002283 _LIBCPP_INLINE_VISIBILITY allocator_type get_allocator() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002284 {return allocator_type(this->__alloc());}
2285
Howard Hinnant1c936782011-06-03 19:40:40 +00002286 size_type max_size() const _NOEXCEPT;
2287 _LIBCPP_INLINE_VISIBILITY
2288 size_type capacity() const _NOEXCEPT
2289 {return __internal_cap_to_external(__cap());}
2290 _LIBCPP_INLINE_VISIBILITY
2291 size_type size() const _NOEXCEPT
2292 {return __size_;}
2293 _LIBCPP_INLINE_VISIBILITY
2294 bool empty() const _NOEXCEPT
2295 {return __size_ == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002296 void reserve(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +00002297 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002298
Howard Hinnant1c936782011-06-03 19:40:40 +00002299 _LIBCPP_INLINE_VISIBILITY
2300 iterator begin() _NOEXCEPT
2301 {return __make_iter(0);}
2302 _LIBCPP_INLINE_VISIBILITY
2303 const_iterator begin() const _NOEXCEPT
2304 {return __make_iter(0);}
2305 _LIBCPP_INLINE_VISIBILITY
2306 iterator end() _NOEXCEPT
2307 {return __make_iter(__size_);}
2308 _LIBCPP_INLINE_VISIBILITY
2309 const_iterator end() const _NOEXCEPT
2310 {return __make_iter(__size_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002311
Howard Hinnant1c936782011-06-03 19:40:40 +00002312 _LIBCPP_INLINE_VISIBILITY
2313 reverse_iterator rbegin() _NOEXCEPT
2314 {return reverse_iterator(end());}
2315 _LIBCPP_INLINE_VISIBILITY
2316 const_reverse_iterator rbegin() const _NOEXCEPT
2317 {return const_reverse_iterator(end());}
2318 _LIBCPP_INLINE_VISIBILITY
2319 reverse_iterator rend() _NOEXCEPT
2320 {return reverse_iterator(begin());}
2321 _LIBCPP_INLINE_VISIBILITY
2322 const_reverse_iterator rend() const _NOEXCEPT
2323 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002324
Howard Hinnant1c936782011-06-03 19:40:40 +00002325 _LIBCPP_INLINE_VISIBILITY
2326 const_iterator cbegin() const _NOEXCEPT
2327 {return __make_iter(0);}
2328 _LIBCPP_INLINE_VISIBILITY
2329 const_iterator cend() const _NOEXCEPT
2330 {return __make_iter(__size_);}
2331 _LIBCPP_INLINE_VISIBILITY
2332 const_reverse_iterator crbegin() const _NOEXCEPT
2333 {return rbegin();}
2334 _LIBCPP_INLINE_VISIBILITY
2335 const_reverse_iterator crend() const _NOEXCEPT
2336 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002337
2338 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) {return __make_ref(__n);}
2339 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const {return __make_ref(__n);}
2340 reference at(size_type __n);
2341 const_reference at(size_type __n) const;
2342
2343 _LIBCPP_INLINE_VISIBILITY reference front() {return __make_ref(0);}
2344 _LIBCPP_INLINE_VISIBILITY const_reference front() const {return __make_ref(0);}
2345 _LIBCPP_INLINE_VISIBILITY reference back() {return __make_ref(__size_ - 1);}
2346 _LIBCPP_INLINE_VISIBILITY const_reference back() const {return __make_ref(__size_ - 1);}
2347
2348 void push_back(const value_type& __x);
Marshall Clowc46bb8e2013-08-13 23:54:12 +00002349#if _LIBCPP_STD_VER > 11
2350 template <class... _Args>
Marshall Clowea52cc42017-01-24 23:09:12 +00002351#if _LIBCPP_STD_VER > 14
2352 _LIBCPP_INLINE_VISIBILITY reference emplace_back(_Args&&... __args)
2353#else
2354 _LIBCPP_INLINE_VISIBILITY void emplace_back(_Args&&... __args)
2355#endif
2356 {
Eric Fiselier34ba5b92016-07-21 03:20:17 +00002357 push_back ( value_type ( _VSTD::forward<_Args>(__args)... ));
Marshall Clowea52cc42017-01-24 23:09:12 +00002358#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00002359 return this->back();
Marshall Clowea52cc42017-01-24 23:09:12 +00002360#endif
Eric Fiselier34ba5b92016-07-21 03:20:17 +00002361 }
Marshall Clowc46bb8e2013-08-13 23:54:12 +00002362#endif
2363
Howard Hinnantc51e1022010-05-11 19:42:16 +00002364 _LIBCPP_INLINE_VISIBILITY void pop_back() {--__size_;}
2365
Marshall Clowc46bb8e2013-08-13 23:54:12 +00002366#if _LIBCPP_STD_VER > 11
2367 template <class... _Args>
2368 _LIBCPP_INLINE_VISIBILITY iterator emplace(const_iterator position, _Args&&... __args)
2369 { return insert ( position, value_type ( _VSTD::forward<_Args>(__args)... )); }
2370#endif
2371
Howard Hinnantc51e1022010-05-11 19:42:16 +00002372 iterator insert(const_iterator __position, const value_type& __x);
2373 iterator insert(const_iterator __position, size_type __n, const value_type& __x);
2374 iterator insert(const_iterator __position, size_type __n, const_reference __x);
2375 template <class _InputIterator>
2376 typename enable_if
2377 <
2378 __is_input_iterator <_InputIterator>::value &&
2379 !__is_forward_iterator<_InputIterator>::value,
2380 iterator
2381 >::type
2382 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
2383 template <class _ForwardIterator>
2384 typename enable_if
2385 <
2386 __is_forward_iterator<_ForwardIterator>::value,
2387 iterator
2388 >::type
2389 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnant33711792011-08-12 21:56:02 +00002390#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002391 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002392 iterator insert(const_iterator __position, initializer_list<value_type> __il)
2393 {return insert(__position, __il.begin(), __il.end());}
Howard Hinnant33711792011-08-12 21:56:02 +00002394#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002395
Howard Hinnantcf823322010-12-17 14:46:43 +00002396 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002397 iterator erase(const_iterator __first, const_iterator __last);
2398
Howard Hinnant1c936782011-06-03 19:40:40 +00002399 _LIBCPP_INLINE_VISIBILITY
2400 void clear() _NOEXCEPT {__size_ = 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002401
Howard Hinnant1c936782011-06-03 19:40:40 +00002402 void swap(vector&)
Marshall Clow8982dcd2015-07-13 20:04:56 +00002403#if _LIBCPP_STD_VER >= 14
2404 _NOEXCEPT;
2405#else
Eric Fiselier69c51982016-12-28 06:06:09 +00002406 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00002407 __is_nothrow_swappable<allocator_type>::value);
2408#endif
Marshall Clow89eb3822016-04-07 14:20:31 +00002409 static void swap(reference __x, reference __y) _NOEXCEPT { _VSTD::swap(__x, __y); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002410
2411 void resize(size_type __sz, value_type __x = false);
Howard Hinnant1c936782011-06-03 19:40:40 +00002412 void flip() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002413
2414 bool __invariants() const;
2415
2416private:
Howard Hinnantcf823322010-12-17 14:46:43 +00002417 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002418 void allocate(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +00002419 void deallocate() _NOEXCEPT;
2420 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantea382952013-08-14 18:00:20 +00002421 static size_type __align_it(size_type __new_size) _NOEXCEPT
Marshall Clow66f34152014-07-28 15:02:42 +00002422 {return __new_size + (__bits_per_word-1) & ~((size_type)__bits_per_word-1);};
Howard Hinnantcf823322010-12-17 14:46:43 +00002423 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
2424 _LIBCPP_INLINE_VISIBILITY void __construct_at_end(size_type __n, bool __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002425 template <class _ForwardIterator>
2426 typename enable_if
2427 <
2428 __is_forward_iterator<_ForwardIterator>::value,
2429 void
2430 >::type
2431 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
2432 void __append(size_type __n, const_reference __x);
Howard Hinnant1c936782011-06-03 19:40:40 +00002433 _LIBCPP_INLINE_VISIBILITY
2434 reference __make_ref(size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002435 {return reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnant1c936782011-06-03 19:40:40 +00002436 _LIBCPP_INLINE_VISIBILITY
2437 const_reference __make_ref(size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002438 {return const_reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnant1c936782011-06-03 19:40:40 +00002439 _LIBCPP_INLINE_VISIBILITY
2440 iterator __make_iter(size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002441 {return iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnant1c936782011-06-03 19:40:40 +00002442 _LIBCPP_INLINE_VISIBILITY
2443 const_iterator __make_iter(size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002444 {return const_iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnant1c936782011-06-03 19:40:40 +00002445 _LIBCPP_INLINE_VISIBILITY
2446 iterator __const_iterator_cast(const_iterator __p) _NOEXCEPT
Howard Hinnant76053d72013-06-27 19:35:32 +00002447 {return begin() + (__p - cbegin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002448
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002449 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002450 void __copy_assign_alloc(const vector& __v)
2451 {__copy_assign_alloc(__v, integral_constant<bool,
2452 __storage_traits::propagate_on_container_copy_assignment::value>());}
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002453 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002454 void __copy_assign_alloc(const vector& __c, true_type)
2455 {
2456 if (__alloc() != __c.__alloc())
2457 deallocate();
2458 __alloc() = __c.__alloc();
2459 }
2460
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002461 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00002462 void __copy_assign_alloc(const vector&, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002463 {}
2464
2465 void __move_assign(vector& __c, false_type);
Howard Hinnant1c936782011-06-03 19:40:40 +00002466 void __move_assign(vector& __c, true_type)
2467 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002468 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002469 void __move_assign_alloc(vector& __c)
Howard Hinnant1c936782011-06-03 19:40:40 +00002470 _NOEXCEPT_(
2471 !__storage_traits::propagate_on_container_move_assignment::value ||
2472 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002473 {__move_assign_alloc(__c, integral_constant<bool,
2474 __storage_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002475 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +00002476 void __move_assign_alloc(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002477 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002478 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002479 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002480 }
2481
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002482 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00002483 void __move_assign_alloc(vector&, false_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002484 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002485 {}
2486
Howard Hinnant1c936782011-06-03 19:40:40 +00002487 size_t __hash_code() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002488
2489 friend class __bit_reference<vector>;
2490 friend class __bit_const_reference<vector>;
2491 friend class __bit_iterator<vector, false>;
2492 friend class __bit_iterator<vector, true>;
Howard Hinnant4210a0f2012-08-17 17:10:18 +00002493 friend struct __bit_array<vector>;
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002494 friend struct _LIBCPP_TEMPLATE_VIS hash<vector>;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002495};
2496
2497template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002498inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002499void
2500vector<bool, _Allocator>::__invalidate_all_iterators()
2501{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002502}
2503
2504// Allocate space for __n objects
2505// throws length_error if __n > max_size()
2506// throws (probably bad_alloc) if memory run out
2507// Precondition: __begin_ == __end_ == __cap() == 0
2508// Precondition: __n > 0
2509// Postcondition: capacity() == __n
2510// Postcondition: size() == 0
2511template <class _Allocator>
2512void
2513vector<bool, _Allocator>::allocate(size_type __n)
2514{
2515 if (__n > max_size())
2516 this->__throw_length_error();
2517 __n = __external_cap_to_internal(__n);
2518 this->__begin_ = __storage_traits::allocate(this->__alloc(), __n);
2519 this->__size_ = 0;
2520 this->__cap() = __n;
2521}
2522
2523template <class _Allocator>
2524void
Howard Hinnant1c936782011-06-03 19:40:40 +00002525vector<bool, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002526{
Howard Hinnant76053d72013-06-27 19:35:32 +00002527 if (this->__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002528 {
2529 __storage_traits::deallocate(this->__alloc(), this->__begin_, __cap());
2530 __invalidate_all_iterators();
Howard Hinnant76053d72013-06-27 19:35:32 +00002531 this->__begin_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002532 this->__size_ = this->__cap() = 0;
2533 }
2534}
2535
2536template <class _Allocator>
2537typename vector<bool, _Allocator>::size_type
Howard Hinnant1c936782011-06-03 19:40:40 +00002538vector<bool, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002539{
2540 size_type __amax = __storage_traits::max_size(__alloc());
2541 size_type __nmax = numeric_limits<size_type>::max() / 2; // end() >= begin(), always
2542 if (__nmax / __bits_per_word <= __amax)
2543 return __nmax;
2544 return __internal_cap_to_external(__amax);
2545}
2546
2547// Precondition: __new_size > capacity()
2548template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002549inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002550typename vector<bool, _Allocator>::size_type
2551vector<bool, _Allocator>::__recommend(size_type __new_size) const
2552{
2553 const size_type __ms = max_size();
2554 if (__new_size > __ms)
2555 this->__throw_length_error();
2556 const size_type __cap = capacity();
2557 if (__cap >= __ms / 2)
2558 return __ms;
Howard Hinnantea382952013-08-14 18:00:20 +00002559 return _VSTD::max(2*__cap, __align_it(__new_size));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002560}
2561
2562// Default constructs __n objects starting at __end_
2563// Precondition: __n > 0
2564// Precondition: size() + __n <= capacity()
2565// Postcondition: size() == size() + __n
2566template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002567inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002568void
2569vector<bool, _Allocator>::__construct_at_end(size_type __n, bool __x)
2570{
2571 size_type __old_size = this->__size_;
2572 this->__size_ += __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002573 _VSTD::fill_n(__make_iter(__old_size), __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002574}
2575
2576template <class _Allocator>
2577template <class _ForwardIterator>
2578typename enable_if
2579<
2580 __is_forward_iterator<_ForwardIterator>::value,
2581 void
2582>::type
2583vector<bool, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
2584{
2585 size_type __old_size = this->__size_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002586 this->__size_ += _VSTD::distance(__first, __last);
2587 _VSTD::copy(__first, __last, __make_iter(__old_size));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002588}
2589
2590template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002591inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002592vector<bool, _Allocator>::vector()
Marshall Clowe546cbb2015-06-04 02:05:41 +00002593 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant76053d72013-06-27 19:35:32 +00002594 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002595 __size_(0),
2596 __cap_alloc_(0)
2597{
2598}
2599
2600template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002601inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002602vector<bool, _Allocator>::vector(const allocator_type& __a)
Marshall Clow8f282f42015-06-04 00:10:20 +00002603#if _LIBCPP_STD_VER <= 14
2604 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
2605#else
2606 _NOEXCEPT
2607#endif
Howard Hinnant76053d72013-06-27 19:35:32 +00002608 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002609 __size_(0),
2610 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2611{
2612}
2613
2614template <class _Allocator>
2615vector<bool, _Allocator>::vector(size_type __n)
Howard Hinnant76053d72013-06-27 19:35:32 +00002616 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002617 __size_(0),
2618 __cap_alloc_(0)
2619{
2620 if (__n > 0)
2621 {
2622 allocate(__n);
2623 __construct_at_end(__n, false);
2624 }
2625}
2626
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00002627#if _LIBCPP_STD_VER > 11
2628template <class _Allocator>
2629vector<bool, _Allocator>::vector(size_type __n, const allocator_type& __a)
2630 : __begin_(nullptr),
2631 __size_(0),
2632 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2633{
2634 if (__n > 0)
2635 {
2636 allocate(__n);
2637 __construct_at_end(__n, false);
2638 }
2639}
2640#endif
2641
Howard Hinnantc51e1022010-05-11 19:42:16 +00002642template <class _Allocator>
2643vector<bool, _Allocator>::vector(size_type __n, const value_type& __x)
Howard Hinnant76053d72013-06-27 19:35:32 +00002644 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002645 __size_(0),
2646 __cap_alloc_(0)
2647{
2648 if (__n > 0)
2649 {
2650 allocate(__n);
2651 __construct_at_end(__n, __x);
2652 }
2653}
2654
2655template <class _Allocator>
2656vector<bool, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002657 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002658 __size_(0),
2659 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2660{
2661 if (__n > 0)
2662 {
2663 allocate(__n);
2664 __construct_at_end(__n, __x);
2665 }
2666}
2667
2668template <class _Allocator>
2669template <class _InputIterator>
2670vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last,
2671 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2672 !__is_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002673 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002674 __size_(0),
2675 __cap_alloc_(0)
2676{
2677#ifndef _LIBCPP_NO_EXCEPTIONS
2678 try
2679 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002680#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002681 for (; __first != __last; ++__first)
2682 push_back(*__first);
2683#ifndef _LIBCPP_NO_EXCEPTIONS
2684 }
2685 catch (...)
2686 {
Howard Hinnant76053d72013-06-27 19:35:32 +00002687 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002688 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2689 __invalidate_all_iterators();
2690 throw;
2691 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002692#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002693}
2694
2695template <class _Allocator>
2696template <class _InputIterator>
2697vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2698 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2699 !__is_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002700 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002701 __size_(0),
2702 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2703{
2704#ifndef _LIBCPP_NO_EXCEPTIONS
2705 try
2706 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002707#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002708 for (; __first != __last; ++__first)
2709 push_back(*__first);
2710#ifndef _LIBCPP_NO_EXCEPTIONS
2711 }
2712 catch (...)
2713 {
Howard Hinnant76053d72013-06-27 19:35:32 +00002714 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002715 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2716 __invalidate_all_iterators();
2717 throw;
2718 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002719#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002720}
2721
2722template <class _Allocator>
2723template <class _ForwardIterator>
2724vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last,
2725 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002726 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002727 __size_(0),
2728 __cap_alloc_(0)
2729{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002730 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002731 if (__n > 0)
2732 {
2733 allocate(__n);
2734 __construct_at_end(__first, __last);
2735 }
2736}
2737
2738template <class _Allocator>
2739template <class _ForwardIterator>
2740vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2741 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002742 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002743 __size_(0),
2744 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2745{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002746 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002747 if (__n > 0)
2748 {
2749 allocate(__n);
2750 __construct_at_end(__first, __last);
2751 }
2752}
2753
Howard Hinnant33711792011-08-12 21:56:02 +00002754#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2755
Howard Hinnantc51e1022010-05-11 19:42:16 +00002756template <class _Allocator>
2757vector<bool, _Allocator>::vector(initializer_list<value_type> __il)
Howard Hinnant76053d72013-06-27 19:35:32 +00002758 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002759 __size_(0),
2760 __cap_alloc_(0)
2761{
2762 size_type __n = static_cast<size_type>(__il.size());
2763 if (__n > 0)
2764 {
2765 allocate(__n);
2766 __construct_at_end(__il.begin(), __il.end());
2767 }
2768}
2769
2770template <class _Allocator>
2771vector<bool, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002772 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002773 __size_(0),
2774 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2775{
2776 size_type __n = static_cast<size_type>(__il.size());
2777 if (__n > 0)
2778 {
2779 allocate(__n);
2780 __construct_at_end(__il.begin(), __il.end());
2781 }
2782}
2783
Howard Hinnant33711792011-08-12 21:56:02 +00002784#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2785
Howard Hinnantc51e1022010-05-11 19:42:16 +00002786template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002787vector<bool, _Allocator>::~vector()
2788{
Howard Hinnant76053d72013-06-27 19:35:32 +00002789 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002790 __storage_traits::deallocate(__alloc(), __begin_, __cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002791 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002792}
2793
2794template <class _Allocator>
2795vector<bool, _Allocator>::vector(const vector& __v)
Howard Hinnant76053d72013-06-27 19:35:32 +00002796 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002797 __size_(0),
2798 __cap_alloc_(0, __storage_traits::select_on_container_copy_construction(__v.__alloc()))
2799{
2800 if (__v.size() > 0)
2801 {
2802 allocate(__v.size());
2803 __construct_at_end(__v.begin(), __v.end());
2804 }
2805}
2806
2807template <class _Allocator>
2808vector<bool, _Allocator>::vector(const vector& __v, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002809 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002810 __size_(0),
2811 __cap_alloc_(0, __a)
2812{
2813 if (__v.size() > 0)
2814 {
2815 allocate(__v.size());
2816 __construct_at_end(__v.begin(), __v.end());
2817 }
2818}
2819
2820template <class _Allocator>
2821vector<bool, _Allocator>&
2822vector<bool, _Allocator>::operator=(const vector& __v)
2823{
2824 if (this != &__v)
2825 {
2826 __copy_assign_alloc(__v);
2827 if (__v.__size_)
2828 {
2829 if (__v.__size_ > capacity())
2830 {
2831 deallocate();
2832 allocate(__v.__size_);
2833 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002834 _VSTD::copy(__v.__begin_, __v.__begin_ + __external_cap_to_internal(__v.__size_), __begin_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002835 }
2836 __size_ = __v.__size_;
2837 }
2838 return *this;
2839}
2840
Howard Hinnant74279a52010-09-04 23:28:19 +00002841#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2842
Howard Hinnantc51e1022010-05-11 19:42:16 +00002843template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002844inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002845vector<bool, _Allocator>::vector(vector&& __v)
Marshall Clowe5108202015-07-14 14:46:32 +00002846#if _LIBCPP_STD_VER > 14
2847 _NOEXCEPT
2848#else
Howard Hinnant1c936782011-06-03 19:40:40 +00002849 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowe5108202015-07-14 14:46:32 +00002850#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002851 : __begin_(__v.__begin_),
2852 __size_(__v.__size_),
2853 __cap_alloc_(__v.__cap_alloc_)
2854{
Howard Hinnant76053d72013-06-27 19:35:32 +00002855 __v.__begin_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002856 __v.__size_ = 0;
2857 __v.__cap() = 0;
2858}
2859
2860template <class _Allocator>
2861vector<bool, _Allocator>::vector(vector&& __v, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002862 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002863 __size_(0),
2864 __cap_alloc_(0, __a)
2865{
2866 if (__a == allocator_type(__v.__alloc()))
2867 {
2868 this->__begin_ = __v.__begin_;
2869 this->__size_ = __v.__size_;
2870 this->__cap() = __v.__cap();
2871 __v.__begin_ = nullptr;
2872 __v.__cap() = __v.__size_ = 0;
2873 }
2874 else if (__v.size() > 0)
2875 {
2876 allocate(__v.size());
2877 __construct_at_end(__v.begin(), __v.end());
2878 }
2879}
2880
2881template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002882inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002883vector<bool, _Allocator>&
2884vector<bool, _Allocator>::operator=(vector&& __v)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002885 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002886{
2887 __move_assign(__v, integral_constant<bool,
2888 __storage_traits::propagate_on_container_move_assignment::value>());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002889 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002890}
2891
2892template <class _Allocator>
2893void
2894vector<bool, _Allocator>::__move_assign(vector& __c, false_type)
2895{
2896 if (__alloc() != __c.__alloc())
2897 assign(__c.begin(), __c.end());
2898 else
2899 __move_assign(__c, true_type());
2900}
2901
2902template <class _Allocator>
2903void
2904vector<bool, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002905 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002906{
2907 deallocate();
Marshall Clowb4871fa2014-07-21 15:15:15 +00002908 __move_assign_alloc(__c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002909 this->__begin_ = __c.__begin_;
2910 this->__size_ = __c.__size_;
2911 this->__cap() = __c.__cap();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002912 __c.__begin_ = nullptr;
2913 __c.__cap() = __c.__size_ = 0;
2914}
Howard Hinnant74279a52010-09-04 23:28:19 +00002915
2916#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002917
2918template <class _Allocator>
2919void
2920vector<bool, _Allocator>::assign(size_type __n, const value_type& __x)
2921{
2922 __size_ = 0;
2923 if (__n > 0)
2924 {
2925 size_type __c = capacity();
2926 if (__n <= __c)
2927 __size_ = __n;
2928 else
2929 {
2930 vector __v(__alloc());
2931 __v.reserve(__recommend(__n));
2932 __v.__size_ = __n;
2933 swap(__v);
2934 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002935 _VSTD::fill_n(begin(), __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002936 }
Eric Fiselier69c51982016-12-28 06:06:09 +00002937 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002938}
2939
2940template <class _Allocator>
2941template <class _InputIterator>
2942typename enable_if
2943<
2944 __is_input_iterator<_InputIterator>::value &&
2945 !__is_forward_iterator<_InputIterator>::value,
2946 void
2947>::type
2948vector<bool, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2949{
2950 clear();
2951 for (; __first != __last; ++__first)
2952 push_back(*__first);
2953}
2954
2955template <class _Allocator>
2956template <class _ForwardIterator>
2957typename enable_if
2958<
2959 __is_forward_iterator<_ForwardIterator>::value,
2960 void
2961>::type
2962vector<bool, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2963{
2964 clear();
Eric Fiselier6003c772016-12-23 23:37:52 +00002965 difference_type __ns = _VSTD::distance(__first, __last);
2966 _LIBCPP_ASSERT(__ns >= 0, "invalid range specified");
2967 const size_t __n = static_cast<size_type>(__ns);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002968 if (__n)
2969 {
2970 if (__n > capacity())
2971 {
2972 deallocate();
2973 allocate(__n);
2974 }
2975 __construct_at_end(__first, __last);
2976 }
2977}
2978
2979template <class _Allocator>
2980void
2981vector<bool, _Allocator>::reserve(size_type __n)
2982{
2983 if (__n > capacity())
2984 {
2985 vector __v(this->__alloc());
2986 __v.allocate(__n);
2987 __v.__construct_at_end(this->begin(), this->end());
2988 swap(__v);
2989 __invalidate_all_iterators();
2990 }
2991}
2992
2993template <class _Allocator>
2994void
Howard Hinnant1c936782011-06-03 19:40:40 +00002995vector<bool, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002996{
2997 if (__external_cap_to_internal(size()) > __cap())
2998 {
2999#ifndef _LIBCPP_NO_EXCEPTIONS
3000 try
3001 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003002#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003003 vector(*this, allocator_type(__alloc())).swap(*this);
3004#ifndef _LIBCPP_NO_EXCEPTIONS
3005 }
3006 catch (...)
3007 {
3008 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003009#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003010 }
3011}
3012
3013template <class _Allocator>
3014typename vector<bool, _Allocator>::reference
3015vector<bool, _Allocator>::at(size_type __n)
3016{
3017 if (__n >= size())
3018 this->__throw_out_of_range();
3019 return (*this)[__n];
3020}
3021
3022template <class _Allocator>
3023typename vector<bool, _Allocator>::const_reference
3024vector<bool, _Allocator>::at(size_type __n) const
3025{
3026 if (__n >= size())
3027 this->__throw_out_of_range();
3028 return (*this)[__n];
3029}
3030
3031template <class _Allocator>
3032void
3033vector<bool, _Allocator>::push_back(const value_type& __x)
3034{
3035 if (this->__size_ == this->capacity())
3036 reserve(__recommend(this->__size_ + 1));
3037 ++this->__size_;
3038 back() = __x;
3039}
3040
3041template <class _Allocator>
3042typename vector<bool, _Allocator>::iterator
3043vector<bool, _Allocator>::insert(const_iterator __position, const value_type& __x)
3044{
3045 iterator __r;
3046 if (size() < capacity())
3047 {
3048 const_iterator __old_end = end();
3049 ++__size_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003050 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003051 __r = __const_iterator_cast(__position);
3052 }
3053 else
3054 {
3055 vector __v(__alloc());
3056 __v.reserve(__recommend(__size_ + 1));
3057 __v.__size_ = __size_ + 1;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003058 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3059 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003060 swap(__v);
3061 }
3062 *__r = __x;
3063 return __r;
3064}
3065
3066template <class _Allocator>
3067typename vector<bool, _Allocator>::iterator
3068vector<bool, _Allocator>::insert(const_iterator __position, size_type __n, const value_type& __x)
3069{
3070 iterator __r;
3071 size_type __c = capacity();
3072 if (__n <= __c && size() <= __c - __n)
3073 {
3074 const_iterator __old_end = end();
3075 __size_ += __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003076 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003077 __r = __const_iterator_cast(__position);
3078 }
3079 else
3080 {
3081 vector __v(__alloc());
3082 __v.reserve(__recommend(__size_ + __n));
3083 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003084 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3085 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003086 swap(__v);
3087 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003088 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003089 return __r;
3090}
3091
3092template <class _Allocator>
3093template <class _InputIterator>
3094typename enable_if
3095<
3096 __is_input_iterator <_InputIterator>::value &&
3097 !__is_forward_iterator<_InputIterator>::value,
3098 typename vector<bool, _Allocator>::iterator
3099>::type
3100vector<bool, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
3101{
3102 difference_type __off = __position - begin();
3103 iterator __p = __const_iterator_cast(__position);
3104 iterator __old_end = end();
3105 for (; size() != capacity() && __first != __last; ++__first)
3106 {
3107 ++this->__size_;
3108 back() = *__first;
3109 }
3110 vector __v(__alloc());
3111 if (__first != __last)
3112 {
3113#ifndef _LIBCPP_NO_EXCEPTIONS
3114 try
3115 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003116#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003117 __v.assign(__first, __last);
3118 difference_type __old_size = static_cast<difference_type>(__old_end - begin());
3119 difference_type __old_p = __p - begin();
3120 reserve(__recommend(size() + __v.size()));
3121 __p = begin() + __old_p;
3122 __old_end = begin() + __old_size;
3123#ifndef _LIBCPP_NO_EXCEPTIONS
3124 }
3125 catch (...)
3126 {
3127 erase(__old_end, end());
3128 throw;
3129 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003130#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003131 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003132 __p = _VSTD::rotate(__p, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003133 insert(__p, __v.begin(), __v.end());
3134 return begin() + __off;
3135}
3136
3137template <class _Allocator>
3138template <class _ForwardIterator>
3139typename enable_if
3140<
3141 __is_forward_iterator<_ForwardIterator>::value,
3142 typename vector<bool, _Allocator>::iterator
3143>::type
3144vector<bool, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
3145{
Eric Fiselier654dd332016-12-11 05:31:00 +00003146 const difference_type __n_signed = _VSTD::distance(__first, __last);
3147 _LIBCPP_ASSERT(__n_signed >= 0, "invalid range specified");
3148 const size_type __n = static_cast<size_type>(__n_signed);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003149 iterator __r;
3150 size_type __c = capacity();
3151 if (__n <= __c && size() <= __c - __n)
3152 {
3153 const_iterator __old_end = end();
3154 __size_ += __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003155 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003156 __r = __const_iterator_cast(__position);
3157 }
3158 else
3159 {
3160 vector __v(__alloc());
3161 __v.reserve(__recommend(__size_ + __n));
3162 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003163 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3164 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003165 swap(__v);
3166 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003167 _VSTD::copy(__first, __last, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003168 return __r;
3169}
3170
3171template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003172inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003173typename vector<bool, _Allocator>::iterator
3174vector<bool, _Allocator>::erase(const_iterator __position)
3175{
3176 iterator __r = __const_iterator_cast(__position);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003177 _VSTD::copy(__position + 1, this->cend(), __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003178 --__size_;
3179 return __r;
3180}
3181
3182template <class _Allocator>
3183typename vector<bool, _Allocator>::iterator
3184vector<bool, _Allocator>::erase(const_iterator __first, const_iterator __last)
3185{
3186 iterator __r = __const_iterator_cast(__first);
3187 difference_type __d = __last - __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003188 _VSTD::copy(__last, this->cend(), __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003189 __size_ -= __d;
3190 return __r;
3191}
3192
3193template <class _Allocator>
3194void
3195vector<bool, _Allocator>::swap(vector& __x)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003196#if _LIBCPP_STD_VER >= 14
3197 _NOEXCEPT
3198#else
Eric Fiselier69c51982016-12-28 06:06:09 +00003199 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003200 __is_nothrow_swappable<allocator_type>::value)
3201#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003202{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003203 _VSTD::swap(this->__begin_, __x.__begin_);
3204 _VSTD::swap(this->__size_, __x.__size_);
3205 _VSTD::swap(this->__cap(), __x.__cap());
Marshall Clow8982dcd2015-07-13 20:04:56 +00003206 __swap_allocator(this->__alloc(), __x.__alloc(),
3207 integral_constant<bool, __alloc_traits::propagate_on_container_swap::value>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003208}
3209
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003210template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003211void
3212vector<bool, _Allocator>::resize(size_type __sz, value_type __x)
3213{
3214 size_type __cs = size();
3215 if (__cs < __sz)
3216 {
3217 iterator __r;
3218 size_type __c = capacity();
3219 size_type __n = __sz - __cs;
3220 if (__n <= __c && __cs <= __c - __n)
3221 {
3222 __r = end();
3223 __size_ += __n;
3224 }
3225 else
3226 {
3227 vector __v(__alloc());
3228 __v.reserve(__recommend(__size_ + __n));
3229 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003230 __r = _VSTD::copy(cbegin(), cend(), __v.begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003231 swap(__v);
3232 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003233 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003234 }
3235 else
3236 __size_ = __sz;
3237}
3238
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003239template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003240void
Howard Hinnant1c936782011-06-03 19:40:40 +00003241vector<bool, _Allocator>::flip() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003242{
3243 // do middle whole words
3244 size_type __n = __size_;
3245 __storage_pointer __p = __begin_;
3246 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3247 *__p = ~*__p;
3248 // do last partial word
3249 if (__n > 0)
3250 {
3251 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3252 __storage_type __b = *__p & __m;
3253 *__p &= ~__m;
3254 *__p |= ~__b & __m;
3255 }
3256}
3257
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003258template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003259bool
3260vector<bool, _Allocator>::__invariants() const
3261{
Howard Hinnant76053d72013-06-27 19:35:32 +00003262 if (this->__begin_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003263 {
3264 if (this->__size_ != 0 || this->__cap() != 0)
3265 return false;
3266 }
3267 else
3268 {
3269 if (this->__cap() == 0)
3270 return false;
3271 if (this->__size_ > this->capacity())
3272 return false;
3273 }
3274 return true;
3275}
3276
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003277template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003278size_t
Howard Hinnant1c936782011-06-03 19:40:40 +00003279vector<bool, _Allocator>::__hash_code() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003280{
3281 size_t __h = 0;
3282 // do middle whole words
3283 size_type __n = __size_;
3284 __storage_pointer __p = __begin_;
3285 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3286 __h ^= *__p;
3287 // do last partial word
3288 if (__n > 0)
3289 {
3290 const __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3291 __h ^= *__p & __m;
3292 }
3293 return __h;
3294}
3295
3296template <class _Allocator>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003297struct _LIBCPP_TEMPLATE_VIS hash<vector<bool, _Allocator> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00003298 : public unary_function<vector<bool, _Allocator>, size_t>
3299{
Howard Hinnant1c265cd2010-09-23 18:58:28 +00003300 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +00003301 size_t operator()(const vector<bool, _Allocator>& __vec) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003302 {return __vec.__hash_code();}
3303};
3304
3305template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003306inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003307bool
3308operator==(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3309{
3310 const typename vector<_Tp, _Allocator>::size_type __sz = __x.size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003311 return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003312}
3313
3314template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003315inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003316bool
3317operator!=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3318{
3319 return !(__x == __y);
3320}
3321
3322template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003323inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003324bool
3325operator< (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3326{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003327 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003328}
3329
3330template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003331inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003332bool
3333operator> (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3334{
3335 return __y < __x;
3336}
3337
3338template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003339inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003340bool
3341operator>=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3342{
3343 return !(__x < __y);
3344}
3345
3346template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003347inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003348bool
3349operator<=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3350{
3351 return !(__y < __x);
3352}
3353
3354template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003355inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003356void
3357swap(vector<_Tp, _Allocator>& __x, vector<_Tp, _Allocator>& __y)
Howard Hinnant1c936782011-06-03 19:40:40 +00003358 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00003359{
3360 __x.swap(__y);
3361}
3362
3363_LIBCPP_END_NAMESPACE_STD
3364
3365#endif // _LIBCPP_VECTOR