blob: ded057b10c8b6911d07d916a29e35003cae68ee0 [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{
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000416 while (__new_last != __end_)
Howard Hinnant76053d72013-06-27 19:35:32 +0000417 __alloc_traits::destroy(__alloc(), _VSTD::__to_raw_pointer(--__end_));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000418}
419
420template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000421inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000422__vector_base<_Tp, _Allocator>::__vector_base()
Howard Hinnant1c936782011-06-03 19:40:40 +0000423 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant76053d72013-06-27 19:35:32 +0000424 : __begin_(nullptr),
425 __end_(nullptr),
426 __end_cap_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000427{
428}
429
430template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000431inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000432__vector_base<_Tp, _Allocator>::__vector_base(const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +0000433 : __begin_(nullptr),
434 __end_(nullptr),
435 __end_cap_(nullptr, __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000436{
437}
438
439template <class _Tp, class _Allocator>
440__vector_base<_Tp, _Allocator>::~__vector_base()
441{
Howard Hinnant76053d72013-06-27 19:35:32 +0000442 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000443 {
444 clear();
445 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
446 }
447}
448
Eric Fiselier876c6862016-02-20 00:19:45 +0000449template <class _Tp, class _Allocator /* = allocator<_Tp> */>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000450class _LIBCPP_TEMPLATE_VIS vector
Howard Hinnantc51e1022010-05-11 19:42:16 +0000451 : private __vector_base<_Tp, _Allocator>
452{
453private:
454 typedef __vector_base<_Tp, _Allocator> __base;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000455 typedef allocator<_Tp> __default_allocator_type;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000456public:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000457 typedef vector __self;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000458 typedef _Tp value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000459 typedef _Allocator allocator_type;
460 typedef typename __base::__alloc_traits __alloc_traits;
461 typedef typename __base::reference reference;
462 typedef typename __base::const_reference const_reference;
463 typedef typename __base::size_type size_type;
464 typedef typename __base::difference_type difference_type;
465 typedef typename __base::pointer pointer;
466 typedef typename __base::const_pointer const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000467 typedef __wrap_iter<pointer> iterator;
468 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000469 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
470 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000471
Howard Hinnanta416ff02013-03-26 19:04:56 +0000472 static_assert((is_same<typename allocator_type::value_type, value_type>::value),
473 "Allocator::value_type must be same type as value_type");
474
Howard Hinnant1c936782011-06-03 19:40:40 +0000475 _LIBCPP_INLINE_VISIBILITY
Marshall Clowe546cbb2015-06-04 02:05:41 +0000476 vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant27e0e772011-09-14 18:33:51 +0000477 {
Howard Hinnanta47c6d52011-09-16 17:29:17 +0000478#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000479 __get_db()->__insert_c(this);
480#endif
481 }
482 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
Marshall Clow8f282f42015-06-04 00:10:20 +0000483#if _LIBCPP_STD_VER <= 14
484 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
485#else
486 _NOEXCEPT
487#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +0000488 : __base(__a)
489 {
Howard Hinnanta47c6d52011-09-16 17:29:17 +0000490#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000491 __get_db()->__insert_c(this);
492#endif
493 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000494 explicit vector(size_type __n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +0000495#if _LIBCPP_STD_VER > 11
496 explicit vector(size_type __n, const allocator_type& __a);
497#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000498 vector(size_type __n, const_reference __x);
499 vector(size_type __n, const_reference __x, const allocator_type& __a);
500 template <class _InputIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +0000501 vector(_InputIterator __first,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000502 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000503 !__is_forward_iterator<_InputIterator>::value &&
504 is_constructible<
505 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +0000506 typename iterator_traits<_InputIterator>::reference>::value,
507 _InputIterator>::type __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000508 template <class _InputIterator>
509 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
510 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000511 !__is_forward_iterator<_InputIterator>::value &&
512 is_constructible<
513 value_type,
514 typename iterator_traits<_InputIterator>::reference>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000515 template <class _ForwardIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +0000516 vector(_ForwardIterator __first,
Howard Hinnant88010252013-03-28 17:44:32 +0000517 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
518 is_constructible<
519 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +0000520 typename iterator_traits<_ForwardIterator>::reference>::value,
521 _ForwardIterator>::type __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000522 template <class _ForwardIterator>
523 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Howard Hinnant88010252013-03-28 17:44:32 +0000524 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
525 is_constructible<
526 value_type,
527 typename iterator_traits<_ForwardIterator>::reference>::value>::type* = 0);
Howard Hinnant33711792011-08-12 21:56:02 +0000528#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantcf823322010-12-17 14:46:43 +0000529 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000530 vector(initializer_list<value_type> __il);
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, const allocator_type& __a);
Howard Hinnant33711792011-08-12 21:56:02 +0000533#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnanta47c6d52011-09-16 17:29:17 +0000534#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000535 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant27e0e772011-09-14 18:33:51 +0000536 ~vector()
537 {
538 __get_db()->__erase_c(this);
539 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000540#endif
541
542 vector(const vector& __x);
543 vector(const vector& __x, const allocator_type& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000544 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000545 vector& operator=(const vector& __x);
Howard Hinnant74279a52010-09-04 23:28:19 +0000546#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantcf823322010-12-17 14:46:43 +0000547 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000548 vector(vector&& __x)
Marshall Clowe5108202015-07-14 14:46:32 +0000549#if _LIBCPP_STD_VER > 14
550 _NOEXCEPT;
551#else
Howard Hinnant1c936782011-06-03 19:40:40 +0000552 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowe5108202015-07-14 14:46:32 +0000553#endif
Howard Hinnantcf823322010-12-17 14:46:43 +0000554 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000555 vector(vector&& __x, const allocator_type& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000556 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000557 vector& operator=(vector&& __x)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000558 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Howard Hinnant74279a52010-09-04 23:28:19 +0000559#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant33711792011-08-12 21:56:02 +0000560#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000561 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000562 vector& operator=(initializer_list<value_type> __il)
563 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnant33711792011-08-12 21:56:02 +0000564#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000565
566 template <class _InputIterator>
567 typename enable_if
568 <
569 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000570 !__is_forward_iterator<_InputIterator>::value &&
571 is_constructible<
572 value_type,
573 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000574 void
575 >::type
576 assign(_InputIterator __first, _InputIterator __last);
577 template <class _ForwardIterator>
578 typename enable_if
579 <
Howard Hinnant88010252013-03-28 17:44:32 +0000580 __is_forward_iterator<_ForwardIterator>::value &&
581 is_constructible<
582 value_type,
583 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000584 void
585 >::type
586 assign(_ForwardIterator __first, _ForwardIterator __last);
587
588 void assign(size_type __n, const_reference __u);
Howard Hinnant33711792011-08-12 21:56:02 +0000589#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000590 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000591 void assign(initializer_list<value_type> __il)
592 {assign(__il.begin(), __il.end());}
Howard Hinnant33711792011-08-12 21:56:02 +0000593#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000594
Howard Hinnant1c936782011-06-03 19:40:40 +0000595 _LIBCPP_INLINE_VISIBILITY
596 allocator_type get_allocator() const _NOEXCEPT
597 {return this->__alloc();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000598
Howard Hinnant1c936782011-06-03 19:40:40 +0000599 _LIBCPP_INLINE_VISIBILITY iterator begin() _NOEXCEPT;
600 _LIBCPP_INLINE_VISIBILITY const_iterator begin() const _NOEXCEPT;
601 _LIBCPP_INLINE_VISIBILITY iterator end() _NOEXCEPT;
602 _LIBCPP_INLINE_VISIBILITY const_iterator end() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000603
Howard Hinnant1c936782011-06-03 19:40:40 +0000604 _LIBCPP_INLINE_VISIBILITY
605 reverse_iterator rbegin() _NOEXCEPT
606 {return reverse_iterator(end());}
607 _LIBCPP_INLINE_VISIBILITY
608 const_reverse_iterator rbegin() const _NOEXCEPT
609 {return const_reverse_iterator(end());}
610 _LIBCPP_INLINE_VISIBILITY
611 reverse_iterator rend() _NOEXCEPT
612 {return reverse_iterator(begin());}
613 _LIBCPP_INLINE_VISIBILITY
614 const_reverse_iterator rend() const _NOEXCEPT
615 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000616
Howard Hinnant1c936782011-06-03 19:40:40 +0000617 _LIBCPP_INLINE_VISIBILITY
618 const_iterator cbegin() const _NOEXCEPT
619 {return begin();}
620 _LIBCPP_INLINE_VISIBILITY
621 const_iterator cend() const _NOEXCEPT
622 {return end();}
623 _LIBCPP_INLINE_VISIBILITY
624 const_reverse_iterator crbegin() const _NOEXCEPT
625 {return rbegin();}
626 _LIBCPP_INLINE_VISIBILITY
627 const_reverse_iterator crend() const _NOEXCEPT
628 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000629
Howard Hinnant1c936782011-06-03 19:40:40 +0000630 _LIBCPP_INLINE_VISIBILITY
631 size_type size() const _NOEXCEPT
632 {return static_cast<size_type>(this->__end_ - this->__begin_);}
633 _LIBCPP_INLINE_VISIBILITY
634 size_type capacity() const _NOEXCEPT
635 {return __base::capacity();}
636 _LIBCPP_INLINE_VISIBILITY
637 bool empty() const _NOEXCEPT
638 {return this->__begin_ == this->__end_;}
639 size_type max_size() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000640 void reserve(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +0000641 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000642
643 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n);
644 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const;
645 reference at(size_type __n);
646 const_reference at(size_type __n) const;
647
Howard Hinnant27e0e772011-09-14 18:33:51 +0000648 _LIBCPP_INLINE_VISIBILITY reference front()
649 {
650 _LIBCPP_ASSERT(!empty(), "front() called for empty vector");
651 return *this->__begin_;
652 }
653 _LIBCPP_INLINE_VISIBILITY const_reference front() const
654 {
655 _LIBCPP_ASSERT(!empty(), "front() called for empty vector");
656 return *this->__begin_;
657 }
658 _LIBCPP_INLINE_VISIBILITY reference back()
659 {
660 _LIBCPP_ASSERT(!empty(), "back() called for empty vector");
661 return *(this->__end_ - 1);
662 }
663 _LIBCPP_INLINE_VISIBILITY const_reference back() const
664 {
665 _LIBCPP_ASSERT(!empty(), "back() called for empty vector");
666 return *(this->__end_ - 1);
667 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000668
Howard Hinnant1c936782011-06-03 19:40:40 +0000669 _LIBCPP_INLINE_VISIBILITY
670 value_type* data() _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000671 {return _VSTD::__to_raw_pointer(this->__begin_);}
Howard Hinnant1c936782011-06-03 19:40:40 +0000672 _LIBCPP_INLINE_VISIBILITY
673 const value_type* data() const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000674 {return _VSTD::__to_raw_pointer(this->__begin_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000675
Howard Hinnantcf823322010-12-17 14:46:43 +0000676 _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
Howard Hinnant74279a52010-09-04 23:28:19 +0000677#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000678 _LIBCPP_INLINE_VISIBILITY void push_back(value_type&& __x);
Howard Hinnant74279a52010-09-04 23:28:19 +0000679#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000680 template <class... _Args>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000681 _LIBCPP_INLINE_VISIBILITY
Marshall Clowea52cc42017-01-24 23:09:12 +0000682#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +0000683 reference emplace_back(_Args&&... __args);
Marshall Clowea52cc42017-01-24 23:09:12 +0000684#else
685 void emplace_back(_Args&&... __args);
686#endif
Howard Hinnant74279a52010-09-04 23:28:19 +0000687#endif // _LIBCPP_HAS_NO_VARIADICS
688#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000689 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000690 void pop_back();
691
692 iterator insert(const_iterator __position, const_reference __x);
Howard Hinnant74279a52010-09-04 23:28:19 +0000693#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +0000694 iterator insert(const_iterator __position, value_type&& __x);
Howard Hinnant74279a52010-09-04 23:28:19 +0000695#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000696 template <class... _Args>
697 iterator emplace(const_iterator __position, _Args&&... __args);
Howard Hinnant74279a52010-09-04 23:28:19 +0000698#endif // _LIBCPP_HAS_NO_VARIADICS
699#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +0000700 iterator insert(const_iterator __position, size_type __n, const_reference __x);
701 template <class _InputIterator>
702 typename enable_if
703 <
704 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000705 !__is_forward_iterator<_InputIterator>::value &&
706 is_constructible<
707 value_type,
708 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000709 iterator
710 >::type
711 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
712 template <class _ForwardIterator>
713 typename enable_if
714 <
Howard Hinnant88010252013-03-28 17:44:32 +0000715 __is_forward_iterator<_ForwardIterator>::value &&
716 is_constructible<
717 value_type,
718 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000719 iterator
720 >::type
721 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnant33711792011-08-12 21:56:02 +0000722#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000723 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000724 iterator insert(const_iterator __position, initializer_list<value_type> __il)
725 {return insert(__position, __il.begin(), __il.end());}
Howard Hinnant33711792011-08-12 21:56:02 +0000726#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000727
Howard Hinnantcf823322010-12-17 14:46:43 +0000728 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000729 iterator erase(const_iterator __first, const_iterator __last);
730
Howard Hinnant1c936782011-06-03 19:40:40 +0000731 _LIBCPP_INLINE_VISIBILITY
732 void clear() _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000733 {
Marshall Clow2cd9d372014-05-08 14:14:06 +0000734 size_type __old_size = size();
Howard Hinnant27e0e772011-09-14 18:33:51 +0000735 __base::clear();
Marshall Clow2cd9d372014-05-08 14:14:06 +0000736 __annotate_shrink(__old_size);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000737 __invalidate_all_iterators();
738 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000739
740 void resize(size_type __sz);
741 void resize(size_type __sz, const_reference __x);
742
Howard Hinnant1c936782011-06-03 19:40:40 +0000743 void swap(vector&)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000744#if _LIBCPP_STD_VER >= 14
Eric Fiselier69c51982016-12-28 06:06:09 +0000745 _NOEXCEPT_DEBUG;
Marshall Clow8982dcd2015-07-13 20:04:56 +0000746#else
Eric Fiselier69c51982016-12-28 06:06:09 +0000747 _NOEXCEPT_DEBUG_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +0000748 __is_nothrow_swappable<allocator_type>::value);
749#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000750
751 bool __invariants() const;
752
Howard Hinnanta47c6d52011-09-16 17:29:17 +0000753#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000754
755 bool __dereferenceable(const const_iterator* __i) const;
756 bool __decrementable(const const_iterator* __i) const;
757 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
758 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
759
Howard Hinnanta47c6d52011-09-16 17:29:17 +0000760#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000761
Howard Hinnantc51e1022010-05-11 19:42:16 +0000762private:
Howard Hinnantcf823322010-12-17 14:46:43 +0000763 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Eric Fiselier69c51982016-12-28 06:06:09 +0000764 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(pointer __new_last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000765 void allocate(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +0000766 void deallocate() _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000767 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
Howard Hinnanta5589382011-01-04 19:53:31 +0000768 void __construct_at_end(size_type __n);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000769 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000770 void __construct_at_end(size_type __n, const_reference __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000771 template <class _ForwardIterator>
772 typename enable_if
773 <
774 __is_forward_iterator<_ForwardIterator>::value,
775 void
776 >::type
Eric Fiselieracfe6f02015-03-31 16:54:19 +0000777 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000778 void __append(size_type __n);
779 void __append(size_type __n, const_reference __x);
Howard Hinnantcf823322010-12-17 14:46:43 +0000780 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000781 iterator __make_iter(pointer __p) _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000782 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000783 const_iterator __make_iter(const_pointer __p) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000784 void __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v);
785 pointer __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p);
786 void __move_range(pointer __from_s, pointer __from_e, pointer __to);
Howard Hinnant1c936782011-06-03 19:40:40 +0000787 void __move_assign(vector& __c, true_type)
788 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000789 void __move_assign(vector& __c, false_type)
790 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000791 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76053d72013-06-27 19:35:32 +0000792 void __destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000793 {
Eric Fiselier69c51982016-12-28 06:06:09 +0000794 __invalidate_iterators_past(__new_last);
Marshall Clow2cd9d372014-05-08 14:14:06 +0000795 size_type __old_size = size();
Howard Hinnant27e0e772011-09-14 18:33:51 +0000796 __base::__destruct_at_end(__new_last);
Marshall Clow2cd9d372014-05-08 14:14:06 +0000797 __annotate_shrink(__old_size);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000798 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000799 template <class _Up>
800 void
801#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
802 __push_back_slow_path(_Up&& __x);
803#else
804 __push_back_slow_path(_Up& __x);
805#endif
Howard Hinnantb6c49562012-02-26 15:30:12 +0000806#if !defined(_LIBCPP_HAS_NO_VARIADICS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
807 template <class... _Args>
808 void
809 __emplace_back_slow_path(_Args&&... __args);
810#endif
Marshall Clow2cd9d372014-05-08 14:14:06 +0000811 // The following functions are no-ops outside of AddressSanitizer mode.
812 // We call annotatations only for the default Allocator because other allocators
813 // may not meet the AddressSanitizer alignment constraints.
814 // See the documentation for __sanitizer_annotate_contiguous_container for more details.
Marshall Clow2cd9d372014-05-08 14:14:06 +0000815#ifndef _LIBCPP_HAS_NO_ASAN
Eric Fiselier6003c772016-12-23 23:37:52 +0000816 void __annotate_contiguous_container(const void *__beg, const void *__end,
817 const void *__old_mid,
818 const void *__new_mid) const
819 {
820
Marshall Clow2cd9d372014-05-08 14:14:06 +0000821 if (__beg && is_same<allocator_type, __default_allocator_type>::value)
822 __sanitizer_annotate_contiguous_container(__beg, __end, __old_mid, __new_mid);
Marshall Clow2cd9d372014-05-08 14:14:06 +0000823 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000824#else
825 _LIBCPP_INLINE_VISIBILITY
826 void __annotate_contiguous_container(const void*, const void*, const void*,
827 const void*) const {}
828#endif
829 _LIBCPP_INLINE_VISIBILITY
830 void __annotate_new(size_type __current_size) const {
Marshall Clow2cd9d372014-05-08 14:14:06 +0000831 __annotate_contiguous_container(data(), data() + capacity(),
832 data() + capacity(), data() + __current_size);
833 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000834
835 _LIBCPP_INLINE_VISIBILITY
836 void __annotate_delete() const {
Marshall Clow2cd9d372014-05-08 14:14:06 +0000837 __annotate_contiguous_container(data(), data() + capacity(),
838 data() + size(), data() + capacity());
839 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000840
841 _LIBCPP_INLINE_VISIBILITY
Kostya Serebryany4963c252014-09-02 23:43:38 +0000842 void __annotate_increase(size_type __n) const
Marshall Clow2cd9d372014-05-08 14:14:06 +0000843 {
844 __annotate_contiguous_container(data(), data() + capacity(),
845 data() + size(), data() + size() + __n);
846 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000847
848 _LIBCPP_INLINE_VISIBILITY
Kostya Serebryany4963c252014-09-02 23:43:38 +0000849 void __annotate_shrink(size_type __old_size) const
Marshall Clow2cd9d372014-05-08 14:14:06 +0000850 {
851 __annotate_contiguous_container(data(), data() + capacity(),
852 data() + __old_size, data() + size());
853 }
Marshall Clowc78ffa52014-09-03 21:37:43 +0000854#ifndef _LIBCPP_HAS_NO_ASAN
Kostya Serebryany4963c252014-09-02 23:43:38 +0000855 // The annotation for size increase should happen before the actual increase,
856 // but if an exception is thrown after that the annotation has to be undone.
857 struct __RAII_IncreaseAnnotator {
858 __RAII_IncreaseAnnotator(const vector &__v, size_type __n = 1)
Eric Fiseliera7c043e2015-03-10 00:25:20 +0000859 : __commit(false), __v(__v), __old_size(__v.size() + __n) {
Kostya Serebryany4963c252014-09-02 23:43:38 +0000860 __v.__annotate_increase(__n);
861 }
862 void __done() { __commit = true; }
863 ~__RAII_IncreaseAnnotator() {
864 if (__commit) return;
Eric Fiseliera7c043e2015-03-10 00:25:20 +0000865 __v.__annotate_shrink(__old_size);
Kostya Serebryany4963c252014-09-02 23:43:38 +0000866 }
867 bool __commit;
Kostya Serebryany4963c252014-09-02 23:43:38 +0000868 const vector &__v;
Eric Fiseliera7c043e2015-03-10 00:25:20 +0000869 size_type __old_size;
Kostya Serebryany4963c252014-09-02 23:43:38 +0000870 };
Marshall Clowc78ffa52014-09-03 21:37:43 +0000871#else
872 struct __RAII_IncreaseAnnotator {
Eric Fiselier6003c772016-12-23 23:37:52 +0000873 _LIBCPP_INLINE_VISIBILITY
874 __RAII_IncreaseAnnotator(const vector &, size_type = 1) {}
875 _LIBCPP_INLINE_VISIBILITY void __done() {}
Marshall Clowc78ffa52014-09-03 21:37:43 +0000876 };
877#endif
878
Howard Hinnantc51e1022010-05-11 19:42:16 +0000879};
880
881template <class _Tp, class _Allocator>
882void
883vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v)
884{
Marshall Clow2cd9d372014-05-08 14:14:06 +0000885 __annotate_delete();
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000886 __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, this->__end_, __v.__begin_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000887 _VSTD::swap(this->__begin_, __v.__begin_);
888 _VSTD::swap(this->__end_, __v.__end_);
889 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000890 __v.__first_ = __v.__begin_;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000891 __annotate_new(size());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000892 __invalidate_all_iterators();
893}
894
895template <class _Tp, class _Allocator>
896typename vector<_Tp, _Allocator>::pointer
897vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p)
898{
Marshall Clow2cd9d372014-05-08 14:14:06 +0000899 __annotate_delete();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000900 pointer __r = __v.__begin_;
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000901 __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, __p, __v.__begin_);
902 __alloc_traits::__construct_forward(this->__alloc(), __p, this->__end_, __v.__end_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000903 _VSTD::swap(this->__begin_, __v.__begin_);
904 _VSTD::swap(this->__end_, __v.__end_);
905 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000906 __v.__first_ = __v.__begin_;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000907 __annotate_new(size());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000908 __invalidate_all_iterators();
909 return __r;
910}
911
912// Allocate space for __n objects
913// throws length_error if __n > max_size()
914// throws (probably bad_alloc) if memory run out
915// Precondition: __begin_ == __end_ == __end_cap() == 0
916// Precondition: __n > 0
917// Postcondition: capacity() == __n
918// Postcondition: size() == 0
919template <class _Tp, class _Allocator>
920void
921vector<_Tp, _Allocator>::allocate(size_type __n)
922{
923 if (__n > max_size())
924 this->__throw_length_error();
925 this->__begin_ = this->__end_ = __alloc_traits::allocate(this->__alloc(), __n);
926 this->__end_cap() = this->__begin_ + __n;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000927 __annotate_new(0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000928}
929
930template <class _Tp, class _Allocator>
931void
Howard Hinnant1c936782011-06-03 19:40:40 +0000932vector<_Tp, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000933{
Howard Hinnant76053d72013-06-27 19:35:32 +0000934 if (this->__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000935 {
936 clear();
937 __alloc_traits::deallocate(this->__alloc(), this->__begin_, capacity());
Howard Hinnant76053d72013-06-27 19:35:32 +0000938 this->__begin_ = this->__end_ = this->__end_cap() = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000939 }
940}
941
942template <class _Tp, class _Allocator>
943typename vector<_Tp, _Allocator>::size_type
Howard Hinnant1c936782011-06-03 19:40:40 +0000944vector<_Tp, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000945{
Eric Fiselierb5d9f442016-11-23 01:18:56 +0000946 return _VSTD::min<size_type>(__alloc_traits::max_size(this->__alloc()),
947 numeric_limits<difference_type>::max());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000948}
949
950// Precondition: __new_size > capacity()
951template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000952inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000953typename vector<_Tp, _Allocator>::size_type
954vector<_Tp, _Allocator>::__recommend(size_type __new_size) const
955{
956 const size_type __ms = max_size();
957 if (__new_size > __ms)
958 this->__throw_length_error();
959 const size_type __cap = capacity();
960 if (__cap >= __ms / 2)
961 return __ms;
Alexis Hunt991d29b2011-07-29 23:31:58 +0000962 return _VSTD::max<size_type>(2*__cap, __new_size);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000963}
964
965// Default constructs __n objects starting at __end_
966// throws if construction throws
967// Precondition: __n > 0
968// Precondition: size() + __n <= capacity()
969// Postcondition: size() == size() + __n
970template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000971void
972vector<_Tp, _Allocator>::__construct_at_end(size_type __n)
973{
Howard Hinnantc51e1022010-05-11 19:42:16 +0000974 allocator_type& __a = this->__alloc();
975 do
976 {
Kostya Serebryany4963c252014-09-02 23:43:38 +0000977 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000978 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000979 ++this->__end_;
980 --__n;
Kostya Serebryany4963c252014-09-02 23:43:38 +0000981 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000982 } while (__n > 0);
983}
984
Howard Hinnantc51e1022010-05-11 19:42:16 +0000985// Copy constructs __n objects starting at __end_ from __x
986// throws if construction throws
987// Precondition: __n > 0
988// Precondition: size() + __n <= capacity()
989// Postcondition: size() == old size() + __n
990// Postcondition: [i] == __x for all i in [size() - __n, __n)
991template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000992inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000993void
994vector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
995{
Howard Hinnantc51e1022010-05-11 19:42:16 +0000996 allocator_type& __a = this->__alloc();
997 do
998 {
Kostya Serebryany4963c252014-09-02 23:43:38 +0000999 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001000 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001001 ++this->__end_;
1002 --__n;
Kostya Serebryany4963c252014-09-02 23:43:38 +00001003 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001004 } while (__n > 0);
1005}
1006
1007template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001008template <class _ForwardIterator>
1009typename enable_if
1010<
1011 __is_forward_iterator<_ForwardIterator>::value,
1012 void
1013>::type
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001014vector<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001015{
1016 allocator_type& __a = this->__alloc();
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001017 __RAII_IncreaseAnnotator __annotator(*this, __n);
1018 __alloc_traits::__construct_range_forward(__a, __first, __last, this->__end_);
1019 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001020}
1021
1022// Default constructs __n objects starting at __end_
1023// throws if construction throws
1024// Postcondition: size() == size() + __n
Howard Hinnant1c936782011-06-03 19:40:40 +00001025// Exception safety: strong.
Howard Hinnantc51e1022010-05-11 19:42:16 +00001026template <class _Tp, class _Allocator>
1027void
1028vector<_Tp, _Allocator>::__append(size_type __n)
1029{
1030 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1031 this->__construct_at_end(__n);
1032 else
1033 {
1034 allocator_type& __a = this->__alloc();
1035 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1036 __v.__construct_at_end(__n);
1037 __swap_out_circular_buffer(__v);
1038 }
1039}
1040
1041// Default constructs __n objects starting at __end_
1042// throws if construction throws
1043// Postcondition: size() == size() + __n
Howard Hinnant1c936782011-06-03 19:40:40 +00001044// Exception safety: strong.
Howard Hinnantc51e1022010-05-11 19:42:16 +00001045template <class _Tp, class _Allocator>
1046void
1047vector<_Tp, _Allocator>::__append(size_type __n, const_reference __x)
1048{
1049 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1050 this->__construct_at_end(__n, __x);
1051 else
1052 {
1053 allocator_type& __a = this->__alloc();
1054 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1055 __v.__construct_at_end(__n, __x);
1056 __swap_out_circular_buffer(__v);
1057 }
1058}
1059
1060template <class _Tp, class _Allocator>
1061vector<_Tp, _Allocator>::vector(size_type __n)
1062{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001063#if _LIBCPP_DEBUG_LEVEL >= 2
1064 __get_db()->__insert_c(this);
1065#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001066 if (__n > 0)
1067 {
1068 allocate(__n);
1069 __construct_at_end(__n);
1070 }
1071}
1072
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00001073#if _LIBCPP_STD_VER > 11
1074template <class _Tp, class _Allocator>
1075vector<_Tp, _Allocator>::vector(size_type __n, const allocator_type& __a)
1076 : __base(__a)
1077{
1078#if _LIBCPP_DEBUG_LEVEL >= 2
1079 __get_db()->__insert_c(this);
1080#endif
1081 if (__n > 0)
1082 {
1083 allocate(__n);
1084 __construct_at_end(__n);
1085 }
1086}
1087#endif
1088
Howard Hinnantc51e1022010-05-11 19:42:16 +00001089template <class _Tp, class _Allocator>
1090vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x)
1091{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001092#if _LIBCPP_DEBUG_LEVEL >= 2
1093 __get_db()->__insert_c(this);
1094#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001095 if (__n > 0)
1096 {
1097 allocate(__n);
1098 __construct_at_end(__n, __x);
1099 }
1100}
1101
1102template <class _Tp, class _Allocator>
1103vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x, const allocator_type& __a)
1104 : __base(__a)
1105{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001106#if _LIBCPP_DEBUG_LEVEL >= 2
1107 __get_db()->__insert_c(this);
1108#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001109 if (__n > 0)
1110 {
1111 allocate(__n);
1112 __construct_at_end(__n, __x);
1113 }
1114}
1115
1116template <class _Tp, class _Allocator>
1117template <class _InputIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +00001118vector<_Tp, _Allocator>::vector(_InputIterator __first,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001119 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001120 !__is_forward_iterator<_InputIterator>::value &&
1121 is_constructible<
1122 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +00001123 typename iterator_traits<_InputIterator>::reference>::value,
1124 _InputIterator>::type __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001125{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001126#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001127 __get_db()->__insert_c(this);
1128#endif
Howard Hinnant9cd22302011-09-16 18:41:29 +00001129 for (; __first != __last; ++__first)
1130 push_back(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001131}
1132
1133template <class _Tp, class _Allocator>
1134template <class _InputIterator>
1135vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
1136 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001137 !__is_forward_iterator<_InputIterator>::value &&
1138 is_constructible<
1139 value_type,
1140 typename iterator_traits<_InputIterator>::reference>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001141 : __base(__a)
1142{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001143#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001144 __get_db()->__insert_c(this);
1145#endif
Howard Hinnant9cd22302011-09-16 18:41:29 +00001146 for (; __first != __last; ++__first)
1147 push_back(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001148}
1149
1150template <class _Tp, class _Allocator>
1151template <class _ForwardIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +00001152vector<_Tp, _Allocator>::vector(_ForwardIterator __first,
Howard Hinnant88010252013-03-28 17:44:32 +00001153 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
1154 is_constructible<
1155 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +00001156 typename iterator_traits<_ForwardIterator>::reference>::value,
1157 _ForwardIterator>::type __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001158{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001159#if _LIBCPP_DEBUG_LEVEL >= 2
1160 __get_db()->__insert_c(this);
1161#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001162 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001163 if (__n > 0)
1164 {
1165 allocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001166 __construct_at_end(__first, __last, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001167 }
1168}
1169
1170template <class _Tp, class _Allocator>
1171template <class _ForwardIterator>
1172vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Howard Hinnant88010252013-03-28 17:44:32 +00001173 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
1174 is_constructible<
1175 value_type,
1176 typename iterator_traits<_ForwardIterator>::reference>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001177 : __base(__a)
1178{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001179#if _LIBCPP_DEBUG_LEVEL >= 2
1180 __get_db()->__insert_c(this);
1181#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001182 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001183 if (__n > 0)
1184 {
1185 allocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001186 __construct_at_end(__first, __last, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001187 }
1188}
1189
1190template <class _Tp, class _Allocator>
1191vector<_Tp, _Allocator>::vector(const vector& __x)
1192 : __base(__alloc_traits::select_on_container_copy_construction(__x.__alloc()))
1193{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001194#if _LIBCPP_DEBUG_LEVEL >= 2
1195 __get_db()->__insert_c(this);
1196#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001197 size_type __n = __x.size();
1198 if (__n > 0)
1199 {
1200 allocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001201 __construct_at_end(__x.__begin_, __x.__end_, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001202 }
1203}
1204
1205template <class _Tp, class _Allocator>
1206vector<_Tp, _Allocator>::vector(const vector& __x, const allocator_type& __a)
1207 : __base(__a)
1208{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001209#if _LIBCPP_DEBUG_LEVEL >= 2
1210 __get_db()->__insert_c(this);
1211#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001212 size_type __n = __x.size();
1213 if (__n > 0)
1214 {
1215 allocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001216 __construct_at_end(__x.__begin_, __x.__end_, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001217 }
1218}
1219
Howard Hinnant74279a52010-09-04 23:28:19 +00001220#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001221
1222template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001223inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001224vector<_Tp, _Allocator>::vector(vector&& __x)
Marshall Clowe5108202015-07-14 14:46:32 +00001225#if _LIBCPP_STD_VER > 14
1226 _NOEXCEPT
1227#else
Howard Hinnant1c936782011-06-03 19:40:40 +00001228 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowe5108202015-07-14 14:46:32 +00001229#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001230 : __base(_VSTD::move(__x.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001231{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001232#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001233 __get_db()->__insert_c(this);
Howard Hinnanta1e60cd2011-09-19 16:34:29 +00001234 __get_db()->swap(this, &__x);
Howard Hinnant27e0e772011-09-14 18:33:51 +00001235#endif
Howard Hinnant9cd22302011-09-16 18:41:29 +00001236 this->__begin_ = __x.__begin_;
1237 this->__end_ = __x.__end_;
1238 this->__end_cap() = __x.__end_cap();
Howard Hinnant76053d72013-06-27 19:35:32 +00001239 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001240}
1241
1242template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001243inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001244vector<_Tp, _Allocator>::vector(vector&& __x, const allocator_type& __a)
1245 : __base(__a)
1246{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001247#if _LIBCPP_DEBUG_LEVEL >= 2
1248 __get_db()->__insert_c(this);
1249#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001250 if (__a == __x.__alloc())
1251 {
1252 this->__begin_ = __x.__begin_;
1253 this->__end_ = __x.__end_;
1254 this->__end_cap() = __x.__end_cap();
1255 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnanta1e60cd2011-09-19 16:34:29 +00001256#if _LIBCPP_DEBUG_LEVEL >= 2
1257 __get_db()->swap(this, &__x);
1258#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001259 }
1260 else
1261 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001262 typedef move_iterator<iterator> _Ip;
1263 assign(_Ip(__x.begin()), _Ip(__x.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001264 }
1265}
1266
Howard Hinnant33711792011-08-12 21:56:02 +00001267#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1268
Howard Hinnantc51e1022010-05-11 19:42:16 +00001269template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001270inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001271vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il)
1272{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001273#if _LIBCPP_DEBUG_LEVEL >= 2
1274 __get_db()->__insert_c(this);
1275#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001276 if (__il.size() > 0)
1277 {
1278 allocate(__il.size());
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001279 __construct_at_end(__il.begin(), __il.end(), __il.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001280 }
1281}
1282
1283template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001284inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001285vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
1286 : __base(__a)
1287{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001288#if _LIBCPP_DEBUG_LEVEL >= 2
1289 __get_db()->__insert_c(this);
1290#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001291 if (__il.size() > 0)
1292 {
1293 allocate(__il.size());
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001294 __construct_at_end(__il.begin(), __il.end(), __il.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001295 }
1296}
1297
Howard Hinnant33711792011-08-12 21:56:02 +00001298#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1299
Howard Hinnantc51e1022010-05-11 19:42:16 +00001300template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001301inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001302vector<_Tp, _Allocator>&
1303vector<_Tp, _Allocator>::operator=(vector&& __x)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001304 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001305{
1306 __move_assign(__x, integral_constant<bool,
1307 __alloc_traits::propagate_on_container_move_assignment::value>());
1308 return *this;
1309}
1310
1311template <class _Tp, class _Allocator>
1312void
1313vector<_Tp, _Allocator>::__move_assign(vector& __c, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001314 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001315{
1316 if (__base::__alloc() != __c.__alloc())
1317 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001318 typedef move_iterator<iterator> _Ip;
1319 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001320 }
1321 else
1322 __move_assign(__c, true_type());
1323}
1324
1325template <class _Tp, class _Allocator>
1326void
1327vector<_Tp, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00001328 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001329{
1330 deallocate();
Marshall Clow136d45c2014-07-21 15:11:13 +00001331 __base::__move_assign_alloc(__c); // this can throw
Howard Hinnantc51e1022010-05-11 19:42:16 +00001332 this->__begin_ = __c.__begin_;
1333 this->__end_ = __c.__end_;
1334 this->__end_cap() = __c.__end_cap();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001335 __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
Howard Hinnanta1e60cd2011-09-19 16:34:29 +00001336#if _LIBCPP_DEBUG_LEVEL >= 2
1337 __get_db()->swap(this, &__c);
1338#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001339}
1340
Howard Hinnant74279a52010-09-04 23:28:19 +00001341#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001342
1343template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001344inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001345vector<_Tp, _Allocator>&
1346vector<_Tp, _Allocator>::operator=(const vector& __x)
1347{
1348 if (this != &__x)
1349 {
1350 __base::__copy_assign_alloc(__x);
1351 assign(__x.__begin_, __x.__end_);
1352 }
1353 return *this;
1354}
1355
1356template <class _Tp, class _Allocator>
1357template <class _InputIterator>
1358typename enable_if
1359<
1360 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001361 !__is_forward_iterator<_InputIterator>::value &&
1362 is_constructible<
1363 _Tp,
1364 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001365 void
1366>::type
1367vector<_Tp, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
1368{
1369 clear();
1370 for (; __first != __last; ++__first)
1371 push_back(*__first);
1372}
1373
1374template <class _Tp, class _Allocator>
1375template <class _ForwardIterator>
1376typename enable_if
1377<
Howard Hinnant88010252013-03-28 17:44:32 +00001378 __is_forward_iterator<_ForwardIterator>::value &&
1379 is_constructible<
1380 _Tp,
1381 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001382 void
1383>::type
1384vector<_Tp, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
1385{
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001386 size_type __new_size = static_cast<size_type>(_VSTD::distance(__first, __last));
1387 if (__new_size <= capacity())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001388 {
1389 _ForwardIterator __mid = __last;
1390 bool __growing = false;
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001391 if (__new_size > size())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001392 {
1393 __growing = true;
1394 __mid = __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001395 _VSTD::advance(__mid, size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001396 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001397 pointer __m = _VSTD::copy(__first, __mid, this->__begin_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001398 if (__growing)
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001399 __construct_at_end(__mid, __last, __new_size - size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001400 else
1401 this->__destruct_at_end(__m);
1402 }
1403 else
1404 {
1405 deallocate();
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001406 allocate(__recommend(__new_size));
1407 __construct_at_end(__first, __last, __new_size);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001408 }
Eric Fiselier69c51982016-12-28 06:06:09 +00001409 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001410}
1411
1412template <class _Tp, class _Allocator>
1413void
1414vector<_Tp, _Allocator>::assign(size_type __n, const_reference __u)
1415{
1416 if (__n <= capacity())
1417 {
1418 size_type __s = size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001419 _VSTD::fill_n(this->__begin_, _VSTD::min(__n, __s), __u);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001420 if (__n > __s)
1421 __construct_at_end(__n - __s, __u);
1422 else
Howard Hinnant155c2af2010-05-24 17:49:41 +00001423 this->__destruct_at_end(this->__begin_ + __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001424 }
1425 else
1426 {
1427 deallocate();
1428 allocate(__recommend(static_cast<size_type>(__n)));
1429 __construct_at_end(__n, __u);
1430 }
Eric Fiselier69c51982016-12-28 06:06:09 +00001431 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001432}
1433
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001434template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001435inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001436typename vector<_Tp, _Allocator>::iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001437vector<_Tp, _Allocator>::__make_iter(pointer __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001438{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001439#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001440 return iterator(this, __p);
1441#else
1442 return iterator(__p);
1443#endif
1444}
1445
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001446template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001447inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001448typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001449vector<_Tp, _Allocator>::__make_iter(const_pointer __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001450{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001451#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001452 return const_iterator(this, __p);
1453#else
1454 return const_iterator(__p);
1455#endif
1456}
1457
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001458template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001459inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001460typename vector<_Tp, _Allocator>::iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001461vector<_Tp, _Allocator>::begin() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001462{
1463 return __make_iter(this->__begin_);
1464}
1465
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001466template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001467inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001468typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001469vector<_Tp, _Allocator>::begin() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001470{
1471 return __make_iter(this->__begin_);
1472}
1473
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001474template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001475inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001476typename vector<_Tp, _Allocator>::iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001477vector<_Tp, _Allocator>::end() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001478{
1479 return __make_iter(this->__end_);
1480}
1481
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001482template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001483inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001484typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001485vector<_Tp, _Allocator>::end() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001486{
1487 return __make_iter(this->__end_);
1488}
1489
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001490template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001491inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001492typename vector<_Tp, _Allocator>::reference
1493vector<_Tp, _Allocator>::operator[](size_type __n)
1494{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001495 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001496 return this->__begin_[__n];
1497}
1498
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001499template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001500inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001501typename vector<_Tp, _Allocator>::const_reference
1502vector<_Tp, _Allocator>::operator[](size_type __n) const
1503{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001504 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001505 return this->__begin_[__n];
1506}
1507
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001508template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001509typename vector<_Tp, _Allocator>::reference
1510vector<_Tp, _Allocator>::at(size_type __n)
1511{
1512 if (__n >= size())
1513 this->__throw_out_of_range();
1514 return this->__begin_[__n];
1515}
1516
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001517template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001518typename vector<_Tp, _Allocator>::const_reference
1519vector<_Tp, _Allocator>::at(size_type __n) const
1520{
1521 if (__n >= size())
1522 this->__throw_out_of_range();
1523 return this->__begin_[__n];
1524}
1525
1526template <class _Tp, class _Allocator>
1527void
1528vector<_Tp, _Allocator>::reserve(size_type __n)
1529{
1530 if (__n > capacity())
1531 {
1532 allocator_type& __a = this->__alloc();
Howard Hinnant1c936782011-06-03 19:40:40 +00001533 __split_buffer<value_type, allocator_type&> __v(__n, size(), __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001534 __swap_out_circular_buffer(__v);
1535 }
1536}
1537
1538template <class _Tp, class _Allocator>
1539void
Howard Hinnant1c936782011-06-03 19:40:40 +00001540vector<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001541{
1542 if (capacity() > size())
1543 {
1544#ifndef _LIBCPP_NO_EXCEPTIONS
1545 try
1546 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001547#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001548 allocator_type& __a = this->__alloc();
Howard Hinnant1c936782011-06-03 19:40:40 +00001549 __split_buffer<value_type, allocator_type&> __v(size(), size(), __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001550 __swap_out_circular_buffer(__v);
1551#ifndef _LIBCPP_NO_EXCEPTIONS
1552 }
1553 catch (...)
1554 {
1555 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001556#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001557 }
1558}
1559
1560template <class _Tp, class _Allocator>
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001561template <class _Up>
1562void
1563#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1564vector<_Tp, _Allocator>::__push_back_slow_path(_Up&& __x)
1565#else
1566vector<_Tp, _Allocator>::__push_back_slow_path(_Up& __x)
1567#endif
1568{
1569 allocator_type& __a = this->__alloc();
1570 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1571 // __v.push_back(_VSTD::forward<_Up>(__x));
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001572 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), _VSTD::forward<_Up>(__x));
1573 __v.__end_++;
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001574 __swap_out_circular_buffer(__v);
1575}
1576
1577template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001578inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001579void
1580vector<_Tp, _Allocator>::push_back(const_reference __x)
1581{
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001582 if (this->__end_ != this->__end_cap())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001583 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001584 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001585 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001586 _VSTD::__to_raw_pointer(this->__end_), __x);
Kostya Serebryany4963c252014-09-02 23:43:38 +00001587 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001588 ++this->__end_;
1589 }
1590 else
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001591 __push_back_slow_path(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001592}
1593
Howard Hinnant74279a52010-09-04 23:28:19 +00001594#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001595
1596template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001597inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001598void
1599vector<_Tp, _Allocator>::push_back(value_type&& __x)
1600{
1601 if (this->__end_ < this->__end_cap())
1602 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001603 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001604 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001605 _VSTD::__to_raw_pointer(this->__end_),
1606 _VSTD::move(__x));
Kostya Serebryany4963c252014-09-02 23:43:38 +00001607 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001608 ++this->__end_;
1609 }
1610 else
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001611 __push_back_slow_path(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001612}
1613
Howard Hinnant74279a52010-09-04 23:28:19 +00001614#ifndef _LIBCPP_HAS_NO_VARIADICS
1615
Howard Hinnantc51e1022010-05-11 19:42:16 +00001616template <class _Tp, class _Allocator>
1617template <class... _Args>
1618void
Howard Hinnantb6c49562012-02-26 15:30:12 +00001619vector<_Tp, _Allocator>::__emplace_back_slow_path(_Args&&... __args)
1620{
1621 allocator_type& __a = this->__alloc();
1622 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1623// __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001624 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), _VSTD::forward<_Args>(__args)...);
1625 __v.__end_++;
Howard Hinnantb6c49562012-02-26 15:30:12 +00001626 __swap_out_circular_buffer(__v);
1627}
1628
1629template <class _Tp, class _Allocator>
1630template <class... _Args>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001631inline
Marshall Clowea52cc42017-01-24 23:09:12 +00001632#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001633typename vector<_Tp, _Allocator>::reference
Marshall Clowea52cc42017-01-24 23:09:12 +00001634#else
1635void
1636#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001637vector<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1638{
1639 if (this->__end_ < this->__end_cap())
1640 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001641 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001642 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001643 _VSTD::__to_raw_pointer(this->__end_),
1644 _VSTD::forward<_Args>(__args)...);
Kostya Serebryany4963c252014-09-02 23:43:38 +00001645 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001646 ++this->__end_;
1647 }
1648 else
Howard Hinnantb6c49562012-02-26 15:30:12 +00001649 __emplace_back_slow_path(_VSTD::forward<_Args>(__args)...);
Marshall Clowea52cc42017-01-24 23:09:12 +00001650#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001651 return this->back();
Marshall Clowea52cc42017-01-24 23:09:12 +00001652#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001653}
1654
Howard Hinnant74279a52010-09-04 23:28:19 +00001655#endif // _LIBCPP_HAS_NO_VARIADICS
1656#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001657
1658template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001659inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001660void
1661vector<_Tp, _Allocator>::pop_back()
1662{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001663 _LIBCPP_ASSERT(!empty(), "vector::pop_back called for empty vector");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001664 this->__destruct_at_end(this->__end_ - 1);
1665}
1666
1667template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001668inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001669typename vector<_Tp, _Allocator>::iterator
1670vector<_Tp, _Allocator>::erase(const_iterator __position)
1671{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001672#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001673 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1674 "vector::erase(iterator) called with an iterator not"
1675 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001676#endif
Howard Hinnant58d52d52013-03-25 22:12:26 +00001677 _LIBCPP_ASSERT(__position != end(),
1678 "vector::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnant76053d72013-06-27 19:35:32 +00001679 difference_type __ps = __position - cbegin();
1680 pointer __p = this->__begin_ + __ps;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001681 this->__destruct_at_end(_VSTD::move(__p + 1, this->__end_, __p));
Eric Fiselier69c51982016-12-28 06:06:09 +00001682 this->__invalidate_iterators_past(__p-1);
1683 iterator __r = __make_iter(__p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001684 return __r;
1685}
1686
1687template <class _Tp, class _Allocator>
1688typename vector<_Tp, _Allocator>::iterator
1689vector<_Tp, _Allocator>::erase(const_iterator __first, const_iterator __last)
1690{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001691#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001692 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
1693 "vector::erase(iterator, iterator) called with an iterator not"
1694 " referring to this vector");
Eric Fiselier69c51982016-12-28 06:06:09 +00001695 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__last) == this,
1696 "vector::erase(iterator, iterator) called with an iterator not"
1697 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001698#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001699 _LIBCPP_ASSERT(__first <= __last, "vector::erase(first, last) called with invalid range");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001700 pointer __p = this->__begin_ + (__first - begin());
Eric Fiselier69c51982016-12-28 06:06:09 +00001701 if (__first != __last) {
Howard Hinnantc580cc32013-04-18 15:02:57 +00001702 this->__destruct_at_end(_VSTD::move(__p + (__last - __first), this->__end_, __p));
Eric Fiselier69c51982016-12-28 06:06:09 +00001703 this->__invalidate_iterators_past(__p - 1);
1704 }
1705 iterator __r = __make_iter(__p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001706 return __r;
1707}
1708
1709template <class _Tp, class _Allocator>
1710void
1711vector<_Tp, _Allocator>::__move_range(pointer __from_s, pointer __from_e, pointer __to)
1712{
1713 pointer __old_last = this->__end_;
1714 difference_type __n = __old_last - __to;
1715 for (pointer __i = __from_s + __n; __i < __from_e; ++__i, ++this->__end_)
1716 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001717 _VSTD::__to_raw_pointer(this->__end_),
1718 _VSTD::move(*__i));
1719 _VSTD::move_backward(__from_s, __from_s + __n, __old_last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001720}
1721
1722template <class _Tp, class _Allocator>
1723typename vector<_Tp, _Allocator>::iterator
1724vector<_Tp, _Allocator>::insert(const_iterator __position, const_reference __x)
1725{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001726#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001727 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1728 "vector::insert(iterator, x) called with an iterator not"
1729 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001730#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001731 pointer __p = this->__begin_ + (__position - begin());
1732 if (this->__end_ < this->__end_cap())
1733 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001734 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001735 if (__p == this->__end_)
1736 {
1737 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001738 _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001739 ++this->__end_;
1740 }
1741 else
1742 {
1743 __move_range(__p, this->__end_, __p + 1);
1744 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1745 if (__p <= __xr && __xr < this->__end_)
1746 ++__xr;
1747 *__p = *__xr;
1748 }
Kostya Serebryany4963c252014-09-02 23:43:38 +00001749 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001750 }
1751 else
1752 {
1753 allocator_type& __a = this->__alloc();
1754 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
1755 __v.push_back(__x);
1756 __p = __swap_out_circular_buffer(__v, __p);
1757 }
1758 return __make_iter(__p);
1759}
1760
Howard Hinnant74279a52010-09-04 23:28:19 +00001761#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001762
1763template <class _Tp, class _Allocator>
1764typename vector<_Tp, _Allocator>::iterator
1765vector<_Tp, _Allocator>::insert(const_iterator __position, value_type&& __x)
1766{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001767#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001768 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1769 "vector::insert(iterator, x) called with an iterator not"
1770 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001771#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001772 pointer __p = this->__begin_ + (__position - begin());
1773 if (this->__end_ < this->__end_cap())
1774 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001775 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001776 if (__p == this->__end_)
1777 {
1778 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001779 _VSTD::__to_raw_pointer(this->__end_),
1780 _VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001781 ++this->__end_;
1782 }
1783 else
1784 {
1785 __move_range(__p, this->__end_, __p + 1);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001786 *__p = _VSTD::move(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001787 }
Kostya Serebryany4963c252014-09-02 23:43:38 +00001788 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001789 }
1790 else
1791 {
1792 allocator_type& __a = this->__alloc();
1793 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001794 __v.push_back(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001795 __p = __swap_out_circular_buffer(__v, __p);
1796 }
1797 return __make_iter(__p);
1798}
1799
Howard Hinnant74279a52010-09-04 23:28:19 +00001800#ifndef _LIBCPP_HAS_NO_VARIADICS
1801
Howard Hinnantc51e1022010-05-11 19:42:16 +00001802template <class _Tp, class _Allocator>
1803template <class... _Args>
1804typename vector<_Tp, _Allocator>::iterator
1805vector<_Tp, _Allocator>::emplace(const_iterator __position, _Args&&... __args)
1806{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001807#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001808 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1809 "vector::emplace(iterator, x) called with an iterator not"
1810 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001811#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001812 pointer __p = this->__begin_ + (__position - begin());
1813 if (this->__end_ < this->__end_cap())
1814 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001815 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001816 if (__p == this->__end_)
1817 {
1818 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001819 _VSTD::__to_raw_pointer(this->__end_),
1820 _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001821 ++this->__end_;
1822 }
1823 else
1824 {
Marshall Clowa591b9a2016-07-11 21:38:08 +00001825 __temp_value<value_type, _Allocator> __tmp(this->__alloc(), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001826 __move_range(__p, this->__end_, __p + 1);
Marshall Clowa591b9a2016-07-11 21:38:08 +00001827 *__p = _VSTD::move(__tmp.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001828 }
Kostya Serebryany4963c252014-09-02 23:43:38 +00001829 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001830 }
1831 else
1832 {
1833 allocator_type& __a = this->__alloc();
1834 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001835 __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001836 __p = __swap_out_circular_buffer(__v, __p);
1837 }
1838 return __make_iter(__p);
1839}
1840
Howard Hinnant74279a52010-09-04 23:28:19 +00001841#endif // _LIBCPP_HAS_NO_VARIADICS
1842#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001843
1844template <class _Tp, class _Allocator>
1845typename vector<_Tp, _Allocator>::iterator
1846vector<_Tp, _Allocator>::insert(const_iterator __position, size_type __n, const_reference __x)
1847{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001848#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001849 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1850 "vector::insert(iterator, n, x) called with an iterator not"
1851 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001852#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001853 pointer __p = this->__begin_ + (__position - begin());
1854 if (__n > 0)
1855 {
1856 if (__n <= static_cast<size_type>(this->__end_cap() - this->__end_))
1857 {
1858 size_type __old_n = __n;
1859 pointer __old_last = this->__end_;
1860 if (__n > static_cast<size_type>(this->__end_ - __p))
1861 {
1862 size_type __cx = __n - (this->__end_ - __p);
1863 __construct_at_end(__cx, __x);
1864 __n -= __cx;
1865 }
1866 if (__n > 0)
1867 {
Eric Fiselier2a649652014-11-14 18:28:36 +00001868 __RAII_IncreaseAnnotator __annotator(*this, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001869 __move_range(__p, __old_last, __p + __old_n);
Kostya Serebryany4963c252014-09-02 23:43:38 +00001870 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001871 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1872 if (__p <= __xr && __xr < this->__end_)
1873 __xr += __old_n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001874 _VSTD::fill_n(__p, __n, *__xr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001875 }
1876 }
1877 else
1878 {
1879 allocator_type& __a = this->__alloc();
1880 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1881 __v.__construct_at_end(__n, __x);
1882 __p = __swap_out_circular_buffer(__v, __p);
1883 }
1884 }
1885 return __make_iter(__p);
1886}
1887
1888template <class _Tp, class _Allocator>
1889template <class _InputIterator>
1890typename enable_if
1891<
1892 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001893 !__is_forward_iterator<_InputIterator>::value &&
1894 is_constructible<
1895 _Tp,
1896 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001897 typename vector<_Tp, _Allocator>::iterator
1898>::type
1899vector<_Tp, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
1900{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001901#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001902 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1903 "vector::insert(iterator, range) called with an iterator not"
1904 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001905#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001906 difference_type __off = __position - begin();
1907 pointer __p = this->__begin_ + __off;
1908 allocator_type& __a = this->__alloc();
1909 pointer __old_last = this->__end_;
1910 for (; this->__end_ != this->__end_cap() && __first != __last; ++__first)
1911 {
Eric Fiselier489fd502015-07-18 18:22:12 +00001912 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001913 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001914 *__first);
1915 ++this->__end_;
Eric Fiselier489fd502015-07-18 18:22:12 +00001916 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001917 }
1918 __split_buffer<value_type, allocator_type&> __v(__a);
1919 if (__first != __last)
1920 {
1921#ifndef _LIBCPP_NO_EXCEPTIONS
1922 try
1923 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001924#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001925 __v.__construct_at_end(__first, __last);
1926 difference_type __old_size = __old_last - this->__begin_;
1927 difference_type __old_p = __p - this->__begin_;
1928 reserve(__recommend(size() + __v.size()));
1929 __p = this->__begin_ + __old_p;
1930 __old_last = this->__begin_ + __old_size;
1931#ifndef _LIBCPP_NO_EXCEPTIONS
1932 }
1933 catch (...)
1934 {
1935 erase(__make_iter(__old_last), end());
1936 throw;
1937 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001938#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001939 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001940 __p = _VSTD::rotate(__p, __old_last, this->__end_);
Howard Hinnant9cd22302011-09-16 18:41:29 +00001941 insert(__make_iter(__p), make_move_iterator(__v.begin()),
1942 make_move_iterator(__v.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001943 return begin() + __off;
1944}
1945
1946template <class _Tp, class _Allocator>
1947template <class _ForwardIterator>
1948typename enable_if
1949<
Howard Hinnant88010252013-03-28 17:44:32 +00001950 __is_forward_iterator<_ForwardIterator>::value &&
1951 is_constructible<
1952 _Tp,
1953 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001954 typename vector<_Tp, _Allocator>::iterator
1955>::type
1956vector<_Tp, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
1957{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001958#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001959 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1960 "vector::insert(iterator, range) called with an iterator not"
1961 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001962#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001963 pointer __p = this->__begin_ + (__position - begin());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001964 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001965 if (__n > 0)
1966 {
1967 if (__n <= this->__end_cap() - this->__end_)
1968 {
1969 size_type __old_n = __n;
1970 pointer __old_last = this->__end_;
1971 _ForwardIterator __m = __last;
1972 difference_type __dx = this->__end_ - __p;
1973 if (__n > __dx)
1974 {
1975 __m = __first;
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001976 difference_type __diff = this->__end_ - __p;
1977 _VSTD::advance(__m, __diff);
1978 __construct_at_end(__m, __last, __n - __diff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001979 __n = __dx;
1980 }
1981 if (__n > 0)
1982 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001983 __RAII_IncreaseAnnotator __annotator(*this, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001984 __move_range(__p, __old_last, __p + __old_n);
Kostya Serebryany4963c252014-09-02 23:43:38 +00001985 __annotator.__done();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001986 _VSTD::copy(__first, __m, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001987 }
1988 }
1989 else
1990 {
1991 allocator_type& __a = this->__alloc();
1992 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1993 __v.__construct_at_end(__first, __last);
1994 __p = __swap_out_circular_buffer(__v, __p);
1995 }
1996 }
1997 return __make_iter(__p);
1998}
1999
2000template <class _Tp, class _Allocator>
2001void
2002vector<_Tp, _Allocator>::resize(size_type __sz)
2003{
2004 size_type __cs = size();
2005 if (__cs < __sz)
2006 this->__append(__sz - __cs);
2007 else if (__cs > __sz)
2008 this->__destruct_at_end(this->__begin_ + __sz);
2009}
2010
2011template <class _Tp, class _Allocator>
2012void
2013vector<_Tp, _Allocator>::resize(size_type __sz, const_reference __x)
2014{
2015 size_type __cs = size();
2016 if (__cs < __sz)
2017 this->__append(__sz - __cs, __x);
2018 else if (__cs > __sz)
2019 this->__destruct_at_end(this->__begin_ + __sz);
2020}
2021
2022template <class _Tp, class _Allocator>
2023void
2024vector<_Tp, _Allocator>::swap(vector& __x)
Marshall Clow8982dcd2015-07-13 20:04:56 +00002025#if _LIBCPP_STD_VER >= 14
Eric Fiselier69c51982016-12-28 06:06:09 +00002026 _NOEXCEPT_DEBUG
Marshall Clow8982dcd2015-07-13 20:04:56 +00002027#else
Eric Fiselier69c51982016-12-28 06:06:09 +00002028 _NOEXCEPT_DEBUG_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00002029 __is_nothrow_swappable<allocator_type>::value)
2030#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002031{
Howard Hinnant27e0e772011-09-14 18:33:51 +00002032 _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
2033 this->__alloc() == __x.__alloc(),
2034 "vector::swap: Either propagate_on_container_swap must be true"
2035 " or the allocators must compare equal");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002036 _VSTD::swap(this->__begin_, __x.__begin_);
2037 _VSTD::swap(this->__end_, __x.__end_);
2038 _VSTD::swap(this->__end_cap(), __x.__end_cap());
Marshall Clow8982dcd2015-07-13 20:04:56 +00002039 __swap_allocator(this->__alloc(), __x.__alloc(),
2040 integral_constant<bool,__alloc_traits::propagate_on_container_swap::value>());
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002041#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002042 __get_db()->swap(this, &__x);
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002043#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00002044}
2045
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002046template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002047bool
2048vector<_Tp, _Allocator>::__invariants() const
2049{
Howard Hinnant76053d72013-06-27 19:35:32 +00002050 if (this->__begin_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002051 {
Howard Hinnant76053d72013-06-27 19:35:32 +00002052 if (this->__end_ != nullptr || this->__end_cap() != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002053 return false;
2054 }
2055 else
2056 {
2057 if (this->__begin_ > this->__end_)
2058 return false;
2059 if (this->__begin_ == this->__end_cap())
2060 return false;
2061 if (this->__end_ > this->__end_cap())
2062 return false;
2063 }
2064 return true;
2065}
2066
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002067#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002068
Howard Hinnantc51e1022010-05-11 19:42:16 +00002069template <class _Tp, class _Allocator>
Howard Hinnant27e0e772011-09-14 18:33:51 +00002070bool
2071vector<_Tp, _Allocator>::__dereferenceable(const const_iterator* __i) const
2072{
2073 return this->__begin_ <= __i->base() && __i->base() < this->__end_;
2074}
2075
2076template <class _Tp, class _Allocator>
2077bool
2078vector<_Tp, _Allocator>::__decrementable(const const_iterator* __i) const
2079{
2080 return this->__begin_ < __i->base() && __i->base() <= this->__end_;
2081}
2082
2083template <class _Tp, class _Allocator>
2084bool
2085vector<_Tp, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
2086{
2087 const_pointer __p = __i->base() + __n;
2088 return this->__begin_ <= __p && __p <= this->__end_;
2089}
2090
2091template <class _Tp, class _Allocator>
2092bool
2093vector<_Tp, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
2094{
2095 const_pointer __p = __i->base() + __n;
2096 return this->__begin_ <= __p && __p < this->__end_;
2097}
2098
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002099#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002100
2101template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002102inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002103void
2104vector<_Tp, _Allocator>::__invalidate_all_iterators()
2105{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002106#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002107 __get_db()->__invalidate_all(this);
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002108#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00002109}
2110
Eric Fiselier69c51982016-12-28 06:06:09 +00002111
2112template <class _Tp, class _Allocator>
2113inline _LIBCPP_INLINE_VISIBILITY
2114void
2115vector<_Tp, _Allocator>::__invalidate_iterators_past(pointer __new_last) {
2116#if _LIBCPP_DEBUG_LEVEL >= 2
2117 __c_node* __c = __get_db()->__find_c_and_lock(this);
2118 for (__i_node** __p = __c->end_; __p != __c->beg_; ) {
2119 --__p;
2120 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
2121 if (__i->base() > __new_last) {
2122 (*__p)->__c_ = nullptr;
2123 if (--__c->end_ != __p)
2124 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
2125 }
2126 }
2127 __get_db()->unlock();
2128#else
2129 ((void)__new_last);
2130#endif
2131}
2132
Howard Hinnantc51e1022010-05-11 19:42:16 +00002133// vector<bool>
2134
2135template <class _Allocator> class vector<bool, _Allocator>;
2136
2137template <class _Allocator> struct hash<vector<bool, _Allocator> >;
2138
2139template <class _Allocator>
Howard Hinnantb4ef6482011-07-02 20:33:23 +00002140struct __has_storage_type<vector<bool, _Allocator> >
2141{
2142 static const bool value = true;
2143};
2144
2145template <class _Allocator>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002146class _LIBCPP_TEMPLATE_VIS vector<bool, _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002147 : private __vector_base_common<true>
2148{
2149public:
2150 typedef vector __self;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002151 typedef bool value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002152 typedef _Allocator allocator_type;
2153 typedef allocator_traits<allocator_type> __alloc_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002154 typedef typename __alloc_traits::size_type size_type;
2155 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant896b7622012-05-07 16:50:38 +00002156 typedef size_type __storage_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002157 typedef __bit_iterator<vector, false> pointer;
2158 typedef __bit_iterator<vector, true> const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002159 typedef pointer iterator;
2160 typedef const_pointer const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002161 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
2162 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002163
2164private:
Marshall Clow940e01c2015-04-07 05:21:38 +00002165 typedef typename __rebind_alloc_helper<__alloc_traits, __storage_type>::type __storage_allocator;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002166 typedef allocator_traits<__storage_allocator> __storage_traits;
2167 typedef typename __storage_traits::pointer __storage_pointer;
2168 typedef typename __storage_traits::const_pointer __const_storage_pointer;
2169
2170 __storage_pointer __begin_;
2171 size_type __size_;
2172 __compressed_pair<size_type, __storage_allocator> __cap_alloc_;
Howard Hinnant71d1c192011-07-09 15:50:42 +00002173public:
Howard Hinnantb4ef6482011-07-02 20:33:23 +00002174 typedef __bit_reference<vector> reference;
2175 typedef __bit_const_reference<vector> const_reference;
Howard Hinnant71d1c192011-07-09 15:50:42 +00002176private:
Howard Hinnant1c936782011-06-03 19:40:40 +00002177 _LIBCPP_INLINE_VISIBILITY
2178 size_type& __cap() _NOEXCEPT
2179 {return __cap_alloc_.first();}
2180 _LIBCPP_INLINE_VISIBILITY
2181 const size_type& __cap() const _NOEXCEPT
2182 {return __cap_alloc_.first();}
2183 _LIBCPP_INLINE_VISIBILITY
2184 __storage_allocator& __alloc() _NOEXCEPT
2185 {return __cap_alloc_.second();}
2186 _LIBCPP_INLINE_VISIBILITY
2187 const __storage_allocator& __alloc() const _NOEXCEPT
2188 {return __cap_alloc_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002189
2190 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
2191
Howard Hinnant1c936782011-06-03 19:40:40 +00002192 _LIBCPP_INLINE_VISIBILITY
2193 static size_type __internal_cap_to_external(size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002194 {return __n * __bits_per_word;}
Howard Hinnant1c936782011-06-03 19:40:40 +00002195 _LIBCPP_INLINE_VISIBILITY
2196 static size_type __external_cap_to_internal(size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002197 {return (__n - 1) / __bits_per_word + 1;}
2198
2199public:
Howard Hinnant1c936782011-06-03 19:40:40 +00002200 _LIBCPP_INLINE_VISIBILITY
Marshall Clowe546cbb2015-06-04 02:05:41 +00002201 vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clow8f282f42015-06-04 00:10:20 +00002202
2203 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
2204#if _LIBCPP_STD_VER <= 14
2205 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
2206#else
2207 _NOEXCEPT;
2208#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002209 ~vector();
2210 explicit vector(size_type __n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00002211#if _LIBCPP_STD_VER > 11
2212 explicit vector(size_type __n, const allocator_type& __a);
2213#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002214 vector(size_type __n, const value_type& __v);
2215 vector(size_type __n, const value_type& __v, const allocator_type& __a);
2216 template <class _InputIterator>
2217 vector(_InputIterator __first, _InputIterator __last,
2218 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2219 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2220 template <class _InputIterator>
2221 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2222 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2223 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2224 template <class _ForwardIterator>
2225 vector(_ForwardIterator __first, _ForwardIterator __last,
2226 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2227 template <class _ForwardIterator>
2228 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2229 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2230
2231 vector(const vector& __v);
2232 vector(const vector& __v, const allocator_type& __a);
2233 vector& operator=(const vector& __v);
Howard Hinnant33711792011-08-12 21:56:02 +00002234#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002235 vector(initializer_list<value_type> __il);
2236 vector(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnant33711792011-08-12 21:56:02 +00002237#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002238
Howard Hinnant74279a52010-09-04 23:28:19 +00002239#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant1c936782011-06-03 19:40:40 +00002240 _LIBCPP_INLINE_VISIBILITY
2241 vector(vector&& __v)
Marshall Clowe5108202015-07-14 14:46:32 +00002242#if _LIBCPP_STD_VER > 14
2243 _NOEXCEPT;
2244#else
Howard Hinnant1c936782011-06-03 19:40:40 +00002245 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowe5108202015-07-14 14:46:32 +00002246#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002247 vector(vector&& __v, const allocator_type& __a);
Howard Hinnant1c936782011-06-03 19:40:40 +00002248 _LIBCPP_INLINE_VISIBILITY
2249 vector& operator=(vector&& __v)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002250 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Howard Hinnant74279a52010-09-04 23:28:19 +00002251#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant33711792011-08-12 21:56:02 +00002252#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002253 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002254 vector& operator=(initializer_list<value_type> __il)
2255 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnant33711792011-08-12 21:56:02 +00002256#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002257
2258 template <class _InputIterator>
2259 typename enable_if
2260 <
2261 __is_input_iterator<_InputIterator>::value &&
2262 !__is_forward_iterator<_InputIterator>::value,
2263 void
2264 >::type
2265 assign(_InputIterator __first, _InputIterator __last);
2266 template <class _ForwardIterator>
2267 typename enable_if
2268 <
2269 __is_forward_iterator<_ForwardIterator>::value,
2270 void
2271 >::type
2272 assign(_ForwardIterator __first, _ForwardIterator __last);
2273
2274 void assign(size_type __n, const value_type& __x);
Howard Hinnant33711792011-08-12 21:56:02 +00002275#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002276 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002277 void assign(initializer_list<value_type> __il)
2278 {assign(__il.begin(), __il.end());}
Howard Hinnant33711792011-08-12 21:56:02 +00002279#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002280
Howard Hinnant1c936782011-06-03 19:40:40 +00002281 _LIBCPP_INLINE_VISIBILITY allocator_type get_allocator() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002282 {return allocator_type(this->__alloc());}
2283
Howard Hinnant1c936782011-06-03 19:40:40 +00002284 size_type max_size() const _NOEXCEPT;
2285 _LIBCPP_INLINE_VISIBILITY
2286 size_type capacity() const _NOEXCEPT
2287 {return __internal_cap_to_external(__cap());}
2288 _LIBCPP_INLINE_VISIBILITY
2289 size_type size() const _NOEXCEPT
2290 {return __size_;}
2291 _LIBCPP_INLINE_VISIBILITY
2292 bool empty() const _NOEXCEPT
2293 {return __size_ == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002294 void reserve(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +00002295 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002296
Howard Hinnant1c936782011-06-03 19:40:40 +00002297 _LIBCPP_INLINE_VISIBILITY
2298 iterator begin() _NOEXCEPT
2299 {return __make_iter(0);}
2300 _LIBCPP_INLINE_VISIBILITY
2301 const_iterator begin() const _NOEXCEPT
2302 {return __make_iter(0);}
2303 _LIBCPP_INLINE_VISIBILITY
2304 iterator end() _NOEXCEPT
2305 {return __make_iter(__size_);}
2306 _LIBCPP_INLINE_VISIBILITY
2307 const_iterator end() const _NOEXCEPT
2308 {return __make_iter(__size_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002309
Howard Hinnant1c936782011-06-03 19:40:40 +00002310 _LIBCPP_INLINE_VISIBILITY
2311 reverse_iterator rbegin() _NOEXCEPT
2312 {return reverse_iterator(end());}
2313 _LIBCPP_INLINE_VISIBILITY
2314 const_reverse_iterator rbegin() const _NOEXCEPT
2315 {return const_reverse_iterator(end());}
2316 _LIBCPP_INLINE_VISIBILITY
2317 reverse_iterator rend() _NOEXCEPT
2318 {return reverse_iterator(begin());}
2319 _LIBCPP_INLINE_VISIBILITY
2320 const_reverse_iterator rend() const _NOEXCEPT
2321 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002322
Howard Hinnant1c936782011-06-03 19:40:40 +00002323 _LIBCPP_INLINE_VISIBILITY
2324 const_iterator cbegin() const _NOEXCEPT
2325 {return __make_iter(0);}
2326 _LIBCPP_INLINE_VISIBILITY
2327 const_iterator cend() const _NOEXCEPT
2328 {return __make_iter(__size_);}
2329 _LIBCPP_INLINE_VISIBILITY
2330 const_reverse_iterator crbegin() const _NOEXCEPT
2331 {return rbegin();}
2332 _LIBCPP_INLINE_VISIBILITY
2333 const_reverse_iterator crend() const _NOEXCEPT
2334 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002335
2336 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) {return __make_ref(__n);}
2337 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const {return __make_ref(__n);}
2338 reference at(size_type __n);
2339 const_reference at(size_type __n) const;
2340
2341 _LIBCPP_INLINE_VISIBILITY reference front() {return __make_ref(0);}
2342 _LIBCPP_INLINE_VISIBILITY const_reference front() const {return __make_ref(0);}
2343 _LIBCPP_INLINE_VISIBILITY reference back() {return __make_ref(__size_ - 1);}
2344 _LIBCPP_INLINE_VISIBILITY const_reference back() const {return __make_ref(__size_ - 1);}
2345
2346 void push_back(const value_type& __x);
Marshall Clowc46bb8e2013-08-13 23:54:12 +00002347#if _LIBCPP_STD_VER > 11
2348 template <class... _Args>
Marshall Clowea52cc42017-01-24 23:09:12 +00002349#if _LIBCPP_STD_VER > 14
2350 _LIBCPP_INLINE_VISIBILITY reference emplace_back(_Args&&... __args)
2351#else
2352 _LIBCPP_INLINE_VISIBILITY void emplace_back(_Args&&... __args)
2353#endif
2354 {
Eric Fiselier34ba5b92016-07-21 03:20:17 +00002355 push_back ( value_type ( _VSTD::forward<_Args>(__args)... ));
Marshall Clowea52cc42017-01-24 23:09:12 +00002356#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00002357 return this->back();
Marshall Clowea52cc42017-01-24 23:09:12 +00002358#endif
Eric Fiselier34ba5b92016-07-21 03:20:17 +00002359 }
Marshall Clowc46bb8e2013-08-13 23:54:12 +00002360#endif
2361
Howard Hinnantc51e1022010-05-11 19:42:16 +00002362 _LIBCPP_INLINE_VISIBILITY void pop_back() {--__size_;}
2363
Marshall Clowc46bb8e2013-08-13 23:54:12 +00002364#if _LIBCPP_STD_VER > 11
2365 template <class... _Args>
2366 _LIBCPP_INLINE_VISIBILITY iterator emplace(const_iterator position, _Args&&... __args)
2367 { return insert ( position, value_type ( _VSTD::forward<_Args>(__args)... )); }
2368#endif
2369
Howard Hinnantc51e1022010-05-11 19:42:16 +00002370 iterator insert(const_iterator __position, const value_type& __x);
2371 iterator insert(const_iterator __position, size_type __n, const value_type& __x);
2372 iterator insert(const_iterator __position, size_type __n, const_reference __x);
2373 template <class _InputIterator>
2374 typename enable_if
2375 <
2376 __is_input_iterator <_InputIterator>::value &&
2377 !__is_forward_iterator<_InputIterator>::value,
2378 iterator
2379 >::type
2380 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
2381 template <class _ForwardIterator>
2382 typename enable_if
2383 <
2384 __is_forward_iterator<_ForwardIterator>::value,
2385 iterator
2386 >::type
2387 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnant33711792011-08-12 21:56:02 +00002388#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002389 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002390 iterator insert(const_iterator __position, initializer_list<value_type> __il)
2391 {return insert(__position, __il.begin(), __il.end());}
Howard Hinnant33711792011-08-12 21:56:02 +00002392#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002393
Howard Hinnantcf823322010-12-17 14:46:43 +00002394 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002395 iterator erase(const_iterator __first, const_iterator __last);
2396
Howard Hinnant1c936782011-06-03 19:40:40 +00002397 _LIBCPP_INLINE_VISIBILITY
2398 void clear() _NOEXCEPT {__size_ = 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002399
Howard Hinnant1c936782011-06-03 19:40:40 +00002400 void swap(vector&)
Marshall Clow8982dcd2015-07-13 20:04:56 +00002401#if _LIBCPP_STD_VER >= 14
2402 _NOEXCEPT;
2403#else
Eric Fiselier69c51982016-12-28 06:06:09 +00002404 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00002405 __is_nothrow_swappable<allocator_type>::value);
2406#endif
Marshall Clow89eb3822016-04-07 14:20:31 +00002407 static void swap(reference __x, reference __y) _NOEXCEPT { _VSTD::swap(__x, __y); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002408
2409 void resize(size_type __sz, value_type __x = false);
Howard Hinnant1c936782011-06-03 19:40:40 +00002410 void flip() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002411
2412 bool __invariants() const;
2413
2414private:
Howard Hinnantcf823322010-12-17 14:46:43 +00002415 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002416 void allocate(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +00002417 void deallocate() _NOEXCEPT;
2418 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantea382952013-08-14 18:00:20 +00002419 static size_type __align_it(size_type __new_size) _NOEXCEPT
Marshall Clow66f34152014-07-28 15:02:42 +00002420 {return __new_size + (__bits_per_word-1) & ~((size_type)__bits_per_word-1);};
Howard Hinnantcf823322010-12-17 14:46:43 +00002421 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
2422 _LIBCPP_INLINE_VISIBILITY void __construct_at_end(size_type __n, bool __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002423 template <class _ForwardIterator>
2424 typename enable_if
2425 <
2426 __is_forward_iterator<_ForwardIterator>::value,
2427 void
2428 >::type
2429 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
2430 void __append(size_type __n, const_reference __x);
Howard Hinnant1c936782011-06-03 19:40:40 +00002431 _LIBCPP_INLINE_VISIBILITY
2432 reference __make_ref(size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002433 {return reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnant1c936782011-06-03 19:40:40 +00002434 _LIBCPP_INLINE_VISIBILITY
2435 const_reference __make_ref(size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002436 {return const_reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnant1c936782011-06-03 19:40:40 +00002437 _LIBCPP_INLINE_VISIBILITY
2438 iterator __make_iter(size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002439 {return iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnant1c936782011-06-03 19:40:40 +00002440 _LIBCPP_INLINE_VISIBILITY
2441 const_iterator __make_iter(size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002442 {return const_iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnant1c936782011-06-03 19:40:40 +00002443 _LIBCPP_INLINE_VISIBILITY
2444 iterator __const_iterator_cast(const_iterator __p) _NOEXCEPT
Howard Hinnant76053d72013-06-27 19:35:32 +00002445 {return begin() + (__p - cbegin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002446
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002447 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002448 void __copy_assign_alloc(const vector& __v)
2449 {__copy_assign_alloc(__v, integral_constant<bool,
2450 __storage_traits::propagate_on_container_copy_assignment::value>());}
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002451 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002452 void __copy_assign_alloc(const vector& __c, true_type)
2453 {
2454 if (__alloc() != __c.__alloc())
2455 deallocate();
2456 __alloc() = __c.__alloc();
2457 }
2458
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002459 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00002460 void __copy_assign_alloc(const vector&, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002461 {}
2462
2463 void __move_assign(vector& __c, false_type);
Howard Hinnant1c936782011-06-03 19:40:40 +00002464 void __move_assign(vector& __c, true_type)
2465 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002466 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002467 void __move_assign_alloc(vector& __c)
Howard Hinnant1c936782011-06-03 19:40:40 +00002468 _NOEXCEPT_(
2469 !__storage_traits::propagate_on_container_move_assignment::value ||
2470 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002471 {__move_assign_alloc(__c, integral_constant<bool,
2472 __storage_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002473 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +00002474 void __move_assign_alloc(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002475 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002476 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002477 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002478 }
2479
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002480 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00002481 void __move_assign_alloc(vector&, false_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002482 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002483 {}
2484
Howard Hinnant1c936782011-06-03 19:40:40 +00002485 size_t __hash_code() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002486
2487 friend class __bit_reference<vector>;
2488 friend class __bit_const_reference<vector>;
2489 friend class __bit_iterator<vector, false>;
2490 friend class __bit_iterator<vector, true>;
Howard Hinnant4210a0f2012-08-17 17:10:18 +00002491 friend struct __bit_array<vector>;
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002492 friend struct _LIBCPP_TEMPLATE_VIS hash<vector>;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002493};
2494
2495template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002496inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002497void
2498vector<bool, _Allocator>::__invalidate_all_iterators()
2499{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002500}
2501
2502// Allocate space for __n objects
2503// throws length_error if __n > max_size()
2504// throws (probably bad_alloc) if memory run out
2505// Precondition: __begin_ == __end_ == __cap() == 0
2506// Precondition: __n > 0
2507// Postcondition: capacity() == __n
2508// Postcondition: size() == 0
2509template <class _Allocator>
2510void
2511vector<bool, _Allocator>::allocate(size_type __n)
2512{
2513 if (__n > max_size())
2514 this->__throw_length_error();
2515 __n = __external_cap_to_internal(__n);
2516 this->__begin_ = __storage_traits::allocate(this->__alloc(), __n);
2517 this->__size_ = 0;
2518 this->__cap() = __n;
2519}
2520
2521template <class _Allocator>
2522void
Howard Hinnant1c936782011-06-03 19:40:40 +00002523vector<bool, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002524{
Howard Hinnant76053d72013-06-27 19:35:32 +00002525 if (this->__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002526 {
2527 __storage_traits::deallocate(this->__alloc(), this->__begin_, __cap());
2528 __invalidate_all_iterators();
Howard Hinnant76053d72013-06-27 19:35:32 +00002529 this->__begin_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002530 this->__size_ = this->__cap() = 0;
2531 }
2532}
2533
2534template <class _Allocator>
2535typename vector<bool, _Allocator>::size_type
Howard Hinnant1c936782011-06-03 19:40:40 +00002536vector<bool, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002537{
2538 size_type __amax = __storage_traits::max_size(__alloc());
2539 size_type __nmax = numeric_limits<size_type>::max() / 2; // end() >= begin(), always
2540 if (__nmax / __bits_per_word <= __amax)
2541 return __nmax;
2542 return __internal_cap_to_external(__amax);
2543}
2544
2545// Precondition: __new_size > capacity()
2546template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002547inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002548typename vector<bool, _Allocator>::size_type
2549vector<bool, _Allocator>::__recommend(size_type __new_size) const
2550{
2551 const size_type __ms = max_size();
2552 if (__new_size > __ms)
2553 this->__throw_length_error();
2554 const size_type __cap = capacity();
2555 if (__cap >= __ms / 2)
2556 return __ms;
Howard Hinnantea382952013-08-14 18:00:20 +00002557 return _VSTD::max(2*__cap, __align_it(__new_size));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002558}
2559
2560// Default constructs __n objects starting at __end_
2561// Precondition: __n > 0
2562// Precondition: size() + __n <= capacity()
2563// Postcondition: size() == size() + __n
2564template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002565inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002566void
2567vector<bool, _Allocator>::__construct_at_end(size_type __n, bool __x)
2568{
2569 size_type __old_size = this->__size_;
2570 this->__size_ += __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002571 _VSTD::fill_n(__make_iter(__old_size), __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002572}
2573
2574template <class _Allocator>
2575template <class _ForwardIterator>
2576typename enable_if
2577<
2578 __is_forward_iterator<_ForwardIterator>::value,
2579 void
2580>::type
2581vector<bool, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
2582{
2583 size_type __old_size = this->__size_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002584 this->__size_ += _VSTD::distance(__first, __last);
2585 _VSTD::copy(__first, __last, __make_iter(__old_size));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002586}
2587
2588template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002589inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002590vector<bool, _Allocator>::vector()
Marshall Clowe546cbb2015-06-04 02:05:41 +00002591 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant76053d72013-06-27 19:35:32 +00002592 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002593 __size_(0),
2594 __cap_alloc_(0)
2595{
2596}
2597
2598template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002599inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002600vector<bool, _Allocator>::vector(const allocator_type& __a)
Marshall Clow8f282f42015-06-04 00:10:20 +00002601#if _LIBCPP_STD_VER <= 14
2602 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
2603#else
2604 _NOEXCEPT
2605#endif
Howard Hinnant76053d72013-06-27 19:35:32 +00002606 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002607 __size_(0),
2608 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2609{
2610}
2611
2612template <class _Allocator>
2613vector<bool, _Allocator>::vector(size_type __n)
Howard Hinnant76053d72013-06-27 19:35:32 +00002614 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002615 __size_(0),
2616 __cap_alloc_(0)
2617{
2618 if (__n > 0)
2619 {
2620 allocate(__n);
2621 __construct_at_end(__n, false);
2622 }
2623}
2624
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00002625#if _LIBCPP_STD_VER > 11
2626template <class _Allocator>
2627vector<bool, _Allocator>::vector(size_type __n, const allocator_type& __a)
2628 : __begin_(nullptr),
2629 __size_(0),
2630 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2631{
2632 if (__n > 0)
2633 {
2634 allocate(__n);
2635 __construct_at_end(__n, false);
2636 }
2637}
2638#endif
2639
Howard Hinnantc51e1022010-05-11 19:42:16 +00002640template <class _Allocator>
2641vector<bool, _Allocator>::vector(size_type __n, const value_type& __x)
Howard Hinnant76053d72013-06-27 19:35:32 +00002642 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002643 __size_(0),
2644 __cap_alloc_(0)
2645{
2646 if (__n > 0)
2647 {
2648 allocate(__n);
2649 __construct_at_end(__n, __x);
2650 }
2651}
2652
2653template <class _Allocator>
2654vector<bool, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002655 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002656 __size_(0),
2657 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2658{
2659 if (__n > 0)
2660 {
2661 allocate(__n);
2662 __construct_at_end(__n, __x);
2663 }
2664}
2665
2666template <class _Allocator>
2667template <class _InputIterator>
2668vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last,
2669 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2670 !__is_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002671 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002672 __size_(0),
2673 __cap_alloc_(0)
2674{
2675#ifndef _LIBCPP_NO_EXCEPTIONS
2676 try
2677 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002678#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002679 for (; __first != __last; ++__first)
2680 push_back(*__first);
2681#ifndef _LIBCPP_NO_EXCEPTIONS
2682 }
2683 catch (...)
2684 {
Howard Hinnant76053d72013-06-27 19:35:32 +00002685 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002686 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2687 __invalidate_all_iterators();
2688 throw;
2689 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002690#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002691}
2692
2693template <class _Allocator>
2694template <class _InputIterator>
2695vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2696 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2697 !__is_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002698 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002699 __size_(0),
2700 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2701{
2702#ifndef _LIBCPP_NO_EXCEPTIONS
2703 try
2704 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002705#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002706 for (; __first != __last; ++__first)
2707 push_back(*__first);
2708#ifndef _LIBCPP_NO_EXCEPTIONS
2709 }
2710 catch (...)
2711 {
Howard Hinnant76053d72013-06-27 19:35:32 +00002712 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002713 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2714 __invalidate_all_iterators();
2715 throw;
2716 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002717#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002718}
2719
2720template <class _Allocator>
2721template <class _ForwardIterator>
2722vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last,
2723 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002724 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002725 __size_(0),
2726 __cap_alloc_(0)
2727{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002728 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002729 if (__n > 0)
2730 {
2731 allocate(__n);
2732 __construct_at_end(__first, __last);
2733 }
2734}
2735
2736template <class _Allocator>
2737template <class _ForwardIterator>
2738vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2739 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002740 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002741 __size_(0),
2742 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2743{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002744 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002745 if (__n > 0)
2746 {
2747 allocate(__n);
2748 __construct_at_end(__first, __last);
2749 }
2750}
2751
Howard Hinnant33711792011-08-12 21:56:02 +00002752#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2753
Howard Hinnantc51e1022010-05-11 19:42:16 +00002754template <class _Allocator>
2755vector<bool, _Allocator>::vector(initializer_list<value_type> __il)
Howard Hinnant76053d72013-06-27 19:35:32 +00002756 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002757 __size_(0),
2758 __cap_alloc_(0)
2759{
2760 size_type __n = static_cast<size_type>(__il.size());
2761 if (__n > 0)
2762 {
2763 allocate(__n);
2764 __construct_at_end(__il.begin(), __il.end());
2765 }
2766}
2767
2768template <class _Allocator>
2769vector<bool, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002770 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002771 __size_(0),
2772 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2773{
2774 size_type __n = static_cast<size_type>(__il.size());
2775 if (__n > 0)
2776 {
2777 allocate(__n);
2778 __construct_at_end(__il.begin(), __il.end());
2779 }
2780}
2781
Howard Hinnant33711792011-08-12 21:56:02 +00002782#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2783
Howard Hinnantc51e1022010-05-11 19:42:16 +00002784template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002785vector<bool, _Allocator>::~vector()
2786{
Howard Hinnant76053d72013-06-27 19:35:32 +00002787 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002788 __storage_traits::deallocate(__alloc(), __begin_, __cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002789 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002790}
2791
2792template <class _Allocator>
2793vector<bool, _Allocator>::vector(const vector& __v)
Howard Hinnant76053d72013-06-27 19:35:32 +00002794 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002795 __size_(0),
2796 __cap_alloc_(0, __storage_traits::select_on_container_copy_construction(__v.__alloc()))
2797{
2798 if (__v.size() > 0)
2799 {
2800 allocate(__v.size());
2801 __construct_at_end(__v.begin(), __v.end());
2802 }
2803}
2804
2805template <class _Allocator>
2806vector<bool, _Allocator>::vector(const vector& __v, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002807 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002808 __size_(0),
2809 __cap_alloc_(0, __a)
2810{
2811 if (__v.size() > 0)
2812 {
2813 allocate(__v.size());
2814 __construct_at_end(__v.begin(), __v.end());
2815 }
2816}
2817
2818template <class _Allocator>
2819vector<bool, _Allocator>&
2820vector<bool, _Allocator>::operator=(const vector& __v)
2821{
2822 if (this != &__v)
2823 {
2824 __copy_assign_alloc(__v);
2825 if (__v.__size_)
2826 {
2827 if (__v.__size_ > capacity())
2828 {
2829 deallocate();
2830 allocate(__v.__size_);
2831 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002832 _VSTD::copy(__v.__begin_, __v.__begin_ + __external_cap_to_internal(__v.__size_), __begin_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002833 }
2834 __size_ = __v.__size_;
2835 }
2836 return *this;
2837}
2838
Howard Hinnant74279a52010-09-04 23:28:19 +00002839#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2840
Howard Hinnantc51e1022010-05-11 19:42:16 +00002841template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002842inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002843vector<bool, _Allocator>::vector(vector&& __v)
Marshall Clowe5108202015-07-14 14:46:32 +00002844#if _LIBCPP_STD_VER > 14
2845 _NOEXCEPT
2846#else
Howard Hinnant1c936782011-06-03 19:40:40 +00002847 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowe5108202015-07-14 14:46:32 +00002848#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002849 : __begin_(__v.__begin_),
2850 __size_(__v.__size_),
2851 __cap_alloc_(__v.__cap_alloc_)
2852{
Howard Hinnant76053d72013-06-27 19:35:32 +00002853 __v.__begin_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002854 __v.__size_ = 0;
2855 __v.__cap() = 0;
2856}
2857
2858template <class _Allocator>
2859vector<bool, _Allocator>::vector(vector&& __v, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002860 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002861 __size_(0),
2862 __cap_alloc_(0, __a)
2863{
2864 if (__a == allocator_type(__v.__alloc()))
2865 {
2866 this->__begin_ = __v.__begin_;
2867 this->__size_ = __v.__size_;
2868 this->__cap() = __v.__cap();
2869 __v.__begin_ = nullptr;
2870 __v.__cap() = __v.__size_ = 0;
2871 }
2872 else if (__v.size() > 0)
2873 {
2874 allocate(__v.size());
2875 __construct_at_end(__v.begin(), __v.end());
2876 }
2877}
2878
2879template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002880inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002881vector<bool, _Allocator>&
2882vector<bool, _Allocator>::operator=(vector&& __v)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002883 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002884{
2885 __move_assign(__v, integral_constant<bool,
2886 __storage_traits::propagate_on_container_move_assignment::value>());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002887 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002888}
2889
2890template <class _Allocator>
2891void
2892vector<bool, _Allocator>::__move_assign(vector& __c, false_type)
2893{
2894 if (__alloc() != __c.__alloc())
2895 assign(__c.begin(), __c.end());
2896 else
2897 __move_assign(__c, true_type());
2898}
2899
2900template <class _Allocator>
2901void
2902vector<bool, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002903 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002904{
2905 deallocate();
Marshall Clowb4871fa2014-07-21 15:15:15 +00002906 __move_assign_alloc(__c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002907 this->__begin_ = __c.__begin_;
2908 this->__size_ = __c.__size_;
2909 this->__cap() = __c.__cap();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002910 __c.__begin_ = nullptr;
2911 __c.__cap() = __c.__size_ = 0;
2912}
Howard Hinnant74279a52010-09-04 23:28:19 +00002913
2914#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002915
2916template <class _Allocator>
2917void
2918vector<bool, _Allocator>::assign(size_type __n, const value_type& __x)
2919{
2920 __size_ = 0;
2921 if (__n > 0)
2922 {
2923 size_type __c = capacity();
2924 if (__n <= __c)
2925 __size_ = __n;
2926 else
2927 {
2928 vector __v(__alloc());
2929 __v.reserve(__recommend(__n));
2930 __v.__size_ = __n;
2931 swap(__v);
2932 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002933 _VSTD::fill_n(begin(), __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002934 }
Eric Fiselier69c51982016-12-28 06:06:09 +00002935 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002936}
2937
2938template <class _Allocator>
2939template <class _InputIterator>
2940typename enable_if
2941<
2942 __is_input_iterator<_InputIterator>::value &&
2943 !__is_forward_iterator<_InputIterator>::value,
2944 void
2945>::type
2946vector<bool, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2947{
2948 clear();
2949 for (; __first != __last; ++__first)
2950 push_back(*__first);
2951}
2952
2953template <class _Allocator>
2954template <class _ForwardIterator>
2955typename enable_if
2956<
2957 __is_forward_iterator<_ForwardIterator>::value,
2958 void
2959>::type
2960vector<bool, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2961{
2962 clear();
Eric Fiselier6003c772016-12-23 23:37:52 +00002963 difference_type __ns = _VSTD::distance(__first, __last);
2964 _LIBCPP_ASSERT(__ns >= 0, "invalid range specified");
2965 const size_t __n = static_cast<size_type>(__ns);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002966 if (__n)
2967 {
2968 if (__n > capacity())
2969 {
2970 deallocate();
2971 allocate(__n);
2972 }
2973 __construct_at_end(__first, __last);
2974 }
2975}
2976
2977template <class _Allocator>
2978void
2979vector<bool, _Allocator>::reserve(size_type __n)
2980{
2981 if (__n > capacity())
2982 {
2983 vector __v(this->__alloc());
2984 __v.allocate(__n);
2985 __v.__construct_at_end(this->begin(), this->end());
2986 swap(__v);
2987 __invalidate_all_iterators();
2988 }
2989}
2990
2991template <class _Allocator>
2992void
Howard Hinnant1c936782011-06-03 19:40:40 +00002993vector<bool, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002994{
2995 if (__external_cap_to_internal(size()) > __cap())
2996 {
2997#ifndef _LIBCPP_NO_EXCEPTIONS
2998 try
2999 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003000#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003001 vector(*this, allocator_type(__alloc())).swap(*this);
3002#ifndef _LIBCPP_NO_EXCEPTIONS
3003 }
3004 catch (...)
3005 {
3006 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003007#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003008 }
3009}
3010
3011template <class _Allocator>
3012typename vector<bool, _Allocator>::reference
3013vector<bool, _Allocator>::at(size_type __n)
3014{
3015 if (__n >= size())
3016 this->__throw_out_of_range();
3017 return (*this)[__n];
3018}
3019
3020template <class _Allocator>
3021typename vector<bool, _Allocator>::const_reference
3022vector<bool, _Allocator>::at(size_type __n) const
3023{
3024 if (__n >= size())
3025 this->__throw_out_of_range();
3026 return (*this)[__n];
3027}
3028
3029template <class _Allocator>
3030void
3031vector<bool, _Allocator>::push_back(const value_type& __x)
3032{
3033 if (this->__size_ == this->capacity())
3034 reserve(__recommend(this->__size_ + 1));
3035 ++this->__size_;
3036 back() = __x;
3037}
3038
3039template <class _Allocator>
3040typename vector<bool, _Allocator>::iterator
3041vector<bool, _Allocator>::insert(const_iterator __position, const value_type& __x)
3042{
3043 iterator __r;
3044 if (size() < capacity())
3045 {
3046 const_iterator __old_end = end();
3047 ++__size_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003048 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003049 __r = __const_iterator_cast(__position);
3050 }
3051 else
3052 {
3053 vector __v(__alloc());
3054 __v.reserve(__recommend(__size_ + 1));
3055 __v.__size_ = __size_ + 1;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003056 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3057 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003058 swap(__v);
3059 }
3060 *__r = __x;
3061 return __r;
3062}
3063
3064template <class _Allocator>
3065typename vector<bool, _Allocator>::iterator
3066vector<bool, _Allocator>::insert(const_iterator __position, size_type __n, const value_type& __x)
3067{
3068 iterator __r;
3069 size_type __c = capacity();
3070 if (__n <= __c && size() <= __c - __n)
3071 {
3072 const_iterator __old_end = end();
3073 __size_ += __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003074 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003075 __r = __const_iterator_cast(__position);
3076 }
3077 else
3078 {
3079 vector __v(__alloc());
3080 __v.reserve(__recommend(__size_ + __n));
3081 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003082 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3083 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003084 swap(__v);
3085 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003086 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003087 return __r;
3088}
3089
3090template <class _Allocator>
3091template <class _InputIterator>
3092typename enable_if
3093<
3094 __is_input_iterator <_InputIterator>::value &&
3095 !__is_forward_iterator<_InputIterator>::value,
3096 typename vector<bool, _Allocator>::iterator
3097>::type
3098vector<bool, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
3099{
3100 difference_type __off = __position - begin();
3101 iterator __p = __const_iterator_cast(__position);
3102 iterator __old_end = end();
3103 for (; size() != capacity() && __first != __last; ++__first)
3104 {
3105 ++this->__size_;
3106 back() = *__first;
3107 }
3108 vector __v(__alloc());
3109 if (__first != __last)
3110 {
3111#ifndef _LIBCPP_NO_EXCEPTIONS
3112 try
3113 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003114#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003115 __v.assign(__first, __last);
3116 difference_type __old_size = static_cast<difference_type>(__old_end - begin());
3117 difference_type __old_p = __p - begin();
3118 reserve(__recommend(size() + __v.size()));
3119 __p = begin() + __old_p;
3120 __old_end = begin() + __old_size;
3121#ifndef _LIBCPP_NO_EXCEPTIONS
3122 }
3123 catch (...)
3124 {
3125 erase(__old_end, end());
3126 throw;
3127 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003128#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003129 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003130 __p = _VSTD::rotate(__p, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003131 insert(__p, __v.begin(), __v.end());
3132 return begin() + __off;
3133}
3134
3135template <class _Allocator>
3136template <class _ForwardIterator>
3137typename enable_if
3138<
3139 __is_forward_iterator<_ForwardIterator>::value,
3140 typename vector<bool, _Allocator>::iterator
3141>::type
3142vector<bool, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
3143{
Eric Fiselier654dd332016-12-11 05:31:00 +00003144 const difference_type __n_signed = _VSTD::distance(__first, __last);
3145 _LIBCPP_ASSERT(__n_signed >= 0, "invalid range specified");
3146 const size_type __n = static_cast<size_type>(__n_signed);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003147 iterator __r;
3148 size_type __c = capacity();
3149 if (__n <= __c && size() <= __c - __n)
3150 {
3151 const_iterator __old_end = end();
3152 __size_ += __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003153 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003154 __r = __const_iterator_cast(__position);
3155 }
3156 else
3157 {
3158 vector __v(__alloc());
3159 __v.reserve(__recommend(__size_ + __n));
3160 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003161 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3162 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003163 swap(__v);
3164 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003165 _VSTD::copy(__first, __last, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003166 return __r;
3167}
3168
3169template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003170inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003171typename vector<bool, _Allocator>::iterator
3172vector<bool, _Allocator>::erase(const_iterator __position)
3173{
3174 iterator __r = __const_iterator_cast(__position);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003175 _VSTD::copy(__position + 1, this->cend(), __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003176 --__size_;
3177 return __r;
3178}
3179
3180template <class _Allocator>
3181typename vector<bool, _Allocator>::iterator
3182vector<bool, _Allocator>::erase(const_iterator __first, const_iterator __last)
3183{
3184 iterator __r = __const_iterator_cast(__first);
3185 difference_type __d = __last - __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003186 _VSTD::copy(__last, this->cend(), __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003187 __size_ -= __d;
3188 return __r;
3189}
3190
3191template <class _Allocator>
3192void
3193vector<bool, _Allocator>::swap(vector& __x)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003194#if _LIBCPP_STD_VER >= 14
3195 _NOEXCEPT
3196#else
Eric Fiselier69c51982016-12-28 06:06:09 +00003197 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003198 __is_nothrow_swappable<allocator_type>::value)
3199#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003200{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003201 _VSTD::swap(this->__begin_, __x.__begin_);
3202 _VSTD::swap(this->__size_, __x.__size_);
3203 _VSTD::swap(this->__cap(), __x.__cap());
Marshall Clow8982dcd2015-07-13 20:04:56 +00003204 __swap_allocator(this->__alloc(), __x.__alloc(),
3205 integral_constant<bool, __alloc_traits::propagate_on_container_swap::value>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003206}
3207
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003208template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003209void
3210vector<bool, _Allocator>::resize(size_type __sz, value_type __x)
3211{
3212 size_type __cs = size();
3213 if (__cs < __sz)
3214 {
3215 iterator __r;
3216 size_type __c = capacity();
3217 size_type __n = __sz - __cs;
3218 if (__n <= __c && __cs <= __c - __n)
3219 {
3220 __r = end();
3221 __size_ += __n;
3222 }
3223 else
3224 {
3225 vector __v(__alloc());
3226 __v.reserve(__recommend(__size_ + __n));
3227 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003228 __r = _VSTD::copy(cbegin(), cend(), __v.begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003229 swap(__v);
3230 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003231 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003232 }
3233 else
3234 __size_ = __sz;
3235}
3236
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003237template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003238void
Howard Hinnant1c936782011-06-03 19:40:40 +00003239vector<bool, _Allocator>::flip() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003240{
3241 // do middle whole words
3242 size_type __n = __size_;
3243 __storage_pointer __p = __begin_;
3244 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3245 *__p = ~*__p;
3246 // do last partial word
3247 if (__n > 0)
3248 {
3249 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3250 __storage_type __b = *__p & __m;
3251 *__p &= ~__m;
3252 *__p |= ~__b & __m;
3253 }
3254}
3255
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003256template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003257bool
3258vector<bool, _Allocator>::__invariants() const
3259{
Howard Hinnant76053d72013-06-27 19:35:32 +00003260 if (this->__begin_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003261 {
3262 if (this->__size_ != 0 || this->__cap() != 0)
3263 return false;
3264 }
3265 else
3266 {
3267 if (this->__cap() == 0)
3268 return false;
3269 if (this->__size_ > this->capacity())
3270 return false;
3271 }
3272 return true;
3273}
3274
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003275template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003276size_t
Howard Hinnant1c936782011-06-03 19:40:40 +00003277vector<bool, _Allocator>::__hash_code() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003278{
3279 size_t __h = 0;
3280 // do middle whole words
3281 size_type __n = __size_;
3282 __storage_pointer __p = __begin_;
3283 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3284 __h ^= *__p;
3285 // do last partial word
3286 if (__n > 0)
3287 {
3288 const __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3289 __h ^= *__p & __m;
3290 }
3291 return __h;
3292}
3293
3294template <class _Allocator>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003295struct _LIBCPP_TEMPLATE_VIS hash<vector<bool, _Allocator> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00003296 : public unary_function<vector<bool, _Allocator>, size_t>
3297{
Howard Hinnant1c265cd2010-09-23 18:58:28 +00003298 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +00003299 size_t operator()(const vector<bool, _Allocator>& __vec) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003300 {return __vec.__hash_code();}
3301};
3302
3303template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003304inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003305bool
3306operator==(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3307{
3308 const typename vector<_Tp, _Allocator>::size_type __sz = __x.size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003309 return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003310}
3311
3312template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003313inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003314bool
3315operator!=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3316{
3317 return !(__x == __y);
3318}
3319
3320template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003321inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003322bool
3323operator< (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3324{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003325 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003326}
3327
3328template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003329inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003330bool
3331operator> (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3332{
3333 return __y < __x;
3334}
3335
3336template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003337inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003338bool
3339operator>=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3340{
3341 return !(__x < __y);
3342}
3343
3344template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003345inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003346bool
3347operator<=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3348{
3349 return !(__y < __x);
3350}
3351
3352template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003353inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003354void
3355swap(vector<_Tp, _Allocator>& __x, vector<_Tp, _Allocator>& __y)
Howard Hinnant1c936782011-06-03 19:40:40 +00003356 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00003357{
3358 __x.swap(__y);
3359}
3360
3361_LIBCPP_END_NAMESPACE_STD
3362
3363#endif // _LIBCPP_VECTOR