blob: 54b1e8831d5bc28730b33730b8950a46de50ea56 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===------------------------------ vector --------------------------------===//
3//
Howard Hinnantc566dc32010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantc51e1022010-05-11 19:42:16 +00005//
Howard Hinnantee11c312010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantc51e1022010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_VECTOR
12#define _LIBCPP_VECTOR
13
14/*
15 vector synopsis
16
17namespace std
18{
19
Howard Hinnant3b6579a2010-08-22 00:02:43 +000020template <class T, class Allocator = allocator<T> >
Howard Hinnantc51e1022010-05-11 19:42:16 +000021class vector
Howard Hinnant3b6579a2010-08-22 00:02:43 +000022{
23public:
24 typedef T value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +000025 typedef Allocator allocator_type;
26 typedef typename allocator_type::reference reference;
27 typedef typename allocator_type::const_reference const_reference;
28 typedef implementation-defined iterator;
29 typedef implementation-defined const_iterator;
30 typedef typename allocator_type::size_type size_type;
31 typedef typename allocator_type::difference_type difference_type;
32 typedef typename allocator_type::pointer pointer;
33 typedef typename allocator_type::const_pointer const_pointer;
34 typedef std::reverse_iterator<iterator> reverse_iterator;
35 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
36
Howard Hinnant1c936782011-06-03 19:40:40 +000037 vector()
38 noexcept(is_nothrow_default_constructible<allocator_type>::value);
39 explicit vector(const allocator_type&);
Howard Hinnantc51e1022010-05-11 19:42:16 +000040 explicit vector(size_type n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +000041 explicit vector(size_type n, const allocator_type&); // C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +000042 vector(size_type n, const value_type& value, const allocator_type& = allocator_type());
43 template <class InputIterator>
44 vector(InputIterator first, InputIterator last, const allocator_type& = allocator_type());
45 vector(const vector& x);
Howard Hinnant1c936782011-06-03 19:40:40 +000046 vector(vector&& x)
47 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +000048 vector(initializer_list<value_type> il);
49 vector(initializer_list<value_type> il, const allocator_type& a);
50 ~vector();
51 vector& operator=(const vector& x);
Howard Hinnant1c936782011-06-03 19:40:40 +000052 vector& operator=(vector&& x)
53 noexcept(
Marshall Clow2fe8a8d2015-08-18 18:57:00 +000054 allocator_type::propagate_on_container_move_assignment::value ||
55 allocator_type::is_always_equal::value); // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +000056 vector& operator=(initializer_list<value_type> il);
57 template <class InputIterator>
58 void assign(InputIterator first, InputIterator last);
59 void assign(size_type n, const value_type& u);
60 void assign(initializer_list<value_type> il);
61
Howard Hinnant1c936782011-06-03 19:40:40 +000062 allocator_type get_allocator() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000063
Howard Hinnant1c936782011-06-03 19:40:40 +000064 iterator begin() noexcept;
65 const_iterator begin() const noexcept;
66 iterator end() noexcept;
67 const_iterator end() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000068
Howard Hinnant1c936782011-06-03 19:40:40 +000069 reverse_iterator rbegin() noexcept;
70 const_reverse_iterator rbegin() const noexcept;
71 reverse_iterator rend() noexcept;
72 const_reverse_iterator rend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000073
Howard Hinnant1c936782011-06-03 19:40:40 +000074 const_iterator cbegin() const noexcept;
75 const_iterator cend() const noexcept;
76 const_reverse_iterator crbegin() const noexcept;
77 const_reverse_iterator crend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000078
Howard Hinnant1c936782011-06-03 19:40:40 +000079 size_type size() const noexcept;
80 size_type max_size() const noexcept;
81 size_type capacity() const noexcept;
82 bool empty() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000083 void reserve(size_type n);
Howard Hinnant1c936782011-06-03 19:40:40 +000084 void shrink_to_fit() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000085
86 reference operator[](size_type n);
87 const_reference operator[](size_type n) const;
88 reference at(size_type n);
89 const_reference at(size_type n) const;
90
91 reference front();
92 const_reference front() const;
93 reference back();
94 const_reference back() const;
95
Howard Hinnant1c936782011-06-03 19:40:40 +000096 value_type* data() noexcept;
97 const value_type* data() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000098
99 void push_back(const value_type& x);
100 void push_back(value_type&& x);
101 template <class... Args>
Marshall Clowea52cc42017-01-24 23:09:12 +0000102 reference emplace_back(Args&&... args); // reference in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000103 void pop_back();
104
105 template <class... Args> iterator emplace(const_iterator position, Args&&... args);
106 iterator insert(const_iterator position, const value_type& x);
107 iterator insert(const_iterator position, value_type&& x);
108 iterator insert(const_iterator position, size_type n, const value_type& x);
109 template <class InputIterator>
110 iterator insert(const_iterator position, InputIterator first, InputIterator last);
111 iterator insert(const_iterator position, initializer_list<value_type> il);
112
113 iterator erase(const_iterator position);
114 iterator erase(const_iterator first, const_iterator last);
115
Howard Hinnant1c936782011-06-03 19:40:40 +0000116 void clear() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000117
118 void resize(size_type sz);
119 void resize(size_type sz, const value_type& c);
120
Howard Hinnant1c936782011-06-03 19:40:40 +0000121 void swap(vector&)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000122 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
123 allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000124
125 bool __invariants() const;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000126};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000127
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000128template <class Allocator = allocator<T> >
Howard Hinnantc51e1022010-05-11 19:42:16 +0000129class vector<bool, Allocator>
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000130{
131public:
132 typedef bool value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000133 typedef Allocator allocator_type;
134 typedef implementation-defined iterator;
135 typedef implementation-defined const_iterator;
136 typedef typename allocator_type::size_type size_type;
137 typedef typename allocator_type::difference_type difference_type;
138 typedef iterator pointer;
139 typedef const_iterator const_pointer;
140 typedef std::reverse_iterator<iterator> reverse_iterator;
141 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
142
143 class reference
144 {
145 public:
Howard Hinnant1c936782011-06-03 19:40:40 +0000146 reference(const reference&) noexcept;
147 operator bool() const noexcept;
148 reference& operator=(const bool x) noexcept;
149 reference& operator=(const reference& x) noexcept;
150 iterator operator&() const noexcept;
151 void flip() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000152 };
153
154 class const_reference
155 {
156 public:
Howard Hinnant1c936782011-06-03 19:40:40 +0000157 const_reference(const reference&) noexcept;
158 operator bool() const noexcept;
159 const_iterator operator&() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000160 };
161
Howard Hinnant1c936782011-06-03 19:40:40 +0000162 vector()
163 noexcept(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnantc2734962011-09-02 20:42:31 +0000164 explicit vector(const allocator_type&);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +0000165 explicit vector(size_type n, const allocator_type& a = allocator_type()); // C++14
166 vector(size_type n, const value_type& value, const allocator_type& = allocator_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000167 template <class InputIterator>
168 vector(InputIterator first, InputIterator last, const allocator_type& = allocator_type());
169 vector(const vector& x);
Howard Hinnant1c936782011-06-03 19:40:40 +0000170 vector(vector&& x)
171 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000172 vector(initializer_list<value_type> il);
173 vector(initializer_list<value_type> il, const allocator_type& a);
174 ~vector();
175 vector& operator=(const vector& x);
Howard Hinnant1c936782011-06-03 19:40:40 +0000176 vector& operator=(vector&& x)
177 noexcept(
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000178 allocator_type::propagate_on_container_move_assignment::value ||
179 allocator_type::is_always_equal::value); // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000180 vector& operator=(initializer_list<value_type> il);
181 template <class InputIterator>
182 void assign(InputIterator first, InputIterator last);
183 void assign(size_type n, const value_type& u);
184 void assign(initializer_list<value_type> il);
185
Howard Hinnant1c936782011-06-03 19:40:40 +0000186 allocator_type get_allocator() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000187
Howard Hinnant1c936782011-06-03 19:40:40 +0000188 iterator begin() noexcept;
189 const_iterator begin() const noexcept;
190 iterator end() noexcept;
191 const_iterator end() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000192
Howard Hinnant1c936782011-06-03 19:40:40 +0000193 reverse_iterator rbegin() noexcept;
194 const_reverse_iterator rbegin() const noexcept;
195 reverse_iterator rend() noexcept;
196 const_reverse_iterator rend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000197
Howard Hinnant1c936782011-06-03 19:40:40 +0000198 const_iterator cbegin() const noexcept;
199 const_iterator cend() const noexcept;
200 const_reverse_iterator crbegin() const noexcept;
201 const_reverse_iterator crend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000202
Howard Hinnant1c936782011-06-03 19:40:40 +0000203 size_type size() const noexcept;
204 size_type max_size() const noexcept;
205 size_type capacity() const noexcept;
206 bool empty() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000207 void reserve(size_type n);
Howard Hinnant1c936782011-06-03 19:40:40 +0000208 void shrink_to_fit() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000209
210 reference operator[](size_type n);
211 const_reference operator[](size_type n) const;
212 reference at(size_type n);
213 const_reference at(size_type n) const;
214
215 reference front();
216 const_reference front() const;
217 reference back();
218 const_reference back() const;
219
220 void push_back(const value_type& x);
Marshall Clowea52cc42017-01-24 23:09:12 +0000221 template <class... Args> reference emplace_back(Args&&... args); // C++14; reference in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000222 void pop_back();
223
Marshall Clowc46bb8e2013-08-13 23:54:12 +0000224 template <class... Args> iterator emplace(const_iterator position, Args&&... args); // C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000225 iterator insert(const_iterator position, const value_type& x);
226 iterator insert(const_iterator position, size_type n, const value_type& x);
227 template <class InputIterator>
228 iterator insert(const_iterator position, InputIterator first, InputIterator last);
229 iterator insert(const_iterator position, initializer_list<value_type> il);
230
231 iterator erase(const_iterator position);
232 iterator erase(const_iterator first, const_iterator last);
233
Howard Hinnant1c936782011-06-03 19:40:40 +0000234 void clear() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000235
236 void resize(size_type sz);
237 void resize(size_type sz, value_type x);
238
Howard Hinnant1c936782011-06-03 19:40:40 +0000239 void swap(vector&)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000240 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
241 allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnant1c936782011-06-03 19:40:40 +0000242 void flip() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000243
244 bool __invariants() const;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000245};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000246
247template <class Allocator> struct hash<std::vector<bool, Allocator>>;
248
249template <class T, class Allocator> bool operator==(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
250template <class T, class Allocator> bool operator< (const vector<T,Allocator>& x, const vector<T,Allocator>& y);
251template <class T, class Allocator> bool operator!=(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
252template <class T, class Allocator> bool operator> (const vector<T,Allocator>& x, const vector<T,Allocator>& y);
253template <class T, class Allocator> bool operator>=(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
254template <class T, class Allocator> bool operator<=(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
255
Howard Hinnant1c936782011-06-03 19:40:40 +0000256template <class T, class Allocator>
257void swap(vector<T,Allocator>& x, vector<T,Allocator>& y)
258 noexcept(noexcept(x.swap(y)));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000259
260} // std
261
262*/
263
264#include <__config>
Eric Fiselier876c6862016-02-20 00:19:45 +0000265#include <iosfwd> // for forward declaration of vector
Howard Hinnantc51e1022010-05-11 19:42:16 +0000266#include <__bit_reference>
267#include <type_traits>
268#include <climits>
269#include <limits>
270#include <initializer_list>
271#include <memory>
272#include <stdexcept>
273#include <algorithm>
274#include <cstring>
275#include <__split_buffer>
276#include <__functional_base>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000277
Eric Fiselier14b6de92014-08-10 23:53:08 +0000278#include <__debug>
Howard Hinnante6ff0b62013-08-02 00:26:35 +0000279
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000280#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000281#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000282#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000283
Eric Fiselierf4433a32017-05-31 22:07:49 +0000284_LIBCPP_PUSH_MACROS
285#include <__undef_macros>
286
287
Howard Hinnantc51e1022010-05-11 19:42:16 +0000288_LIBCPP_BEGIN_NAMESPACE_STD
289
290template <bool>
291class __vector_base_common
292{
293protected:
294 _LIBCPP_ALWAYS_INLINE __vector_base_common() {}
Marshall Clow8fea1612016-08-25 15:09:01 +0000295 _LIBCPP_NORETURN void __throw_length_error() const;
296 _LIBCPP_NORETURN void __throw_out_of_range() const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000297};
298
299template <bool __b>
300void
301__vector_base_common<__b>::__throw_length_error() const
302{
Marshall Clow8fea1612016-08-25 15:09:01 +0000303 _VSTD::__throw_length_error("vector");
Howard Hinnantc51e1022010-05-11 19:42:16 +0000304}
305
306template <bool __b>
307void
308__vector_base_common<__b>::__throw_out_of_range() const
309{
Marshall Clow8fea1612016-08-25 15:09:01 +0000310 _VSTD::__throw_out_of_range("vector");
Howard Hinnantc51e1022010-05-11 19:42:16 +0000311}
312
Eric Fiselier1b57fa82016-09-15 22:27:07 +0000313_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __vector_base_common<true>)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000314
315template <class _Tp, class _Allocator>
316class __vector_base
317 : protected __vector_base_common<true>
318{
319protected:
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000320 typedef _Tp value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000321 typedef _Allocator allocator_type;
322 typedef allocator_traits<allocator_type> __alloc_traits;
323 typedef value_type& reference;
324 typedef const value_type& const_reference;
325 typedef typename __alloc_traits::size_type size_type;
326 typedef typename __alloc_traits::difference_type difference_type;
327 typedef typename __alloc_traits::pointer pointer;
328 typedef typename __alloc_traits::const_pointer const_pointer;
329 typedef pointer iterator;
330 typedef const_pointer const_iterator;
331
332 pointer __begin_;
333 pointer __end_;
334 __compressed_pair<pointer, allocator_type> __end_cap_;
335
Howard Hinnant1c936782011-06-03 19:40:40 +0000336 _LIBCPP_INLINE_VISIBILITY
337 allocator_type& __alloc() _NOEXCEPT
338 {return __end_cap_.second();}
339 _LIBCPP_INLINE_VISIBILITY
340 const allocator_type& __alloc() const _NOEXCEPT
341 {return __end_cap_.second();}
342 _LIBCPP_INLINE_VISIBILITY
343 pointer& __end_cap() _NOEXCEPT
344 {return __end_cap_.first();}
345 _LIBCPP_INLINE_VISIBILITY
346 const pointer& __end_cap() const _NOEXCEPT
347 {return __end_cap_.first();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000348
Howard Hinnant1c936782011-06-03 19:40:40 +0000349 _LIBCPP_INLINE_VISIBILITY
350 __vector_base()
351 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnantcf823322010-12-17 14:46:43 +0000352 _LIBCPP_INLINE_VISIBILITY __vector_base(const allocator_type& __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000353 ~__vector_base();
354
Howard Hinnant1c936782011-06-03 19:40:40 +0000355 _LIBCPP_INLINE_VISIBILITY
356 void clear() _NOEXCEPT {__destruct_at_end(__begin_);}
357 _LIBCPP_INLINE_VISIBILITY
358 size_type capacity() const _NOEXCEPT
359 {return static_cast<size_type>(__end_cap() - __begin_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000360
Howard Hinnant1c936782011-06-03 19:40:40 +0000361 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76053d72013-06-27 19:35:32 +0000362 void __destruct_at_end(pointer __new_last) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000363
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000364 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000365 void __copy_assign_alloc(const __vector_base& __c)
366 {__copy_assign_alloc(__c, integral_constant<bool,
367 __alloc_traits::propagate_on_container_copy_assignment::value>());}
368
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000369 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000370 void __move_assign_alloc(__vector_base& __c)
Howard Hinnant1c936782011-06-03 19:40:40 +0000371 _NOEXCEPT_(
372 !__alloc_traits::propagate_on_container_move_assignment::value ||
373 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000374 {__move_assign_alloc(__c, integral_constant<bool,
375 __alloc_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000376private:
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000377 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000378 void __copy_assign_alloc(const __vector_base& __c, true_type)
379 {
380 if (__alloc() != __c.__alloc())
381 {
382 clear();
383 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
384 __begin_ = __end_ = __end_cap() = nullptr;
385 }
386 __alloc() = __c.__alloc();
387 }
388
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000389 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +0000390 void __copy_assign_alloc(const __vector_base&, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000391 {}
392
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000393 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +0000394 void __move_assign_alloc(__vector_base& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +0000395 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000396 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000397 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000398 }
399
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000400 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +0000401 void __move_assign_alloc(__vector_base&, false_type)
Howard Hinnant1c936782011-06-03 19:40:40 +0000402 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000403 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000404};
405
406template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000407inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000408void
Howard Hinnant76053d72013-06-27 19:35:32 +0000409__vector_base<_Tp, _Allocator>::__destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000410{
Bruce Mitchener405fdcd2017-03-23 14:39:23 +0000411 pointer __soon_to_be_end = __end_;
412 while (__new_last != __soon_to_be_end)
413 __alloc_traits::destroy(__alloc(), _VSTD::__to_raw_pointer(--__soon_to_be_end));
414 __end_ = __new_last;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000415}
416
417template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000418inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000419__vector_base<_Tp, _Allocator>::__vector_base()
Howard Hinnant1c936782011-06-03 19:40:40 +0000420 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant76053d72013-06-27 19:35:32 +0000421 : __begin_(nullptr),
422 __end_(nullptr),
423 __end_cap_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000424{
425}
426
427template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000428inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000429__vector_base<_Tp, _Allocator>::__vector_base(const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +0000430 : __begin_(nullptr),
431 __end_(nullptr),
432 __end_cap_(nullptr, __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000433{
434}
435
436template <class _Tp, class _Allocator>
437__vector_base<_Tp, _Allocator>::~__vector_base()
438{
Howard Hinnant76053d72013-06-27 19:35:32 +0000439 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000440 {
441 clear();
442 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
443 }
444}
445
Eric Fiselier876c6862016-02-20 00:19:45 +0000446template <class _Tp, class _Allocator /* = allocator<_Tp> */>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000447class _LIBCPP_TEMPLATE_VIS vector
Howard Hinnantc51e1022010-05-11 19:42:16 +0000448 : private __vector_base<_Tp, _Allocator>
449{
450private:
451 typedef __vector_base<_Tp, _Allocator> __base;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000452 typedef allocator<_Tp> __default_allocator_type;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000453public:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000454 typedef vector __self;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000455 typedef _Tp value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000456 typedef _Allocator allocator_type;
457 typedef typename __base::__alloc_traits __alloc_traits;
458 typedef typename __base::reference reference;
459 typedef typename __base::const_reference const_reference;
460 typedef typename __base::size_type size_type;
461 typedef typename __base::difference_type difference_type;
462 typedef typename __base::pointer pointer;
463 typedef typename __base::const_pointer const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000464 typedef __wrap_iter<pointer> iterator;
465 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000466 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
467 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000468
Howard Hinnanta416ff02013-03-26 19:04:56 +0000469 static_assert((is_same<typename allocator_type::value_type, value_type>::value),
470 "Allocator::value_type must be same type as value_type");
471
Howard Hinnant1c936782011-06-03 19:40:40 +0000472 _LIBCPP_INLINE_VISIBILITY
Marshall Clowe546cbb2015-06-04 02:05:41 +0000473 vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant27e0e772011-09-14 18:33:51 +0000474 {
Howard Hinnanta47c6d52011-09-16 17:29:17 +0000475#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000476 __get_db()->__insert_c(this);
477#endif
478 }
479 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
Marshall Clow8f282f42015-06-04 00:10:20 +0000480#if _LIBCPP_STD_VER <= 14
481 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
482#else
483 _NOEXCEPT
484#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +0000485 : __base(__a)
486 {
Howard Hinnanta47c6d52011-09-16 17:29:17 +0000487#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000488 __get_db()->__insert_c(this);
489#endif
490 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000491 explicit vector(size_type __n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +0000492#if _LIBCPP_STD_VER > 11
493 explicit vector(size_type __n, const allocator_type& __a);
494#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000495 vector(size_type __n, const_reference __x);
496 vector(size_type __n, const_reference __x, const allocator_type& __a);
497 template <class _InputIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +0000498 vector(_InputIterator __first,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000499 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000500 !__is_forward_iterator<_InputIterator>::value &&
501 is_constructible<
502 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +0000503 typename iterator_traits<_InputIterator>::reference>::value,
504 _InputIterator>::type __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000505 template <class _InputIterator>
506 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
507 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000508 !__is_forward_iterator<_InputIterator>::value &&
509 is_constructible<
510 value_type,
511 typename iterator_traits<_InputIterator>::reference>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000512 template <class _ForwardIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +0000513 vector(_ForwardIterator __first,
Howard Hinnant88010252013-03-28 17:44:32 +0000514 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
515 is_constructible<
516 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +0000517 typename iterator_traits<_ForwardIterator>::reference>::value,
518 _ForwardIterator>::type __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000519 template <class _ForwardIterator>
520 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Howard Hinnant88010252013-03-28 17:44:32 +0000521 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
522 is_constructible<
523 value_type,
524 typename iterator_traits<_ForwardIterator>::reference>::value>::type* = 0);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000525
Howard Hinnanta47c6d52011-09-16 17:29:17 +0000526#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000527 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant27e0e772011-09-14 18:33:51 +0000528 ~vector()
529 {
530 __get_db()->__erase_c(this);
531 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000532#endif
533
534 vector(const vector& __x);
535 vector(const vector& __x, const allocator_type& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000536 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000537 vector& operator=(const vector& __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000538
539#ifndef _LIBCPP_CXX03_LANG
540 _LIBCPP_INLINE_VISIBILITY
541 vector(initializer_list<value_type> __il);
542
543 _LIBCPP_INLINE_VISIBILITY
544 vector(initializer_list<value_type> __il, const allocator_type& __a);
545
Howard Hinnantcf823322010-12-17 14:46:43 +0000546 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000547 vector(vector&& __x)
Marshall Clowe5108202015-07-14 14:46:32 +0000548#if _LIBCPP_STD_VER > 14
549 _NOEXCEPT;
550#else
Howard Hinnant1c936782011-06-03 19:40:40 +0000551 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowe5108202015-07-14 14:46:32 +0000552#endif
Eric Fiseliered9e9362017-04-16 02:40:45 +0000553
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));
Eric Fiseliered9e9362017-04-16 02:40:45 +0000559
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000560 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000561 vector& operator=(initializer_list<value_type> __il)
562 {assign(__il.begin(), __il.end()); return *this;}
Eric Fiseliered9e9362017-04-16 02:40:45 +0000563
564#endif // !_LIBCPP_CXX03_LANG
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);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000589
590#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000591 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000592 void assign(initializer_list<value_type> __il)
593 {assign(__il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +0000594#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000595
Howard Hinnant1c936782011-06-03 19:40:40 +0000596 _LIBCPP_INLINE_VISIBILITY
597 allocator_type get_allocator() const _NOEXCEPT
598 {return this->__alloc();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000599
Howard Hinnant1c936782011-06-03 19:40:40 +0000600 _LIBCPP_INLINE_VISIBILITY iterator begin() _NOEXCEPT;
601 _LIBCPP_INLINE_VISIBILITY const_iterator begin() const _NOEXCEPT;
602 _LIBCPP_INLINE_VISIBILITY iterator end() _NOEXCEPT;
603 _LIBCPP_INLINE_VISIBILITY const_iterator end() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000604
Howard Hinnant1c936782011-06-03 19:40:40 +0000605 _LIBCPP_INLINE_VISIBILITY
606 reverse_iterator rbegin() _NOEXCEPT
607 {return reverse_iterator(end());}
608 _LIBCPP_INLINE_VISIBILITY
609 const_reverse_iterator rbegin() const _NOEXCEPT
610 {return const_reverse_iterator(end());}
611 _LIBCPP_INLINE_VISIBILITY
612 reverse_iterator rend() _NOEXCEPT
613 {return reverse_iterator(begin());}
614 _LIBCPP_INLINE_VISIBILITY
615 const_reverse_iterator rend() const _NOEXCEPT
616 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000617
Howard Hinnant1c936782011-06-03 19:40:40 +0000618 _LIBCPP_INLINE_VISIBILITY
619 const_iterator cbegin() const _NOEXCEPT
620 {return begin();}
621 _LIBCPP_INLINE_VISIBILITY
622 const_iterator cend() const _NOEXCEPT
623 {return end();}
624 _LIBCPP_INLINE_VISIBILITY
625 const_reverse_iterator crbegin() const _NOEXCEPT
626 {return rbegin();}
627 _LIBCPP_INLINE_VISIBILITY
628 const_reverse_iterator crend() const _NOEXCEPT
629 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000630
Howard Hinnant1c936782011-06-03 19:40:40 +0000631 _LIBCPP_INLINE_VISIBILITY
632 size_type size() const _NOEXCEPT
633 {return static_cast<size_type>(this->__end_ - this->__begin_);}
634 _LIBCPP_INLINE_VISIBILITY
635 size_type capacity() const _NOEXCEPT
636 {return __base::capacity();}
Marshall Clow425f5752017-11-15 05:51:26 +0000637 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000638 bool empty() const _NOEXCEPT
639 {return this->__begin_ == this->__end_;}
640 size_type max_size() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000641 void reserve(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +0000642 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000643
644 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n);
645 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const;
646 reference at(size_type __n);
647 const_reference at(size_type __n) const;
648
Howard Hinnant27e0e772011-09-14 18:33:51 +0000649 _LIBCPP_INLINE_VISIBILITY reference front()
650 {
651 _LIBCPP_ASSERT(!empty(), "front() called for empty vector");
652 return *this->__begin_;
653 }
654 _LIBCPP_INLINE_VISIBILITY const_reference front() const
655 {
656 _LIBCPP_ASSERT(!empty(), "front() called for empty vector");
657 return *this->__begin_;
658 }
659 _LIBCPP_INLINE_VISIBILITY reference back()
660 {
661 _LIBCPP_ASSERT(!empty(), "back() called for empty vector");
662 return *(this->__end_ - 1);
663 }
664 _LIBCPP_INLINE_VISIBILITY const_reference back() const
665 {
666 _LIBCPP_ASSERT(!empty(), "back() called for empty vector");
667 return *(this->__end_ - 1);
668 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000669
Howard Hinnant1c936782011-06-03 19:40:40 +0000670 _LIBCPP_INLINE_VISIBILITY
671 value_type* data() _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000672 {return _VSTD::__to_raw_pointer(this->__begin_);}
Howard Hinnant1c936782011-06-03 19:40:40 +0000673 _LIBCPP_INLINE_VISIBILITY
674 const value_type* data() const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000675 {return _VSTD::__to_raw_pointer(this->__begin_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000676
Eric Fiselier96919722017-10-17 13:03:17 +0000677#ifdef _LIBCPP_CXX03_LANG
678 _LIBCPP_INLINE_VISIBILITY
679 void __emplace_back(const value_type& __x) { push_back(__x); }
680#else
681 template <class _Arg>
682 _LIBCPP_INLINE_VISIBILITY
683 void __emplace_back(_Arg&& __arg) {
684 emplace_back(_VSTD::forward<_Arg>(__arg));
685 }
686#endif
687
Howard Hinnantcf823322010-12-17 14:46:43 +0000688 _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000689
690#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000691 _LIBCPP_INLINE_VISIBILITY void push_back(value_type&& __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000692
Howard Hinnantc51e1022010-05-11 19:42:16 +0000693 template <class... _Args>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000694 _LIBCPP_INLINE_VISIBILITY
Marshall Clowea52cc42017-01-24 23:09:12 +0000695#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +0000696 reference emplace_back(_Args&&... __args);
Marshall Clowea52cc42017-01-24 23:09:12 +0000697#else
698 void emplace_back(_Args&&... __args);
699#endif
Eric Fiseliered9e9362017-04-16 02:40:45 +0000700#endif // !_LIBCPP_CXX03_LANG
701
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000702 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000703 void pop_back();
704
705 iterator insert(const_iterator __position, const_reference __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000706
707#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000708 iterator insert(const_iterator __position, value_type&& __x);
709 template <class... _Args>
710 iterator emplace(const_iterator __position, _Args&&... __args);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000711#endif // !_LIBCPP_CXX03_LANG
712
Howard Hinnantc51e1022010-05-11 19:42:16 +0000713 iterator insert(const_iterator __position, size_type __n, const_reference __x);
714 template <class _InputIterator>
715 typename enable_if
716 <
717 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000718 !__is_forward_iterator<_InputIterator>::value &&
719 is_constructible<
720 value_type,
721 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000722 iterator
723 >::type
724 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
725 template <class _ForwardIterator>
726 typename enable_if
727 <
Howard Hinnant88010252013-03-28 17:44:32 +0000728 __is_forward_iterator<_ForwardIterator>::value &&
729 is_constructible<
730 value_type,
731 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000732 iterator
733 >::type
734 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000735
736#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000737 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000738 iterator insert(const_iterator __position, initializer_list<value_type> __il)
739 {return insert(__position, __il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +0000740#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000741
Howard Hinnantcf823322010-12-17 14:46:43 +0000742 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000743 iterator erase(const_iterator __first, const_iterator __last);
744
Howard Hinnant1c936782011-06-03 19:40:40 +0000745 _LIBCPP_INLINE_VISIBILITY
746 void clear() _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000747 {
Marshall Clow2cd9d372014-05-08 14:14:06 +0000748 size_type __old_size = size();
Howard Hinnant27e0e772011-09-14 18:33:51 +0000749 __base::clear();
Marshall Clow2cd9d372014-05-08 14:14:06 +0000750 __annotate_shrink(__old_size);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000751 __invalidate_all_iterators();
752 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000753
754 void resize(size_type __sz);
755 void resize(size_type __sz, const_reference __x);
756
Howard Hinnant1c936782011-06-03 19:40:40 +0000757 void swap(vector&)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000758#if _LIBCPP_STD_VER >= 14
Eric Fiselier69c51982016-12-28 06:06:09 +0000759 _NOEXCEPT_DEBUG;
Marshall Clow8982dcd2015-07-13 20:04:56 +0000760#else
Eric Fiselier69c51982016-12-28 06:06:09 +0000761 _NOEXCEPT_DEBUG_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +0000762 __is_nothrow_swappable<allocator_type>::value);
763#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000764
765 bool __invariants() const;
766
Howard Hinnanta47c6d52011-09-16 17:29:17 +0000767#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000768
769 bool __dereferenceable(const const_iterator* __i) const;
770 bool __decrementable(const const_iterator* __i) const;
771 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
772 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
773
Howard Hinnanta47c6d52011-09-16 17:29:17 +0000774#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000775
Howard Hinnantc51e1022010-05-11 19:42:16 +0000776private:
Howard Hinnantcf823322010-12-17 14:46:43 +0000777 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Eric Fiselier69c51982016-12-28 06:06:09 +0000778 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(pointer __new_last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000779 void allocate(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +0000780 void deallocate() _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000781 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
Howard Hinnanta5589382011-01-04 19:53:31 +0000782 void __construct_at_end(size_type __n);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000783 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000784 void __construct_at_end(size_type __n, const_reference __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000785 template <class _ForwardIterator>
786 typename enable_if
787 <
788 __is_forward_iterator<_ForwardIterator>::value,
789 void
790 >::type
Eric Fiselieracfe6f02015-03-31 16:54:19 +0000791 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000792 void __append(size_type __n);
793 void __append(size_type __n, const_reference __x);
Howard Hinnantcf823322010-12-17 14:46:43 +0000794 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000795 iterator __make_iter(pointer __p) _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000796 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000797 const_iterator __make_iter(const_pointer __p) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000798 void __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v);
799 pointer __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p);
800 void __move_range(pointer __from_s, pointer __from_e, pointer __to);
Howard Hinnant1c936782011-06-03 19:40:40 +0000801 void __move_assign(vector& __c, true_type)
802 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000803 void __move_assign(vector& __c, false_type)
804 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000805 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76053d72013-06-27 19:35:32 +0000806 void __destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000807 {
Eric Fiselier69c51982016-12-28 06:06:09 +0000808 __invalidate_iterators_past(__new_last);
Marshall Clow2cd9d372014-05-08 14:14:06 +0000809 size_type __old_size = size();
Howard Hinnant27e0e772011-09-14 18:33:51 +0000810 __base::__destruct_at_end(__new_last);
Marshall Clow2cd9d372014-05-08 14:14:06 +0000811 __annotate_shrink(__old_size);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000812 }
Eric Fiseliered9e9362017-04-16 02:40:45 +0000813
814#ifndef _LIBCPP_CXX03_LANG
815 template <class _Up> void __push_back_slow_path(_Up&& __x);
816
Howard Hinnantb6c49562012-02-26 15:30:12 +0000817 template <class... _Args>
Eric Fiseliered9e9362017-04-16 02:40:45 +0000818 void __emplace_back_slow_path(_Args&&... __args);
819#else
820 template <class _Up> void __push_back_slow_path(_Up& __x);
Howard Hinnantb6c49562012-02-26 15:30:12 +0000821#endif
Eric Fiseliered9e9362017-04-16 02:40:45 +0000822
Marshall Clow2cd9d372014-05-08 14:14:06 +0000823 // The following functions are no-ops outside of AddressSanitizer mode.
824 // We call annotatations only for the default Allocator because other allocators
825 // may not meet the AddressSanitizer alignment constraints.
826 // See the documentation for __sanitizer_annotate_contiguous_container for more details.
Marshall Clow2cd9d372014-05-08 14:14:06 +0000827#ifndef _LIBCPP_HAS_NO_ASAN
Eric Fiselier6003c772016-12-23 23:37:52 +0000828 void __annotate_contiguous_container(const void *__beg, const void *__end,
829 const void *__old_mid,
830 const void *__new_mid) const
831 {
832
Marshall Clow2cd9d372014-05-08 14:14:06 +0000833 if (__beg && is_same<allocator_type, __default_allocator_type>::value)
834 __sanitizer_annotate_contiguous_container(__beg, __end, __old_mid, __new_mid);
Marshall Clow2cd9d372014-05-08 14:14:06 +0000835 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000836#else
837 _LIBCPP_INLINE_VISIBILITY
838 void __annotate_contiguous_container(const void*, const void*, const void*,
839 const void*) const {}
840#endif
841 _LIBCPP_INLINE_VISIBILITY
842 void __annotate_new(size_type __current_size) const {
Marshall Clow2cd9d372014-05-08 14:14:06 +0000843 __annotate_contiguous_container(data(), data() + capacity(),
844 data() + capacity(), data() + __current_size);
845 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000846
847 _LIBCPP_INLINE_VISIBILITY
848 void __annotate_delete() const {
Marshall Clow2cd9d372014-05-08 14:14:06 +0000849 __annotate_contiguous_container(data(), data() + capacity(),
850 data() + size(), data() + capacity());
851 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000852
853 _LIBCPP_INLINE_VISIBILITY
Kostya Serebryany4963c252014-09-02 23:43:38 +0000854 void __annotate_increase(size_type __n) const
Marshall Clow2cd9d372014-05-08 14:14:06 +0000855 {
856 __annotate_contiguous_container(data(), data() + capacity(),
857 data() + size(), data() + size() + __n);
858 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000859
860 _LIBCPP_INLINE_VISIBILITY
Kostya Serebryany4963c252014-09-02 23:43:38 +0000861 void __annotate_shrink(size_type __old_size) const
Marshall Clow2cd9d372014-05-08 14:14:06 +0000862 {
863 __annotate_contiguous_container(data(), data() + capacity(),
864 data() + __old_size, data() + size());
865 }
Marshall Clowc78ffa52014-09-03 21:37:43 +0000866#ifndef _LIBCPP_HAS_NO_ASAN
Kostya Serebryany4963c252014-09-02 23:43:38 +0000867 // The annotation for size increase should happen before the actual increase,
868 // but if an exception is thrown after that the annotation has to be undone.
869 struct __RAII_IncreaseAnnotator {
870 __RAII_IncreaseAnnotator(const vector &__v, size_type __n = 1)
Eric Fiseliera7c043e2015-03-10 00:25:20 +0000871 : __commit(false), __v(__v), __old_size(__v.size() + __n) {
Kostya Serebryany4963c252014-09-02 23:43:38 +0000872 __v.__annotate_increase(__n);
873 }
874 void __done() { __commit = true; }
875 ~__RAII_IncreaseAnnotator() {
876 if (__commit) return;
Eric Fiseliera7c043e2015-03-10 00:25:20 +0000877 __v.__annotate_shrink(__old_size);
Kostya Serebryany4963c252014-09-02 23:43:38 +0000878 }
879 bool __commit;
Kostya Serebryany4963c252014-09-02 23:43:38 +0000880 const vector &__v;
Eric Fiseliera7c043e2015-03-10 00:25:20 +0000881 size_type __old_size;
Kostya Serebryany4963c252014-09-02 23:43:38 +0000882 };
Marshall Clowc78ffa52014-09-03 21:37:43 +0000883#else
884 struct __RAII_IncreaseAnnotator {
Eric Fiselier6003c772016-12-23 23:37:52 +0000885 _LIBCPP_INLINE_VISIBILITY
886 __RAII_IncreaseAnnotator(const vector &, size_type = 1) {}
887 _LIBCPP_INLINE_VISIBILITY void __done() {}
Marshall Clowc78ffa52014-09-03 21:37:43 +0000888 };
889#endif
890
Howard Hinnantc51e1022010-05-11 19:42:16 +0000891};
892
893template <class _Tp, class _Allocator>
894void
895vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v)
896{
Marshall Clow2cd9d372014-05-08 14:14:06 +0000897 __annotate_delete();
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000898 __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, this->__end_, __v.__begin_);
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}
906
907template <class _Tp, class _Allocator>
908typename vector<_Tp, _Allocator>::pointer
909vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p)
910{
Marshall Clow2cd9d372014-05-08 14:14:06 +0000911 __annotate_delete();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000912 pointer __r = __v.__begin_;
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000913 __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, __p, __v.__begin_);
914 __alloc_traits::__construct_forward(this->__alloc(), __p, this->__end_, __v.__end_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000915 _VSTD::swap(this->__begin_, __v.__begin_);
916 _VSTD::swap(this->__end_, __v.__end_);
917 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000918 __v.__first_ = __v.__begin_;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000919 __annotate_new(size());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000920 __invalidate_all_iterators();
921 return __r;
922}
923
924// Allocate space for __n objects
925// throws length_error if __n > max_size()
926// throws (probably bad_alloc) if memory run out
927// Precondition: __begin_ == __end_ == __end_cap() == 0
928// Precondition: __n > 0
929// Postcondition: capacity() == __n
930// Postcondition: size() == 0
931template <class _Tp, class _Allocator>
932void
933vector<_Tp, _Allocator>::allocate(size_type __n)
934{
935 if (__n > max_size())
936 this->__throw_length_error();
937 this->__begin_ = this->__end_ = __alloc_traits::allocate(this->__alloc(), __n);
938 this->__end_cap() = this->__begin_ + __n;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000939 __annotate_new(0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000940}
941
942template <class _Tp, class _Allocator>
943void
Howard Hinnant1c936782011-06-03 19:40:40 +0000944vector<_Tp, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000945{
Howard Hinnant76053d72013-06-27 19:35:32 +0000946 if (this->__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000947 {
948 clear();
949 __alloc_traits::deallocate(this->__alloc(), this->__begin_, capacity());
Howard Hinnant76053d72013-06-27 19:35:32 +0000950 this->__begin_ = this->__end_ = this->__end_cap() = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000951 }
952}
953
954template <class _Tp, class _Allocator>
955typename vector<_Tp, _Allocator>::size_type
Howard Hinnant1c936782011-06-03 19:40:40 +0000956vector<_Tp, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000957{
Eric Fiselierb5d9f442016-11-23 01:18:56 +0000958 return _VSTD::min<size_type>(__alloc_traits::max_size(this->__alloc()),
959 numeric_limits<difference_type>::max());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000960}
961
962// Precondition: __new_size > capacity()
963template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000964inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000965typename vector<_Tp, _Allocator>::size_type
966vector<_Tp, _Allocator>::__recommend(size_type __new_size) const
967{
968 const size_type __ms = max_size();
969 if (__new_size > __ms)
970 this->__throw_length_error();
971 const size_type __cap = capacity();
972 if (__cap >= __ms / 2)
973 return __ms;
Alexis Hunt991d29b2011-07-29 23:31:58 +0000974 return _VSTD::max<size_type>(2*__cap, __new_size);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000975}
976
977// Default constructs __n objects starting at __end_
978// throws if construction throws
979// Precondition: __n > 0
980// Precondition: size() + __n <= capacity()
981// Postcondition: size() == size() + __n
982template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000983void
984vector<_Tp, _Allocator>::__construct_at_end(size_type __n)
985{
Howard Hinnantc51e1022010-05-11 19:42:16 +0000986 allocator_type& __a = this->__alloc();
987 do
988 {
Kostya Serebryany4963c252014-09-02 23:43:38 +0000989 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000990 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000991 ++this->__end_;
992 --__n;
Kostya Serebryany4963c252014-09-02 23:43:38 +0000993 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000994 } while (__n > 0);
995}
996
Howard Hinnantc51e1022010-05-11 19:42:16 +0000997// Copy constructs __n objects starting at __end_ from __x
998// throws if construction throws
999// Precondition: __n > 0
1000// Precondition: size() + __n <= capacity()
1001// Postcondition: size() == old size() + __n
1002// Postcondition: [i] == __x for all i in [size() - __n, __n)
1003template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001004inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001005void
1006vector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
1007{
Howard Hinnantc51e1022010-05-11 19:42:16 +00001008 allocator_type& __a = this->__alloc();
1009 do
1010 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001011 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001012 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001013 ++this->__end_;
1014 --__n;
Kostya Serebryany4963c252014-09-02 23:43:38 +00001015 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001016 } while (__n > 0);
1017}
1018
1019template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001020template <class _ForwardIterator>
1021typename enable_if
1022<
1023 __is_forward_iterator<_ForwardIterator>::value,
1024 void
1025>::type
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001026vector<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001027{
1028 allocator_type& __a = this->__alloc();
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001029 __RAII_IncreaseAnnotator __annotator(*this, __n);
1030 __alloc_traits::__construct_range_forward(__a, __first, __last, this->__end_);
1031 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001032}
1033
1034// Default constructs __n objects starting at __end_
1035// throws if construction throws
1036// Postcondition: size() == size() + __n
Howard Hinnant1c936782011-06-03 19:40:40 +00001037// Exception safety: strong.
Howard Hinnantc51e1022010-05-11 19:42:16 +00001038template <class _Tp, class _Allocator>
1039void
1040vector<_Tp, _Allocator>::__append(size_type __n)
1041{
1042 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1043 this->__construct_at_end(__n);
1044 else
1045 {
1046 allocator_type& __a = this->__alloc();
1047 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1048 __v.__construct_at_end(__n);
1049 __swap_out_circular_buffer(__v);
1050 }
1051}
1052
1053// Default constructs __n objects starting at __end_
1054// throws if construction throws
1055// Postcondition: size() == size() + __n
Howard Hinnant1c936782011-06-03 19:40:40 +00001056// Exception safety: strong.
Howard Hinnantc51e1022010-05-11 19:42:16 +00001057template <class _Tp, class _Allocator>
1058void
1059vector<_Tp, _Allocator>::__append(size_type __n, const_reference __x)
1060{
1061 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1062 this->__construct_at_end(__n, __x);
1063 else
1064 {
1065 allocator_type& __a = this->__alloc();
1066 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1067 __v.__construct_at_end(__n, __x);
1068 __swap_out_circular_buffer(__v);
1069 }
1070}
1071
1072template <class _Tp, class _Allocator>
1073vector<_Tp, _Allocator>::vector(size_type __n)
1074{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001075#if _LIBCPP_DEBUG_LEVEL >= 2
1076 __get_db()->__insert_c(this);
1077#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001078 if (__n > 0)
1079 {
1080 allocate(__n);
1081 __construct_at_end(__n);
1082 }
1083}
1084
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00001085#if _LIBCPP_STD_VER > 11
1086template <class _Tp, class _Allocator>
1087vector<_Tp, _Allocator>::vector(size_type __n, const allocator_type& __a)
1088 : __base(__a)
1089{
1090#if _LIBCPP_DEBUG_LEVEL >= 2
1091 __get_db()->__insert_c(this);
1092#endif
1093 if (__n > 0)
1094 {
1095 allocate(__n);
1096 __construct_at_end(__n);
1097 }
1098}
1099#endif
1100
Howard Hinnantc51e1022010-05-11 19:42:16 +00001101template <class _Tp, class _Allocator>
1102vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x)
1103{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001104#if _LIBCPP_DEBUG_LEVEL >= 2
1105 __get_db()->__insert_c(this);
1106#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001107 if (__n > 0)
1108 {
1109 allocate(__n);
1110 __construct_at_end(__n, __x);
1111 }
1112}
1113
1114template <class _Tp, class _Allocator>
1115vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x, const allocator_type& __a)
1116 : __base(__a)
1117{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001118#if _LIBCPP_DEBUG_LEVEL >= 2
1119 __get_db()->__insert_c(this);
1120#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001121 if (__n > 0)
1122 {
1123 allocate(__n);
1124 __construct_at_end(__n, __x);
1125 }
1126}
1127
1128template <class _Tp, class _Allocator>
1129template <class _InputIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +00001130vector<_Tp, _Allocator>::vector(_InputIterator __first,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001131 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001132 !__is_forward_iterator<_InputIterator>::value &&
1133 is_constructible<
1134 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +00001135 typename iterator_traits<_InputIterator>::reference>::value,
1136 _InputIterator>::type __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001137{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001138#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001139 __get_db()->__insert_c(this);
1140#endif
Howard Hinnant9cd22302011-09-16 18:41:29 +00001141 for (; __first != __last; ++__first)
Eric Fiselier96919722017-10-17 13:03:17 +00001142 __emplace_back(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001143}
1144
1145template <class _Tp, class _Allocator>
1146template <class _InputIterator>
1147vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
1148 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001149 !__is_forward_iterator<_InputIterator>::value &&
1150 is_constructible<
1151 value_type,
1152 typename iterator_traits<_InputIterator>::reference>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001153 : __base(__a)
1154{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001155#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001156 __get_db()->__insert_c(this);
1157#endif
Howard Hinnant9cd22302011-09-16 18:41:29 +00001158 for (; __first != __last; ++__first)
Eric Fiselier96919722017-10-17 13:03:17 +00001159 __emplace_back(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001160}
1161
1162template <class _Tp, class _Allocator>
1163template <class _ForwardIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +00001164vector<_Tp, _Allocator>::vector(_ForwardIterator __first,
Howard Hinnant88010252013-03-28 17:44:32 +00001165 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
1166 is_constructible<
1167 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +00001168 typename iterator_traits<_ForwardIterator>::reference>::value,
1169 _ForwardIterator>::type __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001170{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001171#if _LIBCPP_DEBUG_LEVEL >= 2
1172 __get_db()->__insert_c(this);
1173#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001174 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001175 if (__n > 0)
1176 {
1177 allocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001178 __construct_at_end(__first, __last, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001179 }
1180}
1181
1182template <class _Tp, class _Allocator>
1183template <class _ForwardIterator>
1184vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Howard Hinnant88010252013-03-28 17:44:32 +00001185 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
1186 is_constructible<
1187 value_type,
1188 typename iterator_traits<_ForwardIterator>::reference>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001189 : __base(__a)
1190{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001191#if _LIBCPP_DEBUG_LEVEL >= 2
1192 __get_db()->__insert_c(this);
1193#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001194 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001195 if (__n > 0)
1196 {
1197 allocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001198 __construct_at_end(__first, __last, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001199 }
1200}
1201
1202template <class _Tp, class _Allocator>
1203vector<_Tp, _Allocator>::vector(const vector& __x)
1204 : __base(__alloc_traits::select_on_container_copy_construction(__x.__alloc()))
1205{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001206#if _LIBCPP_DEBUG_LEVEL >= 2
1207 __get_db()->__insert_c(this);
1208#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001209 size_type __n = __x.size();
1210 if (__n > 0)
1211 {
1212 allocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001213 __construct_at_end(__x.__begin_, __x.__end_, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001214 }
1215}
1216
1217template <class _Tp, class _Allocator>
1218vector<_Tp, _Allocator>::vector(const vector& __x, const allocator_type& __a)
1219 : __base(__a)
1220{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001221#if _LIBCPP_DEBUG_LEVEL >= 2
1222 __get_db()->__insert_c(this);
1223#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001224 size_type __n = __x.size();
1225 if (__n > 0)
1226 {
1227 allocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001228 __construct_at_end(__x.__begin_, __x.__end_, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001229 }
1230}
1231
Eric Fiseliered9e9362017-04-16 02:40:45 +00001232#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001233
1234template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001235inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001236vector<_Tp, _Allocator>::vector(vector&& __x)
Marshall Clowe5108202015-07-14 14:46:32 +00001237#if _LIBCPP_STD_VER > 14
1238 _NOEXCEPT
1239#else
Howard Hinnant1c936782011-06-03 19:40:40 +00001240 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowe5108202015-07-14 14:46:32 +00001241#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001242 : __base(_VSTD::move(__x.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001243{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001244#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001245 __get_db()->__insert_c(this);
Howard Hinnanta1e60cd2011-09-19 16:34:29 +00001246 __get_db()->swap(this, &__x);
Howard Hinnant27e0e772011-09-14 18:33:51 +00001247#endif
Howard Hinnant9cd22302011-09-16 18:41:29 +00001248 this->__begin_ = __x.__begin_;
1249 this->__end_ = __x.__end_;
1250 this->__end_cap() = __x.__end_cap();
Howard Hinnant76053d72013-06-27 19:35:32 +00001251 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001252}
1253
1254template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001255inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001256vector<_Tp, _Allocator>::vector(vector&& __x, const allocator_type& __a)
1257 : __base(__a)
1258{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001259#if _LIBCPP_DEBUG_LEVEL >= 2
1260 __get_db()->__insert_c(this);
1261#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001262 if (__a == __x.__alloc())
1263 {
1264 this->__begin_ = __x.__begin_;
1265 this->__end_ = __x.__end_;
1266 this->__end_cap() = __x.__end_cap();
1267 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnanta1e60cd2011-09-19 16:34:29 +00001268#if _LIBCPP_DEBUG_LEVEL >= 2
1269 __get_db()->swap(this, &__x);
1270#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001271 }
1272 else
1273 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001274 typedef move_iterator<iterator> _Ip;
1275 assign(_Ip(__x.begin()), _Ip(__x.end()));
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)
1282{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001283#if _LIBCPP_DEBUG_LEVEL >= 2
1284 __get_db()->__insert_c(this);
1285#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001286 if (__il.size() > 0)
1287 {
1288 allocate(__il.size());
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001289 __construct_at_end(__il.begin(), __il.end(), __il.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001290 }
1291}
1292
1293template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001294inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001295vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
1296 : __base(__a)
1297{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001298#if _LIBCPP_DEBUG_LEVEL >= 2
1299 __get_db()->__insert_c(this);
1300#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001301 if (__il.size() > 0)
1302 {
1303 allocate(__il.size());
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001304 __construct_at_end(__il.begin(), __il.end(), __il.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001305 }
1306}
1307
1308template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001309inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001310vector<_Tp, _Allocator>&
1311vector<_Tp, _Allocator>::operator=(vector&& __x)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001312 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001313{
1314 __move_assign(__x, integral_constant<bool,
1315 __alloc_traits::propagate_on_container_move_assignment::value>());
1316 return *this;
1317}
1318
1319template <class _Tp, class _Allocator>
1320void
1321vector<_Tp, _Allocator>::__move_assign(vector& __c, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001322 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001323{
1324 if (__base::__alloc() != __c.__alloc())
1325 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001326 typedef move_iterator<iterator> _Ip;
1327 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001328 }
1329 else
1330 __move_assign(__c, true_type());
1331}
1332
1333template <class _Tp, class _Allocator>
1334void
1335vector<_Tp, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00001336 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001337{
1338 deallocate();
Marshall Clow136d45c2014-07-21 15:11:13 +00001339 __base::__move_assign_alloc(__c); // this can throw
Howard Hinnantc51e1022010-05-11 19:42:16 +00001340 this->__begin_ = __c.__begin_;
1341 this->__end_ = __c.__end_;
1342 this->__end_cap() = __c.__end_cap();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001343 __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
Howard Hinnanta1e60cd2011-09-19 16:34:29 +00001344#if _LIBCPP_DEBUG_LEVEL >= 2
1345 __get_db()->swap(this, &__c);
1346#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001347}
1348
Eric Fiseliered9e9362017-04-16 02:40:45 +00001349#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001350
1351template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001352inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001353vector<_Tp, _Allocator>&
1354vector<_Tp, _Allocator>::operator=(const vector& __x)
1355{
1356 if (this != &__x)
1357 {
1358 __base::__copy_assign_alloc(__x);
1359 assign(__x.__begin_, __x.__end_);
1360 }
1361 return *this;
1362}
1363
1364template <class _Tp, class _Allocator>
1365template <class _InputIterator>
1366typename enable_if
1367<
1368 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001369 !__is_forward_iterator<_InputIterator>::value &&
1370 is_constructible<
1371 _Tp,
1372 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001373 void
1374>::type
1375vector<_Tp, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
1376{
1377 clear();
1378 for (; __first != __last; ++__first)
Eric Fiselier96919722017-10-17 13:03:17 +00001379 __emplace_back(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001380}
1381
1382template <class _Tp, class _Allocator>
1383template <class _ForwardIterator>
1384typename enable_if
1385<
Howard Hinnant88010252013-03-28 17:44:32 +00001386 __is_forward_iterator<_ForwardIterator>::value &&
1387 is_constructible<
1388 _Tp,
1389 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001390 void
1391>::type
1392vector<_Tp, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
1393{
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001394 size_type __new_size = static_cast<size_type>(_VSTD::distance(__first, __last));
1395 if (__new_size <= capacity())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001396 {
1397 _ForwardIterator __mid = __last;
1398 bool __growing = false;
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001399 if (__new_size > size())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001400 {
1401 __growing = true;
1402 __mid = __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001403 _VSTD::advance(__mid, size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001404 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001405 pointer __m = _VSTD::copy(__first, __mid, this->__begin_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001406 if (__growing)
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001407 __construct_at_end(__mid, __last, __new_size - size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001408 else
1409 this->__destruct_at_end(__m);
1410 }
1411 else
1412 {
1413 deallocate();
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001414 allocate(__recommend(__new_size));
1415 __construct_at_end(__first, __last, __new_size);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001416 }
Eric Fiselier69c51982016-12-28 06:06:09 +00001417 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001418}
1419
1420template <class _Tp, class _Allocator>
1421void
1422vector<_Tp, _Allocator>::assign(size_type __n, const_reference __u)
1423{
1424 if (__n <= capacity())
1425 {
1426 size_type __s = size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001427 _VSTD::fill_n(this->__begin_, _VSTD::min(__n, __s), __u);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001428 if (__n > __s)
1429 __construct_at_end(__n - __s, __u);
1430 else
Howard Hinnant155c2af2010-05-24 17:49:41 +00001431 this->__destruct_at_end(this->__begin_ + __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001432 }
1433 else
1434 {
1435 deallocate();
1436 allocate(__recommend(static_cast<size_type>(__n)));
1437 __construct_at_end(__n, __u);
1438 }
Eric Fiselier69c51982016-12-28 06:06:09 +00001439 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001440}
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>::iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001445vector<_Tp, _Allocator>::__make_iter(pointer __p) _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 iterator(this, __p);
1449#else
1450 return 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>::const_iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001457vector<_Tp, _Allocator>::__make_iter(const_pointer __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001458{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001459#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001460 return const_iterator(this, __p);
1461#else
1462 return const_iterator(__p);
1463#endif
1464}
1465
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001466template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001467inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001468typename vector<_Tp, _Allocator>::iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001469vector<_Tp, _Allocator>::begin() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001470{
1471 return __make_iter(this->__begin_);
1472}
1473
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001474template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001475inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001476typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001477vector<_Tp, _Allocator>::begin() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001478{
1479 return __make_iter(this->__begin_);
1480}
1481
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001482template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001483inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001484typename vector<_Tp, _Allocator>::iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001485vector<_Tp, _Allocator>::end() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001486{
1487 return __make_iter(this->__end_);
1488}
1489
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001490template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001491inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001492typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001493vector<_Tp, _Allocator>::end() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001494{
1495 return __make_iter(this->__end_);
1496}
1497
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001498template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001499inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001500typename vector<_Tp, _Allocator>::reference
1501vector<_Tp, _Allocator>::operator[](size_type __n)
1502{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001503 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001504 return this->__begin_[__n];
1505}
1506
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001507template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001508inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001509typename vector<_Tp, _Allocator>::const_reference
1510vector<_Tp, _Allocator>::operator[](size_type __n) const
1511{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001512 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001513 return this->__begin_[__n];
1514}
1515
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001516template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001517typename vector<_Tp, _Allocator>::reference
1518vector<_Tp, _Allocator>::at(size_type __n)
1519{
1520 if (__n >= size())
1521 this->__throw_out_of_range();
1522 return this->__begin_[__n];
1523}
1524
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001525template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001526typename vector<_Tp, _Allocator>::const_reference
1527vector<_Tp, _Allocator>::at(size_type __n) const
1528{
1529 if (__n >= size())
1530 this->__throw_out_of_range();
1531 return this->__begin_[__n];
1532}
1533
1534template <class _Tp, class _Allocator>
1535void
1536vector<_Tp, _Allocator>::reserve(size_type __n)
1537{
1538 if (__n > capacity())
1539 {
1540 allocator_type& __a = this->__alloc();
Howard Hinnant1c936782011-06-03 19:40:40 +00001541 __split_buffer<value_type, allocator_type&> __v(__n, size(), __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001542 __swap_out_circular_buffer(__v);
1543 }
1544}
1545
1546template <class _Tp, class _Allocator>
1547void
Howard Hinnant1c936782011-06-03 19:40:40 +00001548vector<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001549{
1550 if (capacity() > size())
1551 {
1552#ifndef _LIBCPP_NO_EXCEPTIONS
1553 try
1554 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001555#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001556 allocator_type& __a = this->__alloc();
Howard Hinnant1c936782011-06-03 19:40:40 +00001557 __split_buffer<value_type, allocator_type&> __v(size(), size(), __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001558 __swap_out_circular_buffer(__v);
1559#ifndef _LIBCPP_NO_EXCEPTIONS
1560 }
1561 catch (...)
1562 {
1563 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001564#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001565 }
1566}
1567
1568template <class _Tp, class _Allocator>
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001569template <class _Up>
1570void
Eric Fiseliered9e9362017-04-16 02:40:45 +00001571#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001572vector<_Tp, _Allocator>::__push_back_slow_path(_Up&& __x)
1573#else
1574vector<_Tp, _Allocator>::__push_back_slow_path(_Up& __x)
1575#endif
1576{
1577 allocator_type& __a = this->__alloc();
1578 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1579 // __v.push_back(_VSTD::forward<_Up>(__x));
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001580 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), _VSTD::forward<_Up>(__x));
1581 __v.__end_++;
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001582 __swap_out_circular_buffer(__v);
1583}
1584
1585template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001586inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001587void
1588vector<_Tp, _Allocator>::push_back(const_reference __x)
1589{
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001590 if (this->__end_ != this->__end_cap())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001591 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001592 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001593 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001594 _VSTD::__to_raw_pointer(this->__end_), __x);
Kostya Serebryany4963c252014-09-02 23:43:38 +00001595 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001596 ++this->__end_;
1597 }
1598 else
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001599 __push_back_slow_path(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001600}
1601
Eric Fiseliered9e9362017-04-16 02:40:45 +00001602#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001603
1604template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001605inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001606void
1607vector<_Tp, _Allocator>::push_back(value_type&& __x)
1608{
1609 if (this->__end_ < this->__end_cap())
1610 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001611 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001612 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001613 _VSTD::__to_raw_pointer(this->__end_),
1614 _VSTD::move(__x));
Kostya Serebryany4963c252014-09-02 23:43:38 +00001615 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001616 ++this->__end_;
1617 }
1618 else
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001619 __push_back_slow_path(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001620}
1621
1622template <class _Tp, class _Allocator>
1623template <class... _Args>
1624void
Howard Hinnantb6c49562012-02-26 15:30:12 +00001625vector<_Tp, _Allocator>::__emplace_back_slow_path(_Args&&... __args)
1626{
1627 allocator_type& __a = this->__alloc();
1628 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1629// __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001630 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), _VSTD::forward<_Args>(__args)...);
1631 __v.__end_++;
Howard Hinnantb6c49562012-02-26 15:30:12 +00001632 __swap_out_circular_buffer(__v);
1633}
1634
1635template <class _Tp, class _Allocator>
1636template <class... _Args>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001637inline
Marshall Clowea52cc42017-01-24 23:09:12 +00001638#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001639typename vector<_Tp, _Allocator>::reference
Marshall Clowea52cc42017-01-24 23:09:12 +00001640#else
1641void
1642#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001643vector<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1644{
1645 if (this->__end_ < this->__end_cap())
1646 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001647 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001648 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001649 _VSTD::__to_raw_pointer(this->__end_),
1650 _VSTD::forward<_Args>(__args)...);
Kostya Serebryany4963c252014-09-02 23:43:38 +00001651 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001652 ++this->__end_;
1653 }
1654 else
Howard Hinnantb6c49562012-02-26 15:30:12 +00001655 __emplace_back_slow_path(_VSTD::forward<_Args>(__args)...);
Marshall Clowea52cc42017-01-24 23:09:12 +00001656#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001657 return this->back();
Marshall Clowea52cc42017-01-24 23:09:12 +00001658#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001659}
1660
Eric Fiseliered9e9362017-04-16 02:40:45 +00001661#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001662
1663template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001664inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001665void
1666vector<_Tp, _Allocator>::pop_back()
1667{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001668 _LIBCPP_ASSERT(!empty(), "vector::pop_back called for empty vector");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001669 this->__destruct_at_end(this->__end_ - 1);
1670}
1671
1672template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001673inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001674typename vector<_Tp, _Allocator>::iterator
1675vector<_Tp, _Allocator>::erase(const_iterator __position)
1676{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001677#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001678 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1679 "vector::erase(iterator) called with an iterator not"
1680 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001681#endif
Howard Hinnant58d52d52013-03-25 22:12:26 +00001682 _LIBCPP_ASSERT(__position != end(),
1683 "vector::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnant76053d72013-06-27 19:35:32 +00001684 difference_type __ps = __position - cbegin();
1685 pointer __p = this->__begin_ + __ps;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001686 this->__destruct_at_end(_VSTD::move(__p + 1, this->__end_, __p));
Eric Fiselier69c51982016-12-28 06:06:09 +00001687 this->__invalidate_iterators_past(__p-1);
1688 iterator __r = __make_iter(__p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001689 return __r;
1690}
1691
1692template <class _Tp, class _Allocator>
1693typename vector<_Tp, _Allocator>::iterator
1694vector<_Tp, _Allocator>::erase(const_iterator __first, const_iterator __last)
1695{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001696#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001697 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
1698 "vector::erase(iterator, iterator) called with an iterator not"
1699 " referring to this vector");
Eric Fiselier69c51982016-12-28 06:06:09 +00001700 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__last) == this,
1701 "vector::erase(iterator, iterator) called with an iterator not"
1702 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001703#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001704 _LIBCPP_ASSERT(__first <= __last, "vector::erase(first, last) called with invalid range");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001705 pointer __p = this->__begin_ + (__first - begin());
Eric Fiselier69c51982016-12-28 06:06:09 +00001706 if (__first != __last) {
Howard Hinnantc580cc32013-04-18 15:02:57 +00001707 this->__destruct_at_end(_VSTD::move(__p + (__last - __first), this->__end_, __p));
Eric Fiselier69c51982016-12-28 06:06:09 +00001708 this->__invalidate_iterators_past(__p - 1);
1709 }
1710 iterator __r = __make_iter(__p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001711 return __r;
1712}
1713
1714template <class _Tp, class _Allocator>
1715void
1716vector<_Tp, _Allocator>::__move_range(pointer __from_s, pointer __from_e, pointer __to)
1717{
1718 pointer __old_last = this->__end_;
1719 difference_type __n = __old_last - __to;
1720 for (pointer __i = __from_s + __n; __i < __from_e; ++__i, ++this->__end_)
1721 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001722 _VSTD::__to_raw_pointer(this->__end_),
1723 _VSTD::move(*__i));
1724 _VSTD::move_backward(__from_s, __from_s + __n, __old_last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001725}
1726
1727template <class _Tp, class _Allocator>
1728typename vector<_Tp, _Allocator>::iterator
1729vector<_Tp, _Allocator>::insert(const_iterator __position, const_reference __x)
1730{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001731#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001732 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1733 "vector::insert(iterator, x) called with an iterator not"
1734 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001735#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001736 pointer __p = this->__begin_ + (__position - begin());
1737 if (this->__end_ < this->__end_cap())
1738 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001739 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001740 if (__p == this->__end_)
1741 {
1742 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001743 _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001744 ++this->__end_;
1745 }
1746 else
1747 {
1748 __move_range(__p, this->__end_, __p + 1);
1749 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1750 if (__p <= __xr && __xr < this->__end_)
1751 ++__xr;
1752 *__p = *__xr;
1753 }
Kostya Serebryany4963c252014-09-02 23:43:38 +00001754 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001755 }
1756 else
1757 {
1758 allocator_type& __a = this->__alloc();
1759 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
1760 __v.push_back(__x);
1761 __p = __swap_out_circular_buffer(__v, __p);
1762 }
1763 return __make_iter(__p);
1764}
1765
Eric Fiseliered9e9362017-04-16 02:40:45 +00001766#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001767
1768template <class _Tp, class _Allocator>
1769typename vector<_Tp, _Allocator>::iterator
1770vector<_Tp, _Allocator>::insert(const_iterator __position, value_type&& __x)
1771{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001772#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001773 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1774 "vector::insert(iterator, x) called with an iterator not"
1775 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001776#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001777 pointer __p = this->__begin_ + (__position - begin());
1778 if (this->__end_ < this->__end_cap())
1779 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001780 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001781 if (__p == this->__end_)
1782 {
1783 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001784 _VSTD::__to_raw_pointer(this->__end_),
1785 _VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001786 ++this->__end_;
1787 }
1788 else
1789 {
1790 __move_range(__p, this->__end_, __p + 1);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001791 *__p = _VSTD::move(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001792 }
Kostya Serebryany4963c252014-09-02 23:43:38 +00001793 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001794 }
1795 else
1796 {
1797 allocator_type& __a = this->__alloc();
1798 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001799 __v.push_back(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001800 __p = __swap_out_circular_buffer(__v, __p);
1801 }
1802 return __make_iter(__p);
1803}
1804
1805template <class _Tp, class _Allocator>
1806template <class... _Args>
1807typename vector<_Tp, _Allocator>::iterator
1808vector<_Tp, _Allocator>::emplace(const_iterator __position, _Args&&... __args)
1809{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001810#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001811 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1812 "vector::emplace(iterator, x) called with an iterator not"
1813 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001814#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001815 pointer __p = this->__begin_ + (__position - begin());
1816 if (this->__end_ < this->__end_cap())
1817 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001818 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001819 if (__p == this->__end_)
1820 {
1821 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001822 _VSTD::__to_raw_pointer(this->__end_),
1823 _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001824 ++this->__end_;
1825 }
1826 else
1827 {
Marshall Clowa591b9a2016-07-11 21:38:08 +00001828 __temp_value<value_type, _Allocator> __tmp(this->__alloc(), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001829 __move_range(__p, this->__end_, __p + 1);
Marshall Clowa591b9a2016-07-11 21:38:08 +00001830 *__p = _VSTD::move(__tmp.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001831 }
Kostya Serebryany4963c252014-09-02 23:43:38 +00001832 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001833 }
1834 else
1835 {
1836 allocator_type& __a = this->__alloc();
1837 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001838 __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001839 __p = __swap_out_circular_buffer(__v, __p);
1840 }
1841 return __make_iter(__p);
1842}
1843
Eric Fiseliered9e9362017-04-16 02:40:45 +00001844#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001845
1846template <class _Tp, class _Allocator>
1847typename vector<_Tp, _Allocator>::iterator
1848vector<_Tp, _Allocator>::insert(const_iterator __position, size_type __n, const_reference __x)
1849{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001850#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001851 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1852 "vector::insert(iterator, n, x) called with an iterator not"
1853 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001854#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001855 pointer __p = this->__begin_ + (__position - begin());
1856 if (__n > 0)
1857 {
1858 if (__n <= static_cast<size_type>(this->__end_cap() - this->__end_))
1859 {
1860 size_type __old_n = __n;
1861 pointer __old_last = this->__end_;
1862 if (__n > static_cast<size_type>(this->__end_ - __p))
1863 {
1864 size_type __cx = __n - (this->__end_ - __p);
1865 __construct_at_end(__cx, __x);
1866 __n -= __cx;
1867 }
1868 if (__n > 0)
1869 {
Eric Fiselier2a649652014-11-14 18:28:36 +00001870 __RAII_IncreaseAnnotator __annotator(*this, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001871 __move_range(__p, __old_last, __p + __old_n);
Kostya Serebryany4963c252014-09-02 23:43:38 +00001872 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001873 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1874 if (__p <= __xr && __xr < this->__end_)
1875 __xr += __old_n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001876 _VSTD::fill_n(__p, __n, *__xr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001877 }
1878 }
1879 else
1880 {
1881 allocator_type& __a = this->__alloc();
1882 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1883 __v.__construct_at_end(__n, __x);
1884 __p = __swap_out_circular_buffer(__v, __p);
1885 }
1886 }
1887 return __make_iter(__p);
1888}
1889
1890template <class _Tp, class _Allocator>
1891template <class _InputIterator>
1892typename enable_if
1893<
1894 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001895 !__is_forward_iterator<_InputIterator>::value &&
1896 is_constructible<
1897 _Tp,
1898 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001899 typename vector<_Tp, _Allocator>::iterator
1900>::type
1901vector<_Tp, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
1902{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001903#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001904 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1905 "vector::insert(iterator, range) called with an iterator not"
1906 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001907#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001908 difference_type __off = __position - begin();
1909 pointer __p = this->__begin_ + __off;
1910 allocator_type& __a = this->__alloc();
1911 pointer __old_last = this->__end_;
1912 for (; this->__end_ != this->__end_cap() && __first != __last; ++__first)
1913 {
Eric Fiselier489fd502015-07-18 18:22:12 +00001914 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001915 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001916 *__first);
1917 ++this->__end_;
Eric Fiselier489fd502015-07-18 18:22:12 +00001918 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001919 }
1920 __split_buffer<value_type, allocator_type&> __v(__a);
1921 if (__first != __last)
1922 {
1923#ifndef _LIBCPP_NO_EXCEPTIONS
1924 try
1925 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001926#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001927 __v.__construct_at_end(__first, __last);
1928 difference_type __old_size = __old_last - this->__begin_;
1929 difference_type __old_p = __p - this->__begin_;
1930 reserve(__recommend(size() + __v.size()));
1931 __p = this->__begin_ + __old_p;
1932 __old_last = this->__begin_ + __old_size;
1933#ifndef _LIBCPP_NO_EXCEPTIONS
1934 }
1935 catch (...)
1936 {
1937 erase(__make_iter(__old_last), end());
1938 throw;
1939 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001940#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001941 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001942 __p = _VSTD::rotate(__p, __old_last, this->__end_);
Howard Hinnant9cd22302011-09-16 18:41:29 +00001943 insert(__make_iter(__p), make_move_iterator(__v.begin()),
1944 make_move_iterator(__v.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001945 return begin() + __off;
1946}
1947
1948template <class _Tp, class _Allocator>
1949template <class _ForwardIterator>
1950typename enable_if
1951<
Howard Hinnant88010252013-03-28 17:44:32 +00001952 __is_forward_iterator<_ForwardIterator>::value &&
1953 is_constructible<
1954 _Tp,
1955 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001956 typename vector<_Tp, _Allocator>::iterator
1957>::type
1958vector<_Tp, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
1959{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001960#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001961 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1962 "vector::insert(iterator, range) called with an iterator not"
1963 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001964#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001965 pointer __p = this->__begin_ + (__position - begin());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001966 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001967 if (__n > 0)
1968 {
1969 if (__n <= this->__end_cap() - this->__end_)
1970 {
1971 size_type __old_n = __n;
1972 pointer __old_last = this->__end_;
1973 _ForwardIterator __m = __last;
1974 difference_type __dx = this->__end_ - __p;
1975 if (__n > __dx)
1976 {
1977 __m = __first;
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001978 difference_type __diff = this->__end_ - __p;
1979 _VSTD::advance(__m, __diff);
1980 __construct_at_end(__m, __last, __n - __diff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001981 __n = __dx;
1982 }
1983 if (__n > 0)
1984 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001985 __RAII_IncreaseAnnotator __annotator(*this, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001986 __move_range(__p, __old_last, __p + __old_n);
Kostya Serebryany4963c252014-09-02 23:43:38 +00001987 __annotator.__done();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001988 _VSTD::copy(__first, __m, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001989 }
1990 }
1991 else
1992 {
1993 allocator_type& __a = this->__alloc();
1994 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1995 __v.__construct_at_end(__first, __last);
1996 __p = __swap_out_circular_buffer(__v, __p);
1997 }
1998 }
1999 return __make_iter(__p);
2000}
2001
2002template <class _Tp, class _Allocator>
2003void
2004vector<_Tp, _Allocator>::resize(size_type __sz)
2005{
2006 size_type __cs = size();
2007 if (__cs < __sz)
2008 this->__append(__sz - __cs);
2009 else if (__cs > __sz)
2010 this->__destruct_at_end(this->__begin_ + __sz);
2011}
2012
2013template <class _Tp, class _Allocator>
2014void
2015vector<_Tp, _Allocator>::resize(size_type __sz, const_reference __x)
2016{
2017 size_type __cs = size();
2018 if (__cs < __sz)
2019 this->__append(__sz - __cs, __x);
2020 else if (__cs > __sz)
2021 this->__destruct_at_end(this->__begin_ + __sz);
2022}
2023
2024template <class _Tp, class _Allocator>
2025void
2026vector<_Tp, _Allocator>::swap(vector& __x)
Marshall Clow8982dcd2015-07-13 20:04:56 +00002027#if _LIBCPP_STD_VER >= 14
Eric Fiselier69c51982016-12-28 06:06:09 +00002028 _NOEXCEPT_DEBUG
Marshall Clow8982dcd2015-07-13 20:04:56 +00002029#else
Eric Fiselier69c51982016-12-28 06:06:09 +00002030 _NOEXCEPT_DEBUG_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00002031 __is_nothrow_swappable<allocator_type>::value)
2032#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002033{
Howard Hinnant27e0e772011-09-14 18:33:51 +00002034 _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
2035 this->__alloc() == __x.__alloc(),
2036 "vector::swap: Either propagate_on_container_swap must be true"
2037 " or the allocators must compare equal");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002038 _VSTD::swap(this->__begin_, __x.__begin_);
2039 _VSTD::swap(this->__end_, __x.__end_);
2040 _VSTD::swap(this->__end_cap(), __x.__end_cap());
Eric Fiseliered9e9362017-04-16 02:40:45 +00002041 __swap_allocator(this->__alloc(), __x.__alloc(),
Marshall Clow8982dcd2015-07-13 20:04:56 +00002042 integral_constant<bool,__alloc_traits::propagate_on_container_swap::value>());
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002043#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002044 __get_db()->swap(this, &__x);
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002045#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00002046}
2047
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002048template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002049bool
2050vector<_Tp, _Allocator>::__invariants() const
2051{
Howard Hinnant76053d72013-06-27 19:35:32 +00002052 if (this->__begin_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002053 {
Howard Hinnant76053d72013-06-27 19:35:32 +00002054 if (this->__end_ != nullptr || this->__end_cap() != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002055 return false;
2056 }
2057 else
2058 {
2059 if (this->__begin_ > this->__end_)
2060 return false;
2061 if (this->__begin_ == this->__end_cap())
2062 return false;
2063 if (this->__end_ > this->__end_cap())
2064 return false;
2065 }
2066 return true;
2067}
2068
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002069#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002070
Howard Hinnantc51e1022010-05-11 19:42:16 +00002071template <class _Tp, class _Allocator>
Howard Hinnant27e0e772011-09-14 18:33:51 +00002072bool
2073vector<_Tp, _Allocator>::__dereferenceable(const const_iterator* __i) const
2074{
2075 return this->__begin_ <= __i->base() && __i->base() < this->__end_;
2076}
2077
2078template <class _Tp, class _Allocator>
2079bool
2080vector<_Tp, _Allocator>::__decrementable(const const_iterator* __i) const
2081{
2082 return this->__begin_ < __i->base() && __i->base() <= this->__end_;
2083}
2084
2085template <class _Tp, class _Allocator>
2086bool
2087vector<_Tp, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
2088{
2089 const_pointer __p = __i->base() + __n;
2090 return this->__begin_ <= __p && __p <= this->__end_;
2091}
2092
2093template <class _Tp, class _Allocator>
2094bool
2095vector<_Tp, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
2096{
2097 const_pointer __p = __i->base() + __n;
2098 return this->__begin_ <= __p && __p < this->__end_;
2099}
2100
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002101#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002102
2103template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002104inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002105void
2106vector<_Tp, _Allocator>::__invalidate_all_iterators()
2107{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002108#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002109 __get_db()->__invalidate_all(this);
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002110#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00002111}
2112
Eric Fiselier69c51982016-12-28 06:06:09 +00002113
2114template <class _Tp, class _Allocator>
2115inline _LIBCPP_INLINE_VISIBILITY
2116void
2117vector<_Tp, _Allocator>::__invalidate_iterators_past(pointer __new_last) {
2118#if _LIBCPP_DEBUG_LEVEL >= 2
2119 __c_node* __c = __get_db()->__find_c_and_lock(this);
2120 for (__i_node** __p = __c->end_; __p != __c->beg_; ) {
2121 --__p;
2122 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
2123 if (__i->base() > __new_last) {
2124 (*__p)->__c_ = nullptr;
2125 if (--__c->end_ != __p)
2126 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
2127 }
2128 }
2129 __get_db()->unlock();
2130#else
2131 ((void)__new_last);
2132#endif
2133}
2134
Howard Hinnantc51e1022010-05-11 19:42:16 +00002135// vector<bool>
2136
2137template <class _Allocator> class vector<bool, _Allocator>;
2138
2139template <class _Allocator> struct hash<vector<bool, _Allocator> >;
2140
2141template <class _Allocator>
Howard Hinnantb4ef6482011-07-02 20:33:23 +00002142struct __has_storage_type<vector<bool, _Allocator> >
2143{
2144 static const bool value = true;
2145};
2146
2147template <class _Allocator>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002148class _LIBCPP_TEMPLATE_VIS vector<bool, _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002149 : private __vector_base_common<true>
2150{
2151public:
2152 typedef vector __self;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002153 typedef bool value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002154 typedef _Allocator allocator_type;
2155 typedef allocator_traits<allocator_type> __alloc_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002156 typedef typename __alloc_traits::size_type size_type;
2157 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant896b7622012-05-07 16:50:38 +00002158 typedef size_type __storage_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002159 typedef __bit_iterator<vector, false> pointer;
2160 typedef __bit_iterator<vector, true> const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002161 typedef pointer iterator;
2162 typedef const_pointer const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002163 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
2164 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002165
2166private:
Marshall Clow940e01c2015-04-07 05:21:38 +00002167 typedef typename __rebind_alloc_helper<__alloc_traits, __storage_type>::type __storage_allocator;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002168 typedef allocator_traits<__storage_allocator> __storage_traits;
2169 typedef typename __storage_traits::pointer __storage_pointer;
2170 typedef typename __storage_traits::const_pointer __const_storage_pointer;
2171
2172 __storage_pointer __begin_;
2173 size_type __size_;
2174 __compressed_pair<size_type, __storage_allocator> __cap_alloc_;
Howard Hinnant71d1c192011-07-09 15:50:42 +00002175public:
Howard Hinnantb4ef6482011-07-02 20:33:23 +00002176 typedef __bit_reference<vector> reference;
2177 typedef __bit_const_reference<vector> const_reference;
Howard Hinnant71d1c192011-07-09 15:50:42 +00002178private:
Howard Hinnant1c936782011-06-03 19:40:40 +00002179 _LIBCPP_INLINE_VISIBILITY
2180 size_type& __cap() _NOEXCEPT
2181 {return __cap_alloc_.first();}
2182 _LIBCPP_INLINE_VISIBILITY
2183 const size_type& __cap() const _NOEXCEPT
2184 {return __cap_alloc_.first();}
2185 _LIBCPP_INLINE_VISIBILITY
2186 __storage_allocator& __alloc() _NOEXCEPT
2187 {return __cap_alloc_.second();}
2188 _LIBCPP_INLINE_VISIBILITY
2189 const __storage_allocator& __alloc() const _NOEXCEPT
2190 {return __cap_alloc_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002191
2192 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
2193
Howard Hinnant1c936782011-06-03 19:40:40 +00002194 _LIBCPP_INLINE_VISIBILITY
2195 static size_type __internal_cap_to_external(size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002196 {return __n * __bits_per_word;}
Howard Hinnant1c936782011-06-03 19:40:40 +00002197 _LIBCPP_INLINE_VISIBILITY
2198 static size_type __external_cap_to_internal(size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002199 {return (__n - 1) / __bits_per_word + 1;}
2200
2201public:
Howard Hinnant1c936782011-06-03 19:40:40 +00002202 _LIBCPP_INLINE_VISIBILITY
Marshall Clowe546cbb2015-06-04 02:05:41 +00002203 vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clow8f282f42015-06-04 00:10:20 +00002204
2205 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
2206#if _LIBCPP_STD_VER <= 14
2207 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
2208#else
2209 _NOEXCEPT;
2210#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002211 ~vector();
2212 explicit vector(size_type __n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00002213#if _LIBCPP_STD_VER > 11
2214 explicit vector(size_type __n, const allocator_type& __a);
2215#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002216 vector(size_type __n, const value_type& __v);
2217 vector(size_type __n, const value_type& __v, const allocator_type& __a);
2218 template <class _InputIterator>
2219 vector(_InputIterator __first, _InputIterator __last,
2220 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2221 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2222 template <class _InputIterator>
2223 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2224 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2225 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2226 template <class _ForwardIterator>
2227 vector(_ForwardIterator __first, _ForwardIterator __last,
2228 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2229 template <class _ForwardIterator>
2230 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2231 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2232
2233 vector(const vector& __v);
2234 vector(const vector& __v, const allocator_type& __a);
2235 vector& operator=(const vector& __v);
Eric Fiseliered9e9362017-04-16 02:40:45 +00002236
2237#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002238 vector(initializer_list<value_type> __il);
2239 vector(initializer_list<value_type> __il, const allocator_type& __a);
2240
Howard Hinnant1c936782011-06-03 19:40:40 +00002241 _LIBCPP_INLINE_VISIBILITY
2242 vector(vector&& __v)
Marshall Clowe5108202015-07-14 14:46:32 +00002243#if _LIBCPP_STD_VER > 14
2244 _NOEXCEPT;
2245#else
Howard Hinnant1c936782011-06-03 19:40:40 +00002246 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowe5108202015-07-14 14:46:32 +00002247#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002248 vector(vector&& __v, const allocator_type& __a);
Howard Hinnant1c936782011-06-03 19:40:40 +00002249 _LIBCPP_INLINE_VISIBILITY
2250 vector& operator=(vector&& __v)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002251 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiseliered9e9362017-04-16 02:40:45 +00002252
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002253 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002254 vector& operator=(initializer_list<value_type> __il)
2255 {assign(__il.begin(), __il.end()); return *this;}
Eric Fiseliered9e9362017-04-16 02:40:45 +00002256
2257#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002258
2259 template <class _InputIterator>
2260 typename enable_if
2261 <
2262 __is_input_iterator<_InputIterator>::value &&
2263 !__is_forward_iterator<_InputIterator>::value,
2264 void
2265 >::type
2266 assign(_InputIterator __first, _InputIterator __last);
2267 template <class _ForwardIterator>
2268 typename enable_if
2269 <
2270 __is_forward_iterator<_ForwardIterator>::value,
2271 void
2272 >::type
2273 assign(_ForwardIterator __first, _ForwardIterator __last);
2274
2275 void assign(size_type __n, const value_type& __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +00002276
2277#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002278 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002279 void assign(initializer_list<value_type> __il)
2280 {assign(__il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +00002281#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002282
Howard Hinnant1c936782011-06-03 19:40:40 +00002283 _LIBCPP_INLINE_VISIBILITY allocator_type get_allocator() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002284 {return allocator_type(this->__alloc());}
2285
Howard Hinnant1c936782011-06-03 19:40:40 +00002286 size_type max_size() const _NOEXCEPT;
2287 _LIBCPP_INLINE_VISIBILITY
2288 size_type capacity() const _NOEXCEPT
2289 {return __internal_cap_to_external(__cap());}
2290 _LIBCPP_INLINE_VISIBILITY
2291 size_type size() const _NOEXCEPT
2292 {return __size_;}
Marshall Clow425f5752017-11-15 05:51:26 +00002293 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +00002294 bool empty() const _NOEXCEPT
2295 {return __size_ == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002296 void reserve(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +00002297 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002298
Howard Hinnant1c936782011-06-03 19:40:40 +00002299 _LIBCPP_INLINE_VISIBILITY
2300 iterator begin() _NOEXCEPT
2301 {return __make_iter(0);}
2302 _LIBCPP_INLINE_VISIBILITY
2303 const_iterator begin() const _NOEXCEPT
2304 {return __make_iter(0);}
2305 _LIBCPP_INLINE_VISIBILITY
2306 iterator end() _NOEXCEPT
2307 {return __make_iter(__size_);}
2308 _LIBCPP_INLINE_VISIBILITY
2309 const_iterator end() const _NOEXCEPT
2310 {return __make_iter(__size_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002311
Howard Hinnant1c936782011-06-03 19:40:40 +00002312 _LIBCPP_INLINE_VISIBILITY
2313 reverse_iterator rbegin() _NOEXCEPT
2314 {return reverse_iterator(end());}
2315 _LIBCPP_INLINE_VISIBILITY
2316 const_reverse_iterator rbegin() const _NOEXCEPT
2317 {return const_reverse_iterator(end());}
2318 _LIBCPP_INLINE_VISIBILITY
2319 reverse_iterator rend() _NOEXCEPT
2320 {return reverse_iterator(begin());}
2321 _LIBCPP_INLINE_VISIBILITY
2322 const_reverse_iterator rend() const _NOEXCEPT
2323 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002324
Howard Hinnant1c936782011-06-03 19:40:40 +00002325 _LIBCPP_INLINE_VISIBILITY
2326 const_iterator cbegin() const _NOEXCEPT
2327 {return __make_iter(0);}
2328 _LIBCPP_INLINE_VISIBILITY
2329 const_iterator cend() const _NOEXCEPT
2330 {return __make_iter(__size_);}
2331 _LIBCPP_INLINE_VISIBILITY
2332 const_reverse_iterator crbegin() const _NOEXCEPT
2333 {return rbegin();}
2334 _LIBCPP_INLINE_VISIBILITY
2335 const_reverse_iterator crend() const _NOEXCEPT
2336 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002337
2338 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) {return __make_ref(__n);}
2339 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const {return __make_ref(__n);}
2340 reference at(size_type __n);
2341 const_reference at(size_type __n) const;
2342
2343 _LIBCPP_INLINE_VISIBILITY reference front() {return __make_ref(0);}
2344 _LIBCPP_INLINE_VISIBILITY const_reference front() const {return __make_ref(0);}
2345 _LIBCPP_INLINE_VISIBILITY reference back() {return __make_ref(__size_ - 1);}
2346 _LIBCPP_INLINE_VISIBILITY const_reference back() const {return __make_ref(__size_ - 1);}
2347
2348 void push_back(const value_type& __x);
Marshall Clowc46bb8e2013-08-13 23:54:12 +00002349#if _LIBCPP_STD_VER > 11
2350 template <class... _Args>
Marshall Clowea52cc42017-01-24 23:09:12 +00002351#if _LIBCPP_STD_VER > 14
2352 _LIBCPP_INLINE_VISIBILITY reference emplace_back(_Args&&... __args)
2353#else
2354 _LIBCPP_INLINE_VISIBILITY void emplace_back(_Args&&... __args)
2355#endif
2356 {
Eric Fiselier34ba5b92016-07-21 03:20:17 +00002357 push_back ( value_type ( _VSTD::forward<_Args>(__args)... ));
Marshall Clowea52cc42017-01-24 23:09:12 +00002358#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00002359 return this->back();
Marshall Clowea52cc42017-01-24 23:09:12 +00002360#endif
Eric Fiselier34ba5b92016-07-21 03:20:17 +00002361 }
Marshall Clowc46bb8e2013-08-13 23:54:12 +00002362#endif
2363
Howard Hinnantc51e1022010-05-11 19:42:16 +00002364 _LIBCPP_INLINE_VISIBILITY void pop_back() {--__size_;}
2365
Marshall Clowc46bb8e2013-08-13 23:54:12 +00002366#if _LIBCPP_STD_VER > 11
2367 template <class... _Args>
2368 _LIBCPP_INLINE_VISIBILITY iterator emplace(const_iterator position, _Args&&... __args)
2369 { return insert ( position, value_type ( _VSTD::forward<_Args>(__args)... )); }
2370#endif
2371
Howard Hinnantc51e1022010-05-11 19:42:16 +00002372 iterator insert(const_iterator __position, const value_type& __x);
2373 iterator insert(const_iterator __position, size_type __n, const value_type& __x);
2374 iterator insert(const_iterator __position, size_type __n, const_reference __x);
2375 template <class _InputIterator>
2376 typename enable_if
2377 <
2378 __is_input_iterator <_InputIterator>::value &&
2379 !__is_forward_iterator<_InputIterator>::value,
2380 iterator
2381 >::type
2382 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
2383 template <class _ForwardIterator>
2384 typename enable_if
2385 <
2386 __is_forward_iterator<_ForwardIterator>::value,
2387 iterator
2388 >::type
2389 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiseliered9e9362017-04-16 02:40:45 +00002390
2391#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002392 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002393 iterator insert(const_iterator __position, initializer_list<value_type> __il)
2394 {return insert(__position, __il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +00002395#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002396
Howard Hinnantcf823322010-12-17 14:46:43 +00002397 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002398 iterator erase(const_iterator __first, const_iterator __last);
2399
Howard Hinnant1c936782011-06-03 19:40:40 +00002400 _LIBCPP_INLINE_VISIBILITY
2401 void clear() _NOEXCEPT {__size_ = 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002402
Howard Hinnant1c936782011-06-03 19:40:40 +00002403 void swap(vector&)
Marshall Clow8982dcd2015-07-13 20:04:56 +00002404#if _LIBCPP_STD_VER >= 14
2405 _NOEXCEPT;
2406#else
Eric Fiselier69c51982016-12-28 06:06:09 +00002407 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00002408 __is_nothrow_swappable<allocator_type>::value);
2409#endif
Marshall Clow89eb3822016-04-07 14:20:31 +00002410 static void swap(reference __x, reference __y) _NOEXCEPT { _VSTD::swap(__x, __y); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002411
2412 void resize(size_type __sz, value_type __x = false);
Howard Hinnant1c936782011-06-03 19:40:40 +00002413 void flip() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002414
2415 bool __invariants() const;
2416
2417private:
Howard Hinnantcf823322010-12-17 14:46:43 +00002418 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002419 void allocate(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +00002420 void deallocate() _NOEXCEPT;
2421 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantea382952013-08-14 18:00:20 +00002422 static size_type __align_it(size_type __new_size) _NOEXCEPT
Marshall Clow66f34152014-07-28 15:02:42 +00002423 {return __new_size + (__bits_per_word-1) & ~((size_type)__bits_per_word-1);};
Howard Hinnantcf823322010-12-17 14:46:43 +00002424 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
2425 _LIBCPP_INLINE_VISIBILITY void __construct_at_end(size_type __n, bool __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002426 template <class _ForwardIterator>
2427 typename enable_if
2428 <
2429 __is_forward_iterator<_ForwardIterator>::value,
2430 void
2431 >::type
2432 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
2433 void __append(size_type __n, const_reference __x);
Howard Hinnant1c936782011-06-03 19:40:40 +00002434 _LIBCPP_INLINE_VISIBILITY
2435 reference __make_ref(size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002436 {return reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnant1c936782011-06-03 19:40:40 +00002437 _LIBCPP_INLINE_VISIBILITY
2438 const_reference __make_ref(size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002439 {return const_reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnant1c936782011-06-03 19:40:40 +00002440 _LIBCPP_INLINE_VISIBILITY
2441 iterator __make_iter(size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002442 {return iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnant1c936782011-06-03 19:40:40 +00002443 _LIBCPP_INLINE_VISIBILITY
2444 const_iterator __make_iter(size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002445 {return const_iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnant1c936782011-06-03 19:40:40 +00002446 _LIBCPP_INLINE_VISIBILITY
2447 iterator __const_iterator_cast(const_iterator __p) _NOEXCEPT
Howard Hinnant76053d72013-06-27 19:35:32 +00002448 {return begin() + (__p - cbegin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002449
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002450 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002451 void __copy_assign_alloc(const vector& __v)
2452 {__copy_assign_alloc(__v, integral_constant<bool,
2453 __storage_traits::propagate_on_container_copy_assignment::value>());}
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002454 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002455 void __copy_assign_alloc(const vector& __c, true_type)
2456 {
2457 if (__alloc() != __c.__alloc())
2458 deallocate();
2459 __alloc() = __c.__alloc();
2460 }
2461
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002462 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00002463 void __copy_assign_alloc(const vector&, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002464 {}
2465
2466 void __move_assign(vector& __c, false_type);
Howard Hinnant1c936782011-06-03 19:40:40 +00002467 void __move_assign(vector& __c, true_type)
2468 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002469 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002470 void __move_assign_alloc(vector& __c)
Howard Hinnant1c936782011-06-03 19:40:40 +00002471 _NOEXCEPT_(
2472 !__storage_traits::propagate_on_container_move_assignment::value ||
2473 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002474 {__move_assign_alloc(__c, integral_constant<bool,
2475 __storage_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002476 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +00002477 void __move_assign_alloc(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002478 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002479 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002480 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002481 }
2482
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002483 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00002484 void __move_assign_alloc(vector&, false_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002485 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002486 {}
2487
Howard Hinnant1c936782011-06-03 19:40:40 +00002488 size_t __hash_code() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002489
2490 friend class __bit_reference<vector>;
2491 friend class __bit_const_reference<vector>;
2492 friend class __bit_iterator<vector, false>;
2493 friend class __bit_iterator<vector, true>;
Howard Hinnant4210a0f2012-08-17 17:10:18 +00002494 friend struct __bit_array<vector>;
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002495 friend struct _LIBCPP_TEMPLATE_VIS hash<vector>;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002496};
2497
2498template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002499inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002500void
2501vector<bool, _Allocator>::__invalidate_all_iterators()
2502{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002503}
2504
2505// Allocate space for __n objects
2506// throws length_error if __n > max_size()
2507// throws (probably bad_alloc) if memory run out
2508// Precondition: __begin_ == __end_ == __cap() == 0
2509// Precondition: __n > 0
2510// Postcondition: capacity() == __n
2511// Postcondition: size() == 0
2512template <class _Allocator>
2513void
2514vector<bool, _Allocator>::allocate(size_type __n)
2515{
2516 if (__n > max_size())
2517 this->__throw_length_error();
2518 __n = __external_cap_to_internal(__n);
2519 this->__begin_ = __storage_traits::allocate(this->__alloc(), __n);
2520 this->__size_ = 0;
2521 this->__cap() = __n;
2522}
2523
2524template <class _Allocator>
2525void
Howard Hinnant1c936782011-06-03 19:40:40 +00002526vector<bool, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002527{
Howard Hinnant76053d72013-06-27 19:35:32 +00002528 if (this->__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002529 {
2530 __storage_traits::deallocate(this->__alloc(), this->__begin_, __cap());
2531 __invalidate_all_iterators();
Howard Hinnant76053d72013-06-27 19:35:32 +00002532 this->__begin_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002533 this->__size_ = this->__cap() = 0;
2534 }
2535}
2536
2537template <class _Allocator>
2538typename vector<bool, _Allocator>::size_type
Howard Hinnant1c936782011-06-03 19:40:40 +00002539vector<bool, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002540{
2541 size_type __amax = __storage_traits::max_size(__alloc());
2542 size_type __nmax = numeric_limits<size_type>::max() / 2; // end() >= begin(), always
2543 if (__nmax / __bits_per_word <= __amax)
2544 return __nmax;
2545 return __internal_cap_to_external(__amax);
2546}
2547
2548// Precondition: __new_size > capacity()
2549template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002550inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002551typename vector<bool, _Allocator>::size_type
2552vector<bool, _Allocator>::__recommend(size_type __new_size) const
2553{
2554 const size_type __ms = max_size();
2555 if (__new_size > __ms)
2556 this->__throw_length_error();
2557 const size_type __cap = capacity();
2558 if (__cap >= __ms / 2)
2559 return __ms;
Howard Hinnantea382952013-08-14 18:00:20 +00002560 return _VSTD::max(2*__cap, __align_it(__new_size));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002561}
2562
2563// Default constructs __n objects starting at __end_
2564// Precondition: __n > 0
2565// Precondition: size() + __n <= capacity()
2566// Postcondition: size() == size() + __n
2567template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002568inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002569void
2570vector<bool, _Allocator>::__construct_at_end(size_type __n, bool __x)
2571{
2572 size_type __old_size = this->__size_;
2573 this->__size_ += __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002574 _VSTD::fill_n(__make_iter(__old_size), __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002575}
2576
2577template <class _Allocator>
2578template <class _ForwardIterator>
2579typename enable_if
2580<
2581 __is_forward_iterator<_ForwardIterator>::value,
2582 void
2583>::type
2584vector<bool, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
2585{
2586 size_type __old_size = this->__size_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002587 this->__size_ += _VSTD::distance(__first, __last);
2588 _VSTD::copy(__first, __last, __make_iter(__old_size));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002589}
2590
2591template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002592inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002593vector<bool, _Allocator>::vector()
Marshall Clowe546cbb2015-06-04 02:05:41 +00002594 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant76053d72013-06-27 19:35:32 +00002595 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002596 __size_(0),
2597 __cap_alloc_(0)
2598{
2599}
2600
2601template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002602inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002603vector<bool, _Allocator>::vector(const allocator_type& __a)
Marshall Clow8f282f42015-06-04 00:10:20 +00002604#if _LIBCPP_STD_VER <= 14
2605 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
2606#else
2607 _NOEXCEPT
2608#endif
Howard Hinnant76053d72013-06-27 19:35:32 +00002609 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002610 __size_(0),
2611 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2612{
2613}
2614
2615template <class _Allocator>
2616vector<bool, _Allocator>::vector(size_type __n)
Howard Hinnant76053d72013-06-27 19:35:32 +00002617 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002618 __size_(0),
2619 __cap_alloc_(0)
2620{
2621 if (__n > 0)
2622 {
2623 allocate(__n);
2624 __construct_at_end(__n, false);
2625 }
2626}
2627
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00002628#if _LIBCPP_STD_VER > 11
2629template <class _Allocator>
2630vector<bool, _Allocator>::vector(size_type __n, const allocator_type& __a)
2631 : __begin_(nullptr),
2632 __size_(0),
2633 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2634{
2635 if (__n > 0)
2636 {
2637 allocate(__n);
2638 __construct_at_end(__n, false);
2639 }
2640}
2641#endif
2642
Howard Hinnantc51e1022010-05-11 19:42:16 +00002643template <class _Allocator>
2644vector<bool, _Allocator>::vector(size_type __n, const value_type& __x)
Howard Hinnant76053d72013-06-27 19:35:32 +00002645 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002646 __size_(0),
2647 __cap_alloc_(0)
2648{
2649 if (__n > 0)
2650 {
2651 allocate(__n);
2652 __construct_at_end(__n, __x);
2653 }
2654}
2655
2656template <class _Allocator>
2657vector<bool, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002658 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002659 __size_(0),
2660 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2661{
2662 if (__n > 0)
2663 {
2664 allocate(__n);
2665 __construct_at_end(__n, __x);
2666 }
2667}
2668
2669template <class _Allocator>
2670template <class _InputIterator>
2671vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last,
2672 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2673 !__is_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002674 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002675 __size_(0),
2676 __cap_alloc_(0)
2677{
2678#ifndef _LIBCPP_NO_EXCEPTIONS
2679 try
2680 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002681#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002682 for (; __first != __last; ++__first)
2683 push_back(*__first);
2684#ifndef _LIBCPP_NO_EXCEPTIONS
2685 }
2686 catch (...)
2687 {
Howard Hinnant76053d72013-06-27 19:35:32 +00002688 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002689 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2690 __invalidate_all_iterators();
2691 throw;
2692 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002693#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002694}
2695
2696template <class _Allocator>
2697template <class _InputIterator>
2698vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2699 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2700 !__is_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002701 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002702 __size_(0),
2703 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2704{
2705#ifndef _LIBCPP_NO_EXCEPTIONS
2706 try
2707 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002708#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002709 for (; __first != __last; ++__first)
2710 push_back(*__first);
2711#ifndef _LIBCPP_NO_EXCEPTIONS
2712 }
2713 catch (...)
2714 {
Howard Hinnant76053d72013-06-27 19:35:32 +00002715 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002716 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2717 __invalidate_all_iterators();
2718 throw;
2719 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002720#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002721}
2722
2723template <class _Allocator>
2724template <class _ForwardIterator>
2725vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last,
2726 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002727 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002728 __size_(0),
2729 __cap_alloc_(0)
2730{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002731 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002732 if (__n > 0)
2733 {
2734 allocate(__n);
2735 __construct_at_end(__first, __last);
2736 }
2737}
2738
2739template <class _Allocator>
2740template <class _ForwardIterator>
2741vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2742 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002743 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002744 __size_(0),
2745 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2746{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002747 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002748 if (__n > 0)
2749 {
2750 allocate(__n);
2751 __construct_at_end(__first, __last);
2752 }
2753}
2754
Eric Fiseliered9e9362017-04-16 02:40:45 +00002755#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002756
Howard Hinnantc51e1022010-05-11 19:42:16 +00002757template <class _Allocator>
2758vector<bool, _Allocator>::vector(initializer_list<value_type> __il)
Howard Hinnant76053d72013-06-27 19:35:32 +00002759 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002760 __size_(0),
2761 __cap_alloc_(0)
2762{
2763 size_type __n = static_cast<size_type>(__il.size());
2764 if (__n > 0)
2765 {
2766 allocate(__n);
2767 __construct_at_end(__il.begin(), __il.end());
2768 }
2769}
2770
2771template <class _Allocator>
2772vector<bool, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002773 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002774 __size_(0),
2775 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2776{
2777 size_type __n = static_cast<size_type>(__il.size());
2778 if (__n > 0)
2779 {
2780 allocate(__n);
2781 __construct_at_end(__il.begin(), __il.end());
2782 }
2783}
2784
Eric Fiseliered9e9362017-04-16 02:40:45 +00002785#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002786
Howard Hinnantc51e1022010-05-11 19:42:16 +00002787template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002788vector<bool, _Allocator>::~vector()
2789{
Howard Hinnant76053d72013-06-27 19:35:32 +00002790 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002791 __storage_traits::deallocate(__alloc(), __begin_, __cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002792 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002793}
2794
2795template <class _Allocator>
2796vector<bool, _Allocator>::vector(const vector& __v)
Howard Hinnant76053d72013-06-27 19:35:32 +00002797 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002798 __size_(0),
2799 __cap_alloc_(0, __storage_traits::select_on_container_copy_construction(__v.__alloc()))
2800{
2801 if (__v.size() > 0)
2802 {
2803 allocate(__v.size());
2804 __construct_at_end(__v.begin(), __v.end());
2805 }
2806}
2807
2808template <class _Allocator>
2809vector<bool, _Allocator>::vector(const vector& __v, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002810 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002811 __size_(0),
2812 __cap_alloc_(0, __a)
2813{
2814 if (__v.size() > 0)
2815 {
2816 allocate(__v.size());
2817 __construct_at_end(__v.begin(), __v.end());
2818 }
2819}
2820
2821template <class _Allocator>
2822vector<bool, _Allocator>&
2823vector<bool, _Allocator>::operator=(const vector& __v)
2824{
2825 if (this != &__v)
2826 {
2827 __copy_assign_alloc(__v);
2828 if (__v.__size_)
2829 {
2830 if (__v.__size_ > capacity())
2831 {
2832 deallocate();
2833 allocate(__v.__size_);
2834 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002835 _VSTD::copy(__v.__begin_, __v.__begin_ + __external_cap_to_internal(__v.__size_), __begin_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002836 }
2837 __size_ = __v.__size_;
2838 }
2839 return *this;
2840}
2841
Eric Fiseliered9e9362017-04-16 02:40:45 +00002842#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant74279a52010-09-04 23:28:19 +00002843
Howard Hinnantc51e1022010-05-11 19:42:16 +00002844template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002845inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002846vector<bool, _Allocator>::vector(vector&& __v)
Marshall Clowe5108202015-07-14 14:46:32 +00002847#if _LIBCPP_STD_VER > 14
2848 _NOEXCEPT
2849#else
Howard Hinnant1c936782011-06-03 19:40:40 +00002850 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowe5108202015-07-14 14:46:32 +00002851#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002852 : __begin_(__v.__begin_),
2853 __size_(__v.__size_),
2854 __cap_alloc_(__v.__cap_alloc_)
2855{
Howard Hinnant76053d72013-06-27 19:35:32 +00002856 __v.__begin_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002857 __v.__size_ = 0;
2858 __v.__cap() = 0;
2859}
2860
2861template <class _Allocator>
2862vector<bool, _Allocator>::vector(vector&& __v, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002863 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002864 __size_(0),
2865 __cap_alloc_(0, __a)
2866{
2867 if (__a == allocator_type(__v.__alloc()))
2868 {
2869 this->__begin_ = __v.__begin_;
2870 this->__size_ = __v.__size_;
2871 this->__cap() = __v.__cap();
2872 __v.__begin_ = nullptr;
2873 __v.__cap() = __v.__size_ = 0;
2874 }
2875 else if (__v.size() > 0)
2876 {
2877 allocate(__v.size());
2878 __construct_at_end(__v.begin(), __v.end());
2879 }
2880}
2881
2882template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002883inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002884vector<bool, _Allocator>&
2885vector<bool, _Allocator>::operator=(vector&& __v)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002886 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002887{
2888 __move_assign(__v, integral_constant<bool,
2889 __storage_traits::propagate_on_container_move_assignment::value>());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002890 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002891}
2892
2893template <class _Allocator>
2894void
2895vector<bool, _Allocator>::__move_assign(vector& __c, false_type)
2896{
2897 if (__alloc() != __c.__alloc())
2898 assign(__c.begin(), __c.end());
2899 else
2900 __move_assign(__c, true_type());
2901}
2902
2903template <class _Allocator>
2904void
2905vector<bool, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002906 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002907{
2908 deallocate();
Marshall Clowb4871fa2014-07-21 15:15:15 +00002909 __move_assign_alloc(__c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002910 this->__begin_ = __c.__begin_;
2911 this->__size_ = __c.__size_;
2912 this->__cap() = __c.__cap();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002913 __c.__begin_ = nullptr;
2914 __c.__cap() = __c.__size_ = 0;
2915}
Howard Hinnant74279a52010-09-04 23:28:19 +00002916
Eric Fiseliered9e9362017-04-16 02:40:45 +00002917#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002918
2919template <class _Allocator>
2920void
2921vector<bool, _Allocator>::assign(size_type __n, const value_type& __x)
2922{
2923 __size_ = 0;
2924 if (__n > 0)
2925 {
2926 size_type __c = capacity();
2927 if (__n <= __c)
2928 __size_ = __n;
2929 else
2930 {
2931 vector __v(__alloc());
2932 __v.reserve(__recommend(__n));
2933 __v.__size_ = __n;
2934 swap(__v);
2935 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002936 _VSTD::fill_n(begin(), __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002937 }
Eric Fiselier69c51982016-12-28 06:06:09 +00002938 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002939}
2940
2941template <class _Allocator>
2942template <class _InputIterator>
2943typename enable_if
2944<
2945 __is_input_iterator<_InputIterator>::value &&
2946 !__is_forward_iterator<_InputIterator>::value,
2947 void
2948>::type
2949vector<bool, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2950{
2951 clear();
2952 for (; __first != __last; ++__first)
2953 push_back(*__first);
2954}
2955
2956template <class _Allocator>
2957template <class _ForwardIterator>
2958typename enable_if
2959<
2960 __is_forward_iterator<_ForwardIterator>::value,
2961 void
2962>::type
2963vector<bool, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2964{
2965 clear();
Eric Fiselier6003c772016-12-23 23:37:52 +00002966 difference_type __ns = _VSTD::distance(__first, __last);
2967 _LIBCPP_ASSERT(__ns >= 0, "invalid range specified");
2968 const size_t __n = static_cast<size_type>(__ns);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002969 if (__n)
2970 {
2971 if (__n > capacity())
2972 {
2973 deallocate();
2974 allocate(__n);
2975 }
2976 __construct_at_end(__first, __last);
2977 }
2978}
2979
2980template <class _Allocator>
2981void
2982vector<bool, _Allocator>::reserve(size_type __n)
2983{
2984 if (__n > capacity())
2985 {
2986 vector __v(this->__alloc());
2987 __v.allocate(__n);
2988 __v.__construct_at_end(this->begin(), this->end());
2989 swap(__v);
2990 __invalidate_all_iterators();
2991 }
2992}
2993
2994template <class _Allocator>
2995void
Howard Hinnant1c936782011-06-03 19:40:40 +00002996vector<bool, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002997{
2998 if (__external_cap_to_internal(size()) > __cap())
2999 {
3000#ifndef _LIBCPP_NO_EXCEPTIONS
3001 try
3002 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003003#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003004 vector(*this, allocator_type(__alloc())).swap(*this);
3005#ifndef _LIBCPP_NO_EXCEPTIONS
3006 }
3007 catch (...)
3008 {
3009 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003010#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003011 }
3012}
3013
3014template <class _Allocator>
3015typename vector<bool, _Allocator>::reference
3016vector<bool, _Allocator>::at(size_type __n)
3017{
3018 if (__n >= size())
3019 this->__throw_out_of_range();
3020 return (*this)[__n];
3021}
3022
3023template <class _Allocator>
3024typename vector<bool, _Allocator>::const_reference
3025vector<bool, _Allocator>::at(size_type __n) const
3026{
3027 if (__n >= size())
3028 this->__throw_out_of_range();
3029 return (*this)[__n];
3030}
3031
3032template <class _Allocator>
3033void
3034vector<bool, _Allocator>::push_back(const value_type& __x)
3035{
3036 if (this->__size_ == this->capacity())
3037 reserve(__recommend(this->__size_ + 1));
3038 ++this->__size_;
3039 back() = __x;
3040}
3041
3042template <class _Allocator>
3043typename vector<bool, _Allocator>::iterator
3044vector<bool, _Allocator>::insert(const_iterator __position, const value_type& __x)
3045{
3046 iterator __r;
3047 if (size() < capacity())
3048 {
3049 const_iterator __old_end = end();
3050 ++__size_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003051 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003052 __r = __const_iterator_cast(__position);
3053 }
3054 else
3055 {
3056 vector __v(__alloc());
3057 __v.reserve(__recommend(__size_ + 1));
3058 __v.__size_ = __size_ + 1;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003059 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3060 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003061 swap(__v);
3062 }
3063 *__r = __x;
3064 return __r;
3065}
3066
3067template <class _Allocator>
3068typename vector<bool, _Allocator>::iterator
3069vector<bool, _Allocator>::insert(const_iterator __position, size_type __n, const value_type& __x)
3070{
3071 iterator __r;
3072 size_type __c = capacity();
3073 if (__n <= __c && size() <= __c - __n)
3074 {
3075 const_iterator __old_end = end();
3076 __size_ += __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003077 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003078 __r = __const_iterator_cast(__position);
3079 }
3080 else
3081 {
3082 vector __v(__alloc());
3083 __v.reserve(__recommend(__size_ + __n));
3084 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003085 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3086 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003087 swap(__v);
3088 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003089 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003090 return __r;
3091}
3092
3093template <class _Allocator>
3094template <class _InputIterator>
3095typename enable_if
3096<
3097 __is_input_iterator <_InputIterator>::value &&
3098 !__is_forward_iterator<_InputIterator>::value,
3099 typename vector<bool, _Allocator>::iterator
3100>::type
3101vector<bool, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
3102{
3103 difference_type __off = __position - begin();
3104 iterator __p = __const_iterator_cast(__position);
3105 iterator __old_end = end();
3106 for (; size() != capacity() && __first != __last; ++__first)
3107 {
3108 ++this->__size_;
3109 back() = *__first;
3110 }
3111 vector __v(__alloc());
3112 if (__first != __last)
3113 {
3114#ifndef _LIBCPP_NO_EXCEPTIONS
3115 try
3116 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003117#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003118 __v.assign(__first, __last);
3119 difference_type __old_size = static_cast<difference_type>(__old_end - begin());
3120 difference_type __old_p = __p - begin();
3121 reserve(__recommend(size() + __v.size()));
3122 __p = begin() + __old_p;
3123 __old_end = begin() + __old_size;
3124#ifndef _LIBCPP_NO_EXCEPTIONS
3125 }
3126 catch (...)
3127 {
3128 erase(__old_end, end());
3129 throw;
3130 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003131#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003132 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003133 __p = _VSTD::rotate(__p, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003134 insert(__p, __v.begin(), __v.end());
3135 return begin() + __off;
3136}
3137
3138template <class _Allocator>
3139template <class _ForwardIterator>
3140typename enable_if
3141<
3142 __is_forward_iterator<_ForwardIterator>::value,
3143 typename vector<bool, _Allocator>::iterator
3144>::type
3145vector<bool, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
3146{
Eric Fiselier654dd332016-12-11 05:31:00 +00003147 const difference_type __n_signed = _VSTD::distance(__first, __last);
3148 _LIBCPP_ASSERT(__n_signed >= 0, "invalid range specified");
3149 const size_type __n = static_cast<size_type>(__n_signed);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003150 iterator __r;
3151 size_type __c = capacity();
3152 if (__n <= __c && size() <= __c - __n)
3153 {
3154 const_iterator __old_end = end();
3155 __size_ += __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003156 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003157 __r = __const_iterator_cast(__position);
3158 }
3159 else
3160 {
3161 vector __v(__alloc());
3162 __v.reserve(__recommend(__size_ + __n));
3163 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003164 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3165 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003166 swap(__v);
3167 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003168 _VSTD::copy(__first, __last, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003169 return __r;
3170}
3171
3172template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003173inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003174typename vector<bool, _Allocator>::iterator
3175vector<bool, _Allocator>::erase(const_iterator __position)
3176{
3177 iterator __r = __const_iterator_cast(__position);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003178 _VSTD::copy(__position + 1, this->cend(), __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003179 --__size_;
3180 return __r;
3181}
3182
3183template <class _Allocator>
3184typename vector<bool, _Allocator>::iterator
3185vector<bool, _Allocator>::erase(const_iterator __first, const_iterator __last)
3186{
3187 iterator __r = __const_iterator_cast(__first);
3188 difference_type __d = __last - __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003189 _VSTD::copy(__last, this->cend(), __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003190 __size_ -= __d;
3191 return __r;
3192}
3193
3194template <class _Allocator>
3195void
3196vector<bool, _Allocator>::swap(vector& __x)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003197#if _LIBCPP_STD_VER >= 14
3198 _NOEXCEPT
3199#else
Eric Fiselier69c51982016-12-28 06:06:09 +00003200 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003201 __is_nothrow_swappable<allocator_type>::value)
3202#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003203{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003204 _VSTD::swap(this->__begin_, __x.__begin_);
3205 _VSTD::swap(this->__size_, __x.__size_);
3206 _VSTD::swap(this->__cap(), __x.__cap());
Eric Fiseliered9e9362017-04-16 02:40:45 +00003207 __swap_allocator(this->__alloc(), __x.__alloc(),
Marshall Clow8982dcd2015-07-13 20:04:56 +00003208 integral_constant<bool, __alloc_traits::propagate_on_container_swap::value>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003209}
3210
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003211template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003212void
3213vector<bool, _Allocator>::resize(size_type __sz, value_type __x)
3214{
3215 size_type __cs = size();
3216 if (__cs < __sz)
3217 {
3218 iterator __r;
3219 size_type __c = capacity();
3220 size_type __n = __sz - __cs;
3221 if (__n <= __c && __cs <= __c - __n)
3222 {
3223 __r = end();
3224 __size_ += __n;
3225 }
3226 else
3227 {
3228 vector __v(__alloc());
3229 __v.reserve(__recommend(__size_ + __n));
3230 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003231 __r = _VSTD::copy(cbegin(), cend(), __v.begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003232 swap(__v);
3233 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003234 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003235 }
3236 else
3237 __size_ = __sz;
3238}
3239
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003240template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003241void
Howard Hinnant1c936782011-06-03 19:40:40 +00003242vector<bool, _Allocator>::flip() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003243{
3244 // do middle whole words
3245 size_type __n = __size_;
3246 __storage_pointer __p = __begin_;
3247 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3248 *__p = ~*__p;
3249 // do last partial word
3250 if (__n > 0)
3251 {
3252 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3253 __storage_type __b = *__p & __m;
3254 *__p &= ~__m;
3255 *__p |= ~__b & __m;
3256 }
3257}
3258
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003259template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003260bool
3261vector<bool, _Allocator>::__invariants() const
3262{
Howard Hinnant76053d72013-06-27 19:35:32 +00003263 if (this->__begin_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003264 {
3265 if (this->__size_ != 0 || this->__cap() != 0)
3266 return false;
3267 }
3268 else
3269 {
3270 if (this->__cap() == 0)
3271 return false;
3272 if (this->__size_ > this->capacity())
3273 return false;
3274 }
3275 return true;
3276}
3277
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003278template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003279size_t
Howard Hinnant1c936782011-06-03 19:40:40 +00003280vector<bool, _Allocator>::__hash_code() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003281{
3282 size_t __h = 0;
3283 // do middle whole words
3284 size_type __n = __size_;
3285 __storage_pointer __p = __begin_;
3286 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3287 __h ^= *__p;
3288 // do last partial word
3289 if (__n > 0)
3290 {
3291 const __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3292 __h ^= *__p & __m;
3293 }
3294 return __h;
3295}
3296
3297template <class _Allocator>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003298struct _LIBCPP_TEMPLATE_VIS hash<vector<bool, _Allocator> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00003299 : public unary_function<vector<bool, _Allocator>, size_t>
3300{
Howard Hinnant1c265cd2010-09-23 18:58:28 +00003301 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +00003302 size_t operator()(const vector<bool, _Allocator>& __vec) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003303 {return __vec.__hash_code();}
3304};
3305
3306template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003307inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003308bool
3309operator==(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3310{
3311 const typename vector<_Tp, _Allocator>::size_type __sz = __x.size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003312 return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003313}
3314
3315template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003316inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003317bool
3318operator!=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3319{
3320 return !(__x == __y);
3321}
3322
3323template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003324inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003325bool
3326operator< (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3327{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003328 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003329}
3330
3331template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003332inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003333bool
3334operator> (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3335{
3336 return __y < __x;
3337}
3338
3339template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003340inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003341bool
3342operator>=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3343{
3344 return !(__x < __y);
3345}
3346
3347template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003348inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003349bool
3350operator<=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3351{
3352 return !(__y < __x);
3353}
3354
3355template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003356inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003357void
3358swap(vector<_Tp, _Allocator>& __x, vector<_Tp, _Allocator>& __y)
Howard Hinnant1c936782011-06-03 19:40:40 +00003359 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00003360{
3361 __x.swap(__y);
3362}
3363
3364_LIBCPP_END_NAMESPACE_STD
3365
Eric Fiselierf4433a32017-05-31 22:07:49 +00003366_LIBCPP_POP_MACROS
3367
Howard Hinnantc51e1022010-05-11 19:42:16 +00003368#endif // _LIBCPP_VECTOR