blob: 8fad9ec54832c3531a16e5a8b9630f2dffae17ae [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
Marshall Clowf0ca1492018-05-21 21:30:12 +0000247template <class InputIterator, class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
248 vector(InputIterator, InputIterator, Allocator = Allocator())
249 -> vector<typename iterator_traits<InputIterator>::value_type, Allocator>;
250
Howard Hinnantc51e1022010-05-11 19:42:16 +0000251template <class Allocator> struct hash<std::vector<bool, Allocator>>;
252
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);
255template <class T, class Allocator> bool operator!=(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
256template <class T, class Allocator> bool operator> (const vector<T,Allocator>& x, const vector<T,Allocator>& y);
257template <class T, class Allocator> bool operator>=(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
258template <class T, class Allocator> bool operator<=(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
259
Howard Hinnant1c936782011-06-03 19:40:40 +0000260template <class T, class Allocator>
261void swap(vector<T,Allocator>& x, vector<T,Allocator>& y)
262 noexcept(noexcept(x.swap(y)));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000263
264} // std
265
266*/
267
268#include <__config>
Eric Fiselier876c6862016-02-20 00:19:45 +0000269#include <iosfwd> // for forward declaration of vector
Howard Hinnantc51e1022010-05-11 19:42:16 +0000270#include <__bit_reference>
271#include <type_traits>
272#include <climits>
273#include <limits>
274#include <initializer_list>
275#include <memory>
276#include <stdexcept>
277#include <algorithm>
278#include <cstring>
279#include <__split_buffer>
280#include <__functional_base>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000281
Eric Fiselier14b6de92014-08-10 23:53:08 +0000282#include <__debug>
Howard Hinnante6ff0b62013-08-02 00:26:35 +0000283
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000284#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000285#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000286#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000287
Eric Fiselierf4433a32017-05-31 22:07:49 +0000288_LIBCPP_PUSH_MACROS
289#include <__undef_macros>
290
291
Howard Hinnantc51e1022010-05-11 19:42:16 +0000292_LIBCPP_BEGIN_NAMESPACE_STD
293
294template <bool>
295class __vector_base_common
296{
297protected:
298 _LIBCPP_ALWAYS_INLINE __vector_base_common() {}
Marshall Clow8fea1612016-08-25 15:09:01 +0000299 _LIBCPP_NORETURN void __throw_length_error() const;
300 _LIBCPP_NORETURN void __throw_out_of_range() const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000301};
302
303template <bool __b>
304void
305__vector_base_common<__b>::__throw_length_error() const
306{
Marshall Clow8fea1612016-08-25 15:09:01 +0000307 _VSTD::__throw_length_error("vector");
Howard Hinnantc51e1022010-05-11 19:42:16 +0000308}
309
310template <bool __b>
311void
312__vector_base_common<__b>::__throw_out_of_range() const
313{
Marshall Clow8fea1612016-08-25 15:09:01 +0000314 _VSTD::__throw_out_of_range("vector");
Howard Hinnantc51e1022010-05-11 19:42:16 +0000315}
316
Eric Fiselier1b57fa82016-09-15 22:27:07 +0000317_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __vector_base_common<true>)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000318
319template <class _Tp, class _Allocator>
320class __vector_base
321 : protected __vector_base_common<true>
322{
Marshall Clowf0ca1492018-05-21 21:30:12 +0000323public:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000324 typedef _Allocator allocator_type;
325 typedef allocator_traits<allocator_type> __alloc_traits;
Marshall Clowf0ca1492018-05-21 21:30:12 +0000326 typedef typename __alloc_traits::size_type size_type;
327protected:
328 typedef _Tp value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000329 typedef value_type& reference;
330 typedef const value_type& const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000331 typedef typename __alloc_traits::difference_type difference_type;
332 typedef typename __alloc_traits::pointer pointer;
333 typedef typename __alloc_traits::const_pointer const_pointer;
334 typedef pointer iterator;
335 typedef const_pointer const_iterator;
336
337 pointer __begin_;
338 pointer __end_;
339 __compressed_pair<pointer, allocator_type> __end_cap_;
340
Howard Hinnant1c936782011-06-03 19:40:40 +0000341 _LIBCPP_INLINE_VISIBILITY
342 allocator_type& __alloc() _NOEXCEPT
343 {return __end_cap_.second();}
344 _LIBCPP_INLINE_VISIBILITY
345 const allocator_type& __alloc() const _NOEXCEPT
346 {return __end_cap_.second();}
347 _LIBCPP_INLINE_VISIBILITY
348 pointer& __end_cap() _NOEXCEPT
349 {return __end_cap_.first();}
350 _LIBCPP_INLINE_VISIBILITY
351 const pointer& __end_cap() const _NOEXCEPT
352 {return __end_cap_.first();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000353
Howard Hinnant1c936782011-06-03 19:40:40 +0000354 _LIBCPP_INLINE_VISIBILITY
355 __vector_base()
356 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnantcf823322010-12-17 14:46:43 +0000357 _LIBCPP_INLINE_VISIBILITY __vector_base(const allocator_type& __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000358 ~__vector_base();
359
Howard Hinnant1c936782011-06-03 19:40:40 +0000360 _LIBCPP_INLINE_VISIBILITY
361 void clear() _NOEXCEPT {__destruct_at_end(__begin_);}
362 _LIBCPP_INLINE_VISIBILITY
363 size_type capacity() const _NOEXCEPT
364 {return static_cast<size_type>(__end_cap() - __begin_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000365
Howard Hinnant1c936782011-06-03 19:40:40 +0000366 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76053d72013-06-27 19:35:32 +0000367 void __destruct_at_end(pointer __new_last) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000368
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000369 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000370 void __copy_assign_alloc(const __vector_base& __c)
371 {__copy_assign_alloc(__c, integral_constant<bool,
372 __alloc_traits::propagate_on_container_copy_assignment::value>());}
373
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000374 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000375 void __move_assign_alloc(__vector_base& __c)
Howard Hinnant1c936782011-06-03 19:40:40 +0000376 _NOEXCEPT_(
377 !__alloc_traits::propagate_on_container_move_assignment::value ||
378 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000379 {__move_assign_alloc(__c, integral_constant<bool,
380 __alloc_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000381private:
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000382 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000383 void __copy_assign_alloc(const __vector_base& __c, true_type)
384 {
385 if (__alloc() != __c.__alloc())
386 {
387 clear();
388 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
389 __begin_ = __end_ = __end_cap() = nullptr;
390 }
391 __alloc() = __c.__alloc();
392 }
393
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000394 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +0000395 void __copy_assign_alloc(const __vector_base&, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000396 {}
397
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000398 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +0000399 void __move_assign_alloc(__vector_base& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +0000400 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000401 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000402 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000403 }
404
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000405 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +0000406 void __move_assign_alloc(__vector_base&, false_type)
Howard Hinnant1c936782011-06-03 19:40:40 +0000407 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000408 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000409};
410
411template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000412inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000413void
Howard Hinnant76053d72013-06-27 19:35:32 +0000414__vector_base<_Tp, _Allocator>::__destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000415{
Bruce Mitchener405fdcd2017-03-23 14:39:23 +0000416 pointer __soon_to_be_end = __end_;
417 while (__new_last != __soon_to_be_end)
418 __alloc_traits::destroy(__alloc(), _VSTD::__to_raw_pointer(--__soon_to_be_end));
419 __end_ = __new_last;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000420}
421
422template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000423inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000424__vector_base<_Tp, _Allocator>::__vector_base()
Howard Hinnant1c936782011-06-03 19:40:40 +0000425 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant76053d72013-06-27 19:35:32 +0000426 : __begin_(nullptr),
427 __end_(nullptr),
428 __end_cap_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000429{
430}
431
432template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000433inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000434__vector_base<_Tp, _Allocator>::__vector_base(const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +0000435 : __begin_(nullptr),
436 __end_(nullptr),
437 __end_cap_(nullptr, __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000438{
439}
440
441template <class _Tp, class _Allocator>
442__vector_base<_Tp, _Allocator>::~__vector_base()
443{
Howard Hinnant76053d72013-06-27 19:35:32 +0000444 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000445 {
446 clear();
447 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
448 }
449}
450
Eric Fiselier876c6862016-02-20 00:19:45 +0000451template <class _Tp, class _Allocator /* = allocator<_Tp> */>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000452class _LIBCPP_TEMPLATE_VIS vector
Howard Hinnantc51e1022010-05-11 19:42:16 +0000453 : private __vector_base<_Tp, _Allocator>
454{
455private:
456 typedef __vector_base<_Tp, _Allocator> __base;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000457 typedef allocator<_Tp> __default_allocator_type;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000458public:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000459 typedef vector __self;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000460 typedef _Tp value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000461 typedef _Allocator allocator_type;
462 typedef typename __base::__alloc_traits __alloc_traits;
463 typedef typename __base::reference reference;
464 typedef typename __base::const_reference const_reference;
465 typedef typename __base::size_type size_type;
466 typedef typename __base::difference_type difference_type;
467 typedef typename __base::pointer pointer;
468 typedef typename __base::const_pointer const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000469 typedef __wrap_iter<pointer> iterator;
470 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000471 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
472 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000473
Howard Hinnanta416ff02013-03-26 19:04:56 +0000474 static_assert((is_same<typename allocator_type::value_type, value_type>::value),
475 "Allocator::value_type must be same type as value_type");
476
Howard Hinnant1c936782011-06-03 19:40:40 +0000477 _LIBCPP_INLINE_VISIBILITY
Marshall Clowe546cbb2015-06-04 02:05:41 +0000478 vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant27e0e772011-09-14 18:33:51 +0000479 {
Howard Hinnanta47c6d52011-09-16 17:29:17 +0000480#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000481 __get_db()->__insert_c(this);
482#endif
483 }
484 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
Marshall Clow8f282f42015-06-04 00:10:20 +0000485#if _LIBCPP_STD_VER <= 14
486 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
487#else
488 _NOEXCEPT
489#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +0000490 : __base(__a)
491 {
Howard Hinnanta47c6d52011-09-16 17:29:17 +0000492#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000493 __get_db()->__insert_c(this);
494#endif
495 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000496 explicit vector(size_type __n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +0000497#if _LIBCPP_STD_VER > 11
498 explicit vector(size_type __n, const allocator_type& __a);
499#endif
Marshall Clowf0ca1492018-05-21 21:30:12 +0000500 vector(size_type __n, const value_type& __x);
501 vector(size_type __n, const value_type& __x, const allocator_type& __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000502 template <class _InputIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +0000503 vector(_InputIterator __first,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000504 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000505 !__is_forward_iterator<_InputIterator>::value &&
506 is_constructible<
507 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +0000508 typename iterator_traits<_InputIterator>::reference>::value,
509 _InputIterator>::type __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000510 template <class _InputIterator>
511 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
512 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000513 !__is_forward_iterator<_InputIterator>::value &&
514 is_constructible<
515 value_type,
516 typename iterator_traits<_InputIterator>::reference>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000517 template <class _ForwardIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +0000518 vector(_ForwardIterator __first,
Howard Hinnant88010252013-03-28 17:44:32 +0000519 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
520 is_constructible<
521 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +0000522 typename iterator_traits<_ForwardIterator>::reference>::value,
523 _ForwardIterator>::type __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000524 template <class _ForwardIterator>
525 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Howard Hinnant88010252013-03-28 17:44:32 +0000526 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
527 is_constructible<
528 value_type,
529 typename iterator_traits<_ForwardIterator>::reference>::value>::type* = 0);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000530
Howard Hinnanta47c6d52011-09-16 17:29:17 +0000531#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000532 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant27e0e772011-09-14 18:33:51 +0000533 ~vector()
534 {
535 __get_db()->__erase_c(this);
536 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000537#endif
538
539 vector(const vector& __x);
540 vector(const vector& __x, const allocator_type& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000541 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000542 vector& operator=(const vector& __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000543
544#ifndef _LIBCPP_CXX03_LANG
545 _LIBCPP_INLINE_VISIBILITY
546 vector(initializer_list<value_type> __il);
547
548 _LIBCPP_INLINE_VISIBILITY
549 vector(initializer_list<value_type> __il, const allocator_type& __a);
550
Howard Hinnantcf823322010-12-17 14:46:43 +0000551 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000552 vector(vector&& __x)
Marshall Clowe5108202015-07-14 14:46:32 +0000553#if _LIBCPP_STD_VER > 14
554 _NOEXCEPT;
555#else
Howard Hinnant1c936782011-06-03 19:40:40 +0000556 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowe5108202015-07-14 14:46:32 +0000557#endif
Eric Fiseliered9e9362017-04-16 02:40:45 +0000558
Howard Hinnantcf823322010-12-17 14:46:43 +0000559 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000560 vector(vector&& __x, const allocator_type& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000561 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000562 vector& operator=(vector&& __x)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000563 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiseliered9e9362017-04-16 02:40:45 +0000564
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000565 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000566 vector& operator=(initializer_list<value_type> __il)
567 {assign(__il.begin(), __il.end()); return *this;}
Eric Fiseliered9e9362017-04-16 02:40:45 +0000568
569#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000570
571 template <class _InputIterator>
572 typename enable_if
573 <
574 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000575 !__is_forward_iterator<_InputIterator>::value &&
576 is_constructible<
577 value_type,
578 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000579 void
580 >::type
581 assign(_InputIterator __first, _InputIterator __last);
582 template <class _ForwardIterator>
583 typename enable_if
584 <
Howard Hinnant88010252013-03-28 17:44:32 +0000585 __is_forward_iterator<_ForwardIterator>::value &&
586 is_constructible<
587 value_type,
588 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000589 void
590 >::type
591 assign(_ForwardIterator __first, _ForwardIterator __last);
592
593 void assign(size_type __n, const_reference __u);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000594
595#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000596 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000597 void assign(initializer_list<value_type> __il)
598 {assign(__il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +0000599#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000600
Howard Hinnant1c936782011-06-03 19:40:40 +0000601 _LIBCPP_INLINE_VISIBILITY
602 allocator_type get_allocator() const _NOEXCEPT
603 {return this->__alloc();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000604
Howard Hinnant1c936782011-06-03 19:40:40 +0000605 _LIBCPP_INLINE_VISIBILITY iterator begin() _NOEXCEPT;
606 _LIBCPP_INLINE_VISIBILITY const_iterator begin() const _NOEXCEPT;
607 _LIBCPP_INLINE_VISIBILITY iterator end() _NOEXCEPT;
608 _LIBCPP_INLINE_VISIBILITY const_iterator end() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000609
Howard Hinnant1c936782011-06-03 19:40:40 +0000610 _LIBCPP_INLINE_VISIBILITY
611 reverse_iterator rbegin() _NOEXCEPT
612 {return reverse_iterator(end());}
613 _LIBCPP_INLINE_VISIBILITY
614 const_reverse_iterator rbegin() const _NOEXCEPT
615 {return const_reverse_iterator(end());}
616 _LIBCPP_INLINE_VISIBILITY
617 reverse_iterator rend() _NOEXCEPT
618 {return reverse_iterator(begin());}
619 _LIBCPP_INLINE_VISIBILITY
620 const_reverse_iterator rend() const _NOEXCEPT
621 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000622
Howard Hinnant1c936782011-06-03 19:40:40 +0000623 _LIBCPP_INLINE_VISIBILITY
624 const_iterator cbegin() const _NOEXCEPT
625 {return begin();}
626 _LIBCPP_INLINE_VISIBILITY
627 const_iterator cend() const _NOEXCEPT
628 {return end();}
629 _LIBCPP_INLINE_VISIBILITY
630 const_reverse_iterator crbegin() const _NOEXCEPT
631 {return rbegin();}
632 _LIBCPP_INLINE_VISIBILITY
633 const_reverse_iterator crend() const _NOEXCEPT
634 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000635
Howard Hinnant1c936782011-06-03 19:40:40 +0000636 _LIBCPP_INLINE_VISIBILITY
637 size_type size() const _NOEXCEPT
638 {return static_cast<size_type>(this->__end_ - this->__begin_);}
639 _LIBCPP_INLINE_VISIBILITY
640 size_type capacity() const _NOEXCEPT
641 {return __base::capacity();}
Marshall Clow425f5752017-11-15 05:51:26 +0000642 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000643 bool empty() const _NOEXCEPT
644 {return this->__begin_ == this->__end_;}
645 size_type max_size() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000646 void reserve(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +0000647 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000648
649 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n);
650 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const;
651 reference at(size_type __n);
652 const_reference at(size_type __n) const;
653
Howard Hinnant27e0e772011-09-14 18:33:51 +0000654 _LIBCPP_INLINE_VISIBILITY reference front()
655 {
656 _LIBCPP_ASSERT(!empty(), "front() called for empty vector");
657 return *this->__begin_;
658 }
659 _LIBCPP_INLINE_VISIBILITY const_reference front() const
660 {
661 _LIBCPP_ASSERT(!empty(), "front() called for empty vector");
662 return *this->__begin_;
663 }
664 _LIBCPP_INLINE_VISIBILITY reference back()
665 {
666 _LIBCPP_ASSERT(!empty(), "back() called for empty vector");
667 return *(this->__end_ - 1);
668 }
669 _LIBCPP_INLINE_VISIBILITY const_reference back() const
670 {
671 _LIBCPP_ASSERT(!empty(), "back() called for empty vector");
672 return *(this->__end_ - 1);
673 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000674
Howard Hinnant1c936782011-06-03 19:40:40 +0000675 _LIBCPP_INLINE_VISIBILITY
676 value_type* data() _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000677 {return _VSTD::__to_raw_pointer(this->__begin_);}
Howard Hinnant1c936782011-06-03 19:40:40 +0000678 _LIBCPP_INLINE_VISIBILITY
679 const value_type* data() const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000680 {return _VSTD::__to_raw_pointer(this->__begin_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000681
Eric Fiselier96919722017-10-17 13:03:17 +0000682#ifdef _LIBCPP_CXX03_LANG
683 _LIBCPP_INLINE_VISIBILITY
684 void __emplace_back(const value_type& __x) { push_back(__x); }
685#else
686 template <class _Arg>
687 _LIBCPP_INLINE_VISIBILITY
688 void __emplace_back(_Arg&& __arg) {
689 emplace_back(_VSTD::forward<_Arg>(__arg));
690 }
691#endif
692
Howard Hinnantcf823322010-12-17 14:46:43 +0000693 _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000694
695#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000696 _LIBCPP_INLINE_VISIBILITY void push_back(value_type&& __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000697
Howard Hinnantc51e1022010-05-11 19:42:16 +0000698 template <class... _Args>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000699 _LIBCPP_INLINE_VISIBILITY
Marshall Clowea52cc42017-01-24 23:09:12 +0000700#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +0000701 reference emplace_back(_Args&&... __args);
Marshall Clowea52cc42017-01-24 23:09:12 +0000702#else
703 void emplace_back(_Args&&... __args);
704#endif
Eric Fiseliered9e9362017-04-16 02:40:45 +0000705#endif // !_LIBCPP_CXX03_LANG
706
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000707 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000708 void pop_back();
709
710 iterator insert(const_iterator __position, const_reference __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000711
712#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000713 iterator insert(const_iterator __position, value_type&& __x);
714 template <class... _Args>
715 iterator emplace(const_iterator __position, _Args&&... __args);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000716#endif // !_LIBCPP_CXX03_LANG
717
Howard Hinnantc51e1022010-05-11 19:42:16 +0000718 iterator insert(const_iterator __position, size_type __n, const_reference __x);
719 template <class _InputIterator>
720 typename enable_if
721 <
722 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000723 !__is_forward_iterator<_InputIterator>::value &&
724 is_constructible<
725 value_type,
726 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000727 iterator
728 >::type
729 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
730 template <class _ForwardIterator>
731 typename enable_if
732 <
Howard Hinnant88010252013-03-28 17:44:32 +0000733 __is_forward_iterator<_ForwardIterator>::value &&
734 is_constructible<
735 value_type,
736 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000737 iterator
738 >::type
739 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000740
741#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000742 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000743 iterator insert(const_iterator __position, initializer_list<value_type> __il)
744 {return insert(__position, __il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +0000745#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000746
Howard Hinnantcf823322010-12-17 14:46:43 +0000747 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000748 iterator erase(const_iterator __first, const_iterator __last);
749
Howard Hinnant1c936782011-06-03 19:40:40 +0000750 _LIBCPP_INLINE_VISIBILITY
751 void clear() _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000752 {
Marshall Clow2cd9d372014-05-08 14:14:06 +0000753 size_type __old_size = size();
Howard Hinnant27e0e772011-09-14 18:33:51 +0000754 __base::clear();
Marshall Clow2cd9d372014-05-08 14:14:06 +0000755 __annotate_shrink(__old_size);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000756 __invalidate_all_iterators();
757 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000758
759 void resize(size_type __sz);
760 void resize(size_type __sz, const_reference __x);
761
Howard Hinnant1c936782011-06-03 19:40:40 +0000762 void swap(vector&)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000763#if _LIBCPP_STD_VER >= 14
Eric Fiselier69c51982016-12-28 06:06:09 +0000764 _NOEXCEPT_DEBUG;
Marshall Clow8982dcd2015-07-13 20:04:56 +0000765#else
Eric Fiselier69c51982016-12-28 06:06:09 +0000766 _NOEXCEPT_DEBUG_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +0000767 __is_nothrow_swappable<allocator_type>::value);
768#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000769
770 bool __invariants() const;
771
Howard Hinnanta47c6d52011-09-16 17:29:17 +0000772#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000773
774 bool __dereferenceable(const const_iterator* __i) const;
775 bool __decrementable(const const_iterator* __i) const;
776 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
777 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
778
Howard Hinnanta47c6d52011-09-16 17:29:17 +0000779#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000780
Howard Hinnantc51e1022010-05-11 19:42:16 +0000781private:
Howard Hinnantcf823322010-12-17 14:46:43 +0000782 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Eric Fiselier69c51982016-12-28 06:06:09 +0000783 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(pointer __new_last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000784 void allocate(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +0000785 void deallocate() _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000786 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
Howard Hinnanta5589382011-01-04 19:53:31 +0000787 void __construct_at_end(size_type __n);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000788 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000789 void __construct_at_end(size_type __n, const_reference __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000790 template <class _ForwardIterator>
791 typename enable_if
792 <
793 __is_forward_iterator<_ForwardIterator>::value,
794 void
795 >::type
Eric Fiselieracfe6f02015-03-31 16:54:19 +0000796 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000797 void __append(size_type __n);
798 void __append(size_type __n, const_reference __x);
Howard Hinnantcf823322010-12-17 14:46:43 +0000799 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000800 iterator __make_iter(pointer __p) _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000801 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000802 const_iterator __make_iter(const_pointer __p) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000803 void __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v);
804 pointer __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p);
805 void __move_range(pointer __from_s, pointer __from_e, pointer __to);
Howard Hinnant1c936782011-06-03 19:40:40 +0000806 void __move_assign(vector& __c, true_type)
807 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000808 void __move_assign(vector& __c, false_type)
809 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000810 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76053d72013-06-27 19:35:32 +0000811 void __destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000812 {
Eric Fiselier69c51982016-12-28 06:06:09 +0000813 __invalidate_iterators_past(__new_last);
Marshall Clow2cd9d372014-05-08 14:14:06 +0000814 size_type __old_size = size();
Howard Hinnant27e0e772011-09-14 18:33:51 +0000815 __base::__destruct_at_end(__new_last);
Marshall Clow2cd9d372014-05-08 14:14:06 +0000816 __annotate_shrink(__old_size);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000817 }
Eric Fiseliered9e9362017-04-16 02:40:45 +0000818
819#ifndef _LIBCPP_CXX03_LANG
820 template <class _Up> void __push_back_slow_path(_Up&& __x);
821
Howard Hinnantb6c49562012-02-26 15:30:12 +0000822 template <class... _Args>
Eric Fiseliered9e9362017-04-16 02:40:45 +0000823 void __emplace_back_slow_path(_Args&&... __args);
824#else
825 template <class _Up> void __push_back_slow_path(_Up& __x);
Howard Hinnantb6c49562012-02-26 15:30:12 +0000826#endif
Eric Fiseliered9e9362017-04-16 02:40:45 +0000827
Marshall Clow2cd9d372014-05-08 14:14:06 +0000828 // The following functions are no-ops outside of AddressSanitizer mode.
829 // We call annotatations only for the default Allocator because other allocators
830 // may not meet the AddressSanitizer alignment constraints.
831 // See the documentation for __sanitizer_annotate_contiguous_container for more details.
Marshall Clow2cd9d372014-05-08 14:14:06 +0000832#ifndef _LIBCPP_HAS_NO_ASAN
Eric Fiselier6003c772016-12-23 23:37:52 +0000833 void __annotate_contiguous_container(const void *__beg, const void *__end,
834 const void *__old_mid,
835 const void *__new_mid) const
836 {
837
Marshall Clow2cd9d372014-05-08 14:14:06 +0000838 if (__beg && is_same<allocator_type, __default_allocator_type>::value)
839 __sanitizer_annotate_contiguous_container(__beg, __end, __old_mid, __new_mid);
Marshall Clow2cd9d372014-05-08 14:14:06 +0000840 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000841#else
842 _LIBCPP_INLINE_VISIBILITY
843 void __annotate_contiguous_container(const void*, const void*, const void*,
844 const void*) const {}
845#endif
846 _LIBCPP_INLINE_VISIBILITY
847 void __annotate_new(size_type __current_size) const {
Marshall Clow2cd9d372014-05-08 14:14:06 +0000848 __annotate_contiguous_container(data(), data() + capacity(),
849 data() + capacity(), data() + __current_size);
850 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000851
852 _LIBCPP_INLINE_VISIBILITY
853 void __annotate_delete() const {
Marshall Clow2cd9d372014-05-08 14:14:06 +0000854 __annotate_contiguous_container(data(), data() + capacity(),
855 data() + size(), data() + capacity());
856 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000857
858 _LIBCPP_INLINE_VISIBILITY
Kostya Serebryany4963c252014-09-02 23:43:38 +0000859 void __annotate_increase(size_type __n) const
Marshall Clow2cd9d372014-05-08 14:14:06 +0000860 {
861 __annotate_contiguous_container(data(), data() + capacity(),
862 data() + size(), data() + size() + __n);
863 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000864
865 _LIBCPP_INLINE_VISIBILITY
Kostya Serebryany4963c252014-09-02 23:43:38 +0000866 void __annotate_shrink(size_type __old_size) const
Marshall Clow2cd9d372014-05-08 14:14:06 +0000867 {
868 __annotate_contiguous_container(data(), data() + capacity(),
869 data() + __old_size, data() + size());
870 }
Marshall Clowc78ffa52014-09-03 21:37:43 +0000871#ifndef _LIBCPP_HAS_NO_ASAN
Kostya Serebryany4963c252014-09-02 23:43:38 +0000872 // The annotation for size increase should happen before the actual increase,
873 // but if an exception is thrown after that the annotation has to be undone.
874 struct __RAII_IncreaseAnnotator {
875 __RAII_IncreaseAnnotator(const vector &__v, size_type __n = 1)
Eric Fiseliera7c043e2015-03-10 00:25:20 +0000876 : __commit(false), __v(__v), __old_size(__v.size() + __n) {
Kostya Serebryany4963c252014-09-02 23:43:38 +0000877 __v.__annotate_increase(__n);
878 }
879 void __done() { __commit = true; }
880 ~__RAII_IncreaseAnnotator() {
881 if (__commit) return;
Eric Fiseliera7c043e2015-03-10 00:25:20 +0000882 __v.__annotate_shrink(__old_size);
Kostya Serebryany4963c252014-09-02 23:43:38 +0000883 }
884 bool __commit;
Kostya Serebryany4963c252014-09-02 23:43:38 +0000885 const vector &__v;
Eric Fiseliera7c043e2015-03-10 00:25:20 +0000886 size_type __old_size;
Kostya Serebryany4963c252014-09-02 23:43:38 +0000887 };
Marshall Clowc78ffa52014-09-03 21:37:43 +0000888#else
889 struct __RAII_IncreaseAnnotator {
Eric Fiselier6003c772016-12-23 23:37:52 +0000890 _LIBCPP_INLINE_VISIBILITY
891 __RAII_IncreaseAnnotator(const vector &, size_type = 1) {}
892 _LIBCPP_INLINE_VISIBILITY void __done() {}
Marshall Clowc78ffa52014-09-03 21:37:43 +0000893 };
894#endif
895
Howard Hinnantc51e1022010-05-11 19:42:16 +0000896};
897
Marshall Clowf0ca1492018-05-21 21:30:12 +0000898#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
899template<class _InputIterator,
900 class _Alloc = typename std::allocator<typename iterator_traits<_InputIterator>::value_type>,
901 class = typename enable_if<__is_allocator<_Alloc>::value, void>::type
902 >
903vector(_InputIterator, _InputIterator)
904 -> vector<typename iterator_traits<_InputIterator>::value_type, _Alloc>;
905
906template<class _InputIterator,
907 class _Alloc,
908 class = typename enable_if<__is_allocator<_Alloc>::value, void>::type
909 >
910vector(_InputIterator, _InputIterator, _Alloc)
911 -> vector<typename iterator_traits<_InputIterator>::value_type, _Alloc>;
912#endif
913
Howard Hinnantc51e1022010-05-11 19:42:16 +0000914template <class _Tp, class _Allocator>
915void
916vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v)
917{
Marshall Clow2cd9d372014-05-08 14:14:06 +0000918 __annotate_delete();
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000919 __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, this->__end_, __v.__begin_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000920 _VSTD::swap(this->__begin_, __v.__begin_);
921 _VSTD::swap(this->__end_, __v.__end_);
922 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000923 __v.__first_ = __v.__begin_;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000924 __annotate_new(size());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000925 __invalidate_all_iterators();
926}
927
928template <class _Tp, class _Allocator>
929typename vector<_Tp, _Allocator>::pointer
930vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p)
931{
Marshall Clow2cd9d372014-05-08 14:14:06 +0000932 __annotate_delete();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000933 pointer __r = __v.__begin_;
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000934 __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, __p, __v.__begin_);
935 __alloc_traits::__construct_forward(this->__alloc(), __p, this->__end_, __v.__end_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000936 _VSTD::swap(this->__begin_, __v.__begin_);
937 _VSTD::swap(this->__end_, __v.__end_);
938 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000939 __v.__first_ = __v.__begin_;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000940 __annotate_new(size());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000941 __invalidate_all_iterators();
942 return __r;
943}
944
945// Allocate space for __n objects
946// throws length_error if __n > max_size()
947// throws (probably bad_alloc) if memory run out
948// Precondition: __begin_ == __end_ == __end_cap() == 0
949// Precondition: __n > 0
950// Postcondition: capacity() == __n
951// Postcondition: size() == 0
952template <class _Tp, class _Allocator>
953void
954vector<_Tp, _Allocator>::allocate(size_type __n)
955{
956 if (__n > max_size())
957 this->__throw_length_error();
958 this->__begin_ = this->__end_ = __alloc_traits::allocate(this->__alloc(), __n);
959 this->__end_cap() = this->__begin_ + __n;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000960 __annotate_new(0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000961}
962
963template <class _Tp, class _Allocator>
964void
Howard Hinnant1c936782011-06-03 19:40:40 +0000965vector<_Tp, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000966{
Howard Hinnant76053d72013-06-27 19:35:32 +0000967 if (this->__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000968 {
969 clear();
970 __alloc_traits::deallocate(this->__alloc(), this->__begin_, capacity());
Howard Hinnant76053d72013-06-27 19:35:32 +0000971 this->__begin_ = this->__end_ = this->__end_cap() = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000972 }
973}
974
975template <class _Tp, class _Allocator>
976typename vector<_Tp, _Allocator>::size_type
Howard Hinnant1c936782011-06-03 19:40:40 +0000977vector<_Tp, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000978{
Eric Fiselierb5d9f442016-11-23 01:18:56 +0000979 return _VSTD::min<size_type>(__alloc_traits::max_size(this->__alloc()),
980 numeric_limits<difference_type>::max());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000981}
982
983// Precondition: __new_size > capacity()
984template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000985inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000986typename vector<_Tp, _Allocator>::size_type
987vector<_Tp, _Allocator>::__recommend(size_type __new_size) const
988{
989 const size_type __ms = max_size();
990 if (__new_size > __ms)
991 this->__throw_length_error();
992 const size_type __cap = capacity();
993 if (__cap >= __ms / 2)
994 return __ms;
Alexis Hunt991d29b2011-07-29 23:31:58 +0000995 return _VSTD::max<size_type>(2*__cap, __new_size);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000996}
997
998// Default constructs __n objects starting at __end_
999// throws if construction throws
1000// Precondition: __n > 0
1001// Precondition: size() + __n <= capacity()
1002// Postcondition: size() == size() + __n
1003template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001004void
1005vector<_Tp, _Allocator>::__construct_at_end(size_type __n)
1006{
Howard Hinnantc51e1022010-05-11 19:42:16 +00001007 allocator_type& __a = this->__alloc();
1008 do
1009 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001010 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001011 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001012 ++this->__end_;
1013 --__n;
Kostya Serebryany4963c252014-09-02 23:43:38 +00001014 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001015 } while (__n > 0);
1016}
1017
Howard Hinnantc51e1022010-05-11 19:42:16 +00001018// Copy constructs __n objects starting at __end_ from __x
1019// throws if construction throws
1020// Precondition: __n > 0
1021// Precondition: size() + __n <= capacity()
1022// Postcondition: size() == old size() + __n
1023// Postcondition: [i] == __x for all i in [size() - __n, __n)
1024template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001025inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001026void
1027vector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
1028{
Howard Hinnantc51e1022010-05-11 19:42:16 +00001029 allocator_type& __a = this->__alloc();
1030 do
1031 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001032 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001033 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001034 ++this->__end_;
1035 --__n;
Kostya Serebryany4963c252014-09-02 23:43:38 +00001036 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001037 } while (__n > 0);
1038}
1039
1040template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001041template <class _ForwardIterator>
1042typename enable_if
1043<
1044 __is_forward_iterator<_ForwardIterator>::value,
1045 void
1046>::type
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001047vector<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001048{
1049 allocator_type& __a = this->__alloc();
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001050 __RAII_IncreaseAnnotator __annotator(*this, __n);
1051 __alloc_traits::__construct_range_forward(__a, __first, __last, this->__end_);
1052 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001053}
1054
1055// Default constructs __n objects starting at __end_
1056// throws if construction throws
1057// Postcondition: size() == size() + __n
Howard Hinnant1c936782011-06-03 19:40:40 +00001058// Exception safety: strong.
Howard Hinnantc51e1022010-05-11 19:42:16 +00001059template <class _Tp, class _Allocator>
1060void
1061vector<_Tp, _Allocator>::__append(size_type __n)
1062{
1063 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1064 this->__construct_at_end(__n);
1065 else
1066 {
1067 allocator_type& __a = this->__alloc();
1068 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1069 __v.__construct_at_end(__n);
1070 __swap_out_circular_buffer(__v);
1071 }
1072}
1073
1074// Default constructs __n objects starting at __end_
1075// throws if construction throws
1076// Postcondition: size() == size() + __n
Howard Hinnant1c936782011-06-03 19:40:40 +00001077// Exception safety: strong.
Howard Hinnantc51e1022010-05-11 19:42:16 +00001078template <class _Tp, class _Allocator>
1079void
1080vector<_Tp, _Allocator>::__append(size_type __n, const_reference __x)
1081{
1082 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1083 this->__construct_at_end(__n, __x);
1084 else
1085 {
1086 allocator_type& __a = this->__alloc();
1087 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1088 __v.__construct_at_end(__n, __x);
1089 __swap_out_circular_buffer(__v);
1090 }
1091}
1092
1093template <class _Tp, class _Allocator>
1094vector<_Tp, _Allocator>::vector(size_type __n)
1095{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001096#if _LIBCPP_DEBUG_LEVEL >= 2
1097 __get_db()->__insert_c(this);
1098#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001099 if (__n > 0)
1100 {
1101 allocate(__n);
1102 __construct_at_end(__n);
1103 }
1104}
1105
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00001106#if _LIBCPP_STD_VER > 11
1107template <class _Tp, class _Allocator>
1108vector<_Tp, _Allocator>::vector(size_type __n, const allocator_type& __a)
1109 : __base(__a)
1110{
1111#if _LIBCPP_DEBUG_LEVEL >= 2
1112 __get_db()->__insert_c(this);
1113#endif
1114 if (__n > 0)
1115 {
1116 allocate(__n);
1117 __construct_at_end(__n);
1118 }
1119}
1120#endif
1121
Howard Hinnantc51e1022010-05-11 19:42:16 +00001122template <class _Tp, class _Allocator>
Marshall Clowf0ca1492018-05-21 21:30:12 +00001123vector<_Tp, _Allocator>::vector(size_type __n, const value_type& __x)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001124{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001125#if _LIBCPP_DEBUG_LEVEL >= 2
1126 __get_db()->__insert_c(this);
1127#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001128 if (__n > 0)
1129 {
1130 allocate(__n);
1131 __construct_at_end(__n, __x);
1132 }
1133}
1134
1135template <class _Tp, class _Allocator>
Marshall Clowf0ca1492018-05-21 21:30:12 +00001136vector<_Tp, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001137 : __base(__a)
1138{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001139#if _LIBCPP_DEBUG_LEVEL >= 2
1140 __get_db()->__insert_c(this);
1141#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001142 if (__n > 0)
1143 {
1144 allocate(__n);
1145 __construct_at_end(__n, __x);
1146 }
1147}
1148
1149template <class _Tp, class _Allocator>
1150template <class _InputIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +00001151vector<_Tp, _Allocator>::vector(_InputIterator __first,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001152 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001153 !__is_forward_iterator<_InputIterator>::value &&
1154 is_constructible<
1155 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +00001156 typename iterator_traits<_InputIterator>::reference>::value,
1157 _InputIterator>::type __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001158{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001159#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001160 __get_db()->__insert_c(this);
1161#endif
Howard Hinnant9cd22302011-09-16 18:41:29 +00001162 for (; __first != __last; ++__first)
Eric Fiselier96919722017-10-17 13:03:17 +00001163 __emplace_back(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001164}
1165
1166template <class _Tp, class _Allocator>
1167template <class _InputIterator>
1168vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
1169 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001170 !__is_forward_iterator<_InputIterator>::value &&
1171 is_constructible<
1172 value_type,
1173 typename iterator_traits<_InputIterator>::reference>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001174 : __base(__a)
1175{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001176#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001177 __get_db()->__insert_c(this);
1178#endif
Howard Hinnant9cd22302011-09-16 18:41:29 +00001179 for (; __first != __last; ++__first)
Eric Fiselier96919722017-10-17 13:03:17 +00001180 __emplace_back(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001181}
1182
1183template <class _Tp, class _Allocator>
1184template <class _ForwardIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +00001185vector<_Tp, _Allocator>::vector(_ForwardIterator __first,
Howard Hinnant88010252013-03-28 17:44:32 +00001186 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
1187 is_constructible<
1188 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +00001189 typename iterator_traits<_ForwardIterator>::reference>::value,
1190 _ForwardIterator>::type __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001191{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001192#if _LIBCPP_DEBUG_LEVEL >= 2
1193 __get_db()->__insert_c(this);
1194#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001195 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001196 if (__n > 0)
1197 {
1198 allocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001199 __construct_at_end(__first, __last, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001200 }
1201}
1202
1203template <class _Tp, class _Allocator>
1204template <class _ForwardIterator>
1205vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Howard Hinnant88010252013-03-28 17:44:32 +00001206 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
1207 is_constructible<
1208 value_type,
1209 typename iterator_traits<_ForwardIterator>::reference>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001210 : __base(__a)
1211{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001212#if _LIBCPP_DEBUG_LEVEL >= 2
1213 __get_db()->__insert_c(this);
1214#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001215 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001216 if (__n > 0)
1217 {
1218 allocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001219 __construct_at_end(__first, __last, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001220 }
1221}
1222
1223template <class _Tp, class _Allocator>
1224vector<_Tp, _Allocator>::vector(const vector& __x)
1225 : __base(__alloc_traits::select_on_container_copy_construction(__x.__alloc()))
1226{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001227#if _LIBCPP_DEBUG_LEVEL >= 2
1228 __get_db()->__insert_c(this);
1229#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001230 size_type __n = __x.size();
1231 if (__n > 0)
1232 {
1233 allocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001234 __construct_at_end(__x.__begin_, __x.__end_, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001235 }
1236}
1237
1238template <class _Tp, class _Allocator>
1239vector<_Tp, _Allocator>::vector(const vector& __x, const allocator_type& __a)
1240 : __base(__a)
1241{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001242#if _LIBCPP_DEBUG_LEVEL >= 2
1243 __get_db()->__insert_c(this);
1244#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001245 size_type __n = __x.size();
1246 if (__n > 0)
1247 {
1248 allocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001249 __construct_at_end(__x.__begin_, __x.__end_, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001250 }
1251}
1252
Eric Fiseliered9e9362017-04-16 02:40:45 +00001253#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001254
1255template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001256inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001257vector<_Tp, _Allocator>::vector(vector&& __x)
Marshall Clowe5108202015-07-14 14:46:32 +00001258#if _LIBCPP_STD_VER > 14
1259 _NOEXCEPT
1260#else
Howard Hinnant1c936782011-06-03 19:40:40 +00001261 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowe5108202015-07-14 14:46:32 +00001262#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001263 : __base(_VSTD::move(__x.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001264{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001265#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001266 __get_db()->__insert_c(this);
Howard Hinnanta1e60cd2011-09-19 16:34:29 +00001267 __get_db()->swap(this, &__x);
Howard Hinnant27e0e772011-09-14 18:33:51 +00001268#endif
Howard Hinnant9cd22302011-09-16 18:41:29 +00001269 this->__begin_ = __x.__begin_;
1270 this->__end_ = __x.__end_;
1271 this->__end_cap() = __x.__end_cap();
Howard Hinnant76053d72013-06-27 19:35:32 +00001272 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001273}
1274
1275template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001276inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001277vector<_Tp, _Allocator>::vector(vector&& __x, const allocator_type& __a)
1278 : __base(__a)
1279{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001280#if _LIBCPP_DEBUG_LEVEL >= 2
1281 __get_db()->__insert_c(this);
1282#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001283 if (__a == __x.__alloc())
1284 {
1285 this->__begin_ = __x.__begin_;
1286 this->__end_ = __x.__end_;
1287 this->__end_cap() = __x.__end_cap();
1288 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnanta1e60cd2011-09-19 16:34:29 +00001289#if _LIBCPP_DEBUG_LEVEL >= 2
1290 __get_db()->swap(this, &__x);
1291#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001292 }
1293 else
1294 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001295 typedef move_iterator<iterator> _Ip;
1296 assign(_Ip(__x.begin()), _Ip(__x.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001297 }
1298}
1299
1300template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001301inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001302vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il)
1303{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001304#if _LIBCPP_DEBUG_LEVEL >= 2
1305 __get_db()->__insert_c(this);
1306#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001307 if (__il.size() > 0)
1308 {
1309 allocate(__il.size());
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001310 __construct_at_end(__il.begin(), __il.end(), __il.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001311 }
1312}
1313
1314template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001315inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001316vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
1317 : __base(__a)
1318{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001319#if _LIBCPP_DEBUG_LEVEL >= 2
1320 __get_db()->__insert_c(this);
1321#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001322 if (__il.size() > 0)
1323 {
1324 allocate(__il.size());
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001325 __construct_at_end(__il.begin(), __il.end(), __il.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001326 }
1327}
1328
1329template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001330inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001331vector<_Tp, _Allocator>&
1332vector<_Tp, _Allocator>::operator=(vector&& __x)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001333 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001334{
1335 __move_assign(__x, integral_constant<bool,
1336 __alloc_traits::propagate_on_container_move_assignment::value>());
1337 return *this;
1338}
1339
1340template <class _Tp, class _Allocator>
1341void
1342vector<_Tp, _Allocator>::__move_assign(vector& __c, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001343 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001344{
1345 if (__base::__alloc() != __c.__alloc())
1346 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001347 typedef move_iterator<iterator> _Ip;
1348 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001349 }
1350 else
1351 __move_assign(__c, true_type());
1352}
1353
1354template <class _Tp, class _Allocator>
1355void
1356vector<_Tp, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00001357 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001358{
1359 deallocate();
Marshall Clow136d45c2014-07-21 15:11:13 +00001360 __base::__move_assign_alloc(__c); // this can throw
Howard Hinnantc51e1022010-05-11 19:42:16 +00001361 this->__begin_ = __c.__begin_;
1362 this->__end_ = __c.__end_;
1363 this->__end_cap() = __c.__end_cap();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001364 __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
Howard Hinnanta1e60cd2011-09-19 16:34:29 +00001365#if _LIBCPP_DEBUG_LEVEL >= 2
1366 __get_db()->swap(this, &__c);
1367#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001368}
1369
Eric Fiseliered9e9362017-04-16 02:40:45 +00001370#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001371
1372template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001373inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001374vector<_Tp, _Allocator>&
1375vector<_Tp, _Allocator>::operator=(const vector& __x)
1376{
1377 if (this != &__x)
1378 {
1379 __base::__copy_assign_alloc(__x);
1380 assign(__x.__begin_, __x.__end_);
1381 }
1382 return *this;
1383}
1384
1385template <class _Tp, class _Allocator>
1386template <class _InputIterator>
1387typename enable_if
1388<
1389 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001390 !__is_forward_iterator<_InputIterator>::value &&
1391 is_constructible<
1392 _Tp,
1393 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001394 void
1395>::type
1396vector<_Tp, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
1397{
1398 clear();
1399 for (; __first != __last; ++__first)
Eric Fiselier96919722017-10-17 13:03:17 +00001400 __emplace_back(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001401}
1402
1403template <class _Tp, class _Allocator>
1404template <class _ForwardIterator>
1405typename enable_if
1406<
Howard Hinnant88010252013-03-28 17:44:32 +00001407 __is_forward_iterator<_ForwardIterator>::value &&
1408 is_constructible<
1409 _Tp,
1410 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001411 void
1412>::type
1413vector<_Tp, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
1414{
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001415 size_type __new_size = static_cast<size_type>(_VSTD::distance(__first, __last));
1416 if (__new_size <= capacity())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001417 {
1418 _ForwardIterator __mid = __last;
1419 bool __growing = false;
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001420 if (__new_size > size())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001421 {
1422 __growing = true;
1423 __mid = __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001424 _VSTD::advance(__mid, size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001425 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001426 pointer __m = _VSTD::copy(__first, __mid, this->__begin_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001427 if (__growing)
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001428 __construct_at_end(__mid, __last, __new_size - size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001429 else
1430 this->__destruct_at_end(__m);
1431 }
1432 else
1433 {
1434 deallocate();
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001435 allocate(__recommend(__new_size));
1436 __construct_at_end(__first, __last, __new_size);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001437 }
Eric Fiselier69c51982016-12-28 06:06:09 +00001438 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001439}
1440
1441template <class _Tp, class _Allocator>
1442void
1443vector<_Tp, _Allocator>::assign(size_type __n, const_reference __u)
1444{
1445 if (__n <= capacity())
1446 {
1447 size_type __s = size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001448 _VSTD::fill_n(this->__begin_, _VSTD::min(__n, __s), __u);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001449 if (__n > __s)
1450 __construct_at_end(__n - __s, __u);
1451 else
Howard Hinnant155c2af2010-05-24 17:49:41 +00001452 this->__destruct_at_end(this->__begin_ + __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001453 }
1454 else
1455 {
1456 deallocate();
1457 allocate(__recommend(static_cast<size_type>(__n)));
1458 __construct_at_end(__n, __u);
1459 }
Eric Fiselier69c51982016-12-28 06:06:09 +00001460 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001461}
1462
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001463template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001464inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001465typename vector<_Tp, _Allocator>::iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001466vector<_Tp, _Allocator>::__make_iter(pointer __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001467{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001468#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001469 return iterator(this, __p);
1470#else
1471 return iterator(__p);
1472#endif
1473}
1474
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001475template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001476inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001477typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001478vector<_Tp, _Allocator>::__make_iter(const_pointer __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001479{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001480#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001481 return const_iterator(this, __p);
1482#else
1483 return const_iterator(__p);
1484#endif
1485}
1486
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001487template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001488inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001489typename vector<_Tp, _Allocator>::iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001490vector<_Tp, _Allocator>::begin() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001491{
1492 return __make_iter(this->__begin_);
1493}
1494
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001495template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001496inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001497typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001498vector<_Tp, _Allocator>::begin() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001499{
1500 return __make_iter(this->__begin_);
1501}
1502
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001503template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001504inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001505typename vector<_Tp, _Allocator>::iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001506vector<_Tp, _Allocator>::end() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001507{
1508 return __make_iter(this->__end_);
1509}
1510
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001511template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001512inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001513typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001514vector<_Tp, _Allocator>::end() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001515{
1516 return __make_iter(this->__end_);
1517}
1518
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001519template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001520inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001521typename vector<_Tp, _Allocator>::reference
1522vector<_Tp, _Allocator>::operator[](size_type __n)
1523{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001524 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001525 return this->__begin_[__n];
1526}
1527
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001528template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001529inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001530typename vector<_Tp, _Allocator>::const_reference
1531vector<_Tp, _Allocator>::operator[](size_type __n) const
1532{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001533 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001534 return this->__begin_[__n];
1535}
1536
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001537template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001538typename vector<_Tp, _Allocator>::reference
1539vector<_Tp, _Allocator>::at(size_type __n)
1540{
1541 if (__n >= size())
1542 this->__throw_out_of_range();
1543 return this->__begin_[__n];
1544}
1545
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001546template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001547typename vector<_Tp, _Allocator>::const_reference
1548vector<_Tp, _Allocator>::at(size_type __n) const
1549{
1550 if (__n >= size())
1551 this->__throw_out_of_range();
1552 return this->__begin_[__n];
1553}
1554
1555template <class _Tp, class _Allocator>
1556void
1557vector<_Tp, _Allocator>::reserve(size_type __n)
1558{
1559 if (__n > capacity())
1560 {
1561 allocator_type& __a = this->__alloc();
Howard Hinnant1c936782011-06-03 19:40:40 +00001562 __split_buffer<value_type, allocator_type&> __v(__n, size(), __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001563 __swap_out_circular_buffer(__v);
1564 }
1565}
1566
1567template <class _Tp, class _Allocator>
1568void
Howard Hinnant1c936782011-06-03 19:40:40 +00001569vector<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001570{
1571 if (capacity() > size())
1572 {
1573#ifndef _LIBCPP_NO_EXCEPTIONS
1574 try
1575 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001576#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001577 allocator_type& __a = this->__alloc();
Howard Hinnant1c936782011-06-03 19:40:40 +00001578 __split_buffer<value_type, allocator_type&> __v(size(), size(), __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001579 __swap_out_circular_buffer(__v);
1580#ifndef _LIBCPP_NO_EXCEPTIONS
1581 }
1582 catch (...)
1583 {
1584 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001585#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001586 }
1587}
1588
1589template <class _Tp, class _Allocator>
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001590template <class _Up>
1591void
Eric Fiseliered9e9362017-04-16 02:40:45 +00001592#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001593vector<_Tp, _Allocator>::__push_back_slow_path(_Up&& __x)
1594#else
1595vector<_Tp, _Allocator>::__push_back_slow_path(_Up& __x)
1596#endif
1597{
1598 allocator_type& __a = this->__alloc();
1599 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1600 // __v.push_back(_VSTD::forward<_Up>(__x));
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001601 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), _VSTD::forward<_Up>(__x));
1602 __v.__end_++;
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001603 __swap_out_circular_buffer(__v);
1604}
1605
1606template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001607inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001608void
1609vector<_Tp, _Allocator>::push_back(const_reference __x)
1610{
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001611 if (this->__end_ != this->__end_cap())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001612 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001613 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001614 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001615 _VSTD::__to_raw_pointer(this->__end_), __x);
Kostya Serebryany4963c252014-09-02 23:43:38 +00001616 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001617 ++this->__end_;
1618 }
1619 else
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001620 __push_back_slow_path(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001621}
1622
Eric Fiseliered9e9362017-04-16 02:40:45 +00001623#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001624
1625template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001626inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001627void
1628vector<_Tp, _Allocator>::push_back(value_type&& __x)
1629{
1630 if (this->__end_ < this->__end_cap())
1631 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001632 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001633 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001634 _VSTD::__to_raw_pointer(this->__end_),
1635 _VSTD::move(__x));
Kostya Serebryany4963c252014-09-02 23:43:38 +00001636 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001637 ++this->__end_;
1638 }
1639 else
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001640 __push_back_slow_path(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001641}
1642
1643template <class _Tp, class _Allocator>
1644template <class... _Args>
1645void
Howard Hinnantb6c49562012-02-26 15:30:12 +00001646vector<_Tp, _Allocator>::__emplace_back_slow_path(_Args&&... __args)
1647{
1648 allocator_type& __a = this->__alloc();
1649 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1650// __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001651 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), _VSTD::forward<_Args>(__args)...);
1652 __v.__end_++;
Howard Hinnantb6c49562012-02-26 15:30:12 +00001653 __swap_out_circular_buffer(__v);
1654}
1655
1656template <class _Tp, class _Allocator>
1657template <class... _Args>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001658inline
Marshall Clowea52cc42017-01-24 23:09:12 +00001659#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001660typename vector<_Tp, _Allocator>::reference
Marshall Clowea52cc42017-01-24 23:09:12 +00001661#else
1662void
1663#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001664vector<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1665{
1666 if (this->__end_ < this->__end_cap())
1667 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001668 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001669 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001670 _VSTD::__to_raw_pointer(this->__end_),
1671 _VSTD::forward<_Args>(__args)...);
Kostya Serebryany4963c252014-09-02 23:43:38 +00001672 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001673 ++this->__end_;
1674 }
1675 else
Howard Hinnantb6c49562012-02-26 15:30:12 +00001676 __emplace_back_slow_path(_VSTD::forward<_Args>(__args)...);
Marshall Clowea52cc42017-01-24 23:09:12 +00001677#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001678 return this->back();
Marshall Clowea52cc42017-01-24 23:09:12 +00001679#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001680}
1681
Eric Fiseliered9e9362017-04-16 02:40:45 +00001682#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001683
1684template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001685inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001686void
1687vector<_Tp, _Allocator>::pop_back()
1688{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001689 _LIBCPP_ASSERT(!empty(), "vector::pop_back called for empty vector");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001690 this->__destruct_at_end(this->__end_ - 1);
1691}
1692
1693template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001694inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001695typename vector<_Tp, _Allocator>::iterator
1696vector<_Tp, _Allocator>::erase(const_iterator __position)
1697{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001698#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001699 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1700 "vector::erase(iterator) called with an iterator not"
1701 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001702#endif
Howard Hinnant58d52d52013-03-25 22:12:26 +00001703 _LIBCPP_ASSERT(__position != end(),
1704 "vector::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnant76053d72013-06-27 19:35:32 +00001705 difference_type __ps = __position - cbegin();
1706 pointer __p = this->__begin_ + __ps;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001707 this->__destruct_at_end(_VSTD::move(__p + 1, this->__end_, __p));
Eric Fiselier69c51982016-12-28 06:06:09 +00001708 this->__invalidate_iterators_past(__p-1);
1709 iterator __r = __make_iter(__p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001710 return __r;
1711}
1712
1713template <class _Tp, class _Allocator>
1714typename vector<_Tp, _Allocator>::iterator
1715vector<_Tp, _Allocator>::erase(const_iterator __first, const_iterator __last)
1716{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001717#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001718 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
1719 "vector::erase(iterator, iterator) called with an iterator not"
1720 " referring to this vector");
Eric Fiselier69c51982016-12-28 06:06:09 +00001721 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__last) == this,
1722 "vector::erase(iterator, iterator) called with an iterator not"
1723 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001724#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001725 _LIBCPP_ASSERT(__first <= __last, "vector::erase(first, last) called with invalid range");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001726 pointer __p = this->__begin_ + (__first - begin());
Eric Fiselier69c51982016-12-28 06:06:09 +00001727 if (__first != __last) {
Howard Hinnantc580cc32013-04-18 15:02:57 +00001728 this->__destruct_at_end(_VSTD::move(__p + (__last - __first), this->__end_, __p));
Eric Fiselier69c51982016-12-28 06:06:09 +00001729 this->__invalidate_iterators_past(__p - 1);
1730 }
1731 iterator __r = __make_iter(__p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001732 return __r;
1733}
1734
1735template <class _Tp, class _Allocator>
1736void
1737vector<_Tp, _Allocator>::__move_range(pointer __from_s, pointer __from_e, pointer __to)
1738{
1739 pointer __old_last = this->__end_;
1740 difference_type __n = __old_last - __to;
1741 for (pointer __i = __from_s + __n; __i < __from_e; ++__i, ++this->__end_)
1742 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001743 _VSTD::__to_raw_pointer(this->__end_),
1744 _VSTD::move(*__i));
1745 _VSTD::move_backward(__from_s, __from_s + __n, __old_last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001746}
1747
1748template <class _Tp, class _Allocator>
1749typename vector<_Tp, _Allocator>::iterator
1750vector<_Tp, _Allocator>::insert(const_iterator __position, const_reference __x)
1751{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001752#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001753 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1754 "vector::insert(iterator, x) called with an iterator not"
1755 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001756#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001757 pointer __p = this->__begin_ + (__position - begin());
1758 if (this->__end_ < this->__end_cap())
1759 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001760 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001761 if (__p == this->__end_)
1762 {
1763 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001764 _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001765 ++this->__end_;
1766 }
1767 else
1768 {
1769 __move_range(__p, this->__end_, __p + 1);
1770 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1771 if (__p <= __xr && __xr < this->__end_)
1772 ++__xr;
1773 *__p = *__xr;
1774 }
Kostya Serebryany4963c252014-09-02 23:43:38 +00001775 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001776 }
1777 else
1778 {
1779 allocator_type& __a = this->__alloc();
1780 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
1781 __v.push_back(__x);
1782 __p = __swap_out_circular_buffer(__v, __p);
1783 }
1784 return __make_iter(__p);
1785}
1786
Eric Fiseliered9e9362017-04-16 02:40:45 +00001787#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001788
1789template <class _Tp, class _Allocator>
1790typename vector<_Tp, _Allocator>::iterator
1791vector<_Tp, _Allocator>::insert(const_iterator __position, value_type&& __x)
1792{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001793#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001794 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1795 "vector::insert(iterator, x) called with an iterator not"
1796 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001797#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001798 pointer __p = this->__begin_ + (__position - begin());
1799 if (this->__end_ < this->__end_cap())
1800 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001801 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001802 if (__p == this->__end_)
1803 {
1804 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001805 _VSTD::__to_raw_pointer(this->__end_),
1806 _VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001807 ++this->__end_;
1808 }
1809 else
1810 {
1811 __move_range(__p, this->__end_, __p + 1);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001812 *__p = _VSTD::move(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001813 }
Kostya Serebryany4963c252014-09-02 23:43:38 +00001814 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001815 }
1816 else
1817 {
1818 allocator_type& __a = this->__alloc();
1819 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001820 __v.push_back(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001821 __p = __swap_out_circular_buffer(__v, __p);
1822 }
1823 return __make_iter(__p);
1824}
1825
1826template <class _Tp, class _Allocator>
1827template <class... _Args>
1828typename vector<_Tp, _Allocator>::iterator
1829vector<_Tp, _Allocator>::emplace(const_iterator __position, _Args&&... __args)
1830{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001831#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001832 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1833 "vector::emplace(iterator, x) called with an iterator not"
1834 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001835#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001836 pointer __p = this->__begin_ + (__position - begin());
1837 if (this->__end_ < this->__end_cap())
1838 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001839 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001840 if (__p == this->__end_)
1841 {
1842 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001843 _VSTD::__to_raw_pointer(this->__end_),
1844 _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001845 ++this->__end_;
1846 }
1847 else
1848 {
Marshall Clowa591b9a2016-07-11 21:38:08 +00001849 __temp_value<value_type, _Allocator> __tmp(this->__alloc(), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001850 __move_range(__p, this->__end_, __p + 1);
Marshall Clowa591b9a2016-07-11 21:38:08 +00001851 *__p = _VSTD::move(__tmp.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001852 }
Kostya Serebryany4963c252014-09-02 23:43:38 +00001853 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001854 }
1855 else
1856 {
1857 allocator_type& __a = this->__alloc();
1858 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001859 __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001860 __p = __swap_out_circular_buffer(__v, __p);
1861 }
1862 return __make_iter(__p);
1863}
1864
Eric Fiseliered9e9362017-04-16 02:40:45 +00001865#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001866
1867template <class _Tp, class _Allocator>
1868typename vector<_Tp, _Allocator>::iterator
1869vector<_Tp, _Allocator>::insert(const_iterator __position, size_type __n, const_reference __x)
1870{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001871#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001872 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1873 "vector::insert(iterator, n, x) called with an iterator not"
1874 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001875#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001876 pointer __p = this->__begin_ + (__position - begin());
1877 if (__n > 0)
1878 {
1879 if (__n <= static_cast<size_type>(this->__end_cap() - this->__end_))
1880 {
1881 size_type __old_n = __n;
1882 pointer __old_last = this->__end_;
1883 if (__n > static_cast<size_type>(this->__end_ - __p))
1884 {
1885 size_type __cx = __n - (this->__end_ - __p);
1886 __construct_at_end(__cx, __x);
1887 __n -= __cx;
1888 }
1889 if (__n > 0)
1890 {
Eric Fiselier2a649652014-11-14 18:28:36 +00001891 __RAII_IncreaseAnnotator __annotator(*this, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001892 __move_range(__p, __old_last, __p + __old_n);
Kostya Serebryany4963c252014-09-02 23:43:38 +00001893 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001894 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1895 if (__p <= __xr && __xr < this->__end_)
1896 __xr += __old_n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001897 _VSTD::fill_n(__p, __n, *__xr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001898 }
1899 }
1900 else
1901 {
1902 allocator_type& __a = this->__alloc();
1903 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1904 __v.__construct_at_end(__n, __x);
1905 __p = __swap_out_circular_buffer(__v, __p);
1906 }
1907 }
1908 return __make_iter(__p);
1909}
1910
1911template <class _Tp, class _Allocator>
1912template <class _InputIterator>
1913typename enable_if
1914<
1915 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001916 !__is_forward_iterator<_InputIterator>::value &&
1917 is_constructible<
1918 _Tp,
1919 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001920 typename vector<_Tp, _Allocator>::iterator
1921>::type
1922vector<_Tp, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
1923{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001924#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001925 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1926 "vector::insert(iterator, range) called with an iterator not"
1927 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001928#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001929 difference_type __off = __position - begin();
1930 pointer __p = this->__begin_ + __off;
1931 allocator_type& __a = this->__alloc();
1932 pointer __old_last = this->__end_;
1933 for (; this->__end_ != this->__end_cap() && __first != __last; ++__first)
1934 {
Eric Fiselier489fd502015-07-18 18:22:12 +00001935 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001936 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001937 *__first);
1938 ++this->__end_;
Eric Fiselier489fd502015-07-18 18:22:12 +00001939 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001940 }
1941 __split_buffer<value_type, allocator_type&> __v(__a);
1942 if (__first != __last)
1943 {
1944#ifndef _LIBCPP_NO_EXCEPTIONS
1945 try
1946 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001947#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001948 __v.__construct_at_end(__first, __last);
1949 difference_type __old_size = __old_last - this->__begin_;
1950 difference_type __old_p = __p - this->__begin_;
1951 reserve(__recommend(size() + __v.size()));
1952 __p = this->__begin_ + __old_p;
1953 __old_last = this->__begin_ + __old_size;
1954#ifndef _LIBCPP_NO_EXCEPTIONS
1955 }
1956 catch (...)
1957 {
1958 erase(__make_iter(__old_last), end());
1959 throw;
1960 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001961#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001962 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001963 __p = _VSTD::rotate(__p, __old_last, this->__end_);
Howard Hinnant9cd22302011-09-16 18:41:29 +00001964 insert(__make_iter(__p), make_move_iterator(__v.begin()),
1965 make_move_iterator(__v.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001966 return begin() + __off;
1967}
1968
1969template <class _Tp, class _Allocator>
1970template <class _ForwardIterator>
1971typename enable_if
1972<
Howard Hinnant88010252013-03-28 17:44:32 +00001973 __is_forward_iterator<_ForwardIterator>::value &&
1974 is_constructible<
1975 _Tp,
1976 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001977 typename vector<_Tp, _Allocator>::iterator
1978>::type
1979vector<_Tp, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
1980{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001981#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001982 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1983 "vector::insert(iterator, range) called with an iterator not"
1984 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001985#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001986 pointer __p = this->__begin_ + (__position - begin());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001987 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001988 if (__n > 0)
1989 {
1990 if (__n <= this->__end_cap() - this->__end_)
1991 {
1992 size_type __old_n = __n;
1993 pointer __old_last = this->__end_;
1994 _ForwardIterator __m = __last;
1995 difference_type __dx = this->__end_ - __p;
1996 if (__n > __dx)
1997 {
1998 __m = __first;
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001999 difference_type __diff = this->__end_ - __p;
2000 _VSTD::advance(__m, __diff);
2001 __construct_at_end(__m, __last, __n - __diff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002002 __n = __dx;
2003 }
2004 if (__n > 0)
2005 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00002006 __RAII_IncreaseAnnotator __annotator(*this, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002007 __move_range(__p, __old_last, __p + __old_n);
Kostya Serebryany4963c252014-09-02 23:43:38 +00002008 __annotator.__done();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002009 _VSTD::copy(__first, __m, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002010 }
2011 }
2012 else
2013 {
2014 allocator_type& __a = this->__alloc();
2015 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
2016 __v.__construct_at_end(__first, __last);
2017 __p = __swap_out_circular_buffer(__v, __p);
2018 }
2019 }
2020 return __make_iter(__p);
2021}
2022
2023template <class _Tp, class _Allocator>
2024void
2025vector<_Tp, _Allocator>::resize(size_type __sz)
2026{
2027 size_type __cs = size();
2028 if (__cs < __sz)
2029 this->__append(__sz - __cs);
2030 else if (__cs > __sz)
2031 this->__destruct_at_end(this->__begin_ + __sz);
2032}
2033
2034template <class _Tp, class _Allocator>
2035void
2036vector<_Tp, _Allocator>::resize(size_type __sz, const_reference __x)
2037{
2038 size_type __cs = size();
2039 if (__cs < __sz)
2040 this->__append(__sz - __cs, __x);
2041 else if (__cs > __sz)
2042 this->__destruct_at_end(this->__begin_ + __sz);
2043}
2044
2045template <class _Tp, class _Allocator>
2046void
2047vector<_Tp, _Allocator>::swap(vector& __x)
Marshall Clow8982dcd2015-07-13 20:04:56 +00002048#if _LIBCPP_STD_VER >= 14
Eric Fiselier69c51982016-12-28 06:06:09 +00002049 _NOEXCEPT_DEBUG
Marshall Clow8982dcd2015-07-13 20:04:56 +00002050#else
Eric Fiselier69c51982016-12-28 06:06:09 +00002051 _NOEXCEPT_DEBUG_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00002052 __is_nothrow_swappable<allocator_type>::value)
2053#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002054{
Howard Hinnant27e0e772011-09-14 18:33:51 +00002055 _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
2056 this->__alloc() == __x.__alloc(),
2057 "vector::swap: Either propagate_on_container_swap must be true"
2058 " or the allocators must compare equal");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002059 _VSTD::swap(this->__begin_, __x.__begin_);
2060 _VSTD::swap(this->__end_, __x.__end_);
2061 _VSTD::swap(this->__end_cap(), __x.__end_cap());
Eric Fiseliered9e9362017-04-16 02:40:45 +00002062 __swap_allocator(this->__alloc(), __x.__alloc(),
Marshall Clow8982dcd2015-07-13 20:04:56 +00002063 integral_constant<bool,__alloc_traits::propagate_on_container_swap::value>());
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002064#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002065 __get_db()->swap(this, &__x);
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002066#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00002067}
2068
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002069template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002070bool
2071vector<_Tp, _Allocator>::__invariants() const
2072{
Howard Hinnant76053d72013-06-27 19:35:32 +00002073 if (this->__begin_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002074 {
Howard Hinnant76053d72013-06-27 19:35:32 +00002075 if (this->__end_ != nullptr || this->__end_cap() != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002076 return false;
2077 }
2078 else
2079 {
2080 if (this->__begin_ > this->__end_)
2081 return false;
2082 if (this->__begin_ == this->__end_cap())
2083 return false;
2084 if (this->__end_ > this->__end_cap())
2085 return false;
2086 }
2087 return true;
2088}
2089
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002090#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002091
Howard Hinnantc51e1022010-05-11 19:42:16 +00002092template <class _Tp, class _Allocator>
Howard Hinnant27e0e772011-09-14 18:33:51 +00002093bool
2094vector<_Tp, _Allocator>::__dereferenceable(const const_iterator* __i) const
2095{
2096 return this->__begin_ <= __i->base() && __i->base() < this->__end_;
2097}
2098
2099template <class _Tp, class _Allocator>
2100bool
2101vector<_Tp, _Allocator>::__decrementable(const const_iterator* __i) const
2102{
2103 return this->__begin_ < __i->base() && __i->base() <= this->__end_;
2104}
2105
2106template <class _Tp, class _Allocator>
2107bool
2108vector<_Tp, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
2109{
2110 const_pointer __p = __i->base() + __n;
2111 return this->__begin_ <= __p && __p <= this->__end_;
2112}
2113
2114template <class _Tp, class _Allocator>
2115bool
2116vector<_Tp, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
2117{
2118 const_pointer __p = __i->base() + __n;
2119 return this->__begin_ <= __p && __p < this->__end_;
2120}
2121
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002122#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002123
2124template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002125inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002126void
2127vector<_Tp, _Allocator>::__invalidate_all_iterators()
2128{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002129#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002130 __get_db()->__invalidate_all(this);
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002131#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00002132}
2133
Eric Fiselier69c51982016-12-28 06:06:09 +00002134
2135template <class _Tp, class _Allocator>
2136inline _LIBCPP_INLINE_VISIBILITY
2137void
2138vector<_Tp, _Allocator>::__invalidate_iterators_past(pointer __new_last) {
2139#if _LIBCPP_DEBUG_LEVEL >= 2
2140 __c_node* __c = __get_db()->__find_c_and_lock(this);
2141 for (__i_node** __p = __c->end_; __p != __c->beg_; ) {
2142 --__p;
2143 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
2144 if (__i->base() > __new_last) {
2145 (*__p)->__c_ = nullptr;
2146 if (--__c->end_ != __p)
2147 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
2148 }
2149 }
2150 __get_db()->unlock();
2151#else
2152 ((void)__new_last);
2153#endif
2154}
2155
Howard Hinnantc51e1022010-05-11 19:42:16 +00002156// vector<bool>
2157
2158template <class _Allocator> class vector<bool, _Allocator>;
2159
2160template <class _Allocator> struct hash<vector<bool, _Allocator> >;
2161
2162template <class _Allocator>
Howard Hinnantb4ef6482011-07-02 20:33:23 +00002163struct __has_storage_type<vector<bool, _Allocator> >
2164{
2165 static const bool value = true;
2166};
2167
2168template <class _Allocator>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002169class _LIBCPP_TEMPLATE_VIS vector<bool, _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002170 : private __vector_base_common<true>
2171{
2172public:
2173 typedef vector __self;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002174 typedef bool value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002175 typedef _Allocator allocator_type;
2176 typedef allocator_traits<allocator_type> __alloc_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002177 typedef typename __alloc_traits::size_type size_type;
2178 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant896b7622012-05-07 16:50:38 +00002179 typedef size_type __storage_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002180 typedef __bit_iterator<vector, false> pointer;
2181 typedef __bit_iterator<vector, true> const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002182 typedef pointer iterator;
2183 typedef const_pointer const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002184 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
2185 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002186
2187private:
Marshall Clow940e01c2015-04-07 05:21:38 +00002188 typedef typename __rebind_alloc_helper<__alloc_traits, __storage_type>::type __storage_allocator;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002189 typedef allocator_traits<__storage_allocator> __storage_traits;
2190 typedef typename __storage_traits::pointer __storage_pointer;
2191 typedef typename __storage_traits::const_pointer __const_storage_pointer;
2192
2193 __storage_pointer __begin_;
2194 size_type __size_;
2195 __compressed_pair<size_type, __storage_allocator> __cap_alloc_;
Howard Hinnant71d1c192011-07-09 15:50:42 +00002196public:
Howard Hinnantb4ef6482011-07-02 20:33:23 +00002197 typedef __bit_reference<vector> reference;
2198 typedef __bit_const_reference<vector> const_reference;
Howard Hinnant71d1c192011-07-09 15:50:42 +00002199private:
Howard Hinnant1c936782011-06-03 19:40:40 +00002200 _LIBCPP_INLINE_VISIBILITY
2201 size_type& __cap() _NOEXCEPT
2202 {return __cap_alloc_.first();}
2203 _LIBCPP_INLINE_VISIBILITY
2204 const size_type& __cap() const _NOEXCEPT
2205 {return __cap_alloc_.first();}
2206 _LIBCPP_INLINE_VISIBILITY
2207 __storage_allocator& __alloc() _NOEXCEPT
2208 {return __cap_alloc_.second();}
2209 _LIBCPP_INLINE_VISIBILITY
2210 const __storage_allocator& __alloc() const _NOEXCEPT
2211 {return __cap_alloc_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002212
2213 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
2214
Howard Hinnant1c936782011-06-03 19:40:40 +00002215 _LIBCPP_INLINE_VISIBILITY
2216 static size_type __internal_cap_to_external(size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002217 {return __n * __bits_per_word;}
Howard Hinnant1c936782011-06-03 19:40:40 +00002218 _LIBCPP_INLINE_VISIBILITY
2219 static size_type __external_cap_to_internal(size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002220 {return (__n - 1) / __bits_per_word + 1;}
2221
2222public:
Howard Hinnant1c936782011-06-03 19:40:40 +00002223 _LIBCPP_INLINE_VISIBILITY
Marshall Clowe546cbb2015-06-04 02:05:41 +00002224 vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clow8f282f42015-06-04 00:10:20 +00002225
2226 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
2227#if _LIBCPP_STD_VER <= 14
2228 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
2229#else
2230 _NOEXCEPT;
2231#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002232 ~vector();
2233 explicit vector(size_type __n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00002234#if _LIBCPP_STD_VER > 11
2235 explicit vector(size_type __n, const allocator_type& __a);
2236#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002237 vector(size_type __n, const value_type& __v);
2238 vector(size_type __n, const value_type& __v, const allocator_type& __a);
2239 template <class _InputIterator>
2240 vector(_InputIterator __first, _InputIterator __last,
2241 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2242 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2243 template <class _InputIterator>
2244 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2245 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2246 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2247 template <class _ForwardIterator>
2248 vector(_ForwardIterator __first, _ForwardIterator __last,
2249 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2250 template <class _ForwardIterator>
2251 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2252 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2253
2254 vector(const vector& __v);
2255 vector(const vector& __v, const allocator_type& __a);
2256 vector& operator=(const vector& __v);
Eric Fiseliered9e9362017-04-16 02:40:45 +00002257
2258#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002259 vector(initializer_list<value_type> __il);
2260 vector(initializer_list<value_type> __il, const allocator_type& __a);
2261
Howard Hinnant1c936782011-06-03 19:40:40 +00002262 _LIBCPP_INLINE_VISIBILITY
2263 vector(vector&& __v)
Marshall Clowe5108202015-07-14 14:46:32 +00002264#if _LIBCPP_STD_VER > 14
2265 _NOEXCEPT;
2266#else
Howard Hinnant1c936782011-06-03 19:40:40 +00002267 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowe5108202015-07-14 14:46:32 +00002268#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002269 vector(vector&& __v, const allocator_type& __a);
Howard Hinnant1c936782011-06-03 19:40:40 +00002270 _LIBCPP_INLINE_VISIBILITY
2271 vector& operator=(vector&& __v)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002272 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiseliered9e9362017-04-16 02:40:45 +00002273
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002274 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002275 vector& operator=(initializer_list<value_type> __il)
2276 {assign(__il.begin(), __il.end()); return *this;}
Eric Fiseliered9e9362017-04-16 02:40:45 +00002277
2278#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002279
2280 template <class _InputIterator>
2281 typename enable_if
2282 <
2283 __is_input_iterator<_InputIterator>::value &&
2284 !__is_forward_iterator<_InputIterator>::value,
2285 void
2286 >::type
2287 assign(_InputIterator __first, _InputIterator __last);
2288 template <class _ForwardIterator>
2289 typename enable_if
2290 <
2291 __is_forward_iterator<_ForwardIterator>::value,
2292 void
2293 >::type
2294 assign(_ForwardIterator __first, _ForwardIterator __last);
2295
2296 void assign(size_type __n, const value_type& __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +00002297
2298#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002299 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002300 void assign(initializer_list<value_type> __il)
2301 {assign(__il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +00002302#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002303
Howard Hinnant1c936782011-06-03 19:40:40 +00002304 _LIBCPP_INLINE_VISIBILITY allocator_type get_allocator() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002305 {return allocator_type(this->__alloc());}
2306
Howard Hinnant1c936782011-06-03 19:40:40 +00002307 size_type max_size() const _NOEXCEPT;
2308 _LIBCPP_INLINE_VISIBILITY
2309 size_type capacity() const _NOEXCEPT
2310 {return __internal_cap_to_external(__cap());}
2311 _LIBCPP_INLINE_VISIBILITY
2312 size_type size() const _NOEXCEPT
2313 {return __size_;}
Marshall Clow425f5752017-11-15 05:51:26 +00002314 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +00002315 bool empty() const _NOEXCEPT
2316 {return __size_ == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002317 void reserve(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +00002318 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002319
Howard Hinnant1c936782011-06-03 19:40:40 +00002320 _LIBCPP_INLINE_VISIBILITY
2321 iterator begin() _NOEXCEPT
2322 {return __make_iter(0);}
2323 _LIBCPP_INLINE_VISIBILITY
2324 const_iterator begin() const _NOEXCEPT
2325 {return __make_iter(0);}
2326 _LIBCPP_INLINE_VISIBILITY
2327 iterator end() _NOEXCEPT
2328 {return __make_iter(__size_);}
2329 _LIBCPP_INLINE_VISIBILITY
2330 const_iterator end() const _NOEXCEPT
2331 {return __make_iter(__size_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002332
Howard Hinnant1c936782011-06-03 19:40:40 +00002333 _LIBCPP_INLINE_VISIBILITY
2334 reverse_iterator rbegin() _NOEXCEPT
2335 {return reverse_iterator(end());}
2336 _LIBCPP_INLINE_VISIBILITY
2337 const_reverse_iterator rbegin() const _NOEXCEPT
2338 {return const_reverse_iterator(end());}
2339 _LIBCPP_INLINE_VISIBILITY
2340 reverse_iterator rend() _NOEXCEPT
2341 {return reverse_iterator(begin());}
2342 _LIBCPP_INLINE_VISIBILITY
2343 const_reverse_iterator rend() const _NOEXCEPT
2344 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002345
Howard Hinnant1c936782011-06-03 19:40:40 +00002346 _LIBCPP_INLINE_VISIBILITY
2347 const_iterator cbegin() const _NOEXCEPT
2348 {return __make_iter(0);}
2349 _LIBCPP_INLINE_VISIBILITY
2350 const_iterator cend() const _NOEXCEPT
2351 {return __make_iter(__size_);}
2352 _LIBCPP_INLINE_VISIBILITY
2353 const_reverse_iterator crbegin() const _NOEXCEPT
2354 {return rbegin();}
2355 _LIBCPP_INLINE_VISIBILITY
2356 const_reverse_iterator crend() const _NOEXCEPT
2357 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002358
2359 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) {return __make_ref(__n);}
2360 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const {return __make_ref(__n);}
2361 reference at(size_type __n);
2362 const_reference at(size_type __n) const;
2363
2364 _LIBCPP_INLINE_VISIBILITY reference front() {return __make_ref(0);}
2365 _LIBCPP_INLINE_VISIBILITY const_reference front() const {return __make_ref(0);}
2366 _LIBCPP_INLINE_VISIBILITY reference back() {return __make_ref(__size_ - 1);}
2367 _LIBCPP_INLINE_VISIBILITY const_reference back() const {return __make_ref(__size_ - 1);}
2368
2369 void push_back(const value_type& __x);
Marshall Clowc46bb8e2013-08-13 23:54:12 +00002370#if _LIBCPP_STD_VER > 11
2371 template <class... _Args>
Marshall Clowea52cc42017-01-24 23:09:12 +00002372#if _LIBCPP_STD_VER > 14
2373 _LIBCPP_INLINE_VISIBILITY reference emplace_back(_Args&&... __args)
2374#else
2375 _LIBCPP_INLINE_VISIBILITY void emplace_back(_Args&&... __args)
2376#endif
2377 {
Eric Fiselier34ba5b92016-07-21 03:20:17 +00002378 push_back ( value_type ( _VSTD::forward<_Args>(__args)... ));
Marshall Clowea52cc42017-01-24 23:09:12 +00002379#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00002380 return this->back();
Marshall Clowea52cc42017-01-24 23:09:12 +00002381#endif
Eric Fiselier34ba5b92016-07-21 03:20:17 +00002382 }
Marshall Clowc46bb8e2013-08-13 23:54:12 +00002383#endif
2384
Howard Hinnantc51e1022010-05-11 19:42:16 +00002385 _LIBCPP_INLINE_VISIBILITY void pop_back() {--__size_;}
2386
Marshall Clowc46bb8e2013-08-13 23:54:12 +00002387#if _LIBCPP_STD_VER > 11
2388 template <class... _Args>
2389 _LIBCPP_INLINE_VISIBILITY iterator emplace(const_iterator position, _Args&&... __args)
2390 { return insert ( position, value_type ( _VSTD::forward<_Args>(__args)... )); }
2391#endif
2392
Howard Hinnantc51e1022010-05-11 19:42:16 +00002393 iterator insert(const_iterator __position, const value_type& __x);
2394 iterator insert(const_iterator __position, size_type __n, const value_type& __x);
2395 iterator insert(const_iterator __position, size_type __n, const_reference __x);
2396 template <class _InputIterator>
2397 typename enable_if
2398 <
2399 __is_input_iterator <_InputIterator>::value &&
2400 !__is_forward_iterator<_InputIterator>::value,
2401 iterator
2402 >::type
2403 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
2404 template <class _ForwardIterator>
2405 typename enable_if
2406 <
2407 __is_forward_iterator<_ForwardIterator>::value,
2408 iterator
2409 >::type
2410 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiseliered9e9362017-04-16 02:40:45 +00002411
2412#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002413 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002414 iterator insert(const_iterator __position, initializer_list<value_type> __il)
2415 {return insert(__position, __il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +00002416#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002417
Howard Hinnantcf823322010-12-17 14:46:43 +00002418 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002419 iterator erase(const_iterator __first, const_iterator __last);
2420
Howard Hinnant1c936782011-06-03 19:40:40 +00002421 _LIBCPP_INLINE_VISIBILITY
2422 void clear() _NOEXCEPT {__size_ = 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002423
Howard Hinnant1c936782011-06-03 19:40:40 +00002424 void swap(vector&)
Marshall Clow8982dcd2015-07-13 20:04:56 +00002425#if _LIBCPP_STD_VER >= 14
2426 _NOEXCEPT;
2427#else
Eric Fiselier69c51982016-12-28 06:06:09 +00002428 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00002429 __is_nothrow_swappable<allocator_type>::value);
2430#endif
Marshall Clow89eb3822016-04-07 14:20:31 +00002431 static void swap(reference __x, reference __y) _NOEXCEPT { _VSTD::swap(__x, __y); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002432
2433 void resize(size_type __sz, value_type __x = false);
Howard Hinnant1c936782011-06-03 19:40:40 +00002434 void flip() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002435
2436 bool __invariants() const;
2437
2438private:
Howard Hinnantcf823322010-12-17 14:46:43 +00002439 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002440 void allocate(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +00002441 void deallocate() _NOEXCEPT;
2442 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantea382952013-08-14 18:00:20 +00002443 static size_type __align_it(size_type __new_size) _NOEXCEPT
Marshall Clow66f34152014-07-28 15:02:42 +00002444 {return __new_size + (__bits_per_word-1) & ~((size_type)__bits_per_word-1);};
Howard Hinnantcf823322010-12-17 14:46:43 +00002445 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
2446 _LIBCPP_INLINE_VISIBILITY void __construct_at_end(size_type __n, bool __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002447 template <class _ForwardIterator>
2448 typename enable_if
2449 <
2450 __is_forward_iterator<_ForwardIterator>::value,
2451 void
2452 >::type
2453 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
2454 void __append(size_type __n, const_reference __x);
Howard Hinnant1c936782011-06-03 19:40:40 +00002455 _LIBCPP_INLINE_VISIBILITY
2456 reference __make_ref(size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002457 {return reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnant1c936782011-06-03 19:40:40 +00002458 _LIBCPP_INLINE_VISIBILITY
2459 const_reference __make_ref(size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002460 {return const_reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnant1c936782011-06-03 19:40:40 +00002461 _LIBCPP_INLINE_VISIBILITY
2462 iterator __make_iter(size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002463 {return iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnant1c936782011-06-03 19:40:40 +00002464 _LIBCPP_INLINE_VISIBILITY
2465 const_iterator __make_iter(size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002466 {return const_iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnant1c936782011-06-03 19:40:40 +00002467 _LIBCPP_INLINE_VISIBILITY
2468 iterator __const_iterator_cast(const_iterator __p) _NOEXCEPT
Howard Hinnant76053d72013-06-27 19:35:32 +00002469 {return begin() + (__p - cbegin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002470
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002471 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002472 void __copy_assign_alloc(const vector& __v)
2473 {__copy_assign_alloc(__v, integral_constant<bool,
2474 __storage_traits::propagate_on_container_copy_assignment::value>());}
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002475 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002476 void __copy_assign_alloc(const vector& __c, true_type)
2477 {
2478 if (__alloc() != __c.__alloc())
2479 deallocate();
2480 __alloc() = __c.__alloc();
2481 }
2482
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002483 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00002484 void __copy_assign_alloc(const vector&, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002485 {}
2486
2487 void __move_assign(vector& __c, false_type);
Howard Hinnant1c936782011-06-03 19:40:40 +00002488 void __move_assign(vector& __c, true_type)
2489 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002490 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002491 void __move_assign_alloc(vector& __c)
Howard Hinnant1c936782011-06-03 19:40:40 +00002492 _NOEXCEPT_(
2493 !__storage_traits::propagate_on_container_move_assignment::value ||
2494 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002495 {__move_assign_alloc(__c, integral_constant<bool,
2496 __storage_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002497 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +00002498 void __move_assign_alloc(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002499 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002500 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002501 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002502 }
2503
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002504 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00002505 void __move_assign_alloc(vector&, false_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002506 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002507 {}
2508
Howard Hinnant1c936782011-06-03 19:40:40 +00002509 size_t __hash_code() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002510
2511 friend class __bit_reference<vector>;
2512 friend class __bit_const_reference<vector>;
2513 friend class __bit_iterator<vector, false>;
2514 friend class __bit_iterator<vector, true>;
Howard Hinnant4210a0f2012-08-17 17:10:18 +00002515 friend struct __bit_array<vector>;
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002516 friend struct _LIBCPP_TEMPLATE_VIS hash<vector>;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002517};
2518
2519template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002520inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002521void
2522vector<bool, _Allocator>::__invalidate_all_iterators()
2523{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002524}
2525
2526// Allocate space for __n objects
2527// throws length_error if __n > max_size()
2528// throws (probably bad_alloc) if memory run out
2529// Precondition: __begin_ == __end_ == __cap() == 0
2530// Precondition: __n > 0
2531// Postcondition: capacity() == __n
2532// Postcondition: size() == 0
2533template <class _Allocator>
2534void
2535vector<bool, _Allocator>::allocate(size_type __n)
2536{
2537 if (__n > max_size())
2538 this->__throw_length_error();
2539 __n = __external_cap_to_internal(__n);
2540 this->__begin_ = __storage_traits::allocate(this->__alloc(), __n);
2541 this->__size_ = 0;
2542 this->__cap() = __n;
2543}
2544
2545template <class _Allocator>
2546void
Howard Hinnant1c936782011-06-03 19:40:40 +00002547vector<bool, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002548{
Howard Hinnant76053d72013-06-27 19:35:32 +00002549 if (this->__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002550 {
2551 __storage_traits::deallocate(this->__alloc(), this->__begin_, __cap());
2552 __invalidate_all_iterators();
Howard Hinnant76053d72013-06-27 19:35:32 +00002553 this->__begin_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002554 this->__size_ = this->__cap() = 0;
2555 }
2556}
2557
2558template <class _Allocator>
2559typename vector<bool, _Allocator>::size_type
Howard Hinnant1c936782011-06-03 19:40:40 +00002560vector<bool, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002561{
2562 size_type __amax = __storage_traits::max_size(__alloc());
2563 size_type __nmax = numeric_limits<size_type>::max() / 2; // end() >= begin(), always
2564 if (__nmax / __bits_per_word <= __amax)
2565 return __nmax;
2566 return __internal_cap_to_external(__amax);
2567}
2568
2569// Precondition: __new_size > capacity()
2570template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002571inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002572typename vector<bool, _Allocator>::size_type
2573vector<bool, _Allocator>::__recommend(size_type __new_size) const
2574{
2575 const size_type __ms = max_size();
2576 if (__new_size > __ms)
2577 this->__throw_length_error();
2578 const size_type __cap = capacity();
2579 if (__cap >= __ms / 2)
2580 return __ms;
Howard Hinnantea382952013-08-14 18:00:20 +00002581 return _VSTD::max(2*__cap, __align_it(__new_size));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002582}
2583
2584// Default constructs __n objects starting at __end_
2585// Precondition: __n > 0
2586// Precondition: size() + __n <= capacity()
2587// Postcondition: size() == size() + __n
2588template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002589inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002590void
2591vector<bool, _Allocator>::__construct_at_end(size_type __n, bool __x)
2592{
2593 size_type __old_size = this->__size_;
2594 this->__size_ += __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002595 _VSTD::fill_n(__make_iter(__old_size), __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002596}
2597
2598template <class _Allocator>
2599template <class _ForwardIterator>
2600typename enable_if
2601<
2602 __is_forward_iterator<_ForwardIterator>::value,
2603 void
2604>::type
2605vector<bool, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
2606{
2607 size_type __old_size = this->__size_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002608 this->__size_ += _VSTD::distance(__first, __last);
2609 _VSTD::copy(__first, __last, __make_iter(__old_size));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002610}
2611
2612template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002613inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002614vector<bool, _Allocator>::vector()
Marshall Clowe546cbb2015-06-04 02:05:41 +00002615 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant76053d72013-06-27 19:35:32 +00002616 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002617 __size_(0),
2618 __cap_alloc_(0)
2619{
2620}
2621
2622template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002623inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002624vector<bool, _Allocator>::vector(const allocator_type& __a)
Marshall Clow8f282f42015-06-04 00:10:20 +00002625#if _LIBCPP_STD_VER <= 14
2626 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
2627#else
2628 _NOEXCEPT
2629#endif
Howard Hinnant76053d72013-06-27 19:35:32 +00002630 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002631 __size_(0),
2632 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2633{
2634}
2635
2636template <class _Allocator>
2637vector<bool, _Allocator>::vector(size_type __n)
Howard Hinnant76053d72013-06-27 19:35:32 +00002638 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002639 __size_(0),
2640 __cap_alloc_(0)
2641{
2642 if (__n > 0)
2643 {
2644 allocate(__n);
2645 __construct_at_end(__n, false);
2646 }
2647}
2648
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00002649#if _LIBCPP_STD_VER > 11
2650template <class _Allocator>
2651vector<bool, _Allocator>::vector(size_type __n, const allocator_type& __a)
2652 : __begin_(nullptr),
2653 __size_(0),
2654 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2655{
2656 if (__n > 0)
2657 {
2658 allocate(__n);
2659 __construct_at_end(__n, false);
2660 }
2661}
2662#endif
2663
Howard Hinnantc51e1022010-05-11 19:42:16 +00002664template <class _Allocator>
2665vector<bool, _Allocator>::vector(size_type __n, const value_type& __x)
Howard Hinnant76053d72013-06-27 19:35:32 +00002666 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002667 __size_(0),
2668 __cap_alloc_(0)
2669{
2670 if (__n > 0)
2671 {
2672 allocate(__n);
2673 __construct_at_end(__n, __x);
2674 }
2675}
2676
2677template <class _Allocator>
2678vector<bool, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002679 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002680 __size_(0),
2681 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2682{
2683 if (__n > 0)
2684 {
2685 allocate(__n);
2686 __construct_at_end(__n, __x);
2687 }
2688}
2689
2690template <class _Allocator>
2691template <class _InputIterator>
2692vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last,
2693 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2694 !__is_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002695 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002696 __size_(0),
2697 __cap_alloc_(0)
2698{
2699#ifndef _LIBCPP_NO_EXCEPTIONS
2700 try
2701 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002702#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002703 for (; __first != __last; ++__first)
2704 push_back(*__first);
2705#ifndef _LIBCPP_NO_EXCEPTIONS
2706 }
2707 catch (...)
2708 {
Howard Hinnant76053d72013-06-27 19:35:32 +00002709 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002710 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2711 __invalidate_all_iterators();
2712 throw;
2713 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002714#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002715}
2716
2717template <class _Allocator>
2718template <class _InputIterator>
2719vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2720 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2721 !__is_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002722 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002723 __size_(0),
2724 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2725{
2726#ifndef _LIBCPP_NO_EXCEPTIONS
2727 try
2728 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002729#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002730 for (; __first != __last; ++__first)
2731 push_back(*__first);
2732#ifndef _LIBCPP_NO_EXCEPTIONS
2733 }
2734 catch (...)
2735 {
Howard Hinnant76053d72013-06-27 19:35:32 +00002736 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002737 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2738 __invalidate_all_iterators();
2739 throw;
2740 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002741#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002742}
2743
2744template <class _Allocator>
2745template <class _ForwardIterator>
2746vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last,
2747 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002748 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002749 __size_(0),
2750 __cap_alloc_(0)
2751{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002752 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002753 if (__n > 0)
2754 {
2755 allocate(__n);
2756 __construct_at_end(__first, __last);
2757 }
2758}
2759
2760template <class _Allocator>
2761template <class _ForwardIterator>
2762vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2763 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002764 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002765 __size_(0),
2766 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2767{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002768 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002769 if (__n > 0)
2770 {
2771 allocate(__n);
2772 __construct_at_end(__first, __last);
2773 }
2774}
2775
Eric Fiseliered9e9362017-04-16 02:40:45 +00002776#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002777
Howard Hinnantc51e1022010-05-11 19:42:16 +00002778template <class _Allocator>
2779vector<bool, _Allocator>::vector(initializer_list<value_type> __il)
Howard Hinnant76053d72013-06-27 19:35:32 +00002780 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002781 __size_(0),
2782 __cap_alloc_(0)
2783{
2784 size_type __n = static_cast<size_type>(__il.size());
2785 if (__n > 0)
2786 {
2787 allocate(__n);
2788 __construct_at_end(__il.begin(), __il.end());
2789 }
2790}
2791
2792template <class _Allocator>
2793vector<bool, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002794 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002795 __size_(0),
2796 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2797{
2798 size_type __n = static_cast<size_type>(__il.size());
2799 if (__n > 0)
2800 {
2801 allocate(__n);
2802 __construct_at_end(__il.begin(), __il.end());
2803 }
2804}
2805
Eric Fiseliered9e9362017-04-16 02:40:45 +00002806#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002807
Howard Hinnantc51e1022010-05-11 19:42:16 +00002808template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002809vector<bool, _Allocator>::~vector()
2810{
Howard Hinnant76053d72013-06-27 19:35:32 +00002811 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002812 __storage_traits::deallocate(__alloc(), __begin_, __cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002813 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002814}
2815
2816template <class _Allocator>
2817vector<bool, _Allocator>::vector(const vector& __v)
Howard Hinnant76053d72013-06-27 19:35:32 +00002818 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002819 __size_(0),
2820 __cap_alloc_(0, __storage_traits::select_on_container_copy_construction(__v.__alloc()))
2821{
2822 if (__v.size() > 0)
2823 {
2824 allocate(__v.size());
2825 __construct_at_end(__v.begin(), __v.end());
2826 }
2827}
2828
2829template <class _Allocator>
2830vector<bool, _Allocator>::vector(const vector& __v, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002831 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002832 __size_(0),
2833 __cap_alloc_(0, __a)
2834{
2835 if (__v.size() > 0)
2836 {
2837 allocate(__v.size());
2838 __construct_at_end(__v.begin(), __v.end());
2839 }
2840}
2841
2842template <class _Allocator>
2843vector<bool, _Allocator>&
2844vector<bool, _Allocator>::operator=(const vector& __v)
2845{
2846 if (this != &__v)
2847 {
2848 __copy_assign_alloc(__v);
2849 if (__v.__size_)
2850 {
2851 if (__v.__size_ > capacity())
2852 {
2853 deallocate();
2854 allocate(__v.__size_);
2855 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002856 _VSTD::copy(__v.__begin_, __v.__begin_ + __external_cap_to_internal(__v.__size_), __begin_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002857 }
2858 __size_ = __v.__size_;
2859 }
2860 return *this;
2861}
2862
Eric Fiseliered9e9362017-04-16 02:40:45 +00002863#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant74279a52010-09-04 23:28:19 +00002864
Howard Hinnantc51e1022010-05-11 19:42:16 +00002865template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002866inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002867vector<bool, _Allocator>::vector(vector&& __v)
Marshall Clowe5108202015-07-14 14:46:32 +00002868#if _LIBCPP_STD_VER > 14
2869 _NOEXCEPT
2870#else
Howard Hinnant1c936782011-06-03 19:40:40 +00002871 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowe5108202015-07-14 14:46:32 +00002872#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002873 : __begin_(__v.__begin_),
2874 __size_(__v.__size_),
2875 __cap_alloc_(__v.__cap_alloc_)
2876{
Howard Hinnant76053d72013-06-27 19:35:32 +00002877 __v.__begin_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002878 __v.__size_ = 0;
2879 __v.__cap() = 0;
2880}
2881
2882template <class _Allocator>
2883vector<bool, _Allocator>::vector(vector&& __v, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002884 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002885 __size_(0),
2886 __cap_alloc_(0, __a)
2887{
2888 if (__a == allocator_type(__v.__alloc()))
2889 {
2890 this->__begin_ = __v.__begin_;
2891 this->__size_ = __v.__size_;
2892 this->__cap() = __v.__cap();
2893 __v.__begin_ = nullptr;
2894 __v.__cap() = __v.__size_ = 0;
2895 }
2896 else if (__v.size() > 0)
2897 {
2898 allocate(__v.size());
2899 __construct_at_end(__v.begin(), __v.end());
2900 }
2901}
2902
2903template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002904inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002905vector<bool, _Allocator>&
2906vector<bool, _Allocator>::operator=(vector&& __v)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002907 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002908{
2909 __move_assign(__v, integral_constant<bool,
2910 __storage_traits::propagate_on_container_move_assignment::value>());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002911 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002912}
2913
2914template <class _Allocator>
2915void
2916vector<bool, _Allocator>::__move_assign(vector& __c, false_type)
2917{
2918 if (__alloc() != __c.__alloc())
2919 assign(__c.begin(), __c.end());
2920 else
2921 __move_assign(__c, true_type());
2922}
2923
2924template <class _Allocator>
2925void
2926vector<bool, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002927 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002928{
2929 deallocate();
Marshall Clowb4871fa2014-07-21 15:15:15 +00002930 __move_assign_alloc(__c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002931 this->__begin_ = __c.__begin_;
2932 this->__size_ = __c.__size_;
2933 this->__cap() = __c.__cap();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002934 __c.__begin_ = nullptr;
2935 __c.__cap() = __c.__size_ = 0;
2936}
Howard Hinnant74279a52010-09-04 23:28:19 +00002937
Eric Fiseliered9e9362017-04-16 02:40:45 +00002938#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002939
2940template <class _Allocator>
2941void
2942vector<bool, _Allocator>::assign(size_type __n, const value_type& __x)
2943{
2944 __size_ = 0;
2945 if (__n > 0)
2946 {
2947 size_type __c = capacity();
2948 if (__n <= __c)
2949 __size_ = __n;
2950 else
2951 {
2952 vector __v(__alloc());
2953 __v.reserve(__recommend(__n));
2954 __v.__size_ = __n;
2955 swap(__v);
2956 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002957 _VSTD::fill_n(begin(), __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002958 }
Eric Fiselier69c51982016-12-28 06:06:09 +00002959 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002960}
2961
2962template <class _Allocator>
2963template <class _InputIterator>
2964typename enable_if
2965<
2966 __is_input_iterator<_InputIterator>::value &&
2967 !__is_forward_iterator<_InputIterator>::value,
2968 void
2969>::type
2970vector<bool, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2971{
2972 clear();
2973 for (; __first != __last; ++__first)
2974 push_back(*__first);
2975}
2976
2977template <class _Allocator>
2978template <class _ForwardIterator>
2979typename enable_if
2980<
2981 __is_forward_iterator<_ForwardIterator>::value,
2982 void
2983>::type
2984vector<bool, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2985{
2986 clear();
Eric Fiselier6003c772016-12-23 23:37:52 +00002987 difference_type __ns = _VSTD::distance(__first, __last);
2988 _LIBCPP_ASSERT(__ns >= 0, "invalid range specified");
2989 const size_t __n = static_cast<size_type>(__ns);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002990 if (__n)
2991 {
2992 if (__n > capacity())
2993 {
2994 deallocate();
2995 allocate(__n);
2996 }
2997 __construct_at_end(__first, __last);
2998 }
2999}
3000
3001template <class _Allocator>
3002void
3003vector<bool, _Allocator>::reserve(size_type __n)
3004{
3005 if (__n > capacity())
3006 {
3007 vector __v(this->__alloc());
3008 __v.allocate(__n);
3009 __v.__construct_at_end(this->begin(), this->end());
3010 swap(__v);
3011 __invalidate_all_iterators();
3012 }
3013}
3014
3015template <class _Allocator>
3016void
Howard Hinnant1c936782011-06-03 19:40:40 +00003017vector<bool, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003018{
3019 if (__external_cap_to_internal(size()) > __cap())
3020 {
3021#ifndef _LIBCPP_NO_EXCEPTIONS
3022 try
3023 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003024#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003025 vector(*this, allocator_type(__alloc())).swap(*this);
3026#ifndef _LIBCPP_NO_EXCEPTIONS
3027 }
3028 catch (...)
3029 {
3030 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003031#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003032 }
3033}
3034
3035template <class _Allocator>
3036typename vector<bool, _Allocator>::reference
3037vector<bool, _Allocator>::at(size_type __n)
3038{
3039 if (__n >= size())
3040 this->__throw_out_of_range();
3041 return (*this)[__n];
3042}
3043
3044template <class _Allocator>
3045typename vector<bool, _Allocator>::const_reference
3046vector<bool, _Allocator>::at(size_type __n) const
3047{
3048 if (__n >= size())
3049 this->__throw_out_of_range();
3050 return (*this)[__n];
3051}
3052
3053template <class _Allocator>
3054void
3055vector<bool, _Allocator>::push_back(const value_type& __x)
3056{
3057 if (this->__size_ == this->capacity())
3058 reserve(__recommend(this->__size_ + 1));
3059 ++this->__size_;
3060 back() = __x;
3061}
3062
3063template <class _Allocator>
3064typename vector<bool, _Allocator>::iterator
3065vector<bool, _Allocator>::insert(const_iterator __position, const value_type& __x)
3066{
3067 iterator __r;
3068 if (size() < capacity())
3069 {
3070 const_iterator __old_end = end();
3071 ++__size_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003072 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003073 __r = __const_iterator_cast(__position);
3074 }
3075 else
3076 {
3077 vector __v(__alloc());
3078 __v.reserve(__recommend(__size_ + 1));
3079 __v.__size_ = __size_ + 1;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003080 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3081 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003082 swap(__v);
3083 }
3084 *__r = __x;
3085 return __r;
3086}
3087
3088template <class _Allocator>
3089typename vector<bool, _Allocator>::iterator
3090vector<bool, _Allocator>::insert(const_iterator __position, size_type __n, const value_type& __x)
3091{
3092 iterator __r;
3093 size_type __c = capacity();
3094 if (__n <= __c && size() <= __c - __n)
3095 {
3096 const_iterator __old_end = end();
3097 __size_ += __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003098 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003099 __r = __const_iterator_cast(__position);
3100 }
3101 else
3102 {
3103 vector __v(__alloc());
3104 __v.reserve(__recommend(__size_ + __n));
3105 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003106 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3107 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003108 swap(__v);
3109 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003110 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003111 return __r;
3112}
3113
3114template <class _Allocator>
3115template <class _InputIterator>
3116typename enable_if
3117<
3118 __is_input_iterator <_InputIterator>::value &&
3119 !__is_forward_iterator<_InputIterator>::value,
3120 typename vector<bool, _Allocator>::iterator
3121>::type
3122vector<bool, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
3123{
3124 difference_type __off = __position - begin();
3125 iterator __p = __const_iterator_cast(__position);
3126 iterator __old_end = end();
3127 for (; size() != capacity() && __first != __last; ++__first)
3128 {
3129 ++this->__size_;
3130 back() = *__first;
3131 }
3132 vector __v(__alloc());
3133 if (__first != __last)
3134 {
3135#ifndef _LIBCPP_NO_EXCEPTIONS
3136 try
3137 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003138#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003139 __v.assign(__first, __last);
3140 difference_type __old_size = static_cast<difference_type>(__old_end - begin());
3141 difference_type __old_p = __p - begin();
3142 reserve(__recommend(size() + __v.size()));
3143 __p = begin() + __old_p;
3144 __old_end = begin() + __old_size;
3145#ifndef _LIBCPP_NO_EXCEPTIONS
3146 }
3147 catch (...)
3148 {
3149 erase(__old_end, end());
3150 throw;
3151 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003152#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003153 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003154 __p = _VSTD::rotate(__p, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003155 insert(__p, __v.begin(), __v.end());
3156 return begin() + __off;
3157}
3158
3159template <class _Allocator>
3160template <class _ForwardIterator>
3161typename enable_if
3162<
3163 __is_forward_iterator<_ForwardIterator>::value,
3164 typename vector<bool, _Allocator>::iterator
3165>::type
3166vector<bool, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
3167{
Eric Fiselier654dd332016-12-11 05:31:00 +00003168 const difference_type __n_signed = _VSTD::distance(__first, __last);
3169 _LIBCPP_ASSERT(__n_signed >= 0, "invalid range specified");
3170 const size_type __n = static_cast<size_type>(__n_signed);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003171 iterator __r;
3172 size_type __c = capacity();
3173 if (__n <= __c && size() <= __c - __n)
3174 {
3175 const_iterator __old_end = end();
3176 __size_ += __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003177 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003178 __r = __const_iterator_cast(__position);
3179 }
3180 else
3181 {
3182 vector __v(__alloc());
3183 __v.reserve(__recommend(__size_ + __n));
3184 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003185 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3186 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003187 swap(__v);
3188 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003189 _VSTD::copy(__first, __last, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003190 return __r;
3191}
3192
3193template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003194inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003195typename vector<bool, _Allocator>::iterator
3196vector<bool, _Allocator>::erase(const_iterator __position)
3197{
3198 iterator __r = __const_iterator_cast(__position);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003199 _VSTD::copy(__position + 1, this->cend(), __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003200 --__size_;
3201 return __r;
3202}
3203
3204template <class _Allocator>
3205typename vector<bool, _Allocator>::iterator
3206vector<bool, _Allocator>::erase(const_iterator __first, const_iterator __last)
3207{
3208 iterator __r = __const_iterator_cast(__first);
3209 difference_type __d = __last - __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003210 _VSTD::copy(__last, this->cend(), __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003211 __size_ -= __d;
3212 return __r;
3213}
3214
3215template <class _Allocator>
3216void
3217vector<bool, _Allocator>::swap(vector& __x)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003218#if _LIBCPP_STD_VER >= 14
3219 _NOEXCEPT
3220#else
Eric Fiselier69c51982016-12-28 06:06:09 +00003221 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003222 __is_nothrow_swappable<allocator_type>::value)
3223#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003224{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003225 _VSTD::swap(this->__begin_, __x.__begin_);
3226 _VSTD::swap(this->__size_, __x.__size_);
3227 _VSTD::swap(this->__cap(), __x.__cap());
Eric Fiseliered9e9362017-04-16 02:40:45 +00003228 __swap_allocator(this->__alloc(), __x.__alloc(),
Marshall Clow8982dcd2015-07-13 20:04:56 +00003229 integral_constant<bool, __alloc_traits::propagate_on_container_swap::value>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003230}
3231
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003232template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003233void
3234vector<bool, _Allocator>::resize(size_type __sz, value_type __x)
3235{
3236 size_type __cs = size();
3237 if (__cs < __sz)
3238 {
3239 iterator __r;
3240 size_type __c = capacity();
3241 size_type __n = __sz - __cs;
3242 if (__n <= __c && __cs <= __c - __n)
3243 {
3244 __r = end();
3245 __size_ += __n;
3246 }
3247 else
3248 {
3249 vector __v(__alloc());
3250 __v.reserve(__recommend(__size_ + __n));
3251 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003252 __r = _VSTD::copy(cbegin(), cend(), __v.begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003253 swap(__v);
3254 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003255 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003256 }
3257 else
3258 __size_ = __sz;
3259}
3260
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003261template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003262void
Howard Hinnant1c936782011-06-03 19:40:40 +00003263vector<bool, _Allocator>::flip() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003264{
3265 // do middle whole words
3266 size_type __n = __size_;
3267 __storage_pointer __p = __begin_;
3268 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3269 *__p = ~*__p;
3270 // do last partial word
3271 if (__n > 0)
3272 {
3273 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3274 __storage_type __b = *__p & __m;
3275 *__p &= ~__m;
3276 *__p |= ~__b & __m;
3277 }
3278}
3279
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003280template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003281bool
3282vector<bool, _Allocator>::__invariants() const
3283{
Howard Hinnant76053d72013-06-27 19:35:32 +00003284 if (this->__begin_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003285 {
3286 if (this->__size_ != 0 || this->__cap() != 0)
3287 return false;
3288 }
3289 else
3290 {
3291 if (this->__cap() == 0)
3292 return false;
3293 if (this->__size_ > this->capacity())
3294 return false;
3295 }
3296 return true;
3297}
3298
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003299template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003300size_t
Howard Hinnant1c936782011-06-03 19:40:40 +00003301vector<bool, _Allocator>::__hash_code() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003302{
3303 size_t __h = 0;
3304 // do middle whole words
3305 size_type __n = __size_;
3306 __storage_pointer __p = __begin_;
3307 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3308 __h ^= *__p;
3309 // do last partial word
3310 if (__n > 0)
3311 {
3312 const __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3313 __h ^= *__p & __m;
3314 }
3315 return __h;
3316}
3317
3318template <class _Allocator>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003319struct _LIBCPP_TEMPLATE_VIS hash<vector<bool, _Allocator> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00003320 : public unary_function<vector<bool, _Allocator>, size_t>
3321{
Howard Hinnant1c265cd2010-09-23 18:58:28 +00003322 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +00003323 size_t operator()(const vector<bool, _Allocator>& __vec) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003324 {return __vec.__hash_code();}
3325};
3326
3327template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003328inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003329bool
3330operator==(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3331{
3332 const typename vector<_Tp, _Allocator>::size_type __sz = __x.size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003333 return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003334}
3335
3336template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003337inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003338bool
3339operator!=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3340{
3341 return !(__x == __y);
3342}
3343
3344template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003345inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003346bool
3347operator< (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3348{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003349 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003350}
3351
3352template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003353inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003354bool
3355operator> (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3356{
3357 return __y < __x;
3358}
3359
3360template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003361inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003362bool
3363operator>=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3364{
3365 return !(__x < __y);
3366}
3367
3368template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003369inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003370bool
3371operator<=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3372{
3373 return !(__y < __x);
3374}
3375
3376template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003377inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003378void
3379swap(vector<_Tp, _Allocator>& __x, vector<_Tp, _Allocator>& __y)
Howard Hinnant1c936782011-06-03 19:40:40 +00003380 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00003381{
3382 __x.swap(__y);
3383}
3384
3385_LIBCPP_END_NAMESPACE_STD
3386
Eric Fiselierf4433a32017-05-31 22:07:49 +00003387_LIBCPP_POP_MACROS
3388
Howard Hinnantc51e1022010-05-11 19:42:16 +00003389#endif // _LIBCPP_VECTOR