blob: 6e9920a0f80f06ed08131d4ca8895b330c350e07 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===------------------------------ vector --------------------------------===//
3//
Howard Hinnantc566dc32010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantc51e1022010-05-11 19:42:16 +00005//
Howard Hinnantee11c312010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantc51e1022010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_VECTOR
12#define _LIBCPP_VECTOR
13
14/*
15 vector synopsis
16
17namespace std
18{
19
Howard Hinnant3b6579a2010-08-22 00:02:43 +000020template <class T, class Allocator = allocator<T> >
Howard Hinnantc51e1022010-05-11 19:42:16 +000021class vector
Howard Hinnant3b6579a2010-08-22 00:02:43 +000022{
23public:
24 typedef T value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +000025 typedef Allocator allocator_type;
26 typedef typename allocator_type::reference reference;
27 typedef typename allocator_type::const_reference const_reference;
28 typedef implementation-defined iterator;
29 typedef implementation-defined const_iterator;
30 typedef typename allocator_type::size_type size_type;
31 typedef typename allocator_type::difference_type difference_type;
32 typedef typename allocator_type::pointer pointer;
33 typedef typename allocator_type::const_pointer const_pointer;
34 typedef std::reverse_iterator<iterator> reverse_iterator;
35 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
36
Howard Hinnant1c936782011-06-03 19:40:40 +000037 vector()
38 noexcept(is_nothrow_default_constructible<allocator_type>::value);
39 explicit vector(const allocator_type&);
Howard Hinnantc51e1022010-05-11 19:42:16 +000040 explicit vector(size_type n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +000041 explicit vector(size_type n, const allocator_type&); // C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +000042 vector(size_type n, const value_type& value, const allocator_type& = allocator_type());
43 template <class InputIterator>
44 vector(InputIterator first, InputIterator last, const allocator_type& = allocator_type());
45 vector(const vector& x);
Howard Hinnant1c936782011-06-03 19:40:40 +000046 vector(vector&& x)
47 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +000048 vector(initializer_list<value_type> il);
49 vector(initializer_list<value_type> il, const allocator_type& a);
50 ~vector();
51 vector& operator=(const vector& x);
Howard Hinnant1c936782011-06-03 19:40:40 +000052 vector& operator=(vector&& x)
53 noexcept(
Marshall Clow2fe8a8d2015-08-18 18:57:00 +000054 allocator_type::propagate_on_container_move_assignment::value ||
55 allocator_type::is_always_equal::value); // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +000056 vector& operator=(initializer_list<value_type> il);
57 template <class InputIterator>
58 void assign(InputIterator first, InputIterator last);
59 void assign(size_type n, const value_type& u);
60 void assign(initializer_list<value_type> il);
61
Howard Hinnant1c936782011-06-03 19:40:40 +000062 allocator_type get_allocator() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000063
Howard Hinnant1c936782011-06-03 19:40:40 +000064 iterator begin() noexcept;
65 const_iterator begin() const noexcept;
66 iterator end() noexcept;
67 const_iterator end() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000068
Howard Hinnant1c936782011-06-03 19:40:40 +000069 reverse_iterator rbegin() noexcept;
70 const_reverse_iterator rbegin() const noexcept;
71 reverse_iterator rend() noexcept;
72 const_reverse_iterator rend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000073
Howard Hinnant1c936782011-06-03 19:40:40 +000074 const_iterator cbegin() const noexcept;
75 const_iterator cend() const noexcept;
76 const_reverse_iterator crbegin() const noexcept;
77 const_reverse_iterator crend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000078
Howard Hinnant1c936782011-06-03 19:40:40 +000079 size_type size() const noexcept;
80 size_type max_size() const noexcept;
81 size_type capacity() const noexcept;
82 bool empty() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000083 void reserve(size_type n);
Howard Hinnant1c936782011-06-03 19:40:40 +000084 void shrink_to_fit() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000085
86 reference operator[](size_type n);
87 const_reference operator[](size_type n) const;
88 reference at(size_type n);
89 const_reference at(size_type n) const;
90
91 reference front();
92 const_reference front() const;
93 reference back();
94 const_reference back() const;
95
Howard Hinnant1c936782011-06-03 19:40:40 +000096 value_type* data() noexcept;
97 const value_type* data() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000098
99 void push_back(const value_type& x);
100 void push_back(value_type&& x);
101 template <class... Args>
Marshall Clowea52cc42017-01-24 23:09:12 +0000102 reference emplace_back(Args&&... args); // reference in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000103 void pop_back();
104
105 template <class... Args> iterator emplace(const_iterator position, Args&&... args);
106 iterator insert(const_iterator position, const value_type& x);
107 iterator insert(const_iterator position, value_type&& x);
108 iterator insert(const_iterator position, size_type n, const value_type& x);
109 template <class InputIterator>
110 iterator insert(const_iterator position, InputIterator first, InputIterator last);
111 iterator insert(const_iterator position, initializer_list<value_type> il);
112
113 iterator erase(const_iterator position);
114 iterator erase(const_iterator first, const_iterator last);
115
Howard Hinnant1c936782011-06-03 19:40:40 +0000116 void clear() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000117
118 void resize(size_type sz);
119 void resize(size_type sz, const value_type& c);
120
Howard Hinnant1c936782011-06-03 19:40:40 +0000121 void swap(vector&)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000122 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
123 allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000124
125 bool __invariants() const;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000126};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000127
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000128template <class Allocator = allocator<T> >
Howard Hinnantc51e1022010-05-11 19:42:16 +0000129class vector<bool, Allocator>
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000130{
131public:
132 typedef bool value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000133 typedef Allocator allocator_type;
134 typedef implementation-defined iterator;
135 typedef implementation-defined const_iterator;
136 typedef typename allocator_type::size_type size_type;
137 typedef typename allocator_type::difference_type difference_type;
138 typedef iterator pointer;
139 typedef const_iterator const_pointer;
140 typedef std::reverse_iterator<iterator> reverse_iterator;
141 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
142
143 class reference
144 {
145 public:
Howard Hinnant1c936782011-06-03 19:40:40 +0000146 reference(const reference&) noexcept;
147 operator bool() const noexcept;
148 reference& operator=(const bool x) noexcept;
149 reference& operator=(const reference& x) noexcept;
150 iterator operator&() const noexcept;
151 void flip() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000152 };
153
154 class const_reference
155 {
156 public:
Howard Hinnant1c936782011-06-03 19:40:40 +0000157 const_reference(const reference&) noexcept;
158 operator bool() const noexcept;
159 const_iterator operator&() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000160 };
161
Howard Hinnant1c936782011-06-03 19:40:40 +0000162 vector()
163 noexcept(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnantc2734962011-09-02 20:42:31 +0000164 explicit vector(const allocator_type&);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +0000165 explicit vector(size_type n, const allocator_type& a = allocator_type()); // C++14
166 vector(size_type n, const value_type& value, const allocator_type& = allocator_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000167 template <class InputIterator>
168 vector(InputIterator first, InputIterator last, const allocator_type& = allocator_type());
169 vector(const vector& x);
Howard Hinnant1c936782011-06-03 19:40:40 +0000170 vector(vector&& x)
171 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000172 vector(initializer_list<value_type> il);
173 vector(initializer_list<value_type> il, const allocator_type& a);
174 ~vector();
175 vector& operator=(const vector& x);
Howard Hinnant1c936782011-06-03 19:40:40 +0000176 vector& operator=(vector&& x)
177 noexcept(
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000178 allocator_type::propagate_on_container_move_assignment::value ||
179 allocator_type::is_always_equal::value); // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000180 vector& operator=(initializer_list<value_type> il);
181 template <class InputIterator>
182 void assign(InputIterator first, InputIterator last);
183 void assign(size_type n, const value_type& u);
184 void assign(initializer_list<value_type> il);
185
Howard Hinnant1c936782011-06-03 19:40:40 +0000186 allocator_type get_allocator() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000187
Howard Hinnant1c936782011-06-03 19:40:40 +0000188 iterator begin() noexcept;
189 const_iterator begin() const noexcept;
190 iterator end() noexcept;
191 const_iterator end() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000192
Howard Hinnant1c936782011-06-03 19:40:40 +0000193 reverse_iterator rbegin() noexcept;
194 const_reverse_iterator rbegin() const noexcept;
195 reverse_iterator rend() noexcept;
196 const_reverse_iterator rend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000197
Howard Hinnant1c936782011-06-03 19:40:40 +0000198 const_iterator cbegin() const noexcept;
199 const_iterator cend() const noexcept;
200 const_reverse_iterator crbegin() const noexcept;
201 const_reverse_iterator crend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000202
Howard Hinnant1c936782011-06-03 19:40:40 +0000203 size_type size() const noexcept;
204 size_type max_size() const noexcept;
205 size_type capacity() const noexcept;
206 bool empty() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000207 void reserve(size_type n);
Howard Hinnant1c936782011-06-03 19:40:40 +0000208 void shrink_to_fit() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000209
210 reference operator[](size_type n);
211 const_reference operator[](size_type n) const;
212 reference at(size_type n);
213 const_reference at(size_type n) const;
214
215 reference front();
216 const_reference front() const;
217 reference back();
218 const_reference back() const;
219
220 void push_back(const value_type& x);
Marshall Clowea52cc42017-01-24 23:09:12 +0000221 template <class... Args> reference emplace_back(Args&&... args); // C++14; reference in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000222 void pop_back();
223
Marshall Clowc46bb8e2013-08-13 23:54:12 +0000224 template <class... Args> iterator emplace(const_iterator position, Args&&... args); // C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000225 iterator insert(const_iterator position, const value_type& x);
226 iterator insert(const_iterator position, size_type n, const value_type& x);
227 template <class InputIterator>
228 iterator insert(const_iterator position, InputIterator first, InputIterator last);
229 iterator insert(const_iterator position, initializer_list<value_type> il);
230
231 iterator erase(const_iterator position);
232 iterator erase(const_iterator first, const_iterator last);
233
Howard Hinnant1c936782011-06-03 19:40:40 +0000234 void clear() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000235
236 void resize(size_type sz);
237 void resize(size_type sz, value_type x);
238
Howard Hinnant1c936782011-06-03 19:40:40 +0000239 void swap(vector&)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000240 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
241 allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnant1c936782011-06-03 19:40:40 +0000242 void flip() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000243
244 bool __invariants() const;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000245};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000246
247template <class Allocator> struct hash<std::vector<bool, Allocator>>;
248
249template <class T, class Allocator> bool operator==(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
250template <class T, class Allocator> bool operator< (const vector<T,Allocator>& x, const vector<T,Allocator>& y);
251template <class T, class Allocator> bool operator!=(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
252template <class T, class Allocator> bool operator> (const vector<T,Allocator>& x, const vector<T,Allocator>& y);
253template <class T, class Allocator> bool operator>=(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
254template <class T, class Allocator> bool operator<=(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
255
Howard Hinnant1c936782011-06-03 19:40:40 +0000256template <class T, class Allocator>
257void swap(vector<T,Allocator>& x, vector<T,Allocator>& y)
258 noexcept(noexcept(x.swap(y)));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000259
260} // std
261
262*/
263
264#include <__config>
Eric Fiselier876c6862016-02-20 00:19:45 +0000265#include <iosfwd> // for forward declaration of vector
Howard Hinnantc51e1022010-05-11 19:42:16 +0000266#include <__bit_reference>
267#include <type_traits>
268#include <climits>
269#include <limits>
270#include <initializer_list>
271#include <memory>
272#include <stdexcept>
273#include <algorithm>
274#include <cstring>
275#include <__split_buffer>
276#include <__functional_base>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000277
Eric Fiselier14b6de92014-08-10 23:53:08 +0000278#include <__debug>
Howard Hinnante6ff0b62013-08-02 00:26:35 +0000279
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000280#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000281#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000282#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000283
Eric Fiselierf4433a32017-05-31 22:07:49 +0000284_LIBCPP_PUSH_MACROS
285#include <__undef_macros>
286
287
Howard Hinnantc51e1022010-05-11 19:42:16 +0000288_LIBCPP_BEGIN_NAMESPACE_STD
289
290template <bool>
291class __vector_base_common
292{
293protected:
294 _LIBCPP_ALWAYS_INLINE __vector_base_common() {}
Marshall Clow8fea1612016-08-25 15:09:01 +0000295 _LIBCPP_NORETURN void __throw_length_error() const;
296 _LIBCPP_NORETURN void __throw_out_of_range() const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000297};
298
299template <bool __b>
300void
301__vector_base_common<__b>::__throw_length_error() const
302{
Marshall Clow8fea1612016-08-25 15:09:01 +0000303 _VSTD::__throw_length_error("vector");
Howard Hinnantc51e1022010-05-11 19:42:16 +0000304}
305
306template <bool __b>
307void
308__vector_base_common<__b>::__throw_out_of_range() const
309{
Marshall Clow8fea1612016-08-25 15:09:01 +0000310 _VSTD::__throw_out_of_range("vector");
Howard Hinnantc51e1022010-05-11 19:42:16 +0000311}
312
Eric Fiselier1b57fa82016-09-15 22:27:07 +0000313_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __vector_base_common<true>)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000314
315template <class _Tp, class _Allocator>
316class __vector_base
317 : protected __vector_base_common<true>
318{
319protected:
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000320 typedef _Tp value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000321 typedef _Allocator allocator_type;
322 typedef allocator_traits<allocator_type> __alloc_traits;
323 typedef value_type& reference;
324 typedef const value_type& const_reference;
325 typedef typename __alloc_traits::size_type size_type;
326 typedef typename __alloc_traits::difference_type difference_type;
327 typedef typename __alloc_traits::pointer pointer;
328 typedef typename __alloc_traits::const_pointer const_pointer;
329 typedef pointer iterator;
330 typedef const_pointer const_iterator;
331
332 pointer __begin_;
333 pointer __end_;
334 __compressed_pair<pointer, allocator_type> __end_cap_;
335
Howard Hinnant1c936782011-06-03 19:40:40 +0000336 _LIBCPP_INLINE_VISIBILITY
337 allocator_type& __alloc() _NOEXCEPT
338 {return __end_cap_.second();}
339 _LIBCPP_INLINE_VISIBILITY
340 const allocator_type& __alloc() const _NOEXCEPT
341 {return __end_cap_.second();}
342 _LIBCPP_INLINE_VISIBILITY
343 pointer& __end_cap() _NOEXCEPT
344 {return __end_cap_.first();}
345 _LIBCPP_INLINE_VISIBILITY
346 const pointer& __end_cap() const _NOEXCEPT
347 {return __end_cap_.first();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000348
Howard Hinnant1c936782011-06-03 19:40:40 +0000349 _LIBCPP_INLINE_VISIBILITY
350 __vector_base()
351 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnantcf823322010-12-17 14:46:43 +0000352 _LIBCPP_INLINE_VISIBILITY __vector_base(const allocator_type& __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000353 ~__vector_base();
354
Howard Hinnant1c936782011-06-03 19:40:40 +0000355 _LIBCPP_INLINE_VISIBILITY
356 void clear() _NOEXCEPT {__destruct_at_end(__begin_);}
357 _LIBCPP_INLINE_VISIBILITY
358 size_type capacity() const _NOEXCEPT
359 {return static_cast<size_type>(__end_cap() - __begin_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000360
Howard Hinnant1c936782011-06-03 19:40:40 +0000361 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76053d72013-06-27 19:35:32 +0000362 void __destruct_at_end(pointer __new_last) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000363
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000364 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000365 void __copy_assign_alloc(const __vector_base& __c)
366 {__copy_assign_alloc(__c, integral_constant<bool,
367 __alloc_traits::propagate_on_container_copy_assignment::value>());}
368
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000369 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000370 void __move_assign_alloc(__vector_base& __c)
Howard Hinnant1c936782011-06-03 19:40:40 +0000371 _NOEXCEPT_(
372 !__alloc_traits::propagate_on_container_move_assignment::value ||
373 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000374 {__move_assign_alloc(__c, integral_constant<bool,
375 __alloc_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000376private:
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000377 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000378 void __copy_assign_alloc(const __vector_base& __c, true_type)
379 {
380 if (__alloc() != __c.__alloc())
381 {
382 clear();
383 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
384 __begin_ = __end_ = __end_cap() = nullptr;
385 }
386 __alloc() = __c.__alloc();
387 }
388
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000389 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +0000390 void __copy_assign_alloc(const __vector_base&, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000391 {}
392
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000393 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +0000394 void __move_assign_alloc(__vector_base& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +0000395 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000396 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000397 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000398 }
399
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000400 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +0000401 void __move_assign_alloc(__vector_base&, false_type)
Howard Hinnant1c936782011-06-03 19:40:40 +0000402 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000403 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000404};
405
406template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000407inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000408void
Howard Hinnant76053d72013-06-27 19:35:32 +0000409__vector_base<_Tp, _Allocator>::__destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000410{
Bruce Mitchener405fdcd2017-03-23 14:39:23 +0000411 pointer __soon_to_be_end = __end_;
412 while (__new_last != __soon_to_be_end)
413 __alloc_traits::destroy(__alloc(), _VSTD::__to_raw_pointer(--__soon_to_be_end));
414 __end_ = __new_last;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000415}
416
417template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000418inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000419__vector_base<_Tp, _Allocator>::__vector_base()
Howard Hinnant1c936782011-06-03 19:40:40 +0000420 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant76053d72013-06-27 19:35:32 +0000421 : __begin_(nullptr),
422 __end_(nullptr),
423 __end_cap_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000424{
425}
426
427template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000428inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000429__vector_base<_Tp, _Allocator>::__vector_base(const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +0000430 : __begin_(nullptr),
431 __end_(nullptr),
432 __end_cap_(nullptr, __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000433{
434}
435
436template <class _Tp, class _Allocator>
437__vector_base<_Tp, _Allocator>::~__vector_base()
438{
Howard Hinnant76053d72013-06-27 19:35:32 +0000439 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000440 {
441 clear();
442 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
443 }
444}
445
Eric Fiselier876c6862016-02-20 00:19:45 +0000446template <class _Tp, class _Allocator /* = allocator<_Tp> */>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000447class _LIBCPP_TEMPLATE_VIS vector
Howard Hinnantc51e1022010-05-11 19:42:16 +0000448 : private __vector_base<_Tp, _Allocator>
449{
450private:
451 typedef __vector_base<_Tp, _Allocator> __base;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000452 typedef allocator<_Tp> __default_allocator_type;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000453public:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000454 typedef vector __self;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000455 typedef _Tp value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000456 typedef _Allocator allocator_type;
457 typedef typename __base::__alloc_traits __alloc_traits;
458 typedef typename __base::reference reference;
459 typedef typename __base::const_reference const_reference;
460 typedef typename __base::size_type size_type;
461 typedef typename __base::difference_type difference_type;
462 typedef typename __base::pointer pointer;
463 typedef typename __base::const_pointer const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000464 typedef __wrap_iter<pointer> iterator;
465 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000466 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
467 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000468
Howard Hinnanta416ff02013-03-26 19:04:56 +0000469 static_assert((is_same<typename allocator_type::value_type, value_type>::value),
470 "Allocator::value_type must be same type as value_type");
471
Howard Hinnant1c936782011-06-03 19:40:40 +0000472 _LIBCPP_INLINE_VISIBILITY
Marshall Clowe546cbb2015-06-04 02:05:41 +0000473 vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant27e0e772011-09-14 18:33:51 +0000474 {
Howard Hinnanta47c6d52011-09-16 17:29:17 +0000475#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000476 __get_db()->__insert_c(this);
477#endif
478 }
479 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
Marshall Clow8f282f42015-06-04 00:10:20 +0000480#if _LIBCPP_STD_VER <= 14
481 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
482#else
483 _NOEXCEPT
484#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +0000485 : __base(__a)
486 {
Howard Hinnanta47c6d52011-09-16 17:29:17 +0000487#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000488 __get_db()->__insert_c(this);
489#endif
490 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000491 explicit vector(size_type __n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +0000492#if _LIBCPP_STD_VER > 11
493 explicit vector(size_type __n, const allocator_type& __a);
494#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000495 vector(size_type __n, const_reference __x);
496 vector(size_type __n, const_reference __x, const allocator_type& __a);
497 template <class _InputIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +0000498 vector(_InputIterator __first,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000499 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000500 !__is_forward_iterator<_InputIterator>::value &&
501 is_constructible<
502 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +0000503 typename iterator_traits<_InputIterator>::reference>::value,
504 _InputIterator>::type __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000505 template <class _InputIterator>
506 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
507 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000508 !__is_forward_iterator<_InputIterator>::value &&
509 is_constructible<
510 value_type,
511 typename iterator_traits<_InputIterator>::reference>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000512 template <class _ForwardIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +0000513 vector(_ForwardIterator __first,
Howard Hinnant88010252013-03-28 17:44:32 +0000514 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
515 is_constructible<
516 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +0000517 typename iterator_traits<_ForwardIterator>::reference>::value,
518 _ForwardIterator>::type __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000519 template <class _ForwardIterator>
520 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Howard Hinnant88010252013-03-28 17:44:32 +0000521 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
522 is_constructible<
523 value_type,
524 typename iterator_traits<_ForwardIterator>::reference>::value>::type* = 0);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000525
Howard Hinnanta47c6d52011-09-16 17:29:17 +0000526#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000527 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant27e0e772011-09-14 18:33:51 +0000528 ~vector()
529 {
530 __get_db()->__erase_c(this);
531 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000532#endif
533
534 vector(const vector& __x);
535 vector(const vector& __x, const allocator_type& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000536 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000537 vector& operator=(const vector& __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000538
539#ifndef _LIBCPP_CXX03_LANG
540 _LIBCPP_INLINE_VISIBILITY
541 vector(initializer_list<value_type> __il);
542
543 _LIBCPP_INLINE_VISIBILITY
544 vector(initializer_list<value_type> __il, const allocator_type& __a);
545
Howard Hinnantcf823322010-12-17 14:46:43 +0000546 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000547 vector(vector&& __x)
Marshall Clowe5108202015-07-14 14:46:32 +0000548#if _LIBCPP_STD_VER > 14
549 _NOEXCEPT;
550#else
Howard Hinnant1c936782011-06-03 19:40:40 +0000551 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowe5108202015-07-14 14:46:32 +0000552#endif
Eric Fiseliered9e9362017-04-16 02:40:45 +0000553
Howard Hinnantcf823322010-12-17 14:46:43 +0000554 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000555 vector(vector&& __x, const allocator_type& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000556 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000557 vector& operator=(vector&& __x)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000558 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiseliered9e9362017-04-16 02:40:45 +0000559
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000560 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000561 vector& operator=(initializer_list<value_type> __il)
562 {assign(__il.begin(), __il.end()); return *this;}
Eric Fiseliered9e9362017-04-16 02:40:45 +0000563
564#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000565
566 template <class _InputIterator>
567 typename enable_if
568 <
569 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000570 !__is_forward_iterator<_InputIterator>::value &&
571 is_constructible<
572 value_type,
573 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000574 void
575 >::type
576 assign(_InputIterator __first, _InputIterator __last);
577 template <class _ForwardIterator>
578 typename enable_if
579 <
Howard Hinnant88010252013-03-28 17:44:32 +0000580 __is_forward_iterator<_ForwardIterator>::value &&
581 is_constructible<
582 value_type,
583 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000584 void
585 >::type
586 assign(_ForwardIterator __first, _ForwardIterator __last);
587
588 void assign(size_type __n, const_reference __u);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000589
590#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000591 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000592 void assign(initializer_list<value_type> __il)
593 {assign(__il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +0000594#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000595
Howard Hinnant1c936782011-06-03 19:40:40 +0000596 _LIBCPP_INLINE_VISIBILITY
597 allocator_type get_allocator() const _NOEXCEPT
598 {return this->__alloc();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000599
Howard Hinnant1c936782011-06-03 19:40:40 +0000600 _LIBCPP_INLINE_VISIBILITY iterator begin() _NOEXCEPT;
601 _LIBCPP_INLINE_VISIBILITY const_iterator begin() const _NOEXCEPT;
602 _LIBCPP_INLINE_VISIBILITY iterator end() _NOEXCEPT;
603 _LIBCPP_INLINE_VISIBILITY const_iterator end() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000604
Howard Hinnant1c936782011-06-03 19:40:40 +0000605 _LIBCPP_INLINE_VISIBILITY
606 reverse_iterator rbegin() _NOEXCEPT
607 {return reverse_iterator(end());}
608 _LIBCPP_INLINE_VISIBILITY
609 const_reverse_iterator rbegin() const _NOEXCEPT
610 {return const_reverse_iterator(end());}
611 _LIBCPP_INLINE_VISIBILITY
612 reverse_iterator rend() _NOEXCEPT
613 {return reverse_iterator(begin());}
614 _LIBCPP_INLINE_VISIBILITY
615 const_reverse_iterator rend() const _NOEXCEPT
616 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000617
Howard Hinnant1c936782011-06-03 19:40:40 +0000618 _LIBCPP_INLINE_VISIBILITY
619 const_iterator cbegin() const _NOEXCEPT
620 {return begin();}
621 _LIBCPP_INLINE_VISIBILITY
622 const_iterator cend() const _NOEXCEPT
623 {return end();}
624 _LIBCPP_INLINE_VISIBILITY
625 const_reverse_iterator crbegin() const _NOEXCEPT
626 {return rbegin();}
627 _LIBCPP_INLINE_VISIBILITY
628 const_reverse_iterator crend() const _NOEXCEPT
629 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000630
Howard Hinnant1c936782011-06-03 19:40:40 +0000631 _LIBCPP_INLINE_VISIBILITY
632 size_type size() const _NOEXCEPT
633 {return static_cast<size_type>(this->__end_ - this->__begin_);}
634 _LIBCPP_INLINE_VISIBILITY
635 size_type capacity() const _NOEXCEPT
636 {return __base::capacity();}
637 _LIBCPP_INLINE_VISIBILITY
638 bool empty() const _NOEXCEPT
639 {return this->__begin_ == this->__end_;}
640 size_type max_size() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000641 void reserve(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +0000642 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000643
644 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n);
645 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const;
646 reference at(size_type __n);
647 const_reference at(size_type __n) const;
648
Howard Hinnant27e0e772011-09-14 18:33:51 +0000649 _LIBCPP_INLINE_VISIBILITY reference front()
650 {
651 _LIBCPP_ASSERT(!empty(), "front() called for empty vector");
652 return *this->__begin_;
653 }
654 _LIBCPP_INLINE_VISIBILITY const_reference front() const
655 {
656 _LIBCPP_ASSERT(!empty(), "front() called for empty vector");
657 return *this->__begin_;
658 }
659 _LIBCPP_INLINE_VISIBILITY reference back()
660 {
661 _LIBCPP_ASSERT(!empty(), "back() called for empty vector");
662 return *(this->__end_ - 1);
663 }
664 _LIBCPP_INLINE_VISIBILITY const_reference back() const
665 {
666 _LIBCPP_ASSERT(!empty(), "back() called for empty vector");
667 return *(this->__end_ - 1);
668 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000669
Howard Hinnant1c936782011-06-03 19:40:40 +0000670 _LIBCPP_INLINE_VISIBILITY
671 value_type* data() _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000672 {return _VSTD::__to_raw_pointer(this->__begin_);}
Howard Hinnant1c936782011-06-03 19:40:40 +0000673 _LIBCPP_INLINE_VISIBILITY
674 const value_type* data() const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000675 {return _VSTD::__to_raw_pointer(this->__begin_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000676
Howard Hinnantcf823322010-12-17 14:46:43 +0000677 _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000678
679#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000680 _LIBCPP_INLINE_VISIBILITY void push_back(value_type&& __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000681
Howard Hinnantc51e1022010-05-11 19:42:16 +0000682 template <class... _Args>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000683 _LIBCPP_INLINE_VISIBILITY
Marshall Clowea52cc42017-01-24 23:09:12 +0000684#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +0000685 reference emplace_back(_Args&&... __args);
Marshall Clowea52cc42017-01-24 23:09:12 +0000686#else
687 void emplace_back(_Args&&... __args);
688#endif
Eric Fiseliered9e9362017-04-16 02:40:45 +0000689#endif // !_LIBCPP_CXX03_LANG
690
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000691 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000692 void pop_back();
693
694 iterator insert(const_iterator __position, const_reference __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000695
696#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000697 iterator insert(const_iterator __position, value_type&& __x);
698 template <class... _Args>
699 iterator emplace(const_iterator __position, _Args&&... __args);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000700#endif // !_LIBCPP_CXX03_LANG
701
Howard Hinnantc51e1022010-05-11 19:42:16 +0000702 iterator insert(const_iterator __position, size_type __n, const_reference __x);
703 template <class _InputIterator>
704 typename enable_if
705 <
706 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000707 !__is_forward_iterator<_InputIterator>::value &&
708 is_constructible<
709 value_type,
710 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000711 iterator
712 >::type
713 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
714 template <class _ForwardIterator>
715 typename enable_if
716 <
Howard Hinnant88010252013-03-28 17:44:32 +0000717 __is_forward_iterator<_ForwardIterator>::value &&
718 is_constructible<
719 value_type,
720 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000721 iterator
722 >::type
723 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiseliered9e9362017-04-16 02:40:45 +0000724
725#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000726 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000727 iterator insert(const_iterator __position, initializer_list<value_type> __il)
728 {return insert(__position, __il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +0000729#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000730
Howard Hinnantcf823322010-12-17 14:46:43 +0000731 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000732 iterator erase(const_iterator __first, const_iterator __last);
733
Howard Hinnant1c936782011-06-03 19:40:40 +0000734 _LIBCPP_INLINE_VISIBILITY
735 void clear() _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000736 {
Marshall Clow2cd9d372014-05-08 14:14:06 +0000737 size_type __old_size = size();
Howard Hinnant27e0e772011-09-14 18:33:51 +0000738 __base::clear();
Marshall Clow2cd9d372014-05-08 14:14:06 +0000739 __annotate_shrink(__old_size);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000740 __invalidate_all_iterators();
741 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000742
743 void resize(size_type __sz);
744 void resize(size_type __sz, const_reference __x);
745
Howard Hinnant1c936782011-06-03 19:40:40 +0000746 void swap(vector&)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000747#if _LIBCPP_STD_VER >= 14
Eric Fiselier69c51982016-12-28 06:06:09 +0000748 _NOEXCEPT_DEBUG;
Marshall Clow8982dcd2015-07-13 20:04:56 +0000749#else
Eric Fiselier69c51982016-12-28 06:06:09 +0000750 _NOEXCEPT_DEBUG_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +0000751 __is_nothrow_swappable<allocator_type>::value);
752#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000753
754 bool __invariants() const;
755
Howard Hinnanta47c6d52011-09-16 17:29:17 +0000756#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000757
758 bool __dereferenceable(const const_iterator* __i) const;
759 bool __decrementable(const const_iterator* __i) const;
760 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
761 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
762
Howard Hinnanta47c6d52011-09-16 17:29:17 +0000763#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000764
Howard Hinnantc51e1022010-05-11 19:42:16 +0000765private:
Howard Hinnantcf823322010-12-17 14:46:43 +0000766 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Eric Fiselier69c51982016-12-28 06:06:09 +0000767 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(pointer __new_last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000768 void allocate(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +0000769 void deallocate() _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000770 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
Howard Hinnanta5589382011-01-04 19:53:31 +0000771 void __construct_at_end(size_type __n);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000772 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000773 void __construct_at_end(size_type __n, const_reference __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000774 template <class _ForwardIterator>
775 typename enable_if
776 <
777 __is_forward_iterator<_ForwardIterator>::value,
778 void
779 >::type
Eric Fiselieracfe6f02015-03-31 16:54:19 +0000780 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000781 void __append(size_type __n);
782 void __append(size_type __n, const_reference __x);
Howard Hinnantcf823322010-12-17 14:46:43 +0000783 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000784 iterator __make_iter(pointer __p) _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000785 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000786 const_iterator __make_iter(const_pointer __p) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000787 void __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v);
788 pointer __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p);
789 void __move_range(pointer __from_s, pointer __from_e, pointer __to);
Howard Hinnant1c936782011-06-03 19:40:40 +0000790 void __move_assign(vector& __c, true_type)
791 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000792 void __move_assign(vector& __c, false_type)
793 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000794 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76053d72013-06-27 19:35:32 +0000795 void __destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000796 {
Eric Fiselier69c51982016-12-28 06:06:09 +0000797 __invalidate_iterators_past(__new_last);
Marshall Clow2cd9d372014-05-08 14:14:06 +0000798 size_type __old_size = size();
Howard Hinnant27e0e772011-09-14 18:33:51 +0000799 __base::__destruct_at_end(__new_last);
Marshall Clow2cd9d372014-05-08 14:14:06 +0000800 __annotate_shrink(__old_size);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000801 }
Eric Fiseliered9e9362017-04-16 02:40:45 +0000802
803#ifndef _LIBCPP_CXX03_LANG
804 template <class _Up> void __push_back_slow_path(_Up&& __x);
805
Howard Hinnantb6c49562012-02-26 15:30:12 +0000806 template <class... _Args>
Eric Fiseliered9e9362017-04-16 02:40:45 +0000807 void __emplace_back_slow_path(_Args&&... __args);
808#else
809 template <class _Up> void __push_back_slow_path(_Up& __x);
Howard Hinnantb6c49562012-02-26 15:30:12 +0000810#endif
Eric Fiseliered9e9362017-04-16 02:40:45 +0000811
Marshall Clow2cd9d372014-05-08 14:14:06 +0000812 // The following functions are no-ops outside of AddressSanitizer mode.
813 // We call annotatations only for the default Allocator because other allocators
814 // may not meet the AddressSanitizer alignment constraints.
815 // See the documentation for __sanitizer_annotate_contiguous_container for more details.
Marshall Clow2cd9d372014-05-08 14:14:06 +0000816#ifndef _LIBCPP_HAS_NO_ASAN
Eric Fiselier6003c772016-12-23 23:37:52 +0000817 void __annotate_contiguous_container(const void *__beg, const void *__end,
818 const void *__old_mid,
819 const void *__new_mid) const
820 {
821
Marshall Clow2cd9d372014-05-08 14:14:06 +0000822 if (__beg && is_same<allocator_type, __default_allocator_type>::value)
823 __sanitizer_annotate_contiguous_container(__beg, __end, __old_mid, __new_mid);
Marshall Clow2cd9d372014-05-08 14:14:06 +0000824 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000825#else
826 _LIBCPP_INLINE_VISIBILITY
827 void __annotate_contiguous_container(const void*, const void*, const void*,
828 const void*) const {}
829#endif
830 _LIBCPP_INLINE_VISIBILITY
831 void __annotate_new(size_type __current_size) const {
Marshall Clow2cd9d372014-05-08 14:14:06 +0000832 __annotate_contiguous_container(data(), data() + capacity(),
833 data() + capacity(), data() + __current_size);
834 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000835
836 _LIBCPP_INLINE_VISIBILITY
837 void __annotate_delete() const {
Marshall Clow2cd9d372014-05-08 14:14:06 +0000838 __annotate_contiguous_container(data(), data() + capacity(),
839 data() + size(), data() + capacity());
840 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000841
842 _LIBCPP_INLINE_VISIBILITY
Kostya Serebryany4963c252014-09-02 23:43:38 +0000843 void __annotate_increase(size_type __n) const
Marshall Clow2cd9d372014-05-08 14:14:06 +0000844 {
845 __annotate_contiguous_container(data(), data() + capacity(),
846 data() + size(), data() + size() + __n);
847 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000848
849 _LIBCPP_INLINE_VISIBILITY
Kostya Serebryany4963c252014-09-02 23:43:38 +0000850 void __annotate_shrink(size_type __old_size) const
Marshall Clow2cd9d372014-05-08 14:14:06 +0000851 {
852 __annotate_contiguous_container(data(), data() + capacity(),
853 data() + __old_size, data() + size());
854 }
Marshall Clowc78ffa52014-09-03 21:37:43 +0000855#ifndef _LIBCPP_HAS_NO_ASAN
Kostya Serebryany4963c252014-09-02 23:43:38 +0000856 // The annotation for size increase should happen before the actual increase,
857 // but if an exception is thrown after that the annotation has to be undone.
858 struct __RAII_IncreaseAnnotator {
859 __RAII_IncreaseAnnotator(const vector &__v, size_type __n = 1)
Eric Fiseliera7c043e2015-03-10 00:25:20 +0000860 : __commit(false), __v(__v), __old_size(__v.size() + __n) {
Kostya Serebryany4963c252014-09-02 23:43:38 +0000861 __v.__annotate_increase(__n);
862 }
863 void __done() { __commit = true; }
864 ~__RAII_IncreaseAnnotator() {
865 if (__commit) return;
Eric Fiseliera7c043e2015-03-10 00:25:20 +0000866 __v.__annotate_shrink(__old_size);
Kostya Serebryany4963c252014-09-02 23:43:38 +0000867 }
868 bool __commit;
Kostya Serebryany4963c252014-09-02 23:43:38 +0000869 const vector &__v;
Eric Fiseliera7c043e2015-03-10 00:25:20 +0000870 size_type __old_size;
Kostya Serebryany4963c252014-09-02 23:43:38 +0000871 };
Marshall Clowc78ffa52014-09-03 21:37:43 +0000872#else
873 struct __RAII_IncreaseAnnotator {
Eric Fiselier6003c772016-12-23 23:37:52 +0000874 _LIBCPP_INLINE_VISIBILITY
875 __RAII_IncreaseAnnotator(const vector &, size_type = 1) {}
876 _LIBCPP_INLINE_VISIBILITY void __done() {}
Marshall Clowc78ffa52014-09-03 21:37:43 +0000877 };
878#endif
879
Howard Hinnantc51e1022010-05-11 19:42:16 +0000880};
881
882template <class _Tp, class _Allocator>
883void
884vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v)
885{
Marshall Clow2cd9d372014-05-08 14:14:06 +0000886 __annotate_delete();
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000887 __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, this->__end_, __v.__begin_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000888 _VSTD::swap(this->__begin_, __v.__begin_);
889 _VSTD::swap(this->__end_, __v.__end_);
890 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000891 __v.__first_ = __v.__begin_;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000892 __annotate_new(size());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000893 __invalidate_all_iterators();
894}
895
896template <class _Tp, class _Allocator>
897typename vector<_Tp, _Allocator>::pointer
898vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p)
899{
Marshall Clow2cd9d372014-05-08 14:14:06 +0000900 __annotate_delete();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000901 pointer __r = __v.__begin_;
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000902 __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, __p, __v.__begin_);
903 __alloc_traits::__construct_forward(this->__alloc(), __p, this->__end_, __v.__end_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000904 _VSTD::swap(this->__begin_, __v.__begin_);
905 _VSTD::swap(this->__end_, __v.__end_);
906 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000907 __v.__first_ = __v.__begin_;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000908 __annotate_new(size());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000909 __invalidate_all_iterators();
910 return __r;
911}
912
913// Allocate space for __n objects
914// throws length_error if __n > max_size()
915// throws (probably bad_alloc) if memory run out
916// Precondition: __begin_ == __end_ == __end_cap() == 0
917// Precondition: __n > 0
918// Postcondition: capacity() == __n
919// Postcondition: size() == 0
920template <class _Tp, class _Allocator>
921void
922vector<_Tp, _Allocator>::allocate(size_type __n)
923{
924 if (__n > max_size())
925 this->__throw_length_error();
926 this->__begin_ = this->__end_ = __alloc_traits::allocate(this->__alloc(), __n);
927 this->__end_cap() = this->__begin_ + __n;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000928 __annotate_new(0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000929}
930
931template <class _Tp, class _Allocator>
932void
Howard Hinnant1c936782011-06-03 19:40:40 +0000933vector<_Tp, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000934{
Howard Hinnant76053d72013-06-27 19:35:32 +0000935 if (this->__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000936 {
937 clear();
938 __alloc_traits::deallocate(this->__alloc(), this->__begin_, capacity());
Howard Hinnant76053d72013-06-27 19:35:32 +0000939 this->__begin_ = this->__end_ = this->__end_cap() = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000940 }
941}
942
943template <class _Tp, class _Allocator>
944typename vector<_Tp, _Allocator>::size_type
Howard Hinnant1c936782011-06-03 19:40:40 +0000945vector<_Tp, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000946{
Eric Fiselierb5d9f442016-11-23 01:18:56 +0000947 return _VSTD::min<size_type>(__alloc_traits::max_size(this->__alloc()),
948 numeric_limits<difference_type>::max());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000949}
950
951// Precondition: __new_size > capacity()
952template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000953inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000954typename vector<_Tp, _Allocator>::size_type
955vector<_Tp, _Allocator>::__recommend(size_type __new_size) const
956{
957 const size_type __ms = max_size();
958 if (__new_size > __ms)
959 this->__throw_length_error();
960 const size_type __cap = capacity();
961 if (__cap >= __ms / 2)
962 return __ms;
Alexis Hunt991d29b2011-07-29 23:31:58 +0000963 return _VSTD::max<size_type>(2*__cap, __new_size);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000964}
965
966// Default constructs __n objects starting at __end_
967// throws if construction throws
968// Precondition: __n > 0
969// Precondition: size() + __n <= capacity()
970// Postcondition: size() == size() + __n
971template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000972void
973vector<_Tp, _Allocator>::__construct_at_end(size_type __n)
974{
Howard Hinnantc51e1022010-05-11 19:42:16 +0000975 allocator_type& __a = this->__alloc();
976 do
977 {
Kostya Serebryany4963c252014-09-02 23:43:38 +0000978 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000979 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000980 ++this->__end_;
981 --__n;
Kostya Serebryany4963c252014-09-02 23:43:38 +0000982 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000983 } while (__n > 0);
984}
985
Howard Hinnantc51e1022010-05-11 19:42:16 +0000986// Copy constructs __n objects starting at __end_ from __x
987// throws if construction throws
988// Precondition: __n > 0
989// Precondition: size() + __n <= capacity()
990// Postcondition: size() == old size() + __n
991// Postcondition: [i] == __x for all i in [size() - __n, __n)
992template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000993inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000994void
995vector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
996{
Howard Hinnantc51e1022010-05-11 19:42:16 +0000997 allocator_type& __a = this->__alloc();
998 do
999 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001000 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001001 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001002 ++this->__end_;
1003 --__n;
Kostya Serebryany4963c252014-09-02 23:43:38 +00001004 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001005 } while (__n > 0);
1006}
1007
1008template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001009template <class _ForwardIterator>
1010typename enable_if
1011<
1012 __is_forward_iterator<_ForwardIterator>::value,
1013 void
1014>::type
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001015vector<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001016{
1017 allocator_type& __a = this->__alloc();
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001018 __RAII_IncreaseAnnotator __annotator(*this, __n);
1019 __alloc_traits::__construct_range_forward(__a, __first, __last, this->__end_);
1020 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001021}
1022
1023// Default constructs __n objects starting at __end_
1024// throws if construction throws
1025// Postcondition: size() == size() + __n
Howard Hinnant1c936782011-06-03 19:40:40 +00001026// Exception safety: strong.
Howard Hinnantc51e1022010-05-11 19:42:16 +00001027template <class _Tp, class _Allocator>
1028void
1029vector<_Tp, _Allocator>::__append(size_type __n)
1030{
1031 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1032 this->__construct_at_end(__n);
1033 else
1034 {
1035 allocator_type& __a = this->__alloc();
1036 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1037 __v.__construct_at_end(__n);
1038 __swap_out_circular_buffer(__v);
1039 }
1040}
1041
1042// Default constructs __n objects starting at __end_
1043// throws if construction throws
1044// Postcondition: size() == size() + __n
Howard Hinnant1c936782011-06-03 19:40:40 +00001045// Exception safety: strong.
Howard Hinnantc51e1022010-05-11 19:42:16 +00001046template <class _Tp, class _Allocator>
1047void
1048vector<_Tp, _Allocator>::__append(size_type __n, const_reference __x)
1049{
1050 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1051 this->__construct_at_end(__n, __x);
1052 else
1053 {
1054 allocator_type& __a = this->__alloc();
1055 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1056 __v.__construct_at_end(__n, __x);
1057 __swap_out_circular_buffer(__v);
1058 }
1059}
1060
1061template <class _Tp, class _Allocator>
1062vector<_Tp, _Allocator>::vector(size_type __n)
1063{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001064#if _LIBCPP_DEBUG_LEVEL >= 2
1065 __get_db()->__insert_c(this);
1066#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001067 if (__n > 0)
1068 {
1069 allocate(__n);
1070 __construct_at_end(__n);
1071 }
1072}
1073
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00001074#if _LIBCPP_STD_VER > 11
1075template <class _Tp, class _Allocator>
1076vector<_Tp, _Allocator>::vector(size_type __n, const allocator_type& __a)
1077 : __base(__a)
1078{
1079#if _LIBCPP_DEBUG_LEVEL >= 2
1080 __get_db()->__insert_c(this);
1081#endif
1082 if (__n > 0)
1083 {
1084 allocate(__n);
1085 __construct_at_end(__n);
1086 }
1087}
1088#endif
1089
Howard Hinnantc51e1022010-05-11 19:42:16 +00001090template <class _Tp, class _Allocator>
1091vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x)
1092{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001093#if _LIBCPP_DEBUG_LEVEL >= 2
1094 __get_db()->__insert_c(this);
1095#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001096 if (__n > 0)
1097 {
1098 allocate(__n);
1099 __construct_at_end(__n, __x);
1100 }
1101}
1102
1103template <class _Tp, class _Allocator>
1104vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x, const allocator_type& __a)
1105 : __base(__a)
1106{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001107#if _LIBCPP_DEBUG_LEVEL >= 2
1108 __get_db()->__insert_c(this);
1109#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001110 if (__n > 0)
1111 {
1112 allocate(__n);
1113 __construct_at_end(__n, __x);
1114 }
1115}
1116
1117template <class _Tp, class _Allocator>
1118template <class _InputIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +00001119vector<_Tp, _Allocator>::vector(_InputIterator __first,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001120 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001121 !__is_forward_iterator<_InputIterator>::value &&
1122 is_constructible<
1123 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +00001124 typename iterator_traits<_InputIterator>::reference>::value,
1125 _InputIterator>::type __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001126{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001127#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001128 __get_db()->__insert_c(this);
1129#endif
Howard Hinnant9cd22302011-09-16 18:41:29 +00001130 for (; __first != __last; ++__first)
1131 push_back(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001132}
1133
1134template <class _Tp, class _Allocator>
1135template <class _InputIterator>
1136vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
1137 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001138 !__is_forward_iterator<_InputIterator>::value &&
1139 is_constructible<
1140 value_type,
1141 typename iterator_traits<_InputIterator>::reference>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001142 : __base(__a)
1143{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001144#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001145 __get_db()->__insert_c(this);
1146#endif
Howard Hinnant9cd22302011-09-16 18:41:29 +00001147 for (; __first != __last; ++__first)
1148 push_back(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001149}
1150
1151template <class _Tp, class _Allocator>
1152template <class _ForwardIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +00001153vector<_Tp, _Allocator>::vector(_ForwardIterator __first,
Howard Hinnant88010252013-03-28 17:44:32 +00001154 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
1155 is_constructible<
1156 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +00001157 typename iterator_traits<_ForwardIterator>::reference>::value,
1158 _ForwardIterator>::type __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001159{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001160#if _LIBCPP_DEBUG_LEVEL >= 2
1161 __get_db()->__insert_c(this);
1162#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001163 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001164 if (__n > 0)
1165 {
1166 allocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001167 __construct_at_end(__first, __last, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001168 }
1169}
1170
1171template <class _Tp, class _Allocator>
1172template <class _ForwardIterator>
1173vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Howard Hinnant88010252013-03-28 17:44:32 +00001174 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
1175 is_constructible<
1176 value_type,
1177 typename iterator_traits<_ForwardIterator>::reference>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001178 : __base(__a)
1179{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001180#if _LIBCPP_DEBUG_LEVEL >= 2
1181 __get_db()->__insert_c(this);
1182#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001183 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001184 if (__n > 0)
1185 {
1186 allocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001187 __construct_at_end(__first, __last, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001188 }
1189}
1190
1191template <class _Tp, class _Allocator>
1192vector<_Tp, _Allocator>::vector(const vector& __x)
1193 : __base(__alloc_traits::select_on_container_copy_construction(__x.__alloc()))
1194{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001195#if _LIBCPP_DEBUG_LEVEL >= 2
1196 __get_db()->__insert_c(this);
1197#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001198 size_type __n = __x.size();
1199 if (__n > 0)
1200 {
1201 allocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001202 __construct_at_end(__x.__begin_, __x.__end_, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001203 }
1204}
1205
1206template <class _Tp, class _Allocator>
1207vector<_Tp, _Allocator>::vector(const vector& __x, const allocator_type& __a)
1208 : __base(__a)
1209{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001210#if _LIBCPP_DEBUG_LEVEL >= 2
1211 __get_db()->__insert_c(this);
1212#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001213 size_type __n = __x.size();
1214 if (__n > 0)
1215 {
1216 allocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001217 __construct_at_end(__x.__begin_, __x.__end_, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001218 }
1219}
1220
Eric Fiseliered9e9362017-04-16 02:40:45 +00001221#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001222
1223template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001224inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001225vector<_Tp, _Allocator>::vector(vector&& __x)
Marshall Clowe5108202015-07-14 14:46:32 +00001226#if _LIBCPP_STD_VER > 14
1227 _NOEXCEPT
1228#else
Howard Hinnant1c936782011-06-03 19:40:40 +00001229 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowe5108202015-07-14 14:46:32 +00001230#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001231 : __base(_VSTD::move(__x.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001232{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001233#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001234 __get_db()->__insert_c(this);
Howard Hinnanta1e60cd2011-09-19 16:34:29 +00001235 __get_db()->swap(this, &__x);
Howard Hinnant27e0e772011-09-14 18:33:51 +00001236#endif
Howard Hinnant9cd22302011-09-16 18:41:29 +00001237 this->__begin_ = __x.__begin_;
1238 this->__end_ = __x.__end_;
1239 this->__end_cap() = __x.__end_cap();
Howard Hinnant76053d72013-06-27 19:35:32 +00001240 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001241}
1242
1243template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001244inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001245vector<_Tp, _Allocator>::vector(vector&& __x, const allocator_type& __a)
1246 : __base(__a)
1247{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001248#if _LIBCPP_DEBUG_LEVEL >= 2
1249 __get_db()->__insert_c(this);
1250#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001251 if (__a == __x.__alloc())
1252 {
1253 this->__begin_ = __x.__begin_;
1254 this->__end_ = __x.__end_;
1255 this->__end_cap() = __x.__end_cap();
1256 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnanta1e60cd2011-09-19 16:34:29 +00001257#if _LIBCPP_DEBUG_LEVEL >= 2
1258 __get_db()->swap(this, &__x);
1259#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001260 }
1261 else
1262 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001263 typedef move_iterator<iterator> _Ip;
1264 assign(_Ip(__x.begin()), _Ip(__x.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001265 }
1266}
1267
1268template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001269inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001270vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il)
1271{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001272#if _LIBCPP_DEBUG_LEVEL >= 2
1273 __get_db()->__insert_c(this);
1274#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001275 if (__il.size() > 0)
1276 {
1277 allocate(__il.size());
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001278 __construct_at_end(__il.begin(), __il.end(), __il.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001279 }
1280}
1281
1282template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001283inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001284vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
1285 : __base(__a)
1286{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001287#if _LIBCPP_DEBUG_LEVEL >= 2
1288 __get_db()->__insert_c(this);
1289#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001290 if (__il.size() > 0)
1291 {
1292 allocate(__il.size());
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001293 __construct_at_end(__il.begin(), __il.end(), __il.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001294 }
1295}
1296
1297template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001298inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001299vector<_Tp, _Allocator>&
1300vector<_Tp, _Allocator>::operator=(vector&& __x)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001301 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001302{
1303 __move_assign(__x, integral_constant<bool,
1304 __alloc_traits::propagate_on_container_move_assignment::value>());
1305 return *this;
1306}
1307
1308template <class _Tp, class _Allocator>
1309void
1310vector<_Tp, _Allocator>::__move_assign(vector& __c, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001311 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001312{
1313 if (__base::__alloc() != __c.__alloc())
1314 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001315 typedef move_iterator<iterator> _Ip;
1316 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001317 }
1318 else
1319 __move_assign(__c, true_type());
1320}
1321
1322template <class _Tp, class _Allocator>
1323void
1324vector<_Tp, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00001325 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001326{
1327 deallocate();
Marshall Clow136d45c2014-07-21 15:11:13 +00001328 __base::__move_assign_alloc(__c); // this can throw
Howard Hinnantc51e1022010-05-11 19:42:16 +00001329 this->__begin_ = __c.__begin_;
1330 this->__end_ = __c.__end_;
1331 this->__end_cap() = __c.__end_cap();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001332 __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
Howard Hinnanta1e60cd2011-09-19 16:34:29 +00001333#if _LIBCPP_DEBUG_LEVEL >= 2
1334 __get_db()->swap(this, &__c);
1335#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001336}
1337
Eric Fiseliered9e9362017-04-16 02:40:45 +00001338#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001339
1340template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001341inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001342vector<_Tp, _Allocator>&
1343vector<_Tp, _Allocator>::operator=(const vector& __x)
1344{
1345 if (this != &__x)
1346 {
1347 __base::__copy_assign_alloc(__x);
1348 assign(__x.__begin_, __x.__end_);
1349 }
1350 return *this;
1351}
1352
1353template <class _Tp, class _Allocator>
1354template <class _InputIterator>
1355typename enable_if
1356<
1357 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001358 !__is_forward_iterator<_InputIterator>::value &&
1359 is_constructible<
1360 _Tp,
1361 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001362 void
1363>::type
1364vector<_Tp, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
1365{
1366 clear();
1367 for (; __first != __last; ++__first)
1368 push_back(*__first);
1369}
1370
1371template <class _Tp, class _Allocator>
1372template <class _ForwardIterator>
1373typename enable_if
1374<
Howard Hinnant88010252013-03-28 17:44:32 +00001375 __is_forward_iterator<_ForwardIterator>::value &&
1376 is_constructible<
1377 _Tp,
1378 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001379 void
1380>::type
1381vector<_Tp, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
1382{
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001383 size_type __new_size = static_cast<size_type>(_VSTD::distance(__first, __last));
1384 if (__new_size <= capacity())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001385 {
1386 _ForwardIterator __mid = __last;
1387 bool __growing = false;
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001388 if (__new_size > size())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001389 {
1390 __growing = true;
1391 __mid = __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001392 _VSTD::advance(__mid, size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001393 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001394 pointer __m = _VSTD::copy(__first, __mid, this->__begin_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001395 if (__growing)
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001396 __construct_at_end(__mid, __last, __new_size - size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001397 else
1398 this->__destruct_at_end(__m);
1399 }
1400 else
1401 {
1402 deallocate();
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001403 allocate(__recommend(__new_size));
1404 __construct_at_end(__first, __last, __new_size);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001405 }
Eric Fiselier69c51982016-12-28 06:06:09 +00001406 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001407}
1408
1409template <class _Tp, class _Allocator>
1410void
1411vector<_Tp, _Allocator>::assign(size_type __n, const_reference __u)
1412{
1413 if (__n <= capacity())
1414 {
1415 size_type __s = size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001416 _VSTD::fill_n(this->__begin_, _VSTD::min(__n, __s), __u);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001417 if (__n > __s)
1418 __construct_at_end(__n - __s, __u);
1419 else
Howard Hinnant155c2af2010-05-24 17:49:41 +00001420 this->__destruct_at_end(this->__begin_ + __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001421 }
1422 else
1423 {
1424 deallocate();
1425 allocate(__recommend(static_cast<size_type>(__n)));
1426 __construct_at_end(__n, __u);
1427 }
Eric Fiselier69c51982016-12-28 06:06:09 +00001428 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001429}
1430
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001431template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001432inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001433typename vector<_Tp, _Allocator>::iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001434vector<_Tp, _Allocator>::__make_iter(pointer __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001435{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001436#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001437 return iterator(this, __p);
1438#else
1439 return iterator(__p);
1440#endif
1441}
1442
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001443template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001444inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001445typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001446vector<_Tp, _Allocator>::__make_iter(const_pointer __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001447{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001448#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001449 return const_iterator(this, __p);
1450#else
1451 return const_iterator(__p);
1452#endif
1453}
1454
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001455template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001456inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001457typename vector<_Tp, _Allocator>::iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001458vector<_Tp, _Allocator>::begin() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001459{
1460 return __make_iter(this->__begin_);
1461}
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>::const_iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001466vector<_Tp, _Allocator>::begin() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001467{
1468 return __make_iter(this->__begin_);
1469}
1470
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001471template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001472inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001473typename vector<_Tp, _Allocator>::iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001474vector<_Tp, _Allocator>::end() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001475{
1476 return __make_iter(this->__end_);
1477}
1478
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001479template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001480inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001481typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001482vector<_Tp, _Allocator>::end() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001483{
1484 return __make_iter(this->__end_);
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>::reference
1490vector<_Tp, _Allocator>::operator[](size_type __n)
1491{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001492 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001493 return this->__begin_[__n];
1494}
1495
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001496template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001497inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001498typename vector<_Tp, _Allocator>::const_reference
1499vector<_Tp, _Allocator>::operator[](size_type __n) const
1500{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001501 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001502 return this->__begin_[__n];
1503}
1504
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001505template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001506typename vector<_Tp, _Allocator>::reference
1507vector<_Tp, _Allocator>::at(size_type __n)
1508{
1509 if (__n >= size())
1510 this->__throw_out_of_range();
1511 return this->__begin_[__n];
1512}
1513
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001514template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001515typename vector<_Tp, _Allocator>::const_reference
1516vector<_Tp, _Allocator>::at(size_type __n) const
1517{
1518 if (__n >= size())
1519 this->__throw_out_of_range();
1520 return this->__begin_[__n];
1521}
1522
1523template <class _Tp, class _Allocator>
1524void
1525vector<_Tp, _Allocator>::reserve(size_type __n)
1526{
1527 if (__n > capacity())
1528 {
1529 allocator_type& __a = this->__alloc();
Howard Hinnant1c936782011-06-03 19:40:40 +00001530 __split_buffer<value_type, allocator_type&> __v(__n, size(), __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001531 __swap_out_circular_buffer(__v);
1532 }
1533}
1534
1535template <class _Tp, class _Allocator>
1536void
Howard Hinnant1c936782011-06-03 19:40:40 +00001537vector<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001538{
1539 if (capacity() > size())
1540 {
1541#ifndef _LIBCPP_NO_EXCEPTIONS
1542 try
1543 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001544#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001545 allocator_type& __a = this->__alloc();
Howard Hinnant1c936782011-06-03 19:40:40 +00001546 __split_buffer<value_type, allocator_type&> __v(size(), size(), __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001547 __swap_out_circular_buffer(__v);
1548#ifndef _LIBCPP_NO_EXCEPTIONS
1549 }
1550 catch (...)
1551 {
1552 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001553#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001554 }
1555}
1556
1557template <class _Tp, class _Allocator>
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001558template <class _Up>
1559void
Eric Fiseliered9e9362017-04-16 02:40:45 +00001560#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001561vector<_Tp, _Allocator>::__push_back_slow_path(_Up&& __x)
1562#else
1563vector<_Tp, _Allocator>::__push_back_slow_path(_Up& __x)
1564#endif
1565{
1566 allocator_type& __a = this->__alloc();
1567 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1568 // __v.push_back(_VSTD::forward<_Up>(__x));
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001569 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), _VSTD::forward<_Up>(__x));
1570 __v.__end_++;
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001571 __swap_out_circular_buffer(__v);
1572}
1573
1574template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001575inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001576void
1577vector<_Tp, _Allocator>::push_back(const_reference __x)
1578{
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001579 if (this->__end_ != this->__end_cap())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001580 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001581 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001582 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001583 _VSTD::__to_raw_pointer(this->__end_), __x);
Kostya Serebryany4963c252014-09-02 23:43:38 +00001584 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001585 ++this->__end_;
1586 }
1587 else
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001588 __push_back_slow_path(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001589}
1590
Eric Fiseliered9e9362017-04-16 02:40:45 +00001591#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001592
1593template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001594inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001595void
1596vector<_Tp, _Allocator>::push_back(value_type&& __x)
1597{
1598 if (this->__end_ < this->__end_cap())
1599 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001600 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001601 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001602 _VSTD::__to_raw_pointer(this->__end_),
1603 _VSTD::move(__x));
Kostya Serebryany4963c252014-09-02 23:43:38 +00001604 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001605 ++this->__end_;
1606 }
1607 else
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001608 __push_back_slow_path(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001609}
1610
1611template <class _Tp, class _Allocator>
1612template <class... _Args>
1613void
Howard Hinnantb6c49562012-02-26 15:30:12 +00001614vector<_Tp, _Allocator>::__emplace_back_slow_path(_Args&&... __args)
1615{
1616 allocator_type& __a = this->__alloc();
1617 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1618// __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001619 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), _VSTD::forward<_Args>(__args)...);
1620 __v.__end_++;
Howard Hinnantb6c49562012-02-26 15:30:12 +00001621 __swap_out_circular_buffer(__v);
1622}
1623
1624template <class _Tp, class _Allocator>
1625template <class... _Args>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001626inline
Marshall Clowea52cc42017-01-24 23:09:12 +00001627#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001628typename vector<_Tp, _Allocator>::reference
Marshall Clowea52cc42017-01-24 23:09:12 +00001629#else
1630void
1631#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001632vector<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1633{
1634 if (this->__end_ < this->__end_cap())
1635 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001636 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001637 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001638 _VSTD::__to_raw_pointer(this->__end_),
1639 _VSTD::forward<_Args>(__args)...);
Kostya Serebryany4963c252014-09-02 23:43:38 +00001640 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001641 ++this->__end_;
1642 }
1643 else
Howard Hinnantb6c49562012-02-26 15:30:12 +00001644 __emplace_back_slow_path(_VSTD::forward<_Args>(__args)...);
Marshall Clowea52cc42017-01-24 23:09:12 +00001645#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001646 return this->back();
Marshall Clowea52cc42017-01-24 23:09:12 +00001647#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001648}
1649
Eric Fiseliered9e9362017-04-16 02:40:45 +00001650#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001651
1652template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001653inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001654void
1655vector<_Tp, _Allocator>::pop_back()
1656{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001657 _LIBCPP_ASSERT(!empty(), "vector::pop_back called for empty vector");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001658 this->__destruct_at_end(this->__end_ - 1);
1659}
1660
1661template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001662inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001663typename vector<_Tp, _Allocator>::iterator
1664vector<_Tp, _Allocator>::erase(const_iterator __position)
1665{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001666#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001667 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1668 "vector::erase(iterator) called with an iterator not"
1669 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001670#endif
Howard Hinnant58d52d52013-03-25 22:12:26 +00001671 _LIBCPP_ASSERT(__position != end(),
1672 "vector::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnant76053d72013-06-27 19:35:32 +00001673 difference_type __ps = __position - cbegin();
1674 pointer __p = this->__begin_ + __ps;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001675 this->__destruct_at_end(_VSTD::move(__p + 1, this->__end_, __p));
Eric Fiselier69c51982016-12-28 06:06:09 +00001676 this->__invalidate_iterators_past(__p-1);
1677 iterator __r = __make_iter(__p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001678 return __r;
1679}
1680
1681template <class _Tp, class _Allocator>
1682typename vector<_Tp, _Allocator>::iterator
1683vector<_Tp, _Allocator>::erase(const_iterator __first, const_iterator __last)
1684{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001685#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001686 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
1687 "vector::erase(iterator, iterator) called with an iterator not"
1688 " referring to this vector");
Eric Fiselier69c51982016-12-28 06:06:09 +00001689 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__last) == this,
1690 "vector::erase(iterator, iterator) called with an iterator not"
1691 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001692#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001693 _LIBCPP_ASSERT(__first <= __last, "vector::erase(first, last) called with invalid range");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001694 pointer __p = this->__begin_ + (__first - begin());
Eric Fiselier69c51982016-12-28 06:06:09 +00001695 if (__first != __last) {
Howard Hinnantc580cc32013-04-18 15:02:57 +00001696 this->__destruct_at_end(_VSTD::move(__p + (__last - __first), this->__end_, __p));
Eric Fiselier69c51982016-12-28 06:06:09 +00001697 this->__invalidate_iterators_past(__p - 1);
1698 }
1699 iterator __r = __make_iter(__p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001700 return __r;
1701}
1702
1703template <class _Tp, class _Allocator>
1704void
1705vector<_Tp, _Allocator>::__move_range(pointer __from_s, pointer __from_e, pointer __to)
1706{
1707 pointer __old_last = this->__end_;
1708 difference_type __n = __old_last - __to;
1709 for (pointer __i = __from_s + __n; __i < __from_e; ++__i, ++this->__end_)
1710 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001711 _VSTD::__to_raw_pointer(this->__end_),
1712 _VSTD::move(*__i));
1713 _VSTD::move_backward(__from_s, __from_s + __n, __old_last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001714}
1715
1716template <class _Tp, class _Allocator>
1717typename vector<_Tp, _Allocator>::iterator
1718vector<_Tp, _Allocator>::insert(const_iterator __position, const_reference __x)
1719{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001720#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001721 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1722 "vector::insert(iterator, x) called with an iterator not"
1723 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001724#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001725 pointer __p = this->__begin_ + (__position - begin());
1726 if (this->__end_ < this->__end_cap())
1727 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001728 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001729 if (__p == this->__end_)
1730 {
1731 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001732 _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001733 ++this->__end_;
1734 }
1735 else
1736 {
1737 __move_range(__p, this->__end_, __p + 1);
1738 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1739 if (__p <= __xr && __xr < this->__end_)
1740 ++__xr;
1741 *__p = *__xr;
1742 }
Kostya Serebryany4963c252014-09-02 23:43:38 +00001743 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001744 }
1745 else
1746 {
1747 allocator_type& __a = this->__alloc();
1748 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
1749 __v.push_back(__x);
1750 __p = __swap_out_circular_buffer(__v, __p);
1751 }
1752 return __make_iter(__p);
1753}
1754
Eric Fiseliered9e9362017-04-16 02:40:45 +00001755#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001756
1757template <class _Tp, class _Allocator>
1758typename vector<_Tp, _Allocator>::iterator
1759vector<_Tp, _Allocator>::insert(const_iterator __position, value_type&& __x)
1760{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001761#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001762 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1763 "vector::insert(iterator, x) called with an iterator not"
1764 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001765#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001766 pointer __p = this->__begin_ + (__position - begin());
1767 if (this->__end_ < this->__end_cap())
1768 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001769 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001770 if (__p == this->__end_)
1771 {
1772 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001773 _VSTD::__to_raw_pointer(this->__end_),
1774 _VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001775 ++this->__end_;
1776 }
1777 else
1778 {
1779 __move_range(__p, this->__end_, __p + 1);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001780 *__p = _VSTD::move(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001781 }
Kostya Serebryany4963c252014-09-02 23:43:38 +00001782 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001783 }
1784 else
1785 {
1786 allocator_type& __a = this->__alloc();
1787 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001788 __v.push_back(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001789 __p = __swap_out_circular_buffer(__v, __p);
1790 }
1791 return __make_iter(__p);
1792}
1793
1794template <class _Tp, class _Allocator>
1795template <class... _Args>
1796typename vector<_Tp, _Allocator>::iterator
1797vector<_Tp, _Allocator>::emplace(const_iterator __position, _Args&&... __args)
1798{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001799#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001800 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1801 "vector::emplace(iterator, x) called with an iterator not"
1802 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001803#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001804 pointer __p = this->__begin_ + (__position - begin());
1805 if (this->__end_ < this->__end_cap())
1806 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001807 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001808 if (__p == this->__end_)
1809 {
1810 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001811 _VSTD::__to_raw_pointer(this->__end_),
1812 _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001813 ++this->__end_;
1814 }
1815 else
1816 {
Marshall Clowa591b9a2016-07-11 21:38:08 +00001817 __temp_value<value_type, _Allocator> __tmp(this->__alloc(), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001818 __move_range(__p, this->__end_, __p + 1);
Marshall Clowa591b9a2016-07-11 21:38:08 +00001819 *__p = _VSTD::move(__tmp.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001820 }
Kostya Serebryany4963c252014-09-02 23:43:38 +00001821 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001822 }
1823 else
1824 {
1825 allocator_type& __a = this->__alloc();
1826 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001827 __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001828 __p = __swap_out_circular_buffer(__v, __p);
1829 }
1830 return __make_iter(__p);
1831}
1832
Eric Fiseliered9e9362017-04-16 02:40:45 +00001833#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001834
1835template <class _Tp, class _Allocator>
1836typename vector<_Tp, _Allocator>::iterator
1837vector<_Tp, _Allocator>::insert(const_iterator __position, size_type __n, const_reference __x)
1838{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001839#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001840 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1841 "vector::insert(iterator, n, x) called with an iterator not"
1842 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001843#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001844 pointer __p = this->__begin_ + (__position - begin());
1845 if (__n > 0)
1846 {
1847 if (__n <= static_cast<size_type>(this->__end_cap() - this->__end_))
1848 {
1849 size_type __old_n = __n;
1850 pointer __old_last = this->__end_;
1851 if (__n > static_cast<size_type>(this->__end_ - __p))
1852 {
1853 size_type __cx = __n - (this->__end_ - __p);
1854 __construct_at_end(__cx, __x);
1855 __n -= __cx;
1856 }
1857 if (__n > 0)
1858 {
Eric Fiselier2a649652014-11-14 18:28:36 +00001859 __RAII_IncreaseAnnotator __annotator(*this, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001860 __move_range(__p, __old_last, __p + __old_n);
Kostya Serebryany4963c252014-09-02 23:43:38 +00001861 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001862 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1863 if (__p <= __xr && __xr < this->__end_)
1864 __xr += __old_n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001865 _VSTD::fill_n(__p, __n, *__xr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001866 }
1867 }
1868 else
1869 {
1870 allocator_type& __a = this->__alloc();
1871 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1872 __v.__construct_at_end(__n, __x);
1873 __p = __swap_out_circular_buffer(__v, __p);
1874 }
1875 }
1876 return __make_iter(__p);
1877}
1878
1879template <class _Tp, class _Allocator>
1880template <class _InputIterator>
1881typename enable_if
1882<
1883 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001884 !__is_forward_iterator<_InputIterator>::value &&
1885 is_constructible<
1886 _Tp,
1887 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001888 typename vector<_Tp, _Allocator>::iterator
1889>::type
1890vector<_Tp, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
1891{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001892#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001893 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1894 "vector::insert(iterator, range) called with an iterator not"
1895 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001896#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001897 difference_type __off = __position - begin();
1898 pointer __p = this->__begin_ + __off;
1899 allocator_type& __a = this->__alloc();
1900 pointer __old_last = this->__end_;
1901 for (; this->__end_ != this->__end_cap() && __first != __last; ++__first)
1902 {
Eric Fiselier489fd502015-07-18 18:22:12 +00001903 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001904 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001905 *__first);
1906 ++this->__end_;
Eric Fiselier489fd502015-07-18 18:22:12 +00001907 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001908 }
1909 __split_buffer<value_type, allocator_type&> __v(__a);
1910 if (__first != __last)
1911 {
1912#ifndef _LIBCPP_NO_EXCEPTIONS
1913 try
1914 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001915#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001916 __v.__construct_at_end(__first, __last);
1917 difference_type __old_size = __old_last - this->__begin_;
1918 difference_type __old_p = __p - this->__begin_;
1919 reserve(__recommend(size() + __v.size()));
1920 __p = this->__begin_ + __old_p;
1921 __old_last = this->__begin_ + __old_size;
1922#ifndef _LIBCPP_NO_EXCEPTIONS
1923 }
1924 catch (...)
1925 {
1926 erase(__make_iter(__old_last), end());
1927 throw;
1928 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001929#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001930 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001931 __p = _VSTD::rotate(__p, __old_last, this->__end_);
Howard Hinnant9cd22302011-09-16 18:41:29 +00001932 insert(__make_iter(__p), make_move_iterator(__v.begin()),
1933 make_move_iterator(__v.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001934 return begin() + __off;
1935}
1936
1937template <class _Tp, class _Allocator>
1938template <class _ForwardIterator>
1939typename enable_if
1940<
Howard Hinnant88010252013-03-28 17:44:32 +00001941 __is_forward_iterator<_ForwardIterator>::value &&
1942 is_constructible<
1943 _Tp,
1944 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001945 typename vector<_Tp, _Allocator>::iterator
1946>::type
1947vector<_Tp, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
1948{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001949#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001950 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1951 "vector::insert(iterator, range) called with an iterator not"
1952 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001953#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001954 pointer __p = this->__begin_ + (__position - begin());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001955 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001956 if (__n > 0)
1957 {
1958 if (__n <= this->__end_cap() - this->__end_)
1959 {
1960 size_type __old_n = __n;
1961 pointer __old_last = this->__end_;
1962 _ForwardIterator __m = __last;
1963 difference_type __dx = this->__end_ - __p;
1964 if (__n > __dx)
1965 {
1966 __m = __first;
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001967 difference_type __diff = this->__end_ - __p;
1968 _VSTD::advance(__m, __diff);
1969 __construct_at_end(__m, __last, __n - __diff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001970 __n = __dx;
1971 }
1972 if (__n > 0)
1973 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001974 __RAII_IncreaseAnnotator __annotator(*this, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001975 __move_range(__p, __old_last, __p + __old_n);
Kostya Serebryany4963c252014-09-02 23:43:38 +00001976 __annotator.__done();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001977 _VSTD::copy(__first, __m, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001978 }
1979 }
1980 else
1981 {
1982 allocator_type& __a = this->__alloc();
1983 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1984 __v.__construct_at_end(__first, __last);
1985 __p = __swap_out_circular_buffer(__v, __p);
1986 }
1987 }
1988 return __make_iter(__p);
1989}
1990
1991template <class _Tp, class _Allocator>
1992void
1993vector<_Tp, _Allocator>::resize(size_type __sz)
1994{
1995 size_type __cs = size();
1996 if (__cs < __sz)
1997 this->__append(__sz - __cs);
1998 else if (__cs > __sz)
1999 this->__destruct_at_end(this->__begin_ + __sz);
2000}
2001
2002template <class _Tp, class _Allocator>
2003void
2004vector<_Tp, _Allocator>::resize(size_type __sz, const_reference __x)
2005{
2006 size_type __cs = size();
2007 if (__cs < __sz)
2008 this->__append(__sz - __cs, __x);
2009 else if (__cs > __sz)
2010 this->__destruct_at_end(this->__begin_ + __sz);
2011}
2012
2013template <class _Tp, class _Allocator>
2014void
2015vector<_Tp, _Allocator>::swap(vector& __x)
Marshall Clow8982dcd2015-07-13 20:04:56 +00002016#if _LIBCPP_STD_VER >= 14
Eric Fiselier69c51982016-12-28 06:06:09 +00002017 _NOEXCEPT_DEBUG
Marshall Clow8982dcd2015-07-13 20:04:56 +00002018#else
Eric Fiselier69c51982016-12-28 06:06:09 +00002019 _NOEXCEPT_DEBUG_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00002020 __is_nothrow_swappable<allocator_type>::value)
2021#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002022{
Howard Hinnant27e0e772011-09-14 18:33:51 +00002023 _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
2024 this->__alloc() == __x.__alloc(),
2025 "vector::swap: Either propagate_on_container_swap must be true"
2026 " or the allocators must compare equal");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002027 _VSTD::swap(this->__begin_, __x.__begin_);
2028 _VSTD::swap(this->__end_, __x.__end_);
2029 _VSTD::swap(this->__end_cap(), __x.__end_cap());
Eric Fiseliered9e9362017-04-16 02:40:45 +00002030 __swap_allocator(this->__alloc(), __x.__alloc(),
Marshall Clow8982dcd2015-07-13 20:04:56 +00002031 integral_constant<bool,__alloc_traits::propagate_on_container_swap::value>());
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002032#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002033 __get_db()->swap(this, &__x);
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002034#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00002035}
2036
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002037template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002038bool
2039vector<_Tp, _Allocator>::__invariants() const
2040{
Howard Hinnant76053d72013-06-27 19:35:32 +00002041 if (this->__begin_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002042 {
Howard Hinnant76053d72013-06-27 19:35:32 +00002043 if (this->__end_ != nullptr || this->__end_cap() != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002044 return false;
2045 }
2046 else
2047 {
2048 if (this->__begin_ > this->__end_)
2049 return false;
2050 if (this->__begin_ == this->__end_cap())
2051 return false;
2052 if (this->__end_ > this->__end_cap())
2053 return false;
2054 }
2055 return true;
2056}
2057
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002058#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002059
Howard Hinnantc51e1022010-05-11 19:42:16 +00002060template <class _Tp, class _Allocator>
Howard Hinnant27e0e772011-09-14 18:33:51 +00002061bool
2062vector<_Tp, _Allocator>::__dereferenceable(const const_iterator* __i) const
2063{
2064 return this->__begin_ <= __i->base() && __i->base() < this->__end_;
2065}
2066
2067template <class _Tp, class _Allocator>
2068bool
2069vector<_Tp, _Allocator>::__decrementable(const const_iterator* __i) const
2070{
2071 return this->__begin_ < __i->base() && __i->base() <= this->__end_;
2072}
2073
2074template <class _Tp, class _Allocator>
2075bool
2076vector<_Tp, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
2077{
2078 const_pointer __p = __i->base() + __n;
2079 return this->__begin_ <= __p && __p <= this->__end_;
2080}
2081
2082template <class _Tp, class _Allocator>
2083bool
2084vector<_Tp, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
2085{
2086 const_pointer __p = __i->base() + __n;
2087 return this->__begin_ <= __p && __p < this->__end_;
2088}
2089
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002090#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002091
2092template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002093inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002094void
2095vector<_Tp, _Allocator>::__invalidate_all_iterators()
2096{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002097#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002098 __get_db()->__invalidate_all(this);
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002099#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00002100}
2101
Eric Fiselier69c51982016-12-28 06:06:09 +00002102
2103template <class _Tp, class _Allocator>
2104inline _LIBCPP_INLINE_VISIBILITY
2105void
2106vector<_Tp, _Allocator>::__invalidate_iterators_past(pointer __new_last) {
2107#if _LIBCPP_DEBUG_LEVEL >= 2
2108 __c_node* __c = __get_db()->__find_c_and_lock(this);
2109 for (__i_node** __p = __c->end_; __p != __c->beg_; ) {
2110 --__p;
2111 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
2112 if (__i->base() > __new_last) {
2113 (*__p)->__c_ = nullptr;
2114 if (--__c->end_ != __p)
2115 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
2116 }
2117 }
2118 __get_db()->unlock();
2119#else
2120 ((void)__new_last);
2121#endif
2122}
2123
Howard Hinnantc51e1022010-05-11 19:42:16 +00002124// vector<bool>
2125
2126template <class _Allocator> class vector<bool, _Allocator>;
2127
2128template <class _Allocator> struct hash<vector<bool, _Allocator> >;
2129
2130template <class _Allocator>
Howard Hinnantb4ef6482011-07-02 20:33:23 +00002131struct __has_storage_type<vector<bool, _Allocator> >
2132{
2133 static const bool value = true;
2134};
2135
2136template <class _Allocator>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002137class _LIBCPP_TEMPLATE_VIS vector<bool, _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002138 : private __vector_base_common<true>
2139{
2140public:
2141 typedef vector __self;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002142 typedef bool value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002143 typedef _Allocator allocator_type;
2144 typedef allocator_traits<allocator_type> __alloc_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002145 typedef typename __alloc_traits::size_type size_type;
2146 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant896b7622012-05-07 16:50:38 +00002147 typedef size_type __storage_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002148 typedef __bit_iterator<vector, false> pointer;
2149 typedef __bit_iterator<vector, true> const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002150 typedef pointer iterator;
2151 typedef const_pointer const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002152 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
2153 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002154
2155private:
Marshall Clow940e01c2015-04-07 05:21:38 +00002156 typedef typename __rebind_alloc_helper<__alloc_traits, __storage_type>::type __storage_allocator;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002157 typedef allocator_traits<__storage_allocator> __storage_traits;
2158 typedef typename __storage_traits::pointer __storage_pointer;
2159 typedef typename __storage_traits::const_pointer __const_storage_pointer;
2160
2161 __storage_pointer __begin_;
2162 size_type __size_;
2163 __compressed_pair<size_type, __storage_allocator> __cap_alloc_;
Howard Hinnant71d1c192011-07-09 15:50:42 +00002164public:
Howard Hinnantb4ef6482011-07-02 20:33:23 +00002165 typedef __bit_reference<vector> reference;
2166 typedef __bit_const_reference<vector> const_reference;
Howard Hinnant71d1c192011-07-09 15:50:42 +00002167private:
Howard Hinnant1c936782011-06-03 19:40:40 +00002168 _LIBCPP_INLINE_VISIBILITY
2169 size_type& __cap() _NOEXCEPT
2170 {return __cap_alloc_.first();}
2171 _LIBCPP_INLINE_VISIBILITY
2172 const size_type& __cap() const _NOEXCEPT
2173 {return __cap_alloc_.first();}
2174 _LIBCPP_INLINE_VISIBILITY
2175 __storage_allocator& __alloc() _NOEXCEPT
2176 {return __cap_alloc_.second();}
2177 _LIBCPP_INLINE_VISIBILITY
2178 const __storage_allocator& __alloc() const _NOEXCEPT
2179 {return __cap_alloc_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002180
2181 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
2182
Howard Hinnant1c936782011-06-03 19:40:40 +00002183 _LIBCPP_INLINE_VISIBILITY
2184 static size_type __internal_cap_to_external(size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002185 {return __n * __bits_per_word;}
Howard Hinnant1c936782011-06-03 19:40:40 +00002186 _LIBCPP_INLINE_VISIBILITY
2187 static size_type __external_cap_to_internal(size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002188 {return (__n - 1) / __bits_per_word + 1;}
2189
2190public:
Howard Hinnant1c936782011-06-03 19:40:40 +00002191 _LIBCPP_INLINE_VISIBILITY
Marshall Clowe546cbb2015-06-04 02:05:41 +00002192 vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clow8f282f42015-06-04 00:10:20 +00002193
2194 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
2195#if _LIBCPP_STD_VER <= 14
2196 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
2197#else
2198 _NOEXCEPT;
2199#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002200 ~vector();
2201 explicit vector(size_type __n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00002202#if _LIBCPP_STD_VER > 11
2203 explicit vector(size_type __n, const allocator_type& __a);
2204#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002205 vector(size_type __n, const value_type& __v);
2206 vector(size_type __n, const value_type& __v, const allocator_type& __a);
2207 template <class _InputIterator>
2208 vector(_InputIterator __first, _InputIterator __last,
2209 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2210 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2211 template <class _InputIterator>
2212 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2213 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2214 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2215 template <class _ForwardIterator>
2216 vector(_ForwardIterator __first, _ForwardIterator __last,
2217 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2218 template <class _ForwardIterator>
2219 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2220 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2221
2222 vector(const vector& __v);
2223 vector(const vector& __v, const allocator_type& __a);
2224 vector& operator=(const vector& __v);
Eric Fiseliered9e9362017-04-16 02:40:45 +00002225
2226#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002227 vector(initializer_list<value_type> __il);
2228 vector(initializer_list<value_type> __il, const allocator_type& __a);
2229
Howard Hinnant1c936782011-06-03 19:40:40 +00002230 _LIBCPP_INLINE_VISIBILITY
2231 vector(vector&& __v)
Marshall Clowe5108202015-07-14 14:46:32 +00002232#if _LIBCPP_STD_VER > 14
2233 _NOEXCEPT;
2234#else
Howard Hinnant1c936782011-06-03 19:40:40 +00002235 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowe5108202015-07-14 14:46:32 +00002236#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002237 vector(vector&& __v, const allocator_type& __a);
Howard Hinnant1c936782011-06-03 19:40:40 +00002238 _LIBCPP_INLINE_VISIBILITY
2239 vector& operator=(vector&& __v)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002240 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiseliered9e9362017-04-16 02:40:45 +00002241
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002242 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002243 vector& operator=(initializer_list<value_type> __il)
2244 {assign(__il.begin(), __il.end()); return *this;}
Eric Fiseliered9e9362017-04-16 02:40:45 +00002245
2246#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002247
2248 template <class _InputIterator>
2249 typename enable_if
2250 <
2251 __is_input_iterator<_InputIterator>::value &&
2252 !__is_forward_iterator<_InputIterator>::value,
2253 void
2254 >::type
2255 assign(_InputIterator __first, _InputIterator __last);
2256 template <class _ForwardIterator>
2257 typename enable_if
2258 <
2259 __is_forward_iterator<_ForwardIterator>::value,
2260 void
2261 >::type
2262 assign(_ForwardIterator __first, _ForwardIterator __last);
2263
2264 void assign(size_type __n, const value_type& __x);
Eric Fiseliered9e9362017-04-16 02:40:45 +00002265
2266#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002267 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002268 void assign(initializer_list<value_type> __il)
2269 {assign(__il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +00002270#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002271
Howard Hinnant1c936782011-06-03 19:40:40 +00002272 _LIBCPP_INLINE_VISIBILITY allocator_type get_allocator() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002273 {return allocator_type(this->__alloc());}
2274
Howard Hinnant1c936782011-06-03 19:40:40 +00002275 size_type max_size() const _NOEXCEPT;
2276 _LIBCPP_INLINE_VISIBILITY
2277 size_type capacity() const _NOEXCEPT
2278 {return __internal_cap_to_external(__cap());}
2279 _LIBCPP_INLINE_VISIBILITY
2280 size_type size() const _NOEXCEPT
2281 {return __size_;}
2282 _LIBCPP_INLINE_VISIBILITY
2283 bool empty() const _NOEXCEPT
2284 {return __size_ == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002285 void reserve(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +00002286 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002287
Howard Hinnant1c936782011-06-03 19:40:40 +00002288 _LIBCPP_INLINE_VISIBILITY
2289 iterator begin() _NOEXCEPT
2290 {return __make_iter(0);}
2291 _LIBCPP_INLINE_VISIBILITY
2292 const_iterator begin() const _NOEXCEPT
2293 {return __make_iter(0);}
2294 _LIBCPP_INLINE_VISIBILITY
2295 iterator end() _NOEXCEPT
2296 {return __make_iter(__size_);}
2297 _LIBCPP_INLINE_VISIBILITY
2298 const_iterator end() const _NOEXCEPT
2299 {return __make_iter(__size_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002300
Howard Hinnant1c936782011-06-03 19:40:40 +00002301 _LIBCPP_INLINE_VISIBILITY
2302 reverse_iterator rbegin() _NOEXCEPT
2303 {return reverse_iterator(end());}
2304 _LIBCPP_INLINE_VISIBILITY
2305 const_reverse_iterator rbegin() const _NOEXCEPT
2306 {return const_reverse_iterator(end());}
2307 _LIBCPP_INLINE_VISIBILITY
2308 reverse_iterator rend() _NOEXCEPT
2309 {return reverse_iterator(begin());}
2310 _LIBCPP_INLINE_VISIBILITY
2311 const_reverse_iterator rend() const _NOEXCEPT
2312 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002313
Howard Hinnant1c936782011-06-03 19:40:40 +00002314 _LIBCPP_INLINE_VISIBILITY
2315 const_iterator cbegin() const _NOEXCEPT
2316 {return __make_iter(0);}
2317 _LIBCPP_INLINE_VISIBILITY
2318 const_iterator cend() const _NOEXCEPT
2319 {return __make_iter(__size_);}
2320 _LIBCPP_INLINE_VISIBILITY
2321 const_reverse_iterator crbegin() const _NOEXCEPT
2322 {return rbegin();}
2323 _LIBCPP_INLINE_VISIBILITY
2324 const_reverse_iterator crend() const _NOEXCEPT
2325 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002326
2327 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) {return __make_ref(__n);}
2328 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const {return __make_ref(__n);}
2329 reference at(size_type __n);
2330 const_reference at(size_type __n) const;
2331
2332 _LIBCPP_INLINE_VISIBILITY reference front() {return __make_ref(0);}
2333 _LIBCPP_INLINE_VISIBILITY const_reference front() const {return __make_ref(0);}
2334 _LIBCPP_INLINE_VISIBILITY reference back() {return __make_ref(__size_ - 1);}
2335 _LIBCPP_INLINE_VISIBILITY const_reference back() const {return __make_ref(__size_ - 1);}
2336
2337 void push_back(const value_type& __x);
Marshall Clowc46bb8e2013-08-13 23:54:12 +00002338#if _LIBCPP_STD_VER > 11
2339 template <class... _Args>
Marshall Clowea52cc42017-01-24 23:09:12 +00002340#if _LIBCPP_STD_VER > 14
2341 _LIBCPP_INLINE_VISIBILITY reference emplace_back(_Args&&... __args)
2342#else
2343 _LIBCPP_INLINE_VISIBILITY void emplace_back(_Args&&... __args)
2344#endif
2345 {
Eric Fiselier34ba5b92016-07-21 03:20:17 +00002346 push_back ( value_type ( _VSTD::forward<_Args>(__args)... ));
Marshall Clowea52cc42017-01-24 23:09:12 +00002347#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00002348 return this->back();
Marshall Clowea52cc42017-01-24 23:09:12 +00002349#endif
Eric Fiselier34ba5b92016-07-21 03:20:17 +00002350 }
Marshall Clowc46bb8e2013-08-13 23:54:12 +00002351#endif
2352
Howard Hinnantc51e1022010-05-11 19:42:16 +00002353 _LIBCPP_INLINE_VISIBILITY void pop_back() {--__size_;}
2354
Marshall Clowc46bb8e2013-08-13 23:54:12 +00002355#if _LIBCPP_STD_VER > 11
2356 template <class... _Args>
2357 _LIBCPP_INLINE_VISIBILITY iterator emplace(const_iterator position, _Args&&... __args)
2358 { return insert ( position, value_type ( _VSTD::forward<_Args>(__args)... )); }
2359#endif
2360
Howard Hinnantc51e1022010-05-11 19:42:16 +00002361 iterator insert(const_iterator __position, const value_type& __x);
2362 iterator insert(const_iterator __position, size_type __n, const value_type& __x);
2363 iterator insert(const_iterator __position, size_type __n, const_reference __x);
2364 template <class _InputIterator>
2365 typename enable_if
2366 <
2367 __is_input_iterator <_InputIterator>::value &&
2368 !__is_forward_iterator<_InputIterator>::value,
2369 iterator
2370 >::type
2371 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
2372 template <class _ForwardIterator>
2373 typename enable_if
2374 <
2375 __is_forward_iterator<_ForwardIterator>::value,
2376 iterator
2377 >::type
2378 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiseliered9e9362017-04-16 02:40:45 +00002379
2380#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002381 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002382 iterator insert(const_iterator __position, initializer_list<value_type> __il)
2383 {return insert(__position, __il.begin(), __il.end());}
Eric Fiseliered9e9362017-04-16 02:40:45 +00002384#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002385
Howard Hinnantcf823322010-12-17 14:46:43 +00002386 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002387 iterator erase(const_iterator __first, const_iterator __last);
2388
Howard Hinnant1c936782011-06-03 19:40:40 +00002389 _LIBCPP_INLINE_VISIBILITY
2390 void clear() _NOEXCEPT {__size_ = 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002391
Howard Hinnant1c936782011-06-03 19:40:40 +00002392 void swap(vector&)
Marshall Clow8982dcd2015-07-13 20:04:56 +00002393#if _LIBCPP_STD_VER >= 14
2394 _NOEXCEPT;
2395#else
Eric Fiselier69c51982016-12-28 06:06:09 +00002396 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00002397 __is_nothrow_swappable<allocator_type>::value);
2398#endif
Marshall Clow89eb3822016-04-07 14:20:31 +00002399 static void swap(reference __x, reference __y) _NOEXCEPT { _VSTD::swap(__x, __y); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002400
2401 void resize(size_type __sz, value_type __x = false);
Howard Hinnant1c936782011-06-03 19:40:40 +00002402 void flip() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002403
2404 bool __invariants() const;
2405
2406private:
Howard Hinnantcf823322010-12-17 14:46:43 +00002407 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002408 void allocate(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +00002409 void deallocate() _NOEXCEPT;
2410 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantea382952013-08-14 18:00:20 +00002411 static size_type __align_it(size_type __new_size) _NOEXCEPT
Marshall Clow66f34152014-07-28 15:02:42 +00002412 {return __new_size + (__bits_per_word-1) & ~((size_type)__bits_per_word-1);};
Howard Hinnantcf823322010-12-17 14:46:43 +00002413 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
2414 _LIBCPP_INLINE_VISIBILITY void __construct_at_end(size_type __n, bool __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002415 template <class _ForwardIterator>
2416 typename enable_if
2417 <
2418 __is_forward_iterator<_ForwardIterator>::value,
2419 void
2420 >::type
2421 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
2422 void __append(size_type __n, const_reference __x);
Howard Hinnant1c936782011-06-03 19:40:40 +00002423 _LIBCPP_INLINE_VISIBILITY
2424 reference __make_ref(size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002425 {return reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnant1c936782011-06-03 19:40:40 +00002426 _LIBCPP_INLINE_VISIBILITY
2427 const_reference __make_ref(size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002428 {return const_reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnant1c936782011-06-03 19:40:40 +00002429 _LIBCPP_INLINE_VISIBILITY
2430 iterator __make_iter(size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002431 {return iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnant1c936782011-06-03 19:40:40 +00002432 _LIBCPP_INLINE_VISIBILITY
2433 const_iterator __make_iter(size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002434 {return const_iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnant1c936782011-06-03 19:40:40 +00002435 _LIBCPP_INLINE_VISIBILITY
2436 iterator __const_iterator_cast(const_iterator __p) _NOEXCEPT
Howard Hinnant76053d72013-06-27 19:35:32 +00002437 {return begin() + (__p - cbegin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002438
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002439 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002440 void __copy_assign_alloc(const vector& __v)
2441 {__copy_assign_alloc(__v, integral_constant<bool,
2442 __storage_traits::propagate_on_container_copy_assignment::value>());}
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002443 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002444 void __copy_assign_alloc(const vector& __c, true_type)
2445 {
2446 if (__alloc() != __c.__alloc())
2447 deallocate();
2448 __alloc() = __c.__alloc();
2449 }
2450
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002451 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00002452 void __copy_assign_alloc(const vector&, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002453 {}
2454
2455 void __move_assign(vector& __c, false_type);
Howard Hinnant1c936782011-06-03 19:40:40 +00002456 void __move_assign(vector& __c, true_type)
2457 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002458 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002459 void __move_assign_alloc(vector& __c)
Howard Hinnant1c936782011-06-03 19:40:40 +00002460 _NOEXCEPT_(
2461 !__storage_traits::propagate_on_container_move_assignment::value ||
2462 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002463 {__move_assign_alloc(__c, integral_constant<bool,
2464 __storage_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002465 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +00002466 void __move_assign_alloc(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002467 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002468 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002469 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002470 }
2471
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002472 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00002473 void __move_assign_alloc(vector&, false_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002474 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002475 {}
2476
Howard Hinnant1c936782011-06-03 19:40:40 +00002477 size_t __hash_code() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002478
2479 friend class __bit_reference<vector>;
2480 friend class __bit_const_reference<vector>;
2481 friend class __bit_iterator<vector, false>;
2482 friend class __bit_iterator<vector, true>;
Howard Hinnant4210a0f2012-08-17 17:10:18 +00002483 friend struct __bit_array<vector>;
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002484 friend struct _LIBCPP_TEMPLATE_VIS hash<vector>;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002485};
2486
2487template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002488inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002489void
2490vector<bool, _Allocator>::__invalidate_all_iterators()
2491{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002492}
2493
2494// Allocate space for __n objects
2495// throws length_error if __n > max_size()
2496// throws (probably bad_alloc) if memory run out
2497// Precondition: __begin_ == __end_ == __cap() == 0
2498// Precondition: __n > 0
2499// Postcondition: capacity() == __n
2500// Postcondition: size() == 0
2501template <class _Allocator>
2502void
2503vector<bool, _Allocator>::allocate(size_type __n)
2504{
2505 if (__n > max_size())
2506 this->__throw_length_error();
2507 __n = __external_cap_to_internal(__n);
2508 this->__begin_ = __storage_traits::allocate(this->__alloc(), __n);
2509 this->__size_ = 0;
2510 this->__cap() = __n;
2511}
2512
2513template <class _Allocator>
2514void
Howard Hinnant1c936782011-06-03 19:40:40 +00002515vector<bool, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002516{
Howard Hinnant76053d72013-06-27 19:35:32 +00002517 if (this->__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002518 {
2519 __storage_traits::deallocate(this->__alloc(), this->__begin_, __cap());
2520 __invalidate_all_iterators();
Howard Hinnant76053d72013-06-27 19:35:32 +00002521 this->__begin_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002522 this->__size_ = this->__cap() = 0;
2523 }
2524}
2525
2526template <class _Allocator>
2527typename vector<bool, _Allocator>::size_type
Howard Hinnant1c936782011-06-03 19:40:40 +00002528vector<bool, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002529{
2530 size_type __amax = __storage_traits::max_size(__alloc());
2531 size_type __nmax = numeric_limits<size_type>::max() / 2; // end() >= begin(), always
2532 if (__nmax / __bits_per_word <= __amax)
2533 return __nmax;
2534 return __internal_cap_to_external(__amax);
2535}
2536
2537// Precondition: __new_size > capacity()
2538template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002539inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002540typename vector<bool, _Allocator>::size_type
2541vector<bool, _Allocator>::__recommend(size_type __new_size) const
2542{
2543 const size_type __ms = max_size();
2544 if (__new_size > __ms)
2545 this->__throw_length_error();
2546 const size_type __cap = capacity();
2547 if (__cap >= __ms / 2)
2548 return __ms;
Howard Hinnantea382952013-08-14 18:00:20 +00002549 return _VSTD::max(2*__cap, __align_it(__new_size));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002550}
2551
2552// Default constructs __n objects starting at __end_
2553// Precondition: __n > 0
2554// Precondition: size() + __n <= capacity()
2555// Postcondition: size() == size() + __n
2556template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002557inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002558void
2559vector<bool, _Allocator>::__construct_at_end(size_type __n, bool __x)
2560{
2561 size_type __old_size = this->__size_;
2562 this->__size_ += __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002563 _VSTD::fill_n(__make_iter(__old_size), __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002564}
2565
2566template <class _Allocator>
2567template <class _ForwardIterator>
2568typename enable_if
2569<
2570 __is_forward_iterator<_ForwardIterator>::value,
2571 void
2572>::type
2573vector<bool, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
2574{
2575 size_type __old_size = this->__size_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002576 this->__size_ += _VSTD::distance(__first, __last);
2577 _VSTD::copy(__first, __last, __make_iter(__old_size));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002578}
2579
2580template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002581inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002582vector<bool, _Allocator>::vector()
Marshall Clowe546cbb2015-06-04 02:05:41 +00002583 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant76053d72013-06-27 19:35:32 +00002584 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002585 __size_(0),
2586 __cap_alloc_(0)
2587{
2588}
2589
2590template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002591inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002592vector<bool, _Allocator>::vector(const allocator_type& __a)
Marshall Clow8f282f42015-06-04 00:10:20 +00002593#if _LIBCPP_STD_VER <= 14
2594 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
2595#else
2596 _NOEXCEPT
2597#endif
Howard Hinnant76053d72013-06-27 19:35:32 +00002598 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002599 __size_(0),
2600 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2601{
2602}
2603
2604template <class _Allocator>
2605vector<bool, _Allocator>::vector(size_type __n)
Howard Hinnant76053d72013-06-27 19:35:32 +00002606 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002607 __size_(0),
2608 __cap_alloc_(0)
2609{
2610 if (__n > 0)
2611 {
2612 allocate(__n);
2613 __construct_at_end(__n, false);
2614 }
2615}
2616
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00002617#if _LIBCPP_STD_VER > 11
2618template <class _Allocator>
2619vector<bool, _Allocator>::vector(size_type __n, const allocator_type& __a)
2620 : __begin_(nullptr),
2621 __size_(0),
2622 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2623{
2624 if (__n > 0)
2625 {
2626 allocate(__n);
2627 __construct_at_end(__n, false);
2628 }
2629}
2630#endif
2631
Howard Hinnantc51e1022010-05-11 19:42:16 +00002632template <class _Allocator>
2633vector<bool, _Allocator>::vector(size_type __n, const value_type& __x)
Howard Hinnant76053d72013-06-27 19:35:32 +00002634 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002635 __size_(0),
2636 __cap_alloc_(0)
2637{
2638 if (__n > 0)
2639 {
2640 allocate(__n);
2641 __construct_at_end(__n, __x);
2642 }
2643}
2644
2645template <class _Allocator>
2646vector<bool, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002647 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002648 __size_(0),
2649 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2650{
2651 if (__n > 0)
2652 {
2653 allocate(__n);
2654 __construct_at_end(__n, __x);
2655 }
2656}
2657
2658template <class _Allocator>
2659template <class _InputIterator>
2660vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last,
2661 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2662 !__is_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002663 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002664 __size_(0),
2665 __cap_alloc_(0)
2666{
2667#ifndef _LIBCPP_NO_EXCEPTIONS
2668 try
2669 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002670#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002671 for (; __first != __last; ++__first)
2672 push_back(*__first);
2673#ifndef _LIBCPP_NO_EXCEPTIONS
2674 }
2675 catch (...)
2676 {
Howard Hinnant76053d72013-06-27 19:35:32 +00002677 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002678 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2679 __invalidate_all_iterators();
2680 throw;
2681 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002682#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002683}
2684
2685template <class _Allocator>
2686template <class _InputIterator>
2687vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2688 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2689 !__is_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002690 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002691 __size_(0),
2692 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2693{
2694#ifndef _LIBCPP_NO_EXCEPTIONS
2695 try
2696 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002697#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002698 for (; __first != __last; ++__first)
2699 push_back(*__first);
2700#ifndef _LIBCPP_NO_EXCEPTIONS
2701 }
2702 catch (...)
2703 {
Howard Hinnant76053d72013-06-27 19:35:32 +00002704 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002705 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2706 __invalidate_all_iterators();
2707 throw;
2708 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002709#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002710}
2711
2712template <class _Allocator>
2713template <class _ForwardIterator>
2714vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last,
2715 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002716 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002717 __size_(0),
2718 __cap_alloc_(0)
2719{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002720 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002721 if (__n > 0)
2722 {
2723 allocate(__n);
2724 __construct_at_end(__first, __last);
2725 }
2726}
2727
2728template <class _Allocator>
2729template <class _ForwardIterator>
2730vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2731 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002732 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002733 __size_(0),
2734 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2735{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002736 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002737 if (__n > 0)
2738 {
2739 allocate(__n);
2740 __construct_at_end(__first, __last);
2741 }
2742}
2743
Eric Fiseliered9e9362017-04-16 02:40:45 +00002744#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002745
Howard Hinnantc51e1022010-05-11 19:42:16 +00002746template <class _Allocator>
2747vector<bool, _Allocator>::vector(initializer_list<value_type> __il)
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{
2752 size_type __n = static_cast<size_type>(__il.size());
2753 if (__n > 0)
2754 {
2755 allocate(__n);
2756 __construct_at_end(__il.begin(), __il.end());
2757 }
2758}
2759
2760template <class _Allocator>
2761vector<bool, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002762 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002763 __size_(0),
2764 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2765{
2766 size_type __n = static_cast<size_type>(__il.size());
2767 if (__n > 0)
2768 {
2769 allocate(__n);
2770 __construct_at_end(__il.begin(), __il.end());
2771 }
2772}
2773
Eric Fiseliered9e9362017-04-16 02:40:45 +00002774#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002775
Howard Hinnantc51e1022010-05-11 19:42:16 +00002776template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002777vector<bool, _Allocator>::~vector()
2778{
Howard Hinnant76053d72013-06-27 19:35:32 +00002779 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002780 __storage_traits::deallocate(__alloc(), __begin_, __cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002781 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002782}
2783
2784template <class _Allocator>
2785vector<bool, _Allocator>::vector(const vector& __v)
Howard Hinnant76053d72013-06-27 19:35:32 +00002786 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002787 __size_(0),
2788 __cap_alloc_(0, __storage_traits::select_on_container_copy_construction(__v.__alloc()))
2789{
2790 if (__v.size() > 0)
2791 {
2792 allocate(__v.size());
2793 __construct_at_end(__v.begin(), __v.end());
2794 }
2795}
2796
2797template <class _Allocator>
2798vector<bool, _Allocator>::vector(const vector& __v, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002799 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002800 __size_(0),
2801 __cap_alloc_(0, __a)
2802{
2803 if (__v.size() > 0)
2804 {
2805 allocate(__v.size());
2806 __construct_at_end(__v.begin(), __v.end());
2807 }
2808}
2809
2810template <class _Allocator>
2811vector<bool, _Allocator>&
2812vector<bool, _Allocator>::operator=(const vector& __v)
2813{
2814 if (this != &__v)
2815 {
2816 __copy_assign_alloc(__v);
2817 if (__v.__size_)
2818 {
2819 if (__v.__size_ > capacity())
2820 {
2821 deallocate();
2822 allocate(__v.__size_);
2823 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002824 _VSTD::copy(__v.__begin_, __v.__begin_ + __external_cap_to_internal(__v.__size_), __begin_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002825 }
2826 __size_ = __v.__size_;
2827 }
2828 return *this;
2829}
2830
Eric Fiseliered9e9362017-04-16 02:40:45 +00002831#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant74279a52010-09-04 23:28:19 +00002832
Howard Hinnantc51e1022010-05-11 19:42:16 +00002833template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002834inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002835vector<bool, _Allocator>::vector(vector&& __v)
Marshall Clowe5108202015-07-14 14:46:32 +00002836#if _LIBCPP_STD_VER > 14
2837 _NOEXCEPT
2838#else
Howard Hinnant1c936782011-06-03 19:40:40 +00002839 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowe5108202015-07-14 14:46:32 +00002840#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002841 : __begin_(__v.__begin_),
2842 __size_(__v.__size_),
2843 __cap_alloc_(__v.__cap_alloc_)
2844{
Howard Hinnant76053d72013-06-27 19:35:32 +00002845 __v.__begin_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002846 __v.__size_ = 0;
2847 __v.__cap() = 0;
2848}
2849
2850template <class _Allocator>
2851vector<bool, _Allocator>::vector(vector&& __v, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002852 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002853 __size_(0),
2854 __cap_alloc_(0, __a)
2855{
2856 if (__a == allocator_type(__v.__alloc()))
2857 {
2858 this->__begin_ = __v.__begin_;
2859 this->__size_ = __v.__size_;
2860 this->__cap() = __v.__cap();
2861 __v.__begin_ = nullptr;
2862 __v.__cap() = __v.__size_ = 0;
2863 }
2864 else if (__v.size() > 0)
2865 {
2866 allocate(__v.size());
2867 __construct_at_end(__v.begin(), __v.end());
2868 }
2869}
2870
2871template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002872inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002873vector<bool, _Allocator>&
2874vector<bool, _Allocator>::operator=(vector&& __v)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002875 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002876{
2877 __move_assign(__v, integral_constant<bool,
2878 __storage_traits::propagate_on_container_move_assignment::value>());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002879 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002880}
2881
2882template <class _Allocator>
2883void
2884vector<bool, _Allocator>::__move_assign(vector& __c, false_type)
2885{
2886 if (__alloc() != __c.__alloc())
2887 assign(__c.begin(), __c.end());
2888 else
2889 __move_assign(__c, true_type());
2890}
2891
2892template <class _Allocator>
2893void
2894vector<bool, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002895 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002896{
2897 deallocate();
Marshall Clowb4871fa2014-07-21 15:15:15 +00002898 __move_assign_alloc(__c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002899 this->__begin_ = __c.__begin_;
2900 this->__size_ = __c.__size_;
2901 this->__cap() = __c.__cap();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002902 __c.__begin_ = nullptr;
2903 __c.__cap() = __c.__size_ = 0;
2904}
Howard Hinnant74279a52010-09-04 23:28:19 +00002905
Eric Fiseliered9e9362017-04-16 02:40:45 +00002906#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002907
2908template <class _Allocator>
2909void
2910vector<bool, _Allocator>::assign(size_type __n, const value_type& __x)
2911{
2912 __size_ = 0;
2913 if (__n > 0)
2914 {
2915 size_type __c = capacity();
2916 if (__n <= __c)
2917 __size_ = __n;
2918 else
2919 {
2920 vector __v(__alloc());
2921 __v.reserve(__recommend(__n));
2922 __v.__size_ = __n;
2923 swap(__v);
2924 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002925 _VSTD::fill_n(begin(), __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002926 }
Eric Fiselier69c51982016-12-28 06:06:09 +00002927 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002928}
2929
2930template <class _Allocator>
2931template <class _InputIterator>
2932typename enable_if
2933<
2934 __is_input_iterator<_InputIterator>::value &&
2935 !__is_forward_iterator<_InputIterator>::value,
2936 void
2937>::type
2938vector<bool, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2939{
2940 clear();
2941 for (; __first != __last; ++__first)
2942 push_back(*__first);
2943}
2944
2945template <class _Allocator>
2946template <class _ForwardIterator>
2947typename enable_if
2948<
2949 __is_forward_iterator<_ForwardIterator>::value,
2950 void
2951>::type
2952vector<bool, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2953{
2954 clear();
Eric Fiselier6003c772016-12-23 23:37:52 +00002955 difference_type __ns = _VSTD::distance(__first, __last);
2956 _LIBCPP_ASSERT(__ns >= 0, "invalid range specified");
2957 const size_t __n = static_cast<size_type>(__ns);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002958 if (__n)
2959 {
2960 if (__n > capacity())
2961 {
2962 deallocate();
2963 allocate(__n);
2964 }
2965 __construct_at_end(__first, __last);
2966 }
2967}
2968
2969template <class _Allocator>
2970void
2971vector<bool, _Allocator>::reserve(size_type __n)
2972{
2973 if (__n > capacity())
2974 {
2975 vector __v(this->__alloc());
2976 __v.allocate(__n);
2977 __v.__construct_at_end(this->begin(), this->end());
2978 swap(__v);
2979 __invalidate_all_iterators();
2980 }
2981}
2982
2983template <class _Allocator>
2984void
Howard Hinnant1c936782011-06-03 19:40:40 +00002985vector<bool, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002986{
2987 if (__external_cap_to_internal(size()) > __cap())
2988 {
2989#ifndef _LIBCPP_NO_EXCEPTIONS
2990 try
2991 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002992#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002993 vector(*this, allocator_type(__alloc())).swap(*this);
2994#ifndef _LIBCPP_NO_EXCEPTIONS
2995 }
2996 catch (...)
2997 {
2998 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002999#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003000 }
3001}
3002
3003template <class _Allocator>
3004typename vector<bool, _Allocator>::reference
3005vector<bool, _Allocator>::at(size_type __n)
3006{
3007 if (__n >= size())
3008 this->__throw_out_of_range();
3009 return (*this)[__n];
3010}
3011
3012template <class _Allocator>
3013typename vector<bool, _Allocator>::const_reference
3014vector<bool, _Allocator>::at(size_type __n) const
3015{
3016 if (__n >= size())
3017 this->__throw_out_of_range();
3018 return (*this)[__n];
3019}
3020
3021template <class _Allocator>
3022void
3023vector<bool, _Allocator>::push_back(const value_type& __x)
3024{
3025 if (this->__size_ == this->capacity())
3026 reserve(__recommend(this->__size_ + 1));
3027 ++this->__size_;
3028 back() = __x;
3029}
3030
3031template <class _Allocator>
3032typename vector<bool, _Allocator>::iterator
3033vector<bool, _Allocator>::insert(const_iterator __position, const value_type& __x)
3034{
3035 iterator __r;
3036 if (size() < capacity())
3037 {
3038 const_iterator __old_end = end();
3039 ++__size_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003040 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003041 __r = __const_iterator_cast(__position);
3042 }
3043 else
3044 {
3045 vector __v(__alloc());
3046 __v.reserve(__recommend(__size_ + 1));
3047 __v.__size_ = __size_ + 1;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003048 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3049 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003050 swap(__v);
3051 }
3052 *__r = __x;
3053 return __r;
3054}
3055
3056template <class _Allocator>
3057typename vector<bool, _Allocator>::iterator
3058vector<bool, _Allocator>::insert(const_iterator __position, size_type __n, const value_type& __x)
3059{
3060 iterator __r;
3061 size_type __c = capacity();
3062 if (__n <= __c && size() <= __c - __n)
3063 {
3064 const_iterator __old_end = end();
3065 __size_ += __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003066 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003067 __r = __const_iterator_cast(__position);
3068 }
3069 else
3070 {
3071 vector __v(__alloc());
3072 __v.reserve(__recommend(__size_ + __n));
3073 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003074 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3075 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003076 swap(__v);
3077 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003078 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003079 return __r;
3080}
3081
3082template <class _Allocator>
3083template <class _InputIterator>
3084typename enable_if
3085<
3086 __is_input_iterator <_InputIterator>::value &&
3087 !__is_forward_iterator<_InputIterator>::value,
3088 typename vector<bool, _Allocator>::iterator
3089>::type
3090vector<bool, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
3091{
3092 difference_type __off = __position - begin();
3093 iterator __p = __const_iterator_cast(__position);
3094 iterator __old_end = end();
3095 for (; size() != capacity() && __first != __last; ++__first)
3096 {
3097 ++this->__size_;
3098 back() = *__first;
3099 }
3100 vector __v(__alloc());
3101 if (__first != __last)
3102 {
3103#ifndef _LIBCPP_NO_EXCEPTIONS
3104 try
3105 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003106#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003107 __v.assign(__first, __last);
3108 difference_type __old_size = static_cast<difference_type>(__old_end - begin());
3109 difference_type __old_p = __p - begin();
3110 reserve(__recommend(size() + __v.size()));
3111 __p = begin() + __old_p;
3112 __old_end = begin() + __old_size;
3113#ifndef _LIBCPP_NO_EXCEPTIONS
3114 }
3115 catch (...)
3116 {
3117 erase(__old_end, end());
3118 throw;
3119 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003120#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003121 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003122 __p = _VSTD::rotate(__p, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003123 insert(__p, __v.begin(), __v.end());
3124 return begin() + __off;
3125}
3126
3127template <class _Allocator>
3128template <class _ForwardIterator>
3129typename enable_if
3130<
3131 __is_forward_iterator<_ForwardIterator>::value,
3132 typename vector<bool, _Allocator>::iterator
3133>::type
3134vector<bool, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
3135{
Eric Fiselier654dd332016-12-11 05:31:00 +00003136 const difference_type __n_signed = _VSTD::distance(__first, __last);
3137 _LIBCPP_ASSERT(__n_signed >= 0, "invalid range specified");
3138 const size_type __n = static_cast<size_type>(__n_signed);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003139 iterator __r;
3140 size_type __c = capacity();
3141 if (__n <= __c && size() <= __c - __n)
3142 {
3143 const_iterator __old_end = end();
3144 __size_ += __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003145 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003146 __r = __const_iterator_cast(__position);
3147 }
3148 else
3149 {
3150 vector __v(__alloc());
3151 __v.reserve(__recommend(__size_ + __n));
3152 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003153 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3154 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003155 swap(__v);
3156 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003157 _VSTD::copy(__first, __last, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003158 return __r;
3159}
3160
3161template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003162inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003163typename vector<bool, _Allocator>::iterator
3164vector<bool, _Allocator>::erase(const_iterator __position)
3165{
3166 iterator __r = __const_iterator_cast(__position);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003167 _VSTD::copy(__position + 1, this->cend(), __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003168 --__size_;
3169 return __r;
3170}
3171
3172template <class _Allocator>
3173typename vector<bool, _Allocator>::iterator
3174vector<bool, _Allocator>::erase(const_iterator __first, const_iterator __last)
3175{
3176 iterator __r = __const_iterator_cast(__first);
3177 difference_type __d = __last - __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003178 _VSTD::copy(__last, this->cend(), __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003179 __size_ -= __d;
3180 return __r;
3181}
3182
3183template <class _Allocator>
3184void
3185vector<bool, _Allocator>::swap(vector& __x)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003186#if _LIBCPP_STD_VER >= 14
3187 _NOEXCEPT
3188#else
Eric Fiselier69c51982016-12-28 06:06:09 +00003189 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003190 __is_nothrow_swappable<allocator_type>::value)
3191#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003192{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003193 _VSTD::swap(this->__begin_, __x.__begin_);
3194 _VSTD::swap(this->__size_, __x.__size_);
3195 _VSTD::swap(this->__cap(), __x.__cap());
Eric Fiseliered9e9362017-04-16 02:40:45 +00003196 __swap_allocator(this->__alloc(), __x.__alloc(),
Marshall Clow8982dcd2015-07-13 20:04:56 +00003197 integral_constant<bool, __alloc_traits::propagate_on_container_swap::value>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003198}
3199
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003200template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003201void
3202vector<bool, _Allocator>::resize(size_type __sz, value_type __x)
3203{
3204 size_type __cs = size();
3205 if (__cs < __sz)
3206 {
3207 iterator __r;
3208 size_type __c = capacity();
3209 size_type __n = __sz - __cs;
3210 if (__n <= __c && __cs <= __c - __n)
3211 {
3212 __r = end();
3213 __size_ += __n;
3214 }
3215 else
3216 {
3217 vector __v(__alloc());
3218 __v.reserve(__recommend(__size_ + __n));
3219 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003220 __r = _VSTD::copy(cbegin(), cend(), __v.begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003221 swap(__v);
3222 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003223 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003224 }
3225 else
3226 __size_ = __sz;
3227}
3228
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003229template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003230void
Howard Hinnant1c936782011-06-03 19:40:40 +00003231vector<bool, _Allocator>::flip() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003232{
3233 // do middle whole words
3234 size_type __n = __size_;
3235 __storage_pointer __p = __begin_;
3236 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3237 *__p = ~*__p;
3238 // do last partial word
3239 if (__n > 0)
3240 {
3241 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3242 __storage_type __b = *__p & __m;
3243 *__p &= ~__m;
3244 *__p |= ~__b & __m;
3245 }
3246}
3247
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003248template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003249bool
3250vector<bool, _Allocator>::__invariants() const
3251{
Howard Hinnant76053d72013-06-27 19:35:32 +00003252 if (this->__begin_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003253 {
3254 if (this->__size_ != 0 || this->__cap() != 0)
3255 return false;
3256 }
3257 else
3258 {
3259 if (this->__cap() == 0)
3260 return false;
3261 if (this->__size_ > this->capacity())
3262 return false;
3263 }
3264 return true;
3265}
3266
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003267template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003268size_t
Howard Hinnant1c936782011-06-03 19:40:40 +00003269vector<bool, _Allocator>::__hash_code() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003270{
3271 size_t __h = 0;
3272 // do middle whole words
3273 size_type __n = __size_;
3274 __storage_pointer __p = __begin_;
3275 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3276 __h ^= *__p;
3277 // do last partial word
3278 if (__n > 0)
3279 {
3280 const __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3281 __h ^= *__p & __m;
3282 }
3283 return __h;
3284}
3285
3286template <class _Allocator>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003287struct _LIBCPP_TEMPLATE_VIS hash<vector<bool, _Allocator> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00003288 : public unary_function<vector<bool, _Allocator>, size_t>
3289{
Howard Hinnant1c265cd2010-09-23 18:58:28 +00003290 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +00003291 size_t operator()(const vector<bool, _Allocator>& __vec) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003292 {return __vec.__hash_code();}
3293};
3294
3295template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003296inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003297bool
3298operator==(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3299{
3300 const typename vector<_Tp, _Allocator>::size_type __sz = __x.size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003301 return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003302}
3303
3304template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003305inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003306bool
3307operator!=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3308{
3309 return !(__x == __y);
3310}
3311
3312template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003313inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003314bool
3315operator< (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3316{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003317 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003318}
3319
3320template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003321inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003322bool
3323operator> (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3324{
3325 return __y < __x;
3326}
3327
3328template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003329inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003330bool
3331operator>=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3332{
3333 return !(__x < __y);
3334}
3335
3336template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003337inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003338bool
3339operator<=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3340{
3341 return !(__y < __x);
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 +00003346void
3347swap(vector<_Tp, _Allocator>& __x, vector<_Tp, _Allocator>& __y)
Howard Hinnant1c936782011-06-03 19:40:40 +00003348 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00003349{
3350 __x.swap(__y);
3351}
3352
3353_LIBCPP_END_NAMESPACE_STD
3354
Eric Fiselierf4433a32017-05-31 22:07:49 +00003355_LIBCPP_POP_MACROS
3356
Howard Hinnantc51e1022010-05-11 19:42:16 +00003357#endif // _LIBCPP_VECTOR