blob: 81c514ee6b78bb57c17a065e0d08651990aa9a03 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===------------------------------ vector --------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-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 Hinnantbc8d3f92010-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 Hinnant324bb032010-08-22 00:02:43 +000020template <class T, class Allocator = allocator<T> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000021class vector
Howard Hinnant324bb032010-08-22 00:02:43 +000022{
23public:
24 typedef T value_type;
Howard Hinnantbc8d3f92010-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 Hinnantd1d27a42011-06-03 19:40:40 +000037 vector()
38 noexcept(is_nothrow_default_constructible<allocator_type>::value);
39 explicit vector(const allocator_type&);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000040 explicit vector(size_type n);
Marshall Clowa49a2c92013-09-14 00:47:59 +000041 explicit vector(size_type n, const allocator_type&); // C++14
Howard Hinnantbc8d3f92010-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 Hinnantd1d27a42011-06-03 19:40:40 +000046 vector(vector&& x)
47 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-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 Hinnantd1d27a42011-06-03 19:40:40 +000052 vector& operator=(vector&& x)
53 noexcept(
Marshall Clowaf961ed2015-08-18 18:57:00 +000054 allocator_type::propagate_on_container_move_assignment::value ||
55 allocator_type::is_always_equal::value); // C++17
Howard Hinnantbc8d3f92010-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 Hinnantd1d27a42011-06-03 19:40:40 +000062 allocator_type get_allocator() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000063
Howard Hinnantd1d27a42011-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 Hinnantbc8d3f92010-05-11 19:42:16 +000068
Howard Hinnantd1d27a42011-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 Hinnantbc8d3f92010-05-11 19:42:16 +000073
Howard Hinnantd1d27a42011-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 Hinnantbc8d3f92010-05-11 19:42:16 +000078
Howard Hinnantd1d27a42011-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 Hinnantbc8d3f92010-05-11 19:42:16 +000083 void reserve(size_type n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +000084 void shrink_to_fit() noexcept;
Howard Hinnantbc8d3f92010-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 Hinnantd1d27a42011-06-03 19:40:40 +000096 value_type* data() noexcept;
97 const value_type* data() const noexcept;
Howard Hinnantbc8d3f92010-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>
102 void emplace_back(Args&&... args);
103 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 Hinnantd1d27a42011-06-03 19:40:40 +0000116 void clear() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000117
118 void resize(size_type sz);
119 void resize(size_type sz, const value_type& c);
120
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000121 void swap(vector&)
Marshall Clow7d914d12015-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 Hinnantbc8d3f92010-05-11 19:42:16 +0000124
125 bool __invariants() const;
Howard Hinnant324bb032010-08-22 00:02:43 +0000126};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000127
Howard Hinnant324bb032010-08-22 00:02:43 +0000128template <class Allocator = allocator<T> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000129class vector<bool, Allocator>
Howard Hinnant324bb032010-08-22 00:02:43 +0000130{
131public:
132 typedef bool value_type;
Howard Hinnantbc8d3f92010-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 Hinnantd1d27a42011-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 Hinnantbc8d3f92010-05-11 19:42:16 +0000152 };
153
154 class const_reference
155 {
156 public:
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000157 const_reference(const reference&) noexcept;
158 operator bool() const noexcept;
159 const_iterator operator&() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000160 };
161
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000162 vector()
163 noexcept(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnant9cbee432011-09-02 20:42:31 +0000164 explicit vector(const allocator_type&);
Marshall Clowa49a2c92013-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 Hinnantbc8d3f92010-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 Hinnantd1d27a42011-06-03 19:40:40 +0000170 vector(vector&& x)
171 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-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 Hinnantd1d27a42011-06-03 19:40:40 +0000176 vector& operator=(vector&& x)
177 noexcept(
Marshall Clowaf961ed2015-08-18 18:57:00 +0000178 allocator_type::propagate_on_container_move_assignment::value ||
179 allocator_type::is_always_equal::value); // C++17
Howard Hinnantbc8d3f92010-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 Hinnantd1d27a42011-06-03 19:40:40 +0000186 allocator_type get_allocator() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000187
Howard Hinnantd1d27a42011-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 Hinnantbc8d3f92010-05-11 19:42:16 +0000192
Howard Hinnantd1d27a42011-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 Hinnantbc8d3f92010-05-11 19:42:16 +0000197
Howard Hinnantd1d27a42011-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 Hinnantbc8d3f92010-05-11 19:42:16 +0000202
Howard Hinnantd1d27a42011-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 Hinnantbc8d3f92010-05-11 19:42:16 +0000207 void reserve(size_type n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000208 void shrink_to_fit() noexcept;
Howard Hinnantbc8d3f92010-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 Clow198a2a52013-08-13 23:54:12 +0000221 template <class... Args> void emplace_back(Args&&... args); // C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000222 void pop_back();
223
Marshall Clow198a2a52013-08-13 23:54:12 +0000224 template <class... Args> iterator emplace(const_iterator position, Args&&... args); // C++14
Howard Hinnantbc8d3f92010-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 Hinnantd1d27a42011-06-03 19:40:40 +0000234 void clear() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000235
236 void resize(size_type sz);
237 void resize(size_type sz, value_type x);
238
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000239 void swap(vector&)
Marshall Clow7d914d12015-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 Hinnantd1d27a42011-06-03 19:40:40 +0000242 void flip() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000243
244 bool __invariants() const;
Howard Hinnant324bb032010-08-22 00:02:43 +0000245};
Howard Hinnantbc8d3f92010-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 Hinnantd1d27a42011-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 Hinnantbc8d3f92010-05-11 19:42:16 +0000259
260} // std
261
262*/
263
264#include <__config>
Eric Fiselierb3792282016-02-20 00:19:45 +0000265#include <iosfwd> // for forward declaration of vector
Howard Hinnantbc8d3f92010-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 Hinnantbc8d3f92010-05-11 19:42:16 +0000277
Howard Hinnant66c6f972011-11-29 16:45:27 +0000278#include <__undef_min_max>
279
Eric Fiselierb9536102014-08-10 23:53:08 +0000280#include <__debug>
Howard Hinnant8b00e6c2013-08-02 00:26:35 +0000281
Howard Hinnant08e17472011-10-17 20:05:10 +0000282#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000283#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000284#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000285
286_LIBCPP_BEGIN_NAMESPACE_STD
287
288template <bool>
289class __vector_base_common
290{
291protected:
292 _LIBCPP_ALWAYS_INLINE __vector_base_common() {}
293 void __throw_length_error() const;
294 void __throw_out_of_range() const;
295};
296
297template <bool __b>
298void
299__vector_base_common<__b>::__throw_length_error() const
300{
301#ifndef _LIBCPP_NO_EXCEPTIONS
302 throw length_error("vector");
303#else
304 assert(!"vector length_error");
305#endif
306}
307
308template <bool __b>
309void
310__vector_base_common<__b>::__throw_out_of_range() const
311{
312#ifndef _LIBCPP_NO_EXCEPTIONS
313 throw out_of_range("vector");
314#else
315 assert(!"vector out_of_range");
316#endif
317}
318
Howard Hinnante9df0a52013-08-01 18:17:34 +0000319#ifdef _LIBCPP_MSVC
Howard Hinnant78b68282011-10-22 20:59:45 +0000320#pragma warning( push )
321#pragma warning( disable: 4231 )
Howard Hinnante9df0a52013-08-01 18:17:34 +0000322#endif // _LIBCPP_MSVC
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000323_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_TYPE_VIS __vector_base_common<true>)
Howard Hinnante9df0a52013-08-01 18:17:34 +0000324#ifdef _LIBCPP_MSVC
Howard Hinnant78b68282011-10-22 20:59:45 +0000325#pragma warning( pop )
Howard Hinnante9df0a52013-08-01 18:17:34 +0000326#endif // _LIBCPP_MSVC
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000327
328template <class _Tp, class _Allocator>
329class __vector_base
330 : protected __vector_base_common<true>
331{
332protected:
Howard Hinnant324bb032010-08-22 00:02:43 +0000333 typedef _Tp value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000334 typedef _Allocator allocator_type;
335 typedef allocator_traits<allocator_type> __alloc_traits;
336 typedef value_type& reference;
337 typedef const value_type& const_reference;
338 typedef typename __alloc_traits::size_type size_type;
339 typedef typename __alloc_traits::difference_type difference_type;
340 typedef typename __alloc_traits::pointer pointer;
341 typedef typename __alloc_traits::const_pointer const_pointer;
342 typedef pointer iterator;
343 typedef const_pointer const_iterator;
344
345 pointer __begin_;
346 pointer __end_;
347 __compressed_pair<pointer, allocator_type> __end_cap_;
348
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000349 _LIBCPP_INLINE_VISIBILITY
350 allocator_type& __alloc() _NOEXCEPT
351 {return __end_cap_.second();}
352 _LIBCPP_INLINE_VISIBILITY
353 const allocator_type& __alloc() const _NOEXCEPT
354 {return __end_cap_.second();}
355 _LIBCPP_INLINE_VISIBILITY
356 pointer& __end_cap() _NOEXCEPT
357 {return __end_cap_.first();}
358 _LIBCPP_INLINE_VISIBILITY
359 const pointer& __end_cap() const _NOEXCEPT
360 {return __end_cap_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000361
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000362 _LIBCPP_INLINE_VISIBILITY
363 __vector_base()
364 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000365 _LIBCPP_INLINE_VISIBILITY __vector_base(const allocator_type& __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000366 ~__vector_base();
367
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000368 _LIBCPP_INLINE_VISIBILITY
369 void clear() _NOEXCEPT {__destruct_at_end(__begin_);}
370 _LIBCPP_INLINE_VISIBILITY
371 size_type capacity() const _NOEXCEPT
372 {return static_cast<size_type>(__end_cap() - __begin_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000373
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000374 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000375 void __destruct_at_end(pointer __new_last) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000376
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000377 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000378 void __copy_assign_alloc(const __vector_base& __c)
379 {__copy_assign_alloc(__c, integral_constant<bool,
380 __alloc_traits::propagate_on_container_copy_assignment::value>());}
381
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000382 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000383 void __move_assign_alloc(__vector_base& __c)
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000384 _NOEXCEPT_(
385 !__alloc_traits::propagate_on_container_move_assignment::value ||
386 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000387 {__move_assign_alloc(__c, integral_constant<bool,
388 __alloc_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000389private:
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000390 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000391 void __copy_assign_alloc(const __vector_base& __c, true_type)
392 {
393 if (__alloc() != __c.__alloc())
394 {
395 clear();
396 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
397 __begin_ = __end_ = __end_cap() = nullptr;
398 }
399 __alloc() = __c.__alloc();
400 }
401
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000402 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +0000403 void __copy_assign_alloc(const __vector_base&, false_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000404 {}
405
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000406 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31 +0000407 void __move_assign_alloc(__vector_base& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000408 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000409 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000410 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000411 }
412
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000413 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +0000414 void __move_assign_alloc(__vector_base&, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000415 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000416 {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000417};
418
419template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +0000420inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000421void
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000422__vector_base<_Tp, _Allocator>::__destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000423{
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000424 while (__new_last != __end_)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000425 __alloc_traits::destroy(__alloc(), _VSTD::__to_raw_pointer(--__end_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000426}
427
428template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +0000429inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000430__vector_base<_Tp, _Allocator>::__vector_base()
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000431 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000432 : __begin_(nullptr),
433 __end_(nullptr),
434 __end_cap_(nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000435{
436}
437
438template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +0000439inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000440__vector_base<_Tp, _Allocator>::__vector_base(const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000441 : __begin_(nullptr),
442 __end_(nullptr),
443 __end_cap_(nullptr, __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000444{
445}
446
447template <class _Tp, class _Allocator>
448__vector_base<_Tp, _Allocator>::~__vector_base()
449{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000450 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000451 {
452 clear();
453 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
454 }
455}
456
Eric Fiselierb3792282016-02-20 00:19:45 +0000457template <class _Tp, class _Allocator /* = allocator<_Tp> */>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000458class _LIBCPP_TYPE_VIS_ONLY vector
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000459 : private __vector_base<_Tp, _Allocator>
460{
461private:
462 typedef __vector_base<_Tp, _Allocator> __base;
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000463 typedef allocator<_Tp> __default_allocator_type;
Howard Hinnant324bb032010-08-22 00:02:43 +0000464public:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000465 typedef vector __self;
Howard Hinnant324bb032010-08-22 00:02:43 +0000466 typedef _Tp value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000467 typedef _Allocator allocator_type;
468 typedef typename __base::__alloc_traits __alloc_traits;
469 typedef typename __base::reference reference;
470 typedef typename __base::const_reference const_reference;
471 typedef typename __base::size_type size_type;
472 typedef typename __base::difference_type difference_type;
473 typedef typename __base::pointer pointer;
474 typedef typename __base::const_pointer const_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000475 typedef __wrap_iter<pointer> iterator;
476 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnant0949eed2011-06-30 21:18:19 +0000477 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
478 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000479
Howard Hinnant02d5e182013-03-26 19:04:56 +0000480 static_assert((is_same<typename allocator_type::value_type, value_type>::value),
481 "Allocator::value_type must be same type as value_type");
482
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000483 _LIBCPP_INLINE_VISIBILITY
Marshall Clowc912c0c2015-06-04 02:05:41 +0000484 vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant7a563db2011-09-14 18:33:51 +0000485 {
Howard Hinnantabe26282011-09-16 17:29:17 +0000486#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000487 __get_db()->__insert_c(this);
488#endif
489 }
490 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
Marshall Clow127db912015-06-04 00:10:20 +0000491#if _LIBCPP_STD_VER <= 14
492 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
493#else
494 _NOEXCEPT
495#endif
Howard Hinnant7a563db2011-09-14 18:33:51 +0000496 : __base(__a)
497 {
Howard Hinnantabe26282011-09-16 17:29:17 +0000498#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000499 __get_db()->__insert_c(this);
500#endif
501 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000502 explicit vector(size_type __n);
Marshall Clowa49a2c92013-09-14 00:47:59 +0000503#if _LIBCPP_STD_VER > 11
504 explicit vector(size_type __n, const allocator_type& __a);
505#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000506 vector(size_type __n, const_reference __x);
507 vector(size_type __n, const_reference __x, const allocator_type& __a);
508 template <class _InputIterator>
Howard Hinnantde589f22013-09-21 21:13:54 +0000509 vector(_InputIterator __first,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000510 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +0000511 !__is_forward_iterator<_InputIterator>::value &&
512 is_constructible<
513 value_type,
Howard Hinnantde589f22013-09-21 21:13:54 +0000514 typename iterator_traits<_InputIterator>::reference>::value,
515 _InputIterator>::type __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000516 template <class _InputIterator>
517 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
518 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +0000519 !__is_forward_iterator<_InputIterator>::value &&
520 is_constructible<
521 value_type,
522 typename iterator_traits<_InputIterator>::reference>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000523 template <class _ForwardIterator>
Howard Hinnantde589f22013-09-21 21:13:54 +0000524 vector(_ForwardIterator __first,
Howard Hinnant742fecb2013-03-28 17:44:32 +0000525 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
526 is_constructible<
527 value_type,
Howard Hinnantde589f22013-09-21 21:13:54 +0000528 typename iterator_traits<_ForwardIterator>::reference>::value,
529 _ForwardIterator>::type __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000530 template <class _ForwardIterator>
531 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Howard Hinnant742fecb2013-03-28 17:44:32 +0000532 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
533 is_constructible<
534 value_type,
535 typename iterator_traits<_ForwardIterator>::reference>::value>::type* = 0);
Howard Hinnante3e32912011-08-12 21:56:02 +0000536#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000537 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000538 vector(initializer_list<value_type> __il);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000539 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000540 vector(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnante3e32912011-08-12 21:56:02 +0000541#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantabe26282011-09-16 17:29:17 +0000542#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000543 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7a563db2011-09-14 18:33:51 +0000544 ~vector()
545 {
546 __get_db()->__erase_c(this);
547 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000548#endif
549
550 vector(const vector& __x);
551 vector(const vector& __x, const allocator_type& __a);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000552 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000553 vector& operator=(const vector& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000554#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000555 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000556 vector(vector&& __x)
Marshall Clow119ed472015-07-14 14:46:32 +0000557#if _LIBCPP_STD_VER > 14
558 _NOEXCEPT;
559#else
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000560 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clow119ed472015-07-14 14:46:32 +0000561#endif
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000562 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000563 vector(vector&& __x, const allocator_type& __a);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000564 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000565 vector& operator=(vector&& __x)
Marshall Clowaf961ed2015-08-18 18:57:00 +0000566 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Howard Hinnant73d21a42010-09-04 23:28:19 +0000567#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnante3e32912011-08-12 21:56:02 +0000568#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000569 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000570 vector& operator=(initializer_list<value_type> __il)
571 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnante3e32912011-08-12 21:56:02 +0000572#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000573
574 template <class _InputIterator>
575 typename enable_if
576 <
577 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +0000578 !__is_forward_iterator<_InputIterator>::value &&
579 is_constructible<
580 value_type,
581 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000582 void
583 >::type
584 assign(_InputIterator __first, _InputIterator __last);
585 template <class _ForwardIterator>
586 typename enable_if
587 <
Howard Hinnant742fecb2013-03-28 17:44:32 +0000588 __is_forward_iterator<_ForwardIterator>::value &&
589 is_constructible<
590 value_type,
591 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000592 void
593 >::type
594 assign(_ForwardIterator __first, _ForwardIterator __last);
595
596 void assign(size_type __n, const_reference __u);
Howard Hinnante3e32912011-08-12 21:56:02 +0000597#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000598 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000599 void assign(initializer_list<value_type> __il)
600 {assign(__il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +0000601#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000602
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000603 _LIBCPP_INLINE_VISIBILITY
604 allocator_type get_allocator() const _NOEXCEPT
605 {return this->__alloc();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000606
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000607 _LIBCPP_INLINE_VISIBILITY iterator begin() _NOEXCEPT;
608 _LIBCPP_INLINE_VISIBILITY const_iterator begin() const _NOEXCEPT;
609 _LIBCPP_INLINE_VISIBILITY iterator end() _NOEXCEPT;
610 _LIBCPP_INLINE_VISIBILITY const_iterator end() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000611
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000612 _LIBCPP_INLINE_VISIBILITY
613 reverse_iterator rbegin() _NOEXCEPT
614 {return reverse_iterator(end());}
615 _LIBCPP_INLINE_VISIBILITY
616 const_reverse_iterator rbegin() const _NOEXCEPT
617 {return const_reverse_iterator(end());}
618 _LIBCPP_INLINE_VISIBILITY
619 reverse_iterator rend() _NOEXCEPT
620 {return reverse_iterator(begin());}
621 _LIBCPP_INLINE_VISIBILITY
622 const_reverse_iterator rend() const _NOEXCEPT
623 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000624
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000625 _LIBCPP_INLINE_VISIBILITY
626 const_iterator cbegin() const _NOEXCEPT
627 {return begin();}
628 _LIBCPP_INLINE_VISIBILITY
629 const_iterator cend() const _NOEXCEPT
630 {return end();}
631 _LIBCPP_INLINE_VISIBILITY
632 const_reverse_iterator crbegin() const _NOEXCEPT
633 {return rbegin();}
634 _LIBCPP_INLINE_VISIBILITY
635 const_reverse_iterator crend() const _NOEXCEPT
636 {return rend();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000637
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000638 _LIBCPP_INLINE_VISIBILITY
639 size_type size() const _NOEXCEPT
640 {return static_cast<size_type>(this->__end_ - this->__begin_);}
641 _LIBCPP_INLINE_VISIBILITY
642 size_type capacity() const _NOEXCEPT
643 {return __base::capacity();}
644 _LIBCPP_INLINE_VISIBILITY
645 bool empty() const _NOEXCEPT
646 {return this->__begin_ == this->__end_;}
647 size_type max_size() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000648 void reserve(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000649 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000650
651 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n);
652 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const;
653 reference at(size_type __n);
654 const_reference at(size_type __n) const;
655
Howard Hinnant7a563db2011-09-14 18:33:51 +0000656 _LIBCPP_INLINE_VISIBILITY reference front()
657 {
658 _LIBCPP_ASSERT(!empty(), "front() called for empty vector");
659 return *this->__begin_;
660 }
661 _LIBCPP_INLINE_VISIBILITY const_reference front() const
662 {
663 _LIBCPP_ASSERT(!empty(), "front() called for empty vector");
664 return *this->__begin_;
665 }
666 _LIBCPP_INLINE_VISIBILITY reference back()
667 {
668 _LIBCPP_ASSERT(!empty(), "back() called for empty vector");
669 return *(this->__end_ - 1);
670 }
671 _LIBCPP_INLINE_VISIBILITY const_reference back() const
672 {
673 _LIBCPP_ASSERT(!empty(), "back() called for empty vector");
674 return *(this->__end_ - 1);
675 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000676
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000677 _LIBCPP_INLINE_VISIBILITY
678 value_type* data() _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +0000679 {return _VSTD::__to_raw_pointer(this->__begin_);}
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000680 _LIBCPP_INLINE_VISIBILITY
681 const value_type* data() const _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +0000682 {return _VSTD::__to_raw_pointer(this->__begin_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000683
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000684 _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000685#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000686 _LIBCPP_INLINE_VISIBILITY void push_back(value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000687#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000688 template <class... _Args>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000689 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000690 void emplace_back(_Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000691#endif // _LIBCPP_HAS_NO_VARIADICS
692#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000693 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000694 void pop_back();
695
696 iterator insert(const_iterator __position, const_reference __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000697#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000698 iterator insert(const_iterator __position, value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000699#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000700 template <class... _Args>
701 iterator emplace(const_iterator __position, _Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000702#endif // _LIBCPP_HAS_NO_VARIADICS
703#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000704 iterator insert(const_iterator __position, size_type __n, const_reference __x);
705 template <class _InputIterator>
706 typename enable_if
707 <
708 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +0000709 !__is_forward_iterator<_InputIterator>::value &&
710 is_constructible<
711 value_type,
712 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000713 iterator
714 >::type
715 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
716 template <class _ForwardIterator>
717 typename enable_if
718 <
Howard Hinnant742fecb2013-03-28 17:44:32 +0000719 __is_forward_iterator<_ForwardIterator>::value &&
720 is_constructible<
721 value_type,
722 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000723 iterator
724 >::type
725 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnante3e32912011-08-12 21:56:02 +0000726#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000727 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000728 iterator insert(const_iterator __position, initializer_list<value_type> __il)
729 {return insert(__position, __il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +0000730#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000731
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000732 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000733 iterator erase(const_iterator __first, const_iterator __last);
734
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000735 _LIBCPP_INLINE_VISIBILITY
736 void clear() _NOEXCEPT
Howard Hinnant7a563db2011-09-14 18:33:51 +0000737 {
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000738 size_type __old_size = size();
Howard Hinnant7a563db2011-09-14 18:33:51 +0000739 __base::clear();
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000740 __annotate_shrink(__old_size);
Howard Hinnant7a563db2011-09-14 18:33:51 +0000741 __invalidate_all_iterators();
742 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000743
744 void resize(size_type __sz);
745 void resize(size_type __sz, const_reference __x);
746
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000747 void swap(vector&)
Marshall Clow7d914d12015-07-13 20:04:56 +0000748#if _LIBCPP_STD_VER >= 14
749 _NOEXCEPT;
750#else
751 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
752 __is_nothrow_swappable<allocator_type>::value);
753#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000754
755 bool __invariants() const;
756
Howard Hinnantabe26282011-09-16 17:29:17 +0000757#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000758
759 bool __dereferenceable(const const_iterator* __i) const;
760 bool __decrementable(const const_iterator* __i) const;
761 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
762 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
763
Howard Hinnantabe26282011-09-16 17:29:17 +0000764#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000765
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000766private:
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000767 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000768 void allocate(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000769 void deallocate() _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000770 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
Howard Hinnant04240d92011-01-04 19:53:31 +0000771 void __construct_at_end(size_type __n);
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000772 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000773 void __construct_at_end(size_type __n, const_reference __x);
Howard Hinnantbc8d3f92010-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 Fiselier088ed9f2015-03-31 16:54:19 +0000780 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000781 void __append(size_type __n);
782 void __append(size_type __n, const_reference __x);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000783 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000784 iterator __make_iter(pointer __p) _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000785 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000786 const_iterator __make_iter(const_pointer __p) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-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 Hinnantd1d27a42011-06-03 19:40:40 +0000790 void __move_assign(vector& __c, true_type)
791 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Marshall Clowaf961ed2015-08-18 18:57:00 +0000792 void __move_assign(vector& __c, false_type)
793 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnant7a563db2011-09-14 18:33:51 +0000794 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000795 void __destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnant7a563db2011-09-14 18:33:51 +0000796 {
Howard Hinnantabe26282011-09-16 17:29:17 +0000797#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000798 __c_node* __c = __get_db()->__find_c_and_lock(this);
799 for (__i_node** __p = __c->end_; __p != __c->beg_; )
800 {
801 --__p;
802 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
803 if (__i->base() > __new_last)
804 {
805 (*__p)->__c_ = nullptr;
806 if (--__c->end_ != __p)
807 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
808 }
809 }
810 __get_db()->unlock();
811#endif
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000812 size_type __old_size = size();
Howard Hinnant7a563db2011-09-14 18:33:51 +0000813 __base::__destruct_at_end(__new_last);
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000814 __annotate_shrink(__old_size);
Howard Hinnant7a563db2011-09-14 18:33:51 +0000815 }
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000816 template <class _Up>
817 void
818#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
819 __push_back_slow_path(_Up&& __x);
820#else
821 __push_back_slow_path(_Up& __x);
822#endif
Howard Hinnant0438ea22012-02-26 15:30:12 +0000823#if !defined(_LIBCPP_HAS_NO_VARIADICS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
824 template <class... _Args>
825 void
826 __emplace_back_slow_path(_Args&&... __args);
827#endif
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000828 // The following functions are no-ops outside of AddressSanitizer mode.
829 // We call annotatations only for the default Allocator because other allocators
830 // may not meet the AddressSanitizer alignment constraints.
831 // See the documentation for __sanitizer_annotate_contiguous_container for more details.
832 void __annotate_contiguous_container
Kostya Serebryany497f9122014-09-02 23:43:38 +0000833 (const void *__beg, const void *__end, const void *__old_mid, const void *__new_mid) const
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000834 {
835#ifndef _LIBCPP_HAS_NO_ASAN
836 if (__beg && is_same<allocator_type, __default_allocator_type>::value)
837 __sanitizer_annotate_contiguous_container(__beg, __end, __old_mid, __new_mid);
838#endif
839 }
840
Kostya Serebryany497f9122014-09-02 23:43:38 +0000841 void __annotate_new(size_type __current_size) const
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000842 {
843 __annotate_contiguous_container(data(), data() + capacity(),
844 data() + capacity(), data() + __current_size);
845 }
Kostya Serebryany497f9122014-09-02 23:43:38 +0000846 void __annotate_delete() const
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000847 {
848 __annotate_contiguous_container(data(), data() + capacity(),
849 data() + size(), data() + capacity());
850 }
Kostya Serebryany497f9122014-09-02 23:43:38 +0000851 void __annotate_increase(size_type __n) const
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000852 {
853 __annotate_contiguous_container(data(), data() + capacity(),
854 data() + size(), data() + size() + __n);
855 }
Kostya Serebryany497f9122014-09-02 23:43:38 +0000856 void __annotate_shrink(size_type __old_size) const
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000857 {
858 __annotate_contiguous_container(data(), data() + capacity(),
859 data() + __old_size, data() + size());
860 }
Marshall Clow26f472d2014-09-03 21:37:43 +0000861#ifndef _LIBCPP_HAS_NO_ASAN
Kostya Serebryany497f9122014-09-02 23:43:38 +0000862 // The annotation for size increase should happen before the actual increase,
863 // but if an exception is thrown after that the annotation has to be undone.
864 struct __RAII_IncreaseAnnotator {
865 __RAII_IncreaseAnnotator(const vector &__v, size_type __n = 1)
Eric Fiselier9f4f2212015-03-10 00:25:20 +0000866 : __commit(false), __v(__v), __old_size(__v.size() + __n) {
Kostya Serebryany497f9122014-09-02 23:43:38 +0000867 __v.__annotate_increase(__n);
868 }
869 void __done() { __commit = true; }
870 ~__RAII_IncreaseAnnotator() {
871 if (__commit) return;
Eric Fiselier9f4f2212015-03-10 00:25:20 +0000872 __v.__annotate_shrink(__old_size);
Kostya Serebryany497f9122014-09-02 23:43:38 +0000873 }
874 bool __commit;
Kostya Serebryany497f9122014-09-02 23:43:38 +0000875 const vector &__v;
Eric Fiselier9f4f2212015-03-10 00:25:20 +0000876 size_type __old_size;
Kostya Serebryany497f9122014-09-02 23:43:38 +0000877 };
Marshall Clow26f472d2014-09-03 21:37:43 +0000878#else
879 struct __RAII_IncreaseAnnotator {
880 inline __RAII_IncreaseAnnotator(const vector &, size_type __n = 1) {}
881 inline void __done() {}
882 };
883#endif
884
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000885};
886
887template <class _Tp, class _Allocator>
888void
889vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v)
890{
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000891 __annotate_delete();
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000892 __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, this->__end_, __v.__begin_);
Howard Hinnant0949eed2011-06-30 21:18:19 +0000893 _VSTD::swap(this->__begin_, __v.__begin_);
894 _VSTD::swap(this->__end_, __v.__end_);
895 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000896 __v.__first_ = __v.__begin_;
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000897 __annotate_new(size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000898 __invalidate_all_iterators();
899}
900
901template <class _Tp, class _Allocator>
902typename vector<_Tp, _Allocator>::pointer
903vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p)
904{
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000905 __annotate_delete();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000906 pointer __r = __v.__begin_;
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000907 __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, __p, __v.__begin_);
908 __alloc_traits::__construct_forward(this->__alloc(), __p, this->__end_, __v.__end_);
Howard Hinnant0949eed2011-06-30 21:18:19 +0000909 _VSTD::swap(this->__begin_, __v.__begin_);
910 _VSTD::swap(this->__end_, __v.__end_);
911 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000912 __v.__first_ = __v.__begin_;
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000913 __annotate_new(size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000914 __invalidate_all_iterators();
915 return __r;
916}
917
918// Allocate space for __n objects
919// throws length_error if __n > max_size()
920// throws (probably bad_alloc) if memory run out
921// Precondition: __begin_ == __end_ == __end_cap() == 0
922// Precondition: __n > 0
923// Postcondition: capacity() == __n
924// Postcondition: size() == 0
925template <class _Tp, class _Allocator>
926void
927vector<_Tp, _Allocator>::allocate(size_type __n)
928{
929 if (__n > max_size())
930 this->__throw_length_error();
931 this->__begin_ = this->__end_ = __alloc_traits::allocate(this->__alloc(), __n);
932 this->__end_cap() = this->__begin_ + __n;
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000933 __annotate_new(0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000934}
935
936template <class _Tp, class _Allocator>
937void
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000938vector<_Tp, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000939{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000940 if (this->__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000941 {
942 clear();
943 __alloc_traits::deallocate(this->__alloc(), this->__begin_, capacity());
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000944 this->__begin_ = this->__end_ = this->__end_cap() = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000945 }
946}
947
948template <class _Tp, class _Allocator>
949typename vector<_Tp, _Allocator>::size_type
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000950vector<_Tp, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000951{
Sean Hunt110b8bf2011-07-29 23:31:58 +0000952 return _VSTD::min<size_type>(__alloc_traits::max_size(this->__alloc()), numeric_limits<size_type>::max() / 2); // end() >= begin(), always
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000953}
954
955// Precondition: __new_size > capacity()
956template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +0000957inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000958typename vector<_Tp, _Allocator>::size_type
959vector<_Tp, _Allocator>::__recommend(size_type __new_size) const
960{
961 const size_type __ms = max_size();
962 if (__new_size > __ms)
963 this->__throw_length_error();
964 const size_type __cap = capacity();
965 if (__cap >= __ms / 2)
966 return __ms;
Sean Hunt110b8bf2011-07-29 23:31:58 +0000967 return _VSTD::max<size_type>(2*__cap, __new_size);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000968}
969
970// Default constructs __n objects starting at __end_
971// throws if construction throws
972// Precondition: __n > 0
973// Precondition: size() + __n <= capacity()
974// Postcondition: size() == size() + __n
975template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000976void
977vector<_Tp, _Allocator>::__construct_at_end(size_type __n)
978{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000979 allocator_type& __a = this->__alloc();
980 do
981 {
Kostya Serebryany497f9122014-09-02 23:43:38 +0000982 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnant0949eed2011-06-30 21:18:19 +0000983 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000984 ++this->__end_;
985 --__n;
Kostya Serebryany497f9122014-09-02 23:43:38 +0000986 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000987 } while (__n > 0);
988}
989
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000990// Copy constructs __n objects starting at __end_ from __x
991// throws if construction throws
992// Precondition: __n > 0
993// Precondition: size() + __n <= capacity()
994// Postcondition: size() == old size() + __n
995// Postcondition: [i] == __x for all i in [size() - __n, __n)
996template <class _Tp, class _Allocator>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000997inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000998void
999vector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
1000{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001001 allocator_type& __a = this->__alloc();
1002 do
1003 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001004 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001005 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001006 ++this->__end_;
1007 --__n;
Kostya Serebryany497f9122014-09-02 23:43:38 +00001008 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001009 } while (__n > 0);
1010}
1011
1012template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001013template <class _ForwardIterator>
1014typename enable_if
1015<
1016 __is_forward_iterator<_ForwardIterator>::value,
1017 void
1018>::type
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001019vector<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001020{
1021 allocator_type& __a = this->__alloc();
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001022 __RAII_IncreaseAnnotator __annotator(*this, __n);
1023 __alloc_traits::__construct_range_forward(__a, __first, __last, this->__end_);
1024 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001025}
1026
1027// Default constructs __n objects starting at __end_
1028// throws if construction throws
1029// Postcondition: size() == size() + __n
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001030// Exception safety: strong.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001031template <class _Tp, class _Allocator>
1032void
1033vector<_Tp, _Allocator>::__append(size_type __n)
1034{
1035 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1036 this->__construct_at_end(__n);
1037 else
1038 {
1039 allocator_type& __a = this->__alloc();
1040 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1041 __v.__construct_at_end(__n);
1042 __swap_out_circular_buffer(__v);
1043 }
1044}
1045
1046// Default constructs __n objects starting at __end_
1047// throws if construction throws
1048// Postcondition: size() == size() + __n
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001049// Exception safety: strong.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001050template <class _Tp, class _Allocator>
1051void
1052vector<_Tp, _Allocator>::__append(size_type __n, const_reference __x)
1053{
1054 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1055 this->__construct_at_end(__n, __x);
1056 else
1057 {
1058 allocator_type& __a = this->__alloc();
1059 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1060 __v.__construct_at_end(__n, __x);
1061 __swap_out_circular_buffer(__v);
1062 }
1063}
1064
1065template <class _Tp, class _Allocator>
1066vector<_Tp, _Allocator>::vector(size_type __n)
1067{
Howard Hinnant0442b122011-09-16 18:41:29 +00001068#if _LIBCPP_DEBUG_LEVEL >= 2
1069 __get_db()->__insert_c(this);
1070#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001071 if (__n > 0)
1072 {
1073 allocate(__n);
1074 __construct_at_end(__n);
1075 }
1076}
1077
Marshall Clowa49a2c92013-09-14 00:47:59 +00001078#if _LIBCPP_STD_VER > 11
1079template <class _Tp, class _Allocator>
1080vector<_Tp, _Allocator>::vector(size_type __n, const allocator_type& __a)
1081 : __base(__a)
1082{
1083#if _LIBCPP_DEBUG_LEVEL >= 2
1084 __get_db()->__insert_c(this);
1085#endif
1086 if (__n > 0)
1087 {
1088 allocate(__n);
1089 __construct_at_end(__n);
1090 }
1091}
1092#endif
1093
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001094template <class _Tp, class _Allocator>
1095vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x)
1096{
Howard Hinnant0442b122011-09-16 18:41:29 +00001097#if _LIBCPP_DEBUG_LEVEL >= 2
1098 __get_db()->__insert_c(this);
1099#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001100 if (__n > 0)
1101 {
1102 allocate(__n);
1103 __construct_at_end(__n, __x);
1104 }
1105}
1106
1107template <class _Tp, class _Allocator>
1108vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x, const allocator_type& __a)
1109 : __base(__a)
1110{
Howard Hinnant0442b122011-09-16 18:41:29 +00001111#if _LIBCPP_DEBUG_LEVEL >= 2
1112 __get_db()->__insert_c(this);
1113#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001114 if (__n > 0)
1115 {
1116 allocate(__n);
1117 __construct_at_end(__n, __x);
1118 }
1119}
1120
1121template <class _Tp, class _Allocator>
1122template <class _InputIterator>
Howard Hinnantde589f22013-09-21 21:13:54 +00001123vector<_Tp, _Allocator>::vector(_InputIterator __first,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001124 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +00001125 !__is_forward_iterator<_InputIterator>::value &&
1126 is_constructible<
1127 value_type,
Howard Hinnantde589f22013-09-21 21:13:54 +00001128 typename iterator_traits<_InputIterator>::reference>::value,
1129 _InputIterator>::type __last)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001130{
Howard Hinnantabe26282011-09-16 17:29:17 +00001131#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001132 __get_db()->__insert_c(this);
1133#endif
Howard Hinnant0442b122011-09-16 18:41:29 +00001134 for (; __first != __last; ++__first)
1135 push_back(*__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001136}
1137
1138template <class _Tp, class _Allocator>
1139template <class _InputIterator>
1140vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
1141 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +00001142 !__is_forward_iterator<_InputIterator>::value &&
1143 is_constructible<
1144 value_type,
1145 typename iterator_traits<_InputIterator>::reference>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001146 : __base(__a)
1147{
Howard Hinnantabe26282011-09-16 17:29:17 +00001148#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001149 __get_db()->__insert_c(this);
1150#endif
Howard Hinnant0442b122011-09-16 18:41:29 +00001151 for (; __first != __last; ++__first)
1152 push_back(*__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001153}
1154
1155template <class _Tp, class _Allocator>
1156template <class _ForwardIterator>
Howard Hinnantde589f22013-09-21 21:13:54 +00001157vector<_Tp, _Allocator>::vector(_ForwardIterator __first,
Howard Hinnant742fecb2013-03-28 17:44:32 +00001158 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
1159 is_constructible<
1160 value_type,
Howard Hinnantde589f22013-09-21 21:13:54 +00001161 typename iterator_traits<_ForwardIterator>::reference>::value,
1162 _ForwardIterator>::type __last)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001163{
Howard Hinnant0442b122011-09-16 18:41:29 +00001164#if _LIBCPP_DEBUG_LEVEL >= 2
1165 __get_db()->__insert_c(this);
1166#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00001167 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001168 if (__n > 0)
1169 {
1170 allocate(__n);
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001171 __construct_at_end(__first, __last, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001172 }
1173}
1174
1175template <class _Tp, class _Allocator>
1176template <class _ForwardIterator>
1177vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Howard Hinnant742fecb2013-03-28 17:44:32 +00001178 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
1179 is_constructible<
1180 value_type,
1181 typename iterator_traits<_ForwardIterator>::reference>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001182 : __base(__a)
1183{
Howard Hinnant0442b122011-09-16 18:41:29 +00001184#if _LIBCPP_DEBUG_LEVEL >= 2
1185 __get_db()->__insert_c(this);
1186#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00001187 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001188 if (__n > 0)
1189 {
1190 allocate(__n);
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001191 __construct_at_end(__first, __last, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001192 }
1193}
1194
1195template <class _Tp, class _Allocator>
1196vector<_Tp, _Allocator>::vector(const vector& __x)
1197 : __base(__alloc_traits::select_on_container_copy_construction(__x.__alloc()))
1198{
Howard Hinnant0442b122011-09-16 18:41:29 +00001199#if _LIBCPP_DEBUG_LEVEL >= 2
1200 __get_db()->__insert_c(this);
1201#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001202 size_type __n = __x.size();
1203 if (__n > 0)
1204 {
1205 allocate(__n);
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001206 __construct_at_end(__x.__begin_, __x.__end_, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001207 }
1208}
1209
1210template <class _Tp, class _Allocator>
1211vector<_Tp, _Allocator>::vector(const vector& __x, const allocator_type& __a)
1212 : __base(__a)
1213{
Howard Hinnant0442b122011-09-16 18:41:29 +00001214#if _LIBCPP_DEBUG_LEVEL >= 2
1215 __get_db()->__insert_c(this);
1216#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001217 size_type __n = __x.size();
1218 if (__n > 0)
1219 {
1220 allocate(__n);
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001221 __construct_at_end(__x.__begin_, __x.__end_, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001222 }
1223}
1224
Howard Hinnant73d21a42010-09-04 23:28:19 +00001225#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001226
1227template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001228inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001229vector<_Tp, _Allocator>::vector(vector&& __x)
Marshall Clow119ed472015-07-14 14:46:32 +00001230#if _LIBCPP_STD_VER > 14
1231 _NOEXCEPT
1232#else
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001233 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clow119ed472015-07-14 14:46:32 +00001234#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00001235 : __base(_VSTD::move(__x.__alloc()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001236{
Howard Hinnantabe26282011-09-16 17:29:17 +00001237#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001238 __get_db()->__insert_c(this);
Howard Hinnante6125bd2011-09-19 16:34:29 +00001239 __get_db()->swap(this, &__x);
Howard Hinnant7a563db2011-09-14 18:33:51 +00001240#endif
Howard Hinnant0442b122011-09-16 18:41:29 +00001241 this->__begin_ = __x.__begin_;
1242 this->__end_ = __x.__end_;
1243 this->__end_cap() = __x.__end_cap();
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00001244 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001245}
1246
1247template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001248inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001249vector<_Tp, _Allocator>::vector(vector&& __x, const allocator_type& __a)
1250 : __base(__a)
1251{
Howard Hinnant0442b122011-09-16 18:41:29 +00001252#if _LIBCPP_DEBUG_LEVEL >= 2
1253 __get_db()->__insert_c(this);
1254#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001255 if (__a == __x.__alloc())
1256 {
1257 this->__begin_ = __x.__begin_;
1258 this->__end_ = __x.__end_;
1259 this->__end_cap() = __x.__end_cap();
1260 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnante6125bd2011-09-19 16:34:29 +00001261#if _LIBCPP_DEBUG_LEVEL >= 2
1262 __get_db()->swap(this, &__x);
1263#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001264 }
1265 else
1266 {
Howard Hinnant99968442011-11-29 18:15:50 +00001267 typedef move_iterator<iterator> _Ip;
1268 assign(_Ip(__x.begin()), _Ip(__x.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001269 }
1270}
1271
Howard Hinnante3e32912011-08-12 21:56:02 +00001272#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1273
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001274template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001275inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001276vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il)
1277{
Howard Hinnant0442b122011-09-16 18:41:29 +00001278#if _LIBCPP_DEBUG_LEVEL >= 2
1279 __get_db()->__insert_c(this);
1280#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001281 if (__il.size() > 0)
1282 {
1283 allocate(__il.size());
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001284 __construct_at_end(__il.begin(), __il.end(), __il.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001285 }
1286}
1287
1288template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001289inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001290vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
1291 : __base(__a)
1292{
Howard Hinnant0442b122011-09-16 18:41:29 +00001293#if _LIBCPP_DEBUG_LEVEL >= 2
1294 __get_db()->__insert_c(this);
1295#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001296 if (__il.size() > 0)
1297 {
1298 allocate(__il.size());
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001299 __construct_at_end(__il.begin(), __il.end(), __il.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001300 }
1301}
1302
Howard Hinnante3e32912011-08-12 21:56:02 +00001303#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1304
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001305template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001306inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001307vector<_Tp, _Allocator>&
1308vector<_Tp, _Allocator>::operator=(vector&& __x)
Marshall Clowaf961ed2015-08-18 18:57:00 +00001309 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001310{
1311 __move_assign(__x, integral_constant<bool,
1312 __alloc_traits::propagate_on_container_move_assignment::value>());
1313 return *this;
1314}
1315
1316template <class _Tp, class _Allocator>
1317void
1318vector<_Tp, _Allocator>::__move_assign(vector& __c, false_type)
Marshall Clowaf961ed2015-08-18 18:57:00 +00001319 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001320{
1321 if (__base::__alloc() != __c.__alloc())
1322 {
Howard Hinnant99968442011-11-29 18:15:50 +00001323 typedef move_iterator<iterator> _Ip;
1324 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001325 }
1326 else
1327 __move_assign(__c, true_type());
1328}
1329
1330template <class _Tp, class _Allocator>
1331void
1332vector<_Tp, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001333 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001334{
1335 deallocate();
Marshall Clow3c2eac62014-07-21 15:11:13 +00001336 __base::__move_assign_alloc(__c); // this can throw
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001337 this->__begin_ = __c.__begin_;
1338 this->__end_ = __c.__end_;
1339 this->__end_cap() = __c.__end_cap();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001340 __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
Howard Hinnante6125bd2011-09-19 16:34:29 +00001341#if _LIBCPP_DEBUG_LEVEL >= 2
1342 __get_db()->swap(this, &__c);
1343#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001344}
1345
Howard Hinnant73d21a42010-09-04 23:28:19 +00001346#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001347
1348template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001349inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001350vector<_Tp, _Allocator>&
1351vector<_Tp, _Allocator>::operator=(const vector& __x)
1352{
1353 if (this != &__x)
1354 {
1355 __base::__copy_assign_alloc(__x);
1356 assign(__x.__begin_, __x.__end_);
1357 }
1358 return *this;
1359}
1360
1361template <class _Tp, class _Allocator>
1362template <class _InputIterator>
1363typename enable_if
1364<
1365 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +00001366 !__is_forward_iterator<_InputIterator>::value &&
1367 is_constructible<
1368 _Tp,
1369 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001370 void
1371>::type
1372vector<_Tp, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
1373{
1374 clear();
1375 for (; __first != __last; ++__first)
1376 push_back(*__first);
1377}
1378
1379template <class _Tp, class _Allocator>
1380template <class _ForwardIterator>
1381typename enable_if
1382<
Howard Hinnant742fecb2013-03-28 17:44:32 +00001383 __is_forward_iterator<_ForwardIterator>::value &&
1384 is_constructible<
1385 _Tp,
1386 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001387 void
1388>::type
1389vector<_Tp, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
1390{
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001391 size_type __new_size = static_cast<size_type>(_VSTD::distance(__first, __last));
1392 if (__new_size <= capacity())
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001393 {
1394 _ForwardIterator __mid = __last;
1395 bool __growing = false;
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001396 if (__new_size > size())
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001397 {
1398 __growing = true;
1399 __mid = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001400 _VSTD::advance(__mid, size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001401 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00001402 pointer __m = _VSTD::copy(__first, __mid, this->__begin_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001403 if (__growing)
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001404 __construct_at_end(__mid, __last, __new_size - size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001405 else
1406 this->__destruct_at_end(__m);
1407 }
1408 else
1409 {
1410 deallocate();
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001411 allocate(__recommend(__new_size));
1412 __construct_at_end(__first, __last, __new_size);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001413 }
1414}
1415
1416template <class _Tp, class _Allocator>
1417void
1418vector<_Tp, _Allocator>::assign(size_type __n, const_reference __u)
1419{
1420 if (__n <= capacity())
1421 {
1422 size_type __s = size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001423 _VSTD::fill_n(this->__begin_, _VSTD::min(__n, __s), __u);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001424 if (__n > __s)
1425 __construct_at_end(__n - __s, __u);
1426 else
Howard Hinnantadff4892010-05-24 17:49:41 +00001427 this->__destruct_at_end(this->__begin_ + __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001428 }
1429 else
1430 {
1431 deallocate();
1432 allocate(__recommend(static_cast<size_type>(__n)));
1433 __construct_at_end(__n, __u);
1434 }
1435}
1436
Howard Hinnant324bb032010-08-22 00:02:43 +00001437template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001438inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001439typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001440vector<_Tp, _Allocator>::__make_iter(pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001441{
Howard Hinnantabe26282011-09-16 17:29:17 +00001442#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001443 return iterator(this, __p);
1444#else
1445 return iterator(__p);
1446#endif
1447}
1448
Howard Hinnant324bb032010-08-22 00:02:43 +00001449template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001450inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001451typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001452vector<_Tp, _Allocator>::__make_iter(const_pointer __p) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001453{
Howard Hinnantabe26282011-09-16 17:29:17 +00001454#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001455 return const_iterator(this, __p);
1456#else
1457 return const_iterator(__p);
1458#endif
1459}
1460
Howard Hinnant324bb032010-08-22 00:02:43 +00001461template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001462inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001463typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001464vector<_Tp, _Allocator>::begin() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001465{
1466 return __make_iter(this->__begin_);
1467}
1468
Howard Hinnant324bb032010-08-22 00:02:43 +00001469template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001470inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001471typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001472vector<_Tp, _Allocator>::begin() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001473{
1474 return __make_iter(this->__begin_);
1475}
1476
Howard Hinnant324bb032010-08-22 00:02:43 +00001477template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001478inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001479typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001480vector<_Tp, _Allocator>::end() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001481{
1482 return __make_iter(this->__end_);
1483}
1484
Howard Hinnant324bb032010-08-22 00:02:43 +00001485template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001486inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001487typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001488vector<_Tp, _Allocator>::end() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001489{
1490 return __make_iter(this->__end_);
1491}
1492
Howard Hinnant324bb032010-08-22 00:02:43 +00001493template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001494inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001495typename vector<_Tp, _Allocator>::reference
1496vector<_Tp, _Allocator>::operator[](size_type __n)
1497{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001498 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001499 return this->__begin_[__n];
1500}
1501
Howard Hinnant324bb032010-08-22 00:02:43 +00001502template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001503inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001504typename vector<_Tp, _Allocator>::const_reference
1505vector<_Tp, _Allocator>::operator[](size_type __n) const
1506{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001507 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001508 return this->__begin_[__n];
1509}
1510
Howard Hinnant324bb032010-08-22 00:02:43 +00001511template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001512typename vector<_Tp, _Allocator>::reference
1513vector<_Tp, _Allocator>::at(size_type __n)
1514{
1515 if (__n >= size())
1516 this->__throw_out_of_range();
1517 return this->__begin_[__n];
1518}
1519
Howard Hinnant324bb032010-08-22 00:02:43 +00001520template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001521typename vector<_Tp, _Allocator>::const_reference
1522vector<_Tp, _Allocator>::at(size_type __n) const
1523{
1524 if (__n >= size())
1525 this->__throw_out_of_range();
1526 return this->__begin_[__n];
1527}
1528
1529template <class _Tp, class _Allocator>
1530void
1531vector<_Tp, _Allocator>::reserve(size_type __n)
1532{
1533 if (__n > capacity())
1534 {
1535 allocator_type& __a = this->__alloc();
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001536 __split_buffer<value_type, allocator_type&> __v(__n, size(), __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001537 __swap_out_circular_buffer(__v);
1538 }
1539}
1540
1541template <class _Tp, class _Allocator>
1542void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001543vector<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001544{
1545 if (capacity() > size())
1546 {
1547#ifndef _LIBCPP_NO_EXCEPTIONS
1548 try
1549 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001550#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001551 allocator_type& __a = this->__alloc();
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001552 __split_buffer<value_type, allocator_type&> __v(size(), size(), __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001553 __swap_out_circular_buffer(__v);
1554#ifndef _LIBCPP_NO_EXCEPTIONS
1555 }
1556 catch (...)
1557 {
1558 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001559#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001560 }
1561}
1562
1563template <class _Tp, class _Allocator>
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001564template <class _Up>
1565void
1566#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1567vector<_Tp, _Allocator>::__push_back_slow_path(_Up&& __x)
1568#else
1569vector<_Tp, _Allocator>::__push_back_slow_path(_Up& __x)
1570#endif
1571{
1572 allocator_type& __a = this->__alloc();
1573 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1574 // __v.push_back(_VSTD::forward<_Up>(__x));
Howard Hinnantf619e232013-01-11 20:36:59 +00001575 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), _VSTD::forward<_Up>(__x));
1576 __v.__end_++;
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001577 __swap_out_circular_buffer(__v);
1578}
1579
1580template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001581inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001582void
1583vector<_Tp, _Allocator>::push_back(const_reference __x)
1584{
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001585 if (this->__end_ != this->__end_cap())
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001586 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001587 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001588 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001589 _VSTD::__to_raw_pointer(this->__end_), __x);
Kostya Serebryany497f9122014-09-02 23:43:38 +00001590 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001591 ++this->__end_;
1592 }
1593 else
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001594 __push_back_slow_path(__x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001595}
1596
Howard Hinnant73d21a42010-09-04 23:28:19 +00001597#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001598
1599template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001600inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001601void
1602vector<_Tp, _Allocator>::push_back(value_type&& __x)
1603{
1604 if (this->__end_ < this->__end_cap())
1605 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001606 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001607 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001608 _VSTD::__to_raw_pointer(this->__end_),
1609 _VSTD::move(__x));
Kostya Serebryany497f9122014-09-02 23:43:38 +00001610 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001611 ++this->__end_;
1612 }
1613 else
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001614 __push_back_slow_path(_VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001615}
1616
Howard Hinnant73d21a42010-09-04 23:28:19 +00001617#ifndef _LIBCPP_HAS_NO_VARIADICS
1618
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001619template <class _Tp, class _Allocator>
1620template <class... _Args>
1621void
Howard Hinnant0438ea22012-02-26 15:30:12 +00001622vector<_Tp, _Allocator>::__emplace_back_slow_path(_Args&&... __args)
1623{
1624 allocator_type& __a = this->__alloc();
1625 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1626// __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantf619e232013-01-11 20:36:59 +00001627 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), _VSTD::forward<_Args>(__args)...);
1628 __v.__end_++;
Howard Hinnant0438ea22012-02-26 15:30:12 +00001629 __swap_out_circular_buffer(__v);
1630}
1631
1632template <class _Tp, class _Allocator>
1633template <class... _Args>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001634inline
Howard Hinnant0438ea22012-02-26 15:30:12 +00001635void
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001636vector<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1637{
1638 if (this->__end_ < this->__end_cap())
1639 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001640 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001641 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001642 _VSTD::__to_raw_pointer(this->__end_),
1643 _VSTD::forward<_Args>(__args)...);
Kostya Serebryany497f9122014-09-02 23:43:38 +00001644 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001645 ++this->__end_;
1646 }
1647 else
Howard Hinnant0438ea22012-02-26 15:30:12 +00001648 __emplace_back_slow_path(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001649}
1650
Howard Hinnant73d21a42010-09-04 23:28:19 +00001651#endif // _LIBCPP_HAS_NO_VARIADICS
1652#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001653
1654template <class _Tp, class _Allocator>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001655inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001656void
1657vector<_Tp, _Allocator>::pop_back()
1658{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001659 _LIBCPP_ASSERT(!empty(), "vector::pop_back called for empty vector");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001660 this->__destruct_at_end(this->__end_ - 1);
1661}
1662
1663template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001664inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001665typename vector<_Tp, _Allocator>::iterator
1666vector<_Tp, _Allocator>::erase(const_iterator __position)
1667{
Howard Hinnantabe26282011-09-16 17:29:17 +00001668#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001669 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1670 "vector::erase(iterator) called with an iterator not"
1671 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001672#endif
Howard Hinnant782da332013-03-25 22:12:26 +00001673 _LIBCPP_ASSERT(__position != end(),
1674 "vector::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00001675 difference_type __ps = __position - cbegin();
1676 pointer __p = this->__begin_ + __ps;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001677 iterator __r = __make_iter(__p);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001678 this->__destruct_at_end(_VSTD::move(__p + 1, this->__end_, __p));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001679 return __r;
1680}
1681
1682template <class _Tp, class _Allocator>
1683typename vector<_Tp, _Allocator>::iterator
1684vector<_Tp, _Allocator>::erase(const_iterator __first, const_iterator __last)
1685{
Howard Hinnantabe26282011-09-16 17:29:17 +00001686#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001687 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
1688 "vector::erase(iterator, iterator) called with an iterator not"
1689 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001690#endif
Howard Hinnant7a563db2011-09-14 18:33:51 +00001691 _LIBCPP_ASSERT(__first <= __last, "vector::erase(first, last) called with invalid range");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001692 pointer __p = this->__begin_ + (__first - begin());
1693 iterator __r = __make_iter(__p);
Howard Hinnantb4e67cf2013-04-18 15:02:57 +00001694 if (__first != __last)
1695 this->__destruct_at_end(_VSTD::move(__p + (__last - __first), this->__end_, __p));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001696 return __r;
1697}
1698
1699template <class _Tp, class _Allocator>
1700void
1701vector<_Tp, _Allocator>::__move_range(pointer __from_s, pointer __from_e, pointer __to)
1702{
1703 pointer __old_last = this->__end_;
1704 difference_type __n = __old_last - __to;
1705 for (pointer __i = __from_s + __n; __i < __from_e; ++__i, ++this->__end_)
1706 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001707 _VSTD::__to_raw_pointer(this->__end_),
1708 _VSTD::move(*__i));
1709 _VSTD::move_backward(__from_s, __from_s + __n, __old_last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001710}
1711
1712template <class _Tp, class _Allocator>
1713typename vector<_Tp, _Allocator>::iterator
1714vector<_Tp, _Allocator>::insert(const_iterator __position, const_reference __x)
1715{
Howard Hinnantabe26282011-09-16 17:29:17 +00001716#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001717 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1718 "vector::insert(iterator, x) called with an iterator not"
1719 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001720#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001721 pointer __p = this->__begin_ + (__position - begin());
1722 if (this->__end_ < this->__end_cap())
1723 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001724 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001725 if (__p == this->__end_)
1726 {
1727 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001728 _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001729 ++this->__end_;
1730 }
1731 else
1732 {
1733 __move_range(__p, this->__end_, __p + 1);
1734 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1735 if (__p <= __xr && __xr < this->__end_)
1736 ++__xr;
1737 *__p = *__xr;
1738 }
Kostya Serebryany497f9122014-09-02 23:43:38 +00001739 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001740 }
1741 else
1742 {
1743 allocator_type& __a = this->__alloc();
1744 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
1745 __v.push_back(__x);
1746 __p = __swap_out_circular_buffer(__v, __p);
1747 }
1748 return __make_iter(__p);
1749}
1750
Howard Hinnant73d21a42010-09-04 23:28:19 +00001751#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001752
1753template <class _Tp, class _Allocator>
1754typename vector<_Tp, _Allocator>::iterator
1755vector<_Tp, _Allocator>::insert(const_iterator __position, value_type&& __x)
1756{
Howard Hinnantabe26282011-09-16 17:29:17 +00001757#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001758 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1759 "vector::insert(iterator, x) called with an iterator not"
1760 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001761#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001762 pointer __p = this->__begin_ + (__position - begin());
1763 if (this->__end_ < this->__end_cap())
1764 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001765 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001766 if (__p == this->__end_)
1767 {
1768 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001769 _VSTD::__to_raw_pointer(this->__end_),
1770 _VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001771 ++this->__end_;
1772 }
1773 else
1774 {
1775 __move_range(__p, this->__end_, __p + 1);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001776 *__p = _VSTD::move(__x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001777 }
Kostya Serebryany497f9122014-09-02 23:43:38 +00001778 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001779 }
1780 else
1781 {
1782 allocator_type& __a = this->__alloc();
1783 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001784 __v.push_back(_VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001785 __p = __swap_out_circular_buffer(__v, __p);
1786 }
1787 return __make_iter(__p);
1788}
1789
Howard Hinnant73d21a42010-09-04 23:28:19 +00001790#ifndef _LIBCPP_HAS_NO_VARIADICS
1791
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001792template <class _Tp, class _Allocator>
1793template <class... _Args>
1794typename vector<_Tp, _Allocator>::iterator
1795vector<_Tp, _Allocator>::emplace(const_iterator __position, _Args&&... __args)
1796{
Howard Hinnantabe26282011-09-16 17:29:17 +00001797#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001798 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1799 "vector::emplace(iterator, x) called with an iterator not"
1800 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001801#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001802 pointer __p = this->__begin_ + (__position - begin());
1803 if (this->__end_ < this->__end_cap())
1804 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001805 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001806 if (__p == this->__end_)
1807 {
1808 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001809 _VSTD::__to_raw_pointer(this->__end_),
1810 _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001811 ++this->__end_;
1812 }
1813 else
1814 {
Howard Hinnanta58402a2012-07-08 23:23:04 +00001815 value_type __tmp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001816 __move_range(__p, this->__end_, __p + 1);
Howard Hinnanta58402a2012-07-08 23:23:04 +00001817 *__p = _VSTD::move(__tmp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001818 }
Kostya Serebryany497f9122014-09-02 23:43:38 +00001819 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001820 }
1821 else
1822 {
1823 allocator_type& __a = this->__alloc();
1824 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001825 __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001826 __p = __swap_out_circular_buffer(__v, __p);
1827 }
1828 return __make_iter(__p);
1829}
1830
Howard Hinnant73d21a42010-09-04 23:28:19 +00001831#endif // _LIBCPP_HAS_NO_VARIADICS
1832#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001833
1834template <class _Tp, class _Allocator>
1835typename vector<_Tp, _Allocator>::iterator
1836vector<_Tp, _Allocator>::insert(const_iterator __position, size_type __n, const_reference __x)
1837{
Howard Hinnantabe26282011-09-16 17:29:17 +00001838#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001839 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1840 "vector::insert(iterator, n, x) called with an iterator not"
1841 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001842#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001843 pointer __p = this->__begin_ + (__position - begin());
1844 if (__n > 0)
1845 {
1846 if (__n <= static_cast<size_type>(this->__end_cap() - this->__end_))
1847 {
1848 size_type __old_n = __n;
1849 pointer __old_last = this->__end_;
1850 if (__n > static_cast<size_type>(this->__end_ - __p))
1851 {
1852 size_type __cx = __n - (this->__end_ - __p);
1853 __construct_at_end(__cx, __x);
1854 __n -= __cx;
1855 }
1856 if (__n > 0)
1857 {
Eric Fiselierd7590952014-11-14 18:28:36 +00001858 __RAII_IncreaseAnnotator __annotator(*this, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001859 __move_range(__p, __old_last, __p + __old_n);
Kostya Serebryany497f9122014-09-02 23:43:38 +00001860 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001861 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1862 if (__p <= __xr && __xr < this->__end_)
1863 __xr += __old_n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001864 _VSTD::fill_n(__p, __n, *__xr);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001865 }
1866 }
1867 else
1868 {
1869 allocator_type& __a = this->__alloc();
1870 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1871 __v.__construct_at_end(__n, __x);
1872 __p = __swap_out_circular_buffer(__v, __p);
1873 }
1874 }
1875 return __make_iter(__p);
1876}
1877
1878template <class _Tp, class _Allocator>
1879template <class _InputIterator>
1880typename enable_if
1881<
1882 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +00001883 !__is_forward_iterator<_InputIterator>::value &&
1884 is_constructible<
1885 _Tp,
1886 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001887 typename vector<_Tp, _Allocator>::iterator
1888>::type
1889vector<_Tp, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
1890{
Howard Hinnantabe26282011-09-16 17:29:17 +00001891#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001892 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1893 "vector::insert(iterator, range) called with an iterator not"
1894 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001895#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001896 difference_type __off = __position - begin();
1897 pointer __p = this->__begin_ + __off;
1898 allocator_type& __a = this->__alloc();
1899 pointer __old_last = this->__end_;
1900 for (; this->__end_ != this->__end_cap() && __first != __last; ++__first)
1901 {
Eric Fiselier7d439a42015-07-18 18:22:12 +00001902 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001903 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001904 *__first);
1905 ++this->__end_;
Eric Fiselier7d439a42015-07-18 18:22:12 +00001906 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001907 }
1908 __split_buffer<value_type, allocator_type&> __v(__a);
1909 if (__first != __last)
1910 {
1911#ifndef _LIBCPP_NO_EXCEPTIONS
1912 try
1913 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001914#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001915 __v.__construct_at_end(__first, __last);
1916 difference_type __old_size = __old_last - this->__begin_;
1917 difference_type __old_p = __p - this->__begin_;
1918 reserve(__recommend(size() + __v.size()));
1919 __p = this->__begin_ + __old_p;
1920 __old_last = this->__begin_ + __old_size;
1921#ifndef _LIBCPP_NO_EXCEPTIONS
1922 }
1923 catch (...)
1924 {
1925 erase(__make_iter(__old_last), end());
1926 throw;
1927 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001928#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001929 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00001930 __p = _VSTD::rotate(__p, __old_last, this->__end_);
Howard Hinnant0442b122011-09-16 18:41:29 +00001931 insert(__make_iter(__p), make_move_iterator(__v.begin()),
1932 make_move_iterator(__v.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001933 return begin() + __off;
1934}
1935
1936template <class _Tp, class _Allocator>
1937template <class _ForwardIterator>
1938typename enable_if
1939<
Howard Hinnant742fecb2013-03-28 17:44:32 +00001940 __is_forward_iterator<_ForwardIterator>::value &&
1941 is_constructible<
1942 _Tp,
1943 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001944 typename vector<_Tp, _Allocator>::iterator
1945>::type
1946vector<_Tp, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
1947{
Howard Hinnantabe26282011-09-16 17:29:17 +00001948#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001949 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1950 "vector::insert(iterator, range) called with an iterator not"
1951 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001952#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001953 pointer __p = this->__begin_ + (__position - begin());
Howard Hinnant0949eed2011-06-30 21:18:19 +00001954 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001955 if (__n > 0)
1956 {
1957 if (__n <= this->__end_cap() - this->__end_)
1958 {
1959 size_type __old_n = __n;
1960 pointer __old_last = this->__end_;
1961 _ForwardIterator __m = __last;
1962 difference_type __dx = this->__end_ - __p;
1963 if (__n > __dx)
1964 {
1965 __m = __first;
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001966 difference_type __diff = this->__end_ - __p;
1967 _VSTD::advance(__m, __diff);
1968 __construct_at_end(__m, __last, __n - __diff);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001969 __n = __dx;
1970 }
1971 if (__n > 0)
1972 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001973 __RAII_IncreaseAnnotator __annotator(*this, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001974 __move_range(__p, __old_last, __p + __old_n);
Kostya Serebryany497f9122014-09-02 23:43:38 +00001975 __annotator.__done();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001976 _VSTD::copy(__first, __m, __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001977 }
1978 }
1979 else
1980 {
1981 allocator_type& __a = this->__alloc();
1982 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1983 __v.__construct_at_end(__first, __last);
1984 __p = __swap_out_circular_buffer(__v, __p);
1985 }
1986 }
1987 return __make_iter(__p);
1988}
1989
1990template <class _Tp, class _Allocator>
1991void
1992vector<_Tp, _Allocator>::resize(size_type __sz)
1993{
1994 size_type __cs = size();
1995 if (__cs < __sz)
1996 this->__append(__sz - __cs);
1997 else if (__cs > __sz)
1998 this->__destruct_at_end(this->__begin_ + __sz);
1999}
2000
2001template <class _Tp, class _Allocator>
2002void
2003vector<_Tp, _Allocator>::resize(size_type __sz, const_reference __x)
2004{
2005 size_type __cs = size();
2006 if (__cs < __sz)
2007 this->__append(__sz - __cs, __x);
2008 else if (__cs > __sz)
2009 this->__destruct_at_end(this->__begin_ + __sz);
2010}
2011
2012template <class _Tp, class _Allocator>
2013void
2014vector<_Tp, _Allocator>::swap(vector& __x)
Marshall Clow7d914d12015-07-13 20:04:56 +00002015#if _LIBCPP_STD_VER >= 14
2016 _NOEXCEPT
2017#else
2018 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
2019 __is_nothrow_swappable<allocator_type>::value)
2020#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002021{
Howard Hinnant7a563db2011-09-14 18:33:51 +00002022 _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
2023 this->__alloc() == __x.__alloc(),
2024 "vector::swap: Either propagate_on_container_swap must be true"
2025 " or the allocators must compare equal");
Howard Hinnant0949eed2011-06-30 21:18:19 +00002026 _VSTD::swap(this->__begin_, __x.__begin_);
2027 _VSTD::swap(this->__end_, __x.__end_);
2028 _VSTD::swap(this->__end_cap(), __x.__end_cap());
Marshall Clow7d914d12015-07-13 20:04:56 +00002029 __swap_allocator(this->__alloc(), __x.__alloc(),
2030 integral_constant<bool,__alloc_traits::propagate_on_container_swap::value>());
Howard Hinnantabe26282011-09-16 17:29:17 +00002031#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00002032 __get_db()->swap(this, &__x);
Howard Hinnantabe26282011-09-16 17:29:17 +00002033#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002034}
2035
Howard Hinnant324bb032010-08-22 00:02:43 +00002036template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002037bool
2038vector<_Tp, _Allocator>::__invariants() const
2039{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002040 if (this->__begin_ == nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002041 {
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002042 if (this->__end_ != nullptr || this->__end_cap() != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002043 return false;
2044 }
2045 else
2046 {
2047 if (this->__begin_ > this->__end_)
2048 return false;
2049 if (this->__begin_ == this->__end_cap())
2050 return false;
2051 if (this->__end_ > this->__end_cap())
2052 return false;
2053 }
2054 return true;
2055}
2056
Howard Hinnantabe26282011-09-16 17:29:17 +00002057#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00002058
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002059template <class _Tp, class _Allocator>
Howard Hinnant7a563db2011-09-14 18:33:51 +00002060bool
2061vector<_Tp, _Allocator>::__dereferenceable(const const_iterator* __i) const
2062{
2063 return this->__begin_ <= __i->base() && __i->base() < this->__end_;
2064}
2065
2066template <class _Tp, class _Allocator>
2067bool
2068vector<_Tp, _Allocator>::__decrementable(const const_iterator* __i) const
2069{
2070 return this->__begin_ < __i->base() && __i->base() <= this->__end_;
2071}
2072
2073template <class _Tp, class _Allocator>
2074bool
2075vector<_Tp, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
2076{
2077 const_pointer __p = __i->base() + __n;
2078 return this->__begin_ <= __p && __p <= this->__end_;
2079}
2080
2081template <class _Tp, class _Allocator>
2082bool
2083vector<_Tp, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
2084{
2085 const_pointer __p = __i->base() + __n;
2086 return this->__begin_ <= __p && __p < this->__end_;
2087}
2088
Howard Hinnantabe26282011-09-16 17:29:17 +00002089#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00002090
2091template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002092inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002093void
2094vector<_Tp, _Allocator>::__invalidate_all_iterators()
2095{
Howard Hinnantabe26282011-09-16 17:29:17 +00002096#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00002097 __get_db()->__invalidate_all(this);
Howard Hinnantabe26282011-09-16 17:29:17 +00002098#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002099}
2100
2101// vector<bool>
2102
2103template <class _Allocator> class vector<bool, _Allocator>;
2104
2105template <class _Allocator> struct hash<vector<bool, _Allocator> >;
2106
2107template <class _Allocator>
Howard Hinnantf03c3b42011-07-02 20:33:23 +00002108struct __has_storage_type<vector<bool, _Allocator> >
2109{
2110 static const bool value = true;
2111};
2112
2113template <class _Allocator>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002114class _LIBCPP_TYPE_VIS_ONLY vector<bool, _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002115 : private __vector_base_common<true>
2116{
2117public:
2118 typedef vector __self;
Howard Hinnant324bb032010-08-22 00:02:43 +00002119 typedef bool value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002120 typedef _Allocator allocator_type;
2121 typedef allocator_traits<allocator_type> __alloc_traits;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002122 typedef typename __alloc_traits::size_type size_type;
2123 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnantf867f632012-05-07 16:50:38 +00002124 typedef size_type __storage_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002125 typedef __bit_iterator<vector, false> pointer;
2126 typedef __bit_iterator<vector, true> const_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002127 typedef pointer iterator;
2128 typedef const_pointer const_iterator;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002129 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
2130 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002131
2132private:
Marshall Clow66302c62015-04-07 05:21:38 +00002133 typedef typename __rebind_alloc_helper<__alloc_traits, __storage_type>::type __storage_allocator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002134 typedef allocator_traits<__storage_allocator> __storage_traits;
2135 typedef typename __storage_traits::pointer __storage_pointer;
2136 typedef typename __storage_traits::const_pointer __const_storage_pointer;
2137
2138 __storage_pointer __begin_;
2139 size_type __size_;
2140 __compressed_pair<size_type, __storage_allocator> __cap_alloc_;
Howard Hinnant2bf1c082011-07-09 15:50:42 +00002141public:
Howard Hinnantf03c3b42011-07-02 20:33:23 +00002142 typedef __bit_reference<vector> reference;
2143 typedef __bit_const_reference<vector> const_reference;
Howard Hinnant2bf1c082011-07-09 15:50:42 +00002144private:
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002145 _LIBCPP_INLINE_VISIBILITY
2146 size_type& __cap() _NOEXCEPT
2147 {return __cap_alloc_.first();}
2148 _LIBCPP_INLINE_VISIBILITY
2149 const size_type& __cap() const _NOEXCEPT
2150 {return __cap_alloc_.first();}
2151 _LIBCPP_INLINE_VISIBILITY
2152 __storage_allocator& __alloc() _NOEXCEPT
2153 {return __cap_alloc_.second();}
2154 _LIBCPP_INLINE_VISIBILITY
2155 const __storage_allocator& __alloc() const _NOEXCEPT
2156 {return __cap_alloc_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002157
2158 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
2159
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002160 _LIBCPP_INLINE_VISIBILITY
2161 static size_type __internal_cap_to_external(size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002162 {return __n * __bits_per_word;}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002163 _LIBCPP_INLINE_VISIBILITY
2164 static size_type __external_cap_to_internal(size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002165 {return (__n - 1) / __bits_per_word + 1;}
2166
2167public:
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002168 _LIBCPP_INLINE_VISIBILITY
Marshall Clowc912c0c2015-06-04 02:05:41 +00002169 vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clow127db912015-06-04 00:10:20 +00002170
2171 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
2172#if _LIBCPP_STD_VER <= 14
2173 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
2174#else
2175 _NOEXCEPT;
2176#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002177 ~vector();
2178 explicit vector(size_type __n);
Marshall Clowa49a2c92013-09-14 00:47:59 +00002179#if _LIBCPP_STD_VER > 11
2180 explicit vector(size_type __n, const allocator_type& __a);
2181#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002182 vector(size_type __n, const value_type& __v);
2183 vector(size_type __n, const value_type& __v, const allocator_type& __a);
2184 template <class _InputIterator>
2185 vector(_InputIterator __first, _InputIterator __last,
2186 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2187 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2188 template <class _InputIterator>
2189 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2190 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2191 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2192 template <class _ForwardIterator>
2193 vector(_ForwardIterator __first, _ForwardIterator __last,
2194 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2195 template <class _ForwardIterator>
2196 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2197 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2198
2199 vector(const vector& __v);
2200 vector(const vector& __v, const allocator_type& __a);
2201 vector& operator=(const vector& __v);
Howard Hinnante3e32912011-08-12 21:56:02 +00002202#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002203 vector(initializer_list<value_type> __il);
2204 vector(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnante3e32912011-08-12 21:56:02 +00002205#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002206
Howard Hinnant73d21a42010-09-04 23:28:19 +00002207#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002208 _LIBCPP_INLINE_VISIBILITY
2209 vector(vector&& __v)
Marshall Clow119ed472015-07-14 14:46:32 +00002210#if _LIBCPP_STD_VER > 14
2211 _NOEXCEPT;
2212#else
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002213 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clow119ed472015-07-14 14:46:32 +00002214#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002215 vector(vector&& __v, const allocator_type& __a);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002216 _LIBCPP_INLINE_VISIBILITY
2217 vector& operator=(vector&& __v)
Marshall Clowaf961ed2015-08-18 18:57:00 +00002218 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Howard Hinnant73d21a42010-09-04 23:28:19 +00002219#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnante3e32912011-08-12 21:56:02 +00002220#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002221 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002222 vector& operator=(initializer_list<value_type> __il)
2223 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnante3e32912011-08-12 21:56:02 +00002224#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002225
2226 template <class _InputIterator>
2227 typename enable_if
2228 <
2229 __is_input_iterator<_InputIterator>::value &&
2230 !__is_forward_iterator<_InputIterator>::value,
2231 void
2232 >::type
2233 assign(_InputIterator __first, _InputIterator __last);
2234 template <class _ForwardIterator>
2235 typename enable_if
2236 <
2237 __is_forward_iterator<_ForwardIterator>::value,
2238 void
2239 >::type
2240 assign(_ForwardIterator __first, _ForwardIterator __last);
2241
2242 void assign(size_type __n, const value_type& __x);
Howard Hinnante3e32912011-08-12 21:56:02 +00002243#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002244 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002245 void assign(initializer_list<value_type> __il)
2246 {assign(__il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +00002247#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002248
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002249 _LIBCPP_INLINE_VISIBILITY allocator_type get_allocator() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002250 {return allocator_type(this->__alloc());}
2251
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002252 size_type max_size() const _NOEXCEPT;
2253 _LIBCPP_INLINE_VISIBILITY
2254 size_type capacity() const _NOEXCEPT
2255 {return __internal_cap_to_external(__cap());}
2256 _LIBCPP_INLINE_VISIBILITY
2257 size_type size() const _NOEXCEPT
2258 {return __size_;}
2259 _LIBCPP_INLINE_VISIBILITY
2260 bool empty() const _NOEXCEPT
2261 {return __size_ == 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002262 void reserve(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002263 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002264
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002265 _LIBCPP_INLINE_VISIBILITY
2266 iterator begin() _NOEXCEPT
2267 {return __make_iter(0);}
2268 _LIBCPP_INLINE_VISIBILITY
2269 const_iterator begin() const _NOEXCEPT
2270 {return __make_iter(0);}
2271 _LIBCPP_INLINE_VISIBILITY
2272 iterator end() _NOEXCEPT
2273 {return __make_iter(__size_);}
2274 _LIBCPP_INLINE_VISIBILITY
2275 const_iterator end() const _NOEXCEPT
2276 {return __make_iter(__size_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002277
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002278 _LIBCPP_INLINE_VISIBILITY
2279 reverse_iterator rbegin() _NOEXCEPT
2280 {return reverse_iterator(end());}
2281 _LIBCPP_INLINE_VISIBILITY
2282 const_reverse_iterator rbegin() const _NOEXCEPT
2283 {return const_reverse_iterator(end());}
2284 _LIBCPP_INLINE_VISIBILITY
2285 reverse_iterator rend() _NOEXCEPT
2286 {return reverse_iterator(begin());}
2287 _LIBCPP_INLINE_VISIBILITY
2288 const_reverse_iterator rend() const _NOEXCEPT
2289 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002290
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002291 _LIBCPP_INLINE_VISIBILITY
2292 const_iterator cbegin() const _NOEXCEPT
2293 {return __make_iter(0);}
2294 _LIBCPP_INLINE_VISIBILITY
2295 const_iterator cend() const _NOEXCEPT
2296 {return __make_iter(__size_);}
2297 _LIBCPP_INLINE_VISIBILITY
2298 const_reverse_iterator crbegin() const _NOEXCEPT
2299 {return rbegin();}
2300 _LIBCPP_INLINE_VISIBILITY
2301 const_reverse_iterator crend() const _NOEXCEPT
2302 {return rend();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002303
2304 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) {return __make_ref(__n);}
2305 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const {return __make_ref(__n);}
2306 reference at(size_type __n);
2307 const_reference at(size_type __n) const;
2308
2309 _LIBCPP_INLINE_VISIBILITY reference front() {return __make_ref(0);}
2310 _LIBCPP_INLINE_VISIBILITY const_reference front() const {return __make_ref(0);}
2311 _LIBCPP_INLINE_VISIBILITY reference back() {return __make_ref(__size_ - 1);}
2312 _LIBCPP_INLINE_VISIBILITY const_reference back() const {return __make_ref(__size_ - 1);}
2313
2314 void push_back(const value_type& __x);
Marshall Clow198a2a52013-08-13 23:54:12 +00002315#if _LIBCPP_STD_VER > 11
2316 template <class... _Args>
2317 _LIBCPP_INLINE_VISIBILITY void emplace_back(_Args&&... __args)
2318 { push_back ( value_type ( _VSTD::forward<_Args>(__args)... )); }
2319#endif
2320
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002321 _LIBCPP_INLINE_VISIBILITY void pop_back() {--__size_;}
2322
Marshall Clow198a2a52013-08-13 23:54:12 +00002323#if _LIBCPP_STD_VER > 11
2324 template <class... _Args>
2325 _LIBCPP_INLINE_VISIBILITY iterator emplace(const_iterator position, _Args&&... __args)
2326 { return insert ( position, value_type ( _VSTD::forward<_Args>(__args)... )); }
2327#endif
2328
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002329 iterator insert(const_iterator __position, const value_type& __x);
2330 iterator insert(const_iterator __position, size_type __n, const value_type& __x);
2331 iterator insert(const_iterator __position, size_type __n, const_reference __x);
2332 template <class _InputIterator>
2333 typename enable_if
2334 <
2335 __is_input_iterator <_InputIterator>::value &&
2336 !__is_forward_iterator<_InputIterator>::value,
2337 iterator
2338 >::type
2339 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
2340 template <class _ForwardIterator>
2341 typename enable_if
2342 <
2343 __is_forward_iterator<_ForwardIterator>::value,
2344 iterator
2345 >::type
2346 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnante3e32912011-08-12 21:56:02 +00002347#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002348 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002349 iterator insert(const_iterator __position, initializer_list<value_type> __il)
2350 {return insert(__position, __il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +00002351#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002352
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002353 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002354 iterator erase(const_iterator __first, const_iterator __last);
2355
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002356 _LIBCPP_INLINE_VISIBILITY
2357 void clear() _NOEXCEPT {__size_ = 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002358
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002359 void swap(vector&)
Marshall Clow7d914d12015-07-13 20:04:56 +00002360#if _LIBCPP_STD_VER >= 14
2361 _NOEXCEPT;
2362#else
2363 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
2364 __is_nothrow_swappable<allocator_type>::value);
2365#endif
Marshall Clow005c60b2016-04-07 14:20:31 +00002366 static void swap(reference __x, reference __y) _NOEXCEPT { _VSTD::swap(__x, __y); }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002367
2368 void resize(size_type __sz, value_type __x = false);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002369 void flip() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002370
2371 bool __invariants() const;
2372
2373private:
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002374 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002375 void allocate(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002376 void deallocate() _NOEXCEPT;
2377 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7f764502013-08-14 18:00:20 +00002378 static size_type __align_it(size_type __new_size) _NOEXCEPT
Marshall Clow0d1965d2014-07-28 15:02:42 +00002379 {return __new_size + (__bits_per_word-1) & ~((size_type)__bits_per_word-1);};
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002380 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
2381 _LIBCPP_INLINE_VISIBILITY void __construct_at_end(size_type __n, bool __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002382 template <class _ForwardIterator>
2383 typename enable_if
2384 <
2385 __is_forward_iterator<_ForwardIterator>::value,
2386 void
2387 >::type
2388 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
2389 void __append(size_type __n, const_reference __x);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002390 _LIBCPP_INLINE_VISIBILITY
2391 reference __make_ref(size_type __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002392 {return reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002393 _LIBCPP_INLINE_VISIBILITY
2394 const_reference __make_ref(size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002395 {return const_reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002396 _LIBCPP_INLINE_VISIBILITY
2397 iterator __make_iter(size_type __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002398 {return iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002399 _LIBCPP_INLINE_VISIBILITY
2400 const_iterator __make_iter(size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002401 {return const_iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002402 _LIBCPP_INLINE_VISIBILITY
2403 iterator __const_iterator_cast(const_iterator __p) _NOEXCEPT
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002404 {return begin() + (__p - cbegin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002405
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002406 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002407 void __copy_assign_alloc(const vector& __v)
2408 {__copy_assign_alloc(__v, integral_constant<bool,
2409 __storage_traits::propagate_on_container_copy_assignment::value>());}
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002410 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002411 void __copy_assign_alloc(const vector& __c, true_type)
2412 {
2413 if (__alloc() != __c.__alloc())
2414 deallocate();
2415 __alloc() = __c.__alloc();
2416 }
2417
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002418 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00002419 void __copy_assign_alloc(const vector&, false_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002420 {}
2421
2422 void __move_assign(vector& __c, false_type);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002423 void __move_assign(vector& __c, true_type)
2424 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002425 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002426 void __move_assign_alloc(vector& __c)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002427 _NOEXCEPT_(
2428 !__storage_traits::propagate_on_container_move_assignment::value ||
2429 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002430 {__move_assign_alloc(__c, integral_constant<bool,
2431 __storage_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002432 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31 +00002433 void __move_assign_alloc(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002434 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002435 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002436 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002437 }
2438
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002439 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00002440 void __move_assign_alloc(vector&, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002441 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002442 {}
2443
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002444 size_t __hash_code() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002445
2446 friend class __bit_reference<vector>;
2447 friend class __bit_const_reference<vector>;
2448 friend class __bit_iterator<vector, false>;
2449 friend class __bit_iterator<vector, true>;
Howard Hinnant4ae952a2012-08-17 17:10:18 +00002450 friend struct __bit_array<vector>;
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002451 friend struct _LIBCPP_TYPE_VIS_ONLY hash<vector>;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002452};
2453
2454template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002455inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002456void
2457vector<bool, _Allocator>::__invalidate_all_iterators()
2458{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002459}
2460
2461// Allocate space for __n objects
2462// throws length_error if __n > max_size()
2463// throws (probably bad_alloc) if memory run out
2464// Precondition: __begin_ == __end_ == __cap() == 0
2465// Precondition: __n > 0
2466// Postcondition: capacity() == __n
2467// Postcondition: size() == 0
2468template <class _Allocator>
2469void
2470vector<bool, _Allocator>::allocate(size_type __n)
2471{
2472 if (__n > max_size())
2473 this->__throw_length_error();
2474 __n = __external_cap_to_internal(__n);
2475 this->__begin_ = __storage_traits::allocate(this->__alloc(), __n);
2476 this->__size_ = 0;
2477 this->__cap() = __n;
2478}
2479
2480template <class _Allocator>
2481void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002482vector<bool, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002483{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002484 if (this->__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002485 {
2486 __storage_traits::deallocate(this->__alloc(), this->__begin_, __cap());
2487 __invalidate_all_iterators();
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002488 this->__begin_ = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002489 this->__size_ = this->__cap() = 0;
2490 }
2491}
2492
2493template <class _Allocator>
2494typename vector<bool, _Allocator>::size_type
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002495vector<bool, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002496{
2497 size_type __amax = __storage_traits::max_size(__alloc());
2498 size_type __nmax = numeric_limits<size_type>::max() / 2; // end() >= begin(), always
2499 if (__nmax / __bits_per_word <= __amax)
2500 return __nmax;
2501 return __internal_cap_to_external(__amax);
2502}
2503
2504// Precondition: __new_size > capacity()
2505template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002506inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002507typename vector<bool, _Allocator>::size_type
2508vector<bool, _Allocator>::__recommend(size_type __new_size) const
2509{
2510 const size_type __ms = max_size();
2511 if (__new_size > __ms)
2512 this->__throw_length_error();
2513 const size_type __cap = capacity();
2514 if (__cap >= __ms / 2)
2515 return __ms;
Howard Hinnant7f764502013-08-14 18:00:20 +00002516 return _VSTD::max(2*__cap, __align_it(__new_size));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002517}
2518
2519// Default constructs __n objects starting at __end_
2520// Precondition: __n > 0
2521// Precondition: size() + __n <= capacity()
2522// Postcondition: size() == size() + __n
2523template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002524inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002525void
2526vector<bool, _Allocator>::__construct_at_end(size_type __n, bool __x)
2527{
2528 size_type __old_size = this->__size_;
2529 this->__size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002530 _VSTD::fill_n(__make_iter(__old_size), __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002531}
2532
2533template <class _Allocator>
2534template <class _ForwardIterator>
2535typename enable_if
2536<
2537 __is_forward_iterator<_ForwardIterator>::value,
2538 void
2539>::type
2540vector<bool, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
2541{
2542 size_type __old_size = this->__size_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002543 this->__size_ += _VSTD::distance(__first, __last);
2544 _VSTD::copy(__first, __last, __make_iter(__old_size));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002545}
2546
2547template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002548inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002549vector<bool, _Allocator>::vector()
Marshall Clowc912c0c2015-06-04 02:05:41 +00002550 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002551 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002552 __size_(0),
2553 __cap_alloc_(0)
2554{
2555}
2556
2557template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002558inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002559vector<bool, _Allocator>::vector(const allocator_type& __a)
Marshall Clow127db912015-06-04 00:10:20 +00002560#if _LIBCPP_STD_VER <= 14
2561 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
2562#else
2563 _NOEXCEPT
2564#endif
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002565 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002566 __size_(0),
2567 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2568{
2569}
2570
2571template <class _Allocator>
2572vector<bool, _Allocator>::vector(size_type __n)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002573 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002574 __size_(0),
2575 __cap_alloc_(0)
2576{
2577 if (__n > 0)
2578 {
2579 allocate(__n);
2580 __construct_at_end(__n, false);
2581 }
2582}
2583
Marshall Clowa49a2c92013-09-14 00:47:59 +00002584#if _LIBCPP_STD_VER > 11
2585template <class _Allocator>
2586vector<bool, _Allocator>::vector(size_type __n, const allocator_type& __a)
2587 : __begin_(nullptr),
2588 __size_(0),
2589 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2590{
2591 if (__n > 0)
2592 {
2593 allocate(__n);
2594 __construct_at_end(__n, false);
2595 }
2596}
2597#endif
2598
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002599template <class _Allocator>
2600vector<bool, _Allocator>::vector(size_type __n, const value_type& __x)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002601 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002602 __size_(0),
2603 __cap_alloc_(0)
2604{
2605 if (__n > 0)
2606 {
2607 allocate(__n);
2608 __construct_at_end(__n, __x);
2609 }
2610}
2611
2612template <class _Allocator>
2613vector<bool, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002614 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002615 __size_(0),
2616 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2617{
2618 if (__n > 0)
2619 {
2620 allocate(__n);
2621 __construct_at_end(__n, __x);
2622 }
2623}
2624
2625template <class _Allocator>
2626template <class _InputIterator>
2627vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last,
2628 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2629 !__is_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002630 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002631 __size_(0),
2632 __cap_alloc_(0)
2633{
2634#ifndef _LIBCPP_NO_EXCEPTIONS
2635 try
2636 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002637#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002638 for (; __first != __last; ++__first)
2639 push_back(*__first);
2640#ifndef _LIBCPP_NO_EXCEPTIONS
2641 }
2642 catch (...)
2643 {
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002644 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002645 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2646 __invalidate_all_iterators();
2647 throw;
2648 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002649#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002650}
2651
2652template <class _Allocator>
2653template <class _InputIterator>
2654vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2655 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2656 !__is_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002657 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002658 __size_(0),
2659 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2660{
2661#ifndef _LIBCPP_NO_EXCEPTIONS
2662 try
2663 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002664#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002665 for (; __first != __last; ++__first)
2666 push_back(*__first);
2667#ifndef _LIBCPP_NO_EXCEPTIONS
2668 }
2669 catch (...)
2670 {
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002671 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002672 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2673 __invalidate_all_iterators();
2674 throw;
2675 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002676#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002677}
2678
2679template <class _Allocator>
2680template <class _ForwardIterator>
2681vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last,
2682 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002683 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002684 __size_(0),
2685 __cap_alloc_(0)
2686{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002687 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002688 if (__n > 0)
2689 {
2690 allocate(__n);
2691 __construct_at_end(__first, __last);
2692 }
2693}
2694
2695template <class _Allocator>
2696template <class _ForwardIterator>
2697vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2698 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002699 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002700 __size_(0),
2701 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2702{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002703 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002704 if (__n > 0)
2705 {
2706 allocate(__n);
2707 __construct_at_end(__first, __last);
2708 }
2709}
2710
Howard Hinnante3e32912011-08-12 21:56:02 +00002711#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2712
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002713template <class _Allocator>
2714vector<bool, _Allocator>::vector(initializer_list<value_type> __il)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002715 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002716 __size_(0),
2717 __cap_alloc_(0)
2718{
2719 size_type __n = static_cast<size_type>(__il.size());
2720 if (__n > 0)
2721 {
2722 allocate(__n);
2723 __construct_at_end(__il.begin(), __il.end());
2724 }
2725}
2726
2727template <class _Allocator>
2728vector<bool, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002729 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002730 __size_(0),
2731 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2732{
2733 size_type __n = static_cast<size_type>(__il.size());
2734 if (__n > 0)
2735 {
2736 allocate(__n);
2737 __construct_at_end(__il.begin(), __il.end());
2738 }
2739}
2740
Howard Hinnante3e32912011-08-12 21:56:02 +00002741#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2742
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002743template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002744vector<bool, _Allocator>::~vector()
2745{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002746 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002747 __storage_traits::deallocate(__alloc(), __begin_, __cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002748 __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002749}
2750
2751template <class _Allocator>
2752vector<bool, _Allocator>::vector(const vector& __v)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002753 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002754 __size_(0),
2755 __cap_alloc_(0, __storage_traits::select_on_container_copy_construction(__v.__alloc()))
2756{
2757 if (__v.size() > 0)
2758 {
2759 allocate(__v.size());
2760 __construct_at_end(__v.begin(), __v.end());
2761 }
2762}
2763
2764template <class _Allocator>
2765vector<bool, _Allocator>::vector(const vector& __v, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002766 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002767 __size_(0),
2768 __cap_alloc_(0, __a)
2769{
2770 if (__v.size() > 0)
2771 {
2772 allocate(__v.size());
2773 __construct_at_end(__v.begin(), __v.end());
2774 }
2775}
2776
2777template <class _Allocator>
2778vector<bool, _Allocator>&
2779vector<bool, _Allocator>::operator=(const vector& __v)
2780{
2781 if (this != &__v)
2782 {
2783 __copy_assign_alloc(__v);
2784 if (__v.__size_)
2785 {
2786 if (__v.__size_ > capacity())
2787 {
2788 deallocate();
2789 allocate(__v.__size_);
2790 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002791 _VSTD::copy(__v.__begin_, __v.__begin_ + __external_cap_to_internal(__v.__size_), __begin_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002792 }
2793 __size_ = __v.__size_;
2794 }
2795 return *this;
2796}
2797
Howard Hinnant73d21a42010-09-04 23:28:19 +00002798#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2799
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002800template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002801inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002802vector<bool, _Allocator>::vector(vector&& __v)
Marshall Clow119ed472015-07-14 14:46:32 +00002803#if _LIBCPP_STD_VER > 14
2804 _NOEXCEPT
2805#else
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002806 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clow119ed472015-07-14 14:46:32 +00002807#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002808 : __begin_(__v.__begin_),
2809 __size_(__v.__size_),
2810 __cap_alloc_(__v.__cap_alloc_)
2811{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002812 __v.__begin_ = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002813 __v.__size_ = 0;
2814 __v.__cap() = 0;
2815}
2816
2817template <class _Allocator>
2818vector<bool, _Allocator>::vector(vector&& __v, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002819 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002820 __size_(0),
2821 __cap_alloc_(0, __a)
2822{
2823 if (__a == allocator_type(__v.__alloc()))
2824 {
2825 this->__begin_ = __v.__begin_;
2826 this->__size_ = __v.__size_;
2827 this->__cap() = __v.__cap();
2828 __v.__begin_ = nullptr;
2829 __v.__cap() = __v.__size_ = 0;
2830 }
2831 else if (__v.size() > 0)
2832 {
2833 allocate(__v.size());
2834 __construct_at_end(__v.begin(), __v.end());
2835 }
2836}
2837
2838template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002839inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002840vector<bool, _Allocator>&
2841vector<bool, _Allocator>::operator=(vector&& __v)
Marshall Clowaf961ed2015-08-18 18:57:00 +00002842 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002843{
2844 __move_assign(__v, integral_constant<bool,
2845 __storage_traits::propagate_on_container_move_assignment::value>());
Argyrios Kyrtzidis1dc6f7a2012-10-13 02:03:45 +00002846 return *this;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002847}
2848
2849template <class _Allocator>
2850void
2851vector<bool, _Allocator>::__move_assign(vector& __c, false_type)
2852{
2853 if (__alloc() != __c.__alloc())
2854 assign(__c.begin(), __c.end());
2855 else
2856 __move_assign(__c, true_type());
2857}
2858
2859template <class _Allocator>
2860void
2861vector<bool, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002862 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002863{
2864 deallocate();
Marshall Clowdb5e54d2014-07-21 15:15:15 +00002865 __move_assign_alloc(__c);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002866 this->__begin_ = __c.__begin_;
2867 this->__size_ = __c.__size_;
2868 this->__cap() = __c.__cap();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002869 __c.__begin_ = nullptr;
2870 __c.__cap() = __c.__size_ = 0;
2871}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002872
2873#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002874
2875template <class _Allocator>
2876void
2877vector<bool, _Allocator>::assign(size_type __n, const value_type& __x)
2878{
2879 __size_ = 0;
2880 if (__n > 0)
2881 {
2882 size_type __c = capacity();
2883 if (__n <= __c)
2884 __size_ = __n;
2885 else
2886 {
2887 vector __v(__alloc());
2888 __v.reserve(__recommend(__n));
2889 __v.__size_ = __n;
2890 swap(__v);
2891 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002892 _VSTD::fill_n(begin(), __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002893 }
2894}
2895
2896template <class _Allocator>
2897template <class _InputIterator>
2898typename enable_if
2899<
2900 __is_input_iterator<_InputIterator>::value &&
2901 !__is_forward_iterator<_InputIterator>::value,
2902 void
2903>::type
2904vector<bool, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2905{
2906 clear();
2907 for (; __first != __last; ++__first)
2908 push_back(*__first);
2909}
2910
2911template <class _Allocator>
2912template <class _ForwardIterator>
2913typename enable_if
2914<
2915 __is_forward_iterator<_ForwardIterator>::value,
2916 void
2917>::type
2918vector<bool, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2919{
2920 clear();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002921 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002922 if (__n)
2923 {
2924 if (__n > capacity())
2925 {
2926 deallocate();
2927 allocate(__n);
2928 }
2929 __construct_at_end(__first, __last);
2930 }
2931}
2932
2933template <class _Allocator>
2934void
2935vector<bool, _Allocator>::reserve(size_type __n)
2936{
2937 if (__n > capacity())
2938 {
2939 vector __v(this->__alloc());
2940 __v.allocate(__n);
2941 __v.__construct_at_end(this->begin(), this->end());
2942 swap(__v);
2943 __invalidate_all_iterators();
2944 }
2945}
2946
2947template <class _Allocator>
2948void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002949vector<bool, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002950{
2951 if (__external_cap_to_internal(size()) > __cap())
2952 {
2953#ifndef _LIBCPP_NO_EXCEPTIONS
2954 try
2955 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002956#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002957 vector(*this, allocator_type(__alloc())).swap(*this);
2958#ifndef _LIBCPP_NO_EXCEPTIONS
2959 }
2960 catch (...)
2961 {
2962 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002963#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002964 }
2965}
2966
2967template <class _Allocator>
2968typename vector<bool, _Allocator>::reference
2969vector<bool, _Allocator>::at(size_type __n)
2970{
2971 if (__n >= size())
2972 this->__throw_out_of_range();
2973 return (*this)[__n];
2974}
2975
2976template <class _Allocator>
2977typename vector<bool, _Allocator>::const_reference
2978vector<bool, _Allocator>::at(size_type __n) const
2979{
2980 if (__n >= size())
2981 this->__throw_out_of_range();
2982 return (*this)[__n];
2983}
2984
2985template <class _Allocator>
2986void
2987vector<bool, _Allocator>::push_back(const value_type& __x)
2988{
2989 if (this->__size_ == this->capacity())
2990 reserve(__recommend(this->__size_ + 1));
2991 ++this->__size_;
2992 back() = __x;
2993}
2994
2995template <class _Allocator>
2996typename vector<bool, _Allocator>::iterator
2997vector<bool, _Allocator>::insert(const_iterator __position, const value_type& __x)
2998{
2999 iterator __r;
3000 if (size() < capacity())
3001 {
3002 const_iterator __old_end = end();
3003 ++__size_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003004 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003005 __r = __const_iterator_cast(__position);
3006 }
3007 else
3008 {
3009 vector __v(__alloc());
3010 __v.reserve(__recommend(__size_ + 1));
3011 __v.__size_ = __size_ + 1;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003012 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3013 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003014 swap(__v);
3015 }
3016 *__r = __x;
3017 return __r;
3018}
3019
3020template <class _Allocator>
3021typename vector<bool, _Allocator>::iterator
3022vector<bool, _Allocator>::insert(const_iterator __position, size_type __n, const value_type& __x)
3023{
3024 iterator __r;
3025 size_type __c = capacity();
3026 if (__n <= __c && size() <= __c - __n)
3027 {
3028 const_iterator __old_end = end();
3029 __size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003030 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003031 __r = __const_iterator_cast(__position);
3032 }
3033 else
3034 {
3035 vector __v(__alloc());
3036 __v.reserve(__recommend(__size_ + __n));
3037 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003038 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3039 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003040 swap(__v);
3041 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00003042 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003043 return __r;
3044}
3045
3046template <class _Allocator>
3047template <class _InputIterator>
3048typename enable_if
3049<
3050 __is_input_iterator <_InputIterator>::value &&
3051 !__is_forward_iterator<_InputIterator>::value,
3052 typename vector<bool, _Allocator>::iterator
3053>::type
3054vector<bool, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
3055{
3056 difference_type __off = __position - begin();
3057 iterator __p = __const_iterator_cast(__position);
3058 iterator __old_end = end();
3059 for (; size() != capacity() && __first != __last; ++__first)
3060 {
3061 ++this->__size_;
3062 back() = *__first;
3063 }
3064 vector __v(__alloc());
3065 if (__first != __last)
3066 {
3067#ifndef _LIBCPP_NO_EXCEPTIONS
3068 try
3069 {
Howard Hinnant324bb032010-08-22 00:02:43 +00003070#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003071 __v.assign(__first, __last);
3072 difference_type __old_size = static_cast<difference_type>(__old_end - begin());
3073 difference_type __old_p = __p - begin();
3074 reserve(__recommend(size() + __v.size()));
3075 __p = begin() + __old_p;
3076 __old_end = begin() + __old_size;
3077#ifndef _LIBCPP_NO_EXCEPTIONS
3078 }
3079 catch (...)
3080 {
3081 erase(__old_end, end());
3082 throw;
3083 }
Howard Hinnant324bb032010-08-22 00:02:43 +00003084#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003085 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00003086 __p = _VSTD::rotate(__p, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003087 insert(__p, __v.begin(), __v.end());
3088 return begin() + __off;
3089}
3090
3091template <class _Allocator>
3092template <class _ForwardIterator>
3093typename enable_if
3094<
3095 __is_forward_iterator<_ForwardIterator>::value,
3096 typename vector<bool, _Allocator>::iterator
3097>::type
3098vector<bool, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
3099{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003100 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003101 iterator __r;
3102 size_type __c = capacity();
3103 if (__n <= __c && size() <= __c - __n)
3104 {
3105 const_iterator __old_end = end();
3106 __size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003107 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003108 __r = __const_iterator_cast(__position);
3109 }
3110 else
3111 {
3112 vector __v(__alloc());
3113 __v.reserve(__recommend(__size_ + __n));
3114 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003115 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3116 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003117 swap(__v);
3118 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00003119 _VSTD::copy(__first, __last, __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003120 return __r;
3121}
3122
3123template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003124inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003125typename vector<bool, _Allocator>::iterator
3126vector<bool, _Allocator>::erase(const_iterator __position)
3127{
3128 iterator __r = __const_iterator_cast(__position);
Howard Hinnant0949eed2011-06-30 21:18:19 +00003129 _VSTD::copy(__position + 1, this->cend(), __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003130 --__size_;
3131 return __r;
3132}
3133
3134template <class _Allocator>
3135typename vector<bool, _Allocator>::iterator
3136vector<bool, _Allocator>::erase(const_iterator __first, const_iterator __last)
3137{
3138 iterator __r = __const_iterator_cast(__first);
3139 difference_type __d = __last - __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003140 _VSTD::copy(__last, this->cend(), __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003141 __size_ -= __d;
3142 return __r;
3143}
3144
3145template <class _Allocator>
3146void
3147vector<bool, _Allocator>::swap(vector& __x)
Marshall Clow7d914d12015-07-13 20:04:56 +00003148#if _LIBCPP_STD_VER >= 14
3149 _NOEXCEPT
3150#else
3151 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
3152 __is_nothrow_swappable<allocator_type>::value)
3153#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003154{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003155 _VSTD::swap(this->__begin_, __x.__begin_);
3156 _VSTD::swap(this->__size_, __x.__size_);
3157 _VSTD::swap(this->__cap(), __x.__cap());
Marshall Clow7d914d12015-07-13 20:04:56 +00003158 __swap_allocator(this->__alloc(), __x.__alloc(),
3159 integral_constant<bool, __alloc_traits::propagate_on_container_swap::value>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003160}
3161
Howard Hinnant324bb032010-08-22 00:02:43 +00003162template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003163void
3164vector<bool, _Allocator>::resize(size_type __sz, value_type __x)
3165{
3166 size_type __cs = size();
3167 if (__cs < __sz)
3168 {
3169 iterator __r;
3170 size_type __c = capacity();
3171 size_type __n = __sz - __cs;
3172 if (__n <= __c && __cs <= __c - __n)
3173 {
3174 __r = end();
3175 __size_ += __n;
3176 }
3177 else
3178 {
3179 vector __v(__alloc());
3180 __v.reserve(__recommend(__size_ + __n));
3181 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003182 __r = _VSTD::copy(cbegin(), cend(), __v.begin());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003183 swap(__v);
3184 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00003185 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003186 }
3187 else
3188 __size_ = __sz;
3189}
3190
Howard Hinnant324bb032010-08-22 00:02:43 +00003191template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003192void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003193vector<bool, _Allocator>::flip() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003194{
3195 // do middle whole words
3196 size_type __n = __size_;
3197 __storage_pointer __p = __begin_;
3198 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3199 *__p = ~*__p;
3200 // do last partial word
3201 if (__n > 0)
3202 {
3203 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3204 __storage_type __b = *__p & __m;
3205 *__p &= ~__m;
3206 *__p |= ~__b & __m;
3207 }
3208}
3209
Howard Hinnant324bb032010-08-22 00:02:43 +00003210template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003211bool
3212vector<bool, _Allocator>::__invariants() const
3213{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00003214 if (this->__begin_ == nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003215 {
3216 if (this->__size_ != 0 || this->__cap() != 0)
3217 return false;
3218 }
3219 else
3220 {
3221 if (this->__cap() == 0)
3222 return false;
3223 if (this->__size_ > this->capacity())
3224 return false;
3225 }
3226 return true;
3227}
3228
Howard Hinnant324bb032010-08-22 00:02:43 +00003229template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003230size_t
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003231vector<bool, _Allocator>::__hash_code() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003232{
3233 size_t __h = 0;
3234 // do middle whole words
3235 size_type __n = __size_;
3236 __storage_pointer __p = __begin_;
3237 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3238 __h ^= *__p;
3239 // do last partial word
3240 if (__n > 0)
3241 {
3242 const __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3243 __h ^= *__p & __m;
3244 }
3245 return __h;
3246}
3247
3248template <class _Allocator>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003249struct _LIBCPP_TYPE_VIS_ONLY hash<vector<bool, _Allocator> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003250 : public unary_function<vector<bool, _Allocator>, size_t>
3251{
Howard Hinnantee6ccd02010-09-23 18:58:28 +00003252 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003253 size_t operator()(const vector<bool, _Allocator>& __vec) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003254 {return __vec.__hash_code();}
3255};
3256
3257template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003258inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003259bool
3260operator==(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3261{
3262 const typename vector<_Tp, _Allocator>::size_type __sz = __x.size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00003263 return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003264}
3265
3266template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003267inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003268bool
3269operator!=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3270{
3271 return !(__x == __y);
3272}
3273
3274template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003275inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003276bool
3277operator< (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3278{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003279 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003280}
3281
3282template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003283inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003284bool
3285operator> (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3286{
3287 return __y < __x;
3288}
3289
3290template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003291inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003292bool
3293operator>=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3294{
3295 return !(__x < __y);
3296}
3297
3298template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003299inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003300bool
3301operator<=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3302{
3303 return !(__y < __x);
3304}
3305
3306template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003307inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003308void
3309swap(vector<_Tp, _Allocator>& __x, vector<_Tp, _Allocator>& __y)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003310 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003311{
3312 __x.swap(__y);
3313}
3314
3315_LIBCPP_END_NAMESPACE_STD
3316
3317#endif // _LIBCPP_VECTOR