blob: 0e014d4bed131eac2eca9dc8ffd6428f8acab717 [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>
Eric Fiselier34ba5b92016-07-21 03:20:17 +0000102 reference emplace_back(Args&&... args);
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);
Eric Fiselier34ba5b92016-07-21 03:20:17 +0000221 template <class... Args> reference emplace_back(Args&&... args); // C++14
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> */>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000450class _LIBCPP_TYPE_VIS_ONLY 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
Eric Fiselier34ba5b92016-07-21 03:20:17 +0000682 reference emplace_back(_Args&&... __args);
Howard Hinnant74279a52010-09-04 23:28:19 +0000683#endif // _LIBCPP_HAS_NO_VARIADICS
684#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000685 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000686 void pop_back();
687
688 iterator insert(const_iterator __position, const_reference __x);
Howard Hinnant74279a52010-09-04 23:28:19 +0000689#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +0000690 iterator insert(const_iterator __position, value_type&& __x);
Howard Hinnant74279a52010-09-04 23:28:19 +0000691#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000692 template <class... _Args>
693 iterator emplace(const_iterator __position, _Args&&... __args);
Howard Hinnant74279a52010-09-04 23:28:19 +0000694#endif // _LIBCPP_HAS_NO_VARIADICS
695#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +0000696 iterator insert(const_iterator __position, size_type __n, const_reference __x);
697 template <class _InputIterator>
698 typename enable_if
699 <
700 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000701 !__is_forward_iterator<_InputIterator>::value &&
702 is_constructible<
703 value_type,
704 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000705 iterator
706 >::type
707 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
708 template <class _ForwardIterator>
709 typename enable_if
710 <
Howard Hinnant88010252013-03-28 17:44:32 +0000711 __is_forward_iterator<_ForwardIterator>::value &&
712 is_constructible<
713 value_type,
714 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000715 iterator
716 >::type
717 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnant33711792011-08-12 21:56:02 +0000718#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000719 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000720 iterator insert(const_iterator __position, initializer_list<value_type> __il)
721 {return insert(__position, __il.begin(), __il.end());}
Howard Hinnant33711792011-08-12 21:56:02 +0000722#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000723
Howard Hinnantcf823322010-12-17 14:46:43 +0000724 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000725 iterator erase(const_iterator __first, const_iterator __last);
726
Howard Hinnant1c936782011-06-03 19:40:40 +0000727 _LIBCPP_INLINE_VISIBILITY
728 void clear() _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000729 {
Marshall Clow2cd9d372014-05-08 14:14:06 +0000730 size_type __old_size = size();
Howard Hinnant27e0e772011-09-14 18:33:51 +0000731 __base::clear();
Marshall Clow2cd9d372014-05-08 14:14:06 +0000732 __annotate_shrink(__old_size);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000733 __invalidate_all_iterators();
734 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000735
736 void resize(size_type __sz);
737 void resize(size_type __sz, const_reference __x);
738
Howard Hinnant1c936782011-06-03 19:40:40 +0000739 void swap(vector&)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000740#if _LIBCPP_STD_VER >= 14
Eric Fiselier69c51982016-12-28 06:06:09 +0000741 _NOEXCEPT_DEBUG;
Marshall Clow8982dcd2015-07-13 20:04:56 +0000742#else
Eric Fiselier69c51982016-12-28 06:06:09 +0000743 _NOEXCEPT_DEBUG_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +0000744 __is_nothrow_swappable<allocator_type>::value);
745#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000746
747 bool __invariants() const;
748
Howard Hinnanta47c6d52011-09-16 17:29:17 +0000749#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000750
751 bool __dereferenceable(const const_iterator* __i) const;
752 bool __decrementable(const const_iterator* __i) const;
753 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
754 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
755
Howard Hinnanta47c6d52011-09-16 17:29:17 +0000756#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000757
Howard Hinnantc51e1022010-05-11 19:42:16 +0000758private:
Howard Hinnantcf823322010-12-17 14:46:43 +0000759 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Eric Fiselier69c51982016-12-28 06:06:09 +0000760 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(pointer __new_last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000761 void allocate(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +0000762 void deallocate() _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000763 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
Howard Hinnanta5589382011-01-04 19:53:31 +0000764 void __construct_at_end(size_type __n);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000765 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000766 void __construct_at_end(size_type __n, const_reference __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000767 template <class _ForwardIterator>
768 typename enable_if
769 <
770 __is_forward_iterator<_ForwardIterator>::value,
771 void
772 >::type
Eric Fiselieracfe6f02015-03-31 16:54:19 +0000773 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000774 void __append(size_type __n);
775 void __append(size_type __n, const_reference __x);
Howard Hinnantcf823322010-12-17 14:46:43 +0000776 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000777 iterator __make_iter(pointer __p) _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000778 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000779 const_iterator __make_iter(const_pointer __p) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000780 void __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v);
781 pointer __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p);
782 void __move_range(pointer __from_s, pointer __from_e, pointer __to);
Howard Hinnant1c936782011-06-03 19:40:40 +0000783 void __move_assign(vector& __c, true_type)
784 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000785 void __move_assign(vector& __c, false_type)
786 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000787 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76053d72013-06-27 19:35:32 +0000788 void __destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000789 {
Eric Fiselier69c51982016-12-28 06:06:09 +0000790 __invalidate_iterators_past(__new_last);
Marshall Clow2cd9d372014-05-08 14:14:06 +0000791 size_type __old_size = size();
Howard Hinnant27e0e772011-09-14 18:33:51 +0000792 __base::__destruct_at_end(__new_last);
Marshall Clow2cd9d372014-05-08 14:14:06 +0000793 __annotate_shrink(__old_size);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000794 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000795 template <class _Up>
796 void
797#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
798 __push_back_slow_path(_Up&& __x);
799#else
800 __push_back_slow_path(_Up& __x);
801#endif
Howard Hinnantb6c49562012-02-26 15:30:12 +0000802#if !defined(_LIBCPP_HAS_NO_VARIADICS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
803 template <class... _Args>
804 void
805 __emplace_back_slow_path(_Args&&... __args);
806#endif
Marshall Clow2cd9d372014-05-08 14:14:06 +0000807 // The following functions are no-ops outside of AddressSanitizer mode.
808 // We call annotatations only for the default Allocator because other allocators
809 // may not meet the AddressSanitizer alignment constraints.
810 // See the documentation for __sanitizer_annotate_contiguous_container for more details.
Marshall Clow2cd9d372014-05-08 14:14:06 +0000811#ifndef _LIBCPP_HAS_NO_ASAN
Eric Fiselier6003c772016-12-23 23:37:52 +0000812 void __annotate_contiguous_container(const void *__beg, const void *__end,
813 const void *__old_mid,
814 const void *__new_mid) const
815 {
816
Marshall Clow2cd9d372014-05-08 14:14:06 +0000817 if (__beg && is_same<allocator_type, __default_allocator_type>::value)
818 __sanitizer_annotate_contiguous_container(__beg, __end, __old_mid, __new_mid);
Marshall Clow2cd9d372014-05-08 14:14:06 +0000819 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000820#else
821 _LIBCPP_INLINE_VISIBILITY
822 void __annotate_contiguous_container(const void*, const void*, const void*,
823 const void*) const {}
824#endif
825 _LIBCPP_INLINE_VISIBILITY
826 void __annotate_new(size_type __current_size) const {
Marshall Clow2cd9d372014-05-08 14:14:06 +0000827 __annotate_contiguous_container(data(), data() + capacity(),
828 data() + capacity(), data() + __current_size);
829 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000830
831 _LIBCPP_INLINE_VISIBILITY
832 void __annotate_delete() const {
Marshall Clow2cd9d372014-05-08 14:14:06 +0000833 __annotate_contiguous_container(data(), data() + capacity(),
834 data() + size(), data() + capacity());
835 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000836
837 _LIBCPP_INLINE_VISIBILITY
Kostya Serebryany4963c252014-09-02 23:43:38 +0000838 void __annotate_increase(size_type __n) const
Marshall Clow2cd9d372014-05-08 14:14:06 +0000839 {
840 __annotate_contiguous_container(data(), data() + capacity(),
841 data() + size(), data() + size() + __n);
842 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000843
844 _LIBCPP_INLINE_VISIBILITY
Kostya Serebryany4963c252014-09-02 23:43:38 +0000845 void __annotate_shrink(size_type __old_size) const
Marshall Clow2cd9d372014-05-08 14:14:06 +0000846 {
847 __annotate_contiguous_container(data(), data() + capacity(),
848 data() + __old_size, data() + size());
849 }
Marshall Clowc78ffa52014-09-03 21:37:43 +0000850#ifndef _LIBCPP_HAS_NO_ASAN
Kostya Serebryany4963c252014-09-02 23:43:38 +0000851 // The annotation for size increase should happen before the actual increase,
852 // but if an exception is thrown after that the annotation has to be undone.
853 struct __RAII_IncreaseAnnotator {
854 __RAII_IncreaseAnnotator(const vector &__v, size_type __n = 1)
Eric Fiseliera7c043e2015-03-10 00:25:20 +0000855 : __commit(false), __v(__v), __old_size(__v.size() + __n) {
Kostya Serebryany4963c252014-09-02 23:43:38 +0000856 __v.__annotate_increase(__n);
857 }
858 void __done() { __commit = true; }
859 ~__RAII_IncreaseAnnotator() {
860 if (__commit) return;
Eric Fiseliera7c043e2015-03-10 00:25:20 +0000861 __v.__annotate_shrink(__old_size);
Kostya Serebryany4963c252014-09-02 23:43:38 +0000862 }
863 bool __commit;
Kostya Serebryany4963c252014-09-02 23:43:38 +0000864 const vector &__v;
Eric Fiseliera7c043e2015-03-10 00:25:20 +0000865 size_type __old_size;
Kostya Serebryany4963c252014-09-02 23:43:38 +0000866 };
Marshall Clowc78ffa52014-09-03 21:37:43 +0000867#else
868 struct __RAII_IncreaseAnnotator {
Eric Fiselier6003c772016-12-23 23:37:52 +0000869 _LIBCPP_INLINE_VISIBILITY
870 __RAII_IncreaseAnnotator(const vector &, size_type = 1) {}
871 _LIBCPP_INLINE_VISIBILITY void __done() {}
Marshall Clowc78ffa52014-09-03 21:37:43 +0000872 };
873#endif
874
Howard Hinnantc51e1022010-05-11 19:42:16 +0000875};
876
877template <class _Tp, class _Allocator>
878void
879vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v)
880{
Marshall Clow2cd9d372014-05-08 14:14:06 +0000881 __annotate_delete();
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000882 __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, this->__end_, __v.__begin_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000883 _VSTD::swap(this->__begin_, __v.__begin_);
884 _VSTD::swap(this->__end_, __v.__end_);
885 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000886 __v.__first_ = __v.__begin_;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000887 __annotate_new(size());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000888 __invalidate_all_iterators();
889}
890
891template <class _Tp, class _Allocator>
892typename vector<_Tp, _Allocator>::pointer
893vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p)
894{
Marshall Clow2cd9d372014-05-08 14:14:06 +0000895 __annotate_delete();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000896 pointer __r = __v.__begin_;
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000897 __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, __p, __v.__begin_);
898 __alloc_traits::__construct_forward(this->__alloc(), __p, this->__end_, __v.__end_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000899 _VSTD::swap(this->__begin_, __v.__begin_);
900 _VSTD::swap(this->__end_, __v.__end_);
901 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000902 __v.__first_ = __v.__begin_;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000903 __annotate_new(size());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000904 __invalidate_all_iterators();
905 return __r;
906}
907
908// Allocate space for __n objects
909// throws length_error if __n > max_size()
910// throws (probably bad_alloc) if memory run out
911// Precondition: __begin_ == __end_ == __end_cap() == 0
912// Precondition: __n > 0
913// Postcondition: capacity() == __n
914// Postcondition: size() == 0
915template <class _Tp, class _Allocator>
916void
917vector<_Tp, _Allocator>::allocate(size_type __n)
918{
919 if (__n > max_size())
920 this->__throw_length_error();
921 this->__begin_ = this->__end_ = __alloc_traits::allocate(this->__alloc(), __n);
922 this->__end_cap() = this->__begin_ + __n;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000923 __annotate_new(0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000924}
925
926template <class _Tp, class _Allocator>
927void
Howard Hinnant1c936782011-06-03 19:40:40 +0000928vector<_Tp, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000929{
Howard Hinnant76053d72013-06-27 19:35:32 +0000930 if (this->__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000931 {
932 clear();
933 __alloc_traits::deallocate(this->__alloc(), this->__begin_, capacity());
Howard Hinnant76053d72013-06-27 19:35:32 +0000934 this->__begin_ = this->__end_ = this->__end_cap() = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000935 }
936}
937
938template <class _Tp, class _Allocator>
939typename vector<_Tp, _Allocator>::size_type
Howard Hinnant1c936782011-06-03 19:40:40 +0000940vector<_Tp, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000941{
Eric Fiselierb5d9f442016-11-23 01:18:56 +0000942 return _VSTD::min<size_type>(__alloc_traits::max_size(this->__alloc()),
943 numeric_limits<difference_type>::max());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000944}
945
946// Precondition: __new_size > capacity()
947template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000948inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000949typename vector<_Tp, _Allocator>::size_type
950vector<_Tp, _Allocator>::__recommend(size_type __new_size) const
951{
952 const size_type __ms = max_size();
953 if (__new_size > __ms)
954 this->__throw_length_error();
955 const size_type __cap = capacity();
956 if (__cap >= __ms / 2)
957 return __ms;
Alexis Hunt991d29b2011-07-29 23:31:58 +0000958 return _VSTD::max<size_type>(2*__cap, __new_size);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000959}
960
961// Default constructs __n objects starting at __end_
962// throws if construction throws
963// Precondition: __n > 0
964// Precondition: size() + __n <= capacity()
965// Postcondition: size() == size() + __n
966template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000967void
968vector<_Tp, _Allocator>::__construct_at_end(size_type __n)
969{
Howard Hinnantc51e1022010-05-11 19:42:16 +0000970 allocator_type& __a = this->__alloc();
971 do
972 {
Kostya Serebryany4963c252014-09-02 23:43:38 +0000973 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000974 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000975 ++this->__end_;
976 --__n;
Kostya Serebryany4963c252014-09-02 23:43:38 +0000977 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000978 } while (__n > 0);
979}
980
Howard Hinnantc51e1022010-05-11 19:42:16 +0000981// Copy constructs __n objects starting at __end_ from __x
982// throws if construction throws
983// Precondition: __n > 0
984// Precondition: size() + __n <= capacity()
985// Postcondition: size() == old size() + __n
986// Postcondition: [i] == __x for all i in [size() - __n, __n)
987template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000988inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000989void
990vector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
991{
Howard Hinnantc51e1022010-05-11 19:42:16 +0000992 allocator_type& __a = this->__alloc();
993 do
994 {
Kostya Serebryany4963c252014-09-02 23:43:38 +0000995 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000996 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000997 ++this->__end_;
998 --__n;
Kostya Serebryany4963c252014-09-02 23:43:38 +0000999 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001000 } while (__n > 0);
1001}
1002
1003template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001004template <class _ForwardIterator>
1005typename enable_if
1006<
1007 __is_forward_iterator<_ForwardIterator>::value,
1008 void
1009>::type
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001010vector<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001011{
1012 allocator_type& __a = this->__alloc();
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001013 __RAII_IncreaseAnnotator __annotator(*this, __n);
1014 __alloc_traits::__construct_range_forward(__a, __first, __last, this->__end_);
1015 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001016}
1017
1018// Default constructs __n objects starting at __end_
1019// throws if construction throws
1020// Postcondition: size() == size() + __n
Howard Hinnant1c936782011-06-03 19:40:40 +00001021// Exception safety: strong.
Howard Hinnantc51e1022010-05-11 19:42:16 +00001022template <class _Tp, class _Allocator>
1023void
1024vector<_Tp, _Allocator>::__append(size_type __n)
1025{
1026 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1027 this->__construct_at_end(__n);
1028 else
1029 {
1030 allocator_type& __a = this->__alloc();
1031 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1032 __v.__construct_at_end(__n);
1033 __swap_out_circular_buffer(__v);
1034 }
1035}
1036
1037// Default constructs __n objects starting at __end_
1038// throws if construction throws
1039// Postcondition: size() == size() + __n
Howard Hinnant1c936782011-06-03 19:40:40 +00001040// Exception safety: strong.
Howard Hinnantc51e1022010-05-11 19:42:16 +00001041template <class _Tp, class _Allocator>
1042void
1043vector<_Tp, _Allocator>::__append(size_type __n, const_reference __x)
1044{
1045 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1046 this->__construct_at_end(__n, __x);
1047 else
1048 {
1049 allocator_type& __a = this->__alloc();
1050 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1051 __v.__construct_at_end(__n, __x);
1052 __swap_out_circular_buffer(__v);
1053 }
1054}
1055
1056template <class _Tp, class _Allocator>
1057vector<_Tp, _Allocator>::vector(size_type __n)
1058{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001059#if _LIBCPP_DEBUG_LEVEL >= 2
1060 __get_db()->__insert_c(this);
1061#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001062 if (__n > 0)
1063 {
1064 allocate(__n);
1065 __construct_at_end(__n);
1066 }
1067}
1068
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00001069#if _LIBCPP_STD_VER > 11
1070template <class _Tp, class _Allocator>
1071vector<_Tp, _Allocator>::vector(size_type __n, const allocator_type& __a)
1072 : __base(__a)
1073{
1074#if _LIBCPP_DEBUG_LEVEL >= 2
1075 __get_db()->__insert_c(this);
1076#endif
1077 if (__n > 0)
1078 {
1079 allocate(__n);
1080 __construct_at_end(__n);
1081 }
1082}
1083#endif
1084
Howard Hinnantc51e1022010-05-11 19:42:16 +00001085template <class _Tp, class _Allocator>
1086vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x)
1087{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001088#if _LIBCPP_DEBUG_LEVEL >= 2
1089 __get_db()->__insert_c(this);
1090#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001091 if (__n > 0)
1092 {
1093 allocate(__n);
1094 __construct_at_end(__n, __x);
1095 }
1096}
1097
1098template <class _Tp, class _Allocator>
1099vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x, const allocator_type& __a)
1100 : __base(__a)
1101{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001102#if _LIBCPP_DEBUG_LEVEL >= 2
1103 __get_db()->__insert_c(this);
1104#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001105 if (__n > 0)
1106 {
1107 allocate(__n);
1108 __construct_at_end(__n, __x);
1109 }
1110}
1111
1112template <class _Tp, class _Allocator>
1113template <class _InputIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +00001114vector<_Tp, _Allocator>::vector(_InputIterator __first,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001115 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001116 !__is_forward_iterator<_InputIterator>::value &&
1117 is_constructible<
1118 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +00001119 typename iterator_traits<_InputIterator>::reference>::value,
1120 _InputIterator>::type __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001121{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001122#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001123 __get_db()->__insert_c(this);
1124#endif
Howard Hinnant9cd22302011-09-16 18:41:29 +00001125 for (; __first != __last; ++__first)
1126 push_back(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001127}
1128
1129template <class _Tp, class _Allocator>
1130template <class _InputIterator>
1131vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
1132 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001133 !__is_forward_iterator<_InputIterator>::value &&
1134 is_constructible<
1135 value_type,
1136 typename iterator_traits<_InputIterator>::reference>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001137 : __base(__a)
1138{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001139#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001140 __get_db()->__insert_c(this);
1141#endif
Howard Hinnant9cd22302011-09-16 18:41:29 +00001142 for (; __first != __last; ++__first)
1143 push_back(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001144}
1145
1146template <class _Tp, class _Allocator>
1147template <class _ForwardIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +00001148vector<_Tp, _Allocator>::vector(_ForwardIterator __first,
Howard Hinnant88010252013-03-28 17:44:32 +00001149 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
1150 is_constructible<
1151 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +00001152 typename iterator_traits<_ForwardIterator>::reference>::value,
1153 _ForwardIterator>::type __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001154{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001155#if _LIBCPP_DEBUG_LEVEL >= 2
1156 __get_db()->__insert_c(this);
1157#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001158 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001159 if (__n > 0)
1160 {
1161 allocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001162 __construct_at_end(__first, __last, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001163 }
1164}
1165
1166template <class _Tp, class _Allocator>
1167template <class _ForwardIterator>
1168vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Howard Hinnant88010252013-03-28 17:44:32 +00001169 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
1170 is_constructible<
1171 value_type,
1172 typename iterator_traits<_ForwardIterator>::reference>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001173 : __base(__a)
1174{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001175#if _LIBCPP_DEBUG_LEVEL >= 2
1176 __get_db()->__insert_c(this);
1177#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001178 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001179 if (__n > 0)
1180 {
1181 allocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001182 __construct_at_end(__first, __last, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001183 }
1184}
1185
1186template <class _Tp, class _Allocator>
1187vector<_Tp, _Allocator>::vector(const vector& __x)
1188 : __base(__alloc_traits::select_on_container_copy_construction(__x.__alloc()))
1189{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001190#if _LIBCPP_DEBUG_LEVEL >= 2
1191 __get_db()->__insert_c(this);
1192#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001193 size_type __n = __x.size();
1194 if (__n > 0)
1195 {
1196 allocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001197 __construct_at_end(__x.__begin_, __x.__end_, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001198 }
1199}
1200
1201template <class _Tp, class _Allocator>
1202vector<_Tp, _Allocator>::vector(const vector& __x, const allocator_type& __a)
1203 : __base(__a)
1204{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001205#if _LIBCPP_DEBUG_LEVEL >= 2
1206 __get_db()->__insert_c(this);
1207#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001208 size_type __n = __x.size();
1209 if (__n > 0)
1210 {
1211 allocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001212 __construct_at_end(__x.__begin_, __x.__end_, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001213 }
1214}
1215
Howard Hinnant74279a52010-09-04 23:28:19 +00001216#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001217
1218template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001219inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001220vector<_Tp, _Allocator>::vector(vector&& __x)
Marshall Clowe5108202015-07-14 14:46:32 +00001221#if _LIBCPP_STD_VER > 14
1222 _NOEXCEPT
1223#else
Howard Hinnant1c936782011-06-03 19:40:40 +00001224 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowe5108202015-07-14 14:46:32 +00001225#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001226 : __base(_VSTD::move(__x.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001227{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001228#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001229 __get_db()->__insert_c(this);
Howard Hinnanta1e60cd2011-09-19 16:34:29 +00001230 __get_db()->swap(this, &__x);
Howard Hinnant27e0e772011-09-14 18:33:51 +00001231#endif
Howard Hinnant9cd22302011-09-16 18:41:29 +00001232 this->__begin_ = __x.__begin_;
1233 this->__end_ = __x.__end_;
1234 this->__end_cap() = __x.__end_cap();
Howard Hinnant76053d72013-06-27 19:35:32 +00001235 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001236}
1237
1238template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001239inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001240vector<_Tp, _Allocator>::vector(vector&& __x, const allocator_type& __a)
1241 : __base(__a)
1242{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001243#if _LIBCPP_DEBUG_LEVEL >= 2
1244 __get_db()->__insert_c(this);
1245#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001246 if (__a == __x.__alloc())
1247 {
1248 this->__begin_ = __x.__begin_;
1249 this->__end_ = __x.__end_;
1250 this->__end_cap() = __x.__end_cap();
1251 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnanta1e60cd2011-09-19 16:34:29 +00001252#if _LIBCPP_DEBUG_LEVEL >= 2
1253 __get_db()->swap(this, &__x);
1254#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001255 }
1256 else
1257 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001258 typedef move_iterator<iterator> _Ip;
1259 assign(_Ip(__x.begin()), _Ip(__x.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001260 }
1261}
1262
Howard Hinnant33711792011-08-12 21:56:02 +00001263#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1264
Howard Hinnantc51e1022010-05-11 19:42:16 +00001265template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001266inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001267vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il)
1268{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001269#if _LIBCPP_DEBUG_LEVEL >= 2
1270 __get_db()->__insert_c(this);
1271#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001272 if (__il.size() > 0)
1273 {
1274 allocate(__il.size());
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001275 __construct_at_end(__il.begin(), __il.end(), __il.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001276 }
1277}
1278
1279template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001280inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001281vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
1282 : __base(__a)
1283{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001284#if _LIBCPP_DEBUG_LEVEL >= 2
1285 __get_db()->__insert_c(this);
1286#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001287 if (__il.size() > 0)
1288 {
1289 allocate(__il.size());
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001290 __construct_at_end(__il.begin(), __il.end(), __il.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001291 }
1292}
1293
Howard Hinnant33711792011-08-12 21:56:02 +00001294#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1295
Howard Hinnantc51e1022010-05-11 19:42:16 +00001296template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001297inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001298vector<_Tp, _Allocator>&
1299vector<_Tp, _Allocator>::operator=(vector&& __x)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001300 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001301{
1302 __move_assign(__x, integral_constant<bool,
1303 __alloc_traits::propagate_on_container_move_assignment::value>());
1304 return *this;
1305}
1306
1307template <class _Tp, class _Allocator>
1308void
1309vector<_Tp, _Allocator>::__move_assign(vector& __c, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001310 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001311{
1312 if (__base::__alloc() != __c.__alloc())
1313 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001314 typedef move_iterator<iterator> _Ip;
1315 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001316 }
1317 else
1318 __move_assign(__c, true_type());
1319}
1320
1321template <class _Tp, class _Allocator>
1322void
1323vector<_Tp, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00001324 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001325{
1326 deallocate();
Marshall Clow136d45c2014-07-21 15:11:13 +00001327 __base::__move_assign_alloc(__c); // this can throw
Howard Hinnantc51e1022010-05-11 19:42:16 +00001328 this->__begin_ = __c.__begin_;
1329 this->__end_ = __c.__end_;
1330 this->__end_cap() = __c.__end_cap();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001331 __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
Howard Hinnanta1e60cd2011-09-19 16:34:29 +00001332#if _LIBCPP_DEBUG_LEVEL >= 2
1333 __get_db()->swap(this, &__c);
1334#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001335}
1336
Howard Hinnant74279a52010-09-04 23:28:19 +00001337#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001338
1339template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001340inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001341vector<_Tp, _Allocator>&
1342vector<_Tp, _Allocator>::operator=(const vector& __x)
1343{
1344 if (this != &__x)
1345 {
1346 __base::__copy_assign_alloc(__x);
1347 assign(__x.__begin_, __x.__end_);
1348 }
1349 return *this;
1350}
1351
1352template <class _Tp, class _Allocator>
1353template <class _InputIterator>
1354typename enable_if
1355<
1356 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001357 !__is_forward_iterator<_InputIterator>::value &&
1358 is_constructible<
1359 _Tp,
1360 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001361 void
1362>::type
1363vector<_Tp, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
1364{
1365 clear();
1366 for (; __first != __last; ++__first)
1367 push_back(*__first);
1368}
1369
1370template <class _Tp, class _Allocator>
1371template <class _ForwardIterator>
1372typename enable_if
1373<
Howard Hinnant88010252013-03-28 17:44:32 +00001374 __is_forward_iterator<_ForwardIterator>::value &&
1375 is_constructible<
1376 _Tp,
1377 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001378 void
1379>::type
1380vector<_Tp, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
1381{
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001382 size_type __new_size = static_cast<size_type>(_VSTD::distance(__first, __last));
1383 if (__new_size <= capacity())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001384 {
1385 _ForwardIterator __mid = __last;
1386 bool __growing = false;
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001387 if (__new_size > size())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001388 {
1389 __growing = true;
1390 __mid = __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001391 _VSTD::advance(__mid, size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001392 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001393 pointer __m = _VSTD::copy(__first, __mid, this->__begin_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001394 if (__growing)
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001395 __construct_at_end(__mid, __last, __new_size - size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001396 else
1397 this->__destruct_at_end(__m);
1398 }
1399 else
1400 {
1401 deallocate();
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001402 allocate(__recommend(__new_size));
1403 __construct_at_end(__first, __last, __new_size);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001404 }
Eric Fiselier69c51982016-12-28 06:06:09 +00001405 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001406}
1407
1408template <class _Tp, class _Allocator>
1409void
1410vector<_Tp, _Allocator>::assign(size_type __n, const_reference __u)
1411{
1412 if (__n <= capacity())
1413 {
1414 size_type __s = size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001415 _VSTD::fill_n(this->__begin_, _VSTD::min(__n, __s), __u);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001416 if (__n > __s)
1417 __construct_at_end(__n - __s, __u);
1418 else
Howard Hinnant155c2af2010-05-24 17:49:41 +00001419 this->__destruct_at_end(this->__begin_ + __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001420 }
1421 else
1422 {
1423 deallocate();
1424 allocate(__recommend(static_cast<size_type>(__n)));
1425 __construct_at_end(__n, __u);
1426 }
Eric Fiselier69c51982016-12-28 06:06:09 +00001427 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001428}
1429
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001430template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001431inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001432typename vector<_Tp, _Allocator>::iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001433vector<_Tp, _Allocator>::__make_iter(pointer __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001434{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001435#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001436 return iterator(this, __p);
1437#else
1438 return iterator(__p);
1439#endif
1440}
1441
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001442template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001443inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001444typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001445vector<_Tp, _Allocator>::__make_iter(const_pointer __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001446{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001447#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001448 return const_iterator(this, __p);
1449#else
1450 return const_iterator(__p);
1451#endif
1452}
1453
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001454template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001455inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001456typename vector<_Tp, _Allocator>::iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001457vector<_Tp, _Allocator>::begin() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001458{
1459 return __make_iter(this->__begin_);
1460}
1461
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001462template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001463inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001464typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001465vector<_Tp, _Allocator>::begin() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001466{
1467 return __make_iter(this->__begin_);
1468}
1469
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001470template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001471inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001472typename vector<_Tp, _Allocator>::iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001473vector<_Tp, _Allocator>::end() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001474{
1475 return __make_iter(this->__end_);
1476}
1477
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001478template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001479inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001480typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001481vector<_Tp, _Allocator>::end() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001482{
1483 return __make_iter(this->__end_);
1484}
1485
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001486template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001487inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001488typename vector<_Tp, _Allocator>::reference
1489vector<_Tp, _Allocator>::operator[](size_type __n)
1490{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001491 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001492 return this->__begin_[__n];
1493}
1494
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001495template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001496inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001497typename vector<_Tp, _Allocator>::const_reference
1498vector<_Tp, _Allocator>::operator[](size_type __n) const
1499{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001500 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001501 return this->__begin_[__n];
1502}
1503
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001504template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001505typename vector<_Tp, _Allocator>::reference
1506vector<_Tp, _Allocator>::at(size_type __n)
1507{
1508 if (__n >= size())
1509 this->__throw_out_of_range();
1510 return this->__begin_[__n];
1511}
1512
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001513template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001514typename vector<_Tp, _Allocator>::const_reference
1515vector<_Tp, _Allocator>::at(size_type __n) const
1516{
1517 if (__n >= size())
1518 this->__throw_out_of_range();
1519 return this->__begin_[__n];
1520}
1521
1522template <class _Tp, class _Allocator>
1523void
1524vector<_Tp, _Allocator>::reserve(size_type __n)
1525{
1526 if (__n > capacity())
1527 {
1528 allocator_type& __a = this->__alloc();
Howard Hinnant1c936782011-06-03 19:40:40 +00001529 __split_buffer<value_type, allocator_type&> __v(__n, size(), __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001530 __swap_out_circular_buffer(__v);
1531 }
1532}
1533
1534template <class _Tp, class _Allocator>
1535void
Howard Hinnant1c936782011-06-03 19:40:40 +00001536vector<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001537{
1538 if (capacity() > size())
1539 {
1540#ifndef _LIBCPP_NO_EXCEPTIONS
1541 try
1542 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001543#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001544 allocator_type& __a = this->__alloc();
Howard Hinnant1c936782011-06-03 19:40:40 +00001545 __split_buffer<value_type, allocator_type&> __v(size(), size(), __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001546 __swap_out_circular_buffer(__v);
1547#ifndef _LIBCPP_NO_EXCEPTIONS
1548 }
1549 catch (...)
1550 {
1551 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001552#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001553 }
1554}
1555
1556template <class _Tp, class _Allocator>
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001557template <class _Up>
1558void
1559#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1560vector<_Tp, _Allocator>::__push_back_slow_path(_Up&& __x)
1561#else
1562vector<_Tp, _Allocator>::__push_back_slow_path(_Up& __x)
1563#endif
1564{
1565 allocator_type& __a = this->__alloc();
1566 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1567 // __v.push_back(_VSTD::forward<_Up>(__x));
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001568 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), _VSTD::forward<_Up>(__x));
1569 __v.__end_++;
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001570 __swap_out_circular_buffer(__v);
1571}
1572
1573template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001574inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001575void
1576vector<_Tp, _Allocator>::push_back(const_reference __x)
1577{
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001578 if (this->__end_ != this->__end_cap())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001579 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001580 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001581 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001582 _VSTD::__to_raw_pointer(this->__end_), __x);
Kostya Serebryany4963c252014-09-02 23:43:38 +00001583 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001584 ++this->__end_;
1585 }
1586 else
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001587 __push_back_slow_path(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001588}
1589
Howard Hinnant74279a52010-09-04 23:28:19 +00001590#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001591
1592template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001593inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001594void
1595vector<_Tp, _Allocator>::push_back(value_type&& __x)
1596{
1597 if (this->__end_ < this->__end_cap())
1598 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001599 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001600 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001601 _VSTD::__to_raw_pointer(this->__end_),
1602 _VSTD::move(__x));
Kostya Serebryany4963c252014-09-02 23:43:38 +00001603 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001604 ++this->__end_;
1605 }
1606 else
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001607 __push_back_slow_path(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001608}
1609
Howard Hinnant74279a52010-09-04 23:28:19 +00001610#ifndef _LIBCPP_HAS_NO_VARIADICS
1611
Howard Hinnantc51e1022010-05-11 19:42:16 +00001612template <class _Tp, class _Allocator>
1613template <class... _Args>
1614void
Howard Hinnantb6c49562012-02-26 15:30:12 +00001615vector<_Tp, _Allocator>::__emplace_back_slow_path(_Args&&... __args)
1616{
1617 allocator_type& __a = this->__alloc();
1618 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1619// __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001620 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), _VSTD::forward<_Args>(__args)...);
1621 __v.__end_++;
Howard Hinnantb6c49562012-02-26 15:30:12 +00001622 __swap_out_circular_buffer(__v);
1623}
1624
1625template <class _Tp, class _Allocator>
1626template <class... _Args>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001627inline
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001628typename vector<_Tp, _Allocator>::reference
Howard Hinnantc51e1022010-05-11 19:42:16 +00001629vector<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1630{
1631 if (this->__end_ < this->__end_cap())
1632 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001633 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001634 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001635 _VSTD::__to_raw_pointer(this->__end_),
1636 _VSTD::forward<_Args>(__args)...);
Kostya Serebryany4963c252014-09-02 23:43:38 +00001637 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001638 ++this->__end_;
1639 }
1640 else
Howard Hinnantb6c49562012-02-26 15:30:12 +00001641 __emplace_back_slow_path(_VSTD::forward<_Args>(__args)...);
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001642 return this->back();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001643}
1644
Howard Hinnant74279a52010-09-04 23:28:19 +00001645#endif // _LIBCPP_HAS_NO_VARIADICS
1646#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001647
1648template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001649inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001650void
1651vector<_Tp, _Allocator>::pop_back()
1652{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001653 _LIBCPP_ASSERT(!empty(), "vector::pop_back called for empty vector");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001654 this->__destruct_at_end(this->__end_ - 1);
1655}
1656
1657template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001658inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001659typename vector<_Tp, _Allocator>::iterator
1660vector<_Tp, _Allocator>::erase(const_iterator __position)
1661{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001662#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001663 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1664 "vector::erase(iterator) called with an iterator not"
1665 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001666#endif
Howard Hinnant58d52d52013-03-25 22:12:26 +00001667 _LIBCPP_ASSERT(__position != end(),
1668 "vector::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnant76053d72013-06-27 19:35:32 +00001669 difference_type __ps = __position - cbegin();
1670 pointer __p = this->__begin_ + __ps;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001671 this->__destruct_at_end(_VSTD::move(__p + 1, this->__end_, __p));
Eric Fiselier69c51982016-12-28 06:06:09 +00001672 this->__invalidate_iterators_past(__p-1);
1673 iterator __r = __make_iter(__p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001674 return __r;
1675}
1676
1677template <class _Tp, class _Allocator>
1678typename vector<_Tp, _Allocator>::iterator
1679vector<_Tp, _Allocator>::erase(const_iterator __first, const_iterator __last)
1680{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001681#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001682 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
1683 "vector::erase(iterator, iterator) called with an iterator not"
1684 " referring to this vector");
Eric Fiselier69c51982016-12-28 06:06:09 +00001685 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__last) == this,
1686 "vector::erase(iterator, iterator) called with an iterator not"
1687 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001688#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001689 _LIBCPP_ASSERT(__first <= __last, "vector::erase(first, last) called with invalid range");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001690 pointer __p = this->__begin_ + (__first - begin());
Eric Fiselier69c51982016-12-28 06:06:09 +00001691 if (__first != __last) {
Howard Hinnantc580cc32013-04-18 15:02:57 +00001692 this->__destruct_at_end(_VSTD::move(__p + (__last - __first), this->__end_, __p));
Eric Fiselier69c51982016-12-28 06:06:09 +00001693 this->__invalidate_iterators_past(__p - 1);
1694 }
1695 iterator __r = __make_iter(__p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001696 return __r;
1697}
1698
1699template <class _Tp, class _Allocator>
1700void
1701vector<_Tp, _Allocator>::__move_range(pointer __from_s, pointer __from_e, pointer __to)
1702{
1703 pointer __old_last = this->__end_;
1704 difference_type __n = __old_last - __to;
1705 for (pointer __i = __from_s + __n; __i < __from_e; ++__i, ++this->__end_)
1706 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001707 _VSTD::__to_raw_pointer(this->__end_),
1708 _VSTD::move(*__i));
1709 _VSTD::move_backward(__from_s, __from_s + __n, __old_last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001710}
1711
1712template <class _Tp, class _Allocator>
1713typename vector<_Tp, _Allocator>::iterator
1714vector<_Tp, _Allocator>::insert(const_iterator __position, const_reference __x)
1715{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001716#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001717 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1718 "vector::insert(iterator, x) called with an iterator not"
1719 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001720#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001721 pointer __p = this->__begin_ + (__position - begin());
1722 if (this->__end_ < this->__end_cap())
1723 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001724 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001725 if (__p == this->__end_)
1726 {
1727 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001728 _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001729 ++this->__end_;
1730 }
1731 else
1732 {
1733 __move_range(__p, this->__end_, __p + 1);
1734 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1735 if (__p <= __xr && __xr < this->__end_)
1736 ++__xr;
1737 *__p = *__xr;
1738 }
Kostya Serebryany4963c252014-09-02 23:43:38 +00001739 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001740 }
1741 else
1742 {
1743 allocator_type& __a = this->__alloc();
1744 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
1745 __v.push_back(__x);
1746 __p = __swap_out_circular_buffer(__v, __p);
1747 }
1748 return __make_iter(__p);
1749}
1750
Howard Hinnant74279a52010-09-04 23:28:19 +00001751#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001752
1753template <class _Tp, class _Allocator>
1754typename vector<_Tp, _Allocator>::iterator
1755vector<_Tp, _Allocator>::insert(const_iterator __position, value_type&& __x)
1756{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001757#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001758 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1759 "vector::insert(iterator, x) called with an iterator not"
1760 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001761#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001762 pointer __p = this->__begin_ + (__position - begin());
1763 if (this->__end_ < this->__end_cap())
1764 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001765 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001766 if (__p == this->__end_)
1767 {
1768 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001769 _VSTD::__to_raw_pointer(this->__end_),
1770 _VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001771 ++this->__end_;
1772 }
1773 else
1774 {
1775 __move_range(__p, this->__end_, __p + 1);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001776 *__p = _VSTD::move(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001777 }
Kostya Serebryany4963c252014-09-02 23:43:38 +00001778 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001779 }
1780 else
1781 {
1782 allocator_type& __a = this->__alloc();
1783 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001784 __v.push_back(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001785 __p = __swap_out_circular_buffer(__v, __p);
1786 }
1787 return __make_iter(__p);
1788}
1789
Howard Hinnant74279a52010-09-04 23:28:19 +00001790#ifndef _LIBCPP_HAS_NO_VARIADICS
1791
Howard Hinnantc51e1022010-05-11 19:42:16 +00001792template <class _Tp, class _Allocator>
1793template <class... _Args>
1794typename vector<_Tp, _Allocator>::iterator
1795vector<_Tp, _Allocator>::emplace(const_iterator __position, _Args&&... __args)
1796{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001797#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001798 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1799 "vector::emplace(iterator, x) called with an iterator not"
1800 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001801#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001802 pointer __p = this->__begin_ + (__position - begin());
1803 if (this->__end_ < this->__end_cap())
1804 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001805 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001806 if (__p == this->__end_)
1807 {
1808 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001809 _VSTD::__to_raw_pointer(this->__end_),
1810 _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001811 ++this->__end_;
1812 }
1813 else
1814 {
Marshall Clowa591b9a2016-07-11 21:38:08 +00001815 __temp_value<value_type, _Allocator> __tmp(this->__alloc(), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001816 __move_range(__p, this->__end_, __p + 1);
Marshall Clowa591b9a2016-07-11 21:38:08 +00001817 *__p = _VSTD::move(__tmp.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001818 }
Kostya Serebryany4963c252014-09-02 23:43:38 +00001819 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001820 }
1821 else
1822 {
1823 allocator_type& __a = this->__alloc();
1824 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001825 __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001826 __p = __swap_out_circular_buffer(__v, __p);
1827 }
1828 return __make_iter(__p);
1829}
1830
Howard Hinnant74279a52010-09-04 23:28:19 +00001831#endif // _LIBCPP_HAS_NO_VARIADICS
1832#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001833
1834template <class _Tp, class _Allocator>
1835typename vector<_Tp, _Allocator>::iterator
1836vector<_Tp, _Allocator>::insert(const_iterator __position, size_type __n, const_reference __x)
1837{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001838#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001839 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1840 "vector::insert(iterator, n, x) called with an iterator not"
1841 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001842#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001843 pointer __p = this->__begin_ + (__position - begin());
1844 if (__n > 0)
1845 {
1846 if (__n <= static_cast<size_type>(this->__end_cap() - this->__end_))
1847 {
1848 size_type __old_n = __n;
1849 pointer __old_last = this->__end_;
1850 if (__n > static_cast<size_type>(this->__end_ - __p))
1851 {
1852 size_type __cx = __n - (this->__end_ - __p);
1853 __construct_at_end(__cx, __x);
1854 __n -= __cx;
1855 }
1856 if (__n > 0)
1857 {
Eric Fiselier2a649652014-11-14 18:28:36 +00001858 __RAII_IncreaseAnnotator __annotator(*this, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001859 __move_range(__p, __old_last, __p + __old_n);
Kostya Serebryany4963c252014-09-02 23:43:38 +00001860 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001861 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1862 if (__p <= __xr && __xr < this->__end_)
1863 __xr += __old_n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001864 _VSTD::fill_n(__p, __n, *__xr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001865 }
1866 }
1867 else
1868 {
1869 allocator_type& __a = this->__alloc();
1870 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1871 __v.__construct_at_end(__n, __x);
1872 __p = __swap_out_circular_buffer(__v, __p);
1873 }
1874 }
1875 return __make_iter(__p);
1876}
1877
1878template <class _Tp, class _Allocator>
1879template <class _InputIterator>
1880typename enable_if
1881<
1882 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001883 !__is_forward_iterator<_InputIterator>::value &&
1884 is_constructible<
1885 _Tp,
1886 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001887 typename vector<_Tp, _Allocator>::iterator
1888>::type
1889vector<_Tp, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
1890{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001891#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001892 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1893 "vector::insert(iterator, range) called with an iterator not"
1894 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001895#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001896 difference_type __off = __position - begin();
1897 pointer __p = this->__begin_ + __off;
1898 allocator_type& __a = this->__alloc();
1899 pointer __old_last = this->__end_;
1900 for (; this->__end_ != this->__end_cap() && __first != __last; ++__first)
1901 {
Eric Fiselier489fd502015-07-18 18:22:12 +00001902 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001903 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001904 *__first);
1905 ++this->__end_;
Eric Fiselier489fd502015-07-18 18:22:12 +00001906 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001907 }
1908 __split_buffer<value_type, allocator_type&> __v(__a);
1909 if (__first != __last)
1910 {
1911#ifndef _LIBCPP_NO_EXCEPTIONS
1912 try
1913 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001914#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001915 __v.__construct_at_end(__first, __last);
1916 difference_type __old_size = __old_last - this->__begin_;
1917 difference_type __old_p = __p - this->__begin_;
1918 reserve(__recommend(size() + __v.size()));
1919 __p = this->__begin_ + __old_p;
1920 __old_last = this->__begin_ + __old_size;
1921#ifndef _LIBCPP_NO_EXCEPTIONS
1922 }
1923 catch (...)
1924 {
1925 erase(__make_iter(__old_last), end());
1926 throw;
1927 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001928#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001929 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001930 __p = _VSTD::rotate(__p, __old_last, this->__end_);
Howard Hinnant9cd22302011-09-16 18:41:29 +00001931 insert(__make_iter(__p), make_move_iterator(__v.begin()),
1932 make_move_iterator(__v.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001933 return begin() + __off;
1934}
1935
1936template <class _Tp, class _Allocator>
1937template <class _ForwardIterator>
1938typename enable_if
1939<
Howard Hinnant88010252013-03-28 17:44:32 +00001940 __is_forward_iterator<_ForwardIterator>::value &&
1941 is_constructible<
1942 _Tp,
1943 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001944 typename vector<_Tp, _Allocator>::iterator
1945>::type
1946vector<_Tp, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
1947{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001948#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001949 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1950 "vector::insert(iterator, range) called with an iterator not"
1951 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001952#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001953 pointer __p = this->__begin_ + (__position - begin());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001954 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001955 if (__n > 0)
1956 {
1957 if (__n <= this->__end_cap() - this->__end_)
1958 {
1959 size_type __old_n = __n;
1960 pointer __old_last = this->__end_;
1961 _ForwardIterator __m = __last;
1962 difference_type __dx = this->__end_ - __p;
1963 if (__n > __dx)
1964 {
1965 __m = __first;
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001966 difference_type __diff = this->__end_ - __p;
1967 _VSTD::advance(__m, __diff);
1968 __construct_at_end(__m, __last, __n - __diff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001969 __n = __dx;
1970 }
1971 if (__n > 0)
1972 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001973 __RAII_IncreaseAnnotator __annotator(*this, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001974 __move_range(__p, __old_last, __p + __old_n);
Kostya Serebryany4963c252014-09-02 23:43:38 +00001975 __annotator.__done();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001976 _VSTD::copy(__first, __m, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001977 }
1978 }
1979 else
1980 {
1981 allocator_type& __a = this->__alloc();
1982 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1983 __v.__construct_at_end(__first, __last);
1984 __p = __swap_out_circular_buffer(__v, __p);
1985 }
1986 }
1987 return __make_iter(__p);
1988}
1989
1990template <class _Tp, class _Allocator>
1991void
1992vector<_Tp, _Allocator>::resize(size_type __sz)
1993{
1994 size_type __cs = size();
1995 if (__cs < __sz)
1996 this->__append(__sz - __cs);
1997 else if (__cs > __sz)
1998 this->__destruct_at_end(this->__begin_ + __sz);
1999}
2000
2001template <class _Tp, class _Allocator>
2002void
2003vector<_Tp, _Allocator>::resize(size_type __sz, const_reference __x)
2004{
2005 size_type __cs = size();
2006 if (__cs < __sz)
2007 this->__append(__sz - __cs, __x);
2008 else if (__cs > __sz)
2009 this->__destruct_at_end(this->__begin_ + __sz);
2010}
2011
2012template <class _Tp, class _Allocator>
2013void
2014vector<_Tp, _Allocator>::swap(vector& __x)
Marshall Clow8982dcd2015-07-13 20:04:56 +00002015#if _LIBCPP_STD_VER >= 14
Eric Fiselier69c51982016-12-28 06:06:09 +00002016 _NOEXCEPT_DEBUG
Marshall Clow8982dcd2015-07-13 20:04:56 +00002017#else
Eric Fiselier69c51982016-12-28 06:06:09 +00002018 _NOEXCEPT_DEBUG_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00002019 __is_nothrow_swappable<allocator_type>::value)
2020#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002021{
Howard Hinnant27e0e772011-09-14 18:33:51 +00002022 _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
2023 this->__alloc() == __x.__alloc(),
2024 "vector::swap: Either propagate_on_container_swap must be true"
2025 " or the allocators must compare equal");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002026 _VSTD::swap(this->__begin_, __x.__begin_);
2027 _VSTD::swap(this->__end_, __x.__end_);
2028 _VSTD::swap(this->__end_cap(), __x.__end_cap());
Marshall Clow8982dcd2015-07-13 20:04:56 +00002029 __swap_allocator(this->__alloc(), __x.__alloc(),
2030 integral_constant<bool,__alloc_traits::propagate_on_container_swap::value>());
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002031#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002032 __get_db()->swap(this, &__x);
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002033#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00002034}
2035
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002036template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002037bool
2038vector<_Tp, _Allocator>::__invariants() const
2039{
Howard Hinnant76053d72013-06-27 19:35:32 +00002040 if (this->__begin_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002041 {
Howard Hinnant76053d72013-06-27 19:35:32 +00002042 if (this->__end_ != nullptr || this->__end_cap() != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002043 return false;
2044 }
2045 else
2046 {
2047 if (this->__begin_ > this->__end_)
2048 return false;
2049 if (this->__begin_ == this->__end_cap())
2050 return false;
2051 if (this->__end_ > this->__end_cap())
2052 return false;
2053 }
2054 return true;
2055}
2056
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002057#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002058
Howard Hinnantc51e1022010-05-11 19:42:16 +00002059template <class _Tp, class _Allocator>
Howard Hinnant27e0e772011-09-14 18:33:51 +00002060bool
2061vector<_Tp, _Allocator>::__dereferenceable(const const_iterator* __i) const
2062{
2063 return this->__begin_ <= __i->base() && __i->base() < this->__end_;
2064}
2065
2066template <class _Tp, class _Allocator>
2067bool
2068vector<_Tp, _Allocator>::__decrementable(const const_iterator* __i) const
2069{
2070 return this->__begin_ < __i->base() && __i->base() <= this->__end_;
2071}
2072
2073template <class _Tp, class _Allocator>
2074bool
2075vector<_Tp, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
2076{
2077 const_pointer __p = __i->base() + __n;
2078 return this->__begin_ <= __p && __p <= this->__end_;
2079}
2080
2081template <class _Tp, class _Allocator>
2082bool
2083vector<_Tp, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
2084{
2085 const_pointer __p = __i->base() + __n;
2086 return this->__begin_ <= __p && __p < this->__end_;
2087}
2088
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002089#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002090
2091template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002092inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002093void
2094vector<_Tp, _Allocator>::__invalidate_all_iterators()
2095{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002096#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002097 __get_db()->__invalidate_all(this);
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002098#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00002099}
2100
Eric Fiselier69c51982016-12-28 06:06:09 +00002101
2102template <class _Tp, class _Allocator>
2103inline _LIBCPP_INLINE_VISIBILITY
2104void
2105vector<_Tp, _Allocator>::__invalidate_iterators_past(pointer __new_last) {
2106#if _LIBCPP_DEBUG_LEVEL >= 2
2107 __c_node* __c = __get_db()->__find_c_and_lock(this);
2108 for (__i_node** __p = __c->end_; __p != __c->beg_; ) {
2109 --__p;
2110 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
2111 if (__i->base() > __new_last) {
2112 (*__p)->__c_ = nullptr;
2113 if (--__c->end_ != __p)
2114 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
2115 }
2116 }
2117 __get_db()->unlock();
2118#else
2119 ((void)__new_last);
2120#endif
2121}
2122
Howard Hinnantc51e1022010-05-11 19:42:16 +00002123// vector<bool>
2124
2125template <class _Allocator> class vector<bool, _Allocator>;
2126
2127template <class _Allocator> struct hash<vector<bool, _Allocator> >;
2128
2129template <class _Allocator>
Howard Hinnantb4ef6482011-07-02 20:33:23 +00002130struct __has_storage_type<vector<bool, _Allocator> >
2131{
2132 static const bool value = true;
2133};
2134
2135template <class _Allocator>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002136class _LIBCPP_TYPE_VIS_ONLY vector<bool, _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002137 : private __vector_base_common<true>
2138{
2139public:
2140 typedef vector __self;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002141 typedef bool value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002142 typedef _Allocator allocator_type;
2143 typedef allocator_traits<allocator_type> __alloc_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002144 typedef typename __alloc_traits::size_type size_type;
2145 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant896b7622012-05-07 16:50:38 +00002146 typedef size_type __storage_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002147 typedef __bit_iterator<vector, false> pointer;
2148 typedef __bit_iterator<vector, true> const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002149 typedef pointer iterator;
2150 typedef const_pointer const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002151 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
2152 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002153
2154private:
Marshall Clow940e01c2015-04-07 05:21:38 +00002155 typedef typename __rebind_alloc_helper<__alloc_traits, __storage_type>::type __storage_allocator;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002156 typedef allocator_traits<__storage_allocator> __storage_traits;
2157 typedef typename __storage_traits::pointer __storage_pointer;
2158 typedef typename __storage_traits::const_pointer __const_storage_pointer;
2159
2160 __storage_pointer __begin_;
2161 size_type __size_;
2162 __compressed_pair<size_type, __storage_allocator> __cap_alloc_;
Howard Hinnant71d1c192011-07-09 15:50:42 +00002163public:
Howard Hinnantb4ef6482011-07-02 20:33:23 +00002164 typedef __bit_reference<vector> reference;
2165 typedef __bit_const_reference<vector> const_reference;
Howard Hinnant71d1c192011-07-09 15:50:42 +00002166private:
Howard Hinnant1c936782011-06-03 19:40:40 +00002167 _LIBCPP_INLINE_VISIBILITY
2168 size_type& __cap() _NOEXCEPT
2169 {return __cap_alloc_.first();}
2170 _LIBCPP_INLINE_VISIBILITY
2171 const size_type& __cap() const _NOEXCEPT
2172 {return __cap_alloc_.first();}
2173 _LIBCPP_INLINE_VISIBILITY
2174 __storage_allocator& __alloc() _NOEXCEPT
2175 {return __cap_alloc_.second();}
2176 _LIBCPP_INLINE_VISIBILITY
2177 const __storage_allocator& __alloc() const _NOEXCEPT
2178 {return __cap_alloc_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002179
2180 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
2181
Howard Hinnant1c936782011-06-03 19:40:40 +00002182 _LIBCPP_INLINE_VISIBILITY
2183 static size_type __internal_cap_to_external(size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002184 {return __n * __bits_per_word;}
Howard Hinnant1c936782011-06-03 19:40:40 +00002185 _LIBCPP_INLINE_VISIBILITY
2186 static size_type __external_cap_to_internal(size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002187 {return (__n - 1) / __bits_per_word + 1;}
2188
2189public:
Howard Hinnant1c936782011-06-03 19:40:40 +00002190 _LIBCPP_INLINE_VISIBILITY
Marshall Clowe546cbb2015-06-04 02:05:41 +00002191 vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clow8f282f42015-06-04 00:10:20 +00002192
2193 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
2194#if _LIBCPP_STD_VER <= 14
2195 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
2196#else
2197 _NOEXCEPT;
2198#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002199 ~vector();
2200 explicit vector(size_type __n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00002201#if _LIBCPP_STD_VER > 11
2202 explicit vector(size_type __n, const allocator_type& __a);
2203#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002204 vector(size_type __n, const value_type& __v);
2205 vector(size_type __n, const value_type& __v, const allocator_type& __a);
2206 template <class _InputIterator>
2207 vector(_InputIterator __first, _InputIterator __last,
2208 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2209 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2210 template <class _InputIterator>
2211 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2212 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2213 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2214 template <class _ForwardIterator>
2215 vector(_ForwardIterator __first, _ForwardIterator __last,
2216 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2217 template <class _ForwardIterator>
2218 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2219 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2220
2221 vector(const vector& __v);
2222 vector(const vector& __v, const allocator_type& __a);
2223 vector& operator=(const vector& __v);
Howard Hinnant33711792011-08-12 21:56:02 +00002224#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002225 vector(initializer_list<value_type> __il);
2226 vector(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnant33711792011-08-12 21:56:02 +00002227#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002228
Howard Hinnant74279a52010-09-04 23:28:19 +00002229#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant1c936782011-06-03 19:40:40 +00002230 _LIBCPP_INLINE_VISIBILITY
2231 vector(vector&& __v)
Marshall Clowe5108202015-07-14 14:46:32 +00002232#if _LIBCPP_STD_VER > 14
2233 _NOEXCEPT;
2234#else
Howard Hinnant1c936782011-06-03 19:40:40 +00002235 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowe5108202015-07-14 14:46:32 +00002236#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002237 vector(vector&& __v, const allocator_type& __a);
Howard Hinnant1c936782011-06-03 19:40:40 +00002238 _LIBCPP_INLINE_VISIBILITY
2239 vector& operator=(vector&& __v)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002240 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Howard Hinnant74279a52010-09-04 23:28:19 +00002241#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant33711792011-08-12 21:56:02 +00002242#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002243 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002244 vector& operator=(initializer_list<value_type> __il)
2245 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnant33711792011-08-12 21:56:02 +00002246#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002247
2248 template <class _InputIterator>
2249 typename enable_if
2250 <
2251 __is_input_iterator<_InputIterator>::value &&
2252 !__is_forward_iterator<_InputIterator>::value,
2253 void
2254 >::type
2255 assign(_InputIterator __first, _InputIterator __last);
2256 template <class _ForwardIterator>
2257 typename enable_if
2258 <
2259 __is_forward_iterator<_ForwardIterator>::value,
2260 void
2261 >::type
2262 assign(_ForwardIterator __first, _ForwardIterator __last);
2263
2264 void assign(size_type __n, const value_type& __x);
Howard Hinnant33711792011-08-12 21:56:02 +00002265#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002266 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002267 void assign(initializer_list<value_type> __il)
2268 {assign(__il.begin(), __il.end());}
Howard Hinnant33711792011-08-12 21:56:02 +00002269#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002270
Howard Hinnant1c936782011-06-03 19:40:40 +00002271 _LIBCPP_INLINE_VISIBILITY allocator_type get_allocator() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002272 {return allocator_type(this->__alloc());}
2273
Howard Hinnant1c936782011-06-03 19:40:40 +00002274 size_type max_size() const _NOEXCEPT;
2275 _LIBCPP_INLINE_VISIBILITY
2276 size_type capacity() const _NOEXCEPT
2277 {return __internal_cap_to_external(__cap());}
2278 _LIBCPP_INLINE_VISIBILITY
2279 size_type size() const _NOEXCEPT
2280 {return __size_;}
2281 _LIBCPP_INLINE_VISIBILITY
2282 bool empty() const _NOEXCEPT
2283 {return __size_ == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002284 void reserve(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +00002285 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002286
Howard Hinnant1c936782011-06-03 19:40:40 +00002287 _LIBCPP_INLINE_VISIBILITY
2288 iterator begin() _NOEXCEPT
2289 {return __make_iter(0);}
2290 _LIBCPP_INLINE_VISIBILITY
2291 const_iterator begin() const _NOEXCEPT
2292 {return __make_iter(0);}
2293 _LIBCPP_INLINE_VISIBILITY
2294 iterator end() _NOEXCEPT
2295 {return __make_iter(__size_);}
2296 _LIBCPP_INLINE_VISIBILITY
2297 const_iterator end() const _NOEXCEPT
2298 {return __make_iter(__size_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002299
Howard Hinnant1c936782011-06-03 19:40:40 +00002300 _LIBCPP_INLINE_VISIBILITY
2301 reverse_iterator rbegin() _NOEXCEPT
2302 {return reverse_iterator(end());}
2303 _LIBCPP_INLINE_VISIBILITY
2304 const_reverse_iterator rbegin() const _NOEXCEPT
2305 {return const_reverse_iterator(end());}
2306 _LIBCPP_INLINE_VISIBILITY
2307 reverse_iterator rend() _NOEXCEPT
2308 {return reverse_iterator(begin());}
2309 _LIBCPP_INLINE_VISIBILITY
2310 const_reverse_iterator rend() const _NOEXCEPT
2311 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002312
Howard Hinnant1c936782011-06-03 19:40:40 +00002313 _LIBCPP_INLINE_VISIBILITY
2314 const_iterator cbegin() const _NOEXCEPT
2315 {return __make_iter(0);}
2316 _LIBCPP_INLINE_VISIBILITY
2317 const_iterator cend() const _NOEXCEPT
2318 {return __make_iter(__size_);}
2319 _LIBCPP_INLINE_VISIBILITY
2320 const_reverse_iterator crbegin() const _NOEXCEPT
2321 {return rbegin();}
2322 _LIBCPP_INLINE_VISIBILITY
2323 const_reverse_iterator crend() const _NOEXCEPT
2324 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002325
2326 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) {return __make_ref(__n);}
2327 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const {return __make_ref(__n);}
2328 reference at(size_type __n);
2329 const_reference at(size_type __n) const;
2330
2331 _LIBCPP_INLINE_VISIBILITY reference front() {return __make_ref(0);}
2332 _LIBCPP_INLINE_VISIBILITY const_reference front() const {return __make_ref(0);}
2333 _LIBCPP_INLINE_VISIBILITY reference back() {return __make_ref(__size_ - 1);}
2334 _LIBCPP_INLINE_VISIBILITY const_reference back() const {return __make_ref(__size_ - 1);}
2335
2336 void push_back(const value_type& __x);
Marshall Clowc46bb8e2013-08-13 23:54:12 +00002337#if _LIBCPP_STD_VER > 11
2338 template <class... _Args>
Eric Fiselier34ba5b92016-07-21 03:20:17 +00002339 _LIBCPP_INLINE_VISIBILITY reference emplace_back(_Args&&... __args) {
2340 push_back ( value_type ( _VSTD::forward<_Args>(__args)... ));
2341 return this->back();
2342 }
Marshall Clowc46bb8e2013-08-13 23:54:12 +00002343#endif
2344
Howard Hinnantc51e1022010-05-11 19:42:16 +00002345 _LIBCPP_INLINE_VISIBILITY void pop_back() {--__size_;}
2346
Marshall Clowc46bb8e2013-08-13 23:54:12 +00002347#if _LIBCPP_STD_VER > 11
2348 template <class... _Args>
2349 _LIBCPP_INLINE_VISIBILITY iterator emplace(const_iterator position, _Args&&... __args)
2350 { return insert ( position, value_type ( _VSTD::forward<_Args>(__args)... )); }
2351#endif
2352
Howard Hinnantc51e1022010-05-11 19:42:16 +00002353 iterator insert(const_iterator __position, const value_type& __x);
2354 iterator insert(const_iterator __position, size_type __n, const value_type& __x);
2355 iterator insert(const_iterator __position, size_type __n, const_reference __x);
2356 template <class _InputIterator>
2357 typename enable_if
2358 <
2359 __is_input_iterator <_InputIterator>::value &&
2360 !__is_forward_iterator<_InputIterator>::value,
2361 iterator
2362 >::type
2363 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
2364 template <class _ForwardIterator>
2365 typename enable_if
2366 <
2367 __is_forward_iterator<_ForwardIterator>::value,
2368 iterator
2369 >::type
2370 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnant33711792011-08-12 21:56:02 +00002371#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002372 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002373 iterator insert(const_iterator __position, initializer_list<value_type> __il)
2374 {return insert(__position, __il.begin(), __il.end());}
Howard Hinnant33711792011-08-12 21:56:02 +00002375#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002376
Howard Hinnantcf823322010-12-17 14:46:43 +00002377 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002378 iterator erase(const_iterator __first, const_iterator __last);
2379
Howard Hinnant1c936782011-06-03 19:40:40 +00002380 _LIBCPP_INLINE_VISIBILITY
2381 void clear() _NOEXCEPT {__size_ = 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002382
Howard Hinnant1c936782011-06-03 19:40:40 +00002383 void swap(vector&)
Marshall Clow8982dcd2015-07-13 20:04:56 +00002384#if _LIBCPP_STD_VER >= 14
2385 _NOEXCEPT;
2386#else
Eric Fiselier69c51982016-12-28 06:06:09 +00002387 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00002388 __is_nothrow_swappable<allocator_type>::value);
2389#endif
Marshall Clow89eb3822016-04-07 14:20:31 +00002390 static void swap(reference __x, reference __y) _NOEXCEPT { _VSTD::swap(__x, __y); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002391
2392 void resize(size_type __sz, value_type __x = false);
Howard Hinnant1c936782011-06-03 19:40:40 +00002393 void flip() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002394
2395 bool __invariants() const;
2396
2397private:
Howard Hinnantcf823322010-12-17 14:46:43 +00002398 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002399 void allocate(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +00002400 void deallocate() _NOEXCEPT;
2401 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantea382952013-08-14 18:00:20 +00002402 static size_type __align_it(size_type __new_size) _NOEXCEPT
Marshall Clow66f34152014-07-28 15:02:42 +00002403 {return __new_size + (__bits_per_word-1) & ~((size_type)__bits_per_word-1);};
Howard Hinnantcf823322010-12-17 14:46:43 +00002404 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
2405 _LIBCPP_INLINE_VISIBILITY void __construct_at_end(size_type __n, bool __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002406 template <class _ForwardIterator>
2407 typename enable_if
2408 <
2409 __is_forward_iterator<_ForwardIterator>::value,
2410 void
2411 >::type
2412 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
2413 void __append(size_type __n, const_reference __x);
Howard Hinnant1c936782011-06-03 19:40:40 +00002414 _LIBCPP_INLINE_VISIBILITY
2415 reference __make_ref(size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002416 {return reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnant1c936782011-06-03 19:40:40 +00002417 _LIBCPP_INLINE_VISIBILITY
2418 const_reference __make_ref(size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002419 {return const_reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnant1c936782011-06-03 19:40:40 +00002420 _LIBCPP_INLINE_VISIBILITY
2421 iterator __make_iter(size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002422 {return iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnant1c936782011-06-03 19:40:40 +00002423 _LIBCPP_INLINE_VISIBILITY
2424 const_iterator __make_iter(size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002425 {return const_iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnant1c936782011-06-03 19:40:40 +00002426 _LIBCPP_INLINE_VISIBILITY
2427 iterator __const_iterator_cast(const_iterator __p) _NOEXCEPT
Howard Hinnant76053d72013-06-27 19:35:32 +00002428 {return begin() + (__p - cbegin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002429
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002430 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002431 void __copy_assign_alloc(const vector& __v)
2432 {__copy_assign_alloc(__v, integral_constant<bool,
2433 __storage_traits::propagate_on_container_copy_assignment::value>());}
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002434 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002435 void __copy_assign_alloc(const vector& __c, true_type)
2436 {
2437 if (__alloc() != __c.__alloc())
2438 deallocate();
2439 __alloc() = __c.__alloc();
2440 }
2441
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002442 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00002443 void __copy_assign_alloc(const vector&, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002444 {}
2445
2446 void __move_assign(vector& __c, false_type);
Howard Hinnant1c936782011-06-03 19:40:40 +00002447 void __move_assign(vector& __c, true_type)
2448 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002449 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002450 void __move_assign_alloc(vector& __c)
Howard Hinnant1c936782011-06-03 19:40:40 +00002451 _NOEXCEPT_(
2452 !__storage_traits::propagate_on_container_move_assignment::value ||
2453 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002454 {__move_assign_alloc(__c, integral_constant<bool,
2455 __storage_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002456 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +00002457 void __move_assign_alloc(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002458 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002459 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002460 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002461 }
2462
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002463 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00002464 void __move_assign_alloc(vector&, false_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002465 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002466 {}
2467
Howard Hinnant1c936782011-06-03 19:40:40 +00002468 size_t __hash_code() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002469
2470 friend class __bit_reference<vector>;
2471 friend class __bit_const_reference<vector>;
2472 friend class __bit_iterator<vector, false>;
2473 friend class __bit_iterator<vector, true>;
Howard Hinnant4210a0f2012-08-17 17:10:18 +00002474 friend struct __bit_array<vector>;
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002475 friend struct _LIBCPP_TYPE_VIS_ONLY hash<vector>;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002476};
2477
2478template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002479inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002480void
2481vector<bool, _Allocator>::__invalidate_all_iterators()
2482{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002483}
2484
2485// Allocate space for __n objects
2486// throws length_error if __n > max_size()
2487// throws (probably bad_alloc) if memory run out
2488// Precondition: __begin_ == __end_ == __cap() == 0
2489// Precondition: __n > 0
2490// Postcondition: capacity() == __n
2491// Postcondition: size() == 0
2492template <class _Allocator>
2493void
2494vector<bool, _Allocator>::allocate(size_type __n)
2495{
2496 if (__n > max_size())
2497 this->__throw_length_error();
2498 __n = __external_cap_to_internal(__n);
2499 this->__begin_ = __storage_traits::allocate(this->__alloc(), __n);
2500 this->__size_ = 0;
2501 this->__cap() = __n;
2502}
2503
2504template <class _Allocator>
2505void
Howard Hinnant1c936782011-06-03 19:40:40 +00002506vector<bool, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002507{
Howard Hinnant76053d72013-06-27 19:35:32 +00002508 if (this->__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002509 {
2510 __storage_traits::deallocate(this->__alloc(), this->__begin_, __cap());
2511 __invalidate_all_iterators();
Howard Hinnant76053d72013-06-27 19:35:32 +00002512 this->__begin_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002513 this->__size_ = this->__cap() = 0;
2514 }
2515}
2516
2517template <class _Allocator>
2518typename vector<bool, _Allocator>::size_type
Howard Hinnant1c936782011-06-03 19:40:40 +00002519vector<bool, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002520{
2521 size_type __amax = __storage_traits::max_size(__alloc());
2522 size_type __nmax = numeric_limits<size_type>::max() / 2; // end() >= begin(), always
2523 if (__nmax / __bits_per_word <= __amax)
2524 return __nmax;
2525 return __internal_cap_to_external(__amax);
2526}
2527
2528// Precondition: __new_size > capacity()
2529template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002530inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002531typename vector<bool, _Allocator>::size_type
2532vector<bool, _Allocator>::__recommend(size_type __new_size) const
2533{
2534 const size_type __ms = max_size();
2535 if (__new_size > __ms)
2536 this->__throw_length_error();
2537 const size_type __cap = capacity();
2538 if (__cap >= __ms / 2)
2539 return __ms;
Howard Hinnantea382952013-08-14 18:00:20 +00002540 return _VSTD::max(2*__cap, __align_it(__new_size));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002541}
2542
2543// Default constructs __n objects starting at __end_
2544// Precondition: __n > 0
2545// Precondition: size() + __n <= capacity()
2546// Postcondition: size() == size() + __n
2547template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002548inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002549void
2550vector<bool, _Allocator>::__construct_at_end(size_type __n, bool __x)
2551{
2552 size_type __old_size = this->__size_;
2553 this->__size_ += __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002554 _VSTD::fill_n(__make_iter(__old_size), __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002555}
2556
2557template <class _Allocator>
2558template <class _ForwardIterator>
2559typename enable_if
2560<
2561 __is_forward_iterator<_ForwardIterator>::value,
2562 void
2563>::type
2564vector<bool, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
2565{
2566 size_type __old_size = this->__size_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002567 this->__size_ += _VSTD::distance(__first, __last);
2568 _VSTD::copy(__first, __last, __make_iter(__old_size));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002569}
2570
2571template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002572inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002573vector<bool, _Allocator>::vector()
Marshall Clowe546cbb2015-06-04 02:05:41 +00002574 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant76053d72013-06-27 19:35:32 +00002575 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002576 __size_(0),
2577 __cap_alloc_(0)
2578{
2579}
2580
2581template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002582inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002583vector<bool, _Allocator>::vector(const allocator_type& __a)
Marshall Clow8f282f42015-06-04 00:10:20 +00002584#if _LIBCPP_STD_VER <= 14
2585 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
2586#else
2587 _NOEXCEPT
2588#endif
Howard Hinnant76053d72013-06-27 19:35:32 +00002589 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002590 __size_(0),
2591 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2592{
2593}
2594
2595template <class _Allocator>
2596vector<bool, _Allocator>::vector(size_type __n)
Howard Hinnant76053d72013-06-27 19:35:32 +00002597 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002598 __size_(0),
2599 __cap_alloc_(0)
2600{
2601 if (__n > 0)
2602 {
2603 allocate(__n);
2604 __construct_at_end(__n, false);
2605 }
2606}
2607
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00002608#if _LIBCPP_STD_VER > 11
2609template <class _Allocator>
2610vector<bool, _Allocator>::vector(size_type __n, const allocator_type& __a)
2611 : __begin_(nullptr),
2612 __size_(0),
2613 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2614{
2615 if (__n > 0)
2616 {
2617 allocate(__n);
2618 __construct_at_end(__n, false);
2619 }
2620}
2621#endif
2622
Howard Hinnantc51e1022010-05-11 19:42:16 +00002623template <class _Allocator>
2624vector<bool, _Allocator>::vector(size_type __n, const value_type& __x)
Howard Hinnant76053d72013-06-27 19:35:32 +00002625 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002626 __size_(0),
2627 __cap_alloc_(0)
2628{
2629 if (__n > 0)
2630 {
2631 allocate(__n);
2632 __construct_at_end(__n, __x);
2633 }
2634}
2635
2636template <class _Allocator>
2637vector<bool, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002638 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002639 __size_(0),
2640 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2641{
2642 if (__n > 0)
2643 {
2644 allocate(__n);
2645 __construct_at_end(__n, __x);
2646 }
2647}
2648
2649template <class _Allocator>
2650template <class _InputIterator>
2651vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last,
2652 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2653 !__is_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002654 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002655 __size_(0),
2656 __cap_alloc_(0)
2657{
2658#ifndef _LIBCPP_NO_EXCEPTIONS
2659 try
2660 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002661#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002662 for (; __first != __last; ++__first)
2663 push_back(*__first);
2664#ifndef _LIBCPP_NO_EXCEPTIONS
2665 }
2666 catch (...)
2667 {
Howard Hinnant76053d72013-06-27 19:35:32 +00002668 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002669 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2670 __invalidate_all_iterators();
2671 throw;
2672 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002673#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002674}
2675
2676template <class _Allocator>
2677template <class _InputIterator>
2678vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2679 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2680 !__is_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002681 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002682 __size_(0),
2683 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2684{
2685#ifndef _LIBCPP_NO_EXCEPTIONS
2686 try
2687 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002688#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002689 for (; __first != __last; ++__first)
2690 push_back(*__first);
2691#ifndef _LIBCPP_NO_EXCEPTIONS
2692 }
2693 catch (...)
2694 {
Howard Hinnant76053d72013-06-27 19:35:32 +00002695 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002696 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2697 __invalidate_all_iterators();
2698 throw;
2699 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002700#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002701}
2702
2703template <class _Allocator>
2704template <class _ForwardIterator>
2705vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last,
2706 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002707 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002708 __size_(0),
2709 __cap_alloc_(0)
2710{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002711 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002712 if (__n > 0)
2713 {
2714 allocate(__n);
2715 __construct_at_end(__first, __last);
2716 }
2717}
2718
2719template <class _Allocator>
2720template <class _ForwardIterator>
2721vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2722 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002723 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002724 __size_(0),
2725 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2726{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002727 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002728 if (__n > 0)
2729 {
2730 allocate(__n);
2731 __construct_at_end(__first, __last);
2732 }
2733}
2734
Howard Hinnant33711792011-08-12 21:56:02 +00002735#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2736
Howard Hinnantc51e1022010-05-11 19:42:16 +00002737template <class _Allocator>
2738vector<bool, _Allocator>::vector(initializer_list<value_type> __il)
Howard Hinnant76053d72013-06-27 19:35:32 +00002739 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002740 __size_(0),
2741 __cap_alloc_(0)
2742{
2743 size_type __n = static_cast<size_type>(__il.size());
2744 if (__n > 0)
2745 {
2746 allocate(__n);
2747 __construct_at_end(__il.begin(), __il.end());
2748 }
2749}
2750
2751template <class _Allocator>
2752vector<bool, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002753 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002754 __size_(0),
2755 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2756{
2757 size_type __n = static_cast<size_type>(__il.size());
2758 if (__n > 0)
2759 {
2760 allocate(__n);
2761 __construct_at_end(__il.begin(), __il.end());
2762 }
2763}
2764
Howard Hinnant33711792011-08-12 21:56:02 +00002765#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2766
Howard Hinnantc51e1022010-05-11 19:42:16 +00002767template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002768vector<bool, _Allocator>::~vector()
2769{
Howard Hinnant76053d72013-06-27 19:35:32 +00002770 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002771 __storage_traits::deallocate(__alloc(), __begin_, __cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002772 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002773}
2774
2775template <class _Allocator>
2776vector<bool, _Allocator>::vector(const vector& __v)
Howard Hinnant76053d72013-06-27 19:35:32 +00002777 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002778 __size_(0),
2779 __cap_alloc_(0, __storage_traits::select_on_container_copy_construction(__v.__alloc()))
2780{
2781 if (__v.size() > 0)
2782 {
2783 allocate(__v.size());
2784 __construct_at_end(__v.begin(), __v.end());
2785 }
2786}
2787
2788template <class _Allocator>
2789vector<bool, _Allocator>::vector(const vector& __v, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002790 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002791 __size_(0),
2792 __cap_alloc_(0, __a)
2793{
2794 if (__v.size() > 0)
2795 {
2796 allocate(__v.size());
2797 __construct_at_end(__v.begin(), __v.end());
2798 }
2799}
2800
2801template <class _Allocator>
2802vector<bool, _Allocator>&
2803vector<bool, _Allocator>::operator=(const vector& __v)
2804{
2805 if (this != &__v)
2806 {
2807 __copy_assign_alloc(__v);
2808 if (__v.__size_)
2809 {
2810 if (__v.__size_ > capacity())
2811 {
2812 deallocate();
2813 allocate(__v.__size_);
2814 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002815 _VSTD::copy(__v.__begin_, __v.__begin_ + __external_cap_to_internal(__v.__size_), __begin_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002816 }
2817 __size_ = __v.__size_;
2818 }
2819 return *this;
2820}
2821
Howard Hinnant74279a52010-09-04 23:28:19 +00002822#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2823
Howard Hinnantc51e1022010-05-11 19:42:16 +00002824template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002825inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002826vector<bool, _Allocator>::vector(vector&& __v)
Marshall Clowe5108202015-07-14 14:46:32 +00002827#if _LIBCPP_STD_VER > 14
2828 _NOEXCEPT
2829#else
Howard Hinnant1c936782011-06-03 19:40:40 +00002830 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowe5108202015-07-14 14:46:32 +00002831#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002832 : __begin_(__v.__begin_),
2833 __size_(__v.__size_),
2834 __cap_alloc_(__v.__cap_alloc_)
2835{
Howard Hinnant76053d72013-06-27 19:35:32 +00002836 __v.__begin_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002837 __v.__size_ = 0;
2838 __v.__cap() = 0;
2839}
2840
2841template <class _Allocator>
2842vector<bool, _Allocator>::vector(vector&& __v, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002843 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002844 __size_(0),
2845 __cap_alloc_(0, __a)
2846{
2847 if (__a == allocator_type(__v.__alloc()))
2848 {
2849 this->__begin_ = __v.__begin_;
2850 this->__size_ = __v.__size_;
2851 this->__cap() = __v.__cap();
2852 __v.__begin_ = nullptr;
2853 __v.__cap() = __v.__size_ = 0;
2854 }
2855 else if (__v.size() > 0)
2856 {
2857 allocate(__v.size());
2858 __construct_at_end(__v.begin(), __v.end());
2859 }
2860}
2861
2862template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002863inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002864vector<bool, _Allocator>&
2865vector<bool, _Allocator>::operator=(vector&& __v)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002866 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002867{
2868 __move_assign(__v, integral_constant<bool,
2869 __storage_traits::propagate_on_container_move_assignment::value>());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002870 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002871}
2872
2873template <class _Allocator>
2874void
2875vector<bool, _Allocator>::__move_assign(vector& __c, false_type)
2876{
2877 if (__alloc() != __c.__alloc())
2878 assign(__c.begin(), __c.end());
2879 else
2880 __move_assign(__c, true_type());
2881}
2882
2883template <class _Allocator>
2884void
2885vector<bool, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002886 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002887{
2888 deallocate();
Marshall Clowb4871fa2014-07-21 15:15:15 +00002889 __move_assign_alloc(__c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002890 this->__begin_ = __c.__begin_;
2891 this->__size_ = __c.__size_;
2892 this->__cap() = __c.__cap();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002893 __c.__begin_ = nullptr;
2894 __c.__cap() = __c.__size_ = 0;
2895}
Howard Hinnant74279a52010-09-04 23:28:19 +00002896
2897#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002898
2899template <class _Allocator>
2900void
2901vector<bool, _Allocator>::assign(size_type __n, const value_type& __x)
2902{
2903 __size_ = 0;
2904 if (__n > 0)
2905 {
2906 size_type __c = capacity();
2907 if (__n <= __c)
2908 __size_ = __n;
2909 else
2910 {
2911 vector __v(__alloc());
2912 __v.reserve(__recommend(__n));
2913 __v.__size_ = __n;
2914 swap(__v);
2915 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002916 _VSTD::fill_n(begin(), __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002917 }
Eric Fiselier69c51982016-12-28 06:06:09 +00002918 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002919}
2920
2921template <class _Allocator>
2922template <class _InputIterator>
2923typename enable_if
2924<
2925 __is_input_iterator<_InputIterator>::value &&
2926 !__is_forward_iterator<_InputIterator>::value,
2927 void
2928>::type
2929vector<bool, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2930{
2931 clear();
2932 for (; __first != __last; ++__first)
2933 push_back(*__first);
2934}
2935
2936template <class _Allocator>
2937template <class _ForwardIterator>
2938typename enable_if
2939<
2940 __is_forward_iterator<_ForwardIterator>::value,
2941 void
2942>::type
2943vector<bool, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2944{
2945 clear();
Eric Fiselier6003c772016-12-23 23:37:52 +00002946 difference_type __ns = _VSTD::distance(__first, __last);
2947 _LIBCPP_ASSERT(__ns >= 0, "invalid range specified");
2948 const size_t __n = static_cast<size_type>(__ns);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002949 if (__n)
2950 {
2951 if (__n > capacity())
2952 {
2953 deallocate();
2954 allocate(__n);
2955 }
2956 __construct_at_end(__first, __last);
2957 }
2958}
2959
2960template <class _Allocator>
2961void
2962vector<bool, _Allocator>::reserve(size_type __n)
2963{
2964 if (__n > capacity())
2965 {
2966 vector __v(this->__alloc());
2967 __v.allocate(__n);
2968 __v.__construct_at_end(this->begin(), this->end());
2969 swap(__v);
2970 __invalidate_all_iterators();
2971 }
2972}
2973
2974template <class _Allocator>
2975void
Howard Hinnant1c936782011-06-03 19:40:40 +00002976vector<bool, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002977{
2978 if (__external_cap_to_internal(size()) > __cap())
2979 {
2980#ifndef _LIBCPP_NO_EXCEPTIONS
2981 try
2982 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002983#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002984 vector(*this, allocator_type(__alloc())).swap(*this);
2985#ifndef _LIBCPP_NO_EXCEPTIONS
2986 }
2987 catch (...)
2988 {
2989 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002990#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002991 }
2992}
2993
2994template <class _Allocator>
2995typename vector<bool, _Allocator>::reference
2996vector<bool, _Allocator>::at(size_type __n)
2997{
2998 if (__n >= size())
2999 this->__throw_out_of_range();
3000 return (*this)[__n];
3001}
3002
3003template <class _Allocator>
3004typename vector<bool, _Allocator>::const_reference
3005vector<bool, _Allocator>::at(size_type __n) const
3006{
3007 if (__n >= size())
3008 this->__throw_out_of_range();
3009 return (*this)[__n];
3010}
3011
3012template <class _Allocator>
3013void
3014vector<bool, _Allocator>::push_back(const value_type& __x)
3015{
3016 if (this->__size_ == this->capacity())
3017 reserve(__recommend(this->__size_ + 1));
3018 ++this->__size_;
3019 back() = __x;
3020}
3021
3022template <class _Allocator>
3023typename vector<bool, _Allocator>::iterator
3024vector<bool, _Allocator>::insert(const_iterator __position, const value_type& __x)
3025{
3026 iterator __r;
3027 if (size() < capacity())
3028 {
3029 const_iterator __old_end = end();
3030 ++__size_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003031 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003032 __r = __const_iterator_cast(__position);
3033 }
3034 else
3035 {
3036 vector __v(__alloc());
3037 __v.reserve(__recommend(__size_ + 1));
3038 __v.__size_ = __size_ + 1;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003039 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3040 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003041 swap(__v);
3042 }
3043 *__r = __x;
3044 return __r;
3045}
3046
3047template <class _Allocator>
3048typename vector<bool, _Allocator>::iterator
3049vector<bool, _Allocator>::insert(const_iterator __position, size_type __n, const value_type& __x)
3050{
3051 iterator __r;
3052 size_type __c = capacity();
3053 if (__n <= __c && size() <= __c - __n)
3054 {
3055 const_iterator __old_end = end();
3056 __size_ += __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003057 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003058 __r = __const_iterator_cast(__position);
3059 }
3060 else
3061 {
3062 vector __v(__alloc());
3063 __v.reserve(__recommend(__size_ + __n));
3064 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003065 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3066 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003067 swap(__v);
3068 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003069 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003070 return __r;
3071}
3072
3073template <class _Allocator>
3074template <class _InputIterator>
3075typename enable_if
3076<
3077 __is_input_iterator <_InputIterator>::value &&
3078 !__is_forward_iterator<_InputIterator>::value,
3079 typename vector<bool, _Allocator>::iterator
3080>::type
3081vector<bool, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
3082{
3083 difference_type __off = __position - begin();
3084 iterator __p = __const_iterator_cast(__position);
3085 iterator __old_end = end();
3086 for (; size() != capacity() && __first != __last; ++__first)
3087 {
3088 ++this->__size_;
3089 back() = *__first;
3090 }
3091 vector __v(__alloc());
3092 if (__first != __last)
3093 {
3094#ifndef _LIBCPP_NO_EXCEPTIONS
3095 try
3096 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003097#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003098 __v.assign(__first, __last);
3099 difference_type __old_size = static_cast<difference_type>(__old_end - begin());
3100 difference_type __old_p = __p - begin();
3101 reserve(__recommend(size() + __v.size()));
3102 __p = begin() + __old_p;
3103 __old_end = begin() + __old_size;
3104#ifndef _LIBCPP_NO_EXCEPTIONS
3105 }
3106 catch (...)
3107 {
3108 erase(__old_end, end());
3109 throw;
3110 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003111#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003112 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003113 __p = _VSTD::rotate(__p, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003114 insert(__p, __v.begin(), __v.end());
3115 return begin() + __off;
3116}
3117
3118template <class _Allocator>
3119template <class _ForwardIterator>
3120typename enable_if
3121<
3122 __is_forward_iterator<_ForwardIterator>::value,
3123 typename vector<bool, _Allocator>::iterator
3124>::type
3125vector<bool, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
3126{
Eric Fiselier654dd332016-12-11 05:31:00 +00003127 const difference_type __n_signed = _VSTD::distance(__first, __last);
3128 _LIBCPP_ASSERT(__n_signed >= 0, "invalid range specified");
3129 const size_type __n = static_cast<size_type>(__n_signed);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003130 iterator __r;
3131 size_type __c = capacity();
3132 if (__n <= __c && size() <= __c - __n)
3133 {
3134 const_iterator __old_end = end();
3135 __size_ += __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003136 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003137 __r = __const_iterator_cast(__position);
3138 }
3139 else
3140 {
3141 vector __v(__alloc());
3142 __v.reserve(__recommend(__size_ + __n));
3143 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003144 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3145 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003146 swap(__v);
3147 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003148 _VSTD::copy(__first, __last, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003149 return __r;
3150}
3151
3152template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003153inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003154typename vector<bool, _Allocator>::iterator
3155vector<bool, _Allocator>::erase(const_iterator __position)
3156{
3157 iterator __r = __const_iterator_cast(__position);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003158 _VSTD::copy(__position + 1, this->cend(), __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003159 --__size_;
3160 return __r;
3161}
3162
3163template <class _Allocator>
3164typename vector<bool, _Allocator>::iterator
3165vector<bool, _Allocator>::erase(const_iterator __first, const_iterator __last)
3166{
3167 iterator __r = __const_iterator_cast(__first);
3168 difference_type __d = __last - __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003169 _VSTD::copy(__last, this->cend(), __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003170 __size_ -= __d;
3171 return __r;
3172}
3173
3174template <class _Allocator>
3175void
3176vector<bool, _Allocator>::swap(vector& __x)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003177#if _LIBCPP_STD_VER >= 14
3178 _NOEXCEPT
3179#else
Eric Fiselier69c51982016-12-28 06:06:09 +00003180 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003181 __is_nothrow_swappable<allocator_type>::value)
3182#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003183{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003184 _VSTD::swap(this->__begin_, __x.__begin_);
3185 _VSTD::swap(this->__size_, __x.__size_);
3186 _VSTD::swap(this->__cap(), __x.__cap());
Marshall Clow8982dcd2015-07-13 20:04:56 +00003187 __swap_allocator(this->__alloc(), __x.__alloc(),
3188 integral_constant<bool, __alloc_traits::propagate_on_container_swap::value>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003189}
3190
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003191template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003192void
3193vector<bool, _Allocator>::resize(size_type __sz, value_type __x)
3194{
3195 size_type __cs = size();
3196 if (__cs < __sz)
3197 {
3198 iterator __r;
3199 size_type __c = capacity();
3200 size_type __n = __sz - __cs;
3201 if (__n <= __c && __cs <= __c - __n)
3202 {
3203 __r = end();
3204 __size_ += __n;
3205 }
3206 else
3207 {
3208 vector __v(__alloc());
3209 __v.reserve(__recommend(__size_ + __n));
3210 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003211 __r = _VSTD::copy(cbegin(), cend(), __v.begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003212 swap(__v);
3213 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003214 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003215 }
3216 else
3217 __size_ = __sz;
3218}
3219
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003220template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003221void
Howard Hinnant1c936782011-06-03 19:40:40 +00003222vector<bool, _Allocator>::flip() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003223{
3224 // do middle whole words
3225 size_type __n = __size_;
3226 __storage_pointer __p = __begin_;
3227 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3228 *__p = ~*__p;
3229 // do last partial word
3230 if (__n > 0)
3231 {
3232 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3233 __storage_type __b = *__p & __m;
3234 *__p &= ~__m;
3235 *__p |= ~__b & __m;
3236 }
3237}
3238
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003239template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003240bool
3241vector<bool, _Allocator>::__invariants() const
3242{
Howard Hinnant76053d72013-06-27 19:35:32 +00003243 if (this->__begin_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003244 {
3245 if (this->__size_ != 0 || this->__cap() != 0)
3246 return false;
3247 }
3248 else
3249 {
3250 if (this->__cap() == 0)
3251 return false;
3252 if (this->__size_ > this->capacity())
3253 return false;
3254 }
3255 return true;
3256}
3257
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003258template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003259size_t
Howard Hinnant1c936782011-06-03 19:40:40 +00003260vector<bool, _Allocator>::__hash_code() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003261{
3262 size_t __h = 0;
3263 // do middle whole words
3264 size_type __n = __size_;
3265 __storage_pointer __p = __begin_;
3266 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3267 __h ^= *__p;
3268 // do last partial word
3269 if (__n > 0)
3270 {
3271 const __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3272 __h ^= *__p & __m;
3273 }
3274 return __h;
3275}
3276
3277template <class _Allocator>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003278struct _LIBCPP_TYPE_VIS_ONLY hash<vector<bool, _Allocator> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00003279 : public unary_function<vector<bool, _Allocator>, size_t>
3280{
Howard Hinnant1c265cd2010-09-23 18:58:28 +00003281 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +00003282 size_t operator()(const vector<bool, _Allocator>& __vec) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003283 {return __vec.__hash_code();}
3284};
3285
3286template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003287inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003288bool
3289operator==(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3290{
3291 const typename vector<_Tp, _Allocator>::size_type __sz = __x.size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003292 return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003293}
3294
3295template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003296inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003297bool
3298operator!=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3299{
3300 return !(__x == __y);
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{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003308 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003309}
3310
3311template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003312inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003313bool
3314operator> (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3315{
3316 return __y < __x;
3317}
3318
3319template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003320inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003321bool
3322operator>=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3323{
3324 return !(__x < __y);
3325}
3326
3327template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003328inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003329bool
3330operator<=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3331{
3332 return !(__y < __x);
3333}
3334
3335template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003336inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003337void
3338swap(vector<_Tp, _Allocator>& __x, vector<_Tp, _Allocator>& __y)
Howard Hinnant1c936782011-06-03 19:40:40 +00003339 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00003340{
3341 __x.swap(__y);
3342}
3343
3344_LIBCPP_END_NAMESPACE_STD
3345
3346#endif // _LIBCPP_VECTOR